import { existsSync } from 'node:fs'; import type { StrategyThreadBridge } from '@myclaw163/game-agent-runtime/strategy/thread-strategy-executor.js'; import { behaviorSourceUrl, behaviorThreadLoaderConfig, pinBehaviorSource, pinBehaviorSourceAsync } from './behavior-module-loader.js'; import type { ResolvedClawclawBehaviorSource } from './behavior-source.js'; const compiled = new URL('../../../../../dist/strategy-worker/runtime/clawclaw/plugins/behavior/', import.meta.url); const compiledHost = new URL('host.js', compiled); const useCompiledWorker = existsSync(compiledHost); function workerLoader() { const loader = behaviorThreadLoaderConfig(); if (!useCompiledWorker) return loader; const sdkAliases = Object.fromEntries(Object.entries(loader.sdkAliases).map(([specifier, target]) => { if (!specifier.startsWith('clawclaw-cli') && !specifier.startsWith('@myclaw163/clawclaw-cli')) { return [specifier, target]; } const entry = /\/sdk\/([^/]+)\/index\.[jt]s$/.exec(target)?.[1] ?? 'sdk'; return [specifier, new URL(`sdk-${entry}.js`, compiled).href]; })); return { ...loader, sdkAliases }; } /** Game SDK and source validation; pool scheduling belongs to the framework. */ export const CLAWCLAW_STRATEGY_WORKER_BRIDGE: StrategyThreadBridge = { hostUrl: useCompiledWorker ? compiledHost.href : new URL('./behavior-analysis-thread-host.ts', import.meta.url).href, pin: source => source.trustedSourceTreeSha256 ? source : pinBehaviorSource(source), pinAsync: pinBehaviorSourceAsync, resolve: source => source, sourceUrl: behaviorSourceUrl, loader: workerLoader, };