import { createHash } from 'node:crypto'; import { readFileSync } from 'node:fs'; import { pathToFileURL } from 'node:url'; import { runPerceptionWorker } from '@myclaw163/game-agent-runtime/perception/perception-worker-host.js'; import { ClawclawPerception, ClawclawReaction } from '../../../../sdk/perception-plugin/index.js'; import type { PerceptionModuleInput } from '../../../../sdk/perception-plugin/module.js'; import { normalizePerceptionModuleManifest } from '../../perception-module-manifest.js'; import { normalizeReactionManifest } from '../../reaction-manifest.js'; import { CLAWCLAW_PERCEPTION_SPATIAL_API } from './spatial-facts.js'; await runPerceptionWorker(async data => { if (data.embeddedSha256 && createHash('sha256').update(readFileSync(data.modulePath)).digest('hex') !== data.embeddedSha256) { throw Error('strategy_perception_source_changed'); } const exports = await import(pathToFileURL(data.modulePath).href); const Module = data.embeddedSha256 ? exports.Trigger : exports.default; if (!(Module?.prototype instanceof (data.legacy ? ClawclawReaction : ClawclawPerception)) || Module.prototype.evaluate === ClawclawPerception.prototype.evaluate) throw Error('PERCEPTION_MODULE_CLASS_INVALID'); const legacy = data.legacy ? normalizeReactionManifest(Module.manifest) : undefined; const {kind: _kind, ...common} = legacy ?? {} as any; const manifest = legacy ? {...common,apiVersion:3} : normalizePerceptionModuleManifest(Module.manifest); if (JSON.stringify(manifest) !== JSON.stringify(data.expectedManifest)) throw Error('PERCEPTION_MODULE_MANIFEST_CHANGED'); return { create: () => ({ evaluate: (input, api) => new Module().evaluate(input, { ...api, spatial: CLAWCLAW_PERCEPTION_SPATIAL_API }) }), prepareInput: input => ({...input,events:input.events.filter(event=>manifest.consumes?.includes(event.type))}) }; });