mirror of
https://github.com/decolua/9router.git
synced 2026-05-08 12:01:28 +00:00
Fix cloud
This commit is contained in:
@@ -52,11 +52,20 @@ export async function handleComboChat({ body, models, handleSingleModel, log })
|
||||
let errorText = result.statusText || "";
|
||||
try {
|
||||
const errorBody = await result.clone().json();
|
||||
errorText = errorBody.error || errorBody.message || errorText;
|
||||
errorText = errorBody?.error ?? errorBody?.message ?? errorText;
|
||||
} catch {
|
||||
// Ignore JSON parse errors
|
||||
}
|
||||
|
||||
// Normalize error text to string (Worker-safe)
|
||||
if (typeof errorText !== "string") {
|
||||
try {
|
||||
errorText = JSON.stringify(errorText);
|
||||
} catch {
|
||||
errorText = String(errorText);
|
||||
}
|
||||
}
|
||||
|
||||
// Check if should fallback to next model
|
||||
const { shouldFallback } = checkFallbackError(result.status, errorText);
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import { FORMATS } from "../formats.js";
|
||||
import { adjustMaxTokens } from "../helpers/maxTokensHelper.js";
|
||||
|
||||
// Convert Claude request to OpenAI format
|
||||
function claudeToOpenAIRequest(model, body, stream) {
|
||||
export function claudeToOpenAIRequest(model, body, stream) {
|
||||
const result = {
|
||||
model: model,
|
||||
messages: [],
|
||||
|
||||
@@ -3,7 +3,7 @@ import { FORMATS } from "../formats.js";
|
||||
import { adjustMaxTokens } from "../helpers/maxTokensHelper.js";
|
||||
|
||||
// Convert Gemini request to OpenAI format
|
||||
function geminiToOpenAIRequest(model, body, stream) {
|
||||
export function geminiToOpenAIRequest(model, body, stream) {
|
||||
const result = {
|
||||
model: model,
|
||||
messages: [],
|
||||
|
||||
@@ -10,7 +10,7 @@ import { FORMATS } from "../formats.js";
|
||||
/**
|
||||
* Convert OpenAI Responses API request to OpenAI Chat Completions format
|
||||
*/
|
||||
function openaiResponsesToOpenAIRequest(model, body, stream, credentials) {
|
||||
export function openaiResponsesToOpenAIRequest(model, body, stream, credentials) {
|
||||
if (!body.input) return body;
|
||||
|
||||
const result = { ...body };
|
||||
@@ -134,7 +134,7 @@ function openaiResponsesToOpenAIRequest(model, body, stream, credentials) {
|
||||
/**
|
||||
* Convert OpenAI Chat Completions to OpenAI Responses API format
|
||||
*/
|
||||
function openaiToOpenAIResponsesRequest(model, body, stream, credentials) {
|
||||
export function openaiToOpenAIResponsesRequest(model, body, stream, credentials) {
|
||||
const result = {
|
||||
model,
|
||||
input: [],
|
||||
|
||||
@@ -7,7 +7,7 @@ import { adjustMaxTokens } from "../helpers/maxTokensHelper.js";
|
||||
const CLAUDE_OAUTH_TOOL_PREFIX = "proxy_";
|
||||
|
||||
// Convert OpenAI request to Claude format
|
||||
function openaiToClaudeRequest(model, body, stream) {
|
||||
export function openaiToClaudeRequest(model, body, stream) {
|
||||
// Tool name mapping for Claude OAuth (capitalizedName → originalName)
|
||||
const toolNameMap = new Map();
|
||||
const result = {
|
||||
|
||||
@@ -191,12 +191,12 @@ function openaiToGeminiBase(model, body, stream) {
|
||||
}
|
||||
|
||||
// OpenAI -> Gemini (standard API)
|
||||
function openaiToGeminiRequest(model, body, stream) {
|
||||
export function openaiToGeminiRequest(model, body, stream) {
|
||||
return openaiToGeminiBase(model, body, stream);
|
||||
}
|
||||
|
||||
// OpenAI -> Gemini CLI (Cloud Code Assist)
|
||||
function openaiToGeminiCLIRequest(model, body, stream) {
|
||||
export function openaiToGeminiCLIRequest(model, body, stream) {
|
||||
const gemini = openaiToGeminiBase(model, body, stream);
|
||||
const isClaude = model.toLowerCase().includes("claude");
|
||||
|
||||
@@ -386,7 +386,7 @@ function wrapInCloudCodeEnvelopeForClaude(model, claudeRequest, credentials = nu
|
||||
}
|
||||
|
||||
// OpenAI -> Antigravity (Sandbox Cloud Code with wrapper)
|
||||
function openaiToAntigravityRequest(model, body, stream, credentials = null) {
|
||||
export function openaiToAntigravityRequest(model, body, stream, credentials = null) {
|
||||
const isClaude = model.toLowerCase().includes("claude");
|
||||
|
||||
if (isClaude) {
|
||||
|
||||
@@ -230,7 +230,7 @@ function convertMessages(messages, tools, model) {
|
||||
/**
|
||||
* Build Kiro payload from OpenAI format
|
||||
*/
|
||||
function buildKiroPayload(model, body, stream, credentials) {
|
||||
export function buildKiroPayload(model, body, stream, credentials) {
|
||||
const messages = body.messages || [];
|
||||
const tools = body.tools || [];
|
||||
const maxTokens = 32000;
|
||||
@@ -278,5 +278,3 @@ function buildKiroPayload(model, body, stream, credentials) {
|
||||
}
|
||||
|
||||
register(FORMATS.OPENAI, FORMATS.KIRO, buildKiroPayload, null);
|
||||
|
||||
export { buildKiroPayload };
|
||||
|
||||
@@ -17,7 +17,7 @@ function createChunk(state, delta, finishReason = null) {
|
||||
}
|
||||
|
||||
// Convert Claude stream chunk to OpenAI format
|
||||
function claudeToOpenAIResponse(chunk, state) {
|
||||
export function claudeToOpenAIResponse(chunk, state) {
|
||||
if (!chunk) return null;
|
||||
|
||||
const results = [];
|
||||
|
||||
@@ -2,7 +2,7 @@ import { register } from "../index.js";
|
||||
import { FORMATS } from "../formats.js";
|
||||
|
||||
// Convert Gemini response chunk to OpenAI format
|
||||
function geminiToOpenAIResponse(chunk, state) {
|
||||
export function geminiToOpenAIResponse(chunk, state) {
|
||||
if (!chunk) return null;
|
||||
|
||||
// Handle Antigravity wrapper
|
||||
|
||||
@@ -9,7 +9,7 @@ import { FORMATS } from "../formats.js";
|
||||
* Parse Kiro SSE event and convert to OpenAI format
|
||||
* Kiro events: assistantResponseEvent, codeEvent, supplementaryWebLinksEvent, etc.
|
||||
*/
|
||||
function convertKiroToOpenAI(chunk, state) {
|
||||
export function convertKiroToOpenAI(chunk, state) {
|
||||
|
||||
if (!chunk) return null;
|
||||
|
||||
@@ -181,5 +181,3 @@ function convertKiroToOpenAI(chunk, state) {
|
||||
|
||||
// Register translator
|
||||
register(FORMATS.KIRO, FORMATS.OPENAI, null, convertKiroToOpenAI);
|
||||
|
||||
export { convertKiroToOpenAI };
|
||||
|
||||
@@ -9,7 +9,7 @@ import { FORMATS } from "../formats.js";
|
||||
* Translate OpenAI chunk to Responses API events
|
||||
* @returns {Array} Array of events with { event, data } structure
|
||||
*/
|
||||
function openaiToOpenAIResponsesResponse(chunk, state) {
|
||||
export function openaiToOpenAIResponsesResponse(chunk, state) {
|
||||
if (!chunk) {
|
||||
return flushEvents(state);
|
||||
}
|
||||
@@ -359,7 +359,7 @@ function flushEvents(state) {
|
||||
* Translate OpenAI Responses API chunk to OpenAI Chat Completions format
|
||||
* This is for when Codex returns data and we need to send it to an OpenAI-compatible client
|
||||
*/
|
||||
function openaiResponsesToOpenAIResponse(chunk, state) {
|
||||
export function openaiResponsesToOpenAIResponse(chunk, state) {
|
||||
if (!chunk) {
|
||||
// Flush: send final chunk with finish_reason
|
||||
if (!state.finishReasonSent && state.started) {
|
||||
|
||||
@@ -26,7 +26,7 @@ function stopTextBlock(state, results) {
|
||||
}
|
||||
|
||||
// Convert OpenAI stream chunk to Claude format
|
||||
function openaiToClaudeResponse(chunk, state) {
|
||||
export function openaiToClaudeResponse(chunk, state) {
|
||||
if (!chunk || !chunk.choices?.[0]) return null;
|
||||
|
||||
const results = [];
|
||||
|
||||
@@ -2,6 +2,10 @@ import { translateResponse, initState } from "../translator/index.js";
|
||||
import { FORMATS } from "../translator/formats.js";
|
||||
import { saveRequestUsage, trackPendingRequest, appendRequestLog } from "@/lib/usageDb.js";
|
||||
|
||||
// Singleton TextEncoder/Decoder for performance (reuse across all streams)
|
||||
const sharedDecoder = new TextDecoder();
|
||||
const sharedEncoder = new TextEncoder();
|
||||
|
||||
// Get HH:MM:SS timestamp
|
||||
function getTimeString() {
|
||||
return new Date().toLocaleTimeString("en-US", { hour12: false, hour: "2-digit", minute: "2-digit", second: "2-digit" });
|
||||
@@ -91,9 +95,9 @@ function logUsage(provider, usage, model = null, connectionId = null) {
|
||||
});
|
||||
}
|
||||
|
||||
// Parse SSE data line
|
||||
// Parse SSE data line (optimized - reduce string operations)
|
||||
function parseSSELine(line) {
|
||||
if (!line || !line.startsWith("data:")) return null;
|
||||
if (!line || line.charCodeAt(0) !== 100) return null; // 'd' = 100
|
||||
|
||||
const data = line.slice(5).trim();
|
||||
if (data === "[DONE]") return { done: true };
|
||||
@@ -178,8 +182,6 @@ export function createSSEStream(options = {}) {
|
||||
connectionId = null
|
||||
} = options;
|
||||
|
||||
const decoder = new TextDecoder();
|
||||
const encoder = new TextEncoder();
|
||||
let buffer = "";
|
||||
let usage = null;
|
||||
|
||||
@@ -188,7 +190,7 @@ export function createSSEStream(options = {}) {
|
||||
|
||||
return new TransformStream({
|
||||
transform(chunk, controller) {
|
||||
const text = decoder.decode(chunk, { stream: true });
|
||||
const text = sharedDecoder.decode(chunk, { stream: true });
|
||||
buffer += text;
|
||||
reqLogger?.appendProviderChunk?.(text);
|
||||
|
||||
@@ -215,7 +217,7 @@ export function createSSEStream(options = {}) {
|
||||
output = line + "\n";
|
||||
}
|
||||
reqLogger?.appendConvertedChunk?.(output);
|
||||
controller.enqueue(encoder.encode(output));
|
||||
controller.enqueue(sharedEncoder.encode(output));
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -228,7 +230,7 @@ export function createSSEStream(options = {}) {
|
||||
if (parsed && parsed.done) {
|
||||
const output = "data: [DONE]\n\n";
|
||||
reqLogger?.appendConvertedChunk?.(output);
|
||||
controller.enqueue(encoder.encode(output));
|
||||
controller.enqueue(sharedEncoder.encode(output));
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -251,7 +253,7 @@ export function createSSEStream(options = {}) {
|
||||
for (const item of translated) {
|
||||
const output = formatSSE(item, sourceFormat);
|
||||
reqLogger?.appendConvertedChunk?.(output);
|
||||
controller.enqueue(encoder.encode(output));
|
||||
controller.enqueue(sharedEncoder.encode(output));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -260,7 +262,7 @@ export function createSSEStream(options = {}) {
|
||||
flush(controller) {
|
||||
trackPendingRequest(model, provider, connectionId, false);
|
||||
try {
|
||||
const remaining = decoder.decode();
|
||||
const remaining = sharedDecoder.decode();
|
||||
if (remaining) buffer += remaining;
|
||||
|
||||
if (mode === STREAM_MODE.PASSTHROUGH) {
|
||||
@@ -270,7 +272,7 @@ export function createSSEStream(options = {}) {
|
||||
output = "data: " + buffer.slice(5);
|
||||
}
|
||||
reqLogger?.appendConvertedChunk?.(output);
|
||||
controller.enqueue(encoder.encode(output));
|
||||
controller.enqueue(sharedEncoder.encode(output));
|
||||
}
|
||||
if (usage) {
|
||||
logUsage(provider, usage, model, connectionId);
|
||||
@@ -299,7 +301,7 @@ export function createSSEStream(options = {}) {
|
||||
for (const item of translated) {
|
||||
const output = formatSSE(item, sourceFormat);
|
||||
reqLogger?.appendConvertedChunk?.(output);
|
||||
controller.enqueue(encoder.encode(output));
|
||||
controller.enqueue(sharedEncoder.encode(output));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -320,14 +322,14 @@ export function createSSEStream(options = {}) {
|
||||
for (const item of flushed) {
|
||||
const output = formatSSE(item, sourceFormat);
|
||||
reqLogger?.appendConvertedChunk?.(output);
|
||||
controller.enqueue(encoder.encode(output));
|
||||
controller.enqueue(sharedEncoder.encode(output));
|
||||
}
|
||||
}
|
||||
|
||||
// Send [DONE] and log usage
|
||||
const doneOutput = "data: [DONE]\n\n";
|
||||
reqLogger?.appendConvertedChunk?.(doneOutput);
|
||||
controller.enqueue(encoder.encode(doneOutput));
|
||||
controller.enqueue(sharedEncoder.encode(doneOutput));
|
||||
|
||||
if (state?.usage) {
|
||||
logUsage(state.provider || targetFormat, state.usage, model, connectionId);
|
||||
|
||||
Reference in New Issue
Block a user