diff options
| author | Adam Malczewski <[email protected]> | 2026-06-22 02:53:20 +0900 |
|---|---|---|
| committer | Adam Malczewski <[email protected]> | 2026-06-22 02:53:20 +0900 |
| commit | 20db60b0705ab65b6ade67ff614d347e13dc9803 (patch) | |
| tree | 02361b6b94d6a397355b42e7208b1c3ba39fb692 /packages/cli | |
| parent | d233842752d32659bba6f0e47b536e50d03145aa (diff) | |
| download | dispatch-20db60b0705ab65b6ade67ff614d347e13dc9803.tar.gz dispatch-20db60b0705ab65b6ade67ff614d347e13dc9803.zip | |
feat: stop generation mid-turn (POST /conversations/:id/stop)
Add stopTurn to the orchestrator: aborts the in-flight turn's
AbortController without changing conversation status. The turn
seals normally (finishReason: 'aborted'), partial messages are
persisted, and the conversation transitions active → idle via the
normal settle path.
Distinct from closeConversation which marks the conversation closed.
- POST /conversations/:id/stop endpoint
- dispatch stop <id> CLI command
- FE handoff: frontend-stop-generation-handoff.md
Diffstat (limited to 'packages/cli')
| -rw-r--r-- | packages/cli/src/args.ts | 23 | ||||
| -rw-r--r-- | packages/cli/src/http.ts | 20 | ||||
| -rw-r--r-- | packages/cli/src/main.ts | 22 |
3 files changed, 65 insertions, 0 deletions
diff --git a/packages/cli/src/args.ts b/packages/cli/src/args.ts index ecf6e2e..ac5dd4a 100644 --- a/packages/cli/src/args.ts +++ b/packages/cli/src/args.ts @@ -47,6 +47,7 @@ export type ParsedCommand = readonly cwd?: string; readonly reasoningEffort?: ReasoningEffort; } + | { readonly kind: "stop"; readonly server: string; readonly conversationId: string } | { readonly kind: "help" } | { readonly kind: "error"; readonly message: string }; @@ -131,6 +132,28 @@ export function parseArgs(argv: readonly string[], opts: ParseOpts): ParsedComma return { kind: "compact", server, conversationId }; } + if (first === "stop") { + let server = opts.defaultServer; + let conversationId: string | undefined; + for (let i = 1; i < argv.length; i++) { + const arg = argv[i] as string; + if (arg === "--server") { + if (i + 1 >= argv.length) return { kind: "error", message: "--server requires a value" }; + server = argv[++i] as string; + } else if (arg.startsWith("--")) { + return { kind: "error", message: `Unknown flag: ${arg}` }; + } else if (conversationId !== undefined) { + return { kind: "error", message: `Unexpected argument for 'stop': ${arg}` }; + } else { + conversationId = arg; + } + } + if (conversationId === undefined) { + return { kind: "error", message: "'stop' requires a conversation id" }; + } + return { kind: "stop", server, conversationId }; + } + if (first === "read") { let server = opts.defaultServer; let conversationId: string | undefined; diff --git a/packages/cli/src/http.ts b/packages/cli/src/http.ts index 585c678..42fcfec 100644 --- a/packages/cli/src/http.ts +++ b/packages/cli/src/http.ts @@ -204,6 +204,26 @@ export async function compactConversation( return (await res.json()) as CompactResponse; } +interface StopTurnOpts { + readonly server: string; + readonly conversationId: string; +} + +export async function stopTurn( + deps: FetchDeps, + opts: StopTurnOpts, +): Promise<{ conversationId: string; abortedTurn: boolean }> { + const url = `${opts.server}/conversations/${encodeURIComponent(opts.conversationId)}/stop`; + const res = await deps.fetchImpl(url, { method: "POST" }); + + if (!res.ok) { + const body = await res.text(); + throw new Error(`POST /conversations/:id/stop failed with status ${res.status}: ${body}`); + } + + return (await res.json()) as { conversationId: string; abortedTurn: boolean }; +} + /** * The outcome of short-ID resolution: either the full conversation id to use, * or a human-readable error describing why resolution failed. diff --git a/packages/cli/src/main.ts b/packages/cli/src/main.ts index 4e07da9..5935bab 100644 --- a/packages/cli/src/main.ts +++ b/packages/cli/src/main.ts @@ -16,6 +16,7 @@ import { fetchModels, openConversation, resolveConversationId, + stopTurn, streamChat, } from "./http.js"; import { buildChatRequest, composeMessage } from "./message.js"; @@ -24,6 +25,7 @@ import { extractLastText, formatConversationList, renderEvent } from "./render.j const USAGE = `Usage: dispatch models [--server <url>] dispatch list [<prefix>] [--status <active|idle|closed>] [--all] [--server <url>] + dispatch stop <conversationId> [--server <url>] dispatch compact <conversationId> [--server <url>] dispatch read <conversationId> [--server <url>] dispatch open <conversationId> [--server <url>] @@ -99,6 +101,26 @@ async function main(): Promise<void> { ); break; } + case "stop": { + const resolved = await resolveConversationId( + { fetchImpl: globalThis.fetch }, + { server: parsed.server, shortId: parsed.conversationId }, + ); + if (typeof resolved !== "string") { + process.stderr.write(`${resolved.error}\n`); + process.exit(1); + } + const result = await stopTurn( + { fetchImpl: globalThis.fetch }, + { server: parsed.server, conversationId: resolved }, + ); + process.stdout.write( + result.abortedTurn + ? `Stopped generation for ${resolved}\n` + : `No active generation for ${resolved}\n`, + ); + break; + } case "open": { const resolved = await resolveConversationId( { fetchImpl: globalThis.fetch }, |
