Skip to main content

Goal

Use the State worker to share data between Functions without a separate database.

Steps

1. Enable the State worker

iii-config.yaml

2. Write state

state-writer.ts

3. Read state

state-reader.ts

4. Atomic updates

Use state::update with update operations for safe concurrent modifications:
state-update.ts
Append operations are useful for incremental data such as transcript chunks, progress events, or small audit trails:
append-transcript.ts
If transcript is missing, a string append creates a string. If events is missing, a non-string append creates an array with one element. Existing arrays receive one new element per append. Existing strings only accept string values. Paths for set / append / increment / decrement / remove are first-level field names. user.name means the field literally named user.name; it does not traverse nested objects. Use path: "" to target the root value.
Atomic append rewrites and returns the full JSON value. For very large payloads or long-running high-throughput streams, store chunks as separate stream items or use channels instead of repeatedly appending to one large value.

Build per-session structured state with nested merge

merge is the one update op that accepts a nested path. Its path is either a single string (legacy / first-level field) or an array of literal segments. This makes it the right tool for accumulating per-session structured state — a transcript timeline keyed by session, an event log per user, the metadata sidebar of an in-flight call. The example below builds audio::transcripts[<session-id>] = { "<timestamp>": chunk } incrementally. Two writers can append to different timestamps in the same session without stepping on each other; sibling timestamps are preserved by every merge.
record-transcript-chunk.ts
Each segment in a merge path array is a literal key. ["a.b"] writes a single key named "a.b", not a → b. To deep-merge sub-objects, compose multiple ops with deeper paths. If the engine rejects a merge op (path > 32 segments, segment > 256 bytes, value > 16 levels deep, > 1024 top-level keys, or a __proto__ / constructor / prototype segment or top-level key), it returns an entry in the response’s errors array with a stable code, message, and doc_url. Successfully applied ops still reflect in new_value.

Result

State is shared across all Functions in the system. Any Function can read or write to any scope/key pair. The Engine handles consistency and persistence based on the configured adapter.
The default file_based adapter works for single-instance use. For production with multiple replicas, use a persistent adapter like Redis. See the State worker reference.