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:
2026-08-03 17:06:32 +07:00
parent 067f18aaa1
commit fcd3dcb409
4 changed files with 637 additions and 323 deletions

View File

@@ -13,6 +13,8 @@ import { register } from "../index.js";
import { FORMATS } from "../formats.js"; import { FORMATS } from "../formats.js";
import { randomUUID } from "crypto"; import { randomUUID } from "crypto";
import { ROLE, OPENAI_BLOCK } from "../schema/index.js"; import { ROLE, OPENAI_BLOCK } from "../schema/index.js";
import { DEFAULT_IMAGE_MIME } from "../schema/index.js";
import { parseDataUri } from "../concerns/image.js";
import { DEFAULT_MAX_TOKENS } from "../../config/runtimeConfig.js"; import { DEFAULT_MAX_TOKENS } from "../../config/runtimeConfig.js";
function flattenText(content) { function flattenText(content) {
@@ -22,7 +24,8 @@ function flattenText(content) {
const parts = []; const parts = [];
for (const p of content) { for (const p of content) {
if (typeof p === "string") parts.push(p); if (typeof p === "string") parts.push(p);
else if (p && typeof p === "object" && typeof p.text === "string") parts.push(p.text); else if (p && typeof p === "object" && typeof p.text === "string")
parts.push(p.text);
} }
return parts.join("\n"); return parts.join("\n");
} }
@@ -31,7 +34,8 @@ function flattenText(content) {
function toContentBlocks(content) { function toContentBlocks(content) {
if (content == null) return [{ type: OPENAI_BLOCK.TEXT, text: "" }]; if (content == null) return [{ type: OPENAI_BLOCK.TEXT, text: "" }];
if (typeof content === "string") return [{ type: OPENAI_BLOCK.TEXT, text: content }]; if (typeof content === "string")
return [{ type: OPENAI_BLOCK.TEXT, text: content }];
if (Array.isArray(content)) { if (Array.isArray(content)) {
const blocks = []; const blocks = [];
for (const part of content) { for (const part of content) {
@@ -40,8 +44,28 @@ function toContentBlocks(content) {
} else if (part && typeof part === "object") { } else if (part && typeof part === "object") {
if (part.type === OPENAI_BLOCK.TEXT && typeof part.text === "string") { if (part.type === OPENAI_BLOCK.TEXT && typeof part.text === "string") {
blocks.push({ type: OPENAI_BLOCK.TEXT, text: part.text }); blocks.push({ type: OPENAI_BLOCK.TEXT, text: part.text });
} else if (part.type === OPENAI_BLOCK.IMAGE_URL || part.type === OPENAI_BLOCK.IMAGE) { } else if (
blocks.push({ type: OPENAI_BLOCK.TEXT, text: "[image omitted]" }); part.type === OPENAI_BLOCK.IMAGE_URL ||
part.type === OPENAI_BLOCK.IMAGE
) {
// CommandCode `/alpha/generate` accepts {type:"image", image:"<data URI | url>"} —
// same shape the official command-code CLI sends (verified from CLI source).
const src = part.source;
let raw = part.image_url?.url || src?.data || src?.url || "";
let parsed = parseDataUri(raw);
if (!parsed && src?.type === "base64" && src?.data) {
// Claude-style base64 source without a data-URI prefix → wrap it.
raw = `data:${src.media_type || DEFAULT_IMAGE_MIME};base64,${src.data}`;
parsed = parseDataUri(raw);
}
if (parsed) {
blocks.push({
type: "image",
image: `data:${parsed.mimeType};base64,${parsed.base64}`,
});
} else if (raw) {
blocks.push({ type: "image", image: raw });
}
} else if (typeof part.text === "string") { } else if (typeof part.text === "string") {
blocks.push({ type: OPENAI_BLOCK.TEXT, text: part.text }); blocks.push({ type: OPENAI_BLOCK.TEXT, text: part.text });
} }
@@ -55,7 +79,11 @@ function toContentBlocks(content) {
function safeParseJson(s) { function safeParseJson(s) {
if (s == null) return {}; if (s == null) return {};
if (typeof s !== "string") return s; if (typeof s !== "string") return s;
try { return JSON.parse(s); } catch { return {}; } try {
return JSON.parse(s);
} catch {
return {};
}
} }
function convertMessages(messages = []) { function convertMessages(messages = []) {
@@ -73,15 +101,18 @@ function convertMessages(messages = []) {
} }
if (role === ROLE.TOOL) { if (role === ROLE.TOOL) {
const value = typeof m.content === "string" ? m.content : flattenText(m.content); const value =
typeof m.content === "string" ? m.content : flattenText(m.content);
out.push({ out.push({
role: ROLE.TOOL, role: ROLE.TOOL,
content: [{ content: [
{
type: "tool-result", type: "tool-result",
toolCallId: m.tool_call_id || "", toolCallId: m.tool_call_id || "",
toolName: m.name || "", toolName: m.name || "",
output: { type: "text", value }, output: { type: "text", value },
}], },
],
}); });
continue; continue;
} }
@@ -101,7 +132,12 @@ function convertMessages(messages = []) {
}); });
} }
} }
out.push({ role: ROLE.ASSISTANT, content: blocks.length ? blocks : [{ type: OPENAI_BLOCK.TEXT, text: "" }] }); out.push({
role: ROLE.ASSISTANT,
content: blocks.length
? blocks
: [{ type: OPENAI_BLOCK.TEXT, text: "" }],
});
continue; continue;
} }
@@ -133,7 +169,11 @@ function convertTools(tools) {
return result.length ? result : undefined; return result.length ? result : undefined;
} }
export function openaiToCommandCodeRequest(model, body, stream /* , credentials */) { export function openaiToCommandCodeRequest(
model,
body,
stream /* , credentials */,
) {
const { messages, system } = convertMessages(body.messages); const { messages, system } = convertMessages(body.messages);
const params = { const params = {
model, model,

41
test-commandcode-vision.sh Executable file
View File

@@ -0,0 +1,41 @@
#!/usr/bin/env bash
# Test vision support for the commandcode provider in 9router.
#
# Usage:
# ./test-commandcode-vision.sh [base_url] [model]
# BASE_URL=http://localhost:20128 ./test-commandcode-vision.sh
#
# Defaults: base_url=http://localhost:20128, model=commandcode/Qwen/Qwen3.6-Max-Preview
set -euo pipefail
BASE_URL="${1:-${BASE_URL:-http://localhost:20128}}"
MODEL="${2:-${MODEL:-commandcode/Qwen/Qwen3.6-Max-Preview}}"
# 1x1 red pixel PNG (base64) — enough to prove the image reaches the model.
PNG_B64="iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="
echo "==> POST $BASE_URL/v1/chat/completions"
echo "==> model: $MODEL"
echo
curl -sS -N "$BASE_URL/v1/chat/completions" \
-H "Content-Type: application/json" \
-d "$(
cat <<JSON
{
"model": "$MODEL",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "What color is this image? Reply with just the color name."},
{"type": "image_url", "image_url": {"url": "data:image/png;base64,$PNG_B64"}}
]
}
],
"stream": false
}
JSON
)"
echo

