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,160 +13,200 @@ 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) {
if (content == null) return ""; if (content == null) return "";
if (typeof content === "string") return content; if (typeof content === "string") return content;
if (Array.isArray(content)) { if (Array.isArray(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");
return String(content); }
return String(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")
if (Array.isArray(content)) { return [{ type: OPENAI_BLOCK.TEXT, text: content }];
const blocks = []; if (Array.isArray(content)) {
for (const part of content) { const blocks = [];
if (typeof part === "string") { for (const part of content) {
blocks.push({ type: OPENAI_BLOCK.TEXT, text: part }); if (typeof part === "string") {
} else if (part && typeof part === "object") { blocks.push({ type: OPENAI_BLOCK.TEXT, text: part });
if (part.type === OPENAI_BLOCK.TEXT && typeof part.text === "string") { } else if (part && typeof part === "object") {
blocks.push({ type: OPENAI_BLOCK.TEXT, text: part.text }); if (part.type === OPENAI_BLOCK.TEXT && typeof part.text === "string") {
} else if (part.type === OPENAI_BLOCK.IMAGE_URL || part.type === OPENAI_BLOCK.IMAGE) { blocks.push({ type: OPENAI_BLOCK.TEXT, text: part.text });
blocks.push({ type: OPENAI_BLOCK.TEXT, text: "[image omitted]" }); } else if (
} else if (typeof part.text === "string") { part.type === OPENAI_BLOCK.IMAGE_URL ||
blocks.push({ type: OPENAI_BLOCK.TEXT, text: part.text }); 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).
return blocks.length ? blocks : [{ type: OPENAI_BLOCK.TEXT, text: "" }]; const src = part.source;
} let raw = part.image_url?.url || src?.data || src?.url || "";
return [{ type: OPENAI_BLOCK.TEXT, text: String(content) }]; 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") {
blocks.push({ type: OPENAI_BLOCK.TEXT, text: part.text });
}
}
}
return blocks.length ? blocks : [{ type: OPENAI_BLOCK.TEXT, text: "" }];
}
return [{ type: OPENAI_BLOCK.TEXT, text: String(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 = []) {
const out = []; const out = [];
const systemTexts = []; const systemTexts = [];
for (const m of messages) { for (const m of messages) {
if (!m) continue; if (!m) continue;
const role = m.role; const role = m.role;
if (role === ROLE.SYSTEM) { if (role === ROLE.SYSTEM) {
const t = flattenText(m.content); const t = flattenText(m.content);
if (t) systemTexts.push(t); if (t) systemTexts.push(t);
continue; continue;
} }
if (role === ROLE.TOOL) { if (role === ROLE.TOOL) {
const value = typeof m.content === "string" ? m.content : flattenText(m.content); const value =
out.push({ typeof m.content === "string" ? m.content : flattenText(m.content);
role: ROLE.TOOL, out.push({
content: [{ role: ROLE.TOOL,
type: "tool-result", content: [
toolCallId: m.tool_call_id || "", {
toolName: m.name || "", type: "tool-result",
output: { type: "text", value }, toolCallId: m.tool_call_id || "",
}], toolName: m.name || "",
}); output: { type: "text", value },
continue; },
} ],
});
continue;
}
if (role === ROLE.ASSISTANT) { if (role === ROLE.ASSISTANT) {
const blocks = []; const blocks = [];
const text = flattenText(m.content); const text = flattenText(m.content);
if (text) blocks.push({ type: OPENAI_BLOCK.TEXT, text }); if (text) blocks.push({ type: OPENAI_BLOCK.TEXT, text });
if (Array.isArray(m.tool_calls)) { if (Array.isArray(m.tool_calls)) {
for (const tc of m.tool_calls) { for (const tc of m.tool_calls) {
const fn = tc.function || {}; const fn = tc.function || {};
blocks.push({ blocks.push({
type: "tool-call", type: "tool-call",
toolCallId: tc.id || "", toolCallId: tc.id || "",
toolName: fn.name || "", toolName: fn.name || "",
input: safeParseJson(fn.arguments), input: safeParseJson(fn.arguments),
}); });
} }
} }
out.push({ role: ROLE.ASSISTANT, content: blocks.length ? blocks : [{ type: OPENAI_BLOCK.TEXT, text: "" }] }); out.push({
continue; role: ROLE.ASSISTANT,
} content: blocks.length
? blocks
: [{ type: OPENAI_BLOCK.TEXT, text: "" }],
});
continue;
}
out.push({ role: ROLE.USER, content: toContentBlocks(m.content) }); out.push({ role: ROLE.USER, content: toContentBlocks(m.content) });
} }
return { messages: out, system: systemTexts.join("\n\n") }; return { messages: out, system: systemTexts.join("\n\n") };
} }
function convertTools(tools) { function convertTools(tools) {
if (!Array.isArray(tools) || tools.length === 0) return undefined; if (!Array.isArray(tools) || tools.length === 0) return undefined;
const result = []; const result = [];
for (const t of tools) { for (const t of tools) {
if (!t) continue; if (!t) continue;
if (t.type === OPENAI_BLOCK.FUNCTION && t.function) { if (t.type === OPENAI_BLOCK.FUNCTION && t.function) {
result.push({ result.push({
name: t.function.name, name: t.function.name,
description: t.function.description, description: t.function.description,
input_schema: t.function.parameters || { type: "object" }, input_schema: t.function.parameters || { type: "object" },
}); });
} else if (t.name && (t.input_schema || t.parameters)) { } else if (t.name && (t.input_schema || t.parameters)) {
result.push({ result.push({
name: t.name, name: t.name,
description: t.description, description: t.description,
input_schema: t.input_schema || t.parameters, input_schema: t.input_schema || t.parameters,
}); });
} }
} }
return result.length ? result : undefined; return result.length ? result : undefined;
} }
export function openaiToCommandCodeRequest(model, body, stream /* , credentials */) { export function openaiToCommandCodeRequest(
const { messages, system } = convertMessages(body.messages); model,
const params = { body,
model, stream /* , credentials */,
messages, ) {
stream: stream !== false, const { messages, system } = convertMessages(body.messages);
max_tokens: body.max_tokens ?? body.max_output_tokens ?? DEFAULT_MAX_TOKENS, const params = {
temperature: body.temperature ?? 0.3, model,
}; messages,
stream: stream !== false,
max_tokens: body.max_tokens ?? body.max_output_tokens ?? DEFAULT_MAX_TOKENS,
temperature: body.temperature ?? 0.3,
};
if (system) params.system = system; if (system) params.system = system;
const tools = convertTools(body.tools); const tools = convertTools(body.tools);
if (tools) params.tools = tools; if (tools) params.tools = tools;
if (body.top_p != null) params.top_p = body.top_p; if (body.top_p != null) params.top_p = body.top_p;
const today = new Date().toISOString().slice(0, 10); const today = new Date().toISOString().slice(0, 10);
return { return {
threadId: randomUUID(), threadId: randomUUID(),
memory: "", memory: "",
config: { config: {
workingDir: process.cwd(), workingDir: process.cwd(),
date: today, date: today,
environment: process.platform, environment: process.platform,
structure: [], structure: [],
isGitRepo: false, isGitRepo: false,
currentBranch: "", currentBranch: "",
mainBranch: "", mainBranch: "",
gitStatus: "", gitStatus: "",
recentCommits: [], recentCommits: [],
}, },
params, params,
}; };
} }
register(FORMATS.OPENAI, FORMATS.COMMANDCODE, openaiToCommandCodeRequest, null); register(FORMATS.OPENAI, FORMATS.COMMANDCODE, openaiToCommandCodeRequest, null);

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,73 +4,133 @@ 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
// KNOWN BUG // KNOWN BUG
it.fails("multiple system messages are all kept", () => { it.fails("multiple system messages are all kept", () => {
const out = O2G({ const out = O2G({
messages: [ messages: [
{ role: "system", content: "RULE_ONE" }, { role: "system", content: "RULE_ONE" },
{ role: "system", content: "RULE_TWO" }, { role: "system", content: "RULE_TWO" },
{ 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");
});
}); });
describe("OpenAI → Cursor", () => { describe("OpenAI → Cursor", () => {
// openai-to-cursor.js:12-24 — image content fully dropped (text only) // openai-to-cursor.js:12-24 — image content fully dropped (text only)
// 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: [
{ type: "text", text: "look" }, {
{ type: "image_url", image_url: { url: "data:image/png;base64,AAAA" } }, role: "user",
] }], content: [
}); { type: "text", text: "look" },
expect(JSON.stringify(out), "image dropped").toContain("AAAA"); {
}); type: "image_url",
image_url: { url: "data:image/png;base64,AAAA" },
},
],
},
],
});
expect(JSON.stringify(out), "image dropped").toContain("AAAA");
});
// 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({
expect(out.max_tokens).toBe(200); max_tokens: 200,
}); messages: [{ role: "user", content: "hi" }],
});
expect(out.max_tokens).toBe(200);
});
}); });
describe("OpenAI → CommandCode", () => { describe("OpenAI → CommandCode", () => {
// openai-to-commandcode.js:53-57 — safeParseJson returns {} on bad JSON (args silently lost) // openai-to-commandcode.js:53-57 — safeParseJson returns {} on bad JSON (args silently lost)
// KNOWN BUG // KNOWN BUG
it.fails("malformed tool arguments are not silently emptied", () => { it.fails("malformed tool arguments are not silently emptied", () => {
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: "",
{ role: "tool", tool_call_id: "c1", content: "r" }, tool_calls: [
], {
}); id: "c1",
const asst = out.params.messages.find((m) => m.role === "assistant"); type: "function",
const call = asst.content.find((b) => b.type === "tool-call"); function: { name: "f", arguments: "{bad" },
expect(Object.keys(call.input).length, "arguments silently dropped to {}").toBeGreaterThan(0); },
}); ],
},
{ role: "tool", tool_call_id: "c1", content: "r" },
],
});
const asst = out.params.messages.find((m) => m.role === "assistant");
const call = asst.content.find((b) => b.type === "tool-call");
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: [
{ type: "text", text: "look" }, {
{ type: "image_url", image_url: { url: "data:image/png;base64,BBBB" } }, role: "user",
] }], content: [
}); { type: "text", text: "look" },
expect(JSON.stringify(out), "image omitted").toContain("BBBB"); {
}); type: "image_url",
image_url: { url: "data:image/png;base64,BBBB" },
},
],
},
],
});
expect(JSON.stringify(out), "image omitted").toContain("BBBB");
});
}); });

View File

@@ -14,168 +14,341 @@ import { openaiToCommandCodeRequest } from "../../open-sse/translator/request/op
const MODEL = "moonshotai/Kimi-K2.6"; 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(
messages: [{ role: "user", content: "hi" }], MODEL,
}, true); {
messages: [{ role: "user", content: "hi" }],
},
true,
);
expect(out).toHaveProperty("threadId"); expect(out).toHaveProperty("threadId");
expect(out).toHaveProperty("memory"); expect(out).toHaveProperty("memory");
expect(out).toHaveProperty("config"); expect(out).toHaveProperty("config");
expect(out).toHaveProperty("params"); expect(out).toHaveProperty("params");
expect(out.params.model).toBe(MODEL); expect(out.params.model).toBe(MODEL);
expect(out.params.stream).toBe(true); expect(out.params.stream).toBe(true);
}); });
}); });
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(
messages: [ MODEL,
{ role: "system", content: "You are concise." }, {
{ role: "user", content: "hi" }, messages: [
], { role: "system", content: "You are concise." },
}, true); { role: "user", content: "hi" },
],
},
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.");
const roles = out.params.messages.map((m) => m.role); const roles = out.params.messages.map((m) => m.role);
expect(roles).not.toContain("system"); expect(roles).not.toContain("system");
}); });
it("joins multiple system messages with blank line", () => { it("joins multiple system messages with blank line", () => {
const out = openaiToCommandCodeRequest(MODEL, { const out = openaiToCommandCodeRequest(
messages: [ MODEL,
{ role: "system", content: "A" }, {
{ role: "system", content: "B" }, messages: [
{ role: "user", content: "hi" }, { role: "system", content: "A" },
], { role: "system", content: "B" },
}, true); { 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", () => { it("omits params.system when no system messages", () => {
const out = openaiToCommandCodeRequest(MODEL, { const out = openaiToCommandCodeRequest(
messages: [{ role: "user", content: "hi" }], MODEL,
}, true); {
expect(out.params.system).toBeUndefined(); 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", () => { 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(
messages: [{ role: "user", content: "hello" }], MODEL,
}, true); {
messages: [{ role: "user", content: "hello" }],
},
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);
expect(u.content[0]).toEqual({ type: "text", text: "hello" }); expect(u.content[0]).toEqual({ type: "text", text: "hello" });
}); });
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(
messages: [ MODEL,
{ role: "user", content: "a" }, {
{ role: "assistant", content: "b" }, messages: [
], { role: "user", content: "a" },
}, true); { role: "assistant", content: "b" },
const a = out.params.messages[1]; ],
expect(Array.isArray(a.content)).toBe(true); },
expect(a.content[0]).toEqual({ type: "text", text: "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)", () => { 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(
messages: [ MODEL,
{ role: "user", content: "run X" }, {
{ messages: [
role: "assistant", { role: "user", content: "run X" },
content: null, {
tool_calls: [ role: "assistant",
{ id: "call_1", type: "function", function: { name: "do_x", arguments: "{\"a\":1}" } }, content: null,
], tool_calls: [
}, {
{ role: "tool", tool_call_id: "call_1", name: "do_x", content: "RESULT_OK" }, id: "call_1",
], type: "function",
}, true); 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]; const toolMsg = out.params.messages[out.params.messages.length - 1];
expect(toolMsg.role).toBe("tool"); expect(toolMsg.role).toBe("tool");
const block = toolMsg.content[0]; const block = toolMsg.content[0];
expect(block.type).toBe("tool-result"); expect(block.type).toBe("tool-result");
expect(block.toolCallId).toBe("call_1"); expect(block.toolCallId).toBe("call_1");
expect(block.toolName).toBe("do_x"); expect(block.toolName).toBe("do_x");
expect(block.output).toEqual({ type: "text", value: "RESULT_OK" }); expect(block.output).toEqual({ type: "text", value: "RESULT_OK" });
}); });
}); });
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(
messages: [ MODEL,
{ role: "user", content: "go" }, {
{ messages: [
role: "assistant", { role: "user", content: "go" },
content: null, {
tool_calls: [ role: "assistant",
{ id: "call_42", type: "function", function: { name: "search", arguments: "{\"q\":\"hi\"}" } }, content: null,
], tool_calls: [
}, {
], id: "call_42",
}, true); type: "function",
function: { name: "search", arguments: '{"q":"hi"}' },
},
],
},
],
},
true,
);
const asst = out.params.messages[1]; const asst = out.params.messages[1];
expect(asst.role).toBe("assistant"); expect(asst.role).toBe("assistant");
const tc = asst.content.find((b) => b.type === "tool-call"); const tc = asst.content.find((b) => b.type === "tool-call");
expect(tc).toBeDefined(); expect(tc).toBeDefined();
expect(tc.toolCallId).toBe("call_42"); expect(tc.toolCallId).toBe("call_42");
expect(tc.toolName).toBe("search"); expect(tc.toolName).toBe("search");
expect(tc.input).toEqual({ q: "hi" }); expect(tc.input).toEqual({ q: "hi" });
}); });
}); });
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(
messages: [{ role: "user", content: "hi" }], MODEL,
tools: [ {
{ messages: [{ role: "user", content: "hi" }],
type: "function", tools: [
function: { {
name: "weather", type: "function",
description: "Get weather", function: {
parameters: { type: "object", properties: { city: { type: "string" } }, required: ["city"] }, name: "weather",
}, description: "Get weather",
}, parameters: {
], type: "object",
}, true); properties: { city: { type: "string" } },
required: ["city"],
},
},
},
],
},
true,
);
const t = out.params.tools[0]; const t = out.params.tools[0];
expect(t.name).toBe("weather"); expect(t.name).toBe("weather");
expect(t.input_schema).toBeDefined(); expect(t.input_schema).toBeDefined();
expect(t.input_schema.type).toBe("object"); expect(t.input_schema.type).toBe("object");
expect(t.function).toBeUndefined(); expect(t.function).toBeUndefined();
expect(t.parameters).toBeUndefined(); expect(t.parameters).toBeUndefined();
}); });
it("preserves description on converted tool", () => { it("preserves description on converted tool", () => {
const out = openaiToCommandCodeRequest(MODEL, { const out = openaiToCommandCodeRequest(
messages: [{ role: "user", content: "hi" }], MODEL,
tools: [ {
{ type: "function", function: { name: "ping", description: "Ping the server", parameters: { type: "object" } } }, messages: [{ role: "user", content: "hi" }],
], tools: [
}, true); {
expect(out.params.tools[0].description).toBe("Ping the server"); 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", () => { it("does not include tools field when input has none", () => {
const out = openaiToCommandCodeRequest(MODEL, { const out = openaiToCommandCodeRequest(
messages: [{ role: "user", content: "hi" }], MODEL,
}, true); {
expect(out.params.tools).toBeUndefined(); messages: [{ role: "user", content: "hi" }],
}); },
true,
);
expect(out.params.tools).toBeUndefined();
});
}); });