@jeevanand/agent-browserbox-runtime 0.1.1 → 0.3.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-DQLuopgW.js} +2 -2
- package/dist/assets/{build-BNa5TW3M.js → build-Bd5qSPS-.js} +2 -2
- package/dist/assets/bundle-CXrJ5BHI.js +1 -0
- package/dist/assets/{dev-impl-BRiXVAGb.js → dev-impl-DJx7S2dO.js} +4 -4
- package/dist/assets/embed-uUqwwabc.js +3 -0
- package/dist/assets/{html-R2xYS1Rp.js → html-Bd3_N2aN.js} +3 -3
- package/dist/assets/{instance-CppC8T8t.js → instance-Cu6oky-A.js} +1 -1
- package/dist/assets/main-CQvaEX7y.js +372 -0
- package/dist/assets/path-DGG-Mxbd.js +1 -0
- package/dist/assets/{pkg-esbuild-BuQjm7dB.js → pkg-esbuild-C-EHnLD8.js} +1 -1
- package/dist/assets/{pkg-git-jawfIVoq.js → pkg-git-BCxqHW9a.js} +9 -9
- package/dist/assets/pkg-python-B23D6vdG.js +6 -0
- package/dist/assets/{pkg-sqlite-BexM5lLo.js → pkg-sqlite-CKGWzcwt.js} +1 -1
- package/dist/assets/pkg-zip-CuNUYwRy.js +1 -0
- package/dist/assets/rolldown-runtime-WkwZ5CU-.js +1 -0
- package/dist/assets/server-DPVY2l_9.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 +24 -0
- package/lib/client/index.js +17 -0
- package/lib/client/remote-kernel.d.ts +53 -0
- package/lib/client/remote-kernel.js +319 -0
- package/lib/config/origins.d.ts +13 -0
- package/lib/config/origins.js +35 -0
- package/lib/kernel/api.d.ts +153 -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 +32 -0
- package/lib/kernel/instance.js +48 -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/profiles.d.ts +31 -0
- package/lib/kernel/profiles.js +93 -0
- package/lib/kernel/settings.d.ts +29 -0
- package/lib/kernel/settings.js +94 -0
- package/lib/kernel/types.d.ts +54 -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,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The virtual dev server: types shared by the kernel, the windows (over RPC),
|
|
3
|
+
* the shell, the agent's tools and the Preview app.
|
|
4
|
+
*
|
|
5
|
+
* Nothing listens on a real port. A "port" names a server the kernel keeps for
|
|
6
|
+
* a project folder; Preview asks the kernel for each page and request.
|
|
7
|
+
*/
|
|
8
|
+
export interface DevInfo {
|
|
9
|
+
port: number;
|
|
10
|
+
/** The project folder. */
|
|
11
|
+
root: string;
|
|
12
|
+
pid: number;
|
|
13
|
+
/** Shown to people and the model: `http://localhost:<port>/`. Virtual; it only opens in Preview. */
|
|
14
|
+
url: string;
|
|
15
|
+
/** The project's request handler (`server.ts` …), relative to root, if there is one. */
|
|
16
|
+
backend: string | null;
|
|
17
|
+
/** Whether the backend may reach the network. */
|
|
18
|
+
network: boolean;
|
|
19
|
+
}
|
|
20
|
+
export interface WireRequest {
|
|
21
|
+
method: string;
|
|
22
|
+
/** Path and query, e.g. `/api/todos?done=1`. */
|
|
23
|
+
url: string;
|
|
24
|
+
headers: Array<[string, string]>;
|
|
25
|
+
body?: Uint8Array | null;
|
|
26
|
+
}
|
|
27
|
+
export interface WireResponse {
|
|
28
|
+
status: number;
|
|
29
|
+
statusText?: string;
|
|
30
|
+
headers: Array<[string, string]>;
|
|
31
|
+
body: Uint8Array | null;
|
|
32
|
+
}
|
|
33
|
+
export type DevLogLevel = 'log' | 'info' | 'warn' | 'error' | 'debug';
|
|
34
|
+
export interface DevLog {
|
|
35
|
+
/** Increasing per server; `logs({ after })` returns entries newer than this. */
|
|
36
|
+
seq: number;
|
|
37
|
+
/** Date.now() when recorded. */
|
|
38
|
+
t: number;
|
|
39
|
+
level: DevLogLevel;
|
|
40
|
+
/** page: the app's console and errors · server: the backend · build: bundling · network: failed requests. */
|
|
41
|
+
source: 'page' | 'server' | 'build' | 'network';
|
|
42
|
+
text: string;
|
|
43
|
+
}
|
|
44
|
+
export interface DevPage {
|
|
45
|
+
html: string;
|
|
46
|
+
/** Build errors; the page shows them in an overlay too. */
|
|
47
|
+
errors: string[];
|
|
48
|
+
}
|
|
49
|
+
export interface DevStartOptions {
|
|
50
|
+
root: string;
|
|
51
|
+
port?: number;
|
|
52
|
+
network?: boolean;
|
|
53
|
+
ppid?: number;
|
|
54
|
+
}
|
|
55
|
+
export interface DevApi {
|
|
56
|
+
/** Starts a server for a folder, or returns the one already serving it. */
|
|
57
|
+
start(opts: DevStartOptions): Promise<DevInfo>;
|
|
58
|
+
stop(port: number): Promise<boolean>;
|
|
59
|
+
list(): Promise<DevInfo[]>;
|
|
60
|
+
/** Builds the page at `path` (bundling scripts and styles) for Preview to show. */
|
|
61
|
+
page(port: number, path?: string): Promise<DevPage>;
|
|
62
|
+
/** Answers a request the page made: a project file or the backend's response. */
|
|
63
|
+
request(port: number, req: WireRequest): Promise<WireResponse>;
|
|
64
|
+
/** Records what the page printed or threw. */
|
|
65
|
+
log(port: number, entries: Array<Pick<DevLog, 'level' | 'source' | 'text'>>): void;
|
|
66
|
+
/** Recorded output, oldest first. */
|
|
67
|
+
logs(port: number, opts?: {
|
|
68
|
+
after?: number;
|
|
69
|
+
limit?: number;
|
|
70
|
+
}): Promise<DevLog[]>;
|
|
71
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The virtual dev server: types shared by the kernel, the windows (over RPC),
|
|
3
|
+
* the shell, the agent's tools and the Preview app.
|
|
4
|
+
*
|
|
5
|
+
* Nothing listens on a real port. A "port" names a server the kernel keeps for
|
|
6
|
+
* a project folder; Preview asks the kernel for each page and request.
|
|
7
|
+
*/
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type FileKind = 'dir' | 'text' | 'code' | 'markdown' | 'html' | 'image' | 'svg' | 'audio' | 'video' | 'pdf' | 'archive' | 'binary';
|
|
2
|
+
export declare function kindOf(name: string, isDir?: boolean): FileKind;
|
|
3
|
+
/** Whether a file opens in the Editor; everything else goes to Preview. */
|
|
4
|
+
export declare function isTextLike(name: string): boolean;
|
|
5
|
+
export declare function languageOf(name: string): string;
|
|
6
|
+
export declare function mimeOf(name: string): string;
|
|
7
|
+
/** NUL bytes are a reliable enough "this is binary" signal; `limit` bounds the scan. */
|
|
8
|
+
export declare function looksBinary(bytes: Uint8Array, limit?: number): boolean;
|
|
9
|
+
export declare function errMessage(err: unknown): string;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/** File classification: kinds, languages and MIME types. DOM-free; shared by the dev server and the Files, Editor and Preview apps. */
|
|
2
|
+
import { basename, extname } from './vfs/path';
|
|
3
|
+
const LANGS = {
|
|
4
|
+
js: 'JavaScript', mjs: 'JavaScript', cjs: 'JavaScript', jsx: 'JavaScript (JSX)',
|
|
5
|
+
ts: 'TypeScript', mts: 'TypeScript', cts: 'TypeScript', tsx: 'TypeScript (TSX)',
|
|
6
|
+
py: 'Python', json: 'JSON', jsonl: 'JSON Lines', css: 'CSS', scss: 'SCSS', less: 'Less',
|
|
7
|
+
html: 'HTML', htm: 'HTML', xml: 'XML', svg: 'SVG', md: 'Markdown', markdown: 'Markdown',
|
|
8
|
+
sh: 'Shell', bash: 'Shell', zsh: 'Shell', yml: 'YAML', yaml: 'YAML', toml: 'TOML', ini: 'INI',
|
|
9
|
+
csv: 'CSV', tsv: 'TSV', sql: 'SQL', rs: 'Rust', go: 'Go', c: 'C', h: 'C', cpp: 'C++', cc: 'C++',
|
|
10
|
+
hpp: 'C++', java: 'Java', kt: 'Kotlin', rb: 'Ruby', php: 'PHP', swift: 'Swift', lua: 'Lua',
|
|
11
|
+
r: 'R', wat: 'WebAssembly Text', vue: 'Vue', svelte: 'Svelte', txt: 'Plain Text', log: 'Log',
|
|
12
|
+
};
|
|
13
|
+
const PLAIN = new Set(['txt', 'log', 'csv', 'tsv', 'env', 'conf', 'cfg', 'gitignore', 'gitattributes', 'editorconfig', 'lock', 'properties', 'rst', 'tex', 'diff', 'patch']);
|
|
14
|
+
const IMAGE = new Set(['png', 'jpg', 'jpeg', 'gif', 'webp', 'avif', 'bmp', 'ico']);
|
|
15
|
+
const AUDIO = new Set(['mp3', 'wav', 'ogg', 'oga', 'flac', 'm4a', 'aac', 'opus']);
|
|
16
|
+
const VIDEO = new Set(['mp4', 'webm', 'ogv', 'mov', 'm4v']);
|
|
17
|
+
const ARCHIVE = new Set(['zip', 'tar', 'gz', 'tgz', 'bz2', 'xz', '7z', 'rar']);
|
|
18
|
+
export function kindOf(name, isDir = false) {
|
|
19
|
+
if (isDir)
|
|
20
|
+
return 'dir';
|
|
21
|
+
const ext = extname(name);
|
|
22
|
+
if (ext === 'md' || ext === 'markdown')
|
|
23
|
+
return 'markdown';
|
|
24
|
+
if (ext === 'html' || ext === 'htm')
|
|
25
|
+
return 'html';
|
|
26
|
+
if (ext === 'svg')
|
|
27
|
+
return 'svg';
|
|
28
|
+
if (ext === 'pdf')
|
|
29
|
+
return 'pdf';
|
|
30
|
+
if (IMAGE.has(ext))
|
|
31
|
+
return 'image';
|
|
32
|
+
if (AUDIO.has(ext))
|
|
33
|
+
return 'audio';
|
|
34
|
+
if (VIDEO.has(ext))
|
|
35
|
+
return 'video';
|
|
36
|
+
if (ARCHIVE.has(ext))
|
|
37
|
+
return 'archive';
|
|
38
|
+
if (PLAIN.has(ext))
|
|
39
|
+
return 'text';
|
|
40
|
+
if (ext in LANGS)
|
|
41
|
+
return 'code';
|
|
42
|
+
// Extensionless files (README, Makefile, /proc entries) and dotfiles are usually text.
|
|
43
|
+
if (!ext || basename(name).startsWith('.'))
|
|
44
|
+
return 'text';
|
|
45
|
+
return 'binary';
|
|
46
|
+
}
|
|
47
|
+
/** Whether a file opens in the Editor; everything else goes to Preview. */
|
|
48
|
+
export function isTextLike(name) {
|
|
49
|
+
const k = kindOf(name);
|
|
50
|
+
return k === 'text' || k === 'code' || k === 'markdown';
|
|
51
|
+
}
|
|
52
|
+
export function languageOf(name) {
|
|
53
|
+
return LANGS[extname(name)] ?? (kindOf(name) === 'text' ? 'Plain Text' : extname(name).toUpperCase() || 'Plain Text');
|
|
54
|
+
}
|
|
55
|
+
const MIME = {
|
|
56
|
+
png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg', gif: 'image/gif', webp: 'image/webp',
|
|
57
|
+
avif: 'image/avif', bmp: 'image/bmp', ico: 'image/x-icon', svg: 'image/svg+xml',
|
|
58
|
+
mp3: 'audio/mpeg', wav: 'audio/wav', ogg: 'audio/ogg', oga: 'audio/ogg', flac: 'audio/flac',
|
|
59
|
+
m4a: 'audio/mp4', aac: 'audio/aac', opus: 'audio/ogg', mp4: 'video/mp4', webm: 'video/webm',
|
|
60
|
+
ogv: 'video/ogg', mov: 'video/quicktime', m4v: 'video/mp4', pdf: 'application/pdf',
|
|
61
|
+
html: 'text/html', htm: 'text/html', css: 'text/css', js: 'text/javascript', mjs: 'text/javascript',
|
|
62
|
+
json: 'application/json', md: 'text/markdown', txt: 'text/plain', csv: 'text/csv', xml: 'application/xml',
|
|
63
|
+
woff: 'font/woff', woff2: 'font/woff2', ttf: 'font/ttf', otf: 'font/otf', wasm: 'application/wasm',
|
|
64
|
+
};
|
|
65
|
+
export function mimeOf(name) {
|
|
66
|
+
return MIME[extname(name)] ?? (isTextLike(name) ? 'text/plain' : 'application/octet-stream');
|
|
67
|
+
}
|
|
68
|
+
/** NUL bytes are a reliable enough "this is binary" signal; `limit` bounds the scan. */
|
|
69
|
+
export function looksBinary(bytes, limit = 8192) {
|
|
70
|
+
return bytes.subarray(0, limit).includes(0);
|
|
71
|
+
}
|
|
72
|
+
export function errMessage(err) {
|
|
73
|
+
return err instanceof Error ? err.message : String(err);
|
|
74
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** Human-readable numbers, shared by the shell, the agent and the UI. DOM-free. */
|
|
2
|
+
export function fmtBytes(n) {
|
|
3
|
+
if (!Number.isFinite(n))
|
|
4
|
+
return '—';
|
|
5
|
+
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
|
6
|
+
let i = 0;
|
|
7
|
+
while (n >= 1024 && i < units.length - 1) {
|
|
8
|
+
n /= 1024;
|
|
9
|
+
i++;
|
|
10
|
+
}
|
|
11
|
+
return `${i === 0 ? n : n.toFixed(n < 10 ? 1 : 0)} ${units[i]}`;
|
|
12
|
+
}
|
|
13
|
+
export function fmtDuration(ms) {
|
|
14
|
+
if (ms < 1000)
|
|
15
|
+
return `${Math.round(ms)} ms`;
|
|
16
|
+
const s = ms / 1000;
|
|
17
|
+
if (s < 60)
|
|
18
|
+
return `${s.toFixed(1)} s`;
|
|
19
|
+
const m = Math.floor(s / 60);
|
|
20
|
+
if (m < 60)
|
|
21
|
+
return `${m}m ${Math.floor(s % 60)}s`;
|
|
22
|
+
return `${Math.floor(m / 60)}h ${m % 60}m`;
|
|
23
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which kernel this is. The app has one kernel per site (`APP_KERNEL`); every
|
|
3
|
+
* SDK box has its own, with its own files, settings and secrets. The identity
|
|
4
|
+
* travels from the SDK to the runtime page's URL (`embed/?box=…`), to the
|
|
5
|
+
* bridge's URL, and into the worker's name, which the kernel reads at startup.
|
|
6
|
+
*/
|
|
7
|
+
export interface KernelInstance {
|
|
8
|
+
/** Null for the app's own kernel; a box id otherwise. */
|
|
9
|
+
box: string | null;
|
|
10
|
+
/** 'opfs': files and settings persist. 'memory': nothing outlives the box. */
|
|
11
|
+
storage: 'opfs' | 'memory';
|
|
12
|
+
/**
|
|
13
|
+
* For the app's kernel: which profile. Each profile is a kernel of its own,
|
|
14
|
+
* with its own files, settings and keys; absent or null is the first
|
|
15
|
+
* profile ("Default"), whose data is where it always was.
|
|
16
|
+
*/
|
|
17
|
+
profile?: string | null;
|
|
18
|
+
}
|
|
19
|
+
export declare const APP_KERNEL: KernelInstance;
|
|
20
|
+
/** Where boxes and other profiles keep their files, inside the first profile's OPFS root: hidden from it. */
|
|
21
|
+
export declare const HIDDEN_FOLDERS: string[];
|
|
22
|
+
/** Box ids: letters, digits, '-' and '_', up to 64 characters. */
|
|
23
|
+
export declare const BOX_ID: RegExp;
|
|
24
|
+
/** A profile of the app's kernel. */
|
|
25
|
+
export declare const profileKernel: (profile: string | null) => KernelInstance;
|
|
26
|
+
export declare function workerName(k: KernelInstance): string;
|
|
27
|
+
export declare function fromWorkerName(name: string): KernelInstance;
|
|
28
|
+
/** Reads a box from URL parameters (`?box=<id>&storage=memory`); the app's kernel when there is none. */
|
|
29
|
+
export declare function fromSearch(search: string): KernelInstance;
|
|
30
|
+
export declare function toSearch(k: KernelInstance): string;
|
|
31
|
+
/** IndexedDB database name for this kernel's copy of a store; null when nothing may persist. */
|
|
32
|
+
export declare function dbName(base: string, k: KernelInstance): string | null;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export const APP_KERNEL = { box: null, storage: 'opfs' };
|
|
2
|
+
/** Where boxes and other profiles keep their files, inside the first profile's OPFS root: hidden from it. */
|
|
3
|
+
export const HIDDEN_FOLDERS = ['boxes', 'profiles'];
|
|
4
|
+
/** Box ids: letters, digits, '-' and '_', up to 64 characters. */
|
|
5
|
+
export const BOX_ID = /^[A-Za-z0-9_-]{1,64}$/;
|
|
6
|
+
/** A profile of the app's kernel. */
|
|
7
|
+
export const profileKernel = (profile) => (profile ? { box: null, storage: 'opfs', profile } : APP_KERNEL);
|
|
8
|
+
export function workerName(k) {
|
|
9
|
+
if (k.box !== null)
|
|
10
|
+
return `bbx-box:${k.storage}:${k.box}`;
|
|
11
|
+
return k.profile ? `bbx-kernel:profile:${k.profile}` : 'bbx-kernel';
|
|
12
|
+
}
|
|
13
|
+
export function fromWorkerName(name) {
|
|
14
|
+
const p = /^bbx-kernel:profile:([A-Za-z0-9_-]{1,64})$/.exec(name);
|
|
15
|
+
if (p)
|
|
16
|
+
return profileKernel(p[1]);
|
|
17
|
+
const m = /^bbx-box:(opfs|memory):([A-Za-z0-9_-]{1,64})$/.exec(name);
|
|
18
|
+
return m ? { box: m[2], storage: m[1] } : APP_KERNEL;
|
|
19
|
+
}
|
|
20
|
+
/** Reads a box from URL parameters (`?box=<id>&storage=memory`); the app's kernel when there is none. */
|
|
21
|
+
export function fromSearch(search) {
|
|
22
|
+
const q = new URLSearchParams(search);
|
|
23
|
+
const box = q.get('box');
|
|
24
|
+
const profile = q.get('profile');
|
|
25
|
+
if (box === null && profile) {
|
|
26
|
+
if (!BOX_ID.test(profile))
|
|
27
|
+
throw new Error(`invalid profile id: ${profile}`);
|
|
28
|
+
return profileKernel(profile);
|
|
29
|
+
}
|
|
30
|
+
if (box === null)
|
|
31
|
+
return APP_KERNEL;
|
|
32
|
+
if (!BOX_ID.test(box))
|
|
33
|
+
throw new Error(`invalid box id: ${box}`);
|
|
34
|
+
return { box, storage: q.get('storage') === 'memory' ? 'memory' : 'opfs' };
|
|
35
|
+
}
|
|
36
|
+
export function toSearch(k) {
|
|
37
|
+
if (k.box === null)
|
|
38
|
+
return k.profile ? `?profile=${encodeURIComponent(k.profile)}` : '';
|
|
39
|
+
return `?box=${encodeURIComponent(k.box)}&storage=${k.storage}`;
|
|
40
|
+
}
|
|
41
|
+
/** IndexedDB database name for this kernel's copy of a store; null when nothing may persist. */
|
|
42
|
+
export function dbName(base, k) {
|
|
43
|
+
if (k.storage === 'memory')
|
|
44
|
+
return null;
|
|
45
|
+
if (k.box === null)
|
|
46
|
+
return k.profile ? `${base}:profile:${k.profile}` : base;
|
|
47
|
+
return `${base}:box:${k.box}`;
|
|
48
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export interface LineMatch {
|
|
2
|
+
line: number;
|
|
3
|
+
offset: number;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Bindings for the Rust kernel core (crates/kcore). Loaded lazily with
|
|
7
|
+
* streaming compilation; every entry point has a pure-JS fallback so the OS
|
|
8
|
+
* still works where WebAssembly is disabled.
|
|
9
|
+
*/
|
|
10
|
+
export declare class KCore {
|
|
11
|
+
#private;
|
|
12
|
+
get loaded(): boolean;
|
|
13
|
+
load(): Promise<void>;
|
|
14
|
+
hash(data: Uint8Array): string;
|
|
15
|
+
/** Literal search returning matching line numbers and byte offsets. */
|
|
16
|
+
searchLines(hay: Uint8Array, needle: string, ignoreCase?: boolean, cap?: number): {
|
|
17
|
+
total: number;
|
|
18
|
+
matches: LineMatch[];
|
|
19
|
+
};
|
|
20
|
+
wc(data: Uint8Array): {
|
|
21
|
+
lines: number;
|
|
22
|
+
words: number;
|
|
23
|
+
bytes: number;
|
|
24
|
+
};
|
|
25
|
+
estimateTokens(text: string): number;
|
|
26
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import wasmUrl from './kcore.wasm?url';
|
|
2
|
+
const enc = new TextEncoder();
|
|
3
|
+
/**
|
|
4
|
+
* Bindings for the Rust kernel core (crates/kcore). Loaded lazily with
|
|
5
|
+
* streaming compilation; every entry point has a pure-JS fallback so the OS
|
|
6
|
+
* still works where WebAssembly is disabled.
|
|
7
|
+
*/
|
|
8
|
+
export class KCore {
|
|
9
|
+
#x = null;
|
|
10
|
+
#loading = null;
|
|
11
|
+
get loaded() {
|
|
12
|
+
return this.#x !== null;
|
|
13
|
+
}
|
|
14
|
+
load() {
|
|
15
|
+
return (this.#loading ??= (async () => {
|
|
16
|
+
try {
|
|
17
|
+
const { instance } = await WebAssembly.instantiateStreaming(fetch(wasmUrl), {});
|
|
18
|
+
this.#x = instance.exports;
|
|
19
|
+
}
|
|
20
|
+
catch (err) {
|
|
21
|
+
console.warn('[kcore] wasm unavailable, using JS fallbacks', err);
|
|
22
|
+
}
|
|
23
|
+
})());
|
|
24
|
+
}
|
|
25
|
+
#with(data, fn) {
|
|
26
|
+
const x = this.#x;
|
|
27
|
+
const ptr = x.alloc(data.length);
|
|
28
|
+
try {
|
|
29
|
+
new Uint8Array(x.memory.buffer, ptr, data.length).set(data);
|
|
30
|
+
return fn(x, ptr);
|
|
31
|
+
}
|
|
32
|
+
finally {
|
|
33
|
+
x.dealloc(ptr, data.length);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
hash(data) {
|
|
37
|
+
if (this.#x)
|
|
38
|
+
return this.#with(data, (x, p) => BigInt.asUintN(64, x.fnv1a64(p, data.length)).toString(16).padStart(16, '0'));
|
|
39
|
+
let h = 0xcbf29ce484222325n;
|
|
40
|
+
for (const b of data)
|
|
41
|
+
h = BigInt.asUintN(64, (h ^ BigInt(b)) * 0x100000001b3n);
|
|
42
|
+
return h.toString(16).padStart(16, '0');
|
|
43
|
+
}
|
|
44
|
+
/** Literal search returning matching line numbers and byte offsets. */
|
|
45
|
+
searchLines(hay, needle, ignoreCase = false, cap = 1000) {
|
|
46
|
+
const n = enc.encode(needle);
|
|
47
|
+
if (this.#x) {
|
|
48
|
+
return this.#with(hay, (x, hp) => {
|
|
49
|
+
const np = x.alloc(n.length);
|
|
50
|
+
const op = x.alloc(cap * 8);
|
|
51
|
+
try {
|
|
52
|
+
new Uint8Array(x.memory.buffer, np, n.length).set(n);
|
|
53
|
+
const total = x.search_lines(hp, hay.length, np, n.length, ignoreCase ? 1 : 0, op, cap);
|
|
54
|
+
const view = new DataView(x.memory.buffer, op, cap * 8);
|
|
55
|
+
const matches = [];
|
|
56
|
+
for (let i = 0; i < Math.min(total, cap); i++)
|
|
57
|
+
matches.push({ line: view.getUint32(i * 8, true), offset: view.getUint32(i * 8 + 4, true) });
|
|
58
|
+
return { total, matches };
|
|
59
|
+
}
|
|
60
|
+
finally {
|
|
61
|
+
x.dealloc(np, n.length);
|
|
62
|
+
x.dealloc(op, cap * 8);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
const text = new TextDecoder().decode(hay);
|
|
67
|
+
const q = ignoreCase ? needle.toLowerCase() : needle;
|
|
68
|
+
const matches = [];
|
|
69
|
+
let total = 0;
|
|
70
|
+
let offset = 0;
|
|
71
|
+
text.split('\n').forEach((line, i) => {
|
|
72
|
+
if ((ignoreCase ? line.toLowerCase() : line).includes(q)) {
|
|
73
|
+
if (matches.length < cap)
|
|
74
|
+
matches.push({ line: i + 1, offset });
|
|
75
|
+
total++;
|
|
76
|
+
}
|
|
77
|
+
offset += enc.encode(line).length + 1;
|
|
78
|
+
});
|
|
79
|
+
return { total, matches };
|
|
80
|
+
}
|
|
81
|
+
wc(data) {
|
|
82
|
+
if (this.#x) {
|
|
83
|
+
return this.#with(data, (x, p) => {
|
|
84
|
+
const out = x.alloc(12);
|
|
85
|
+
try {
|
|
86
|
+
x.wc(p, data.length, out);
|
|
87
|
+
const v = new DataView(x.memory.buffer, out, 12);
|
|
88
|
+
return { lines: v.getUint32(0, true), words: v.getUint32(4, true), bytes: v.getUint32(8, true) };
|
|
89
|
+
}
|
|
90
|
+
finally {
|
|
91
|
+
x.dealloc(out, 12);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
const text = new TextDecoder().decode(data);
|
|
96
|
+
return { lines: (text.match(/\n/g) ?? []).length, words: (text.match(/\S+/g) ?? []).length, bytes: data.length };
|
|
97
|
+
}
|
|
98
|
+
estimateTokens(text) {
|
|
99
|
+
if (this.#x) {
|
|
100
|
+
const data = enc.encode(text);
|
|
101
|
+
return this.#with(data, (x, p) => x.estimate_tokens(p, data.length));
|
|
102
|
+
}
|
|
103
|
+
return Math.ceil(text.length / 3.6);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
Binary file
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { SettingsApi } from './api';
|
|
2
|
+
/** Things a sandboxed actor must be granted before the kernel will do them. */
|
|
3
|
+
export type Capability = 'fs.write' | 'exec' | 'net' | 'clipboard' | 'notify' | 'system';
|
|
4
|
+
export interface PermRequest {
|
|
5
|
+
cap: Capability;
|
|
6
|
+
/** Who is asking, e.g. "Agent". */
|
|
7
|
+
actor: string;
|
|
8
|
+
/** Short human title, e.g. "Run shell command". */
|
|
9
|
+
title: string;
|
|
10
|
+
/** The exact thing being approved (command, path, URL). */
|
|
11
|
+
detail: string;
|
|
12
|
+
}
|
|
13
|
+
export type PermDecision = 'once' | 'always' | 'deny';
|
|
14
|
+
/** `signal` aborts when the request was answered elsewhere (another window). */
|
|
15
|
+
export type Prompter = (req: PermRequest, signal?: AbortSignal) => Promise<PermDecision>;
|
|
16
|
+
export type PermMode = 'ask' | 'auto';
|
|
17
|
+
/**
|
|
18
|
+
* Capability broker. Policy: in `auto` mode everything inside the sandbox is
|
|
19
|
+
* allowed; in `ask` mode each capability needs a one-off or standing grant.
|
|
20
|
+
*/
|
|
21
|
+
export declare class Permissions {
|
|
22
|
+
#private;
|
|
23
|
+
private settings;
|
|
24
|
+
constructor(settings: SettingsApi);
|
|
25
|
+
setPrompter(p: Prompter): void;
|
|
26
|
+
/** Shows the installed prompt without consulting or changing grants. */
|
|
27
|
+
prompt(req: PermRequest, signal?: AbortSignal): Promise<PermDecision>;
|
|
28
|
+
get mode(): PermMode;
|
|
29
|
+
set mode(m: PermMode);
|
|
30
|
+
grants(): Capability[];
|
|
31
|
+
revoke(cap: Capability): void;
|
|
32
|
+
isGranted(cap: Capability): boolean;
|
|
33
|
+
/** Resolves true when allowed. `prompter` overrides the UI dialog (used by the CLI). */
|
|
34
|
+
request(req: PermRequest, prompter?: Prompter): Promise<boolean>;
|
|
35
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Capability broker. Policy: in `auto` mode everything inside the sandbox is
|
|
3
|
+
* allowed; in `ask` mode each capability needs a one-off or standing grant.
|
|
4
|
+
*/
|
|
5
|
+
export class Permissions {
|
|
6
|
+
settings;
|
|
7
|
+
#prompter = async () => 'deny';
|
|
8
|
+
constructor(settings) {
|
|
9
|
+
this.settings = settings;
|
|
10
|
+
}
|
|
11
|
+
setPrompter(p) {
|
|
12
|
+
this.#prompter = p;
|
|
13
|
+
}
|
|
14
|
+
/** Shows the installed prompt without consulting or changing grants. */
|
|
15
|
+
prompt(req, signal) {
|
|
16
|
+
return this.#prompter(req, signal);
|
|
17
|
+
}
|
|
18
|
+
get mode() {
|
|
19
|
+
return this.settings.get('perms.mode', 'ask');
|
|
20
|
+
}
|
|
21
|
+
set mode(m) {
|
|
22
|
+
this.settings.set('perms.mode', m);
|
|
23
|
+
}
|
|
24
|
+
grants() {
|
|
25
|
+
return this.settings.get('perms.grants', []);
|
|
26
|
+
}
|
|
27
|
+
revoke(cap) {
|
|
28
|
+
this.settings.set('perms.grants', this.grants().filter((c) => c !== cap));
|
|
29
|
+
}
|
|
30
|
+
isGranted(cap) {
|
|
31
|
+
return this.mode === 'auto' || this.grants().includes(cap);
|
|
32
|
+
}
|
|
33
|
+
/** Resolves true when allowed. `prompter` overrides the UI dialog (used by the CLI). */
|
|
34
|
+
async request(req, prompter) {
|
|
35
|
+
if (this.isGranted(req.cap))
|
|
36
|
+
return true;
|
|
37
|
+
const decision = await (prompter ?? this.#prompter)(req);
|
|
38
|
+
if (decision === 'always')
|
|
39
|
+
this.settings.set('perms.grants', [...new Set([...this.grants(), req.cap])]);
|
|
40
|
+
return decision !== 'deny';
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Bus } from './bus';
|
|
2
|
+
import type { KernelEvents } from './types';
|
|
3
|
+
export type ProcKind = 'service' | 'app' | 'shell' | 'agent' | 'sandbox';
|
|
4
|
+
export type ProcState = 'running' | 'idle' | 'blocked';
|
|
5
|
+
export interface Proc {
|
|
6
|
+
readonly pid: number;
|
|
7
|
+
readonly ppid: number;
|
|
8
|
+
readonly name: string;
|
|
9
|
+
readonly kind: ProcKind;
|
|
10
|
+
readonly started: number;
|
|
11
|
+
state: ProcState;
|
|
12
|
+
/** Free-form status line shown by `ps` and the Task Manager. */
|
|
13
|
+
detail: string;
|
|
14
|
+
/** Accumulated busy time in ms, reported by the owner via `account()`. */
|
|
15
|
+
cpuMs: number;
|
|
16
|
+
/** Best-effort memory attribution in bytes (0 = unknown). */
|
|
17
|
+
memBytes: number;
|
|
18
|
+
readonly signal: AbortSignal;
|
|
19
|
+
}
|
|
20
|
+
export interface SpawnOptions {
|
|
21
|
+
name: string;
|
|
22
|
+
kind: ProcKind;
|
|
23
|
+
ppid?: number;
|
|
24
|
+
detail?: string;
|
|
25
|
+
/** Register under a pid allocated by a remote owner (a window's own processes). */
|
|
26
|
+
pid?: number;
|
|
27
|
+
/** Called when the process is killed or exits; release resources here. */
|
|
28
|
+
onKill?: () => void;
|
|
29
|
+
}
|
|
30
|
+
/** The process table. A "process" is anything with a lifecycle the user can inspect and kill. */
|
|
31
|
+
export declare class ProcTable {
|
|
32
|
+
#private;
|
|
33
|
+
private bus;
|
|
34
|
+
constructor(bus: Bus<KernelEvents>);
|
|
35
|
+
spawn(opts: SpawnOptions): Proc;
|
|
36
|
+
/** Update mutable fields and notify observers. */
|
|
37
|
+
update(pid: number, patch: Partial<Pick<Proc, 'state' | 'detail' | 'memBytes'>>): void;
|
|
38
|
+
account(pid: number, ms: number): void;
|
|
39
|
+
/** Terminates a process and (recursively) its children. Returns false if no such pid. */
|
|
40
|
+
kill(pid: number, reason?: string): boolean;
|
|
41
|
+
/** Normal termination — same cleanup path as kill. */
|
|
42
|
+
exit(pid: number): void;
|
|
43
|
+
get(pid: number): Proc | undefined;
|
|
44
|
+
list(): Proc[];
|
|
45
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/** The process table. A "process" is anything with a lifecycle the user can inspect and kill. */
|
|
2
|
+
export class ProcTable {
|
|
3
|
+
bus;
|
|
4
|
+
#next = 1;
|
|
5
|
+
#procs = new Map();
|
|
6
|
+
constructor(bus) {
|
|
7
|
+
this.bus = bus;
|
|
8
|
+
}
|
|
9
|
+
spawn(opts) {
|
|
10
|
+
const ctl = new AbortController();
|
|
11
|
+
const proc = {
|
|
12
|
+
pid: opts.pid ?? this.#next++,
|
|
13
|
+
ppid: opts.ppid ?? 0,
|
|
14
|
+
name: opts.name,
|
|
15
|
+
kind: opts.kind,
|
|
16
|
+
started: Date.now(),
|
|
17
|
+
state: 'running',
|
|
18
|
+
detail: opts.detail ?? '',
|
|
19
|
+
cpuMs: 0,
|
|
20
|
+
memBytes: 0,
|
|
21
|
+
signal: ctl.signal,
|
|
22
|
+
};
|
|
23
|
+
this.#procs.set(proc.pid, { proc, ctl, onKill: opts.onKill });
|
|
24
|
+
this.bus.emit('proc:spawn', proc);
|
|
25
|
+
return proc;
|
|
26
|
+
}
|
|
27
|
+
/** Update mutable fields and notify observers. */
|
|
28
|
+
update(pid, patch) {
|
|
29
|
+
const e = this.#procs.get(pid);
|
|
30
|
+
if (!e)
|
|
31
|
+
return;
|
|
32
|
+
Object.assign(e.proc, patch);
|
|
33
|
+
this.bus.emit('proc:update', e.proc);
|
|
34
|
+
}
|
|
35
|
+
account(pid, ms) {
|
|
36
|
+
const e = this.#procs.get(pid);
|
|
37
|
+
if (e)
|
|
38
|
+
e.proc.cpuMs += ms;
|
|
39
|
+
}
|
|
40
|
+
/** Terminates a process and (recursively) its children. Returns false if no such pid. */
|
|
41
|
+
kill(pid, reason = 'killed') {
|
|
42
|
+
const e = this.#procs.get(pid);
|
|
43
|
+
if (!e)
|
|
44
|
+
return false;
|
|
45
|
+
this.#procs.delete(pid);
|
|
46
|
+
for (const child of this.list())
|
|
47
|
+
if (child.ppid === pid)
|
|
48
|
+
this.kill(child.pid, reason);
|
|
49
|
+
e.ctl.abort(new DOMException(reason, 'AbortError'));
|
|
50
|
+
try {
|
|
51
|
+
e.onKill?.();
|
|
52
|
+
}
|
|
53
|
+
catch (err) {
|
|
54
|
+
console.error(`[proc] onKill for ${e.proc.name} threw`, err);
|
|
55
|
+
}
|
|
56
|
+
this.bus.emit('proc:exit', { proc: e.proc, reason });
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
/** Normal termination — same cleanup path as kill. */
|
|
60
|
+
exit(pid) {
|
|
61
|
+
this.kill(pid, 'exit');
|
|
62
|
+
}
|
|
63
|
+
get(pid) {
|
|
64
|
+
return this.#procs.get(pid)?.proc;
|
|
65
|
+
}
|
|
66
|
+
list() {
|
|
67
|
+
return [...this.#procs.values()].map((e) => e.proc);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The profiles of a BrowserBox site: who uses it, each with a kernel of their
|
|
3
|
+
* own (files, settings, API keys, chats). The list lives in this origin's
|
|
4
|
+
* IndexedDB next to the profiles' own stores, readable by every window of the
|
|
5
|
+
* site before any kernel starts. "default" is the first profile: its data is
|
|
6
|
+
* where it always was, so it can be renamed but not deleted.
|
|
7
|
+
*/
|
|
8
|
+
export interface Profile {
|
|
9
|
+
id: string;
|
|
10
|
+
name: string;
|
|
11
|
+
created: number;
|
|
12
|
+
/** Shown as locked until signed in (the key store has the truth). */
|
|
13
|
+
password: boolean;
|
|
14
|
+
}
|
|
15
|
+
export declare const DEFAULT_PROFILE = "default";
|
|
16
|
+
/** Every profile, the first one first. */
|
|
17
|
+
export declare function listProfiles(): Promise<Profile[]>;
|
|
18
|
+
export declare function getProfile(id: string): Promise<Profile | null>;
|
|
19
|
+
export declare function createProfile(name: string): Promise<Profile>;
|
|
20
|
+
/** Changes a profile's name or password flag. */
|
|
21
|
+
export declare function updateProfile(id: string, patch: Partial<Pick<Profile, 'name' | 'password'>>): Promise<Profile>;
|
|
22
|
+
/** The profile this browser opened last. */
|
|
23
|
+
export declare function lastProfile(): Promise<string>;
|
|
24
|
+
export declare function setLastProfile(id: string): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* Deletes a profile with its files, settings and keys. Close its windows
|
|
27
|
+
* first: a kernel still running keeps what it has in memory.
|
|
28
|
+
*/
|
|
29
|
+
export declare function deleteProfile(id: string): Promise<void>;
|
|
30
|
+
/** The kernel a profile runs in: the first profile is the site's original kernel. */
|
|
31
|
+
export declare const kernelOf: (id: string) => import("./instance").KernelInstance;
|