Skip to content

Commit 5199ea9

Browse files
committed
fix: 修复Gem请求失败
1 parent e2ff23e commit 5199ea9

1 file changed

Lines changed: 28 additions & 5 deletions

File tree

services/gemini_api.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,22 @@ export async function sendGeminiMessage(prompt, context, model, files, signal, o
101101
}
102102
}
103103

104+
// Normalize Gem ID (Strip 'model:' and 'gemini-custom-' prefix if present)
105+
let actualGemId = gemId;
106+
if (actualGemId) {
107+
if (actualGemId.startsWith('model:')) {
108+
actualGemId = actualGemId.substring(6);
109+
}
110+
if (actualGemId.startsWith('gemini-custom-')) {
111+
actualGemId = actualGemId.substring(14);
112+
}
113+
}
114+
115+
let requestUUID = null;
116+
if (actualGemId || model === 'gem') {
117+
requestUUID = self.crypto.randomUUID().toUpperCase();
118+
}
119+
104120
const modelConfig = getModelConfig(model);
105121

106122
// 2. Handle File Uploads (Multimodal)
@@ -173,9 +189,17 @@ export async function sendGeminiMessage(prompt, context, model, files, signal, o
173189
null, // 16
174190
null, // 17
175191
null, // 18
176-
gemId // 19: Gem ID injected here
192+
actualGemId // 19: Gem ID injected here (Normalised)
177193
];
178194

195+
// For Gems, we need to inject the UUID at index 59
196+
if (actualGemId && requestUUID) {
197+
while (data.length < 60) {
198+
data.push(null);
199+
}
200+
data[59] = requestUUID;
201+
}
202+
179203
// The API expects: f.req = JSON.stringify([null, JSON.stringify(data)])
180204
// This wrapper is still required for Batchexecute-style endpoints like StreamGenerate
181205
const fReq = JSON.stringify([null, JSON.stringify(data)]);
@@ -197,10 +221,9 @@ export async function sendGeminiMessage(prompt, context, model, files, signal, o
197221
};
198222

199223
// Gems Header Injection
200-
// For 'gem' model, gemId is required and will be set in the header
201-
// For other models, gemId is optional (can override to use a specific Gem)
202-
if (gemId || model === 'gem') {
203-
headers['x-goog-ext-525005358-jspb'] = `["${gemId}",1]`;
224+
// For 'gem' model, proper headers are required
225+
if (actualGemId || model === 'gem') {
226+
headers['x-goog-ext-525005358-jspb'] = `["${requestUUID}",1]`;
204227
}
205228

206229
// 5. Send Request

0 commit comments

Comments
 (0)