Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
import { register } from "../registry.ts";
import { FORMATS } from "../formats.ts";
import { adjustMaxTokens } from "../helpers/maxTokensHelper.ts";
import { createGeminiToolCallIdPairing } from "../helpers/geminiToolCallIds.ts";
const newCallId = () => `call_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
// Convert Gemini request to OpenAI format
export function geminiToOpenAIRequest(model, body, stream) {
const result: {
model: string;
messages: Array<Record<string, unknown>>;
stream: boolean;
max_tokens?: number;
temperature?: number;
top_p?: number;
tools?: Array<Record<string, unknown>>;
} = {
model: model,
messages: [],
stream: stream,
};
// Generation config
if (body.generationConfig) {
const config = body.generationConfig;
if (config.maxOutputTokens) {
const tempBody = { max_tokens: config.maxOutputTokens, tools: body.tools };
result.max_tokens = adjustMaxTokens(tempBody);
}
if (config.temperature !== undefined) {
result.temperature = config.temperature;
}
if (config.topP !== undefined) {
result.top_p = config.topP;
}
}
// System instruction
if (body.systemInstruction) {
const systemText = extractGeminiText(body.systemInstruction);
if (systemText) {
result.messages.push({
role: "system",
content: systemText,
});
}
}
// Convert contents to messages
if (body.contents && Array.isArray(body.contents)) {
const toolCallIds = createGeminiToolCallIdPairing(newCallId);
for (const content of splitCoLocatedFunctionResponses(body.contents)) {
toolCallIds.beginContent(content);
const converted = convertGeminiContentWithReasoning(content, toolCallIds);
if (converted) {
result.messages.push(converted);
}
}
}
// Tools
if (body.tools && Array.isArray(body.tools)) {
result.tools = [];
for (const tool of body.tools) {
if (tool.functionDeclarations) {
for (const func of tool.functionDeclarations) {
result.tools.push({
type: "function",
function: {
name: func.name,
description: func.description || "",
parameters: func.parameters || { type: "object", properties: {} },
},
});
}
}
}
}
return result;
}
// Convert Gemini content to OpenAI message
// convertGeminiContent() early-returns the tool message on the first
// `functionResponse` part in a content, dropping any co-located parts
// (another functionCall, or trailing text). Gemini clients can send those
// co-located. Pre-split each such content into: one single-part content per
// functionResponse (each early-returns cleanly as a tool message, emitted
// first to keep tool-result-before-next-turn ordering) plus one content for
// the remaining non-functionResponse parts.
function splitCoLocatedFunctionResponses(contents) {
const out = [];
for (const content of contents) {
if (!content || !Array.isArray(content.parts)) {
out.push(content);
continue;
}
const hasFunctionResponse = content.parts.some((p) => p && p.functionResponse);
if (!hasFunctionResponse) {
out.push(content);
continue;
}
for (const part of content.parts) {
if (part && part.functionResponse) {
out.push({ ...content, parts: [part] });
}
}
const nonFRParts = content.parts.filter((p) => !(p && p.functionResponse));
if (nonFRParts.length > 0) {
out.push({ ...content, parts: nonFRParts });
}
}
return out;
}
function convertGeminiContent(content, toolCallIds) {
const role = content.role === "user" ? "user" : "assistant";
if (!content.parts || !Array.isArray(content.parts)) {
return null;
}
const parts = [];
const toolCalls = [];
for (const part of content.parts) {
if (part.text !== undefined) {
parts.push({ type: "text", text: part.text });
}
if (part.inlineData || part.inline_data) {
const data = part.inlineData || part.inline_data;
const mimeType = data.mimeType || data.mime_type || "image/png";
parts.push({
type: "image_url",
image_url: {
url: `data:${mimeType};base64,${data.data}`,
},
});
}
if (part.functionCall) {
toolCalls.push({
id: toolCallIds.callId(part.functionCall),
type: "function",
function: {
name: part.functionCall.name,
arguments: JSON.stringify(part.functionCall.args || {}),
},
});
}
if (part.functionResponse) {
const resp = part.functionResponse.response;
const resultPayload =
resp && typeof resp === "object" && "result" in resp ? resp.result : (resp ?? {});
return {
role: "tool",
tool_call_id: toolCallIds.responseId(part.functionResponse),
content: JSON.stringify(resultPayload),
};
}
}
if (toolCalls.length > 0) {
const result: {
role: string;
content?: string | Array<Record<string, unknown>>;
tool_calls?: Array<Record<string, unknown>>;
} = { role: "assistant" };
if (parts.length > 0) {
result.content = parts.length === 1 ? parts[0].text : parts;
}
result.tool_calls = toolCalls;
return result;
}
if (parts.length > 0) {
return {
role,
content: parts.length === 1 && parts[0].type === "text" ? parts[0].text : parts,
};
}
return null;
}
// Gemini marks thinking-mode output with `part.thought === true` on the model's own
// `parts` array (no separate field on the content itself). Left alone,
// convertGeminiContent() treats a thought part exactly like a visible text part —
// merging the model's internal reasoning into the message's regular `content`, which
// both leaks the private reasoning to whatever the OpenAI pivot forwards to next and
// prevents Reasoning Replay Cache (docs/routing/REASONING_REPLAY.md) from ever seeing
// it as `reasoning_content`. Split thought parts out first and re-attach the joined
// text as `reasoning_content` on the resulting message instead.
function convertGeminiContentWithReasoning(content, toolCallIds) {
if (!content || !Array.isArray(content.parts)) {
return convertGeminiContent(content, toolCallIds);
}
let reasoningContent = "";
const visibleParts = [];
for (const part of content.parts) {
if (part && part.thought === true) {
if (typeof part.text === "string") reasoningContent += part.text;
} else {
visibleParts.push(part);
}
}
if (!reasoningContent) {
return convertGeminiContent(content, toolCallIds);
}
const converted = convertGeminiContent({ ...content, parts: visibleParts }, toolCallIds);
if (converted && converted.role !== "tool") {
return { ...converted, reasoning_content: reasoningContent };
}
if (!converted) {
const role = content.role === "user" ? "user" : "assistant";
return { role, reasoning_content: reasoningContent };
}
// A `tool` message (functionResponse) can't carry reasoning_content — fall back to
// returning it unchanged rather than fabricating a field the tool-message schema
// doesn't expect.
return converted;
}
// Extract text from Gemini content
function extractGeminiText(content) {
if (typeof content === "string") return content;
if (content.parts && Array.isArray(content.parts)) {
return content.parts.map((p) => p.text || "").join("");
}
return "";
}
// Register
register(FORMATS.GEMINI, FORMATS.OPENAI, geminiToOpenAIRequest, null);