This commit is contained in:
agents-git-bot[bot] 2026-01-11 03:23:20 +03:00 committed by GitHub
commit e857dbe70d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -389,7 +389,7 @@ export default {
### Context Management
`agents` wraps all your methods with an `AsyncLocalStorage` to maintain context throughout the request lifecycle. This allows you to access the current agent, connection, request, or email (depending of what event is being handled) from anywhere in your code:
`agents` wraps all your methods with an `AsyncLocalStorage` to maintain context throughout the request lifecycle. This allows you to access the current agent, connection, request, or email (depending of what event is being handled) from anywhere in your code, including nested async functions:
```ts
import { getCurrentAgent } from "agents";
@ -407,6 +407,45 @@ function someUtilityFunction() {
}
```
The context is properly propagated through async operations, making it available in tool execution functions and other nested async calls:
```ts
import { AIChatAgent } from "agents/ai-chat-agent";
import { getCurrentAgent } from "agents";
import { tool } from "ai";
class MyAgent extends AIChatAgent {
async onChatMessage() {
const tools = {
checkStatus: tool({
description: "Check the status of something",
parameters: z.object({}),
execute: async () => {
// getCurrentAgent() works inside tool execution
const { agent, connection } = getCurrentAgent();
if (connection) {
// You can access the connection that triggered this chat
console.log("Tool called by connection:", connection.id);
}
return "Status: OK";
}
})
};
// Use tools in your chat response
const result = await streamText({
model: this.env.AI.openai("gpt-4o"),
messages: this.messages,
tools
});
return result.toUIMessageStreamResponse();
}
}
```
### `this.onError`
`Agent` extends `Server`'s `onError` so it can be used to handle errors that are not necessarily WebSocket errors. It is called with a `Connection` or `unknown` error.