Skip to content

Commit 3274924

Browse files
committed
fixed examples
1 parent 925651e commit 3274924

File tree

1 file changed

+10
-76
lines changed

1 file changed

+10
-76
lines changed

src/content/docs/agents/index.mdx

Lines changed: 10 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ Built on Cloudflare's infrastructure, agents are stateful classes that run on Du
2121
2222
## Quick start
2323

24-
```sh
24+
```bash
2525
npm create cloudflare@latest my-agent -- --template=cloudflare/agents-starter
2626
cd my-agent && npx wrangler dev
2727
```
2828

2929
Or add to an existing project:
3030

31-
```sh
31+
```bash
3232
npm install agents
3333
```
3434

@@ -38,7 +38,7 @@ Create a simple joke agent that remembers previous interactions:
3838

3939
<TypeScriptExample>
4040

41-
```ts
41+
```typescript
4242
import { Agent } from "agents";
4343

4444
export class JokeAgent extends Agent {
@@ -49,14 +49,14 @@ export class JokeAgent extends Agent {
4949

5050
async onMessage(connection, message) {
5151
const response = await this.ai.run(`
52-
You are a friendly joke-telling agent.
53-
Previous jokes shared: ${this.state.jokesShared}
54-
User message: "${message}"
52+
You are a friendly joke-telling agent.
53+
Previous jokes shared: ${this.state.jokesShared}
54+
User message: "${message}"
5555
56-
If they ask for a joke, tell them one.
57-
If they mention a joke type they like, remember it.
58-
Be conversational and fun!
59-
`);
56+
If they ask for a joke, tell them one.
57+
If they mention a joke type they like, remember it.
58+
Be conversational and fun!
59+
`);
6060

6161
connection.send(response);
6262

@@ -79,72 +79,6 @@ export default {
7979

8080
This agent remembers how many jokes it has shared and adapts its responses based on conversation history.
8181

82-
## Building your first agent
83-
84-
Follow these steps to create an agent that can autonomously handle tasks:
85-
86-
### 1. Define agent identity and tools
87-
88-
```ts
89-
export class CustomerServiceAgent extends Agent {
90-
initialState = {
91-
name: "CustomerServiceAgent",
92-
role: "Customer support specialist",
93-
conversationHistory: [],
94-
availableTools: ["search_docs", "create_ticket", "escalate"],
95-
};
96-
}
97-
```
98-
99-
### 2. Add decision-making logic
100-
101-
```ts
102-
async onMessage(connection, message) {
103-
// Agent analyzes request and decides on actions
104-
const analysis = await this.ai.run(`
105-
As ${this.state.name}, analyze this customer request: "${message}"
106-
107-
Available tools: ${this.state.availableTools.join(', ')}
108-
109-
Decide what actions to take and respond appropriately.
110-
`);
111-
112-
// Execute chosen actions
113-
const response = await this.executeActions(analysis, message);
114-
connection.send(response);
115-
}
116-
```
117-
118-
### 3. Implement persistent memory
119-
120-
```ts
121-
async executeActions(analysis, originalMessage) {
122-
// Update conversation history
123-
await this.setState({
124-
conversationHistory: [
125-
...this.state.conversationHistory,
126-
{ message: originalMessage, timestamp: Date.now() }
127-
]
128-
});
129-
130-
// Agent remembers context for future interactions
131-
return analysis;
132-
}
133-
```
134-
135-
### 4. Deploy and test
136-
137-
```bash
138-
npm run dev
139-
# Your agent is now running with autonomous decision-making
140-
```
141-
142-
> **Testing locally**: Use `wrangler dev` or `vitest` to test against Durable Objects and Agents locally.
143-
144-
Your agent can now autonomously decide on sequences of actions, maintain memory across conversations, and consistently apply its role and tools.
145-
146-
> Want a complete example? Follow our [Build a Chat Agent](/agents/getting-started/build-a-chat-agent) tutorial to create a fully functional conversational agent with memory and context.
147-
14882
## Core capabilities
14983

15084
- Persistent state with automatic synchronization

0 commit comments

Comments
 (0)