Skip to content

Commit 8819ca1

Browse files
committed
Handle AI SDK tool-call args
1 parent 87540b7 commit 8819ca1

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,25 @@ 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+
input: undefined,
450+
args: { result: "done" },
451+
}
452+
const chunks = [...processAiSdkStreamPart(part as any)]
453+
454+
expect(chunks).toHaveLength(1)
455+
expect(chunks[0]).toEqual({
456+
type: "tool_call",
457+
id: "call_1",
458+
name: "attempt_completion",
459+
arguments: '{"result":"done"}',
460+
})
461+
})
462+
444463
it("processes source chunks with URL", () => {
445464
const part = {
446465
type: "source" as const,

src/api/transform/ai-sdk.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,17 @@ 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 =
190+
"input" in part && part.input !== undefined
191+
? part.input
192+
: "args" in part
193+
? (part as { args: unknown }).args
194+
: {}
195+
196+
return typeof input === "string" ? input : JSON.stringify(input)
197+
}
198+
188199
/**
189200
* Process a single AI SDK stream part and yield the appropriate ApiStreamChunk(s).
190201
* This generator handles all TextStreamPart types and converts them to the
@@ -234,7 +245,7 @@ export function* processAiSdkStreamPart(part: ExtendedStreamPart): Generator<Api
234245
type: "tool_call",
235246
id: part.toolCallId,
236247
name: part.toolName,
237-
arguments: typeof part.input === "string" ? part.input : JSON.stringify(part.input),
248+
arguments: stringifyToolInput(part),
238249
}
239250
break
240251

0 commit comments

Comments
 (0)