| import "open-sse/index.js"; |
|
|
| import { |
| getProviderCredentials, |
| markAccountUnavailable, |
| clearAccountError, |
| extractApiKey, |
| isValidApiKey, |
| } from "../services/auth.js"; |
| import { cacheClaudeHeaders } from "open-sse/utils/claudeHeaderCache.js"; |
| import { getSettings } from "@/lib/localDb"; |
| import { getModelInfo, getComboModels } from "../services/model.js"; |
| import { handleChatCore } from "open-sse/handlers/chatCore.js"; |
| import { errorResponse, unavailableResponse } from "open-sse/utils/error.js"; |
| import { handleComboChat } from "open-sse/services/combo.js"; |
| import { handleBypassRequest } from "open-sse/utils/bypassHandler.js"; |
| import { HTTP_STATUS } from "open-sse/config/runtimeConfig.js"; |
| import { detectFormatByEndpoint } from "open-sse/translator/formats.js"; |
| import * as log from "../utils/logger.js"; |
| import { updateProviderCredentials, checkAndRefreshToken } from "../services/tokenRefresh.js"; |
| import { getProjectIdForConnection } from "open-sse/services/projectId.js"; |
|
|
| |
| |
| |
| |
| |
| export async function handleChat(request, clientRawRequest = null) { |
| let body; |
| try { |
| body = await request.json(); |
| } catch { |
| log.warn("CHAT", "Invalid JSON body"); |
| return errorResponse(HTTP_STATUS.BAD_REQUEST, "Invalid JSON body"); |
| } |
|
|
| |
| if (!clientRawRequest) { |
| const url = new URL(request.url); |
| clientRawRequest = { |
| endpoint: url.pathname, |
| body, |
| headers: Object.fromEntries(request.headers.entries()) |
| }; |
| } |
| cacheClaudeHeaders(clientRawRequest.headers); |
|
|
| |
| const url = new URL(request.url); |
| const modelStr = body.model; |
|
|
| |
| const msgCount = body.messages?.length || body.input?.length || 0; |
| const toolCount = body.tools?.length || 0; |
| const effort = body.reasoning_effort || body.reasoning?.effort || null; |
| log.request("POST", `${url.pathname} | ${modelStr} | ${msgCount} msgs${toolCount ? ` | ${toolCount} tools` : ""}${effort ? ` | effort=${effort}` : ""}`); |
|
|
| |
| const authHeader = request.headers.get("Authorization"); |
| const apiKey = extractApiKey(request); |
| if (authHeader && apiKey) { |
| const masked = log.maskKey(apiKey); |
| log.debug("AUTH", `API Key: ${masked}`); |
| } else { |
| log.debug("AUTH", "No API key provided (local mode)"); |
| } |
|
|
| |
| const settings = await getSettings(); |
| if (settings.requireApiKey) { |
| if (!apiKey) { |
| log.warn("AUTH", "Missing API key (requireApiKey=true)"); |
| return errorResponse(HTTP_STATUS.UNAUTHORIZED, "Missing API key"); |
| } |
| const valid = await isValidApiKey(apiKey); |
| if (!valid) { |
| log.warn("AUTH", "Invalid API key (requireApiKey=true)"); |
| return errorResponse(HTTP_STATUS.UNAUTHORIZED, "Invalid API key"); |
| } |
| } |
|
|
| if (!modelStr) { |
| log.warn("CHAT", "Missing model"); |
| return errorResponse(HTTP_STATUS.BAD_REQUEST, "Missing model"); |
| } |
|
|
| |
| const userAgent = request?.headers?.get("user-agent") || ""; |
| const bypassResponse = handleBypassRequest(body, modelStr, userAgent, !!settings.ccFilterNaming); |
| if (bypassResponse) return bypassResponse.response || bypassResponse; |
|
|
| |
| const comboModels = await getComboModels(modelStr); |
| if (comboModels) { |
| |
| const comboStrategies = settings.comboStrategies || {}; |
| const comboSpecificStrategy = comboStrategies[modelStr]?.fallbackStrategy; |
| const comboStrategy = comboSpecificStrategy || settings.comboStrategy || "fallback"; |
| |
| const comboStickyLimit = settings.comboStickyRoundRobinLimit; |
| log.info("CHAT", `Combo "${modelStr}" with ${comboModels.length} models (strategy: ${comboStrategy}, sticky: ${comboStickyLimit})`); |
| return handleComboChat({ |
| body, |
| models: comboModels, |
| handleSingleModel: (b, m) => handleSingleModelChat(b, m, clientRawRequest, request, apiKey), |
| log, |
| comboName: modelStr, |
| comboStrategy, |
| comboStickyLimit |
| }); |
| } |
|
|
| |
| return handleSingleModelChat(body, modelStr, clientRawRequest, request, apiKey); |
| } |
|
|
| |
| |
| |
| async function handleSingleModelChat(body, modelStr, clientRawRequest = null, request = null, apiKey = null) { |
| const modelInfo = await getModelInfo(modelStr); |
|
|
| |
| if (!modelInfo.provider) { |
| const comboModels = await getComboModels(modelStr); |
| if (comboModels) { |
| const chatSettings = await getSettings(); |
| |
| const comboStrategies = chatSettings.comboStrategies || {}; |
| const comboSpecificStrategy = comboStrategies[modelStr]?.fallbackStrategy; |
| const comboStrategy = comboSpecificStrategy || chatSettings.comboStrategy || "fallback"; |
| |
| const comboStickyLimit = chatSettings.comboStickyRoundRobinLimit; |
| log.info("CHAT", `Combo "${modelStr}" with ${comboModels.length} models (strategy: ${comboStrategy}, sticky: ${comboStickyLimit})`); |
| return handleComboChat({ |
| body, |
| models: comboModels, |
| handleSingleModel: (b, m) => handleSingleModelChat(b, m, clientRawRequest, request, apiKey), |
| log, |
| comboName: modelStr, |
| comboStrategy, |
| comboStickyLimit |
| }); |
| } |
| log.warn("CHAT", "Invalid model format", { model: modelStr }); |
| return errorResponse(HTTP_STATUS.BAD_REQUEST, "Invalid model format"); |
| } |
|
|
| const { provider, model } = modelInfo; |
|
|
| |
| if (modelStr !== `${provider}/${model}`) { |
| log.info("ROUTING", `${modelStr} → ${provider}/${model}`); |
| } else { |
| log.info("ROUTING", `Provider: ${provider}, Model: ${model}`); |
| } |
|
|
| |
| const userAgent = request?.headers?.get("user-agent") || ""; |
|
|
| |
| const excludeConnectionIds = new Set(); |
| let lastError = null; |
| let lastStatus = null; |
|
|
| while (true) { |
| const credentials = await getProviderCredentials(provider, excludeConnectionIds, model); |
|
|
| |
| if (!credentials || credentials.allRateLimited) { |
| if (credentials?.allRateLimited) { |
| const errorMsg = lastError || credentials.lastError || "Unavailable"; |
| const status = lastStatus || Number(credentials.lastErrorCode) || HTTP_STATUS.SERVICE_UNAVAILABLE; |
| log.warn("CHAT", `[${provider}/${model}] ${errorMsg} (${credentials.retryAfterHuman})`); |
| return unavailableResponse(status, `[${provider}/${model}] ${errorMsg}`, credentials.retryAfter, credentials.retryAfterHuman); |
| } |
| if (excludeConnectionIds.size === 0) { |
| log.warn("AUTH", `No active credentials for provider: ${provider}`); |
| return errorResponse(HTTP_STATUS.NOT_FOUND, `No active credentials for provider: ${provider}`); |
| } |
| log.warn("CHAT", "No more accounts available", { provider }); |
| return errorResponse(lastStatus || HTTP_STATUS.SERVICE_UNAVAILABLE, lastError || "All accounts unavailable"); |
| } |
|
|
| |
| log.info("AUTH", `\x1b[32mUsing ${provider} account: ${credentials.connectionName}\x1b[0m`); |
|
|
| const refreshedCredentials = await checkAndRefreshToken(provider, credentials); |
|
|
| |
| if ((provider === "antigravity" || provider === "gemini-cli") && !refreshedCredentials.projectId) { |
| const pid = await getProjectIdForConnection(credentials.connectionId, refreshedCredentials.accessToken); |
| if (pid) { |
| refreshedCredentials.projectId = pid; |
| |
| updateProviderCredentials(credentials.connectionId, { projectId: pid }).catch(() => { }); |
| } |
| } |
|
|
| |
| const chatSettings = await getSettings(); |
| const providerThinking = (chatSettings.providerThinking || {})[provider] || null; |
| const result = await handleChatCore({ |
| body: { ...body, model: `${provider}/${model}` }, |
| modelInfo: { provider, model }, |
| credentials: refreshedCredentials, |
| log, |
| clientRawRequest, |
| connectionId: credentials.connectionId, |
| userAgent, |
| apiKey, |
| ccFilterNaming: !!chatSettings.ccFilterNaming, |
| rtkEnabled: !!chatSettings.rtkEnabled, |
| cavemanEnabled: !!chatSettings.cavemanEnabled, |
| cavemanLevel: chatSettings.cavemanLevel || "full", |
| providerThinking, |
| |
| sourceFormatOverride: request?.url ? detectFormatByEndpoint(new URL(request.url).pathname, body) : null, |
| onCredentialsRefreshed: async (newCreds) => { |
| await updateProviderCredentials(credentials.connectionId, { |
| ...newCreds, |
| existingProviderSpecificData: credentials.providerSpecificData, |
| testStatus: "active" |
| }); |
| }, |
| onRequestSuccess: async () => { |
| await clearAccountError(credentials.connectionId, credentials, model); |
| } |
| }); |
|
|
| if (result.success) return result.response; |
|
|
| |
| const { shouldFallback } = await markAccountUnavailable(credentials.connectionId, result.status, result.error, provider, model, result.resetsAtMs); |
|
|
| if (shouldFallback) { |
| log.warn("AUTH", `Account ${credentials.connectionName} unavailable (${result.status}), trying fallback`); |
| excludeConnectionIds.add(credentials.connectionId); |
| lastError = result.error; |
| lastStatus = result.status; |
| continue; |
| } |
|
|
| return result.response; |
| } |
| } |
|
|