@zerogpu/browser-extension-sdk 0.4.0-dev.202609241547.g4addd51 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/README.md +24 -0
  2. package/bin/zerogpu.mjs +4 -1
  3. package/dist/assets/zerogpu-assets.json +4 -3
  4. package/dist/assets/zerogpu-worker.js +1 -0
  5. package/dist/extension/zerogpu-offscreen.js +1 -1
  6. package/dist/manifest.json +6 -6
  7. package/dist/types/adapters/cached-load.d.ts +19 -0
  8. package/dist/types/adapters/onnx/adapter.d.ts +5 -10
  9. package/dist/types/adapters/onnx/executor.d.ts +16 -0
  10. package/dist/types/adapters/onnx/runtime.d.ts +18 -7
  11. package/dist/types/adapters/transformers/adapter.d.ts +5 -7
  12. package/dist/types/adapters/transformers/executor.d.ts +10 -0
  13. package/dist/types/adapters/types.d.ts +23 -0
  14. package/dist/types/config/types.d.ts +18 -0
  15. package/dist/types/extension/offscreen.d.ts +9 -2
  16. package/dist/types/inference/execution.d.ts +33 -0
  17. package/dist/types/inference/runtime-locator.d.ts +35 -0
  18. package/dist/types/inference/worker-client.d.ts +64 -0
  19. package/dist/types/inference/worker-entry.d.ts +1 -0
  20. package/dist/types/inference/worker-host.d.ts +14 -0
  21. package/dist/types/inference/worker-protocol.d.ts +80 -0
  22. package/dist/types/inference/worker-source.d.ts +1 -0
  23. package/dist/types/runtime/guardrails.d.ts +21 -8
  24. package/dist/types/utils/logger.d.ts +10 -2
  25. package/dist/v/zerogpu-browser-sdk.87efe0d8c5ba.umd.js +1 -0
  26. package/dist/zerogpu-browser-sdk.cjs +1 -1
  27. package/dist/zerogpu-browser-sdk.esm.js +1 -1
  28. package/dist/zerogpu-browser-sdk.umd.js +1 -1
  29. package/dist/zerogpu-worker.js +1 -0
  30. package/docs/extension-developer-guide.md +5 -1
  31. package/package.json +1 -1
  32. package/dist/v/zerogpu-browser-sdk.2a8813959ec2.umd.js +0 -1
@@ -1,3 +1,5 @@
1
+ import { TRANSFORMERS_CDN } from '../../inference/runtime-locator';
2
+ export { TRANSFORMERS_CDN };
1
3
  /**
2
4
  * transformers.js is loaded at runtime from a CDN (or a vendored copy under
3
5
  * MV3) and ships no types we can import, so these describe only the surface
@@ -61,13 +63,18 @@ export interface TransformersLib {
61
63
  };
62
64
  Tensor: new (type: string, data: BigInt64Array, dims: number[]) => unknown;
63
65
  }
64
- declare global {
65
- interface Window {
66
- TransformersPipeline?: (task: string, modelPath: string, options?: Record<string, unknown>) => Promise<TransformersPipelineFn>;
67
- TransformersEnv?: TransformersEnv;
68
- TransformersLib?: TransformersLib;
69
- }
66
+ export type TransformersPipelineFactory = (task: string, modelPath: string, options?: Record<string, unknown>) => Promise<TransformersPipelineFn>;
67
+ /**
68
+ * The transformers.js exports, published on the global object of whichever
69
+ * thread loaded the runtime — the page, or the inference worker. Always read
70
+ * through {@link transformersGlobals}: the worker has no `window`.
71
+ */
72
+ export interface TransformersGlobals {
73
+ TransformersPipeline?: TransformersPipelineFactory;
74
+ TransformersEnv?: TransformersEnv;
75
+ TransformersLib?: TransformersLib;
70
76
  }
77
+ export declare const transformersGlobals: () => TransformersGlobals;
71
78
  /** Picks the {@link TransformersLib} exports off an imported transformers.js module. */
72
79
  export declare const pickTransformersLib: (mod: unknown) => TransformersLib | undefined;
73
80
  export declare const requireTransformersLib: () => TransformersLib;
@@ -78,7 +85,11 @@ export declare const requireTransformersLib: () => TransformersLib;
78
85
  * layer off there. The SDK's own IndexedDB store stays best-effort either way.
79
86
  */
80
87
  export declare const disableBrowserCacheIfUnavailable: (env: TransformersEnv | undefined) => void;
