/** * Shared ONNX text embedders — SP-100 (MiniLM), SP-156 (Granite trial). * * Embeds prompt text for HyDRA requirement projection and semantic cluster * matching. One ONNX session per instance; share across matchers via a single * factory call and coordinated dispose(). */ import type { Encoder, EncoderCascadeConfig } from '../types/schemas.js'; import { type EncoderGateReasonCode } from './encoder-gate.js'; export declare const EMBEDDING_DIM = 384; /** MiniLM ONNX model (384-dim, 512-token context). */ export declare const MINILM_ONNX_MODEL = "Xenova/all-MiniLM-L6-v2"; /** * Granite 97M ONNX artifact for @huggingface/transformers. * Source weights: ibm-granite/granite-embedding-97m-multilingual-r2 (384-dim). */ export declare const GRANITE_ONNX_MODEL = "onnx-community/granite-embedding-97m-multilingual-r2-ONNX"; export interface TextEmbedder { embed(text: string): Promise; dispose(): Promise; } /** * Artifact pin verification mode: * - `off` — no verification (default; preserves unpinned local dogfood downloads) * - `verify` — verify cached artifacts when pins exist for the model; fail * closed on digest mismatch or missing cached artifact. Models without pins * remain unpinned (first-run download allowed). * - `enforce` — CI/prod mode: pins are REQUIRED for the model; fail closed when * the pin file is missing/unparseable, the model has no pins, a pinned * artifact is missing from the cache, or any digest mismatches. */ export type OnnxArtifactPinMode = 'off' | 'verify' | 'enforce'; export declare const ONNX_ARTIFACT_PIN_MODES: readonly OnnxArtifactPinMode[]; /** Default pin file (relative to process CWD). Override via options or env. */ export declare const DEFAULT_ONNX_PIN_FILE = "config/onnx-artifact-pins.json"; /** Env vars (operator wiring; schemas/hydra-matcher out of SP-259 scope). */ export declare const ONNX_PIN_MODE_ENV = "SMART_ROUTER_ONNX_PIN_MODE"; export declare const ONNX_PIN_FILE_ENV = "SMART_ROUTER_ONNX_PIN_FILE"; /** Pin file shape: per-model map of cache-relative file path → SHA-256 hex. */ export interface OnnxArtifactPinFile { readonly version: number; readonly pins: Record>; } export interface OnnxPinOptions { /** Pin mode. Default: $SMART_ROUTER_ONNX_PIN_MODE, else 'off'. */ readonly pinMode?: OnnxArtifactPinMode; /** Pin file path. Default: $SMART_ROUTER_ONNX_PIN_FILE, else DEFAULT_ONNX_PIN_FILE. */ readonly pinFilePath?: string; } /** * Verify cached ONNX artifacts for `modelId` against configured SHA-256 pins. * Fails closed on digest mismatch or missing pinned artifact; in `enforce` * mode also fails when the pin file or model pins are missing. * * Runs after pipeline load so first-run downloads are verified before the * embedder is returned; a mismatching session is never usable. */ export declare function verifyOnnxArtifactPins(modelId: string, cachePath: string, options?: OnnxPinOptions): Promise; /** * Creates a TextEmbedder backed by @huggingface/transformers ONNX runtime. * Model: Xenova/all-MiniLM-L6-v2 (384-dim). * * The package is loaded dynamically — not required at compile time. * Install: `npm i @huggingface/transformers` */ export declare function createOnnxTextEmbedder(artifactCachePath: string, pinOptions?: OnnxPinOptions): Promise; /** * Granite 97M long-context embedder (384-dim ONNX drop-in for SP-115 head). * Model: ibm-granite/granite-embedding-97m-multilingual-r2 via ONNX runtime. */ export declare function createGraniteOnnxTextEmbedder(artifactCachePath: string, pinOptions?: OnnxPinOptions): Promise; /** Select ONNX text embedder by operator encoder flag. */ export declare function createTextEmbedder(encoder: Encoder | undefined, artifactCachePath: string, pinOptions?: OnnxPinOptions): Promise; /** * Decision telemetry for the most recent cascade embed. Mirrors the routing * feature-sidecar fields from #173 so the pipeline can copy them onto live * decisions without reshaping (SP-292). */ export interface CascadeEmbedderTelemetry { /** Encoder that produced the embedding (never mixed across spaces). */ readonly encoder_selected: Encoder; /** Gate token estimate for the embedded prompt. */ readonly token_estimate: number; /** Configured cascade threshold in effect for the decision. */ readonly cascade_threshold: number; /** SP-291 gate reason code (`under_threshold` / `over_threshold` / `granite_fallback`). */ readonly reason_code: EncoderGateReasonCode; /** * Present when an over-threshold prompt degraded to the primary encoder * because the long-context session was unavailable (degrade, never mix). */ readonly cascade_fallback_reason: EncoderGateReasonCode | null; } export interface CascadingTextEmbedder extends TextEmbedder { /** * Telemetry from the most recently completed embed(); null before the * first embed. Concurrent embeds race on this snapshot — callers on the * routing hot path embed sequentially per request. */ lastTelemetry(): CascadeEmbedderTelemetry | null; } /** Type guard: does this embedder carry cascade decision telemetry? */ export declare function isCascadingTextEmbedder(embedder: TextEmbedder): embedder is CascadingTextEmbedder; export interface CreateCascadingTextEmbedderOptions { readonly pinOptions?: OnnxPinOptions; /** * Primary encoder for under-threshold / fallback embeds. * Default: DEFAULT_ENCODER ('minilm'), matching `hydra.encoder` defaults. */ readonly primaryEncoder?: Encoder; /** * Session factory override for tests. Default: createTextEmbedder bound to * the cascade artifact cache path and pin options. */ readonly sessionFactory?: (encoder: Encoder) => Promise; } /** * Lazy dual-session cascading embedder (SP-292, #173 part 2). * * Owns at most one ONNX session per encoder: the primary session loads on the * first embed, the long-context (Granite) session loads only on the first * over-threshold prompt — memory is paid only when the path fires. Selection * is the SP-291 pure gate; token estimates default to prompt length (the * turn-envelope-parity estimator). * * Failure behavior: degrade, never mix. If the long-context session fails to * load or embed, the request is served by the primary encoder with reason * code `granite_fallback` and the long-context encoder is marked unavailable * for subsequent prompts. Never silently compares cross-encoder vectors. * * dispose() closes every session that was actually loaded (idempotent); * embed() after dispose fails closed (SP-260 precedent). */ export declare function createCascadingTextEmbedder(config: EncoderCascadeConfig, artifactCachePath: string, options?: CreateCascadingTextEmbedderOptions): CascadingTextEmbedder; //# sourceMappingURL=embedding-provider.d.ts.map