Skip to content
Source
Sign inSign up
TanStack/workflow

packages/workflow-core

README.md

@tanstack/workflow-core

Type-safe durable execution. Closure-based workflows with replay, pause/resume, typed middleware, and a pluggable event log.

pnpm add @tanstack/workflow-core zod

Hello workflow

import {
  createWorkflow,
  inMemoryRunStore,
  runWorkflow,
} from '@tanstack/workflow-core'
import { z } from 'zod'

const greet = createWorkflow({
  id: 'greet',
  input: z.object({ name: z.string() }),
}).handler(async (ctx) => {
  const greeting = await ctx.step('build', () => `Hello, ${ctx.input.name}!`)
  return { greeting }
})

for await (const event of runWorkflow({
  workflow: greet,
  input: { name: 'world' },
  runStore: inMemoryRunStore(),
})) {
  console.log(event.type, event)
}

What you get on ctx

FieldTypePurpose
ctx.inputtyped from input schemarequest payload
ctx.statetyped from state schemamutable; tracked between primitives, emitted as STATE_DELTA
ctx.runIdstringstable identifier; safe as an idempotency key
ctx.signalAbortSignalrun-level cancellation
ctx.step(id, fn, opts?)Promise<T>durable side-effect with replay
ctx.sleep(ms, opts?) / ctx.sleepUntil(ts, opts?)Promise<void>durable pause via __timer signal
ctx.waitForEvent(name, opts?)Promise<TPayload>pause until host delivers a signal
ctx.approve({ id?, title, description? })Promise<ApprovalResult>pause for human approval
ctx.now(opts?) / ctx.uuid(opts?)Promise<number / string>deterministic recorded values
ctx.emit(name, value)voidobservability-only custom event

Middleware can add more.

For every durable primitive except ctx.step(id, ...), pass id in the options object when you want replay to survive later reordering: ctx.waitForEvent('payment', { id: 'payment-webhook' }), ctx.approve({ id: 'legal-review', title: 'Ship?' }), ctx.now({ id: 'started-at' }).

Pause and resume

// Run pauses at ctx.approve / ctx.waitForEvent. Capture runId, send a delivery.
const store = inMemoryRunStore()
const phase1 = await collect(runWorkflow({ workflow, input, runStore: store }))
const runId = findRunId(phase1)
const approvalId = phase1.find(
  (e) => e.type === 'APPROVAL_REQUESTED',
)!.approvalId

await collect(
  runWorkflow({
    workflow,
    runId,
    runStore: store,
    approval: { approvalId, approved: true },
    // β€” or β€”
    signalDelivery: {
      signalId: 'evt-1',
      name: 'manager-approval',
      payload: { ok: true },
    },
  }),
)

Status

Pre-alpha. Public API stable in shape. The production runtime, Drizzle/Postgres store, Vercel adapter, and Netlify adapter live in sibling experimental packages. Bindings (React, Solid, Vue, Svelte), additional stores, and devtools are still planned.

Extracted from @tanstack/ai-orchestration (Alem Tuzlak + Tom Beckenham). AI-specific layers (agents, orchestrators) compose on top.

Docs