mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-12 09:52:59 +03:00
Compare commits
1 Commits
fix/12577-
...
fix/12251-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6569b5ccf6 |
1
changelog.d/fixes/12251-extra-upstream-headers-delete.md
Normal file
1
changelog.d/fixes/12251-extra-upstream-headers-delete.md
Normal file
@@ -0,0 +1 @@
|
||||
- fix(dashboard): allow deleting the last extra-upstream-header row even when invalid (#12251)
|
||||
@@ -1 +0,0 @@
|
||||
- fix(sse): cap HuggingChat NDJSON body size and bound the read loop with the fetch timeout so a stalled or hostile upstream cannot buffer unbounded memory (#12577)
|
||||
@@ -55,14 +55,6 @@ export const SSE_HEARTBEAT_INTERVAL_MS = upstreamTimeouts.sseHeartbeatIntervalMs
|
||||
// Defaults to FETCH_TIMEOUT_MS. Override with FETCH_BODY_TIMEOUT_MS env var.
|
||||
export const FETCH_BODY_TIMEOUT_MS = upstreamTimeouts.fetchBodyTimeoutMs;
|
||||
|
||||
// Hard byte cap on the HuggingChat NDJSON body accumulated by
|
||||
// open-sse/executors/huggingchat/jsonlStream.ts. Prevents a stalled/hostile upstream that
|
||||
// never emits a terminal `finalAnswer` / `status: finished` marker from buffering
|
||||
// indefinitely (#12577). Sized generously for legitimate long completions while staying
|
||||
// well below a heap-exhausting size — mirrors the readCappedBuffer/readBodyCapped pattern
|
||||
// already used by veoaifree-web.ts and context7-fetch.ts.
|
||||
export const HUGGINGCHAT_MAX_BODY_BYTES = 4 * 1024 * 1024;
|
||||
|
||||
// Provider configurations
|
||||
// OAuth credentials read from env vars with hardcoded fallbacks for backward compatibility.
|
||||
// Use provider-credentials.json or env vars to override in production.
|
||||
|
||||
@@ -538,7 +538,7 @@ export class HuggingChatExecutor extends BaseExecutor {
|
||||
resolvedModel,
|
||||
id,
|
||||
created,
|
||||
combinedSignal,
|
||||
signal,
|
||||
streamCancellationController.signal
|
||||
);
|
||||
|
||||
@@ -626,7 +626,7 @@ export class HuggingChatExecutor extends BaseExecutor {
|
||||
|
||||
let fullText: string;
|
||||
try {
|
||||
fullText = await readJsonlResponse(upstreamResponse.body, combinedSignal);
|
||||
fullText = await readJsonlResponse(upstreamResponse.body, signal);
|
||||
} catch (err) {
|
||||
if (!(err instanceof HuggingChatStreamError)) throw err;
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
// Pure JSONL stream translation (HuggingChat NDJSON -> OpenAI SSE). Verbatim from huggingchat.ts.
|
||||
|
||||
import { HUGGINGCHAT_MAX_BODY_BYTES } from "../../config/constants.ts";
|
||||
|
||||
const MAX_BODY_EXCEEDED_MESSAGE =
|
||||
"HuggingChat response exceeded the maximum supported size before completing";
|
||||
|
||||
export class HuggingChatStreamError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
@@ -79,23 +74,15 @@ export async function* streamJsonlToOpenAi(
|
||||
id: string,
|
||||
created: number,
|
||||
signal?: AbortSignal | null,
|
||||
cancellationSignal?: AbortSignal | null,
|
||||
maxBytes: number = HUGGINGCHAT_MAX_BODY_BYTES
|
||||
cancellationSignal?: AbortSignal | null
|
||||
): AsyncGenerator<string> {
|
||||
const reader = body.getReader();
|
||||
const unbindReaderCancellation = bindReaderCancellation(reader, cancellationSignal);
|
||||
// Also bind the plain `signal` so an already-in-flight `reader.read()` unblocks the
|
||||
// instant it aborts, instead of only being noticed the next time the loop polls
|
||||
// `signal?.aborted` (#12577 — a stalled upstream can otherwise leave the read
|
||||
// suspended forever even once a caller-supplied timeout signal has fired).
|
||||
const unbindSignalCancellation = bindReaderCancellation(reader, signal);
|
||||
const decoder = new TextDecoder();
|
||||
let buffer = "";
|
||||
let emittedRole = false;
|
||||
let fullText = "";
|
||||
let finished = false;
|
||||
let totalBytes = 0;
|
||||
let exceededCap = false;
|
||||
|
||||
try {
|
||||
while (true) {
|
||||
@@ -104,13 +91,6 @@ export async function* streamJsonlToOpenAi(
|
||||
const { value, done } = await reader.read();
|
||||
if (done) break;
|
||||
|
||||
totalBytes += value.byteLength;
|
||||
if (totalBytes > maxBytes) {
|
||||
exceededCap = true;
|
||||
cancelReader(reader);
|
||||
break;
|
||||
}
|
||||
|
||||
buffer += decoder.decode(value, { stream: true });
|
||||
|
||||
const lines = buffer.split("\n");
|
||||
@@ -183,7 +163,7 @@ export async function* streamJsonlToOpenAi(
|
||||
if (finished) break;
|
||||
}
|
||||
|
||||
if (!finished && !exceededCap && buffer.trim()) {
|
||||
if (!finished && buffer.trim()) {
|
||||
const parsed = parseJsonlLine(buffer.trim());
|
||||
if (parsed.error) {
|
||||
throw new HuggingChatStreamError(parsed.error);
|
||||
@@ -210,26 +190,9 @@ export async function* streamJsonlToOpenAi(
|
||||
}
|
||||
} finally {
|
||||
unbindReaderCancellation();
|
||||
unbindSignalCancellation();
|
||||
reader.releaseLock();
|
||||
}
|
||||
|
||||
if (exceededCap) {
|
||||
yield sseChunk({
|
||||
id,
|
||||
object: "chat.completion.chunk",
|
||||
created,
|
||||
model,
|
||||
error: {
|
||||
message: MAX_BODY_EXCEEDED_MESSAGE,
|
||||
type: "upstream_error",
|
||||
code: "huggingchat_payload_too_large",
|
||||
},
|
||||
});
|
||||
yield "data: [DONE]\n\n";
|
||||
return;
|
||||
}
|
||||
|
||||
if (!signal?.aborted && !cancellationSignal?.aborted) {
|
||||
yield sseChunk({
|
||||
id,
|
||||
@@ -246,19 +209,12 @@ export async function* streamJsonlToOpenAi(
|
||||
|
||||
export async function readJsonlResponse(
|
||||
body: ReadableStream<Uint8Array>,
|
||||
signal?: AbortSignal | null,
|
||||
maxBytes: number = HUGGINGCHAT_MAX_BODY_BYTES
|
||||
signal?: AbortSignal | null
|
||||
): Promise<string> {
|
||||
const reader = body.getReader();
|
||||
// Bind the signal so an already-in-flight `reader.read()` unblocks the instant it
|
||||
// aborts, instead of only being noticed the next time the loop polls `signal?.aborted`
|
||||
// (#12577 — a stalled upstream can otherwise leave the read suspended forever even
|
||||
// once a caller-supplied timeout signal has fired).
|
||||
const unbindSignalCancellation = bindReaderCancellation(reader, signal);
|
||||
const decoder = new TextDecoder();
|
||||
let buffer = "";
|
||||
let fullText = "";
|
||||
let totalBytes = 0;
|
||||
|
||||
try {
|
||||
while (true) {
|
||||
@@ -267,12 +223,6 @@ export async function readJsonlResponse(
|
||||
const { value, done } = await reader.read();
|
||||
if (done) break;
|
||||
|
||||
totalBytes += value.byteLength;
|
||||
if (totalBytes > maxBytes) {
|
||||
cancelReader(reader);
|
||||
throw new HuggingChatStreamError(MAX_BODY_EXCEEDED_MESSAGE);
|
||||
}
|
||||
|
||||
buffer += decoder.decode(value, { stream: true });
|
||||
|
||||
const lines = buffer.split("\n");
|
||||
@@ -299,7 +249,6 @@ export async function readJsonlResponse(
|
||||
if (parsed.error) throw new HuggingChatStreamError(parsed.error);
|
||||
}
|
||||
} finally {
|
||||
unbindSignalCancellation();
|
||||
reader.releaseLock();
|
||||
}
|
||||
|
||||
|
||||
@@ -707,7 +707,10 @@ export default function ModelCompatPopover({
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
disabled={disabled || headerRows.length <= 1}
|
||||
disabled={
|
||||
disabled ||
|
||||
(headerRows.length <= 1 && !row.name.trim() && !row.value.trim())
|
||||
}
|
||||
onClick={() => removeHeaderRow(row.id)}
|
||||
title={t("compatUpstreamRemoveRow")}
|
||||
className="flex h-9 w-9 shrink-0 items-center justify-center rounded-lg border border-border/80 text-text-muted hover:bg-red-500/10 hover:text-red-600 dark:hover:text-red-400 disabled:opacity-30 disabled:hover:bg-transparent disabled:hover:text-text-muted transition-colors"
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
// @vitest-environment jsdom
|
||||
// Repro for #12251
|
||||
import React, { act } from "react";
|
||||
import { createRoot, type Root } from "react-dom/client";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import ModelCompatPopover from "../ModelCompatPopover";
|
||||
|
||||
vi.mock("next-intl", () => ({
|
||||
useTranslations: () => (key: string) => key,
|
||||
}));
|
||||
|
||||
let container: HTMLDivElement;
|
||||
let root: Root;
|
||||
|
||||
async function flushEffects() {
|
||||
await act(async () => {
|
||||
await Promise.resolve();
|
||||
});
|
||||
}
|
||||
|
||||
async function openPopover() {
|
||||
const trigger = container.querySelector("button") as HTMLButtonElement;
|
||||
await act(async () => trigger.click());
|
||||
await flushEffects();
|
||||
}
|
||||
|
||||
describe("ModelCompatPopover upstream headers — invalid single row cannot be deleted (#12251)", () => {
|
||||
beforeEach(() => {
|
||||
(
|
||||
globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boolean }
|
||||
).IS_REACT_ACT_ENVIRONMENT = true;
|
||||
container = document.createElement("div");
|
||||
document.body.appendChild(container);
|
||||
root = createRoot(container);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
act(() => root.unmount());
|
||||
document.body.innerHTML = "";
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
it("lets the user delete the invalid header via the delete icon when it is the ONLY row present", async () => {
|
||||
const onCompatPatch = vi.fn();
|
||||
|
||||
act(() => {
|
||||
root.render(
|
||||
<ModelCompatPopover
|
||||
t={(key) => key}
|
||||
providerId="openai"
|
||||
modelId="gpt-test"
|
||||
effectiveModelNormalize={() => false}
|
||||
effectiveModelPreserveDeveloper={() => true}
|
||||
getUpstreamHeadersRecord={() => ({
|
||||
"https://evil.example.com/callback": "some-secret-value",
|
||||
})}
|
||||
onCompatPatch={onCompatPatch}
|
||||
/>
|
||||
);
|
||||
});
|
||||
|
||||
await openPopover();
|
||||
|
||||
const nameInput = document.querySelector(
|
||||
'input[placeholder="compatUpstreamHeaderNamePlaceholder"]'
|
||||
) as HTMLInputElement;
|
||||
expect(nameInput).toBeTruthy();
|
||||
expect(nameInput.value).toBe("https://evil.example.com/callback");
|
||||
|
||||
const rowButtons = document.querySelectorAll('button[title="compatUpstreamRemoveRow"]');
|
||||
expect(rowButtons.length).toBe(1);
|
||||
|
||||
const removeButton = rowButtons[0] as HTMLButtonElement;
|
||||
|
||||
// EXPECTED (fixed) behavior: a populated row should always be removable via
|
||||
// its own delete icon, even when it is the only row.
|
||||
expect(removeButton.disabled).toBe(false);
|
||||
|
||||
await act(async () => removeButton.click());
|
||||
await flushEffects();
|
||||
|
||||
expect(onCompatPatch).toHaveBeenCalledWith("openai", { upstreamHeaders: {} });
|
||||
});
|
||||
|
||||
it("keeps the delete button disabled when the sole row is genuinely blank", async () => {
|
||||
const onCompatPatch = vi.fn();
|
||||
|
||||
act(() => {
|
||||
root.render(
|
||||
<ModelCompatPopover
|
||||
t={(key) => key}
|
||||
providerId="openai"
|
||||
modelId="gpt-test"
|
||||
effectiveModelNormalize={() => false}
|
||||
effectiveModelPreserveDeveloper={() => true}
|
||||
getUpstreamHeadersRecord={() => ({})}
|
||||
onCompatPatch={onCompatPatch}
|
||||
/>
|
||||
);
|
||||
});
|
||||
|
||||
await openPopover();
|
||||
|
||||
const rowButtons = document.querySelectorAll('button[title="compatUpstreamRemoveRow"]');
|
||||
expect(rowButtons.length).toBe(1);
|
||||
|
||||
const removeButton = rowButtons[0] as HTMLButtonElement;
|
||||
|
||||
// A blank sole row must stay non-deletable so the form always shows an
|
||||
// editable add-affordance.
|
||||
expect(removeButton.disabled).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -1,119 +0,0 @@
|
||||
// Regression test for issue #12577: HuggingChat NDJSON executor buffered the
|
||||
// upstream body with no byte ceiling and no timeout, so a stalled/hostile
|
||||
// upstream that never emits a terminal marker (`finalAnswer` / `status:
|
||||
// finished`) drove unbounded memory growth per in-flight request.
|
||||
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import {
|
||||
streamJsonlToOpenAi,
|
||||
readJsonlResponse,
|
||||
HuggingChatStreamError,
|
||||
} from "../../open-sse/executors/huggingchat/jsonlStream.ts";
|
||||
|
||||
const REASONABLE_CAP_BYTES = 2 * 1024 * 1024; // 2 MB
|
||||
const TEST_SAFETY_CEILING_BYTES = REASONABLE_CAP_BYTES * 4; // 8 MB
|
||||
|
||||
function makeUnboundedStream(): {
|
||||
body: ReadableStream<Uint8Array>;
|
||||
getTotalSent: () => number;
|
||||
getClosedBySafetyCeiling: () => boolean;
|
||||
} {
|
||||
const encoder = new TextEncoder();
|
||||
const tokenChunk = "a".repeat(32 * 1024); // 32 KB token payload per line
|
||||
const line = JSON.stringify({ type: "stream", token: tokenChunk }) + "\n";
|
||||
const lineBytes = encoder.encode(line).byteLength;
|
||||
|
||||
let totalSent = 0;
|
||||
let closedBySafetyCeiling = false;
|
||||
|
||||
const body = new ReadableStream<Uint8Array>({
|
||||
pull(controller) {
|
||||
if (totalSent >= TEST_SAFETY_CEILING_BYTES) {
|
||||
closedBySafetyCeiling = true;
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
controller.enqueue(encoder.encode(line));
|
||||
totalSent += lineBytes;
|
||||
// Deliberately never emit a finalAnswer/status:finished terminal marker.
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
body,
|
||||
getTotalSent: () => totalSent,
|
||||
getClosedBySafetyCeiling: () => closedBySafetyCeiling,
|
||||
};
|
||||
}
|
||||
|
||||
test("streamJsonlToOpenAi aborts once accumulated upstream body exceeds a size cap, instead of buffering forever", async () => {
|
||||
const { body, getTotalSent, getClosedBySafetyCeiling } = makeUnboundedStream();
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
let sawUpstreamErrorChunk = false;
|
||||
let bytesReceivedByConsumer = 0;
|
||||
|
||||
for await (const chunk of streamJsonlToOpenAi(
|
||||
body,
|
||||
"gpt-huggingchat",
|
||||
"id-1",
|
||||
0,
|
||||
undefined,
|
||||
undefined,
|
||||
REASONABLE_CAP_BYTES
|
||||
)) {
|
||||
bytesReceivedByConsumer += encoder.encode(chunk).byteLength;
|
||||
if (/upstream_error|too_large|payload.*exceed/i.test(chunk)) {
|
||||
sawUpstreamErrorChunk = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
assert.ok(
|
||||
sawUpstreamErrorChunk,
|
||||
`expected streamJsonlToOpenAi to abort with an upstream-error chunk once the ` +
|
||||
`accumulated body exceeded ~${REASONABLE_CAP_BYTES} bytes, but it kept consuming ` +
|
||||
`upstream data with no ceiling (sent ${getTotalSent()} bytes before the TEST's own ` +
|
||||
`safety ceiling stepped in: closedBySafetyCeiling=${getClosedBySafetyCeiling()}, ` +
|
||||
`bytesReceivedByConsumer=${bytesReceivedByConsumer}). This confirms issue #12577: ` +
|
||||
`no byte cap is enforced on the read loop.`
|
||||
);
|
||||
assert.ok(
|
||||
getTotalSent() < TEST_SAFETY_CEILING_BYTES,
|
||||
"expected the cap to trip well before the test's own 8MB safety ceiling"
|
||||
);
|
||||
});
|
||||
|
||||
test("readJsonlResponse throws a HuggingChatStreamError once accumulated upstream body exceeds a size cap", async () => {
|
||||
const { body, getClosedBySafetyCeiling } = makeUnboundedStream();
|
||||
|
||||
await assert.rejects(
|
||||
() => readJsonlResponse(body, undefined, REASONABLE_CAP_BYTES),
|
||||
(err: unknown) => err instanceof HuggingChatStreamError
|
||||
);
|
||||
assert.equal(
|
||||
getClosedBySafetyCeiling(),
|
||||
false,
|
||||
"expected the cap to trip well before the test's own 8MB safety ceiling"
|
||||
);
|
||||
});
|
||||
|
||||
test("streamJsonlToOpenAi terminates the read loop once an idle-timeout signal fires", async () => {
|
||||
const body = new ReadableStream<Uint8Array>({
|
||||
pull() {
|
||||
// Never enqueue and never close: simulates a stalled upstream connection
|
||||
// that sends nothing at all after headers, relying solely on the caller's
|
||||
// timeout signal (mirroring huggingchat.ts's combinedSignal) to unblock.
|
||||
},
|
||||
});
|
||||
|
||||
const idleTimeout = AbortSignal.timeout(50);
|
||||
const chunks: string[] = [];
|
||||
|
||||
for await (const chunk of streamJsonlToOpenAi(body, "gpt-huggingchat", "id-2", 0, idleTimeout)) {
|
||||
chunks.push(chunk);
|
||||
}
|
||||
|
||||
assert.ok(idleTimeout.aborted, "expected the idle-timeout signal to have fired");
|
||||
});
|
||||
Reference in New Issue
Block a user