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:
@@ -9,13 +9,13 @@
|
||||
*/
|
||||
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { convertCommandCodeToOpenAI } from "../../open-sse/translator/response/commandcode-to-openai.js";
|
||||
import { commandCodeToOpenAIResponse } from "../../open-sse/translator/response/commandcode-to-openai.js";
|
||||
|
||||
function feed(events) {
|
||||
const state = {};
|
||||
const all = [];
|
||||
for (const e of events) {
|
||||
const out = convertCommandCodeToOpenAI(JSON.stringify(e), state);
|
||||
const out = commandCodeToOpenAIResponse(JSON.stringify(e), state);
|
||||
if (out) for (const c of out) all.push(c);
|
||||
}
|
||||
return { state, chunks: all };
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Locks multimodal quirks flagged in docs 11 §4: image_url.detail drop + input_audio per-format.
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { openaiToClaudeRequest } from "../../open-sse/translator/request/openai-to-claude.js";
|
||||
import { convertOpenAIContentToParts } from "../../open-sse/translator/helpers/geminiHelper.js";
|
||||
import { convertOpenAIContentToParts } from "../../open-sse/translator/formats/gemini.js";
|
||||
|
||||
function userImage(detail) {
|
||||
return {
|
||||
|
||||
@@ -9,13 +9,13 @@
|
||||
*/
|
||||
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { openaiToCommandCode } from "../../open-sse/translator/request/openai-to-commandcode.js";
|
||||
import { openaiToCommandCodeRequest } from "../../open-sse/translator/request/openai-to-commandcode.js";
|
||||
|
||||
const MODEL = "moonshotai/Kimi-K2.6";
|
||||
|
||||
describe("openaiToCommandCode — basic envelope", () => {
|
||||
describe("openaiToCommandCodeRequest — basic envelope", () => {
|
||||
it("returns the expected top-level envelope shape", () => {
|
||||
const out = openaiToCommandCode(MODEL, {
|
||||
const out = openaiToCommandCodeRequest(MODEL, {
|
||||
messages: [{ role: "user", content: "hi" }],
|
||||
}, true);
|
||||
|
||||
@@ -28,9 +28,9 @@ describe("openaiToCommandCode — basic envelope", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("openaiToCommandCode — system handling", () => {
|
||||
describe("openaiToCommandCodeRequest — system handling", () => {
|
||||
it("hoists system messages to params.system (string), not messages[]", () => {
|
||||
const out = openaiToCommandCode(MODEL, {
|
||||
const out = openaiToCommandCodeRequest(MODEL, {
|
||||
messages: [
|
||||
{ role: "system", content: "You are concise." },
|
||||
{ role: "user", content: "hi" },
|
||||
@@ -44,7 +44,7 @@ describe("openaiToCommandCode — system handling", () => {
|
||||
});
|
||||
|
||||
it("joins multiple system messages with blank line", () => {
|
||||
const out = openaiToCommandCode(MODEL, {
|
||||
const out = openaiToCommandCodeRequest(MODEL, {
|
||||
messages: [
|
||||
{ role: "system", content: "A" },
|
||||
{ role: "system", content: "B" },
|
||||
@@ -56,16 +56,16 @@ describe("openaiToCommandCode — system handling", () => {
|
||||
});
|
||||
|
||||
it("omits params.system when no system messages", () => {
|
||||
const out = openaiToCommandCode(MODEL, {
|
||||
const out = openaiToCommandCodeRequest(MODEL, {
|
||||
messages: [{ role: "user", content: "hi" }],
|
||||
}, true);
|
||||
expect(out.params.system).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("openaiToCommandCode — content shape", () => {
|
||||
describe("openaiToCommandCodeRequest — content shape", () => {
|
||||
it("MUST always emit content as Array (never string) for user", () => {
|
||||
const out = openaiToCommandCode(MODEL, {
|
||||
const out = openaiToCommandCodeRequest(MODEL, {
|
||||
messages: [{ role: "user", content: "hello" }],
|
||||
}, true);
|
||||
|
||||
@@ -75,7 +75,7 @@ describe("openaiToCommandCode — content shape", () => {
|
||||
});
|
||||
|
||||
it("MUST always emit content as Array for assistant", () => {
|
||||
const out = openaiToCommandCode(MODEL, {
|
||||
const out = openaiToCommandCodeRequest(MODEL, {
|
||||
messages: [
|
||||
{ role: "user", content: "a" },
|
||||
{ role: "assistant", content: "b" },
|
||||
@@ -87,9 +87,9 @@ describe("openaiToCommandCode — content shape", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("openaiToCommandCode — tool role / tool-result (AI SDK)", () => {
|
||||
describe("openaiToCommandCodeRequest — tool role / tool-result (AI SDK)", () => {
|
||||
it("converts role:\"tool\" to role:\"tool\" with tool-result block; output is {type:\"text\",value}", () => {
|
||||
const out = openaiToCommandCode(MODEL, {
|
||||
const out = openaiToCommandCodeRequest(MODEL, {
|
||||
messages: [
|
||||
{ role: "user", content: "run X" },
|
||||
{
|
||||
@@ -113,9 +113,9 @@ describe("openaiToCommandCode — tool role / tool-result (AI SDK)", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("openaiToCommandCode — assistant tool_calls / tool-call", () => {
|
||||
describe("openaiToCommandCodeRequest — assistant tool_calls / tool-call", () => {
|
||||
it("converts assistant.tool_calls[] into content blocks of type tool-call", () => {
|
||||
const out = openaiToCommandCode(MODEL, {
|
||||
const out = openaiToCommandCodeRequest(MODEL, {
|
||||
messages: [
|
||||
{ role: "user", content: "go" },
|
||||
{
|
||||
@@ -138,9 +138,9 @@ describe("openaiToCommandCode — assistant tool_calls / tool-call", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("openaiToCommandCode — tools schema conversion", () => {
|
||||
describe("openaiToCommandCodeRequest — tools schema conversion", () => {
|
||||
it("converts OpenAI {type:\"function\", function:{...}} to Anthropic plain {name, input_schema}", () => {
|
||||
const out = openaiToCommandCode(MODEL, {
|
||||
const out = openaiToCommandCodeRequest(MODEL, {
|
||||
messages: [{ role: "user", content: "hi" }],
|
||||
tools: [
|
||||
{
|
||||
@@ -163,7 +163,7 @@ describe("openaiToCommandCode — tools schema conversion", () => {
|
||||
});
|
||||
|
||||
it("preserves description on converted tool", () => {
|
||||
const out = openaiToCommandCode(MODEL, {
|
||||
const out = openaiToCommandCodeRequest(MODEL, {
|
||||
messages: [{ role: "user", content: "hi" }],
|
||||
tools: [
|
||||
{ type: "function", function: { name: "ping", description: "Ping the server", parameters: { type: "object" } } },
|
||||
@@ -173,7 +173,7 @@ describe("openaiToCommandCode — tools schema conversion", () => {
|
||||
});
|
||||
|
||||
it("does not include tools field when input has none", () => {
|
||||
const out = openaiToCommandCode(MODEL, {
|
||||
const out = openaiToCommandCodeRequest(MODEL, {
|
||||
messages: [{ role: "user", content: "hi" }],
|
||||
}, true);
|
||||
expect(out.params.tools).toBeUndefined();
|
||||
|
||||
@@ -2,21 +2,21 @@
|
||||
* Unit tests for open-sse/translator/request/openai-to-kiro.js
|
||||
*
|
||||
* Tests cover:
|
||||
* - buildKiroPayload() - basic message conversion
|
||||
* - openaiToKiroRequest() - basic message conversion
|
||||
* - Image forwarding fix: images in currentMessage must be included in payload
|
||||
*/
|
||||
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { buildKiroPayload } from "../../open-sse/translator/request/openai-to-kiro.js";
|
||||
import { openaiToKiroRequest } from "../../open-sse/translator/request/openai-to-kiro.js";
|
||||
|
||||
describe("buildKiroPayload", () => {
|
||||
describe("openaiToKiroRequest", () => {
|
||||
describe("basic message conversion", () => {
|
||||
it("should convert a simple text message", () => {
|
||||
const body = {
|
||||
messages: [{ role: "user", content: "Hello" }]
|
||||
};
|
||||
|
||||
const result = buildKiroPayload("claude-sonnet-4.6", body, true, {});
|
||||
const result = openaiToKiroRequest("claude-sonnet-4.6", body, true, {});
|
||||
|
||||
const currentMsg = result.conversationState.currentMessage;
|
||||
expect(currentMsg.userInputMessage.content).toContain("Hello");
|
||||
@@ -29,7 +29,7 @@ describe("buildKiroPayload", () => {
|
||||
messages: [{ role: "user", content: "No images here" }]
|
||||
};
|
||||
|
||||
const result = buildKiroPayload("claude-sonnet-4.6", body, true, {});
|
||||
const result = openaiToKiroRequest("claude-sonnet-4.6", body, true, {});
|
||||
|
||||
const currentMsg = result.conversationState.currentMessage;
|
||||
expect(currentMsg.userInputMessage.images).toBeUndefined();
|
||||
@@ -51,7 +51,7 @@ describe("buildKiroPayload", () => {
|
||||
]
|
||||
};
|
||||
|
||||
const result = buildKiroPayload("claude-sonnet-4.6", body, true, {});
|
||||
const result = openaiToKiroRequest("claude-sonnet-4.6", body, true, {});
|
||||
|
||||
const currentMsg = result.conversationState.currentMessage;
|
||||
expect(currentMsg.userInputMessage.images).toBeDefined();
|
||||
@@ -75,7 +75,7 @@ describe("buildKiroPayload", () => {
|
||||
]
|
||||
};
|
||||
|
||||
const result = buildKiroPayload("claude-sonnet-4.6", body, true, {});
|
||||
const result = openaiToKiroRequest("claude-sonnet-4.6", body, true, {});
|
||||
|
||||
const currentMsg = result.conversationState.currentMessage;
|
||||
expect(currentMsg.userInputMessage.images).toHaveLength(2);
|
||||
@@ -95,7 +95,7 @@ describe("buildKiroPayload", () => {
|
||||
]
|
||||
};
|
||||
|
||||
const result = buildKiroPayload("claude-sonnet-4.6", body, true, {});
|
||||
const result = openaiToKiroRequest("claude-sonnet-4.6", body, true, {});
|
||||
|
||||
const currentMsg = result.conversationState.currentMessage;
|
||||
expect(currentMsg.userInputMessage.images).toBeUndefined();
|
||||
@@ -115,7 +115,7 @@ describe("buildKiroPayload", () => {
|
||||
]
|
||||
};
|
||||
|
||||
const result = buildKiroPayload("claude-sonnet-4.6", body, true, {});
|
||||
const result = openaiToKiroRequest("claude-sonnet-4.6", body, true, {});
|
||||
|
||||
const currentMsg = result.conversationState.currentMessage;
|
||||
expect(currentMsg.userInputMessage.content).toContain("What is in this image?");
|
||||
@@ -135,7 +135,7 @@ describe("buildKiroPayload", () => {
|
||||
]
|
||||
};
|
||||
|
||||
const result = buildKiroPayload("claude-sonnet-4.6", body, true, {});
|
||||
const result = openaiToKiroRequest("claude-sonnet-4.6", body, true, {});
|
||||
|
||||
const currentMsg = result.conversationState.currentMessage;
|
||||
// HTTP URLs are not supported by Kiro — converted to text placeholder
|
||||
@@ -166,7 +166,7 @@ describe("buildKiroPayload", () => {
|
||||
// note: no `tools`
|
||||
};
|
||||
|
||||
const result = buildKiroPayload("claude-sonnet-4.6", body, true, {});
|
||||
const result = openaiToKiroRequest("claude-sonnet-4.6", body, true, {});
|
||||
const cs = result.conversationState;
|
||||
|
||||
// No structured tool content anywhere
|
||||
@@ -201,7 +201,7 @@ describe("buildKiroPayload", () => {
|
||||
]
|
||||
};
|
||||
|
||||
const result = buildKiroPayload("claude-sonnet-4.6", body, true, {});
|
||||
const result = openaiToKiroRequest("claude-sonnet-4.6", body, true, {});
|
||||
const cs = result.conversationState;
|
||||
|
||||
const allJson = JSON.stringify(cs);
|
||||
@@ -233,7 +233,7 @@ describe("buildKiroPayload", () => {
|
||||
]
|
||||
};
|
||||
|
||||
const result = buildKiroPayload("claude-sonnet-4.6", body, true, {});
|
||||
const result = openaiToKiroRequest("claude-sonnet-4.6", body, true, {});
|
||||
const cs = result.conversationState;
|
||||
|
||||
// Structured tool spec carried on currentMessage
|
||||
@@ -270,7 +270,7 @@ describe("buildKiroPayload", () => {
|
||||
]
|
||||
};
|
||||
|
||||
const result = buildKiroPayload("claude-sonnet-4.6", body, true, {});
|
||||
const result = openaiToKiroRequest("claude-sonnet-4.6", body, true, {});
|
||||
const cs = result.conversationState;
|
||||
const allJson = JSON.stringify(cs);
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Locks edge cases flagged in docs 11 §1/§4 that were only covered indirectly.
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { normalizeClaudePassthrough } from "../../open-sse/translator/helpers/claudeHelper.js";
|
||||
import { parseDataUri, encodeDataUri } from "../../open-sse/translator/helpers/imageHelper.js";
|
||||
import { normalizeClaudePassthrough } from "../../open-sse/translator/formats/claude.js";
|
||||
import { parseDataUri, encodeDataUri } from "../../open-sse/translator/concerns/image.js";
|
||||
|
||||
describe("normalizeClaudePassthrough — haiku adaptive thinking (docs 11 §1)", () => {
|
||||
it("downgrades adaptive thinking to enabled+budget for haiku models", () => {
|
||||
|
||||
@@ -3,7 +3,7 @@ import { describe, it, expect } from "vitest";
|
||||
import { FORMATS } from "../../open-sse/translator/formats.js";
|
||||
import { translateRequest } from "../../open-sse/translator/index.js";
|
||||
import { claudeToOpenAIRequest } from "../../open-sse/translator/request/claude-to-openai.js";
|
||||
import { filterToOpenAIFormat } from "../../open-sse/translator/helpers/openaiHelper.js";
|
||||
import { filterToOpenAIFormat } from "../../open-sse/translator/formats/openai.js";
|
||||
import { parseSSELine } from "../../open-sse/utils/streamHelpers.js";
|
||||
|
||||
describe("request normalization", () => {
|
||||
|
||||
Reference in New Issue
Block a user