View File

@@ -4,9 +4,36 @@ import "./registerAll.js";
import { translateRequest } from "../../open-sse/translator/index.js"; import { translateRequest } from "../../open-sse/translator/index.js";
import { FORMATS } from "../../open-sse/translator/formats.js"; import { FORMATS } from "../../open-sse/translator/formats.js";
const O2G = (body) => translateRequest(FORMATS.OPENAI, FORMATS.GEMINI, "m", body, true, null, "gemini"); const O2G = (body) =>
const O2C = (body) => translateRequest(FORMATS.OPENAI, FORMATS.CURSOR, "m", body, true, null, "cursor"); translateRequest(
const O2CC = (body) => translateRequest(FORMATS.OPENAI, FORMATS.COMMANDCODE, "m", body, true, null, "commandcode"); FORMATS.OPENAI,
FORMATS.GEMINI,
"m",
body,
true,
null,
"gemini",
);
const O2C = (body) =>
translateRequest(
FORMATS.OPENAI,
FORMATS.CURSOR,
"m",
body,
true,
null,
"cursor",
);
const O2CC = (body) =>
translateRequest(
FORMATS.OPENAI,
FORMATS.COMMANDCODE,
"m",
body,
true,
null,
"commandcode",
);
describe("OpenAI → Gemini", () => { describe("OpenAI → Gemini", () => {
// openai-to-gemini.js:92-96 — each system message overwrites systemInstruction → only last kept // openai-to-gemini.js:92-96 — each system message overwrites systemInstruction → only last kept
@@ -19,7 +46,10 @@ describe("OpenAI → Gemini", () => {
{ role: "user", content: "hi" }, { role: "user", content: "hi" },
], ],
}); });
expect(JSON.stringify(out.systemInstruction), "earlier system lost").toContain("RULE_ONE"); expect(
JSON.stringify(out.systemInstruction),
"earlier system lost",
).toContain("RULE_ONE");
}); });
}); });
@@ -28,10 +58,18 @@ describe("OpenAI → Cursor", () => {
// KNOWN BUG // KNOWN BUG
it.fails("image content is preserved", () => { it.fails("image content is preserved", () => {
const out = O2C({ const out = O2C({
messages: [{ role: "user", content: [ messages: [
{
role: "user",
content: [
{ type: "text", text: "look" }, { type: "text", text: "look" },
{ type: "image_url", image_url: { url: "data:image/png;base64,AAAA" } }, {
] }], type: "image_url",
image_url: { url: "data:image/png;base64,AAAA" },
},
],
},
],
}); });
expect(JSON.stringify(out), "image dropped").toContain("AAAA"); expect(JSON.stringify(out), "image dropped").toContain("AAAA");
}); });
@@ -39,7 +77,10 @@ describe("OpenAI → Cursor", () => {
// openai-to-cursor.js:179 — max_tokens hardcoded to 32000 // openai-to-cursor.js:179 — max_tokens hardcoded to 32000
// KNOWN BUG // KNOWN BUG
it.fails("respects client max_tokens", () => { it.fails("respects client max_tokens", () => {
const out = O2C({ max_tokens: 200, messages: [{ role: "user", content: "hi" }] }); const out = O2C({
max_tokens: 200,
messages: [{ role: "user", content: "hi" }],
});
expect(out.max_tokens).toBe(200); expect(out.max_tokens).toBe(200);
}); });
}); });
@@ -51,25 +92,44 @@ describe("OpenAI → CommandCode", () => {
const out = O2CC({ const out = O2CC({
messages: [ messages: [
{ role: "user", content: "go" }, { role: "user", content: "go" },
{ role: "assistant", content: "", tool_calls: [ {
{ id: "c1", type: "function", function: { name: "f", arguments: "{bad" } }, role: "assistant",
] }, content: "",
tool_calls: [
{
id: "c1",
type: "function",
function: { name: "f", arguments: "{bad" },
},
],
},
{ role: "tool", tool_call_id: "c1", content: "r" }, { role: "tool", tool_call_id: "c1", content: "r" },
], ],
}); });
const asst = out.params.messages.find((m) => m.role === "assistant"); const asst = out.params.messages.find((m) => m.role === "assistant");
const call = asst.content.find((b) => b.type === "tool-call"); const call = asst.content.find((b) => b.type === "tool-call");
expect(Object.keys(call.input).length, "arguments silently dropped to {}").toBeGreaterThan(0); expect(
Object.keys(call.input).length,
"arguments silently dropped to {}",
).toBeGreaterThan(0);
}); });
// openai-to-commandcode.js:41-42 — image becomes "[image omitted]" // openai-to-commandcode.js — image blocks now map to {type:"image", image:"data:..."}
// KNOWN BUG // FIXED: was "[image omitted]", now preserved as data URI
it.fails("image content is preserved", () => { it("image content is preserved", () => {
const out = O2CC({ const out = O2CC({
messages: [{ role: "user", content: [ messages: [
{
role: "user",
content: [
{ type: "text", text: "look" }, { type: "text", text: "look" },
{ type: "image_url", image_url: { url: "data:image/png;base64,BBBB" } }, {
] }], type: "image_url",
image_url: { url: "data:image/png;base64,BBBB" },
},
],
},
],
}); });
expect(JSON.stringify(out), "image omitted").toContain("BBBB"); expect(JSON.stringify(out), "image omitted").toContain("BBBB");
}); });

