Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(proxy): accept large inferred SSE events (#5213)

Tsai Yuanf3f2f27fb7ae
diff --git a/packages/anthropic-compat-proxy/src/server.test.ts b/packages/anthropic-compat-proxy/src/server.test.ts
index 905a0c4384..1375bef53a 100644
--- a/packages/anthropic-compat-proxy/src/server.test.ts
+++ b/packages/anthropic-compat-proxy/src/server.test.ts
@@ -4252,6 +4252,89 @@ describe('streaming response validity gate (#2242)', () => {
expect(upstream.bodies[1]).toContain('Remember the test code');
});
+ it('accepts a large complete first SSE event without Content-Type', async () => {
+ const created = JSON.stringify({
+ type: 'response.created',
+ response: { instructions: 'large-context-fixture '.repeat(12_000), output: [] },
+ });
+ const body = `event: response.created\ndata: ${created}\n\ndata: {"type":"response.completed"}\n\n`;
+ expect(Buffer.byteLength(body)).toBeGreaterThan(64 * 1024);
+ const upstream = await startFakeUpstream((_idx, _body, res) => {
+ res.writeHead(200); // Intentionally omit Content-Type for the Codex HTTP fallback.
+ res.write(body.slice(0, 80 * 1024));
+ setImmediate(() => res.end(body.slice(80 * 1024)));
+ });
+ upstreamClose = upstream.close;
+ proxy = await createAnthropicCompatProxy({ upstream: upstream.url });
+
+ const response = await fetch(`${proxy.url}/responses`, {
+ method: 'POST',
+ headers: { 'content-type': 'application/json' },
+ body: JSON.stringify({ model: 'test-model', stream: true }),
+ });
+ const responseText = await response.text();
+ expect(response.status).toBe(200);
+ expect(response.headers.get('content-type')).toBe('text/event-stream');
+ expect(responseText).toBe(body);
+ expect(upstream.bodies).toHaveLength(1);
+ });
+
+ it('bounds an unfinished inferred SSE event with its dedicated error code', async () => {
+ const body = `event: response.created\ndata: ${'x'.repeat(8 * 1024 * 1024)}`;
+ const upstream = await startFakeUpstream((_idx, _body, res) => {
+ res.writeHead(200); // Intentionally omit Content-Type.
+ res.end(body);
+ });
+ upstreamClose = upstream.close;
+ proxy = await createAnthropicCompatProxy({ upstream: upstream.url });
+
+ const result = await post(proxy.url, { model: 'test-model', stream: true });
+ expect(result.status).toBe(502);
+ expect(JSON.parse(result.text).error.code).toBe('sse_inference_limit_exceeded');
+ });
+
+ it('commits a complete inferred event before rejecting bytes after its cap', async () => {
+ const prefix = 'event: response.created\ndata: ';
+ const firstEvent = `${prefix}${'x'.repeat(8 * 1024 * 1024 - Buffer.byteLength(prefix) - 18)}\n\n`;
+ const remainder = 'data: {"type":"response.completed"}\n\n';
+ const upstream = await startFakeUpstream((_idx, _body, res) => {
+ res.writeHead(200); // Intentionally omit Content-Type.
+ res.end(`${firstEvent}${remainder}`);
+ });
+ upstreamClose = upstream.close;
+ proxy = await createAnthropicCompatProxy({ upstream: upstream.url });
+
+ const result = await post(proxy.url, { model: 'test-model', stream: true });
+ expect(result).toEqual({ status: 200, text: `${firstEvent}${remainder}` });
+ });
+
+ it('infers a complete event with mixed LF and CRLF event delimiters', async () => {
+ const body = 'event: response.created\ndata: {}\n\r\n';
+ const upstream = await startFakeUpstream((_idx, _body, res) => {
+ res.writeHead(200); // Intentionally omit Content-Type.
+ res.end(body);
+ });
+ upstreamClose = upstream.close;
+ proxy = await createAnthropicCompatProxy({ upstream: upstream.url });
+
+ const result = await post(proxy.url, { model: 'test-model', stream: true });
+ expect(result).toEqual({ status: 200, text: body });
+ });
+
+ it('still rejects a large unfinished inferred SSE event below its cap', async () => {
+ const body = `event: response.created\ndata: ${'x'.repeat(128 * 1024)}`;
+ const upstream = await startFakeUpstream((_idx, _body, res) => {
+ res.writeHead(200); // Intentionally omit Content-Type.
+ res.end(body);
+ });
+ upstreamClose = upstream.close;
+ proxy = await createAnthropicCompatProxy({ upstream: upstream.url });
+
+ const result = await post(proxy.url, { model: 'test-model', stream: true });
+ expect(result.status).toBe(502);
+ expect(JSON.parse(result.text).error.code).toBe('non_sse_stream_response');
+ });
+
it.each([
{ name: 'JSON without MIME', body: '{"ok":true}', headers: {} },
{ name: 'HTML containing an SSE line', body: '<html>\ndata: fake\n</html>', headers: {} },
diff --git a/packages/anthropic-compat-proxy/src/server.ts b/packages/anthropic-compat-proxy/src/server.ts
index feba88dbc4..e820b7835b 100644
--- a/packages/anthropic-compat-proxy/src/server.ts
+++ b/packages/anthropic-compat-proxy/src/server.ts
@@ -1562,9 +1562,12 @@ function forward(
// 失败语义(finishClientAfterUpstreamFailure → destroy),不补成正常结束。
// 只约束显式流式请求:非流式 JSON 响应照旧字节透传,零行为变化。
const gateStreamValidity = requestDeclaredStream && status >= 200 && status < 300;
- // 合法 SSE 的首个事件必然远早于此(Anthropic 首行即 event: message_start);
- // 上限只为封顶「持续输出无事件垃圾」时的内存与延迟。
+ // 合法 Anthropic SSE 的首个事件必然远早于此(首行即 event: message_start);
+ // 上限只为封顶「持续输出无事件垃圾」时的内存与延迟。缺 MIME 的 Responses
+ // 回退流必须等到完整事件才能确认,response.created 会回显长 instructions,
+ // 所以仅在已确认 SSE 前缀后使用独立且仍有界的首事件预算。
const STREAM_GATE_PENDING_CAP_BYTES = 64 * 1024;
+ const STREAM_GATE_INFERRED_EVENT_CAP_BYTES = 8 * 1024 * 1024;
const SSE_EVENT_MARKER_RE = /(^|\r?\n)(event|data):/;
const SSE_PREFIX_RE = /^\uFEFF?(?:(?:|:[^\r\n]*)\r?\n)*(?:event|data):/;
const SSE_DATA_FIELD_RE = /(?:^\uFEFF?|\r?\n)data:/;
@@ -1572,6 +1575,8 @@ function forward(
const pendingChunks: Buffer[] = [];
let pendingBytes = 0;
let pendingText = '';
+ let inferredSseEventStart = 0;
+ let inferredSseBoundarySearchOffset = 0;
const commitStreamResponse = (): void => {
if (streamGateCommitted || upstreamFailureHandled || clientAborted || clientRes.destroyed) return;
// Resolve the final MIME before constructing an adapter, and construct it
@@ -1687,15 +1692,59 @@ function forward(
if (!isSse) {
if (canInferSse) {
pendingText += chunk.toString('utf8');
- // A field prefix alone cannot dispatch an event: keep buffering until
- // a data-containing block ends in a blank line, including across chunks.
- const completeEvents = pendingText.split(/\r?\n\r?\n/);
- completeEvents.pop(); // The final block has no terminating blank line yet.
- if (SSE_PREFIX_RE.test(pendingText) && completeEvents.some((event) => SSE_DATA_FIELD_RE.test(event))) {
- respHeaders['content-type'] = 'text/event-stream';
- commitStreamResponse();
+ // Fast-path the common first field. SSE_PREFIX_RE also admits BOM and
+ // comment preambles before that field.
+ const hasSsePrefix = pendingText.startsWith('event:')
+ || pendingText.startsWith('data:')
+ || SSE_PREFIX_RE.test(pendingText);
+ // A field prefix alone cannot dispatch an event: wait for a
+ // data-containing block to end in a blank line, including across
+ // chunks. Scan only newly appended text so a large valid first
+ // event cannot turn into repeated whole-buffer work.
+ if (hasSsePrefix) {
+ while (true) {
+ const boundary = [
+ { value: '\n\n', offset: pendingText.indexOf('\n\n', inferredSseBoundarySearchOffset) },
+ { value: '\n\r\n', offset: pendingText.indexOf('\n\r\n', inferredSseBoundarySearchOffset) },
+ { value: '\r\n\n', offset: pendingText.indexOf('\r\n\n', inferredSseBoundarySearchOffset) },
+ { value: '\r\n\r\n', offset: pendingText.indexOf('\r\n\r\n', inferredSseBoundarySearchOffset) },
+ ].filter((candidate) => candidate.offset !== -1)
+ .sort((left, right) => left.offset - right.offset)[0];
+ if (!boundary) {
+ // Retain three trailing characters to catch a delimiter
+ // split across chunks (\n\n or \r\n\r\n).
+ inferredSseBoundarySearchOffset = Math.max(
+ inferredSseEventStart,
+ pendingText.length - 3,
+ );
+ break;
+ }
+ const completeEvent = pendingText.slice(inferredSseEventStart, boundary.offset);
+ inferredSseEventStart = boundary.offset + boundary.value.length;
+ inferredSseBoundarySearchOffset = inferredSseEventStart;
+ if (SSE_DATA_FIELD_RE.test(completeEvent)) {
+ respHeaders['content-type'] = 'text/event-stream';
+ commitStreamResponse();
+ return;
+ }
+ }
+ }
+ const pendingCap = hasSsePrefix
+ ? STREAM_GATE_INFERRED_EVENT_CAP_BYTES
+ : STREAM_GATE_PENDING_CAP_BYTES;
+ if (pendingBytes > pendingCap) {
+ const code = hasSsePrefix
+ ? 'sse_inference_limit_exceeded'
+ : 'non_sse_stream_response';
+ upstreamResponseTerminal = 'error';
+ observerError(new Error(`invalid streaming response (${code})`));
+ rejectInvalidStreamResponse(code, { bytes: totalBytes });
+ upstreamReq.destroy(new Error(`invalid streaming response (${code})`));
return;
}
+ // A valid prefix is waiting only for the terminator of its first data
+ // event; do not also apply the generic non-SSE 64 KiB limit below.
+ if (hasSsePrefix) return;
}
// 非 SSE 2xx:留在门控里等 end 统一转 502(有界缓冲做 errorType 诊断);
// 超上限说明上游在持续输出非流式字节,立刻转 502 并切断上游。