feat: support gemini through ai sdk (#10671)
* add support for gemini in ai sdk * support thought signature conversion * capture extra_content in assistasnt tool calls * fix: update vercelStreamConverter tests for tool-call chunk emission The tool-call case now returns a chunk (for Gemini thoughtSignature support) instead of null, so tests need to expect the new behavior. --------- Co-authored-by: Nate Sesti <33237525+sestinj@users.noreply.github.com> Co-authored-by: Dallin Romney <dallinromney@gmail.com>
This commit is contained in:
parent
ceb3bbbd9f
commit
6e0045e204
10 changed files with 96 additions and 11 deletions
|
|
@ -51,6 +51,7 @@ export function convertToUnifiedMessage(
|
|||
name: tc.function?.name || "",
|
||||
arguments: tc.function?.arguments || "",
|
||||
},
|
||||
...(tc.extra_content && { extra_content: tc.extra_content }),
|
||||
}));
|
||||
}
|
||||
|
||||
|
|
@ -104,6 +105,7 @@ export function convertFromUnifiedMessage(
|
|||
name: tc.function?.name || "",
|
||||
arguments: tc.function?.arguments || "",
|
||||
},
|
||||
...(tc.extra_content && { extra_content: tc.extra_content }),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
|
@ -270,6 +272,7 @@ export function convertToUnifiedHistory(
|
|||
name: tc.function?.name || "",
|
||||
arguments: tc.function?.arguments || "",
|
||||
},
|
||||
...(tc.extra_content && { extra_content: tc.extra_content }),
|
||||
};
|
||||
pendingToolCalls.set(toolCall.id, toolCall);
|
||||
|
||||
|
|
|
|||
|
|
@ -87,6 +87,7 @@ export async function handleToolCalls(
|
|||
name: tc.name,
|
||||
arguments: JSON.stringify(tc.arguments),
|
||||
},
|
||||
...(tc.extra_content && { extra_content: tc.extra_content }),
|
||||
})),
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -241,6 +241,11 @@ export function processToolCallDelta(
|
|||
// JSON not complete yet, continue
|
||||
}
|
||||
}
|
||||
|
||||
// Capture extra_content
|
||||
if (toolCallDelta.extra_content) {
|
||||
toolCall.extra_content = toolCallDelta.extra_content;
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to detect provider from model name
|
||||
|
|
|
|||
|
|
@ -62,6 +62,7 @@ export interface ToolCall {
|
|||
arguments: any;
|
||||
argumentsStr: string;
|
||||
startNotified: boolean;
|
||||
extra_content?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface PreprocessedToolCall extends ToolCall {
|
||||
|
|
|
|||
17
packages/openai-adapters/package-lock.json
generated
17
packages/openai-adapters/package-lock.json
generated
|
|
@ -10,6 +10,7 @@
|
|||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"@ai-sdk/anthropic": "^3.0.44",
|
||||
"@ai-sdk/google": "^3.0.30",
|
||||
"@ai-sdk/deepseek": "^2.0.20",
|
||||
"@ai-sdk/openai": "^3.0.29",
|
||||
"@ai-sdk/xai": "^3.0.57",
|
||||
|
|
@ -96,6 +97,22 @@
|
|||
"zod": "^3.25.76 || ^4.1.8"
|
||||
}
|
||||
},
|
||||
"node_modules/@ai-sdk/google": {
|
||||
"version": "3.0.30",
|
||||
"resolved": "https://registry.npmjs.org/@ai-sdk/google/-/google-3.0.30.tgz",
|
||||
"integrity": "sha512-ZzG6dU0XUSSXbxQJJTQUFpWeKkfzdpR7IykEZwaiaW5d+3u3RZ/zkRiGwAOcUpLp6k0eMd+IJF4looJv21ecxw==",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"@ai-sdk/provider": "3.0.8",
|
||||
"@ai-sdk/provider-utils": "4.0.15"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"zod": "^3.25.76 || ^4.1.8"
|
||||
}
|
||||
},
|
||||
"node_modules/@ai-sdk/openai": {
|
||||
"version": "3.0.30",
|
||||
"resolved": "https://registry.npmjs.org/@ai-sdk/openai/-/openai-3.0.30.tgz",
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@
|
|||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"@ai-sdk/anthropic": "^3.0.44",
|
||||
"@ai-sdk/google": "^3.0.30",
|
||||
"@ai-sdk/deepseek": "^2.0.20",
|
||||
"@ai-sdk/openai": "^3.0.29",
|
||||
"@ai-sdk/xai": "^3.0.57",
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import { createAnthropic } from "@ai-sdk/anthropic";
|
||||
import { createGoogleGenerativeAI } from "@ai-sdk/google";
|
||||
import { createDeepSeek } from "@ai-sdk/deepseek";
|
||||
import { createOpenAI } from "@ai-sdk/openai";
|
||||
import { createXai } from "@ai-sdk/xai";
|
||||
|
|
@ -32,6 +33,7 @@ type AiSdkProviderCreator = (options: {
|
|||
const PROVIDER_MAP: Record<string, AiSdkProviderCreator> = {
|
||||
openai: createOpenAI,
|
||||
anthropic: createAnthropic,
|
||||
google: createGoogleGenerativeAI,
|
||||
xai: createXai,
|
||||
deepseek: createDeepSeek,
|
||||
openrouter: (options) =>
|
||||
|
|
|
|||
|
|
@ -79,11 +79,22 @@ export function convertOpenAIMessagesToVercel(
|
|||
} catch {
|
||||
input = tc.function.arguments;
|
||||
}
|
||||
|
||||
const thoughtSignature = (tc as any)?.extra_content?.google
|
||||
?.thought_signature as string | undefined;
|
||||
|
||||
contentParts.push({
|
||||
type: "tool-call",
|
||||
toolCallId: tc.id,
|
||||
toolName: tc.function.name,
|
||||
input,
|
||||
...(thoughtSignature && {
|
||||
providerOptions: {
|
||||
google: {
|
||||
thoughtSignature,
|
||||
},
|
||||
},
|
||||
}),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ describe("convertVercelStreamPart", () => {
|
|||
expect(result?.choices[0].delta.content).toBe("Let me think...");
|
||||
});
|
||||
|
||||
test("returns null for tool-call (handled by tool-input-start/delta)", () => {
|
||||
test("converts tool-call to chat chunk with tool_calls", () => {
|
||||
const part: VercelStreamPart = {
|
||||
type: "tool-call",
|
||||
toolCallId: "call_abc123",
|
||||
|
|
@ -47,7 +47,19 @@ describe("convertVercelStreamPart", () => {
|
|||
|
||||
const result = convertVercelStreamPart(part, options);
|
||||
|
||||
expect(result).toBeNull();
|
||||
expect(result).not.toBeNull();
|
||||
expect(result?.choices[0].delta.tool_calls).toHaveLength(1);
|
||||
expect(result?.choices[0].delta.tool_calls?.[0]).toEqual(
|
||||
expect.objectContaining({
|
||||
index: 0,
|
||||
id: "call_abc123",
|
||||
type: "function",
|
||||
function: {
|
||||
name: "readFile",
|
||||
arguments: '{"filepath":"/path/to/file"}',
|
||||
},
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
test("converts tool-input-delta to chat chunk", () => {
|
||||
|
|
@ -224,9 +236,9 @@ describe("convertVercelStream", () => {
|
|||
chunks.push(chunk);
|
||||
}
|
||||
|
||||
// Should get chunks for: text-delta (2), tool-input-start (1), tool-input-delta (1), finish (1) = 5
|
||||
// start-step, tool-input-end, tool-call, and finish-step are filtered out
|
||||
expect(chunks).toHaveLength(5);
|
||||
// Should get chunks for: text-delta (2), tool-input-start (1), tool-input-delta (1), tool-call (1), finish (1) = 6
|
||||
// start-step, tool-input-end, and finish-step are filtered out
|
||||
expect(chunks).toHaveLength(6);
|
||||
|
||||
expect(chunks[0].choices[0].delta.content).toBe("Hello ");
|
||||
expect(chunks[1].choices[0].delta.content).toBe("world");
|
||||
|
|
@ -237,7 +249,8 @@ describe("convertVercelStream", () => {
|
|||
expect(chunks[3].choices[0].delta.tool_calls?.[0].function?.arguments).toBe(
|
||||
'{"arg":"value"}',
|
||||
);
|
||||
expect(chunks[4].usage).toBeDefined();
|
||||
expect(chunks[4].choices[0].delta.tool_calls?.[0].id).toBe("call_1");
|
||||
expect(chunks[5].usage).toBeDefined();
|
||||
});
|
||||
|
||||
test("throws error when stream contains error event", async () => {
|
||||
|
|
|
|||
|
|
@ -19,6 +19,14 @@ export type VercelStreamPart =
|
|||
toolCallId: string;
|
||||
toolName: string;
|
||||
input: Record<string, unknown>;
|
||||
providerMetadata?: {
|
||||
google?: {
|
||||
thoughtSignature?: string;
|
||||
};
|
||||
vertex?: {
|
||||
thoughtSignature?: string;
|
||||
};
|
||||
};
|
||||
}
|
||||
| {
|
||||
type: "tool-input-start";
|
||||
|
|
@ -126,11 +134,34 @@ export function convertVercelStreamPart(
|
|||
model,
|
||||
});
|
||||
|
||||
case "tool-call":
|
||||
// tool-call is emitted after tool-input-start/delta/end have already
|
||||
// streamed the complete tool call. Emitting it again would duplicate
|
||||
// the arguments. Skip it since streaming events already handled it.
|
||||
return null;
|
||||
case "tool-call": {
|
||||
const thoughtSignature =
|
||||
part.providerMetadata?.google?.thoughtSignature ??
|
||||
part.providerMetadata?.vertex?.thoughtSignature;
|
||||
return chatChunkFromDelta({
|
||||
delta: {
|
||||
tool_calls: [
|
||||
{
|
||||
index: 0,
|
||||
id: part.toolCallId,
|
||||
type: "function" as const,
|
||||
function: {
|
||||
name: part.toolName,
|
||||
arguments: JSON.stringify(part.input),
|
||||
},
|
||||
...(thoughtSignature && {
|
||||
extra_content: {
|
||||
google: {
|
||||
thought_signature: thoughtSignature,
|
||||
},
|
||||
},
|
||||
}),
|
||||
} as any,
|
||||
],
|
||||
},
|
||||
model,
|
||||
});
|
||||
}
|
||||
|
||||
case "finish":
|
||||
if (part.totalUsage) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue