Skip to content
Looms
Documentation
On this page

API reference

The embed API runs within one process. Actor hosts expose a similar HTTP contract with one writer per run. Signatures below are extracted from the runtime source; the client returns HTTP response envelopes rather than exactly the same shapes as embed methods.

Create a runtime#

ts
import { createLooms } from '@swirls/looms/runtime'import { modules, release } from './review-definition'const looms = createLooms({ modules })try {  const { runId, state } = await looms.start(release, { topic: 'Launch' })  console.log(runId, state.status)} finally {  await looms.stop()}

This uses the quickstart definition and an in-memory store. Pass a persistent store before relying on restart recovery. Nothing is installed by default: modules defaults to an empty list, store to memory, and serve to off.

ts
export interface CreateLoomsOptions<Fields extends RunAttributeFields = RunAttributeFields> {  /** Runtime modules to enable. Pass `agent()`, `workflow()`, `approval()`, and/or your own. */  readonly modules?: readonly AnyRuntimeModule[]  /**   * Declared run attributes, from `defineRunAttributes`. They type `attributes` on every start.   * A start that misses a `required` attribute or sends an undeclared one is rejected.   */  readonly attributes?: Fields  /**   * The id format for every event, run, thread, and wait. Defaults to `typeIds` (TypeID). See   * `CreateRuntimeOptions.ids` for the rules a custom format must keep.   */  readonly ids?: LoomsIds  /**   * Loads a definition that was not registered on a module. Registered definitions are checked   * first. A run pins the resolved version on its start event.   */  readonly definitionResolver?: DefinitionResolver  readonly store?: EventStore | Promise<EventStore> | (() => Promise<EventStore>)  readonly serve?: boolean | { port?: number; hostname?: string }  readonly maxWakeIterations?: number  /** See `CreateRuntimeOptions.livenessLeaseMs`. Only used when `scheduler` implements `keepAlive`. */  readonly livenessLeaseMs?: number  /** Maximum active and queued mutation operations per run. Default 256. */  readonly maxPendingRunOperations?: number  /** Durable events between mid-wake snapshots. Default 200; `0` disables. Parking always snapshots. */  readonly snapshotEvery?: number  /** Defaults to the EventStore's attached store (S2) or an in-memory SnapshotStore. */  readonly snapshotStore?: SnapshotStore  /**   * Content-addressed store for JSON values that would exceed the 1 MiB event limit or the   * 8 MiB snapshot limit. Reads resolve references before folds, projections, and callers see them.   */  readonly blobStore?: BlobStore  readonly trimAfterSnapshot?: {    keepSnapshots: number    coverage?: (runId: string) => EventStoreTrimCoverage | Promise<EventStoreTrimCoverage>  }  /** In-process cursor cache size. Default `0` (stateless between calls). */  readonly runCacheSize?: number  /** Scan existing runs for pending timers on boot. Default true. */  readonly rescanTimers?: boolean  /** Pluggable wake scheduler for timer waits. Defaults to an in-process Effect fiber scheduler. */  readonly scheduler?: WakeScheduler  /** Starts runs from effect handlers. Defaults to launching into this runtime's own store. */  readonly launcher?: RunLauncher  readonly worker?: EffectWorker  /** Maximum concurrent in-flight worker effects dispatched per run (fairness guard). */  readonly maxInFlightWorkerEffects?: number  /** Failure-isolated runtime observation sink. */  readonly observer?: RuntimeObserver  /** Decides what a run holds and what each attempt receives. See `CreateRuntimeOptions`. */  readonly secretProvider?: SecretProvider  /** Encrypts what a run holds before it is stored. See `secretSealer`. */  readonly secretSealer?: SecretSealer  /**   * Authorizes HTTP API requests. Without it, run reads and writes are open while worker callback   * and operations routes return 503; `bearerAuth({ worker, operations })` enables them.   *   * A mounted debugger is authorized as `{ access: 'read', name: 'debugger' }`. Bearer tokens on   * reads block browser navigation. `grantAuth` covers a browser EventSource or WebSocket that can   * only send a query parameter or a subprotocol.   */  readonly authorize?: Authorize  /**   * Serves a debugger UI under `debugger.basePath` (for example `/debugger`). The runtime does not   * bundle a UI; pass `debuggerUi()` from `@swirls/looms/debugger/server`.   */  readonly debugger?: DebuggerHost}

snapshotEvery defaults to 200 durable events; zero disables mid-wake snapshots, while parking still snapshots. runCacheSize defaults to zero. maxWakeIterations defaults to 100 and bounds a wake cycle, not application cost. The default scheduler uses process-local timers. trimAfterSnapshot is opt-in and can remove historical replay data.

Embed methods and return types#

