Skip to content

Commit cf24dd8

Browse files
Add task management system documentation
Adds comprehensive documentation for the new task management system from PR #683: - New Tasks API reference documenting @task() decorator, TaskContext methods, and task lifecycle management - New Durable Tasks guide covering long-running operations backed by Cloudflare Workflows - Updated sidebar ordering to logically place tasks documentation after state management - Adjusted navigation order for browse-the-web, HTTP/SSE, RAG, and WebSockets pages These changes document the new @task() and @task({ durable: true }) decorators that enable tracked background operations with real-time progress updates, cancellation support, and WebSocket broadcasting to connected clients. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent e48bbb5 commit cf24dd8

File tree

6 files changed

+376
-4
lines changed

6 files changed

+376
-4
lines changed

src/content/docs/agents/api-reference/browse-the-web.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Browse the web
33
pcx_content_type: concept
44
sidebar:
5-
order: 7
5+
order: 9
66
---
77

88
import {
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
---
2+
title: Durable Tasks
3+
pcx_content_type: concept
4+
sidebar:
5+
order: 8
6+
7+
---
8+
9+
import { MetaInfo, Render, Type, TypeScriptExample, WranglerConfig } from "~/components";
10+
11+
Durable tasks are long-running operations backed by [Cloudflare Workflows](/workflows/). They survive agent restarts and can run for hours or days.
12+
13+
## Setup
14+
15+
1. Export `DurableTaskWorkflow` from your worker:
16+
17+
<TypeScriptExample>
18+
19+
```ts
20+
// src/index.ts
21+
import { Agent, routeAgentRequest, DurableTaskWorkflow } from "agents";
22+
23+
export { DurableTaskWorkflow };
24+
25+
export class MyAgent extends Agent<Env> {
26+
// Your agent code
27+
}
28+
```
29+
30+
</TypeScriptExample>
31+
32+
2. Add the workflow binding to your configuration:
33+
34+
<WranglerConfig>
35+
36+
```jsonc
37+
{
38+
"workflows": [
39+
{
40+
"name": "durable-tasks",
41+
"binding": "DURABLE_TASKS_WORKFLOW",
42+
"class_name": "DurableTaskWorkflow"
43+
}
44+
]
45+
}
46+
```
47+
48+
</WranglerConfig>
49+
50+
## Usage
51+
52+
Mark a task as durable with `durable: true`:
53+
54+
<TypeScriptExample>
55+
56+
```ts
57+
import { Agent, task, type TaskContext } from "agents";
58+
59+
export class MyAgent extends Agent<Env> {
60+
@task({ durable: true })
61+
async longAnalysis(input: { repoUrl: string }, ctx: TaskContext) {
62+
// Each step is checkpointed - survives restarts
63+
const files = await ctx.step("fetch", () => fetchRepoFiles(input.repoUrl));
64+
65+
// Durable sleep - can wait for hours or days
66+
await ctx.sleep("rate-limit", "1h");
67+
68+
const analysis = await ctx.step("analyze", () => analyzeFiles(files));
69+
70+
return analysis;
71+
}
72+
}
73+
```
74+
75+
</TypeScriptExample>
76+
77+
## Durable Context Methods
78+
79+
Durable tasks have additional `TaskContext` methods:
80+
81+
### ctx.step(name, fn)
82+
83+
Execute a checkpointed step. If the workflow restarts, completed steps are skipped and their results are replayed.
84+
85+
<TypeScriptExample>
86+
87+
```ts
88+
const data = await ctx.step("fetch-data", async () => {
89+
return await fetch(url).then((r) => r.json());
90+
});
91+
```
92+
93+
</TypeScriptExample>
94+
95+
### ctx.sleep(name, duration)
96+
97+
Pause execution for a duration. The workflow hibernates and resumes automatically.
98+
99+
<TypeScriptExample>
100+
101+
```ts
102+
await ctx.sleep("wait", "30m");
103+
await ctx.sleep("daily-check", "24h");
104+
```
105+
106+
</TypeScriptExample>
107+
108+
Accepts: `"30s"`, `"5m"`, `"1h"`, `"7d"`, or `"30 seconds"`, `"5 minutes"`.
109+
110+
### ctx.waitForEvent(name, options)
111+
112+
Wait for an external event (human approval, webhook).
113+
114+
<TypeScriptExample>
115+
116+
```ts
117+
const approval = await ctx.waitForEvent("approval", {
118+
type: "user-approved",
119+
timeout: "24h"
120+
});
121+
```
122+
123+
</TypeScriptExample>
124+
125+
:::note
126+
127+
`waitForEvent` is only available in durable tasks.
128+
129+
:::
130+
131+
## Retry Configuration
132+
133+
Configure automatic retries for durable tasks:
134+
135+
<TypeScriptExample>
136+
137+
```ts
138+
@task({
139+
durable: true,
140+
retry: {
141+
limit: 3,
142+
delay: "10s",
143+
backoff: "exponential"
144+
}
145+
})
146+
async unreliableTask(input: Input, ctx: TaskContext) {
147+
// Automatically retries on failure
148+
}
149+
```
150+
151+
</TypeScriptExample>
152+
153+
## When to Use Durable Tasks
154+
155+
Use `@task()` (simple) for:
156+
157+
- Operations under 30 seconds
158+
- Tasks that can restart from scratch
159+
160+
Use `@task({ durable: true })` for:
161+
162+
- Long-running operations (minutes to days)
163+
- Multi-step workflows with checkpoints
164+
- Operations that must survive restarts
165+
- Tasks requiring durable sleep or external events
166+
167+
## Custom Workflows
168+
169+
For advanced control, extend `AgentWorkflow` instead:
170+
171+
<TypeScriptExample>
172+
173+
```ts
174+
import { AgentWorkflow, type WorkflowTaskContext } from "agents";
175+
176+
export class CustomWorkflow extends AgentWorkflow<Env, { input: string }> {
177+
async run(ctx: WorkflowTaskContext<{ input: string }>) {
178+
ctx.emit("started");
179+
180+
const result = await ctx.step("process", async () => {
181+
return processInput(ctx.params.input);
182+
});
183+
184+
ctx.setProgress(100);
185+
return result;
186+
}
187+
}
188+
```
189+
190+
</TypeScriptExample>
191+
192+
Then dispatch it with `this.workflow()`:
193+
194+
<TypeScriptExample>
195+
196+
```ts
197+
export class MyAgent extends Agent<Env> {
198+
@callable()
199+
async startCustom(input: { data: string }) {
200+
return this.workflow("CUSTOM_WORKFLOW", input);
201+
}
202+
}
203+
```
204+
205+
</TypeScriptExample>
206+
207+
## Related resources
208+
209+
- [Workflows documentation](/workflows/)
210+
- [Tasks API reference](/agents/api-reference/tasks/)

src/content/docs/agents/api-reference/http-sse.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: HTTP and Server-Sent Events
33
pcx_content_type: concept
44
sidebar:
5-
order: 8
5+
order: 10
66

77
---
88

src/content/docs/agents/api-reference/rag.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Retrieval Augmented Generation
33
pcx_content_type: concept
44
sidebar:
5-
order: 9
5+
order: 11
66

77
---
88

0 commit comments

Comments
 (0)