Skip to content

Commit b3168fe

Browse files
committed
improve API ref
1 parent b5c4000 commit b3168fe

File tree

1 file changed

+36
-60
lines changed
  • src/content/docs/agents/api-reference

1 file changed

+36
-60
lines changed

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

Lines changed: 36 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ class MyAgent extends Agent<Env, State> {
7474
// Called when a WebSocket connection is established
7575
// Access the original request via ctx.request for auth etc.
7676
async onConnect(connection: Connection, ctx: ConnectionContext) {
77-
// Authenticate and accept the connection
77+
// Authenticate the connection
78+
// Access the Request on ctx.request to inspect headers, cookies and the URL
79+
80+
// Accept the connection
7881
connection.accept();
7982
}
8083

@@ -110,7 +113,7 @@ class MyAgent extends Agent<Env, State> {
110113
}
111114
```
112115

113-
### Connection Handling
116+
### WebSocket connection Handling
114117

115118
#### Connection
116119

@@ -186,6 +189,14 @@ class Agent<Env, State = unknown> {
186189
}
187190
```
188191

192+
```ts
193+
// Example usage:
194+
this.setState({
195+
...this.state,
196+
counter: this.state.counter + 1,
197+
});
198+
```
199+
189200
### Scheduling
190201

191202
#### Scheduling Tasks
@@ -225,7 +236,7 @@ class Agent<Env, State = unknown> {
225236
}
226237
```
227238

228-
#### Schedule Object
239+
#### Schedule object
229240

230241
Represents a scheduled task.
231242

@@ -264,11 +275,16 @@ type Schedule<T = any> = {
264275
);
265276
```
266277

278+
```ts
279+
// Schedule a task that calls a method after a delay
280+
let task = await this.schedule(300, "methodToCall", { message: "data-to-send-to-method" });
281+
```
282+
267283
### SQL Database Access
268284

269285
#### SQL Query API
270286

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.
272288

273289
```ts
274290
// SQL query API for the Agent's embedded database
@@ -282,6 +298,14 @@ class Agent<Env, State = unknown> {
282298
}
283299
```
284300

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+
285309
### Client API
286310

287311
#### AgentClient
@@ -337,6 +361,8 @@ function agentFetch(
337361

338362
### React Integration
339363

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+
340366
#### useAgent
341367

342368
React hook for connecting to an Agent.
@@ -371,9 +397,13 @@ function useAgent<State = unknown>(
371397

372398
### Chat Agent
373399

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+
374404
#### AIChatAgent
375405

376-
Extension of Agent with built-in chat capabilities.
406+
Extension of the `Agent` class with built-in chat capabilities.
377407

378408
```ts
379409
import { AIChatAgent } from "agents-sdk/ai-chat-agent";
@@ -390,7 +420,7 @@ class AIChatAgent<Env = unknown> extends Agent<Env> {
390420
onFinish: StreamTextOnFinishCallback<any>
391421
): Promise<Response | undefined>;
392422

393-
// Save messages on the server side and trigger AI response
423+
// Persist messages within the Agent's local storage.
394424
async saveMessages(messages: Message[]): Promise<void>;
395425
}
396426
```
@@ -435,60 +465,6 @@ function useAgentChat(options: UseAgentChatOptions): {
435465
};
436466
```
437467

438-
```ts
439-
import { Agent } from "agents-sdk";
440-
441-
442-
class MyAgent extends Agent<Env> {
443-
// Called when an Agent is started (or woken up)
444-
async onStart() {
445-
// Can access this.env and this.state
446-
console.log('Agent started');
447-
}
448-
449-
// Called when a HTTP request is received
450-
// Can be connected to routeAgentRequest to automatically route
451-
// requests to an individual Agent.
452-
async onRequest(request: Request) {
453-
console.log("Received request!");
454-
}
455-
456-
// Called when a WebSocket connection is established
457-
async onConnect(connection: Connection, ctx: ConnectionContext) {
458-
console.log("Connected!");
459-
// Check the request at ctx.request
460-
// Authenticate the client
461-
// Give them the OK.
462-
connection.accept();
463-
}
464-
465-
// Called for each message received on the WebSocket connection
466-
async onMessage(connection: Connection, message: WSMessage) {
467-
console.log(`message from client ID: ${connection.id}`)
468-
// Send messages back to the client
469-
connection.send("Hello!");
470-
}
471-
472-
// WebSocket error and disconnection (close) handling.
473-
async onError(connection: Connection, error: unknown): Promise<void> {
474-
console.error(`WS error: ${error}`);
475-
}
476-
477-
async onClose(connection: Connection, code: number, reason: string, wasClean: boolean): Promise<void> {
478-
console.log(`WS closed: ${code} - ${reason} - wasClean: ${wasClean}`);
479-
connection.close();
480-
}
481-
482-
// Called when the Agent's state is updated
483-
// 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-
export default MyAgent;
490-
```
491-
492468
:::note
493469

494470
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

Comments
 (0)