import { Strategy } from '@myclaw163/game-agent-runtime/sdk/strategy.js'; import type { BehaviorInput, BehaviorDecisionAction, Position, } from '../module-input/index.js'; import type { ClawclawResourceLocalizations } from '../resource-metadata.js'; import type { StrategyMemory } from '../strategy-memory.js'; export type { StrategyMemory, StrategyMemoryValue } from '../strategy-memory.js'; export const BEHAVIOR_PLUGIN_API_VERSION = 3 as const; export { getTaskAvailability, getAvailableTasks, isTaskAvailable, getTaskKind } from '../task-availability.js'; export type { GameTaskAvailability, GameTaskKind } from '../module-input/index.js'; export { hasValidClawclawResourceLocalizations, resolveClawclawResourceMetadata, } from '../resource-metadata.js'; export type { ClawclawLocalizedResourceText, ClawclawResolvedResourceMetadata, ClawclawResourceLocalizations, ClawclawResourceMetadata, } from '../resource-metadata.js'; export type { BehaviorContinuation, BehaviorDecisionAction, BehaviorDecisionFrame, BehaviorDecisionFrameStatus, BehaviorDelegatedBehaviorResult, BehaviorInput, BehaviorScope, GameEvent, GameState, GameTask, KnownPlayer, PlayerRef, Position, VisibleCorpse, VisiblePlayer, } from '../module-input/index.js'; /** How Runtime schedules pure decision computation. Effect commit is ordered in both modes. */ export type BehaviorExecution = 'parallel' | 'serial'; /** Opt-in inheritance of the highest successfully completed epoch's instance data. */ export type BehaviorStateMode = 'isolated' | 'snapshot'; interface BehaviorInputFieldBase { /** Stable key used in BehaviorInput.input. */ readonly id: string; readonly description: string; /** Required fields cannot also declare a default. */ readonly required?: boolean; } export interface BehaviorStringInputField extends BehaviorInputFieldBase { readonly type: 'string'; readonly default?: string; readonly values?: never; readonly min?: never; readonly max?: never; readonly maxItems?: never; } export interface BehaviorNumberInputField extends BehaviorInputFieldBase { readonly type: 'number'; readonly default?: number; readonly min?: number; readonly max?: number; readonly values?: never; readonly maxItems?: never; } export interface BehaviorBooleanInputField extends BehaviorInputFieldBase { readonly type: 'boolean'; readonly default?: boolean; readonly values?: never; readonly min?: never; readonly max?: never; readonly maxItems?: never; } export interface BehaviorEnumInputField extends BehaviorInputFieldBase { readonly type: 'enum'; readonly values: readonly string[]; readonly default?: string; readonly min?: never; readonly max?: never; readonly maxItems?: never; } export interface BehaviorStringListInputField extends BehaviorInputFieldBase { readonly type: 'string_list'; readonly default?: readonly string[]; readonly maxItems?: number; readonly values?: never; readonly min?: never; readonly max?: never; } export interface BehaviorPlayerSeatInputField extends BehaviorInputFieldBase { /** One authoritative player seat, from 1 through 10. */ readonly type: 'player_seat'; readonly default?: number; readonly values?: never; readonly min?: never; readonly max?: never; readonly maxItems?: never; } export interface BehaviorPlayerSeatsInputField extends BehaviorInputFieldBase { /** Authoritative player seats, each an integer from 1 through 10. */ readonly type: 'player_seats'; readonly default?: readonly number[]; readonly maxItems?: number; readonly values?: never; readonly min?: never; readonly max?: never; } /** Minimal, statically readable schema for values delivered through BehaviorInput.input. */ export type BehaviorInputFieldSpec = | BehaviorStringInputField | BehaviorNumberInputField | BehaviorBooleanInputField | BehaviorEnumInputField | BehaviorStringListInputField | BehaviorPlayerSeatInputField | BehaviorPlayerSeatsInputField; export interface SpatialPosition extends Position { readonly room: string | null; } export interface SpatialRoom { readonly name: string; readonly polygon: readonly (readonly [number, number])[]; readonly bbox: Readonly<{ left: number; top: number; right: number; bottom: number }>; } /** Static map facts and deterministic spatial derivations; never dynamic match state. */ export interface SpatialApi { readonly xPositive: 'right'; readonly yPositive: 'down'; readonly playerBodyLengthPx: number; readonly rooms: readonly Readonly[]; roomAt(x: number, y: number): Readonly | null; sideInRoom(position: Readonly): string | null; placeLabel(position: Readonly): string; transitionIsMapped(from: string | null, to: string | null): boolean; } export interface BehaviorPathQuery { readonly from: Readonly; readonly to: Readonly; } export interface ObservedPlayer extends Position { readonly name: string; readonly seat: number; readonly room?: string; readonly distance?: number; readonly isAlive?: boolean; readonly observedAtMs: number; readonly ageMs: number; } export interface ObservedCorpse extends Position { readonly id: string; readonly name?: string; readonly seat?: number; readonly room?: string; readonly distance?: number; readonly observedAtMs: number; readonly ageMs: number; } /** Epoch-bound game queries and resource-scoped private memory. */ export interface BehaviorReadApi { readonly memory: StrategyMemory; readonly spatial: SpatialApi; readonly navigation: { pathDistance(from: Readonly, to: Readonly): Promise; pathDistances( queries: readonly Readonly[], ): Promise; }; readonly observation: { lastSeenPlayer(targetSeat: number, maxAgeMs?: number): Readonly | undefined; lastSeenCorpse(targetSeat?: number): Readonly | undefined; }; } /** The one typed Action union accepted from finite and continuous Behaviors. */ export type GameAction = BehaviorDecisionAction; /** Module status published beside Game State; it never patches authoritative Game State. */ export interface BehaviorStatus { readonly summary: string; readonly activity?: string; readonly room?: string; readonly task?: string; readonly targetPlayer?: string; readonly data?: Readonly>; } /** A fact proposed by a Behavior. Runtime owns identity, dedupe, persistence, and delivery. */ export interface BehaviorEventProposal { readonly type: string; readonly payload: Readonly>; readonly occurredAt?: string; } /** Starts one finite short Behavior from the currently running long Behavior. */ export interface BehaviorDelegation { readonly behavior: string; readonly input?: Readonly>; } export interface StrategyDelegation { readonly strategy: string; readonly input?: Readonly>; } /** All semantic effects of one Behavior decision. */ export interface BehaviorDecision { readonly actions?: readonly Readonly[]; readonly status?: Readonly; readonly events?: readonly Readonly[]; /** Suspends this long Activation and starts one validated short Activation. */ readonly delegate?: Readonly; /** Ends this Activation after the Decision's preceding effects are committed. */ readonly finish?: Readonly; } export interface BehaviorFinish { readonly outcome: 'completed' | 'failed'; readonly reason: string; readonly summary?: string; } /** @deprecated BehaviorDecision now carries the optional finish effect directly. */ export type FiniteBehaviorDecision = BehaviorDecision; /** Structural callback shared by every Behavior activation layer. */ export type BehaviorDecide< TInput extends Readonly>, TDecision extends BehaviorDecision = BehaviorDecision, > = ( input: Readonly>, api: BehaviorReadApi, ) => TDecision | void | Promise; export type BehaviorActivationLayer = 'long' | 'short' | 'reflex'; /** Finite runtime defaults used when a Behavior is started as short or reflex. */ export interface BehaviorRuntimeDefaults { readonly defaultMs: number; readonly deadlineOutcome?: 'completed' | 'failed'; readonly controlGraceMs?: number; } /** One statically readable author contract for long, short, and reflex Behaviors. */ export interface ClawclawBehaviorManifest { readonly stateMode?: BehaviorStateMode; readonly apiVersion: typeof BEHAVIOR_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; readonly inputs: readonly BehaviorInputFieldSpec[]; /** Omitted means isolated parallel decide(); serial retains one Activation instance. */ readonly execution?: BehaviorExecution; /** Legacy input-driven cadence. The fixed-cadence Owner does not use this as a decision gate. */ readonly everyTicks?: number; /** Event types this Behavior may propose. Omit when it produces no custom Events. */ readonly produces?: readonly string[]; /** Required only when this Behavior needs a default finite budget. */ readonly runtime?: Readonly; } export interface BehaviorTiming { readonly timeout_ms?: number; readonly duration_ms?: number; } /** Helpers used once, before an Activation receives its immutable input. */ export interface BehaviorNormalizationApi { readonly manifest: Readonly; timing(input: Readonly>): BehaviorTiming; string(value: unknown, field: string): string; optionalString(value: unknown, field: string): string | undefined; number( value: unknown, field: string, constraints?: Readonly<{ min?: number; max?: number; integer?: boolean }>, ): number; stringArray( value: unknown, field: string, constraints?: Readonly<{ maxItems?: number }>, ): readonly string[]; choice(value: unknown, field: string, values: readonly T[]): T; } /** * The only public authoring base for role-control modules. * * Runtime decides whether one Activation is long, short, or reflex. The module * always receives the same BehaviorInput and returns the same BehaviorDecision. */ export abstract class ClawclawBehavior< TInput extends Readonly> = Readonly>, > extends Strategy, BehaviorReadApi, BehaviorDecision> { static readonly manifest?: ClawclawBehaviorManifest; /** Optional one-time normalization. Schema defaults and validation run first. */ normalize?( input: Readonly>, api: BehaviorNormalizationApi, ): TInput; abstract decide( input: Readonly>, api: BehaviorReadApi, ): BehaviorDecision | void | Promise; protected status(value: BehaviorStatus): BehaviorDecision { return { status: value }; } } export interface ClawclawBehaviorClass< TInput extends Readonly> = Readonly>, > { readonly manifest?: ClawclawBehaviorManifest; new(): ClawclawBehavior; } export type { ProcessingCycleInfo } from '../module-input/index.js';