Files
Davide Silvestri 74d6625a49 feat(assistant): run Kuma replies in the background (#6383)
* feat(assistant): stream Kuma background replies over HTTP

Behind assistant_background_runs, commit and enqueue the message then follow
Redis on the existing POST/200 cumulative JSON response. Resume the same run
without sending the prompt again. Keep refresh, multi-tab discovery, onboarding
and durable outcome recovery in the Kuma adapter and UI integration.

Part of #6346

* Remove unused Kuma chat run response header

* Register Kuma with the shared async streaming router

* Always execute Kuma messages in background runs

* Use chat run constant enums in Kuma

Kuma's model_unavailable error code moves to its own run constants,
because the engine no longer defines run type specific codes.

* Move the tool executor into Kuma

Kuma is its only user, and it is specific to pydantic-ai agents, so it does
not belong to the shared chat run engine.

* Return nothing from Kuma's interruption warning hook

The engine now prunes a run whatever the hook does, so it no longer reports
whether the warning was kept.

* Stop patching the removed ChatRunBuffer.finish

* Fix and simplify Kuma's background run adapter

- Save "nothing changed" when the model was unavailable or the prompt
  never reached it.
- Keep the progress line after a reconnect and build the compatibility
  records from their models, with the right text for runs that did nothing.
- Delete the in-request path, its cache-key Stop and the event bus.
- Map a second message to 409 directly and return one run shape in the
  chat API.
- Fix docstrings, help texts and the POST schema; drop overlapping tests and
  share run test helpers.

* Fix and simplify Kuma's run follower in the browser

- Keep local-only messages on refresh and show a queued run as queued.
- Settle a request aborted before it is sent and never show raw error pages.
- Fall back to the API for a new view whose broadcast is late.
- Stop a run through the engine.
- Delete the reader for the previous backend, the old cancel call and the
  UI location history.
- Share run test helpers and drop overlapping tests.

* Tighten Kuma's e2e tests, CI setup and docs

- Prove a live resume after a reload and drop a test that proved nothing new.
- Keep Kuma's panel closed in every browser context through the storage state.
- Drop the unused OpenAI key from the e2e setup.
- Rewrite the background replies section of the AI assistant docs.

* Handle Kuma runs whose sender lost access, and other review fixes

- A queued run whose sender can no longer use Kuma ends as access_lost
  without calling the model.
- A chat keeps asking runs for a title while its title is provisional.
- Test that a run captures and saves its onboarding target.
- Pass Phoenix settings to the dev export worker; pin CI's Kuma stub to
  Node 24.22.1.

* Fix Kuma's browser edge cases found in the re-review

- A send whose stream is refused after its run started re-reads the
  chat and counts as accepted.
- A chat read that ends after another chat was chosen is dropped, and
  only a 4xx forgets the tab's stored chat.
- An unsaved outcome stays shown while its run is the chat's latest.
- The status line shows that Kuma is answering.
- Empty stopped or failed answers show only their outcome.
- Logout clears Kuma's chats, messages and pending navigation.

* feat(assistant): follow Kuma replies through shared chat runs

* refactor(assistant): remove obsolete stream-limit recovery

* fix(assistant): simplify Kuma cutover and preserve legacy Stop

* fix(assistant): preserve existing Kuma API contracts

* refactor(assistant): match stream event kinds

* refactor(assistant): simplify execution and navigation

* fix(assistant): show that Kuma is cancelling after Stop

* test(assistant): cover the stale refresh guard and trashed workspaces

* refactor(assistant): close the run follower and drop the unused baseline

* refactor(chat-runs): share the run-in-progress error code

* docs(assistant): document the run limit and the deprecated chat status

* fix(assistant): give a duplicated tab its own Kuma tab id
2026-11-18 11:21:31 +01:00

63 lines
1.7 KiB
JavaScript

export const CHAT_RUNS_PAGE = 'chat_runs'
export const CHAT_RUN_CHANGED_MESSAGE = 'chat_run_changed'
export const RUN_CREATED = 'run.created'
export const RUN_STARTED = 'run.started'
export const TEXT_DELTA = 'text.delta'
export const PROGRESS = 'progress'
export const STEP = 'step'
export const RUN_CANCEL_REQUESTED = 'run.cancel_requested'
export const RUN_FINISHED = 'run.finished'
export const RUN_BASELINE = 'run.baseline'
export const RUN_HEARTBEAT = 'run.heartbeat'
export const STEP_STATUS = Object.freeze({
RUNNING: 'running',
WAITING: 'waiting',
DONE: 'done',
FAILED: 'failed',
REJECTED: 'rejected',
STOPPED: 'stopped',
SKIPPED: 'skipped',
})
export const STEP_ERROR_CODE = Object.freeze({
NOT_FOUND: 'not_found',
PERMISSION_DENIED: 'permission_denied',
INVALID_INPUT: 'invalid_input',
INVALID_FORMULA: 'invalid_formula',
CONFLICT: 'conflict',
FAILED: 'failed',
NOT_RUN: 'not_run',
})
export const RUN_STATUS = {
QUEUED: 'queued',
RUNNING: 'running',
COMPLETED: 'completed',
CANCELLED: 'cancelled',
FAILED: 'failed',
}
// Wire values of the backend ChatRunErrorCode enum.
export const RUN_ERROR_CODE = Object.freeze({
FAILED: 'failed',
TIMEOUT: 'timeout',
INTERRUPTED: 'interrupted',
NOT_STARTED: 'not_started',
PERSIST_FAILED: 'persist_failed',
})
export const ERROR_CHAT_RUN_IN_PROGRESS = 'ERROR_CHAT_RUN_IN_PROGRESS'
export const STATUS_CHECK_MS = 5000
// A quiet stream sends a heartbeat about every 5 s, so this much silence means it stalled.
export const STREAM_IDLE_MS = 15000
export const MAX_RECONNECT_ATTEMPTS = 3
export const MAX_FINISHED_FOLLOWERS = 20
export const isTerminalStatus = (status) =>
[RUN_STATUS.COMPLETED, RUN_STATUS.CANCELLED, RUN_STATUS.FAILED].includes(
status
)