View File

@@ -15,9 +15,13 @@ const MODEL = "moonshotai/Kimi-K2.6";
describe("openaiToCommandCodeRequest — basic envelope", () => { describe("openaiToCommandCodeRequest — basic envelope", () => {
it("returns the expected top-level envelope shape", () => { it("returns the expected top-level envelope shape", () => {
const out = openaiToCommandCodeRequest(MODEL, { const out = openaiToCommandCodeRequest(
MODEL,
{
messages: [{ role: "user", content: "hi" }], messages: [{ role: "user", content: "hi" }],
}, true); },
true,
);
expect(out).toHaveProperty("threadId"); expect(out).toHaveProperty("threadId");
expect(out).toHaveProperty("memory"); expect(out).toHaveProperty("memory");
@@ -30,12 +34,16 @@ describe("openaiToCommandCodeRequest — basic envelope", () => {
describe("openaiToCommandCodeRequest — system handling", () => { describe("openaiToCommandCodeRequest — system handling", () => {
it("hoists system messages to params.system (string), not messages[]", () => { it("hoists system messages to params.system (string), not messages[]", () => {
const out = openaiToCommandCodeRequest(MODEL, { const out = openaiToCommandCodeRequest(
MODEL,
{
messages: [ messages: [
{ role: "system", content: "You are concise." }, { role: "system", content: "You are concise." },
{ role: "user", content: "hi" }, { role: "user", content: "hi" },
], ],
}, true); },
true,
);
expect(typeof out.params.system).toBe("string"); expect(typeof out.params.system).toBe("string");
expect(out.params.system).toBe("You are concise."); expect(out.params.system).toBe("You are concise.");
@@ -44,30 +52,147 @@ describe("openaiToCommandCodeRequest — system handling", () => {
}); });
it("joins multiple system messages with blank line", () => { it("joins multiple system messages with blank line", () => {
const out = openaiToCommandCodeRequest(MODEL, { const out = openaiToCommandCodeRequest(
MODEL,
{
messages: [ messages: [
{ role: "system", content: "A" }, { role: "system", content: "A" },
{ role: "system", content: "B" }, { role: "system", content: "B" },
{ role: "user", content: "hi" }, { role: "user", content: "hi" },
], ],
}, true); },
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", () => { it("omits params.system when no system messages", () => {
const out = openaiToCommandCodeRequest(MODEL, { const out = openaiToCommandCodeRequest(
MODEL,
{
messages: [{ role: "user", content: "hi" }], messages: [{ role: "user", content: "hi" }],
}, true); },
true,
);
expect(out.params.system).toBeUndefined(); 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", () => { describe("openaiToCommandCodeRequest — content shape", () => {
it("MUST always emit content as Array (never string) for user", () => { it("MUST always emit content as Array (never string) for user", () => {
const out = openaiToCommandCodeRequest(MODEL, { const out = openaiToCommandCodeRequest(
MODEL,
{
messages: [{ role: "user", content: "hello" }], messages: [{ role: "user", content: "hello" }],
}, true); },
true,
);
const u = out.params.messages[0]; const u = out.params.messages[0];
expect(Array.isArray(u.content)).toBe(true); expect(Array.isArray(u.content)).toBe(true);
@@ -75,12 +200,16 @@ describe("openaiToCommandCodeRequest — content shape", () => {
}); });
it("MUST always emit content as Array for assistant", () => { it("MUST always emit content as Array for assistant", () => {
const out = openaiToCommandCodeRequest(MODEL, { const out = openaiToCommandCodeRequest(
MODEL,
{
messages: [ messages: [
{ role: "user", content: "a" }, { role: "user", content: "a" },
{ role: "assistant", content: "b" }, { role: "assistant", content: "b" },
], ],
}, true); },
true,
);
const a = out.params.messages[1]; const a = out.params.messages[1];
expect(Array.isArray(a.content)).toBe(true); expect(Array.isArray(a.content)).toBe(true);
expect(a.content[0]).toEqual({ type: "text", text: "b" }); expect(a.content[0]).toEqual({ type: "text", text: "b" });
@@ -88,20 +217,33 @@ describe("openaiToCommandCodeRequest — content shape", () => {
}); });
describe("openaiToCommandCodeRequest — 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}", () => { it('converts role:"tool" to role:"tool" with tool-result block; output is {type:"text",value}', () => {
const out = openaiToCommandCodeRequest(MODEL, { const out = openaiToCommandCodeRequest(
MODEL,
{
messages: [ messages: [
{ role: "user", content: "run X" }, { role: "user", content: "run X" },
{ {
role: "assistant", role: "assistant",
content: null, content: null,
tool_calls: [ tool_calls: [
{ id: "call_1", type: "function", function: { name: "do_x", arguments: "{\"a\":1}" } }, {
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" }, {
role: "tool",
tool_call_id: "call_1",
name: "do_x",
content: "RESULT_OK",
},
], ],
}, true); },
true,
);
const toolMsg = out.params.messages[out.params.messages.length - 1]; const toolMsg = out.params.messages[out.params.messages.length - 1];
expect(toolMsg.role).toBe("tool"); expect(toolMsg.role).toBe("tool");
@@ -115,18 +257,26 @@ describe("openaiToCommandCodeRequest — tool role / tool-result (AI SDK)", () =
describe("openaiToCommandCodeRequest — assistant tool_calls / tool-call", () => { describe("openaiToCommandCodeRequest — assistant tool_calls / tool-call", () => {
it("converts assistant.tool_calls[] into content blocks of type tool-call", () => { it("converts assistant.tool_calls[] into content blocks of type tool-call", () => {
const out = openaiToCommandCodeRequest(MODEL, { const out = openaiToCommandCodeRequest(
MODEL,
{
messages: [ messages: [
{ role: "user", content: "go" }, { role: "user", content: "go" },
{ {
role: "assistant", role: "assistant",
content: null, content: null,
tool_calls: [ tool_calls: [
{ id: "call_42", type: "function", function: { name: "search", arguments: "{\"q\":\"hi\"}" } }, {
id: "call_42",
type: "function",
function: { name: "search", arguments: '{"q":"hi"}' },
},
], ],
}, },
], ],
}, true); },
true,
);
const asst = out.params.messages[1]; const asst = out.params.messages[1];
expect(asst.role).toBe("assistant"); expect(asst.role).toBe("assistant");
@@ -139,8 +289,10 @@ describe("openaiToCommandCodeRequest — assistant tool_calls / tool-call", () =
}); });
describe("openaiToCommandCodeRequest — tools schema conversion", () => { describe("openaiToCommandCodeRequest — tools schema conversion", () => {
it("converts OpenAI {type:\"function\", function:{...}} to Anthropic plain {name, input_schema}", () => { it('converts OpenAI {type:"function", function:{...}} to Anthropic plain {name, input_schema}', () => {
const out = openaiToCommandCodeRequest(MODEL, { const out = openaiToCommandCodeRequest(
MODEL,
{
messages: [{ role: "user", content: "hi" }], messages: [{ role: "user", content: "hi" }],
tools: [ tools: [
{ {
@@ -148,11 +300,17 @@ describe("openaiToCommandCodeRequest — tools schema conversion", () => {
function: { function: {
name: "weather", name: "weather",
description: "Get weather", description: "Get weather",
parameters: { type: "object", properties: { city: { type: "string" } }, required: ["city"] }, parameters: {
type: "object",
properties: { city: { type: "string" } },
required: ["city"],
},
}, },
}, },
], ],
}, true); },
true,
);
const t = out.params.tools[0]; const t = out.params.tools[0];
expect(t.name).toBe("weather"); expect(t.name).toBe("weather");
@@ -163,19 +321,34 @@ describe("openaiToCommandCodeRequest — tools schema conversion", () => {
}); });
it("preserves description on converted tool", () => { it("preserves description on converted tool", () => {
const out = openaiToCommandCodeRequest(MODEL, { const out = openaiToCommandCodeRequest(
MODEL,
{
messages: [{ role: "user", content: "hi" }], messages: [{ role: "user", content: "hi" }],
tools: [ tools: [
{ type: "function", function: { name: "ping", description: "Ping the server", parameters: { type: "object" } } }, {
type: "function",
function: {
name: "ping",
description: "Ping the server",
parameters: { type: "object" },
},
},
], ],
}, true); },
true,
);
expect(out.params.tools[0].description).toBe("Ping the server"); expect(out.params.tools[0].description).toBe("Ping the server");
}); });
it("does not include tools field when input has none", () => { it("does not include tools field when input has none", () => {
const out = openaiToCommandCodeRequest(MODEL, { const out = openaiToCommandCodeRequest(
MODEL,
{
messages: [{ role: "user", content: "hi" }], messages: [{ role: "user", content: "hi" }],
}, true); },
true,
);
expect(out.params.tools).toBeUndefined(); expect(out.params.tools).toBeUndefined();
}); });
}); });