以下页面详细介绍了部署 LangChain 的示例应用 **深度代理** on Deno Deploy:流式聊天 UI、子代理和线程历史,全部由 Agent Streaming Protocol 实现为 Hono 服务器上的 HTTP + SSE 路由处理程序。React 前端是一个 Vite SPA(从 Next.js 示例移植);Deno 从单一 main.ts entrypoint.
这是 Next.js 示例到 Deno + Hono 的移植,展示了如何在 Deno Deploy 上运行相同的代理堆栈,而不是 Vercel。
Source: js-deno 在部署 cookbook 中。
部署到 Deno Deploy
Create a Deno Deploy project
Fork 或 clone langchain-ai/deployment-cookbook。在 Deno Deploy 控制台中,创建一个链接到此仓库的新项目。
Configure build settings
- - 设置 **根目录** to
js-deno. - - 设置 **构建命令** to
deno task build:client(将 Vite SPA 构建到dist/). - - 设置 **入口点** to
main.ts. - - 在项目环境变量中添加
OPENAI_API_KEY。
Deploy
从控制台部署。Deno 的构建环境运行构建命令,因此 dist/ 在云端生成,无需提交。
或者,使用内置 deno deploy CLI(Deno 2.x)。 deploy 中的 deno.json 设置 org/app。将这些更改为你自己的(或传递 --org/--app 标志,它们会覆盖配置)。
cd js-deno
# First time only: create the app
deno deploy create --org <your-org> --app <your-app> --source local --region us --entrypoint main.ts
# Set your OpenAI key
deno deploy env add OPENAI_API_KEY <your-key> --org <your-org> --app <your-app>
# Build the client, deploy to production, and clean up dist/
deno task deploy
deno task deploy 运行 deno task build:client && deno deploy --prod,然后 rm -rf dist。必须在本地构建,因为 deno deploy --source local 上传你的工作树(不包括 .gitignore)但不 **运行构建命令。这些命令仅在连接 GitHub 的应用上运行。** CLI 有两个特定的注意事项
可选:添加以下变量以启用 LangSmith 追踪 .env.example.
必需的 API 端点
应用在以下路径公开 Agent Streaming Protocol /api/threads/...。路由处理程序位于 server/routes.ts ,并与以下路径中的 Next.js 处理程序镜像对应 js-next/app/api/threads/.
最小集(流式聊天)
| 方法 | 路径 | 用途 |
|---|---|---|
POST | /api/threads/:threadId/commands | 接受协议命令(run.start、…)并启动 agent 运行 |
POST | /api/threads/:threadId/stream | 运行的协议事件 SSE 流 |
GET / POST | /api/threads/:threadId/state | 读取并引导检查点线程状态 |
可选(侧边栏)
| 方法 | 路径 | 用途 |
|---|---|---|
GET | /api/threads | 列出检查点已知的所有线程 |
DELETE | /api/threads/:threadId | 删除线程的会话和检查点 |
POST | /api/threads/:threadId/history | 分页检查点历史记录(Agent Protocol) |
请求流程
%%{init: {"themeVariables": {"lineColor": "#40668D", "primaryColor": "#E5F4FF", "primaryTextColor": "#030710", "primaryBorderColor": "#006DDD"}}}%%
flowchart TB
subgraph browser["Browser (Vite React SPA)"]
SP["StreamProvider"]
Adapter["HttpAgentServerAdapter"]
SP --- Adapter
end
subgraph deno["Deno.serve + Hono"]
CMD["POST /api/threads/:id/commands"]
STR["POST /api/threads/:id/stream (SSE)"]
STA["GET|POST /api/threads/:id/state"]
end
subgraph server["server/"]
SRV["registry · session · threads"]
end
subgraph agent["server/agent"]
AGT["createDeepAgent + checkpointer"]
end
Adapter -->|POST| CMD
Adapter -->|POST| STR
Adapter -->|GET / POST| STA
CMD --> SRV
STR --> SRV
STA --> SRV
SRV --> AGT
classDef process fill:#E5F4FF,stroke:#006DDD,stroke-width:2px,color:#030710
classDef trigger fill:#F6FFDB,stroke:#6E8900,stroke-width:2px,color:#2E3900
classDef output fill:#EBD0F0,stroke:#885270,stroke-width:2px,color:#441E33
class browser,deno process
class server trigger
class agent output
Deno 后端工作原理
此示例作为 **单个 Deno 进程**:
- - **
main.ts**:Deno.serve+ Hono 应用。挂载/api路由并从以下位置服务 Vite 构建的 SPAdist/. - - **
server/routes.ts**:Agent Streaming Protocol 的 Hono 路由定义。 - - **
server/session.ts**:LocalThreadSession:在 LangGraph 中缓冲协议事件StreamChannel,使用matchesSubscription进行过滤,并将匹配帧通过 SSE 分发ReadableStream. - - **
server/threads.ts**:基于检查点的getState/updateState/getHistory,采用 LangGraph SDK 序列化格式。 - - **
server/registry.ts**:进程本地的单例,拥有 agent 并为每个线程 ID 维护一个会话。 - - **
server/agent/**:相同createDeepAgent编排器,与 Next.js 示例相同(researcher + math-whiz 子代理,mock 工具)。
Deno Deploy 使用各自独立的内存 MemorySaver 检查点器运行各个隔离环境。若需跨隔离环境的生产级持久化,请替换为 持久化检查点器 (Postgres、Redis 等)。路由处理程序和 server/threads.ts 辅助函数保持不变。
生产环境持久化
开箱即用,agent 使用内存 MemorySaver 检查点器(server/agent/index.ts)和进程本地的会话映射(server/registry.ts)。这适用于本地开发和单隔离环境部署,但在 Deno Deploy(多隔离环境、冷启动)上,对话状态 **不是持久化的** 跨实例。
替换 MemorySaver in server/agent/index.ts 为持久化检查点器,如 @langchain/langgraph-checkpoint-postgres or @langchain/langgraph-checkpoint-redis. You will also want a shared session/replay store so SSE reconnection works across isolates.
本地开发
cp .env.example .env # set OPENAI_API_KEY
# Terminal 1 — API + static (after first client build)
deno task build:client # first time only
deno task dev
# Terminal 2 — Vite dev server with HMR (proxies /api to :8000)
cd client && pnpm install && pnpm dev
打开 http://localhost:5173 用于开发并支持热重载。Vite 开发服务器代理 /api 到 Deno 服务器的 8000 端口。
用于类似生产环境的本地运行(单服务器,无 HMR):
deno task build:client
deno task start
项目布局
- -
main.ts:DenoDeploy 入口点 (Deno.serve+ Hono)。 - -
server/agent/:深度代理 (createDeepAgent),包含子代理和模拟工具。 - -
server/:协议服务器逻辑:session.ts,threads.ts,serialize.ts,registry.ts,routes.ts. - -
client/:Vite + React SPA(与 Next.js 示例相同的 UI)。 - -
dist/:Vite 构建输出由 Deno 提供服务(由deno task build:client).
另请参阅
- - 框架和平台概览
- - 使用 Next.js 部署
- - 代理流式传输协议