Skip to content

Commit b141458

Browse files
committed
Fix #34 ai-sdk integration fails to work with other platforms
1 parent be52a88 commit b141458

File tree

2 files changed

+21
-29
lines changed

2 files changed

+21
-29
lines changed

examples/ai-sdk/ai-sdk-model.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,34 @@ const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
1010
const getWeatherTool = tool({
1111
name: 'get_weather',
1212
description: 'Get the weather for a given city',
13-
parameters: z.object({
14-
demo: z.string(),
15-
}),
13+
parameters: z.object({ city: z.string() }),
1614
execute: async (input) => {
1715
await sleep(300);
18-
return `The weather in ${input.demo} is sunny`;
16+
return `The weather in ${input.city} is sunny`;
1917
},
2018
});
2119

22-
const dataAgentTwo = new Agent({
23-
name: 'Data agent',
24-
instructions: 'You are a data agent',
25-
handoffDescription: 'You know everything about the weather',
20+
const dataAgent = new Agent({
21+
name: 'Weather Data Agent',
22+
instructions: 'You are a weather data agent.',
23+
handoffDescription:
24+
'When you are asked about the weather, you will use tools to get the weather.',
2625
tools: [getWeatherTool],
2726
model, // Using the AI SDK model for this agent
2827
});
2928

3029
const agent = new Agent({
31-
name: 'Basic test agent',
32-
instructions: 'You are a basic agent',
33-
handoffs: [dataAgentTwo],
30+
name: 'Helpful Assistant',
31+
instructions:
32+
'You are a helpful assistant. When you need to get the weather, you can hand off the task to the Weather Data Agent.',
33+
handoffs: [dataAgent],
3434
});
3535

3636
async function main() {
3737
const result = await run(
3838
agent,
3939
'Hello what is the weather in San Francisco and oakland?',
4040
);
41-
4241
console.log(result.finalOutput);
4342
}
4443

packages/agents-extensions/src/aiSdk.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import type {
55
LanguageModelV1FunctionTool,
66
LanguageModelV1Message,
77
LanguageModelV1Prompt,
8-
LanguageModelV1TextPart,
98
LanguageModelV1ProviderDefinedTool,
109
LanguageModelV1ToolCallPart,
1110
LanguageModelV1ToolResultPart,
@@ -147,7 +146,7 @@ export function itemsToLanguageV1Messages(
147146
type: 'tool-call',
148147
toolCallId: item.callId,
149148
toolName: item.name,
150-
args: item.arguments,
149+
args: JSON.parse(item.arguments),
151150
};
152151
currentAssistantMessage.content.push(content);
153152
}
@@ -365,13 +364,8 @@ export class AiSdkModel implements Model {
365364
input = [
366365
{
367366
role: 'system',
368-
content: [
369-
{
370-
type: 'text',
371-
text: request.systemInstructions,
372-
} as LanguageModelV1TextPart,
373-
],
374-
} as unknown as LanguageModelV1Message,
367+
content: request.systemInstructions,
368+
},
375369
...input,
376370
];
377371
}
@@ -428,7 +422,11 @@ export class AiSdkModel implements Model {
428422
});
429423
});
430424

431-
if (result.text) {
425+
// Some of other platforms may return both tool calls and text.
426+
// Putting a text message here will let the agent loop to complete,
427+
// so adding this item only when the tool calls are empty.
428+
// Note that the same support is not available for streaming mode.
429+
if (!result.toolCalls && result.text) {
432430
output.push({
433431
type: 'message',
434432
content: [{ type: 'output_text', text: result.text }],
@@ -515,13 +513,8 @@ export class AiSdkModel implements Model {
515513
input = [
516514
{
517515
role: 'system',
518-
content: [
519-
{
520-
type: 'text',
521-
text: request.systemInstructions,
522-
} as LanguageModelV1TextPart,
523-
],
524-
} as unknown as LanguageModelV1Message,
516+
content: request.systemInstructions,
517+
},
525518
...input,
526519
];
527520
}

0 commit comments

Comments
 (0)