Agent 支持 WebSocket 连接以实现实时双向通信。本页介绍服务端 WebSocket 处理。客户端连接请参阅 客户端 SDK。
Agent 在多个时点触发以下生命周期钩子:
| Hook | 调用时机 |
|---|---|
onStart(props?) |
Agent 首次启动时(任何连接之前)一次 |
onRequest(request) |
收到 HTTP 请求时(非 WebSocket) |
onConnect(connection, ctx) |
建立新 WebSocket 连接时 |
onMessage(connection, message) |
收到 WebSocket 消息时 |
onClose(connection, code, reason, wasClean) |
WebSocket 连接关闭时 |
onError(connection, error) |
连接上发生 WebSocket 错误时 |
onError(error) |
发生服务端级错误时(与特定连接无关) |
shouldSendProtocolMessages(connection, ctx) |
是否向此连接发送协议消息(identity、state、MCP)。默认:true |
onStart() 在 Agent 首次启动时调用一次,早于任何连接建立:
export class MyAgent extends Agent {
async onStart() {
// Initialize resources
console.log(`Agent ${this.name} starting...`);
// Load data from storage
const savedData = this.sql`SELECT * FROM cache`;
for (const row of savedData) {
// Rebuild in-memory state from persistent storage
}
}
onConnect(connection) {
// By the time connections arrive, onStart has completed
}
}export class MyAgent extends Agent {
async onStart() {
// Initialize resources
console.log(`Agent ${this.name} starting...`);
// Load data from storage
const savedData = this.sql`SELECT * FROM cache`;
for (const row of savedData) {
// Rebuild in-memory state from persistent storage
}
}
onConnect(connection: Connection) {
// By the time connections arrive, onStart has completed
}
}在 Agent 上定义 onConnect 与 onMessage 方法以接受 WebSocket 连接:
import { Agent, Connection, ConnectionContext, WSMessage } from "agents";
export class ChatAgent extends Agent {
async onConnect(connection, ctx) {
// Connections are automatically accepted
// Access the original request for auth, headers, cookies
const url = new URL(ctx.request.url);
const token = url.searchParams.get("token");
if (!token) {
connection.close(4001, "Unauthorized");
return;
}
// Store user info on this connection
connection.setState({ authenticated: true });
}
async onMessage(connection, message) {
if (typeof message === "string") {
// Handle text message
const data = JSON.parse(message);
connection.send(JSON.stringify({ received: data }));
}
}
}import { Agent, Connection, ConnectionContext, WSMessage } from "agents";
export class ChatAgent extends Agent {
async onConnect(connection: Connection, ctx: ConnectionContext) {
// Connections are automatically accepted
// Access the original request for auth, headers, cookies
const url = new URL(ctx.request.url);
const token = url.searchParams.get("token");
if (!token) {
connection.close(4001, "Unauthorized");
return;
}
// Store user info on this connection
connection.setState({ authenticated: true });
}
async onMessage(connection: Connection, message: WSMessage) {
if (typeof message === "string") {
// Handle text message
const data = JSON.parse(message);
connection.send(JSON.stringify({ received: data }));
}
}
}每个已连接客户端拥有唯一的 Connection 对象:
| 属性/方法 | 类型 | 描述 |
|---|---|---|
id |
string |
此连接的唯一标识符 |
uri |
string | null |
原始 WebSocket 升级请求的 URL。休眠后仍保留 |
state |
State |
每连接 state 对象 |
setState(state) |
void |
更新连接 state |
send(message) |
void |
向此客户端发送消息 |
close(code?, reason?) |
void |
关闭连接 |
tags |
readonly string[] |
通过 getConnectionTags 分配的标签。始终以连接 ID 为第一项 |
server |
string |
Agent 实例名(与 Agent 上 this.name 相同) |
存储各连接特有数据(用户信息、偏好等):
export class ChatAgent extends Agent {
async onConnect(connection, ctx) {
const userId = new URL(ctx.request.url).searchParams.get("userId");
connection.setState({
userId: userId || "anonymous",
role: "user",
joinedAt: Date.now(),
});
}
async onMessage(connection, message) {
// Access connection-specific state
console.log(`Message from ${connection.state.userId}`);
}
}interface ConnectionState {
userId: string;
role: "admin" | "user";
joinedAt: number;
}
export class ChatAgent extends Agent {
async onConnect(
connection: Connection<ConnectionState>,
ctx: ConnectionContext,
) {
const userId = new URL(ctx.request.url).searchParams.get("userId");
connection.setState({
userId: userId || "anonymous",
role: "user",
joinedAt: Date.now(),
});
}
async onMessage(connection: Connection<ConnectionState>, message: WSMessage) {
// Access connection-specific state
console.log(`Message from ${connection.state.userId}`);
}
}使用 this.broadcast() 向所有已连接客户端发送消息:
export class ChatAgent extends Agent {
async onMessage(connection, message) {
// Broadcast to all connected clients
this.broadcast(
JSON.stringify({
from: connection.id,
message: message,
timestamp: Date.now(),
}),
);
}
// Broadcast from any method
async notifyAll(event, data) {
this.broadcast(JSON.stringify({ event, data }));
}
}export class ChatAgent extends Agent {
async onMessage(connection: Connection, message: WSMessage) {
// Broadcast to all connected clients
this.broadcast(
JSON.stringify({
from: connection.id,
message: message,
timestamp: Date.now(),
}),
);
}
// Broadcast from any method
async notifyAll(event: string, data: unknown) {
this.broadcast(JSON.stringify({ event, data }));
}
}传入要从广播中排除的连接 ID 数组:
// Broadcast to everyone except the sender
this.broadcast(
JSON.stringify({ type: "user-typing", userId: "123" }),
[connection.id], // Do not send to the originator
);// Broadcast to everyone except the sender
this.broadcast(
JSON.stringify({ type: "user-typing", userId: "123" }),
[connection.id], // Do not send to the originator
);为连接打标签以便过滤。覆盖 getConnectionTags() 在连接建立时分配标签:
export class ChatAgent extends Agent {
getConnectionTags(connection, ctx) {
const url = new URL(ctx.request.url);
const role = url.searchParams.get("role");
const tags = [];
if (role === "admin") tags.push("admin");
if (role === "moderator") tags.push("moderator");
return tags; // Up to 9 tags, max 256 chars each
}
// Later, broadcast only to admins
notifyAdmins(message) {
for (const conn of this.getConnections("admin")) {
conn.send(message);
}
}
}export class ChatAgent extends Agent {
getConnectionTags(connection: Connection, ctx: ConnectionContext): string[] {
const url = new URL(ctx.request.url);
const role = url.searchParams.get("role");
const tags: string[] = [];
if (role === "admin") tags.push("admin");
if (role === "moderator") tags.push("moderator");
return tags; // Up to 9 tags, max 256 chars each
}
// Later, broadcast only to admins
notifyAdmins(message: string) {
for (const conn of this.getConnections("admin")) {
conn.send(message);
}
}
}| 方法 | Signature | 描述 |
|---|---|---|
getConnections |
(tag?: string) => Iterable<Connection> |
获取所有连接,可按标签过滤 |
getConnection |
(id: string) => Connection | undefined |
按 ID 获取连接 |
getConnectionTags |
(connection, ctx) => string[] |
覆盖以给连接打标签 |
broadcast |
(message, without?: string[]) => void |
发送到所有连接 |
isConnectionReadonly |
(connection) => boolean |
检查连接是否为只读 |
isConnectionProtocolEnabled |
(connection) => boolean |
检查此连接是否启用协议消息 |
消息可为字符串或二进制(ArrayBuffer / ArrayBufferView):
export class FileAgent extends Agent {
async onMessage(connection, message) {
if (message instanceof ArrayBuffer) {
// Handle binary upload
const bytes = new Uint8Array(message);
await this.processFile(bytes);
connection.send(
JSON.stringify({ status: "received", size: bytes.length }),
);
} else if (typeof message === "string") {
// Handle text command
const command = JSON.parse(message);
// ...
}
}
}export class FileAgent extends Agent {
async onMessage(connection: Connection, message: WSMessage) {
if (message instanceof ArrayBuffer) {
// Handle binary upload
const bytes = new Uint8Array(message);
await this.processFile(bytes);
connection.send(
JSON.stringify({ status: "received", size: bytes.length }),
);
} else if (typeof message === "string") {
// Handle text command
const command = JSON.parse(message);
// ...
}
}
}处理连接错误与断开。onError 有两个重载——一个用于 WebSocket 连接错误,一个用于服务端级错误:
export class ChatAgent extends Agent {
// WebSocket connection error
// Server-level error (not tied to a specific connection)
onError(connectionOrError, error) {
if (error) {
console.error(`Connection ${connectionOrError.id} error:`, error);
} else {
console.error("Server error:", connectionOrError);
}
}
async onClose(connection, code, reason, wasClean) {
console.log(`Connection ${connection.id} closed: ${code} ${reason}`);
this.broadcast(
JSON.stringify({
event: "user-left",
userId: connection.state?.userId,
}),
);
}
}export class ChatAgent extends Agent {
// WebSocket connection error
onError(connection: Connection, error: unknown): void;
// Server-level error (not tied to a specific connection)
onError(error: unknown): void;
onError(connectionOrError: Connection | unknown, error?: unknown) {
if (error) {
console.error(
`Connection ${(connectionOrError as Connection).id} error:`,
error,
);
} else {
console.error("Server error:", connectionOrError);
}
}
async onClose(
connection: Connection,
code: number,
reason: string,
wasClean: boolean,
) {
console.log(`Connection ${connection.id} closed: ${code} ${reason}`);
this.broadcast(
JSON.stringify({
event: "user-left",
userId: connection.state?.userId,
}),
);
}
}默认 onError 实现会记录错误并重新抛出。覆盖它以添加自定义错误处理、上报或恢复逻辑。
| 类型 | 描述 |
|---|---|
string |
文本消息(通常为 JSON) |
ArrayBuffer |
二进制数据 |
ArrayBufferView |
二进制数据的类型化数组视图 |
Agent 支持休眠——不活跃时可睡眠,需要时唤醒。在保持 WebSocket 连接的同时节省资源。
休眠默认启用。要禁用:
export class AlwaysOnAgent extends Agent {
static options = { hibernate: false };
}export class AlwaysOnAgent extends Agent {
static options = { hibernate: false };
}- Agent 活跃,处理连接
- 一段时间无消息不活跃后,Agent 休眠(睡眠)
- WebSocket 连接保持打开(由 Cloudflare 处理)
- 消息到达时 Agent 唤醒
- 正常调用
onMessage
| 保留 | 不保留 |
|---|---|
this.state(Agent state) |
内存变量 |
connection.state |
定时器/interval |
SQLite 数据(this.sql) |
进行中的 Promise |
| 连接元数据 | 本地缓存 |
将重要数据存入 this.state 或 SQLite,而非类属性:
export class MyAgent extends Agent {
initialState = { counter: 0 };
// Do not do this - lost on hibernation
localCounter = 0;
onMessage(connection, message) {
// Persists across hibernation
this.setState({ counter: this.state.counter + 1 });
// Lost after hibernation
this.localCounter++;
}
}export class MyAgent extends Agent<Env, { counter: number }> {
initialState = { counter: 0 };
// Do not do this - lost on hibernation
private localCounter = 0;
onMessage(connection: Connection, message: WSMessage) {
// Persists across hibernation
this.setState({ counter: this.state.counter + 1 });
// Lost after hibernation
this.localCounter++;
}
}使用每连接 state 跟踪在线用户。用户断开时连接 state 自动清理:
export class PresenceAgent extends Agent {
onConnect(connection, ctx) {
const url = new URL(ctx.request.url);
const name = url.searchParams.get("name") || "Anonymous";
connection.setState({
name,
joinedAt: Date.now(),
lastSeen: Date.now(),
});
// Send current presence to new user
connection.send(
JSON.stringify({
type: "presence",
users: this.getPresence(),
}),
);
// Notify others that someone joined
this.broadcastPresence();
}
onClose(connection) {
// No manual cleanup needed - connection state is automatically gone
this.broadcastPresence();
}
onMessage(connection, message) {
if (message === "ping") {
connection.setState((prev) => ({
...prev,
lastSeen: Date.now(),
}));
connection.send("pong");
}
}
getPresence() {
const users = {};
for (const conn of this.getConnections()) {
if (conn.state) {
users[conn.id] = {
name: conn.state.name,
lastSeen: conn.state.lastSeen,
};
}
}
return users;
}
broadcastPresence() {
this.broadcast(
JSON.stringify({
type: "presence",
users: this.getPresence(),
}),
);
}
}type UserState = {
name: string;
joinedAt: number;
lastSeen: number;
};
export class PresenceAgent extends Agent {
onConnect(connection: Connection<UserState>, ctx: ConnectionContext) {
const url = new URL(ctx.request.url);
const name = url.searchParams.get("name") || "Anonymous";
connection.setState({
name,
joinedAt: Date.now(),
lastSeen: Date.now(),
});
// Send current presence to new user
connection.send(
JSON.stringify({
type: "presence",
users: this.getPresence(),
}),
);
// Notify others that someone joined
this.broadcastPresence();
}
onClose(connection: Connection) {
// No manual cleanup needed - connection state is automatically gone
this.broadcastPresence();
}
onMessage(connection: Connection<UserState>, message: WSMessage) {
if (message === "ping") {
connection.setState((prev) => ({
...prev!,
lastSeen: Date.now(),
}));
connection.send("pong");
}
}
private getPresence() {
const users: Record<string, { name: string; lastSeen: number }> = {};
for (const conn of this.getConnections<UserState>()) {
if (conn.state) {
users[conn.id] = {
name: conn.state.name,
lastSeen: conn.state.lastSeen,
};
}
}
return users;
}
private broadcastPresence() {
this.broadcast(
JSON.stringify({
type: "presence",
users: this.getPresence(),
}),
);
}
}export class ChatRoom extends Agent {
onConnect(connection, ctx) {
const url = new URL(ctx.request.url);
const username = url.searchParams.get("username") || "Anonymous";
connection.setState({ username });
// Notify others
this.broadcast(
JSON.stringify({
type: "join",
user: username,
timestamp: Date.now(),
}),
[connection.id], // Do not send to the joining user
);
}
onMessage(connection, message) {
if (typeof message !== "string") return;
const { username } = connection.state;
this.broadcast(
JSON.stringify({
type: "message",
user: username,
text: message,
timestamp: Date.now(),
}),
);
}
onClose(connection) {
const { username } = connection.state || {};
if (username) {
this.broadcast(
JSON.stringify({
type: "leave",
user: username,
timestamp: Date.now(),
}),
);
}
}
}type Message = {
type: "message" | "join" | "leave";
user: string;
text?: string;
timestamp: number;
};
export class ChatRoom extends Agent {
onConnect(connection: Connection, ctx: ConnectionContext) {
const url = new URL(ctx.request.url);
const username = url.searchParams.get("username") || "Anonymous";
connection.setState({ username });
// Notify others
this.broadcast(
JSON.stringify({
type: "join",
user: username,
timestamp: Date.now(),
} satisfies Message),
[connection.id], // Do not send to the joining user
);
}
onMessage(connection: Connection, message: WSMessage) {
if (typeof message !== "string") return;
const { username } = connection.state as { username: string };
this.broadcast(
JSON.stringify({
type: "message",
user: username,
text: message,
timestamp: Date.now(),
} satisfies Message),
);
}
onClose(connection: Connection) {
const { username } = (connection.state as { username: string }) || {};
if (username) {
this.broadcast(
JSON.stringify({
type: "leave",
user: username,
timestamp: Date.now(),
} satisfies Message),
);
}
}
}默认 Agent 向每个连接发送 JSON 文本帧(identity、state 同步、MCP server 列表)。覆盖 shouldSendProtocolMessages 可为特定连接抑制——例如无法处理 JSON 文本帧的纯二进制客户端:
export class IoTAgent extends Agent {
shouldSendProtocolMessages(connection, ctx) {
const url = new URL(ctx.request.url);
return url.searchParams.get("protocol") !== "binary";
}
}export class IoTAgent extends Agent {
shouldSendProtocolMessages(
connection: Connection,
ctx: ConnectionContext,
): boolean {
const url = new URL(ctx.request.url);
return url.searchParams.get("protocol") !== "binary";
}
}返回 false 时,连接不会收到身份、状态或 MCP 服务器列表帧——连接时与广播均不会。连接仍可收发常规消息、使用 RPC 并参与所有非协议通信。
使用 isConnectionProtocolEnabled(connection) 在运行时检查任意连接状态。
任意 Agent 方法内的 this 上可用以下属性:
| 属性 | 类型 | 描述 |
|---|---|---|
this.name |
string |
此 Agent 的实例名 |
this.state |
State |
当前 Agent state(从 SQLite 懒加载) |
this.env |
Env |
Worker 环境绑定 |
this.ctx |
DurableObjectState |
Durable Object 上下文(storage、alarm 等) |
this.sql |
template tag | 针对 Agent SQLite 存储执行查询的 SQL 模板标签 |
this.mcp |
MCPClientManager |
连接外部 MCP server 的 MCP client 管理器 |
浏览器连接请使用 Agents 客户端 SDK:
- Vanilla JS:
agents/client的AgentClient - React:
agents/react的useAgenthook
完整文档请参阅 客户端 SDK。