mirror of
https://github.com/Nezumi-2711/9router.git
synced 2026-09-22 20:00:47 +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:
@@ -12,6 +12,8 @@
|
||||
import { register } from "../index.js";
|
||||
import { FORMATS } from "../formats.js";
|
||||
import { randomUUID } from "crypto";
|
||||
import { ROLE, OPENAI_BLOCK } from "../schema/index.js";
|
||||
import { DEFAULT_MAX_TOKENS } from "../../config/runtimeConfig.js";
|
||||
|
||||
function flattenText(content) {
|
||||
if (content == null) return "";
|
||||
@@ -28,26 +30,26 @@ function flattenText(content) {
|
||||
}
|
||||
|
||||
function toContentBlocks(content) {
|
||||
if (content == null) return [{ type: "text", text: "" }];
|
||||
if (typeof content === "string") return [{ type: "text", text: content }];
|
||||
if (content == null) return [{ type: OPENAI_BLOCK.TEXT, text: "" }];
|
||||
if (typeof content === "string") return [{ type: OPENAI_BLOCK.TEXT, text: content }];
|
||||
if (Array.isArray(content)) {
|
||||
const blocks = [];
|
||||
for (const part of content) {
|
||||
if (typeof part === "string") {
|
||||
blocks.push({ type: "text", text: part });
|
||||
blocks.push({ type: OPENAI_BLOCK.TEXT, text: part });
|
||||
} else if (part && typeof part === "object") {
|
||||
if (part.type === "text" && typeof part.text === "string") {
|
||||
blocks.push({ type: "text", text: part.text });
|
||||
} else if (part.type === "image_url" || part.type === "image") {
|
||||
blocks.push({ type: "text", text: "[image omitted]" });
|
||||
if (part.type === OPENAI_BLOCK.TEXT && typeof part.text === "string") {
|
||||
blocks.push({ type: OPENAI_BLOCK.TEXT, text: part.text });
|
||||
} else if (part.type === OPENAI_BLOCK.IMAGE_URL || part.type === OPENAI_BLOCK.IMAGE) {
|
||||
blocks.push({ type: OPENAI_BLOCK.TEXT, text: "[image omitted]" });
|
||||
} else if (typeof part.text === "string") {
|
||||
blocks.push({ type: "text", text: part.text });
|
||||
blocks.push({ type: OPENAI_BLOCK.TEXT, text: part.text });
|
||||
}
|
||||
}
|
||||
}
|
||||
return blocks.length ? blocks : [{ type: "text", text: "" }];
|
||||
return blocks.length ? blocks : [{ type: OPENAI_BLOCK.TEXT, text: "" }];
|
||||
}
|
||||
return [{ type: "text", text: String(content) }];
|
||||
return [{ type: OPENAI_BLOCK.TEXT, text: String(content) }];
|
||||
}
|
||||
|
||||
function safeParseJson(s) {
|
||||
@@ -64,16 +66,16 @@ function convertMessages(messages = []) {
|
||||
if (!m) continue;
|
||||
const role = m.role;
|
||||
|
||||
if (role === "system") {
|
||||
if (role === ROLE.SYSTEM) {
|
||||
const t = flattenText(m.content);
|
||||
if (t) systemTexts.push(t);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (role === "tool") {
|
||||
if (role === ROLE.TOOL) {
|
||||
const value = typeof m.content === "string" ? m.content : flattenText(m.content);
|
||||
out.push({
|
||||
role: "tool",
|
||||
role: ROLE.TOOL,
|
||||
content: [{
|
||||
type: "tool-result",
|
||||
toolCallId: m.tool_call_id || "",
|
||||
@@ -84,10 +86,10 @@ function convertMessages(messages = []) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (role === "assistant") {
|
||||
if (role === ROLE.ASSISTANT) {
|
||||
const blocks = [];
|
||||
const text = flattenText(m.content);
|
||||
if (text) blocks.push({ type: "text", text });
|
||||
if (text) blocks.push({ type: OPENAI_BLOCK.TEXT, text });
|
||||
if (Array.isArray(m.tool_calls)) {
|
||||
for (const tc of m.tool_calls) {
|
||||
const fn = tc.function || {};
|
||||
@@ -99,11 +101,11 @@ function convertMessages(messages = []) {
|
||||
});
|
||||
}
|
||||
}
|
||||
out.push({ role: "assistant", content: blocks.length ? blocks : [{ type: "text", text: "" }] });
|
||||
out.push({ role: ROLE.ASSISTANT, content: blocks.length ? blocks : [{ type: OPENAI_BLOCK.TEXT, text: "" }] });
|
||||
continue;
|
||||
}
|
||||
|
||||
out.push({ role: "user", content: toContentBlocks(m.content) });
|
||||
out.push({ role: ROLE.USER, content: toContentBlocks(m.content) });
|
||||
}
|
||||
|
||||
return { messages: out, system: systemTexts.join("\n\n") };
|
||||
@@ -114,7 +116,7 @@ function convertTools(tools) {
|
||||
const result = [];
|
||||
for (const t of tools) {
|
||||
if (!t) continue;
|
||||
if (t.type === "function" && t.function) {
|
||||
if (t.type === OPENAI_BLOCK.FUNCTION && t.function) {
|
||||
result.push({
|
||||
name: t.function.name,
|
||||
description: t.function.description,
|
||||
@@ -131,13 +133,13 @@ function convertTools(tools) {
|
||||
return result.length ? result : undefined;
|
||||
}
|
||||
|
||||
export function openaiToCommandCode(model, body, stream /* , credentials */) {
|
||||
export function openaiToCommandCodeRequest(model, body, stream /* , credentials */) {
|
||||
const { messages, system } = convertMessages(body.messages);
|
||||
const params = {
|
||||
model,
|
||||
messages,
|
||||
stream: stream !== false,
|
||||
max_tokens: body.max_tokens ?? body.max_output_tokens ?? 64000,
|
||||
max_tokens: body.max_tokens ?? body.max_output_tokens ?? DEFAULT_MAX_TOKENS,
|
||||
temperature: body.temperature ?? 0.3,
|
||||
};
|
||||
|
||||
@@ -167,4 +169,4 @@ export function openaiToCommandCode(model, body, stream /* , credentials */) {
|
||||
};
|
||||
}
|
||||
|
||||
register(FORMATS.OPENAI, FORMATS.COMMANDCODE, openaiToCommandCode, null);
|
||||
register(FORMATS.OPENAI, FORMATS.COMMANDCODE, openaiToCommandCodeRequest, null);
|
||||
|
||||
Reference in New Issue
Block a user