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:
@@ -134,7 +134,7 @@ const KIND_EXAMPLE_CONFIG = {
|
||||
function EmbeddingExampleCard({ providerId, customAlias }) {
|
||||
const isCustom = isCustomEmbeddingProvider(providerId);
|
||||
const providerAlias = isCustom ? (customAlias || providerId) : getProviderAlias(providerId);
|
||||
const embeddingModels = isCustom ? [] : getModelsByProviderId(providerId).filter((m) => m.type === "embedding");
|
||||
const embeddingModels = isCustom ? [] : getModelsByProviderId(providerId).filter((m) => (m.kind || m.type) === "embedding");
|
||||
|
||||
const [selectedModel, setSelectedModel] = useState(embeddingModels[0]?.id ?? "");
|
||||
const [input, setInput] = useState("The quick brown fox jumps over the lazy dog");
|
||||
@@ -431,7 +431,7 @@ function TtsExampleCard({ providerId }) {
|
||||
// Use per-model voices if available, else flat list
|
||||
const voices = (config.voicesPerModel && defaultModel)
|
||||
? (getTtsVoicesForModel(providerId, defaultModel) || [])
|
||||
: getModelsByProviderId(config.voiceKey || providerId).filter((m) => m.type === "tts");
|
||||
: getModelsByProviderId(config.voiceKey || providerId).filter((m) => (m.kind || m.type) === "tts");
|
||||
if (voices.length) {
|
||||
if (config.hasBrowseButton) {
|
||||
// Google TTS: pre-select "en" (English) as default, show as single voice chip
|
||||
@@ -475,7 +475,7 @@ function TtsExampleCard({ providerId }) {
|
||||
if (config.voiceSource === "hardcoded") {
|
||||
// Build languages/byLang from static providerModels data
|
||||
const voiceKey = config.voiceKey || providerId;
|
||||
const voices = getModelsByProviderId(voiceKey).filter((m) => m.type === "tts");
|
||||
const voices = getModelsByProviderId(voiceKey).filter((m) => (m.kind || m.type) === "tts");
|
||||
const byLangMap = {};
|
||||
for (const v of voices) {
|
||||
if (!byLangMap[v.id]) byLangMap[v.id] = { code: v.id, name: v.name, voices: [{ id: v.id, name: v.name }] };
|
||||
@@ -735,13 +735,13 @@ function TtsExampleCard({ providerId }) {
|
||||
<select
|
||||
value={selectedVoice}
|
||||
onChange={(e) => {
|
||||
const m = getModelsByProviderId(providerId).filter((m) => m.type === "tts").find((m) => m.id === e.target.value);
|
||||
const m = getModelsByProviderId(providerId).filter((m) => (m.kind || m.type) === "tts").find((m) => m.id === e.target.value);
|
||||
setSelectedVoice(e.target.value);
|
||||
setSelectedVoiceName(m?.name || e.target.value);
|
||||
}}
|
||||
className="w-full px-3 py-1.5 text-sm border border-border rounded-lg bg-background focus:outline-none focus:border-primary"
|
||||
>
|
||||
{getModelsByProviderId(providerId).filter((m) => m.type === "tts").map((m) => (
|
||||
{getModelsByProviderId(providerId).filter((m) => (m.kind || m.type) === "tts").map((m) => (
|
||||
<option key={m.id} value={m.id}>{m.name || m.id}</option>
|
||||
))}
|
||||
</select>
|
||||
@@ -925,7 +925,7 @@ function GenericExampleCard({ providerId, kind }) {
|
||||
const safeExConfig = exConfig || {};
|
||||
|
||||
// Get models for this kind (e.g., type="image")
|
||||
const kindModels = getModelsByProviderId(providerId).filter((m) => m.type === kind);
|
||||
const kindModels = getModelsByProviderId(providerId).filter((m) => (m.kind || m.type) === kind);
|
||||
// Kinds that need a model identifier in the request (image/video/music)
|
||||
const KIND_NEEDS_MODEL = new Set(["image", "video", "music", "imageToText"]);
|
||||
const needsModel = KIND_NEEDS_MODEL.has(kind);
|
||||
@@ -1429,7 +1429,7 @@ function GenericExampleCard({ providerId, kind }) {
|
||||
// ─── STT Example Card ────────────────────────────────────────────────────────
|
||||
function SttExampleCard({ providerId }) {
|
||||
const providerAlias = getProviderAlias(providerId);
|
||||
const builtinSttModels = getModelsByProviderId(providerId).filter((m) => m.type === "stt");
|
||||
const builtinSttModels = getModelsByProviderId(providerId).filter((m) => (m.kind || m.type) === "stt");
|
||||
const [customSttModels, setCustomSttModels] = useState([]);
|
||||
const sttModels = [...builtinSttModels, ...customSttModels];
|
||||
|
||||
@@ -1467,7 +1467,7 @@ function SttExampleCard({ providerId }) {
|
||||
fetch("/api/models/custom", { cache: "no-store" })
|
||||
.then((r) => r.json())
|
||||
.then((d) => {
|
||||
const list = (d.models || []).filter((m) => m.type === "stt" && m.providerAlias === providerAlias);
|
||||
const list = (d.models || []).filter((m) => (m.kind || m.type) === "stt" && m.providerAlias === providerAlias);
|
||||
setCustomSttModels(list);
|
||||
})
|
||||
.catch(() => {});
|
||||
|
||||
@@ -206,14 +206,14 @@ export default function ModelsCard({ providerId, kindFilter, providerAliasOverri
|
||||
const builtInModels = kindFilter
|
||||
? allBuiltIn.filter((m) => {
|
||||
if (m.kinds) return m.kinds.includes(kindFilter);
|
||||
return (m.type || "llm") === kindFilter;
|
||||
return (m.kind || m.type || "llm") === kindFilter;
|
||||
})
|
||||
: allBuiltIn;
|
||||
|
||||
// Custom models for this provider + kind, dedupe vs built-in
|
||||
const myCustomModels = customModels.filter(
|
||||
(m) => m.providerAlias === providerAlias
|
||||
&& (m.type || "llm") === effectiveType
|
||||
&& (m.kind || m.type || "llm") === effectiveType
|
||||
&& !builtInModels.some((b) => b.id === m.id)
|
||||
);
|
||||
|
||||
|
||||
@@ -122,6 +122,9 @@ export default function ProvidersPage() {
|
||||
|
||||
const sortByPriority = (entries, authType) =>
|
||||
[...entries].sort(([ka, a], [kb, b]) => {
|
||||
const pa = a.priority ?? 999;
|
||||
const pb = b.priority ?? 999;
|
||||
if (pa !== pb) return pa - pb;
|
||||
const sa = getProviderStats(ka, authType);
|
||||
const sb = getProviderStats(kb, authType);
|
||||
const ca = sa.connected > 0 ? 1 : 0;
|
||||
@@ -132,6 +135,9 @@ export default function ProvidersPage() {
|
||||
|
||||
const sortItemsByPriority = (items, authType) =>
|
||||
[...items].sort((a, b) => {
|
||||
const pa = a.priority ?? 999;
|
||||
const pb = b.priority ?? 999;
|
||||
if (pa !== pb) return pa - pb;
|
||||
const sa = getProviderStats(a.id, authType);
|
||||
const sb = getProviderStats(b.id, authType);
|
||||
const ca = sa.connected > 0 ? 1 : 0;
|
||||
@@ -273,15 +279,22 @@ export default function ProvidersPage() {
|
||||
}))
|
||||
.filter((p) => matchSearch(p.name));
|
||||
|
||||
const oauthEntries = Object.entries(OAUTH_PROVIDERS).filter(
|
||||
([, info]) => !info.hidden && matchSearch(info.name),
|
||||
const oauthEntries = sortByPriority(
|
||||
Object.entries(OAUTH_PROVIDERS).filter(([, info]) => !info.hidden && matchSearch(info.name)),
|
||||
"oauth",
|
||||
);
|
||||
const freeEntries = Object.entries(FREE_PROVIDERS).filter(
|
||||
([, info]) => !info.hidden && matchSearch(info.name),
|
||||
);
|
||||
const freeTierEntries = Object.entries(FREE_TIER_PROVIDERS).filter(
|
||||
([, info]) => !info.hidden && matchSearch(info.name),
|
||||
);
|
||||
const freeTierEntries = Object.entries(FREE_TIER_PROVIDERS)
|
||||
.filter(([, info]) => !info.hidden && matchSearch(info.name))
|
||||
.sort(([, a], [, b]) => {
|
||||
// hasFree providers first, then by priority
|
||||
const fa = a.hasFree ? 0 : 1;
|
||||
const fb = b.hasFree ? 0 : 1;
|
||||
if (fa !== fb) return fa - fb;
|
||||
return (a.priority ?? 999) - (b.priority ?? 999);
|
||||
});
|
||||
const apikeyEntries = sortByPriority(
|
||||
Object.entries(APIKEY_PROVIDERS).filter(
|
||||
([, info]) =>
|
||||
|
||||
@@ -2,9 +2,8 @@ import { getProviderConnectionById, updateProviderConnection } from "@/lib/local
|
||||
import { resolveConnectionProxyConfig } from "@/lib/network/connectionProxy";
|
||||
import { testProxyUrl } from "@/lib/network/proxyTest";
|
||||
import { isOpenAICompatibleProvider, isAnthropicCompatibleProvider } from "@/shared/constants/providers";
|
||||
import { PROVIDER_ENDPOINTS } from "@/shared/constants/config";
|
||||
import { getDefaultModel } from "open-sse/config/providerModels.js";
|
||||
import { resolveOllamaLocalHost } from "open-sse/config/providers.js";
|
||||
import { resolveOllamaLocalHost, PROVIDERS } from "open-sse/config/providers.js";
|
||||
import {
|
||||
refreshProviderCredentials,
|
||||
shouldRefreshCredentials,
|
||||
@@ -474,7 +473,7 @@ async function testApiKeyConnection(connection, effectiveProxy = null) {
|
||||
}
|
||||
case "volcengine-ark":
|
||||
case "byteplus": {
|
||||
const res = await fetchWithConnectionProxy(PROVIDER_ENDPOINTS[connection.provider], {
|
||||
const res = await fetchWithConnectionProxy(PROVIDERS[connection.provider]?.baseUrl, {
|
||||
method: "POST",
|
||||
headers: { "Authorization": `Bearer ${connection.apiKey}`, "content-type": "application/json" },
|
||||
body: JSON.stringify({ model: getDefaultModel(connection.provider), max_tokens: 1, messages: [{ role: "user", content: "test" }] }),
|
||||
|
||||
@@ -3,8 +3,7 @@ import { getProviderNodeById } from "@/models";
|
||||
import { isOpenAICompatibleProvider, isAnthropicCompatibleProvider, isCustomEmbeddingProvider, AI_PROVIDERS } from "@/shared/constants/providers";
|
||||
import { getDefaultModel } from "open-sse/config/providerModels.js";
|
||||
import { resolveOllamaLocalHost, resolveXiaomiTokenplanBaseUrl, PROVIDERS } from "open-sse/config/providers.js";
|
||||
import { openaiToCommandCode } from "open-sse/translator/request/openai-to-commandcode.js";
|
||||
import { PROVIDER_ENDPOINTS } from "@/shared/constants/config";
|
||||
import { openaiToCommandCodeRequest } from "open-sse/translator/request/openai-to-commandcode.js";
|
||||
import { normalizeProviderId } from "@/lib/providerNormalization";
|
||||
|
||||
// Probe a webSearch/webFetch provider using its searchConfig/fetchConfig.
|
||||
@@ -326,7 +325,7 @@ export async function POST(request) {
|
||||
}
|
||||
case "volcengine-ark":
|
||||
case "byteplus": {
|
||||
const res = await fetch(PROVIDER_ENDPOINTS[provider], {
|
||||
const res = await fetch(PROVIDERS[provider]?.baseUrl, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Authorization": `Bearer ${apiKey}`,
|
||||
@@ -400,7 +399,7 @@ export async function POST(request) {
|
||||
case "commandcode": {
|
||||
const cfg = PROVIDERS.commandcode;
|
||||
const model = getDefaultModel("commandcode");
|
||||
const payload = openaiToCommandCode(model, {
|
||||
const payload = openaiToCommandCodeRequest(model, {
|
||||
messages: [{ role: "user", content: "ping" }],
|
||||
max_tokens: 1,
|
||||
stream: false,
|
||||
|
||||
@@ -40,7 +40,8 @@ function buildInfo({ alias, providerId, model, kind, providerInfo }) {
|
||||
}
|
||||
|
||||
// id format: "{alias}/{modelId}" - alias may also be providerId
|
||||
function lookup(fullId) {
|
||||
// requestedKind: optional, disambiguates duplicate ids across kinds (e.g. gemini-2.5-pro llm vs stt)
|
||||
function lookup(fullId, requestedKind) {
|
||||
if (!fullId || !fullId.includes("/")) return null;
|
||||
const slash = fullId.indexOf("/");
|
||||
const alias = fullId.slice(0, slash);
|
||||
@@ -50,7 +51,9 @@ function lookup(fullId) {
|
||||
|
||||
// PROVIDER_MODELS lookup (by alias key, fallback to providerId)
|
||||
const list = PROVIDER_MODELS[alias] || PROVIDER_MODELS[providerId] || [];
|
||||
const m = list.find((x) => x.id === modelId);
|
||||
const m = requestedKind
|
||||
? list.find((x) => x.id === modelId && (x.kind || x.type || "llm") === requestedKind)
|
||||
: list.find((x) => x.id === modelId);
|
||||
if (m) {
|
||||
const kind = m.kind || m.type || "llm";
|
||||
return buildInfo({ alias, providerId, model: m, kind, providerInfo });
|
||||
@@ -82,13 +85,14 @@ export async function OPTIONS() {
|
||||
export async function GET(request) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const id = searchParams.get("id");
|
||||
const kind = searchParams.get("kind");
|
||||
if (!id) {
|
||||
return Response.json(
|
||||
{ error: { message: "Missing required query param: id (e.g. ?id=openai/dall-e-3)", type: "invalid_request_error" } },
|
||||
{ status: 400, headers: { "Access-Control-Allow-Origin": "*" } },
|
||||
);
|
||||
}
|
||||
const info = lookup(id);
|
||||
const info = lookup(id, kind);
|
||||
if (!info) {
|
||||
return Response.json(
|
||||
{ error: { message: `Model not found: ${id}`, type: "not_found" } },
|
||||
|
||||
Reference in New Issue
Block a user