Genkit JS API reference
    Preparing search index...

    Class FileSessionStore<S>

    A Node.js file-system backed session snapshot store.

    Snapshots are stored as flat JSON files keyed by their snapshotId, under an optional per-tenant sub-directory prefix:

    File layout: dirPath/<prefix>/<snapshotId>.json

    getSnapshot({ sessionId }) resolves the session's current leaf via a tiny per-session pointer file (<prefix>/.pointers/<sessionId>.json, see PointerDoc) - one pointer read plus one snapshot read. When the pointer is missing (e.g. a legacy store) or stale it transparently falls back to scanning the prefix directory and selecting the single leaf whose sessionId matches, then rewrites the pointer so subsequent lookups are fast again.

    Type Parameters

    • S = unknown

    Implements

    Index

    Constructors

    • Type Parameters

      • S = unknown

      Parameters

      • dirPath: string

        Directory where snapshot JSON files are stored.

      • Optionaloptions: {
            maxPersistedChainLength?: number;
            rejectBranchingSessions?: boolean;
            snapshotPathPrefix?: (options?: SessionStoreOptions) => string;
            snapshotWatchPollIntervalMs?: number;
        }
        • OptionalmaxPersistedChainLength?: number

          When set, snapshots older than this many entries in a chain are automatically deleted on each save.

        • OptionalrejectBranchingSessions?: boolean

          When true, a sessionId lookup that resolves to a branched history (more than one leaf) throws FAILED_PRECONDITION instead of returning the latest leaf. Defaults to false; opt in (e.g. in dev) to surface accidental branching early.

        • OptionalsnapshotPathPrefix?: (options?: SessionStoreOptions) => string

          Returns a sub-directory prefix derived from the call's SessionStoreOptions (e.g. the authenticated user id from options.context), useful for multi-tenant isolation: all reads and writes are scoped to that prefix, so one tenant can never see another's snapshots. Defaults to "global".

        • OptionalsnapshotWatchPollIntervalMs?: number

          Polling interval (ms) for the FileSessionStore.onSnapshotStateChange fallback that backstops fs.watch (which can miss events on some filesystems, e.g. network mounts). Defaults to DEFAULT_SNAPSHOT_WATCH_POLL_INTERVAL_MS.

      Returns FileSessionStore<S>

    Methods

    • Loads a snapshot either by its snapshotId or by sessionId.

      See GetSnapshotOptions for the lookup semantics (exactly one of snapshotId / sessionId; sessionId resolves to the session's latest leaf snapshot, optionally rejecting branching histories).

      Parameters

      • opts: GetSnapshotOptions

      Returns Promise<SessionSnapshot<S> | undefined>

    • Watches a single snapshot file for changes and invokes callback with the parsed snapshot whenever it changes.

      Unlike InMemorySessionStore, file-backed snapshots are frequently mutated by a different process (e.g. the request handler that received an abort writes status: 'aborted', while a detached background worker is the one watching). Detecting that requires observing the filesystem rather than in-process saveSnapshot calls.

      Reliability comes from two layers:

      • fs.watch on the (per-tenant) prefix directory, filtered to the target <snapshotId>.json. This is low latency but can miss events on some filesystems (network mounts, certain container volumes).
      • A polling fallback (snapshotWatchPollIntervalMs) that re-reads the file on an interval, backstopping any events fs.watch drops. Its timer is unref'd so it never keeps the process alive on its own.

      Callbacks are de-duplicated by serialized content, so the noisy/duplicate events fs.watch emits collapse into one callback per real change. Transient read errors (e.g. a partially written file mid-rewrite, or a not-yet-created file) are swallowed; the next event/poll re-reads.

      Parameters

      Returns void | (() => void)

      An unsubscribe function that stops watching and polling.

    • Atomically reads the current snapshot (if snapshotId is provided), passes it to mutator, and persists the result.

      • When snapshotId is provided the store reads the existing snapshot and passes it to the mutator. The mutator can inspect the current state (e.g. to check for concurrent status changes) and return the updated snapshot to save, or null to skip the write.
      • When snapshotId is undefined the store passes undefined to the mutator (signaling a new snapshot). The store assigns a new identifier.

      Implementations should ensure the read→mutate→write cycle is atomic to prevent race conditions (e.g. a "done" write overwriting a concurrent "aborted" status).

      The mutator can:

      • Return a snapshot to save it.
      • Return null to silently skip the write.
      • Throw to abort with an error.

      Parameters

      Returns Promise<string | null>

      The snapshotId that was used, or null when the mutator returned null.