You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/agents/api-reference/agents-api.mdx
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,6 +30,8 @@ An Agent can have many (millions of) instances: each instance is a separate micr
30
30
31
31
Instances of an Agent are addressed by a unique identifier: that identifier (ID) can be the user ID, an email address, GitHub username, a flight ticket number, an invoice ID, or any other identifier that helps to uniquely identify the instance and for whom it is acting on behalf of.
32
32
33
+
<Renderfile="unique-agents" />
34
+
33
35
### Agent
34
36
35
37
Writing an Agent requires you to define a class that extends the `Agent` class from the `agents-sdk` package. An Agent encapsulates all of the logic for an Agent, including how clients can connect to it, how it stores state, the methods it exposes, and any error handling.
Copy file name to clipboardExpand all lines: src/content/docs/agents/api-reference/calling-agents.mdx
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,8 @@ Learn how to call your Agents from Workers, including how to create Agents on-th
14
14
15
15
Agents are created on-the-fly and can serve multiple requests concurrently. Each Agent instance is isolated from other instances, can maintain its own state, and has a unique address.
16
16
17
+
<Renderfile="unique-agents" />
18
+
17
19
You can create and run an instance of an Agent directly from a Worker in one of three ways:
18
20
19
21
1. Using the `routeAgentRequest` helper: this will automatically map requests to an individual Agent based on the `/agents/:agent/:name` URL pattern. The value of `:agent` will be the name of your Agent class converted to `kebab-case`, and the value of `:name` will be the name of the Agent instance you want to create or retrieve.
Copy file name to clipboardExpand all lines: src/content/docs/agents/getting-started/quickstart.mdx
+18-20Lines changed: 18 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,6 +39,14 @@ Inside this directory, there are a number of files, but we only need to worry ab
39
39
40
40
Let's take a look at how the Agent in the quick start is defined.
41
41
42
+
:::note[Escape hatch]
43
+
44
+
You can escape from the rest of this quickstart guide right here: run `npm run start` to run the code locally, open `src/index.ts` to explore the code, and run `npx wrangler@latest deploy` to deploy it your Cloudflare account.
45
+
46
+
The rest of this quickstart briefly steps through the code and sending requests to the Agent: if this is your first time using the Agents SDK and/or Cloudflare Workers, you'll likely want to keep reading.
47
+
48
+
:::
49
+
42
50
### Understand the Agent class
43
51
44
52
Open the `src/index.ts` file in your editor:
@@ -71,33 +79,23 @@ Your Agent is now running locally on your machine, and ready to communicate with
71
79
72
80
### Communicate with your Agent
73
81
74
-
Let's communicate with your Agent and have it run a simple task.
82
+
Let's communicate with your Agent and have it book us a flight (well, pretend to).
75
83
76
-
The example Agent in this quick start project exposes both HTTP and WebSocket endpoints, and so we'll use [`wscat`](https://github.com/websockets/wscat) as a command-line WebSocket client to communicate with our Agent's chat endpoint.
77
-
78
-
Install `wscat`:
79
-
80
-
```sh
81
-
npm install -g wscat
82
-
```
83
-
84
-
Run `wscat` and connect to an instance of your Agent (running locally). Remember that each Agent can have many _instances_, each able to interact with users, tools and other APIs independently, as well as store state specific to that instance.
85
-
86
-
```sh
87
-
# The code in this project will automatically create a new Agent on-the-fly when
88
-
# you provide a name: e.g. /agents/my-agent/foo or /agents/my-agent/user-1238139
89
-
# This allows you to create as many Agents as you want, each with their own
In the `agents-quick-start` we use the `routeAgentRequest` helper to automatically handle routing to existing and creating new Agent instances on-the-fly.
99
95
100
-
If you open up `src/index.ts`,
96
+
If you open up `src/index.ts`, you'll see that `routeAgentRequest` handles requests to `/agents/:agent/:name` - in our case, that's `/agents/flight-agent/:name`, where name is a unique identifier for that Agent instance.
97
+
98
+
<Renderfile="unique-agents" />
101
99
102
100
<TypeScriptExamplefilename="src/index.ts">
103
101
@@ -117,7 +115,7 @@ export default {
117
115
118
116
</TypeScriptExample>
119
117
120
-
You can learn about more ways to call into your Agents, as well as how to add authentication in front of your Agents, by reviewing [documentation on Calling Agents](/agents/api-reference/calling-agents/).
118
+
You can learn about more ways to call into your Agents, as well as how to add authentication in front of your Agents, by reviewing the [documentation on calling Agents](/agents/api-reference/calling-agents/).
An instance of an Agent is globally unique: given the same name (or ID), you will always get the same instance of an agent. This means that you don't have to synchronize state across requests: if an Agent instance represents a specific user, team, channel or other entity, you can use the Agent instance to store state for that entity.
9
+
10
+
If the client disconnects, you can always route the client back to the exact same Agent and pick up where they left off.
0 commit comments