mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-22 23:22:09 +03:00
⭐5 — Fecha #10265: chat do command-code migra do endpoint CLI-only /alpha/generate (version-gated + proxy-blocked para callers externos) para o documentado /provider/v1/chat/completions (OpenAI format). Removido o envelope CLI reverse-engineered (config/memory/taste/skills + headers CLI-impersonation), substituído por passthrough OpenAI plano com normalização de model id vendor-prefixed (#10809), clamp de max_tokens (#5166), sanitização de reasoning_effort. commandCode.ts 1037→171 linhas. Validado no worktree board sobre tip: typecheck:core limpo; 175/175 testes focados (command-code executor/vision/usage/maxtokens/user-array/validation-specialty/responses-handler/provider-models-scoping); changelog-integrity/file-size/complexity/cognitive todos OK. TDD RED→GREEN documentado.
This commit is contained in:
committed by
GitHub
parent
e06f8b7ec0
commit
b6412c6fed
@@ -1,14 +1,13 @@
|
||||
/**
|
||||
* Regression test for #5166 (user-content-array 400 on Command Code / deepseek-v4-pro).
|
||||
* #5166 (user-content-array 400 on Command Code / deepseek-v4-pro) context.
|
||||
*
|
||||
* When a client sends a user message whose `content` is an array of content parts
|
||||
* (e.g. [{type:"text",text:"Hello"},{type:"text",text:"World"}]), the raw array
|
||||
* must NOT reach the Command Code upstream — it requires user content to be a plain
|
||||
* string. The executor must normalise the array to a string before posting.
|
||||
*
|
||||
* NOTE: this file covers ONLY the user-content-array/400 symptom of #5166.
|
||||
* The 0-output-token symptom on mimo-v2.5-pro (reasoning-only models) is tracked
|
||||
* separately and is NOT addressed here.
|
||||
* The original regression was that a user message whose `content` was an array of
|
||||
* content parts reached the CLI-only /alpha/generate endpoint, which required
|
||||
* user content to be a plain string. Since #10265 the executor posts to the
|
||||
* documented /provider/v1/chat/completions endpoint, which natively speaks the
|
||||
* OpenAI chat.completions format — array content (text + image_url parts) is
|
||||
* valid there and passes through unchanged. These tests pin that OpenAI-shaped
|
||||
* passthrough.
|
||||
*/
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
@@ -26,9 +25,8 @@ const core = await import("../../src/lib/db/core.ts");
|
||||
|
||||
const originalFetch = globalThis.fetch;
|
||||
|
||||
function commandCodeStream(lines: unknown[]) {
|
||||
const text = lines.map((l) => JSON.stringify(l)).join("\n") + "\n";
|
||||
return new Response(text, { status: 200, headers: { "Content-Type": "application/x-ndjson" } });
|
||||
function okResponse() {
|
||||
return new Response("{}", { status: 200, headers: { "Content-Type": "application/json" } });
|
||||
}
|
||||
|
||||
test.after(() => {
|
||||
@@ -41,155 +39,100 @@ test.afterEach(() => {
|
||||
globalThis.fetch = originalFetch;
|
||||
});
|
||||
|
||||
// ── helpers ────────────────────────────────────────────────────────────────────
|
||||
// ── helpers ────────────────────────────────────────────────────────────
|
||||
|
||||
type FetchCall = { url: string; init: Record<string, unknown>; body: Record<string, unknown> };
|
||||
|
||||
function captureFetch(response: Response) {
|
||||
const calls: FetchCall[] = [];
|
||||
globalThis.fetch = async (url, init: RequestInit = {}) => {
|
||||
calls.push({ url: String(url), init: init as Record<string, unknown>, body: JSON.parse(String(init.body)) });
|
||||
calls.push({
|
||||
url: String(url),
|
||||
init: init as Record<string, unknown>,
|
||||
body: JSON.parse(String(init.body)),
|
||||
});
|
||||
return response;
|
||||
};
|
||||
return calls;
|
||||
}
|
||||
|
||||
// ── failing tests (before fix, user content is the raw array) ──────────────
|
||||
test("#5166 user message with multi-part array content passes through as an OpenAI array", async () => {
|
||||
const calls = captureFetch(okResponse());
|
||||
await getExecutor("command-code").execute({
|
||||
model: "deepseek/deepseek-v4-pro",
|
||||
stream: false,
|
||||
credentials: { apiKey: "cc_test_key" },
|
||||
body: {
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: [
|
||||
{ type: "text", text: "Hello" },
|
||||
{ type: "text", text: "World" },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
test(
|
||||
"#5166 user message with multi-part array content is flattened to a string (#5166)",
|
||||
async () => {
|
||||
const calls = captureFetch(
|
||||
commandCodeStream([{ type: "text-delta", text: "ok" }, { type: "finish" }])
|
||||
);
|
||||
const userMsg = (calls[0].body.messages as Record<string, unknown>[])[0];
|
||||
// OpenAI array content is valid on /provider/v1 — forwarded as-is.
|
||||
assert.ok(Array.isArray(userMsg.content), "array content forwarded (no CLI flattening)");
|
||||
const parts = userMsg.content as Record<string, unknown>[];
|
||||
assert.equal(parts.length, 2);
|
||||
assert.equal(parts[0].text, "Hello");
|
||||
assert.equal(parts[1].text, "World");
|
||||
});
|
||||
|
||||
await getExecutor("command-code").execute({
|
||||
model: "deepseek/deepseek-v4-pro",
|
||||
stream: false,
|
||||
credentials: { apiKey: "cc_test_key" },
|
||||
body: {
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: [
|
||||
{ type: "text", text: "Hello" },
|
||||
{ type: "text", text: "World" },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
test("#5166 user message with single text-part array passes through", async () => {
|
||||
const calls = captureFetch(okResponse());
|
||||
await getExecutor("command-code").execute({
|
||||
model: "deepseek/deepseek-v4-pro",
|
||||
stream: false,
|
||||
credentials: { apiKey: "cc_test_key" },
|
||||
body: {
|
||||
messages: [{ role: "user", content: [{ type: "text", text: "Hi there" }] }],
|
||||
},
|
||||
});
|
||||
const userMsg = (calls[0].body.messages as Record<string, unknown>[])[0];
|
||||
const parts = userMsg.content as Record<string, unknown>[];
|
||||
assert.equal(parts.length, 1);
|
||||
assert.equal(parts[0].text, "Hi there");
|
||||
});
|
||||
|
||||
const posted = calls[0].body;
|
||||
const userMsg = (posted.params as Record<string, unknown[]>).messages[0] as Record<
|
||||
string,
|
||||
unknown
|
||||
>;
|
||||
test("#5166 user message with plain string content passes through unchanged", async () => {
|
||||
const calls = captureFetch(okResponse());
|
||||
await getExecutor("command-code").execute({
|
||||
model: "deepseek/deepseek-v4-pro",
|
||||
stream: false,
|
||||
credentials: { apiKey: "cc_test_key" },
|
||||
body: { messages: [{ role: "user", content: "Plain string message" }] },
|
||||
});
|
||||
const userMsg = (calls[0].body.messages as Record<string, unknown>[])[0];
|
||||
assert.equal(userMsg.content, "Plain string message");
|
||||
});
|
||||
|
||||
// Must be a string — never an array — otherwise Command Code's upstream returns 400.
|
||||
assert.equal(
|
||||
typeof userMsg.content,
|
||||
"string",
|
||||
`user message content must be a string, got ${typeof userMsg.content}`
|
||||
);
|
||||
// Joined text parts with "\n"
|
||||
assert.equal(userMsg.content, "Hello\nWorld");
|
||||
}
|
||||
);
|
||||
|
||||
test(
|
||||
"#5166 user message with single text-part array is flattened to a plain string",
|
||||
async () => {
|
||||
const calls = captureFetch(
|
||||
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: {
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: [{ type: "text", text: "Hi there" }],
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
const posted = calls[0].body;
|
||||
const userMsg = (posted.params as Record<string, unknown[]>).messages[0] as Record<
|
||||
string,
|
||||
unknown
|
||||
>;
|
||||
assert.equal(typeof userMsg.content, "string");
|
||||
assert.equal(userMsg.content, "Hi there");
|
||||
}
|
||||
);
|
||||
|
||||
test(
|
||||
"#5166 user message with plain string content passes through unchanged (no regression)",
|
||||
async () => {
|
||||
const calls = captureFetch(
|
||||
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: {
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: "Plain string message",
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
const posted = calls[0].body;
|
||||
const userMsg = (posted.params as Record<string, unknown[]>).messages[0] as Record<
|
||||
string,
|
||||
unknown
|
||||
>;
|
||||
assert.equal(typeof userMsg.content, "string");
|
||||
assert.equal(userMsg.content, "Plain string message");
|
||||
}
|
||||
);
|
||||
|
||||
test(
|
||||
"#5166 user message with mixed parts (text + image_url) keeps only text parts",
|
||||
async () => {
|
||||
const calls = captureFetch(
|
||||
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: {
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: [
|
||||
{ type: "text", text: "Describe this:" },
|
||||
{ type: "image_url", image_url: { url: "https://example.com/img.png" } },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
const posted = calls[0].body;
|
||||
const userMsg = (posted.params as Record<string, unknown[]>).messages[0] as Record<
|
||||
string,
|
||||
unknown
|
||||
>;
|
||||
assert.equal(typeof userMsg.content, "string");
|
||||
// Only text parts extracted; image_url part is dropped (not a "text" type)
|
||||
assert.equal(userMsg.content, "Describe this:");
|
||||
}
|
||||
);
|
||||
test("#5166 user message with mixed parts (text + image_url) keeps all parts", async () => {
|
||||
const calls = captureFetch(okResponse());
|
||||
await getExecutor("command-code").execute({
|
||||
model: "deepseek/deepseek-v4-pro",
|
||||
stream: false,
|
||||
credentials: { apiKey: "cc_test_key" },
|
||||
body: {
|
||||
messages: [
|
||||
{
|
||||
role: "user",
|
||||
content: [
|
||||
{ type: "text", text: "Describe this:" },
|
||||
{ type: "image_url", image_url: { url: "https://example.com/img.png" } },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
const userMsg = (calls[0].body.messages as Record<string, unknown>[])[0];
|
||||
const parts = userMsg.content as Record<string, unknown>[];
|
||||
assert.equal(parts.length, 2, "text + image both preserved");
|
||||
assert.equal(parts[0].text, "Describe this:");
|
||||
assert.equal(parts[1].type, "image_url");
|
||||
});
|
||||
Reference in New Issue
Block a user