Files
OmniRoute/open-sse/services/combo/autoConfig.ts
Jan Leon f879a394f4 feat(routing): add prompt-cache affinity (#8008)
* Add prompt cache locality routing

* fix: preserve weighted cache-affinity routing

* feat(routing): add cache-optimized combos

* fix(routing): preserve normal ordering on cache misses

* fix(routing): bind cache affinity to concrete accounts

* feat(routing): add prompt-cache affinity + align combo-auto-config test with new defaults

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

---------

Co-authored-by: Diego Rodrigues de Sa e Souza <diegosouza.pw@gmail.com>
Co-authored-by: JxnLexn <JxnLexn@users.noreply.github.com>
Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
2026-07-22 06:43:31 -03:00

78 lines
3.1 KiB
TypeScript

import {
DEFAULT_WEIGHTS,
normalizeScoringWeights,
type ScoringWeights,
} from "../autoCombo/scoring.ts";
import { getModePack } from "../autoCombo/modePacks.ts";
import { isRecord } from "./comboData.ts";
import { resolveResetWindowConfig, resolveSlaRoutingPolicy } from "./quotaScoring.ts";
import type { ComboLike, ResolvedComboTarget } from "./types.ts";
/**
* Resolve the auto-strategy routing configuration for a combo.
*
* Pure function of `(combo, eligibleTargets)`: derives the router strategy name,
* candidate provider pool, scoring weights, exploration rate, budget cap, mode
* pack, reset-window config and SLA policy from the combo's `autoConfig`/`config`.
* No side effects, no early returns — extracted verbatim from `handleComboChat`
* so its behavior is byte-identical to the previous inline block.
*/
export function parseAutoConfig(combo: ComboLike, eligibleTargets: ResolvedComboTarget[]) {
const rawAutoConfigSource =
combo?.autoConfig ||
(isRecord(combo?.config?.auto) ? combo.config.auto : null) ||
combo?.config ||
{};
const autoConfigSource: Record<string, unknown> = isRecord(rawAutoConfigSource)
? rawAutoConfigSource
: {};
const routingStrategy =
typeof autoConfigSource.routerStrategy === "string"
? autoConfigSource.routerStrategy
: typeof autoConfigSource.routingStrategy === "string"
? autoConfigSource.routingStrategy
: typeof autoConfigSource.strategyName === "string"
? autoConfigSource.strategyName
: "rules";
const candidatePool = Array.isArray(autoConfigSource.candidatePool)
? autoConfigSource.candidatePool
: [...new Set(eligibleTargets.map((target) => target.provider))];
const configuredWeights =
autoConfigSource.weights && typeof autoConfigSource.weights === "object"
? (autoConfigSource.weights as ScoringWeights)
: DEFAULT_WEIGHTS;
const explorationRate = Number.isFinite(Number(autoConfigSource.explorationRate))
? Number(autoConfigSource.explorationRate)
: 0.05;
const budgetCap = Number.isFinite(Number(autoConfigSource.budgetCap))
? Number(autoConfigSource.budgetCap)
: undefined;
// #3470: persisted fallback policy for when EVERY candidate exceeds budgetCap.
// Any other value (including absent) falls through to the engine's "cheapest" default.
const budgetFallback: "strict" | "cheapest" | undefined =
autoConfigSource.budgetFallback === "strict" || autoConfigSource.budgetFallback === "cheapest"
? (autoConfigSource.budgetFallback as "strict" | "cheapest")
: undefined;
const modePack =
typeof autoConfigSource.modePack === "string" ? autoConfigSource.modePack : undefined;
const weights = normalizeScoringWeights(
modePack ? getModePack(modePack) || configuredWeights : configuredWeights
);
const resetWindowConfig = resolveResetWindowConfig(autoConfigSource);
const slaPolicy = resolveSlaRoutingPolicy(autoConfigSource);
return {
routingStrategy,
candidatePool,
weights,
explorationRate,
budgetCap,
budgetFallback,
modePack,
resetWindowConfig,
slaPolicy,
};
}