@independo/inderun-web 0.2.0-dev.6 → 0.2.0-dev.8

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 (78) hide show
  1. package/README.md +71 -1
  2. package/dist/core/browser-host.d.ts.map +1 -0
  3. package/dist/{engine.d.ts → core/engine.d.ts} +6 -0
  4. package/dist/core/engine.d.ts.map +1 -0
  5. package/dist/{engine.js → core/engine.js} +11 -0
  6. package/dist/core/errors.d.ts.map +1 -0
  7. package/dist/core/host.d.ts.map +1 -0
  8. package/dist/{provider.d.ts → core/provider.d.ts} +9 -0
  9. package/dist/core/provider.d.ts.map +1 -0
  10. package/dist/core/registry.d.ts.map +1 -0
  11. package/dist/core/route-planner.d.ts.map +1 -0
  12. package/dist/core/router.d.ts.map +1 -0
  13. package/dist/core/telemetry.d.ts.map +1 -0
  14. package/dist/index.d.ts +9 -9
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +9 -9
  17. package/dist/onnx.d.ts +3 -3
  18. package/dist/onnx.d.ts.map +1 -1
  19. package/dist/onnx.js +3 -3
  20. package/dist/openai.d.ts +1 -1
  21. package/dist/openai.d.ts.map +1 -1
  22. package/dist/openai.js +1 -1
  23. package/dist/{onnx-provider.d.ts → providers/onnx/provider.d.ts} +4 -4
  24. package/dist/providers/onnx/provider.d.ts.map +1 -0
  25. package/dist/{onnx-provider.js → providers/onnx/provider.js} +3 -3
  26. package/dist/{onnx-runtime.d.ts → providers/onnx/runtime.d.ts} +1 -1
  27. package/dist/providers/onnx/runtime.d.ts.map +1 -0
  28. package/dist/{onnx-transformers-runtime.d.ts → providers/onnx/transformers-runtime.d.ts} +2 -2
  29. package/dist/providers/onnx/transformers-runtime.d.ts.map +1 -0
  30. package/dist/{onnx-transformers-runtime.js → providers/onnx/transformers-runtime.js} +1 -1
  31. package/dist/{openai-provider.d.ts → providers/openai/provider.d.ts} +21 -3
  32. package/dist/providers/openai/provider.d.ts.map +1 -0
  33. package/dist/{openai-provider.js → providers/openai/provider.js} +42 -2
  34. package/dist/providers/system-model/chrome-runtime.d.ts +48 -0
  35. package/dist/providers/system-model/chrome-runtime.d.ts.map +1 -0
  36. package/dist/providers/system-model/chrome-runtime.js +137 -0
  37. package/dist/providers/system-model/provider.d.ts +67 -0
  38. package/dist/providers/system-model/provider.d.ts.map +1 -0
  39. package/dist/providers/system-model/provider.js +190 -0
  40. package/dist/providers/system-model/runtime.d.ts +110 -0
  41. package/dist/providers/system-model/runtime.d.ts.map +1 -0
  42. package/dist/providers/system-model/runtime.js +73 -0
  43. package/dist/system-model.d.ts +13 -0
  44. package/dist/system-model.d.ts.map +1 -0
  45. package/dist/system-model.js +12 -0
  46. package/dist/web-sdk.d.ts +14 -6
  47. package/dist/web-sdk.d.ts.map +1 -1
  48. package/dist/web-sdk.js +14 -9
  49. package/package.json +8 -4
  50. package/dist/browser-host.d.ts.map +0 -1
  51. package/dist/engine.d.ts.map +0 -1
  52. package/dist/errors.d.ts.map +0 -1
  53. package/dist/host.d.ts.map +0 -1
  54. package/dist/onnx-provider.d.ts.map +0 -1
  55. package/dist/onnx-runtime.d.ts.map +0 -1
  56. package/dist/onnx-transformers-runtime.d.ts.map +0 -1
  57. package/dist/openai-provider.d.ts.map +0 -1
  58. package/dist/provider.d.ts.map +0 -1
  59. package/dist/registry.d.ts.map +0 -1
  60. package/dist/route-planner.d.ts.map +0 -1
  61. package/dist/router.d.ts.map +0 -1
  62. package/dist/telemetry.d.ts.map +0 -1
  63. /package/dist/{browser-host.d.ts → core/browser-host.d.ts} +0 -0
  64. /package/dist/{browser-host.js → core/browser-host.js} +0 -0
  65. /package/dist/{errors.d.ts → core/errors.d.ts} +0 -0
  66. /package/dist/{errors.js → core/errors.js} +0 -0
  67. /package/dist/{host.d.ts → core/host.d.ts} +0 -0
  68. /package/dist/{host.js → core/host.js} +0 -0
  69. /package/dist/{provider.js → core/provider.js} +0 -0
  70. /package/dist/{registry.d.ts → core/registry.d.ts} +0 -0
  71. /package/dist/{registry.js → core/registry.js} +0 -0
  72. /package/dist/{route-planner.d.ts → core/route-planner.d.ts} +0 -0
  73. /package/dist/{route-planner.js → core/route-planner.js} +0 -0
  74. /package/dist/{router.d.ts → core/router.d.ts} +0 -0
  75. /package/dist/{router.js → core/router.js} +0 -0
  76. /package/dist/{telemetry.d.ts → core/telemetry.d.ts} +0 -0
  77. /package/dist/{telemetry.js → core/telemetry.js} +0 -0
  78. /package/dist/{onnx-runtime.js → providers/onnx/runtime.js} +0 -0
