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 @@