@crewhaus/streaming-tool-executor 0.1.3 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.test.ts DELETED
@@ -1,237 +0,0 @@
1
- import { describe, expect, test } from "bun:test";
2
- import type { StreamEvent } from "@crewhaus/adapter-anthropic";
3
- import { buildTool } from "@crewhaus/tool-builder";
4
- import type { RegisteredTool } from "@crewhaus/tool-catalog";
5
- import { z } from "zod";
6
- import { type StreamingToolEvent, executeStreaming } from "./index";
7
-
8
- /**
9
- * Build an `AsyncIterable<StreamEvent>` from an array, with a manual
10
- * "controller" so tests can interleave tool-use emissions with the
11
- * stream lifecycle (e.g. emit a tool_use, await its result, emit
12
- * another).
13
- */
14
- class StreamController {
15
- private events: StreamEvent[] = [];
16
- private resolve!: () => void;
17
- private done = new Promise<void>((r) => {
18
- this.resolve = r;
19
- });
20
- push(ev: StreamEvent): void {
21
- this.events.push(ev);
22
- }
23
- finish(): void {
24
- this.events.push({ kind: "message_stop" });
25
- this.resolve();
26
- }
27
- iter(): AsyncIterable<StreamEvent> {
28
- const events = this.events;
29
- const done = this.done;
30
- return {
31
- [Symbol.asyncIterator]: async function* () {
32
- // Yield buffered events first, then await further pushes.
33
- let i = 0;
34
- while (i < events.length) {
35
- yield events[i++] as StreamEvent;
36
- }
37
- await done;
38
- while (i < events.length) {
39
- yield events[i++] as StreamEvent;
40
- }
41
- },
42
- };
43
- }
44
- }
45
-
46
- function buildSimpleStream(
47
- toolUses: Array<{ id: string; name: string; input: unknown }>,
48
- ): AsyncIterable<StreamEvent> {
49
- const evs: StreamEvent[] = [{ kind: "message_start" }];
50
- let idx = 0;
51
- for (const tu of toolUses) {
52
- const i = idx++;
53
- evs.push({
54
- kind: "content_block_start",
55
- index: i,
56
- block: { type: "tool_use", id: tu.id, name: tu.name, input: {} },
57
- });
58
- evs.push({
59
- kind: "content_block_delta",
60
- index: i,
61
- delta: { type: "input_json_delta", partial_json: JSON.stringify(tu.input) },
62
- });
63
- evs.push({ kind: "content_block_stop", index: i });
64
- }
65
- evs.push({ kind: "message_delta", stopReason: "tool_use" });
66
- evs.push({ kind: "message_stop" });
67
- return (async function* () {
68
- for (const e of evs) yield e;
69
- })();
70
- }
71
-
72
- function makeReadTool(spy: (id: unknown) => void): RegisteredTool {
73
- return buildTool({
74
- name: "Read",
75
- description: "read",
76
- inputSchema: z.object({ id: z.string() }),
77
- readOnly: true,
78
- concurrencySafe: true,
79
- execute: async (input) => {
80
- spy(input.id);
81
- return `read:${input.id}`;
82
- },
83
- });
84
- }
85
-
86
- function makeBashTool(): RegisteredTool {
87
- return buildTool({
88
- name: "Bash",
89
- description: "bash",
90
- inputSchema: z.object({ cmd: z.string() }),
91
- destructive: true,
92
- execute: async () => "bash-out",
93
- });
94
- }
95
-
96
- describe("executeStreaming over AsyncIterable<StreamEvent>", () => {
97
- test("dispatches a single tool_use and returns its result", async () => {
98
- const ids: unknown[] = [];
99
- const tool = makeReadTool((id) => ids.push(id));
100
- const stream = buildSimpleStream([{ id: "tu1", name: "Read", input: { id: "a" } }]);
101
- const events: StreamingToolEvent[] = [];
102
- const result = await executeStreaming(stream, {
103
- toolByName: new Map([["Read", tool]]),
104
- onEvent: (e) => events.push(e),
105
- });
106
- expect(ids).toEqual(["a"]);
107
- expect(result.toolResults).toHaveLength(1);
108
- expect(result.toolResults[0]?.tool_use_id).toBe("tu1");
109
- const finalContent = result.finalContent;
110
- expect(finalContent).toHaveLength(1);
111
- expect(finalContent[0]).toMatchObject({ type: "tool_use", id: "tu1", name: "Read" });
112
- expect(events.find((e) => e.kind === "tool-finished")).toBeDefined();
113
- });
114
-
115
- test("reconstructs tool_use input from incremental input_json_delta", async () => {
116
- const ids: unknown[] = [];
117
- const tool = makeReadTool((id) => ids.push(id));
118
- const stream = (async function* () {
119
- yield { kind: "message_start" } satisfies StreamEvent;
120
- yield {
121
- kind: "content_block_start",
122
- index: 0,
123
- block: { type: "tool_use", id: "tu1", name: "Read", input: {} },
124
- } satisfies StreamEvent;
125
- yield {
126
- kind: "content_block_delta",
127
- index: 0,
128
- delta: { type: "input_json_delta", partial_json: '{"id":' },
129
- } satisfies StreamEvent;
130
- yield {
131
- kind: "content_block_delta",
132
- index: 0,
133
- delta: { type: "input_json_delta", partial_json: '"streamed"}' },
134
- } satisfies StreamEvent;
135
- yield { kind: "content_block_stop", index: 0 } satisfies StreamEvent;
136
- yield { kind: "message_delta", stopReason: "tool_use" } satisfies StreamEvent;
137
- yield { kind: "message_stop" } satisfies StreamEvent;
138
- })();
139
- const result = await executeStreaming(stream, {
140
- toolByName: new Map([["Read", tool]]),
141
- });
142
- expect(ids).toEqual(["streamed"]);
143
- expect(result.finalContent[0]).toMatchObject({
144
- type: "tool_use",
145
- input: { id: "streamed" },
146
- });
147
- });
148
-
149
- test("preserves text-only blocks and emits onTextDelta callbacks", async () => {
150
- const chunks: string[] = [];
151
- const stream = (async function* () {
152
- yield { kind: "message_start" } satisfies StreamEvent;
153
- yield {
154
- kind: "content_block_start",
155
- index: 0,
156
- block: { type: "text", text: "" },
157
- } satisfies StreamEvent;
158
- yield {
159
- kind: "content_block_delta",
160
- index: 0,
161
- delta: { type: "text_delta", text: "Hi " },
162
- } satisfies StreamEvent;
163
- yield {
164
- kind: "content_block_delta",
165
- index: 0,
166
- delta: { type: "text_delta", text: "there" },
167
- } satisfies StreamEvent;
168
- yield { kind: "content_block_stop", index: 0 } satisfies StreamEvent;
169
- yield { kind: "message_delta", stopReason: "end_turn" } satisfies StreamEvent;
170
- yield { kind: "message_stop" } satisfies StreamEvent;
171
- })();
172
- const result = await executeStreaming(stream, {
173
- toolByName: new Map(),
174
- onTextDelta: (c) => chunks.push(c),
175
- });
176
- expect(chunks).toEqual(["Hi ", "there"]);
177
- expect(result.finalContent[0]).toEqual({ type: "text", text: "Hi there" });
178
- });
179
-
180
- test("uses runTool callback when provided (skips local catalog dispatch)", async () => {
181
- const seen: string[] = [];
182
- const stream = buildSimpleStream([{ id: "tu1", name: "Custom", input: { x: 1 } }]);
183
- const result = await executeStreaming(stream, {
184
- toolByName: new Map(),
185
- runTool: async (block) => {
186
- seen.push(block.name);
187
- return {
188
- type: "tool_result",
189
- tool_use_id: block.id,
190
- content: "from runTool",
191
- is_error: false,
192
- };
193
- },
194
- });
195
- expect(seen).toEqual(["Custom"]);
196
- expect(result.toolResults[0]?.content).toBe("from runTool");
197
- });
198
-
199
- test("synthetic 'unknown tool' result when no tool and no runTool", async () => {
200
- const stream = buildSimpleStream([{ id: "tu1", name: "Missing", input: {} }]);
201
- const result = await executeStreaming(stream, { toolByName: new Map() });
202
- expect(result.toolResults[0]?.is_error).toBe(true);
203
- expect(result.toolResults[0]?.content).toContain("unknown tool");
204
- });
205
-
206
- test("destructive failure aborts pending siblings", async () => {
207
- const bash = makeBashTool();
208
- const stream = buildSimpleStream([
209
- { id: "tu1", name: "Bash", input: { cmd: "ok" } },
210
- { id: "tu2", name: "Bash", input: { cmd: "later" } },
211
- ]);
212
- const events: StreamingToolEvent[] = [];
213
- const result = await executeStreaming(stream, {
214
- toolByName: new Map([["Bash", bash]]),
215
- onEvent: (e) => events.push(e),
216
- runTool: async (block) => {
217
- if (block.id === "tu1") {
218
- return {
219
- type: "tool_result",
220
- tool_use_id: block.id,
221
- content: "boom",
222
- is_error: true,
223
- };
224
- }
225
- return {
226
- type: "tool_result",
227
- tool_use_id: block.id,
228
- content: "should not happen",
229
- is_error: false,
230
- };
231
- },
232
- });
233
- const aborted = result.toolResults.find((r) => r.tool_use_id === "tu2");
234
- expect(aborted?.is_error).toBe(true);
235
- expect(events.find((e) => e.kind === "sibling-aborted")).toBeDefined();
236
- });
237
- });
package/src/index.ts DELETED
@@ -1,432 +0,0 @@
1
- import type Anthropic from "@anthropic-ai/sdk";
2
- /**
3
- * Catalog R3 `streaming-tool-executor` — dispatch tool execution mid-
4
- * stream so the first tool can start running before `message_stop`.
5
- * Latency-sensitive turns (e.g. "read 5 files in parallel") finish
6
- * noticeably faster than the post-stream serial path.
7
- *
8
- * Section 17 refactor: this module previously consumed Anthropic's
9
- * SDK event-emitter (`AnthropicLikeStream` with `on("contentBlock")`).
10
- * It now consumes `AsyncIterable<StreamEvent>` directly so any
11
- * `ProviderAdapter` (Anthropic, OpenAI, Gemini, Bedrock) drives it.
12
- *
13
- * The dispatch logic is unchanged:
14
- *
15
- * - No tools currently running → run.
16
- * - Currently-running set is all concurrency-safe AND this entry is
17
- * concurrency-safe → run.
18
- * - Otherwise → wait for the running set to drain.
19
- *
20
- * Concurrency-safety follows the same triple-conjunction as
21
- * `tool-orchestrator`: `concurrencySafe && readOnly && !destructive`.
22
- *
23
- * Sibling abort: a per-call `AbortController` (`siblingAbortController`)
24
- * is signalled when a tool result errors AND `shouldAbortOnError` says
25
- * the failure is fatal. Pending queued tools are then short-circuited
26
- * with a synthetic error result. In-flight tools that don't honour the
27
- * signal complete normally — the abort affects what gets dispatched
28
- * next, not what is already mid-flight.
29
- */
30
- import type { CanonicalContentBlock, StreamEvent, TokenUsage } from "@crewhaus/adapter-anthropic";
31
- import { RuntimeError } from "@crewhaus/errors";
32
- import type { RegisteredTool } from "@crewhaus/tool-catalog";
33
- import { executeTool } from "@crewhaus/tool-executor";
34
-
35
- export type StreamingToolEvent =
36
- | { kind: "tool-started"; toolUseId: string; toolName: string; startedAt: number }
37
- | {
38
- kind: "tool-finished";
39
- toolUseId: string;
40
- toolName: string;
41
- durationMs: number;
42
- isError: boolean;
43
- }
44
- | { kind: "sibling-aborted"; reason: string; failedToolName: string };
45
-
46
- /** A reconstructed `tool_use` block — what `runTool` callbacks receive. */
47
- export type CompletedToolUseBlock = {
48
- readonly id: string;
49
- readonly name: string;
50
- readonly input: unknown;
51
- };
52
-
53
- export type StreamingExecuteOptions = {
54
- readonly toolByName: Map<string, RegisteredTool>;
55
- readonly onEvent?: (e: StreamingToolEvent) => void;
56
- readonly onTextDelta?: (chunk: string, blockIndex: number) => void;
57
- readonly abortSignal?: AbortSignal;
58
- /**
59
- * Decide whether a sibling tool failure should abort still-running
60
- * tools. Default: abort when the failing tool was destructive
61
- * (mirrors claude-code's Bash-only abort, but generalised).
62
- */
63
- readonly shouldAbortOnError?: (
64
- failedToolName: string,
65
- tool: RegisteredTool | undefined,
66
- ) => boolean;
67
- /**
68
- * Per-tool execution function used in place of the bare `executeTool`
69
- * call. The runtime supplies one that adds permission gating, per-tool
70
- * abort signals, and result-store wrapping so the streaming path
71
- * shares the same contract as the post-stream batch path.
72
- *
73
- * Must return a fully-formed `ToolResultBlockParam`; the streaming
74
- * executor uses it as-is. When omitted, falls back to a minimal
75
- * `executeTool`-only invocation (no permission gate, no result
76
- * persistence).
77
- */
78
- readonly runTool?: (block: CompletedToolUseBlock) => Promise<Anthropic.ToolResultBlockParam>;
79
- };
80
-
81
- export type StreamingExecuteResult = {
82
- readonly finalContent: ReadonlyArray<CanonicalContentBlock>;
83
- readonly toolResults: ReadonlyArray<Anthropic.ToolResultBlockParam>;
84
- readonly stopReason: string;
85
- readonly usage: TokenUsage;
86
- };
87
-
88
- type AccumulatedBlock =
89
- | { kind: "text"; text: string }
90
- | { kind: "tool_use"; id: string; name: string; jsonBuffer: string }
91
- | { kind: "thinking"; thinking: string; signature?: string };
92
-
93
- type QueueEntry = {
94
- readonly block: CompletedToolUseBlock;
95
- readonly tool: RegisteredTool | undefined;
96
- status: "queued" | "running" | "done" | "aborted";
97
- };
98
-
99
- function defaultShouldAbortOnError(
100
- _failedToolName: string,
101
- tool: RegisteredTool | undefined,
102
- ): boolean {
103
- return tool?.destructive === true;
104
- }
105
-
106
- function isConcurrencySafe(tool: RegisteredTool | undefined): boolean {
107
- return tool?.concurrencySafe === true && tool.readOnly && !tool.destructive;
108
- }
109
-
110
- /**
111
- * Drive an `AsyncIterable<StreamEvent>` and dispatch `tool_use` blocks
112
- * as they complete. Resolves once the stream finalises AND every
113
- * dispatched (or aborted) tool settles. The returned `toolResults`
114
- * are sorted to match the order of `tool_use` blocks in
115
- * `finalContent` so downstream code can pair results back to their
116
- * original blocks by index.
117
- */
118
- export async function executeStreaming(
119
- stream: AsyncIterable<StreamEvent>,
120
- opts: StreamingExecuteOptions,
121
- ): Promise<StreamingExecuteResult> {
122
- const queue: QueueEntry[] = [];
123
- const results = new Map<string, Anthropic.ToolResultBlockParam>();
124
- const inFlight = new Set<Promise<void>>();
125
- const siblingAbort = new AbortController();
126
- const shouldAbortOnError = opts.shouldAbortOnError ?? defaultShouldAbortOnError;
127
- const blocks = new Map<number, AccumulatedBlock>();
128
- // Order content blocks were OPENED in — tool_use ordering must
129
- // follow open-order for downstream pairing to work.
130
- const openOrder: number[] = [];
131
- let stopReason = "end_turn";
132
- let usage: TokenUsage = { input: 0, output: 0 };
133
-
134
- if (opts.abortSignal) {
135
- if (opts.abortSignal.aborted) {
136
- siblingAbort.abort("external_abort");
137
- } else {
138
- opts.abortSignal.addEventListener(
139
- "abort",
140
- () => {
141
- siblingAbort.abort("external_abort");
142
- },
143
- { once: true },
144
- );
145
- }
146
- }
147
-
148
- const fire = (e: StreamingToolEvent) => {
149
- if (opts.onEvent) opts.onEvent(e);
150
- };
151
-
152
- // Mark a queued entry as aborted with the synthetic sibling-failure
153
- // result. Used by `processQueue` when the abort signal trips while
154
- // entries are still waiting in the queue.
155
- const markAborted = (entry: QueueEntry): void => {
156
- entry.status = "aborted";
157
- results.set(entry.block.id, {
158
- type: "tool_result",
159
- tool_use_id: entry.block.id,
160
- content: "aborted: sibling tool failed",
161
- is_error: true,
162
- });
163
- };
164
-
165
- // `dispatch` is only ever reached via `processQueue`, which already
166
- // diverts an aborted entry through `markAborted` before calling here,
167
- // so no abort re-check is needed at this point.
168
- const dispatch = (entry: QueueEntry): void => {
169
- if (entry.status !== "queued") return;
170
- const tool = entry.tool;
171
- if (tool === undefined && opts.runTool === undefined) {
172
- entry.status = "done";
173
- results.set(entry.block.id, {
174
- type: "tool_result",
175
- tool_use_id: entry.block.id,
176
- content: `unknown tool "${entry.block.name}"`,
177
- is_error: true,
178
- });
179
- return;
180
- }
181
- entry.status = "running";
182
- const startedAt = Date.now();
183
- fire({
184
- kind: "tool-started",
185
- toolUseId: entry.block.id,
186
- toolName: entry.block.name,
187
- startedAt,
188
- });
189
- // By the guard above, when `runTool` is absent the entry's `tool`
190
- // is guaranteed defined, so the `executeTool` branch never sees an
191
- // undefined tool. (A bare `tool === undefined && runTool === undefined`
192
- // entry has already short-circuited with an "unknown tool" result.)
193
- const runOne = (resolvedTool: RegisteredTool): Promise<Anthropic.ToolResultBlockParam> =>
194
- executeTool(resolvedTool, entry.block.input, { toolUseId: entry.block.id }).then(
195
- (res): Anthropic.ToolResultBlockParam => ({
196
- type: "tool_result",
197
- tool_use_id: entry.block.id,
198
- content:
199
- typeof res.content === "string"
200
- ? res.content
201
- : (res.content as ReadonlyArray<
202
- Anthropic.TextBlockParam | Anthropic.ImageBlockParam
203
- > as Anthropic.ToolResultBlockParam["content"]),
204
- is_error: res.isError,
205
- }),
206
- );
207
- const dispatchPromise: Promise<Anthropic.ToolResultBlockParam> = opts.runTool
208
- ? opts.runTool(entry.block)
209
- : runOne(tool as RegisteredTool);
210
- const promise = dispatchPromise
211
- .then((res) => {
212
- entry.status = "done";
213
- results.set(entry.block.id, res);
214
- const isError = res.is_error ?? false;
215
- fire({
216
- kind: "tool-finished",
217
- toolUseId: entry.block.id,
218
- toolName: entry.block.name,
219
- durationMs: Date.now() - startedAt,
220
- isError,
221
- });
222
- if (
223
- isError &&
224
- !siblingAbort.signal.aborted &&
225
- shouldAbortOnError(entry.block.name, entry.tool)
226
- ) {
227
- siblingAbort.abort("sibling_error");
228
- fire({
229
- kind: "sibling-aborted",
230
- reason: "sibling_error",
231
- failedToolName: entry.block.name,
232
- });
233
- }
234
- })
235
- .catch((err: unknown) => {
236
- entry.status = "done";
237
- const msg = err instanceof Error ? err.message : String(err);
238
- results.set(entry.block.id, {
239
- type: "tool_result",
240
- tool_use_id: entry.block.id,
241
- content: msg,
242
- is_error: true,
243
- });
244
- })
245
- .finally(() => {
246
- inFlight.delete(promise);
247
- processQueue();
248
- });
249
- inFlight.add(promise);
250
- };
251
-
252
- const processQueue = (): void => {
253
- for (const entry of queue) {
254
- if (entry.status !== "queued") continue;
255
- if (siblingAbort.signal.aborted) {
256
- markAborted(entry);
257
- continue;
258
- }
259
- if (inFlight.size === 0) {
260
- dispatch(entry);
261
- continue;
262
- }
263
- // With work already in flight, only dispatch this entry when the
264
- // running set is fully concurrency-safe AND this entry is too;
265
- // otherwise stop scanning and wait for the running set to drain.
266
- const runningAllSafe = queue.every(
267
- (e) => e.status !== "running" || isConcurrencySafe(e.tool),
268
- );
269
- const canRunConcurrently = runningAllSafe && isConcurrencySafe(entry.tool);
270
- if (!canRunConcurrently) return;
271
- dispatch(entry);
272
- }
273
- };
274
-
275
- // Walk the stream, accumulating blocks; on content_block_stop for a
276
- // tool_use, reconstruct it and enqueue.
277
- try {
278
- for await (const ev of stream) {
279
- switch (ev.kind) {
280
- case "message_start":
281
- if (ev.usage) usage = ev.usage;
282
- break;
283
- case "content_block_start":
284
- openOrder.push(ev.index);
285
- switch (ev.block.type) {
286
- case "text":
287
- blocks.set(ev.index, { kind: "text", text: ev.block.text });
288
- break;
289
- case "tool_use":
290
- blocks.set(ev.index, {
291
- kind: "tool_use",
292
- id: ev.block.id,
293
- name: ev.block.name,
294
- jsonBuffer: "",
295
- });
296
- break;
297
- case "thinking":
298
- blocks.set(ev.index, {
299
- kind: "thinking",
300
- thinking: ev.block.thinking,
301
- ...(ev.block.signature !== undefined ? { signature: ev.block.signature } : {}),
302
- });
303
- break;
304
- }
305
- break;
306
- case "content_block_delta": {
307
- const block = blocks.get(ev.index);
308
- if (block === undefined) break;
309
- if (ev.delta.type === "text_delta" && block.kind === "text") {
310
- block.text += ev.delta.text;
311
- opts.onTextDelta?.(ev.delta.text, ev.index);
312
- } else if (ev.delta.type === "input_json_delta" && block.kind === "tool_use") {
313
- block.jsonBuffer += ev.delta.partial_json;
314
- } else if (ev.delta.type === "thinking_delta" && block.kind === "thinking") {
315
- block.thinking += ev.delta.thinking;
316
- } else if (ev.delta.type === "signature_delta" && block.kind === "thinking") {
317
- block.signature = ev.delta.signature;
318
- }
319
- break;
320
- }
321
- case "content_block_stop": {
322
- const block = blocks.get(ev.index);
323
- if (block === undefined) break;
324
- if (block.kind === "tool_use") {
325
- let input: unknown = {};
326
- if (block.jsonBuffer.length > 0) {
327
- try {
328
- input = JSON.parse(block.jsonBuffer);
329
- } catch {
330
- input = { __parse_error: true, raw: block.jsonBuffer };
331
- }
332
- }
333
- const completed: CompletedToolUseBlock = {
334
- id: block.id,
335
- name: block.name,
336
- input,
337
- };
338
- // Persist the parsed input on the block so finalContent
339
- // reflects what we passed to runTool.
340
- (block as { jsonBuffer: string }).jsonBuffer = JSON.stringify(input);
341
- queue.push({
342
- block: completed,
343
- tool: opts.toolByName.get(completed.name),
344
- status: "queued",
345
- });
346
- processQueue();
347
- }
348
- break;
349
- }
350
- case "message_delta":
351
- if (ev.stopReason !== undefined) stopReason = ev.stopReason;
352
- if (ev.usage !== undefined) {
353
- usage = {
354
- input: ev.usage.input > 0 ? ev.usage.input : usage.input,
355
- output: ev.usage.output > 0 ? ev.usage.output : usage.output,
356
- ...(ev.usage.cacheRead !== undefined
357
- ? { cacheRead: ev.usage.cacheRead }
358
- : usage.cacheRead !== undefined
359
- ? { cacheRead: usage.cacheRead }
360
- : {}),
361
- ...(ev.usage.cacheCreate !== undefined
362
- ? { cacheCreate: ev.usage.cacheCreate }
363
- : usage.cacheCreate !== undefined
364
- ? { cacheCreate: usage.cacheCreate }
365
- : {}),
366
- };
367
- }
368
- break;
369
- case "message_stop":
370
- break;
371
- case "error":
372
- throw new RuntimeError(`streaming-tool-executor: stream error: ${ev.error.message}`);
373
- }
374
- }
375
- } catch (err) {
376
- throw new RuntimeError(
377
- `streaming-tool-executor: stream failed: ${err instanceof Error ? err.message : String(err)}`,
378
- err,
379
- );
380
- }
381
-
382
- // Drain any tools still in flight or queued. Re-run the scheduler
383
- // after each settle so an entry unblocked by the just-finished tool
384
- // (each tool's own `finally` also calls `processQueue`, but doing it
385
- // here keeps the drain self-contained) is dispatched and then awaited
386
- // on the next pass. The loop exits only once nothing is in flight,
387
- // which — because `processQueue` has just run — means the queue is
388
- // fully drained.
389
- while (inFlight.size > 0) {
390
- await Promise.race(inFlight);
391
- processQueue();
392
- }
393
-
394
- // Build the final content array in open-order so tool_use blocks
395
- // surface in the same order they came down the wire (matching the
396
- // Anthropic-shaped final message historically returned).
397
- const finalContent: CanonicalContentBlock[] = [];
398
- const seen = new Set<number>();
399
- for (const idx of openOrder) {
400
- if (seen.has(idx)) continue;
401
- seen.add(idx);
402
- const b = blocks.get(idx);
403
- if (b === undefined) continue;
404
- if (b.kind === "text") {
405
- finalContent.push({ type: "text", text: b.text });
406
- } else if (b.kind === "tool_use") {
407
- let input: unknown = {};
408
- try {
409
- input = JSON.parse(b.jsonBuffer);
410
- } catch {
411
- input = { __parse_error: true, raw: b.jsonBuffer };
412
- }
413
- finalContent.push({ type: "tool_use", id: b.id, name: b.name, input });
414
- } else {
415
- finalContent.push({
416
- type: "thinking",
417
- thinking: b.thinking,
418
- ...(b.signature !== undefined ? { signature: b.signature } : {}),
419
- });
420
- }
421
- }
422
-
423
- // Sort tool results to match tool_use block order in finalContent.
424
- const ordered: Anthropic.ToolResultBlockParam[] = [];
425
- for (const block of finalContent) {
426
- if (block.type !== "tool_use") continue;
427
- const r = results.get(block.id);
428
- if (r !== undefined) ordered.push(r);
429
- }
430
-
431
- return { finalContent, toolResults: ordered, stopReason, usage };
432
- }