billion-context 0.1.59-pr.303.152 → 0.1.59-pr.304.154

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 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) {
@@ -52129,15 +52130,8 @@ ${systemPrompt}` : systemPrompt;
52129
52130
  } else if (type === "message_delta") {
52130
52131
  const u2 = data.usage ?? {};
52131
52132
  if (typeof u2.output_tokens === "number") roundOutput = u2.output_tokens;
52132
- const input = typeof u2.input_tokens === "number" ? u2.input_tokens : void 0;
52133
- const cached = typeof u2.cache_read_input_tokens === "number" ? u2.cache_read_input_tokens : void 0;
52134
- if (input !== void 0 && input > 0 && cached !== void 0) {
52135
- roundInput = input;
52136
- roundCached = cached;
52137
- } else {
52138
- if (input !== void 0 && input > 0) roundInput = input;
52139
- if (cached !== void 0 && cached > 0) roundCached = cached;
52140
- }
52133
+ if (typeof u2.input_tokens === "number" && u2.input_tokens > 0) roundInput = u2.input_tokens;
52134
+ if (typeof u2.cache_read_input_tokens === "number" && u2.cache_read_input_tokens > 0) roundCached = u2.cache_read_input_tokens;
52141
52135
  const d = data.delta ?? {};
52142
52136
  if (typeof d.stop_reason === "string") stopReason = d.stop_reason;
52143
52137
  if (!usageYielded) {
@@ -54728,6 +54722,7 @@ async function decodeRequestBody(contentEncoding, input, maxOutputBytes) {
54728
54722
  function bodyDumpEnabled() {
54729
54723
  return process.env.ACP_DUMP_BODY === "1";
54730
54724
  }
54725
+ var BILI_HOP_HEADER = "x-bili-hop";
54731
54726
  function parseLauncherModelWindows(raw) {
54732
54727
  if (!raw) return {};
54733
54728
  try {
@@ -54823,6 +54818,7 @@ async function startServer(opts) {
54823
54818
  const core = createCore();
54824
54819
  const config = opts.kernelConfig;
54825
54820
  const log2 = (level, msg) => logMsg(opts, level, msg);
54821
+ const instanceId = randomUUID2();
54826
54822
  await initSessions();
54827
54823
  loadConversations();
54828
54824
  log2("info", `[persist] ${getStore().enabled ? "enabled" : "disabled"}`);
@@ -54832,7 +54828,7 @@ async function startServer(opts) {
54832
54828
  void loadRegistry();
54833
54829
  const server = http.createServer(async (req, res) => {
54834
54830
  try {
54835
- await handle(req, res, opts, core, config, log2);
54831
+ await handle(req, res, opts, core, config, log2, instanceId);
54836
54832
  } catch (err2) {
54837
54833
  const msg = String(err2);
54838
54834
  log2("error", msg);
@@ -54957,7 +54953,7 @@ function adminTrustedHosts(bindHost, port) {
54957
54953
  }
54958
54954
  return set;
54959
54955
  }
54960
- async function handle(req, res, opts, core, config, log2) {
54956
+ async function handle(req, res, opts, core, config, log2, instanceId) {
54961
54957
  const isAdminPath = req.url === "/__bili/" || req.url?.startsWith("/__bili/") || req.url === "/__acp/" || req.url?.startsWith("/__acp/");
54962
54958
  if (isAdminPath && !isLoopbackAddress(req.socket.remoteAddress)) {
54963
54959
  res.writeHead(403, { "content-type": "application/json" });
@@ -55118,6 +55114,11 @@ async function handle(req, res, opts, core, config, log2) {
55118
55114
  res.end(JSON.stringify({ error: { type: "invalid_request", message: String(err2) } }));
55119
55115
  return;
55120
55116
  }
55117
+ const hopMarker = headerValue2(req, BILI_HOP_HEADER);
55118
+ if (hopMarker !== void 0) {
55119
+ const selfLoop = hopMarker === instanceId;
55120
+ 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.`);
55121
+ }
55121
55122
  const countTokens = isCountTokensRequest(req.method ?? "GET", urlPath, bodyBuffer.length > 0);
