fix: make SSE buffer size configurable to prevent overflow during multi-round searches (#664) (#665)

* fix: make SSE buffer size configurable to prevent overflow during multi-round searches (Issue #664)

- Add NEXT_PUBLIC_MAX_STREAM_BUFFER_SIZE environment variable for frontend SSE stream buffer
- Default to 1MB for backward compatibility, users can increase to 5-10MB for large searches
- Enhance error message with actual buffer sizes and guidance on configuration
- Add validation schema in env.js with positive integer requirement
- Document configuration in .env.example with clear examples and use cases

* Apply suggestions from code review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Willem Jiang
2025-10-27 17:03:38 +08:00
committed by GitHub
parent 83f1334db0
commit ccd7535072
3 changed files with 25 additions and 2 deletions

View File

@@ -1,6 +1,8 @@
// Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
// SPDX-License-Identifier: MIT
import { env } from "~/env";
import { type StreamEvent } from "./StreamEvent";
export async function* fetchStream(
@@ -28,7 +30,8 @@ export async function* fetchStream(
try {
let buffer = "";
const MAX_BUFFER_SIZE = 1024 * 1024; // 1MB buffer size limit
// Use configurable buffer size from environment, default to 1MB (1048576 bytes)
const MAX_BUFFER_SIZE = env.NEXT_PUBLIC_MAX_STREAM_BUFFER_SIZE ?? (1024 * 1024);
while (true) {
const { done, value } = await reader.read();
@@ -47,7 +50,11 @@ export async function* fetchStream(
// Check buffer size to avoid memory overflow
if (buffer.length > MAX_BUFFER_SIZE) {
throw new Error("Buffer overflow - received too much data without proper event boundaries");
throw new Error(
`Buffer overflow - received ${(buffer.length / 1024 / 1024).toFixed(2)}MB of data without proper event boundaries. ` +
`Max buffer size is ${(MAX_BUFFER_SIZE / 1024 / 1024).toFixed(2)}MB. ` +
`You can increase this by setting NEXT_PUBLIC_MAX_STREAM_BUFFER_SIZE environment variable.`
);
}
let newlineIndex;