Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/grumpy-symbols-shine.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@openai/agents-extensions': patch
---

Fix #34 by adjusting the internals of ai-sdk integration
23 changes: 11 additions & 12 deletions examples/ai-sdk/ai-sdk-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,34 @@ 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() {
const result = await run(
agent,
'Hello what is the weather in San Francisco and oakland?',
);

console.log(result.finalOutput);
}

Expand Down
27 changes: 10 additions & 17 deletions packages/agents-extensions/src/aiSdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type {
LanguageModelV1FunctionTool,
LanguageModelV1Message,
LanguageModelV1Prompt,
LanguageModelV1TextPart,
LanguageModelV1ProviderDefinedTool,
LanguageModelV1ToolCallPart,
LanguageModelV1ToolResultPart,
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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,
];
}
Expand Down Expand Up @@ -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 }],
Expand Down Expand Up @@ -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,
];
}
Expand Down
14 changes: 7 additions & 7 deletions packages/agents-extensions/test/aiSdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe('itemsToLanguageV1Messages', () => {
type: 'tool-call',
toolCallId: '1',
toolName: 'foo',
args: '{}',
args: {},
},
],
providerMetadata: { stub: { a: 1 } },
Expand Down Expand Up @@ -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: {} },
},
Expand Down Expand Up @@ -376,7 +376,7 @@ describe('AiSdkModel.getResponse', () => {
toolCallType: 'function',
toolCallId: 'c1',
toolName: 'foo',
args: '{}',
args: {} as any,
},
],
finishReason: 'stop',
Expand Down Expand Up @@ -404,7 +404,7 @@ describe('AiSdkModel.getResponse', () => {
type: 'function_call',
callId: 'c1',
name: 'foo',
arguments: '{}',
arguments: {},
status: 'completed',
providerData: { p: 1 },
},
Expand Down Expand Up @@ -465,7 +465,7 @@ describe('AiSdkModel.getResponse', () => {

expect(received[0]).toEqual({
role: 'system',
content: [{ type: 'text', text: 'inst' }],
content: 'inst',
});
});
});
Expand Down Expand Up @@ -636,7 +636,7 @@ describe('AiSdkModel.getStreamedResponse', () => {

expect(received[0]).toEqual({
role: 'system',
content: [{ type: 'text', text: 'inst' }],
content: 'inst',
});
});
});
Expand Down Expand Up @@ -695,7 +695,7 @@ describe('AiSdkModel', () => {
type: 'tool-call',
toolCallId: 'call1',
toolName: 'do',
args: '{}',
args: {},
},
],
providerMetadata: { fake: { meta: 1 } },
Expand Down