Documentation menu
API reference

Workflow APIs

Co-located durable workflows without repeated agent classes or binding names.

AgentWorkflow<Params, Progress?>

Use this for a workflow.ts beside an agent. Params types event.payload and the agent’s this.workflow(params) call. Progress optionally types reportProgress().

agents/review/workflow.ts
import { AgentWorkflow } from "ayjnt/workflows";
import type {
  AgentWorkflowEvent,
  AgentWorkflowStep,
} from "ayjnt/workflows";

type Params = { documentId: string };

export default class ReviewWorkflow extends AgentWorkflow<Params> {
  async run(
    event: Readonly<AgentWorkflowEvent<Params>>,
    step: AgentWorkflowStep,
  ) {
    const result = await step.do("review", async () => {
      return { documentId: event.payload.documentId, approved: true };
    });
    await step.reportComplete(result);
    return result;
  }
}

Start it from the co-located agent

Co-location supplies the workflow binding. The generated declarations connect the workflow’s Params to this.workflow(), so a missing or misspelled field is a TypeScript error.

agents/review/agent.ts
import { Agent, callable } from "ayjnt";

export default class ReviewAgent extends Agent {
  @callable()
  async review(documentId: string) {
    return this.workflow({ documentId });
  }

  async onWorkflowComplete(
    workflowName: string,
    workflowId: string,
    result?: unknown,
  ) {
    console.log({ workflowName, workflowId, result });
  }
}

Workflow<Params, Env?>

Use Workflow for a plain Cloudflare Workflow without an originating agent. Its first generic is still the payload type; the generated environment is the default second generic.

example
import { Workflow } from "ayjnt/workflows";

export default class NightlyWorkflow
  extends Workflow<{ date: string }> {
  async run(event: WorkflowEvent<{ date: string }>, step: WorkflowStep) {
    return step.do("summarize", () => summarize(event.payload.date));
  }
}

Exports and escape hatches

ExportBaseUse
AgentWorkflowagents/workflows AgentWorkflowCo-located agent workflow; Params first
Workflowcloudflare:workers WorkflowEntrypointPlain workflow; Params first
CloudflareAgentWorkflowdirect upstream exportEscape hatch
WorkflowEntrypointdirect upstream exportEscape hatch