Loading...
Loading...
Compare original and translation side by side
https://github.com/cloudflare/agents/tree/main/docs| Topic | Doc | Use for |
|---|---|---|
| Getting started | | First agent, project setup |
| State | | |
| Routing | | URL patterns, |
| Callable methods | | |
| Scheduling | | |
| Workflows | | |
| HTTP/WebSockets | | Lifecycle hooks, hibernation |
| Email routing, secure reply resolver | |
| MCP client | | Connecting to MCP servers |
| MCP server | | Building MCP servers with |
| Client SDK | | |
| Human-in-the-loop | | Approval flows, pausing workflows |
| Resumable streaming | | Stream recovery on disconnect |
https://github.com/cloudflare/agents/tree/main/docs| 主题 | 文档 | 适用场景 |
|---|---|---|
| 入门指南 | | 首个Agent开发、项目搭建 |
| 状态管理 | | |
| 路由配置 | | URL规则、 |
| 可调用方法 | | |
| 任务调度 | | |
| 工作流 | | |
| HTTP/WebSocket | | 生命周期钩子、休眠机制 |
| 邮件处理 | | 邮件路由、安全回复解析器 |
| MCP客户端 | | 连接MCP服务器 |
| MCP服务器 | | 使用 |
| 客户端SDK | | |
| 人机协同 | | 审批流、工作流暂停 |
| 可恢复流式传输 | | 断开连接后的流恢复 |
@callable()scheduleEveryAgentWorkflowMcpAgentAIChatAgentuseAgentuseAgentChat@callable()scheduleEveryAgentWorkflowMcpAgentAIChatAgentuseAgentuseAgentChatnpm ls agents # Should show agents packagenpm install agentsnpm ls agents # 应显示agents包npm install agents{
"durable_objects": {
"bindings": [{ "name": "MyAgent", "class_name": "MyAgent" }]
},
"migrations": [{ "tag": "v1", "new_sqlite_classes": ["MyAgent"] }]
}{
"durable_objects": {
"bindings": [{ "name": "MyAgent", "class_name": "MyAgent" }]
},
"migrations": [{ "tag": "v1", "new_sqlite_classes": ["MyAgent"] }]
}import { Agent, routeAgentRequest, callable } from "agents";
type State = { count: number };
export class Counter extends Agent<Env, State> {
initialState = { count: 0 };
// Validation hook - runs before state persists (sync, throwing rejects the update)
validateStateChange(nextState: State, source: Connection | "server") {
if (nextState.count < 0) throw new Error("Count cannot be negative");
}
// Notification hook - runs after state persists (async, non-blocking)
onStateUpdate(state: State, source: Connection | "server") {
console.log("State updated:", state);
}
@callable()
increment() {
this.setState({ count: this.state.count + 1 });
return this.state.count;
}
}
export default {
fetch: (req, env) => routeAgentRequest(req, env) ?? new Response("Not found", { status: 404 })
};import { Agent, routeAgentRequest, callable } from "agents";
type State = { count: number };
export class Counter extends Agent<Env, State> {
initialState = { count: 0 };
// 验证钩子 - 在状态持久化前运行(同步,抛出异常则拒绝更新)
validateStateChange(nextState: State, source: Connection | "server") {
if (nextState.count < 0) throw new Error("Count cannot be negative");
}
// 通知钩子 - 在状态持久化后运行(异步,非阻塞)
onStateUpdate(state: State, source: Connection | "server") {
console.log("State updated:", state);
}
@callable()
increment() {
this.setState({ count: this.state.count + 1 });
return this.state.count;
}
}
export default {
fetch: (req, env) => routeAgentRequest(req, env) ?? new Response("Not found", { status: 404 })
};/agents/{agent-name}/{instance-name}| Class | URL |
|---|---|
| |
| |
useAgent({ agent: "Counter", name: "user-123" })/agents/{agent-name}/{instance-name}| 类名 | URL |
|---|---|
| |
| |
useAgent({ agent: "Counter", name: "user-123" })| Task | API |
|---|---|
| Read state | |
| Write state | |
| SQL query | |
| Schedule (delay) | |
| Schedule (cron) | |
| Schedule (interval) | |
| RPC method | |
| Streaming RPC | |
| Start workflow | |
| 任务 | API |
|---|---|
| 读取状态 | |
| 写入状态 | |
| SQL查询 | |
| 延迟调度 | |
| Cron调度 | |
| 间隔调度 | |
| RPC方法 | |
| 流式RPC | |
| 启动工作流 | |
import { useAgent } from "agents/react";
function App() {
const [state, setLocalState] = useState({ count: 0 });
const agent = useAgent({
agent: "Counter",
name: "my-instance",
onStateUpdate: (newState) => setLocalState(newState),
onIdentity: (name, agentType) => console.log(`Connected to ${name}`)
});
return (
<button onClick={() => agent.setState({ count: state.count + 1 })}>
Count: {state.count}
</button>
);
}import { useAgent } from "agents/react";
function App() {
const [state, setLocalState] = useState({ count: 0 });
const agent = useAgent({
agent: "Counter",
name: "my-instance",
onStateUpdate: (newState) => setLocalState(newState),
onIdentity: (name, agentType) => console.log(`Connected to ${name}`)
});
return (
<button onClick={() => agent.setState({ count: state.count + 1 })}>
Count: {state.count}
</button>
);
}