Skip to content

fix(frontend): proxy LangGraph SSE without buffering#2797

Open
LittleChenLiya wants to merge 6 commits into
bytedance:mainfrom
LittleChenLiya:fix/langgraph-sse-proxy-buffering
Open

fix(frontend): proxy LangGraph SSE without buffering#2797
LittleChenLiya wants to merge 6 commits into
bytedance:mainfrom
LittleChenLiya:fix/langgraph-sse-proxy-buffering

Conversation

@LittleChenLiya
Copy link
Copy Markdown
Contributor

@LittleChenLiya LittleChenLiya commented May 8, 2026

修复 #2785

问题

Windows 本地开发环境中,LangGraph SSE 通过 Next rewrites 转发到 gateway 时会被前端代理路径缓冲。表现为模型已经开始输出,但浏览器端要等较久后才一次性收到内容。

对比验证:

  • 直接请求 gateway:http://127.0.0.1:8001/api/threads/.../runs/stream 可以增量收到 SSE event。
  • 通过前端同源路径:http://localhost:3000/api/langgraph/.../stream 会出现 event 延迟集中到达。

这说明后端和模型本身可以流式输出,问题主要在前端 dev proxy 的 rewrite 路径上;不是单纯缺少 messages-tuple stream mode。

修改

  • 只为 LangGraph SSE endpoints 增加 App Router Route Handler:
    • /api/langgraph/runs/stream
    • /api/langgraph/threads/[threadId]/runs/stream
    • /api/langgraph/threads/[threadId]/runs/[runId]/stream
  • 这些 stream routes 直接映射到 gateway 对应 /api/.../stream,并直接返回 upstream response body,避免 SSE 被中间层缓冲。
  • 普通 /api/langgraph/* 不接管,继续通过 fallback rewrite 代理到 gateway,降低非流式 API 的副作用。
  • 只在 stream route 上设置 accept-encoding: identity
  • 处理必要的代理 header:剥离 hop-by-hop header、Connection 指名 header、host 和不可靠的 content-length
  • 配置 NEXT_PUBLIC_LANGGRAPH_BASE_URL 时,不暴露同源 stream proxy 到内部 gateway。

测试

新增/更新单测覆盖:

  • stateless run stream、新 run stream、join/resume stream 三条 SDK stream endpoint。
  • route handler 保留 upstream response body 的流式返回。
  • 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 format
  • pnpm.cmd lint,仅有既有 hook dependency warnings,无 error
  • pnpm.cmd typecheck
  • pnpm.cmd test
  • BETTER_AUTH_SECRET=local-dev-secret pnpm.cmd build

额外验证:

  • 本地 dev server 下,stream URL 的 OPTIONS 响应命中 Route Handler,并带有 X-Accel-Buffering
  • 普通 /api/langgraph/threads 不带 X-Accel-Buffering,确认仍走 fallback rewrite。

@LittleChenLiya LittleChenLiya force-pushed the fix/langgraph-sse-proxy-buffering branch 3 times, most recently from 3f368e5 to a4a6d0a Compare May 8, 2026 15:32
@LittleChenLiya LittleChenLiya marked this pull request as ready for review May 8, 2026 15:49
@WillemJiang WillemJiang requested a review from Copilot May 8, 2026 16:12
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread frontend/src/app/api/langgraph/_lib/stream-proxy.ts
@LittleChenLiya LittleChenLiya force-pushed the fix/langgraph-sse-proxy-buffering branch from a4a6d0a to aae3c14 Compare May 8, 2026 16:22
@LittleChenLiya LittleChenLiya force-pushed the fix/langgraph-sse-proxy-buffering branch from aae3c14 to 8e89f06 Compare May 13, 2026 14:31
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Comment thread frontend/next.config.js Outdated
Comment thread frontend/src/app/api/langgraph/_lib/stream-proxy.ts Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Comment thread frontend/src/app/api/langgraph/_lib/stream-proxy.ts Outdated
@LittleChenLiya LittleChenLiya requested a review from Copilot May 14, 2026 04:57
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated no new comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants