fix(ai): Ensure unique tool call IDs in Google provider

- Google provider sometimes returns duplicate or missing tool call IDs
- Added counter to ensure unique IDs for each tool call
- Check for duplicates and generate new ID when needed
- Fixes issues with multiple tool calls having the same ID
This commit is contained in:
Mario Zechner 2025-09-04 12:41:58 +02:00
parent 7d1daac39e
commit 6c3580828d

View file

@ -33,6 +33,9 @@ export interface GoogleOptions extends GenerateOptions {
};
}
// Counter for generating unique tool call IDs
let toolCallCounter = 0;
export const streamGoogle: GenerateFunction<"google-generative-ai"> = (
model: Model<"google-generative-ai">,
context: Context,
@ -131,7 +134,14 @@ export const streamGoogle: GenerateFunction<"google-generative-ai"> = (
currentBlock = null;
}
const toolCallId = part.functionCall.id || `${part.functionCall.name}_${Date.now()}`;
// Generate unique ID if not provided or if it's a duplicate
const providedId = part.functionCall.id;
const needsNewId =
!providedId || output.content.some((b) => b.type === "toolCall" && b.id === providedId);
const toolCallId = needsNewId
? `${part.functionCall.name}_${Date.now()}_${++toolCallCounter}`
: providedId;
const toolCall: ToolCall = {
type: "toolCall",
id: toolCallId,