UNPKG

mcard-js

Version:

MCard - Content-addressable storage with cryptographic hashing, handle resolution, and vector search for Node.js and browsers

152 lines 6.18 kB
/** * Runtime Factory for creating runtime executors. */ import { checkCommand } from './base.js'; import { JavaScriptRuntime } from './javascript.js'; import { PythonRuntime } from './python.js'; import { BinaryRuntime } from './binary.js'; import { WasmRuntime } from './wasm.js'; import { LeanRuntime } from './lean.js'; import { LoaderRuntime, CollectionLoaderRuntime } from './loader.js'; // Cached runtime instances for LLM/Lambda/Network (lazy loaded) let _LLMRuntime = null; let _LambdaRuntime = null; let _NetworkRuntime = null; export class RuntimeFactory { /** * Get availability status of all runtimes. */ static async getAvailableRuntimes() { const runtimes = ['javascript', 'python', 'rust', 'c', 'wasm', 'lean', 'llm']; const status = {}; for (const r of runtimes) { try { if (r === 'javascript') { status[r] = true; } else if (r === 'python') { status[r] = await checkCommand('python3'); } else if (r === 'rust') { status[r] = await checkCommand('cargo'); } else if (r === 'c') { const gccOk = await checkCommand('gcc'); status[r] = gccOk || await checkCommand('clang'); } else if (r === 'lean') { status[r] = await checkCommand('lean'); } else if (r === 'wasm') { status[r] = typeof WebAssembly !== 'undefined'; } else if (r === 'llm') { const { OllamaProvider } = await import('../../llm/providers/OllamaProvider.js'); const provider = new OllamaProvider(null, 5); status[r] = await provider.validate_connection(); } else { status[r] = false; } } catch (e) { status[r] = false; } } return status; } /** * Get a runtime instance by name (synchronous for most runtimes). * * Note: For llm/lambda/network runtimes, this returns a wrapper that * lazily initializes. Use getRuntimeAsync() for guaranteed initialization. */ static getRuntime(runtimeName, options) { switch (runtimeName) { case 'javascript': return new JavaScriptRuntime(); case 'python': return new PythonRuntime(); case 'rust': case 'c': return new BinaryRuntime(); case 'wasm': return new WasmRuntime(); case 'lean': return new LeanRuntime(); case 'loader': return new LoaderRuntime(); case 'collection_loader': return new CollectionLoaderRuntime(); case 'llm': // Return a wrapper that lazily loads LLMRuntime return { async execute(code, context, config) { if (!_LLMRuntime) { const mod = await import('../../llm/LLMRuntime.js'); _LLMRuntime = mod.LLMRuntime; } const runtime = new _LLMRuntime(); return runtime.execute(code, context, config); } }; case 'lambda': if (!options?.collection) { throw new Error('Lambda runtime requires a CardCollection in options'); } // Return a wrapper that lazily loads LambdaRuntime const collection = options.collection; return { async execute(code, context, config) { if (!_LambdaRuntime) { const mod = await import('../../lambda/LambdaRuntime.js'); _LambdaRuntime = mod.LambdaRuntime; } const runtime = new _LambdaRuntime(collection); return runtime.execute(code, context, config); } }; case 'network': // Return a wrapper that lazily loads NetworkRuntime const netCollection = options?.collection; return { async execute(code, context, config, chapterDir) { if (!_NetworkRuntime) { const mod = await import('../NetworkRuntime.js'); _NetworkRuntime = mod.NetworkRuntime; } const runtime = new _NetworkRuntime(netCollection); return runtime.execute(code, context, config, chapterDir || ''); } }; default: throw new Error(`Unknown runtime: ${runtimeName}`); } } /** * Get a runtime instance asynchronously (for runtimes that need dynamic imports). */ static async getRuntimeAsync(runtimeName, options) { switch (runtimeName) { case 'llm': { const { LLMRuntime } = await import('../../llm/LLMRuntime.js'); return new LLMRuntime(); } case 'lambda': { if (!options?.collection) { throw new Error('Lambda runtime requires a CardCollection in options'); } const { LambdaRuntime } = await import('../../lambda/LambdaRuntime.js'); return new LambdaRuntime(options.collection); } case 'network': { const { NetworkRuntime } = await import('../NetworkRuntime.js'); return new NetworkRuntime(options?.collection); } default: // Fallback to sync version return this.getRuntime(runtimeName, options); } } } //# sourceMappingURL=factory.js.map