♻️ Quality: the Node clients of demo.acp.sdk without smells (3 → 0)
- eventOf as a lookup (text kinds) in nodejs-sdk, and in nodejs, whose #update now goes through it instead of its own switch - lib/events.js in both: the session updates, permission options and tool calls as events (eventOf/pickOption still exported by minime.js) Checked: nodejs-sdk's node --test (16 pass), and the complete, stream, permission and cancel examples of both clients, old and new code, against mm with a scripted fake model: identical output. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
0d4a390 parent: 2e11277 added
demo.acp.sdk/nodejs-sdk/lib/events.js +55 -0 | new file mode 100644 | ||
| @@ -0,0 +1,55 @@ | ||
| 1 | +// The ACP messages as minime's events: session updates, permission options, | |
| 2 | +// tool calls. Shared by lib/minime.js; eventOf and pickOption are exported | |
| 3 | +// from there too. | |
| 4 | + | |
| 5 | +/** | |
| 6 | + * Reduces one ACP `session/update` to an Event; undefined for the updates that | |
| 7 | + * are editor detail (plan, usage_update, available_commands_update…) and for | |
| 8 | + * tool updates that are not an outcome (in_progress). | |
| 9 | + * | |
| 10 | + * @param {any} update | |
| 11 | + * @returns {Event|undefined} | |
| 12 | + */ | |
| 13 | +export function eventOf(update) { | |
| 14 | + const kind = update.sessionUpdate; | |
| 15 | + if (Object.hasOwn(TEXT_KINDS, kind)) { | |
| 16 | + return update.content?.type === "text" ? { kind: TEXT_KINDS[kind], text: update.content.text } : undefined; | |
| 17 | + } | |
| 18 | + if (kind === "tool_call") return { kind: "tool_call", ...toolCall(update) }; | |
| 19 | + if (kind === "tool_call_update" && (update.status === "completed" || update.status === "failed")) { | |
| 20 | + return { kind: "tool_result", id: update.toolCallId, status: update.status, output: outputOf(update) }; | |
| 21 | + } | |
| 22 | + return undefined; | |
| 23 | +} | |
| 24 | + | |
| 25 | +// The text updates, and the kind of event each one is. | |
| 26 | +const TEXT_KINDS = { agent_message_chunk: "text", agent_thought_chunk: "thinking" }; | |
| 27 | + | |
| 28 | +/** | |
| 29 | + * true / false → the "once" options (falling back on the "always" ones); a | |
| 30 | + * string → that optionId when offered, else a rejection. | |
| 31 | + * | |
| 32 | + * @param {Array<{ optionId: string, kind: string }>} options | |
| 33 | + * @param {boolean|string} choice | |
| 34 | + * @returns {string|undefined} | |
| 35 | + * | |
| 36 | + * @example | |
| 37 | + * pickOption(params.options, "allow_always"); | |
| 38 | + */ | |
| 39 | +export function pickOption(options, choice) { | |
| 40 | + const byKind = (kind) => options.find((o) => o.kind === kind)?.optionId; | |
| 41 | + if (typeof choice === "string" && options.some((o) => o.optionId === choice)) return choice; | |
| 42 | + if (choice === true) return byKind("allow_once") ?? byKind("allow_always"); | |
| 43 | + return byKind("reject_once") ?? byKind("reject_always"); | |
| 44 | +} | |
| 45 | + | |
| 46 | +export function toolCall(tc) { | |
| 47 | + return { id: tc.toolCallId, title: tc.title, toolKind: tc.kind ?? undefined, input: tc.rawInput }; | |
| 48 | +} | |
| 49 | + | |
| 50 | +// What a tool produced: mm puts it in rawOutput.output; other agents may only | |
| 51 | +// send it as text content. | |
| 52 | +function outputOf(update) { | |
| 53 | + if (typeof update.rawOutput?.output === "string") return update.rawOutput.output; | |
| 54 | + return (update.content ?? []).map((block) => (block.content?.type === "text" ? block.content.text : "")).join(""); | |
| 55 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,55 @@ | |||
| 1 | +// The ACP messages as minime's events: session updates, permission options, | ||
| 2 | +// tool calls. Shared by lib/minime.js; eventOf and pickOption are exported | ||
| 3 | +// from there too. | ||
| 4 | + | ||
| 5 | +/** | ||
| 6 | + * Reduces one ACP `session/update` to an Event; undefined for the updates that | ||
| 7 | + * are editor detail (plan, usage_update, available_commands_update…) and for | ||
| 8 | + * tool updates that are not an outcome (in_progress). | ||
| 9 | + * | ||
| 10 | + * @param {any} update | ||
| 11 | + * @returns {Event|undefined} | ||
| 12 | + */ | ||
| 13 | +export function eventOf(update) { | ||
| 14 | + const kind = update.sessionUpdate; | ||
| 15 | + if (Object.hasOwn(TEXT_KINDS, kind)) { | ||
| 16 | + return update.content?.type === "text" ? { kind: TEXT_KINDS[kind], text: update.content.text } : undefined; | ||
| 17 | + } | ||
| 18 | + if (kind === "tool_call") return { kind: "tool_call", ...toolCall(update) }; | ||
| 19 | + if (kind === "tool_call_update" && (update.status === "completed" || update.status === "failed")) { | ||
| 20 | + return { kind: "tool_result", id: update.toolCallId, status: update.status, output: outputOf(update) }; | ||
| 21 | + } | ||
| 22 | + return undefined; | ||
| 23 | +} | ||
| 24 | + | ||
| 25 | +// The text updates, and the kind of event each one is. | ||
| 26 | +const TEXT_KINDS = { agent_message_chunk: "text", agent_thought_chunk: "thinking" }; | ||
| 27 | + | ||
| 28 | +/** | ||
| 29 | + * true / false → the "once" options (falling back on the "always" ones); a | ||
| 30 | + * string → that optionId when offered, else a rejection. | ||
| 31 | + * | ||
| 32 | + * @param {Array<{ optionId: string, kind: string }>} options | ||
| 33 | + * @param {boolean|string} choice | ||
| 34 | + * @returns {string|undefined} | ||
| 35 | + * | ||
| 36 | + * @example | ||
| 37 | + * pickOption(params.options, "allow_always"); | ||
| 38 | + */ | ||
| 39 | +export function pickOption(options, choice) { | ||
| 40 | + const byKind = (kind) => options.find((o) => o.kind === kind)?.optionId; | ||
| 41 | + if (typeof choice === "string" && options.some((o) => o.optionId === choice)) return choice; | ||
| 42 | + if (choice === true) return byKind("allow_once") ?? byKind("allow_always"); | ||
| 43 | + return byKind("reject_once") ?? byKind("reject_always"); | ||
| 44 | +} | ||
| 45 | + | ||
| 46 | +export function toolCall(tc) { | ||
| 47 | + return { id: tc.toolCallId, title: tc.title, toolKind: tc.kind ?? undefined, input: tc.rawInput }; | ||
| 48 | +} | ||
| 49 | + | ||
| 50 | +// What a tool produced: mm puts it in rawOutput.output; other agents may only | ||
| 51 | +// send it as text content. | ||
| 52 | +function outputOf(update) { | ||
| 53 | + if (typeof update.rawOutput?.output === "string") return update.rawOutput.output; | ||
| 54 | + return (update.content ?? []).map((block) => (block.content?.type === "text" ? block.content.text : "")).join(""); | ||
| 55 | +} | ||
modified
demo.acp.sdk/nodejs-sdk/lib/minime.js +3 -55 | @@ -20,6 +20,9 @@ import { createInterface } from "node:readline"; | ||
| 20 | 20 | import { Readable, Writable } from "node:stream"; |
| 21 | 21 | import path from "node:path"; |
| 22 | 22 | import { client, ndJsonStream, PROTOCOL_VERSION, RequestError } from "@agentclientprotocol/sdk"; |
| 23 | +import { eventOf, pickOption, toolCall } from "./events.js"; | |
| 24 | + | |
| 25 | +export { eventOf, pickOption } from "./events.js"; | |
| 23 | 26 | |
| 24 | 27 | /** |
| 25 | 28 | * The events a turn produces, reduced from ACP's `session/update` |
| @@ -269,61 +272,6 @@ export class Agent { | ||
| 269 | 272 | } |
| 270 | 273 | } |
| 271 | 274 | |
| 272 | -/** | |
| 273 | - * Reduces one ACP `session/update` to an Event; undefined for the updates that | |
| 274 | - * are editor detail (plan, usage_update, available_commands_update…) and for | |
| 275 | - * tool updates that are not an outcome (in_progress). | |
| 276 | - * | |
| 277 | - * @param {any} update | |
| 278 | - * @returns {Event|undefined} | |
| 279 | - */ | |
| 280 | -export function eventOf(update) { | |
| 281 | - switch (update.sessionUpdate) { | |
| 282 | - case "agent_message_chunk": | |
| 283 | - return update.content?.type === "text" ? { kind: "text", text: update.content.text } : undefined; | |
| 284 | - case "agent_thought_chunk": | |
| 285 | - return update.content?.type === "text" ? { kind: "thinking", text: update.content.text } : undefined; | |
| 286 | - case "tool_call": | |
| 287 | - return { kind: "tool_call", ...toolCall(update) }; | |
| 288 | - case "tool_call_update": | |
| 289 | - if (update.status === "completed" || update.status === "failed") { | |
| 290 | - return { kind: "tool_result", id: update.toolCallId, status: update.status, output: outputOf(update) }; | |
| 291 | - } | |
| 292 | - return undefined; | |
| 293 | - default: | |
| 294 | - return undefined; | |
| 295 | - } | |
| 296 | -} | |
| 297 | - | |
| 298 | -/** | |
| 299 | - * true / false → the "once" options (falling back on the "always" ones); a | |
| 300 | - * string → that optionId when offered, else a rejection. | |
| 301 | - * | |
| 302 | - * @param {Array<{ optionId: string, kind: string }>} options | |
| 303 | - * @param {boolean|string} choice | |
| 304 | - * @returns {string|undefined} | |
| 305 | - * | |
| 306 | - * @example | |
| 307 | - * pickOption(params.options, "allow_always"); | |
| 308 | - */ | |
| 309 | -export function pickOption(options, choice) { | |
| 310 | - const byKind = (kind) => options.find((o) => o.kind === kind)?.optionId; | |
| 311 | - if (typeof choice === "string" && options.some((o) => o.optionId === choice)) return choice; | |
| 312 | - if (choice === true) return byKind("allow_once") ?? byKind("allow_always"); | |
| 313 | - return byKind("reject_once") ?? byKind("reject_always"); | |
| 314 | -} | |
| 315 | - | |
| 316 | -function toolCall(tc) { | |
| 317 | - return { id: tc.toolCallId, title: tc.title, toolKind: tc.kind ?? undefined, input: tc.rawInput }; | |
| 318 | -} | |
| 319 | - | |
| 320 | -// What a tool produced: mm puts it in rawOutput.output; other agents may only | |
| 321 | -// send it as text content. | |
| 322 | -function outputOf(update) { | |
| 323 | - if (typeof update.rawOutput?.output === "string") return update.rawOutput.output; | |
| 324 | - return (update.content ?? []).map((block) => (block.content?.type === "text" ? block.content.text : "")).join(""); | |
| 325 | -} | |
| 326 | - | |
| 327 | 275 | // A JSON-RPC error in words: "Internal error (-32603): <mm's explanation>". |
| 328 | 276 | function rpcWords(err) { |
| 329 | 277 | const detail = typeof err.data?.error === "string" ? `: ${err.data.error}` : ""; |
| @@ -20,6 +20,9 @@ import { createInterface } from "node:readline"; | |||
| 20 | import { Readable, Writable } from "node:stream"; | 20 | import { Readable, Writable } from "node:stream"; |
| 21 | import path from "node:path"; | 21 | import path from "node:path"; |
| 22 | import { client, ndJsonStream, PROTOCOL_VERSION, RequestError } from "@agentclientprotocol/sdk"; | 22 | import { client, ndJsonStream, PROTOCOL_VERSION, RequestError } from "@agentclientprotocol/sdk"; |
| 23 | +import { eventOf, pickOption, toolCall } from "./events.js"; | ||
| 24 | + | ||
| 25 | +export { eventOf, pickOption } from "./events.js"; | ||
| 23 | 26 | ||
| 24 | /** | 27 | /** |
| 25 | * The events a turn produces, reduced from ACP's `session/update` | 28 | * The events a turn produces, reduced from ACP's `session/update` |
| @@ -269,61 +272,6 @@ export class Agent { | |||
| 269 | } | 272 | } |
| 270 | } | 273 | } |
| 271 | 274 | ||
| 272 | -/** | ||
| 273 | - * Reduces one ACP `session/update` to an Event; undefined for the updates that | ||
| 274 | - * are editor detail (plan, usage_update, available_commands_update…) and for | ||
| 275 | - * tool updates that are not an outcome (in_progress). | ||
| 276 | - * | ||
| 277 | - * @param {any} update | ||
| 278 | - * @returns {Event|undefined} | ||
| 279 | - */ | ||
| 280 | -export function eventOf(update) { | ||
| 281 | - switch (update.sessionUpdate) { | ||
| 282 | - case "agent_message_chunk": | ||
| 283 | - return update.content?.type === "text" ? { kind: "text", text: update.content.text } : undefined; | ||
| 284 | - case "agent_thought_chunk": | ||
| 285 | - return update.content?.type === "text" ? { kind: "thinking", text: update.content.text } : undefined; | ||
| 286 | - case "tool_call": | ||
| 287 | - return { kind: "tool_call", ...toolCall(update) }; | ||
| 288 | - case "tool_call_update": | ||
| 289 | - if (update.status === "completed" || update.status === "failed") { | ||
| 290 | - return { kind: "tool_result", id: update.toolCallId, status: update.status, output: outputOf(update) }; | ||
| 291 | - } | ||
| 292 | - return undefined; | ||
| 293 | - default: | ||
| 294 | - return undefined; | ||
| 295 | - } | ||
| 296 | -} | ||
| 297 | - | ||
| 298 | -/** | ||
| 299 | - * true / false → the "once" options (falling back on the "always" ones); a | ||
| 300 | - * string → that optionId when offered, else a rejection. | ||
| 301 | - * | ||
| 302 | - * @param {Array<{ optionId: string, kind: string }>} options | ||
| 303 | - * @param {boolean|string} choice | ||
| 304 | - * @returns {string|undefined} | ||
| 305 | - * | ||
| 306 | - * @example | ||
| 307 | - * pickOption(params.options, "allow_always"); | ||
| 308 | - */ | ||
| 309 | -export function pickOption(options, choice) { | ||
| 310 | - const byKind = (kind) => options.find((o) => o.kind === kind)?.optionId; | ||
| 311 | - if (typeof choice === "string" && options.some((o) => o.optionId === choice)) return choice; | ||
| 312 | - if (choice === true) return byKind("allow_once") ?? byKind("allow_always"); | ||
| 313 | - return byKind("reject_once") ?? byKind("reject_always"); | ||
| 314 | -} | ||
| 315 | - | ||
| 316 | -function toolCall(tc) { | ||
| 317 | - return { id: tc.toolCallId, title: tc.title, toolKind: tc.kind ?? undefined, input: tc.rawInput }; | ||
| 318 | -} | ||
| 319 | - | ||
| 320 | -// What a tool produced: mm puts it in rawOutput.output; other agents may only | ||
| 321 | -// send it as text content. | ||
| 322 | -function outputOf(update) { | ||
| 323 | - if (typeof update.rawOutput?.output === "string") return update.rawOutput.output; | ||
| 324 | - return (update.content ?? []).map((block) => (block.content?.type === "text" ? block.content.text : "")).join(""); | ||
| 325 | -} | ||
| 326 | - | ||
| 327 | // A JSON-RPC error in words: "Internal error (-32603): <mm's explanation>". | 275 | // A JSON-RPC error in words: "Internal error (-32603): <mm's explanation>". |
| 328 | function rpcWords(err) { | 276 | function rpcWords(err) { |
| 329 | const detail = typeof err.data?.error === "string" ? `: ${err.data.error}` : ""; | 277 | const detail = typeof err.data?.error === "string" ? `: ${err.data.error}` : ""; |
added
demo.acp.sdk/nodejs/lib/events.js +44 -0 | new file mode 100644 | ||
| @@ -0,0 +1,44 @@ | ||
| 1 | +// The ACP messages as minime's events: session updates, permission options, | |
| 2 | +// tool calls. Used by lib/minime.js, which exports pickOption too. | |
| 3 | + | |
| 4 | +/** | |
| 5 | + * Reduces one ACP `session/update` to an event; undefined for the updates | |
| 6 | + * that are editor detail (plan, usage_update, available_commands_update…) | |
| 7 | + * and for tool updates that are not an outcome (in_progress). | |
| 8 | + * | |
| 9 | + * @example | |
| 10 | + * eventOf({ sessionUpdate: "agent_message_chunk", content: { type: "text", text: "Hi" } }); // { kind: "text", text: "Hi" } | |
| 11 | + */ | |
| 12 | +export function eventOf(update) { | |
| 13 | + const kind = update.sessionUpdate; | |
| 14 | + if (Object.hasOwn(TEXT_KINDS, kind)) { | |
| 15 | + return update.content?.type === "text" ? { kind: TEXT_KINDS[kind], text: update.content.text } : undefined; | |
| 16 | + } | |
| 17 | + if (kind === "tool_call") return { kind: "tool_call", ...toolCall(update) }; | |
| 18 | + if (kind === "tool_call_update" && (update.status === "completed" || update.status === "failed")) { | |
| 19 | + return { kind: "tool_result", id: update.toolCallId, status: update.status, output: outputOf(update) }; | |
| 20 | + } | |
| 21 | + return undefined; | |
| 22 | +} | |
| 23 | + | |
| 24 | +// The text updates, and the kind of event each one is. | |
| 25 | +const TEXT_KINDS = { agent_message_chunk: "text", agent_thought_chunk: "thinking" }; | |
| 26 | + | |
| 27 | +export function toolCall(tc) { | |
| 28 | + return { id: tc.toolCallId, title: tc.title, toolKind: tc.kind, input: tc.rawInput }; | |
| 29 | +} | |
| 30 | + | |
| 31 | +function outputOf(update) { | |
| 32 | + if (update.rawOutput?.output !== undefined) return update.rawOutput.output; | |
| 33 | + return (update.content ?? []) | |
| 34 | + .map((block) => block.content?.text ?? "") | |
| 35 | + .join(""); | |
| 36 | +} | |
| 37 | + | |
| 38 | +// true / false → the "once" options; a string → that optionId when offered. | |
| 39 | +export function pickOption(options, choice) { | |
| 40 | + const byKind = (kind) => options.find((o) => o.kind === kind)?.optionId; | |
| 41 | + if (typeof choice === "string" && options.some((o) => o.optionId === choice)) return choice; | |
| 42 | + if (choice === true) return byKind("allow_once") ?? byKind("allow_always"); | |
| 43 | + return byKind("reject_once") ?? byKind("reject_always"); | |
| 44 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,44 @@ | |||
| 1 | +// The ACP messages as minime's events: session updates, permission options, | ||
| 2 | +// tool calls. Used by lib/minime.js, which exports pickOption too. | ||
| 3 | + | ||
| 4 | +/** | ||
| 5 | + * Reduces one ACP `session/update` to an event; undefined for the updates | ||
| 6 | + * that are editor detail (plan, usage_update, available_commands_update…) | ||
| 7 | + * and for tool updates that are not an outcome (in_progress). | ||
| 8 | + * | ||
| 9 | + * @example | ||
| 10 | + * eventOf({ sessionUpdate: "agent_message_chunk", content: { type: "text", text: "Hi" } }); // { kind: "text", text: "Hi" } | ||
| 11 | + */ | ||
| 12 | +export function eventOf(update) { | ||
| 13 | + const kind = update.sessionUpdate; | ||
| 14 | + if (Object.hasOwn(TEXT_KINDS, kind)) { | ||
| 15 | + return update.content?.type === "text" ? { kind: TEXT_KINDS[kind], text: update.content.text } : undefined; | ||
| 16 | + } | ||
| 17 | + if (kind === "tool_call") return { kind: "tool_call", ...toolCall(update) }; | ||
| 18 | + if (kind === "tool_call_update" && (update.status === "completed" || update.status === "failed")) { | ||
| 19 | + return { kind: "tool_result", id: update.toolCallId, status: update.status, output: outputOf(update) }; | ||
| 20 | + } | ||
| 21 | + return undefined; | ||
| 22 | +} | ||
| 23 | + | ||
| 24 | +// The text updates, and the kind of event each one is. | ||
| 25 | +const TEXT_KINDS = { agent_message_chunk: "text", agent_thought_chunk: "thinking" }; | ||
| 26 | + | ||
| 27 | +export function toolCall(tc) { | ||
| 28 | + return { id: tc.toolCallId, title: tc.title, toolKind: tc.kind, input: tc.rawInput }; | ||
| 29 | +} | ||
| 30 | + | ||
| 31 | +function outputOf(update) { | ||
| 32 | + if (update.rawOutput?.output !== undefined) return update.rawOutput.output; | ||
| 33 | + return (update.content ?? []) | ||
| 34 | + .map((block) => block.content?.text ?? "") | ||
| 35 | + .join(""); | ||
| 36 | +} | ||
| 37 | + | ||
| 38 | +// true / false → the "once" options; a string → that optionId when offered. | ||
| 39 | +export function pickOption(options, choice) { | ||
| 40 | + const byKind = (kind) => options.find((o) => o.kind === kind)?.optionId; | ||
| 41 | + if (typeof choice === "string" && options.some((o) => o.optionId === choice)) return choice; | ||
| 42 | + if (choice === true) return byKind("allow_once") ?? byKind("allow_always"); | ||
| 43 | + return byKind("reject_once") ?? byKind("reject_always"); | ||
| 44 | +} | ||
modified
demo.acp.sdk/nodejs/lib/minime.js +7 -40 | @@ -17,6 +17,9 @@ | ||
| 17 | 17 | import { spawn } from "node:child_process"; |
| 18 | 18 | import { createInterface } from "node:readline"; |
| 19 | 19 | import path from "node:path"; |
| 20 | +import { eventOf, pickOption, toolCall } from "./events.js"; | |
| 21 | + | |
| 22 | +export { pickOption } from "./events.js"; | |
| 20 | 23 | |
| 21 | 24 | /** |
| 22 | 25 | * The events a turn produces, reduced from ACP's `session/update` |
| @@ -248,27 +251,10 @@ export class Agent { | ||
| 248 | 251 | #update({ update }) { |
| 249 | 252 | const turn = this.#turn; |
| 250 | 253 | if (!turn) return; // e.g. available_commands_update right after session/new |
| 251 | - switch (update.sessionUpdate) { | |
| 252 | - case "agent_message_chunk": | |
| 253 | - if (update.content?.type === "text") { | |
| 254 | - turn.text += update.content.text; | |
| 255 | - turn.push({ kind: "text", text: update.content.text }); | |
| 256 | - } | |
| 257 | - break; | |
| 258 | - case "agent_thought_chunk": | |
| 259 | - if (update.content?.type === "text") turn.push({ kind: "thinking", text: update.content.text }); | |
| 260 | - break; | |
| 261 | - case "tool_call": | |
| 262 | - turn.push({ kind: "tool_call", ...toolCall(update) }); | |
| 263 | - break; | |
| 264 | - case "tool_call_update": | |
| 265 | - if (update.status === "completed" || update.status === "failed") { | |
| 266 | - turn.push({ kind: "tool_result", id: update.toolCallId, status: update.status, output: outputOf(update) }); | |
| 267 | - } | |
| 268 | - break; | |
| 269 | - default: | |
| 270 | - break; // plan, usage_update, available_commands_update…: editor detail | |
| 271 | - } | |
| 254 | + const ev = eventOf(update); // undefined for plan, usage_update…: editor detail | |
| 255 | + if (!ev) return; | |
| 256 | + if (ev.kind === "text") turn.text += ev.text; | |
| 257 | + turn.push(ev); | |
| 272 | 258 | } |
| 273 | 259 | |
| 274 | 260 | #fail(err) { |
| @@ -286,22 +272,3 @@ function rpcError({ message, code, data }) { | ||
| 286 | 272 | const detail = data === undefined ? "" : `: ${typeof data === "string" ? data : JSON.stringify(data)}`; |
| 287 | 273 | return new Error(`minime: ${message} (${code})${detail}`); |
| 288 | 274 | } |
| 289 | - | |
| 290 | -function toolCall(tc) { | |
| 291 | - return { id: tc.toolCallId, title: tc.title, toolKind: tc.kind, input: tc.rawInput }; | |
| 292 | -} | |
| 293 | - | |
| 294 | -function outputOf(update) { | |
| 295 | - if (update.rawOutput?.output !== undefined) return update.rawOutput.output; | |
| 296 | - return (update.content ?? []) | |
| 297 | - .map((block) => block.content?.text ?? "") | |
| 298 | - .join(""); | |
| 299 | -} | |
| 300 | - | |
| 301 | -// true / false → the "once" options; a string → that optionId when offered. | |
| 302 | -export function pickOption(options, choice) { | |
| 303 | - const byKind = (kind) => options.find((o) => o.kind === kind)?.optionId; | |
| 304 | - if (typeof choice === "string" && options.some((o) => o.optionId === choice)) return choice; | |
| 305 | - if (choice === true) return byKind("allow_once") ?? byKind("allow_always"); | |
| 306 | - return byKind("reject_once") ?? byKind("reject_always"); | |
| 307 | -} | |
| @@ -17,6 +17,9 @@ | |||
| 17 | import { spawn } from "node:child_process"; | 17 | import { spawn } from "node:child_process"; |
| 18 | import { createInterface } from "node:readline"; | 18 | import { createInterface } from "node:readline"; |
| 19 | import path from "node:path"; | 19 | import path from "node:path"; |
| 20 | +import { eventOf, pickOption, toolCall } from "./events.js"; | ||
| 21 | + | ||
| 22 | +export { pickOption } from "./events.js"; | ||
| 20 | 23 | ||
| 21 | /** | 24 | /** |
| 22 | * The events a turn produces, reduced from ACP's `session/update` | 25 | * The events a turn produces, reduced from ACP's `session/update` |
| @@ -248,27 +251,10 @@ export class Agent { | |||
| 248 | #update({ update }) { | 251 | #update({ update }) { |
| 249 | const turn = this.#turn; | 252 | const turn = this.#turn; |
| 250 | if (!turn) return; // e.g. available_commands_update right after session/new | 253 | if (!turn) return; // e.g. available_commands_update right after session/new |
| 251 | - switch (update.sessionUpdate) { | 254 | + const ev = eventOf(update); // undefined for plan, usage_update…: editor detail |
| 252 | - case "agent_message_chunk": | 255 | + if (!ev) return; |
| 253 | - if (update.content?.type === "text") { | 256 | + if (ev.kind === "text") turn.text += ev.text; |
| 254 | - turn.text += update.content.text; | 257 | + turn.push(ev); |
| 255 | - turn.push({ kind: "text", text: update.content.text }); | ||
| 256 | - } | ||
| 257 | - break; | ||
| 258 | - case "agent_thought_chunk": | ||
| 259 | - if (update.content?.type === "text") turn.push({ kind: "thinking", text: update.content.text }); | ||
| 260 | - break; | ||
| 261 | - case "tool_call": | ||
| 262 | - turn.push({ kind: "tool_call", ...toolCall(update) }); | ||
| 263 | - break; | ||
| 264 | - case "tool_call_update": | ||
| 265 | - if (update.status === "completed" || update.status === "failed") { | ||
| 266 | - turn.push({ kind: "tool_result", id: update.toolCallId, status: update.status, output: outputOf(update) }); | ||
| 267 | - } | ||
| 268 | - break; | ||
| 269 | - default: | ||
| 270 | - break; // plan, usage_update, available_commands_update…: editor detail | ||
| 271 | - } | ||
| 272 | } | 258 | } |
| 273 | 259 | ||
| 274 | #fail(err) { | 260 | #fail(err) { |
| @@ -286,22 +272,3 @@ function rpcError({ message, code, data }) { | |||
| 286 | const detail = data === undefined ? "" : `: ${typeof data === "string" ? data : JSON.stringify(data)}`; | 272 | const detail = data === undefined ? "" : `: ${typeof data === "string" ? data : JSON.stringify(data)}`; |
| 287 | return new Error(`minime: ${message} (${code})${detail}`); | 273 | return new Error(`minime: ${message} (${code})${detail}`); |
| 288 | } | 274 | } |
| 289 | - | ||
| 290 | -function toolCall(tc) { | ||
| 291 | - return { id: tc.toolCallId, title: tc.title, toolKind: tc.kind, input: tc.rawInput }; | ||
| 292 | -} | ||
| 293 | - | ||
| 294 | -function outputOf(update) { | ||
| 295 | - if (update.rawOutput?.output !== undefined) return update.rawOutput.output; | ||
| 296 | - return (update.content ?? []) | ||
| 297 | - .map((block) => block.content?.text ?? "") | ||
| 298 | - .join(""); | ||
| 299 | -} | ||
| 300 | - | ||
| 301 | -// true / false → the "once" options; a string → that optionId when offered. | ||
| 302 | -export function pickOption(options, choice) { | ||
| 303 | - const byKind = (kind) => options.find((o) => o.kind === kind)?.optionId; | ||
| 304 | - if (typeof choice === "string" && options.some((o) => o.optionId === choice)) return choice; | ||
| 305 | - if (choice === true) return byKind("allow_once") ?? byKind("allow_always"); | ||
| 306 | - return byKind("reject_once") ?? byKind("reject_always"); | ||
| 307 | -} | ||