mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-13 18:32:12 +03:00
Compare commits
1 Commits
fix/12577-
...
fix/12783-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a37c39ef73 |
@@ -35,16 +35,24 @@ export function resolveOpencodeTarget(opts = {}) {
|
||||
baseUrl = `http://localhost:${Number(opts.port ?? process.env.PORT ?? 20128) || 20128}`;
|
||||
}
|
||||
|
||||
// Precedence: explicit --api-key flag > OMNIROUTE_API_KEY env var > active
|
||||
// context's management token. A context's accessToken/apiKey is a CLI
|
||||
// management credential (oma_live_...) with no /v1/* inference scope — it
|
||||
// must never silently outrank a real inference key the caller supplied
|
||||
// either as a flag or via the ambient env var (mirrors the explicit >
|
||||
// ambient-env > context precedence documented in bin/cli/api.mjs's
|
||||
// buildHeaders()). Only fall back to the context token when neither an
|
||||
// explicit flag nor the env var is set.
|
||||
let apiKey = opts.apiKey ?? opts["api-key"];
|
||||
if (!apiKey) apiKey = process.env.OMNIROUTE_API_KEY || "";
|
||||
if (!apiKey) {
|
||||
try {
|
||||
const c = resolveActiveContext(opts.context ?? process.env.OMNIROUTE_CONTEXT);
|
||||
apiKey = c?.accessToken || c?.apiKey;
|
||||
apiKey = c?.accessToken || c?.apiKey || "";
|
||||
} catch {
|
||||
/* no context auth */
|
||||
}
|
||||
}
|
||||
if (!apiKey) apiKey = process.env.OMNIROUTE_API_KEY || "";
|
||||
return { baseUrl: baseUrl.replace(/\/+$/, ""), apiKey };
|
||||
}
|
||||
|
||||
@@ -177,8 +185,17 @@ export function registerSetupOpencode(program) {
|
||||
"--allow-container-write",
|
||||
"Write even when the target is inside a container and not mounted from the host"
|
||||
)
|
||||
.action(async (opts) => {
|
||||
const code = await runSetupOpencodeCommand(opts);
|
||||
.action(async (opts, cmd) => {
|
||||
// Commander parses the ancestor program's own global --api-key option
|
||||
// (bin/cli/program.mjs, bound to .env("OMNIROUTE_API_KEY")) against any
|
||||
// occurrence of the flag in argv, so it wins the value even when the
|
||||
// user typed --api-key AFTER `setup-opencode` — this local option's own
|
||||
// `opts.apiKey` never sees it. cmd.optsWithGlobals() resolves to the
|
||||
// correct value either way ("globals overwrite locals" is exactly the
|
||||
// outcome we want here, since the global option is where the value
|
||||
// always actually lands).
|
||||
const resolvedOpts = { ...opts, apiKey: cmd.optsWithGlobals().apiKey ?? opts.apiKey };
|
||||
const code = await runSetupOpencodeCommand(resolvedOpts);
|
||||
if (code !== 0) process.exit(code);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
@@ -0,0 +1 @@
|
||||
- fix(cli): setup-opencode no longer sends an active context's management token to `/v1/models` when `--api-key`/`OMNIROUTE_API_KEY` is supplied — an explicit flag or the env var now always outranks the context's token, and the flag itself is no longer swallowed by the parent program's global `--api-key` option (#12783)
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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");
|
||||
});
|
||||
121
tests/unit/repro-12783-setup-opencode-apikey.test.ts
Normal file
121
tests/unit/repro-12783-setup-opencode-apikey.test.ts
Normal file
@@ -0,0 +1,121 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { test } from "node:test";
|
||||
|
||||
import { resolveOpencodeTarget } from "../../bin/cli/commands/setup-opencode.mjs";
|
||||
|
||||
/** Point OMNIROUTE_CONTEXT config resolution at an isolated, throwaway DATA_DIR. */
|
||||
function withIsolatedContext(contextConfig, fn) {
|
||||
const dir = mkdtempSync(join(tmpdir(), "omniroute-setup-opencode-test-"));
|
||||
const originalDataDir = process.env.DATA_DIR;
|
||||
process.env.DATA_DIR = dir;
|
||||
writeFileSync(
|
||||
join(dir, "config.json"),
|
||||
JSON.stringify({
|
||||
version: 1,
|
||||
currentContext: "remote",
|
||||
contexts: { remote: contextConfig },
|
||||
})
|
||||
);
|
||||
try {
|
||||
return fn();
|
||||
} finally {
|
||||
if (originalDataDir === undefined) delete process.env.DATA_DIR;
|
||||
else process.env.DATA_DIR = originalDataDir;
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
function withEnvApiKey(value, fn) {
|
||||
const original = process.env.OMNIROUTE_API_KEY;
|
||||
if (value === undefined) delete process.env.OMNIROUTE_API_KEY;
|
||||
else process.env.OMNIROUTE_API_KEY = value;
|
||||
try {
|
||||
return fn();
|
||||
} finally {
|
||||
if (original === undefined) delete process.env.OMNIROUTE_API_KEY;
|
||||
else process.env.OMNIROUTE_API_KEY = original;
|
||||
}
|
||||
}
|
||||
|
||||
test("setup-opencode: --api-key typed AFTER the subcommand name is not stolen by the parent program's global option", async () => {
|
||||
const { createProgram } = await import("../../bin/cli/program.mjs");
|
||||
const program = createProgram();
|
||||
const setupOpencode = program.commands.find((c) => c.name() === "setup-opencode");
|
||||
assert.ok(setupOpencode, "setup-opencode subcommand must be registered");
|
||||
|
||||
let capturedApiKey;
|
||||
setupOpencode._actionHandler = null; // avoid the real network-calling action
|
||||
setupOpencode.action((opts, cmd) => {
|
||||
capturedApiKey = cmd.optsWithGlobals().apiKey ?? opts.apiKey;
|
||||
});
|
||||
|
||||
await program.parseAsync(
|
||||
[
|
||||
"node",
|
||||
"omniroute",
|
||||
"setup-opencode",
|
||||
"--remote",
|
||||
"http://100.64.0.1:20128",
|
||||
"--api-key",
|
||||
"sk-TESTKEY123",
|
||||
],
|
||||
{ from: "node" }
|
||||
);
|
||||
|
||||
assert.equal(
|
||||
capturedApiKey,
|
||||
"sk-TESTKEY123",
|
||||
"the CLI-supplied --api-key value must reach the setup-opencode action handler"
|
||||
);
|
||||
});
|
||||
|
||||
test("resolveOpencodeTarget: (a) explicit --api-key flag wins over an active context's management token", () => {
|
||||
withEnvApiKey(undefined, () => {
|
||||
withIsolatedContext(
|
||||
{ baseUrl: "http://100.64.0.1:20128", accessToken: "oma_live_CONTEXT_TOKEN" },
|
||||
() => {
|
||||
const { apiKey } = resolveOpencodeTarget({ apiKey: "sk-FLAG", context: "remote" });
|
||||
assert.equal(apiKey, "sk-FLAG");
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
test("resolveOpencodeTarget: (b) OMNIROUTE_API_KEY env wins over an active context's management token when no flag is passed", () => {
|
||||
withEnvApiKey("sk-ENVKEY", () => {
|
||||
withIsolatedContext(
|
||||
{ baseUrl: "http://100.64.0.1:20128", accessToken: "oma_live_CONTEXT_TOKEN" },
|
||||
() => {
|
||||
const { apiKey } = resolveOpencodeTarget({ context: "remote" });
|
||||
assert.equal(apiKey, "sk-ENVKEY");
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
test("resolveOpencodeTarget: (c) the context's token is used only when neither a flag nor the env var is set", () => {
|
||||
withEnvApiKey(undefined, () => {
|
||||
withIsolatedContext(
|
||||
{ baseUrl: "http://100.64.0.1:20128", accessToken: "oma_live_CONTEXT_TOKEN" },
|
||||
() => {
|
||||
const { apiKey } = resolveOpencodeTarget({ context: "remote" });
|
||||
assert.equal(apiKey, "oma_live_CONTEXT_TOKEN");
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
test("resolveOpencodeTarget: falls back to '' when neither a flag, env var, nor a resolvable context is present", () => {
|
||||
withEnvApiKey(undefined, () => {
|
||||
withIsolatedContext({ baseUrl: "http://100.64.0.1:20128" }, () => {
|
||||
const { apiKey } = resolveOpencodeTarget({
|
||||
remote: "http://100.64.0.1:20128",
|
||||
context: "__no-such-context__",
|
||||
});
|
||||
assert.equal(apiKey, "");
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user