Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

fix(stream): emit structured textual tool calls

Dmitry Kuznetsovbb18a049e91b
diff --git a/open-sse/utils/stream.ts b/open-sse/utils/stream.ts
index afe8e26c61..b9eda7e997 100644
--- a/open-sse/utils/stream.ts
+++ b/open-sse/utils/stream.ts
@@ -249,11 +249,11 @@ function containsMalformedTextualToolCall(text: unknown): boolean {
function collectPassthroughTextualToolCall(
text: string,
toolCalls: Map<string, ToolCall>
-): boolean {
+): ToolCall | null {
const parsed = parseTextualToolCallFromContent(text);
- if (!parsed) return false;
+ if (!parsed) return null;
const key = `textual:${toolCalls.size}`;
- toolCalls.set(key, {
+ const toolCall: ToolCall = {
id: `call_${Date.now()}_${toolCalls.size}`,
index: toolCalls.size,
type: "function",
@@ -261,8 +261,21 @@ function collectPassthroughTextualToolCall(
name: parsed.name,
arguments: JSON.stringify(parsed.args || {}),
},
- });
- return true;
+ };
+ toolCalls.set(key, toolCall);
+ return toolCall;
+}
+
+function toStreamingToolCallDelta(toolCall: ToolCall) {
+ return {
+ index: toolCall.index,
+ id: toolCall.id,
+ type: toolCall.type,
+ function: {
+ name: toolCall.function.name,
+ arguments: toolCall.function.arguments,
+ },
+ };
}
function toStreamFailureStatus(value: unknown): number | null {
@@ -1384,11 +1397,17 @@ export function createSSEStream(options: StreamOptions = {}) {
) {
const parsedCandidate = parseTextualToolCallCandidate(bufferedCandidate);
if (parsedCandidate?.kind === "complete") {
- collectPassthroughTextualToolCall(bufferedCandidate, passthroughToolCalls);
+ const collectedToolCall = collectPassthroughTextualToolCall(
+ bufferedCandidate,
+ passthroughToolCalls
+ );
+ if (collectedToolCall) {
+ delta.tool_calls = [toStreamingToolCallDelta(collectedToolCall)];
+ }
passthroughHasToolCalls = true;
textualToolCallConverted = true;
passthroughBufferedTextualToolCallContent = "";
- delta.content = "";
+ delete delta.content;
} else if (parsedCandidate?.kind === "partial") {
passthroughBufferedTextualToolCallContent = appendBoundedText(
passthroughBufferedTextualToolCallContent,
diff --git a/tests/unit/stream-utils.test.ts b/tests/unit/stream-utils.test.ts
index c6d93d6774..1a35f19975 100644
--- a/tests/unit/stream-utils.test.ts
+++ b/tests/unit/stream-utils.test.ts
@@ -163,6 +163,9 @@ test("createSSEStream passthrough converts textual tool-call content into struct
);
assert.equal(onCompletePayload.status, 200);
+ assert.match(text, /"tool_calls":\[/);
+ assert.match(text, /"name":"terminal"/);
+ assert.doesNotMatch(text, /"content":"\[Tool call: terminal/);
const choice = onCompletePayload.responseBody.choices[0];
assert.equal(choice.finish_reason, "tool_calls");
assert.equal(choice.message.content, null);
@@ -216,6 +219,9 @@ test("createSSEStream passthrough converts split textual tool-call content at co
}
);
+ assert.match(text, /"tool_calls":\[/);
+ assert.match(text, /"name":"terminal"/);
+ assert.doesNotMatch(text, /"content":"\[Tool call: terminal/);
const choice = onCompletePayload.responseBody.choices[0];
assert.equal(choice.finish_reason, "tool_calls");
assert.equal(choice.message.content, null);