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
// Access the Request on ctx.request to inspect headers, cookies and the URL
79
+
80
+
// Accept the connection
78
81
connection.accept();
79
82
}
80
83
@@ -110,7 +113,7 @@ class MyAgent extends Agent<Env, State> {
110
113
}
111
114
```
112
115
113
-
### Connection Handling
116
+
### WebSocket connection Handling
114
117
115
118
#### Connection
116
119
@@ -186,6 +189,14 @@ class Agent<Env, State = unknown> {
186
189
}
187
190
```
188
191
192
+
```ts
193
+
// Example usage:
194
+
this.setState({
195
+
...this.state,
196
+
counter: this.state.counter+1,
197
+
});
198
+
```
199
+
189
200
### Scheduling
190
201
191
202
#### Scheduling Tasks
@@ -225,7 +236,7 @@ class Agent<Env, State = unknown> {
225
236
}
226
237
```
227
238
228
-
#### Schedule Object
239
+
#### Schedule object
229
240
230
241
Represents a scheduled task.
231
242
@@ -264,11 +275,16 @@ type Schedule<T = any> = {
264
275
);
265
276
```
266
277
278
+
```ts
279
+
// Schedule a task that calls a method after a delay
280
+
let task =awaitthis.schedule(300, "methodToCall", { message: "data-to-send-to-method" });
281
+
```
282
+
267
283
### SQL Database Access
268
284
269
285
#### SQL Query API
270
286
271
-
Execute SQL queries against the Agent's built-in SQLite database.
287
+
Execute SQL queries against the Agent's built-in SQLite database using the `this.sql` method within any method on your `Agent` class.
272
288
273
289
```ts
274
290
// SQL query API for the Agent's embedded database
@@ -282,6 +298,14 @@ class Agent<Env, State = unknown> {
282
298
}
283
299
```
284
300
301
+
```ts
302
+
// Example usage
303
+
let user =this.sql<YourUserType>`
304
+
SELECT * FROM users WHERE id = ${userId}`;
305
+
```
306
+
307
+
Visit the documentation on [storing and syncing state](/agents/api-reference/store-and-sync-state/) to learn more about the `this.setState` and `this.sql` APIs.
308
+
285
309
### Client API
286
310
287
311
#### AgentClient
@@ -337,6 +361,8 @@ function agentFetch(
337
361
338
362
### React Integration
339
363
364
+
The `agents-sdk` provides a React API for simplifying connection and routing to Agents from front-end frameworks, including React Router (Remix), Next.js, and Astro.
365
+
340
366
#### useAgent
341
367
342
368
React hook for connecting to an Agent.
@@ -371,9 +397,13 @@ function useAgent<State = unknown>(
371
397
372
398
### Chat Agent
373
399
400
+
The `agents-sdk` exposes an `AIChatAgent` class that extends the `Agent` class and exposes an `onChatMessage` method that simplifies building interactive chat agents.
401
+
402
+
You can combine this with the `useAgentChat` React hook from the `agents-sdk/ai-react` package to manage chat state and messages between a user and your Agent(s).
403
+
374
404
#### AIChatAgent
375
405
376
-
Extension of Agent with built-in chat capabilities.
406
+
Extension of the `Agent` class with built-in chat capabilities.
// via this.setState or the useAgent hook from the agents-sdk/react package.
484
-
async onStateUpdate(state:any) {
485
-
// 'state' will be typed if you supply a type parameter to the Agent class.
486
-
}
487
-
}
488
-
489
-
exportdefaultMyAgent;
490
-
```
491
-
492
468
:::note
493
469
494
470
To learn more about how to manage state within an Agent, refer to the documentation on [managing and syncing state](/agents/api-reference/store-and-sync-state/).
0 commit comments