UNPKG

@struktoai/mirage-core

Version:

A Unified Virtual Filesystem For AI Agents

135 lines • 5.54 kB
// ========= Copyright 2026 @ Strukto.AI All Rights Reserved. ========= // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // ========= Copyright 2026 @ Strukto.AI All Rights Reserved. ========= import { WorkspaceBinding } from "../../../binding.js"; import { adoptModule } from "../../../../rs/stream.js"; import { PathSpec } from "../../../../types.js"; import { PrefixResolver } from "../../../resolver.js"; import { PyodideRuntime } from "../runtime.js"; import { failureText, requestOverBridge, requestSync } from "../../../wasm/transport.js"; const node = globalThis.process?.versions?.node; const port = node === undefined ? null : (await import('node:worker_threads')).parentPort; const browser = globalThis; const post = (message) => { if (port !== null) port.postMessage(message); else browser.postMessage(message); }; let interrupt; let bridge; const call = (request) => bridge === undefined ? requestSync(post, request, interrupt) : requestOverBridge(post, request, bridge); console.warn = (...messages) => { post({ kind: 'notice', message: messages.map(String).join(' ') }); }; const sync = { checkPath: (path, follow) => { call({ op: 'dispatch', path, args: ['stat', path, undefined, undefined, { admitOnly: true, nofollow: !follow }], }); }, process: (payload) => call({ op: 'process', path: '', payload }), stdin: (size) => call({ op: 'stdin', path: '', size }), output: (fd, bytes) => call({ op: 'output', path: '', fh: fd, bytes }), open: (path, mode) => call({ op: 'open', path, mode }), pread: (fh, offset, size) => call({ op: 'pread', path: '', fh, offset, size }), pwrite: (fh, offset, bytes) => { call({ op: 'pwrite', path: '', fh, offset, bytes }); }, fstat: (fh) => call({ op: 'fstat', path: '', fh }), ftruncate: (fh, size) => { call({ op: 'ftruncate', path: '', fh, size }); }, close: (fh) => { call({ op: 'close', path: '', fh }); }, stat: (path) => call({ op: 'stat', path }), readdir: (path, classify = true) => call({ op: 'readdir', path, classify }), readlink: (path) => call({ op: 'readlink', path }), flush: (mutations) => { if (mutations.length > 0) return call({ op: 'flush', path: '', mutations }); }, xattr: (op, path, name, value, attrs) => call({ op: 'dispatch', path, args: [op, path, value, name, attrs] }), }; const dispatch = (...args) => Promise.resolve(call({ op: 'dispatch', path: args[1], args })); let runtime = null; let prefixes = []; async function execute(request) { try { prefixes = request.prefixes; if (runtime === null) { if (request.wasm !== undefined) adoptModule(request.wasm); interrupt = request.interruptBuffer === undefined ? undefined : new Int32Array(request.interruptBuffer); bridge = request.bridge; runtime = new PyodideRuntime({ config: request.config }, sync, request.interruptBuffer); // No files cross to the worker: its file adapter rides the bridge the // main thread answers. runtime.bind(WorkspaceBinding.overBridge(dispatch, new PrefixResolver(() => prefixes))); } // The program the main thread resolved, its script rebuilt from the // fields structured clone kept. const { scriptPath, ...program } = request.program; // The guest's wall clock, which Emscripten reads from this worker's // `Date.now`, runs at the workspace's system_time for the run. The // worker is this runtime's own and its interpreter runs one program // at a time. const hostNow = Date.now; const offset = request.offset ?? 0; if (offset !== 0) Date.now = () => hostNow() + offset * 1000; const value = await runtime .interpret(undefined, { ...program, ...(scriptPath !== undefined ? { scriptPath: new PathSpec(scriptPath) } : {}), }, { stdin: null, flags: request.flags, cwd: PathSpec.fromStrPath(request.cwd), env: request.env, ...(request.timeoutSeconds !== undefined ? { timeoutSeconds: request.timeoutSeconds } : {}), }) .finally(() => { Date.now = hostNow; }); post({ kind: 'result', value }); } catch (error) { post({ kind: 'error', message: failureText(error), name: error instanceof Error ? error.name : 'Error', ...(error?.seconds !== undefined ? { seconds: error.seconds } : {}), }); } } if (port !== null) port.on('message', (request) => { void execute(request); }); else browser.onmessage = (event) => { void execute(event.data); }; post({ kind: 'ready' }); //# sourceMappingURL=entry.js.map