fix(frontend): proxy LangGraph SSE without buffering#2797
Open
LittleChenLiya wants to merge 6 commits into
Open
fix(frontend): proxy LangGraph SSE without buffering#2797LittleChenLiya wants to merge 6 commits into
LittleChenLiya wants to merge 6 commits into
Conversation
3f368e5 to
a4a6d0a
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR changes how the frontend proxies LangGraph-compatible gateway endpoints by replacing Next.js rewrites with an App Router Route Handler, aiming to preserve SSE streaming behavior (avoid response buffering) on /api/langgraph/*.
Changes:
- Add
/api/langgraph/[[...path]]Route Handler that proxies to the gateway while streaming the upstream response body and normalizing headers to discourage buffering/transforms. - Add unit tests to validate URL construction, header forwarding/stripping, and streaming response behavior.
- Replace the previous
/api/:path*catch-all gateway rewrite with an explicit allowlist to prevent it from intercepting/api/langgraph/*.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| frontend/src/app/api/langgraph/[[...path]]/route.ts | Introduces a Node.js runtime Route Handler proxy for LangGraph paths with streaming response passthrough and header normalization. |
| frontend/tests/unit/app/api/langgraph-route.test.ts | Adds unit coverage for URL encoding, header filtering, request body streaming, and response header normalization. |
| frontend/next.config.js | Removes /api/langgraph rewrites and replaces the gateway catch-all rewrite with an explicit set of gateway route rewrites. |
a4a6d0a to
aae3c14
Compare
Open
aae3c14 to
8e89f06
Compare
8e89f06 to
2bad24a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
修复 #2785
问题
Windows 本地开发环境中,LangGraph SSE 通过 Next rewrites 转发到 gateway 时会被前端代理路径缓冲。表现为模型已经开始输出,但浏览器端要等较久后才一次性收到内容。
对比验证:
http://127.0.0.1:8001/api/threads/.../runs/stream可以增量收到 SSE event。http://localhost:3000/api/langgraph/.../stream会出现 event 延迟集中到达。这说明后端和模型本身可以流式输出,问题主要在前端 dev proxy 的 rewrite 路径上;不是单纯缺少
messages-tuplestream mode。修改
/api/langgraph/runs/stream/api/langgraph/threads/[threadId]/runs/stream/api/langgraph/threads/[threadId]/runs/[runId]/stream/api/.../stream,并直接返回 upstream response body,避免 SSE 被中间层缓冲。/api/langgraph/*不接管,继续通过 fallback rewrite 代理到 gateway,降低非流式 API 的副作用。accept-encoding: identity。Connection指名 header、host和不可靠的content-length。NEXT_PUBLIC_LANGGRAPH_BASE_URL时,不暴露同源 stream proxy 到内部 gateway。测试
新增/更新单测覆盖:
Cache-Control保持 upstream 原值;upstream 没有时代理不自行生成。Connection指名的 hop-by-hop headers 会在请求和响应两侧被剥离。HEAD/OPTIONS不附带 body 或duplex。DEER_FLOW_INTERNAL_GATEWAY_BASE_URL回退到默认 gateway URL。NEXT_PUBLIC_LANGGRAPH_BASE_URL配置后,同源 stream proxy 不继续映射到内部 gateway。next.config.js使用 fallback rewrites,让 stream App Route 优先命中,同时普通/api/langgraph/*仍回落到 gateway。本地已执行:
pnpm.cmd formatpnpm.cmd lint,仅有既有 hook dependency warnings,无 errorpnpm.cmd typecheckpnpm.cmd testBETTER_AUTH_SECRET=local-dev-secret pnpm.cmd build额外验证:
OPTIONS响应命中 Route Handler,并带有X-Accel-Buffering。/api/langgraph/threads不带X-Accel-Buffering,确认仍走 fallback rewrite。