mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-11 17:32:35 +03:00
Compare commits
1 Commits
fix/12577-
...
fix/12196-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bdca029cbd |
1
changelog.d/fixes/12196-opencode-go-gpt56luna.md
Normal file
1
changelog.d/fixes/12196-opencode-go-gpt56luna.md
Normal file
@@ -0,0 +1 @@
|
||||
- fix(providers): route opencode-go/gpt-5.6-luna to /responses instead of /chat/completions (#12196)
|
||||
@@ -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.
|
||||
|
||||
@@ -250,6 +250,16 @@ export const opencode_goProvider: RegistryEntry = {
|
||||
supportedThinkingEfforts: ["none", "low", "high", "max"],
|
||||
targetFormat: "openai-responses",
|
||||
},
|
||||
// #12196: the Go upstream serves this model only on /responses —
|
||||
// /chat/completions 500s for it. github already declares the same model
|
||||
// id with targetFormat:"openai-responses" (see github/index.ts).
|
||||
{
|
||||
id: "gpt-5.6-luna",
|
||||
name: "GPT-5.6 Luna",
|
||||
supportsReasoning: true,
|
||||
targetFormat: "openai-responses",
|
||||
maxOutputTokens: 128000,
|
||||
},
|
||||
// Console Go free GLM-tier model (live-verified 2026-08-23): the upstream
|
||||
// rejects every reasoning_effort outside {low, high, max} whenever tools
|
||||
// are present — "[1210] This model always engages in thinking and cannot
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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");
|
||||
});
|
||||
33
tests/unit/issue-12196-opencode-go-gpt56luna.test.ts
Normal file
33
tests/unit/issue-12196-opencode-go-gpt56luna.test.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
|
||||
import { resolveOpencodeTargetFormat } from "../../open-sse/executors/opencode.ts";
|
||||
|
||||
// Issue #12196: opencode-go/gpt-5.6-luna is served by the Go upstream ONLY on
|
||||
// /responses — /chat/completions 500s for this model. The github provider
|
||||
// already declares targetFormat:"openai-responses" for the same model id, and
|
||||
// opencode-go already does the same for deepseek-v4-pro/deepseek-v4-flash on
|
||||
// this exact provider — but gpt-5.6-luna itself is missing from the
|
||||
// opencode-go registry, so getModelTargetFormat() falls through to null and
|
||||
// resolveOpencodeTargetFormat() defaults to "openai", which makes
|
||||
// OpencodeExecutor.buildUrl() post to /chat/completions instead of /responses.
|
||||
test("opencode-go/gpt-5.6-luna must resolve to the openai-responses target format", () => {
|
||||
const resolved = resolveOpencodeTargetFormat("opencode-go", "gpt-5.6-luna");
|
||||
assert.equal(
|
||||
resolved,
|
||||
"openai-responses",
|
||||
"opencode-go/gpt-5.6-luna resolved to '" +
|
||||
resolved +
|
||||
"' instead of 'openai-responses' — OpencodeExecutor.buildUrl() will post to " +
|
||||
"/chat/completions, which the Go upstream 500s on for this model (issue #12196)"
|
||||
);
|
||||
});
|
||||
|
||||
// Control: the sibling deepseek-v4-flash entry on the SAME opencode-go
|
||||
// provider already declares targetFormat:"openai-responses" and must keep
|
||||
// working — proves the assertion above isn't failing for an unrelated reason
|
||||
// (e.g. a broken import or alias resolution).
|
||||
test("control: opencode-go/deepseek-v4-flash already resolves to openai-responses", () => {
|
||||
const resolved = resolveOpencodeTargetFormat("opencode-go", "deepseek-v4-flash");
|
||||
assert.equal(resolved, "openai-responses");
|
||||
});
|
||||
Reference in New Issue
Block a user