Skip to content

Commit 29424f3

Browse files
committed
update
1 parent ed0c7de commit 29424f3

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,72 @@ export class MyAgent extends Agent<Env> {
5959
```
6060
</TypeScriptExample>
6161

62+
#### Calling methods directly
63+
64+
When using `getAgentByName`, you can pass both requests (including WebSocket) connections and call methods defined directly on the Agent itself using the native [JavaScript RPC](/workers/runtime-apis/rpc/) (JSRPC) API.
65+
66+
For example, once you have a handle (or "stub") to an unique instance of your Agent, you can call methods on it:
67+
68+
<TypeScriptExample>
69+
70+
```ts
71+
import { Agent, AgentNamespace, getAgentByName } from 'agents';
72+
73+
interface Env {
74+
// Define your Agent on the environment here
75+
// Passing your Agent class as a TypeScript type parameter allows you to call
76+
// methods defined on your Agent.
77+
MyAgent: AgentNamespace<MyAgent>;
78+
}
79+
80+
interface UserHistory {
81+
history: string[];
82+
lastUpdated: Date;
83+
}
84+
85+
export default {
86+
async fetch(request, env, ctx): Promise<Response> {
87+
let namedAgent = getAgentByName<Env, MyAgent>(env.MyAgent, 'my-unique-agent-id');
88+
// Call methods directly on the Agent, and pass native JavaScript objects
89+
let chatResponse = namedAgent.chat('Hello!');
90+
// No need to serialize/deserialize it from a HTTP request or WebSocket
91+
// message and back again
92+
let agentState = getState() // agentState is of type UserHistory
93+
return namedResp
94+
},
95+
} satisfies ExportedHandler<Env>;
96+
97+
export class MyAgent extends Agent<Env, UserHistory> {
98+
// Your Agent implementation goes here
99+
async chat(prompt: string) {
100+
// call your favorite LLM
101+
return "result"
102+
}
103+
104+
async getState() {
105+
// Return the Agent's state directly
106+
return this.state;
107+
}
108+
109+
// Other methods as you see fit!
110+
}
111+
```
112+
</TypeScriptExample>
113+
114+
When using TypeScript, ensure you pass your Agent class as a TypeScript type parameter to the AgentNamespace type so that types are correctly inferred:
115+
116+
```ts
117+
interface Env {
118+
// Passing your Agent class as a TypeScript type parameter allows you to call
119+
// methods defined on your Agent.
120+
MyAgent: AgentNamespace<CodeReviewAgent>;
121+
}
122+
123+
export class CodeReviewAgent extends Agent<Env, AgentState> {
124+
// Agent methods here
125+
}
126+
```
127+
62128
### Naming your Agents
63129

64130
When creating names for your Agents, think about what the Agent represents. A unique user? A team or company? A room or channel for collaboration?
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
title: Add an Agent to Workers
3+
pcx_content_type: get-started
4+
sidebar:
5+
order: 3
6+
---
7+
8+
import { Render, PackageManagers, WranglerConfig } from "~/components"
9+
10+
TODO
11+
12+
### Add an Agent to an existing project
13+
14+
TODO
15+
16+
### Install the Agents SDK
17+
18+
TODO
19+
20+
### Define your Agent class
21+
22+
TODO
23+
24+
### Attach the Agent to your Worker
25+
26+
TODO
27+
28+
### Deploy it
29+
30+
TODO
31+
32+
### Next steps
33+
34+
* Review the [API documentation](/agents/api-reference/agents-api/) for the Agents class to learn how to define
35+
* [Build a chat Agent](/agents/getting-started/build-a-chat-agent/) using the Agents SDK and deploy it to Workers.
36+
* Learn more [using WebSockets](/agents/api-reference/websockets/) to build interactive Agents and stream data back from your Agent.
37+
* [Orchestrate asynchronous workflows](/agents/api-reference/run-workflows) from your Agent by combining the Agents SDK and [Workflows](/workflows).

0 commit comments

Comments
 (0)