mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-07-31 04:12:10 +03:00
* perf: hoist Date.now, hoist hasActiveDeltaValue, avoid per-chunk buffer.split in SSE stream - Hoist to transform entry, remove 2 inner declarations that shadowed the outer one (stream.ts) - Hoist from inline closure to module-level function to avoid allocation per chunk (stream.ts) - Replace unconditional per chunk with -gated split to avoid allocating array when no embedded newline (stream.ts streamHelpers.ts) - Guard to avoid allocating when already at/above limit * fix: correct appendBoundedText slice offset when keep is zero * chore(ci): rebaseline stream.ts 2796->2801 for perf/p1-fixes Add _rebaseline_ entry documenting the +5 line growth from: -da7b1e2b2: hoist Date.now, hoist hasActiveDeltaValue, avoid per-chunk split -df89846cb: fix appendBoundedText slice offset when keep=0 These are irreducible optimizations at the stream dispatch chokepoint. * chore: trigger CI re-run * fix(#7066): bump stream.ts frozen baseline 2801->2802 (file grew +1 from parallel merges) * fix(ci): shrink stream.ts under the frozen file-size cap instead of rebaselining Two prior commits on this branch bumped config/quality/file-size-baseline.json (2796->2801->2802) to accommodate this PR's own +6 line growth in open-sse/utils/stream.ts, rather than fixing the cause. Per project policy the baseline gate is a ratchet — it may only shrink, never grow to paper over a regression. Reverted both bogus rebaseline edits (file-size-baseline.json now matches release/v3.8.49 exactly for this key) and instead extracted the two pure helper functions this PR added/touched (appendBoundedText, hasActiveDeltaValue) out of the god-file stream.ts into the existing sibling module streamHelpers.ts, mirroring the extraction precedent already documented in this baseline file. stream.ts now sits at 2778 lines (cap 2796); streamHelpers.ts grew to 487 lines, well under its own 800-line new-file cap. No behavior change — pure move. Verified via the existing tests/unit/streamHelpers.test.ts (20/20) and the full createSSEStream test set across 11 files (94/94), plus check:file-size, check:complexity, check:cognitive-complexity, check:test-discovery, check:any-budget:t11, and typecheck:core, all green. Co-authored-by: diegosouzapw <diegosouza.pw@gmail.com> * test(sse): cover appendBoundedText slice(-0) trap and hasActiveDeltaValue Addresses the gemini-code-assist review finding on appendBoundedText. The bug it describes (`keep === 0` -> `current.slice(-0)` returns the WHOLE string, so the function returns current + next and blows past STREAM_SUMMARY_TEXT_LIMIT) was real, and was already fixed on this branch bydf89846c— but with zero test coverage guarding it. Both helpers were unexported internals of stream.ts and therefore untestable; the extraction into streamHelpers.ts made them reachable, so this adds the regression tests that lock the behavior in. 15 new tests. Verified as genuine guards, not decoration: temporarily restoring the buggy `keep = LIMIT > next.length ? LIMIT - next.length : 0` form makes exactly the two boundary tests fail ("next is exactly the limit" and "next is larger than the limit"); restoring the fix makes all 35 pass. Covers: - appendBoundedText: empty next, normal concat, tail-keep at overflow, window slide at limit, next === limit (the slice(-0) trap), next > limit, and a 40-iteration loop asserting the bound never breaks. - hasActiveDeltaValue: strings, null/undefined, empty/populated arrays and objects, nested recursion, and the number/boolean cases (0 and false are meaningful values, not absence). Co-authored-by: diegosouzapw <diegosouza.pw@gmail.com> --------- Co-authored-by: oyi77 <oyi77@users.noreply.github.com> Co-authored-by: Diego Rodrigues de Sa e Souza <diegosouza.pw@gmail.com>
228 lines
8.9 KiB
TypeScript
228 lines
8.9 KiB
TypeScript
import { describe, it } from "node:test";
|
|
import assert from "node:assert";
|
|
import {
|
|
hasValuableContent,
|
|
unwrapGeminiChunk,
|
|
appendBoundedText,
|
|
hasActiveDeltaValue,
|
|
} from "../../open-sse/utils/streamHelpers.ts";
|
|
import { FORMATS } from "../../open-sse/translator/formats.ts";
|
|
|
|
describe("hasValuableContent", () => {
|
|
describe("OpenAI format", () => {
|
|
it("returns true for content with text", () => {
|
|
const chunk = { choices: [{ delta: { content: "Hello" } }] };
|
|
assert.strictEqual(hasValuableContent(chunk, FORMATS.OPENAI), true);
|
|
});
|
|
|
|
it("returns false for empty delta", () => {
|
|
const chunk = { choices: [{ delta: {} }] };
|
|
assert.strictEqual(hasValuableContent(chunk, FORMATS.OPENAI), false);
|
|
});
|
|
|
|
it("returns false for delta with empty string content", () => {
|
|
const chunk = { choices: [{ delta: { content: "" } }] };
|
|
assert.strictEqual(hasValuableContent(chunk, FORMATS.OPENAI), false);
|
|
});
|
|
|
|
it("returns true for reasoning_content", () => {
|
|
const chunk = { choices: [{ delta: { reasoning_content: "thinking" } }] };
|
|
assert.strictEqual(hasValuableContent(chunk, FORMATS.OPENAI), true);
|
|
});
|
|
|
|
it("returns true for client-readable reasoning", () => {
|
|
const chunk = { choices: [{ delta: { reasoning: "thinking" } }] };
|
|
assert.strictEqual(hasValuableContent(chunk, FORMATS.OPENAI), true);
|
|
});
|
|
|
|
it("returns true for Copilot reasoning_text", () => {
|
|
const chunk = { choices: [{ delta: { reasoning_text: "thinking" } }] };
|
|
assert.strictEqual(hasValuableContent(chunk, FORMATS.OPENAI), true);
|
|
});
|
|
|
|
it("returns true for OpenRouter reasoning_details", () => {
|
|
const chunk = {
|
|
choices: [{ delta: { reasoning_details: [{ type: "reasoning.text", text: "thinking" }] } }],
|
|
};
|
|
assert.strictEqual(hasValuableContent(chunk, FORMATS.OPENAI), true);
|
|
});
|
|
|
|
it("returns true for finish_reason", () => {
|
|
const chunk = { choices: [{ delta: {}, finish_reason: "stop" }] };
|
|
assert.strictEqual(hasValuableContent(chunk, FORMATS.OPENAI), true);
|
|
});
|
|
|
|
it("returns true for role delta", () => {
|
|
const chunk = { choices: [{ delta: { role: "assistant" } }] };
|
|
assert.strictEqual(hasValuableContent(chunk, FORMATS.OPENAI), true);
|
|
});
|
|
});
|
|
|
|
describe("Claude format", () => {
|
|
it("returns true for content_block_delta with text", () => {
|
|
const chunk = { type: "content_block_delta", delta: { text: "Hello" } };
|
|
assert.strictEqual(hasValuableContent(chunk, FORMATS.CLAUDE), true);
|
|
});
|
|
|
|
it("returns false for empty content_block_delta", () => {
|
|
const chunk = { type: "content_block_delta", delta: {} };
|
|
assert.strictEqual(hasValuableContent(chunk, FORMATS.CLAUDE), false);
|
|
});
|
|
|
|
it("returns true for thinking blocks", () => {
|
|
const chunk = { type: "content_block_delta", delta: { thinking: "reasoning" } };
|
|
assert.strictEqual(hasValuableContent(chunk, FORMATS.CLAUDE), true);
|
|
});
|
|
});
|
|
|
|
describe("Gemini format", () => {
|
|
it("returns true for content with text", () => {
|
|
const chunk = { candidates: [{ content: { parts: [{ text: "Hello" }] } }] };
|
|
assert.strictEqual(hasValuableContent(chunk, FORMATS.GEMINI), true);
|
|
});
|
|
|
|
it("returns false for empty parts", () => {
|
|
const chunk = { candidates: [{ content: { parts: [] } }] };
|
|
assert.strictEqual(hasValuableContent(chunk, FORMATS.GEMINI), false);
|
|
});
|
|
|
|
it("returns true for finishReason", () => {
|
|
const chunk = { candidates: [{ finishReason: "STOP" }] };
|
|
assert.strictEqual(hasValuableContent(chunk, FORMATS.GEMINI), true);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("unwrapGeminiChunk", () => {
|
|
it("returns chunk directly when candidates is at top level (standard Gemini)", () => {
|
|
const chunk = { candidates: [{ content: { parts: [{ text: "Hi" }] } }], usageMetadata: {} };
|
|
const result = unwrapGeminiChunk(chunk);
|
|
assert.strictEqual(result, chunk);
|
|
});
|
|
|
|
it("unwraps Cloud Code envelope { response: { candidates: [...] } }", () => {
|
|
const inner = { candidates: [{ content: { parts: [{ text: "Hello" }] } }] };
|
|
const chunk = { response: inner, modelVersion: "gemini-2.5-flash" };
|
|
const result = unwrapGeminiChunk(chunk);
|
|
assert.strictEqual(result, inner);
|
|
assert.deepEqual(result.candidates[0].content.parts[0].text, "Hello");
|
|
});
|
|
|
|
it("returns parsed directly when no candidates and no response", () => {
|
|
const chunk = { someOther: "data" };
|
|
const result = unwrapGeminiChunk(chunk);
|
|
assert.strictEqual(result, chunk);
|
|
});
|
|
|
|
it("returns parsed when response is null (falsy) — no valid envelope to unwrap", () => {
|
|
const chunk = { response: null, other: "data" };
|
|
const result = unwrapGeminiChunk(chunk);
|
|
assert.strictEqual(result, chunk);
|
|
});
|
|
|
|
it("prefers top-level candidates over response when both exist", () => {
|
|
const inner = { candidates: [{ content: { parts: [{ text: "inner" }] } }] };
|
|
const chunk = {
|
|
candidates: [{ content: { parts: [{ text: "outer" }] } }],
|
|
response: inner,
|
|
};
|
|
const result = unwrapGeminiChunk(chunk);
|
|
assert.strictEqual(result, chunk);
|
|
assert.equal(result.candidates[0].content.parts[0].text, "outer");
|
|
});
|
|
});
|
|
|
|
const STREAM_SUMMARY_TEXT_LIMIT = 64 * 1024;
|
|
|
|
describe("appendBoundedText", () => {
|
|
it("returns current unchanged when next is empty", () => {
|
|
assert.strictEqual(appendBoundedText("abc", ""), "abc");
|
|
});
|
|
|
|
it("concatenates normally while under the limit", () => {
|
|
assert.strictEqual(appendBoundedText("abc", "def"), "abcdef");
|
|
});
|
|
|
|
it("keeps the tail once the combined length exceeds the limit", () => {
|
|
const current = "a".repeat(STREAM_SUMMARY_TEXT_LIMIT - 1);
|
|
const result = appendBoundedText(current, "bb");
|
|
assert.strictEqual(result.length, STREAM_SUMMARY_TEXT_LIMIT);
|
|
assert.ok(result.endsWith("bb"), "must retain the newest text");
|
|
});
|
|
|
|
it("slides the window when current is already at the limit", () => {
|
|
const current = "a".repeat(STREAM_SUMMARY_TEXT_LIMIT);
|
|
const result = appendBoundedText(current, "xyz");
|
|
assert.strictEqual(result.length, STREAM_SUMMARY_TEXT_LIMIT);
|
|
assert.ok(result.endsWith("xyz"), "must retain the newest text");
|
|
});
|
|
|
|
// Regression: `keep` is 0 when next.length === LIMIT. `current.slice(-0)` is
|
|
// `slice(0)` — the WHOLE string — so a naive impl returns current + next and
|
|
// blows past the bound. Must return only the tail of next.
|
|
it("stays bounded when next is exactly the limit (slice(-0) trap)", () => {
|
|
const current = "a".repeat(STREAM_SUMMARY_TEXT_LIMIT);
|
|
const next = "b".repeat(STREAM_SUMMARY_TEXT_LIMIT);
|
|
const result = appendBoundedText(current, next);
|
|
assert.strictEqual(result.length, STREAM_SUMMARY_TEXT_LIMIT);
|
|
assert.strictEqual(result, next, "must be next only — no 'a' may survive");
|
|
assert.ok(!result.includes("a"), "must not leak the old buffer");
|
|
});
|
|
|
|
it("stays bounded when next is larger than the limit", () => {
|
|
const current = "a".repeat(STREAM_SUMMARY_TEXT_LIMIT);
|
|
const next = "b".repeat(STREAM_SUMMARY_TEXT_LIMIT + 500);
|
|
const result = appendBoundedText(current, next);
|
|
assert.strictEqual(result.length, STREAM_SUMMARY_TEXT_LIMIT);
|
|
assert.ok(!result.includes("a"), "must not leak the old buffer");
|
|
});
|
|
|
|
it("never exceeds the limit across repeated appends", () => {
|
|
let acc = "";
|
|
for (let i = 0; i < 40; i++) {
|
|
acc = appendBoundedText(acc, "z".repeat(4096));
|
|
assert.ok(acc.length <= STREAM_SUMMARY_TEXT_LIMIT, `overflow at iteration ${i}`);
|
|
}
|
|
assert.strictEqual(acc.length, STREAM_SUMMARY_TEXT_LIMIT);
|
|
});
|
|
});
|
|
|
|
describe("hasActiveDeltaValue", () => {
|
|
it("returns true for a non-empty string", () => {
|
|
assert.strictEqual(hasActiveDeltaValue("hi"), true);
|
|
});
|
|
|
|
it("returns false for an empty string", () => {
|
|
assert.strictEqual(hasActiveDeltaValue(""), false);
|
|
});
|
|
|
|
it("returns false for null and undefined", () => {
|
|
assert.strictEqual(hasActiveDeltaValue(null), false);
|
|
assert.strictEqual(hasActiveDeltaValue(undefined), false);
|
|
});
|
|
|
|
it("returns false for an empty array and an array of empty strings", () => {
|
|
assert.strictEqual(hasActiveDeltaValue([]), false);
|
|
assert.strictEqual(hasActiveDeltaValue(["", ""]), false);
|
|
});
|
|
|
|
it("returns true when any array entry is meaningful", () => {
|
|
assert.strictEqual(hasActiveDeltaValue(["", "x"]), true);
|
|
});
|
|
|
|
it("returns false for an empty object and an object of empty values", () => {
|
|
assert.strictEqual(hasActiveDeltaValue({}), false);
|
|
assert.strictEqual(hasActiveDeltaValue({ a: "", b: null }), false);
|
|
});
|
|
|
|
it("recurses into nested structures", () => {
|
|
assert.strictEqual(hasActiveDeltaValue({ a: { b: [{ c: "" }] } }), false);
|
|
assert.strictEqual(hasActiveDeltaValue({ a: { b: [{ c: "found" }] } }), true);
|
|
});
|
|
|
|
it("treats numbers and booleans as meaningful", () => {
|
|
assert.strictEqual(hasActiveDeltaValue(0), true);
|
|
assert.strictEqual(hasActiveDeltaValue(false), true);
|
|
});
|
|
});
|