Files
OmniRoute/open-sse/services/compression/planResolution.ts
Diego Rodrigues de Sa e Souza 5e9c11006b feat(compression): per-request x-omniroute-compression header (Phase 3) (#4645)
* docs(compression): Phase 3 per-request header design spec

Approved brainstorming output for the x-omniroute-compression header:
header-first precedence, name-first combo matching (Decision A), explicit
value bypasses auto-trigger (Decision B), DerivedPlan.source, and the
X-OmniRoute-Compression response header.

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

* docs(compression): Phase 3 per-request header implementation plan

4-task TDD plan (resolver header-first + source, parser, chatCore wiring +
response header, docs/file-size) with full code and exact commands.

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

* feat(compression): header-first resolver + plan source (Phase 3 core)

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

* feat(compression): resolveCompressionHeader parser (Phase 3)

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

* feat(compression): wire x-omniroute-compression header + response header (Phase 3)

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

* refactor(compression): extract plan-resolution leaf (planResolution.ts) under size cap (Phase 3)

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

* docs(compression): document x-omniroute-compression header (Phase 3)

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

* fix(compression): harden named-combo map + trim engine: header id (Phase 3 review)

Addresses gemini-code-assist review on #4645:
- Extract buildNamedComboLookup (pure) so a blank/whitespace/null combo name
  contributes only its id key (no '' key, no throw that disables all combos).
- Trim the engine:<id> header value so 'engine: rtk' resolves.

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

---------

Co-authored-by: Diego Rodrigues de Sa e Souza <diego.souza@cdwasolutions.com.br>
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-authored-by: Diego Rodrigues de Sa e Souza <souzamiriamrodrigues790@gmail.com>
2026-06-22 17:30:45 -03:00

107 lines
4.3 KiB
TypeScript

import type { CompressionConfig, CompressionPipelineStep } from "./types.ts";
import { resolveCompressionPlan } from "./resolveCompressionPlan.ts";
import {
deriveDefaultPlan,
type DerivedPlan,
type CompressionSource,
} from "./deriveDefaultPlan.ts";
/** Named-combo map: combo id → its stacked pipeline (operator-defined profiles). */
type NamedCombos = Record<string, CompressionPipelineStep[]>;
/** Tags a plan with the precedence layer that produced it (Phase 3 observability). */
export function withSource(plan: DerivedPlan, source: CompressionSource): DerivedPlan {
return { ...plan, source };
}
/**
* Interprets the `x-omniroute-compression` request header into a plan, or null when the
* value is unrecognized (caller falls through to normal resolution). Pure.
* off -> no compression
* default -> the panel-derived Default (ignores active profile / routing / auto-trigger)
* engine:<id> -> that single engine, when enabled in config.engines
* <combo> -> a named combo, matched name-first (lowercased) then exact id (Decision A)
*/
export function planFromHeader(
config: CompressionConfig,
header: string,
combos: NamedCombos
): DerivedPlan | null {
const h = header.trim();
if (!h) return null;
const lower = h.toLowerCase();
if (lower === "off") return withSource({ mode: "off", stackedPipeline: [] }, "request-header");
if (lower === "default") {
// Empty combos + null comboId yields the pure panel default (no active-combo leak).
return withSource(deriveDefaultPlanFromConfig(config, null, {}), "request-header");
}
if (lower.startsWith("engine:")) {
const id = lower.slice("engine:".length).trim();
const engine = config.engines?.[id];
return engine?.enabled
? withSource(deriveDefaultPlan({ [id]: engine }, true), "request-header")
: null;
}
const combo = combos[lower] ?? combos[h];
return combo ? withSource({ mode: "stacked", stackedPipeline: combo }, "request-header") : null;
}
/** Renders the X-OmniRoute-Compression response header value. */
export function formatCompressionMeta(plan: DerivedPlan): string {
return `${plan.mode}; source=${plan.source ?? "off"}`;
}
/**
* Builds the named-combo lookup keyed by BOTH combo id and lowercased (trimmed) name, so the
* `<combo>` header form can match by either. A combo with a blank/whitespace/missing name
* contributes only its id key — a blank name would otherwise register a useless "" key, and a
* single malformed row must not throw and disable the whole map (`name` is NOT NULL in the DB
* but may be an empty string). Pure.
*/
export function buildNamedComboLookup(
combos: Array<{ id: string; name?: string | null; pipeline: CompressionPipelineStep[] }>
): NamedCombos {
const map: NamedCombos = {};
for (const c of combos) {
map[c.id] = c.pipeline;
const name = c.name?.trim();
if (name) map[name.toLowerCase()] = c.pipeline;
}
return map;
}
/**
* Derived-default step. The per-engine toggle map drives the default ONLY when it was
* EXPLICITLY configured via the panel (a stored `engines` row — `config.enginesExplicit`).
* For legacy installs the map is backfilled for DISPLAY only (so the panel shows current
* state); dispatch falls back to the historical `config.defaultMode` so behaviour is
* byte-for-byte preserved until the operator opts into the panel by saving. This avoids a
* silent behaviour change for installs whose backfilled engine flags don't exactly match
* their old defaultMode.
*/
export function deriveDefaultPlanFromConfig(
config: CompressionConfig,
comboId: string | null,
combos: NamedCombos = {}
): DerivedPlan {
if (config.enginesExplicit) {
// Panel-configured: the engines map (via the resolver, which stays header/active-combo
// aware for Phases 2-3) is authoritative — including an explicit "everything off".
return resolveCompressionPlan(config, { comboId, combos });
}
// Legacy path: defaultMode carries the effective mode (the engines map is display-only here).
const legacyMode = config.defaultMode;
if (legacyMode && legacyMode !== "off") {
return legacyMode === "stacked"
? { mode: legacyMode, stackedPipeline: config.stackedPipeline ?? [] }
: { mode: legacyMode, stackedPipeline: [] };
}
return { mode: "off", stackedPipeline: [] };
}