Skip to content

Durable extension work

Define tasks, docs and signals with ctx.durable so an extension's work survives a restart of the host.

ctx.durable gives an extension the durable kernel: tasks that go on after the host restarts, versioned docs and signals. Define them with defineTask, defineDoc and defineSignal from zidane/durable.

TypeScript
import { defineDoc, defineTask } from 'zidane/durable'
import { defineExtension } from 'zidane/extensions'

const notes = defineDoc<{ items: string[] }>({ kind: 'notes', version: 1, scope: 'conversation', initial: () => ({ items: [] }) })
const digest = defineTask<{ topic: string }, null, string>({
  kind: 'digest',
  version: 1,
  initial: () => ({ phase: 'write', checkpoint: null }),
  phases: { write: async rt => ({ done: `digest of ${rt.input.topic}` }) },
})

export default defineExtension({
  apiVersion: 1,
  id: 'digests',
  setup(ctx) {
    ctx.durable.define(notes, digest)
    ctx.tools.register('remember', {
      spec: { name: 'remember', description: 'Keep a note and start its digest', inputSchema: { type: 'object', properties: { text: { type: 'string' } }, required: ['text'] } },
      execute: async ({ text }, toolCtx) => {
        const session = ctx.durable.session(toolCtx.session?.id)
        await session.doc(notes).update((draft) => {
          draft.items.push(text as string)
        })
        await session.startTask(digest, { topic: text as string }, { key: text as string })
        return 'kept'
      },
    })
  },
})
define(...definitions)
Registers them for this extension. Unloading the extension withdraws them, and its tasks wait until it loads again.
session(id?)
The extension's work in a session. Without an id, the session the call runs for.
session().doc(def, target?)
read, update (one commit) and watch a doc. A conversation doc defaults to the root conversation. watch calls the listener with { kind: 'value', value, ops } at once and after each commit that changed the doc, then with { kind: 'failed', error, value } if the session stops.
session().startTask(def, input, { key? })
Starts a task. The same key returns the same task.
session().signal(taskId, def, value)
Delivers a signal to a waiting task.
session().waitTask(taskId), abortTask(taskId)
Waits for the end of a task, or asks for its abort. A wait rejects with DisposedError when the extension unloads first.
project(id).doc(def)
A doc every session and person of a project share, with a revision and who wrote it last. No define needed. See project docs.
  • Kinds are stored as ext.<extension id>.<kind>, so two extensions never share one. An extension reaches only its own definitions and tasks.
  • A phase is your code, which the kernel runs. When a phase throws, the task fails, and the kernel records the error.
  • The daemon and zidane-server keep these on their sessions file. At start the daemon opens every session with a live task, so a task goes on once its extension defines it. To keep the daemon running with no window open, run zidane daemon install (the daemon).
  • On a host without a durable runtime, every member throws an error that names the host.
  • In tests, pass a runtime from openDurable: createTestHost({ extensions, ports: { host: { durable } } }).