55122
55123
  let parsed = null;
55123
55124
  if (protocol && bodyBuffer.length > 0) {
@@ -55181,7 +55182,7 @@ ${bodyBuffer.toString("utf8")}`);
55181
55182
  }
55182
55183
  }
55183
55184
  let prepared = null;
55184
- if (!opts.passthrough && protocol && parsed && typeof parsed === "object") {
55185
+ if (!opts.passthrough && hopMarker === void 0 && protocol && parsed && typeof parsed === "object") {
55185
55186
  const sessionHeader = headerValue2(req, opts.sessionHeader);
55186
55187
  let pluginAgent = pluginAgentHeader(req.headers);
55187
55188
  let pluginConversation = pluginConversationHeader(req.headers);
@@ -55281,10 +55282,11 @@ ${bodyBuffer.toString("utf8")}`);
55281
55282
  parsed.model,
55282
55283
  route,
55283
55284
  affinity,
55284
- log2
55285
+ log2,
55286
+ instanceId
55285
55287
  );
55286
55288
  }
55287
- await forward(req, res, opts, prepared.body, prepared, core, reqConfig, log2, route, affinity);
55289
+ await forward(req, res, opts, prepared.body, prepared, core, reqConfig, log2, route, instanceId, affinity);
55288
55290
  if (prepared) {
55289
55291
  rememberPluginMessages(sessionId, prepared.processedMessages, prepared.originalMessages, prepared.nudge);
55290
55292
  }
