All custom methods automatically have full agent context! The framework automatically detects and wraps your custom methods during initialization, ensuring getCurrentAgent() works seamlessly everywhere.
import { AIChatAgent } from "@cloudflare/ai-chat";
import { getCurrentAgent } from "agents";
export class MyAgent extends AIChatAgent {
async customMethod() {
const { agent } = getCurrentAgent<MyAgent>();
// ✅ agent is automatically available!
console.log(agent.name);
}
async anotherMethod() {
// ✅ This works too - no setup needed!
const { agent } = getCurrentAgent<MyAgent>();
return agent.state;
}
}Zero configuration required! The framework automatically:
- Scans your agent class for custom methods
- Wraps them with agent context during initialization
- Ensures
getCurrentAgent()works in all external functions called from your methods
import { AIChatAgent } from "@cloudflare/ai-chat";
import { getCurrentAgent } from "agents";
import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";
// External utility function that needs agent context
async function processWithAI(prompt: string) {
const { agent } = getCurrentAgent<MyAgent>();
// ✅ External functions can access the current agent!
return await generateText({
model: openai("gpt-4"),
prompt: `Agent ${agent?.name}: ${prompt}`
});
}
export class MyAgent extends AIChatAgent {
async customMethod(message: string) {
// Use this.* to access agent properties directly
console.log("Agent name:", this.name);
console.log("Agent state:", this.state);
// External functions automatically work!
const result = await processWithAI(message);
return result.text;
}
}- Built-in methods (onRequest, onEmail, onStateChanged): Already have context
- Custom methods (your methods): Automatically wrapped during initialization
- External functions: Access context through
getCurrentAgent()
// When you call a custom method:
agent.customMethod()
→ automatically wrapped with agentContext.run()
→ your method executes with full context
→ external functions can use getCurrentAgent()import { AIChatAgent } from "@cloudflare/ai-chat";
import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";
export class MyAgent extends AIChatAgent {
async generateResponse(prompt: string) {
// AI SDK tools automatically work
const response = await generateText({
model: openai("gpt-4"),
prompt,
tools: {
// Tools that use getCurrentAgent() work perfectly
}
});
return response.text;
}
}import { AIChatAgent } from "@cloudflare/ai-chat";
import { getCurrentAgent } from "agents";
async function saveToDatabase(data: any) {
const { agent } = getCurrentAgent<MyAgent>();
// Can access agent info for logging, context, etc.
console.log(`Saving data for agent: ${agent?.name}`);
}
export class MyAgent extends AIChatAgent {
async processData(data: any) {
// External functions automatically have context
await saveToDatabase(data);
}
}The agents package exports one main function for context management:
Gets the current agent from any context where it's available.
Returns:
{
agent: T | undefined,
connection: Connection | undefined,
request: Request | undefined
}Usage:
import { AIChatAgent } from "@cloudflare/ai-chat";
import { getCurrentAgent } from "agents";
export class MyAgent extends AIChatAgent {
async customMethod() {
const { agent, connection, request } = getCurrentAgent<MyAgent>();
// agent is properly typed as MyAgent
// connection and request available if called from a request handler
}
}