Skip to main content
Distributed key-value state storage with scope-based organization and reactive triggers that fire on any state change.

Architecture

State is server-side key-value storage with trigger-based reactivity. Unlike streams, state does not push updates to WebSocket clients — it fires triggers that workers handle server-side.

Sample Configuration

Configuration

adapter
Adapter
The adapter to use for state persistence and distribution. Defaults to kv when not specified.

Adapters

kv

Built-in key-value store. Supports both in-memory and file-based persistence.

Configuration

store_method
string
Storage method. Options: in_memory (lost on restart) or file_based (persisted to disk).
file_path
string
Directory path for file-based storage. Each scope is stored as a separate file.
save_interval_ms
number
Interval in milliseconds between automatic disk saves. Defaults to 5000.

redis

Uses Redis as the state backend.

Configuration

redis_url
string
The URL of the Redis instance to use.

bridge

Forwards state operations to a remote III Engine instance via the Bridge Client.

Functions

state::set
function
Set a value in state. Fires a state:created trigger if the key did not exist, or state:updated if it did.
scope
string
required
The scope (namespace) to organize state within.
key
string
required
The key to store the value under.
value
any
required
The value to store. Can be any JSON-serializable value. Also accepted as data (backward-compatible alias).
old_value
any
The previous value, or null if the key did not exist.
new_value
any
The value that was stored.
state::get
function
Get a value from state.
scope
string
required
The scope to read from.
key
string
required
The key to retrieve.
value
any
The stored value, or null if the key does not exist.
state::delete
function
Delete a value from state. Fires a state:deleted trigger.
scope
string
required
The scope to delete from.
key
string
required
The key to delete.
value
any
The deleted value, or null if the key did not exist.
state::update
function
Atomically update a value using one or more operations. Fires state:created or state:updated depending on whether the key existed.
scope
string
required
The scope to update within.
key
string
required
The key to update.
ops
UpdateOp[]
required
Array of update operations applied in order. Each operation is a tagged object with a type field and a path. Use path: "" (or omit path) to target the root value.For set, increment, decrement, and remove, paths are first-level field names. For example, user.name updates the field named user.name; it does not traverse into { "user": { "name": ... } }.For merge and append, path accepts either a single string (legacy / first-level field) or an array of literal segments for nested traversal:
Each array element is a literal key. ["a.b"] writes a single key named "a.b", not a → b.For root operations (no path), the SDK encoders omit the path field from the wire payload entirely (e.g. { "type": "append", "value": "first" }) rather than emitting "path": null. Servers accept either form on input — null, missing, and empty string all route to the root.Append at a nested missing leaf is always an array. When append walks to a missing leaf at the end of an array-form path, it creates [value] regardless of the value’s type — including string values, which would be kept as a string under the legacy single-string path’s string-concat tier. This is the core fix for issue #1552.Append walks through array intermediates by replacing them with objects. When the path traverses an existing non-object intermediate (array, scalar, or null), the engine replaces it with a fresh {} and continues — mirroring merge’s walk_or_create semantics. So {"a": [1,2,3]} + append(["a", "b"], 42) yields {"a": {"b": [42]}} (the prior array at a is dropped). Callers that need to preserve the array should pre-check with state.get rather than relying on append to error.Note on append.type_mismatch for nested paths: the structured append.type_mismatch error for object/scalar leaves shipped in #1555 for the single-string-path case. The nested-path form added here returns the same error code with the same shape, so consumers parsing errors[] need no new branches. Callers using path: "" or path: "field" against array, string, null, or missing-field leaves are unaffected.Validation: invalid update inputs are rejected with a structured error in the response’s errors array. Reasons include path depth > 32 segments, segment > 256 bytes, value depth > 16, > 1024 top-level keys, type mismatches, non-object targets, or any segment / top-level key matching __proto__ / constructor / prototype. Successfully applied ops still reflect in new_value.
old_value
any
The value before the operations were applied, or null if the key did not exist.
new_value
any
The value after all operations were applied.
errors
UpdateOpError[]
Per-op validation errors. Field is omitted when empty. Each entry has op_index, code, message, and an optional doc_url.

Error codes

Each state::update op may add an entry to the response errors array. Operations are best-effort: successfully applied ops still reflect in new_value, and failed ops are skipped. Each error includes op_index, code, and message; doc_url is optional.
state::list
function
List all values within a scope.
scope
string
required
The scope to list entries from.
A flat JSON array of all stored values within the scope: any[].
state::list_groups
function
List all scopes that contain state data.
An object with a single groups field:
groups
string[]
A sorted, deduplicated array of all scope names that contain at least one key.

Trigger Type

This worker adds a new Trigger Type: state. When a state value is created, updated, or deleted, all registered state triggers are evaluated and fired if they match.

State Event Payload

When the trigger fires, the handler receives a state event object:
type
string
Always "state".
event_type
string
The kind of change: "state:created", "state:updated", or "state:deleted".
scope
string
The scope where the change occurred.
key
string
The key that changed.
old_value
any
The previous value before the change, or null for newly created keys.
new_value
any
The new value after the change. null for deleted keys.

Sample Code

Usage Example: User Profile with Reactive Sync

Store user profiles in state and react when they change:

Usage Example: Conditional Trigger

Only process profile updates when the email field changed:

State Flow