Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions extensions/cli/src/stream/processToolCallDelta.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { describe, expect, it } from "vitest";

import { processToolCallDelta } from "./streamChatResponse.helpers.js";

Check failure on line 3 in extensions/cli/src/stream/processToolCallDelta.test.ts

View workflow job for this annotation

GitHub Actions / lint

There should be at least one empty line between import groups
import { ToolCall } from "../tools/types.js";

Check failure on line 4 in extensions/cli/src/stream/processToolCallDelta.test.ts

View workflow job for this annotation

GitHub Actions / lint

`../tools/types.js` import should occur before import of `./streamChatResponse.helpers.js`

describe("processToolCallDelta", () => {
it("synthesizes a stable id when the first delta has index but no id", () => {
const toolCallsMap = new Map<string, ToolCall>();
const indexToIdMap = new Map<number, string>();

processToolCallDelta(
{
index: 0,
function: { name: "Read", arguments: '{"filepath":' },
},
toolCallsMap,
indexToIdMap,
);

expect(indexToIdMap.get(0)).toBe("call_0");
expect(toolCallsMap.has("call_0")).toBe(true);
expect(toolCallsMap.get("call_0")?.name).toBe("Read");
expect(toolCallsMap.get("call_0")?.argumentsStr).toBe('{"filepath":');

processToolCallDelta(
{
index: 0,
function: { arguments: '"convex/schema.ts"}' },
},
toolCallsMap,
indexToIdMap,
);

expect(toolCallsMap.size).toBe(1);
expect(toolCallsMap.get("call_0")?.arguments).toEqual({
filepath: "convex/schema.ts",
});
});

it("still prefers a real id when provided", () => {
const toolCallsMap = new Map<string, ToolCall>();
const indexToIdMap = new Map<number, string>();

processToolCallDelta(
{
index: 1,
id: "toolu_real",
function: { name: "Write", arguments: "{}" },
},
toolCallsMap,
indexToIdMap,
);

expect(indexToIdMap.get(1)).toBe("toolu_real");
expect(toolCallsMap.has("toolu_real")).toBe(true);
expect(toolCallsMap.has("call_1")).toBe(false);
});
});
8 changes: 7 additions & 1 deletion extensions/cli/src/stream/streamChatResponse.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,14 @@ export function processToolCallDelta(
indexToIdMap.set(toolCallDelta.index, toolCallId);
}
} else if (toolCallDelta.index !== undefined) {
// No ID, but we have an index - look up the ID from our map
// No ID, but we have an index - look up the ID from our map.
// Some providers (e.g. Ollama) send the first delta with index only.
// Synthesize a stable id so the call is not dropped before later fragments.
toolCallId = indexToIdMap.get(toolCallDelta.index);
if (!toolCallId) {
toolCallId = `call_${toolCallDelta.index}`;
indexToIdMap.set(toolCallDelta.index, toolCallId);
}
}

if (!toolCallId) {
Expand Down
Loading