Added support for Gemini models

This commit is contained in:
Badri Narayanan S
2025-12-27 14:09:20 +05:30
parent 9b7dcf3a6c
commit c1e1dbb0ef
13 changed files with 641 additions and 176 deletions

View File

@@ -15,7 +15,9 @@ import {
ANTIGRAVITY_HEADERS,
MAX_RETRIES,
MAX_WAIT_BEFORE_ERROR_MS,
MIN_SIGNATURE_LENGTH
MIN_SIGNATURE_LENGTH,
getModelFamily,
isThinkingModel
} from './constants.js';
import {
convertAnthropicToGoogle,
@@ -244,9 +246,10 @@ function buildHeaders(token, model, accept = 'application/json') {
...ANTIGRAVITY_HEADERS
};
// Add interleaved thinking header for Claude thinking models
const isThinkingModel = model.toLowerCase().includes('claude') && model.toLowerCase().includes('thinking');
if (isThinkingModel) {
const modelFamily = getModelFamily(model);
// Add interleaved thinking header only for Claude thinking models
if (modelFamily === 'claude' && isThinkingModel(model)) {
headers['anthropic-beta'] = 'interleaved-thinking-2025-05-14';
}
@@ -272,7 +275,7 @@ function buildHeaders(token, model, accept = 'application/json') {
*/
export async function sendMessage(anthropicRequest, accountManager) {
const model = anthropicRequest.model;
const isThinkingModel = model.toLowerCase().includes('claude') && model.toLowerCase().includes('thinking');
const isThinking = isThinkingModel(model);
// Retry loop with account failover
// Ensure we try at least as many times as there are accounts to cycle through everyone
@@ -330,13 +333,13 @@ export async function sendMessage(anthropicRequest, accountManager) {
let lastError = null;
for (const endpoint of ANTIGRAVITY_ENDPOINT_FALLBACKS) {
try {
const url = isThinkingModel
const url = isThinking
? `${endpoint}/v1internal:streamGenerateContent?alt=sse`
: `${endpoint}/v1internal:generateContent`;
const response = await fetch(url, {
method: 'POST',
headers: buildHeaders(token, model, isThinkingModel ? 'text/event-stream' : 'application/json'),
headers: buildHeaders(token, model, isThinking ? 'text/event-stream' : 'application/json'),
body: JSON.stringify(payload)
});
@@ -370,7 +373,7 @@ export async function sendMessage(anthropicRequest, accountManager) {
}
// For thinking models, parse SSE and accumulate all parts
if (isThinkingModel) {
if (isThinking) {
return await parseThinkingSSEResponse(response, anthropicRequest.model);
}
@@ -812,6 +815,10 @@ async function* streamSSEResponse(response, originalModel) {
} else if (part.functionCall) {
// Handle tool use
// For Gemini 3+, capture thoughtSignature from the functionCall part
// The signature is a sibling to functionCall, not inside it
const functionCallSignature = part.thoughtSignature || '';
if (currentBlockType === 'thinking' && currentThinkingSignature) {
yield {
type: 'content_block_delta',
@@ -829,15 +836,24 @@ async function* streamSSEResponse(response, originalModel) {
const toolId = part.functionCall.id || `toolu_${crypto.randomBytes(12).toString('hex')}`;
// For Gemini, include the thoughtSignature in the tool_use block
// so it can be sent back in subsequent requests
const toolUseBlock = {
type: 'tool_use',
id: toolId,
name: part.functionCall.name,
input: {}
};
// Store the signature in the tool_use block for later retrieval
if (functionCallSignature && functionCallSignature.length >= MIN_SIGNATURE_LENGTH) {
toolUseBlock.thoughtSignature = functionCallSignature;
}
yield {
type: 'content_block_start',
index: blockIndex,
content_block: {
type: 'tool_use',
id: toolId,
name: part.functionCall.name,
input: {}
}
content_block: toolUseBlock
};
yield {

View File

@@ -84,6 +84,40 @@ export const MAX_WAIT_BEFORE_ERROR_MS = 120000; // 2 minutes - throw error if wa
// Thinking model constants
export const MIN_SIGNATURE_LENGTH = 50; // Minimum valid thinking signature length
// Gemini-specific limits
export const GEMINI_MAX_OUTPUT_TOKENS = 16384;
/**
* Get the model family from model name (dynamic detection, no hardcoded list).
* @param {string} modelName - The model name from the request
* @returns {'claude' | 'gemini' | 'unknown'} The model family
*/
export function getModelFamily(modelName) {
const lower = (modelName || '').toLowerCase();
if (lower.includes('claude')) return 'claude';
if (lower.includes('gemini')) return 'gemini';
return 'unknown';
}
/**
* Check if a model supports thinking/reasoning output.
* @param {string} modelName - The model name from the request
* @returns {boolean} True if the model supports thinking blocks
*/
export function isThinkingModel(modelName) {
const lower = (modelName || '').toLowerCase();
// Claude thinking models have "thinking" in the name
if (lower.includes('claude') && lower.includes('thinking')) return true;
// Gemini thinking models: explicit "thinking" in name, OR gemini version 3+
if (lower.includes('gemini')) {
if (lower.includes('thinking')) return true;
// Check for gemini-3 or higher (e.g., gemini-3, gemini-3.5, gemini-4, etc.)
const versionMatch = lower.match(/gemini-(\d+)/);
if (versionMatch && parseInt(versionMatch[1], 10) >= 3) return true;
}
return false;
}
// Google OAuth configuration (from opencode-antigravity-auth)
export const OAUTH_CONFIG = {
clientId: '1071006060591-tmhssin2h21lcre235vtolojh4g403ep.apps.googleusercontent.com',
@@ -117,6 +151,9 @@ export default {
MAX_ACCOUNTS,
MAX_WAIT_BEFORE_ERROR_MS,
MIN_SIGNATURE_LENGTH,
GEMINI_MAX_OUTPUT_TOKENS,
getModelFamily,
isThinkingModel,
OAUTH_CONFIG,
OAUTH_REDIRECT_URI
};

View File

@@ -9,9 +9,20 @@
import crypto from 'crypto';
import {
MIN_SIGNATURE_LENGTH
MIN_SIGNATURE_LENGTH,
GEMINI_MAX_OUTPUT_TOKENS,
getModelFamily,
isThinkingModel
} from './constants.js';
/**
* Sentinel value to skip thought signature validation for Gemini models.
* Per Google documentation, this value can be used when Claude Code strips
* the thoughtSignature field from tool_use blocks in multi-turn requests.
* See: https://ai.google.dev/gemini-api/docs/thought-signatures
*/
const GEMINI_SKIP_SIGNATURE = 'skip_thought_signature_validator';
/**
* Check if a part is a thinking block
* @param {Object} part - Content part to check
@@ -272,7 +283,7 @@ export function reorderAssistantContent(content) {
/**
* Convert Anthropic message content to Google Generative AI parts
*/
function convertContentToParts(content, isClaudeModel = false) {
function convertContentToParts(content, isClaudeModel = false, isGeminiModel = false) {
if (typeof content === 'string') {
return [{ text: content }];
}
@@ -337,7 +348,19 @@ function convertContentToParts(content, isClaudeModel = false) {
functionCall.id = block.id;
}
parts.push({ functionCall });
// Build the part with functionCall
const part = { functionCall };
// For Gemini models, include thoughtSignature at the part level
// This is required by Gemini 3+ for tool calls to work correctly
if (isGeminiModel) {
// Use thoughtSignature from the block if Claude Code preserved it
// Otherwise, use the sentinel value to skip validation (Claude Code strips non-standard fields)
// See: https://ai.google.dev/gemini-api/docs/thought-signatures
part.thoughtSignature = block.thoughtSignature || GEMINI_SKIP_SIGNATURE;
}
parts.push(part);
} else if (block.type === 'tool_result') {
// Convert tool_result to functionResponse (Google format)
let responseContent = block.content;
@@ -400,8 +423,10 @@ function convertRole(role) {
export function convertAnthropicToGoogle(anthropicRequest) {
const { messages, system, max_tokens, temperature, top_p, top_k, stop_sequences, tools, tool_choice, thinking } = anthropicRequest;
const modelName = anthropicRequest.model || '';
const isClaudeModel = modelName.toLowerCase().includes('claude');
const isClaudeThinkingModel = isClaudeModel && modelName.toLowerCase().includes('thinking');
const modelFamily = getModelFamily(modelName);
const isClaudeModel = modelFamily === 'claude';
const isGeminiModel = modelFamily === 'gemini';
const isThinking = isThinkingModel(modelName);
const googleRequest = {
contents: [],
@@ -429,7 +454,7 @@ export function convertAnthropicToGoogle(anthropicRequest) {
}
// Add interleaved thinking hint for Claude thinking models with tools
if (isClaudeThinkingModel && tools && tools.length > 0) {
if (isClaudeModel && isThinking && tools && tools.length > 0) {
const hint = 'Interleaved thinking is enabled. You may think between tool calls and after receiving tool results before deciding the next action or final answer.';
if (!googleRequest.systemInstruction) {
googleRequest.systemInstruction = { parts: [{ text: hint }] };
@@ -458,7 +483,7 @@ export function convertAnthropicToGoogle(anthropicRequest) {
msgContent = reorderAssistantContent(msgContent);
}
const parts = convertContentToParts(msgContent, isClaudeModel);
const parts = convertContentToParts(msgContent, isClaudeModel, isGeminiModel);
const content = {
role: convertRole(msg.role),
parts: parts
@@ -488,22 +513,34 @@ export function convertAnthropicToGoogle(anthropicRequest) {
googleRequest.generationConfig.stopSequences = stop_sequences;
}
// Enable thinking for Claude thinking models
if (isClaudeThinkingModel) {
const thinkingConfig = {
include_thoughts: true
};
// Enable thinking for thinking models (Claude and Gemini 3+)
if (isThinking) {
if (isClaudeModel) {
// Claude thinking config
const thinkingConfig = {
include_thoughts: true
};
// Only set thinking_budget if explicitly provided
const thinkingBudget = thinking?.budget_tokens;
if (thinkingBudget) {
thinkingConfig.thinking_budget = thinkingBudget;
console.log('[FormatConverter] Thinking enabled with budget:', thinkingBudget);
} else {
console.log('[FormatConverter] Thinking enabled (no budget specified)');
// Only set thinking_budget if explicitly provided
const thinkingBudget = thinking?.budget_tokens;
if (thinkingBudget) {
thinkingConfig.thinking_budget = thinkingBudget;
console.log('[FormatConverter] Claude thinking enabled with budget:', thinkingBudget);
} else {
console.log('[FormatConverter] Claude thinking enabled (no budget specified)');
}
googleRequest.generationConfig.thinkingConfig = thinkingConfig;
} else if (isGeminiModel) {
// Gemini thinking config (uses camelCase)
const thinkingConfig = {
includeThoughts: true,
thinkingBudget: thinking?.budget_tokens || 16000
};
console.log('[FormatConverter] Gemini thinking enabled with budget:', thinkingConfig.thinkingBudget);
googleRequest.generationConfig.thinkingConfig = thinkingConfig;
}
googleRequest.generationConfig.thinkingConfig = thinkingConfig;
}
// Convert tools to Google format
@@ -523,10 +560,18 @@ export function convertAnthropicToGoogle(anthropicRequest) {
|| tool.parameters
|| { type: 'object' };
// Sanitize schema for general compatibility
let parameters = sanitizeSchema(schema);
// For Gemini models, apply additional cleaning for VALIDATED mode
if (isGeminiModel) {
parameters = cleanSchemaForGemini(parameters);
}
return {
name: String(name).replace(/[^a-zA-Z0-9_-]/g, '_').slice(0, 64),
description: description,
parameters: sanitizeSchema(schema)
parameters
};
});
@@ -534,6 +579,12 @@ export function convertAnthropicToGoogle(anthropicRequest) {
console.log('[FormatConverter] Tools:', JSON.stringify(googleRequest.tools).substring(0, 300));
}
// Cap max tokens for Gemini models
if (isGeminiModel && googleRequest.generationConfig.maxOutputTokens > GEMINI_MAX_OUTPUT_TOKENS) {
console.log(`[FormatConverter] Capping Gemini max_tokens from ${googleRequest.generationConfig.maxOutputTokens} to ${GEMINI_MAX_OUTPUT_TOKENS}`);
googleRequest.generationConfig.maxOutputTokens = GEMINI_MAX_OUTPUT_TOKENS;
}
return googleRequest;
}
@@ -620,6 +671,63 @@ function sanitizeSchema(schema) {
return sanitized;
}
/**
* Cleans JSON schema for Gemini API compatibility.
* Removes unsupported fields that cause VALIDATED mode errors.
*
* Gemini's VALIDATED mode rejects schemas with certain JSON Schema keywords
* that are not supported by the Gemini API.
*
* @param {Object} schema - The JSON schema to clean
* @returns {Object} Cleaned schema safe for Gemini API
*/
function cleanSchemaForGemini(schema) {
if (!schema || typeof schema !== 'object') return schema;
if (Array.isArray(schema)) return schema.map(cleanSchemaForGemini);
const result = { ...schema };
// Remove unsupported keywords that cause VALIDATED mode errors
const unsupported = [
'additionalProperties', 'default', '$schema', '$defs',
'definitions', '$ref', '$id', '$comment', 'title',
'minLength', 'maxLength', 'pattern', 'format',
'minItems', 'maxItems', 'examples'
];
for (const key of unsupported) {
delete result[key];
}
// Check for unsupported 'format' in string types
if (result.type === 'string' && result.format) {
const allowed = ['enum', 'date-time'];
if (!allowed.includes(result.format)) {
delete result.format;
}
}
// Recursively clean nested schemas
for (const [key, value] of Object.entries(result)) {
if (typeof value === 'object' && value !== null) {
result[key] = cleanSchemaForGemini(value);
}
}
// Validate that required array only contains properties that exist
// Gemini's VALIDATED mode requires this
if (result.required && Array.isArray(result.required) && result.properties) {
const definedProps = new Set(Object.keys(result.properties));
result.required = result.required.filter(prop => definedProps.has(prop));
// If required is now empty, remove it
if (result.required.length === 0) {
delete result.required;
}
}
return result;
}
/**
* Convert Google Generative AI response to Anthropic Messages API format
*
@@ -661,12 +769,20 @@ export function convertGoogleToAnthropic(googleResponse, model) {
} else if (part.functionCall) {
// Convert functionCall to tool_use
// Use the id from the response if available, otherwise generate one
anthropicContent.push({
const toolId = part.functionCall.id || `toolu_${crypto.randomBytes(12).toString('hex')}`;
const toolUseBlock = {
type: 'tool_use',
id: part.functionCall.id || `toolu_${crypto.randomBytes(12).toString('hex')}`,
id: toolId,
name: part.functionCall.name,
input: part.functionCall.args || {}
});
};
// For Gemini 3+, include thoughtSignature from the part level
if (part.thoughtSignature && part.thoughtSignature.length >= MIN_SIGNATURE_LENGTH) {
toolUseBlock.thoughtSignature = part.thoughtSignature;
}
anthropicContent.push(toolUseBlock);
hasToolCalls = true;
}
}