Files
OmniRoute/src/lib/compression/judgeModelClient.ts
Paijo 7cfabfc5c9 perf(executors): lazy-load the executor registry — defer class imports + construction to first use (#11220) (#11421)
Validated in a combined 3-PR batch worktree off release/v3.8.51 tip (a sibling PR from the same author, #11495, was held out — a typecheck error in zai-web.ts only reproduced with this PR + #11495 boarded together, and cleared without #11495; isolated this PR alone confirmed clean on its own too, so the interaction belonged to #11495's side — see its comment).
- Golden lock: executor-map-golden.test.ts — passes byte-identical (same keys, classes, provider identities, dispatch guards)
- Focused tests part of batch's 94/94 node:test run
- typecheck:core, file-size, changelog-integrity, complexity, cognitive-complexity — all OK
- Full-repo lint: 228 pre-existing dashboard react-hooks/* findings, unrelated to this diff

Thanks for the measured, careful methodology here — the golden-lock contract plus the isolated DATA_DIR benchmarking make this an easy PR to trust despite the wide surface (72 files).
2026-08-25 18:22:46 -03:00

45 lines
1.8 KiB
TypeScript

import { getExecutor } from "@omniroute/open-sse/executors/index";
import type { ExecuteInput, ProviderCredentials } from "@omniroute/open-sse/executors/base";
import type {
ChatTurn,
ModelCallResult,
ModelClient,
} from "@omniroute/open-sse/services/compression/eval/types";
import { calculateCost } from "@/lib/usage/costCalculator";
/**
* Cost-aware judge ModelClient for the compression playground's fidelity verify.
* Hard Rule #18 — NOT unit-tested (touches the real executor); the cost math is calculateCost
* (already covered) and the cap logic is judgeFidelityBatch (unit-tested with a stub). Computes
* usdCost from FULL usage (prompt + completion tokens) via the canonical pricing engine, so the
* USD cap actually engages and totalUsd is real.
*/
export function createPricedJudgeClient(
provider: string,
credentials: ProviderCredentials
): ModelClient {
return {
async complete(model: string, messages: ChatTurn[]): Promise<ModelCallResult> {
// #11220: getExecutor is async (lazy registry) — resolve per call.
const executor = await getExecutor(provider);
const input: ExecuteInput = {
model,
body: { model, messages, stream: false },
stream: false,
credentials,
};
const raw = (await executor.execute(input)) as { response: Response };
const json = (await raw.response.json()) as {
choices?: Array<{ message?: { content?: string } }>;
usage?: { prompt_tokens?: number; completion_tokens?: number };
};
const text = json.choices?.[0]?.message?.content ?? "";
const usdCost = await calculateCost(provider, model, {
prompt_tokens: json.usage?.prompt_tokens,
completion_tokens: json.usage?.completion_tokens,
});
return usdCost > 0 ? { text, usdCost } : { text };
},
};
}