useStream 配置,请参阅 Going to production。
Architecture
此设置包含三部分:-
带有 sandbox backend 的 deep agent: Agent 会自动从 sandbox 获得
filesystem tools(
read_file、write_file、edit_file、execute) -
Custom API server,通过
langgraph.json的http.app字段暴露的 FastAPI app,提供 frontend 可以调用的 file browsing endpoints - Three-panel frontend: File tree、code/diff viewer 和 chat panel, 会在 agent 做出更改时实时同步文件
Sandbox lifecycle
在连接 frontend 之前,先选择 sandbox 的存活时间以及谁可以共享它。 如需了解 thread-scoped 与 assistant-scoped sandboxes、async graph factory 设置、TTL 行为和 SDK 调用示例, 请参阅 Sandbox lifecycle。 本指南默认使用 thread-scoped sandboxes。Frontend 和 custom API server 都会从 LangGraph thread ID 解析 sandbox。 这会隔离各个 conversation,并且在你持久化 thread ID 后, 页面重新加载时可以重新连接到同一个环境。 对于 multi-tenant apps, 请改为在 backend factory 中按 user 或 assistant 限定 sandboxes。对于没有 LangGraph threads 的 demo,请在 API URL 中传入客户端生成的 session ID。 该 session ID 不会跨浏览器会话持久化。Connect the agent and API server
按照 Execution environment 中的说明,使用 sandbox backend 配置 deep agent。 Agent 会自动获得 filesystem tools 和execute tool,不需要额外的 tool 配置。
构建这个 UI 会在 production 设置之外增加一个要求:需要一个在 agent graph
之外运行的 custom API server。因此,对于每个 thread,agent backend
和 file-browsing routes 必须解析到同一个 sandbox。将 sandbox ID
存储在线程 metadata 上,并在两者之间共享同一个 lookup function。
Resolve the sandbox from thread metadata
与 Going to production 中的示例类似,
agent 是一个在每次 run 时调用的 async graph factory。将 sandbox ID
存储在线程 metadata 上,使 custom
http.app routes 可以调用同一个
getOrCreateSandboxForThread helper。当 LangGraph SDK 是唯一入口时,
Going to production 会改用 provider label lookup。Seed project files
在 agent 运行前,使用uploadFiles / upload_files 上传 starter files。
如需了解 seeding patterns、provider examples,以及如何将
memories 或 skills 同步到
sandbox,请参阅 File transfers。
对于 LangSmith sandboxes,请在创建 container 时传入来自
sandbox snapshot 的 templateName。
Adding the file browsing API
Agent 可以读写文件,但 frontend 也需要直接访问 sandbox filesystem 来浏览文件。添加一个 custom FastAPI API server, 并通过langgraph.json 中的 http.app 字段将其暴露出来。
Create the API server
Sandbox API endpoints 使用 thread ID 作为 URL path parameter。这可以确保 frontend 始终访问当前 conversation 对应的正确 sandbox,并使用与 agent backend 相同的 helper:get_or_create_sandbox_for_thread
Agent backend 和 API server 都会调用同一个
get_or_create_sandbox_for_thread function。这可以确保它们始终解析到给定 thread 的同一个 sandbox。Thread metadata 中的 sandbox ID
是唯一事实来源,不需要 in-memory caches。Configure langgraph.json
同时注册 agent graph 和 API server。http.app 字段会告诉 LangGraph
platform 将你的 custom routes 与默认 routes 一起提供服务。有关完整的
langgraph.json 选项,请参阅 application structure
和 LangSmith Deployments。
langgraph dev 的本地开发环境,该 host 是 http://localhost:2024。
http.app 中定义的 custom routes 优先级高于默认 LangGraph routes。这意味着你
可以在需要时覆盖内置 endpoints,但要小心不要意外覆盖 /threads 或 /runs
这样的 routes。Building the frontend
Frontend 有三个 panel:file tree sidebar、code/diff viewer 和 chat panel。 它使用useStream 处理 agent conversation,并使用 custom API endpoints
进行 file browsing。
对于 production deployment,请将 apiUrl 指向你的
LangSmith Deployment,启用 reconnectOnMount
和 fetchStateHistory,并在每次 run 时传入稳定的 thread_id。
如需了解这些设置,以及如何使用 thread_id 和 runtime context
调用 agent,请参阅
Going to production 中的
Frontend。
Thread creation
在页面加载时创建 LangGraph thread,并将其 ID 持久化到sessionStorage,
使页面重新加载后可以重新连接到同一个 sandbox:
File state management
跟踪 sandbox filesystem 的两个 snapshot:original state(agent 运行前) 和 current state(实时更新)。API URL 中包含 thread ID,因此请求始终会命中 正确的 sandbox:Real-time file sync
IDE 体验的关键是在 agent 工作时更新文件,而不是等它结束后再更新。 监听 stream messages 中来自 file-mutating tools 的ToolMessage 实例。
当 write_file 或 edit_file tool call 完成时,刷新对应文件。当 execute
完成时,刷新全部内容,因为 shell command 可能修改任意文件:
Detecting changed files
在每次 agent run 之前,对当前文件内容创建 snapshot。文件刷新后, 将其与 snapshot 比较,以识别哪些文件发生了更改:Displaying diffs
使用适合 framework 的 diff library 渲染 unified diffs:| Framework | Library | Component |
|---|---|---|
| React | @pierre/diffs | <FileDiff> 搭配 parseDiffFromFile |
| Vue | @git-diff-view/vue | <DiffView> 搭配来自 @git-diff-view/file 的 generateDiffFile |
| Svelte | @git-diff-view/svelte | <DiffView> 搭配来自 @git-diff-view/file 的 generateDiffFile |
| Angular | ngx-diff | <ngx-unified-diff> 搭配 [before] 和 [after] |
@pierre/diffs 的示例(React):
Changed files summary
显示所有已修改文件的摘要,并包含行级 addition/deletion 计数。这会让用户快速了解 agent 的影响,类似于git status:
Use cases
在以下场景中,sandbox 是合适选择:- Coding agents 会创建、修改和运行代码,并且需要聊天之外的可视化界面。
- Code review workflows 中,agent 建议更改,用户在接受前查看 diffs。
- Tutorial or learning apps 中,AI assistant 帮助用户逐步构建项目, 并在上下文中显示变更。
- Prototyping tools 中,用户用自然语言描述功能,并实时观察 agent 实现它们。
Best practices
Frontend 相关:- 在
sessionStorage中持久化threadId,使页面重新加载时重新连接到同一个 thread 和 sandbox,而不是创建新的 thread 和 sandbox。 - 在每个相关 tool call 上同步文件,不要只在 run 结束时同步。监听
write_file、edit_file和executetool messages,并立即刷新。 - 对已更改文件默认显示 diff view。当用户点击 agent 修改过的文件时, 先显示 diff,因为这是他们最关心的内容。
- 对 read-only operations 显示紧凑的 tool results。不要在 chat 中倾倒
read_file的完整输出,而是显示类似Read router.js L1-42的单行内容。 将完整输出展示留给 mutating tools。 - 从 file tree 中过滤
node_modules。用户不需要浏览数千个 dependency files。 获取 tree 时将它们过滤掉。
- 对 production apps 使用 thread-scoped sandboxes。请参阅 Sandbox lifecycle。
- 在 agent backend 和 API server 之间共享 sandbox resolution。通过 thread metadata 让两者解析同一个环境,且不需要 in-memory caches。
- 用真实项目填充 sandbox。请参阅 File transfers。
- 将 secrets 留在 sandbox 外部。对于 API keys,请使用 sandbox auth proxy, 而不是 environment variables 或 file uploads。
- 在上线前添加 guardrails。为 autonomous coding agents 配置 rate limits、 error handling 和 data privacy middleware。
Related
Going to production
使用 persistent sandboxes、auth、guardrails 和 production
useStream 设置部署 agent。Sandboxes
Sandbox providers、security model 和 file transfer APIs。
Frontend overview
其他 deep agent UI patterns:subagent streaming、todo lists 和 custom state。
Application structure
完整的
langgraph.json reference,包括 custom http.app routes。Connect these docs to Claude, VSCode, and more via MCP for real-time answers.

