@zerogpu/browser-extension-sdk 0.1.1 → 0.1.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.
@@ -0,0 +1,204 @@
1
+ # ZeroGPU SDK — Chrome Extension Developer Guide
2
+
3
+ Add on-device AI to your Chrome extension. The ZeroGPU SDK runs a small language
4
+ model on your users' own devices — so you get private, on-device text AI
5
+ (summarizing, classifying, extracting, and more) without running any inference
6
+ servers yourself.
7
+
8
+ This guide takes you from an empty Manifest V3 extension to a working integration
9
+ in a few minutes, then shows how to validate it and keep it up to date.
10
+
11
+ ---
12
+
13
+ ## How it works (the short version)
14
+
15
+ - **Where it runs:** the SDK runs in a background *offscreen document* — an
16
+ invisible page Chrome provides for code that needs a full browser context. The
17
+ tooling sets this up for you.
18
+ - **What ships in your extension:** Chrome Manifest V3 doesn't allow extensions to
19
+ load code from the internet, so the SDK and its runtime are copied into your
20
+ extension at build time. Only model data is fetched while running. The `zerogpu`
21
+ CLI does the copying for you.
22
+ - **When it starts:** once integrated, the SDK starts and registers the device
23
+ **automatically** whenever your extension loads — there's nothing to trigger by
24
+ hand.
25
+
26
+ You integrate once. After that, new SDK versions reach your users when you update a
27
+ dependency and republish your extension.
28
+
29
+ ---
30
+
31
+ ## Prerequisites
32
+
33
+ - A Manifest V3 Chrome extension.
34
+ - An **operator key** from ZeroGPU (looks like `zgpu-sdk-…`).
35
+ - Node.js and npm — used only at build time. Nothing from `node_modules` ships in
36
+ your extension.
37
+
38
+ ---
39
+
40
+ ## 1. Install
41
+
42
+ ```bash
43
+ npm install @zerogpu/browser-extension-sdk
44
+ ```
45
+
46
+ ## 2. Integrate
47
+
48
+ ```bash
49
+ npx zerogpu init
50
+ ```
51
+
52
+ This single command:
53
+
54
+ - copies the SDK runtime into your extension at `vendor/zerogpu/`,
55
+ - creates `offscreen.html`, `offscreen.js`, and `background.js` (it never overwrites
56
+ files you already have),
57
+ - adds the entries the SDK needs to your `manifest.json` (the `offscreen` and
58
+ `storage` permissions, a content-security-policy entry, and the host permissions
59
+ used to download models).
60
+
61
+ ## 3. Add your operator key
62
+
63
+ Open the generated `offscreen.js` and replace the placeholder:
64
+
65
+ ```js
66
+ import { initZeroGpuOffscreen } from "./vendor/zerogpu/zerogpu-offscreen.js";
67
+
68
+ initZeroGpuOffscreen({
69
+ edgeOperatorKey: "zgpu-sdk-your-key",
70
+ enableConsoleLogs: true
71
+ });
72
+ ```
73
+
74
+ That's the whole integration. The next time your extension loads, the SDK starts
75
+ automatically.
76
+
77
+ > **Using your own service worker or build pipeline?** `init` won't overwrite your
78
+ > files — merge the generated `background.js` snippet into your existing worker, and
79
+ > add `npx zerogpu vendor` to your build script (see [§5](#5-keep-it-up-to-date)).
80
+
81
+ ---
82
+
83
+ ## 4. Validate
84
+
85
+ 1. Go to `chrome://extensions`, turn on **Developer mode**, and click **Load
86
+ unpacked** — select your extension folder.
87
+ 2. Open the offscreen document's console: your extension's card →
88
+ **Inspect views: offscreen.html**.
89
+ 3. With `enableConsoleLogs: true`, you'll see the SDK start, register the device,
90
+ load a model, and become ready.
91
+
92
+ Run the built-in check any time:
93
+
94
+ ```bash
95
+ npx zerogpu doctor
96
+ ```
97
+
98
+ It confirms your manifest is wired correctly and that the vendored runtime is
99
+ present and matches the installed SDK version.
100
+
101
+ ---
102
+
103
+ ## 5. Keep it up to date
104
+
105
+ Because Manifest V3 extensions can't change their code at runtime, your users get a
106
+ new SDK version when you **publish a new build of your extension**. The aim is to
107
+ make adopting new versions as low-effort as possible.
108
+
109
+ **The one rule:** whenever the SDK version changes, the runtime in `vendor/zerogpu/`
110
+ must be refreshed. `npx zerogpu vendor` does exactly that — it copies the currently
111
+ installed SDK runtime into your extension. Wire it into your build so it can't be
112
+ forgotten:
113
+
114
+ ```json
115
+ { "scripts": { "prebuild": "zerogpu vendor" } }
116
+ ```
117
+
118
+ Now every build re-vendors the runtime automatically. Pick whichever update style
119
+ below fits your workflow.
120
+
121
+ ### Option A — Automatic (recommended)
122
+
123
+ Let a bot open a pull request whenever a new SDK version ships, and let your build
124
+ re-vendor on merge.
125
+
126
+ - **Dependabot** — add `.github/dependabot.yml`:
127
+ ```yaml
128
+ version: 2
129
+ updates:
130
+ - package-ecosystem: "npm"
131
+ directory: "/"
132
+ schedule: { interval: "weekly" }
133
+ ```
134
+ - **Renovate** — add `renovate.json`:
135
+ ```json
136
+ { "extends": ["config:recommended"] }
137
+ ```
138
+
139
+ When the bot's PR lands, your build re-runs `zerogpu vendor` (via `prebuild`) and you
140
+ republish. Reviewing the PR before merging is recommended so you can skim the
141
+ changelog — especially for major version bumps.
142
+
143
+ You control **how aggressively** updates are accepted via the version range in
144
+ `package.json`:
145
+
146
+ | Range | Accepts |
147
+ | :--- | :--- |
148
+ | `"^1.2.0"` | Any compatible `1.x` update (recommended) |
149
+ | `"~1.2.0"` | Patch updates only (`1.2.x`) |
150
+ | `"1.2.0"` | Nothing automatic — you bump this line deliberately |
151
+
152
+ ### Option B — Manual
153
+
154
+ Update on your own schedule:
155
+
156
+ ```bash
157
+ npm update @zerogpu/browser-extension-sdk
158
+ npx zerogpu vendor
159
+ npx zerogpu doctor # confirm the vendored runtime matches the installed version
160
+ ```
161
+
162
+ Then republish.
163
+
164
+ ### Option C — No build tooling
165
+
166
+ If your extension has no build step, vendor on demand without adding a dependency:
167
+
168
+ ```bash
169
+ npx -p @zerogpu/browser-extension-sdk zerogpu vendor
170
+ ```
171
+
172
+ Run it again whenever you want a newer version, commit the refreshed
173
+ `vendor/zerogpu/`, and republish.
174
+
175
+ ### Delivering the update to users
176
+
177
+ After you upload the new build to the Chrome Web Store, Chrome updates installed
178
+ users automatically — nothing else to do.
179
+
180
+ ---
181
+
182
+ ## Configuration reference
183
+
184
+ `initZeroGpuOffscreen(options)`:
185
+
186
+ | Option | Required | Description |
187
+ | :--- | :--- | :--- |
188
+ | `edgeOperatorKey` | yes | Your ZeroGPU operator key. |
189
+ | `enableConsoleLogs` | no | Logs SDK activity to the offscreen console. Handy while integrating. |
190
+ | `appUserId` | no | A stable identifier for the current user, if you want consistent identity across sessions. |
191
+ | `orchestratorBaseUrl` | no | Point at a non-default ZeroGPU environment, if you've been given one. Defaults to production. |
192
+
193
+ ---
194
+
195
+ ## Troubleshooting
196
+
197
+ | Symptom | Fix |
198
+ | :--- | :--- |
199
+ | The SDK doesn't start | Open the offscreen console (*Inspect views: offscreen.html*) and check for errors, then run `npx zerogpu doctor`. |
200
+ | CSP error mentioning `'wasm-unsafe-eval'` | Your manifest's content security policy must allow `'wasm-unsafe-eval'`. Re-run `npx zerogpu init` (or `doctor`). |
201
+ | Import fails / runtime files missing | Run `npx zerogpu vendor` and confirm `vendor/zerogpu/` exists. |
202
+ | Model download blocked | If your extension limits network access, allow the model host in your manifest's host permissions (`npx zerogpu doctor` will flag this). |
203
+ | Rejected at registration / 401 | Your operator key isn't valid for the environment you're targeting. Check the key (and `orchestratorBaseUrl` if you set one). |
204
+ | Out of date after an update | `npx zerogpu doctor` reports drift → run `npx zerogpu vendor`. |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zerogpu/browser-extension-sdk",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "ZeroGPU device-side SLM runtime SDK for browsers and Chrome (MV3) extensions",
5
5
  "type": "module",
6
6
  "main": "dist/zerogpu-browser-sdk.cjs",
@@ -57,7 +57,7 @@
57
57
  "url": "git+https://github.com/zerogpu/zerogpu-browser-sdk.git",
58
58
  "directory": "sdk"
59
59
  },
60
- "homepage": "https://github.com/zerogpu/zerogpu-browser-sdk/blob/main/sdk/docs/INTEGRATION.md",
60
+ "homepage": "https://github.com/zerogpu/zerogpu-browser-sdk/blob/main/sdk/docs/extension-developer-guide.md",
61
61
  "bugs": {
62
62
  "url": "https://github.com/zerogpu/zerogpu-browser-sdk/issues"
63
63
  },
@@ -1,213 +0,0 @@
1
- # Integrate the ZeroGPU SDK into a Chrome Extension (MV3)
2
-
3
- This guide takes a Chrome extension from zero to running on-device AI (all model
4
- types — ONNX **and** GGUF) with the fewest possible steps, and keeps it
5
- auto-updating after that. No prior knowledge of the SDK is needed.
6
-
7
- ---
8
-
9
- ## 1. What this is / what runs where
10
-
11
- The ZeroGPU SDK runs a small language model **on the user's device** inside your
12
- extension. Because Chrome Manifest V3 **bans remotely-hosted code**, the SDK and
13
- its model runtimes must ship *inside* your extension package — you can't load them
14
- from a CDN at runtime. Only the model **weights** (data, not code) download at run
15
- time, which is allowed.
16
-
17
- ```
18
- [1] ZeroGPU publishes ──npm──▶ @zerogpu/browser-extension-sdk@x.y.z
19
- (SDK + offscreen helper + transformers + wllama + wasm)
20
- │
21
- [2] you: npm install ────────────────┘ → lands in node_modules/ (not shipped)
22
- │
23
- [3] you: npx zerogpu vendor ──────────┘ → copies the runtime into vendor/zerogpu/
24
- │
25
- [4] you: republish to Chrome Web Store ┘ → Chrome auto-updates every user
26
- │
27
- [5] runtime: the offscreen document loads vendor/zerogpu/ locally; wasm is fetched
28
- from that local folder; only model WEIGHTS come over the network.
29
- ```
30
-
31
- The SDK runs in an **offscreen document** because it needs a `window`/DOM context
32
- and Web Workers, which a service worker doesn't have.
33
-
34
- ---
35
-
36
- ## 2. Prerequisites
37
-
38
- - A Manifest V3 extension.
39
- - An **operator key** from the ZeroGPU dashboard (looks like `zgpu-sdk-…`).
40
- - Node.js (only for the one-time `npx` step; it does not ship in your extension).
41
-
42
- ---
43
-
44
- ## 3. Quickstart (≈3 steps)
45
-
46
- ```bash
47
- npm install @zerogpu/browser-extension-sdk
48
- npx zerogpu init # vendors the runtime + scaffolds the glue + patches manifest.json
49
- ```
50
-
51
- Then open `offscreen.js`, replace `REPLACE_WITH_YOUR_OPERATOR_KEY` with your key,
52
- build/zip, and **Load unpacked** at `chrome://extensions`.
53
-
54
- **What `npx zerogpu init` did:**
55
- - Vendored the runtime into `vendor/zerogpu/` (offscreen helper, SDK, transformers
56
- + ONNX wasm, wllama + wasm).
57
- - Created `offscreen.html`, `offscreen.js`, and a `background.js` bootstrap (only
58
- if absent — existing files are left untouched).
59
- - Patched `manifest.json`: added the `offscreen` + `storage` permissions, the CSP
60
- `script-src 'self' 'wasm-unsafe-eval'`, and the model-host `optional_host_permissions`.
61
-
62
- > Already have a build pipeline? Add `npx zerogpu vendor` to your build script so the
63
- > runtime is refreshed on every build (see §7).
64
-
65
- ---
66
-
67
- ## 4. Manual integration (if you skip the scaffolder)
68
-
69
- Run `npx zerogpu vendor` to place the runtime in `vendor/zerogpu/`, then add these
70
- files yourself.
71
-
72
- **`offscreen.html`**
73
- ```html
74
- <!DOCTYPE html>
75
- <html>
76
- <head><meta charset="utf-8" /><title>ZeroGPU offscreen</title></head>
77
- <body>
78
- <script type="module" src="offscreen.js"></script>
79
- </body>
80
- </html>
81
- ```
82
-
83
- **`offscreen.js`** (the whole integration is one call)
84
- ```js
85
- import { registerZeroGpuOffscreenMessaging } from "./vendor/zerogpu/zerogpu-offscreen.js";
86
-
87
- registerZeroGpuOffscreenMessaging({
88
- edgeOperatorKey: "zgpu-sdk-your-key",
89
- enableConsoleLogs: true
90
- });
91
- ```
92
-
93
- > Prefer to drive init yourself? Import `initZeroGpuOffscreen` instead and call it
94
- > when you're ready. `registerZeroGpuOffscreenMessaging` just wires the standard
95
- > `zerogpu:forceInit` / `zerogpu:getStatus` messages so your `background.js`/popup
96
- > can start and inspect the SDK.
97
-
98
- **`background.js`** (service worker — ensures the offscreen document exists)
99
- ```js
100
- const OFFSCREEN_PATH = "offscreen.html";
101
- let creating = null;
102
- async function ensureOffscreen() {
103
- const ctx = await chrome.runtime.getContexts({
104
- contextTypes: ["OFFSCREEN_DOCUMENT"],
105
- documentUrls: [chrome.runtime.getURL(OFFSCREEN_PATH)]
106
- });
107
- if (ctx.length) return;
108
- creating = creating || chrome.offscreen.createDocument({
109
- url: OFFSCREEN_PATH, reasons: ["WORKERS"],
110
- justification: "Run the ZeroGPU SDK, which needs a window/DOM context."
111
- });
112
- await creating; creating = null;
113
- }
114
- chrome.runtime.onMessage.addListener((msg, _s, send) => {
115
- if (msg?.type === "zerogpu:forceInit" || msg?.type === "zerogpu:getStatus") {
116
- ensureOffscreen().then(() => chrome.runtime.sendMessage(msg)).then(send);
117
- return true;
118
- }
119
- });
120
- ```
121
-
122
- **`manifest.json`** — required entries
123
- ```jsonc
124
- {
125
- "permissions": ["offscreen", "storage"],
126
- "background": { "service_worker": "background.js" },
127
- "content_security_policy": {
128
- "extension_pages": "script-src 'self' 'wasm-unsafe-eval'; object-src 'self'"
129
- },
130
- "optional_host_permissions": [
131
- "https://*.zerogpu.ai/*",
132
- "https://huggingface.co/*",
133
- "https://*.hf.co/*",
134
- "https://cdn-lfs.huggingface.co/*"
135
- ]
136
- }
137
- ```
138
- The model-host permissions let the offscreen document fetch model weights. If your
139
- models are served only from `*.zerogpu.ai`, you can drop the Hugging Face hosts.
140
-
141
- ---
142
-
143
- ## 5. Choosing the environment
144
-
145
- The published build targets **production** by default. To test against another
146
- environment without rebuilding, pass `orchestratorBaseUrl`:
147
-
148
- ```js
149
- registerZeroGpuOffscreenMessaging({
150
- edgeOperatorKey: "zgpu-sdk-your-key",
151
- orchestratorBaseUrl: "https://dev.devices.zerogpu.ai" // develop
152
- });
153
- ```
154
-
155
- Your operator key must be valid in whichever environment you point at.
156
-
157
- ---
158
-
159
- ## 6. Run & test locally
160
-
161
- 1. `npx zerogpu vendor` (if you haven't already), then **Load unpacked** at
162
- `chrome://extensions`.
163
- 2. Trigger the AI feature (or send `zerogpu:forceInit` from your popup).
164
- 3. Open the **offscreen** document console: `chrome://extensions` → your extension
165
- → *Inspect views: offscreen.html*.
166
-
167
- Expect to see: a `register` POST to your orchestrator, a model download, the model
168
- loading from the **local** wasm (no `jsDelivr`/CDN request for code), a sample
169
- inference, and a WebSocket connect. Run `npx zerogpu doctor` to verify the manifest
170
- and that the vendored assets are present and in sync.
171
-
172
- ---
173
-
174
- ## 7. Keep it up to date ("integrate once, forget")
175
-
176
- You can't hot-swap code at runtime under MV3, so "latest" means *latest at build
177
- time*. Make that automatic:
178
-
179
- 1. **Auto-vendor on build** — add to your `package.json`:
180
- ```json
181
- { "scripts": { "prebuild": "zerogpu vendor" } }
182
- ```
183
- 2. **Auto-PR new SDK versions** — add Dependabot (`.github/dependabot.yml`):
184
- ```yaml
185
- version: 2
186
- updates:
187
- - package-ecosystem: "npm"
188
- directory: "/"
189
- schedule: { interval: "weekly" }
190
- ```
191
- (or the Renovate equivalent).
192
- 3. Merge the bump PR → your build re-runs `zerogpu vendor` → **republish** to the
193
- Chrome Web Store. Chrome auto-updates every installed user.
194
-
195
- That's the whole maintenance loop: *merge a PR and republish.*
196
-
197
- ---
198
-
199
- ## 8. Troubleshooting
200
-
201
- | Symptom | Fix |
202
- | :--- | :--- |
203
- | CSP error / "Refused to … 'wasm-unsafe-eval'" | Ensure `content_security_policy.extension_pages` includes `'wasm-unsafe-eval'`. |
204
- | "SDK global not found" / helper import fails | Re-run `npx zerogpu vendor`; confirm `vendor/zerogpu/zerogpu-offscreen.js` exists and `offscreen.html` uses `type="module"`. |
205
- | Model download blocked (CORS / network) | Add the model's host to `optional_host_permissions` (and grant it). Models on Hugging Face need the `huggingface.co` / `cdn-lfs.huggingface.co` hosts. |
206
- | GGUF model: `wllama.wasm` 404 | Re-run `npx zerogpu vendor` so `vendor/zerogpu/wllama/**` is present. |
207
- | `edge_sdk_type_not_allowed` or 401 at register | Your operator key isn't valid/allowed in the targeted environment. Check the key and `orchestratorBaseUrl`. |
208
- | Out of sync after a version bump | `npx zerogpu doctor` reports drift → run `npx zerogpu vendor`. |
209
-
210
- ---
211
-
212
- For a complete working example, see the
213
- [28-day-habit-tracker reference extension](https://github.com/zerogpu/zerogpu-browser-sdk).