mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 13:38:31 +00:00
refactor(open-sse): translator DRY + schema enums, bug fixes, dead code cleanup
- Bug B1-B7: media UI m.kind||m.type, serviceKinds, gemini mediaPriority, schema kind, models/info lookup by kind - Dead code D1-D6: safeParseJSON, drop PROVIDER_ENDPOINTS, orphan fetcher, GITHUB_CONFIG derive, getProviderConfig internal, legacy kiro file - Translator concerns: toOpenAIUsage, toOpenAIFinish (gemini/kiro/ollama + fix kiro tool finish), thinking effort maps - Reorg helpers/ → concerns/ (logic) + formats/ (per-format) + schema/ (pure enums: roles/blocks/finishReasons/defaults) - Wire ~280 hardcoded role/block/finish/default literals to schema enums across 20+ files - collapseTextParts + extractTextContent dedup - Normalize translator fn names to openaiToXRequest / xToOpenAIResponse - Golden tests lock behavior; 0 regression (byte-for-byte providers/alias, 26=26 known fails) Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
// Build OpenAI chat.completion.chunk. Caller supplies id/created/model so each
|
||||
// translator keeps its exact id-generation + created semantics (no Date.now here).
|
||||
export function buildChunk({ id, created, model }, delta, finishReason = null) {
|
||||
return {
|
||||
id,
|
||||
object: "chat.completion.chunk",
|
||||
created,
|
||||
model,
|
||||
choices: [{ index: 0, delta, finish_reason: finishReason }],
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
// Concern #6: finish_reason / stop_reason mapping.
|
||||
// One entry per direction; switch by special format, default handles common providers.
|
||||
import { OPENAI_FINISH, CLAUDE_STOP, GEMINI_FINISH } from "../schema/finishReasons.js";
|
||||
|
||||
// upstream finish/stop reason → OpenAI finish_reason
|
||||
export function toOpenAIFinish(reason, format) {
|
||||
switch (format) {
|
||||
case "claude":
|
||||
switch (reason) {
|
||||
case CLAUDE_STOP.END_TURN: return OPENAI_FINISH.STOP;
|
||||
case CLAUDE_STOP.MAX_TOKENS: return OPENAI_FINISH.LENGTH;
|
||||
case CLAUDE_STOP.TOOL_USE: return OPENAI_FINISH.TOOL_CALLS;
|
||||
case CLAUDE_STOP.STOP_SEQUENCE: return OPENAI_FINISH.STOP;
|
||||
default: return OPENAI_FINISH.STOP;
|
||||
}
|
||||
case "commandcode":
|
||||
switch (reason) {
|
||||
case "stop": return OPENAI_FINISH.STOP;
|
||||
case "length": return OPENAI_FINISH.LENGTH;
|
||||
case "tool-calls":
|
||||
case "tool_use": return OPENAI_FINISH.TOOL_CALLS;
|
||||
case "content-filter": return OPENAI_FINISH.CONTENT_FILTER;
|
||||
case "error": return OPENAI_FINISH.STOP;
|
||||
default: return reason || OPENAI_FINISH.STOP;
|
||||
}
|
||||
case "gemini":
|
||||
switch (String(reason).toUpperCase()) {
|
||||
case GEMINI_FINISH.STOP: return OPENAI_FINISH.STOP;
|
||||
case GEMINI_FINISH.MAX_TOKENS: return OPENAI_FINISH.LENGTH;
|
||||
case GEMINI_FINISH.SAFETY:
|
||||
case GEMINI_FINISH.RECITATION:
|
||||
case GEMINI_FINISH.BLOCKLIST:
|
||||
case GEMINI_FINISH.PROHIBITED_CONTENT: return OPENAI_FINISH.CONTENT_FILTER;
|
||||
default: return OPENAI_FINISH.STOP;
|
||||
}
|
||||
case "kiro":
|
||||
case "ollama":
|
||||
switch (reason) {
|
||||
case "tool_calls":
|
||||
case "tool_use": return OPENAI_FINISH.TOOL_CALLS;
|
||||
case "length":
|
||||
case "max_tokens": return OPENAI_FINISH.LENGTH;
|
||||
default: return OPENAI_FINISH.STOP;
|
||||
}
|
||||
default:
|
||||
return reason || OPENAI_FINISH.STOP;
|
||||
}
|
||||
}
|
||||
|
||||
// OpenAI finish_reason → upstream stop reason
|
||||
export function fromOpenAIFinish(reason, format) {
|
||||
switch (format) {
|
||||
case "claude":
|
||||
switch (reason) {
|
||||
case OPENAI_FINISH.STOP: return CLAUDE_STOP.END_TURN;
|
||||
case OPENAI_FINISH.LENGTH: return CLAUDE_STOP.MAX_TOKENS;
|
||||
case OPENAI_FINISH.TOOL_CALLS: return CLAUDE_STOP.TOOL_USE;
|
||||
default: return CLAUDE_STOP.END_TURN;
|
||||
}
|
||||
default:
|
||||
return reason;
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
// Concern #6: finish_reason / stop_reason mapping.
|
||||
// One entry per direction; switch by special format, default handles common providers.
|
||||
|
||||
// upstream finish/stop reason → OpenAI finish_reason
|
||||
export function toOpenAIFinish(reason, format) {
|
||||
switch (format) {
|
||||
case "claude":
|
||||
switch (reason) {
|
||||
case "end_turn": return "stop";
|
||||
case "max_tokens": return "length";
|
||||
case "tool_use": return "tool_calls";
|
||||
case "stop_sequence": return "stop";
|
||||
default: return "stop";
|
||||
}
|
||||
case "commandcode":
|
||||
switch (reason) {
|
||||
case "stop": return "stop";
|
||||
case "length": return "length";
|
||||
case "tool-calls":
|
||||
case "tool_use": return "tool_calls";
|
||||
case "content-filter": return "content_filter";
|
||||
case "error": return "stop";
|
||||
default: return reason || "stop";
|
||||
}
|
||||
default:
|
||||
return reason || "stop";
|
||||
}
|
||||
}
|
||||
|
||||
// OpenAI finish_reason → upstream stop reason
|
||||
export function fromOpenAIFinish(reason, format) {
|
||||
switch (format) {
|
||||
case "claude":
|
||||
switch (reason) {
|
||||
case "stop": return "end_turn";
|
||||
case "length": return "max_tokens";
|
||||
case "tool_calls": return "tool_use";
|
||||
default: return "end_turn";
|
||||
}
|
||||
default:
|
||||
return reason;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
// Build a base64 data URI from mime + base64 payload
|
||||
export function encodeDataUri(mimeType, base64) {
|
||||
return `data:${mimeType};base64,${base64}`;
|
||||
}
|
||||
|
||||
// Parse a base64 data URI → { mimeType, base64 }, or null if not a data URI.
|
||||
// [\s\S] tolerates newlines inside the base64 payload.
|
||||
const DATA_URI_RE = /^data:([^;]+);base64,([\s\S]+)$/;
|
||||
export function parseDataUri(url) {
|
||||
if (typeof url !== "string") return null;
|
||||
const m = url.match(DATA_URI_RE);
|
||||
return m ? { mimeType: m[1], base64: m[2] } : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a remote image URL and return it as a base64 data URI.
|
||||
* Used when upstream providers (Codex, etc.) require inline base64 images
|
||||
* instead of remote URLs they cannot fetch.
|
||||
* Returns null if fetch fails.
|
||||
*
|
||||
* @param {string} imageUrl - HTTP(S) URL of the image
|
||||
* @param {object} options - { signal, timeoutMs }
|
||||
* @returns {Promise<{url: string, mimeType: string}|null>}
|
||||
*/
|
||||
export async function fetchImageAsBase64(imageUrl, options = {}) {
|
||||
const { signal, timeoutMs = 10000 } = options;
|
||||
if (!imageUrl || (!imageUrl.startsWith("http://") && !imageUrl.startsWith("https://"))) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const controller = new AbortController();
|
||||
const timeout = signal ? null : setTimeout(() => controller.abort(), timeoutMs);
|
||||
const fetchSignal = signal || controller.signal;
|
||||
|
||||
try {
|
||||
const response = await fetch(imageUrl, { signal: fetchSignal });
|
||||
if (!response.ok) return null;
|
||||
|
||||
const mimeType = response.headers.get("Content-Type") || "image/jpeg";
|
||||
const arrayBuffer = await response.arrayBuffer();
|
||||
const base64 = Buffer.from(arrayBuffer).toString("base64");
|
||||
return { url: `data:${mimeType};base64,${base64}`, mimeType };
|
||||
} catch {
|
||||
return null;
|
||||
} finally {
|
||||
if (timeout) clearTimeout(timeout);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
// Safe JSON.parse: non-string passthrough; on parse error return caller-chosen `fallback`.
|
||||
export function safeParseJSON(str, fallback) {
|
||||
if (typeof str !== "string") return str;
|
||||
try { return JSON.parse(str); } catch { return fallback; }
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { OPENAI_BLOCK } from "../schema/index.js";
|
||||
|
||||
// Collapse an OpenAI content-part array: a lone text part becomes a plain string,
|
||||
// otherwise the array is returned as-is. Matches existing translator behavior.
|
||||
export function collapseTextParts(parts) {
|
||||
return parts.length === 1 && parts[0].type === OPENAI_BLOCK.TEXT ? parts[0].text : parts;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import { ROLE } from "../schema/index.js";
|
||||
|
||||
// Build OpenAI delta carrying reasoning_content (optional leading assistant role)
|
||||
export function reasoningDelta(text, withRole = false) {
|
||||
return withRole
|
||||
? { role: ROLE.ASSISTANT, reasoning_content: text }
|
||||
: { reasoning_content: text };
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// Concern: reasoning_effort ↔ provider-native thinking config.
|
||||
// Each provider expresses "how much to think" differently — centralize the maps here.
|
||||
// Streaming reasoning delta shape lives in reasoning.js; this file is request-side config only.
|
||||
|
||||
// OpenAI reasoning_effort → Claude thinking.budget_tokens
|
||||
const EFFORT_TO_BUDGET = { none: 0, low: 4096, medium: 8192, high: 16384, xhigh: 32768 };
|
||||
|
||||
// Returns budget_tokens for a reasoning_effort, or undefined if unknown effort.
|
||||
// 0 means "no thinking" (caller skips enabling); undefined means "effort not recognized".
|
||||
export function effortToBudget(effort) {
|
||||
if (!effort) return undefined;
|
||||
return EFFORT_TO_BUDGET[String(effort).toLowerCase()];
|
||||
}
|
||||
|
||||
// OpenAI reasoning_effort → Gemini thinkingLevel (gemini-3 enum: minimal|low|medium|high).
|
||||
// Gemini 3 cannot fully disable thinking; "none"/"off" map to "minimal" (closest to off).
|
||||
export function effortToThinkingLevel(effort) {
|
||||
const e = String(effort).toLowerCase().trim();
|
||||
return e === "none" || e === "off" ? "minimal" : e;
|
||||
}
|
||||
|
||||
// Gemini thinkingBudget (numeric) → OpenAI reasoning_effort (antigravity reverse map).
|
||||
// Returns null when budget <= 0 (no reasoning).
|
||||
export function budgetToEffort(budget) {
|
||||
if (!budget || budget <= 0) return null;
|
||||
if (budget <= 2048) return "low";
|
||||
if (budget <= 16384) return "medium";
|
||||
return "high";
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
// Tool call helper functions for translator
|
||||
|
||||
// Anthropic tool_use.id must match: ^[a-zA-Z0-9_-]+$
|
||||
const TOOL_ID_PATTERN = /^[a-zA-Z0-9_-]+$/;
|
||||
|
||||
// Fallback streaming tool_call id when provider omits one (index optional)
|
||||
export function fallbackToolCallId(index) {
|
||||
return index === undefined ? `call_${Date.now()}` : `call_${index}_${Date.now()}`;
|
||||
}
|
||||
|
||||
// Generate deterministic tool call ID from position + tool name (cache-friendly)
|
||||
export function generateToolCallId(msgIndex = 0, tcIndex = 0, toolName = "") {
|
||||
const name = toolName ? `_${toolName.replace(/[^a-zA-Z0-9_-]/g, "")}` : "";
|
||||
return `call_msg${msgIndex}_tc${tcIndex}${name}`;
|
||||
}
|
||||
|
||||
// Sanitize ID to match Anthropic pattern: keep only alphanumeric, underscore, hyphen
|
||||
function sanitizeToolId(id) {
|
||||
if (!id || typeof id !== "string") return null;
|
||||
const sanitized = id.replace(/[^a-zA-Z0-9_-]/g, "");
|
||||
return sanitized.length > 0 ? sanitized : null;
|
||||
}
|
||||
|
||||
// Ensure all tool_calls have valid id field and arguments is string (some providers require it)
|
||||
export function ensureToolCallIds(body) {
|
||||
if (!body.messages || !Array.isArray(body.messages)) return body;
|
||||
|
||||
for (let i = 0; i < body.messages.length; i++) {
|
||||
const msg = body.messages[i];
|
||||
if (msg.role === "assistant" && msg.tool_calls && Array.isArray(msg.tool_calls)) {
|
||||
for (let j = 0; j < msg.tool_calls.length; j++) {
|
||||
const tc = msg.tool_calls[j];
|
||||
// Validate or regenerate ID for Anthropic compatibility
|
||||
if (!tc.id || !TOOL_ID_PATTERN.test(tc.id)) {
|
||||
const sanitized = sanitizeToolId(tc.id);
|
||||
tc.id = sanitized || generateToolCallId(i, j, tc.function?.name);
|
||||
}
|
||||
if (!tc.type) {
|
||||
tc.type = "function";
|
||||
}
|
||||
// Ensure arguments is JSON string, not object
|
||||
if (tc.function?.arguments && typeof tc.function.arguments !== "string") {
|
||||
tc.function.arguments = JSON.stringify(tc.function.arguments);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Validate tool_call_id in tool messages (role: "tool")
|
||||
if (msg.role === "tool" && msg.tool_call_id && !TOOL_ID_PATTERN.test(msg.tool_call_id)) {
|
||||
const sanitized = sanitizeToolId(msg.tool_call_id);
|
||||
msg.tool_call_id = sanitized || generateToolCallId(i, 0);
|
||||
}
|
||||
|
||||
// Also validate tool_use blocks in content (Claude format)
|
||||
if (Array.isArray(msg.content)) {
|
||||
for (let k = 0; k < msg.content.length; k++) {
|
||||
const block = msg.content[k];
|
||||
if (block.type === "tool_use" && block.id && !TOOL_ID_PATTERN.test(block.id)) {
|
||||
const sanitized = sanitizeToolId(block.id);
|
||||
block.id = sanitized || generateToolCallId(i, k, block.name);
|
||||
}
|
||||
// Validate tool_use_id in tool_result blocks
|
||||
if (block.type === "tool_result" && block.tool_use_id && !TOOL_ID_PATTERN.test(block.tool_use_id)) {
|
||||
const sanitized = sanitizeToolId(block.tool_use_id);
|
||||
block.tool_use_id = sanitized || generateToolCallId(i, k);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return body;
|
||||
}
|
||||
|
||||
// Get tool_call ids from assistant message (OpenAI format: tool_calls, Claude format: tool_use in content)
|
||||
export function getToolCallIds(msg) {
|
||||
if (msg.role !== "assistant") return [];
|
||||
|
||||
const ids = [];
|
||||
|
||||
// OpenAI format: tool_calls array
|
||||
if (msg.tool_calls && Array.isArray(msg.tool_calls)) {
|
||||
for (const tc of msg.tool_calls) {
|
||||
if (tc.id) ids.push(tc.id);
|
||||
}
|
||||
}
|
||||
|
||||
// Claude format: tool_use blocks in content
|
||||
if (Array.isArray(msg.content)) {
|
||||
for (const block of msg.content) {
|
||||
if (block.type === "tool_use" && block.id) {
|
||||
ids.push(block.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ids;
|
||||
}
|
||||
|
||||
// Check if user message has tool_result for given ids (OpenAI format: role=tool, Claude format: tool_result in content)
|
||||
export function hasToolResults(msg, toolCallIds) {
|
||||
if (!msg || !toolCallIds.length) return false;
|
||||
|
||||
// OpenAI format: role = "tool" with tool_call_id
|
||||
if (msg.role === "tool" && msg.tool_call_id) {
|
||||
return toolCallIds.includes(msg.tool_call_id);
|
||||
}
|
||||
|
||||
// Claude format: tool_result blocks in user message content
|
||||
if (msg.role === "user" && Array.isArray(msg.content)) {
|
||||
for (const block of msg.content) {
|
||||
if (block.type === "tool_result" && toolCallIds.includes(block.tool_use_id)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Fix missing tool responses - insert empty tool_result if assistant has tool_use but next message has no tool_result
|
||||
export function fixMissingToolResponses(body) {
|
||||
if (!body.messages || !Array.isArray(body.messages)) return body;
|
||||
|
||||
const newMessages = [];
|
||||
|
||||
for (let i = 0; i < body.messages.length; i++) {
|
||||
const msg = body.messages[i];
|
||||
const nextMsg = body.messages[i + 1];
|
||||
|
||||
newMessages.push(msg);
|
||||
|
||||
// Check if this is assistant with tool_calls/tool_use
|
||||
const toolCallIds = getToolCallIds(msg);
|
||||
if (toolCallIds.length === 0) continue;
|
||||
|
||||
// Check if next message has tool_result
|
||||
if (nextMsg && !hasToolResults(nextMsg, toolCallIds)) {
|
||||
// Insert tool responses for each tool_call
|
||||
for (const id of toolCallIds) {
|
||||
// OpenAI format: role = "tool"
|
||||
newMessages.push({
|
||||
role: "tool",
|
||||
tool_call_id: id,
|
||||
content: ""
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
body.messages = newMessages;
|
||||
return body;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
// Build OpenAI usage object. Caller computes prompt/completion/total (provider math).
|
||||
// Optional details added only when > 0 (matches existing claude/gemini/codex behavior).
|
||||
export function buildUsage({ promptTokens, completionTokens, totalTokens, cachedTokens = 0, cacheCreationTokens = 0, reasoningTokens = 0 }) {
|
||||
const usage = { prompt_tokens: promptTokens, completion_tokens: completionTokens, total_tokens: totalTokens };
|
||||
if (cachedTokens > 0 || cacheCreationTokens > 0) {
|
||||
usage.prompt_tokens_details = {};
|
||||
if (cachedTokens > 0) usage.prompt_tokens_details.cached_tokens = cachedTokens;
|
||||
if (cacheCreationTokens > 0) usage.prompt_tokens_details.cache_creation_tokens = cacheCreationTokens;
|
||||
}
|
||||
if (reasoningTokens > 0) {
|
||||
usage.completion_tokens_details = { reasoning_tokens: reasoningTokens };
|
||||
}
|
||||
return usage;
|
||||
}
|
||||
|
||||
const n = (v) => (typeof v === "number" ? v : 0);
|
||||
|
||||
// Per-provider raw token field-map + math. Returns buildUsage() args (NOT the usage object).
|
||||
// Keeps each provider's exact semantics: claude/gemini fold cache+reasoning, others don't.
|
||||
const USAGE_EXTRACTORS = {
|
||||
claude(raw) {
|
||||
const input = n(raw.input_tokens), output = n(raw.output_tokens);
|
||||
const cacheRead = n(raw.cache_read_input_tokens), cacheCreate = n(raw.cache_creation_input_tokens);
|
||||
const prompt = input + cacheRead + cacheCreate;
|
||||
return { promptTokens: prompt, completionTokens: output, totalTokens: prompt + output, cachedTokens: cacheRead, cacheCreationTokens: cacheCreate };
|
||||
},
|
||||
gemini(raw) {
|
||||
const cached = n(raw.cachedContentTokenCount);
|
||||
const prompt = n(raw.promptTokenCount);
|
||||
const thoughts = n(raw.thoughtsTokenCount);
|
||||
const total = n(raw.totalTokenCount);
|
||||
let candidates = n(raw.candidatesTokenCount);
|
||||
// Fallback: derive candidates from total when upstream omits it
|
||||
if (candidates === 0 && total > 0) {
|
||||
candidates = total - prompt - thoughts;
|
||||
if (candidates < 0) candidates = 0;
|
||||
}
|
||||
return { promptTokens: prompt, completionTokens: candidates + thoughts, totalTokens: total, cachedTokens: cached, reasoningTokens: thoughts };
|
||||
},
|
||||
kiro(raw) {
|
||||
const input = n(raw.inputTokens), output = n(raw.outputTokens);
|
||||
return { promptTokens: input, completionTokens: output, totalTokens: input + output };
|
||||
},
|
||||
ollama(raw) {
|
||||
const input = n(raw.prompt_eval_count), output = n(raw.eval_count);
|
||||
return { promptTokens: input, completionTokens: output, totalTokens: input + output };
|
||||
},
|
||||
commandcode(raw) {
|
||||
const input = n(raw.inputTokens), output = n(raw.outputTokens);
|
||||
const total = typeof raw.totalTokens === "number" ? raw.totalTokens : input + output;
|
||||
return { promptTokens: input, completionTokens: output, totalTokens: total };
|
||||
},
|
||||
};
|
||||
|
||||
// Convert provider-native usage object → OpenAI usage. Returns null if no extractor/raw.
|
||||
export function toOpenAIUsage(raw, kind) {
|
||||
const extract = USAGE_EXTRACTORS[kind];
|
||||
if (!extract || !raw || typeof raw !== "object") return null;
|
||||
return buildUsage(extract(raw));
|
||||
}
|
||||
Reference in New Issue
Block a user