ai-agent
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAI Agent
AI Agent
IMPORTANT: Before doing anything, you MUST read in this skill's directory. It contains essential guidance on debugging, error handling, state management, deployment, and project setup. Those rules and patterns apply to all RivetKit work. Everything below assumes you have already read and understood it.
BASE_SKILL.md重要提示:在进行任何操作之前,你必须阅读本技能目录下的文件。其中包含了调试、错误处理、状态管理、部署和项目设置的关键指南。这些规则和模式适用于所有RivetKit工作。以下所有内容均假设你已阅读并理解该文件。
BASE_SKILL.mdWorking Examples
示例实现
If you need a reference implementation, read the raw working example code in these templates:
Patterns for building AI agent backends with RivetKit, where each conversation is one Rivet Actor that owns its memory, its message queue, and its streaming output.
Starter Code
初始代码
Start with one of the working examples on GitHub and adapt it. The sections below describe the flagship example unless a variant is called out explicitly.
ai-agent| Variant | Starter Code | Use When |
|---|---|---|
| Queue-driven AI SDK agent | GitHub | You want a streaming chat agent where each conversation keeps its own persistent memory and processes one message at a time. |
| Sandbox coding agent | GitHub | The agent should run a coding agent (Codex by default) inside an isolated sandbox via Docker, Daytona, or E2B. |
| Durable streams agent (experimental) | GitHub | You want replayable, restart-safe prompt and response delivery through durable streams instead of actor state and events. |
| Agent with a workspace (agentOS) | GitHub | The agent needs its own persistent computer: a filesystem, processes, shells, and preview URLs. See the cookbook: AI Agent Workspaces. |
从GitHub上的示例代码开始进行适配:GitHub。除非特别说明变体,否则以下章节均以旗舰版示例为例。
ai-agentConversation Memory
对话记忆
Use one actor per conversation, keyed by a conversation or agent id (see Actor Keys). The agent actor's persistent state is the conversation memory: in the example, and live in JSON actor state and survive sleep and restarts with no external database. Every model call rebuilds the prompt from plus a system prompt, so memory and inference input are the same data.
ai-agentmessagesstatusc.state.messages| Variant | Where Memory Lives | Persisted State Fields |
|---|---|---|
| JSON actor state | |
| JSON actor state plus the sandbox ACP session | |
| Durable streams; the actor stores only its conversation id and a read cursor | |
每个对话对应一个Actor,通过对话ID或Agent ID作为键(参考Actor Keys)。Agent Actor的持久化state即为对话记忆:在示例中,和存储在JSON格式的Actor状态中,即使Actor休眠或重启也不会丢失,无需外部数据库。每次模型调用都会从加上系统提示词重建输入,因此记忆和推理输入是同一数据。
ai-agentmessagesstatusc.state.messages| 变体 | 记忆存储位置 | 持久化状态字段 |
|---|---|---|
| JSON Actor状态 | |
| JSON Actor状态 + 沙箱ACP会话 | |
| 持久化流;Actor仅存储对话ID和读取游标 | |
Message Handling
消息处理
In the example, the client pushes user input onto the agent's queue with . This is a queue push, not an action call. The actor's hook (see Lifecycle) consumes the queue serially with .
ai-agentmessageagent.connection.send("message", { text, sender })runfor await (const queued of c.queue.iter())Serial queue consumption is the per-conversation concurrency guarantee: at most one in-flight model call per actor, with no extra locking. The field ( while a model call is in flight) is UI signal only; the run loop is the actual lock. The loop also checks inside the token stream so shutdown exits gracefully.
statusthinkingc.aborted| Variant | Message Ingress | Serialization Guarantee |
|---|---|---|
| | |
| | Each call awaits the sandbox round trip before broadcasting the result. |
| Durable prompt stream long-polled from | |
在示例中,客户端通过将用户输入推送到Agent的queue中。这是队列推送操作,而非动作调用。Actor的钩子(参考Lifecycle)通过串行消费队列中的消息。
ai-agentagent.connection.send("message", { text, sender })messagerunfor await (const queued of c.queue.iter())串行队列消费保证了每个对话的并发控制:每个Actor最多同时处理一个模型调用,无需额外锁机制。字段(模型调用进行中时为)仅用于UI提示;实际的锁由运行循环实现。循环还会在令牌流中检查,以便在关闭时优雅退出。
statusthinkingc.aborted| 变体 | 消息入口 | 序列化保证 |
|---|---|---|
| 通过 | |
| | 每次调用都会等待沙箱往返完成后再广播结果。 |
| 从 | |
Streaming Responses
流式响应
The actor broadcasts a event for every model text delta. The payload carries , the per-token , the cumulative , and a flag (plus on failure), so clients can either append deltas or idempotently replace the message by using . The example frontend replaces by , which tolerates dropped events. The terminal broadcast has an empty , the full , and .
ai-agentresponsemessageIddeltacontentdoneerrormessageIdcontentmessageIddeltacontentdone: trueBecause the assistant message object lives in and is mutated in place during streaming, partial content persists if the actor restarts mid-stream. The example broadcasts once per AI SDK delta with no throttling; batching or throttling deltas is a recommended extension for high-traffic deployments, not something the example implements.
c.state.messagesVariant differences: sends a single broadcast with after the sandbox finishes (no incremental streaming), and appends per-token chunks to a durable response stream, then broadcasts or .
sandbox-coding-agentresponsedone: trueexperimental-durable-streams-ai-agentresponseCompleteresponseErrorai-agentresponsemessageIddeltacontentdoneerrormessageIdcontentmessageIddeltacontentdone: true由于助手消息对象存储在中,并在流式传输过程中被原地修改,因此如果Actor在流式传输中途重启,部分内容仍会保留。示例中每次AI SDK产生增量时都会立即广播,未做限流;对于高流量部署,建议扩展实现批量处理或限流增量,但示例未包含此功能。
c.state.messages变体差异:在沙箱完成后发送单个广播,(无增量流式传输);将每个令牌块追加到持久化响应流,然后广播或。
sandbox-coding-agentresponsedone: trueexperimental-durable-streams-ai-agentresponseCompleteresponseErrorArchitecture
架构
| Topic | Summary |
|---|---|
| Topology | |
| Ingress | Client pushes |
| Streaming | One |
| Memory | Full transcript and status in JSON actor state; no external database. |
The manager creates records and warms each agent through actor-to-actor communication: calls , then and awaits so the conversation actor exists before the client connects. The sandbox variant extends this topology with a actor that shares the agent's key (), so the agent-to-sandbox mapping is implicit in the key space.
AgentInfocreateAgentc.client<typeof registry>()client.agent.getOrCreate([info.id])getStatus()codingSandboxcodingSandbox.getOrCreate([c.key[0]])Actors
-
Key:
agentManager["primary"] -
Responsibility: Directory actor. Createsrecords, lists agents, and warms each agent actor via
AgentInfo.c.client() -
Actions
createAgentlistAgents
-
Queues
- None
-
State
- JSON
agents
-
Key:
agent[agentId] -
Responsibility: One actor per conversation. Holds the full message history and status, consumes queued user messages in itsloop, calls the model via the AI SDK, and broadcasts streaming deltas.
run -
Actions
getHistorygetStatus
-
Queues
message
-
Events
messageAddedstatusresponse
-
State
- JSON
messagesstatus
Lifecycle
mermaid
sequenceDiagram
participant C as Client
participant AM as agentManager
participant A as agent
participant LLM as Model API
C->>AM: createAgent(name)
AM->>A: getOrCreate([info.id]) + getStatus()
AM-->>C: AgentInfo
C->>A: connection.send("message", {text, sender})
Note over A: run loop pops queue via c.queue.iter()
A-->>C: messageAdded (user message)
A-->>C: messageAdded (assistant placeholder)
A-->>C: status (thinking)
A->>LLM: streamText(system prompt + history)
loop each text delta
LLM-->>A: delta
A-->>C: response {messageId, delta, content, done: false}
end
A-->>C: response {delta: "", content, done: true}
A-->>C: status (idle)| 主题 | 概述 |
|---|---|
| 拓扑结构 | |
| 入口 | 客户端通过 |
| 流式传输 | 每个模型增量对应一次 |
| 记忆 | 完整对话记录和状态存储在JSON Actor状态中;无需外部数据库。 |
管理器创建记录,并通过Actor间通信预热每个Agent:调用,然后调用并等待,确保对话Actor在客户端连接前已存在。沙箱变体扩展了此拓扑,增加了Actor,其与Agent共享键(),因此Agent与沙箱的映射在键空间中是隐式的。
AgentInfocreateAgentc.client<typeof registry>()client.agent.getOrCreate([info.id])getStatus()codingSandboxcodingSandbox.getOrCreate([c.key[0]])Actors
-
键:
agentManager["primary"] -
职责: 目录Actor。创建记录,列出所有Agent,并通过
AgentInfo预热每个Agent Actor。c.client() -
Actions
createAgentlistAgents
-
Queues
- 无
-
State
- JSON
agents
-
键:
agent[agentId] -
职责: 每个对话对应一个Actor。保存完整消息历史和状态,在循环中消费队列中的用户消息,通过AI SDK调用模型,并广播流式增量。
run -
Actions
getHistorygetStatus
-
Queues
message
-
Events
messageAddedstatusresponse
-
State
- JSON
messagesstatus
生命周期
mermaid
sequenceDiagram
participant C as Client
participant AM as agentManager
participant A as agent
participant LLM as Model API
C->>AM: createAgent(name)
AM->>A: getOrCreate([info.id]) + getStatus()
AM-->>C: AgentInfo
C->>A: connection.send("message", {text, sender})
Note over A: run loop pops queue via c.queue.iter()
A-->>C: messageAdded (user message)
A-->>C: messageAdded (assistant placeholder)
A-->>C: status (thinking)
A->>LLM: streamText(system prompt + history)
loop each text delta
LLM-->>A: delta
A-->>C: response {messageId, delta, content, done: false}
end
A-->>C: response {delta: "", content, done: true}
A-->>C: status (idle)Security Checklist
安全检查清单
The examples ship without auth so they stay minimal. Apply this baseline before exposing an agent backend.
- API keys stay server-side: (or
OPENAI_API_KEY) is read by the AI SDK inside the actor process. The key never reaches the browser; clients only talk to the actor over RivetKit. The sandbox variant forwards keys into the sandbox env, never to the client.ANTHROPIC_API_KEY - Add authentication: The examples have no auth, so anyone who reaches the server can create agents, list them, and message any agent whose key they can guess. Add or
onBeforeConnectchecks with scoped tokens as a recommended extension. See Authentication.createConnState - Validate and rate-limit queue payloads: The example only skips bodies without a string . Enforce payload size limits, schema validation, and per-connection rate limits as a recommended extension.
text - Derive sender identity server-side: The example trusts the client-supplied field verbatim. Bind sender identity to the authenticated connection instead.
sender - Cap or trim message history: The example sends the full transcript on every model call with no cap. Trim or summarize old messages as a recommended extension so prompts and state stay bounded.
- Set cost ceilings per conversation: Add per-agent token budgets and quotas as a recommended extension. The sandbox variant runs real compute, so also enforce per-user sandbox quotas and restrict sandbox network egress.
示例未包含认证功能以保持简洁。在对外暴露Agent后端前,请应用以下基线安全措施。
- API密钥保留在服务端: (或
OPENAI_API_KEY)由AI SDK在Actor进程内部读取。密钥永远不会传到浏览器;客户端仅通过RivetKit与Actor通信。沙箱变体将密钥转发到沙箱环境,而非客户端。ANTHROPIC_API_KEY - 添加认证机制: 示例无认证功能,因此任何能访问服务器的人都可以创建Agent、列出所有Agent,以及向任何能猜到键的Agent发送消息。建议扩展实现或
onBeforeConnect检查,并使用范围令牌。参考Authentication。createConnState - 验证并限流队列负载: 示例仅跳过没有字符串类型的请求体。建议扩展实现负载大小限制、Schema验证和每连接限流。
text - 服务端推导发送者身份: 示例直接信任客户端提供的字段。应将发送者身份与已认证的连接绑定。
sender - 限制或裁剪消息历史: 示例每次模型调用都会发送完整对话记录,无上限。建议扩展实现裁剪或总结旧消息,以确保提示词和状态的大小可控。
- 设置每个对话的成本上限: 建议扩展实现每个Agent的令牌预算和配额。沙箱变体运行实际计算,因此还需强制每个用户的沙箱配额,并限制沙箱网络出站流量。
Reference Map
参考映射
Actors
Actors
- Access Control
- Actions
- Actor Keys
- Actor Scheduling
- Actor Statuses
- AI and User-Generated Rivet Actors
- Authentication
- Communicating Between Actors
- Connections
- Custom Inspector Tabs
- Debugging
- Design Patterns
- Destroying Actors
- Errors
- Fetch and WebSocket Handler
- Helper Types
- Icons & Names
- Input Parameters
- Lifecycle
- Limits
- Low-Level HTTP Request Handler
- Low-Level KV Storage
- Low-Level WebSocket Handler
- Metadata
- Next.js Quickstart
- Node.js & Bun Quickstart
- Queues & Run Loops
- React Quickstart
- Realtime
- Rust Quickstart (Preview)
- Sandbox Actor
- Scaling & Concurrency
- Sharing and Joining State
- SQLite
- SQLite + Drizzle
- State & Storage
- Testing
- Troubleshooting
- Types
- Vanilla HTTP API
- Versions & Upgrades
- Workflows
- 访问控制
- Actions
- Actor Keys
- Actor调度
- Actor状态
- AI与用户生成的Rivet Actors
- 认证
- Actor间通信
- 连接
- 自定义检查器标签页
- 调试
- 设计模式
- 销毁Actors
- 错误
- Fetch与WebSocket处理器
- 辅助类型
- 图标与名称
- 输入参数
- 生命周期
- 限制
- 底层HTTP请求处理器
- 底层KV存储
- 底层WebSocket处理器
- 元数据
- Next.js快速开始
- Node.js & Bun快速开始
- 队列与运行循环
- React快速开始
- 实时功能
- Rust快速开始(预览版)
- 沙箱Actor
- 扩容与并发
- 状态共享与合并
- SQLite
- SQLite + Drizzle
- 状态与存储
- 测试
- 故障排除
- 类型
- 原生HTTP API
- 版本与升级
- 工作流
Agent Os
Agent Os
- Agent-to-Agent Communication
- agentOS vs Sandbox
- Authentication
- Benchmarks
- Configuration
- Core Package
- Cron Jobs
- Deployment
- Embedded LLM Gateway
- Events
- Filesystem
- Limitations
- LLM Credentials
- Multiplayer
- Networking & Previews
- Overview
- Permissions
- Persistence & Sleep
- Pi
- Processes & Shell
- Queues
- Quickstart
- Sandbox Mounting
- Security & Auth
- Security Model
- Sessions
- Software
- SQLite
- System Prompt
- Tools
- Webhooks
- Workflow Automation
- Agent间通信
- agentOS vs 沙箱
- 认证
- 基准测试
- 配置
- 核心包
- 定时任务
- 部署
- 嵌入式LLM网关
- 事件
- 文件系统
- 限制
- LLM凭证
- 多人协作
- 网络与预览
- 概述
- 权限
- 持久化与休眠
- Pi
- 进程与Shell
- 队列
- 快速开始
- 沙箱挂载
- 安全与认证
- 安全模型
- 会话
- 软件
- SQLite
- 系统提示词
- 工具
- Webhooks
- 工作流自动化
Clients
Clients
- Node.js & Bun
- React
- Swift
- SwiftUI
- Node.js & Bun
- React
- Swift
- SwiftUI
Connect
Connect
- Deploy To Amazon Web Services Lambda
- Deploying to AWS ECS
- Deploying to Cloudflare Workers
- Deploying to Freestyle
- Deploying to Google Cloud Run
- Deploying to Hetzner
- Deploying to Kubernetes
- Deploying to Railway
- Deploying to Rivet Compute
- Deploying to Supabase Functions
- Deploying to Vercel
- Deploying to VMs & Bare Metal
- 部署到Amazon Web Services Lambda
- 部署到AWS ECS
- 部署到Cloudflare Workers
- 部署到Freestyle
- 部署到Google Cloud Run
- 部署到Hetzner
- 部署到Kubernetes
- 部署到Railway
- 部署到Rivet Compute
- 部署到Supabase Functions
- 部署到Vercel
- 部署到虚拟机与裸金属服务器
Cookbook
Cookbook
- AI Agent
- AI Agent Workspaces
- Chat Room
- Collaborative Text Editor
- Cron Jobs and Scheduled Tasks
- Database per Tenant
- Deploying Rivet in a VPC or Air-Gapped Network
- Live Cursors and Presence
- Multiplayer Game
- AI Agent
- AI Agent工作区
- 聊天室
- 协作文本编辑器
- 定时任务与计划任务
- 租户专属数据库
- 在VPC或隔离网络中部署Rivet
- 实时光标与在线状态
- 多人游戏
General
General
- Actor Configuration
- Architecture
- Cross-Origin Resource Sharing
- Documentation for LLMs & AI
- Edge Networking
- Endpoints
- Environment Variables
- HTTP Server
- Logging
- Pool Configuration
- Production Checklist
- Registry Configuration
- Runtime Modes
- Actor配置
- 架构
- 跨域资源共享
- LLM与AI相关文档
- 边缘网络
- 端点
- 环境变量
- HTTP服务器
- 日志
- 池配置
- 生产环境检查清单
- 注册表配置
- 运行时模式
Self Hosting
Self Hosting
- Configuration
- Docker Compose
- Docker Container
- File System
- FoundationDB (Enterprise)
- Installing Rivet Engine
- Kubernetes
- Multi-Region
- PostgreSQL
- Production Checklist
- Railway Deployment
- Render Deployment
- TLS & Certificates
- 配置
- Docker Compose
- Docker容器
- 文件系统
- FoundationDB(企业版)
- 安装Rivet引擎
- Kubernetes
- 多区域部署
- PostgreSQL
- 生产环境检查清单
- Railway部署
- Render部署
- TLS与证书