Skip to content

Commit 0f4850e

Browse files
authored
Fix #34 ai-sdk integration fails to work with other platforms (#38)
* Fix #34 ai-sdk integration fails to work with other platforms * Add changeset file * Fix tests
1 parent 826329b commit 0f4850e

File tree

4 files changed

+33
-36
lines changed

4 files changed

+33
-36
lines changed

.changeset/grumpy-symbols-shine.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@openai/agents-extensions': patch
3+
---
4+
5+
Fix #34 by adjusting the internals of ai-sdk integration

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
}

packages/agents-extensions/test/aiSdk.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ describe('itemsToLanguageV1Messages', () => {
118118
type: 'tool-call',
119119
toolCallId: '1',
120120
toolName: 'foo',
121-
args: '{}',
121+
args: {},
122122
},
123123
],
124124
providerMetadata: { stub: { a: 1 } },
@@ -181,7 +181,7 @@ describe('itemsToLanguageV1Messages', () => {
181181
{
182182
role: 'assistant',
183183
content: [
184-
{ type: 'tool-call', toolCallId: '1', toolName: 'do', args: '{}' },
184+
{ type: 'tool-call', toolCallId: '1', toolName: 'do', args: {} },
185185
],
186186
providerMetadata: { stub: {} },
187187
},
@@ -376,7 +376,7 @@ describe('AiSdkModel.getResponse', () => {
376376
toolCallType: 'function',
377377
toolCallId: 'c1',
378378
toolName: 'foo',
379-
args: '{}',
379+
args: {} as any,
380380
},
381381
],
382382
finishReason: 'stop',
@@ -404,7 +404,7 @@ describe('AiSdkModel.getResponse', () => {
404404
type: 'function_call',
405405
callId: 'c1',
406406
name: 'foo',
407-
arguments: '{}',
407+
arguments: {},
408408
status: 'completed',
409409
providerData: { p: 1 },
410410
},
@@ -465,7 +465,7 @@ describe('AiSdkModel.getResponse', () => {
465465

466466
expect(received[0]).toEqual({
467467
role: 'system',
468-
content: [{ type: 'text', text: 'inst' }],
468+
content: 'inst',
469469
});
470470
});
471471
});
@@ -636,7 +636,7 @@ describe('AiSdkModel.getStreamedResponse', () => {
636636

637637
expect(received[0]).toEqual({
638638
role: 'system',
639-
content: [{ type: 'text', text: 'inst' }],
639+
content: 'inst',
640640
});
641641
});
642642
});
@@ -695,7 +695,7 @@ describe('AiSdkModel', () => {
695695
type: 'tool-call',
696696
toolCallId: 'call1',
697697
toolName: 'do',
698-
args: '{}',
698+
args: {},
699699
},
700700
],
701701
providerMetadata: { fake: { meta: 1 } },

0 commit comments

Comments
 (0)