# Class AgentStateMachine

Agent State Machine.

Manages agent lifecycle states with validation, event callbacks, and error recovery mechanisms.

- Defined in packages/nexus-agents/src/agents/state-machine.ts:42

##### Index

### Constructors

constructor

### Accessors

state
transitionHistory
errors

### Methods

canTransition
getNextState
getValidEvents
transition
forceError
recover
resetErrorCount
reset
onStateChange
onTransitionError
isAvailable
isWorking
hasError

## Constructors

### constructor

- new AgentStateMachine(options?: StateMachineOptions): AgentStateMachine

  #### Parameters

  - options: StateMachineOptions = {}

  #### Returns AgentStateMachine

  - Defined in packages/nexus-agents/src/agents/state-machine.ts:53

## Accessors

### state

- get state(): AgentState

  Gets the current state.

  #### Returns AgentState

  - Defined in packages/nexus-agents/src/agents/state-machine.ts:63

### transitionHistory

- get transitionHistory(): readonly StateTransition[]

  Gets the transition history.

  #### Returns readonly StateTransition[]

  - Defined in packages/nexus-agents/src/agents/state-machine.ts:70

### errors

- get errors(): number

  Gets the current error count.

  #### Returns number

  - Defined in packages/nexus-agents/src/agents/state-machine.ts:77

## Methods

### canTransition

- canTransition(event: StateTransitionEvent): boolean

  Checks if a transition is valid from the current state.

  #### Parameters

  - event: StateTransitionEvent

  #### Returns boolean

  - Defined in packages/nexus-agents/src/agents/state-machine.ts:84

### getNextState

- getNextState(event: StateTransitionEvent): AgentState | undefined

  Gets the next state for an event, if valid.

  #### Parameters

  - event: StateTransitionEvent

  #### Returns AgentState | undefined

  - Defined in packages/nexus-agents/src/agents/state-machine.ts:92

### getValidEvents

- getValidEvents(): StateTransitionEvent[]

  Gets all valid events from the current state.

  #### Returns StateTransitionEvent[]

  - Defined in packages/nexus-agents/src/agents/state-machine.ts:100

### transition

- transition(
  event: StateTransitionEvent,
  context?: Record<string, unknown>,
  ): Result<AgentState, AgentError>

  Attempts a state transition.

  #### Parameters

  - event: StateTransitionEvent

    The event triggering the transition

  - `Optional` context: Record<string, unknown>

    Optional context data for the transition

  #### Returns Result<AgentState, AgentError>

  Result with the new state or an AgentError

  - Defined in packages/nexus-agents/src/agents/state-machine.ts:112

### forceError

- forceError(context?: Record<string, unknown>): void

  Forces a transition to the error state. Use for unrecoverable errors that should bypass normal transition rules.

  #### Parameters

  - `Optional` context: Record<string, unknown>

    Optional context data about the error

  #### Returns void

  - Defined in packages/nexus-agents/src/agents/state-machine.ts:146

### recover

- recover(context?: Record<string, unknown>): Result<AgentState, AgentError>

  Attempts recovery from the error state.

  #### Parameters

  - `Optional` context: Record<string, unknown>

    Optional context data about the recovery

  #### Returns Result<AgentState, AgentError>

  Result with the new state or an AgentError if recovery failed

  - Defined in packages/nexus-agents/src/agents/state-machine.ts:161

### resetErrorCount

- resetErrorCount(): void

  Resets the error count. Call after successful task completion.

  #### Returns void

  - Defined in packages/nexus-agents/src/agents/state-machine.ts:190

### reset

- reset(clearHistory?: boolean): void

  Resets the state machine to its initial state.

  #### Parameters

  - clearHistory: boolean = false

    Whether to clear the transition history

  #### Returns void

  - Defined in packages/nexus-agents/src/agents/state-machine.ts:199

### onStateChange

- onStateChange(callback: StateChangeCallback): () => void

  Subscribes to state change events.

  #### Parameters

  - callback: StateChangeCallback

    Callback to invoke on state changes

  #### Returns () => void

  Unsubscribe function

  - Defined in packages/nexus-agents/src/agents/state-machine.ts:213

### onTransitionError

- onTransitionError(callback: TransitionErrorCallback): () => void

  Subscribes to transition error events.

  #### Parameters

  - callback: TransitionErrorCallback

    Callback to invoke on transition errors

  #### Returns () => void

  Unsubscribe function

  - Defined in packages/nexus-agents/src/agents/state-machine.ts:226

### isAvailable

- isAvailable(): boolean

  Checks if the agent is in a state where it can accept new tasks.

  #### Returns boolean

  - Defined in packages/nexus-agents/src/agents/state-machine.ts:236

### isWorking

- isWorking(): boolean

  Checks if the agent is currently working.

  #### Returns boolean

  - Defined in packages/nexus-agents/src/agents/state-machine.ts:243

### hasError

- hasError(): boolean

  Checks if the agent is in an error state.

  #### Returns boolean

  - Defined in packages/nexus-agents/src/agents/state-machine.ts:250