mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 20:00:47 +00:00
fix(gemini): normalize contents to prevent 400 invalid_argument (#2192)
Merge adjacent same-role blocks and strip empty parts before sending to Gemini, avoiding 400 INVALID_ARGUMENT on consecutive same-role messages. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
committed by
decolua
co-authored by
Cursor
parent
9e3866658a
commit
8d1db46beb
@@ -35,6 +35,17 @@ function sanitizeGeminiFunctionName(name) {
|
|||||||
return sanitized.substring(0, 64);
|
return sanitized.substring(0, 64);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function normalizeGeminiContents(contents) {
|
||||||
|
const out = [];
|
||||||
|
for (const c of contents || []) {
|
||||||
|
if (!c?.role || !Array.isArray(c.parts) || c.parts.length === 0) continue;
|
||||||
|
const last = out.at(-1);
|
||||||
|
if (last?.role === c.role) last.parts.push(...c.parts);
|
||||||
|
else out.push({ ...c, parts: [...c.parts] });
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
// Core: Convert OpenAI request to Gemini format (base for all variants)
|
// Core: Convert OpenAI request to Gemini format (base for all variants)
|
||||||
function openaiToGeminiBase(model, body, stream, signature = DEFAULT_THINKING_AG_SIGNATURE) {
|
function openaiToGeminiBase(model, body, stream, signature = DEFAULT_THINKING_AG_SIGNATURE) {
|
||||||
const result = {
|
const result = {
|
||||||
@@ -217,6 +228,7 @@ function openaiToGeminiBase(model, body, stream, signature = DEFAULT_THINKING_AG
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result.contents = normalizeGeminiContents(result.contents);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -426,6 +438,7 @@ function wrapInCloudCodeEnvelopeForClaude(model, claudeRequest, credentials = nu
|
|||||||
envelope.request.systemInstruction = { role: GEMINI_ROLE.USER, parts: systemParts };
|
envelope.request.systemInstruction = { role: GEMINI_ROLE.USER, parts: systemParts };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
envelope.request.contents = normalizeGeminiContents(envelope.request.contents);
|
||||||
return envelope;
|
return envelope;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user