Files
OmniRoute/open-sse/services/compression/eval/executorModelClient.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

46 lines
2.1 KiB
TypeScript

import { getExecutor } from "../../../executors/index.ts";
import type { ExecuteInput, ProviderCredentials } from "../../../executors/base.ts";
import type { ChatTurn, ModelCallResult, ModelClient } from "./types.ts";
/**
* Production ModelClient adapter (Hard Rule #18 — NOT unit-tested; validated on a real
* VPS/account). Wraps the server executor: builds a minimal non-stream chat body, calls
* `getExecutor(provider).execute(...)`, reads the response text + (best-effort) usage cost.
*
* The pure runner depends only on the `ModelClient` interface; this adapter is the single
* place that touches credentials, the executor, and Response parsing — so the eval stays
* faithful to production while the runner/scorers remain fully testable with a stub.
*/
export function createExecutorModelClient(
provider: string,
credentials: ProviderCredentials,
costPerKTokenOut?: number
): 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 body = { model, messages, stream: false };
const input: ExecuteInput = {
model,
body,
stream: false,
credentials,
};
// BaseExecutor.execute resolves to { response, url, headers, transformedBody } — the
// upstream Response lives on `.response` (never a bare Response). Validated live on VPS.
const raw = (await executor.execute(input)) as { response: Response };
const response = raw.response;
const json = (await response.json()) as {
choices?: Array<{ message?: { content?: string } }>;
usage?: { completion_tokens?: number };
};
const text = json.choices?.[0]?.message?.content ?? "";
const outTokens = json.usage?.completion_tokens ?? 0;
const usdCost =
typeof costPerKTokenOut === "number" ? (outTokens / 1000) * costPerKTokenOut : undefined;
return usdCost === undefined ? { text } : { text, usdCost };
},
};
}