@@ -55297,7 +55299,7 @@ ${bodyBuffer.toString("utf8")}`);
55297
55299
  if (protocol === null && !opts.passthrough) {
55298
55300
  log2("warn", `unrecognized path ${maskUrlsInText(req.url ?? "")} \u2014 not a known protocol (/chat/completions, /v1/messages, /responses, /responses/compact); forwarding unchanged`);
55299
55301
  }
55300
- await forward(req, res, opts, bodyBuffer, null, core, reqConfig, log2, route, void 0);
55302
+ await forward(req, res, opts, bodyBuffer, null, core, reqConfig, log2, route, instanceId, void 0);
55301
55303
  }
55302
55304
  }
55303
55305
  var ACP_TAG_MARK = "<acp ";
@@ -55658,7 +55660,7 @@ function injectResponsesTool(tools, toolsToAdd = ACP_TOOLS_RESPONSES) {
55658
55660
  const additions = toolsToAdd.filter((t) => !present.has(t.name));
55659
55661
  return [...tools, ...additions];
55660
55662
  }
55661
- function buildForwardTarget(req, opts, route, affinity) {
55663
+ function buildForwardTarget(req, opts, route, affinity, hopMarker) {
55662
55664
  const reqUrl = req.url ?? "";
55663
55665
  const isAbsoluteUrl = /^https?:\/\//i.test(reqUrl);
55664
55666
  const rewritten = route ? route.rewrittenUrl : isAbsoluteUrl ? reqUrl : opts.upstream + reqUrl;
@@ -55670,6 +55672,7 @@ function buildForwardTarget(req, opts, route, affinity) {
55670
55672
  if (UPSTREAM_HOP_HEADERS.has(lower) || reqConnNamed.has(lower) || v2 === void 0) continue;
55671
55673
  headers[k2] = Array.isArray(v2) ? v2.join(", ") : v2;
55672
55674
  }
55675
+ if (hopMarker !== void 0) headers[BILI_HOP_HEADER] = hopMarker;
55673
55676
  headers["host"] = new URL(upstreamUrl).host;
55674
55677
  const betaKey = Object.keys(headers).find((h) => h.toLowerCase() === "x-codex-beta-features");
55675
55678
  if (betaKey) {
@@ -55683,14 +55686,14 @@ function buildForwardTarget(req, opts, route, affinity) {
55683
55686
  const proxyUrl = resolveProxy(opts.routes, opts.proxy, route?.rewrittenUrl ?? upstreamUrl, opts.proxyFallback);
55684
55687
  return { upstreamUrl, headers, proxyUrl };
55685
55688
  }
55686
- async function preflightCompressIfNeeded(prepared, runPrepare, req, res, opts, core, config, model, route, affinity, log2) {
55689
+ async function preflightCompressIfNeeded(prepared, runPrepare, req, res, opts, core, config, model, route, affinity, log2, instanceId) {
55687
55690
  const session = prepared.session;
55688
55691
  const limit = config.modelContextLimit;
55689
55692
  const tokenCount = Math.max(session.stats.lastInputTokens, estimateCoreMessages(prepared.processedMessages));
55690
55693
  if (limit <= 0 || !model || tokenCount < limit) return prepared;
55691
55694
  if ((prepared.nudge?.compressibleRanges ?? []).length === 0) return prepared;
55692
55695
  log2("warn", `[${session.id}] context ${tokenCount} tokens exceeds model window ${limit} (model=${model}); preflight compressing before forward`);
55693
- const { upstreamUrl, headers, proxyUrl } = buildForwardTarget(req, opts, route, affinity);
55696
+ const { upstreamUrl, headers, proxyUrl } = buildForwardTarget(req, opts, route, affinity, instanceId);
55694
55697
  const clientAbort = new AbortController();
55695
55698
  res.on("close", () => {
55696
55699
  if (!res.writableEnded) clientAbort.abort();
@@ -55721,8 +55724,8 @@ async function preflightCompressIfNeeded(prepared, runPrepare, req, res, opts, c
55721
55724
  log2("warn", `[${session.id}] preflight could not compress enough (~${result.savedTokens} tokens saved); forwarding as-is`);
55722
55725
  return prepared;
55723
55726
  }
55724
- async function forward(req, res, opts, body, prepared, core, config, log2, route, affinity) {
55725
- const { upstreamUrl, headers, proxyUrl } = buildForwardTarget(req, opts, route, affinity);
55727
+ async function forward(req, res, opts, body, prepared, core, config, log2, route, instanceId, affinity) {
55728
+ const { upstreamUrl, headers, proxyUrl } = buildForwardTarget(req, opts, route, affinity, prepared !== null ? instanceId : void 0);
55726
55729
  log2("info", `forward ${req.method} \u2192 ${maskUrlForLog(upstreamUrl)}`);
55727
55730
  if (process.env.ACP_DEBUG && prepared) {
55728
55731
  const sid = prepared.session.id;
@@ -60062,7 +60065,7 @@ function pluginStatusAll() {
60062
60065
  }
60063
60066
 
60064
60067
  // src/launcher.ts
60065
- import { randomUUID as randomUUID2 } from "crypto";
60068
+ import { randomUUID as randomUUID3 } from "crypto";
60066
60069
  import fs10 from "fs";
60067
60070
  import net2 from "net";
60068
60071
  import os5 from "os";
@@ -61042,7 +61045,7 @@ async function runLaunch(params, deps = {}) {
61042
61045
  const dshAcpPatch = writeDshAcpPatch(dshHomeDir);
61043
61046
  if (dshAcpPatch) clientArgs = dshArgsWithPatch(clientArgs, dshAcpPatch);
61044
61047
  } else if (base === "codex") {
61045
- const codexConversationId = injectMcp ? randomUUID2() : void 0;
61048
+ const codexConversationId = injectMcp ? randomUUID3() : void 0;
61046
61049
  if (directUrl) {
61047
61050
  env = { ...process.env, BILLION_CONTEXT_PROXY: origin };
61048
61051
  if (codexConversationId) clientArgs = [...buildCodexMcpArgs(origin, codexConversationId), ...clientArgs];