Interface IPersistBase<Entity>

Defines the core interface for persistent storage operations in the swarm system. Provides methods for managing entities stored as JSON files in the file system, used across swarm utilities.

interface IPersistBase<Entity extends IEntity = IEntity> {
    hasValue(entityId: EntityId): Promise<boolean>;
    readValue(entityId: EntityId): Promise<Entity>;
    waitForInit(initial: boolean): Promise<void>;
    writeValue(entityId: EntityId, entity: Entity): Promise<void>;
}

Type Parameters

  • Entity extends IEntity = IEntity

    The type of entity stored, defaults to IEntity (e.g., IPersistAliveData, IPersistStateData). IPersistBase

Methods

  • Checks if an entity exists in persistent storage by its ID. Useful for conditional operations without reading the full entity (e.g., checking session memory existence).

    Parameters

    • entityId: EntityId

    Returns Promise<boolean>

    If checking existence fails for reasons other than the entity not existing.

  • Reads an entity from persistent storage by its ID, parsing it from a JSON file. Used to retrieve persisted data such as agent states, memory, or alive status.

    Parameters

    • entityId: EntityId

    Returns Promise<Entity>

    If the entity is not found (ENOENT) or reading/parsing fails (e.g., invalid JSON).

  • Initializes the storage directory, creating it if needed and validating existing data by removing invalid entities. Ensures the persistence layer is ready for use, handling corrupt files during setup.

    Parameters

    • initial: boolean

    Returns Promise<void>

    If directory creation, file access, or validation fails.

  • Writes an entity to persistent storage with the specified ID, serializing it to JSON. Uses atomic writes to ensure data integrity, critical for reliable state persistence across swarm operations.

    Parameters

    • entityId: EntityId
    • entity: Entity

    Returns Promise<void>

    If writing to the file system fails (e.g., permissions or disk issues).