-
-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathgemini.ts
More file actions
180 lines (151 loc) · 6.32 KB
/
gemini.ts
File metadata and controls
180 lines (151 loc) · 6.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import type { FormatDescriptor, ToolOutput } from "../types"
import type { PluginState } from "../../state"
function isNudgeContent(content: any, nudgeText: string): boolean {
if (Array.isArray(content.parts) && content.parts.length === 1) {
const part = content.parts[0]
return part?.text === nudgeText
}
return false
}
function injectSynth(contents: any[], instruction: string, nudgeText: string, systemReminder: string): boolean {
const fullInstruction = systemReminder + '\n\n' + instruction
for (let i = contents.length - 1; i >= 0; i--) {
const content = contents[i]
if (content.role === 'user' && Array.isArray(content.parts)) {
if (isNudgeContent(content, nudgeText)) continue
const alreadyInjected = content.parts.some(
(part: any) => part?.text && typeof part.text === 'string' && part.text.includes(instruction)
)
if (alreadyInjected) return false
content.parts.push({ text: fullInstruction })
return true
}
}
return false
}
function injectPrunableList(contents: any[], injection: string): boolean {
if (!injection) return false
contents.push({ role: 'user', parts: [{ text: injection }] })
return true
}
/**
* Gemini doesn't include tool call IDs in its native format.
* We use position-based correlation via state.googleToolCallMapping which maps
* "toolName:index" -> "toolCallId" (populated by hooks.ts from message events).
*/
export const geminiFormat: FormatDescriptor = {
name: 'gemini',
detect(body: any): boolean {
return body.contents && Array.isArray(body.contents)
},
getDataArray(body: any): any[] | undefined {
return body.contents
},
injectSynth(data: any[], instruction: string, nudgeText: string, systemReminder: string): boolean {
return injectSynth(data, instruction, nudgeText, systemReminder)
},
injectPrunableList(data: any[], injection: string): boolean {
return injectPrunableList(data, injection)
},
extractToolOutputs(data: any[], state: PluginState): ToolOutput[] {
const outputs: ToolOutput[] = []
let positionMapping: Map<string, string> | undefined
for (const [_sessionId, mapping] of state.googleToolCallMapping) {
if (mapping && mapping.size > 0) {
positionMapping = mapping
break
}
}
if (!positionMapping) {
return outputs
}
const toolPositionCounters = new Map<string, number>()
for (const content of data) {
if (!Array.isArray(content.parts)) continue
for (const part of content.parts) {
if (part.functionResponse) {
const funcName = part.functionResponse.name?.toLowerCase()
if (funcName) {
const currentIndex = toolPositionCounters.get(funcName) || 0
toolPositionCounters.set(funcName, currentIndex + 1)
const positionKey = `${funcName}:${currentIndex}`
const toolCallId = positionMapping.get(positionKey)
if (toolCallId) {
const metadata = state.toolParameters.get(toolCallId.toLowerCase())
outputs.push({
id: toolCallId.toLowerCase(),
toolName: metadata?.tool
})
}
}
}
}
}
return outputs
},
replaceToolOutput(data: any[], toolId: string, prunedMessage: string, state: PluginState): boolean {
let positionMapping: Map<string, string> | undefined
for (const [_sessionId, mapping] of state.googleToolCallMapping) {
if (mapping && mapping.size > 0) {
positionMapping = mapping
break
}
}
if (!positionMapping) {
return false
}
const toolIdLower = toolId.toLowerCase()
const toolPositionCounters = new Map<string, number>()
let replaced = false
for (let i = 0; i < data.length; i++) {
const content = data[i]
if (!Array.isArray(content.parts)) continue
let contentModified = false
const newParts = content.parts.map((part: any) => {
if (part.functionResponse) {
const funcName = part.functionResponse.name?.toLowerCase()
if (funcName) {
const currentIndex = toolPositionCounters.get(funcName) || 0
toolPositionCounters.set(funcName, currentIndex + 1)
const positionKey = `${funcName}:${currentIndex}`
const mappedToolId = positionMapping!.get(positionKey)
if (mappedToolId?.toLowerCase() === toolIdLower) {
contentModified = true
replaced = true
// Preserve thoughtSignature if present (required for Gemini 3 Pro)
return {
...part,
functionResponse: {
...part.functionResponse,
response: {
name: part.functionResponse.name,
content: prunedMessage
}
}
}
}
}
}
return part
})
if (contentModified) {
data[i] = { ...content, parts: newParts }
}
}
return replaced
},
hasToolOutputs(data: any[]): boolean {
return data.some((content: any) =>
Array.isArray(content.parts) &&
content.parts.some((part: any) => part.functionResponse)
)
},
getLogMetadata(data: any[], replacedCount: number, inputUrl: string): Record<string, any> {
return {
url: inputUrl,
replacedCount,
totalContents: data.length,
format: 'google-gemini'
}
}
}