@crewhaus/streaming-tool-executor 0.1.0

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/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@crewhaus/streaming-tool-executor",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "description": "Dispatch tool execution mid-stream so tools start before message_stop",
6
+ "main": "src/index.ts",
7
+ "types": "src/index.ts",
8
+ "exports": {
9
+ ".": "./src/index.ts"
10
+ },
11
+ "scripts": {
12
+ "test": "bun test src"
13
+ },
14
+ "dependencies": {
15
+ "@anthropic-ai/sdk": "^0.96.0",
16
+ "@crewhaus/adapter-anthropic": "0.0.0",
17
+ "@crewhaus/errors": "0.0.0",
18
+ "@crewhaus/tool-builder": "0.0.0",
19
+ "@crewhaus/tool-catalog": "0.0.0",
20
+ "@crewhaus/tool-executor": "0.0.0",
21
+ "@crewhaus/turn-state-machine": "0.0.0",
22
+ "zod": "^3.23.8"
23
+ },
24
+ "license": "Apache-2.0",
25
+ "author": {
26
+ "name": "Max Meier",
27
+ "email": "max@studiomax.io",
28
+ "url": "https://studiomax.io"
29
+ },
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "git+https://github.com/crewhaus/factory.git",
33
+ "directory": "packages/streaming-tool-executor"
34
+ },
35
+ "homepage": "https://github.com/crewhaus/factory/tree/main/packages/streaming-tool-executor#readme",
36
+ "bugs": {
37
+ "url": "https://github.com/crewhaus/factory/issues"
38
+ },
39
+ "publishConfig": {
40
+ "access": "restricted"
41
+ },
42
+ "files": [
43
+ "src",
44
+ "README.md",
45
+ "LICENSE",
46
+ "NOTICE"
47
+ ]
48
+ }
@@ -0,0 +1,237 @@
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 ADDED
@@ -0,0 +1,428 @@
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
+ const dispatch = (entry: QueueEntry): void => {
153
+ if (entry.status !== "queued") return;
154
+ if (siblingAbort.signal.aborted) {
155
+ entry.status = "aborted";
156
+ results.set(entry.block.id, {
157
+ type: "tool_result",
158
+ tool_use_id: entry.block.id,
159
+ content: "aborted: sibling tool failed",
160
+ is_error: true,
161
+ });
162
+ return;
163
+ }
164
+ if (entry.tool === undefined && opts.runTool === undefined) {
165
+ entry.status = "done";
166
+ results.set(entry.block.id, {
167
+ type: "tool_result",
168
+ tool_use_id: entry.block.id,
169
+ content: `unknown tool "${entry.block.name}"`,
170
+ is_error: true,
171
+ });
172
+ return;
173
+ }
174
+ entry.status = "running";
175
+ const startedAt = Date.now();
176
+ fire({
177
+ kind: "tool-started",
178
+ toolUseId: entry.block.id,
179
+ toolName: entry.block.name,
180
+ startedAt,
181
+ });
182
+ const dispatchPromise: Promise<Anthropic.ToolResultBlockParam> = opts.runTool
183
+ ? opts.runTool(entry.block)
184
+ : entry.tool === undefined
185
+ ? Promise.resolve<Anthropic.ToolResultBlockParam>({
186
+ type: "tool_result",
187
+ tool_use_id: entry.block.id,
188
+ content: `unknown tool "${entry.block.name}"`,
189
+ is_error: true,
190
+ })
191
+ : executeTool(entry.tool, entry.block.input, { toolUseId: entry.block.id }).then(
192
+ (res): Anthropic.ToolResultBlockParam => ({
193
+ type: "tool_result",
194
+ tool_use_id: entry.block.id,
195
+ content:
196
+ typeof res.content === "string"
197
+ ? res.content
198
+ : (res.content as ReadonlyArray<
199
+ Anthropic.TextBlockParam | Anthropic.ImageBlockParam
200
+ > as Anthropic.ToolResultBlockParam["content"]),
201
+ is_error: res.isError,
202
+ }),
203
+ );
204
+ const promise = dispatchPromise
205
+ .then((res) => {
206
+ entry.status = "done";
207
+ results.set(entry.block.id, res);
208
+ const isError = res.is_error ?? false;
209
+ fire({
210
+ kind: "tool-finished",
211
+ toolUseId: entry.block.id,
212
+ toolName: entry.block.name,
213
+ durationMs: Date.now() - startedAt,
214
+ isError,
215
+ });
216
+ if (
217
+ isError &&
218
+ !siblingAbort.signal.aborted &&
219
+ shouldAbortOnError(entry.block.name, entry.tool)
220
+ ) {
221
+ siblingAbort.abort("sibling_error");
222
+ fire({
223
+ kind: "sibling-aborted",
224
+ reason: "sibling_error",
225
+ failedToolName: entry.block.name,
226
+ });
227
+ }
228
+ })
229
+ .catch((err: unknown) => {
230
+ entry.status = "done";
231
+ const msg = err instanceof Error ? err.message : String(err);
232
+ results.set(entry.block.id, {
233
+ type: "tool_result",
234
+ tool_use_id: entry.block.id,
235
+ content: msg,
236
+ is_error: true,
237
+ });
238
+ })
239
+ .finally(() => {
240
+ inFlight.delete(promise);
241
+ processQueue();
242
+ });
243
+ inFlight.add(promise);
244
+ };
245
+
246
+ const processQueue = (): void => {
247
+ for (const entry of queue) {
248
+ if (entry.status !== "queued") continue;
249
+ if (siblingAbort.signal.aborted) {
250
+ entry.status = "aborted";
251
+ results.set(entry.block.id, {
252
+ type: "tool_result",
253
+ tool_use_id: entry.block.id,
254
+ content: "aborted: sibling tool failed",
255
+ is_error: true,
256
+ });
257
+ continue;
258
+ }
259
+ if (inFlight.size === 0) {
260
+ dispatch(entry);
261
+ continue;
262
+ }
263
+ const runningAllSafe = queue.every(
264
+ (e) => e.status !== "running" || isConcurrencySafe(e.tool),
265
+ );
266
+ if (runningAllSafe && isConcurrencySafe(entry.tool)) {
267
+ dispatch(entry);
268
+ } else {
269
+ return;
270
+ }
271
+ }
272
+ };
273
+
274
+ // Walk the stream, accumulating blocks; on content_block_stop for a
275
+ // tool_use, reconstruct it and enqueue.
276
+ try {
277
+ for await (const ev of stream) {
278
+ switch (ev.kind) {
279
+ case "message_start":
280
+ if (ev.usage) usage = ev.usage;
281
+ break;
282
+ case "content_block_start":
283
+ openOrder.push(ev.index);
284
+ switch (ev.block.type) {
285
+ case "text":
286
+ blocks.set(ev.index, { kind: "text", text: ev.block.text });
287
+ break;
288
+ case "tool_use":
289
+ blocks.set(ev.index, {
290
+ kind: "tool_use",
291
+ id: ev.block.id,
292
+ name: ev.block.name,
293
+ jsonBuffer: "",
294
+ });
295
+ break;
296
+ case "thinking":
297
+ blocks.set(ev.index, {
298
+ kind: "thinking",
299
+ thinking: ev.block.thinking,
300
+ ...(ev.block.signature !== undefined ? { signature: ev.block.signature } : {}),
301
+ });
302
+ break;
303
+ }
304
+ break;
305
+ case "content_block_delta": {
306
+ const block = blocks.get(ev.index);
307
+ if (block === undefined) break;
308
+ if (ev.delta.type === "text_delta" && block.kind === "text") {
309
+ block.text += ev.delta.text;
310
+ opts.onTextDelta?.(ev.delta.text, ev.index);
311
+ } else if (ev.delta.type === "input_json_delta" && block.kind === "tool_use") {
312
+ block.jsonBuffer += ev.delta.partial_json;
313
+ } else if (ev.delta.type === "thinking_delta" && block.kind === "thinking") {
314
+ block.thinking += ev.delta.thinking;
315
+ } else if (ev.delta.type === "signature_delta" && block.kind === "thinking") {
316
+ block.signature = ev.delta.signature;
317
+ }
318
+ break;
319
+ }
320
+ case "content_block_stop": {
321
+ const block = blocks.get(ev.index);
322
+ if (block === undefined) break;
323
+ if (block.kind === "tool_use") {
324
+ let input: unknown = {};
325
+ if (block.jsonBuffer.length > 0) {
326
+ try {
327
+ input = JSON.parse(block.jsonBuffer);
328
+ } catch {
329
+ input = { __parse_error: true, raw: block.jsonBuffer };
330
+ }
331
+ }
332
+ const completed: CompletedToolUseBlock = {
333
+ id: block.id,
334
+ name: block.name,
335
+ input,
336
+ };
337
+ // Persist the parsed input on the block so finalContent
338
+ // reflects what we passed to runTool.
339
+ (block as { jsonBuffer: string }).jsonBuffer = JSON.stringify(input);
340
+ queue.push({
341
+ block: completed,
342
+ tool: opts.toolByName.get(completed.name),
343
+ status: "queued",
344
+ });
345
+ processQueue();
346
+ }
347
+ break;
348
+ }
349
+ case "message_delta":
350
+ if (ev.stopReason !== undefined) stopReason = ev.stopReason;
351
+ if (ev.usage !== undefined) {
352
+ usage = {
353
+ input: ev.usage.input > 0 ? ev.usage.input : usage.input,
354
+ output: ev.usage.output > 0 ? ev.usage.output : usage.output,
355
+ ...(ev.usage.cacheRead !== undefined
356
+ ? { cacheRead: ev.usage.cacheRead }
357
+ : usage.cacheRead !== undefined
358
+ ? { cacheRead: usage.cacheRead }
359
+ : {}),
360
+ ...(ev.usage.cacheCreate !== undefined
361
+ ? { cacheCreate: ev.usage.cacheCreate }
362
+ : usage.cacheCreate !== undefined
363
+ ? { cacheCreate: usage.cacheCreate }
364
+ : {}),
365
+ };
366
+ }
367
+ break;
368
+ case "message_stop":
369
+ break;
370
+ case "error":
371
+ throw new RuntimeError(`streaming-tool-executor: stream error: ${ev.error.message}`);
372
+ }
373
+ }
374
+ } catch (err) {
375
+ throw new RuntimeError(
376
+ `streaming-tool-executor: stream failed: ${err instanceof Error ? err.message : String(err)}`,
377
+ err,
378
+ );
379
+ }
380
+
381
+ // Drain any tools still in flight or queued.
382
+ while (inFlight.size > 0) {
383
+ await Promise.race(inFlight);
384
+ }
385
+ processQueue();
386
+ while (inFlight.size > 0) {
387
+ await Promise.race(inFlight);
388
+ }
389
+
390
+ // Build the final content array in open-order so tool_use blocks
391
+ // surface in the same order they came down the wire (matching the
392
+ // Anthropic-shaped final message historically returned).
393
+ const finalContent: CanonicalContentBlock[] = [];
394
+ const seen = new Set<number>();
395
+ for (const idx of openOrder) {
396
+ if (seen.has(idx)) continue;
397
+ seen.add(idx);
398
+ const b = blocks.get(idx);
399
+ if (b === undefined) continue;
400
+ if (b.kind === "text") {
401
+ finalContent.push({ type: "text", text: b.text });
402
+ } else if (b.kind === "tool_use") {
403
+ let input: unknown = {};
404
+ try {
405
+ input = JSON.parse(b.jsonBuffer);
406
+ } catch {
407
+ input = { __parse_error: true, raw: b.jsonBuffer };
408
+ }
409
+ finalContent.push({ type: "tool_use", id: b.id, name: b.name, input });
410
+ } else {
411
+ finalContent.push({
412
+ type: "thinking",
413
+ thinking: b.thinking,
414
+ ...(b.signature !== undefined ? { signature: b.signature } : {}),
415
+ });
416
+ }
417
+ }
418
+
419
+ // Sort tool results to match tool_use block order in finalContent.
420
+ const ordered: Anthropic.ToolResultBlockParam[] = [];
421
+ for (const block of finalContent) {
422
+ if (block.type !== "tool_use") continue;
423
+ const r = results.get(block.id);
424
+ if (r !== undefined) ordered.push(r);
425
+ }
426
+
427
+ return { finalContent, toolResults: ordered, stopReason, usage };
428
+ }
@@ -0,0 +1,60 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ /**
3
+ * T7 load test for streaming-tool-executor: 50 tool_use blocks arrive
4
+ * on the stream, all running a fast read-only no-op tool. We confirm
5
+ * completion under a time budget, no race-induced lost results, and
6
+ * stable ordering of results vs. finalContent.
7
+ *
8
+ * Section 17 refactor: the test now feeds an AsyncIterable<StreamEvent>
9
+ * instead of the old AnthropicLikeStream event emitter.
10
+ */
11
+ import type { StreamEvent } from "@crewhaus/adapter-anthropic";
12
+ import { buildTool } from "@crewhaus/tool-builder";
13
+ import { z } from "zod";
14
+ import { executeStreaming } from "./index";
15
+
16
+ const noop = buildTool({
17
+ name: "Read",
18
+ description: "noop read",
19
+ inputSchema: z.object({ id: z.number() }),
20
+ readOnly: true,
21
+ concurrencySafe: true,
22
+ execute: async (input) => `r:${input.id}`,
23
+ });
24
+
25
+ async function* fiftyToolUseStream(): AsyncIterable<StreamEvent> {
26
+ yield { kind: "message_start" };
27
+ for (let i = 0; i < 50; i++) {
28
+ yield {
29
+ kind: "content_block_start",
30
+ index: i,
31
+ block: { type: "tool_use", id: `tu_${i}`, name: "Read", input: {} },
32
+ };
33
+ yield {
34
+ kind: "content_block_delta",
35
+ index: i,
36
+ delta: { type: "input_json_delta", partial_json: JSON.stringify({ id: i }) },
37
+ };
38
+ yield { kind: "content_block_stop", index: i };
39
+ // Mimic realistic mid-stream arrival.
40
+ await new Promise<void>((r) => setImmediate(r));
41
+ }
42
+ yield { kind: "message_delta", stopReason: "tool_use" };
43
+ yield { kind: "message_stop" };
44
+ }
45
+
46
+ describe("streaming-tool-executor — T7 load", () => {
47
+ test("50 tool_use blocks complete under 1s and preserve order", async () => {
48
+ const start = Date.now();
49
+ const out = await executeStreaming(fiftyToolUseStream(), {
50
+ toolByName: new Map([["Read", noop]]),
51
+ });
52
+ const elapsed = Date.now() - start;
53
+ expect(elapsed).toBeLessThan(1000);
54
+ expect(out.toolResults.length).toBe(50);
55
+
56
+ // Order matches finalContent block order.
57
+ const expectedIds = Array.from({ length: 50 }, (_, i) => `tu_${i}`);
58
+ expect(out.toolResults.map((r) => r.tool_use_id)).toEqual(expectedIds);
59
+ });
60
+ });