Skip to content

Commit 9e0f7b0

Browse files
Add DurableAgent and WorkflowChatTransport doc pages (#44)
Co-authored-by: Adrian <[email protected]>
1 parent f2b1619 commit 9e0f7b0

File tree

7 files changed

+594
-7
lines changed

7 files changed

+594
-7
lines changed

.changeset/mean-mangos-hope.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as CalloutComponents from '@/components/ui/callout';
2+
3+
interface ExperimentalPackageCalloutProps {
4+
packageName: string;
5+
}
6+
7+
export function ExperimentalPackageCallout({
8+
packageName,
9+
}: ExperimentalPackageCalloutProps) {
10+
return (
11+
<CalloutComponents.Callout type="warn">
12+
The <code>{packageName}</code> package is currently in active development
13+
and should be considered experimental.
14+
</CalloutComponents.Callout>
15+
);
16+
}
Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
---
2+
title: DurableAgent
3+
---
4+
5+
import { ExperimentalPackageCallout } from "@/components/experimental-package-callout"
6+
import { generateDefinition } from "@/lib/tsdoc"
7+
8+
# `DurableAgent`
9+
10+
<ExperimentalPackageCallout packageName="@workflow/ai" />
11+
12+
The `DurableAgent` class enables you to create AI-powered agents that can maintain state across workflow steps, call tools, and gracefully handle interruptions and resumptions.
13+
14+
Tool calls can be implemented as workflow steps for automatic retries, or as regular workflow-level logic utilizing core library features such as [`sleep()`](/docs/api-reference/workflow/sleep) and [Hooks](/docs/foundations/hooks).
15+
16+
```typescript lineNumbers
17+
import { DurableAgent } from '@workflow/ai/agent';
18+
import { z } from 'zod';
19+
20+
async function getWeather({ city }: { city: string }) {
21+
"use step";
22+
23+
return `Weather in ${city} is sunny`;
24+
}
25+
26+
async function myAgent() {
27+
"use workflow";
28+
29+
const agent = new DurableAgent({
30+
model: 'anthropic/claude-haiku-4.5',
31+
system: 'You are a helpful weather assistant.',
32+
tools: {
33+
getWeather: {
34+
description: 'Get weather for a city',
35+
inputSchema: z.object({ city: z.string() }),
36+
execute: getWeather,
37+
},
38+
},
39+
});
40+
41+
await agent.stream({
42+
messages: [{ role: 'user', content: 'How is the weather in San Francisco?' }],
43+
});
44+
}
45+
```
46+
47+
## API Signature
48+
49+
### Class
50+
51+
<TSDoc
52+
definition={generateDefinition({
53+
code: `
54+
import { DurableAgent } from "@workflow/ai/agent";
55+
export default DurableAgent;`
56+
})}
57+
/>
58+
59+
### DurableAgentOptions
60+
61+
<TSDoc
62+
definition={generateDefinition({
63+
code: `
64+
import type { DurableAgentOptions } from "@workflow/ai/agent";
65+
export default DurableAgentOptions;`
66+
})}
67+
/>
68+
69+
### DurableAgentStreamOptions
70+
71+
<TSDoc
72+
definition={generateDefinition({
73+
code: `
74+
import type { DurableAgentStreamOptions } from "@workflow/ai/agent";
75+
export default DurableAgentStreamOptions;`
76+
})}
77+
/>
78+
79+
## Key Features
80+
81+
- **Durable Execution**: Agents can be interrupted and resumed without losing state
82+
- **Flexible Tool Implementation**: Tools can be implemented as workflow steps for automatic retries, or as regular workflow-level logic
83+
- **Stream Processing**: Handles streaming responses and tool calls in a structured way
84+
- **Workflow Native**: Fully integrated with Workflow DevKit for production-grade reliability
85+
86+
## Good to Know
87+
88+
- Tools can be implemented as workflow steps (using `"use step"` for automatic retries), or as regular workflow-level logic
89+
- Tools can use core library features like `sleep()` and Hooks within their `execute` functions
90+
- The agent processes tool calls iteratively until completion
91+
- When no custom `writable` stream is provided, the agent uses the workflow's default writable stream
92+
93+
## Examples
94+
95+
### Basic Agent with Tools
96+
97+
```typescript
98+
import { DurableAgent } from '@workflow/ai/agent';
99+
import { z } from 'zod';
100+
101+
async function getWeather({ location }: { location: string }) {
102+
"use step";
103+
// Fetch weather data
104+
const response = await fetch(`https://api.weather.com?location=${location}`);
105+
return response.json();
106+
}
107+
108+
async function weatherAgentWorkflow(userQuery: string) {
109+
'use workflow';
110+
111+
const agent = new DurableAgent({
112+
model: 'anthropic/claude-haiku-4.5',
113+
tools: {
114+
getWeather: {
115+
description: 'Get current weather for a location',
116+
inputSchema: z.object({ location: z.string() }),
117+
execute: getWeather,
118+
},
119+
},
120+
system: 'You are a helpful weather assistant. Always provide accurate weather information.',
121+
});
122+
123+
await agent.stream({
124+
messages: [
125+
{
126+
role: 'user',
127+
content: userQuery,
128+
},
129+
],
130+
});
131+
}
132+
```
133+
134+
### Multiple Tools
135+
136+
```typescript
137+
import { DurableAgent } from '@workflow/ai/agent';
138+
import { z } from 'zod';
139+
140+
async function getWeather({ location }: { location: string }) {
141+
"use step";
142+
return `Weather in ${location}: Sunny, 72°F`;
143+
}
144+
145+
async function searchEvents({ location, category }: { location: string; category: string }) {
146+
"use step";
147+
return `Found 5 ${category} events in ${location}`;
148+
}
149+
150+
async function multiToolAgentWorkflow(userQuery: string) {
151+
'use workflow';
152+
153+
const agent = new DurableAgent({
154+
model: 'anthropic/claude-haiku-4.5',
155+
tools: {
156+
getWeather: {
157+
description: 'Get weather for a location',
158+
inputSchema: z.object({ location: z.string() }),
159+
execute: getWeather,
160+
},
161+
searchEvents: {
162+
description: 'Search for upcoming events in a location',
163+
inputSchema: z.object({ location: z.string(), category: z.string() }),
164+
execute: searchEvents,
165+
},
166+
},
167+
});
168+
169+
await agent.stream({
170+
messages: [
171+
{
172+
role: 'user',
173+
content: userQuery,
174+
},
175+
],
176+
});
177+
}
178+
```
179+
180+
### Tools with Workflow Library Features
181+
182+
```typescript
183+
import { DurableAgent } from '@workflow/ai/agent';
184+
import { sleep, defineHook } from 'workflow';
185+
import { z } from 'zod';
186+
187+
// Define a reusable hook type
188+
const approvalHook = defineHook<{ approved: boolean; reason: string }>();
189+
190+
async function scheduleTask({ delaySeconds }: { delaySeconds: number }) {
191+
"use step";
192+
// Use core library features like sleep() in tools
193+
await sleep(`${delaySeconds}s`);
194+
return `Slept for ${delaySeconds} seconds`;
195+
}
196+
197+
async function requestApproval({ message }: { message: string }) {
198+
// Note: No "use step" for this tool call
199+
200+
// Utilize a Hook for Human-in-the-loop approval
201+
const hook = approvalHook.create({
202+
metadata: { message }
203+
});
204+
205+
console.log(`Approval needed - token: ${hook.token}`);
206+
207+
// Wait for the approval payload
208+
const approval = await hook;
209+
210+
if (approval.approved) {
211+
return `Request approved: ${approval.reason}`;
212+
} else {
213+
throw new Error(`Request denied: ${approval.reason}`);
214+
}
215+
}
216+
217+
async function agentWithLibraryFeaturesWorkflow(userRequest: string) {
218+
'use workflow';
219+
220+
const agent = new DurableAgent({
221+
model: 'anthropic/claude-haiku-4.5',
222+
tools: {
223+
scheduleTask: {
224+
description: 'Pause the workflow for the specified number of seconds',
225+
inputSchema: z.object({
226+
delaySeconds: z.number(),
227+
}),
228+
execute: scheduleTask,
229+
},
230+
requestApproval: {
231+
description: 'Request approval for an action',
232+
inputSchema: z.object({ message: z.string() }),
233+
execute: requestApproval,
234+
},
235+
},
236+
});
237+
238+
await agent.stream({
239+
messages: [{ role: 'user', content: userRequest }],
240+
});
241+
}
242+
```
243+
244+
## See Also
245+
246+
- [WorkflowChatTransport](/docs/api-reference/workflow-ai/workflow-chat-transport) - Transport layer for AI SDK streams
247+
- [Workflows and Steps](/docs/foundations/workflows-and-steps) - Understanding workflow fundamentals
248+
- [AI SDK Documentation](https://ai-sdk.dev/docs) - AI SDK documentation reference

docs/content/docs/api-reference/workflow-ai/index.mdx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
---
22
title: "@workflow/ai"
3+
icon: FlaskConical
34
---
45

6+
import { ExperimentalPackageCallout } from "@/components/experimental-package-callout"
7+
58
# `@workflow/ai`
69

7-
<Callout type="warn">
8-
The `@workflow/ai` package is currently in active development.
9-
</Callout>
10+
<ExperimentalPackageCallout packageName="@workflow/ai" />
1011

1112
Helpers for integrating AI SDK for building AI-powered workflows.
1213

1314
---
1415

15-
## Functions
16+
## Classes
1617

1718
<Cards>
19+
<Card title="DurableAgent" href="/docs/api-reference/workflow-ai/durable-agent">
20+
A class for building durable AI agents that maintain state across workflow steps and handle tool execution with automatic retries.
21+
</Card>
1822
<Card title="WorkflowChatTransport" href="/docs/api-reference/workflow-ai/workflow-chat-transport">
1923
A drop-in transport for the AI SDK for automatic reconnection in interrupted streams.
2024
</Card>

0 commit comments

Comments
 (0)