mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-03 13:52:09 +03:00
* docs(compression): design spec for the unified compression config panel
Engine-centric central panel (single source for master + per-engine on/off + level),
default pipeline derived from the engines map; Combos page owns named ordered pipelines +
active-profile selection; per-engine pages keep only detailed config. Phased: (1) core
consolidation + Model A migration, (2) named profiles + active selector, (3) per-request
x-omniroute-compression header.
* docs(compression): Phase 1 implementation plan for the unified config panel
12 TDD tasks: engine catalog, engines map + activeComboId, deriveDefaultPlan,
migration 102 + backfill, resolveCompressionPlan, runtime wiring, API, default-combo
shim, the engine-grid panel, consolidation, menu. Ends with a recap + the pending
follow-ups (Phase 2 named profiles/active selector, Phase 3 header, deferred items).
* feat(compression): engine catalog metadata (levels, single-mode, order)
* feat(compression): add engines map + activeComboId to CompressionConfig
* feat(compression): deriveDefaultPlan (engines map -> mode/pipeline)
Pure function that converts a per-engine EngineToggle map + masterEnabled
flag into a { mode, stackedPipeline } plan: off when master is off or no
engines on; single-mode when exactly one single-mode engine is enabled;
stacked (sorted by stackPriority) otherwise.
* feat(compression): persist+backfill engines map and activeComboId (migration 102)
* feat(compression): resolveCompressionPlan precedence resolver (header>override>active>default)
* feat(compression): selectCompressionStrategy uses resolveCompressionPlan
* feat(api): settings/compression carries engines map + activeComboId
Extends compressionSettingsUpdateSchema with engines (Record<string,{enabled:boolean,level?:string}>)
and activeComboId (string|null) so the PUT route accepts and persists these fields.
GET already returns the full getCompressionSettings() object which includes both fields.
TDD: added route round-trip tests (PUT+GET) for engines, activeComboId, null clear,
and schema rejection of invalid engines shape.
* refactor(compression): default-combo route is a read-only shim (default derived from engines)
* feat(dashboard): engine-grid compression panel (single source for on/off + level)
* refactor(dashboard): remove duplicate compression toggles; per-engine pages keep only detailed config
* feat(compression): unified panel menu order + derived-pipeline integration coverage
* fix(compression): import ENGINE_IDS via types re-export so it resolves under vitest
The bare "@omniroute/open-sse/.../engineCatalog.ts" specifier resolves under tsc/tsx
but not under vitest's MCP config: Vite externalizes a brand-new open-sse module to
Node, which can't load the .ts subpath. types.ts is already in Vite's graph, so route
ENGINE_IDS through its re-export. Fixes 3 failing MCP vitest suites (cacheTools,
dbHealthTool, essentialTools).
* fix(compression): engines map drives dispatch only when explicitly panel-saved
Code-review finding: the legacy seeded default combo (present on every install via
migration 042/043) was silently overriding a panel-configured engines map — the
default-combo block in chatCore set compressionComboApplied=true, skipping the
engines-map override, so an operator's panel toggles were ignored.
Gate the engines-map path on a new runtime-only CompressionConfig.enginesExplicit
(true when a stored engines row exists). Panel-saved installs: the engines map is
authoritative (deriveDefaultPlanFromConfig + new enginesMapDerivesStackedPipeline
guard the chatCore default-combo block). Legacy/backfilled installs: the map is
display-only and dispatch stays on the historical defaultMode/default-combo path —
zero behaviour change until the operator opts in via the panel.
Also documents why resolveCacheAwareConfig's getCacheAwareStrategy(config.defaultMode)
arg is safe (skipSystemPrompt is mode-independent).
* docs(compression): add MDX frontmatter + escape inline brace to fix fumadocs build
docs/compression/**/*.md is compiled as MDX by the Next build (fumadocs,
source.config.ts). The two planning docs lacked the required `title`
frontmatter (build error: 'title: expected string, received undefined') and the
design doc had a bare {rtk,caveman} that MDX parsed as a JSX expression. Adds
frontmatter matching the sibling docs and backticks the brace. Fixes the
dast-smoke build step.
307 lines
10 KiB
TypeScript
307 lines
10 KiB
TypeScript
import { describe, it } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import {
|
|
selectCompressionStrategy,
|
|
selectCompressionPlan,
|
|
enginesMapDerivesStackedPipeline,
|
|
getEffectiveMode,
|
|
applyCompression,
|
|
checkComboOverride,
|
|
shouldAutoTrigger,
|
|
} from "../../../open-sse/services/compression/strategySelector.ts";
|
|
import {
|
|
DEFAULT_COMPRESSION_CONFIG,
|
|
type CompressionConfig,
|
|
} from "../../../open-sse/services/compression/types.ts";
|
|
|
|
const baseConfig: CompressionConfig = {
|
|
enabled: true,
|
|
defaultMode: "lite",
|
|
autoTriggerTokens: 0,
|
|
cacheMinutes: 5,
|
|
preserveSystemPrompt: true,
|
|
comboOverrides: {},
|
|
};
|
|
|
|
/**
|
|
* Builds a PANEL-CONFIGURED config whose only enabled engines are the ones named (all others
|
|
* off). `enginesExplicit: true` models a stored engines row (the operator used the panel), so
|
|
* the engines map drives dispatch. Pass `enginesExplicit: false` via overrides to model a
|
|
* legacy/backfilled install where dispatch falls back to defaultMode.
|
|
*/
|
|
function engineConfig(
|
|
engines: CompressionConfig["engines"],
|
|
overrides: Partial<CompressionConfig> = {}
|
|
): CompressionConfig {
|
|
return {
|
|
...DEFAULT_COMPRESSION_CONFIG,
|
|
enabled: true,
|
|
engines,
|
|
enginesExplicit: true,
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
describe("checkComboOverride", () => {
|
|
it("returns null when comboId is null", () => {
|
|
assert.equal(checkComboOverride(baseConfig, null), null);
|
|
});
|
|
|
|
it("returns null when comboOverrides is empty", () => {
|
|
assert.equal(checkComboOverride(baseConfig, "my-combo"), null);
|
|
});
|
|
|
|
it("returns mode when combo override exists", () => {
|
|
const config = { ...baseConfig, comboOverrides: { "my-combo": "off" as const } };
|
|
assert.equal(checkComboOverride(config, "my-combo"), "off");
|
|
});
|
|
|
|
it("returns null for non-existent combo", () => {
|
|
const config = { ...baseConfig, comboOverrides: { "other-combo": "lite" as const } };
|
|
assert.equal(checkComboOverride(config, "my-combo"), null);
|
|
});
|
|
});
|
|
|
|
describe("shouldAutoTrigger", () => {
|
|
it("returns false when autoTriggerTokens is 0", () => {
|
|
assert.equal(shouldAutoTrigger(baseConfig, 5000), false);
|
|
});
|
|
|
|
it("returns false when tokens below threshold", () => {
|
|
const config = { ...baseConfig, autoTriggerTokens: 1000 };
|
|
assert.equal(shouldAutoTrigger(config, 500), false);
|
|
});
|
|
|
|
it("returns true when tokens at threshold", () => {
|
|
const config = { ...baseConfig, autoTriggerTokens: 1000 };
|
|
assert.equal(shouldAutoTrigger(config, 1000), true);
|
|
});
|
|
|
|
it("returns true when tokens above threshold", () => {
|
|
const config = { ...baseConfig, autoTriggerTokens: 1000 };
|
|
assert.equal(shouldAutoTrigger(config, 1500), true);
|
|
});
|
|
});
|
|
|
|
describe("getEffectiveMode", () => {
|
|
it("returns off when not enabled", () => {
|
|
const config = { ...baseConfig, enabled: false };
|
|
assert.equal(getEffectiveMode(config, null, 100), "off");
|
|
});
|
|
|
|
it("keeps disabled config off despite combo override and auto-trigger", () => {
|
|
const config = {
|
|
...baseConfig,
|
|
enabled: false,
|
|
autoTriggerTokens: 100,
|
|
comboOverrides: { "my-combo": "lite" as const },
|
|
};
|
|
|
|
assert.equal(getEffectiveMode(config, "my-combo", 500), "off");
|
|
});
|
|
|
|
it("returns default mode when no overrides", () => {
|
|
assert.equal(getEffectiveMode(baseConfig, null, 100), "lite");
|
|
});
|
|
|
|
it("returns combo override mode when present", () => {
|
|
const config = {
|
|
...baseConfig,
|
|
defaultMode: "off" as const,
|
|
comboOverrides: { "my-combo": "lite" as const },
|
|
};
|
|
assert.equal(getEffectiveMode(config, "my-combo", 100), "lite");
|
|
});
|
|
|
|
it("returns lite when auto-trigger threshold reached", () => {
|
|
const config = { ...baseConfig, defaultMode: "off" as const, autoTriggerTokens: 1000 };
|
|
assert.equal(getEffectiveMode(config, null, 1500), "lite");
|
|
});
|
|
|
|
it("combo override takes precedence over auto-trigger", () => {
|
|
const config = {
|
|
...baseConfig,
|
|
defaultMode: "off" as const,
|
|
autoTriggerTokens: 100,
|
|
comboOverrides: { "my-combo": "off" as const },
|
|
};
|
|
assert.equal(getEffectiveMode(config, "my-combo", 500), "off");
|
|
});
|
|
});
|
|
|
|
describe("selectCompressionStrategy resolves via the engines map (Task 7)", () => {
|
|
it("resolves mode rtk when only rtk is enabled in the engines map", () => {
|
|
const config = engineConfig({ rtk: { enabled: true } });
|
|
assert.equal(selectCompressionStrategy(config, null, 0), "rtk");
|
|
});
|
|
|
|
it("resolves mode stacked when rtk + caveman are both enabled, exposing the derived pipeline", () => {
|
|
const config = engineConfig({
|
|
rtk: { enabled: true },
|
|
caveman: { enabled: true, level: "full" },
|
|
});
|
|
assert.equal(selectCompressionStrategy(config, null, 0), "stacked");
|
|
const plan = selectCompressionPlan(config, null, 0);
|
|
assert.equal(plan.mode, "stacked");
|
|
// stackPriority order: rtk (10) before caveman (20).
|
|
assert.deepEqual(plan.stackedPipeline, [
|
|
{ engine: "rtk" },
|
|
{ engine: "caveman", intensity: "full" },
|
|
]);
|
|
});
|
|
|
|
it("auto-trigger still overrides the derived default", () => {
|
|
const config = engineConfig(
|
|
{ rtk: { enabled: true } },
|
|
{ autoTriggerTokens: 1000, autoTriggerMode: "aggressive" }
|
|
);
|
|
// Below threshold: derived default (rtk) wins.
|
|
assert.equal(selectCompressionStrategy(config, null, 500), "rtk");
|
|
// At/above threshold: auto-trigger mode wins.
|
|
assert.equal(selectCompressionStrategy(config, null, 1500), "aggressive");
|
|
});
|
|
|
|
it("routing-combo override still wins over the derived default", () => {
|
|
const config = engineConfig(
|
|
{ rtk: { enabled: true } },
|
|
{ comboOverrides: { "my-combo": "off" } }
|
|
);
|
|
assert.equal(selectCompressionStrategy(config, "my-combo", 0), "off");
|
|
});
|
|
});
|
|
|
|
describe("engines map drives dispatch ONLY when explicit (zero behaviour change for legacy)", () => {
|
|
it("legacy install (enginesExplicit false) ignores the backfilled engines map, uses defaultMode", () => {
|
|
// A backfilled map with rtk+caveman would derive "stacked", but a legacy install must keep
|
|
// its historical defaultMode until the operator saves via the panel.
|
|
const legacy: CompressionConfig = {
|
|
...DEFAULT_COMPRESSION_CONFIG,
|
|
enabled: true,
|
|
defaultMode: "lite",
|
|
engines: { rtk: { enabled: true }, caveman: { enabled: true, level: "full" } },
|
|
enginesExplicit: false,
|
|
};
|
|
assert.equal(selectCompressionStrategy(legacy, null, 0), "lite");
|
|
});
|
|
|
|
it("explicit install (enginesExplicit true) uses the engines map over defaultMode", () => {
|
|
const explicit = engineConfig(
|
|
{ rtk: { enabled: true }, caveman: { enabled: true, level: "full" } },
|
|
{ defaultMode: "lite" }
|
|
);
|
|
assert.equal(selectCompressionStrategy(explicit, null, 0), "stacked");
|
|
});
|
|
});
|
|
|
|
describe("enginesMapDerivesStackedPipeline", () => {
|
|
it("true only for an explicit multi-engine stacked map", () => {
|
|
assert.equal(
|
|
enginesMapDerivesStackedPipeline(
|
|
engineConfig({ rtk: { enabled: true }, caveman: { enabled: true, level: "full" } })
|
|
),
|
|
true
|
|
);
|
|
});
|
|
it("false for a single-mode explicit map (not stacked)", () => {
|
|
assert.equal(enginesMapDerivesStackedPipeline(engineConfig({ rtk: { enabled: true } })), false);
|
|
});
|
|
it("false for an empty/all-off explicit map", () => {
|
|
assert.equal(enginesMapDerivesStackedPipeline(engineConfig({})), false);
|
|
});
|
|
it("false for a legacy (non-explicit) install even when the backfilled map is stacked", () => {
|
|
const legacy: CompressionConfig = {
|
|
...DEFAULT_COMPRESSION_CONFIG,
|
|
enabled: true,
|
|
engines: { rtk: { enabled: true }, caveman: { enabled: true, level: "full" } },
|
|
enginesExplicit: false,
|
|
};
|
|
assert.equal(enginesMapDerivesStackedPipeline(legacy), false);
|
|
});
|
|
});
|
|
|
|
describe("selectCompressionStrategy", () => {
|
|
it("returns effective mode", () => {
|
|
assert.equal(selectCompressionStrategy(baseConfig, null, 100), "lite");
|
|
});
|
|
|
|
it("downgrades aggressive cache-control requests for caching-aware providers", () => {
|
|
const config = { ...baseConfig, defaultMode: "aggressive" as const };
|
|
const body = {
|
|
messages: [
|
|
{
|
|
role: "user",
|
|
content: [{ type: "text", text: "cached", cache_control: { type: "ephemeral" } }],
|
|
},
|
|
],
|
|
};
|
|
|
|
assert.equal(
|
|
selectCompressionStrategy(config, null, 100, body, {
|
|
provider: "anthropic",
|
|
targetFormat: "claude",
|
|
model: "claude-3-5-sonnet",
|
|
}),
|
|
"standard"
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("applyCompression", () => {
|
|
it("returns unchanged body for off mode", () => {
|
|
const body = { messages: [{ role: "user", content: "test" }] };
|
|
const result = applyCompression(body, "off");
|
|
assert.equal(result.compressed, false);
|
|
assert.equal(result.stats, null);
|
|
assert.deepEqual(result.body, body);
|
|
});
|
|
|
|
it("applies lite compression for lite mode", () => {
|
|
const body = { messages: [{ role: "user", content: "test\n\n\n\nmessage" }] };
|
|
const result = applyCompression(body, "lite");
|
|
assert.equal(result.compressed, true);
|
|
assert.ok(result.stats);
|
|
assert.equal(result.stats.mode, "lite");
|
|
});
|
|
|
|
it("returns unchanged body for standard mode (Phase 2)", () => {
|
|
const body = { messages: [{ role: "user", content: "test" }] };
|
|
const result = applyCompression(body, "standard");
|
|
assert.equal(result.compressed, false);
|
|
});
|
|
|
|
it("applies rtk compression to tool output", () => {
|
|
const body = {
|
|
messages: [
|
|
{
|
|
role: "tool",
|
|
content: Array.from({ length: 20 }, () => "same noisy line").join("\n"),
|
|
},
|
|
],
|
|
};
|
|
const result = applyCompression(body, "rtk");
|
|
assert.equal(result.stats?.mode, "rtk");
|
|
assert.equal(result.stats?.engine, "rtk");
|
|
assert.equal(result.compressed, true);
|
|
});
|
|
|
|
it("applies stacked compression with RTK followed by Caveman", () => {
|
|
const body = {
|
|
messages: [
|
|
{
|
|
role: "tool",
|
|
content: Array.from({ length: 20 }, () => "same noisy line").join("\n"),
|
|
},
|
|
{
|
|
role: "user",
|
|
content: "Could you please explain in detail what I need to do?",
|
|
},
|
|
],
|
|
};
|
|
const result = applyCompression(body, "stacked");
|
|
assert.equal(result.stats?.mode, "stacked");
|
|
assert.equal(result.stats?.engine, "stacked");
|
|
assert.ok(result.stats?.engineBreakdown?.some((entry) => entry.engine === "rtk"));
|
|
assert.ok(result.stats?.engineBreakdown?.some((entry) => entry.engine === "caveman"));
|
|
});
|
|
});
|