billion-context 0.1.60 → 0.1.61
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +141 -33
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -47852,6 +47852,7 @@ function rejectLegacyRoute(key, value) {
|
|
|
47852
47852
|
// src/server.ts
|
|
47853
47853
|
import http from "http";
|
|
47854
47854
|
import fs6 from "fs";
|
|
47855
|
+
import { randomUUID as randomUUID2 } from "crypto";
|
|
47855
47856
|
|
|
47856
47857
|
// src/compress-settings.ts
|
|
47857
47858
|
function resolveContextLimitValue(raw, nativeLimit) {
|
|
@@ -50227,16 +50228,21 @@ TASK: The conversation segment below (messages ${startRef}\u2013${endRef}) must
|
|
|
50227
50228
|
clearTimer();
|
|
50228
50229
|
}
|
|
50229
50230
|
}
|
|
50231
|
+
var ABORTED_FAILURE = { kind: "aborted", detail: "the client disconnected during preflight compression" };
|
|
50230
50232
|
async function preflightCompress(deps, messages) {
|
|
50231
50233
|
const limit = deps.config.modelContextLimit;
|
|
50232
|
-
const result = { compressedRanges: 0, savedTokens: 0 };
|
|
50234
|
+
const result = { compressedRanges: 0, savedTokens: 0, payloadEstimate: estimateCoreMessages(messages) };
|
|
50233
50235
|
if (limit <= 0) return result;
|
|
50234
50236
|
const budget = Math.max(MIN_CHUNK_TOKENS, Math.floor(limit * CHUNK_FRACTION));
|
|
50235
50237
|
const minChars = deps.config.compress.minCompressRange;
|
|
50236
50238
|
let currentTokens = deps.session.stats.lastInputTokens;
|
|
50237
50239
|
let startTokens = -1;
|
|
50240
|
+
let failure;
|
|
50238
50241
|
for (let round = 0; round < MAX_PREFLIGHT_ROUNDS; round++) {
|
|
50239
|
-
if (deps.signal?.aborted)
|
|
50242
|
+
if (deps.signal?.aborted) {
|
|
50243
|
+
failure = ABORTED_FAILURE;
|
|
50244
|
+
break;
|
|
50245
|
+
}
|
|
50240
50246
|
const turn = deps.core.processTurn({
|
|
50241
50247
|
messages,
|
|
50242
50248
|
state: deps.session.state,
|
|
@@ -50246,19 +50252,29 @@ async function preflightCompress(deps, messages) {
|
|
|
50246
50252
|
});
|
|
50247
50253
|
deps.session.state = turn.state;
|
|
50248
50254
|
currentTokens = Math.max(deps.session.stats.lastInputTokens, estimateCoreMessages(turn.messages));
|
|
50255
|
+
result.payloadEstimate = estimateCoreMessages(turn.messages);
|
|
50249
50256
|
if (startTokens < 0) startTokens = currentTokens;
|
|
50250
50257
|
if (currentTokens < limit) break;
|
|
50251
50258
|
const ranges = viableRanges(turn.nudge?.compressibleRanges ?? []);
|
|
50252
|
-
if (ranges.length === 0)
|
|
50259
|
+
if (ranges.length === 0) {
|
|
50260
|
+
failure = { kind: "exhausted", detail: "no compressible ranges remain in the conversation" };
|
|
50261
|
+
break;
|
|
50262
|
+
}
|
|
50253
50263
|
const range = [...ranges].sort((a, b2) => refNum2(a.startRef) - refNum2(b2.startRef))[0];
|
|
50254
50264
|
const { refToIdx } = refMaps(messages, deps.session.state);
|
|
50255
50265
|
const startIdx = refToIdx.get(range.startRef);
|
|
50256
50266
|
const endIdx = refToIdx.get(range.endRef);
|
|
50257
|
-
if (startIdx === void 0 || endIdx === void 0 || startIdx > endIdx)
|
|
50267
|
+
if (startIdx === void 0 || endIdx === void 0 || startIdx > endIdx) {
|
|
50268
|
+
failure = { kind: "exhausted", detail: "the compressible range no longer resolves to payload messages" };
|
|
50269
|
+
break;
|
|
50270
|
+
}
|
|
50258
50271
|
let appliedThisRound = 0;
|
|
50259
50272
|
for (const [cs2, ce2] of splitChunks(messages, startIdx, endIdx, budget)) {
|
|
50260
50273
|
if (currentTokens < limit) break;
|
|
50261
|
-
if (deps.signal?.aborted)
|
|
50274
|
+
if (deps.signal?.aborted) {
|
|
50275
|
+
failure = ABORTED_FAILURE;
|
|
50276
|
+
break;
|
|
50277
|
+
}
|
|
50262
50278
|
const maps = refMaps(messages, deps.session.state);
|
|
50263
50279
|
const startRef = maps.idxToRef.get(cs2);
|
|
50264
50280
|
const endRef = maps.idxToRef.get(ce2);
|
|
@@ -50271,8 +50287,17 @@ async function preflightCompress(deps, messages) {
|
|
|
50271
50287
|
summary = await summarizeRange(deps, content, startRef, endRef);
|
|
50272
50288
|
} catch (err2) {
|
|
50273
50289
|
if (err2 instanceof UpstreamHttpError) {
|
|
50290
|
+
failure = {
|
|
50291
|
+
kind: "upstream",
|
|
50292
|
+
status: err2.status,
|
|
50293
|
+
detail: err2.status === 429 ? `the summarization call was rate-limited by the upstream (HTTP 429)` : `the summarization call was rejected by the upstream (HTTP ${err2.status})`
|
|
50294
|
+
};
|
|
50274
50295
|
deps.log("warn", `[preflight] summarization failed: HTTP ${err2.status} ${err2.body.slice(0, 200)}`);
|
|
50296
|
+
} else if (deps.signal?.aborted) {
|
|
50297
|
+
failure = ABORTED_FAILURE;
|
|
50298
|
+
deps.log("warn", `[preflight] summarization aborted: client disconnected`);
|
|
50275
50299
|
} else {
|
|
50300
|
+
failure = { kind: "upstream", detail: `the summarization call failed: ${String(err2)}` };
|
|
50276
50301
|
deps.log("warn", `[preflight] summarization failed: ${String(err2)}`);
|
|
50277
50302
|
}
|
|
50278
50303
|
break;
|
|
@@ -50297,10 +50322,19 @@ async function preflightCompress(deps, messages) {
|
|
|
50297
50322
|
appliedThisRound += 1;
|
|
50298
50323
|
result.compressedRanges += 1;
|
|
50299
50324
|
}
|
|
50300
|
-
if (appliedThisRound === 0)
|
|
50325
|
+
if (appliedThisRound === 0) {
|
|
50326
|
+
if (!failure) {
|
|
50327
|
+
failure = { kind: "exhausted", detail: "no range could be compressed (chunks below minCompressRange or the summarization responses were unusable)" };
|
|
50328
|
+
}
|
|
50329
|
+
break;
|
|
50330
|
+
}
|
|
50331
|
+
}
|
|
50332
|
+
if (!failure && currentTokens >= limit) {
|
|
50333
|
+
failure = { kind: "exhausted", detail: `the compress budget was exhausted after ${MAX_PREFLIGHT_ROUNDS} rounds` };
|
|
50301
50334
|
}
|
|
50302
50335
|
if (result.compressedRanges > 0) deps.session.stats.lastInputTokens = currentTokens;
|
|
50303
50336
|
result.savedTokens = Math.max(0, startTokens - currentTokens);
|
|
50337
|
+
if (currentTokens >= limit) result.failure = failure;
|
|
50304
50338
|
return result;
|
|
50305
50339
|
}
|
|
50306
50340
|
|
|
@@ -52146,8 +52180,15 @@ ${systemPrompt}` : systemPrompt;
|
|
|
52146
52180
|
} else if (type === "message_delta") {
|
|
52147
52181
|
const u2 = data.usage ?? {};
|
|
52148
52182
|
if (typeof u2.output_tokens === "number") roundOutput = u2.output_tokens;
|
|
52149
|
-
|
|
52150
|
-
|
|
52183
|
+
const input = typeof u2.input_tokens === "number" ? u2.input_tokens : void 0;
|
|
52184
|
+
const cached = typeof u2.cache_read_input_tokens === "number" ? u2.cache_read_input_tokens : void 0;
|
|
52185
|
+
if (input !== void 0 && input > 0 && cached !== void 0) {
|
|
52186
|
+
roundInput = input;
|
|
52187
|
+
roundCached = cached;
|
|
52188
|
+
} else {
|
|
52189
|
+
if (input !== void 0 && input > 0) roundInput = input;
|
|
52190
|
+
if (cached !== void 0 && cached > 0) roundCached = cached;
|
|
52191
|
+
}
|
|
52151
52192
|
const d = data.delta ?? {};
|
|
52152
52193
|
if (typeof d.stop_reason === "string") stopReason = d.stop_reason;
|
|
52153
52194
|
if (!usageYielded) {
|
|
@@ -54834,6 +54875,7 @@ async function decodeRequestBody(contentEncoding, input, maxOutputBytes) {
|
|
|
54834
54875
|
function bodyDumpEnabled() {
|
|
54835
54876
|
return process.env.ACP_DUMP_BODY === "1";
|
|
54836
54877
|
}
|
|
54878
|
+
var BILI_HOP_HEADER = "x-bili-hop";
|
|
54837
54879
|
function parseLauncherModelWindows(raw) {
|
|
54838
54880
|
if (!raw) return {};
|
|
54839
54881
|
try {
|
|
@@ -54852,6 +54894,21 @@ var LAUNCHER_MODEL_WINDOWS = parseLauncherModelWindows(process.env.BILI_LAUNCHER
|
|
|
54852
54894
|
function launcherContextWindow(model) {
|
|
54853
54895
|
return LAUNCHER_MODEL_WINDOWS[model];
|
|
54854
54896
|
}
|
|
54897
|
+
function anthropicBetaContextWindow(headers) {
|
|
54898
|
+
const raw = headers["anthropic-beta"];
|
|
54899
|
+
if (raw === void 0) return void 0;
|
|
54900
|
+
const list = Array.isArray(raw) ? raw.join(",") : raw;
|
|
54901
|
+
let best;
|
|
54902
|
+
for (const part of list.split(",")) {
|
|
54903
|
+
const m2 = /^context-(\d+)m\b/.exec(part.trim().toLowerCase());
|
|
54904
|
+
if (!m2) continue;
|
|
54905
|
+
const n = Number.parseInt(m2[1], 10);
|
|
54906
|
+
if (!Number.isFinite(n) || n <= 0) continue;
|
|
54907
|
+
const w2 = n * 1e6;
|
|
54908
|
+
if (best === void 0 || w2 > best) best = w2;
|
|
54909
|
+
}
|
|
54910
|
+
return best;
|
|
54911
|
+
}
|
|
54855
54912
|
function safeSessionId(id) {
|
|
54856
54913
|
return (id ?? "unknown").replace(/[^a-zA-Z0-9._-]/g, "_");
|
|
54857
54914
|
}
|
|
@@ -54929,6 +54986,7 @@ async function startServer(opts) {
|
|
|
54929
54986
|
const core = createCore();
|
|
54930
54987
|
const config = opts.kernelConfig;
|
|
54931
54988
|
const log2 = (level, msg) => logMsg(opts, level, msg);
|
|
54989
|
+
const instanceId = randomUUID2();
|
|
54932
54990
|
await initSessions();
|
|
54933
54991
|
loadConversations();
|
|
54934
54992
|
log2("info", `[persist] ${getStore().enabled ? "enabled" : "disabled"}`);
|
|
@@ -54938,7 +54996,7 @@ async function startServer(opts) {
|
|
|
54938
54996
|
void loadRegistry();
|
|
54939
54997
|
const server = http.createServer(async (req, res) => {
|
|
54940
54998
|
try {
|
|
54941
|
-
await handle(req, res, opts, core, config, log2);
|
|
54999
|
+
await handle(req, res, opts, core, config, log2, instanceId);
|
|
54942
55000
|
} catch (err2) {
|
|
54943
55001
|
const msg = String(err2);
|
|
54944
55002
|
log2("error", msg);
|
|
@@ -55063,7 +55121,7 @@ function adminTrustedHosts(bindHost, port) {
|
|
|
55063
55121
|
}
|
|
55064
55122
|
return set;
|
|
55065
55123
|
}
|
|
55066
|
-
async function handle(req, res, opts, core, config, log2) {
|
|
55124
|
+
async function handle(req, res, opts, core, config, log2, instanceId) {
|
|
55067
55125
|
const isAdminPath = req.url === "/__bili/" || req.url?.startsWith("/__bili/") || req.url === "/__acp/" || req.url?.startsWith("/__acp/");
|
|
55068
55126
|
if (isAdminPath && !isLoopbackAddress(req.socket.remoteAddress)) {
|
|
55069
55127
|
res.writeHead(403, { "content-type": "application/json" });
|
|
@@ -55224,6 +55282,11 @@ async function handle(req, res, opts, core, config, log2) {
|
|
|
55224
55282
|
res.end(JSON.stringify({ error: { type: "invalid_request", message: String(err2) } }));
|
|
55225
55283
|
return;
|
|
55226
55284
|
}
|
|
55285
|
+
const hopMarker = headerValue2(req, BILI_HOP_HEADER);
|
|
55286
|
+
if (hopMarker !== void 0) {
|
|
55287
|
+
const selfLoop = hopMarker === instanceId;
|
|
55288
|
+
log2("warn", selfLoop ? `[chain] inbound request carries THIS instance's ${BILI_HOP_HEADER} marker (${hopMarker}) \u2014 self-loop detected. Passing through without processing; check your upstream config (it may point back to this instance).` : `[chain] inbound request carries ${BILI_HOP_HEADER} from another bili instance (${hopMarker}) \u2014 bili\u2192bili chain detected. Passing through without processing to avoid double compression; keep only one bili instance in the chain.`);
|
|
55289
|
+
}
|
|
55227
55290
|
const countTokens = isCountTokensRequest(req.method ?? "GET", urlPath, bodyBuffer.length > 0);
|
|
55228
55291
|
let parsed = null;
|
|
55229
55292
|
if (protocol && bodyBuffer.length > 0) {
|
|
@@ -55271,13 +55334,14 @@ ${bodyBuffer.toString("utf8")}`);
|
|
|
55271
55334
|
return void 0;
|
|
55272
55335
|
}
|
|
55273
55336
|
})();
|
|
55337
|
+
const betaWindow = anthropicBetaContextWindow(req.headers);
|
|
55274
55338
|
const pluginWindow = pluginReportedContextWindow(req.headers);
|
|
55275
55339
|
const launcherWindow = launcherContextWindow(model);
|
|
55276
55340
|
const peekWindow = host ? peekRegistryContext(model, host) : void 0;
|
|
55277
55341
|
const configuredWindow = resolveConfiguredContextLimit(opts.routes, embeddedUrl, model);
|
|
55278
|
-
let native = pluginWindow ?? launcherWindow ?? peekWindow ?? configuredWindow ?? lookupContextLimit(model);
|
|
55342
|
+
let native = betaWindow ?? pluginWindow ?? launcherWindow ?? peekWindow ?? configuredWindow ?? lookupContextLimit(model);
|
|
55279
55343
|
const operatorWindowTuned = resolveCompress(opts.routes, embeddedUrl, model, opts.compress).modelContextLimit !== void 0;
|
|
55280
|
-
nativeFromFallback = !pluginWindow && !launcherWindow && !peekWindow && !configuredWindow && !operatorWindowTuned;
|
|
55344
|
+
nativeFromFallback = !betaWindow && !pluginWindow && !launcherWindow && !peekWindow && !configuredWindow && !operatorWindowTuned;
|
|
55281
55345
|
if (!native && host) {
|
|
55282
55346
|
native = await contextFromRegistry(model, host);
|
|
55283
55347
|
if (native) nativeFromFallback = false;
|
|
@@ -55287,7 +55351,7 @@ ${bodyBuffer.toString("utf8")}`);
|
|
|
55287
55351
|
}
|
|
55288
55352
|
}
|
|
55289
55353
|
let prepared = null;
|
|
55290
|
-
if (!opts.passthrough && protocol && parsed && typeof parsed === "object") {
|
|
55354
|
+
if (!opts.passthrough && hopMarker === void 0 && protocol && parsed && typeof parsed === "object") {
|
|
55291
55355
|
const sessionHeader = headerValue2(req, opts.sessionHeader);
|
|
55292
55356
|
let pluginAgent = pluginAgentHeader(req.headers);
|
|
55293
55357
|
let pluginConversation = pluginConversationHeader(req.headers);
|
|
@@ -55395,7 +55459,7 @@ ${bodyBuffer.toString("utf8")}`);
|
|
|
55395
55459
|
const runPrepare = () => countTokens ? prepareCountTokens(parsed, core, reqConfig, log2, session) : protocol === "anthropic" ? prepareAnthropic(parsed, req, opts, core, reqConfig, reqPrompts, log2, session, pluginMode) : protocol === "openai" ? prepareOpenai(parsed, req, opts, core, reqConfig, reqPrompts, log2, session, pluginMode) : responsesCompact ? prepareResponsesCompact(bodyBuffer, parsed, session) : prepareResponses(parsed, req, opts, core, reqConfig, reqPrompts, log2, session, responsesIdentity, pluginMode, upstreamOrigin);
|
|
55396
55460
|
prepared = runPrepare();
|
|
55397
55461
|
if (!countTokens && !responsesCompact) {
|
|
55398
|
-
|
|
55462
|
+
const outcome = await preflightCompressIfNeeded(
|
|
55399
55463
|
prepared,
|
|
55400
55464
|
runPrepare,
|
|
55401
55465
|
req,
|
|
@@ -55406,10 +55470,29 @@ ${bodyBuffer.toString("utf8")}`);
|
|
|
55406
55470
|
parsed.model,
|
|
55407
55471
|
route,
|
|
55408
55472
|
affinity,
|
|
55409
|
-
log2
|
|
55473
|
+
log2,
|
|
55474
|
+
instanceId
|
|
55410
55475
|
);
|
|
55476
|
+
if (isPreflightFailFast(outcome)) {
|
|
55477
|
+
if (outcome.respond && !res.headersSent && !res.destroyed) {
|
|
55478
|
+
res.writeHead(outcome.status, {
|
|
55479
|
+
"content-type": "application/json",
|
|
55480
|
+
...outcome.status === 503 ? { "retry-after": "30" } : {}
|
|
55481
|
+
});
|
|
55482
|
+
res.end(JSON.stringify({
|
|
55483
|
+
error: {
|
|
55484
|
+
type: "server_error",
|
|
55485
|
+
code: "preflight_compress_failed",
|
|
55486
|
+
message: outcome.message,
|
|
55487
|
+
retryable: outcome.retryable
|
|
55488
|
+
}
|
|
55489
|
+
}));
|
|
55490
|
+
}
|
|
55491
|
+
return;
|
|
55492
|
+
}
|
|
55493
|
+
prepared = outcome;
|
|
55411
55494
|
}
|
|
55412
|
-
await forward(req, res, opts, prepared.body, prepared, core, reqConfig, log2, route, affinity);
|
|
55495
|
+
await forward(req, res, opts, prepared.body, prepared, core, reqConfig, log2, route, instanceId, affinity);
|
|
55413
55496
|
if (prepared) {
|
|
55414
55497
|
rememberPluginMessages(sessionId, prepared.processedMessages, prepared.originalMessages, prepared.nudge);
|
|
55415
55498
|
}
|
|
@@ -55422,7 +55505,7 @@ ${bodyBuffer.toString("utf8")}`);
|
|
|
55422
55505
|
if (protocol === null && !opts.passthrough) {
|
|
55423
55506
|
log2("warn", `unrecognized path ${maskUrlsInText(req.url ?? "")} \u2014 not a known protocol (/chat/completions, /v1/messages, /responses, /responses/compact); forwarding unchanged`);
|
|
55424
55507
|
}
|
|
55425
|
-
await forward(req, res, opts, bodyBuffer, null, core, reqConfig, log2, route, void 0);
|
|
55508
|
+
await forward(req, res, opts, bodyBuffer, null, core, reqConfig, log2, route, instanceId, void 0);
|
|
55426
55509
|
}
|
|
55427
55510
|
}
|
|
55428
55511
|
var ACP_TAG_MARK = "<acp ";
|
|
@@ -55783,7 +55866,7 @@ function injectResponsesTool(tools, toolsToAdd = ACP_TOOLS_RESPONSES) {
|
|
|
55783
55866
|
const additions = toolsToAdd.filter((t) => !present.has(t.name));
|
|
55784
55867
|
return [...tools, ...additions];
|
|
55785
55868
|
}
|
|
55786
|
-
function buildForwardTarget(req, opts, route, affinity) {
|
|
55869
|
+
function buildForwardTarget(req, opts, route, affinity, hopMarker) {
|
|
55787
55870
|
const reqUrl = req.url ?? "";
|
|
55788
55871
|
const isAbsoluteUrl = /^https?:\/\//i.test(reqUrl);
|
|
55789
55872
|
const rewritten = route ? route.rewrittenUrl : isAbsoluteUrl ? reqUrl : opts.upstream + reqUrl;
|
|
@@ -55795,6 +55878,7 @@ function buildForwardTarget(req, opts, route, affinity) {
|
|
|
55795
55878
|
if (UPSTREAM_HOP_HEADERS.has(lower) || reqConnNamed.has(lower) || v2 === void 0) continue;
|
|
55796
55879
|
headers[k2] = Array.isArray(v2) ? v2.join(", ") : v2;
|
|
55797
55880
|
}
|
|
55881
|
+
if (hopMarker !== void 0) headers[BILI_HOP_HEADER] = hopMarker;
|
|
55798
55882
|
headers["host"] = new URL(upstreamUrl).host;
|
|
55799
55883
|
const betaKey = Object.keys(headers).find((h) => h.toLowerCase() === "x-codex-beta-features");
|
|
55800
55884
|
if (betaKey) {
|
|
@@ -55808,14 +55892,29 @@ function buildForwardTarget(req, opts, route, affinity) {
|
|
|
55808
55892
|
const proxyUrl = resolveProxy(opts.routes, opts.proxy, route?.rewrittenUrl ?? upstreamUrl, opts.proxyFallback);
|
|
55809
55893
|
return { upstreamUrl, headers, proxyUrl };
|
|
55810
55894
|
}
|
|
55811
|
-
|
|
55895
|
+
function isPreflightFailFast(outcome) {
|
|
55896
|
+
return "failFast" in outcome;
|
|
55897
|
+
}
|
|
55898
|
+
async function preflightCompressIfNeeded(prepared, runPrepare, req, res, opts, core, config, model, route, affinity, log2, instanceId) {
|
|
55812
55899
|
const session = prepared.session;
|
|
55813
55900
|
const limit = config.modelContextLimit;
|
|
55814
|
-
const
|
|
55901
|
+
const payloadEstimate = estimateCoreMessages(prepared.processedMessages);
|
|
55902
|
+
const tokenCount = Math.max(session.stats.lastInputTokens, payloadEstimate);
|
|
55815
55903
|
if (limit <= 0 || !model || tokenCount < limit) return prepared;
|
|
55816
|
-
|
|
55904
|
+
const failFast = (status2, detail, retryable) => {
|
|
55905
|
+
const message = `context ~${tokenCount} tokens exceeds the model window ${limit} (model=${model}) and preflight compression could not bring it under: ${detail}. The over-window payload was NOT forwarded.`;
|
|
55906
|
+
log2("error", `[${session.id}] preflight fail-fast ${status2} (retryable=${retryable}): ${message}`);
|
|
55907
|
+
return { failFast: true, status: status2, message, retryable, respond: !res.writableEnded };
|
|
55908
|
+
};
|
|
55909
|
+
if ((prepared.nudge?.compressibleRanges ?? []).length === 0) {
|
|
55910
|
+
if (payloadEstimate < limit) {
|
|
55911
|
+
log2("warn", `[${session.id}] preflight trigger fired on a stale baseline (~${tokenCount}) but the payload fits (~${payloadEstimate}/${limit}); forwarding as-is`);
|
|
55912
|
+
return prepared;
|
|
55913
|
+
}
|
|
55914
|
+
return failFast(502, "no part of the conversation is compressible (nothing left to fold)", false);
|
|
55915
|
+
}
|
|
55817
55916
|
log2("warn", `[${session.id}] context ${tokenCount} tokens exceeds model window ${limit} (model=${model}); preflight compressing before forward`);
|
|
55818
|
-
const { upstreamUrl, headers, proxyUrl } = buildForwardTarget(req, opts, route, affinity);
|
|
55917
|
+
const { upstreamUrl, headers, proxyUrl } = buildForwardTarget(req, opts, route, affinity, instanceId);
|
|
55819
55918
|
const clientAbort = new AbortController();
|
|
55820
55919
|
res.on("close", () => {
|
|
55821
55920
|
if (!res.writableEnded) clientAbort.abort();
|
|
@@ -55837,17 +55936,26 @@ async function preflightCompressIfNeeded(prepared, runPrepare, req, res, opts, c
|
|
|
55837
55936
|
},
|
|
55838
55937
|
prepared.originalMessages
|
|
55839
55938
|
);
|
|
55840
|
-
if (result.
|
|
55841
|
-
|
|
55842
|
-
|
|
55843
|
-
|
|
55844
|
-
|
|
55939
|
+
if (result.payloadEstimate < limit) {
|
|
55940
|
+
if (result.compressedRanges > 0) {
|
|
55941
|
+
log2("info", `[${session.id}] preflight compressed ${result.compressedRanges} range(s), ~${result.savedTokens} tokens saved (${tokenCount} \u2192 ${session.stats.lastInputTokens}) in ${Date.now() - started}ms; rebuilding payload`);
|
|
55942
|
+
const rebuilt = runPrepare();
|
|
55943
|
+
session.stats.requests -= 1;
|
|
55944
|
+
return rebuilt;
|
|
55945
|
+
}
|
|
55946
|
+
log2("warn", `[${session.id}] preflight made no progress but the payload fits (~${result.payloadEstimate}/${limit}); forwarding as-is`);
|
|
55947
|
+
return prepared;
|
|
55845
55948
|
}
|
|
55846
|
-
|
|
55847
|
-
|
|
55949
|
+
const f2 = result.failure;
|
|
55950
|
+
if (f2?.kind === "aborted") {
|
|
55951
|
+
log2("warn", `[${session.id}] preflight aborted (${f2.detail}); not forwarding`);
|
|
55952
|
+
return { failFast: true, status: 0, message: f2.detail, retryable: false, respond: false };
|
|
55953
|
+
}
|
|
55954
|
+
const status = f2?.kind === "upstream" && f2.status === 429 ? 503 : 502;
|
|
55955
|
+
return failFast(status, f2?.detail ?? "unknown reason", status === 503);
|
|
55848
55956
|
}
|
|
55849
|
-
async function forward(req, res, opts, body, prepared, core, config, log2, route, affinity) {
|
|
55850
|
-
const { upstreamUrl, headers, proxyUrl } = buildForwardTarget(req, opts, route, affinity);
|
|
55957
|
+
async function forward(req, res, opts, body, prepared, core, config, log2, route, instanceId, affinity) {
|
|
55958
|
+
const { upstreamUrl, headers, proxyUrl } = buildForwardTarget(req, opts, route, affinity, prepared !== null ? instanceId : void 0);
|
|
55851
55959
|
log2("info", `forward ${req.method} \u2192 ${maskUrlForLog(upstreamUrl)}`);
|
|
55852
55960
|
if (process.env.ACP_DEBUG && prepared) {
|
|
55853
55961
|
const sid = prepared.session.id;
|
|
@@ -60187,7 +60295,7 @@ function pluginStatusAll() {
|
|
|
60187
60295
|
}
|
|
60188
60296
|
|
|
60189
60297
|
// src/launcher.ts
|
|
60190
|
-
import { randomUUID as
|
|
60298
|
+
import { randomUUID as randomUUID3 } from "crypto";
|
|
60191
60299
|
import fs10 from "fs";
|
|
60192
60300
|
import net2 from "net";
|
|
60193
60301
|
import os5 from "os";
|
|
@@ -61167,7 +61275,7 @@ async function runLaunch(params, deps = {}) {
|
|
|
61167
61275
|
const dshAcpPatch = writeDshAcpPatch(dshHomeDir);
|
|
61168
61276
|
if (dshAcpPatch) clientArgs = dshArgsWithPatch(clientArgs, dshAcpPatch);
|
|
61169
61277
|
} else if (base === "codex") {
|
|
61170
|
-
const codexConversationId = injectMcp ?
|
|
61278
|
+
const codexConversationId = injectMcp ? randomUUID3() : void 0;
|
|
61171
61279
|
if (directUrl) {
|
|
61172
61280
|
env = { ...process.env, BILLION_CONTEXT_PROXY: origin };
|
|
61173
61281
|
if (codexConversationId) clientArgs = [...buildCodexMcpArgs(origin, codexConversationId), ...clientArgs];
|