Skip to main content

Overview

AgentWorkflow extends Cloudflare’s WorkflowEntrypoint to provide seamless access to the Agent that started the workflow, enabling bidirectional communication and typed RPC.

Type Parameters

Agent
default:"Agent"
The Agent class type (for typed RPC access)
unknown
default:"unknown"
User-defined params passed to the workflow
DefaultProgress
default:"DefaultProgress"
Type for progress reporting
Cloudflare.Env
default:"Cloudflare.Env"
Environment type

Properties

agent

DurableObjectStub<AgentType>
required
The Agent stub for RPC calls. Provides typed access to the Agent’s methods.

workflowId

string
required
Workflow instance ID (from Cloudflare Workflows)

workflowName

string
required
Workflow binding name (from environment)

Lifecycle

run()

AgentWorkflowEvent<Params>
required
Workflow event with user-defined params
AgentWorkflowStep
required
Durable step object with Agent communication methods
Main workflow implementation. Override this method with your workflow logic.
Returns: Promise<unknown> - Workflow result

AgentWorkflowStep

The step parameter is a standard WorkflowStep extended with Agent communication methods:

step.reportComplete()

Report successful completion to the Agent.
T
Result data to send
Returns: Promise<void>

step.reportError()

Report an error to the Agent.
Error | string
required
Error to report
Returns: Promise<void>
Errors are automatically reported if a workflow throws without explicitly calling reportError().

step.sendEvent()

Send a custom event to the Agent.
T
required
Event data to send
Returns: Promise<void>

step.updateAgentState()

Replace the Agent’s entire state.
unknown
required
New state
Returns: Promise<void>

step.mergeAgentState()

Merge partial state into the Agent’s state.
Record<string, unknown>
required
Partial state to merge
Returns: Promise<void>

step.resetAgentState()

Reset the Agent’s state to initialState.
Returns: Promise<void>

Protected Methods

reportProgress()

Report typed progress to the Agent.
ProgressType
required
Typed progress data
Example:

broadcastToClients()

Broadcast a message to all connected WebSocket clients via the Agent.
unknown
required
Message to broadcast (will be JSON-stringified)
Example:
broadcastToClients() is non-durable and may repeat on workflow retry. Use step.sendEvent() for durable messages.

waitForApproval()

Wait for approval from the Agent.
AgentWorkflowStep
required
Step object
WaitForApprovalOptions
string
Timeout duration (e.g., “7 days”, “1 hour”)
string
default:"approval"
Event type to wait for
string
default:"wait-for-approval"
Step name for the workflow
Example:
Returns: Promise<T> - Approval metadata Throws: WorkflowRejectedError if rejected

Running Workflows from Agents

runWorkflow()

Start a workflow from an Agent.
See Agent.runWorkflow() for full documentation.

approveWorkflow()

Approve a waiting workflow.

rejectWorkflow()

Reject a waiting workflow.

Agent Callbacks

onWorkflowProgress()

Called when a workflow reports progress.

onWorkflowComplete()

Called when a workflow completes successfully.

onWorkflowError()

Called when a workflow errors.

Full Example