@@ -0,0 +1,137 @@
1
+ import { SystemModelRuntimeError } from "./runtime.js";
2
+ /**
3
+ * Creates the default Web system-model runtime, backed by Chrome's built-in Prompt API
4
+ * (`LanguageModel`).
5
+ *
6
+ * The browser owns model availability, download, and execution; this runtime never bundles or
7
+ * fetches a model itself. When `LanguageModel` is not defined (unsupported browser, disabled
8
+ * flag, or origin trial not enrolled) the runtime reports itself unavailable instead of throwing,
9
+ * so routing degrades to an explainable `capability_unavailable` rejection.
10
+ *
11
+ * @param options - Generation control overrides forwarded to the Prompt API.
12
+ */
13
+ export function createChromePromptApiRuntime(options = {}) {
14
+ const promptOptions = toPromptApiOptions(options.generation);
15
+ return {
16
+ async availability() {
17
+ const languageModel = getLanguageModel();
18
+ if (!languageModel) {
19
+ return {
20
+ kind: "api_missing",
21
+ reason: "system model API missing: 'LanguageModel' is not defined on this browser. See https://developer.chrome.com/docs/ai/prompt-api."
22
+ };
23
+ }
24
+ let result;
25
+ try {
26
+ result = await languageModel.availability(promptOptions);
27
+ }
28
+ catch (err) {
29
+ return mapAvailabilityError(err);
30
+ }
31
+ switch (result) {
32
+ case "available":
33
+ return { kind: "available" };
34
+ case "downloadable":
35
+ return {
36
+ kind: "downloadable",
37
+ reason: "model downloadable: the model can be downloaded but is not installed yet."
38
+ };
39
+ case "downloading":
40
+ return {
41
+ kind: "downloading",
42
+ reason: "model downloading: the model is currently downloading."
43
+ };
44
+ case "unavailable":
45
+ return {
46
+ kind: "model_unavailable",
47
+ reason: "model unavailable: the model is unavailable on this device."
48
+ };
49
+ default:
50
+ return {
51
+ kind: "unavailable",
52
+ reason: `provider temporarily unavailable: unrecognized availability result '${result}'.`
53
+ };
54
+ }
55
+ },
56
+ async generate(input, signal) {
57
+ const languageModel = getLanguageModel();
58
+ if (!languageModel) {
59
+ throw new SystemModelRuntimeError("capability", "system model API missing: 'LanguageModel' is not defined on this browser.");
60
+ }
61
+ let session;
62
+ try {
63
+ session = await languageModel.create({ ...promptOptions, signal });
64
+ }
65
+ catch (err) {
66
+ throw mapGenerateError(err);
67
+ }
68
+ try {
69
+ const text = await session.prompt(toPromptText(input), signal !== undefined ? { signal } : {});
70
+ return { text };
71
+ }
72
+ catch (err) {
73
+ throw mapGenerateError(err);
74
+ }
75
+ finally {
76
+ session.destroy();
77
+ }
78
+ }
79
+ };
80
+ }
81
+ function getLanguageModel() {
82
+ return globalThis.LanguageModel;
83
+ }
84
+ function toPromptApiOptions(generation) {
85
+ const promptOptions = {};
86
+ if (generation?.temperature !== undefined)
87
+ promptOptions.temperature = generation.temperature;
88
+ if (generation?.topK !== undefined)
89
+ promptOptions.topK = generation.topK;
90
+ return promptOptions;
91
+ }
92
+ function toPromptText(input) {
93
+ return input.messages.map((message) => `${message.role}: ${message.content}`).join("\n");
94
+ }
95
+ function mapAvailabilityError(error) {
96
+ const name = getDomExceptionName(error);
97
+ if (name === "NotSupportedError") {
98
+ return {
99
+ kind: "browser_unsupported",
100
+ reason: `browser unsupported: ${getErrorMessage(error)}`
101
+ };
102
+ }
103
+ if (name === "NotAllowedError" || name === "SecurityError") {
104
+ return {
105
+ kind: "feature_disabled",
106
+ reason: `browser feature disabled: ${getErrorMessage(error)}`
107
+ };
108
+ }
109
+ return {
110
+ kind: "unavailable",
111
+ reason: `provider temporarily unavailable: ${getErrorMessage(error)}`
112
+ };
113
+ }
114
+ function mapGenerateError(error) {
115
+ if (error instanceof SystemModelRuntimeError)
116
+ return error;
117
+ const name = getDomExceptionName(error);
118
+ switch (name) {
119
+ case "NotSupportedError":
120
+ return new SystemModelRuntimeError("capability", `hardware unsupported: ${getErrorMessage(error)}`, error);
121
+ case "NotAllowedError":
122
+ case "SecurityError":
123
+ return new SystemModelRuntimeError("capability", `browser feature disabled: ${getErrorMessage(error)}`, error);
124
+ case "QuotaExceededError":
125
+ return new SystemModelRuntimeError("unavailable", `storage or network constraints: ${getErrorMessage(error)}`, error);
126
+ case "AbortError":
127
+ return new SystemModelRuntimeError("timeout", `system model generation was aborted: ${getErrorMessage(error)}`, error);
128
+ default:
129
+ return new SystemModelRuntimeError("internal", `system model generation failed: ${getErrorMessage(error)}`, error);
130
+ }
131
+ }
132
+ function getDomExceptionName(error) {
133
+ return error instanceof DOMException ? error.name : undefined;
134
+ }
135
+ function getErrorMessage(error) {
136
+ return error instanceof Error ? error.message : String(error);
137
+ }
@@ -0,0 +1,67 @@
1
+ import type { TaskRequest, TaskResult } from "@independo/inderun-contracts";
2
+ import type { HostServices } from "../../core/host.js";
3
+ import { type SystemModelRuntime } from "./runtime.js";
4
+ import type { ProviderAdapter, ProviderDescriptor, ProviderDynamicCapabilities, RunContext } from "../../core/provider.js";
5
+ /** Default provider id of the Web member of the system-model provider family. */
6
+ export declare const DEFAULT_SYSTEM_MODEL_WEB_PROVIDER_ID = "local.system-model.web";
7
+ /**
8
+ * Configuration for the Web system-model provider.
9
+ */
10
+ export interface SystemModelProviderOptions {
11
+ /**
12
+ * Provider id exposed in telemetry and routing explanations.
13
+ */
14
+ id?: string;
15
+ /**
16
+ * Runtime implementation used to check availability and execute generation. Defaults to
17
+ * `createChromePromptApiRuntime()`; pass `createFixtureSystemModelRuntime()` from
18
+ * `system-model.js` for tests and offline demos.
19
+ */
20
+ runtime?: SystemModelRuntime;
21
+ /**
22
+ * Optional generation timeout in milliseconds. A request-level `constraints.timeoutMs` wins.
23
+ */
24
+ timeoutMs?: number;
25
+ }
26
+ /**
27
+ * Local provider adapter that executes Mode-1 text-to-text requests against a browser-managed
28
+ * on-device model (for example Chrome's Prompt API / Gemini Nano).
29
+ *
30
+ * Unlike the ONNX Runtime provider family, this provider takes no developer-supplied model: the
31
+ * browser owns model availability, download, and execution. The adapter owns the IndeRun-facing
32
+ * contract (descriptor, capability checks, error taxonomy) and delegates everything else to an
33
+ * injectable {@link SystemModelRuntime}.
34
+ */
35
+ export declare class SystemModelWebProvider implements ProviderAdapter {
36
+ private readonly options;
37
+ private readonly id;
38
+ private readonly runtime;
39
+ /**
40
+ * Creates a Web system-model provider.
41
+ * @param options - Optional id, runtime, and timeout overrides.
42
+ */
43
+ constructor(options?: SystemModelProviderOptions);
44
+ /**
45
+ * Returns static metadata used by the router for deterministic provider selection.
46
+ */
47
+ describe(): ProviderDescriptor;
48
+ /**
49
+ * Reports dynamic provider availability for the current host.
50
+ *
51
+ * Every failure below is provider-internal detail: the shared route planner flattens it into a
52
+ * single `capability_unavailable` rejection carrying the returned reason.
53
+ *
54
+ * @param _host - Host services available to the engine (unused; the browser owns execution).
55
+ */
56
+ capabilities(_host: HostServices): Promise<ProviderDynamicCapabilities>;
57
+ /**
58
+ * Executes a normalized text-to-text task against the browser-managed on-device model.
59
+ * @param request - Canonical IndeRun task request.
60
+ * @param context - Engine execution context containing run id and host services.
61
+ * @returns Normalized IndeRun task result.
62
+ * @throws IndeRunException mapped to the standard error taxonomy.
63
+ */
64
+ run(request: TaskRequest, context: RunContext): Promise<TaskResult>;
65
+ private mapRuntimeError;
66
+ }
67
+ //# sourceMappingURL=provider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../../../src/providers/system-model/provider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAS5E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAEvD,OAAO,EAIL,KAAK,kBAAkB,EACxB,MAAM,cAAc,CAAC;AACtB,OAAO,KAAK,EACV,eAAe,EACf,kBAAkB,EAClB,2BAA2B,EAC3B,UAAU,EACX,MAAM,wBAAwB,CAAC;AAEhC,iFAAiF;AACjF,eAAO,MAAM,oCAAoC,2BAA2B,CAAC;AAK7E;;GAEG;AACH,MAAM,WAAW,0BAA0B;IACzC;;OAEG;IACH,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ;;;;OAIG;IACH,OAAO,CAAC,EAAE,kBAAkB,CAAC;IAC7B;;OAEG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;;;;;;;GAQG;AACH,qBAAa,sBAAuB,YAAW,eAAe;IAQhD,OAAO,CAAC,QAAQ,CAAC,OAAO;IAPpC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAS;IAC5B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAqB;IAE7C;;;OAGG;gBAC0B,OAAO,GAAE,0BAA+B;IAKrE;;OAEG;IACH,QAAQ,IAAI,kBAAkB;IAsB9B;;;;;;;OAOG;IACG,YAAY,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,2BAA2B,CAAC;IAK7E;;;;;;OAMG;IACG,GAAG,CAAC,OAAO,EAAE,WAAW,EAAE,OAAO,EAAE,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;IA8DzE,OAAO,CAAC,eAAe;CA2BxB"}
@@ -0,0 +1,190 @@
1
+ import { createCapabilityMismatch, createInternal, createTimeout, createUnavailable, toIndeRunException } from "../../core/errors.js";
2
+ import { createChromePromptApiRuntime } from "./chrome-runtime.js";
3
+ import { SystemModelRuntimeError } from "./runtime.js";
4
+ /** Default provider id of the Web member of the system-model provider family. */
5
+ export const DEFAULT_SYSTEM_MODEL_WEB_PROVIDER_ID = "local.system-model.web";
6
+ /** Task kind this provider implements in Milestone 2 (Mode 1 only). */
7
+ const TEXT_TO_TEXT = "text_to_text";
8
+ /**
9
+ * Local provider adapter that executes Mode-1 text-to-text requests against a browser-managed
10
+ * on-device model (for example Chrome's Prompt API / Gemini Nano).
11
+ *
12
+ * Unlike the ONNX Runtime provider family, this provider takes no developer-supplied model: the
13
+ * browser owns model availability, download, and execution. The adapter owns the IndeRun-facing
14
+ * contract (descriptor, capability checks, error taxonomy) and delegates everything else to an
15
+ * injectable {@link SystemModelRuntime}.
16
+ */
17
+ export class SystemModelWebProvider {
18
+ options;
19
+ id;
20
+ runtime;
21
+ /**
22
+ * Creates a Web system-model provider.
23
+ * @param options - Optional id, runtime, and timeout overrides.
24
+ */
25
+ constructor(options = {}) {
26
+ this.options = options;
27
+ this.id = options.id ?? DEFAULT_SYSTEM_MODEL_WEB_PROVIDER_ID;
28
+ this.runtime = options.runtime ?? createChromePromptApiRuntime();
29
+ }
30
+ /**
31
+ * Returns static metadata used by the router for deterministic provider selection.
32
+ */
33
+ describe() {
34
+ return {
35
+ id: this.id,
36
+ type: "local",
37
+ transport: "system_service",
38
+ supports: {
39
+ run: true,
40
+ streaming: false,
41
+ realtime: false,
42
+ tools: false,
43
+ reasoningEvents: false,
44
+ structuredOutput: false,
45
+ multimodal: false
46
+ },
47
+ cancel: "soft",
48
+ tasks: [TEXT_TO_TEXT],
49
+ privacy: {
50
+ dataLeavesDevice: false
51
+ }
52
+ };
53
+ }
54
+ /**
55
+ * Reports dynamic provider availability for the current host.
56
+ *
57
+ * Every failure below is provider-internal detail: the shared route planner flattens it into a
58
+ * single `capability_unavailable` rejection carrying the returned reason.
59
+ *
60
+ * @param _host - Host services available to the engine (unused; the browser owns execution).
61
+ */
62
+ async capabilities(_host) {
63
+ const availability = await this.runtime.availability();
64
+ return toDynamicCapabilities(availability);
65
+ }
66
+ /**
67
+ * Executes a normalized text-to-text task against the browser-managed on-device model.
68
+ * @param request - Canonical IndeRun task request.
69
+ * @param context - Engine execution context containing run id and host services.
70
+ * @returns Normalized IndeRun task result.
71
+ * @throws IndeRunException mapped to the standard error taxonomy.
72
+ */
73
+ async run(request, context) {
74
+ const availability = await this.runtime.availability();
75
+ const capabilities = toDynamicCapabilities(availability);
76
+ if (!capabilities.available) {
77
+ throw createCapabilityMismatch(capabilities.reason ?? "Web system-model provider is unavailable on this host.", { providerId: this.id, runId: context.runId });
78
+ }
79
+ const messages = createMessages(request);
80
+ if (messages.length === 0) {
81
+ throw createInternal("Web system-model provider requires a prompt or messages.", {
82
+ providerId: this.id,
83
+ runId: context.runId
84
+ });
85
+ }
86
+ const timeoutMs = request.constraints?.timeoutMs ?? this.options.timeoutMs;
87
+ const controller = new AbortController();
88
+ const timer = timeoutMs !== undefined && timeoutMs > 0
89
+ ? setTimeout(() => controller.abort(), timeoutMs)
90
+ : undefined;
91
+ let output;
92
+ try {
93
+ output = await this.runtime.generate({
94
+ messages,
95
+ ...(request.generation !== undefined ? { generation: request.generation } : {})
96
+ }, controller.signal);
97
+ }
98
+ catch (err) {
99
+ throw this.mapRuntimeError(err, context);
100
+ }
101
+ finally {
102
+ if (timer !== undefined)
103
+ clearTimeout(timer);
104
+ }
105
+ if (typeof output.text !== "string" || output.text.length === 0) {
106
+ throw createInternal("Web system-model provider returned no text output.", {
107
+ providerId: this.id,
108
+ runId: context.runId
109
+ });
110
+ }
111
+ return {
112
+ schemaVersion: "1.0",
113
+ runId: context.runId,
114
+ output: {
115
+ type: "text",
116
+ text: output.text
117
+ },
118
+ finishReason: output.finishReason ?? "stop",
119
+ telemetry: {
120
+ providerUsed: this.id,
121
+ totalMs: 0
122
+ }
123
+ };
124
+ }
125
+ mapRuntimeError(error, context) {
126
+ const extra = { providerId: this.id, runId: context.runId };
127
+ if (error instanceof SystemModelRuntimeError) {
128
+ const details = error.originalError !== undefined
129
+ ? { details: { originalError: getErrorSummary(error.originalError) } }
130
+ : {};
131
+ switch (error.kind) {
132
+ case "capability":
133
+ return createCapabilityMismatch(error.message, { ...extra, ...details });
134
+ case "unavailable":
135
+ return createUnavailable(error.message, { ...extra, ...details });
136
+ case "timeout":
137
+ return createTimeout(error.message, { ...extra, ...details });
138
+ default:
139
+ return createInternal(error.message, { ...extra, ...details });
140
+ }
141
+ }
142
+ if (isAbortError(error)) {
143
+ return createTimeout("Web system-model generation timed out.", extra);
144
+ }
145
+ return toIndeRunException(error, extra);
146
+ }
147
+ }
148
+ const REASON_PREFIX = {
149
+ available: undefined,
150
+ downloadable: "model downloadable",
151
+ downloading: "model downloading",
152
+ model_unavailable: "model unavailable",
153
+ api_missing: "system model API missing",
154
+ browser_unsupported: "browser unsupported",
155
+ feature_disabled: "browser feature disabled",
156
+ hardware_unsupported: "hardware unsupported",
157
+ resource_constrained: "storage or network constraints",
158
+ unavailable: "provider temporarily unavailable"
159
+ };
160
+ function toDynamicCapabilities(availability) {
161
+ if (availability.kind === "available") {
162
+ return { available: true };
163
+ }
164
+ const prefix = REASON_PREFIX[availability.kind];
165
+ const reason = availability.reason
166
+ ? `${prefix}: ${availability.reason}`
167
+ : `${prefix}: the Web system-model provider is unavailable.`;
168
+ return { available: false, reason };
169
+ }
170
+ function createMessages(request) {
171
+ if (request.messages && request.messages.length > 0) {
172
+ return request.messages.map((message) => ({
173
+ role: message.role,
174
+ content: message.content
175
+ }));
176
+ }
177
+ const prompt = request.prompt;
178
+ return prompt !== undefined && prompt.length > 0 ? [{ role: "user", content: prompt }] : [];
179
+ }
180
+ function isAbortError(error) {
181
+ return (typeof error === "object" &&
182
+ error !== null &&
183
+ error.name === "AbortError");
184
+ }
185
+ function getErrorSummary(error) {
186
+ if (error instanceof Error) {
187
+ return { name: error.name, message: error.message, stack: error.stack };
188
+ }
189
+ return { message: String(error) };
190
+ }
@@ -0,0 +1,110 @@
1
+ import type { TaskRequest, TaskResult } from "@independo/inderun-contracts";
2
+ /**
3
+ * Capability state reported by a system-model runtime.
4
+ *
5
+ * The `reason` string uses the provider-internal capability vocabulary documented in
6
+ * `docs/architecture/web-system-model-provider-family.md`. It is not a public enum: the provider
7
+ * flattens every non-`available` kind into a single `capability_unavailable` route rejection and
8
+ * carries the reason as human-readable text.
9
+ */
10
+ export type SystemModelAvailabilityKind = "available" | "downloadable" | "downloading" | "model_unavailable" | "api_missing" | "browser_unsupported" | "feature_disabled" | "hardware_unsupported" | "resource_constrained" | "unavailable";
11
+ /**
12
+ * Availability snapshot reported by a system-model runtime.
13
+ */
14
+ export interface SystemModelAvailability {
15
+ /** The specific availability state the runtime observed. */
16
+ kind: SystemModelAvailabilityKind;
17
+ /** Human-readable detail describing the state; expected for every non-`available` kind. */
18
+ reason?: string;
19
+ }
20
+ /**
21
+ * Normalized conversation turn handed to a system-model runtime.
22
+ */
23
+ export interface SystemModelPromptMessage {
24
+ /** The role of the author of this turn. */
25
+ role: "system" | "user" | "assistant";
26
+ /** Plain-text content of this turn. */
27
+ content: string;
28
+ }
29
+ /**
30
+ * Normalized generation request handed to a system-model runtime.
31
+ */
32
+ export interface SystemModelGenerationInput {
33
+ /** Conversation turns, normalized from `prompt` or `messages` by the provider. */
34
+ messages: SystemModelPromptMessage[];
35
+ /** Optional generation controls copied from the task request. */
36
+ generation?: NonNullable<TaskRequest["generation"]>;
37
+ }
38
+ /**
39
+ * Normalized generation result returned by a system-model runtime.
40
+ */
41
+ export interface SystemModelGenerationOutput {
42
+ /** The generated text. */
43
+ text: string;
44
+ /** Optional normalized finish reason; the provider defaults to `stop`. */
45
+ finishReason?: TaskResult["finishReason"];
46
+ }
47
+ /**
48
+ * Failure category a runtime can signal so the provider maps it onto the IndeRun error taxonomy.
49
+ *
50
+ * - `capability`: the browser or hardware cannot serve this request (`CapabilityMismatch`).
51
+ * - `unavailable`: resource exhaustion or transient failure (`Unavailable`).
52
+ * - `timeout`: generation exceeded its budget or was aborted (`Timeout`).
53
+ * - `internal`: unexpected runtime failure (`Internal`).
54
+ */
55
+ export type SystemModelRuntimeErrorKind = "capability" | "unavailable" | "timeout" | "internal";
56
+ /**
57
+ * Error type system-model 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 SystemModelRuntimeError extends Error {
62
+ /** Failure category used by the provider to select an IndeRun error class. */
63
+ readonly kind: SystemModelRuntimeErrorKind;
64
+ /** Optional underlying error captured for telemetry details. */
65
+ readonly originalError?: unknown;
66
+ constructor(kind: SystemModelRuntimeErrorKind, message: string, originalError?: unknown);
67
+ }
68
+ /**
69
+ * Injectable seam between the IndeRun Web system-model provider and the browser's on-device model
70
+ * API.
71
+ *
72
+ * IndeRun ships a Chrome Prompt API-backed implementation as the default and a deterministic
73
+ * fixture for tests. The seam exposes only Mode-1 generation (no session object, no streaming
74
+ * method) so streaming/session support cannot leak in even as unused scaffolding.
75
+ */
76
+ export interface SystemModelRuntime {
77
+ /**
78
+ * Checks browser API presence and model readiness. Implementations must resolve with an
79
+ * availability snapshot rather than throwing.
80
+ */
81
+ availability(): Promise<SystemModelAvailability>;
82
+ /**
83
+ * Runs one Mode-1 generation. Implementations should honor `signal` where the underlying API
84
+ * allows it.
85
+ */
86
+ generate(input: SystemModelGenerationInput, signal?: AbortSignal): Promise<SystemModelGenerationOutput>;
87
+ }
88
+ /**
89
+ * Configuration for the deterministic fixture runtime.
90
+ */
91
+ export interface FixtureSystemModelRuntimeOptions {
92
+ /** Availability snapshot returned by `availability`. Defaults to `{ kind: "available" }`. */
93
+ availability?: SystemModelAvailability;
94
+ /** Text or full output produced by `generate`. Defaults to a deterministic echo. */
95
+ respond?: (input: SystemModelGenerationInput) => string | SystemModelGenerationOutput;
96
+ /** Error thrown by `generate` instead of producing output. */
97
+ failWith?: unknown;
98
+ /** Artificial generation delay in milliseconds, used to exercise timeouts. */
99
+ delayMs?: number;
100
+ }
101
+ /**
102
+ * Creates a deterministic in-memory system-model runtime.
103
+ *
104
+ * This is the test seam for the Web system-model provider family: it proves capability checks,
105
+ * routing, and error normalization without depending on a real browser Prompt API implementation.
106
+ *
107
+ * @param options - Scripted availability, response, failure, and delay behavior.
108
+ */
109
+ export declare function createFixtureSystemModelRuntime(options?: FixtureSystemModelRuntimeOptions): SystemModelRuntime;
110
+ //# sourceMappingURL=runtime.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runtime.d.ts","sourceRoot":"","sources":["../../../src/providers/system-model/runtime.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAC;AAE5E;;;;;;;GAOG;AACH,MAAM,MAAM,2BAA2B,GACnC,WAAW,GACX,cAAc,GACd,aAAa,GACb,mBAAmB,GACnB,aAAa,GACb,qBAAqB,GACrB,kBAAkB,GAClB,sBAAsB,GACtB,sBAAsB,GACtB,aAAa,CAAC;AAElB;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC,4DAA4D;IAC5D,IAAI,EAAE,2BAA2B,CAAC;IAClC,2FAA2F;IAC3F,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,wBAAwB;IACvC,2CAA2C;IAC3C,IAAI,EAAE,QAAQ,GAAG,MAAM,GAAG,WAAW,CAAC;IACtC,uCAAuC;IACvC,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,0BAA0B;IACzC,kFAAkF;IAClF,QAAQ,EAAE,wBAAwB,EAAE,CAAC;IACrC,iEAAiE;IACjE,UAAU,CAAC,EAAE,WAAW,CAAC,WAAW,CAAC,YAAY,CAAC,CAAC,CAAC;CACrD;AAED;;GAEG;AACH,MAAM,WAAW,2BAA2B;IAC1C,0BAA0B;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,0EAA0E;IAC1E,YAAY,CAAC,EAAE,UAAU,CAAC,cAAc,CAAC,CAAC;CAC3C;AAED;;;;;;;GAOG;AACH,MAAM,MAAM,2BAA2B,GAAG,YAAY,GAAG,aAAa,GAAG,SAAS,GAAG,UAAU,CAAC;AAEhG;;;;GAIG;AACH,qBAAa,uBAAwB,SAAQ,KAAK;IAChD,8EAA8E;IAC9E,QAAQ,CAAC,IAAI,EAAE,2BAA2B,CAAC;IAC3C,gEAAgE;IAChE,QAAQ,CAAC,aAAa,CAAC,EAAE,OAAO,CAAC;gBAErB,IAAI,EAAE,2BAA2B,EAAE,OAAO,EAAE,MAAM,EAAE,aAAa,CAAC,EAAE,OAAO;CAQxF;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,kBAAkB;IACjC;;;OAGG;IACH,YAAY,IAAI,OAAO,CAAC,uBAAuB,CAAC,CAAC;IACjD;;;OAGG;IACH,QAAQ,CACN,KAAK,EAAE,0BAA0B,EACjC,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,2BAA2B,CAAC,CAAC;CACzC;AAED;;GAEG;AACH,MAAM,WAAW,gCAAgC;IAC/C,6FAA6F;IAC7F,YAAY,CAAC,EAAE,uBAAuB,CAAC;IACvC,oFAAoF;IACpF,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,0BAA0B,KAAK,MAAM,GAAG,2BAA2B,CAAC;IACtF,8DAA8D;IAC9D,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,8EAA8E;IAC9E,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;GAOG;AACH,wBAAgB,+BAA+B,CAC7C,OAAO,GAAE,gCAAqC,GAC7C,kBAAkB,CA0BpB"}
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Error type system-model runtime implementations throw to steer IndeRun error normalization.
3
+ *
4
+ * Anything else thrown by a runtime is normalized to `Internal`.
5
+ */
6
+ export class SystemModelRuntimeError 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 = "SystemModelRuntimeError";
14
+ this.kind = kind;
15
+ if (originalError !== undefined)
16
+ this.originalError = originalError;
17
+ Object.setPrototypeOf(this, SystemModelRuntimeError.prototype);
18
+ }
19
+ }
20
+ /**
21
+ * Creates a deterministic in-memory system-model runtime.
22
+ *
23
+ * This is the test seam for the Web system-model provider family: it proves capability checks,
24
+ * routing, and error normalization without depending on a real browser Prompt API implementation.
25
+ *
26
+ * @param options - Scripted availability, response, failure, and delay behavior.
27
+ */
28
+ export function createFixtureSystemModelRuntime(options = {}) {
29
+ const availability = options.availability ?? { kind: "available" };
30
+ return {
31
+ async availability() {
32
+ return availability;
33
+ },
34
+ async generate(input, signal) {
35
+ if (options.delayMs !== undefined && options.delayMs > 0) {
36
+ await delay(options.delayMs, signal);
37
+ }
38
+ throwIfAborted(signal);
39
+ if (options.failWith !== undefined) {
40
+ throw options.failWith;
41
+ }
42
+ const response = options.respond ? options.respond(input) : createFixtureText(input.messages);
43
+ return typeof response === "string" ? { text: response } : response;
44
+ }
45
+ };
46
+ }
47
+ function createFixtureText(messages) {
48
+ const lastUserMessage = [...messages].reverse().find((message) => message.role === "user");
49
+ const prompt = lastUserMessage?.content ?? "";
50
+ return `[fixture:system-model] ${prompt}`;
51
+ }
52
+ function delay(ms, signal) {
53
+ return new Promise((resolve, reject) => {
54
+ const timer = setTimeout(() => {
55
+ signal?.removeEventListener("abort", onAbort);
56
+ resolve();
57
+ }, ms);
58
+ function onAbort() {
59
+ clearTimeout(timer);
60
+ reject(new SystemModelRuntimeError("timeout", "Fixture system-model generation was aborted."));
61
+ }
62
+ if (signal?.aborted) {
63
+ onAbort();
64
+ return;
65
+ }
66
+ signal?.addEventListener("abort", onAbort, { once: true });
67
+ });
68
+ }
69
+ function throwIfAborted(signal) {
70
+ if (signal?.aborted) {
71
+ throw new SystemModelRuntimeError("timeout", "Fixture system-model generation was aborted.");
72
+ }
73
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Provider-specific entry point for the Web member of the system-model provider family.
3
+ *
4
+ * Kept out of the unified SDK index (`@independo/inderun-web`) so the top-level
5
+ * surface stays provider-agnostic. Import from `@independo/inderun-web/system-model`
6
+ * when registering the adapter manually or when implementing a custom
7
+ * `SystemModelRuntime`; most browser apps should pass `systemModel` to
8
+ * `createIndeRunWeb` from the main entry point instead.
9
+ */
10
+ export { DEFAULT_SYSTEM_MODEL_WEB_PROVIDER_ID, SystemModelWebProvider, type SystemModelProviderOptions } from "./providers/system-model/provider.js";
11
+ export { SystemModelRuntimeError, createFixtureSystemModelRuntime, type FixtureSystemModelRuntimeOptions, type SystemModelAvailability, type SystemModelAvailabilityKind, type SystemModelGenerationInput, type SystemModelGenerationOutput, type SystemModelPromptMessage, type SystemModelRuntime, type SystemModelRuntimeErrorKind } from "./providers/system-model/runtime.js";
12
+ export { createChromePromptApiRuntime, type ChromePromptApiGenerationOptions, type ChromePromptApiRuntimeOptions, type PromptApiLanguageModel, type PromptApiSession } from "./providers/system-model/chrome-runtime.js";
13
+ //# sourceMappingURL=system-model.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"system-model.d.ts","sourceRoot":"","sources":["../src/system-model.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,EACL,oCAAoC,EACpC,sBAAsB,EACtB,KAAK,0BAA0B,EAChC,MAAM,sCAAsC,CAAC;AAE9C,OAAO,EACL,uBAAuB,EACvB,+BAA+B,EAC/B,KAAK,gCAAgC,EACrC,KAAK,uBAAuB,EAC5B,KAAK,2BAA2B,EAChC,KAAK,0BAA0B,EAC/B,KAAK,2BAA2B,EAChC,KAAK,wBAAwB,EAC7B,KAAK,kBAAkB,EACvB,KAAK,2BAA2B,EACjC,MAAM,qCAAqC,CAAC;AAE7C,OAAO,EACL,4BAA4B,EAC5B,KAAK,gCAAgC,EACrC,KAAK,6BAA6B,EAClC,KAAK,sBAAsB,EAC3B,KAAK,gBAAgB,EACtB,MAAM,4CAA4C,CAAC"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Provider-specific entry point for the Web member of the system-model provider family.
3
+ *
4
+ * Kept out of the unified SDK index (`@independo/inderun-web`) so the top-level
5
+ * surface stays provider-agnostic. Import from `@independo/inderun-web/system-model`
6
+ * when registering the adapter manually or when implementing a custom
7
+ * `SystemModelRuntime`; most browser apps should pass `systemModel` to
8
+ * `createIndeRunWeb` from the main entry point instead.
9
+ */
10
+ export { DEFAULT_SYSTEM_MODEL_WEB_PROVIDER_ID, SystemModelWebProvider } from "./providers/system-model/provider.js";
11
+ export { SystemModelRuntimeError, createFixtureSystemModelRuntime } from "./providers/system-model/runtime.js";
12
+ export { createChromePromptApiRuntime } from "./providers/system-model/chrome-runtime.js";
package/dist/web-sdk.d.ts CHANGED
@@ -1,7 +1,8 @@
1
- import { type CreateBrowserHostServicesOptions } from "./browser-host.js";
2
- import { IndeRun } from "./engine.js";
3
- import { type OnnxProviderOptions } from "./onnx-provider.js";
4
- import { type OpenAIProviderOptions } from "./openai-provider.js";
1
+ import { type CreateBrowserHostServicesOptions } from "./core/browser-host.js";
2
+ import { IndeRun } from "./core/engine.js";
3
+ import { type OnnxProviderOptions } from "./providers/onnx/provider.js";
4
+ import { type OpenAIProviderOptions } from "./providers/openai/provider.js";
5
+ import { type SystemModelProviderOptions } from "./providers/system-model/provider.js";
5
6
  /**
6
7
  * Configuration for the default Web SDK factory.
7
8
  */
@@ -15,6 +16,12 @@ export interface CreateIndeRunWebOptions {
15
16
  * local models. Registering it is what makes `constraints.privacy = "local_required"` routable.
16
17
  */
17
18
  onnx?: OnnxProviderOptions;
19
+ /**
20
+ * Web system-model provider configuration registered by the factory, for browser-managed
21
+ * on-device models (for example Chrome's Prompt API). Unlike `onnx`, this provider takes no
22
+ * developer-supplied model: the browser owns model availability, download, and execution.
23
+ */
24
+ systemModel?: SystemModelProviderOptions;
18
25
  /**
19
26
  * Optional browser host service overrides.
20
27
  */
@@ -31,8 +38,9 @@ export interface CreateIndeRunWebOptions {
31
38
  /**
32
39
  * Creates a Web SDK instance with the configured providers registered.
33
40
  *
34
- * Pass `openAI` for cloud execution, `onnx` for developer-supplied local models, or both to let
35
- * routing choose between them; at least one is required.
41
+ * Pass `openAI` for cloud execution, `onnx` for developer-supplied local models, `systemModel` for
42
+ * browser-managed on-device models, or any combination to let routing choose between them; at
43
+ * least one is required.
36
44
  *
37
45
  * Use `openAI.auth = "none"` with a proxy endpoint for production browser apps so the OpenAI API key never ships
38
46
  * to the client. Direct OpenAI calls require `allowDirectOpenAIEndpoint` and should resolve credentials through
@@ -1 +1 @@
1
- {"version":3,"file":"web-sdk.d.ts","sourceRoot":"","sources":["../src/web-sdk.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,gCAAgC,EACtC,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AACtC,OAAO,EAA0B,KAAK,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AACtF,OAAO,EAGL,KAAK,qBAAqB,EAC3B,MAAM,sBAAsB,CAAC;AAG9B;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC;;OAEG;IACH,MAAM,CAAC,EAAE,qBAAqB,CAAC;IAC/B;;;OAGG;IACH,IAAI,CAAC,EAAE,mBAAmB,CAAC;IAC3B;;OAEG;IACH,YAAY,CAAC,EAAE,gCAAgC,CAAC;IAChD;;;;;;OAMG;IACH,yBAAyB,CAAC,EAAE,OAAO,CAAC;CACrC;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,uBAAuB,GAAG,OAAO,CAmB1E"}
1
+ {"version":3,"file":"web-sdk.d.ts","sourceRoot":"","sources":["../src/web-sdk.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,gCAAgC,EACtC,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC3C,OAAO,EAA0B,KAAK,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AAChG,OAAO,EAGL,KAAK,qBAAqB,EAC3B,MAAM,gCAAgC,CAAC;AAExC,OAAO,EAEL,KAAK,0BAA0B,EAChC,MAAM,sCAAsC,CAAC;AAE9C;;GAEG;AACH,MAAM,WAAW,uBAAuB;IACtC;;OAEG;IACH,MAAM,CAAC,EAAE,qBAAqB,CAAC;IAC/B;;;OAGG;IACH,IAAI,CAAC,EAAE,mBAAmB,CAAC;IAC3B;;;;OAIG;IACH,WAAW,CAAC,EAAE,0BAA0B,CAAC;IACzC;;OAEG;IACH,YAAY,CAAC,EAAE,gCAAgC,CAAC;IAChD;;;;;;OAMG;IACH,yBAAyB,CAAC,EAAE,OAAO,CAAC;CACrC;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,uBAAuB,GAAG,OAAO,CAuB1E"}