Skip to content

Commit eee0530

Browse files
committed
onward
1 parent 63eaf79 commit eee0530

File tree

15 files changed

+270
-59
lines changed

15 files changed

+270
-59
lines changed

src/content/docs/agents/concepts/communcating-with-users.mdx

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/content/docs/agents/examples/ask-ai-models.mdx

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
2-
title: Self-hosting Agents
2+
title: Browse the web
33
pcx_content_type: concept
44
sidebar:
55
order: 99
66
---
77

8-
import { MetaInfo, Render, Type, WranglerConfig } from "~/components";
8+
import { MetaInfo, Render, Type, TypeScriptExample, WranglerConfig } from "~/components";
99

10-
TODO - send emails
10+
TODO
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
2-
title: Asynchronous Workflows
2+
title: Evals
33
pcx_content_type: concept
44
sidebar:
55
order: 15
66

77
---
88

9-
import { MetaInfo, Render, Type, WranglerConfig } from "~/components";
9+
import { MetaInfo, Render, Type, TypeScriptExample, WranglerConfig } from "~/components";
1010

1111
TODO
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
title: Manage and sync state
3+
pcx_content_type: concept
4+
sidebar:
5+
order: 6
6+
---
7+
8+
import { MetaInfo, Render, Type, TypeScriptExample, WranglerConfig } from "~/components";
9+
10+
Every Agent has built-in state management capabilities, including built-in storage and synchronization between the Agent and frontend applications. State within an Agent is:
11+
12+
* Persisted across Agent restarts: data is permanently persisted within the Agent.
13+
* Automatically serialized/deserialized: you can store any JSON-serializable data.
14+
* Immediately consistent within the Agent: read your own writes.
15+
* Thread-safe for concurrent updates
16+
17+
### State management
18+
19+
Every agent has built-in state management capabilities. You can set and update the agent's state directly using `this.state`:
20+
21+
<TypeScriptExample>
22+
23+
```ts
24+
import { Agent } from "@cloudflare/agents";
25+
26+
export class MyAgent extends Agent {
27+
// Update state in response to events
28+
async incrementCounter() {
29+
this.setState({
30+
...this.state,
31+
counter: this.state.counter + 1,
32+
});
33+
}
34+
35+
// Handle incoming messages
36+
async onMessage(message) {
37+
if (message.type === "update") {
38+
this.setState({
39+
...this.state,
40+
...message.data,
41+
});
42+
}
43+
}
44+
45+
// Handle state updates
46+
onStateUpdate(state, source: "server" | Connection) {
47+
console.log("state updated", state);
48+
}
49+
}
50+
```
51+
52+
</TypeScriptExample>
53+
54+
### Synchronizing state
55+
56+
Clients can connect to an Agent and stay synchronized with its state using the React hooks provided as part of `@cloudflare/agents/react`:
57+
58+
<TypeScriptExample>
59+
60+
```ts
61+
62+
63+
```
64+
65+
</TypeScriptExample>
66+
67+
68+
The state synchronization system:
69+
70+
* Automatically syncs the agent's state to all connected clients
71+
* Handles client disconnections and reconnections gracefully
72+
* Provides immediate local updates
73+
* Supports multiple simultaneous client connections
74+
75+
Common use cases:
76+
77+
* Real-time collaborative features
78+
* Multi-window/tab synchronization
79+
* Live updates across multiple devices
80+
* Maintaining consistent UI state across clients
81+
* When new clients connect, they automatically receive the current state from the agent, ensuring all clients start with the latest data.

src/content/docs/agents/examples/persist-data.mdx

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
2-
title: Asynchronous Workflows
2+
title: Retrieval Augmented Generation
33
pcx_content_type: concept
44
sidebar:
55
order: 7
66

77
---
88

9-
import { MetaInfo, Render, Type, WranglerConfig } from "~/components";
9+
import { MetaInfo, Render, Type, TypeScriptExample, WranglerConfig } from "~/components";
1010

1111
TODO

src/content/docs/agents/examples/run-workflows.mdx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ sidebar:
66

77
---
88

9-
import { MetaInfo, Render, Type, WranglerConfig } from "~/components";
9+
import { MetaInfo, Render, Type, TypeScriptExample, WranglerConfig } from "~/components";
1010

1111
TODO - show how to invoke a Workflow from an Agent
12-
Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,65 @@
11
---
2-
title: Persist data
2+
title: Schedule tasks
33
pcx_content_type: concept
44
sidebar:
55
order: 5
66

77
---
88

9-
import { MetaInfo, Render, Type, WranglerConfig } from "~/components";
9+
import { MetaInfo, Render, Type, TypeScriptExample, WranglerConfig } from "~/components";
1010

11-
TODO
11+
An Agent can schedule tasks to be run in the future by calling `this.schedule(when, callback, data)`, where `when` can be a delay, a `Date`, or a cron string; callback the function name to call, and data is an object of data to pass to the function.
12+
13+
### Scheduling tasks
14+
15+
You can call `this.schedule` within any method on an Agent:
16+
17+
<TypeScriptExample>
18+
19+
```ts
20+
// schedule a task to run in 10 seconds
21+
this.schedule(10, "myTask", { message: "hello" });
22+
23+
// schedule a task to run at a specific date
24+
this.schedule(new Date("2025-01-01"), "myTask", { message: "hello" });
25+
26+
// schedule a task to run every 10 seconds
27+
this.schedule("*/10 * * * *", "myTask", { message: "hello" });
28+
29+
// schedule a task to run every 10 seconds, but only on Mondays
30+
let task = await this.schedule("0 0 * * 1", "myTask", { message: "hello" });
31+
32+
// cancel a scheduled task
33+
this.cancelSchedule(task.id);
34+
```
35+
36+
</TypeScriptExample>
37+
38+
39+
### Managing scheduled tasks
40+
41+
You can get, cancel and filter across scheduled tasks within an Agent using the scheduling API:
42+
43+
<TypeScriptExample>
44+
45+
```ts
46+
// Get a specific schedule by ID
47+
let task = await this.getSchedule(task.id)
48+
49+
// Get all scheduled tasks
50+
let tasks = await this.getScheduledTasks();
51+
52+
// Cancel a task by its ID
53+
await this.cancelSchedule(task.id);
54+
55+
// Filter for specific tasks
56+
// e.g. all tasks starting in the next hour
57+
let tasks = this.getSchedules({ // returns an iterator
58+
timeRange: {
59+
start: new Date(Date.now()),
60+
end: new Date(Date.now() + 60 * 60 * 1000),
61+
},
62+
);
63+
```
64+
65+
</TypeScriptExample>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
2-
title: Send Emails
2+
title: Self-hosting Agents
33
pcx_content_type: concept
44
sidebar:
55
order: 100
66

77
---
88

9-
import { MetaInfo, Render, Type, WranglerConfig } from "~/components";
9+
import { MetaInfo, Render, Type, TypeScriptExample, WranglerConfig } from "~/components";
1010

11-
TODO - send emails
11+
TODO

0 commit comments

Comments
 (0)