/** Frozen API v2 compatibility version; new modules use PERCEPTION_MODULE_API_VERSION (3). */ export const PERCEPTION_PLUGIN_API_VERSION = 2 as const; export * from './module.js'; export * from './lifecycle.js'; import { Perception } from '@myclaw163/game-agent-runtime/sdk/perception.js'; import type { PerceptionModuleManifest, PerceptionModuleInput, PerceptionModuleResult, PerceptionModuleApi } from './module.js'; export { ClawclawReaction } from './reaction.js'; export type { ClawclawReactionManifest, ReactionBehaviorBinding, ReactionActivation, ReactionInput, ReactionResult, ReactionEvent, ReactionFact } from './reaction.js'; export * from './reminder.js'; import { defaultReminder } from './reminder-rules.js'; import type { ReminderInput, ReminderResult, ReminderTopic } from './reminder.js'; /** Optional task/speech helper accepts both the unified input and its v2 compatibility input. */ export function evaluateReminder(topic: ReminderTopic, input: ReminderInput | PerceptionModuleInput): ReminderResult { return defaultReminder(topic, 'events' in input ? { ...input, facts: input.events } : input); } export { hasValidClawclawResourceLocalizations, resolveClawclawResourceMetadata, } from '../resource-metadata.js'; export type { ClawclawLocalizedResourceText, ClawclawResolvedResourceMetadata, ClawclawResourceLocalizations, ClawclawResourceMetadata, } from '../resource-metadata.js'; export { MOVE_MIN_PX, STEP_MOVE_MIN_PX, STILL_STEP_MAX_PX, TICK_MS, actorLabel, actorValue, angleDegrees, cosine, cross, direction, distance, dot, headingDirection, length, positionCopy, round1, timeRange, unit, vector, } from './math.js'; export type { Direction, Vec } from './math.js'; import type { ClawclawResourceLocalizations } from '../resource-metadata.js'; import type { ObservationFrame } from '../module-input/index.js'; import type { SpatialApi, SpatialRoom } from '../behavior-plugin/index.js'; export type { GameEvent, GameState, KnownPlayer, PlayerRef, Position, VisibleCorpse, VisiblePlayer, ObservationFrame, VisualCorpse, VisualObservation, VisualPlayer, VisualPosition, } from '../module-input/index.js'; export type PerceptionSubjectScope = | 'others' | 'self_and_others' | 'visible_pairs' | 'corpses'; /** Perception's public name for the shared observation-frame boundary. */ export type PerceptionFrame = ObservationFrame; export type PerceptionMapRoom = SpatialRoom; export type PerceptionSpatialApi = SpatialApi; export interface PerceptionApi { readonly spatial: PerceptionSpatialApi; } export interface ClawclawPerceptionEventMetadata { readonly name: string; readonly description: string; readonly localizations?: ClawclawResourceLocalizations; } /** * Immutable, epoch-bound input for one fresh parallel analyzer instance. * Frames may be skipped and different inputs may be analyzed concurrently. */ export interface PerceptionAnalysisInput { /** Current visual frame. Its continuity fields are resolved against history. */ readonly frame: Readonly; /** Older visual frames, ascending and excluding `frame`; total window is at most 10. */ readonly history: readonly Readonly[]; /** True only when a prior visual frame exists but continuity into `frame` is not proven. */ readonly gapBefore: boolean; } export type PerceptionBoundaryKind = | 'events_requested' | 'phase_changed' | 'trajectory_gap' | 'segment_ending' | 'shutdown'; export interface PerceptionBoundary { readonly kind: PerceptionBoundaryKind; /** snapshot reports an open slice and keeps it alive; close ends and resets the slice. */ readonly mode: 'snapshot' | 'close'; /** Last visual frame the module may include in this output. */ readonly through: Readonly | null; /** First frame after a close boundary, when one exists; never part of the closing slice. */ readonly next?: Readonly | null; } export interface PerceptionEventProposal { /** Whether this Event may initiate Agent delivery; omitted means true. */ readonly wakeAgent?: boolean; readonly type: string; /** Stable semantic key for replaying the same epoch/output; not cross-frame suppression. */ readonly dedupeKey: string; /** Identity namespace; epoch and output position still participate in event identity. */ readonly dedupeScope?: 'segment' | 'session'; readonly payload: Readonly>; readonly occurredAt?: string; } /** @deprecated Visual API v2 compatibility; new scripts use PerceptionModuleManifest. */ export interface ClawclawPerceptionManifest { /** Whether this Event may initiate Agent delivery; omitted means true. */ readonly wakeAgent?: boolean; readonly apiVersion: typeof PERCEPTION_PLUGIN_API_VERSION; readonly id: string; /** Legacy modules may provide a name; human-facing text is managed by the host. */ readonly name?: string; readonly description: string; readonly localizations?: ClawclawResourceLocalizations; /** Describes Event subjects; every module still receives self as a spatial reference. */ readonly subjectScope: PerceptionSubjectScope; readonly produces: readonly string[]; /** Optional display metadata for every Event in `produces`. */ readonly eventMetadata?: Readonly>; /** * Omit for the persistent, ordered `update()` contract. * `parallel_analysis` batches every enabled parallel module for one frame on * reusable analysis Workers, then applies each persistent reducer in order * on its reducer Worker lane. */ readonly execution?: 'parallel_analysis'; /** * The analysis contains a current-frame observation that `commit()` can * safely install into a freshly reset reducer without retaining any * relationship to `input.history`. CCL suppresses Events from that boundary * commit. Omit unless this invariant is guaranteed by the module. */ readonly boundaryBaseline?: 'current_only'; } /** * Canonical authoring surface for one independent perception module. * * API v3 runs evaluate() in a bounded Worker with a fresh instance per call; * return state for the next call and recovery. API v2 update/parallel_analysis * retains its original persistent reducer semantics on the compatibility path. * A slow call can be terminated without blocking the Runtime Owner. */ export abstract class ClawclawPerception extends Perception { static readonly manifest?: ClawclawPerceptionManifest | PerceptionModuleManifest; /** API v3 combines observation, notification and fixed Behavior activation. */ evaluate(_input: Readonly, _api?: PerceptionModuleApi): PerceptionModuleResult | Promise { throw new Error('PERCEPTION_EVALUATE_NOT_IMPLEMENTED'); } /** @deprecated API v2 compatibility; new modules implement evaluate(). */ update( frame: Readonly, api: PerceptionApi, ): readonly PerceptionEventProposal[] | void | Promise { throw new Error('PERCEPTION_UPDATE_NOT_IMPLEMENTED'); } /** Called only when CCL requires an open long-running slice to be reported. */ flush?( boundary: Readonly, api: PerceptionApi, ): readonly PerceptionEventProposal[] | void | Promise; /** Clears private trajectory state after a hard boundary or replay restart. */ reset?(reason: Exclude | 'replay'): void | Promise; /** Developer-only state included in diagnostics and offline replay, never in Agent Events. */ debugSnapshot?(): unknown; } /** * Opt-in authoring surface for perception work that may exceed one game tick. * * CCL prewarms two Worker Threads (or the configured maximum when smaller), * grows on queued frame pressure up to ten, and retires surplus Workers after * 30 seconds of continuous idleness without dropping below current demand. * Every ready thread has imported and cached all enabled parallel module classes. * One frame is one batch containing all those modules; different frame batches * may run concurrently on different threads. CCL constructs a fresh analyzer * instance for every module/frame job, so `analyze()` must depend only on its * arguments and cannot rely on an earlier instance having run. * * A separate long-lived reducer instance lives on a memory-bounded Worker lane * and receives successful analysis values through `commit()` in frame order. * `commit()` is the only place this module may mutate cross-frame state or emit * Events and must remain short. A completed analysis is still only a proposal: * when the 1-10 frame window is exhausted, CCL may discard it before `commit()`. * Old frames may therefore be skipped, and CCL resets ordered trajectory state * at that boundary. * Aborting an active batch retires and replaces only its occupied pool slot. * * Analysis values cross the Worker boundary and therefore must be supported by * `structuredClone`. CCL rejects a value that cannot be cloned. Worker Threads * isolate failures and bound V8 old-generation memory, but are not an * operating-system security sandbox; load only trusted code. */ export abstract class ClawclawParallelPerception extends ClawclawPerception { override update(): never { throw new Error('parallel_perception_uses_analyze_and_commit'); } abstract analyze( input: Readonly, api: PerceptionApi, ): TAnalysis | Promise; abstract commit( frame: Readonly, analysis: Readonly, api: PerceptionApi, ): readonly PerceptionEventProposal[] | void | Promise; } export interface ClawclawPerceptionClass { readonly manifest?: ClawclawPerceptionManifest; new(): ClawclawPerception; } export type { ProcessingCycleInfo } from '../module-input/index.js';