-
-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathgemini.ts
More file actions
134 lines (114 loc) · 4.59 KB
/
gemini.ts
File metadata and controls
134 lines (114 loc) · 4.59 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
import type { FetchHandlerContext, FetchHandlerResult } from "./types"
import {
PRUNED_CONTENT_MESSAGE,
getAllPrunedIds,
fetchSessionMessages
} from "./types"
/**
* Handles Google/Gemini format (body.contents array with functionResponse parts).
* Uses position-based correlation since Google's native format doesn't include tool call IDs.
*/
export async function handleGemini(
body: any,
ctx: FetchHandlerContext,
inputUrl: string
): Promise<FetchHandlerResult> {
if (!body.contents || !Array.isArray(body.contents)) {
return { modified: false, body }
}
// Check for functionResponse parts in any content item
const hasFunctionResponses = body.contents.some((content: any) =>
Array.isArray(content.parts) &&
content.parts.some((part: any) => part.functionResponse)
)
if (!hasFunctionResponses) {
return { modified: false, body }
}
const { allSessions, allPrunedIds } = await getAllPrunedIds(ctx.client, ctx.state)
if (allPrunedIds.size === 0) {
return { modified: false, body }
}
// Find the active session to get the position mapping
const activeSessions = allSessions.data?.filter((s: any) => !s.parentID) || []
let positionMapping: Map<string, string> | undefined
for (const session of activeSessions) {
const mapping = ctx.state.googleToolCallMapping.get(session.id)
if (mapping && mapping.size > 0) {
positionMapping = mapping
break
}
}
if (!positionMapping) {
ctx.logger.info("fetch", "No Google tool call mapping found, skipping pruning for Gemini format")
return { modified: false, body }
}
// Build position counters to track occurrence of each tool name
const toolPositionCounters = new Map<string, number>()
let replacedCount = 0
let totalFunctionResponses = 0
body.contents = body.contents.map((content: any) => {
if (!Array.isArray(content.parts)) return content
let contentModified = false
const newParts = content.parts.map((part: any) => {
if (part.functionResponse) {
totalFunctionResponses++
const funcName = part.functionResponse.name?.toLowerCase()
if (funcName) {
// Get current position for this tool name and increment counter
const currentIndex = toolPositionCounters.get(funcName) || 0
toolPositionCounters.set(funcName, currentIndex + 1)
// Look up the tool call ID using position
const positionKey = `${funcName}:${currentIndex}`
const toolCallId = positionMapping!.get(positionKey)
if (toolCallId && allPrunedIds.has(toolCallId)) {
contentModified = true
replacedCount++
// Preserve thoughtSignature if present (required for Gemini 3 Pro)
// response must be a Struct (object), not a plain string
return {
...part,
functionResponse: {
...part.functionResponse,
response: {
name: part.functionResponse.name,
content: PRUNED_CONTENT_MESSAGE
}
}
}
}
}
}
return part
})
if (contentModified) {
return { ...content, parts: newParts }
}
return content
})
if (replacedCount > 0) {
ctx.logger.info("fetch", "Replaced pruned tool outputs (Google/Gemini)", {
replaced: replacedCount,
total: totalFunctionResponses
})
if (ctx.logger.enabled) {
let sessionMessages: any[] | undefined
if (activeSessions.length > 0) {
const mostRecentSession = activeSessions[0]
sessionMessages = await fetchSessionMessages(ctx.client, mostRecentSession.id)
}
await ctx.logger.saveWrappedContext(
"global",
body.contents,
{
url: inputUrl,
replacedCount,
totalContents: body.contents.length,
format: 'google-gemini'
},
sessionMessages
)
}
return { modified: true, body }
}
return { modified: false, body }
}