Skip to main content

Documentation Index

Fetch the complete documentation index at: https://iii.dev/docs/llms.txt

Use this file to discover all available pages before exploring further.

Goal

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

Steps

1. Enable the State worker

iii-config.yaml
workers:
  - name: iii-state
    config:
      adapter:
        name: kv
        config:
          store_method: file_based  # Options: in_memory, file_based
          file_path: ./data/state_store.db  # required for file_based

2. Write state

state-writer.ts
import { registerWorker, Logger } from 'iii-sdk'

const iii = registerWorker(process.env.III_URL ?? 'ws://localhost:49134')

iii.registerFunction('users::create', async (input) => {
  const logger = new Logger()
  const userId = crypto.randomUUID()
  const user = { id: userId, name: input.name, email: input.email }

  await iii.trigger({
    function_id: 'state::set',
    payload: { scope: 'users', key: userId, value: user },
  })

  logger.info('User saved to state', { userId })
  return { userId }
})

// Then call from another function or worker
const { userId } = await iii.trigger({
  function_id: 'users::create',
  payload: { name: 'Alice', email: 'alice@example.com' },
})
logger.info('Created user', { userId })

3. Read state

state-reader.ts
iii.registerFunction('users::get', async (input) => {
  const logger = new Logger()

  const user = await iii.trigger({
    function_id: 'state::get',
    payload: { scope: 'users', key: input.userId },
  })

  logger.info('User retrieved', { userId: input.userId })
  return user
})

// Then call from another function or worker
const user = await iii.trigger({
  function_id: 'users::get',
  payload: { userId: 'some-user-id' },
})
logger.info('Retrieved user', user)

4. Atomic updates

Use state::update with update operations for safe concurrent modifications:
state-update.ts
iii.registerFunction('users::record-login', async (input) => {
  const logger = new Logger()

  await iii.trigger({
    function_id: 'state::update',
    payload: {
      scope: 'users',
      key: input.userId,
      ops: [
        { type: 'set', path: 'lastLogin', value: new Date().toISOString() },
        { type: 'increment', path: 'loginCount', by: 1 },
      ],
    },
  })

  logger.info('User login recorded', { userId: input.userId })
  return { updated: true }
})

// Then call from another function or worker
await iii.trigger({
  function_id: 'users::record-login',
  payload: { userId: 'some-user-id' },
})
logger.info('Login recorded')
Append operations are useful for incremental data such as transcript chunks, progress events, or small audit trails:
append-transcript.ts
await iii.trigger({
  function_id: 'state::update',
  payload: {
    scope: 'calls',
    key: callId,
    ops: [
      { type: 'append', path: 'transcript', value: 'Hello ' },
      { type: 'append', path: 'events', value: { type: 'chunk', offset: 0 } },
    ],
  },
})
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
await iii.trigger({
  function_id: 'state::update',
  payload: {
    scope: 'audio::transcripts',
    key: 'session-abc',
    ops: [
      // Nested path: walks into "session-abc" → "metadata", auto-
      // creating each intermediate object. Sibling keys at any level
      // are preserved.
      {
        type: 'merge',
        path: ['session-abc', 'metadata'],
        value: { author: 'alice' },
      },
      // First-level form (sugar for path: ['session-abc']).
      {
        type: 'merge',
        path: 'session-abc',
        value: { [Date.now()]: 'transcript chunk' },
      },
    ],
  },
})
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.