UNPKG

mcard-js

Version:

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

97 lines 3.96 kB
import { MCardStore } from '../storage/MCardStore.js'; import { WebSocketClient } from '../network/WebSocketClient.js'; export class ServiceWorkerPTR { store; ws; constructor(serverUrl) { this.store = new MCardStore(); this.ws = new WebSocketClient(serverUrl); this.ws.setMessageHandler(this.onMeshMessage.bind(this)); } start() { this.ws.connect(); // Listen for messages from controlled clients (Browser UI) self.addEventListener('message', (event) => { if (event.data && event.data.type === 'clm_execute') { this.handleLocalRequest(event); } }); console.log('[ServiceWorkerPTR] Started'); } getStore() { return this.store; } async onMeshMessage(msg) { console.log('[ServiceWorkerPTR] Received from mesh:', msg); if (msg.type === 'clm_execute') { await this.executeCLM(msg.clm_hash, msg.input_hash, msg.request_id); } else if (msg.type === 'clm_result') { // Forward result to all clients const clients = await self.clients.matchAll(); for (const client of clients) { client.postMessage(msg); } } } async handleLocalRequest(event) { const { clm_hash, input_hash, request_id } = event.data; console.log('[ServiceWorkerPTR] Local request:', request_id); // In the future, this is where we'd execute locally or forward to mesh // For now, let's just forward to mesh as a test or echo back this.ws.send({ type: 'clm_execute', clm_hash, input_hash, request_id, origin: 'browser' }); } async executeCLM(clmHash, inputHash, requestId) { console.log(`[ServiceWorkerPTR] Executing CLM ${clmHash} with input ${inputHash}`); // 1. Retrieve Input from store const input = await this.store.getCard(inputHash); // 2. Retrieve CLM (Source) from store - mocking strictly for demo // In reality, we'd fetch the CLM content by hash. // For now, assuming we know what to run based on some hardcoded logic or fetched content. // Let's assume the clmHash *is* the code or we mock a lookup. // Mocking a simple JS execution for demonstration try { const result = await this.executeJavaScript('return input.content.count + 1;', input || { content: { count: 0 } }, {}); // 4. Store Result // Note: putCard takes (hash, content). The store handles wrapping. // We should ensure we are storing clean content. const resultHash = `sha256:result_${requestId}`; await this.store.putCard(resultHash, result); // 5. Respond this.ws.send({ type: 'clm_result', request_id: requestId, output_hash: resultHash, success: true }); } catch (e) { console.error('[ServiceWorkerPTR] Execution error:', e); this.ws.send({ type: 'clm_result', request_id: requestId, output_hash: '', success: false, error: e.message }); } } // --- Execution Logic Ported from SandboxWorker --- async executeJavaScript(code, input, context) { const fn = new Function('input', 'context', code); return fn(input, context || {}); } // Pyodide support placeholder // Implementing full Pyodide loading requires handling caching and CDN usage // similar to SandboxWorker.ts but adapted for ServiceWorker environment. async executePython(code, input, context) { throw new Error('Python execution not fully implemented yet in ServiceWorkerPTR'); } } //# sourceMappingURL=ServiceWorkerPTR.js.map