|
| 1 | +import type { FormatDescriptor, ToolOutput } from "../types" |
| 2 | +import { PRUNED_CONTENT_MESSAGE } from "../types" |
| 3 | +import type { PluginState } from "../../state" |
| 4 | +import type { Logger } from "../../logger" |
| 5 | +import type { ToolTracker } from "../../api-formats/synth-instruction" |
| 6 | +import { injectSynthGemini, trackNewToolResultsGemini } from "../../api-formats/synth-instruction" |
| 7 | +import { injectPrunableListGemini } from "../../api-formats/prunable-list" |
| 8 | + |
| 9 | +/** |
| 10 | + * Format descriptor for Google/Gemini API. |
| 11 | + * |
| 12 | + * Uses body.contents array with: |
| 13 | + * - parts[].functionCall for tool invocations |
| 14 | + * - parts[].functionResponse for tool results |
| 15 | + * |
| 16 | + * IMPORTANT: Gemini doesn't include tool call IDs in its native format. |
| 17 | + * We use position-based correlation via state.googleToolCallMapping which maps |
| 18 | + * "toolName:index" -> "toolCallId" (populated by hooks.ts from message events). |
| 19 | + */ |
| 20 | +export const geminiFormat: FormatDescriptor = { |
| 21 | + name: 'gemini', |
| 22 | + |
| 23 | + detect(body: any): boolean { |
| 24 | + return body.contents && Array.isArray(body.contents) |
| 25 | + }, |
| 26 | + |
| 27 | + getDataArray(body: any): any[] | undefined { |
| 28 | + return body.contents |
| 29 | + }, |
| 30 | + |
| 31 | + cacheToolParameters(_data: any[], _state: PluginState, _logger?: Logger): void { |
| 32 | + // Gemini format doesn't include tool parameters in the request body. |
| 33 | + // Tool parameters are captured via message events in hooks.ts and stored |
| 34 | + // in state.googleToolCallMapping for position-based correlation. |
| 35 | + // No-op here. |
| 36 | + }, |
| 37 | + |
| 38 | + injectSynth(data: any[], instruction: string, nudgeText: string): boolean { |
| 39 | + return injectSynthGemini(data, instruction, nudgeText) |
| 40 | + }, |
| 41 | + |
| 42 | + trackNewToolResults(data: any[], tracker: ToolTracker, protectedTools: Set<string>): number { |
| 43 | + return trackNewToolResultsGemini(data, tracker, protectedTools) |
| 44 | + }, |
| 45 | + |
| 46 | + injectPrunableList(data: any[], injection: string): boolean { |
| 47 | + return injectPrunableListGemini(data, injection) |
| 48 | + }, |
| 49 | + |
| 50 | + extractToolOutputs(data: any[], state: PluginState): ToolOutput[] { |
| 51 | + const outputs: ToolOutput[] = [] |
| 52 | + |
| 53 | + // We need the position mapping to correlate functionResponses to tool call IDs |
| 54 | + // Find the mapping from any active session |
| 55 | + let positionMapping: Map<string, string> | undefined |
| 56 | + for (const [_sessionId, mapping] of state.googleToolCallMapping) { |
| 57 | + if (mapping && mapping.size > 0) { |
| 58 | + positionMapping = mapping |
| 59 | + break |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + if (!positionMapping) { |
| 64 | + return outputs |
| 65 | + } |
| 66 | + |
| 67 | + // Track position counters per tool name |
| 68 | + const toolPositionCounters = new Map<string, number>() |
| 69 | + |
| 70 | + for (const content of data) { |
| 71 | + if (!Array.isArray(content.parts)) continue |
| 72 | + |
| 73 | + for (const part of content.parts) { |
| 74 | + if (part.functionResponse) { |
| 75 | + const funcName = part.functionResponse.name?.toLowerCase() |
| 76 | + if (funcName) { |
| 77 | + const currentIndex = toolPositionCounters.get(funcName) || 0 |
| 78 | + toolPositionCounters.set(funcName, currentIndex + 1) |
| 79 | + |
| 80 | + const positionKey = `${funcName}:${currentIndex}` |
| 81 | + const toolCallId = positionMapping.get(positionKey) |
| 82 | + |
| 83 | + if (toolCallId) { |
| 84 | + outputs.push({ |
| 85 | + id: toolCallId.toLowerCase(), |
| 86 | + toolName: funcName |
| 87 | + }) |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return outputs |
| 95 | + }, |
| 96 | + |
| 97 | + replaceToolOutput(data: any[], toolId: string, prunedMessage: string, state: PluginState): boolean { |
| 98 | + // Find the position mapping |
| 99 | + let positionMapping: Map<string, string> | undefined |
| 100 | + for (const [_sessionId, mapping] of state.googleToolCallMapping) { |
| 101 | + if (mapping && mapping.size > 0) { |
| 102 | + positionMapping = mapping |
| 103 | + break |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + if (!positionMapping) { |
| 108 | + return false |
| 109 | + } |
| 110 | + |
| 111 | + const toolIdLower = toolId.toLowerCase() |
| 112 | + const toolPositionCounters = new Map<string, number>() |
| 113 | + let replaced = false |
| 114 | + |
| 115 | + for (let i = 0; i < data.length; i++) { |
| 116 | + const content = data[i] |
| 117 | + if (!Array.isArray(content.parts)) continue |
| 118 | + |
| 119 | + let contentModified = false |
| 120 | + const newParts = content.parts.map((part: any) => { |
| 121 | + if (part.functionResponse) { |
| 122 | + const funcName = part.functionResponse.name?.toLowerCase() |
| 123 | + if (funcName) { |
| 124 | + const currentIndex = toolPositionCounters.get(funcName) || 0 |
| 125 | + toolPositionCounters.set(funcName, currentIndex + 1) |
| 126 | + |
| 127 | + const positionKey = `${funcName}:${currentIndex}` |
| 128 | + const mappedToolId = positionMapping!.get(positionKey) |
| 129 | + |
| 130 | + if (mappedToolId?.toLowerCase() === toolIdLower) { |
| 131 | + contentModified = true |
| 132 | + replaced = true |
| 133 | + // Preserve thoughtSignature if present (required for Gemini 3 Pro) |
| 134 | + return { |
| 135 | + ...part, |
| 136 | + functionResponse: { |
| 137 | + ...part.functionResponse, |
| 138 | + response: { |
| 139 | + name: part.functionResponse.name, |
| 140 | + content: prunedMessage |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + return part |
| 148 | + }) |
| 149 | + |
| 150 | + if (contentModified) { |
| 151 | + data[i] = { ...content, parts: newParts } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return replaced |
| 156 | + }, |
| 157 | + |
| 158 | + hasToolOutputs(data: any[]): boolean { |
| 159 | + return data.some((content: any) => |
| 160 | + Array.isArray(content.parts) && |
| 161 | + content.parts.some((part: any) => part.functionResponse) |
| 162 | + ) |
| 163 | + }, |
| 164 | + |
| 165 | + getLogMetadata(data: any[], replacedCount: number, inputUrl: string): Record<string, any> { |
| 166 | + return { |
| 167 | + url: inputUrl, |
| 168 | + replacedCount, |
| 169 | + totalContents: data.length, |
| 170 | + format: 'google-gemini' |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments