import test from "node:test"; import assert from "node:assert/strict"; import fs from "node:fs"; import os from "node:os"; import path from "node:path"; const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-command-code-executor-")); process.env.DATA_DIR = TEST_DATA_DIR; const { REGISTRY, getRegistryEntry } = await import("../../open-sse/config/providerRegistry.ts"); const { CommandCodeExecutor, COMMAND_CODE_VERSION } = await import("../../open-sse/executors/commandCode.ts"); const { getExecutor, hasSpecializedExecutor } = await import("../../open-sse/executors/index.ts"); const { createResponsesApiTransformStream } = await import("../../open-sse/transformer/responsesTransformer.ts"); const core = await import("../../src/lib/db/core.ts"); const originalFetch = globalThis.fetch; type JsonRecord = Record; type ResponsesEvent = { event: string; data: { response: JsonRecord & { usage?: unknown; output?: JsonRecord[] } }; }; const PINNED_COMMAND_CODE_MODELS = [ "claude-opus-4-7", "claude-opus-4-6", "claude-sonnet-4-6", "claude-haiku-4-5-20251001", "gpt-5.5", "gpt-5.4", "gpt-5.3-codex", "gpt-5.4-mini", "deepseek/deepseek-v4-pro", "deepseek/deepseek-v4-flash", "moonshotai/Kimi-K2.6", "moonshotai/Kimi-K2.5", "zai-org/GLM-5.1", "zai-org/GLM-5", "MiniMaxAI/MiniMax-M2.7", "MiniMaxAI/MiniMax-M2.5", "Qwen/Qwen3.6-Max-Preview", "Qwen/Qwen3.6-Plus", ]; function commandCodeStream(lines: unknown[], { sse = false } = {}) { const text = lines .map((line) => { const json = JSON.stringify(line); return sse ? `data: ${json}\n\n` : `${json}\n`; }) .join(""); return new Response(text, { status: 200, headers: { "Content-Type": "application/x-ndjson" } }); } function toPlainHeaders(headers: Headers | Record) { if (headers instanceof Headers) return Object.fromEntries(headers.entries()); return Object.fromEntries(Object.entries(headers).map(([key, value]) => [key, String(value)])); } function parseSsePayloads(sse: string) { return sse .split("\n") .filter((line) => line.startsWith("data: ")) .map((line) => line.slice(6).trim()) .filter((line) => line && line !== "[DONE]") .map((line) => JSON.parse(line)); } async function responsesFromChatSse(sse: string): Promise { const input = new ReadableStream({ start(controller) { controller.enqueue(new TextEncoder().encode(sse)); controller.close(); }, }); const transformed = await new Response( input.pipeThrough(createResponsesApiTransformStream(null, 60_000)) ).text(); return transformed .split("\n\n") .map((part) => { const event = part.match(/^event:\s*(.+)$/m)?.[1]; const data = part.match(/^data:\s*(.+)$/m)?.[1]; return event && data ? ({ event, data: JSON.parse(data) } as ResponsesEvent) : null; }) .filter((entry): entry is ResponsesEvent => entry !== null); } test.afterEach(() => { globalThis.fetch = originalFetch; }); test.after(() => { globalThis.fetch = originalFetch; core.resetDbInstance(); fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true }); }); test("Command Code provider catalog has pinned models and alias lookup", () => { const entry = REGISTRY["command-code"]; assert.ok(entry); assert.equal(entry.alias, "cmd"); assert.equal(entry.executor, "command-code"); assert.equal(entry.baseUrl, "https://api.commandcode.ai"); assert.equal(entry.chatPath, "/alpha/generate"); assert.deepEqual( entry.models.map((model) => model.id), PINNED_COMMAND_CODE_MODELS ); assert.equal(getRegistryEntry("cmd"), entry); }); test("getExecutor returns the specialized Command Code executor", () => { assert.equal(hasSpecializedExecutor("command-code"), true); assert.ok(getExecutor("command-code") instanceof CommandCodeExecutor); assert.ok(getExecutor("cmd") instanceof CommandCodeExecutor); }); type FetchCall = { url: string; init: Record; body?: unknown }; test("Command Code executor posts wrapped body and required headers to /alpha/generate", async () => { const calls: FetchCall[] = []; globalThis.fetch = async (url, init = {}) => { calls.push({ url: String(url), init }); return commandCodeStream([{ type: "text-delta", text: "hello" }, { type: "finish" }]); }; const executor = getExecutor("command-code"); const { response, url, headers, transformedBody } = await executor.execute({ model: "gpt-5.4-mini", stream: false, credentials: { apiKey: "cc_test_key" }, body: { stream: false, messages: [ { role: "system", content: "You are concise." }, { role: "user", content: "Hi" }, ], tools: [{ type: "function", function: { name: "lookup", parameters: { type: "object" } } }], max_tokens: 42, }, }); assert.equal(url, "https://api.commandcode.ai/alpha/generate"); assert.equal(calls.length, 1); assert.equal(calls[0].url, "https://api.commandcode.ai/alpha/generate"); assert.equal(calls[0].init.method, "POST"); assert.equal(headers.Authorization, "Bearer cc_test_key"); assert.equal(headers["x-command-code-version"], COMMAND_CODE_VERSION); assert.equal(headers["x-cli-environment"], "external"); assert.equal(headers["x-project-slug"], "pi-cc"); assert.equal(headers["x-taste-learning"], "false"); assert.equal(headers["x-co-flag"], "false"); assert.equal(typeof headers["x-session-id"], "string"); const posted = JSON.parse(String(calls[0].init.body)); assert.deepEqual(posted, transformedBody); for (const key of ["config", "memory", "taste", "skills", "permissionMode", "params"]) { assert.ok(key in posted, `missing ${key}`); } assert.equal(posted.skills, ""); assert.equal(posted.params.model, "gpt-5.4-mini"); assert.equal(posted.params.stream, true); assert.equal(posted.params.system, "You are concise."); assert.equal(posted.params.messages[0].role, "user"); assert.equal(posted.params.tools[0].name, "lookup"); const json = await response.json(); assert.equal(json.choices[0].message.content, "hello"); }); test("Command Code executor passes reasoning/thinking fields through to params (#2986 follow-up)", async () => { const calls: FetchCall[] = []; globalThis.fetch = async (url, init = {}) => { calls.push({ url: String(url), init }); return commandCodeStream([{ type: "text-delta", text: "ok" }, { type: "finish" }]); }; await getExecutor("command-code").execute({ model: "deepseek/deepseek-v4-pro", stream: false, credentials: { apiKey: "cc_test_key" }, body: { stream: false, messages: [{ role: "user", content: "Hi" }], reasoning_effort: "high", thinking: { type: "enabled" }, effort: "high", output_config: { effort: "high" }, extra_body: { enable_thinking: true }, }, }); const posted = JSON.parse(String(calls[0].init.body)); assert.equal(posted.params.reasoning_effort, "high"); assert.deepEqual(posted.params.thinking, { type: "enabled" }); assert.equal(posted.params.effort, "high"); assert.deepEqual(posted.params.output_config, { effort: "high" }); assert.deepEqual(posted.params.extra_body, { enable_thinking: true }); }); test("Command Code executor honors body.model rewrite from payload rules", async () => { const calls: FetchCall[] = []; globalThis.fetch = async (url, init = {}) => { calls.push({ url: String(url), init }); return commandCodeStream([{ type: "text-delta", text: "ok" }, { type: "finish" }]); }; // Simulate a payload-rule rewrite: combo resolves to "deepseek-v4-pro-max" // (passed as the execute() model arg), but the payload rule overwrites // body.model to "deepseek/deepseek-v4-pro" (the vendor-prefixed form // Command Code's API expects). await getExecutor("command-code").execute({ model: "deepseek-v4-pro-max", stream: false, credentials: { apiKey: "cc_test_key" }, body: { stream: false, model: "deepseek/deepseek-v4-pro", messages: [{ role: "user", content: "Hi" }], reasoning_effort: "max", }, }); const posted = JSON.parse(String(calls[0].init.body)); assert.equal(posted.params.model, "deepseek/deepseek-v4-pro"); assert.equal(posted.params.reasoning_effort, "max"); }); test("Command Code raw NDJSON stream becomes OpenAI chat SSE chunks", async () => { const calls: FetchCall[] = []; globalThis.fetch = async (url, init = {}) => { calls.push({ url: String(url), init, body: JSON.parse(String(init.body)) }); return commandCodeStream([ { type: "text-delta", text: "Hello" }, { type: "reasoning-delta", text: "thinking" }, { type: "tool-call", toolCallId: "call_1", toolName: "search", input: { q: "docs" } }, { type: "finish", finishReason: "tool-calls" }, ]); }; const { response } = await getExecutor("command-code").execute({ model: "gpt-5.4", stream: true, credentials: { apiKey: "cc_test_key" }, body: { messages: [{ role: "user", content: "Hi" }] }, }); assert.equal(calls[0].body.params.stream, true); assert.equal(response.headers.get("Content-Type"), "text/event-stream; charset=utf-8"); const sse = await response.text(); assert.match(sse, /data: \[DONE\]/); const chunks = parseSsePayloads(sse); assert.equal(chunks[0].object, "chat.completion.chunk"); assert.deepEqual(chunks[0].choices[0].delta, { role: "assistant" }); assert.equal(chunks[1].choices[0].delta.content, "Hello"); assert.equal(chunks[2].choices[0].delta.reasoning_content, "thinking"); assert.equal(chunks[3].choices[0].delta.tool_calls[0].function.name, "search"); assert.equal(chunks.at(-1).choices[0].finish_reason, "tool_calls"); }); test("Command Code data: SSE lines aggregate into non-stream ChatCompletion JSON", async () => { globalThis.fetch = async () => commandCodeStream( [ { type: "text-delta", text: "Hel" }, { type: "text-delta", text: "lo" }, { type: "reasoning-delta", text: "because" }, { type: "tool-call", id: "call_2", name: "lookup", arguments: { id: 7 } }, { type: "finish", finishReason: "max_tokens", totalUsage: { inputTokens: 3, inputTokenDetails: { cacheReadTokens: 2 }, outputTokens: 5, }, }, ], { sse: true } ); const { response } = await getExecutor("command-code").execute({ model: "gpt-5.4-mini", stream: false, credentials: { apiKey: "cc_test_key" }, body: { messages: [{ role: "user", content: "Hi" }] }, }); assert.equal(response.headers.get("Content-Type"), "application/json"); const json = await response.json(); assert.equal(json.object, "chat.completion"); assert.equal(json.choices[0].message.content, "Hello"); assert.equal(json.choices[0].message.reasoning_content, "because"); assert.equal(json.choices[0].message.tool_calls[0].function.arguments, JSON.stringify({ id: 7 })); assert.equal(json.choices[0].finish_reason, "length"); assert.deepEqual(json.usage, { prompt_tokens: 3, prompt_tokens_details: { cached_tokens: 2 }, completion_tokens: 5, completion_tokens_details: { reasoning_tokens: 0 }, total_tokens: 8, cache_read_input_tokens: 2, }); }); test("Command Code executor surfaces upstream and streamed errors", async () => { globalThis.fetch = async () => new Response("bad key", { status: 401, statusText: "Unauthorized" }); const upstreamFailure = await getExecutor("command-code").execute({ model: "gpt-5.4-mini", stream: false, credentials: { apiKey: "cc_test_key" }, body: { messages: [{ role: "user", content: "Hi" }] }, }); assert.equal(upstreamFailure.response.status, 401); assert.equal(await upstreamFailure.response.text(), "bad key"); globalThis.fetch = async () => commandCodeStream([{ type: "error", error: { message: "boom" } }]); await assert.rejects(async () => { await getExecutor("command-code").execute({ model: "gpt-5.4-mini", stream: false, credentials: { apiKey: "cc_test_key" }, body: { messages: [{ role: "user", content: "Hi" }] }, }); }, /boom/); }); test("Command Code executor omits max_tokens when the client does not supply one (GLM-5.x)", async () => { const calls: FetchCall[] = []; globalThis.fetch = async (url, init = {}) => { calls.push({ url: String(url), init, body: JSON.parse(String(init.body)) }); return commandCodeStream([{ type: "text-delta", text: "ok" }, { type: "finish" }]); }; // No client max_tokens: we must NOT fabricate one. Omitting the field lets // Command Code's upstream apply the model's own native default. await getExecutor("command-code").execute({ model: "zai-org/GLM-5.1", stream: false, credentials: { apiKey: "cc_test_key" }, body: { messages: [{ role: "user", content: "Hi" }] }, }); assert.ok(!("max_tokens" in calls[0].body.params)); }); test("Command Code executor omits max_tokens for DeepSeek v4 when the client does not supply one", async () => { const calls: FetchCall[] = []; globalThis.fetch = async (url, init = {}) => { calls.push({ url: String(url), init, body: JSON.parse(String(init.body)) }); return commandCodeStream([{ type: "text-delta", text: "ok" }, { type: "finish" }]); }; // Regression: previously the executor invented max_tokens from the registry // (384000), which /alpha/generate rejects with a 400 // "Too big: expected number to be <=200000". With no client value we now omit // the field entirely, so the request succeeds and upstream picks the default. await getExecutor("command-code").execute({ model: "deepseek/deepseek-v4-pro", stream: false, credentials: { apiKey: "cc_test_key" }, body: { messages: [{ role: "user", content: "Hi" }] }, }); assert.ok(!("max_tokens" in calls[0].body.params)); }); test("Command Code executor clamps an oversized client-supplied max_tokens to the endpoint ceiling", async () => { const calls: FetchCall[] = []; globalThis.fetch = async (url, init = {}) => { calls.push({ url: String(url), init, body: JSON.parse(String(init.body)) }); return commandCodeStream([{ type: "text-delta", text: "ok" }, { type: "finish" }]); }; // A client asking for more than the 200000 endpoint ceiling is clamped down // (not 400'd), mirroring the provider-driven clamp in antigravity.ts. await getExecutor("command-code").execute({ model: "deepseek/deepseek-v4-pro", stream: false, credentials: { apiKey: "cc_test_key" }, body: { messages: [{ role: "user", content: "Hi" }], max_tokens: 500000 }, }); assert.equal(calls[0].body.params.max_tokens, 200000); }); test("Command Code executor honors a smaller client-provided max_tokens under the per-model cap", async () => { const calls: FetchCall[] = []; globalThis.fetch = async (url, init = {}) => { calls.push({ url: String(url), init, body: JSON.parse(String(init.body)) }); return commandCodeStream([{ type: "text-delta", text: "ok" }, { type: "finish" }]); }; await getExecutor("command-code").execute({ model: "zai-org/GLM-5.1", stream: false, credentials: { apiKey: "cc_test_key" }, body: { messages: [{ role: "user", content: "Hi" }], max_tokens: 2048 }, }); assert.equal(calls[0].body.params.max_tokens, 2048); }); test("Command Code non-stream aggregation throws when the final error event lacks a trailing newline", async () => { globalThis.fetch = async () => new Response( `${JSON.stringify({ type: "text-delta", text: "Hello" })}\n${JSON.stringify({ type: "error", error: { message: "boom" }, })}`, { status: 200, headers: { "Content-Type": "application/x-ndjson" } } ); await assert.rejects(async () => { await getExecutor("command-code").execute({ model: "gpt-5.4-mini", stream: false, credentials: { apiKey: "cc_test_key" }, body: { messages: [{ role: "user", content: "Hi" }] }, }); }, /boom/); }); test("Command Code usage chunk surfaces cache_read and no_cache for the stream pipeline", async () => { globalThis.fetch = async () => commandCodeStream([ { type: "text-delta", text: "Hi" }, { type: "finish", finishReason: "stop", totalUsage: { inputTokens: 10, inputTokenDetails: { noCacheTokens: 6, cacheReadTokens: 4 }, outputTokens: 6, }, }, ]); const { response } = await getExecutor("command-code").execute({ model: "gpt-5.4-mini", stream: true, credentials: { apiKey: "cc_test_key" }, body: { messages: [{ role: "user", content: "Hi" }] }, }); const sse = await response.text(); const chunks = parseSsePayloads(sse); const usageChunk = chunks.find( (chunk) => Array.isArray(chunk.choices) && chunk.choices.length === 0 ); assert.ok(usageChunk, "expected a usage-only chunk (choices: []) in the stream"); // The usage-only chunk feeds stream.ts's extractUsage, which surfaces // cache_read_input_tokens / no_cache_tokens into the [USAGE] line. const { extractUsage } = await import("../../open-sse/utils/usageTracking.ts"); const extracted = extractUsage(usageChunk); assert.ok(extracted, "extractUsage should recognize the usage-only chunk"); assert.equal(extracted.prompt_tokens, 10); assert.equal(extracted.completion_tokens, 6); assert.equal(extracted.cache_read_input_tokens, 4); assert.equal(extracted.no_cache_tokens, 6); }); test("Command Code stream emits a usage-only chunk with actual tokens before [DONE]", async () => { globalThis.fetch = async () => commandCodeStream([ { type: "text-delta", text: "Hi" }, { type: "finish", finishReason: "stop", totalUsage: { inputTokens: 10, inputTokenDetails: { cacheReadTokens: 4, cacheCreationTokens: 2 }, outputTokens: 6, reasoningTokenDetails: { reasoningTokens: 1 }, }, }, ]); const { response } = await getExecutor("command-code").execute({ model: "gpt-5.4-mini", stream: true, credentials: { apiKey: "cc_test_key" }, body: { messages: [{ role: "user", content: "Hi" }] }, }); const sse = await response.text(); const chunks = parseSsePayloads(sse); // Find the usage-only chunk: choices must be [] and usage must carry the // actual upstream numbers. prompt_tokens = inputTokens (10) — cacheRead 4 is // already included in that 10, so it is reported separately, NOT re-added. const usageChunk = chunks.find( (chunk) => Array.isArray(chunk.choices) && chunk.choices.length === 0 ); assert.ok(usageChunk, "expected a usage-only chunk (choices: []) in the stream"); assert.deepEqual(usageChunk.usage, { prompt_tokens: 10, prompt_tokens_details: { cached_tokens: 4 }, completion_tokens: 6, completion_tokens_details: { reasoning_tokens: 1 }, total_tokens: 16, cache_read_input_tokens: 4, reasoning_tokens: 1, }); // The usage chunk must come before the [DONE] marker. assert.match(sse, /"usage":/); const doneIndex = sse.indexOf("data: [DONE]"); const usageIndex = sse.indexOf(`"choices":[]`); assert.ok(usageIndex > -1 && usageIndex < doneIndex, "usage chunk must precede [DONE]"); }); test("Command Code non-stream usage keeps inputTokens as prompt_tokens and reports cache separately", async () => { globalThis.fetch = async () => commandCodeStream( [ { type: "text-delta", text: "ok" }, { type: "finish", finishReason: "stop", totalUsage: { inputTokens: 5, inputTokenDetails: { noCacheTokens: 2, cacheReadTokens: 3 }, outputTokens: 2, }, }, ], { sse: true } ); const { response } = await getExecutor("command-code").execute({ model: "gpt-5.4-mini", stream: false, credentials: { apiKey: "cc_test_key" }, body: { messages: [{ role: "user", content: "Hi" }] }, }); const json = await response.json(); assert.deepEqual(json.usage, { prompt_tokens: 5, prompt_tokens_details: { cached_tokens: 3 }, completion_tokens: 2, completion_tokens_details: { reasoning_tokens: 0 }, total_tokens: 7, cache_read_input_tokens: 3, no_cache_tokens: 2, }); }); test("Command Code preserves finish-step usage through a finish without totalUsage", async () => { globalThis.fetch = async () => commandCodeStream([ { type: "text-delta", text: "Hi" }, { type: "finish-step", usage: { inputTokens: 7308, inputTokenDetails: { noCacheTokens: 27, cacheReadTokens: 7281 }, outputTokens: 177, outputTokenDetails: { textTokens: 12, reasoningTokens: 165 }, totalTokens: 7485, }, }, { type: "finish", finishReason: "stop", totalUsage: null }, ]); const { response } = await getExecutor("command-code").execute({ model: "gpt-5.4-mini", stream: true, credentials: { apiKey: "cc_test_key" }, body: { messages: [{ role: "user", content: "Hi" }] }, }); const sse = await response.text(); const chunks = parseSsePayloads(sse); const usageChunk = chunks.find( (chunk) => Array.isArray(chunk.choices) && chunk.choices.length === 0 ); assert.deepEqual(usageChunk?.usage, { prompt_tokens: 7308, prompt_tokens_details: { cached_tokens: 7281 }, completion_tokens: 177, completion_tokens_details: { reasoning_tokens: 165 }, total_tokens: 7485, cache_read_input_tokens: 7281, no_cache_tokens: 27, reasoning_tokens: 165, }); const completed = (await responsesFromChatSse(sse)).find( (event) => event.event === "response.completed" ); assert.deepEqual(completed?.data.response.usage, { input_tokens: 7308, input_tokens_details: { cached_tokens: 7281 }, output_tokens: 177, output_tokens_details: { reasoning_tokens: 165 }, total_tokens: 7485, }); }); test("Command Code accepts OpenAI-style usage aliases with absent optional details", async () => { globalThis.fetch = async () => commandCodeStream([ { type: "finish-step", usage: { prompt_tokens: 11, prompt_tokens_details: { cached_tokens: 4 }, completion_tokens: 5, completion_tokens_details: { reasoning_tokens: 2 }, total_tokens: 16, }, }, { type: "finish", finishReason: "stop" }, ]); const { response } = await getExecutor("command-code").execute({ model: "gpt-5.4-mini", stream: true, credentials: { apiKey: "cc_test_key" }, body: { messages: [{ role: "user", content: "Hi" }] }, }); const sse = await response.text(); const usageChunk = parseSsePayloads(sse).find( (chunk) => Array.isArray(chunk.choices) && chunk.choices.length === 0 ); assert.deepEqual(usageChunk?.usage, { prompt_tokens: 11, prompt_tokens_details: { cached_tokens: 4 }, completion_tokens: 5, completion_tokens_details: { reasoning_tokens: 2 }, total_tokens: 16, cache_read_input_tokens: 4, reasoning_tokens: 2, }); globalThis.fetch = async () => commandCodeStream([ { type: "finish-step", usage: { inputTokens: 4, outputTokens: 3, totalTokens: 7 } }, { type: "finish", finishReason: "stop", totalUsage: null }, ]); const fallback = await getExecutor("command-code").execute({ model: "gpt-5.4-mini", stream: true, credentials: { apiKey: "cc_test_key" }, body: { messages: [{ role: "user", content: "Hi" }] }, }); const fallbackSse = await fallback.response.text(); const completed = (await responsesFromChatSse(fallbackSse)).find( (event) => event.event === "response.completed" ); assert.deepEqual(completed?.data.response.usage, { input_tokens: 4, input_tokens_details: { cached_tokens: 0 }, output_tokens: 3, output_tokens_details: { reasoning_tokens: 0 }, total_tokens: 7, }); }); test("Command Code preserves tool-call streaming while finalizing finish-step usage", async () => { globalThis.fetch = async () => commandCodeStream([ { type: "tool-call", toolCallId: "call_1", toolName: "lookup", input: { query: "hello" }, }, { type: "finish-step", usage: { inputTokens: 3, outputTokens: 2, totalTokens: 5 } }, { type: "finish", finishReason: "tool-calls" }, ]); const { response } = await getExecutor("command-code").execute({ model: "gpt-5.4-mini", stream: true, credentials: { apiKey: "cc_test_key" }, body: { messages: [{ role: "user", content: "Hi" }] }, }); const sse = await response.text(); assert.ok( parseSsePayloads(sse).some( (chunk) => chunk.choices?.[0]?.delta?.tool_calls?.[0]?.id === "call_1" ) ); const completed = (await responsesFromChatSse(sse)).find( (event) => event.event === "response.completed" ); assert.equal( completed?.data.response.output?.some((item) => item.type === "function_call"), true ); assert.deepEqual(completed?.data.response.usage, { input_tokens: 3, input_tokens_details: { cached_tokens: 0 }, output_tokens: 2, output_tokens_details: { reasoning_tokens: 0 }, total_tokens: 5, }); });