Skip to content

Commit 19a9bb2

Browse files
committed
Handle AI SDK tool-call args
1 parent 87540b7 commit 19a9bb2

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

src/api/transform/__tests__/ai-sdk.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,24 @@ describe("AI SDK conversion utilities", () => {
441441
})
442442
})
443443

444+
it("processes complete tool-call chunks with args", () => {
445+
const part = {
446+
type: "tool-call" as const,
447+
toolCallId: "call_1",
448+
toolName: "attempt_completion",
449+
args: { result: "done" },
450+
}
451+
const chunks = [...processAiSdkStreamPart(part as any)]
452+
453+
expect(chunks).toHaveLength(1)
454+
expect(chunks[0]).toEqual({
455+
type: "tool_call",
456+
id: "call_1",
457+
name: "attempt_completion",
458+
arguments: '{"result":"done"}',
459+
})
460+
})
461+
444462
it("processes source chunks with URL", () => {
445463
const part = {
446464
type: "source" as const,

src/api/transform/ai-sdk.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,12 @@ export function convertToolsForAiSdk(
185185
*/
186186
type ExtendedStreamPart = TextStreamPart<any> | { type: "text"; text: string } | { type: "reasoning"; text: string }
187187

188+
function stringifyToolInput(part: Extract<TextStreamPart<any>, { type: "tool-call" }>) {
189+
const input = "input" in part ? part.input : "args" in part ? (part as { args: unknown }).args : {}
190+
191+
return typeof input === "string" ? input : JSON.stringify(input)
192+
}
193+
188194
/**
189195
* Process a single AI SDK stream part and yield the appropriate ApiStreamChunk(s).
190196
* This generator handles all TextStreamPart types and converts them to the
@@ -234,7 +240,7 @@ export function* processAiSdkStreamPart(part: ExtendedStreamPart): Generator<Api
234240
type: "tool_call",
235241
id: part.toolCallId,
236242
name: part.toolName,
237-
arguments: typeof part.input === "string" ? part.input : JSON.stringify(part.input),
243+
arguments: stringifyToolInput(part),
238244
}
239245
break
240246

0 commit comments

Comments
 (0)