@zerogpu/browser-extension-sdk 0.5.1 → 0.6.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/README.md +99 -41
- package/bin/browser-extension-sdk.mjs +23 -0
- package/bin/cli.mjs +781 -0
- package/bin/herogpu.mjs +6 -0
- package/bin/zerogpu.mjs +6 -585
- package/dist/assets/herogpu-assets.json +21 -0
- package/dist/assets/herogpu-worker.js +1 -0
- package/dist/assets/zerogpu-assets.json +2 -2
- package/dist/assets/zerogpu-worker.js +1 -1
- package/dist/extension/herogpu-background.js +1 -0
- package/dist/extension/herogpu-offscreen.js +1 -0
- package/dist/extension/herogpu-settings-link.js +1 -0
- package/dist/extension/zerogpu-background.js +1 -1
- package/dist/extension/zerogpu-offscreen.js +1 -1
- package/dist/extension/zerogpu-settings-link.js +1 -1
- package/dist/herogpu-browser-sdk.cjs +1 -0
- package/dist/herogpu-browser-sdk.esm.js +1 -0
- package/dist/herogpu-browser-sdk.umd.js +1 -0
- package/dist/herogpu-worker.js +1 -0
- package/dist/loader.js +1 -1
- package/dist/manifest.json +5 -5
- package/dist/types/appinference/store.d.ts +6 -2
- package/dist/types/appinference/types.d.ts +29 -10
- package/dist/types/branding.d.ts +27 -0
- package/dist/types/config/types.d.ts +1 -1
- package/dist/types/extension/background.d.ts +68 -19
- package/dist/types/extension/consent.d.ts +61 -10
- package/dist/types/extension/offscreen.d.ts +64 -23
- package/dist/types/extension/settings-link.d.ts +12 -1
- package/dist/types/inference/runtime-locator.d.ts +1 -1
- package/dist/types/inference/worker-host.d.ts +1 -1
- package/dist/types/loader.d.ts +15 -3
- package/dist/types/public-api.d.ts +16 -10
- package/dist/types/runtime/app-identity.d.ts +2 -2
- package/dist/types/runtime/app-inference.d.ts +1 -1
- package/dist/types/runtime/custom-params.d.ts +1 -1
- package/dist/types/runtime/host-api.d.ts +2 -1
- package/dist/types/runtime/init-guard.d.ts +2 -1
- package/dist/types/runtime/tracking-script.d.ts +1 -1
- package/dist/types/storage/adopted-key.d.ts +23 -0
- package/dist/types/storage/cached-model.d.ts +4 -1
- package/dist/types/storage/model-store.d.ts +6 -0
- package/dist/types/transport/protocol.d.ts +1 -1
- package/dist/v/herogpu-browser-sdk.a957716b164a.umd.js +1 -0
- package/dist/v/zerogpu-browser-sdk.a957716b164a.umd.js +1 -0
- package/dist/zerogpu-browser-sdk.cjs +1 -1
- package/dist/zerogpu-browser-sdk.esm.js +1 -1
- package/dist/zerogpu-browser-sdk.umd.js +1 -1
- package/dist/zerogpu-worker.js +1 -1
- package/docs/app-side-inference.md +55 -45
- package/docs/auto-update-loader.md +33 -19
- package/docs/extension-developer-guide.md +158 -75
- package/docs/multi-model-support.md +14 -14
- package/package.json +13 -10
- package/dist/v/zerogpu-browser-sdk.686affbdf19f.umd.js +0 -1
package/bin/herogpu.mjs
ADDED
package/bin/zerogpu.mjs
CHANGED
|
@@ -1,587 +1,8 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
* origin to manifest.json if it is missing.
|
|
8
|
-
* Run this in your build.
|
|
9
|
-
* npx zerogpu init [--dir .] Vendor + scaffold offscreen/background glue and
|
|
10
|
-
* patch manifest.json. For a brand-new extension.
|
|
11
|
-
* npx zerogpu doctor [--dir .] Check manifest + that vendored assets are in sync.
|
|
12
|
-
*
|
|
13
|
-
* MV3 forbids remotely-hosted code, so everything must live inside the extension
|
|
14
|
-
* package; `vendor` is the one step that puts the runtime files in place.
|
|
15
|
-
*/
|
|
16
|
-
import {
|
|
17
|
-
cpSync,
|
|
18
|
-
existsSync,
|
|
19
|
-
mkdirSync,
|
|
20
|
-
readFileSync,
|
|
21
|
-
readdirSync,
|
|
22
|
-
writeFileSync
|
|
23
|
-
} from 'node:fs';
|
|
24
|
-
import path from 'node:path';
|
|
25
|
-
import { fileURLToPath } from 'node:url';
|
|
2
|
+
// `zerogpu`: the name this CLI had up to 0.5.1, kept so build scripts and
|
|
3
|
+
// guides written for it keep working. Same implementation as `herogpu`
|
|
4
|
+
// (./cli.mjs); the only difference is the layout written into an extension
|
|
5
|
+
// that has none yet: vendor/zerogpu/zerogpu-*.js, exactly as before.
|
|
6
|
+
import { runCli } from './cli.mjs';
|
|
26
7
|
|
|
27
|
-
|
|
28
|
-
const pkgRoot = path.resolve(__dirname, '..');
|
|
29
|
-
const distDir = path.join(pkgRoot, 'dist');
|
|
30
|
-
const distAssets = path.join(distDir, 'assets');
|
|
31
|
-
const distOffscreen = path.join(distDir, 'extension', 'zerogpu-offscreen.js');
|
|
32
|
-
const distBackground = path.join(distDir, 'extension', 'zerogpu-background.js');
|
|
33
|
-
const distSettingsLink = path.join(distDir, 'extension', 'zerogpu-settings-link.js');
|
|
34
|
-
// The inference worker: MV3 refuses blob: workers, so it must be a real file.
|
|
35
|
-
const distWorker = path.join(distAssets, 'zerogpu-worker.js');
|
|
36
|
-
|
|
37
|
-
const HOST_PERMISSIONS = ['<all_urls>'];
|
|
38
|
-
const REQUIRED_CSP = "script-src 'self' 'wasm-unsafe-eval'; object-src 'self'";
|
|
39
|
-
|
|
40
|
-
// Origins allowed to reach the extension via chrome.runtime.sendMessage, i.e.
|
|
41
|
-
// the ZeroGPU-hosted end-user settings page where a user can turn sharing off.
|
|
42
|
-
// Must stay in step with DEFAULT_ALLOWED_ORIGINS in src/extension/background.ts.
|
|
43
|
-
const EXTERNALLY_CONNECTABLE_MATCHES = ['https://*.zerogpu.ai/*'];
|
|
44
|
-
|
|
45
|
-
const c = {
|
|
46
|
-
ok: (s) => `\x1b[32m${s}\x1b[0m`,
|
|
47
|
-
warn: (s) => `\x1b[33m${s}\x1b[0m`,
|
|
48
|
-
err: (s) => `\x1b[31m${s}\x1b[0m`,
|
|
49
|
-
dim: (s) => `\x1b[2m${s}\x1b[0m`
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
const log = (...a) => console.log(...a);
|
|
53
|
-
const die = (msg) => {
|
|
54
|
-
console.error(c.err(`✗ ${msg}`));
|
|
55
|
-
process.exit(1);
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
const parseFlags = (argv) => {
|
|
59
|
-
const flags = {};
|
|
60
|
-
const positional = [];
|
|
61
|
-
for (let i = 0; i < argv.length; i += 1) {
|
|
62
|
-
const arg = argv[i];
|
|
63
|
-
if (arg.startsWith('--')) {
|
|
64
|
-
const key = arg.slice(2);
|
|
65
|
-
const next = argv[i + 1];
|
|
66
|
-
if (next && !next.startsWith('--')) {
|
|
67
|
-
flags[key] = next;
|
|
68
|
-
i += 1;
|
|
69
|
-
} else {
|
|
70
|
-
flags[key] = true;
|
|
71
|
-
}
|
|
72
|
-
} else {
|
|
73
|
-
positional.push(arg);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
return { flags, positional };
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
const ensureDistPresent = () => {
|
|
80
|
-
if (
|
|
81
|
-
!existsSync(distOffscreen) ||
|
|
82
|
-
!existsSync(distBackground) ||
|
|
83
|
-
!existsSync(distSettingsLink) ||
|
|
84
|
-
!existsSync(distAssets) ||
|
|
85
|
-
!existsSync(distWorker)
|
|
86
|
-
) {
|
|
87
|
-
die(
|
|
88
|
-
'Built SDK assets not found. Reinstall @zerogpu/browser-extension-sdk (its dist/ ships the runtime).'
|
|
89
|
-
);
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
// --- manifest helpers (shared by vendor + init) -------------------------------
|
|
94
|
-
|
|
95
|
-
const uniqueMerge = (existing, additions) => {
|
|
96
|
-
const out = Array.isArray(existing) ? [...existing] : [];
|
|
97
|
-
for (const item of additions) {
|
|
98
|
-
if (!out.includes(item)) out.push(item);
|
|
99
|
-
}
|
|
100
|
-
return out;
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Merge the ZeroGPU settings-page origin into `externally_connectable.matches`.
|
|
105
|
-
* Merged, never replaced — a host may already allow its own site, and its `ids`
|
|
106
|
-
* entry is left alone. Returns whether anything actually changed, so a caller
|
|
107
|
-
* that runs on every build can leave a correct manifest untouched.
|
|
108
|
-
*
|
|
109
|
-
* Creating the key is not a neutral act. Chrome reads an absent
|
|
110
|
-
* `externally_connectable` as "every extension may connect, no web page may";
|
|
111
|
-
* once the key exists, an unspecified `ids` means "no extension may". So adding
|
|
112
|
-
* only `matches` to a manifest that had no key would quietly revoke
|
|
113
|
-
* extension-to-extension messaging the host still has today. `ids: ["*"]`
|
|
114
|
-
* records what was already in force rather than granting anything new.
|
|
115
|
-
*
|
|
116
|
-
* A host that declared the key itself has already made that choice — even as an
|
|
117
|
-
* empty object — so `ids` is never added to one that exists.
|
|
118
|
-
*/
|
|
119
|
-
const ensureExternallyConnectable = (manifest) => {
|
|
120
|
-
const declared = manifest.externally_connectable;
|
|
121
|
-
const existing = declared?.matches;
|
|
122
|
-
const merged = uniqueMerge(existing, EXTERNALLY_CONNECTABLE_MATCHES);
|
|
123
|
-
if (Array.isArray(existing) && merged.length === existing.length) return false;
|
|
124
|
-
if (!declared) {
|
|
125
|
-
manifest.externally_connectable = { matches: merged, ids: ['*'] };
|
|
126
|
-
return true;
|
|
127
|
-
}
|
|
128
|
-
declared.matches = merged;
|
|
129
|
-
return true;
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* The file's own indentation, so rewriting it produces a one-line diff instead
|
|
134
|
-
* of reformatting a manifest the host may keep at 4 spaces or tabs.
|
|
135
|
-
*/
|
|
136
|
-
const detectIndent = (raw) => {
|
|
137
|
-
const match = raw.match(/\n([ \t]+)"/);
|
|
138
|
-
if (!match) return 2;
|
|
139
|
-
return match[1].includes('\t') ? '\t' : match[1].length;
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* The file's own line ending, for the same reason as the indentation: a manifest
|
|
144
|
-
* kept at CRLF must come back CRLF, or the one-key diff becomes a whole-file one.
|
|
145
|
-
* `JSON.stringify` only ever emits `\n`, and a newline inside a JSON string is
|
|
146
|
-
* escaped rather than literal, so rewriting every `\n` is safe.
|
|
147
|
-
*/
|
|
148
|
-
const detectEol = (raw) => (raw.includes('\r\n') ? '\r\n' : '\n');
|
|
149
|
-
|
|
150
|
-
// --- vendor ------------------------------------------------------------------
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Add the settings-page origin to the extension's manifest if it is missing.
|
|
154
|
-
*
|
|
155
|
-
* `vendor` is a build step, so this writes only when the entry is actually
|
|
156
|
-
* absent: once added, every later build is a no-op that never touches the file.
|
|
157
|
-
* It is here rather than in `init` alone because `init` runs once, at scaffold
|
|
158
|
-
* time — every extension integrated before the opt-out shipped would otherwise
|
|
159
|
-
* keep a manifest that silently makes the opt-out link a dead end.
|
|
160
|
-
*/
|
|
161
|
-
const patchExternallyConnectable = (root) => {
|
|
162
|
-
const file = path.join(root, 'manifest.json');
|
|
163
|
-
// Vendoring from a build directory is legitimate, and there is no way to guess
|
|
164
|
-
// where the real manifest lives. `doctor` is what reports the gap in that case.
|
|
165
|
-
if (!existsSync(file)) return;
|
|
166
|
-
|
|
167
|
-
const raw = readFileSync(file, 'utf8');
|
|
168
|
-
let manifest;
|
|
169
|
-
try {
|
|
170
|
-
manifest = JSON.parse(raw);
|
|
171
|
-
} catch {
|
|
172
|
-
log(c.warn('! manifest.json is not valid JSON — left unchanged.'));
|
|
173
|
-
return;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
if (!ensureExternallyConnectable(manifest)) return;
|
|
177
|
-
|
|
178
|
-
const eol = detectEol(raw);
|
|
179
|
-
const body = JSON.stringify(manifest, null, detectIndent(raw)).replace(/\n/g, eol);
|
|
180
|
-
writeFileSync(file, body + (raw.endsWith('\n') ? eol : ''));
|
|
181
|
-
log(c.ok(`✓ Added ${EXTERNALLY_CONNECTABLE_MATCHES.join(', ')} to manifest.json`));
|
|
182
|
-
log(c.dim(' externally_connectable — without it the ZeroGPU settings page cannot'));
|
|
183
|
-
log(c.dim(' reach this extension, so end users have no way to opt out.'));
|
|
184
|
-
};
|
|
185
|
-
|
|
186
|
-
const vendor = (outDir, manifestRoot = process.cwd()) => {
|
|
187
|
-
ensureDistPresent();
|
|
188
|
-
const target = path.resolve(process.cwd(), outDir || path.join('vendor', 'zerogpu'));
|
|
189
|
-
mkdirSync(target, { recursive: true });
|
|
190
|
-
|
|
191
|
-
// The offscreen host bundle (contains the SDK itself).
|
|
192
|
-
cpSync(distOffscreen, path.join(target, 'zerogpu-offscreen.js'));
|
|
193
|
-
// The service-worker consent bridge (tiny; no SDK runtime inside).
|
|
194
|
-
cpSync(distBackground, path.join(target, 'zerogpu-background.js'));
|
|
195
|
-
cpSync(distSettingsLink, path.join(target, 'zerogpu-settings-link.js'));
|
|
196
|
-
// All pinned runtime assets (transformers + ONNX wasm, wllama + wasm, manifest).
|
|
197
|
-
cpSync(distAssets, target, { recursive: true });
|
|
198
|
-
|
|
199
|
-
patchExternallyConnectable(manifestRoot);
|
|
200
|
-
|
|
201
|
-
const manifest = readAssetsManifest(target);
|
|
202
|
-
const rel = path.relative(process.cwd(), target) || '.';
|
|
203
|
-
log(c.ok(`✓ Vendored ZeroGPU runtime into ${rel}`));
|
|
204
|
-
if (manifest) {
|
|
205
|
-
log(
|
|
206
|
-
c.dim(
|
|
207
|
-
` sdk ${manifest.sdkVersion} · transformers ${manifest.transformersVersion} · wllama ${manifest.wllamaVersion}`
|
|
208
|
-
)
|
|
209
|
-
);
|
|
210
|
-
}
|
|
211
|
-
return target;
|
|
212
|
-
};
|
|
213
|
-
|
|
214
|
-
const readAssetsManifest = (dir) => {
|
|
215
|
-
const file = path.join(dir, 'zerogpu-assets.json');
|
|
216
|
-
if (!existsSync(file)) return null;
|
|
217
|
-
try {
|
|
218
|
-
return JSON.parse(readFileSync(file, 'utf8'));
|
|
219
|
-
} catch {
|
|
220
|
-
return null;
|
|
221
|
-
}
|
|
222
|
-
};
|
|
223
|
-
|
|
224
|
-
// --- init (scaffold) ---------------------------------------------------------
|
|
225
|
-
|
|
226
|
-
const OFFSCREEN_HTML = `<!DOCTYPE html>
|
|
227
|
-
<html>
|
|
228
|
-
<head><meta charset="utf-8" /><title>ZeroGPU offscreen</title></head>
|
|
229
|
-
<body>
|
|
230
|
-
<script type="module" src="offscreen.js"></script>
|
|
231
|
-
</body>
|
|
232
|
-
</html>
|
|
233
|
-
`;
|
|
234
|
-
|
|
235
|
-
const OFFSCREEN_JS = `import { initZeroGpuOffscreen } from "./vendor/zerogpu/zerogpu-offscreen.js";
|
|
236
|
-
|
|
237
|
-
// The offscreen document is where the SDK runs (it needs window/DOM + workers).
|
|
238
|
-
// It registers the device and starts automatically as soon as this document loads.
|
|
239
|
-
initZeroGpuOffscreen({
|
|
240
|
-
edgeOperatorKey: "REPLACE_WITH_YOUR_OPERATOR_KEY",
|
|
241
|
-
enableConsoleLogs: true
|
|
242
|
-
});
|
|
243
|
-
`;
|
|
244
|
-
|
|
245
|
-
const BACKGROUND_JS = `// ZeroGPU service worker. Owns the offscreen document where the SDK runs, and
|
|
246
|
-
// gates it on the end user's choice: while they are opted out, the document is
|
|
247
|
-
// never created, so the decision survives browser restarts. Merge into your
|
|
248
|
-
// existing service worker if you already have one.
|
|
249
|
-
importScripts("vendor/zerogpu/zerogpu-background.js");
|
|
250
|
-
|
|
251
|
-
// Installs the consent bridge: it answers the ZeroGPU-hosted settings page
|
|
252
|
-
// (via externally_connectable), answers your own popup/options page, reacts to
|
|
253
|
-
// storage changes, and starts the SDK on install/startup only if opted in.
|
|
254
|
-
ZeroGpuBackground.registerZeroGpuConsentBridge({
|
|
255
|
-
offscreenPath: "offscreen.html"
|
|
256
|
-
});
|
|
257
|
-
|
|
258
|
-
// Read or change the choice from your own UI:
|
|
259
|
-
// await ZeroGpuBackground.getZeroGpuConsent() -> { optedIn, updatedAt, source }
|
|
260
|
-
// await ZeroGpuBackground.setZeroGpuConsent(false) -> stops the SDK now
|
|
261
|
-
// ZeroGpuBackground.buildZeroGpuSettingsUrl({ extensionId: chrome.runtime.id })
|
|
262
|
-
//
|
|
263
|
-
// Use the model the device holds in your own extension (app-side inference, 0.5.0):
|
|
264
|
-
// await ZeroGpuBackground.getZeroGpuEligibleModels() -> every model of the list, judged for this device
|
|
265
|
-
// await ZeroGpuBackground.switchZeroGpuModel(modelId) -> { granted, activeModelId, reason? }
|
|
266
|
-
// await ZeroGpuBackground.runZeroGpuLocal({ text }) -> { response, modelId, location: "device", … }
|
|
267
|
-
// ZeroGpuBackground.onZeroGpuEvent(({ event, detail }) => {})
|
|
268
|
-
`;
|
|
269
|
-
|
|
270
|
-
const OPTIONS_HTML = `<!DOCTYPE html>
|
|
271
|
-
<html>
|
|
272
|
-
<head>
|
|
273
|
-
<meta charset="utf-8" />
|
|
274
|
-
<title>Settings</title>
|
|
275
|
-
<style>
|
|
276
|
-
body { font-family: system-ui, sans-serif; margin: 2rem; max-width: 34rem; line-height: 1.5; }
|
|
277
|
-
.row { display: flex; align-items: flex-start; gap: .75rem; margin-top: 1.5rem; }
|
|
278
|
-
.muted { color: #666; font-size: .875rem; }
|
|
279
|
-
.error { color: #b3261e; font-weight: 500; }
|
|
280
|
-
</style>
|
|
281
|
-
</head>
|
|
282
|
-
<body>
|
|
283
|
-
<h1>Resource sharing</h1>
|
|
284
|
-
<p class="muted">
|
|
285
|
-
This app can use some of your device's free or spare resources, such as
|
|
286
|
-
unused computing power and network bandwidth. You can turn this off at any
|
|
287
|
-
time.
|
|
288
|
-
</p>
|
|
289
|
-
<div class="row">
|
|
290
|
-
<input type="checkbox" id="zerogpu-opt-in" />
|
|
291
|
-
<label for="zerogpu-opt-in">Allow this app to use my device's spare resources</label>
|
|
292
|
-
</div>
|
|
293
|
-
<p class="muted" id="zerogpu-status"></p>
|
|
294
|
-
<script src="options.js"></script>
|
|
295
|
-
</body>
|
|
296
|
-
</html>
|
|
297
|
-
`;
|
|
298
|
-
|
|
299
|
-
const OPTIONS_JS = `// Reads and writes the ZeroGPU consent flag through the service-worker bridge.
|
|
300
|
-
const box = document.getElementById("zerogpu-opt-in");
|
|
301
|
-
const status = document.getElementById("zerogpu-status");
|
|
302
|
-
|
|
303
|
-
// The last state the service worker confirmed, and which request confirmed it.
|
|
304
|
-
// A failed write puts the box back to this, instead of leaving the user's click
|
|
305
|
-
// standing as though it had saved.
|
|
306
|
-
let stored = null;
|
|
307
|
-
let storedSeq = 0;
|
|
308
|
-
|
|
309
|
-
// The bridge answers each request on its own: a read is a storage lookup, a
|
|
310
|
-
// write also waits for the offscreen document to start or stop. So the initial
|
|
311
|
-
// read can land after a write the user has already made. Only the newest
|
|
312
|
-
// request is allowed to paint; an older answer is stale by definition.
|
|
313
|
-
let latest = 0;
|
|
314
|
-
|
|
315
|
-
const request = (message, done) => {
|
|
316
|
-
const seq = ++latest;
|
|
317
|
-
chrome.runtime.sendMessage(message, (res) => {
|
|
318
|
-
// Read lastError either way — left unread, Chrome logs it as unchecked. It
|
|
319
|
-
// is also the only signal when the service worker never answers at all.
|
|
320
|
-
const error = chrome.runtime.lastError?.message;
|
|
321
|
-
// Too stale to paint is not too stale to be true. A write that landed while
|
|
322
|
-
// a newer one was in flight is still the freshest word on what is actually
|
|
323
|
-
// stored, and dropping it would leave a later failure restoring the box to
|
|
324
|
-
// a state two toggles old. Only an older answer than the one already
|
|
325
|
-
// recorded is discarded.
|
|
326
|
-
if (res?.ok && seq > storedSeq) {
|
|
327
|
-
stored = res.consent;
|
|
328
|
-
storedSeq = seq;
|
|
329
|
-
}
|
|
330
|
-
if (seq === latest) done(res, error);
|
|
331
|
-
});
|
|
332
|
-
};
|
|
333
|
-
|
|
334
|
-
const setStatus = (text, isError) => {
|
|
335
|
-
status.textContent = text;
|
|
336
|
-
status.classList.toggle("error", !!isError);
|
|
337
|
-
};
|
|
338
|
-
|
|
339
|
-
const render = (consent) => {
|
|
340
|
-
box.indeterminate = false;
|
|
341
|
-
box.checked = !!consent?.optedIn;
|
|
342
|
-
setStatus(
|
|
343
|
-
consent?.updatedAt
|
|
344
|
-
? \`Last changed \${new Date(consent.updatedAt).toLocaleString()}.\`
|
|
345
|
-
: "",
|
|
346
|
-
false
|
|
347
|
-
);
|
|
348
|
-
};
|
|
349
|
-
|
|
350
|
-
// Nothing was stored, so the SDK is still doing whatever it was doing before the
|
|
351
|
-
// click. Show that, and never a bare unchecked box — that would claim sharing is
|
|
352
|
-
// off while it is running.
|
|
353
|
-
const restore = (message) => {
|
|
354
|
-
if (stored) {
|
|
355
|
-
box.indeterminate = false;
|
|
356
|
-
box.checked = !!stored.optedIn;
|
|
357
|
-
} else {
|
|
358
|
-
box.indeterminate = true;
|
|
359
|
-
}
|
|
360
|
-
setStatus(message, true);
|
|
361
|
-
};
|
|
362
|
-
|
|
363
|
-
request({ type: "zerogpu:getConsent" }, (res, error) => {
|
|
364
|
-
if (res?.ok) {
|
|
365
|
-
render(res.consent);
|
|
366
|
-
return;
|
|
367
|
-
}
|
|
368
|
-
restore(error || res?.error || "Could not read your current setting.");
|
|
369
|
-
});
|
|
370
|
-
|
|
371
|
-
box.addEventListener("change", () => {
|
|
372
|
-
request({ type: "zerogpu:setConsent", optedIn: box.checked }, (res, error) => {
|
|
373
|
-
if (res?.ok) {
|
|
374
|
-
render(res.consent);
|
|
375
|
-
return;
|
|
376
|
-
}
|
|
377
|
-
restore(error || res?.error || "Could not save your choice — sharing is unchanged.");
|
|
378
|
-
});
|
|
379
|
-
});
|
|
380
|
-
`;
|
|
381
|
-
|
|
382
|
-
const patchManifest = (dir) => {
|
|
383
|
-
const file = path.join(dir, 'manifest.json');
|
|
384
|
-
if (!existsSync(file)) {
|
|
385
|
-
log(c.warn(' ! manifest.json not found — skipping manifest patch.'));
|
|
386
|
-
return;
|
|
387
|
-
}
|
|
388
|
-
const manifest = JSON.parse(readFileSync(file, 'utf8'));
|
|
389
|
-
manifest.permissions = uniqueMerge(manifest.permissions, ['offscreen', 'storage']);
|
|
390
|
-
manifest.host_permissions = uniqueMerge(manifest.host_permissions, HOST_PERMISSIONS);
|
|
391
|
-
manifest.content_security_policy = manifest.content_security_policy || {};
|
|
392
|
-
if (!manifest.content_security_policy.extension_pages) {
|
|
393
|
-
manifest.content_security_policy.extension_pages = REQUIRED_CSP;
|
|
394
|
-
}
|
|
395
|
-
if (!manifest.background) {
|
|
396
|
-
manifest.background = { service_worker: 'background.js' };
|
|
397
|
-
}
|
|
398
|
-
// Lets the ZeroGPU-hosted settings page message this extension so an end user
|
|
399
|
-
// can turn sharing off. No content script and no extra host permission needed.
|
|
400
|
-
ensureExternallyConnectable(manifest);
|
|
401
|
-
if (!manifest.options_page && !manifest.options_ui) {
|
|
402
|
-
manifest.options_ui = { page: 'options.html', open_in_tab: true };
|
|
403
|
-
}
|
|
404
|
-
writeFileSync(file, JSON.stringify(manifest, null, 2) + '\n');
|
|
405
|
-
log(c.ok('✓ Patched manifest.json (permissions, CSP, host permissions)'));
|
|
406
|
-
};
|
|
407
|
-
|
|
408
|
-
const scaffoldFile = (dir, name, contents) => {
|
|
409
|
-
const file = path.join(dir, name);
|
|
410
|
-
if (existsSync(file)) {
|
|
411
|
-
log(c.dim(` • ${name} exists — left unchanged`));
|
|
412
|
-
return;
|
|
413
|
-
}
|
|
414
|
-
writeFileSync(file, contents);
|
|
415
|
-
log(c.ok(`✓ Created ${name}`));
|
|
416
|
-
};
|
|
417
|
-
|
|
418
|
-
const init = (dir) => {
|
|
419
|
-
const root = path.resolve(process.cwd(), dir || '.');
|
|
420
|
-
if (!existsSync(root)) die(`Directory not found: ${root}`);
|
|
421
|
-
vendor(path.join(root, 'vendor', 'zerogpu'), root);
|
|
422
|
-
scaffoldFile(root, 'offscreen.html', OFFSCREEN_HTML);
|
|
423
|
-
scaffoldFile(root, 'offscreen.js', OFFSCREEN_JS);
|
|
424
|
-
scaffoldFile(root, 'background.js', BACKGROUND_JS);
|
|
425
|
-
scaffoldFile(root, 'options.html', OPTIONS_HTML);
|
|
426
|
-
scaffoldFile(root, 'options.js', OPTIONS_JS);
|
|
427
|
-
patchManifest(root);
|
|
428
|
-
log('');
|
|
429
|
-
log(c.ok('ZeroGPU integrated.') + ' Next:');
|
|
430
|
-
log(' 1. Put your operator key in offscreen.js (REPLACE_WITH_YOUR_OPERATOR_KEY).');
|
|
431
|
-
log(' 2. Load the extension unpacked — it registers automatically on startup.');
|
|
432
|
-
log(' 3. Re-run ' + c.dim('npx zerogpu vendor') + ' in your build to stay up to date.');
|
|
433
|
-
log(' 4. Give users the opt-out link (options.html ships one, or link them to');
|
|
434
|
-
log(' ' + c.dim('https://zerogpu.ai/extension-settings?browser=chrome&extension_id=<your id>') + ').');
|
|
435
|
-
};
|
|
436
|
-
|
|
437
|
-
// --- doctor ------------------------------------------------------------------
|
|
438
|
-
|
|
439
|
-
/**
|
|
440
|
-
* A mention is not a call. Block comments and whole-line `//` comments are
|
|
441
|
-
* dropped first, so a commented-out invocation — the shape someone leaves
|
|
442
|
-
* behind while debugging — is not read as wired up. Mid-line `//` is left
|
|
443
|
-
* alone: stripping it would eat the `//` of any URL in a string literal.
|
|
444
|
-
*/
|
|
445
|
-
const callsConsentBridge = (source) => {
|
|
446
|
-
const code = source
|
|
447
|
-
.replace(/\/\*[\s\S]*?\*\//g, '')
|
|
448
|
-
.split('\n')
|
|
449
|
-
.filter((line) => !/^\s*\/\//.test(line))
|
|
450
|
-
.join('\n');
|
|
451
|
-
return /registerZeroGpuConsentBridge\s*\(/.test(code);
|
|
452
|
-
};
|
|
453
|
-
|
|
454
|
-
const doctor = (dir) => {
|
|
455
|
-
const root = path.resolve(process.cwd(), dir || '.');
|
|
456
|
-
const vendorDir = path.join(root, 'vendor', 'zerogpu');
|
|
457
|
-
let problems = 0;
|
|
458
|
-
|
|
459
|
-
const installed = readAssetsManifest(distAssets);
|
|
460
|
-
const vendored = readAssetsManifest(vendorDir);
|
|
461
|
-
|
|
462
|
-
if (!vendored) {
|
|
463
|
-
log(c.err('✗ No vendored assets found. Run: npx zerogpu vendor'));
|
|
464
|
-
problems += 1;
|
|
465
|
-
} else {
|
|
466
|
-
for (const f of vendored.files || []) {
|
|
467
|
-
if (!existsSync(path.join(vendorDir, f))) {
|
|
468
|
-
log(c.err(`✗ Missing vendored file: ${f}`));
|
|
469
|
-
problems += 1;
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
if (installed && vendored.sdkVersion !== installed.sdkVersion) {
|
|
473
|
-
log(
|
|
474
|
-
c.warn(
|
|
475
|
-
`! Vendored SDK ${vendored.sdkVersion} differs from installed ${installed.sdkVersion}. Run: npx zerogpu vendor`
|
|
476
|
-
)
|
|
477
|
-
);
|
|
478
|
-
problems += 1;
|
|
479
|
-
} else if (installed) {
|
|
480
|
-
log(c.ok(`✓ Vendored assets in sync (sdk ${vendored.sdkVersion})`));
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
const manifestFile = path.join(root, 'manifest.json');
|
|
485
|
-
let manifest = null;
|
|
486
|
-
if (!existsSync(manifestFile)) {
|
|
487
|
-
log(c.warn('! manifest.json not found.'));
|
|
488
|
-
} else {
|
|
489
|
-
manifest = JSON.parse(readFileSync(manifestFile, 'utf8'));
|
|
490
|
-
const csp = manifest.content_security_policy?.extension_pages || '';
|
|
491
|
-
if (!csp.includes("'wasm-unsafe-eval'")) {
|
|
492
|
-
log(c.err("✗ CSP extension_pages missing 'wasm-unsafe-eval'"));
|
|
493
|
-
problems += 1;
|
|
494
|
-
}
|
|
495
|
-
if (!(manifest.permissions || []).includes('offscreen')) {
|
|
496
|
-
log(c.err('✗ manifest permissions missing "offscreen"'));
|
|
497
|
-
problems += 1;
|
|
498
|
-
}
|
|
499
|
-
const hosts = [
|
|
500
|
-
...(manifest.host_permissions || []),
|
|
501
|
-
...(manifest.optional_host_permissions || [])
|
|
502
|
-
];
|
|
503
|
-
if (!hosts.some((h) => h === '<all_urls>' || h.includes('zerogpu.ai'))) {
|
|
504
|
-
log(c.warn('! No zerogpu.ai host permission found (register/WS may be blocked).'));
|
|
505
|
-
}
|
|
506
|
-
// Without this the hosted settings page cannot reach the extension, so an
|
|
507
|
-
// end user has no way to opt out from the web.
|
|
508
|
-
const connectable = manifest.externally_connectable?.matches || [];
|
|
509
|
-
if (!connectable.some((m) => m.includes('zerogpu.ai'))) {
|
|
510
|
-
log(
|
|
511
|
-
c.err(
|
|
512
|
-
'✗ manifest externally_connectable.matches missing a zerogpu.ai entry (end users cannot opt out from the settings page)'
|
|
513
|
-
)
|
|
514
|
-
);
|
|
515
|
-
problems += 1;
|
|
516
|
-
}
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
// Check the worker the manifest actually loads. An extension that already had
|
|
520
|
-
// a service worker keeps its own path, and that merged file — not a root
|
|
521
|
-
// background.js — is where the bridge has to be registered.
|
|
522
|
-
const workerPath = manifest?.background?.service_worker || 'background.js';
|
|
523
|
-
const backgroundFile = path.join(root, workerPath);
|
|
524
|
-
if (!existsSync(backgroundFile)) {
|
|
525
|
-
log(c.err(`✗ Service worker not found: ${workerPath}`));
|
|
526
|
-
problems += 1;
|
|
527
|
-
} else {
|
|
528
|
-
const background = readFileSync(backgroundFile, 'utf8');
|
|
529
|
-
if (!callsConsentBridge(background)) {
|
|
530
|
-
// Say which of the two it is — "does not call" reads like a doctor bug to
|
|
531
|
-
// someone who can see the name right there in their file.
|
|
532
|
-
const commentedOut = background.includes('registerZeroGpuConsentBridge');
|
|
533
|
-
log(
|
|
534
|
-
c.warn(
|
|
535
|
-
`! ${workerPath} ${
|
|
536
|
-
commentedOut
|
|
537
|
-
? 'mentions registerZeroGpuConsentBridge but never calls it'
|
|
538
|
-
: 'does not call registerZeroGpuConsentBridge'
|
|
539
|
-
} — the SDK will start regardless of the user's choice.`
|
|
540
|
-
)
|
|
541
|
-
);
|
|
542
|
-
problems += 1;
|
|
543
|
-
}
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
if (problems === 0) {
|
|
547
|
-
log(c.ok('✓ ZeroGPU integration looks healthy.'));
|
|
548
|
-
} else {
|
|
549
|
-
process.exitCode = 1;
|
|
550
|
-
}
|
|
551
|
-
};
|
|
552
|
-
|
|
553
|
-
// --- main --------------------------------------------------------------------
|
|
554
|
-
|
|
555
|
-
const main = () => {
|
|
556
|
-
const [command, ...rest] = process.argv.slice(2);
|
|
557
|
-
const { flags, positional } = parseFlags(rest);
|
|
558
|
-
switch (command) {
|
|
559
|
-
case 'vendor':
|
|
560
|
-
vendor(positional[0]);
|
|
561
|
-
break;
|
|
562
|
-
case 'init':
|
|
563
|
-
init(flags.dir);
|
|
564
|
-
break;
|
|
565
|
-
case 'doctor':
|
|
566
|
-
doctor(flags.dir);
|
|
567
|
-
break;
|
|
568
|
-
case undefined:
|
|
569
|
-
case 'help':
|
|
570
|
-
case '--help':
|
|
571
|
-
case '-h':
|
|
572
|
-
log(`zerogpu — integrate the ZeroGPU SDK into a Chrome (MV3) extension
|
|
573
|
-
|
|
574
|
-
Usage:
|
|
575
|
-
npx zerogpu vendor [outDir] Copy SDK + model runtimes into the extension
|
|
576
|
-
(default: vendor/zerogpu) + add the opt-out origin
|
|
577
|
-
to manifest.json. Run this in your build.
|
|
578
|
-
npx zerogpu init [--dir .] Vendor + scaffold offscreen/background + patch manifest
|
|
579
|
-
npx zerogpu doctor [--dir .] Verify the integration is wired and in sync
|
|
580
|
-
`);
|
|
581
|
-
break;
|
|
582
|
-
default:
|
|
583
|
-
die(`Unknown command: ${command}. Run "npx zerogpu help".`);
|
|
584
|
-
}
|
|
585
|
-
};
|
|
586
|
-
|
|
587
|
-
main();
|
|
8
|
+
runCli('zerogpu');
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"sdkVersion": "0.6.0+17c3989",
|
|
3
|
+
"channel": "official",
|
|
4
|
+
"transformersVersion": "2.17.2",
|
|
5
|
+
"wllamaVersion": "2.3.5",
|
|
6
|
+
"files": [
|
|
7
|
+
"herogpu-offscreen.js",
|
|
8
|
+
"herogpu-background.js",
|
|
9
|
+
"herogpu-settings-link.js",
|
|
10
|
+
"herogpu-worker.js",
|
|
11
|
+
"transformers.min.js",
|
|
12
|
+
"ort-wasm.wasm",
|
|
13
|
+
"ort-wasm-simd.wasm",
|
|
14
|
+
"ort-wasm-threaded.wasm",
|
|
15
|
+
"ort-wasm-simd-threaded.wasm",
|
|
16
|
+
"wllama/index.js",
|
|
17
|
+
"wllama/single-thread/wllama.wasm",
|
|
18
|
+
"wllama/multi-thread/wllama.wasm"
|
|
19
|
+
],
|
|
20
|
+
"builtAt": "2026-12-03T13:52:23.789Z"
|
|
21
|
+
}
|