81
- export declare const TRANSFORMERS_CDN = "https://cdn.jsdelivr.net/npm/@xenova/transformers@2.17.2/dist/transformers.min.js";
88
+ /**
89
+ * Imports transformers.js from wherever the runtime locator points (CDN by
90
+ * default, the vendored copy under MV3) and publishes its exports on this
91
+ * thread's global. Idempotent; a failed import is retried on the next call.
92
+ */
82
93
  export declare const ensureTransformersRuntime: () => Promise<void>;
83
94
  export declare const sanitizeBaseUrl: (url: string) => string;
84
95
  export declare const isHttpUrl: (value: string) => boolean;
@@ -1,10 +1,8 @@
1
1
  import { SlmConfig } from '../../config/types';
2
2
  import { AdapterLoadOptions, AdapterLoadResult } from '../types';
3
- export interface ExtractedEntity {
4
- text: string;
5
- label: string;
6
- score: number;
7
- start: number;
8
- end: number;
9
- }
3
+ export type { ExtractedEntity } from './executor';
4
+ /**
5
+ * Transformers (token-classification) adapter: the model cache around the
6
+ * executor, which runs in the inference worker by default.
7
+ */
10
8
  export declare const loadTransformersAdapter: (slm: SlmConfig, options?: AdapterLoadOptions) => Promise<AdapterLoadResult>;
@@ -0,0 +1,10 @@
1
+ import { ExecutorLoader } from '../types';
2
+ export interface ExtractedEntity {
3
+ text: string;
4
+ label: string;
5
+ score: number;
6
+ start: number;
7
+ end: number;
8
+ }
9
+ /** Loads a token-classification model on the calling thread and returns its runner. */
10
+ export declare const createTransformersExecutor: ExecutorLoader;
@@ -59,6 +59,29 @@ export interface ModelAdapter {
59
59
  runSample(sample: TaskSample): Promise<SampleTestResult>;
60
60
  dispose?(): Promise<void>;
61
61
  }
62
+ /**
63
+ * A loaded model that runs — the thread-agnostic core of an adapter. The
64
+ * model cache (IndexedDB), load metrics and registry bookkeeping stay on the
65
+ * main thread and wrap an executor whether it lives there or in the inference
66
+ * worker (see inference/execution.ts).
67
+ */
68
+ export interface ModelExecutor {
69
+ readonly loadModelMs: number;
70
+ runInference(input: InferenceInput): Promise<InferenceOutput>;
71
+ runSample(sample: TaskSample): Promise<SampleTestResult>;
72
+ dispose?(): Promise<void>;
73
+ }
74
+ /** Called for every model file the runtime had to fetch (cache key → bytes). */
75
+ export type FileDownloadedCallback = (filename: string, blob: Blob) => void;
76
+ export interface ExecutorContext {
77
+ /** Served wasm guardrails, when the caller resolves them elsewhere (the worker). */
78
+ wasm?: {
79
+ maxThreads: number;
80
+ proxyWorker: boolean;
81
+ };
82
+ }
83
+ /** Instantiates a model from its cached files, downloading whatever is missing. */
84
+ export type ExecutorLoader = (slm: SlmConfig, cachedFiles: Record<string, Blob>, onFileDownloaded: FileDownloadedCallback, context?: ExecutorContext) => Promise<ModelExecutor>;
62
85
  export interface AdapterLoadOptions {
63
86
  allowDownload?: boolean;
64
87
  }
@@ -71,8 +71,26 @@ export interface TelemetryConfig {
71
71
  cameraData?: boolean;
72
72
  events: boolean;
73
73
  }
74
+ export interface InferenceConfig {
75
+ /**
76
+ * Run model download, session creation and every inference for ONNX-backed
77
+ * models in a dedicated Worker so the host page's main thread never blocks
78
+ * (default: true). `false` forces the historical main-thread mode, e.g. for
79
+ * an environment known to lack Workers. When a Worker cannot be created or
80
+ * boot at runtime the SDK falls back to the main thread by itself and
81
+ * reports it once (`guardrail.pressure_warning` / `inference_worker_unavailable`).
82
+ */
83
+ worker: boolean;
84
+ /**
85
+ * Same-origin URL of the SDK's worker script (`dist/zerogpu-worker.js`) for
86
+ * hosts whose Content-Security-Policy forbids `blob:` workers. Absent → the
87
+ * worker source bundled into the SDK is spawned from a blob: URL.
88
+ */
89
+ workerUrl?: string;
90
+ }
74
91
  export interface SdkConfig {
75
92
  sdkVersion: string;
76
93
  orchestrator: OrchestratorConfig;
77
94
  telemetry: TelemetryConfig;
95
+ inference: InferenceConfig;
78
96
  }
