|
|
|
|
|
import config from '../../config/config.js'; |
|
|
import { generateRequestId } from '../idGenerator.js'; |
|
|
import { getReasoningSignature, getToolSignature } from '../thoughtSignatureCache.js'; |
|
|
import { setToolNameMapping } from '../toolNameCache.js'; |
|
|
import { getThoughtSignatureForModel, getToolSignatureForModel, sanitizeToolName, modelMapping, isEnableThinking, generateGenerationConfig } from '../utils.js'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getSignatureContext(sessionId, actualModelName) { |
|
|
const cachedReasoningSig = getReasoningSignature(sessionId, actualModelName); |
|
|
const cachedToolSig = getToolSignature(sessionId, actualModelName); |
|
|
|
|
|
return { |
|
|
reasoningSignature: cachedReasoningSig || getThoughtSignatureForModel(actualModelName), |
|
|
toolSignature: cachedToolSig || getToolSignatureForModel(actualModelName) |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function pushUserMessage(extracted, antigravityMessages) { |
|
|
antigravityMessages.push({ |
|
|
role: 'user', |
|
|
parts: [{ text: extracted.text }, ...extracted.images] |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function findFunctionNameById(toolCallId, antigravityMessages) { |
|
|
for (let i = antigravityMessages.length - 1; i >= 0; i--) { |
|
|
if (antigravityMessages[i].role === 'model') { |
|
|
const parts = antigravityMessages[i].parts; |
|
|
for (const part of parts) { |
|
|
if (part.functionCall && part.functionCall.id === toolCallId) { |
|
|
return part.functionCall.name; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
return ''; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function pushFunctionResponse(toolCallId, functionName, resultContent, antigravityMessages) { |
|
|
const lastMessage = antigravityMessages[antigravityMessages.length - 1]; |
|
|
const functionResponse = { |
|
|
functionResponse: { |
|
|
id: toolCallId, |
|
|
name: functionName, |
|
|
response: { output: resultContent } |
|
|
} |
|
|
}; |
|
|
|
|
|
if (lastMessage?.role === 'user' && lastMessage.parts.some(p => p.functionResponse)) { |
|
|
lastMessage.parts.push(functionResponse); |
|
|
} else { |
|
|
antigravityMessages.push({ role: 'user', parts: [functionResponse] }); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function createThoughtPart(text) { |
|
|
return { text: text || ' ', thought: true } |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function createFunctionCallPart(id, name, args, signature = null) { |
|
|
const part = { |
|
|
functionCall: { |
|
|
id, |
|
|
name, |
|
|
args: typeof args === 'string' ? { query: args } : args |
|
|
} |
|
|
}; |
|
|
if (signature) { |
|
|
part.thoughtSignature = signature; |
|
|
} |
|
|
return part; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function processToolName(originalName, sessionId, actualModelName) { |
|
|
const safeName = sanitizeToolName(originalName); |
|
|
if (sessionId && actualModelName && safeName !== originalName) { |
|
|
setToolNameMapping(sessionId, actualModelName, safeName, originalName); |
|
|
} |
|
|
return safeName; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function pushModelMessage({ parts, toolCalls, hasContent }, antigravityMessages) { |
|
|
const lastMessage = antigravityMessages[antigravityMessages.length - 1]; |
|
|
const hasToolCalls = toolCalls && toolCalls.length > 0; |
|
|
|
|
|
if (lastMessage?.role === 'model' && hasToolCalls && !hasContent) { |
|
|
lastMessage.parts.push(...toolCalls); |
|
|
} else { |
|
|
const allParts = [...parts, ...(toolCalls || [])]; |
|
|
antigravityMessages.push({ role: 'model', parts: allParts }); |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function buildRequestBody({ contents, tools, generationConfig, sessionId, systemInstruction }, token, actualModelName) { |
|
|
const requestBody = { |
|
|
project: token.projectId, |
|
|
requestId: generateRequestId(), |
|
|
request: { |
|
|
contents, |
|
|
tools: tools || [], |
|
|
toolConfig: { functionCallingConfig: { mode: 'VALIDATED' } }, |
|
|
generationConfig, |
|
|
sessionId |
|
|
}, |
|
|
model: actualModelName, |
|
|
userAgent: 'antigravity' |
|
|
}; |
|
|
|
|
|
if (systemInstruction) { |
|
|
requestBody.request.systemInstruction = { |
|
|
role: 'user', |
|
|
parts: [{ text: systemInstruction }] |
|
|
}; |
|
|
} |
|
|
|
|
|
return requestBody; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function mergeSystemInstruction(baseSystem, contextSystem) { |
|
|
if (!config.useContextSystemPrompt || !contextSystem) { |
|
|
return baseSystem || ''; |
|
|
} |
|
|
|
|
|
const parts = []; |
|
|
if (baseSystem && typeof baseSystem === 'string' && baseSystem.trim()) parts.push(baseSystem.trim()); |
|
|
if (contextSystem && typeof contextSystem === 'string' && contextSystem.trim()) parts.push(contextSystem.trim()); |
|
|
return parts.join('\n\n'); |
|
|
} |
|
|
|
|
|
|
|
|
export { sanitizeToolName, modelMapping, isEnableThinking, generateGenerationConfig }; |
|
|
|
|
|
|
|
|
export { |
|
|
normalizeOpenAIParameters, |
|
|
normalizeClaudeParameters, |
|
|
normalizeGeminiParameters, |
|
|
normalizeParameters, |
|
|
toGenerationConfig |
|
|
} from '../parameterNormalizer.js'; |
|
|
|