chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,387 @@
|
||||
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 core = await import("../../src/lib/db/core.ts");
|
||||
|
||||
const originalFetch = globalThis.fetch;
|
||||
|
||||
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<string, string>) {
|
||||
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));
|
||||
}
|
||||
|
||||
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<string, unknown>; 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: 5, completion_tokens: 5, total_tokens: 10 });
|
||||
});
|
||||
|
||||
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/);
|
||||
});
|
||||
Reference in New Issue
Block a user