From b1414587afd2a3bfa48d861aa8b70c8c95875331 Mon Sep 17 00:00:00 2001 From: Kazuhiro Sera Date: Fri, 6 Jun 2025 19:33:06 +0900 Subject: [PATCH 1/3] Fix #34 ai-sdk integration fails to work with other platforms --- examples/ai-sdk/ai-sdk-model.ts | 23 ++++++++++----------- packages/agents-extensions/src/aiSdk.ts | 27 +++++++++---------------- 2 files changed, 21 insertions(+), 29 deletions(-) diff --git a/examples/ai-sdk/ai-sdk-model.ts b/examples/ai-sdk/ai-sdk-model.ts index 9f89ffa0..7c0cf785 100644 --- a/examples/ai-sdk/ai-sdk-model.ts +++ b/examples/ai-sdk/ai-sdk-model.ts @@ -10,27 +10,27 @@ const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); const getWeatherTool = tool({ name: 'get_weather', description: 'Get the weather for a given city', - parameters: z.object({ - demo: z.string(), - }), + parameters: z.object({ city: z.string() }), execute: async (input) => { await sleep(300); - return `The weather in ${input.demo} is sunny`; + return `The weather in ${input.city} is sunny`; }, }); -const dataAgentTwo = new Agent({ - name: 'Data agent', - instructions: 'You are a data agent', - handoffDescription: 'You know everything about the weather', +const dataAgent = new Agent({ + name: 'Weather Data Agent', + instructions: 'You are a weather data agent.', + handoffDescription: + 'When you are asked about the weather, you will use tools to get the weather.', tools: [getWeatherTool], model, // Using the AI SDK model for this agent }); const agent = new Agent({ - name: 'Basic test agent', - instructions: 'You are a basic agent', - handoffs: [dataAgentTwo], + name: 'Helpful Assistant', + instructions: + 'You are a helpful assistant. When you need to get the weather, you can hand off the task to the Weather Data Agent.', + handoffs: [dataAgent], }); async function main() { @@ -38,7 +38,6 @@ async function main() { agent, 'Hello what is the weather in San Francisco and oakland?', ); - console.log(result.finalOutput); } diff --git a/packages/agents-extensions/src/aiSdk.ts b/packages/agents-extensions/src/aiSdk.ts index 4592b150..2829ab46 100644 --- a/packages/agents-extensions/src/aiSdk.ts +++ b/packages/agents-extensions/src/aiSdk.ts @@ -5,7 +5,6 @@ import type { LanguageModelV1FunctionTool, LanguageModelV1Message, LanguageModelV1Prompt, - LanguageModelV1TextPart, LanguageModelV1ProviderDefinedTool, LanguageModelV1ToolCallPart, LanguageModelV1ToolResultPart, @@ -147,7 +146,7 @@ export function itemsToLanguageV1Messages( type: 'tool-call', toolCallId: item.callId, toolName: item.name, - args: item.arguments, + args: JSON.parse(item.arguments), }; currentAssistantMessage.content.push(content); } @@ -365,13 +364,8 @@ export class AiSdkModel implements Model { input = [ { role: 'system', - content: [ - { - type: 'text', - text: request.systemInstructions, - } as LanguageModelV1TextPart, - ], - } as unknown as LanguageModelV1Message, + content: request.systemInstructions, + }, ...input, ]; } @@ -428,7 +422,11 @@ export class AiSdkModel implements Model { }); }); - if (result.text) { + // Some of other platforms may return both tool calls and text. + // Putting a text message here will let the agent loop to complete, + // so adding this item only when the tool calls are empty. + // Note that the same support is not available for streaming mode. + if (!result.toolCalls && result.text) { output.push({ type: 'message', content: [{ type: 'output_text', text: result.text }], @@ -515,13 +513,8 @@ export class AiSdkModel implements Model { input = [ { role: 'system', - content: [ - { - type: 'text', - text: request.systemInstructions, - } as LanguageModelV1TextPart, - ], - } as unknown as LanguageModelV1Message, + content: request.systemInstructions, + }, ...input, ]; } From 768ba5d6ff2e802217aa01890f845441e1ccbd25 Mon Sep 17 00:00:00 2001 From: Kazuhiro Sera Date: Fri, 6 Jun 2025 19:34:44 +0900 Subject: [PATCH 2/3] Add changeset file --- .changeset/grumpy-symbols-shine.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/grumpy-symbols-shine.md diff --git a/.changeset/grumpy-symbols-shine.md b/.changeset/grumpy-symbols-shine.md new file mode 100644 index 00000000..d0523ed0 --- /dev/null +++ b/.changeset/grumpy-symbols-shine.md @@ -0,0 +1,5 @@ +--- +'@openai/agents-extensions': patch +--- + +Fix #34 by adjusting the internals of ai-sdk integration From 0608ef14bc7eab9eaf67bf3e626ecde4ecacd5eb Mon Sep 17 00:00:00 2001 From: Kazuhiro Sera Date: Fri, 6 Jun 2025 19:43:12 +0900 Subject: [PATCH 3/3] Fix tests --- packages/agents-extensions/test/aiSdk.test.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/agents-extensions/test/aiSdk.test.ts b/packages/agents-extensions/test/aiSdk.test.ts index 4e8bd91d..e5b90881 100644 --- a/packages/agents-extensions/test/aiSdk.test.ts +++ b/packages/agents-extensions/test/aiSdk.test.ts @@ -118,7 +118,7 @@ describe('itemsToLanguageV1Messages', () => { type: 'tool-call', toolCallId: '1', toolName: 'foo', - args: '{}', + args: {}, }, ], providerMetadata: { stub: { a: 1 } }, @@ -181,7 +181,7 @@ describe('itemsToLanguageV1Messages', () => { { role: 'assistant', content: [ - { type: 'tool-call', toolCallId: '1', toolName: 'do', args: '{}' }, + { type: 'tool-call', toolCallId: '1', toolName: 'do', args: {} }, ], providerMetadata: { stub: {} }, }, @@ -376,7 +376,7 @@ describe('AiSdkModel.getResponse', () => { toolCallType: 'function', toolCallId: 'c1', toolName: 'foo', - args: '{}', + args: {} as any, }, ], finishReason: 'stop', @@ -404,7 +404,7 @@ describe('AiSdkModel.getResponse', () => { type: 'function_call', callId: 'c1', name: 'foo', - arguments: '{}', + arguments: {}, status: 'completed', providerData: { p: 1 }, }, @@ -465,7 +465,7 @@ describe('AiSdkModel.getResponse', () => { expect(received[0]).toEqual({ role: 'system', - content: [{ type: 'text', text: 'inst' }], + content: 'inst', }); }); }); @@ -636,7 +636,7 @@ describe('AiSdkModel.getStreamedResponse', () => { expect(received[0]).toEqual({ role: 'system', - content: [{ type: 'text', text: 'inst' }], + content: 'inst', }); }); }); @@ -695,7 +695,7 @@ describe('AiSdkModel', () => { type: 'tool-call', toolCallId: 'call1', toolName: 'do', - args: '{}', + args: {}, }, ], providerMetadata: { fake: { meta: 1 } },