Skip to content

nodestep.state.integrations.inmemory

InMemoryStateStore, which keeps each thread's history in memory.

See State stores.

InMemoryStateStore

Thread-safe state store that keeps history in memory.

Attributes
eventslist[HistoryEvent]

All stored events.

checkpointslist[CheckpointRecord]

All stored checkpoints.

Methods

async append_event(event)

Store an event as given.

async append_event(event) -> HistoryEvent

Parameters
eventHistoryEvent
Returns
HistoryEvent

The same event.

async append_next_event(event)

Store an event with the next sequence number of its thread.

async append_next_event(event) -> HistoryEvent

Parameters
eventHistoryEvent

Event whose sequence is replaced.

Returns
HistoryEvent

The stored event with its sequence number.

async save_checkpoint(checkpoint)

Store a state snapshot.

async save_checkpoint(checkpoint) -> CheckpointRecord

Parameters
checkpointCheckpointRecord
Returns
CheckpointRecord

The same checkpoint.

async get_history(thread_id, branch_id)

Return the events and checkpoints of a thread branch.

async get_history(thread_id, branch_id='main') -> History

Parameters
thread_idstr
branch_idstr

Branch to read; "main" by default.

Returns
History

Empty lists for a thread or branch the store does not know.

async latest_checkpoint(thread_id, branch_id, at)

Return the latest checkpoint, optionally at or before an event.

async latest_checkpoint(thread_id, branch_id='main', at=None) -> CheckpointRecord | None

Parameters
thread_idstr
branch_idstr

Branch to read; "main" by default.

atstr | None

Event id; only checkpoints up to that event count.

Returns
CheckpointRecord or None

None when the branch has no such checkpoint.

Raises
StateUpdateError

If at is not an event of the branch.

async fork(thread_id, ...)

Record a new branch starting at an event.

async fork(thread_id, *, from_, branch_id='main', name=None) -> BranchRecord

Parameters
thread_idstr
from_str

Event id to branch from.

branch_idstr

Branch to fork; "main" by default.

namestr | None

Id of the new branch; a random id when omitted.

Raises
StateStoreError

If the thread already has a branch with that id, including "main".

async list_branches(thread_id)

Return the branches forked from a thread, oldest first.

async list_branches(thread_id) -> list[BranchRecord]

Parameters
thread_idstr
Returns
list[BranchRecord]

Every recorded fork of the thread; "main" is not listed.