-
Notifications
You must be signed in to change notification settings - Fork 9.1k
fix(frontend): proxy LangGraph SSE without buffering #2797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LittleChenLiya
wants to merge
6
commits into
bytedance:main
Choose a base branch
from
LittleChenLiya:fix/langgraph-sse-proxy-buffering
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2bad24a
fix(frontend): proxy LangGraph SSE without buffering
LittleChenLiya 8c33a48
fix(frontend): scope LangGraph SSE proxy to stream routes
LittleChenLiya f7ef742
fix(frontend): trim public LangGraph URL checks
LittleChenLiya f5afea1
test(frontend): cover blank backend URL rewrites
LittleChenLiya 822ff42
test(frontend): cover stateless LangGraph stream proxy
LittleChenLiya 7d9817d
refactor(frontend): share internal gateway URL resolution
LittleChenLiya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import { type NextRequest } from "next/server"; | ||
|
|
||
| const DEFAULT_GATEWAY_BASE_URL = "http://127.0.0.1:8001"; | ||
|
|
||
| const HOP_BY_HOP_HEADERS = [ | ||
| "connection", | ||
| "keep-alive", | ||
| "proxy-authenticate", | ||
| "proxy-authorization", | ||
| "te", | ||
| "trailer", | ||
| "transfer-encoding", | ||
| "upgrade", | ||
| ] as const; | ||
|
|
||
| function getGatewayBaseUrl() { | ||
| const configured = process.env.DEER_FLOW_INTERNAL_GATEWAY_BASE_URL?.trim(); | ||
| return ( | ||
| configured && configured.length > 0 ? configured : DEFAULT_GATEWAY_BASE_URL | ||
| ).replace(/\/+$/, ""); | ||
| } | ||
|
LittleChenLiya marked this conversation as resolved.
Outdated
|
||
|
|
||
| function getConnectionHeaderNames(headers: Headers) { | ||
| return ( | ||
| headers | ||
| .get("connection") | ||
| ?.split(",") | ||
| .map((name) => name.trim().toLowerCase()) | ||
| .filter(Boolean) ?? [] | ||
| ); | ||
| } | ||
|
|
||
| function deleteHopByHopHeaders( | ||
| headers: Headers, | ||
| additionalNames: string[] = [], | ||
| ) { | ||
| for (const name of [...HOP_BY_HOP_HEADERS, ...additionalNames]) { | ||
| headers.delete(name); | ||
| } | ||
| } | ||
|
|
||
| function buildGatewayUrl(path: string[], search: string) { | ||
| const pathname = ["api", ...path.map(encodeURIComponent)].join("/"); | ||
| return `${getGatewayBaseUrl()}/${pathname}${search}`; | ||
| } | ||
|
|
||
| function buildHeaders(request: NextRequest) { | ||
| const headers = new Headers(request.headers); | ||
| deleteHopByHopHeaders(headers, getConnectionHeaderNames(headers)); | ||
| for (const name of ["host", "content-length"]) { | ||
| headers.delete(name); | ||
| } | ||
| headers.set("accept-encoding", "identity"); | ||
| return headers; | ||
| } | ||
|
|
||
| export async function proxyLangGraphStream( | ||
| request: NextRequest, | ||
| path: string[], | ||
| ) { | ||
| if (process.env.NEXT_PUBLIC_LANGGRAPH_BASE_URL) { | ||
|
LittleChenLiya marked this conversation as resolved.
Outdated
|
||
| return new Response(null, { status: 404 }); | ||
| } | ||
|
|
||
| const target = buildGatewayUrl(path, request.nextUrl.search); | ||
| const method = request.method.toUpperCase(); | ||
| const hasBody = !["GET", "HEAD", "OPTIONS"].includes(method); | ||
| const init: RequestInit & { duplex?: "half" } = { | ||
| method, | ||
| headers: buildHeaders(request), | ||
| redirect: "manual", | ||
| cache: "no-store", | ||
| signal: request.signal, | ||
| }; | ||
|
|
||
| if (hasBody) { | ||
| init.body = request.body; | ||
| init.duplex = "half"; | ||
| } | ||
|
|
||
| const upstream = await fetch(target, init); | ||
| const headers = new Headers(upstream.headers); | ||
|
LittleChenLiya marked this conversation as resolved.
|
||
| deleteHopByHopHeaders(headers, getConnectionHeaderNames(headers)); | ||
| headers.delete("content-length"); | ||
| headers.set("X-Accel-Buffering", "no"); | ||
|
|
||
| return new Response(upstream.body, { | ||
| status: upstream.status, | ||
| statusText: upstream.statusText, | ||
| headers, | ||
| }); | ||
| } | ||
27 changes: 27 additions & 0 deletions
27
frontend/src/app/api/langgraph/threads/[threadId]/runs/[runId]/stream/route.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import { type NextRequest } from "next/server"; | ||
|
|
||
| import { proxyLangGraphStream } from "@/app/api/langgraph/_lib/stream-proxy"; | ||
|
|
||
| export const runtime = "nodejs"; | ||
| export const dynamic = "force-dynamic"; | ||
|
|
||
| type RouteContext = { | ||
| params: Promise<{ threadId: string; runId: string }>; | ||
| }; | ||
|
|
||
| async function proxyRunJoinStream(request: NextRequest, context: RouteContext) { | ||
| // Keep resumable/join streams on the same direct proxy path as new run streams. | ||
| const { threadId, runId } = await context.params; | ||
| return proxyLangGraphStream(request, [ | ||
| "threads", | ||
| threadId, | ||
| "runs", | ||
| runId, | ||
| "stream", | ||
| ]); | ||
| } | ||
|
|
||
| export const GET = proxyRunJoinStream; | ||
| export const HEAD = proxyRunJoinStream; | ||
| export const POST = proxyRunJoinStream; | ||
| export const OPTIONS = proxyRunJoinStream; |
22 changes: 22 additions & 0 deletions
22
frontend/src/app/api/langgraph/threads/[threadId]/runs/stream/route.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { type NextRequest } from "next/server"; | ||
|
|
||
| import { proxyLangGraphStream } from "@/app/api/langgraph/_lib/stream-proxy"; | ||
|
|
||
| export const runtime = "nodejs"; | ||
| export const dynamic = "force-dynamic"; | ||
|
|
||
| type RouteContext = { | ||
| params: Promise<{ threadId: string }>; | ||
| }; | ||
|
|
||
| async function proxyRunStream(request: NextRequest, context: RouteContext) { | ||
| // Keep run streams out of Next rewrites because the dev proxy can buffer SSE | ||
| // events on Windows. Returning the upstream body directly preserves streaming. | ||
| const { threadId } = await context.params; | ||
| return proxyLangGraphStream(request, ["threads", threadId, "runs", "stream"]); | ||
| } | ||
|
|
||
| export const GET = proxyRunStream; | ||
| export const HEAD = proxyRunStream; | ||
| export const POST = proxyRunStream; | ||
| export const OPTIONS = proxyRunStream; |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.