| import { CORS_ORIGIN } from "@/shared/utils/cors"; |
| import { handleChat } from "@/sse/handlers/chat"; |
| import { initTranslators } from "@omniroute/open-sse/translator/index.ts"; |
|
|
| let initialized = false; |
|
|
| |
| |
| |
| async function ensureInitialized() { |
| if (!initialized) { |
| await initTranslators(); |
| initialized = true; |
| console.log("[SSE] Translators initialized for /v1beta/models"); |
| } |
| } |
|
|
| |
| |
| |
| export async function OPTIONS() { |
| return new Response(null, { |
| headers: { |
| "Access-Control-Allow-Origin": CORS_ORIGIN, |
| "Access-Control-Allow-Methods": "GET, POST, OPTIONS", |
| "Access-Control-Allow-Headers": "*", |
| }, |
| }); |
| } |
|
|
| |
| |
| |
| |
| export async function POST(request, { params }) { |
| await ensureInitialized(); |
|
|
| try { |
| const { path } = await params; |
| |
|
|
| let model; |
| if (path.length >= 2) { |
| |
| const provider = path[0]; |
| const modelAction = path[1]; |
| const modelName = modelAction |
| .replace(":generateContent", "") |
| .replace(":streamGenerateContent", ""); |
| model = `${provider}/${modelName}`; |
| } else { |
| |
| const modelAction = path[0]; |
| model = modelAction.replace(":generateContent", "").replace(":streamGenerateContent", ""); |
| } |
|
|
| const body = await request.json(); |
|
|
| |
| const convertedBody = convertGeminiToInternal(body, model); |
|
|
| |
| const newRequest = new Request(request.url, { |
| method: "POST", |
| headers: request.headers, |
| body: JSON.stringify(convertedBody), |
| }); |
|
|
| return await handleChat(newRequest); |
| } catch (error) { |
| console.log("Error handling Gemini request:", error); |
| return Response.json({ error: { message: error.message, code: 500 } }, { status: 500 }); |
| } |
| } |
|
|
| |
| |
| |
| function convertGeminiToInternal(geminiBody, model) { |
| const messages = []; |
|
|
| |
| if (geminiBody.systemInstruction) { |
| const systemText = geminiBody.systemInstruction.parts?.map((p) => p.text).join("\n") || ""; |
| if (systemText) { |
| messages.push({ role: "system", content: systemText }); |
| } |
| } |
|
|
| |
| 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 }); |
| } |
| } |
|
|
| |
| const stream = geminiBody.generationConfig?.stream !== false; |
|
|
| return { |
| model, |
| messages, |
| stream, |
| max_tokens: geminiBody.generationConfig?.maxOutputTokens, |
| temperature: geminiBody.generationConfig?.temperature, |
| top_p: geminiBody.generationConfig?.topP, |
| }; |
| } |
|
|