Files
OmniRoute/tests/unit/stream-numeric-ids.test.ts
Diego Rodrigues de Sa e Souza 8169b97d84 Release v3.8.18 (#3482)
* chore(release): open v3.8.18 development cycle

* fix(catalog): stop Codex CLI model-catalog refresh from erroring (#3481)

Codex's model-catalog refresh (codex_models_manager) does
GET /v1/models?client_version=<v> and decodes a JSON object with a
TOP-LEVEL `models` array. OmniRoute answers in the OpenAI-standard
`{object,data}` shape, so codex fails with "missing field `models`"
and logs "failed to refresh available models" on every startup.

Detect codex clients via the `originator` / `user-agent` = `codex_*`
headers they send and add an EMPTY top-level `models: []` so the decode
succeeds. Non-codex OpenAI clients keep the byte-identical `{object,data}`
response.

The array is intentionally empty: codex replaces its built-in per-model
agent prompt (`base_instructions`, ~21k chars) with whatever a populated
entry carries for the selected model, so emitting our catalog would drop
the agent prompt to nothing and break codex's agent behaviour (verified
empirically against codex 0.137). An empty list keeps codex on its
built-in model info — same inference as before, minus the error.

Validated end-to-end with the real handler against codex 0.137:
"failed to refresh available models" → 0 occurrences, instructions
preserved (built-in Codex agent prompt, not empty).

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* chore: ignore quality reports and local prompt artifacts

Add generated quality gate reports, metrics files, and local setup prompt
artifacts to .gitignore to prevent committing environment-specific or
temporary files.

* fix(provider): detect Responses API format when body has `input` but … (#3490)

Integrated into release/v3.8.18

* fix(sse): normalize numeric provider ids to strings (#3451)

Integrated into release/v3.8.18

* feat(browserPool): resolve Playwright proxy from proxy_registry DB (#3492)

Integrated into release/v3.8.18

* fix(theoldllm): generate X-Request-Token server-side, drop Playwright (#3491)

Integrated into release/v3.8.18

* feat(plugins): add lifecycle hooks and theme-manager plugin (#3473)

Integrated into release/v3.8.18

* fix(combo): parallel pre-screen + circuit-breaker fast-exit for priority combos (#3169)

Integrated into release/v3.8.18

* feat(ui): unifi active and finished requests into single view #1422 (#3401)

Integrated into release/v3.8.18

* docs(changelog): record #3401, #3473, #3492, #3490, #3451, #3491, #3169 under v3.8.18

* feat(docs): add doc accuracy gate + refresh AGENTS.md counts (#3510)

Integrated into release/v3.8.18

* fix(sse): drop empty-choices chunks without usage instead of injecting retry text (#3513)

PR #3422 ('allow OpenAI usage-only empty choices chunks') reintroduced the
assistant-content injection '[OmniRoute] Upstream returned an empty response.
Please retry.' for empty `choices: []` chunks that carry no valid usage. Clients
(Goose/opencode) feed that text back as a turn and spin in a retry loop -- the
exact regression #3400 had fixed by dropping the chunk.

Restore the drop behavior for the no-usage case while preserving #3422's
standards-compliant forwarding of usage-only `include_usage` final chunks.
Realign the mislabeled stream-utils test (it asserted the injection) and add a
dedicated regression guard.

Reported-by: @mochizzan
Refs: #3502, #3388, #3400, #3422

* fix(authz): fall back to URL token when Authorization isn't a usable Bearer (#3504)

Integrated into release/v3.8.18

* fix(playground): authenticate via session, test key policy by id (#3503)

Integrated into release/v3.8.18

* docs(changelog): record #3510, #3504, #3503 under v3.8.18

* fix: llama base url normalization (#3519)

* docs(changelog): reconcile v3.8.18 — add #3519, #3513, #3435-repair, gitignore chore (full commit↔changelog coverage)

* fix(opencode-plugin): bound regex quantifiers in normaliseFreeLabel (polynomial-ReDoS)

CodeQL js/polynomial-redos: unbounded \s* before an anchored \s*$ allowed
O(n²) backtracking on attacker-influenced display names. Bounded to {0,8}/{1,8}
(ample for any real label spacing). Plugin builds + 254 tests green.

* fix(types): restore clean typecheck:core for v3.8.18 release gate

- getPendingRequests() typed to real shape (was widened to object) → fixes
  unknown 'count' in the unified-requests view (#3401)
- streamChunks log payload cast to its declared type (callLogs.ts)
- preScreenTargets aligned to canonical IsModelAvailable signature (#3169),
  Promise.resolve-normalized so .catch never hits a bare boolean

All 5 gates green: lint(0 err) + typecheck:core + cycles + docs-all + unit + vitest(146).

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: Andrey Borodulin <borodulin@gmail.com>
Co-authored-by: Dmitrii Safronov <zimniy@cyberbrain.cc>
Co-authored-by: Paijo <14921983+oyi77@users.noreply.github.com>
Co-authored-by: PizzaV <103120356+pizzav-xyz@users.noreply.github.com>
Co-authored-by: Markus Hartung <mail@hartmark.se>
Co-authored-by: Felipe Almeman <4226997+zhiru@users.noreply.github.com>
2026-06-09 15:56:24 -03:00

505 lines
15 KiB
TypeScript

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-stream-numeric-ids-"));
process.env.DATA_DIR = TEST_DATA_DIR;
const core = await import("../../src/lib/db/core.ts");
const { createSSEStream } = await import("../../open-sse/utils/stream.ts");
const { FORMATS } = await import("../../open-sse/translator/formats.ts");
const textEncoder = new TextEncoder();
async function readTransformed(chunks, options) {
const source = new ReadableStream({
start(controller) {
for (const chunk of chunks) {
controller.enqueue(textEncoder.encode(chunk));
}
controller.close();
},
});
return new Response(source.pipeThrough(createSSEStream(options))).text();
}
test.after(() => {
core.resetDbInstance();
if (fs.existsSync(TEST_DATA_DIR)) {
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
}
});
test("createSSEStream passthrough coerces numeric tool_call id to string", async () => {
let onCompletePayload = null;
const text = await readTransformed(
[
`data: ${JSON.stringify({
id: "chatcmpl_num",
object: "chat.completion.chunk",
created: 1,
model: "gpt-4.1-mini",
choices: [{ index: 0, delta: { role: "assistant", content: "Hello " } }],
})}\n\n`,
`data: ${JSON.stringify({
id: "chatcmpl_num",
object: "chat.completion.chunk",
created: 1,
model: "gpt-4.1-mini",
choices: [
{
index: 0,
delta: {
tool_calls: [
{
index: 0,
id: 12345,
type: "function",
function: { name: "read_file", arguments: '{"path":"/tmp/a"}' },
},
],
},
},
],
})}\n\n`,
`data: ${JSON.stringify({
id: "chatcmpl_num",
object: "chat.completion.chunk",
created: 1,
model: "gpt-4.1-mini",
choices: [{ index: 0, delta: {}, finish_reason: "stop" }],
})}\n\n`,
],
{
mode: "passthrough",
sourceFormat: FORMATS.OPENAI,
provider: "openai",
model: "gpt-4.1-mini",
body: { messages: [{ role: "user", content: "hello" }] },
onComplete(payload) {
onCompletePayload = payload;
},
}
);
const lines = text
.trim()
.split("\n")
.filter((line) => line.startsWith("data: ") && !line.includes("[DONE]"));
let sawStreamedToolCall = false;
for (const line of lines) {
const payload = JSON.parse(line.slice(6));
const tc = payload?.choices?.[0]?.delta?.tool_calls?.[0];
if (tc?.id) {
assert.equal(typeof tc.id, "string", "tool_call.id should be a string");
assert.equal(tc.id, "12345");
sawStreamedToolCall = true;
}
}
assert.equal(sawStreamedToolCall, true, "expected streamed tool_call.id to be present");
const finalId = onCompletePayload?.responseBody?.choices?.[0]?.message?.tool_calls?.[0]?.id;
assert.equal(typeof finalId, "string", "tool_call.id in final message should be a string");
assert.equal(finalId, "12345");
});
test("createSSEStream passthrough preserves numeric top-level id as string", async () => {
const text = await readTransformed(
[
`data: ${JSON.stringify({
id: 123,
object: "chat.completion.chunk",
created: 1,
model: "gpt-4.1-mini",
choices: [{ index: 0, delta: { content: "Hello" } }],
})}\n\n`,
`data: ${JSON.stringify({
id: 123,
object: "chat.completion.chunk",
created: 1,
model: "gpt-4.1-mini",
choices: [{ index: 0, delta: {}, finish_reason: "stop" }],
})}\n\n`,
],
{
mode: "passthrough",
sourceFormat: FORMATS.OPENAI,
provider: "openai",
model: "gpt-4.1-mini",
body: { messages: [{ role: "user", content: "hello" }] },
}
);
const lines = text
.trim()
.split("\n")
.filter((line) => line.startsWith("data: ") && !line.includes("[DONE]"));
assert.ok(lines.length > 0, "expected streamed chunks");
for (const line of lines) {
const payload = JSON.parse(line.slice(6));
assert.equal(typeof payload.id, "string", "top-level chunk id should be a string");
assert.equal(payload.id, "123");
assert.notEqual(payload.id.startsWith("chatcmpl-"), true);
}
});
test("createSSEStream responses passthrough coerces numeric ids to strings", async () => {
const text = await readTransformed(
[
`data: ${JSON.stringify({
type: "response.created",
response: {
id: 987,
model: "gpt-4.1-mini",
status: "in_progress",
output: [],
},
})}\n\n`,
`data: ${JSON.stringify({
type: "response.output_item.added",
response_id: 987,
output_index: 0,
item: {
id: 456,
call_id: 654,
type: "function_call",
status: "in_progress",
name: "lookup",
arguments: "",
},
})}\n\n`,
`data: ${JSON.stringify({
type: "response.function_call_arguments.delta",
response_id: 987,
item_id: 456,
output_index: 0,
delta: '{"q":',
})}\n\n`,
`data: ${JSON.stringify({
type: "response.function_call_arguments.done",
response_id: 987,
item_id: 456,
output_index: 0,
arguments: '{"q":1}',
})}\n\n`,
`data: ${JSON.stringify({
type: "response.completed",
response: {
id: 987,
model: "gpt-4.1-mini",
status: "completed",
output: [
{
id: 456,
call_id: 654,
type: "function_call",
status: "completed",
name: "lookup",
arguments: '{"q":1}',
},
],
},
})}\n\n`,
],
{
mode: "passthrough",
sourceFormat: FORMATS.OPENAI,
provider: "openai",
model: "gpt-4.1-mini",
body: { input: "hello" },
}
);
const payloads = text
.trim()
.split("\n")
.filter((line) => line.startsWith("data: ") && !line.includes("[DONE]"))
.map((line) => JSON.parse(line.slice(6)));
const created = payloads.find((payload) => payload.type === "response.created");
assert.equal(typeof created.response.id, "string");
assert.equal(created.response.id, "987");
const added = payloads.find((payload) => payload.type === "response.output_item.added");
assert.equal(typeof added.response_id, "string");
assert.equal(added.response_id, "987");
assert.equal(typeof added.item.id, "string");
assert.equal(added.item.id, "456");
assert.equal(typeof added.item.call_id, "string");
assert.equal(added.item.call_id, "654");
const delta = payloads.find((payload) => payload.type === "response.function_call_arguments.delta");
assert.equal(typeof delta.response_id, "string");
assert.equal(delta.response_id, "987");
assert.equal(typeof delta.item_id, "string");
assert.equal(delta.item_id, "456");
const completed = payloads.find((payload) => payload.type === "response.completed");
assert.equal(typeof completed.response.id, "string");
assert.equal(completed.response.id, "987");
assert.equal(typeof completed.response.output[0].id, "string");
assert.equal(completed.response.output[0].id, "456");
assert.equal(typeof completed.response.output[0].call_id, "string");
assert.equal(completed.response.output[0].call_id, "654");
});
test("createSSEStream responses passthrough does not normalize unrelated top-level id", async () => {
const text = await readTransformed(
[
`data: ${JSON.stringify({
type: "response.output_text.delta",
id: 1,
response_id: 987,
item_id: 456,
delta: "x",
})}\n\n`,
],
{
mode: "passthrough",
sourceFormat: FORMATS.OPENAI,
provider: "openai",
model: "gpt-4.1-mini",
body: { input: "hello" },
}
);
const payloads = text
.trim()
.split("\n")
.filter((line) => line.startsWith("data: ") && !line.includes("[DONE]"))
.map((line) => JSON.parse(line.slice(6)));
const delta = payloads.find((p) => p.type === "response.output_text.delta");
assert.equal(typeof delta.id, "number", "unrelated top-level id should stay numeric");
assert.equal(delta.id, 1);
assert.equal(typeof delta.response_id, "string");
assert.equal(delta.response_id, "987");
assert.equal(typeof delta.item_id, "string");
assert.equal(delta.item_id, "456");
});
test("createSSEStream passthrough normalizes numeric id in final chunk without trailing newline", async () => {
const text = await readTransformed(
[
`data: ${JSON.stringify({
id: 123,
object: "chat.completion.chunk",
created: 1,
model: "gpt-4.1-mini",
choices: [{ index: 0, delta: { content: "Hello" } }],
})}\n\n`,
`data: ${JSON.stringify({
id: 123,
object: "chat.completion.chunk",
created: 1,
model: "gpt-4.1-mini",
choices: [{ index: 0, delta: {}, finish_reason: "stop" }],
})}`,
],
{
mode: "passthrough",
sourceFormat: FORMATS.OPENAI,
provider: "openai",
model: "gpt-4.1-mini",
body: { messages: [{ role: "user", content: "hello" }] },
}
);
const lines = text
.trim()
.split("\n")
.filter((line) => line.startsWith("data: ") && !line.includes("[DONE]"));
for (const line of lines) {
const payload = JSON.parse(line.slice(6));
if (payload.id) {
assert.equal(typeof payload.id, "string", "top-level chunk id should be a string");
assert.equal(payload.id, "123");
}
}
});
test("createSSEStream responses passthrough normalizes numeric ids in final chunk without trailing newline", async () => {
const text = await readTransformed(
[
`data: ${JSON.stringify({
type: "response.created",
response: { id: 987, model: "gpt-4.1-mini", status: "in_progress", output: [] },
})}\n\n`,
`data: ${JSON.stringify({
type: "response.completed",
response: {
id: 987,
model: "gpt-4.1-mini",
status: "completed",
output: [
{
id: 456,
call_id: 654,
type: "function_call",
status: "completed",
name: "lookup",
arguments: "{}",
},
],
},
})}`,
],
{
mode: "passthrough",
sourceFormat: FORMATS.OPENAI,
provider: "openai",
model: "gpt-4.1-mini",
body: { input: "hello" },
}
);
const payloads = text
.trim()
.split("\n")
.filter((line) => line.startsWith("data: ") && !line.includes("[DONE]"))
.map((line) => JSON.parse(line.slice(6)));
const completed = payloads.find((p) => p.type === "response.completed");
assert.equal(typeof completed.response.id, "string");
assert.equal(completed.response.id, "987");
assert.equal(typeof completed.response.output[0].id, "string");
assert.equal(completed.response.output[0].id, "456");
assert.equal(typeof completed.response.output[0].call_id, "string");
assert.equal(completed.response.output[0].call_id, "654");
});
test("createSSEStream passthrough coerces tool_call id 0 without index", async () => {
let onCompletePayload = null;
const text = await readTransformed(
[
`data: ${JSON.stringify({
id: "cmpl_0",
object: "chat.completion.chunk",
created: 1,
model: "gpt-4.1-mini",
choices: [{ index: 0, delta: { role: "assistant", content: "" } }],
})}\n\n`,
`data: ${JSON.stringify({
id: "cmpl_0",
object: "chat.completion.chunk",
created: 1,
model: "gpt-4.1-mini",
choices: [
{
index: 0,
delta: {
tool_calls: [
{
id: 0,
type: "function",
function: { name: "zero", arguments: '{"path":"/tmp"}' },
},
],
},
},
],
})}\n\n`,
`data: ${JSON.stringify({
id: "cmpl_0",
object: "chat.completion.chunk",
created: 1,
model: "gpt-4.1-mini",
choices: [{ index: 0, delta: {}, finish_reason: "stop" }],
})}\n\n`,
],
{
mode: "passthrough",
sourceFormat: FORMATS.OPENAI,
provider: "openai",
model: "gpt-4.1-mini",
body: { messages: [{ role: "user", content: "hello" }] },
onComplete(payload) {
onCompletePayload = payload;
},
}
);
const lines = text
.trim()
.split("\n")
.filter((line) => line.startsWith("data: ") && !line.includes("[DONE]"));
let sawStreamedToolCall = false;
for (const line of lines) {
const payload = JSON.parse(line.slice(6));
const tc = payload?.choices?.[0]?.delta?.tool_calls?.[0];
if (tc?.id != null) {
assert.equal(typeof tc.id, "string", "tool_call.id should be a string");
assert.equal(tc.id, "0");
sawStreamedToolCall = true;
}
}
assert.equal(sawStreamedToolCall, true, "expected streamed tool_call.id to be present");
const finalId = onCompletePayload?.responseBody?.choices?.[0]?.message?.tool_calls?.[0]?.id;
assert.equal(typeof finalId, "string", "tool_call.id in final message should be a string");
assert.equal(finalId, "0");
});
test("createSSEStream Claude passthrough does not normalize numeric ids", async () => {
const text = await readTransformed(
[
`data: ${JSON.stringify({
type: "message_start",
message: {
id: 987,
type: "message",
role: "assistant",
content: [],
model: "claude-3-5-sonnet",
},
})}\n\n`,
`data: ${JSON.stringify({
type: "content_block_start",
index: 0,
content_block: {
id: 456,
type: "text",
text: "",
},
})}\n\n`,
`data: ${JSON.stringify({
type: "content_block_delta",
index: 0,
delta: {
type: "text_delta",
text: "Hello",
},
})}\n\n`,
`data: ${JSON.stringify({
type: "message_stop",
})}\n`,
],
{
mode: "passthrough",
sourceFormat: FORMATS.ANTHROPIC,
provider: "anthropic",
model: "claude-3-5-sonnet-20241022",
body: { messages: [{ role: "user", content: "hello" }] },
}
);
const payloads = text
.trim()
.split("\n")
.filter((line) => line.startsWith("data: ") && !line.includes("[DONE]"))
.map((line) => JSON.parse(line.slice(6)));
const start = payloads.find((p) => p.type === "message_start");
assert.equal(start.message.id, 987, "Claude message id should remain numeric");
assert.equal(typeof start.message.id, "number");
const contentStart = payloads.find((p) => p.type === "content_block_start");
assert.equal(contentStart.content_block.id, 456, "Claude content block id should remain numeric");
assert.equal(typeof contentStart.content_block.id, "number");
});