@iicp/web-node 0.1.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.
package/LICENSE ADDED
@@ -0,0 +1,180 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship made available under
36
+ the License, as indicated by a copyright notice that is included in
37
+ or attached to the work (an example is provided in the Appendix below).
38
+
39
+ "Derivative Works" shall mean any work, whether in Source or Object
40
+ form, that is based on (or derived from) the Work and for which the
41
+ editorial revisions, annotations, elaborations, or other transformations
42
+ represent, as a whole, an original work of authorship. For the purposes
43
+ of this License, Derivative Works shall not include works that remain
44
+ separable from, or merely link (or bind by name) to the interfaces of,
45
+ the Work and its Derivative Works thereof.
46
+
47
+ "Contribution" shall mean, as submitted to the Licensor for inclusion
48
+ in the Work by the copyright owner or by an individual or Legal Entity
49
+ authorized to submit on behalf of the copyright owner. For the purposes
50
+ of this definition, "submitted" means any form of electronic, verbal,
51
+ or written communication sent to the Licensor or its representatives,
52
+ including but not limited to communication on electronic mailing lists,
53
+ source code control systems, and issue tracking systems that are managed
54
+ by, or on behalf of, the Licensor for the purpose of discussing and
55
+ improving the Work, but excluding communication that is conspicuously
56
+ marked or otherwise designated in writing by the copyright owner as
57
+ "Not a Contribution."
58
+
59
+ "Contributor" shall mean Licensor and any Legal Entity on behalf of
60
+ whom a Contribution has been received by the Licensor and incorporated
61
+ within the Work.
62
+
63
+ 2. Grant of Copyright License. Subject to the terms and conditions of
64
+ this License, each Contributor hereby grants to You a perpetual,
65
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
66
+ copyright license to reproduce, prepare Derivative Works of,
67
+ publicly display, publicly perform, sublicense, and distribute the
68
+ Work and such Derivative Works in Source or Object form.
69
+
70
+ 3. Grant of Patent License. Subject to the terms and conditions of
71
+ this License, each Contributor hereby grants to You a perpetual,
72
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
73
+ (except as stated in this section) patent license to make, have made,
74
+ use, offer to sell, sell, import, and otherwise transfer the Work,
75
+ where such license applies only to those patent claims licensable
76
+ by such Contributor that are necessarily infringed by their
77
+ Contribution(s) alone or in combination with the Work to which such
78
+ Contribution(s) was submitted. If You institute patent litigation
79
+ against any entity (including a cross-claim or counterclaim in a
80
+ lawsuit) alleging that the Work or a Contribution incorporated within
81
+ the Work constitutes direct or contributory patent infringement, then
82
+ any patent licenses granted to You under this License for that Work
83
+ shall terminate as of the date such litigation is filed.
84
+
85
+ 4. Redistribution. You may reproduce and distribute copies of the
86
+ Work or Derivative Works thereof in any medium, with or without
87
+ modifications, and in Source or Object form, provided that You
88
+ meet the following conditions:
89
+
90
+ (a) You must give any other recipients of the Work or Derivative
91
+ Works a copy of this License; and
92
+
93
+ (b) You must cause any modified files to carry prominent notices
94
+ stating that You changed the files; and
95
+
96
+ (c) You must retain, in the Source form of any Derivative Works
97
+ that You distribute, all copyright, patent, trademark, and
98
+ attribution notices from the Source form of the Work,
99
+ excluding those notices that do not pertain to any part of
100
+ the Derivative Works; and
101
+
102
+ (d) If the Work includes a "NOTICE" text file as part of its
103
+ distribution, You must include a readable copy of the
104
+ attribution notices contained within such NOTICE file, in
105
+ at least one of the following places: within a NOTICE text
106
+ file distributed as part of the Derivative Works; within
107
+ the Source form or documentation, if provided along with the
108
+ Derivative Works; or, within a display generated by the
109
+ Derivative Works, if and wherever such third-party notices
110
+ normally appear. The contents of the NOTICE file are for
111
+ informational purposes only and do not modify the License.
112
+ You may add Your own attribution notices within Derivative
113
+ Works that You distribute, alongside or as an addendum to
114
+ the NOTICE text from the Work, provided that such additional
115
+ attribution notices cannot be construed as modifying the License.
116
+
117
+ You may add Your own license statement for Your modifications and
118
+ may provide additional grant of rights to use, copy, modify, merge,
119
+ publish, distribute, sublicense, and/or sell copies of the Work,
120
+ and to permit persons to whom the Work is furnished to do so, subject
121
+ to Your separate additional terms and conditions.
122
+
123
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
124
+ any Contribution intentionally submitted for inclusion in the Work
125
+ by You to the Licensor shall be under the terms and conditions of
126
+ this License, without any additional terms or conditions.
127
+ Notwithstanding the above, nothing herein shall supersede or modify
128
+ the terms of any separate license agreement you may have executed
129
+ with Licensor regarding such Contributions.
130
+
131
+ 6. Trademarks. This License does not grant permission to use the trade
132
+ names, trademarks, service marks, or product names of the Licensor,
133
+ except as required for reasonable and customary use in describing the
134
+ origin of the Work and reproducing the content of the NOTICE file.
135
+
136
+ 7. Disclaimer of Warranty. Unless required by applicable law or
137
+ agreed to in writing, Licensor provides the Work (and each
138
+ Contributor provides its Contributions) on an "AS IS" BASIS,
139
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
140
+ implied, including, without limitation, any conditions of title,
141
+ non-infringement, merchantability, or fitness for a particular
142
+ purpose. You are solely responsible for determining the
143
+ appropriateness of using or reproducing the Work and assume any
144
+ risks associated with Your exercise of permissions under this License.
145
+
146
+ 8. Limitation of Liability. In no event and under no legal theory,
147
+ whether in tort (including negligence), contract, or otherwise,
148
+ unless required by applicable law (such as deliberate and grossly
149
+ negligent acts) or agreed to in writing, shall any Contributor be
150
+ liable to You for damages, including any direct, indirect, special,
151
+ incidental, or exemplary damages of any character arising as a
152
+ result of this License or out of the use or inability to use the
153
+ Work (including but not limited to damages for loss of goodwill,
154
+ work stoppage, computer failure or malfunction, or all other
155
+ commercial damages or losses), even if such Contributor has been
156
+ advised of the possibility of such damages.
157
+
158
+ 9. Accepting Warranty or Additional Liability. While redistributing
159
+ the Work or Derivative Works thereof, You may choose to offer,
160
+ and charge a fee for, acceptance of support, warranty, indemnity,
161
+ or other liability obligations and/or rights consistent with this
162
+ License. However, in accepting such obligations, You may offer only
163
+ conditions fully consistent with this License to any recipient of
164
+ the Work.
165
+
166
+ END OF TERMS AND CONDITIONS
167
+
168
+ Copyright 2026 Rob Lemumin
169
+
170
+ Licensed under the Apache License, Version 2.0 (the "License");
171
+ you may not use this file except in compliance with the License.
172
+ You may obtain a copy of the License at
173
+
174
+ http://www.apache.org/licenses/LICENSE-2.0
175
+
176
+ Unless required by applicable law or agreed to in writing, software
177
+ distributed under the License is distributed on an "AS IS" BASIS,
178
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
179
+ See the License for the specific language governing permissions and
180
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # @iicp/web-node
2
+
3
+ Browser-native **IICP node** for the [IICP](https://iicp.network) mesh — **consume** AI
4
+ from the network *and* **serve** a model from a browser tab. ESM, built on the platform's
5
+ native `fetch` / `SubtleCrypto` / WebGPU (and runs in Node ≥18). End-to-end payload
6
+ encryption is **mandatory** and on by default.
7
+
8
+ ```
9
+ npm install @iicp/web-node
10
+ ```
11
+
12
+ ## Consume — call the mesh
13
+
14
+ ```ts
15
+ import { IicpBrowserClient, nodeCxKey } from "@iicp/web-node";
16
+
17
+ const client = new IicpBrowserClient(); // defaults to https://iicp.network
18
+ const nodes = await client.discover("urn:iicp:intent:llm:chat:v1");
19
+ // By default, discover() keeps only browser-usable HTTPS/loopback endpoints.
20
+ // Pass { browser_usable_only: false } if you are running in Node and want all routes.
21
+
22
+ const node = nodes[0];
23
+ const reply = await client.chat(
24
+ [{ role: "user", content: "Hello!" }],
25
+ { endpoint: node.endpoint, model: "llama3", cxPublicKey: nodeCxKey(node) },
26
+ );
27
+ ```
28
+
29
+ When the chosen node advertises an encryption key (`nodeCxKey(node)`), the payload is
30
+ **sealed end-to-end** — the directory, relays, and network see only ciphertext. There is
31
+ no opt-out. A node that does not advertise `cx_public_key`/`public_key` is refused before
32
+ any network send so browser use cannot silently fall back to plaintext.
33
+
34
+ ## Serve — be a node from the browser
35
+
36
+ ```ts
37
+ import { BrowserNodeProvider, WEBLLM_MODELS, assessDevice } from "@iicp/web-node";
38
+ // Loads a small model in-tab via WebLLM (WebGPU/WASM) and serves it behind a relay.
39
+ ```
40
+
41
+ Browser providers advertise a generated `cx_public_key`, decrypt incoming `iicp_conf`
42
+ payloads locally, report task success/failure/latency in heartbeats, and use a relay path
43
+ because browser tabs cannot accept raw inbound TCP.
44
+
45
+ Serving requires WebLLM — add it alongside this package:
46
+
47
+ ```
48
+ npm install @mlc-ai/web-llm # peer dependency, only needed to serve
49
+ ```
50
+
51
+ ## Encryption
52
+
53
+ IICP-CX (S.16) Tier-1: ephemeral **X25519** key agreement → **HKDF-SHA256** →
54
+ **AES-256-GCM** authenticated encryption, computed with `@noble/curves` + `@noble/hashes`
55
+ and the platform's `SubtleCrypto`. The envelope is wire-compatible with the Python,
56
+ TypeScript, and Rust IICP clients and the provider/adapter that decrypts it.
57
+
58
+ ## CORS reality
59
+
60
+ Discovery (`GET /api/v1/discover`) works from any `https://` page because `iicp.network`
61
+ sends CORS headers. Routing a task to a discovered node is subject to the browser's CORS
62
+ and mixed-content policy: an `https://` page cannot reach an `http://localhost` model, a
63
+ node without CORS headers, or an IPv6-firewalled node. In **Node** there is no CORS
64
+ restriction and everything works.
65
+
66
+ ## License
67
+
68
+ Apache-2.0. Part of the open [IICP protocol](https://github.com/RobLe3/IICP).
@@ -0,0 +1,73 @@
1
+ import type { ChatMessage } from "./iicpConsumer.js";
2
+ export interface BrowserProviderRuntime {
3
+ chat(messages: ChatMessage[], opts?: {
4
+ temperature?: number;
5
+ max_tokens?: number;
6
+ }): Promise<string>;
7
+ }
8
+ export interface BrowserProviderConfig {
9
+ /** Relay node base URL, e.g. "http://127.0.0.1:9484". Required. */
10
+ relayUrl: string;
11
+ /** Directory API base. Default: "https://iicp.network/api". */
12
+ directoryUrl?: string;
13
+ /** Model name advertised to the directory (the loaded WebLLM model id). */
14
+ model: string;
15
+ region?: string;
16
+ onLog?: (line: string) => void;
17
+ /** Called after each served task with the running total. */
18
+ onTaskServed?: (total: number) => void;
19
+ onStateChange?: (state: BrowserProviderState) => void;
20
+ }
21
+ export type BrowserProviderState = "stopped" | "starting" | "serving" | "error";
22
+ export declare const BROWSER_NODE_SDK_VERSION = "0.7.71-browser";
23
+ /**
24
+ * Coarse region autodetect from the browser's timezone (no network, no
25
+ * geolocation permission). Matches the mesh's region convention
26
+ * (eu-central / us-east / …) as a SCORING HINT — deliberately coarse;
27
+ * operators can override via config. "(Browser)" is shown separately on
28
+ * the nodes page via sdk_language.
29
+ */
30
+ export declare function detectRegion(): string;
31
+ export interface DiscoveredRelay {
32
+ endpoint: string;
33
+ nodeId: string;
34
+ reputation: number;
35
+ probation: boolean;
36
+ }
37
+ export declare function discoverRelay(directoryUrl?: string): Promise<DiscoveredRelay | null>;
38
+ export declare class BrowserProviderError extends Error {
39
+ readonly stage: "register" | "bind" | "serve";
40
+ constructor(message: string, stage: "register" | "bind" | "serve");
41
+ }
42
+ export declare class BrowserNodeProvider {
43
+ private readonly runtime;
44
+ private readonly cfg;
45
+ readonly nodeId: string;
46
+ private _state;
47
+ private _nodeToken;
48
+ private _sessionToken;
49
+ private _tasksServed;
50
+ private _okSinceBeat;
51
+ private _failSinceBeat;
52
+ private _latencyMsSinceBeat;
53
+ private readonly _cx;
54
+ private _heartbeatTimer;
55
+ private _stopRequested;
56
+ /** True when the directory accepted the registration (mesh-discoverable). */
57
+ directoryListed: boolean;
58
+ constructor(runtime: BrowserProviderRuntime, cfg: BrowserProviderConfig);
59
+ get state(): BrowserProviderState;
60
+ get tasksServed(): number;
61
+ private get directoryBase();
62
+ private get relayBase();
63
+ private log;
64
+ private setState;
65
+ /** Register → bind → start serving. Throws BrowserProviderError on failure. */
66
+ start(): Promise<void>;
67
+ /** Unbind from the relay and deregister from the directory. */
68
+ stop(): Promise<void>;
69
+ private unbindQuietly;
70
+ private heartbeat;
71
+ private pollLoop;
72
+ private serveCall;
73
+ }