@independo/inderun-web 0.3.0-dev.12 → 0.3.0-dev.13

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.
@@ -0,0 +1,49 @@
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
+ * "System model" means a model the browser itself manages, so availability is a
11
+ * richer answer than a boolean: it may be present, downloadable, mid-download, or
12
+ * blocked for a browser, device, or policy reason. The provider flattens every
13
+ * non-available state into one route rejection and carries the detail as text.
14
+ */
15
+
16
+ /** The adapter itself, plus the default id the router plans against. */
17
+ export {
18
+ DEFAULT_SYSTEM_MODEL_WEB_PROVIDER_ID,
19
+ SystemModelWebProvider,
20
+ type SystemModelProviderOptions
21
+ } from "./providers/system-model/provider.js";
22
+
23
+ /**
24
+ * The runtime seam. The provider delegates generation to a `SystemModelRuntime`,
25
+ * so an app can target a different browser API, or use
26
+ * `createFixtureSystemModelRuntime()` in demos and tests where no system model
27
+ * exists.
28
+ */
29
+ export {
30
+ SystemModelRuntimeError,
31
+ createFixtureSystemModelRuntime,
32
+ type FixtureSystemModelRuntimeOptions,
33
+ type SystemModelAvailability,
34
+ type SystemModelAvailabilityKind,
35
+ type SystemModelGenerationInput,
36
+ type SystemModelGenerationOutput,
37
+ type SystemModelPromptMessage,
38
+ type SystemModelRuntime,
39
+ type SystemModelRuntimeErrorKind
40
+ } from "./providers/system-model/runtime.js";
41
+
42
+ /** The default runtime, backed by Chrome's built-in Prompt API. */
43
+ export {
44
+ createChromePromptApiRuntime,
45
+ type ChromePromptApiGenerationOptions,
46
+ type ChromePromptApiRuntimeOptions,
47
+ type PromptApiLanguageModel,
48
+ type PromptApiSession
49
+ } from "./providers/system-model/chrome-runtime.js";
package/src/web-sdk.ts ADDED
@@ -0,0 +1,114 @@
1
+ import {
2
+ createBrowserHostServices,
3
+ type CreateBrowserHostServicesOptions
4
+ } from "./core/browser-host.js";
5
+ import { IndeRun } from "./core/engine.js";
6
+ import { OnnxRuntimeWebProvider, type OnnxProviderOptions } from "./providers/onnx/provider.js";
7
+ import {
8
+ DEFAULT_OPENAI_RESPONSES_ENDPOINT,
9
+ OpenAIResponsesProvider,
10
+ type OpenAIProviderOptions
11
+ } from "./providers/openai/provider.js";
12
+ import { ProviderRegistry } from "./core/registry.js";
13
+ import {
14
+ SystemModelWebProvider,
15
+ type SystemModelProviderOptions
16
+ } from "./providers/system-model/provider.js";
17
+
18
+ /**
19
+ * Configuration for the default Web SDK factory.
20
+ */
21
+ export interface CreateIndeRunWebOptions {
22
+ /**
23
+ * OpenAI Responses provider configuration registered by the factory.
24
+ */
25
+ openAI?: OpenAIProviderOptions;
26
+ /**
27
+ * Web ONNX Runtime provider configuration registered by the factory, for developer-supplied
28
+ * local models. Registering it is what makes `constraints.privacy = "local_required"` routable.
29
+ */
30
+ onnx?: OnnxProviderOptions;
31
+ /**
32
+ * Web system-model provider configuration registered by the factory, for browser-managed
33
+ * on-device models (for example Chrome's Prompt API). Unlike `onnx`, this provider takes no
34
+ * developer-supplied model: the browser owns model availability, download, and execution.
35
+ */
36
+ systemModel?: SystemModelProviderOptions;
37
+ /**
38
+ * Optional browser host service overrides.
39
+ */
40
+ hostServices?: CreateBrowserHostServicesOptions;
41
+ /**
42
+ * Explicit opt-in for calling the public OpenAI endpoint directly from this Web SDK factory.
43
+ *
44
+ * Browser apps should leave this unset and use a proxy endpoint with `openAI.auth = "none"` instead.
45
+ * Direct calls can expose credentials to client-side code and are appropriate only for controlled
46
+ * non-production environments.
47
+ */
48
+ allowDirectOpenAIEndpoint?: boolean;
49
+ }
50
+
51
+ /**
52
+ * Creates a Web SDK instance with the configured providers registered.
53
+ *
54
+ * Pass `openAI` for cloud execution, `onnx` for developer-supplied local models, `systemModel` for
55
+ * browser-managed on-device models, or any combination to let routing choose between them; at
56
+ * least one is required.
57
+ *
58
+ * Use `openAI.auth = "none"` with a proxy endpoint for production browser apps so the OpenAI API key never ships
59
+ * to the client. Direct OpenAI calls require `allowDirectOpenAIEndpoint` and should resolve credentials through
60
+ * `authContextRef` and `SecureStorageService`.
61
+ *
62
+ * @param options - Provider configuration and optional host service overrides.
63
+ */
64
+ export function createIndeRunWeb(options: CreateIndeRunWebOptions): IndeRun {
65
+ if (!options.openAI && !options.onnx && !options.systemModel) {
66
+ throw new Error(
67
+ "createIndeRunWeb requires at least one provider configuration (openAI, onnx, and/or systemModel)."
68
+ );
69
+ }
70
+
71
+ const registry = new ProviderRegistry();
72
+
73
+ if (options.openAI) {
74
+ assertSafeOpenAIEndpoint(options.openAI, options.allowDirectOpenAIEndpoint);
75
+ registry.register(new OpenAIResponsesProvider(options.openAI));
76
+ }
77
+
78
+ if (options.onnx) {
79
+ registry.register(new OnnxRuntimeWebProvider(options.onnx));
80
+ }
81
+
82
+ if (options.systemModel) {
83
+ registry.register(new SystemModelWebProvider(options.systemModel));
84
+ }
85
+
86
+ return new IndeRun(registry, createBrowserHostServices(options.hostServices));
87
+ }
88
+
89
+ function assertSafeOpenAIEndpoint(
90
+ openAI: OpenAIProviderOptions,
91
+ allowDirectOpenAIEndpoint?: boolean
92
+ ): void {
93
+ const endpointUrl = openAI.endpointUrl ?? DEFAULT_OPENAI_RESPONSES_ENDPOINT;
94
+ const auth = openAI.auth ?? "authContextRef";
95
+ const isDirectOpenAIEndpoint = isOpenAIResponsesEndpoint(endpointUrl);
96
+
97
+ if (isDirectOpenAIEndpoint && auth !== "none" && !allowDirectOpenAIEndpoint) {
98
+ throw new Error(
99
+ 'createIndeRunWeb is proxy-first for browser safety. Configure openAI.endpointUrl to a server-side proxy with auth: "none", or set allowDirectOpenAIEndpoint: true for controlled direct OpenAI calls.'
100
+ );
101
+ }
102
+ }
103
+
104
+ function isOpenAIResponsesEndpoint(endpointUrl: string): boolean {
105
+ let parsed: URL;
106
+ try {
107
+ parsed = new URL(endpointUrl);
108
+ } catch {
109
+ return false;
110
+ }
111
+
112
+ const pathname = parsed.pathname.replace(/\/+$/u, "");
113
+ return parsed.hostname.toLowerCase() === "api.openai.com" && pathname === "/v1/responses";
114
+ }