@jeevanand/agent-browserbox-runtime 0.1.1 → 0.2.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 +18 -0
- package/dist/assets/{bridge-DY2WnNx7.js → bridge-BEwLXp_5.js} +2 -2
- package/dist/assets/{build-BNa5TW3M.js → build-DqisMe_5.js} +2 -2
- package/dist/assets/bundle-qe7w2kMM.js +1 -0
- package/dist/assets/{dev-impl-BRiXVAGb.js → dev-impl-DLIrrRxu.js} +4 -4
- package/dist/assets/embed-CqexLx9X.js +3 -0
- package/dist/assets/{html-R2xYS1Rp.js → html-BggiUWIu.js} +3 -3
- package/dist/assets/{instance-CppC8T8t.js → instance-6eYt0v1_.js} +1 -1
- package/dist/assets/main-DzFuvDws.js +371 -0
- package/dist/assets/path-DGG-Mxbd.js +1 -0
- package/dist/assets/{pkg-esbuild-BuQjm7dB.js → pkg-esbuild-QrU0ybpL.js} +1 -1
- package/dist/assets/{pkg-git-jawfIVoq.js → pkg-git-BQFdAkWZ.js} +9 -9
- package/dist/assets/pkg-python-ytZLbJ8W.js +6 -0
- package/dist/assets/{pkg-sqlite-BexM5lLo.js → pkg-sqlite-CKGWzcwt.js} +1 -1
- package/dist/assets/pkg-zip-CL-568aV.js +1 -0
- package/dist/assets/rolldown-runtime-WkwZ5CU-.js +1 -0
- package/dist/assets/server-B0hTONK2.js +4 -0
- package/dist/embed/index.html +5 -5
- package/dist/kernel/bridge.html +2 -2
- package/dist/kernel/packages.json +1 -1
- package/lib/client/connect.d.ts +23 -0
- package/lib/client/connect.js +64 -0
- package/lib/client/index.d.ts +23 -0
- package/lib/client/index.js +16 -0
- package/lib/client/remote-kernel.d.ts +53 -0
- package/lib/client/remote-kernel.js +315 -0
- package/lib/config/origins.d.ts +13 -0
- package/lib/config/origins.js +35 -0
- package/lib/kernel/api.d.ts +145 -0
- package/lib/kernel/api.js +1 -0
- package/lib/kernel/bus.d.ts +6 -0
- package/lib/kernel/bus.js +24 -0
- package/lib/kernel/caps.d.ts +10 -0
- package/lib/kernel/caps.js +50 -0
- package/lib/kernel/dev/types.d.ts +71 -0
- package/lib/kernel/dev/types.js +8 -0
- package/lib/kernel/filetypes.d.ts +9 -0
- package/lib/kernel/filetypes.js +74 -0
- package/lib/kernel/format.d.ts +3 -0
- package/lib/kernel/format.js +23 -0
- package/lib/kernel/instance.d.ts +22 -0
- package/lib/kernel/instance.js +29 -0
- package/lib/kernel/kcore.d.ts +26 -0
- package/lib/kernel/kcore.js +105 -0
- package/lib/kernel/kcore.wasm +0 -0
- package/lib/kernel/paths.d.ts +3 -0
- package/lib/kernel/paths.js +3 -0
- package/lib/kernel/perms.d.ts +35 -0
- package/lib/kernel/perms.js +42 -0
- package/lib/kernel/proc.d.ts +45 -0
- package/lib/kernel/proc.js +69 -0
- package/lib/kernel/settings.d.ts +29 -0
- package/lib/kernel/settings.js +94 -0
- package/lib/kernel/types.d.ts +48 -0
- package/lib/kernel/types.js +1 -0
- package/lib/kernel/vfs/path.d.ts +11 -0
- package/lib/kernel/vfs/path.js +72 -0
- package/lib/kernel/vfs/types.d.ts +34 -0
- package/lib/kernel/vfs/types.js +17 -0
- package/lib/kernel-worker/wire.d.ts +38 -0
- package/lib/kernel-worker/wire.js +2 -0
- package/lib/rpc/peer.d.ts +12 -0
- package/lib/rpc/peer.js +85 -0
- package/lib/sandbox/isolation.d.ts +8 -0
- package/lib/sandbox/isolation.js +12 -0
- package/lib/shell/ansi.d.ts +15 -0
- package/lib/shell/ansi.js +23 -0
- package/package.json +8 -3
- package/dist/assets/anthropic-S3ckT02t.js +0 -17
- package/dist/assets/bundle-8opp8__C.js +0 -1
- package/dist/assets/embed-CMbAHE2J.js +0 -3
- package/dist/assets/github-Dwhv0h55.js +0 -2
- package/dist/assets/lib-aA6_aWm_.js +0 -68
- package/dist/assets/main-VEpSfsM_.js +0 -482
- package/dist/assets/node.browser-1OJr47If.js +0 -1
- package/dist/assets/node.browser-cOXEGsLT.js +0 -1
- package/dist/assets/openai-CThAXVqN.js +0 -3
- package/dist/assets/pkg-python-CoNK0IjJ.js +0 -6
- package/dist/assets/pkg-zip-BdZ_Lkdk.js +0 -1
- package/dist/assets/publish-CHt_Qrq5.js +0 -2
- package/dist/assets/publish-impl-BqjdM7Ux.js +0 -7
- package/dist/assets/rolldown-runtime-XKyaHtl9.js +0 -1
- package/dist/assets/server-C5Sd62CK.js +0 -4
- package/dist/assets/webllm-wsrhCAP4.js +0 -11
- package/dist/icon.svg +0 -1
- package/dist/manifest.webmanifest +0 -10
- package/dist/preview/frame.html +0 -27
- package/dist/sw.js +0 -73
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
import { Bus } from '../kernel/bus';
|
|
2
|
+
import { detectCaps } from '../kernel/caps';
|
|
3
|
+
import { KCore } from '../kernel/kcore';
|
|
4
|
+
import { Permissions } from '../kernel/perms';
|
|
5
|
+
import { Settings } from '../kernel/settings';
|
|
6
|
+
import { normalize } from '../kernel/vfs/path';
|
|
7
|
+
import { Peer } from '../rpc/peer';
|
|
8
|
+
const NEVER = new AbortController().signal;
|
|
9
|
+
const dec = new TextDecoder();
|
|
10
|
+
/**
|
|
11
|
+
* A window's handle on the kernel. Read-mostly state (settings, the process
|
|
12
|
+
* table, mounts) is mirrored locally so UI code can read it synchronously;
|
|
13
|
+
* everything else is an RPC to the kernel worker. It implements the same
|
|
14
|
+
* `Kernel` interface as the real kernel, so apps cannot tell the difference.
|
|
15
|
+
*/
|
|
16
|
+
export class RemoteKernel {
|
|
17
|
+
version = '';
|
|
18
|
+
rootKind = 'opfs';
|
|
19
|
+
/** False when this window had to start a private kernel. */
|
|
20
|
+
shared = true;
|
|
21
|
+
bootStart = 0;
|
|
22
|
+
bootMarks = [];
|
|
23
|
+
bus = new Bus();
|
|
24
|
+
settings = new Settings(this.bus, (key, value) => void this.call('settings.set', key, value));
|
|
25
|
+
perms = new Permissions(this.settings);
|
|
26
|
+
/** Not loaded here: windows use its JS fallbacks; the WASM core runs in the kernel. */
|
|
27
|
+
kcore = new KCore();
|
|
28
|
+
caps = detectCaps();
|
|
29
|
+
ready;
|
|
30
|
+
/** The kernel log as of connecting, kept current by `kmsg` events. */
|
|
31
|
+
kernelLog = [];
|
|
32
|
+
#peer;
|
|
33
|
+
#procs = new Map();
|
|
34
|
+
#owned = new Map();
|
|
35
|
+
#nextPid = 0;
|
|
36
|
+
#mounts = [];
|
|
37
|
+
#shells = new Map();
|
|
38
|
+
#runs = new Map();
|
|
39
|
+
#nextRun = 1;
|
|
40
|
+
#asking = new Map();
|
|
41
|
+
constructor(link, role = 'ui') {
|
|
42
|
+
this.#peer = link.then(({ port, lock, shared }) => {
|
|
43
|
+
this.shared = shared;
|
|
44
|
+
const peer = new Peer(port);
|
|
45
|
+
this.#wire(peer);
|
|
46
|
+
peer.emit('alive', lock);
|
|
47
|
+
return peer;
|
|
48
|
+
});
|
|
49
|
+
this.ready = this.#peer.then(async (peer) => this.#hydrate(await peer.call('hello', role)));
|
|
50
|
+
}
|
|
51
|
+
/** Calls a kernel method once connected. */
|
|
52
|
+
async call(method, ...args) {
|
|
53
|
+
return (await this.#peer).call(method, ...args);
|
|
54
|
+
}
|
|
55
|
+
/** Lets the kernel call into this window (used by the sandbox host). */
|
|
56
|
+
async handle(handlers) {
|
|
57
|
+
return (await this.#peer).handle(handlers);
|
|
58
|
+
}
|
|
59
|
+
#hydrate(hello) {
|
|
60
|
+
this.version = hello.version;
|
|
61
|
+
this.rootKind = hello.rootKind;
|
|
62
|
+
this.#nextPid = hello.pidBase + 1;
|
|
63
|
+
this.#mounts = hello.mounts.map((m) => ({ point: m.point, fs: { label: m.label, readonly: m.readonly } }));
|
|
64
|
+
this.bootMarks.push(...hello.bootMarks.map((m) => ({ name: `kernel:${m.name}`, at: m.at })));
|
|
65
|
+
// Lines that arrived as events before this reply are also in it.
|
|
66
|
+
this.kernelLog.splice(0, this.kernelLog.length, ...hello.log);
|
|
67
|
+
this.settings.hydrate(hello.settings);
|
|
68
|
+
for (const p of hello.procs)
|
|
69
|
+
this.bus.emit('proc:spawn', this.#mirror(p));
|
|
70
|
+
this.mark('kernel-connected');
|
|
71
|
+
}
|
|
72
|
+
#mirror(w) {
|
|
73
|
+
const existing = this.#procs.get(w.pid);
|
|
74
|
+
if (existing)
|
|
75
|
+
return Object.assign(existing, w);
|
|
76
|
+
const proc = { ...w, signal: NEVER };
|
|
77
|
+
this.#procs.set(w.pid, proc);
|
|
78
|
+
return proc;
|
|
79
|
+
}
|
|
80
|
+
#wire(peer) {
|
|
81
|
+
peer.on('bus', ({ type, payload }) => {
|
|
82
|
+
if (type === 'settings:change') {
|
|
83
|
+
const { key, value } = payload;
|
|
84
|
+
this.settings.applyRemote(key, value);
|
|
85
|
+
}
|
|
86
|
+
else if (type === 'proc:spawn' || type === 'proc:update') {
|
|
87
|
+
this.bus.emit(type, this.#mirror(payload));
|
|
88
|
+
}
|
|
89
|
+
else if (type === 'proc:exit') {
|
|
90
|
+
const { proc, reason } = payload;
|
|
91
|
+
const local = this.#mirror(proc);
|
|
92
|
+
this.#procs.delete(proc.pid);
|
|
93
|
+
this.#reap(proc.pid, reason);
|
|
94
|
+
this.bus.emit('proc:exit', { proc: local, reason });
|
|
95
|
+
}
|
|
96
|
+
else if (type === 'fs:change' && payload.type === 'mount') {
|
|
97
|
+
void peer.call('fs.mounts').then((list) => {
|
|
98
|
+
this.#mounts = list.map((m) => ({ point: m.point, fs: { label: m.label, readonly: m.readonly } }));
|
|
99
|
+
this.bus.emit(type, payload);
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
if (type === 'kmsg' && !this.kernelLog.some((l) => l.t === payload.t && l.msg === payload.msg))
|
|
104
|
+
this.kernelLog.push(payload);
|
|
105
|
+
this.bus.emit(type, payload);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
peer.on('proc:kill', (pid) => this.#reap(pid, 'killed'));
|
|
109
|
+
peer.on('shell:out', ({ runId, s, t }) => {
|
|
110
|
+
const io = this.#runs.get(runId);
|
|
111
|
+
(s === 'o' ? io?.out : io?.err)?.(t);
|
|
112
|
+
});
|
|
113
|
+
peer.on('shell:clear', (id) => this.#shells.get(id)?.clear?.());
|
|
114
|
+
peer.on('ask', (ask) => void this.#answer(peer, ask));
|
|
115
|
+
peer.on('ask:done', (askId) => {
|
|
116
|
+
this.#asking.get(askId)?.abort();
|
|
117
|
+
this.#asking.delete(askId);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
/** The kernel needs something only a window can provide: a decision, an answer, a line of input. */
|
|
121
|
+
async #answer(peer, ask) {
|
|
122
|
+
const ctl = new AbortController();
|
|
123
|
+
this.#asking.set(ask.askId, ctl);
|
|
124
|
+
let value;
|
|
125
|
+
try {
|
|
126
|
+
if (ask.kind === 'perm') {
|
|
127
|
+
value = await this.perms.prompt(ask.payload, ctl.signal);
|
|
128
|
+
}
|
|
129
|
+
else if (ask.kind === 'user') {
|
|
130
|
+
return; // agents ask through their own service (the app's agent client)
|
|
131
|
+
}
|
|
132
|
+
else if (ask.kind === 'tty') {
|
|
133
|
+
const reader = ask.shellId ? this.#shells.get(ask.shellId)?.reader : undefined;
|
|
134
|
+
const { prompt, secret } = ask.payload;
|
|
135
|
+
value = reader ? await reader(prompt, secret ?? false) : null;
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
const { method, args } = ask.payload;
|
|
139
|
+
if (method === 'clipboard.write')
|
|
140
|
+
value = await navigator.clipboard.writeText(String(args[0]));
|
|
141
|
+
else if (method === 'clipboard.read')
|
|
142
|
+
value = await navigator.clipboard.readText();
|
|
143
|
+
else if (method === 'speak')
|
|
144
|
+
speechSynthesis.speak(new SpeechSynthesisUtterance(String(args[0])));
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
catch (err) {
|
|
148
|
+
if (ctl.signal.aborted)
|
|
149
|
+
return;
|
|
150
|
+
value = ask.kind === 'perm' ? 'deny' : ask.kind === 'ui' ? `failed: ${err.message}` : null;
|
|
151
|
+
}
|
|
152
|
+
if (ctl.signal.aborted)
|
|
153
|
+
return;
|
|
154
|
+
this.#asking.delete(ask.askId);
|
|
155
|
+
void peer.call('answer', ask.askId, value);
|
|
156
|
+
}
|
|
157
|
+
/** Local cleanup for one of this window's own processes. */
|
|
158
|
+
#reap(pid, reason) {
|
|
159
|
+
const o = this.#owned.get(pid);
|
|
160
|
+
if (!o)
|
|
161
|
+
return;
|
|
162
|
+
this.#owned.delete(pid);
|
|
163
|
+
o.ctl.abort(new DOMException(reason, 'AbortError'));
|
|
164
|
+
try {
|
|
165
|
+
o.onKill?.();
|
|
166
|
+
}
|
|
167
|
+
catch (err) {
|
|
168
|
+
console.error('[procs] onKill threw', err);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
procs = {
|
|
172
|
+
spawn: (opts) => {
|
|
173
|
+
if (!this.#nextPid)
|
|
174
|
+
throw new Error('kernel not connected yet');
|
|
175
|
+
const ctl = new AbortController();
|
|
176
|
+
const proc = { pid: this.#nextPid++, ppid: opts.ppid ?? 0, name: opts.name, kind: opts.kind, started: Date.now(), state: 'running', detail: opts.detail ?? '', cpuMs: 0, memBytes: 0, signal: ctl.signal };
|
|
177
|
+
this.#procs.set(proc.pid, proc);
|
|
178
|
+
this.#owned.set(proc.pid, { proc, ctl, onKill: opts.onKill });
|
|
179
|
+
const { signal: _signal, ...wire } = proc;
|
|
180
|
+
void this.call('procs.spawn', wire);
|
|
181
|
+
return proc;
|
|
182
|
+
},
|
|
183
|
+
update: (pid, patch) => {
|
|
184
|
+
const p = this.#procs.get(pid);
|
|
185
|
+
if (p)
|
|
186
|
+
Object.assign(p, patch);
|
|
187
|
+
void this.call('procs.update', pid, patch);
|
|
188
|
+
},
|
|
189
|
+
account: (pid, ms) => {
|
|
190
|
+
const p = this.#procs.get(pid);
|
|
191
|
+
if (p)
|
|
192
|
+
p.cpuMs += ms;
|
|
193
|
+
void this.call('procs.account', pid, ms);
|
|
194
|
+
},
|
|
195
|
+
kill: (pid, reason = 'killed') => {
|
|
196
|
+
if (!this.#procs.has(pid))
|
|
197
|
+
return false;
|
|
198
|
+
if (this.#owned.has(pid))
|
|
199
|
+
this.#procs.delete(pid);
|
|
200
|
+
this.#reap(pid, reason);
|
|
201
|
+
void this.call('procs.kill', pid, reason);
|
|
202
|
+
return true;
|
|
203
|
+
},
|
|
204
|
+
exit: (pid) => void this.procs.kill(pid, 'exit'),
|
|
205
|
+
get: (pid) => this.#procs.get(pid),
|
|
206
|
+
list: () => [...this.#procs.values()].sort((a, b) => a.started - b.started || a.pid - b.pid),
|
|
207
|
+
};
|
|
208
|
+
fs = ((self) => ({
|
|
209
|
+
get ready() {
|
|
210
|
+
return self.ready;
|
|
211
|
+
},
|
|
212
|
+
readFile: (p) => self.call('fs.readFile', p),
|
|
213
|
+
readText: async (p) => dec.decode(await self.call('fs.readFile', p)),
|
|
214
|
+
writeFile: (p, d) => self.call('fs.writeFile', p, d),
|
|
215
|
+
appendFile: (p, d) => self.call('fs.appendFile', p, d),
|
|
216
|
+
readdir: (p) => self.call('fs.readdir', p),
|
|
217
|
+
stat: (p) => self.call('fs.stat', p),
|
|
218
|
+
exists: (p) => self.call('fs.stat', p).then(Boolean, () => false),
|
|
219
|
+
mkdir: (p) => self.call('fs.mkdir', p),
|
|
220
|
+
rm: (p, o) => self.call('fs.rm', p, o),
|
|
221
|
+
cp: (a, b) => self.call('fs.cp', a, b),
|
|
222
|
+
mv: (a, b) => self.call('fs.mv', a, b),
|
|
223
|
+
glob: (pattern, cwd, limit) => self.call('fs.glob', pattern, cwd, limit),
|
|
224
|
+
async *walk(root, opts = {}) {
|
|
225
|
+
const stack = [[normalize(root), 0]];
|
|
226
|
+
while (stack.length) {
|
|
227
|
+
const [dir, depth] = stack.pop();
|
|
228
|
+
opts.signal?.throwIfAborted();
|
|
229
|
+
const entries = await self.call('fs.readdir', dir).catch(() => []);
|
|
230
|
+
for (const e of entries) {
|
|
231
|
+
yield e;
|
|
232
|
+
if (e.kind === 'dir' && depth + 1 < (opts.maxDepth ?? 32) && e.path !== '/proc')
|
|
233
|
+
stack.push([e.path, depth + 1]);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
},
|
|
237
|
+
mounts: () => self.#mounts,
|
|
238
|
+
unmount: (point) => void self.call('fs.unmount', point),
|
|
239
|
+
}))(this);
|
|
240
|
+
secrets = {
|
|
241
|
+
has: (name) => this.call('secrets.has', name),
|
|
242
|
+
set: (name, value) => this.call('secrets.set', name, value),
|
|
243
|
+
delete: (name) => this.call('secrets.delete', name),
|
|
244
|
+
};
|
|
245
|
+
dev = {
|
|
246
|
+
start: (o) => this.call('dev.start', o),
|
|
247
|
+
stop: (port) => this.call('dev.stop', port),
|
|
248
|
+
list: () => this.call('dev.list'),
|
|
249
|
+
page: (port, path) => this.call('dev.page', port, path),
|
|
250
|
+
request: (port, req) => this.call('dev.request', port, req),
|
|
251
|
+
log: (port, entries) => void this.call('dev.log', port, entries).catch(() => undefined),
|
|
252
|
+
logs: (port, o) => this.call('dev.logs', port, o),
|
|
253
|
+
};
|
|
254
|
+
shells = {
|
|
255
|
+
open: async (opts) => {
|
|
256
|
+
const info = await this.call('shell.open', opts);
|
|
257
|
+
const local = {};
|
|
258
|
+
this.#shells.set(info.id, local);
|
|
259
|
+
const handle = {
|
|
260
|
+
id: info.id,
|
|
261
|
+
cwd: info.cwd,
|
|
262
|
+
home: info.home,
|
|
263
|
+
run: async (line, io) => {
|
|
264
|
+
const runId = this.#nextRun++;
|
|
265
|
+
this.#runs.set(runId, io);
|
|
266
|
+
const abort = () => void this.#peer.then((p) => p.emit('shell:abort', runId));
|
|
267
|
+
io.signal?.addEventListener('abort', abort);
|
|
268
|
+
try {
|
|
269
|
+
const r = await this.call('shell.run', info.id, runId, line);
|
|
270
|
+
handle.cwd = r.cwd;
|
|
271
|
+
return r.code;
|
|
272
|
+
}
|
|
273
|
+
finally {
|
|
274
|
+
io.signal?.removeEventListener('abort', abort);
|
|
275
|
+
this.#runs.delete(runId);
|
|
276
|
+
}
|
|
277
|
+
},
|
|
278
|
+
complete: (line) => this.call('shell.complete', info.id, line),
|
|
279
|
+
onReadLine: (fn) => {
|
|
280
|
+
local.reader = fn;
|
|
281
|
+
},
|
|
282
|
+
onClear: (fn) => {
|
|
283
|
+
local.clear = fn;
|
|
284
|
+
},
|
|
285
|
+
close: () => {
|
|
286
|
+
this.#shells.delete(info.id);
|
|
287
|
+
void this.call('shell.close', info.id);
|
|
288
|
+
},
|
|
289
|
+
};
|
|
290
|
+
return handle;
|
|
291
|
+
},
|
|
292
|
+
};
|
|
293
|
+
mark(name) {
|
|
294
|
+
this.bootMarks.push({ name, at: Math.round(performance.now() * 10) / 10 });
|
|
295
|
+
}
|
|
296
|
+
hasCap(id) {
|
|
297
|
+
return this.caps.find((c) => c.id === id)?.ok ?? false;
|
|
298
|
+
}
|
|
299
|
+
notify(notice) {
|
|
300
|
+
const payload = typeof notice === 'string' ? { title: notice } : notice;
|
|
301
|
+
this.bus.emit('notify', payload);
|
|
302
|
+
void this.call('bus.emit', 'notify', payload);
|
|
303
|
+
}
|
|
304
|
+
openApp(id, args) {
|
|
305
|
+
this.bus.emit('app:open', { id, args });
|
|
306
|
+
void this.call('bus.emit', 'app:open', { id, args });
|
|
307
|
+
}
|
|
308
|
+
/** Shows the folder picker here (it needs a window) and hands the folder to the kernel. */
|
|
309
|
+
async mountNativeFolder() {
|
|
310
|
+
const picker = globalThis.showDirectoryPicker;
|
|
311
|
+
if (!picker)
|
|
312
|
+
throw new Error('This browser does not support mounting local folders (File System Access API).');
|
|
313
|
+
return this.call('fs.mountHandle', await picker({ mode: 'readwrite' }));
|
|
314
|
+
}
|
|
315
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Serves the kernel worker, the bridge frame and the sandbox frames. */
|
|
2
|
+
export declare const KERNEL_URL: string;
|
|
3
|
+
/** Origins allowed to embed the kernel bridge and talk to the kernel. Empty = same origin only. */
|
|
4
|
+
export declare const ALLOWED_ORIGINS: string[];
|
|
5
|
+
export declare const kernelOrigin: () => string;
|
|
6
|
+
/**
|
|
7
|
+
* Origins allowed to drive the SDK runtime (embed/) from a page of their own.
|
|
8
|
+
* Default: any origin. That is safe because browsers partition a cross-site
|
|
9
|
+
* frame's storage and workers by the top-level site, so each embedding site
|
|
10
|
+
* gets its own box and never sees another site's files or the app's own data.
|
|
11
|
+
* Restrict it with VITE_EMBED_ORIGINS=https://a.example,https://b.example.
|
|
12
|
+
*/
|
|
13
|
+
export declare const EMBED_ORIGINS: '*' | string[];
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where each part of BrowserBox is served from.
|
|
3
|
+
*
|
|
4
|
+
* By default everything lives under one origin as sibling paths
|
|
5
|
+
* (`/kernel/`, `/agent/`, `/os/`). To isolate the parts on their own origins,
|
|
6
|
+
* build with e.g.
|
|
7
|
+
* VITE_KERNEL_URL=https://kernel.example.com/
|
|
8
|
+
* VITE_AGENT_URL=https://agent.example.com/
|
|
9
|
+
* VITE_OS_URL=https://os.example.com/
|
|
10
|
+
* VITE_ALLOWED_ORIGINS=https://agent.example.com,https://os.example.com
|
|
11
|
+
* Subdomains of one registrable domain share storage partitions, so every
|
|
12
|
+
* window still reaches the same kernel. Nothing else in the code changes.
|
|
13
|
+
*/
|
|
14
|
+
const env = import.meta.env;
|
|
15
|
+
const here = (rel) => new URL(rel, globalThis.location?.href ?? 'http://localhost/').href;
|
|
16
|
+
/** Serves the kernel worker, the bridge frame and the sandbox frames. */
|
|
17
|
+
export const KERNEL_URL = env.VITE_KERNEL_URL || here('../kernel/');
|
|
18
|
+
/** Origins allowed to embed the kernel bridge and talk to the kernel. Empty = same origin only. */
|
|
19
|
+
export const ALLOWED_ORIGINS = (env.VITE_ALLOWED_ORIGINS || '')
|
|
20
|
+
.split(',')
|
|
21
|
+
.map((s) => s.trim())
|
|
22
|
+
.filter(Boolean);
|
|
23
|
+
export const kernelOrigin = () => new URL(KERNEL_URL).origin;
|
|
24
|
+
/**
|
|
25
|
+
* Origins allowed to drive the SDK runtime (embed/) from a page of their own.
|
|
26
|
+
* Default: any origin. That is safe because browsers partition a cross-site
|
|
27
|
+
* frame's storage and workers by the top-level site, so each embedding site
|
|
28
|
+
* gets its own box and never sees another site's files or the app's own data.
|
|
29
|
+
* Restrict it with VITE_EMBED_ORIGINS=https://a.example,https://b.example.
|
|
30
|
+
*/
|
|
31
|
+
export const EMBED_ORIGINS = env.VITE_EMBED_ORIGINS
|
|
32
|
+
? env.VITE_EMBED_ORIGINS.split(',').map((s) => s.trim()).filter(Boolean)
|
|
33
|
+
: ALLOWED_ORIGINS.length
|
|
34
|
+
? ALLOWED_ORIGINS
|
|
35
|
+
: '*';
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import type { Bus } from './bus';
|
|
2
|
+
import type { DevApi } from './dev/types';
|
|
3
|
+
import type { PlatformCap } from './caps';
|
|
4
|
+
import type { KCore } from './kcore';
|
|
5
|
+
import type { Permissions, Prompter } from './perms';
|
|
6
|
+
import type { Proc, SpawnOptions } from './proc';
|
|
7
|
+
import type { KernelEvents, Notice } from './types';
|
|
8
|
+
import type { Stat } from './vfs/types';
|
|
9
|
+
/**
|
|
10
|
+
* The kernel's public surface. Two things implement it:
|
|
11
|
+
* - `KernelCore`, the real kernel, which runs headless in a worker;
|
|
12
|
+
* - `RemoteKernel`, a client in each window that mirrors read-mostly state
|
|
13
|
+
* and forwards everything else over RPC.
|
|
14
|
+
* Apps, the shell and the agent are written against this interface only,
|
|
15
|
+
* which is what lets any GUI (or none) sit in front of the same kernel.
|
|
16
|
+
*
|
|
17
|
+
* Services built on top of the kernel add themselves to this interface by
|
|
18
|
+
* module augmentation (see src/agent/api.ts, which adds `agents`), so the
|
|
19
|
+
* kernel never depends on them.
|
|
20
|
+
*/
|
|
21
|
+
export interface Kernel {
|
|
22
|
+
readonly version: string;
|
|
23
|
+
readonly bus: Bus<KernelEvents>;
|
|
24
|
+
readonly settings: SettingsApi;
|
|
25
|
+
readonly secrets: SecretsApi;
|
|
26
|
+
readonly perms: Permissions;
|
|
27
|
+
readonly procs: ProcsApi;
|
|
28
|
+
readonly fs: FsApi;
|
|
29
|
+
readonly kcore: KCore;
|
|
30
|
+
readonly shells: ShellsApi;
|
|
31
|
+
/** Virtual dev servers for project folders (`serve`). */
|
|
32
|
+
readonly dev: DevApi;
|
|
33
|
+
readonly caps: PlatformCap[];
|
|
34
|
+
rootKind: 'opfs' | 'memory';
|
|
35
|
+
readonly bootStart: number;
|
|
36
|
+
readonly bootMarks: Array<{
|
|
37
|
+
name: string;
|
|
38
|
+
at: number;
|
|
39
|
+
}>;
|
|
40
|
+
mark(name: string): void;
|
|
41
|
+
hasCap(id: string): boolean;
|
|
42
|
+
notify(notice: Notice | string): void;
|
|
43
|
+
openApp(id: string, args?: Record<string, unknown>): void;
|
|
44
|
+
mountNativeFolder(): Promise<string>;
|
|
45
|
+
}
|
|
46
|
+
export interface MountInfo {
|
|
47
|
+
point: string;
|
|
48
|
+
fs: {
|
|
49
|
+
label: string;
|
|
50
|
+
readonly?: boolean;
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
export interface FsApi {
|
|
54
|
+
readonly ready: Promise<void>;
|
|
55
|
+
readFile(path: string): Promise<Uint8Array>;
|
|
56
|
+
readText(path: string): Promise<string>;
|
|
57
|
+
writeFile(path: string, data: string | Uint8Array): Promise<void>;
|
|
58
|
+
appendFile(path: string, data: string | Uint8Array): Promise<void>;
|
|
59
|
+
readdir(path: string): Promise<Stat[]>;
|
|
60
|
+
stat(path: string): Promise<Stat | null>;
|
|
61
|
+
exists(path: string): Promise<boolean>;
|
|
62
|
+
mkdir(path: string): Promise<void>;
|
|
63
|
+
rm(path: string, opts?: {
|
|
64
|
+
recursive?: boolean;
|
|
65
|
+
}): Promise<void>;
|
|
66
|
+
cp(from: string, to: string): Promise<void>;
|
|
67
|
+
mv(from: string, to: string): Promise<void>;
|
|
68
|
+
glob(pattern: string, cwd?: string, limit?: number): Promise<string[]>;
|
|
69
|
+
walk(root: string, opts?: {
|
|
70
|
+
maxDepth?: number;
|
|
71
|
+
signal?: AbortSignal;
|
|
72
|
+
}): AsyncGenerator<Stat>;
|
|
73
|
+
mounts(): readonly MountInfo[];
|
|
74
|
+
unmount(point: string): void;
|
|
75
|
+
}
|
|
76
|
+
export interface ProcsApi {
|
|
77
|
+
spawn(opts: SpawnOptions): Proc;
|
|
78
|
+
update(pid: number, patch: Partial<Pick<Proc, 'state' | 'detail' | 'memBytes'>>): void;
|
|
79
|
+
account(pid: number, ms: number): void;
|
|
80
|
+
kill(pid: number, reason?: string): boolean;
|
|
81
|
+
exit(pid: number): void;
|
|
82
|
+
get(pid: number): Proc | undefined;
|
|
83
|
+
list(): Proc[];
|
|
84
|
+
}
|
|
85
|
+
export interface SettingsApi {
|
|
86
|
+
get<T>(key: string, fallback: T): T;
|
|
87
|
+
set(key: string, value: unknown): void;
|
|
88
|
+
delete(key: string): void;
|
|
89
|
+
keys(): string[];
|
|
90
|
+
}
|
|
91
|
+
/** Windows can store and remove secrets but never read them back; only the kernel uses them. */
|
|
92
|
+
export interface SecretsApi {
|
|
93
|
+
has(name: string): Promise<boolean>;
|
|
94
|
+
set(name: string, value: string): Promise<void>;
|
|
95
|
+
delete(name: string): Promise<void>;
|
|
96
|
+
}
|
|
97
|
+
export interface ShellIo {
|
|
98
|
+
out(text: string): void;
|
|
99
|
+
err(text: string): void;
|
|
100
|
+
signal?: AbortSignal;
|
|
101
|
+
}
|
|
102
|
+
export interface ShellHandle {
|
|
103
|
+
readonly id: string;
|
|
104
|
+
/** Current directory; refreshed after every command. */
|
|
105
|
+
cwd: string;
|
|
106
|
+
readonly home: string;
|
|
107
|
+
run(line: string, io: ShellIo): Promise<number>;
|
|
108
|
+
complete(line: string): Promise<{
|
|
109
|
+
start: number;
|
|
110
|
+
options: string[];
|
|
111
|
+
}>;
|
|
112
|
+
/** Supplies interactive input when a command reads from the terminal. */
|
|
113
|
+
onReadLine(fn: (prompt: string, secret: boolean) => Promise<string | null>): void;
|
|
114
|
+
onClear(fn: () => void): void;
|
|
115
|
+
close(): void;
|
|
116
|
+
}
|
|
117
|
+
/** A command run by someone else (a kernel client), offered in every shell. */
|
|
118
|
+
export interface ContributedCommand {
|
|
119
|
+
name: string;
|
|
120
|
+
summary: string;
|
|
121
|
+
usage?: string;
|
|
122
|
+
run(io: {
|
|
123
|
+
args: string[];
|
|
124
|
+
stdin: string;
|
|
125
|
+
cwd: string;
|
|
126
|
+
out(text: string): void;
|
|
127
|
+
err(text: string): void;
|
|
128
|
+
signal: AbortSignal;
|
|
129
|
+
readLine?(prompt: string, secret: boolean): Promise<string | null>;
|
|
130
|
+
}): Promise<number>;
|
|
131
|
+
}
|
|
132
|
+
export interface ShellsApi {
|
|
133
|
+
/** Offers a command in every shell; returns a function that withdraws it. (Kernel side only.) */
|
|
134
|
+
contribute?(cmd: ContributedCommand): () => void;
|
|
135
|
+
/**
|
|
136
|
+
* `confined`: the shell runs code for someone else (the SDK runtime), so network access needs a permission grant.
|
|
137
|
+
* `prompter`: who answers its permission requests (kernel side only; it cannot cross RPC).
|
|
138
|
+
*/
|
|
139
|
+
open(opts?: {
|
|
140
|
+
cwd?: string;
|
|
141
|
+
ppid?: number;
|
|
142
|
+
confined?: boolean;
|
|
143
|
+
prompter?: Prompter;
|
|
144
|
+
}): Promise<ShellHandle>;
|
|
145
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Typed synchronous event bus — the kernel's only cross-module coupling. */
|
|
2
|
+
export declare class Bus<Events extends Record<string, unknown>> {
|
|
3
|
+
#private;
|
|
4
|
+
on<K extends keyof Events>(type: K, fn: (payload: Events[K]) => void): () => void;
|
|
5
|
+
emit<K extends keyof Events>(type: K, payload: Events[K]): void;
|
|
6
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Typed synchronous event bus — the kernel's only cross-module coupling. */
|
|
2
|
+
export class Bus {
|
|
3
|
+
#subs = new Map();
|
|
4
|
+
on(type, fn) {
|
|
5
|
+
let set = this.#subs.get(type);
|
|
6
|
+
if (!set)
|
|
7
|
+
this.#subs.set(type, (set = new Set()));
|
|
8
|
+
set.add(fn);
|
|
9
|
+
return () => set.delete(fn);
|
|
10
|
+
}
|
|
11
|
+
emit(type, payload) {
|
|
12
|
+
const set = this.#subs.get(type);
|
|
13
|
+
if (!set)
|
|
14
|
+
return;
|
|
15
|
+
for (const fn of [...set]) {
|
|
16
|
+
try {
|
|
17
|
+
fn(payload);
|
|
18
|
+
}
|
|
19
|
+
catch (err) {
|
|
20
|
+
console.error(`[bus] handler for "${String(type)}" threw`, err);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** Feature detection for every platform API BrowserBox can use. */
|
|
2
|
+
export interface PlatformCap {
|
|
3
|
+
id: string;
|
|
4
|
+
group: 'Compute' | 'Storage' | 'Graphics & Media' | 'System' | 'Network' | 'Devices' | 'AI';
|
|
5
|
+
label: string;
|
|
6
|
+
ok: boolean;
|
|
7
|
+
/** How the OS uses it. */
|
|
8
|
+
use: string;
|
|
9
|
+
}
|
|
10
|
+
export declare function detectCaps(): PlatformCap[];
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export function detectCaps() {
|
|
2
|
+
const g = globalThis;
|
|
3
|
+
const nav = (globalThis.navigator ?? {});
|
|
4
|
+
const has = (o, k) => typeof o === 'object' && o !== null && k in o;
|
|
5
|
+
const c = (id, group, label, ok, use) => ({ id, group, label, ok: Boolean(ok), use });
|
|
6
|
+
return [
|
|
7
|
+
c('wasm', 'Compute', 'WebAssembly', has(g, 'WebAssembly'), 'Rust kernel core, WASI programs, Pyodide'),
|
|
8
|
+
c('wasm-streaming', 'Compute', 'WASM streaming compile', typeof WebAssembly?.instantiateStreaming === 'function', 'Fast kernel boot'),
|
|
9
|
+
c('workers', 'Compute', 'Web Workers', has(g, 'Worker'), 'Sandboxed code execution'),
|
|
10
|
+
c('sab', 'Compute', 'SharedArrayBuffer', has(g, 'SharedArrayBuffer') && g.crossOriginIsolated === true, 'Threads (needs cross-origin isolation)'),
|
|
11
|
+
c('webgpu', 'Compute', 'WebGPU', has(nav, 'gpu'), 'GPU compute tool, GPU Lab, in-browser LLM'),
|
|
12
|
+
c('scheduler', 'Compute', 'Scheduler API', has(g, 'scheduler'), 'Prioritised background work'),
|
|
13
|
+
c('compression', 'Compute', 'Compression Streams', has(g, 'CompressionStream'), 'gzip / gunzip'),
|
|
14
|
+
c('crypto', 'Compute', 'Web Crypto', has(g.crypto, 'subtle'), 'Secret encryption, hashing'),
|
|
15
|
+
c('opfs', 'Storage', 'Origin Private File System', has(nav.storage, 'getDirectory'), 'Persistent root filesystem'),
|
|
16
|
+
c('fsaccess', 'Storage', 'File System Access', has(g, 'showDirectoryPicker'), 'Mount real folders at /mnt'),
|
|
17
|
+
c('indexeddb', 'Storage', 'IndexedDB', has(g, 'indexedDB'), 'Encrypted secrets'),
|
|
18
|
+
c('persist', 'Storage', 'Persistent storage', has(nav.storage, 'persist'), 'Protect data from eviction'),
|
|
19
|
+
c('cache', 'Storage', 'Cache Storage', has(g, 'caches'), 'Offline boot, model weights'),
|
|
20
|
+
c('sw', 'System', 'Service Worker', has(nav, 'serviceWorker'), 'Instant offline boot'),
|
|
21
|
+
c('locks', 'System', 'Web Locks', has(nav, 'locks'), 'Cross-tab coordination'),
|
|
22
|
+
c('broadcast', 'System', 'BroadcastChannel', has(g, 'BroadcastChannel'), 'Cross-tab filesystem events'),
|
|
23
|
+
c('notifications', 'System', 'Notifications', has(g, 'Notification'), 'Agent task alerts'),
|
|
24
|
+
c('clipboard', 'System', 'Async Clipboard', has(nav, 'clipboard'), 'Clipboard tools'),
|
|
25
|
+
c('wakelock', 'System', 'Screen Wake Lock', has(nav, 'wakeLock'), 'Keep awake during long agent runs'),
|
|
26
|
+
c('share', 'System', 'Web Share', has(nav, 'share'), 'Share files'),
|
|
27
|
+
c('viewtransitions', 'System', 'View Transitions', has(globalThis.document, 'startViewTransition'), 'Window animations'),
|
|
28
|
+
c('memory', 'System', 'Memory measurement', has(globalThis.performance, 'memory') || has(globalThis.performance, 'measureUserAgentSpecificMemory'), 'Task Manager'),
|
|
29
|
+
c('battery', 'System', 'Battery Status', has(nav, 'getBattery'), 'System monitor'),
|
|
30
|
+
c('fetch-stream', 'Network', 'Streaming fetch', has(g, 'ReadableStream'), 'LLM token streaming'),
|
|
31
|
+
c('websocket', 'Network', 'WebSocket', has(g, 'WebSocket'), 'Realtime connections'),
|
|
32
|
+
c('webrtc', 'Network', 'WebRTC', has(g, 'RTCPeerConnection'), 'Peer-to-peer'),
|
|
33
|
+
c('webtransport', 'Network', 'WebTransport', has(g, 'WebTransport'), 'Low-latency transport'),
|
|
34
|
+
c('netinfo', 'Network', 'Network Information', has(nav, 'connection'), 'System monitor'),
|
|
35
|
+
c('offscreen', 'Graphics & Media', 'OffscreenCanvas', has(g, 'OffscreenCanvas'), 'Off-thread rendering'),
|
|
36
|
+
c('webgl2', 'Graphics & Media', 'WebGL 2', has(g, 'WebGL2RenderingContext'), 'Graphics fallback'),
|
|
37
|
+
c('webcodecs', 'Graphics & Media', 'WebCodecs', has(g, 'VideoEncoder'), 'Media processing'),
|
|
38
|
+
c('webaudio', 'Graphics & Media', 'Web Audio', has(g, 'AudioContext'), 'Sound'),
|
|
39
|
+
c('speech-tts', 'Graphics & Media', 'Speech synthesis', has(g, 'speechSynthesis'), 'Agent speak tool'),
|
|
40
|
+
c('speech-stt', 'Graphics & Media', 'Speech recognition', has(g, 'SpeechRecognition') || has(g, 'webkitSpeechRecognition'), 'Voice input'),
|
|
41
|
+
c('capture', 'Graphics & Media', 'Screen Capture', has(nav.mediaDevices, 'getDisplayMedia'), 'Screenshots'),
|
|
42
|
+
c('usb', 'Devices', 'WebUSB', has(nav, 'usb'), 'Hardware access'),
|
|
43
|
+
c('serial', 'Devices', 'Web Serial', has(nav, 'serial'), 'Hardware access'),
|
|
44
|
+
c('bluetooth', 'Devices', 'Web Bluetooth', has(nav, 'bluetooth'), 'Hardware access'),
|
|
45
|
+
c('hid', 'Devices', 'WebHID', has(nav, 'hid'), 'Hardware access'),
|
|
46
|
+
c('gamepad', 'Devices', 'Gamepad', has(nav, 'getGamepads'), 'Input'),
|
|
47
|
+
c('prompt-api', 'AI', 'Built-in Prompt API', has(g, 'LanguageModel'), 'On-device model'),
|
|
48
|
+
c('webnn', 'AI', 'WebNN', has(nav, 'ml'), 'Neural network acceleration'),
|
|
49
|
+
];
|
|
50
|
+
}
|