diff options
| author | Adam Malczewski <[email protected]> | 2026-05-29 18:45:45 +0900 |
|---|---|---|
| committer | Adam Malczewski <[email protected]> | 2026-05-29 18:45:45 +0900 |
| commit | dcbc51e22dcd3d7b5a01d6c3b7b0285efa49bca4 (patch) | |
| tree | 72e1a635662ed20deaee1f0b01408a5954445b26 /packages/api | |
| parent | 5b3e1ac64681e233f35e1b4d2230d9988667c37e (diff) | |
| download | dispatch-dcbc51e22dcd3d7b5a01d6c3b7b0285efa49bca4.tar.gz dispatch-dcbc51e22dcd3d7b5a01d6c3b7b0285efa49bca4.zip | |
feat: stop generation button with abort signal plumbing
- Add POST /chat/stop endpoint on API
- Thread abortSignal from agent-manager through Agent.run() to streamText
- Thread abortSignal option through the Agent.run() signature
- Emit status:idle on stopTab() so frontend WS gets the update
- Add stopGeneration() store method on frontend tabStore
- Add stop button in ChatInput (btn-sm lg:btn-xs for mobile tap target)
- Add tests for /chat/stop endpoint
- Refactor processMessage to pass abortSignal to agent.run
Diffstat (limited to 'packages/api')
| -rw-r--r-- | packages/api/src/agent-manager.ts | 9 | ||||
| -rw-r--r-- | packages/api/src/app.ts | 9 | ||||
| -rw-r--r-- | packages/api/tests/routes.test.ts | 22 |
3 files changed, 36 insertions, 4 deletions
diff --git a/packages/api/src/agent-manager.ts b/packages/api/src/agent-manager.ts index 69c071d..c09a607 100644 --- a/packages/api/src/agent-manager.ts +++ b/packages/api/src/agent-manager.ts @@ -885,6 +885,7 @@ export class AgentManager { } tabAgent.abortController?.abort(); tabAgent.status = "idle"; + this.emit({ type: "status", status: "idle" }, tabId); tabAgent.agent = null; // Resolve any pending completion promise so retrieve doesn't hang tabAgent.completionResolve?.({ status: "error", error: "Agent was stopped." }); @@ -1193,10 +1194,10 @@ export class AgentManager { // Best-effort — if this fails, appendMessage will throw and we'll catch it below } - for await (const event of agent.run( - message, - reasoningEffort ? { reasoningEffort } : undefined, - )) { + for await (const event of agent.run(message, { + ...(reasoningEffort ? { reasoningEffort } : {}), + abortSignal: tabAgent.abortController?.signal, + })) { // Stop processing if the tab was aborted (closed/stopped). // stopTab() already injected a `cancelled` system chunk into // `chunks` before flipping the abort flag, so we just need diff --git a/packages/api/src/app.ts b/packages/api/src/app.ts index ba5dabd..73d3de5 100644 --- a/packages/api/src/app.ts +++ b/packages/api/src/app.ts @@ -94,6 +94,15 @@ app.post("/chat/cancel", async (c) => { return c.json({ success: cancelled }); }); +app.post("/chat/stop", async (c) => { + const body = await c.req.json(); + if (typeof body.tabId !== "string") { + return c.json({ error: "tabId is required" }, 400); + } + agentManager.stopTab(body.tabId); + return c.json({ success: true }); +}); + app.route("/skills", skillsRoutes); app.route("/models", modelsRoutes); app.route("/tabs", tabsRoutes); diff --git a/packages/api/tests/routes.test.ts b/packages/api/tests/routes.test.ts index 49bae49..eba2226 100644 --- a/packages/api/tests/routes.test.ts +++ b/packages/api/tests/routes.test.ts @@ -318,3 +318,25 @@ describe("POST /chat", () => { expect(typeof body.messageId).toBe("string"); }); }); + +describe("POST /chat/stop", () => { + it("returns 200 with success: true for valid tabId", async () => { + const res = await app.request("/chat/stop", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ tabId: "tab-stop-1" }), + }); + expect(res.status).toBe(200); + const body = await res.json(); + expect(body).toEqual({ success: true }); + }); + + it("returns 400 when tabId is missing", async () => { + const res = await app.request("/chat/stop", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({}), + }); + expect(res.status).toBe(400); + }); +}); |
