From fbaff900a09b2543c291fb0545ecfaf361bfaafc Mon Sep 17 00:00:00 2001 From: nullptr-error <104547331+nullptr-error@users.noreply.github.com> Date: Wed, 9 Sep 2026 16:43:33 +0800 Subject: [PATCH] feat(chat): append messages to a running agent turn (#3123) * feat(stream): persist live-run markers and per-run steer queues Mid-run sends need a cross-replica pointer to the generating assistant and a control-plane list that never hits the SSE stream. * feat(agent): inject queued user messages at ReAct round boundaries The engine drains the steer sink after each round, persists accepted rows, and delays Done until after a loop-end inject so the turn can continue. * feat(session): expose /steer and hand leftover messages to a follow-up turn Owner-scoped steer routes queue or inject into the live run, single-flight AgentQA via the live-run marker, and history replay keeps multiple user rows inside one turn. * feat(frontend): queue mid-run messages while an agent turn streams Stop and Send stay visible together, the overlay follows server delivery, and history splits the transcript around injected user bubbles. * fix(frontend): type pending and client_id on SteerQueueItem vue-tsc failed because the overlay sets those fields while POST /steer is in flight. * fix(session): harden steer persist, handoff, and new_run Keep SetLiveRun ahead of SSE and roll back orphan turns, make inject persist idempotent, append-then-delete on rebind, cap overrun rounds, and stop aborting a live stream when /steer returns new_run. * fix(session): satisfy pre-push lint on steer paths Wrap long lines, check discarded Error/Emit results, and keep context as the first argument so the fork push hook can pass. --------- Co-authored-by: hailongzhao Co-authored-by: wizardchen --- frontend/src/api/chat/steer.ts | 55 + .../Input-field.agent-switch.test.mjs | 71 ++ frontend/src/components/Input-field.vue | 184 ++- .../composables/useChatStreamHandler.test.mjs | 99 ++ .../src/composables/useChatStreamHandler.ts | 125 +- frontend/src/i18n/locales/en-US.ts | 11 + frontend/src/i18n/locales/ja-JP.ts | 11 + frontend/src/i18n/locales/ko-KR.ts | 11 + frontend/src/i18n/locales/ru-RU.ts | 11 + frontend/src/i18n/locales/zh-CN.ts | 11 + frontend/src/utils/steerStreamFork.test.mjs | 221 ++++ frontend/src/utils/steerStreamFork.ts | 221 ++++ .../chat/components/AgentStreamDisplay.vue | 17 +- .../answerToolbarCompletion.test.mjs | 2 +- frontend/src/views/chat/index.vue | 327 ++++- .../src/views/chat/steerFollowUp.test.mjs | 182 +++ internal/agent/engine.go | 65 +- internal/agent/observe.go | 18 +- internal/agent/steer.go | 92 ++ internal/agent/steer_test.go | 488 ++++++++ internal/application/service/agent_history.go | 177 ++- .../application/service/agent_history_test.go | 91 ++ .../application/service/session_agent_qa.go | 11 +- .../service/tenant_skill_install_test.go | 3 +- .../service/tenant_skill_transcript_test.go | 40 + internal/event/event.go | 5 + internal/event/event_data.go | 11 + internal/handler/sandbox_skill_test.go | 40 + .../handler/session/agent_stream_handler.go | 28 + .../continue_stream_resource_urls_test.go | 40 + internal/handler/session/qa.go | 292 ++++- internal/handler/session/steer.go | 1059 +++++++++++++++++ .../handler/session/steer_delivery_test.go | 350 ++++++ .../handler/session/steer_live_run_test.go | 462 +++++++ internal/handler/session/steer_wiring_test.go | 198 +++ internal/router/routes_chat.go | 8 + internal/stream/factory.go | 5 +- internal/stream/memory_manager.go | 181 ++- internal/stream/redis_live_run_test.go | 152 +++ internal/stream/redis_manager.go | 351 +++++- internal/types/chat.go | 15 + internal/types/interfaces/agent.go | 6 + internal/types/interfaces/stream_manager.go | 41 + internal/types/message.go | 54 + internal/types/qa_request.go | 36 + 45 files changed, 5724 insertions(+), 154 deletions(-) create mode 100644 frontend/src/api/chat/steer.ts create mode 100644 frontend/src/utils/steerStreamFork.test.mjs create mode 100644 frontend/src/utils/steerStreamFork.ts create mode 100644 frontend/src/views/chat/steerFollowUp.test.mjs create mode 100644 internal/agent/steer.go create mode 100644 internal/agent/steer_test.go create mode 100644 internal/handler/session/steer.go create mode 100644 internal/handler/session/steer_delivery_test.go create mode 100644 internal/handler/session/steer_live_run_test.go create mode 100644 internal/handler/session/steer_wiring_test.go create mode 100644 internal/stream/redis_live_run_test.go diff --git a/frontend/src/api/chat/steer.ts b/frontend/src/api/chat/steer.ts new file mode 100644 index 00000000..443bc9cf --- /dev/null +++ b/frontend/src/api/chat/steer.ts @@ -0,0 +1,55 @@ +import { del, get, post } from "../../utils/request"; + +export type SteerDelivery = 'inject' | 'after' + +export type SteerQueueItem = { + steer_id: string + content: string + delivery: SteerDelivery + mentioned_items?: unknown[] + promoting?: boolean + awaitingIdleSend?: boolean + // True while POST /steer is in flight. The overlay uses a client-side + // id until the server replies with the durable one; promote/remove are + // disabled in that window so they cannot hit /steer/{client_id}. + pending?: boolean + client_id?: string +} + +/** + * Append a message to a running agent turn. + * Returns { success, status: 'queued' | 'new_run', steer_id?, delivery?, assistant_message_id? }. + * - delivery 'after' (default): waits until the current run exits, then starts a follow-up turn. + * - delivery 'inject': the running engine injects it at the next round boundary. + * - 'new_run': no run is live; the caller should fall back to a normal send. + * Lookup failures (503) reject rather than returning new_run — treating them + * as idle would start a second turn on top of the one still generating. + */ +export async function steerSession( + session_id: string, + query: string, + mentionedItems: any[] = [], + delivery: SteerDelivery = 'after', +) { + return post(`/api/v1/sessions/${session_id}/steer`, { + query, + mentioned_items: mentionedItems, + channel: 'web', + delivery, + }); +} + +/** Flip a queued after-message to inject so the running turn reads it next. */ +export async function promoteSteerSession(session_id: string, steer_id: string) { + return post(`/api/v1/sessions/${session_id}/steer/${steer_id}/inject`, {}); +} + +/** Pending overlay items for the live run. Empty when nothing is generating. */ +export async function listSteerSession(session_id: string) { + return get(`/api/v1/sessions/${session_id}/steer`); +} + +/** Drop a queued overlay item so it is neither injected nor sent as a follow-up. */ +export async function removeSteerSession(session_id: string, steer_id: string) { + return del(`/api/v1/sessions/${session_id}/steer/${steer_id}`); +} diff --git a/frontend/src/components/Input-field.agent-switch.test.mjs b/frontend/src/components/Input-field.agent-switch.test.mjs index 59f1df8c..67cc2f10 100644 --- a/frontend/src/components/Input-field.agent-switch.test.mjs +++ b/frontend/src/components/Input-field.agent-switch.test.mjs @@ -35,3 +35,74 @@ test('shared-agent web search button waits for source readiness metadata', () => assert.match(showWebSearchButton, /isWebSearchReadinessKnown/) assert.match(showWebSearchButton, /selectedSharedAgent\.value\?\.web_search_ready/) }) + +test('replying keeps stop visible; typed text also shows send', () => { + const controlsStart = inputField.indexOf('class="control-right"') + const controls = inputField.slice(controlsStart, controlsStart + 1200) + assert.notEqual(controlsStart, -1) + assert.doesNotMatch(inputField, /steer-delivery-toggle/) + assert.doesNotMatch(inputField, /setSteerDelivery/) + assert.match(inputField, /emit\('steer-msg', val\.trim\(\), steerMentions, 'after'\)/) + assert.match(controls, /v-if="isReplying"/) + assert.match(controls, /handleStop/) + assert.match(controls, /v-if="!isReplying \|\| query\.trim\(\)"[\s\S]*createSession\(query\)[\s\S]*send-btn/) + assert.doesNotMatch(controls, /v-if="isReplying && \(!canSteer \|\| !query\.trim\(\)\)"/) +}) + +// Only agent turns have a round boundary to take a mid-run message at, and a +// teardown that hands leftovers to a follow-up run. Queueing on a quick-answer +// turn would park the message until it expired, so the composer keeps its old +// stop-only behaviour there. +test('mid-run send is limited to agent sessions', () => { + const chatView = readFileSync( + new URL('../views/chat/index.vue', import.meta.url), 'utf8') + assert.match(chatView, /:canSteer="isAgentStreamSession\(\)"/) + + const fnStart = inputField.indexOf('const createSession = async') + const fn = inputField.slice(fnStart, fnStart + 1600) + assert.notEqual(fnStart, -1) + assert.match(fn, /if \(!props\.canSteer\)/) + // The guard must come before anything queues the message. + assert.ok( + fn.indexOf('props.canSteer') < fn.indexOf("emit('steer-msg'"), + 'canSteer must be checked before emitting steer-msg', + ) +}) + +test('queued follow-ups sit in one block with send-now and remove', () => { + assert.match(inputField, /class="steer-queue"/) + assert.match(inputField, /queuedSteers/) + assert.match(inputField, /emit\('promote-steer', item\.steer_id\)/) + assert.match(inputField, /emit\('remove-steer', item\.steer_id\)/) + assert.match(inputField, /class="steer-queue-remove"/) + assert.match(inputField, /\$t\('input\.steerQueueSendNow'\)/) + assert.match(inputField, /:disabled="item\.promoting \|\| item\.pending"/) + assert.match(inputField, /\.steer-queue-item \+ \.steer-queue-item/) +}) + +test('mid-run send refuses attachments that have already uploaded', () => { + const fnStart = inputField.indexOf('const createSession = async') + const fn = inputField.slice(fnStart, fnStart + 2200) + assert.notEqual(fnStart, -1) + assert.match(fn, /steerHasAttachments/) + assert.match(fn, /uploadedImages\.value\.length/) +}) + +test('stop waits for the API before confirming discard', () => { + const fnStart = inputField.indexOf('const handleStop = async') + const fnEnd = inputField.indexOf('onBeforeRouteUpdate', fnStart) + assert.notEqual(fnStart, -1) + assert.notEqual(fnEnd, -1) + const fn = inputField.slice(fnStart, fnEnd) + assert.match(fn, /emit\('stop-generation'\)/) + assert.match(fn, /emit\('stop-confirmed'\)/) + assert.match(fn, /emit\('stop-failed'\)/) + assert.ok( + fn.indexOf("emit('stop-generation')") < fn.indexOf('await stopSession'), + 'UI stop must fire before the stop API', + ) + assert.ok( + fn.indexOf('await stopSession') < fn.indexOf("emit('stop-confirmed')"), + 'queue discard must wait for stop API success', + ) +}) diff --git a/frontend/src/components/Input-field.vue b/frontend/src/components/Input-field.vue index 1997423b..2d476af8 100644 --- a/frontend/src/components/Input-field.vue +++ b/frontend/src/components/Input-field.vue @@ -1,5 +1,5 @@