@@ -41,6 +41,13 @@ export interface OffscreenInitOptions {
41
41
  trackingRunnerUrl?: string | false;
42
42
  /** Enable verbose SDK console logging (default false). */
43
43
  enableConsoleLogs?: boolean;
44
+ /**
45
+ * Run model loading and inference in the SDK's inference worker (the
46
+ * vendored `zerogpu-worker.js`) rather than on the offscreen document's own
47
+ * thread. Default true; the SDK falls back to the document's thread by
48
+ * itself if the worker cannot start. Set false to force that mode.
49
+ */
50
+ inferenceWorker?: boolean;
44
51
  /**
45
52
  * Operator macros (`cust_param1`..`cust_param3`). Values set here survive the
46
53
  * document being recreated; values sent later with
@@ -51,8 +58,8 @@ export interface OffscreenInitOptions {
51
58
  overrides?: ConfigOverrides;
52
59
  }
53
60
  /**
54
- * Pre-load the vendored model runtimes (so the SDK never reaches the CDN) and
55
- * initialize the ZeroGPU SDK. Safe to call more than once; the SDK ignores
61
+ * Point the SDK at the vendored model runtimes (so it never reaches the CDN)
62
+ * and initialize the ZeroGPU SDK. Safe to call more than once; the SDK ignores
56
63
  * repeated init.
57
64
  */
58
65
  export declare const initZeroGpuOffscreen: (options?: OffscreenInitOptions) => Promise<void>;
@@ -0,0 +1,33 @@
1
+ import { ExecutorLoader } from '../adapters/types';
2
+ import { InferenceConfig } from '../config/types';
3
+ import { WorkerLike } from './worker-client';
4
+ import { WorkerFramework } from './worker-protocol';
5
+ /**
6
+ * Execution strategy for transformers.js-backed models.
7
+ *
8
+ * Default: model download, session creation and every inference run in the
9
+ * SDK's dedicated inference worker, so the host page's main thread never
10
+ * blocks (ONNX session creation alone froze it for 1–2 s per model, and a
11
+ * seq2seq generation for hundreds of ms per request). `inference.worker:
12
+ * false` forces the historical main-thread mode. When a worker cannot be
13
+ * created, fails to boot, or dies, the SDK falls back to the main thread for
14
+ * the rest of the session and reports it once as a warning.
15
+ */
16
+ export type InferenceExecutionMode = 'worker' | 'main-thread';
17
+ export declare const setInferenceExecutionConfig: (next: Partial<InferenceConfig> | undefined) => void;
18
+ /** Where the next transformers.js-backed model will load. */
19
+ export declare const getInferenceExecutionMode: () => InferenceExecutionMode;
20
+ /** The reason main-thread mode was entered this session, if it was. */
21
+ export declare const getInferenceWorkerFallback: () => {
22
+ code: string;
23
+ message: string;
24
+ } | null;
25
+ /**
26
+ * Wraps a thread-agnostic executor loader: the worker when it is wanted and
27
+ * available, the given (main-thread) loader otherwise. A model-level failure
28
+ * in the worker surfaces as-is — the main thread would fail the same way.
29
+ */
30
+ export declare const resolveExecutorLoader: (framework: WorkerFramework, local: ExecutorLoader) => ExecutorLoader;
31
+ /** Test hooks. */
32
+ export declare const __setInferenceWorkerSpawn: (spawn: (() => WorkerLike) | null) => void;
33
+ export declare const __resetInferenceExecution: () => void;
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Where the model runtimes come from.
3
+ *
4
+ * Web pages import transformers.js (and, through it, the ONNX Runtime wasm)
5
+ * from the CDN. A Chrome MV3 offscreen document may not load remote code, so
6
+ * it points everything at the files `npx zerogpu vendor` copied into the
7
+ * extension. Whatever the surface configures here is handed to the inference
8
+ * worker at boot, so both threads resolve the identical files.
9
+ */
10
+ export declare const TRANSFORMERS_CDN = "https://cdn.jsdelivr.net/npm/@xenova/transformers@2.17.2/dist/transformers.min.js";
11
+ export interface ModelRuntimeLocator {
12
+ /** ESM bundle of transformers.js to import. */
13
+ transformersUrl: string;
14
+ /** Directory holding the ORT wasm binaries; probed ahead of the built-in candidates when set. */
15
+ wasmPaths?: string;
16
+ /** Pin ORT to one wasm thread (offscreen documents are never cross-origin isolated). */
17
+ forceSingleThread?: boolean;
18
+ /**
19
+ * Script URL of the inference worker. Absent → the worker source inlined in
20
+ * this bundle is spawned from a blob: URL (MV3 forbids blob: workers, so the
21
+ * offscreen kit sets the vendored file here).
22
+ */
23
+ workerUrl?: string;
24
+ /**
25
+ * Origin whose `/onnx-wasm/` is probed for the wasm binaries. Defaults to the
26
+ * current global's own origin; the worker is told the page's, since a blob:
27
+ * worker cannot rely on its own location.
28
+ */
29
+ pageOrigin?: string;
30
+ }
31
+ /** Merges the given fields; `undefined` values leave the current setting alone. */
32
+ export declare const configureModelRuntime: (overrides: Partial<ModelRuntimeLocator>) => void;
33
+ export declare const getModelRuntimeLocator: () => ModelRuntimeLocator;
34
+ /** Test hook. */
35
+ export declare const __resetModelRuntimeLocator: () => void;
@@ -0,0 +1,64 @@
1
+ import { ModelExecutor } from '../adapters/types';
2
+ import { SlmConfig } from '../config/types';
3
+ import { WasmGuardrails } from '../runtime/guardrails';
4
+ import { WorkerBootConfig, WorkerFramework } from './worker-protocol';
5
+ /** A worker that has not answered its boot handshake by then is broken. */
6
+ export declare const WORKER_BOOT_TIMEOUT_MS = 20000;
7
+ /**
8
+ * A failure of the worker itself (spawn, boot, crash) — never of a model. The
9
+ * execution strategy answers these by loading on the main thread instead.
10
+ */
11
+ export declare class InferenceWorkerError extends Error {
12
+ readonly code: string;
13
+ constructor(code: string, detail?: string);
14
+ }
15
+ /** True for errors the main thread should answer by re-loading locally. */
16
+ export declare const isInferenceWorkerInfrastructureError: (error: unknown) => boolean;
17
+ export declare const inferenceWorkerErrorCode: (error: unknown) => string;
18
+ /** The slice of the Worker interface the client drives (fakeable in tests). */
19
+ export interface WorkerLike {
20
+ postMessage(message: unknown): void;
21
+ terminate(): void;
22
+ onmessage: ((event: MessageEvent) => void) | null;
23
+ onerror: ((event: ErrorEvent) => void) | null;
24
+ onmessageerror?: ((event: MessageEvent) => void) | null;
25
+ }
26
+ export interface InferenceWorkerClientOptions {
27
+ spawn: () => WorkerLike;
28
+ bootConfig: () => WorkerBootConfig;
29
+ bootTimeoutMs?: number;
30
+ /** The worker died after booting; `modelIds` had been loaded into it. */
31
+ onCrash?: (error: InferenceWorkerError, modelIds: string[]) => void;
32
+ }
33
+ /**
34
+ * Main-thread side of the inference worker: spawns it lazily, runs the boot
35
+ * handshake, correlates requests with results and relays the worker's log
36
+ * lines into this thread's logger. One client hosts any number of loaded
37
+ * models (handles); the worker is terminated when the last one is disposed.
38
+ */
39
+ export declare class InferenceWorkerClient {
40
+ private readonly options;
41
+ private worker;
42
+ private ready;
43
+ private booted;
44
+ private dead;
45
+ private nextId;
46
+ private handleSeq;
47
+ private readonly pending;
48
+ /** handle → modelId, for crash attribution. */
49
+ private readonly loaded;
50
+ constructor(options: InferenceWorkerClientOptions);
51
+ isAlive(): boolean;
52
+ loadedCount(): number;
53
+ load(framework: WorkerFramework, slm: SlmConfig, cachedFiles: Record<string, Blob>, wasm: WasmGuardrails): Promise<{
54
+ executor: ModelExecutor;
55
+ downloadedFiles: Record<string, Blob>;
56
+ }>;
57
+ /** Stops the worker; in-flight requests reject with `reason`. */
58
+ terminate(reason?: InferenceWorkerError): void;
59
+ private boot;
60
+ private request;
61
+ private dispatch;
62
+ private crash;
63
+ private rejectAll;
64
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,14 @@
1
+ import { ExecutorLoader } from '../adapters/types';
2
+ import { WorkerFramework } from './worker-protocol';
3
+ /**
4
+ * Worker-side host: answers the main thread's protocol by loading executors
5
+ * through the same loaders the main thread would use, and relays this
6
+ * realm's log lines back over the channel (the page's logger is the single
7
+ * console sink). Runs inside `dist/zerogpu-worker.js` (see worker-entry.ts).
8
+ */
9
+ /** The slice of DedicatedWorkerGlobalScope the host needs (fakeable in tests). */
10
+ export interface WorkerScopeLike {
11
+ postMessage(message: unknown): void;
12
+ onmessage: ((event: MessageEvent) => void) | null;
13
+ }
14
+ export declare const startInferenceWorkerHost: (scope: WorkerScopeLike, loaders: Partial<Record<WorkerFramework, ExecutorLoader>>) => void;
@@ -0,0 +1,80 @@
1
+ import { SlmConfig } from '../config/types';
2
+ import { InferenceInput, TaskSample } from '../adapters/types';
3
+ import { WasmGuardrails } from '../runtime/guardrails';
4
+ import { LogLevel, SafeFields } from '../utils/logger';
5
+ import { ModelRuntimeLocator } from './runtime-locator';
6
+ /**
7
+ * Main thread ⇄ inference worker protocol. Every request carries an `id` the
8
+ * worker echoes in exactly one `result`. Model files travel as Blobs (handles,
9
+ * not copies) in both directions: the main thread keeps the IndexedDB cache
10
+ * and hands cached files in; the worker hands whatever it downloaded back for
11
+ * persisting.
12
+ */
13
+ export declare const INFERENCE_WORKER_PROTOCOL = 1;
14
+ /** Frameworks whose executors the worker can host (GGUF/wllama runs its own worker). */
15
+ export type WorkerFramework = 'ONNX' | 'Transformers';
16
+ export interface WorkerBootConfig {
17
+ protocol: number;
18
+ logging: {
19
+ enabled: boolean;
20
+ };
21
+ runtime: ModelRuntimeLocator;
22
+ }
23
+ export interface WorkerLoadResult {
24
+ loadModelMs: number;
25
+ downloadedFiles: Record<string, Blob>;
26
+ }
27
+ export type MainToWorkerMessage = {
28
+ type: 'init';
29
+ id: number;
30
+ config: WorkerBootConfig;
31
+ } | {
32
+ type: 'load';
33
+ id: number;
34
+ handle: string;
35
+ framework: WorkerFramework;
36
+ slm: SlmConfig;
37
+ cachedFiles: Record<string, Blob>;
38
+ wasm: WasmGuardrails;
39
+ } | {
40
+ type: 'infer';
41
+ id: number;
42
+ handle: string;
43
+ input: InferenceInput;
44
+ } | {
45
+ type: 'sample';
46
+ id: number;
47
+ handle: string;
48
+ sample: TaskSample;
49
+ } | {
50
+ type: 'dispose';
51
+ id: number;
52
+ handle: string;
53
+ };
54
+ export interface WorkerErrorShape {
55
+ name: string;
56
+ message: string;
57
+ /** Set for environment failures the main thread should fall back on. */
58
+ code?: string;
59
+ }
60
+ export type WorkerToMainMessage = {
61
+ type: 'ready';
62
+ protocol: number;
63
+ } | {
64
+ type: 'result';
65
+ id: number;
66
+ ok: true;
67
+ value: unknown;
68
+ } | {
69
+ type: 'result';
70
+ id: number;
71
+ ok: false;
72
+ error: WorkerErrorShape;
73
+ } | {
74
+ type: 'log';
75
+ level: LogLevel;
76
+ message: string;
77
+ fields?: SafeFields;
78
+ };
79
+ /** The transformers.js import failed inside the worker: retry the load on the main thread. */
80
+ export declare const WORKER_RUNTIME_UNAVAILABLE = "transformers_runtime_load_failed";
@@ -0,0 +1 @@
1
+ export declare const getInlineWorkerSource: () => string | null;
@@ -25,10 +25,12 @@ export interface ResolvedGuardrails {
25
25
  maxInputTokens: number;
26
26
  /** Warm-up latency budget in ms; 0 = success-only readiness gating. */
27
27
  sampleLatencyBudgetMs: number;
28
- wasm: {
29
- maxThreads: number;
30
- proxyWorker: boolean;
31
- };
28
+ wasm: WasmGuardrails;
29
+ }
30
+ /** Served `guardrails.wasm`, resolved. Handed to the inference worker per load. */
31
+ export interface WasmGuardrails {
32
+ maxThreads: number;
33
+ proxyWorker: boolean;
32
34
  }
33
35
  /** Devices that report no memory are treated as modest 2 GB devices (same as the server). */
34
36
  export declare const UNKNOWN_DEVICE_MEMORY_GB = 2;
@@ -54,12 +56,18 @@ export declare const estimateInputTokens: (text: string) => number;
54
56
  /** True when a served cap exists and the input exceeds it (0/absent cap = no gate). */
55
57
  export declare const inputExceedsTokenCap: (text: string, capTokens?: number) => boolean;
56
58
  export interface WasmEnvironmentProbe {
57
- /** window.crossOriginIsolated && SharedArrayBuffer usable. */
59
+ /** crossOriginIsolated && SharedArrayBuffer usable (the page's status is inherited by its workers). */
58
60
  isolated: boolean;
59
61
  /** navigator.hardwareConcurrency (0/absent → treated as 1). */
60
62
  cores: number;
61
63
  /** typeof Worker === 'function'. */
62
64
  workerAvailable: boolean;
65
+ /**
66
+ * A document exists, i.e. this is the main thread. ORT's proxy is the
67
+ * main-thread→worker hop, so it means nothing inside the inference worker
68
+ * (ORT itself ignores it there). Absent → true.
69
+ */
70
+ inDocument?: boolean;
63
71
  }
64
72
  /**
65
73
  * ONNX wasm execution settings for this environment + served config:
@@ -67,10 +75,15 @@ export interface WasmEnvironmentProbe {
67
75
  * cross-origin-isolated pages (COOP/COEP on the loader). When available, use
68
76
  * min(hardwareConcurrency − 1, served maxThreads) — one core is left for the
69
77
  * page. Not isolated → 1, exactly the long-standing behavior.
70
- * - proxy: worker execution is opt-in via `guardrails.wasm.proxyWorker` (per
71
- * surface, after verifying the target WebView), and only where Worker exists.
78
+ * - proxy: ORT's own wasm proxy worker is opt-in via `guardrails.wasm
79
+ * .proxyWorker`, only where Worker exists, and only on the main thread. It
80
+ * predates the SDK's inference worker (inference/execution.ts), which is the
81
+ * default and hosts ORT entirely; the proxy still applies when a host forces
82
+ * main-thread mode.
83
+ *
84
+ * `wasm` defaults to the served block; the worker passes the one it was handed.
72
85
  */
73
- export declare const resolveWasmSettings: (probe?: WasmEnvironmentProbe) => {
86
+ export declare const resolveWasmSettings: (probe?: WasmEnvironmentProbe, wasm?: WasmGuardrails) => {
74
87
  numThreads: number;
75
88
  proxy: boolean;
76
89
  };
@@ -38,9 +38,17 @@ export declare const summarizeError: (error: unknown) => string;
38
38
  * error summaries comes out the other side.
39
39
  */
40
40
  export declare const sanitizeLogFields: (fields: unknown) => Record<string, unknown>;
41
+ /**
42
+ * Alternative destination for log lines. The inference worker installs one
43
+ * that relays every line to the main thread, whose logger is the single
44
+ * console sink: fields arrive there unsanitized and pass the redaction gate
45
+ * exactly once, on the way to the console.
46
+ */
47
+ export type LogSink = (level: LogLevel, message: string, fields?: SafeFields | Error) => void;
41
48
  declare class Logger {
42
49
  private enabled;
43
- constructor(config: TelemetryConfig);
50
+ private readonly sink;
51
+ constructor(config: TelemetryConfig, sink?: LogSink);
44
52
  setEnabled(enabled: boolean): void;
45
53
  isEnabled(): boolean;
46
54
  private emit;
@@ -50,5 +58,5 @@ declare class Logger {
50
58
  error(message: string, fields?: SafeFields | Error): void;
51
59
  }
52
60
  export type { Logger };
53
- export declare const initLogger: (config: TelemetryConfig) => Logger;
61
+ export declare const initLogger: (config: TelemetryConfig, sink?: LogSink) => Logger;
54
62
  export declare const getLogger: () => Logger;