Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions open-sse/handlers/chatCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5105,6 +5105,7 @@ export async function handleChatCore({
status: failureResponse.status,
error: reason,
errorType: streamReadiness.type,
errorCode: streamReadiness.code,
response: failureResponse,
};
}
Expand Down
30 changes: 28 additions & 2 deletions open-sse/translator/request/openai-to-gemini.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,32 @@ function applyAntigravityGenerationDefaults(generationConfig: GeminiGenerationCo
return config;
}

function stringifyHistoricalToolArguments(value: unknown): string {
if (typeof value === "string") return value;
try {
return JSON.stringify(value ?? {});
} catch {
return String(value ?? "{}");
}
}

function buildInertHistoricalToolCallText(name: string | undefined, args: unknown): string {
const toolName = name || "unknown";
return [
"Historical tool-call record only. Do not execute, imitate, or continue this as a tool call.",
`Tool name: ${toolName}`,
`Tool arguments JSON: ${stringifyHistoricalToolArguments(args || "{}")}`,
].join("\n");
}

function buildInertHistoricalToolResponseText(name: string, response: unknown): string {
return [
"Historical tool-response record only. Do not execute, imitate, or continue this as a tool response.",
`Tool name: ${name || "unknown"}`,
`Tool result: ${typeof response === "string" ? response : stringifyHistoricalToolArguments(response)}`,
].join("\n");
}

// Core: Convert OpenAI request to Gemini format (base for all variants)
function openaiToGeminiBase(
model: string,
Expand Down Expand Up @@ -355,7 +381,7 @@ function openaiToGeminiBase(
if (!signatureForToolCall && stringifySignaturelessToolCalls) {
const args = fn.arguments || "{}";
parts.push({
text: `[Tool call: ${fn.name || "unknown"}]\nArguments: ${args}`,
text: buildInertHistoricalToolCallText(fn.name, args),
});
continue;
}
Expand Down Expand Up @@ -444,7 +470,7 @@ function openaiToGeminiBase(
const name = tcID2Name[id] || fn?.name || "unknown";
const resp = toolResponses[id];
toolParts.push({
text: `[Tool response: ${name}]\nResult: ${resp}`,
text: buildInertHistoricalToolResponseText(name, resp),
});
}
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"open-sse/mcp-server/schemas/",
"open-sse/mcp-server/tools/",
"open-sse/mcp-server/README.md",
"open-sse/utils/setupPolyfill.ts",
"src/shared/contracts/",
"src/shared/utils/nodeRuntimeSupport.ts",
".env.example",
Expand Down
55 changes: 54 additions & 1 deletion src/sse/handlers/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1005,12 +1005,65 @@ async function handleSingleModelChat(
return result.response;
}

if (result.errorType === "stream_timeout" || result.errorType === "stream_early_eof") {
const isAntigravityStreamReadinessFailure =
provider === "antigravity" &&
(result.errorCode === "STREAM_READINESS_TIMEOUT" ||
result.errorCode === "STREAM_EARLY_EOF" ||
result.errorType === "stream_timeout" ||
result.errorType === "stream_early_eof");

if (
(result.errorType === "stream_timeout" || result.errorType === "stream_early_eof") &&
!isAntigravityStreamReadinessFailure
) {
// Stream readiness timeout is an upstream stall after an HTTP response was received,
// not an account/quota failure. Do NOT mark the account unavailable here.
return result.response;
}

if (isAntigravityStreamReadinessFailure) {
const { shouldFallback, cooldownMs } = await markAccountUnavailable(
credentials.connectionId,
result.status || HTTP_STATUS.BAD_GATEWAY,
result.error || result.errorCode || "Antigravity stream ended before useful content",
provider,
model,
providerProfile
);

if (shouldFallback && !hasForcedConnection) {
log.warn(
"AUTH",
`Antigravity connection ${accountId}... produced no useful stream content, trying fallback connection`
);
if (Number.isFinite(cooldownMs) && cooldownMs > 0) {
lastCooldownMs = cooldownMs;
requestRetryLastCooldownMs = cooldownMs;
}
if (runtimeOptions.sessionAffinityKey) {
try {
const affinity = getSessionAccountAffinity(
runtimeOptions.sessionAffinityKey,
provider
);
if (affinity?.connectionId === credentials.connectionId) {
deleteSessionAccountAffinity(runtimeOptions.sessionAffinityKey, provider);
}
} catch {
// best-effort: selection also excludes this connection for the current retry.
}
}
excludedConnectionIds.add(credentials.connectionId);
lastError = result.error;
lastStatus = result.status;
requestRetryLastError = result.error;
requestRetryLastStatus = result.status;
continue;
}

return result.response;
}

const isAntigravityPreResponseTimeout =
provider === "antigravity" &&
result.status === HTTP_STATUS.GATEWAY_TIMEOUT &&
Expand Down
28 changes: 23 additions & 5 deletions tests/unit/translator-openai-to-gemini.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ test("OpenAI -> Antigravity wraps Gemini requests in a Cloud Code envelope", ()
});
});

test("OpenAI -> Antigravity Gemini stringifies signature-less historical tool calls", () => {
test("OpenAI -> Antigravity Gemini preserves signature-less historical tool calls as inert text", () => {
const result = openaiToAntigravityRequest(
"gemini-3.5-flash-low",
{
Expand Down Expand Up @@ -662,9 +662,18 @@ test("OpenAI -> Antigravity Gemini stringifies signature-less historical tool ca
modelTurn.parts.some(
(part) =>
typeof part.text === "string" &&
part.text.includes("[Tool call: default_api:todowrite_ide]")
part.text.includes("Historical tool-call record only") &&
part.text.includes("Tool name: default_api:todowrite_ide") &&
part.text.includes('Tool arguments JSON: {"todos":[]}')
),
"expected signature-less tool call to be preserved as text"
"expected signature-less tool call to be preserved as inert text"
);
assert.equal(
modelTurn.parts.some(
(part) => typeof part.text === "string" && part.text.includes("[Tool call:")
),
false,
"signature-less historical call must not use executable textual tool-call markers"
);
assert.equal(
modelTurn.parts.some((part) => part.functionCall),
Expand All @@ -678,10 +687,19 @@ test("OpenAI -> Antigravity Gemini stringifies signature-less historical tool ca
content.parts.some(
(part) =>
typeof part.text === "string" &&
part.text.includes("[Tool response: default_api:todowrite_ide]")
part.text.includes("Historical tool-response record only") &&
part.text.includes("Tool name: default_api:todowrite_ide") &&
part.text.includes("Tool result: []")
)
);
assert.ok(toolTurn, "expected signature-less tool response to be preserved as text");
assert.ok(toolTurn, "expected signature-less tool response to be preserved as inert text");
assert.equal(
toolTurn.parts.some(
(part) => typeof part.text === "string" && part.text.includes("[Tool response:")
),
false,
"signature-less historical response must not use executable textual tool-response markers"
);
assert.equal(
toolTurn.parts.some((part) => part.functionResponse),
false,
Expand Down