import type { GameClient } from '../../../lib/game-client.js'; import { ClawclawStrategyMemory } from '../strategy-memory.js'; import { ClawclawReminderPlugin, CLAWCLAW_REMINDER_PLUGIN_ID } from './derived-event/reminder-plugin.js'; import { listClawclawPerceptionSources, isSupportedPerceptionSource, type ResolvedClawclawPerceptionSource } from './perception/perception-source.js'; import { DEFAULT_PERCEPTION_MODULES } from './perception/official-module-ids.js'; import { ClawclawPerceptionModulePlugin, CLAWCLAW_PERCEPTION_MODULE_PLUGIN_ID } from './perception/perception-module-plugin.js'; import type { RuntimeEpochPlugin } from '../../framework/plugins/index.js'; import { CLAWCLAW_ACTION_PLUGIN_ID, ClawclawActionPlugin, } from './action/index.js'; import { CLAWCLAW_BEHAVIOR_PLUGIN_ID, ClawclawBehaviorCatalog, ClawclawBehaviorPlugin, type ClawclawRoleFallbackBehaviors, } from './behavior/index.js'; import { createSystemPerceptions } from './system-perception/system-perceptions.js'; import { BUILTIN_PERCEPTION_PREFIX } from '../notification-policy.js'; import { CLAWCLAW_GAME_QUERY_PLUGIN_ID, ClawclawGameQueryPlugin, } from './game-query/index.js'; import { CLAWCLAW_MAILBOX_PLUGIN_ID, ClawclawMailboxPlugin, } from './mailbox/index.js'; import { CLAWCLAW_PERCEPTION_PLUGIN_ID, type ClawclawPerceptionConfig, ClawclawPerceptionPlugin, } from './perception/index.js'; import { CLAWCLAW_TRIGGER_PLUGIN_ID, type ClawclawTriggerRegistration, ClawclawTriggerPlugin, } from './trigger/index.js'; export interface ClawclawPluginAssemblyOptions { epochPlugins?: readonly RuntimeEpochPlugin, Record>[]; perception?: ClawclawPerceptionConfig; behaviorCatalog?: Pick & Partial>; behaviorDiagnosticsEnabled?: boolean; triggerDiagnosticsEnabled?: boolean; fallbackBehaviors?: ClawclawRoleFallbackBehaviors; triggers?: readonly ClawclawTriggerRegistration[]; embeddedPerceptions?: readonly ResolvedClawclawPerceptionSource[]; /** All enabled Behavior Event declarations frozen into the startup plugin graph. */ behaviorEventTypes?: readonly string[]; } export interface ClawclawPluginAssembly { modules?: ClawclawPerceptionModulePlugin; plugins: RuntimeEpochPlugin, Record>[]; action: ClawclawActionPlugin; gameQuery: ClawclawGameQueryPlugin; perception: ClawclawPerceptionPlugin; reminders: ClawclawReminderPlugin; trigger?: ClawclawTriggerPlugin; behavior: ClawclawBehaviorPlugin; mailbox: ClawclawMailboxPlugin; } /** Builds the official ClawClaw plugin graph without teaching Runtime plugin names. */ export function assembleClawclawPlugins( client: GameClient, options: ClawclawPluginAssemblyOptions = {}, ): ClawclawPluginAssembly { const plugins = [...(options.epochPlugins ?? [])]; for (const plugin of plugins) { if (plugin.id.startsWith(BUILTIN_PERCEPTION_PREFIX) || plugin.id === 'clawclaw.derived-event') { throw new Error(`Builtin perceptions are mandatory and cannot be replaced: ${plugin.id}`); } const failureMode = typeof plugin.failurePolicy === 'string' ? plugin.failurePolicy : plugin.failurePolicy?.mode; if (failureMode === 'terminate') { throw new Error( `Runtime Plugin ${plugin.id} cannot use the terminate failure policy in the ClawClaw plugin graph.`, ); } } const configuredAction = plugins.find((plugin) => plugin.id === CLAWCLAW_ACTION_PLUGIN_ID); if (configuredAction && !(configuredAction instanceof ClawclawActionPlugin)) { throw new Error(`Runtime Plugin ${CLAWCLAW_ACTION_PLUGIN_ID} is not a ClawclawActionPlugin.`); } const action = configuredAction as ClawclawActionPlugin | undefined ?? new ClawclawActionPlugin(client); if (!configuredAction) plugins.push(action); plugins.push(...createSystemPerceptions(options.behaviorEventTypes)); const configuredReminders = plugins.find(plugin => plugin.id === CLAWCLAW_REMINDER_PLUGIN_ID); if (configuredReminders && !(configuredReminders instanceof ClawclawReminderPlugin)) { throw new Error(`Runtime Plugin ${CLAWCLAW_REMINDER_PLUGIN_ID} is not a ClawclawReminderPlugin.`); } const selected = new Set(options.perception?.modules ?? DEFAULT_PERCEPTION_MODULES); const availableSources = options.perception?.moduleSources ?? listClawclawPerceptionSources(options.perception?.workspaceDir); const explicitlyRequired = (id: string) => plugins.some(plugin => plugin.after?.includes(id)); const moduleSources = availableSources .filter(source => source.module && isSupportedPerceptionSource(source) && selected.has(source.id)); const reminderSources = availableSources.filter(source => source.reminder && selected.has(source.id)); const reminders = configuredReminders as ClawclawReminderPlugin | undefined ?? new ClawclawReminderPlugin({ sources: reminderSources, }); if (!configuredReminders && (reminderSources.length > 0 || explicitlyRequired(reminders.id))) plugins.push(reminders); const configuredGameQuery = plugins.find((plugin) => plugin.id === CLAWCLAW_GAME_QUERY_PLUGIN_ID); if (configuredGameQuery && !(configuredGameQuery instanceof ClawclawGameQueryPlugin)) { throw new Error(`Runtime Plugin ${CLAWCLAW_GAME_QUERY_PLUGIN_ID} is not a ClawclawGameQueryPlugin.`); } const gameQuery = configuredGameQuery as ClawclawGameQueryPlugin | undefined ?? new ClawclawGameQueryPlugin(); if (!configuredGameQuery) plugins.push(gameQuery); const configuredPerception = plugins.find((plugin) => plugin.id === CLAWCLAW_PERCEPTION_PLUGIN_ID); if (configuredPerception && !(configuredPerception instanceof ClawclawPerceptionPlugin)) { throw new Error(`Runtime Plugin ${CLAWCLAW_PERCEPTION_PLUGIN_ID} is not a ClawclawPerceptionPlugin.`); } const perception = configuredPerception as ClawclawPerceptionPlugin | undefined ?? new ClawclawPerceptionPlugin({ ...options.perception, moduleSources: availableSources }); if (!configuredPerception && (perception.produces.events.length > 0 || explicitlyRequired(perception.id))) plugins.push(perception); const configuredTrigger = plugins.find((plugin) => plugin.id === CLAWCLAW_TRIGGER_PLUGIN_ID); if (configuredTrigger && !(configuredTrigger instanceof ClawclawTriggerPlugin)) { throw new Error(`Runtime Plugin ${CLAWCLAW_TRIGGER_PLUGIN_ID} is not a ClawclawTriggerPlugin.`); } const trigger = configuredTrigger as ClawclawTriggerPlugin | undefined // Compatibility only for previously installed standalone Trigger resources. ?? (options.triggers?.length ? new ClawclawTriggerPlugin({ after: [], triggers: options.triggers, diagnosticsEnabled: options.triggerDiagnosticsEnabled, }) : undefined); if (trigger && !configuredTrigger) plugins.push(trigger); const sources = [...moduleSources, ...(options.embeddedPerceptions ?? [])]; const modules = sources.length ? new ClawclawPerceptionModulePlugin({ sources, after: [], behaviors: () => options.behaviorCatalog?.list?.() ?? [], }) : undefined; if (modules) plugins.push(modules); const configuredBehavior = plugins.find((plugin) => plugin.id === CLAWCLAW_BEHAVIOR_PLUGIN_ID); if (configuredBehavior && !(configuredBehavior instanceof ClawclawBehaviorPlugin)) { throw new Error(`Runtime Plugin ${CLAWCLAW_BEHAVIOR_PLUGIN_ID} is not a ClawclawBehaviorPlugin.`); } const behavior = configuredBehavior as ClawclawBehaviorPlugin | undefined ?? new ClawclawBehaviorPlugin({ catalog: options.behaviorCatalog ?? new ClawclawBehaviorCatalog([]), fallbackBehaviors: options.fallbackBehaviors, after: [ CLAWCLAW_ACTION_PLUGIN_ID, CLAWCLAW_REMINDER_PLUGIN_ID, CLAWCLAW_GAME_QUERY_PLUGIN_ID, CLAWCLAW_PERCEPTION_PLUGIN_ID, CLAWCLAW_TRIGGER_PLUGIN_ID, ...(modules ? [CLAWCLAW_PERCEPTION_MODULE_PLUGIN_ID] : []), ].filter(id => plugins.some(plugin => plugin.id === id)), eventTypes: options.behaviorEventTypes, diagnosticsEnabled: options.behaviorDiagnosticsEnabled, }); if (!configuredBehavior) plugins.push(behavior); const strategyMemory = new ClawclawStrategyMemory(); trigger?.attachMemory(strategyMemory); behavior.attachMemory(strategyMemory); modules?.attachMemory(strategyMemory); modules?.attachDriver(behavior); trigger?.attachDriver(behavior); // Direct Agent/user commands remain ActionPlugin calls this round. Preserve // their original activity timing: mark activity at Action admission, before // the backend request starts, without routing the command through Behavior. action.attachAgentActivity(() => { void behavior.noteAgentActivity().catch(() => {}); }); const configuredMailbox = plugins.find((plugin) => plugin.id === CLAWCLAW_MAILBOX_PLUGIN_ID); if (configuredMailbox && !(configuredMailbox instanceof ClawclawMailboxPlugin)) { throw new Error(`Runtime Plugin ${CLAWCLAW_MAILBOX_PLUGIN_ID} is not a ClawclawMailboxPlugin.`); } const eventProducers = plugins .filter((plugin) => plugin.id !== CLAWCLAW_MAILBOX_PLUGIN_ID) .filter((plugin) => (plugin.produces?.events?.length ?? 0) > 0 || (plugin.controlsEventDelivery?.length ?? 0) > 0) .map((plugin) => plugin.id); const mailbox = configuredMailbox as ClawclawMailboxPlugin | undefined ?? new ClawclawMailboxPlugin({ after: eventProducers }); if (!configuredMailbox) plugins.push(mailbox); return { plugins, action, gameQuery, perception, reminders, trigger, behavior, mailbox, modules }; }