ts
export interface StartResult {  readonly runId: string  readonly threadId: string  readonly state: RunState}export type LoomsStartOptions<Fields extends RunAttributeFields = RunAttributeFields> = {  runId?: string  threadId?: string  idempotencyKey?: string  secrets?: StartRunArgs['secrets']  trace?: StartRunArgs['trace']} & RunAttributesOption<Fields>export type LoomsStartRunArgs<Fields extends RunAttributeFields = RunAttributeFields> = {  kind: string  definitionName: string  definitionVersion?: string  input?: JsonValue} & LoomsStartOptions<Fields>/** `start`'s arguments after the definition. Options are required when an attribute is. */export type LoomsStartArgs<  TDef extends DefinitionRef,  Fields extends RunAttributeFields = RunAttributeFields,> =  HasRequiredAttributes<Fields> extends true    ? [input: DefinitionInput<TDef> | undefined, options: LoomsStartOptions<Fields>]    : [input?: DefinitionInput<TDef>, options?: LoomsStartOptions<Fields>]export interface Looms<Fields extends RunAttributeFields = RunAttributeFields> {  readonly runtime: Promise<LoomsRuntime>  readonly store: Promise<EventStore>  ready(): Promise<{ store: EventStore; runtime: LoomsRuntime }>  /** Start a run from a definition object; the input type follows the definition's schema. */  start<TDef extends DefinitionRef>(    definition: TDef,    ...args: LoomsStartArgs<TDef, Fields>  ): Promise<StartResult>  /**   * Like `start`, but answers once the start is committed and leaves the first wake to the   * scheduler.   */  launch<TDef extends DefinitionRef>(    definition: TDef,    ...args: LoomsStartArgs<TDef, Fields>  ): Promise<StartResult>  /** Start a run by `{ kind, definitionName }` when you only have names (HTTP bodies, CLIs). */  startRun(args: LoomsStartRunArgs<Fields>): Promise<StartResult>  /**   * Like `startRun`, but answers once the start is committed and leaves the first wake to the   * scheduler.   */  launchRun(args: LoomsStartRunArgs<Fields>): Promise<StartResult>  getRun(runId: string): Promise<RunState>  getEvents(runId: string, options?: { fromSeq?: number; limit?: number }): Promise<EventEnvelope[]>  signal(    runId: string,    events: ReadonlyArray<EventInput>,    options?: RunSignalOptions,  ): Promise<RunState>  wake(runId: string): Promise<RunState>  rescanTimers(): Promise<number>  recoverDeadlines(): Promise<number>  inspectRun(runId: string): Promise<RunOperationalStatus>  retryEffect(runId: string, effectId: string): Promise<RunState>  listRuns(): Promise<string[]>  project<S>(runId: string, definition: ProjectionDefinition<S>): Promise<S>  replayTo(runId: string, seq: number): Promise<ReplayStep | null>  cancel(runId: string, threadId?: string): Promise<RunState>  /** Start a new run that continues where a finished run ended. */  fork(    sourceRunId: string,    options?: { runId?: string; secrets?: StartRunArgs['secrets'] },  ): Promise<StartResult>  workerCallback(runId: string, input: WorkerCallbackInput): Promise<WorkerCallbackResult>  fetch(req: Request): Promise<Response | null>  serve(options?: { port?: number; hostname?: string }): RunningServer  stop(): Promise<void>}

start and startRun#

start infers input from a definition; startRun accepts registered names. Both run a wake cycle and resolve with runId, threadId, and state. A gate or child may still be pending when either method resolves. They reject on invalid input, missing definitions, storage failures, or execution limits.

Use a stable runId and idempotencyKey when retrying creation of the same logical run. Generating a new runId on every retry creates a different run. Register all definitions before starting or spawning them.

signal, wake, and cancel#

signal validates and appends inputs, then wakes execution and returns state. Its optional idempotencyKey identifies retries of the same request within a run. A completed, failed, or cancelled run rejects new signals with RunTerminalError; a retry of an already-accepted idempotencyKey still returns state. wake processes pending work without appending a new domain signal. cancel records cancellation for a run or thread; it cannot undo an external action.

fork#

fork(sourceRunId, { runId? }) starts a new run that continues where a failed or cancelled run stopped, and resolves like start. See continue a finished run. It rejects a missing run with RunNotFoundError, a run that has not finished with RunNotFinishedError, a runId that already holds a different run with ForkConflictError, and a source without its full history from seq 1 with ForkSourceError. Retrying with the same runId returns the existing fork.

Read and inspect#

getRun returns folded RunState. getEvents returns recorded envelopes ordered by sequence, with optional fromSeq and limit. project folds a projection and returns its state. replayTo returns before/after state at a sequence, or null when no matching step is available; it does not execute effects.

ready initializes storage and modules. rescanTimers re-registers persisted timers on the configured scheduler. fetch returns a Response or null for unhandled paths. serve starts the Bun HTTP server. stop disposes the runtime scheduler and any server it started; it does not cancel runs or back up storage.

HTTP contract#

Put authentication and authorization ahead of every route, including event streams. JSON requests use content-type: application/json.

