fix(translator): resolve custom provider prefix in debug endpoint

Use getModelInfo instead of parseModel in /api/translator/translate
so custom OpenAI/Anthropic-compatible provider prefixes resolve
correctly, aligning the debug path with the runtime chat path.

Fixes #1083

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
hamsa0x7
2026-06-26 17:20:57 +07:00
committed by decolua
parent 4a54824f7f
commit 90b336d9dd
2 changed files with 58 additions and 3 deletions

View File

@@ -2,7 +2,7 @@ import { NextResponse } from "next/server";
import { detectFormat, getTargetFormat } from "open-sse/services/provider.js";
import { translateRequest } from "open-sse/translator/index.js";
import { FORMATS } from "open-sse/translator/formats.js";
import { parseModel } from "open-sse/services/model.js";
import { getModelInfo } from "@/sse/services/model.js";
import { getProviderConnections } from "@/lib/localDb.js";
import { getExecutor } from "open-sse/executors/index.js";
@@ -18,7 +18,7 @@ export async function POST(request) {
case 1: {
// Detect provider + formats from 1_req_client.json
const clientBody = body.body || body;
const { provider, model } = parseModel(clientBody.model);
const { provider, model } = await getModelInfo(clientBody.model);
const sourceFormat = detectFormat(clientBody);
const targetFormat = getTargetFormat(provider);
return NextResponse.json({ success: true, result: { provider, model, sourceFormat, targetFormat } });
@@ -28,7 +28,7 @@ export async function POST(request) {
// source → OpenAI intermediate (mirrors 3_req_openai.json)
// Translate source→openai only (half of the pipeline)
const clientBody = body.body || body;
const { provider, model } = parseModel(clientBody.model);
const { provider, model } = await getModelInfo(clientBody.model);
const sourceFormat = detectFormat(clientBody);
const stream = clientBody.stream !== false;

View File

@@ -0,0 +1,55 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
// Mock getModelInfo to simulate custom provider prefix resolution
vi.mock("@/sse/services/model.js", () => ({
getModelInfo: vi.fn(async (modelId) => {
// Simulate prefix resolution for custom providers
if (modelId.startsWith("my-prefix/")) {
return {
provider: "openai-compatible-chat-abc123",
model: modelId.replace("my-prefix/", "")
};
}
if (modelId.startsWith("acme/")) {
return {
provider: "anthropic-compatible-claude-xyz",
model: modelId.replace("acme/", "")
};
}
// Default: no prefix
return {
provider: modelId.split("/")[1] || "unknown",
model: modelId.split("/")[1] || modelId
};
})
}));
describe("Translator custom provider prefix resolution", () => {
beforeEach(() => {
vi.clearAllMocks();
});
it("should resolve OpenAI-compatible provider prefix via getModelInfo", async () => {
const { getModelInfo } = await import("@/sse/services/model.js");
const result = await getModelInfo("my-prefix/gpt-4");
expect(result.provider).toBe("openai-compatible-chat-abc123");
expect(result.model).toBe("gpt-4");
});
it("should resolve Anthropic-compatible provider prefix via getModelInfo", async () => {
const { getModelInfo } = await import("@/sse/services/model.js");
const result = await getModelInfo("acme/claude-3");
expect(result.provider).toBe("anthropic-compatible-claude-xyz");
expect(result.model).toBe("claude-3");
});
it("should handle model ID without prefix", async () => {
const { getModelInfo } = await import("@/sse/services/model.js");
const result = await getModelInfo("openai/gpt-4");
expect(result.provider).toBeDefined();
expect(result.model).toBe("gpt-4");
});
});