wayflow/runtime

The execution engine — createRuntime, running graphs, tools, sessions, and pausing.

The headless execution engine. It runs a graph with the handlers you register — no editor required.

createRuntime(options?)

Creates a runtime that can run graphs.

runtime.ts ts
const runtime = createRuntime({ handlers, tools })

Options

OptionTypeDefaultNotes
handlersRecord<string, Handler>Maps each node type to the function that runs it (keyed llm, imageGeneration, …). See Providers & models.
toolsRecord<string, Tool>Tools an LLM node may call; build them with defineTool. See Tools.
loggerLoggerA diagnostics sink. See Debugging.
debugbooleanfalseShorthand for a console logger; a logger wins over it.

Handler

The function that runs a node type — what you register in handlers. The built-in createLLMHandler / createImageGenerationHandler return one; a custom node type supplies its own. See Custom node types.

handler.ts ts
type Handler = (
  node: Node,
  inputs: Record<string, unknown>,
  ctx: HandlerContext,
) => Promise<unknown> | AsyncIterable<unknown>

Return a Promise for a one-shot result, or an async function* to stream (each yield emits a chunk). inputs is keyed by the node’s input-port id.

HandlerContext

MemberNotes
signalAn AbortSignal for cancellation.
inputsThe caller’s run inputs (from runtime.run({ inputs })).
emit(event)Emit a RuntimeEvent into the run stream.
reportInputs(inputs)Replace the inputs shown for this node in run views with the values it actually used.
collectAsResult(fields)Contribute fields to the run’s final result (last-write-wins on conflict).
toolsThe tools matching this node’s tools-select config.
runGraph(graph, inputs?)Run a sub-graph on the same engine (a workflow exposed as a tool); resolves to its result.

The Runtime handle

MethodNotes
run(graph, options?)Run to the first outcome (completed or paused). Returns a Promise<RunOutcome>.
stream(graph, options?)Run and yield RuntimeEvents as they happen (AsyncIterable) — for streaming into the editor or an SSE response.
describe()The registered tools with handlers stripped — a catalog safe to hand the editor.

RunOptions

OptionTypeNotes
inputsRecord<string, unknown>Values for the workflow’s Input fields.
signalAbortSignalAbort the run — how cancellation works.
resume{ checkpoint, decision }Resume a paused run from its checkpoint (see Sessions for the higher-level API).

RunOutcome

A run settles into one of two shapes, discriminated by status:

statusCarriesMeans
'completed'result: RThe run finished; result holds the Output fields.
'paused'suspension: SuspensionA node suspended (e.g. human review); resume to continue.

Tools

FunctionNotes
defineTool({ description, args, handler })Define a tool an LLM can call — a description, typed args, and the handler that does the work. See Tools.
defineToolMetadata({ description, args })The same shape without a handler — for the editor or client, which only need a tool’s name and description.
graphToTool(graph, options?)Expose a whole workflow as one tool; options.description overrides the graph’s own. See Workflows as tools.
graphsToTools(graphs)Turn many graphs into a tool map, keyed by each graph’s name.

The object passed to defineTool (and to defineToolMetadata, minus handler):

FieldTypeNotes
descriptionstringWhat the tool does — the model reads this to decide when to call it.
argsRecord<string, ArgDecl>The typed arguments, e.g. { city: 'string' }; the model fills them in.
handler(args, ctx) => Promise<unknown>Runs the tool with the typed args and returns what the model sees back.

Sessions

Pause and resume runs across requests — human-in-the-loop on a server.

FunctionNotes
createRunSessions(runtime, { store? })Wrap a runtime with checkpoint storage so paused runs survive between requests. Defaults to in-memory. See Human-in-the-loop.
createMemoryCheckpointStore()The default in-memory CheckpointStore — swap for one backed by your database in production.

The RunSessions handle

MethodNotes
run(graph, options?)One-shot run to the first outcome; stores the checkpoint on a pause. The paused outcome carries the runId.
stream(graph, options?)Streaming run; stores and strips the checkpoint on a pause. Wrap in your transport (streamResponse).
resume(request)One-shot resume by runId → the next outcome, or null if the runId is unknown.
resumeStream(request)Streaming resume by runId, or null if unknown or already resumed.
cancel(runId)Drop a paused run’s checkpoint without resuming.
listPending()Every waiting run — powers an approval inbox and the editor’s re-attach.
getPending(runId)One waiting run by runId, or null.

resume and resumeStream take a ResumeRequest:

FieldTypeNotes
runIdstringThe paused run to continue.
decision{ branch, data? }The review outcome — which branch to route to, and optionally edited data.
signalAbortSignalAbort the resumed run.

CheckpointStore

Implement this to persist paused runs in your own database.

MethodNotes
save(runId, record)Store a paused run’s checkpoint record.
load(runId)Load a record by runId, or undefined.
delete(runId)Remove a record — after a resume or cancel.
list()All stored records, for the pending inbox.

Pausing a run

ExportNotes
suspend(params?)Called inside a handler to pause the run for input, returning the reviewer’s decision when resumed. See Human-in-the-loop.

SuspendParams

FieldTypeNotes
instructionsstringA message shown to the reviewer about what to decide.
dataunknownThe value under review, surfaced to the reviewer.

Two union types describe a run’s branch and state:

types.ts ts
type ReviewBranch = 'approved' | 'rejected'
type RunStatus = 'running' | 'completed' | 'paused' | 'cancelled' | 'error'
  • ReviewBranch — the branch you set in a resume Decision (which output port the gate routes to).
  • RunStatus — a run’s status, on a RunOutcome and on run-status events.

wayflow/runtime/client

Drives a server-side runtime from the browser — what the editor’s Run button calls.

run(options)

Stream a run from your server endpoints into the editor. See In the browser.

OptionTypeNotes
urlstringThe endpoint that runs the workflow. Required.
editorWorkflowEditorThe editor to stream the run into. Required.
resumeUrlstringEndpoint that resumes a paused run.
cancelUrlstringEndpoint that drops a paused run’s checkpoint.
inputsRecord<string, unknown>Values for the workflow’s Input fields.
initRequestInit, or () => RequestInitPer-request fetch options (auth headers, credentials); a function is resolved fresh per request.

ApprovalDriver is the editor capability that shows the approval card during a paused run — supplied by the editor automatically.

wayflow/runtime/sse

Helpers for streaming a run over Server-Sent Events from your server.

streamResponse(events, options?)

Wrap a runtime event stream as a web-standard Response — for servers built on the Fetch API. See On a server.

ParameterTypeNotes
eventsAsyncIterable<RuntimeEvent>The stream from runtime.stream() or sessions.stream().
optionsStreamResponseOptionsOptional status and extra headers (below).

StreamResponseOptions

OptionTypeDefaultNotes
statusnumber200The response status code.
headersRecord<string, string>Extra headers, merged onto the SSE defaults.

writeSSE(res, events)

Stream events to a Node-style response instead — for servers not built on the Fetch API. Returns a Promise<void>.

ParameterTypeNotes
resSSEWritableA Node-style response — anything with setHeader, write, and end.
eventsAsyncIterable<RuntimeEvent>The stream from runtime.stream() or sessions.stream().