text
GET  /health                            → { ok: true }GET  /definitions                       → definitions, projections, and modulesGET  /definitions/effects               → { effects: [{ type, placement, retry, inputSchema }] }GET  /definitions/signals               → { signals: [{ namespace, name, description?, inputSchema? }] }GET  /definitions/events                → { events: [{ type, namespace, payloadSchema? }] }POST /definitions/signals/:ns/:name/preview → { events } (validate + build, no append)POST /definitions/events/preview        → { events } (validate payloads, no append)POST /runs                              → { runId, threadId, state }GET  /runs                              → { runIds } (embed only)GET  /runs/summaries                    → { runs }GET  /runs/:id                          → { runId, state }GET  /runs/:id/events                   → { runId, events }POST /runs/:id/events                   → { runId, state }POST /runs/:id/signals/:ns/:name        → { runId, state } (build from signal input, then append)GET  /runs/:id/threads                  → { runId, threads }GET  /runs/:id/status                   → run, deadline, and effect statusGET  /runs/:id/effects                  → { runId, effects } (includes placement)POST /runs/:id/effects/:effect/retry    → operator-reviewed retryPOST /runs/:id/wake                     → { runId, state, appended }POST /runs/:id/cancel                   → { runId, state }POST /runs/:id/fork                     → { runId, threadId, state } (embed only)GET  /runs/:id/replay?seq=10             → { runId, step }GET  /runs/:id/projections/:name         → { runId, name, value }GET  /runs/:id/events?live=true          → SSEPOST /operations/deadlines/rescan        → rebuild wake deadlines

POST /runs accepts kind, definitionName, input, and optionally runId and idempotencyKey. POST /runs/:id/events accepts an events array and optionally idempotencyKey. POST /runs/:id/cancel accepts an optional threadId and cancels the root thread without one. POST /runs/:id/wake reports in appended how many events the run gained while it ran. POST /runs/:id/fork accepts an optional runId for the new run. The embed handler also recognizes the Idempotency-Key header. GET event history supports fromSeq and limit. Request the same resource with Accept: text/event-stream or live=true for SSE. Streams resume after Last-Event-ID, or begin at fromSeq when that header is absent.

bash
curl -X POST http://localhost:8787/runs \  -H 'content-type: application/json' \  -d '{"kind":"workflow","definitionName":"release-v1","input":{"topic":"Launch"}}'

Actor hosts return 501 for global GET /runs, where you would use an application registry or shared index, and for POST /runs/:id/fork, because each run lives in its own cell. The embed handler maps InvalidInputError and InvalidEventError to 400, unknown projections and RunNotFoundError to 404, RunTerminalError and the fork errors to 409, and other caught execution failures to 500. Routing behavior differs by host; inspect status and body. Client request helpers reject non-success responses.

Client and streaming#

createLoomsClient accepts baseUrl and an optional fetch implementation. getRun, signal, wake, cancel, and fork resolve to objects containing state; getEvents resolves to an object containing events. subscribeEvents returns an unsubscribe function and reconnects using its last cursor. stream(definition, input) or streamRun(args) returns an async iterable with a runId for events during the initial wake cycle. A stream ending does not mean a parked workflow has finished.

The current client start/signal helpers do not expose every embed idempotency option. For retry-sensitive requests, use an application endpoint or explicit HTTP bodies/headers with stable operation identifiers. See integration.

Package modules and extension APIs#

  • @swirls/looms/core: defineModule, defineKind, createKind, defineEventCatalog, createModuleScope, defineThread, defineEffect, defineProjection; invoke, spawn, wait, emit, complete, fail; findCatalogEntry, validateEventInputSync. Modules expose signals (event builders) and steps (effect composers). The bun-sqlite entry supplies Bun storage.
  • @swirls/looms/runtime: createLooms and lower-level runtime APIs.
  • @swirls/looms/agent: agent, defineAgent, defineTool, asThreadTool, asAgentTool, asWorkflowTool, asEvaluatorTool, defineAgentSession, submitAgentMessage, conversation, and userMessage.
  • @swirls/looms/workflow: workflow, defineWorkflow, graph validation, failure policies, and map, fan-out, and while helpers.
  • @swirls/looms/approval: approval, gate, decision, pendingApprovals.
  • @swirls/looms/evaluator: evaluator, defineEvaluator, evaluate, evaluations.
  • @swirls/looms/actor: createActorCell and createLocalActorHost.
  • @swirls/looms/cloudflare: LoomsDurableObject and routeToDurableObject.
  • @swirls/looms/client and @swirls/looms/react: HTTP clients, streams, providers, and projection hooks.
  • @swirls/looms/ai-vercel: vercelLlm for AI SDK chat models; vercelEvaluator for evaluation models such as TypeSafe Jev.
  • @swirls/looms/projectors: post-commit indexes and fan-out. @swirls/looms/s2: stream replication.
  • @swirls/looms/testing: deterministic in-memory helpers and moduleConformance, which also folds module examples.
  • @swirls/looms/otel: OpenTelemetry spans and metrics for runtime observations.
  • @swirls/looms/cli and @swirls/looms/debugger: inspection tools.

For module authoring, see custom modules. For agent options and tools, see agents. Package type declarations remain the exhaustive reference for lower-level interfaces.