fix(gemini): guard terminal model turns and unresponded functionCalls in normalizeGeminiContents

This commit is contained in:
akmal safari pellu
2026-09-26 11:17:35 +07:00
parent 249f6c2fb8
commit 30464bc227
2 changed files with 159 additions and 1 deletions

View File

@@ -432,7 +432,7 @@ export function cleanJSONSchemaForAntigravity(schema) {
return cleaned;
}
// Merge adjacent same-role messages, strip empty parts, ensure initial user turn
// Merge adjacent same-role messages, strip empty parts, ensure initial and terminal user turns
export function normalizeGeminiContents(contents) {
const out = [];
for (const c of contents || []) {
@@ -446,6 +446,23 @@ export function normalizeGeminiContents(contents) {
if (out.length > 0 && out[0].role !== "user") {
out.unshift({ role: "user", parts: [{ text: "..." }] });
}
if (out.length > 0 && out.at(-1).role === "model") {
const fnCalls = (out.at(-1).parts || []).filter(p => p && p.functionCall);
if (fnCalls.length > 0) {
const responses = fnCalls.map(p => {
const call = p.functionCall || {};
const fr = {
name: call.name || "tool",
response: { result: "Continue." }
};
if (call.id) fr.id = call.id;
return { functionResponse: fr };
});
out.push({ role: "user", parts: responses });
} else {
out.push({ role: "user", parts: [{ text: "Continue." }] });
}
}
return out;
}