xonecas.io
64875
Mc

mini-coder / commit cbd2db1

fix(stream): ignore empty tool-input-start events

xonecas2026-03-14 cbd2db166f6127eed5e6cdbc571a779b8dad79c5 parent 00475cb

3 files changed

src/llm-api/turn-execution.test.ts+36−0
@@ -53,4 +53,40 @@ describe("mapFullStreamToTurnEvents", () => {
5353 const result = events[1] as Extract<TurnEvent, { type: "tool-result" }>;
5454 expect(result.toolCallId).toBe("call-1");
5555 });
56+
57+ test("prefers tool-call over empty tool-input-start chunks", async () => {
58+ const events = await collectEvents([
59+ {
60+ type: "tool-input-start",
61+ toolCallId: "call-2",
62+ toolName: "shell",
63+ input: {},
64+ },
65+ {
66+ type: "tool-call",
67+ toolCallId: "call-2",
68+ toolName: "shell",
69+ input: { command: "ls -1" },
70+ },
71+ {
72+ type: "tool-result",
73+ toolCallId: "call-2",
74+ toolName: "shell",
75+ output: "ok",
76+ },
77+ ]);
78+
79+ expect(events).toHaveLength(2);
80+ expect(events[0]).toMatchObject({
81+ type: "tool-call-start",
82+ toolCallId: "call-2",
83+ toolName: "shell",
84+ args: { command: "ls -1" },
85+ });
86+ expect(events[1]).toMatchObject({
87+ type: "tool-result",
88+ toolCallId: "call-2",
89+ toolName: "shell",
90+ });
91+ });
5692 });
src/llm-api/turn-stream-events.test.ts+11−0
@@ -52,6 +52,17 @@ describe("mapStreamChunkToTurnEvent", () => {
5252 });
5353 });
5454
55+ test("ignores empty tool-input-start chunks", () => {
56+ expect(
57+ mapStreamChunkToTurnEvent({
58+ type: "tool-input-start",
59+ toolCallId: "id-empty",
60+ toolName: "shell",
61+ input: {},
62+ }),
63+ ).toBeNull();
64+ });
65+
5566 test("maps tool-result events from output/result and preserves isError", () => {
5667 expect(
5768 mapStreamChunkToTurnEvent({
src/llm-api/turn-stream-events.ts+25−6
@@ -11,6 +11,18 @@ export function shouldLogStreamChunk(c: StreamChunk): boolean {
1111 );
1212 }
1313
14+function extractToolArgs(c: StreamChunk): unknown {
15+ return c.input ?? c.args;
16+}
17+
18+function hasRenderableToolArgs(args: unknown): boolean {
19+ if (args === null || args === undefined) return false;
20+ if (typeof args === "string") return args.trim().length > 0;
21+ if (Array.isArray(args)) return args.length > 0;
22+ if (typeof args === "object") return Object.keys(args).length > 0;
23+ return true;
24+}
25+
1426 export function mapStreamChunkToTurnEvent(c: StreamChunk): TurnEvent | null {
1527 switch (c.type) {
1628 case "text-delta": {
@@ -29,15 +41,22 @@ export function mapStreamChunkToTurnEvent(c: StreamChunk): TurnEvent | null {
2941 delta,
3042 };
3143 }
32- case "tool-input-start":
44+ case "tool-input-start": {
45+ const args = extractToolArgs(c);
46+ if (!hasRenderableToolArgs(args)) return null;
47+ return {
48+ type: "tool-call-start",
49+ toolCallId: String(c.toolCallId ?? ""),
50+ toolName: String(c.toolName ?? ""),
51+ args,
52+ };
53+ }
3354 case "tool-call": {
34- const toolName = String(c.toolName ?? "");
35- const toolCallId = String(c.toolCallId ?? "");
3655 return {
3756 type: "tool-call-start",
38- toolCallId,
39- toolName,
40- args: c.input ?? c.args,
57+ toolCallId: String(c.toolCallId ?? ""),
58+ toolName: String(c.toolName ?? ""),
59+ args: extractToolArgs(c),
4160 };
4261 }
4362 case "tool-result": {