@independo/inderun-web 0.2.0-dev.1 → 0.2.0-dev.3
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.
- package/README.md +117 -1
- package/dist/engine.d.ts.map +1 -1
- package/dist/engine.js +3 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/onnx-provider.d.ts +78 -0
- package/dist/onnx-provider.d.ts.map +1 -0
- package/dist/onnx-provider.js +217 -0
- package/dist/onnx-runtime.d.ts +110 -0
- package/dist/onnx-runtime.d.ts.map +1 -0
- package/dist/onnx-runtime.js +76 -0
- package/dist/onnx-transformers-runtime.d.ts +72 -0
- package/dist/onnx-transformers-runtime.d.ts.map +1 -0
- package/dist/onnx-transformers-runtime.js +210 -0
- package/dist/onnx.d.ts +13 -0
- package/dist/onnx.d.ts.map +1 -0
- package/dist/onnx.js +12 -0
- package/dist/route-planner.d.ts +31 -8
- package/dist/route-planner.d.ts.map +1 -1
- package/dist/route-planner.js +46 -17
- package/dist/router.d.ts +11 -1
- package/dist/router.d.ts.map +1 -1
- package/dist/router.js +26 -20
- package/dist/web-sdk.d.ts +12 -3
- package/dist/web-sdk.d.ts.map +1 -1
- package/dist/web-sdk.js +20 -8
- package/package.json +15 -3
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
|
|
9
9
|
TypeScript/Web SDK for IndeRun.
|
|
10
10
|
|
|
11
|
-
This package provides the Web SDK entrypoint, the engine core, routing, telemetry, error normalization,
|
|
11
|
+
This package provides the Web SDK entrypoint, the engine core, routing, telemetry, error normalization, the OpenAI-compatible cloud provider, and the Web ONNX Runtime provider for developer-supplied local models.
|
|
12
12
|
|
|
13
13
|
## Basic Usage
|
|
14
14
|
|
|
@@ -48,6 +48,122 @@ taxonomy (`AuthError`, `RateLimited`, `Timeout`, `Unavailable`, `Internal`). The
|
|
|
48
48
|
exact status-to-class mapping is documented on the provider in code — see
|
|
49
49
|
`OpenAIResponsesProvider` — so it stays in sync with behavior.
|
|
50
50
|
|
|
51
|
+
## On-Device Models: ONNX Runtime (Web)
|
|
52
|
+
|
|
53
|
+
`local.onnx.genai.web` runs developer-supplied ONNX models in the browser. Registering it is what
|
|
54
|
+
makes `constraints.privacy = "local_required"` routable. Pass `onnx` to the factory (`openAI` and
|
|
55
|
+
`onnx` are both optional, but at least one is required):
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
import { createIndeRunWeb } from "@independo/inderun-web";
|
|
59
|
+
|
|
60
|
+
const inderun = createIndeRunWeb({
|
|
61
|
+
onnx: {
|
|
62
|
+
modelPackage: {
|
|
63
|
+
id: "phi-3-mini-web",
|
|
64
|
+
format: "onnx",
|
|
65
|
+
tasks: ["text_to_text"],
|
|
66
|
+
runtime: { platforms: ["web"] },
|
|
67
|
+
source: { sourceType: "registry", ref: "onnx-community/Phi-3-mini-4k-instruct" }
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
`modelPackage` is the provider-neutral `ModelPackage` contract from
|
|
74
|
+
`@independo/inderun-contracts`; only `id` and `format` are required, and the package is validated
|
|
75
|
+
(including the inline-secret and URL-userinfo rules) before every attempt. Model files must never
|
|
76
|
+
carry credentials — use `authContextRef` and secure storage instead.
|
|
77
|
+
|
|
78
|
+
Options: `id` (defaults to `local.onnx.genai.web`), `modelPackage` (required), `runtime`, and
|
|
79
|
+
`timeoutMs` (a request's `constraints.timeoutMs` wins).
|
|
80
|
+
|
|
81
|
+
The default runtime loads **quantized** weights (`q4f16` on WebGPU, `q4` otherwise). Transformers.js
|
|
82
|
+
would otherwise pick `fp32`, which makes ONNX Runtime fail session creation with an allocation error
|
|
83
|
+
(`std::bad_alloc`) for anything but tiny models. Override with `dtype` when your model does not
|
|
84
|
+
publish that variant:
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
import { OnnxRuntimeWebProvider, createTransformersJsRuntime } from "@independo/inderun-web/onnx";
|
|
88
|
+
|
|
89
|
+
const provider = new OnnxRuntimeWebProvider({
|
|
90
|
+
modelPackage,
|
|
91
|
+
runtime: createTransformersJsRuntime({ dtype: "q8", device: "wasm" })
|
|
92
|
+
});
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
The default runtime targets models that load through the Transformers.js `text-generation` pipeline
|
|
96
|
+
— for example `onnx-community/gemma-3-1b-it-ONNX`. Multimodal exports split across separate
|
|
97
|
+
encoder/decoder graphs need `AutoProcessor` plus a model-specific class and should implement the
|
|
98
|
+
runtime seam instead — see [Custom runtimes](#custom-runtimes).
|
|
99
|
+
|
|
100
|
+
### Runtime dependency
|
|
101
|
+
|
|
102
|
+
The default runtime uses [Transformers.js](https://www.npmjs.com/package/@huggingface/transformers),
|
|
103
|
+
which runs ONNX models on `onnxruntime-web` and owns the generation loop (ONNX Runtime GenAI has no
|
|
104
|
+
browser build). It is an optional dependency that IndeRun does not bundle — install it yourself:
|
|
105
|
+
|
|
106
|
+
```sh
|
|
107
|
+
pnpm add @huggingface/transformers
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
If the package is present but fails to initialize, the provider reports itself unavailable and
|
|
111
|
+
routing produces an explainable `capability_unavailable` rejection instead of throwing. Bundlers do
|
|
112
|
+
need to resolve the specifier, so an app that imports `@independo/inderun-web/onnx` without
|
|
113
|
+
installing Transformers.js should pass its own runtime (or a `load` override) — see
|
|
114
|
+
[Custom runtimes](#custom-runtimes). WASM threads additionally require
|
|
115
|
+
cross-origin isolation (`Cross-Origin-Opener-Policy: same-origin`,
|
|
116
|
+
`Cross-Origin-Embedder-Policy: require-corp`) on whatever serves your app.
|
|
117
|
+
|
|
118
|
+
### Supported model sources
|
|
119
|
+
|
|
120
|
+
| `source.sourceType` | Web |
|
|
121
|
+
| ------------------- | ----------- |
|
|
122
|
+
| `registry` | Supported (hub model id in `source.ref`) |
|
|
123
|
+
| `bundled` | Supported (assets served by your app) |
|
|
124
|
+
| `app_managed` | Supported (assets served by your app) |
|
|
125
|
+
| `programmatic` | Supported (requires `createGenerator`) |
|
|
126
|
+
| `remote` | Deferred — download yourself, then declare the files |
|
|
127
|
+
| `filesystem` | Unsupported — browsers cannot read arbitrary local paths |
|
|
128
|
+
|
|
129
|
+
### Errors
|
|
130
|
+
|
|
131
|
+
| Condition | `errorClass` |
|
|
132
|
+
| ----------------------------------------------------------- | -------------------- |
|
|
133
|
+
| Capability gate fails pre-attempt, model missing/incompatible | `CapabilityMismatch` |
|
|
134
|
+
| Runtime initialization failure, resource exhaustion | `Unavailable` |
|
|
135
|
+
| Generation exceeded its timeout budget | `Timeout` |
|
|
136
|
+
| Malformed/empty model output, unexpected runtime failure | `Internal` |
|
|
137
|
+
|
|
138
|
+
A purely local runtime never produces `AuthError`, `RateLimited`, or `Offline`.
|
|
139
|
+
|
|
140
|
+
### Custom runtimes
|
|
141
|
+
|
|
142
|
+
Swap the execution backend without touching provider semantics by implementing
|
|
143
|
+
`OnnxTextGenerationRuntime`:
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
import {
|
|
147
|
+
OnnxRuntimeWebProvider,
|
|
148
|
+
OnnxRuntimeError,
|
|
149
|
+
createFixtureOnnxRuntime,
|
|
150
|
+
type OnnxTextGenerationRuntime
|
|
151
|
+
} from "@independo/inderun-web/onnx";
|
|
152
|
+
|
|
153
|
+
const runtime: OnnxTextGenerationRuntime = {
|
|
154
|
+
async prepare(modelPackage) {
|
|
155
|
+
return { available: true };
|
|
156
|
+
},
|
|
157
|
+
async generate({ messages, generation }, signal) {
|
|
158
|
+
// Throw OnnxRuntimeError("capability" | "unavailable" | "timeout" | "internal", …)
|
|
159
|
+
// to steer IndeRun error normalization.
|
|
160
|
+
return { text: "…" };
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
`createFixtureOnnxRuntime()` provides a deterministic in-memory runtime for tests and offline demos.
|
|
166
|
+
|
|
51
167
|
## Commands
|
|
52
168
|
|
|
53
169
|
```sh
|
package/dist/engine.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,WAAW,EAChB,KAAK,UAAU,EAChB,MAAM,8BAA8B,CAAC;AACtC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAC9C,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAGjD,OAAO,EAAuB,KAAK,gBAAgB,EAAwB,MAAM,gBAAgB,CAAC;AAElG;;;;GAIG;AACH,qBAAa,OAAO;IAWhB,OAAO,CAAC,QAAQ;IAChB,OAAO,CAAC,YAAY;IAXtB,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,gBAAgB,CAAmB;IAE3C;;;;;OAKG;gBAEO,QAAQ,EAAE,gBAAgB,EAC1B,YAAY,EAAE,YAAY,EAClC,gBAAgB,CAAC,EAAE,gBAAgB;IAOrC;;;OAGG;IACH,OAAO,CAAC,QAAQ;IAShB;;;OAGG;IACH,OAAO,CAAC,gBAAgB;IAoBxB;;;;;;;OAOG;IACG,GAAG,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,WAAW,EAChB,KAAK,UAAU,EAChB,MAAM,8BAA8B,CAAC;AACtC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAC9C,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAGjD,OAAO,EAAuB,KAAK,gBAAgB,EAAwB,MAAM,gBAAgB,CAAC;AAElG;;;;GAIG;AACH,qBAAa,OAAO;IAWhB,OAAO,CAAC,QAAQ;IAChB,OAAO,CAAC,YAAY;IAXtB,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,gBAAgB,CAAmB;IAE3C;;;;;OAKG;gBAEO,QAAQ,EAAE,gBAAgB,EAC1B,YAAY,EAAE,YAAY,EAClC,gBAAgB,CAAC,EAAE,gBAAgB;IAOrC;;;OAGG;IACH,OAAO,CAAC,QAAQ;IAShB;;;OAGG;IACH,OAAO,CAAC,gBAAgB;IAoBxB;;;;;;;OAOG;IACG,GAAG,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC;CAsIrD"}
|
package/dist/engine.js
CHANGED
|
@@ -101,7 +101,9 @@ export class IndeRun {
|
|
|
101
101
|
taskKind: request.task.kind,
|
|
102
102
|
explanation: routeSelection.explanation,
|
|
103
103
|
constraints: request.constraints ?? null,
|
|
104
|
-
preferences: request.preferences ?? null
|
|
104
|
+
preferences: request.preferences ?? null,
|
|
105
|
+
plannerSource: routeSelection.plannerSource,
|
|
106
|
+
plannerUnavailableReason: routeSelection.plannerUnavailableReason ?? null
|
|
105
107
|
}
|
|
106
108
|
});
|
|
107
109
|
// 3. Execute the run task using the planned provider chain
|
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ export { type ConnectivityService, type DeviceConstraintsService, type SecureSto
|
|
|
2
2
|
export { type ProviderDescriptor, type ProviderDynamicCapabilities, type RunContext, type ProviderAdapter } from "./provider.js";
|
|
3
3
|
export { ProviderRegistry } from "./registry.js";
|
|
4
4
|
export { type RouteSelection, Router } from "./router.js";
|
|
5
|
-
export { type RoutePlanner, type SharedPlannerInput, type SharedPlannerRoutePlan, WasmRoutePlanner } from "./route-planner.js";
|
|
5
|
+
export { type RoutePlanner, type PlannerOutcome, type WasmUnavailableReason, type SharedPlannerInput, type SharedPlannerRoutePlan, WasmRoutePlanner } from "./route-planner.js";
|
|
6
6
|
export { type IndeRunExceptionParams, IndeRunException, createCapabilityMismatch, createOffline, createAuthError, createRateLimited, createTimeout, createUnavailable, createInternal, toIndeRunException } from "./errors.js";
|
|
7
7
|
export { IndeRun } from "./engine.js";
|
|
8
8
|
export { type TelemetryEventType, type TelemetryEvent, type TelemetryService, NoOpTelemetryService } from "./telemetry.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,mBAAmB,EACxB,KAAK,wBAAwB,EAC7B,KAAK,oBAAoB,EACzB,KAAK,YAAY,EACjB,KAAK,iBAAiB,EACtB,KAAK,WAAW,EAChB,KAAK,YAAY,EACjB,KAAK,YAAY,EACjB,KAAK,YAAY,EAClB,MAAM,WAAW,CAAC;AAEnB,OAAO,EACL,KAAK,kBAAkB,EACvB,KAAK,2BAA2B,EAChC,KAAK,UAAU,EACf,KAAK,eAAe,EACrB,MAAM,eAAe,CAAC;AAEvB,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEjD,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,kBAAkB,EACvB,KAAK,sBAAsB,EAC3B,gBAAgB,EACjB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EACL,KAAK,sBAAsB,EAC3B,gBAAgB,EAChB,wBAAwB,EACxB,aAAa,EACb,eAAe,EACf,iBAAiB,EACjB,aAAa,EACb,iBAAiB,EACjB,cAAc,EACd,kBAAkB,EACnB,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAEtC,OAAO,EACL,KAAK,kBAAkB,EACvB,KAAK,cAAc,EACnB,KAAK,gBAAgB,EACrB,oBAAoB,EACrB,MAAM,gBAAgB,CAAC;AAExB,OAAO,EACL,0BAA0B,EAC1B,eAAe,EACf,kBAAkB,EAClB,yBAAyB,EACzB,KAAK,gCAAgC,EACrC,KAAK,sBAAsB,EAC5B,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,gBAAgB,EAAE,KAAK,uBAAuB,EAAE,MAAM,cAAc,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,mBAAmB,EACxB,KAAK,wBAAwB,EAC7B,KAAK,oBAAoB,EACzB,KAAK,YAAY,EACjB,KAAK,iBAAiB,EACtB,KAAK,WAAW,EAChB,KAAK,YAAY,EACjB,KAAK,YAAY,EACjB,KAAK,YAAY,EAClB,MAAM,WAAW,CAAC;AAEnB,OAAO,EACL,KAAK,kBAAkB,EACvB,KAAK,2BAA2B,EAChC,KAAK,UAAU,EACf,KAAK,eAAe,EACrB,MAAM,eAAe,CAAC;AAEvB,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAEjD,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,cAAc,EACnB,KAAK,qBAAqB,EAC1B,KAAK,kBAAkB,EACvB,KAAK,sBAAsB,EAC3B,gBAAgB,EACjB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EACL,KAAK,sBAAsB,EAC3B,gBAAgB,EAChB,wBAAwB,EACxB,aAAa,EACb,eAAe,EACf,iBAAiB,EACjB,aAAa,EACb,iBAAiB,EACjB,cAAc,EACd,kBAAkB,EACnB,MAAM,aAAa,CAAC;AAErB,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAEtC,OAAO,EACL,KAAK,kBAAkB,EACvB,KAAK,cAAc,EACnB,KAAK,gBAAgB,EACrB,oBAAoB,EACrB,MAAM,gBAAgB,CAAC;AAExB,OAAO,EACL,0BAA0B,EAC1B,eAAe,EACf,kBAAkB,EAClB,yBAAyB,EACzB,KAAK,gCAAgC,EACrC,KAAK,sBAAsB,EAC5B,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,gBAAgB,EAAE,KAAK,uBAAuB,EAAE,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { type ModelPackage, type ModelSourceType, type TaskRequest, type TaskResult } from "@independo/inderun-contracts";
|
|
2
|
+
import type { HostServices } from "./host.js";
|
|
3
|
+
import { type OnnxTextGenerationRuntime } from "./onnx-runtime.js";
|
|
4
|
+
import type { ProviderAdapter, ProviderDescriptor, ProviderDynamicCapabilities, RunContext } from "./provider.js";
|
|
5
|
+
/** Default provider id of the Web member of the ONNX Runtime provider family. */
|
|
6
|
+
export declare const DEFAULT_ONNX_WEB_PROVIDER_ID = "local.onnx.genai.web";
|
|
7
|
+
/**
|
|
8
|
+
* Model source types the Web member of the ONNX Runtime provider family can honor.
|
|
9
|
+
*
|
|
10
|
+
* `filesystem` is unsupported because browsers do not expose arbitrary local paths, and `remote`
|
|
11
|
+
* is deferred: hosts download model files themselves and then supply them as `bundled`,
|
|
12
|
+
* `app_managed`, or `programmatic`.
|
|
13
|
+
*/
|
|
14
|
+
export declare const SUPPORTED_WEB_MODEL_SOURCE_TYPES: readonly ModelSourceType[];
|
|
15
|
+
/**
|
|
16
|
+
* Configuration for the Web ONNX Runtime provider.
|
|
17
|
+
*/
|
|
18
|
+
export interface OnnxProviderOptions {
|
|
19
|
+
/**
|
|
20
|
+
* Provider id exposed in telemetry and routing explanations.
|
|
21
|
+
*/
|
|
22
|
+
id?: string;
|
|
23
|
+
/**
|
|
24
|
+
* Provider-neutral bootstrap metadata describing the developer-supplied model.
|
|
25
|
+
*/
|
|
26
|
+
modelPackage: ModelPackage;
|
|
27
|
+
/**
|
|
28
|
+
* Runtime implementation used to execute generation. Defaults to the Transformers.js-backed
|
|
29
|
+
* runtime; applications can inject their own implementation of the seam.
|
|
30
|
+
*/
|
|
31
|
+
runtime?: OnnxTextGenerationRuntime;
|
|
32
|
+
/**
|
|
33
|
+
* Optional generation timeout in milliseconds. A request-level `constraints.timeoutMs` wins.
|
|
34
|
+
*/
|
|
35
|
+
timeoutMs?: number;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Local provider adapter that executes Mode-1 text-to-text requests with a developer-supplied ONNX
|
|
39
|
+
* model in the browser.
|
|
40
|
+
*
|
|
41
|
+
* The adapter owns the IndeRun-facing contract (descriptor, capability checks, error taxonomy) and
|
|
42
|
+
* delegates execution to an injectable {@link OnnxTextGenerationRuntime}. ONNX Runtime execution
|
|
43
|
+
* backends (WASM, WebGPU, WebNN) stay behind that seam and are never public routing concepts.
|
|
44
|
+
*/
|
|
45
|
+
export declare class OnnxRuntimeWebProvider implements ProviderAdapter {
|
|
46
|
+
private readonly options;
|
|
47
|
+
private readonly id;
|
|
48
|
+
private readonly modelPackage;
|
|
49
|
+
private readonly runtime;
|
|
50
|
+
/**
|
|
51
|
+
* Creates a Web ONNX Runtime provider.
|
|
52
|
+
* @param options - Model package plus optional id, runtime, and timeout overrides.
|
|
53
|
+
*/
|
|
54
|
+
constructor(options: OnnxProviderOptions);
|
|
55
|
+
/**
|
|
56
|
+
* Returns static metadata used by the router for deterministic provider selection.
|
|
57
|
+
*/
|
|
58
|
+
describe(): ProviderDescriptor;
|
|
59
|
+
/**
|
|
60
|
+
* Reports dynamic provider availability for the current host.
|
|
61
|
+
*
|
|
62
|
+
* Every failure below is provider-internal detail: the shared route planner flattens it into a
|
|
63
|
+
* single `capability_unavailable` rejection carrying the returned reason.
|
|
64
|
+
*
|
|
65
|
+
* @param _host - Host services available to the engine (unused; ONNX runs in-process).
|
|
66
|
+
*/
|
|
67
|
+
capabilities(_host: HostServices): Promise<ProviderDynamicCapabilities>;
|
|
68
|
+
/**
|
|
69
|
+
* Executes a normalized text-to-text task on the developer-supplied local ONNX model.
|
|
70
|
+
* @param request - Canonical IndeRun task request.
|
|
71
|
+
* @param context - Engine execution context containing run id and host services.
|
|
72
|
+
* @returns Normalized IndeRun task result.
|
|
73
|
+
* @throws IndeRunException mapped to the standard error taxonomy.
|
|
74
|
+
*/
|
|
75
|
+
run(request: TaskRequest, context: RunContext): Promise<TaskResult>;
|
|
76
|
+
private mapRuntimeError;
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=onnx-provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"onnx-provider.d.ts","sourceRoot":"","sources":["../src/onnx-provider.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,YAAY,EACjB,KAAK,eAAe,EACpB,KAAK,WAAW,EAChB,KAAK,UAAU,EAChB,MAAM,8BAA8B,CAAC;AAStC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AAC9C,OAAO,EAIL,KAAK,yBAAyB,EAC/B,MAAM,mBAAmB,CAAC;AAE3B,OAAO,KAAK,EACV,eAAe,EACf,kBAAkB,EAClB,2BAA2B,EAC3B,UAAU,EACX,MAAM,eAAe,CAAC;AAEvB,iFAAiF;AACjF,eAAO,MAAM,4BAA4B,yBAAyB,CAAC;AAKnE;;;;;;GAMG;AACH,eAAO,MAAM,gCAAgC,EAAE,SAAS,eAAe,EAKtE,CAAC;AAEF;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC;;OAEG;IACH,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ;;OAEG;IACH,YAAY,EAAE,YAAY,CAAC;IAC3B;;;OAGG;IACH,OAAO,CAAC,EAAE,yBAAyB,CAAC;IACpC;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;;;;;;GAOG;AACH,qBAAa,sBAAuB,YAAW,eAAe;IAShD,OAAO,CAAC,QAAQ,CAAC,OAAO;IARpC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAS;IAC5B,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAe;IAC5C,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA4B;IAEpD;;;OAGG;gBAC0B,OAAO,EAAE,mBAAmB;IAMzD;;OAEG;IACH,QAAQ,IAAI,kBAAkB;IAsB9B;;;;;;;OAOG;IACG,YAAY,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,2BAA2B,CAAC;IAoC7E;;;;;;OAMG;IACG,GAAG,CAAC,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;IAoEzE,OAAO,CAAC,eAAe;CA2BxB"}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { getModelPackageValidationIssues } from "@independo/inderun-contracts";
|
|
2
|
+
import { createCapabilityMismatch, createInternal, createTimeout, createUnavailable, toIndeRunException } from "./errors.js";
|
|
3
|
+
import { OnnxRuntimeError } from "./onnx-runtime.js";
|
|
4
|
+
import { createTransformersJsRuntime } from "./onnx-transformers-runtime.js";
|
|
5
|
+
/** Default provider id of the Web member of the ONNX Runtime provider family. */
|
|
6
|
+
export const DEFAULT_ONNX_WEB_PROVIDER_ID = "local.onnx.genai.web";
|
|
7
|
+
/** Task kind this provider implements in Milestone 2 (Mode 1 only). */
|
|
8
|
+
const TEXT_TO_TEXT = "text_to_text";
|
|
9
|
+
/**
|
|
10
|
+
* Model source types the Web member of the ONNX Runtime provider family can honor.
|
|
11
|
+
*
|
|
12
|
+
* `filesystem` is unsupported because browsers do not expose arbitrary local paths, and `remote`
|
|
13
|
+
* is deferred: hosts download model files themselves and then supply them as `bundled`,
|
|
14
|
+
* `app_managed`, or `programmatic`.
|
|
15
|
+
*/
|
|
16
|
+
export const SUPPORTED_WEB_MODEL_SOURCE_TYPES = [
|
|
17
|
+
"registry",
|
|
18
|
+
"bundled",
|
|
19
|
+
"programmatic",
|
|
20
|
+
"app_managed"
|
|
21
|
+
];
|
|
22
|
+
/**
|
|
23
|
+
* Local provider adapter that executes Mode-1 text-to-text requests with a developer-supplied ONNX
|
|
24
|
+
* model in the browser.
|
|
25
|
+
*
|
|
26
|
+
* The adapter owns the IndeRun-facing contract (descriptor, capability checks, error taxonomy) and
|
|
27
|
+
* delegates execution to an injectable {@link OnnxTextGenerationRuntime}. ONNX Runtime execution
|
|
28
|
+
* backends (WASM, WebGPU, WebNN) stay behind that seam and are never public routing concepts.
|
|
29
|
+
*/
|
|
30
|
+
export class OnnxRuntimeWebProvider {
|
|
31
|
+
options;
|
|
32
|
+
id;
|
|
33
|
+
modelPackage;
|
|
34
|
+
runtime;
|
|
35
|
+
/**
|
|
36
|
+
* Creates a Web ONNX Runtime provider.
|
|
37
|
+
* @param options - Model package plus optional id, runtime, and timeout overrides.
|
|
38
|
+
*/
|
|
39
|
+
constructor(options) {
|
|
40
|
+
this.options = options;
|
|
41
|
+
this.id = options.id ?? DEFAULT_ONNX_WEB_PROVIDER_ID;
|
|
42
|
+
this.modelPackage = options.modelPackage;
|
|
43
|
+
this.runtime = options.runtime ?? createTransformersJsRuntime();
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Returns static metadata used by the router for deterministic provider selection.
|
|
47
|
+
*/
|
|
48
|
+
describe() {
|
|
49
|
+
return {
|
|
50
|
+
id: this.id,
|
|
51
|
+
type: "local",
|
|
52
|
+
transport: "in_process",
|
|
53
|
+
supports: {
|
|
54
|
+
run: true,
|
|
55
|
+
streaming: false,
|
|
56
|
+
realtime: false,
|
|
57
|
+
tools: false,
|
|
58
|
+
reasoningEvents: false,
|
|
59
|
+
structuredOutput: false,
|
|
60
|
+
multimodal: false
|
|
61
|
+
},
|
|
62
|
+
cancel: "soft",
|
|
63
|
+
tasks: getDeclaredTasks(this.modelPackage),
|
|
64
|
+
privacy: {
|
|
65
|
+
dataLeavesDevice: false
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Reports dynamic provider availability for the current host.
|
|
71
|
+
*
|
|
72
|
+
* Every failure below is provider-internal detail: the shared route planner flattens it into a
|
|
73
|
+
* single `capability_unavailable` rejection carrying the returned reason.
|
|
74
|
+
*
|
|
75
|
+
* @param _host - Host services available to the engine (unused; ONNX runs in-process).
|
|
76
|
+
*/
|
|
77
|
+
async capabilities(_host) {
|
|
78
|
+
const issues = getModelPackageValidationIssues(this.modelPackage);
|
|
79
|
+
if (issues.length > 0) {
|
|
80
|
+
const summary = issues.map((issue) => `${issue.path} ${issue.message}`).join("; ");
|
|
81
|
+
return { available: false, reason: `model package malformed: ${summary}` };
|
|
82
|
+
}
|
|
83
|
+
const sourceType = this.modelPackage.source?.sourceType;
|
|
84
|
+
if (sourceType !== undefined && !SUPPORTED_WEB_MODEL_SOURCE_TYPES.includes(sourceType)) {
|
|
85
|
+
return {
|
|
86
|
+
available: false,
|
|
87
|
+
reason: sourceType === "filesystem"
|
|
88
|
+
? "model source unavailable: 'filesystem' model sources are unsupported on Web because browsers cannot read arbitrary local paths."
|
|
89
|
+
: `model source unavailable: '${sourceType}' model sources are deferred on Web; supply model files as ${SUPPORTED_WEB_MODEL_SOURCE_TYPES.join(", ")}.`
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
const platforms = this.modelPackage.runtime?.platforms;
|
|
93
|
+
if (Array.isArray(platforms) && platforms.length > 0 && !platforms.includes("web")) {
|
|
94
|
+
return {
|
|
95
|
+
available: false,
|
|
96
|
+
reason: `platform unsupported: the model package targets ${platforms.join(", ")} and does not list 'web'.`
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
if (!getDeclaredTasks(this.modelPackage).includes(TEXT_TO_TEXT)) {
|
|
100
|
+
return {
|
|
101
|
+
available: false,
|
|
102
|
+
reason: `unsupported task: the model package declares ${getDeclaredTasks(this.modelPackage).join(", ")} and does not support '${TEXT_TO_TEXT}'.`
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
return this.runtime.prepare(this.modelPackage);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Executes a normalized text-to-text task on the developer-supplied local ONNX model.
|
|
109
|
+
* @param request - Canonical IndeRun task request.
|
|
110
|
+
* @param context - Engine execution context containing run id and host services.
|
|
111
|
+
* @returns Normalized IndeRun task result.
|
|
112
|
+
* @throws IndeRunException mapped to the standard error taxonomy.
|
|
113
|
+
*/
|
|
114
|
+
async run(request, context) {
|
|
115
|
+
const availability = await this.capabilities(context.hostServices);
|
|
116
|
+
if (!availability.available) {
|
|
117
|
+
throw createCapabilityMismatch(availability.reason ?? "ONNX Runtime Web provider is unavailable on this host.", { providerId: this.id, runId: context.runId });
|
|
118
|
+
}
|
|
119
|
+
const messages = createMessages(request);
|
|
120
|
+
if (messages.length === 0) {
|
|
121
|
+
throw createInternal("ONNX Runtime Web provider requires a prompt or messages.", {
|
|
122
|
+
providerId: this.id,
|
|
123
|
+
runId: context.runId
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
const timeoutMs = request.constraints?.timeoutMs ?? this.options.timeoutMs;
|
|
127
|
+
const controller = new AbortController();
|
|
128
|
+
const timer = timeoutMs !== undefined && timeoutMs > 0
|
|
129
|
+
? setTimeout(() => controller.abort(), timeoutMs)
|
|
130
|
+
: undefined;
|
|
131
|
+
let output;
|
|
132
|
+
try {
|
|
133
|
+
output = await this.runtime.generate({
|
|
134
|
+
modelPackage: this.modelPackage,
|
|
135
|
+
messages,
|
|
136
|
+
...(request.generation !== undefined ? { generation: request.generation } : {})
|
|
137
|
+
}, controller.signal);
|
|
138
|
+
}
|
|
139
|
+
catch (err) {
|
|
140
|
+
throw this.mapRuntimeError(err, context);
|
|
141
|
+
}
|
|
142
|
+
finally {
|
|
143
|
+
if (timer !== undefined)
|
|
144
|
+
clearTimeout(timer);
|
|
145
|
+
}
|
|
146
|
+
if (typeof output.text !== "string" || output.text.length === 0) {
|
|
147
|
+
throw createInternal("ONNX Runtime Web provider returned no text output.", {
|
|
148
|
+
providerId: this.id,
|
|
149
|
+
runId: context.runId
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
const result = {
|
|
153
|
+
schemaVersion: "1.0",
|
|
154
|
+
runId: context.runId,
|
|
155
|
+
output: {
|
|
156
|
+
type: "text",
|
|
157
|
+
text: output.text
|
|
158
|
+
},
|
|
159
|
+
finishReason: output.finishReason ?? "stop",
|
|
160
|
+
telemetry: {
|
|
161
|
+
providerUsed: this.id,
|
|
162
|
+
totalMs: 0
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
if (output.usage) {
|
|
166
|
+
result.usage = output.usage;
|
|
167
|
+
}
|
|
168
|
+
return result;
|
|
169
|
+
}
|
|
170
|
+
mapRuntimeError(error, context) {
|
|
171
|
+
const extra = { providerId: this.id, runId: context.runId };
|
|
172
|
+
if (error instanceof OnnxRuntimeError) {
|
|
173
|
+
const details = error.originalError !== undefined
|
|
174
|
+
? { details: { originalError: getErrorSummary(error.originalError) } }
|
|
175
|
+
: {};
|
|
176
|
+
switch (error.kind) {
|
|
177
|
+
case "capability":
|
|
178
|
+
return createCapabilityMismatch(error.message, { ...extra, ...details });
|
|
179
|
+
case "unavailable":
|
|
180
|
+
return createUnavailable(error.message, { ...extra, ...details });
|
|
181
|
+
case "timeout":
|
|
182
|
+
return createTimeout(error.message, { ...extra, ...details });
|
|
183
|
+
default:
|
|
184
|
+
return createInternal(error.message, { ...extra, ...details });
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (isAbortError(error)) {
|
|
188
|
+
return createTimeout("ONNX Runtime Web generation timed out.", extra);
|
|
189
|
+
}
|
|
190
|
+
return toIndeRunException(error, extra);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
function getDeclaredTasks(modelPackage) {
|
|
194
|
+
const tasks = modelPackage.tasks;
|
|
195
|
+
return Array.isArray(tasks) && tasks.length > 0 ? tasks : [TEXT_TO_TEXT];
|
|
196
|
+
}
|
|
197
|
+
function createMessages(request) {
|
|
198
|
+
if (request.messages && request.messages.length > 0) {
|
|
199
|
+
return request.messages.map((message) => ({
|
|
200
|
+
role: message.role,
|
|
201
|
+
content: message.content
|
|
202
|
+
}));
|
|
203
|
+
}
|
|
204
|
+
const prompt = request.prompt;
|
|
205
|
+
return prompt !== undefined && prompt.length > 0 ? [{ role: "user", content: prompt }] : [];
|
|
206
|
+
}
|
|
207
|
+
function isAbortError(error) {
|
|
208
|
+
return (typeof error === "object" &&
|
|
209
|
+
error !== null &&
|
|
210
|
+
error.name === "AbortError");
|
|
211
|
+
}
|
|
212
|
+
function getErrorSummary(error) {
|
|
213
|
+
if (error instanceof Error) {
|
|
214
|
+
return { name: error.name, message: error.message, stack: error.stack };
|
|
215
|
+
}
|
|
216
|
+
return { message: String(error) };
|
|
217
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import type { ModelPackage, TaskRequest, TaskResult } from "@independo/inderun-contracts";
|
|
2
|
+
/**
|
|
3
|
+
* Availability snapshot reported by an ONNX text generation runtime.
|
|
4
|
+
*
|
|
5
|
+
* The `reason` string uses the provider-internal capability vocabulary documented in
|
|
6
|
+
* `docs/architecture/onnx-runtime-provider-family.md`. It is not a public enum: the provider
|
|
7
|
+
* flattens every failure into a single `capability_unavailable` route rejection and carries the
|
|
8
|
+
* reason as human-readable text.
|
|
9
|
+
*/
|
|
10
|
+
export interface OnnxRuntimeAvailability {
|
|
11
|
+
/** Whether the runtime can execute the given model package on this host right now. */
|
|
12
|
+
available: boolean;
|
|
13
|
+
/** Human-readable detail describing why the runtime is unavailable. */
|
|
14
|
+
reason?: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Normalized conversation turn handed to an ONNX text generation runtime.
|
|
18
|
+
*/
|
|
19
|
+
export interface OnnxGenerationMessage {
|
|
20
|
+
/** The role of the author of this turn. */
|
|
21
|
+
role: "system" | "user" | "assistant";
|
|
22
|
+
/** Plain-text content of this turn. */
|
|
23
|
+
content: string;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Normalized generation request handed to an ONNX text generation runtime.
|
|
27
|
+
*/
|
|
28
|
+
export interface OnnxGenerationInput {
|
|
29
|
+
/** The model package the provider was configured with. */
|
|
30
|
+
modelPackage: ModelPackage;
|
|
31
|
+
/** Conversation turns, normalized from `prompt` or `messages` by the provider. */
|
|
32
|
+
messages: OnnxGenerationMessage[];
|
|
33
|
+
/** Optional generation controls copied from the task request. */
|
|
34
|
+
generation?: NonNullable<TaskRequest["generation"]>;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Normalized generation result returned by an ONNX text generation runtime.
|
|
38
|
+
*/
|
|
39
|
+
export interface OnnxGenerationOutput {
|
|
40
|
+
/** The generated text. */
|
|
41
|
+
text: string;
|
|
42
|
+
/** Optional normalized finish reason; the provider defaults to `stop`. */
|
|
43
|
+
finishReason?: TaskResult["finishReason"];
|
|
44
|
+
/** Optional token accounting, when the runtime reports it. */
|
|
45
|
+
usage?: TaskResult["usage"];
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Failure category a runtime can signal so the provider maps it onto the IndeRun error taxonomy.
|
|
49
|
+
*
|
|
50
|
+
* - `capability`: model or runtime cannot serve this request (`CapabilityMismatch`).
|
|
51
|
+
* - `unavailable`: runtime initialization failure or resource exhaustion (`Unavailable`).
|
|
52
|
+
* - `timeout`: generation exceeded its budget or was aborted (`Timeout`).
|
|
53
|
+
* - `internal`: unexpected runtime failure (`Internal`).
|
|
54
|
+
*/
|
|
55
|
+
export type OnnxRuntimeErrorKind = "capability" | "unavailable" | "timeout" | "internal";
|
|
56
|
+
/**
|
|
57
|
+
* Error type ONNX runtime implementations throw to steer IndeRun error normalization.
|
|
58
|
+
*
|
|
59
|
+
* Anything else thrown by a runtime is normalized to `Internal`.
|
|
60
|
+
*/
|
|
61
|
+
export declare class OnnxRuntimeError extends Error {
|
|
62
|
+
/** Failure category used by the provider to select an IndeRun error class. */
|
|
63
|
+
readonly kind: OnnxRuntimeErrorKind;
|
|
64
|
+
/** Optional underlying error captured for telemetry details. */
|
|
65
|
+
readonly originalError?: unknown;
|
|
66
|
+
constructor(kind: OnnxRuntimeErrorKind, message: string, originalError?: unknown);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Injectable seam between the IndeRun ONNX Web provider and the actual on-device runtime.
|
|
70
|
+
*
|
|
71
|
+
* IndeRun ships a Transformers.js-backed implementation as the default and a deterministic
|
|
72
|
+
* fixture for tests, but applications can supply their own implementation (for example a
|
|
73
|
+
* hand-rolled `onnxruntime-web` pipeline) without touching the provider.
|
|
74
|
+
*/
|
|
75
|
+
export interface OnnxTextGenerationRuntime {
|
|
76
|
+
/**
|
|
77
|
+
* Checks runtime package availability, execution backend availability, and model readiness.
|
|
78
|
+
* Implementations must resolve with an availability snapshot rather than throwing.
|
|
79
|
+
*/
|
|
80
|
+
prepare(modelPackage: ModelPackage): Promise<OnnxRuntimeAvailability>;
|
|
81
|
+
/**
|
|
82
|
+
* Runs one Mode-1 generation. Implementations should honor `signal` where the underlying
|
|
83
|
+
* runtime allows it; ONNX inference is not hard-interruptible, so cancellation is best effort.
|
|
84
|
+
*/
|
|
85
|
+
generate(input: OnnxGenerationInput, signal?: AbortSignal): Promise<OnnxGenerationOutput>;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Configuration for the deterministic fixture runtime.
|
|
89
|
+
*/
|
|
90
|
+
export interface FixtureOnnxRuntimeOptions {
|
|
91
|
+
/** Availability snapshot returned by `prepare`. Defaults to `{ available: true }`. */
|
|
92
|
+
availability?: OnnxRuntimeAvailability;
|
|
93
|
+
/** Text or full output produced by `generate`. Defaults to a deterministic echo. */
|
|
94
|
+
respond?: (input: OnnxGenerationInput) => string | OnnxGenerationOutput;
|
|
95
|
+
/** Error thrown by `generate` instead of producing output. */
|
|
96
|
+
failWith?: unknown;
|
|
97
|
+
/** Artificial generation delay in milliseconds, used to exercise timeouts. */
|
|
98
|
+
delayMs?: number;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Creates a deterministic in-memory ONNX runtime.
|
|
102
|
+
*
|
|
103
|
+
* This is the test seam mandated by the ONNX Runtime provider family specification: it proves the
|
|
104
|
+
* model package contract, capability checks, routing, and error normalization without depending on
|
|
105
|
+
* a real ONNX runtime, and it lets demos exercise the on-device route offline.
|
|
106
|
+
*
|
|
107
|
+
* @param options - Scripted availability, response, failure, and delay behavior.
|
|
108
|
+
*/
|
|
109
|
+
export declare function createFixtureOnnxRuntime(options?: FixtureOnnxRuntimeOptions): OnnxTextGenerationRuntime;
|
|
110
|
+
//# sourceMappingURL=onnx-runtime.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"onnx-runtime.d.ts","sourceRoot":"","sources":["../src/onnx-runtime.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAE1F;;;;;;;GAOG;AACH,MAAM,WAAW,uBAAuB;IACtC,sFAAsF;IACtF,SAAS,EAAE,OAAO,CAAC;IACnB,uEAAuE;IACvE,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,2CAA2C;IAC3C,IAAI,EAAE,QAAQ,GAAG,MAAM,GAAG,WAAW,CAAC;IACtC,uCAAuC;IACvC,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,0DAA0D;IAC1D,YAAY,EAAE,YAAY,CAAC;IAC3B,kFAAkF;IAClF,QAAQ,EAAE,qBAAqB,EAAE,CAAC;IAClC,iEAAiE;IACjE,UAAU,CAAC,EAAE,WAAW,CAAC,WAAW,CAAC,YAAY,CAAC,CAAC,CAAC;CACrD;AAED;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC,0BAA0B;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,0EAA0E;IAC1E,YAAY,CAAC,EAAE,UAAU,CAAC,cAAc,CAAC,CAAC;IAC1C,8DAA8D;IAC9D,KAAK,CAAC,EAAE,UAAU,CAAC,OAAO,CAAC,CAAC;CAC7B;AAED;;;;;;;GAOG;AACH,MAAM,MAAM,oBAAoB,GAAG,YAAY,GAAG,aAAa,GAAG,SAAS,GAAG,UAAU,CAAC;AAEzF;;;;GAIG;AACH,qBAAa,gBAAiB,SAAQ,KAAK;IACzC,8EAA8E;IAC9E,QAAQ,CAAC,IAAI,EAAE,oBAAoB,CAAC;IACpC,gEAAgE;IAChE,QAAQ,CAAC,aAAa,CAAC,EAAE,OAAO,CAAC;gBAErB,IAAI,EAAE,oBAAoB,EAAE,OAAO,EAAE,MAAM,EAAE,aAAa,CAAC,EAAE,OAAO;CAQjF;AAED;;;;;;GAMG;AACH,MAAM,WAAW,yBAAyB;IACxC;;;OAGG;IACH,OAAO,CAAC,YAAY,EAAE,YAAY,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAAC;IACtE;;;OAGG;IACH,QAAQ,CAAC,KAAK,EAAE,mBAAmB,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAAC;CAC3F;AAED;;GAEG;AACH,MAAM,WAAW,yBAAyB;IACxC,sFAAsF;IACtF,YAAY,CAAC,EAAE,uBAAuB,CAAC;IACvC,oFAAoF;IACpF,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,mBAAmB,KAAK,MAAM,GAAG,oBAAoB,CAAC;IACxE,8DAA8D;IAC9D,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,8EAA8E;IAC9E,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;;GAQG;AACH,wBAAgB,wBAAwB,CACtC,OAAO,GAAE,yBAA8B,GACtC,yBAAyB,CA4B3B"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error type ONNX runtime implementations throw to steer IndeRun error normalization.
|
|
3
|
+
*
|
|
4
|
+
* Anything else thrown by a runtime is normalized to `Internal`.
|
|
5
|
+
*/
|
|
6
|
+
export class OnnxRuntimeError extends Error {
|
|
7
|
+
/** Failure category used by the provider to select an IndeRun error class. */
|
|
8
|
+
kind;
|
|
9
|
+
/** Optional underlying error captured for telemetry details. */
|
|
10
|
+
originalError;
|
|
11
|
+
constructor(kind, message, originalError) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.name = "OnnxRuntimeError";
|
|
14
|
+
this.kind = kind;
|
|
15
|
+
if (originalError !== undefined)
|
|
16
|
+
this.originalError = originalError;
|
|
17
|
+
Object.setPrototypeOf(this, OnnxRuntimeError.prototype);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Creates a deterministic in-memory ONNX runtime.
|
|
22
|
+
*
|
|
23
|
+
* This is the test seam mandated by the ONNX Runtime provider family specification: it proves the
|
|
24
|
+
* model package contract, capability checks, routing, and error normalization without depending on
|
|
25
|
+
* a real ONNX runtime, and it lets demos exercise the on-device route offline.
|
|
26
|
+
*
|
|
27
|
+
* @param options - Scripted availability, response, failure, and delay behavior.
|
|
28
|
+
*/
|
|
29
|
+
export function createFixtureOnnxRuntime(options = {}) {
|
|
30
|
+
const availability = options.availability ?? { available: true };
|
|
31
|
+
return {
|
|
32
|
+
async prepare() {
|
|
33
|
+
return availability;
|
|
34
|
+
},
|
|
35
|
+
async generate(input, signal) {
|
|
36
|
+
if (options.delayMs !== undefined && options.delayMs > 0) {
|
|
37
|
+
await delay(options.delayMs, signal);
|
|
38
|
+
}
|
|
39
|
+
throwIfAborted(signal);
|
|
40
|
+
if (options.failWith !== undefined) {
|
|
41
|
+
throw options.failWith;
|
|
42
|
+
}
|
|
43
|
+
const response = options.respond
|
|
44
|
+
? options.respond(input)
|
|
45
|
+
: createFixtureText(input.modelPackage, input.messages);
|
|
46
|
+
return typeof response === "string" ? { text: response } : response;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function createFixtureText(modelPackage, messages) {
|
|
51
|
+
const lastUserMessage = [...messages].reverse().find((message) => message.role === "user");
|
|
52
|
+
const prompt = lastUserMessage?.content ?? "";
|
|
53
|
+
return `[fixture:${modelPackage.id}] ${prompt}`;
|
|
54
|
+
}
|
|
55
|
+
function delay(ms, signal) {
|
|
56
|
+
return new Promise((resolve, reject) => {
|
|
57
|
+
const timer = setTimeout(() => {
|
|
58
|
+
signal?.removeEventListener("abort", onAbort);
|
|
59
|
+
resolve();
|
|
60
|
+
}, ms);
|
|
61
|
+
function onAbort() {
|
|
62
|
+
clearTimeout(timer);
|
|
63
|
+
reject(new OnnxRuntimeError("timeout", "Fixture ONNX generation was aborted."));
|
|
64
|
+
}
|
|
65
|
+
if (signal?.aborted) {
|
|
66
|
+
onAbort();
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
function throwIfAborted(signal) {
|
|
73
|
+
if (signal?.aborted) {
|
|
74
|
+
throw new OnnxRuntimeError("timeout", "Fixture ONNX generation was aborted.");
|
|
75
|
+
}
|
|
76
|
+
}
|