import { parentPort, workerData } from 'node:worker_threads'; import { pathToFileURL } from 'node:url'; import { ClawclawReminder, type ReminderInput } from '../../../../sdk/perception-plugin/reminder.js'; import { normalizeReminderManifest } from '../../reminder-manifest.js'; function freeze(value: T): T { if (value && typeof value === 'object' && !Object.isFrozen(value)) { for (const child of Object.values(value)) freeze(child); Object.freeze(value); } return value; } try { const imported = await import(pathToFileURL(workerData.modulePath).href); const Module = imported.default; if (!(Module?.prototype instanceof ClawclawReminder)) throw new Error('Invalid reminder perception class.'); const manifest = normalizeReminderManifest(Module.manifest); if (JSON.stringify(manifest) !== JSON.stringify(workerData.expectedManifest)) throw new Error('perception_manifest_source_mismatch'); const config = manifest.config ?? {}; parentPort!.postMessage({ type: 'ready', manifest, config }); parentPort!.on('message', async ({ id, input }: { id: number; input: ReminderInput }) => { try { const result = await new Module().update(freeze({ ...input, config, facts: input.facts.filter(fact => manifest.consumes.includes(fact.type)) })); if (Buffer.byteLength(JSON.stringify(result) ?? '') > 1024 * 1024) throw new Error('Reminder output exceeds 1 MB.'); parentPort!.postMessage({ type: 'result', id, result }); } catch (error) { parentPort!.postMessage({ type: 'error', id, error: error instanceof Error ? error.message : String(error) }); } }); } catch (error) { parentPort!.postMessage({ type: 'error', error: error instanceof Error ? error.message : String(error) }); }