refactor(ai): Simplify Google provider with cleaner block handling

This commit is contained in:
Mario Zechner 2025-08-31 20:31:08 +02:00
parent f29752ac82
commit 7c8cdacc09

View file

@ -16,6 +16,8 @@ import type {
Message, Message,
Model, Model,
StopReason, StopReason,
TextContent,
ThinkingContent,
Tool, Tool,
ToolCall, ToolCall,
Usage, Usage,
@ -96,10 +98,8 @@ export class GoogleLLM implements LLM<GoogleLLMOptions> {
const stream = await this.client.models.generateContentStream(params); const stream = await this.client.models.generateContentStream(params);
let content = ""; const blocks: AssistantMessage["content"] = [];
let thinking = ""; let currentBlock: TextContent | ThinkingContent | null = null;
let thoughtSignature: string | undefined;
const toolCalls: ToolCall[] = [];
let usage: Usage = { let usage: Usage = {
input: 0, input: 0,
output: 0, output: 0,
@ -108,8 +108,6 @@ export class GoogleLLM implements LLM<GoogleLLMOptions> {
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
}; };
let stopReason: StopReason = "stop"; let stopReason: StopReason = "stop";
let inTextBlock = false;
let inThinkingBlock = false;
// Process the stream // Process the stream
for await (const chunk of stream) { for await (const chunk of stream) {
@ -117,52 +115,69 @@ export class GoogleLLM implements LLM<GoogleLLMOptions> {
const candidate = chunk.candidates?.[0]; const candidate = chunk.candidates?.[0];
if (candidate?.content?.parts) { if (candidate?.content?.parts) {
for (const part of candidate.content.parts) { for (const part of candidate.content.parts) {
// Cast to any to access thinking properties not yet in SDK types if (part.text !== undefined) {
const partWithThinking = part; const isThinking = part.thought === true;
if (partWithThinking.text !== undefined) {
// Check if it's thinking content using the thought boolean flag // Check if we need to switch blocks
if (partWithThinking.thought === true) { if (
if (inTextBlock) { !currentBlock ||
options?.onText?.("", true); (isThinking && currentBlock.type !== "thinking") ||
inTextBlock = false; (!isThinking && currentBlock.type !== "text")
} ) {
thinking += partWithThinking.text; // Save and finalize current block
options?.onThinking?.(partWithThinking.text, false); if (currentBlock) {
inThinkingBlock = true; if (currentBlock.type === "text") {
// Capture thought signature if present options?.onEvent?.({ type: "text_end", content: currentBlock.text });
if (partWithThinking.thoughtSignature) {
thoughtSignature = partWithThinking.thoughtSignature;
}
} else { } else {
if (inThinkingBlock) { options?.onEvent?.({ type: "thinking_end", content: currentBlock.thinking });
options?.onThinking?.("", true);
inThinkingBlock = false;
} }
content += partWithThinking.text; blocks.push(currentBlock);
options?.onText?.(partWithThinking.text, false); }
inTextBlock = true;
// Start new block
if (isThinking) {
currentBlock = { type: "thinking", thinking: "", thinkingSignature: undefined };
options?.onEvent?.({ type: "thinking_start" });
} else {
currentBlock = { type: "text", text: "" };
options?.onEvent?.({ type: "text_start" });
}
}
// Append content to current block
if (currentBlock.type === "thinking") {
currentBlock.thinking += part.text;
currentBlock.thinkingSignature = part.thoughtSignature;
options?.onEvent?.({type: "thinking_delta", content: currentBlock.thinking, delta: part.text });
} else {
currentBlock.text += part.text;
options?.onEvent?.({ type: "text_delta", content: currentBlock.text, delta: part.text });
} }
} }
// Handle function calls // Handle function calls
if (part.functionCall) { if (part.functionCall) {
if (inTextBlock) { // Save current block if exists
options?.onText?.("", true); if (currentBlock) {
inTextBlock = false; if (currentBlock.type === "text") {
options?.onEvent?.({ type: "text_end", content: currentBlock.text });
} else {
options?.onEvent?.({ type: "thinking_end", content: currentBlock.thinking });
} }
if (inThinkingBlock) { blocks.push(currentBlock);
options?.onThinking?.("", true); currentBlock = null;
inThinkingBlock = false;
} }
// Gemini doesn't provide tool call IDs, so we need to generate them // Add tool call
// Use the function name as part of the ID for better debugging const toolCallId = part.functionCall.id || `${part.functionCall.name}_${Date.now()}`;
const toolCallId = `${part.functionCall.name}_${Date.now()}`; const toolCall: ToolCall = {
toolCalls.push({ type: "toolCall",
id: toolCallId, id: toolCallId,
name: part.functionCall.name || "", name: part.functionCall.name || "",
arguments: part.functionCall.args as Record<string, any>, arguments: part.functionCall.args as Record<string, any>,
}); };
blocks.push(toolCall);
options?.onEvent?.({ type: "toolCall", toolCall });
} }
} }
} }
@ -170,7 +185,8 @@ export class GoogleLLM implements LLM<GoogleLLMOptions> {
// Map finish reason // Map finish reason
if (candidate?.finishReason) { if (candidate?.finishReason) {
stopReason = this.mapStopReason(candidate.finishReason); stopReason = this.mapStopReason(candidate.finishReason);
if (toolCalls.length > 0) { // Check if we have tool calls in blocks
if (blocks.some((b) => b.type === "toolCall")) {
stopReason = "toolUse"; stopReason = "toolUse";
} }
} }
@ -194,46 +210,32 @@ export class GoogleLLM implements LLM<GoogleLLMOptions> {
} }
} }
// Signal end of blocks // Save final block if exists
if (inTextBlock) { if (currentBlock) {
options?.onText?.("", true); if (currentBlock.type === "text") {
options?.onEvent?.({ type: "text_end", content: currentBlock.text });
} else {
options?.onEvent?.({ type: "thinking_end", content: currentBlock.thinking });
} }
if (inThinkingBlock) { blocks.push(currentBlock);
options?.onThinking?.("", true);
} }
// Generate a thinking signature if we have thinking content but no signature from API
// This is needed for proper multi-turn conversations with thinking
if (thinking && !thoughtSignature) {
// Create a base64-encoded signature as Gemini expects
// In production, Gemini API should provide this
const encoder = new TextEncoder();
const data = encoder.encode(thinking);
// Create a simple hash-like signature and encode to base64
const signature = `gemini_thinking_${data.length}_${Date.now()}`;
thoughtSignature = Buffer.from(signature).toString("base64");
}
// Calculate cost
calculateCost(this.model, usage); calculateCost(this.model, usage);
// Usage metadata is in the last chunk const output = {
// Already captured during streaming
return {
role: "assistant", role: "assistant",
content: content || undefined, content: blocks,
thinking: thinking || undefined,
thinkingSignature: thoughtSignature,
toolCalls: toolCalls.length > 0 ? toolCalls : undefined,
provider: this.model.provider, provider: this.model.provider,
model: this.model.id, model: this.model.id,
usage, usage,
stopReason, stopReason,
}; } satisfies AssistantMessage;
options?.onEvent?.({ type: "done", reason: stopReason, message: output });
return output;
} catch (error) { } catch (error) {
return { const output = {
role: "assistant", role: "assistant",
content: [],
provider: this.model.provider, provider: this.model.provider,
model: this.model.id, model: this.model.id,
usage: { usage: {
@ -244,8 +246,10 @@ export class GoogleLLM implements LLM<GoogleLLMOptions> {
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
}, },
stopReason: "error", stopReason: "error",
error: error instanceof Error ? error.message : String(error), error: error instanceof Error ? error.message : JSON.stringify(error),
}; } satisfies AssistantMessage;
options?.onEvent?.({ type: "error", error: output.error });
return output;
} }
} }
@ -283,28 +287,23 @@ export class GoogleLLM implements LLM<GoogleLLMOptions> {
} else if (msg.role === "assistant") { } else if (msg.role === "assistant") {
const parts: Part[] = []; const parts: Part[] = [];
// Add thinking if present // Process content blocks
// Note: We include thinkingSignature in our response for multi-turn context, for (const block of msg.content) {
// but don't send it back to Gemini API as it may cause errors if (block.type === "text") {
if (msg.thinking) { parts.push({ text: block.text });
parts.push({ } else if (block.type === "thinking") {
text: msg.thinking, const thinkingPart: Part = {
thought: true, thought: true,
// Don't include thoughtSignature when sending back to API thoughtSignature: block.thinkingSignature,
// thoughtSignature: msg.thinkingSignature, text: block.thinking,
}); };
} parts.push(thinkingPart);
} else if (block.type === "toolCall") {
if (msg.content) {
parts.push({ text: msg.content });
}
if (msg.toolCalls) {
for (const toolCall of msg.toolCalls) {
parts.push({ parts.push({
functionCall: { functionCall: {
name: toolCall.name, id: block.id,
args: toolCall.arguments, name: block.name,
args: block.arguments,
}, },
}); });
} }
@ -317,18 +316,16 @@ export class GoogleLLM implements LLM<GoogleLLMOptions> {
}); });
} }
} else if (msg.role === "toolResult") { } else if (msg.role === "toolResult") {
// Tool results are sent as function responses
// Extract function name from the tool call ID (format: "functionName_timestamp")
const functionName = msg.toolCallId.substring(0, msg.toolCallId.lastIndexOf("_"));
contents.push({ contents.push({
role: "user", role: "user",
parts: [ parts: [
{ {
functionResponse: { functionResponse: {
name: functionName, id: msg.toolCallId,
name: msg.toolName,
response: { response: {
result: msg.content, result: msg.content,
isError: msg.isError || false, isError: msg.isError,
}, },
}, },
}, },