feat(translator): add vision support for commandcode provider
Map OpenAI image_url / Claude-style image blocks to the {type:"image",
image:"<data URI|url>"} shape the command-code CLI sends to /alpha/generate
instead of dropping them to "[image omitted]". Handles data URIs, raw base64
(with media_type / image/png fallback), and remote URLs.
- Promote bugs-gemini-cursor-commandcode "image content is preserved" from
it.fails to a real assertion (bug fixed)
- Add vision unit tests to openai-to-commandcode.test.js
- Add test-commandcode-vision.sh curl helper for live verification
Co-authored-by: CommandCodeBot <noreply@commandcode.ai>
This commit is contained in:
@@ -14,168 +14,341 @@ import { openaiToCommandCodeRequest } from "../../open-sse/translator/request/op
|
||||
const MODEL = "moonshotai/Kimi-K2.6";
|
||||
|
||||
describe("openaiToCommandCodeRequest — basic envelope", () => {
|
||||
it("returns the expected top-level envelope shape", () => {
|
||||
const out = openaiToCommandCodeRequest(MODEL, {
|
||||
messages: [{ role: "user", content: "hi" }],
|
||||
}, true);
|
||||
it("returns the expected top-level envelope shape", () => {
|
||||
const out = openaiToCommandCodeRequest(
|
||||
MODEL,
|
||||
{
|
||||
messages: [{ role: "user", content: "hi" }],
|
||||
},
|
||||
true,
|
||||
);
|
||||
|
||||
expect(out).toHaveProperty("threadId");
|
||||
expect(out).toHaveProperty("memory");
|
||||
expect(out).toHaveProperty("config");
|
||||
expect(out).toHaveProperty("params");
|
||||
expect(out.params.model).toBe(MODEL);
|
||||
expect(out.params.stream).toBe(true);
|
||||
});
|
||||
expect(out).toHaveProperty("threadId");
|
||||
expect(out).toHaveProperty("memory");
|
||||
expect(out).toHaveProperty("config");
|
||||
expect(out).toHaveProperty("params");
|
||||
expect(out.params.model).toBe(MODEL);
|
||||
expect(out.params.stream).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("openaiToCommandCodeRequest — system handling", () => {
|
||||
it("hoists system messages to params.system (string), not messages[]", () => {
|
||||
const out = openaiToCommandCodeRequest(MODEL, {
|
||||
messages: [
|
||||
{ role: "system", content: "You are concise." },
|
||||
{ role: "user", content: "hi" },
|
||||
],
|
||||
}, true);
|
||||
it("hoists system messages to params.system (string), not messages[]", () => {
|
||||
const out = openaiToCommandCodeRequest(
|
||||
MODEL,
|
||||
{
|
||||
messages: [
|
||||
{ role: "system", content: "You are concise." },
|
||||
{ role: "user", content: "hi" },
|
||||
],
|
||||
},
|
||||
true,
|
||||
);
|
||||
|
||||
expect(typeof out.params.system).toBe("string");
|
||||
expect(out.params.system).toBe("You are concise.");
|
||||
const roles = out.params.messages.map((m) => m.role);
|
||||
expect(roles).not.toContain("system");
|
||||
});
|
||||
expect(typeof out.params.system).toBe("string");
|
||||
expect(out.params.system).toBe("You are concise.");
|
||||
const roles = out.params.messages.map((m) => m.role);
|
||||
expect(roles).not.toContain("system");
|
||||
});
|
||||
|
||||
it("joins multiple system messages with blank line", () => {
|
||||
const out = openaiToCommandCodeRequest(MODEL, {
|
||||
messages: [
|
||||
{ role: "system", content: "A" },
|
||||
{ role: "system", content: "B" },
|
||||
{ role: "user", content: "hi" },
|
||||
],
|
||||
}, true);
|
||||
it("joins multiple system messages with blank line", () => {
|
||||
const out = openaiToCommandCodeRequest(
|
||||
MODEL,
|
||||
{
|
||||
messages: [
|
||||
{ role: "system", content: "A" },
|
||||
{ role: "system", content: "B" },
|
||||
{ role: "user", content: "hi" },
|
||||
],
|
||||
},
|
||||
true,
|
||||
);
|
||||
|
||||
expect(out.params.system).toBe("A\n\nB");
|
||||
});
|
||||
expect(out.params.system).toBe("A\n\nB");
|
||||
});
|
||||
|
||||
it("omits params.system when no system messages", () => {
|
||||
const out = openaiToCommandCodeRequest(MODEL, {
|
||||
messages: [{ role: "user", content: "hi" }],
|
||||
}, true);
|
||||
expect(out.params.system).toBeUndefined();
|
||||
});
|
||||
it("omits params.system when no system messages", () => {
|
||||
const out = openaiToCommandCodeRequest(
|
||||
MODEL,
|
||||
{
|
||||
messages: [{ role: "user", content: "hi" }],
|
||||
},
|
||||
true,
|
||||
);
|
||||
expect(out.params.system).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("openaiToCommandCodeRequest — vision / image blocks", () => {
|
||||
const PNG =
|
||||
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==";
|
||||
|
||||
it('maps OpenAI image_url (data URI) → {type:"image", image:"data:..."}', () => {
|
||||
const out = openaiToCommandCodeRequest(
|
||||
MODEL,
|
||||
{
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: [
|
||||
{ type: "text", text: "What color?" },
|
||||
{
|
||||
type: "image_url",
|
||||
image_url: { url: `data:image/png;base64,${PNG}` },
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
true,
|
||||
);
|
||||
|
||||
const blocks = out.params.messages[0].content;
|
||||
expect(blocks[0]).toEqual({ type: "text", text: "What color?" });
|
||||
expect(blocks[1]).toEqual({
|
||||
type: "image",
|
||||
image: `data:image/png;base64,${PNG}`,
|
||||
});
|
||||
});
|
||||
|
||||
it("maps Claude-style image block (source.base64) → data URI with media_type", () => {
|
||||
const out = openaiToCommandCodeRequest(
|
||||
MODEL,
|
||||
{
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: [
|
||||
{
|
||||
type: "image",
|
||||
source: {
|
||||
type: "base64",
|
||||
media_type: "image/jpeg",
|
||||
data: "AAAA",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
true,
|
||||
);
|
||||
|
||||
expect(out.params.messages[0].content[0]).toEqual({
|
||||
type: "image",
|
||||
image: "data:image/jpeg;base64,AAAA",
|
||||
});
|
||||
});
|
||||
|
||||
it("passes raw URL through when image_url is a remote http(s) URL", () => {
|
||||
const out = openaiToCommandCodeRequest(
|
||||
MODEL,
|
||||
{
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: [
|
||||
{
|
||||
type: "image_url",
|
||||
image_url: { url: "https://example.com/a.png" },
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
true,
|
||||
);
|
||||
|
||||
expect(out.params.messages[0].content[0]).toEqual({
|
||||
type: "image",
|
||||
image: "https://example.com/a.png",
|
||||
});
|
||||
});
|
||||
|
||||
it("skips image block when no usable image source", () => {
|
||||
const out = openaiToCommandCodeRequest(
|
||||
MODEL,
|
||||
{
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: [{ type: "image_url", image_url: { url: "" } }],
|
||||
},
|
||||
],
|
||||
},
|
||||
true,
|
||||
);
|
||||
|
||||
const blocks = out.params.messages[0].content;
|
||||
expect(blocks.every((b) => b.type !== "image")).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("openaiToCommandCodeRequest — content shape", () => {
|
||||
it("MUST always emit content as Array (never string) for user", () => {
|
||||
const out = openaiToCommandCodeRequest(MODEL, {
|
||||
messages: [{ role: "user", content: "hello" }],
|
||||
}, true);
|
||||
it("MUST always emit content as Array (never string) for user", () => {
|
||||
const out = openaiToCommandCodeRequest(
|
||||
MODEL,
|
||||
{
|
||||
messages: [{ role: "user", content: "hello" }],
|
||||
},
|
||||
true,
|
||||
);
|
||||
|
||||
const u = out.params.messages[0];
|
||||
expect(Array.isArray(u.content)).toBe(true);
|
||||
expect(u.content[0]).toEqual({ type: "text", text: "hello" });
|
||||
});
|
||||
const u = out.params.messages[0];
|
||||
expect(Array.isArray(u.content)).toBe(true);
|
||||
expect(u.content[0]).toEqual({ type: "text", text: "hello" });
|
||||
});
|
||||
|
||||
it("MUST always emit content as Array for assistant", () => {
|
||||
const out = openaiToCommandCodeRequest(MODEL, {
|
||||
messages: [
|
||||
{ role: "user", content: "a" },
|
||||
{ role: "assistant", content: "b" },
|
||||
],
|
||||
}, true);
|
||||
const a = out.params.messages[1];
|
||||
expect(Array.isArray(a.content)).toBe(true);
|
||||
expect(a.content[0]).toEqual({ type: "text", text: "b" });
|
||||
});
|
||||
it("MUST always emit content as Array for assistant", () => {
|
||||
const out = openaiToCommandCodeRequest(
|
||||
MODEL,
|
||||
{
|
||||
messages: [
|
||||
{ role: "user", content: "a" },
|
||||
{ role: "assistant", content: "b" },
|
||||
],
|
||||
},
|
||||
true,
|
||||
);
|
||||
const a = out.params.messages[1];
|
||||
expect(Array.isArray(a.content)).toBe(true);
|
||||
expect(a.content[0]).toEqual({ type: "text", text: "b" });
|
||||
});
|
||||
});
|
||||
|
||||
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 = openaiToCommandCodeRequest(MODEL, {
|
||||
messages: [
|
||||
{ role: "user", content: "run X" },
|
||||
{
|
||||
role: "assistant",
|
||||
content: null,
|
||||
tool_calls: [
|
||||
{ id: "call_1", type: "function", function: { name: "do_x", arguments: "{\"a\":1}" } },
|
||||
],
|
||||
},
|
||||
{ role: "tool", tool_call_id: "call_1", name: "do_x", content: "RESULT_OK" },
|
||||
],
|
||||
}, true);
|
||||
it('converts role:"tool" to role:"tool" with tool-result block; output is {type:"text",value}', () => {
|
||||
const out = openaiToCommandCodeRequest(
|
||||
MODEL,
|
||||
{
|
||||
messages: [
|
||||
{ role: "user", content: "run X" },
|
||||
{
|
||||
role: "assistant",
|
||||
content: null,
|
||||
tool_calls: [
|
||||
{
|
||||
id: "call_1",
|
||||
type: "function",
|
||||
function: { name: "do_x", arguments: '{"a":1}' },
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
role: "tool",
|
||||
tool_call_id: "call_1",
|
||||
name: "do_x",
|
||||
content: "RESULT_OK",
|
||||
},
|
||||
],
|
||||
},
|
||||
true,
|
||||
);
|
||||
|
||||
const toolMsg = out.params.messages[out.params.messages.length - 1];
|
||||
expect(toolMsg.role).toBe("tool");
|
||||
const block = toolMsg.content[0];
|
||||
expect(block.type).toBe("tool-result");
|
||||
expect(block.toolCallId).toBe("call_1");
|
||||
expect(block.toolName).toBe("do_x");
|
||||
expect(block.output).toEqual({ type: "text", value: "RESULT_OK" });
|
||||
});
|
||||
const toolMsg = out.params.messages[out.params.messages.length - 1];
|
||||
expect(toolMsg.role).toBe("tool");
|
||||
const block = toolMsg.content[0];
|
||||
expect(block.type).toBe("tool-result");
|
||||
expect(block.toolCallId).toBe("call_1");
|
||||
expect(block.toolName).toBe("do_x");
|
||||
expect(block.output).toEqual({ type: "text", value: "RESULT_OK" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("openaiToCommandCodeRequest — assistant tool_calls / tool-call", () => {
|
||||
it("converts assistant.tool_calls[] into content blocks of type tool-call", () => {
|
||||
const out = openaiToCommandCodeRequest(MODEL, {
|
||||
messages: [
|
||||
{ role: "user", content: "go" },
|
||||
{
|
||||
role: "assistant",
|
||||
content: null,
|
||||
tool_calls: [
|
||||
{ id: "call_42", type: "function", function: { name: "search", arguments: "{\"q\":\"hi\"}" } },
|
||||
],
|
||||
},
|
||||
],
|
||||
}, true);
|
||||
it("converts assistant.tool_calls[] into content blocks of type tool-call", () => {
|
||||
const out = openaiToCommandCodeRequest(
|
||||
MODEL,
|
||||
{
|
||||
messages: [
|
||||
{ role: "user", content: "go" },
|
||||
{
|
||||
role: "assistant",
|
||||
content: null,
|
||||
tool_calls: [
|
||||
{
|
||||
id: "call_42",
|
||||
type: "function",
|
||||
function: { name: "search", arguments: '{"q":"hi"}' },
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
true,
|
||||
);
|
||||
|
||||
const asst = out.params.messages[1];
|
||||
expect(asst.role).toBe("assistant");
|
||||
const tc = asst.content.find((b) => b.type === "tool-call");
|
||||
expect(tc).toBeDefined();
|
||||
expect(tc.toolCallId).toBe("call_42");
|
||||
expect(tc.toolName).toBe("search");
|
||||
expect(tc.input).toEqual({ q: "hi" });
|
||||
});
|
||||
const asst = out.params.messages[1];
|
||||
expect(asst.role).toBe("assistant");
|
||||
const tc = asst.content.find((b) => b.type === "tool-call");
|
||||
expect(tc).toBeDefined();
|
||||
expect(tc.toolCallId).toBe("call_42");
|
||||
expect(tc.toolName).toBe("search");
|
||||
expect(tc.input).toEqual({ q: "hi" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("openaiToCommandCodeRequest — tools schema conversion", () => {
|
||||
it("converts OpenAI {type:\"function\", function:{...}} to Anthropic plain {name, input_schema}", () => {
|
||||
const out = openaiToCommandCodeRequest(MODEL, {
|
||||
messages: [{ role: "user", content: "hi" }],
|
||||
tools: [
|
||||
{
|
||||
type: "function",
|
||||
function: {
|
||||
name: "weather",
|
||||
description: "Get weather",
|
||||
parameters: { type: "object", properties: { city: { type: "string" } }, required: ["city"] },
|
||||
},
|
||||
},
|
||||
],
|
||||
}, true);
|
||||
it('converts OpenAI {type:"function", function:{...}} to Anthropic plain {name, input_schema}', () => {
|
||||
const out = openaiToCommandCodeRequest(
|
||||
MODEL,
|
||||
{
|
||||
messages: [{ role: "user", content: "hi" }],
|
||||
tools: [
|
||||
{
|
||||
type: "function",
|
||||
function: {
|
||||
name: "weather",
|
||||
description: "Get weather",
|
||||
parameters: {
|
||||
type: "object",
|
||||
properties: { city: { type: "string" } },
|
||||
required: ["city"],
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
true,
|
||||
);
|
||||
|
||||
const t = out.params.tools[0];
|
||||
expect(t.name).toBe("weather");
|
||||
expect(t.input_schema).toBeDefined();
|
||||
expect(t.input_schema.type).toBe("object");
|
||||
expect(t.function).toBeUndefined();
|
||||
expect(t.parameters).toBeUndefined();
|
||||
});
|
||||
const t = out.params.tools[0];
|
||||
expect(t.name).toBe("weather");
|
||||
expect(t.input_schema).toBeDefined();
|
||||
expect(t.input_schema.type).toBe("object");
|
||||
expect(t.function).toBeUndefined();
|
||||
expect(t.parameters).toBeUndefined();
|
||||
});
|
||||
|
||||
it("preserves description on converted tool", () => {
|
||||
const out = openaiToCommandCodeRequest(MODEL, {
|
||||
messages: [{ role: "user", content: "hi" }],
|
||||
tools: [
|
||||
{ type: "function", function: { name: "ping", description: "Ping the server", parameters: { type: "object" } } },
|
||||
],
|
||||
}, true);
|
||||
expect(out.params.tools[0].description).toBe("Ping the server");
|
||||
});
|
||||
it("preserves description on converted tool", () => {
|
||||
const out = openaiToCommandCodeRequest(
|
||||
MODEL,
|
||||
{
|
||||
messages: [{ role: "user", content: "hi" }],
|
||||
tools: [
|
||||
{
|
||||
type: "function",
|
||||
function: {
|
||||
name: "ping",
|
||||
description: "Ping the server",
|
||||
parameters: { type: "object" },
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
true,
|
||||
);
|
||||
expect(out.params.tools[0].description).toBe("Ping the server");
|
||||
});
|
||||
|
||||
it("does not include tools field when input has none", () => {
|
||||
const out = openaiToCommandCodeRequest(MODEL, {
|
||||
messages: [{ role: "user", content: "hi" }],
|
||||
}, true);
|
||||
expect(out.params.tools).toBeUndefined();
|
||||
});
|
||||
it("does not include tools field when input has none", () => {
|
||||
const out = openaiToCommandCodeRequest(
|
||||
MODEL,
|
||||
{
|
||||
messages: [{ role: "user", content: "hi" }],
|
||||
},
|
||||
true,
|
||||
);
|
||||
expect(out.params.tools).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user