Spaces:
Sleeping
Sleeping
File size: 19,826 Bytes
ebd68ab | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 | import { handleChat } from "@/sse/handlers/chat.js";
import {
clearAccountError,
getProviderCredentials,
isValidApiKey,
markAccountUnavailable,
} from "@/sse/services/auth.js";
import { getSettings } from "@/lib/localDb";
import { PROVIDER_MODELS } from "@/shared/constants/models";
import { GEMINI_NATIVE_TTS_FETCH_TIMEOUT_MS } from "open-sse/config/runtimeConfig.js";
import { initTranslators } from "open-sse/translator/index.js";
let initialized = false;
const GEMINI_NATIVE_BASE_URL = "https://generativelanguage.googleapis.com/v1beta/models";
// Gemini model id charset (matches sanitizeGeminiFunctionName); blocks path traversal in upstream URL.
const GEMINI_NATIVE_MODEL_PATTERN = /^[a-zA-Z0-9_.:-]+$/;
/**
* Initialize translators once
*/
async function ensureInitialized() {
if (!initialized) {
await initTranslators();
initialized = true;
}
}
/**
* Handle CORS preflight
*/
export async function OPTIONS() {
return new Response(null, {
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Headers": "*"
}
});
}
/**
* POST /v1beta/models/{model}:generateContent — non-streaming
* POST /v1beta/models/{model}:streamGenerateContent — streaming (SSE)
*
* Streaming intent is determined by the URL action suffix (canonical Gemini API
* convention), NOT by a body field. generationConfig.stream is not a real
* Gemini API field and Gemini CLI never sets it.
*
* The @google/genai SDK always uses :streamGenerateContent?alt=sse for chat.
* The upstream handleChat returns OpenAI SSE format; we transform it to
* Gemini SSE format on the fly via transformOpenAISSEToGeminiSSE().
*/
export async function POST(request, { params }) {
await ensureInitialized();
try {
const { path } = await params;
// path = ["provider", "model:action"] or ["model:action"]
let model;
let action; // ":generateContent" | ":streamGenerateContent"
if (path.length >= 2) {
// Format: /v1beta/models/provider/model:generateContent
const provider = path[0];
const modelAction = path[1];
action = modelAction.includes(":streamGenerateContent")
? ":streamGenerateContent"
: ":generateContent";
const modelName = modelAction
.replace(":streamGenerateContent", "")
.replace(":generateContent", "");
model = provider + "/" + modelName;
} else {
// Format: /v1beta/models/model:generateContent
const modelAction = path[0];
action = modelAction.includes(":streamGenerateContent")
? ":streamGenerateContent"
: ":generateContent";
model = modelAction
.replace(":streamGenerateContent", "")
.replace(":generateContent", "");
}
const body = await request.json();
if (isGeminiNativeTtsRequest(model, body)) {
return await forwardGeminiNativeRequest(request, body, model, action);
}
// Streaming is determined by URL action suffix:
// :streamGenerateContent => stream: true (SSE)
// :generateContent => stream: false (plain JSON)
const stream = action === ":streamGenerateContent";
// Convert Gemini request format to OpenAI/internal format
const convertedBody = convertGeminiToInternal(body, model, stream);
// Create new request with converted body
const newRequest = new Request(request.url, {
method: "POST",
headers: request.headers,
body: JSON.stringify(convertedBody),
});
const response = await handleChat(newRequest);
if (stream) {
// Transform OpenAI SSE => Gemini SSE on the fly.
// The @google/genai SDK always uses :streamGenerateContent?alt=sse and
// expects Gemini SSE chunks (no [DONE] sentinel — stream just closes).
return transformOpenAISSEToGeminiSSE(response, model);
} else {
// Convert OpenAI JSON response => Gemini GenerateContentResponse
return await convertOpenAIResponseToGemini(response, model);
}
} catch (error) {
console.log("Error handling Gemini request:", error);
return Response.json(
{ error: { message: error.message, code: 500 } },
{ status: 500 }
);
}
}
function extractGeminiClientApiKey(request) {
const authHeader = request.headers.get("Authorization");
if (authHeader?.startsWith("Bearer ")) return authHeader.slice(7);
const googleApiKey = request.headers.get("x-goog-api-key");
if (googleApiKey) return googleApiKey;
const url = new URL(request.url);
return url.searchParams.get("key");
}
function normalizeGeminiNativeModel(model) {
return String(model || "")
.replace(/^models\//, "")
.replace(/^gemini\//, "");
}
function getGeminiTtsModelIds() {
return new Set([
...(PROVIDER_MODELS.gemini || [])
.filter((model) => (model.kind || model.type) === "tts")
.map((model) => model.id),
...(PROVIDER_MODELS["gemini-tts-models"] || []).map((model) => model.id),
]);
}
function hasAudioResponseModality(body) {
const modalities = body?.generationConfig?.responseModalities;
return Array.isArray(modalities)
&& modalities.some((modality) => String(modality).toUpperCase() === "AUDIO");
}
function isGeminiNativeTtsRequest(model, body) {
const rawModel = String(model || "");
if (rawModel.includes("/") && !rawModel.startsWith("gemini/") && !rawModel.startsWith("models/")) {
return false;
}
const modelId = normalizeGeminiNativeModel(model);
return hasAudioResponseModality(body) || getGeminiTtsModelIds().has(modelId);
}
function buildGeminiNativeUrl(requestUrl, model, action) {
const sourceUrl = new URL(requestUrl);
const upstreamUrl = new URL(`${GEMINI_NATIVE_BASE_URL}/${normalizeGeminiNativeModel(model)}${action}`);
for (const [key, value] of sourceUrl.searchParams.entries()) {
if (key === "key") continue;
upstreamUrl.searchParams.append(key, value);
}
return upstreamUrl.toString();
}
async function validateGeminiNativeClientKey(request) {
const settings = await getSettings();
if (!settings.requireApiKey) return null;
const apiKey = extractGeminiClientApiKey(request);
if (!apiKey) {
return Response.json({ error: { message: "Missing API key" } }, { status: 401 });
}
const valid = await isValidApiKey(apiKey);
if (!valid) {
return Response.json({ error: { message: "Invalid API key" } }, { status: 401 });
}
return null;
}
function buildGeminiNativeAuthHeaders(credentials) {
if (credentials?.apiKey) return { "x-goog-api-key": credentials.apiKey };
if (credentials?.accessToken) return { Authorization: `Bearer ${credentials.accessToken}` };
return null;
}
function corsHeadersFrom(response) {
const headers = new Headers(response.headers);
// Node fetch may expose a decoded body while preserving upstream compression
// headers. Forwarding those headers makes clients decompress plain bytes again.
headers.delete("content-encoding");
headers.delete("content-length");
headers.delete("transfer-encoding");
headers.set("Access-Control-Allow-Origin", "*");
return headers;
}
function getSafeGeminiConnectionLabel(credentials) {
const connectionId = String(credentials?.connectionId || "unknown");
const shortId = connectionId.slice(0, 8);
const connectionName = String(credentials?.connectionName || "");
if (!connectionName || connectionName.includes("@")) return shortId;
return `${connectionName}:${shortId}`;
}
function getGeminiNativeErrorCode(error) {
return error?.cause?.code || error?.code || error?.cause?.name || error?.name || "UNKNOWN";
}
function isGeminiNativeTimeoutError(error, timedOut) {
if (timedOut) return true;
const code = getGeminiNativeErrorCode(error);
return code === "UND_ERR_HEADERS_TIMEOUT" || code === "HeadersTimeoutError";
}
function getSafeGeminiNativeErrorText(error) {
const message = error?.message || String(error);
const code = getGeminiNativeErrorCode(error);
return `${message} (${code})`;
}
async function forwardGeminiNativeRequest(request, body, model, action) {
const authError = await validateGeminiNativeClientKey(request);
if (authError) return authError;
const modelId = normalizeGeminiNativeModel(model);
if (!GEMINI_NATIVE_MODEL_PATTERN.test(modelId)) {
return Response.json({ error: { message: "Invalid model" } }, { status: 400 });
}
const excludeConnectionIds = new Set();
const bodyText = JSON.stringify(body);
let lastError = null;
let lastStatus = null;
while (true) {
const credentials = await getProviderCredentials("gemini", excludeConnectionIds, modelId);
if (!credentials || credentials.allRateLimited) {
console.log(`[GEMINI_NATIVE] exhausted model=${modelId} status=${lastStatus || Number(credentials?.lastErrorCode) || 503} error=${lastError || credentials?.lastError || "No active credentials for provider: gemini"}`);
return Response.json(
{ error: { message: lastError || credentials?.lastError || "No active credentials for provider: gemini" } },
{ status: lastStatus || Number(credentials?.lastErrorCode) || 503 }
);
}
const authHeaders = buildGeminiNativeAuthHeaders(credentials);
if (!authHeaders) {
return Response.json(
{ error: { message: "No Gemini API key configured" } },
{ status: 404 }
);
}
const safeConnection = getSafeGeminiConnectionLabel(credentials);
const startedAt = Date.now();
const upstreamUrl = buildGeminiNativeUrl(request.url, modelId, action);
const attemptController = new AbortController();
let timedOut = false;
const timeout = setTimeout(() => {
timedOut = true;
attemptController.abort();
}, GEMINI_NATIVE_TTS_FETCH_TIMEOUT_MS);
const abortAttempt = () => attemptController.abort();
if (request.signal?.aborted) {
console.log(`[GEMINI_NATIVE] client aborted model=${modelId} ms=0 conn=${safeConnection}`);
return Response.json({ error: { message: "Client closed request" } }, { status: 499 });
}
request.signal?.addEventListener("abort", abortAttempt, { once: true });
console.log(`[GEMINI_NATIVE] start model=${modelId} action=${action} conn=${safeConnection} body=${Buffer.byteLength(bodyText)}B timeout=${GEMINI_NATIVE_TTS_FETCH_TIMEOUT_MS}`);
let upstreamResponse;
try {
upstreamResponse = await fetch(upstreamUrl, {
method: "POST",
headers: {
"Content-Type": request.headers.get("Content-Type") || "application/json",
...authHeaders,
},
body: bodyText,
signal: attemptController.signal,
});
} catch (error) {
const durationMs = Date.now() - startedAt;
if (request.signal?.aborted && !timedOut) {
console.log(`[GEMINI_NATIVE] client aborted model=${modelId} ms=${durationMs} conn=${safeConnection}`);
return Response.json({ error: { message: "Client closed request" } }, { status: 499 });
}
const status = isGeminiNativeTimeoutError(error, timedOut) ? 504 : 502;
const errorText = getSafeGeminiNativeErrorText(error);
console.log(`[GEMINI_NATIVE] fetch failed model=${modelId} status=${status} ms=${durationMs} conn=${safeConnection} error=${errorText}`);
const { shouldFallback } = await markAccountUnavailable(
credentials.connectionId,
status,
errorText,
"gemini",
modelId
);
if (shouldFallback) {
excludeConnectionIds.add(credentials.connectionId);
lastError = errorText;
lastStatus = status;
console.log(`[GEMINI_NATIVE] fallback model=${modelId} status=${status} conn=${safeConnection} exclude=${excludeConnectionIds.size}`);
continue;
}
return Response.json({ error: { message: errorText } }, { status });
} finally {
clearTimeout(timeout);
request.signal?.removeEventListener("abort", abortAttempt);
}
console.log(`[GEMINI_NATIVE] upstream model=${modelId} status=${upstreamResponse.status} ms=${Date.now() - startedAt} conn=${safeConnection} ct=${upstreamResponse.headers.get("content-type") || "?"} cl=${upstreamResponse.headers.get("content-length") || "?"}`);
if (upstreamResponse.ok) {
await clearAccountError(credentials.connectionId, credentials, modelId);
return new Response(upstreamResponse.body, {
status: upstreamResponse.status,
statusText: upstreamResponse.statusText,
headers: corsHeadersFrom(upstreamResponse),
});
}
const errorText = await upstreamResponse.text();
const { shouldFallback } = await markAccountUnavailable(
credentials.connectionId,
upstreamResponse.status,
errorText,
"gemini",
modelId
);
if (shouldFallback) {
excludeConnectionIds.add(credentials.connectionId);
lastError = errorText;
lastStatus = upstreamResponse.status;
continue;
}
return new Response(errorText, {
status: upstreamResponse.status,
statusText: upstreamResponse.statusText,
headers: corsHeadersFrom(upstreamResponse),
});
}
}
/**
* Convert Gemini request format to OpenAI/internal format.
*
* @param {object} geminiBody - parsed Gemini request body
* @param {string} model - resolved model string (e.g. "gemini-pro-high")
* @param {boolean} stream - whether to stream (from URL action)
*/
function convertGeminiToInternal(geminiBody, model, stream) {
const messages = [];
// Convert system instruction
if (geminiBody.systemInstruction) {
const systemText = geminiBody.systemInstruction.parts
?.map(p => p.text)
.join("\n") || "";
if (systemText) {
messages.push({ role: "system", content: systemText });
}
}
// Convert contents to messages
if (geminiBody.contents) {
for (const content of geminiBody.contents) {
const role = content.role === "model" ? "assistant" : "user";
const text = content.parts?.map(p => p.text).join("\n") || "";
messages.push({ role, content: text });
}
}
return {
model,
messages,
stream,
max_tokens: geminiBody.generationConfig?.maxOutputTokens,
temperature: geminiBody.generationConfig?.temperature,
top_p: geminiBody.generationConfig?.topP,
};
}
/** Map OpenAI finish_reason => Gemini finishReason */
const FINISH_REASON_MAP = {
stop: "STOP",
length: "MAX_TOKENS",
tool_calls: "STOP",
content_filter: "SAFETY",
};
/**
* Transform an OpenAI SSE stream into a Gemini SSE stream.
*
* OpenAI SSE format (what handleChat returns):
* data: {"choices":[{"delta":{"content":"Hi"},"finish_reason":null}]}
* data: {"choices":[{"delta":{},"finish_reason":"stop"}],"usage":{...}}
* data: [DONE]
*
* Gemini SSE format (what @google/genai SDK expects):
* data: {"candidates":[{"content":{"role":"model","parts":[{"text":"Hi"}]},"index":0}]}
* data: {"candidates":[{"content":{"role":"model","parts":[{"text":""}]},"finishReason":"STOP","index":0}],"usageMetadata":{...}}
* (stream closes — no [DONE])
*/
function transformOpenAISSEToGeminiSSE(upstreamResponse, model) {
if (!upstreamResponse.ok || !upstreamResponse.body) {
return upstreamResponse;
}
const decoder = new TextDecoder();
const encoder = new TextEncoder();
const transformStream = new TransformStream({
transform(chunk, controller) {
const text = decoder.decode(chunk, { stream: true });
const lines = text.split("\n");
for (const line of lines) {
if (!line.startsWith("data:")) continue;
const data = line.slice(5).trim();
// Drop empty lines and the OpenAI [DONE] sentinel.
// Gemini SSE ends by stream close, no sentinel needed.
if (!data || data === "[DONE]") continue;
let parsed;
try {
parsed = JSON.parse(data);
} catch {
continue;
}
const choice = parsed.choices?.[0];
if (!choice) continue;
const delta = choice.delta || {};
const parts = [];
if (delta.reasoning_content) {
parts.push({ text: delta.reasoning_content, thought: true });
}
if (delta.content) {
parts.push({ text: delta.content });
}
// Skip pure role-only deltas with no content and no finish signal
if (parts.length === 0 && !choice.finish_reason) continue;
const candidate = {
content: {
role: "model",
parts: parts.length > 0 ? parts : [{ text: "" }],
},
index: 0,
};
if (choice.finish_reason) {
candidate.finishReason = FINISH_REASON_MAP[choice.finish_reason] || "STOP";
}
const geminiChunk = { candidates: [candidate] };
// Attach usage + modelVersion on the final chunk (when finish_reason is set)
if (choice.finish_reason && parsed.usage) {
geminiChunk.usageMetadata = {
promptTokenCount: parsed.usage.prompt_tokens || 0,
candidatesTokenCount: parsed.usage.completion_tokens || 0,
totalTokenCount: parsed.usage.total_tokens || 0,
};
const reasoningTokens =
parsed.usage.completion_tokens_details?.reasoning_tokens;
if (reasoningTokens) {
geminiChunk.usageMetadata.thoughtsTokenCount = reasoningTokens;
}
geminiChunk.modelVersion = parsed.model || model;
}
controller.enqueue(
encoder.encode("data: " + JSON.stringify(geminiChunk) + "\r\n\r\n")
);
}
},
// No flush() needed: Gemini SSE ends by stream close, not a sentinel
});
return new Response(upstreamResponse.body.pipeThrough(transformStream), {
status: 200,
headers: {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"Access-Control-Allow-Origin": "*",
},
});
}
/**
* Convert an OpenAI chat.completion JSON response into a Gemini
* GenerateContentResponse so that Gemini CLI can parse it.
*/
async function convertOpenAIResponseToGemini(response, model) {
if (!response.ok) return response;
let body;
try {
body = await response.json();
} catch {
return response;
}
if (body.candidates) return Response.json(body, {
headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" }
});
if (body.error) return Response.json(body, {
status: response.status,
headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" }
});
const choice = body.choices?.[0];
if (!choice) {
return Response.json(body, {
headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" }
});
}
const { message, finish_reason } = choice;
const parts = [];
if (message.reasoning_content) {
parts.push({ text: message.reasoning_content, thought: true });
}
parts.push({ text: message.content || "" });
const finishReason = FINISH_REASON_MAP[finish_reason] || "STOP";
const geminiResponse = {
candidates: [
{
content: { role: "model", parts },
finishReason,
index: 0,
},
],
modelVersion: body.model || model,
};
if (body.usage) {
geminiResponse.usageMetadata = {
promptTokenCount: body.usage.prompt_tokens || 0,
candidatesTokenCount: body.usage.completion_tokens || 0,
totalTokenCount: body.usage.total_tokens || 0,
};
const reasoningTokens = body.usage.completion_tokens_details?.reasoning_tokens;
if (reasoningTokens) {
geminiResponse.usageMetadata.thoughtsTokenCount = reasoningTokens;
}
}
return Response.json(geminiResponse, {
headers: { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*" }
});
}
|