billion-context 0.1.83-pr.528.346 → 0.1.83-pr.533.352

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
@@ -51446,7 +51446,7 @@ function reapOrphanBlocks(session, visible, deactivate) {
51446
51446
  }
51447
51447
 
51448
51448
  // src/instance.ts
51449
- import { createHash as createHash4, randomUUID as randomUUID3 } from "crypto";
51449
+ import { randomUUID as randomUUID3 } from "crypto";
51450
51450
  import fs3 from "fs";
51451
51451
  import path7 from "path";
51452
51452
  function isProxyInstanceFile(v2) {
@@ -51492,13 +51492,14 @@ function readProxyInstanceFile(file) {
51492
51492
  function instanceFilePath() {
51493
51493
  return path7.join(stateDir(), "proxy-origin");
51494
51494
  }
51495
- function atomicWriteJson(obj, filePath) {
51495
+ function atomicWriteInstanceFile(info, file) {
51496
+ const filePath = file ?? instanceFilePath();
51496
51497
  fs3.mkdirSync(path7.dirname(filePath), { recursive: true });
51497
51498
  const tempPath = `${filePath}.${process.pid}.${randomUUID3()}.tmp`;
51498
51499
  let descriptor;
51499
51500
  try {
51500
51501
  descriptor = fs3.openSync(tempPath, "wx", 420);
51501
- fs3.writeSync(descriptor, JSON.stringify(obj) + "\n", null, "utf8");
51502
+ fs3.writeSync(descriptor, JSON.stringify(info) + "\n", null, "utf8");
51502
51503
  fs3.fsyncSync(descriptor);
51503
51504
  fs3.closeSync(descriptor);
51504
51505
  descriptor = void 0;
@@ -51517,9 +51518,6 @@ function atomicWriteJson(obj, filePath) {
51517
51518
  throw error;
51518
51519
  }
51519
51520
  }
51520
- function atomicWriteInstanceFile(info, file) {
51521
- atomicWriteJson(info, file ?? instanceFilePath());
51522
- }
51523
51521
  function clearProxyInstanceFile(instanceId, file) {
51524
51522
  const filePath = file ?? instanceFilePath();
51525
51523
  const current = readProxyInstanceFile(filePath);
@@ -51539,99 +51537,44 @@ function isPidAlive(pid) {
51539
51537
  return err2.code === "EPERM";
51540
51538
  }
51541
51539
  }
51542
- function registryDirPath() {
51543
- return path7.join(stateDir(), "instances");
51544
- }
51545
- function legacyRegistryFilePath() {
51540
+ function registryFilePath() {
51546
51541
  return path7.join(stateDir(), "instances.json");
51547
51542
  }
51548
- var SAFE_NAME_RE = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
51549
- function safeRegistryName(instanceId) {
51550
- if (instanceId !== "." && instanceId !== ".." && instanceId.length <= 128 && SAFE_NAME_RE.test(instanceId)) {
51551
- return instanceId;
51552
- }
51553
- return createHash4("sha256").update(instanceId).digest("hex");
51554
- }
51555
- function registryEntryFile(instanceId) {
51556
- return path7.join(registryDirPath(), `${safeRegistryName(instanceId)}.json`);
51557
- }
51558
- function safeReadJson2(file) {
51559
- try {
51560
- return JSON.parse(fs3.readFileSync(file, "utf8"));
51561
- } catch {
51562
- return void 0;
51563
- }
51564
- }
51565
- function coerceEntry(value) {
51566
- if (!value || typeof value !== "object") return void 0;
51567
- const o = value;
51568
- if (typeof o.instanceId !== "string" || o.instanceId === "") return void 0;
51569
- return {
51570
- instanceId: o.instanceId,
51571
- pid: typeof o.pid === "number" ? o.pid : 0,
51572
- port: typeof o.port === "number" ? o.port : 0,
51573
- origin: typeof o.origin === "string" ? o.origin : "",
51574
- startedAt: typeof o.startedAt === "number" ? o.startedAt : 0
51575
- };
51576
- }
51577
- function readMarkerNames() {
51543
+ function registerInstanceAndWarn(entry, warn) {
51544
+ const file = registryFilePath();
51545
+ const entries = [];
51578
51546
  try {
51579
- return fs3.readdirSync(registryDirPath());
51580
- } catch {
51581
- return [];
51582
- }
51583
- }
51584
- function readAllRegistryEntries() {
51585
- const seen = /* @__PURE__ */ new Set();
51586
- const out = [];
51587
- for (const name of readMarkerNames()) {
51588
- if (!name.endsWith(".json")) continue;
51589
- const entry = coerceEntry(safeReadJson2(path7.join(registryDirPath(), name)));
51590
- if (entry && !seen.has(entry.instanceId)) {
51591
- seen.add(entry.instanceId);
51592
- out.push(entry);
51593
- }
51594
- }
51595
- const legacy = safeReadJson2(legacyRegistryFilePath());
51596
- if (legacy && Array.isArray(legacy.instances)) {
51597
- for (const raw of legacy.instances) {
51598
- const entry = coerceEntry(raw);
51599
- if (entry && !seen.has(entry.instanceId)) {
51600
- seen.add(entry.instanceId);
51601
- out.push(entry);
51547
+ const parsed = JSON.parse(fs3.readFileSync(file, "utf8"));
51548
+ if (Array.isArray(parsed.instances)) {
51549
+ for (const e of parsed.instances) {
51550
+ if (e && typeof e.instanceId === "string" && e.instanceId !== entry.instanceId && isPidAlive(e.pid)) {
51551
+ entries.push(e);
51552
+ }
51602
51553
  }
51603
51554
  }
51555
+ } catch {
51604
51556
  }
51605
- return out;
51606
- }
51607
- function reapDeadMarkers(ours) {
51608
- for (const name of readMarkerNames()) {
51609
- if (!name.endsWith(".json")) continue;
51610
- const file = path7.join(registryDirPath(), name);
51611
- const entry = coerceEntry(safeReadJson2(file));
51612
- if (!entry || entry.instanceId === ours || isPidAlive(entry.pid)) continue;
51613
- try {
51614
- fs3.unlinkSync(file);
51615
- } catch {
51616
- }
51617
- }
51618
- }
51619
- function registerInstanceAndWarn(entry, warn) {
51620
- const others = readAllRegistryEntries().filter((e) => e.instanceId !== entry.instanceId && isPidAlive(e.pid));
51621
- for (const other of others) {
51557
+ for (const other of entries) {
51622
51558
  warn(
51623
51559
  `another bili instance is running (pid ${other.pid}, ${other.origin}) \u2014 both processes will write the same sessions directory; stop one to avoid state pollution (#394)`
51624
51560
  );
51625
51561
  }
51626
- reapDeadMarkers(entry.instanceId);
51562
+ entries.push(entry);
51627
51563
  try {
51628
- atomicWriteJson(entry, registryEntryFile(entry.instanceId));
51564
+ fs3.mkdirSync(path7.dirname(file), { recursive: true });
51565
+ fs3.writeFileSync(file, JSON.stringify({ instances: entries }) + "\n");
51629
51566
  } catch {
51630
51567
  }
51631
51568
  }
51632
51569
  function unregisterInstance(instanceId) {
51570
+ const file = registryFilePath();
51633
51571
  try {
51634
- fs3.unlinkSync(registryEntryFile(instanceId));
51572
+ const parsed = JSON.parse(fs3.readFileSync(file, "utf8"));
51573
+ if (!Array.isArray(parsed.instances)) return;
51574
+ const kept = parsed.instances.filter(
51575
+ (e) => !(e && typeof e.instanceId === "string" && e.instanceId === instanceId) && isPidAlive(e.pid)
51576
+ );
51577
+ fs3.writeFileSync(file, JSON.stringify({ instances: kept }) + "\n");
51635
51578
  } catch {
51636
51579
  }
51637
51580
  }
@@ -52175,9 +52118,9 @@ async function* runCompressLoop(upstream, ctx, requestBody, requestOptions, adap
52175
52118
  }
52176
52119
 
52177
52120
  // src/util.ts
52178
- import { createHash as createHash5 } from "crypto";
52121
+ import { createHash as createHash4 } from "crypto";
52179
52122
  function hashId2(s3) {
52180
- return createHash5("sha256").update(s3, "utf8").digest("hex").slice(0, 16);
52123
+ return createHash4("sha256").update(s3, "utf8").digest("hex").slice(0, 16);
52181
52124
  }
52182
52125
  function isLoopbackAddress(addr) {
52183
52126
  return !!addr && (addr.startsWith("127.") || addr === "::1" || addr.startsWith("::ffff:127."));
@@ -54135,7 +54078,7 @@ function codexTurnIdentity(headers) {
54135
54078
  }
54136
54079
 
54137
54080
  // src/prefix-affinity.ts
54138
- import { createHash as createHash6 } from "crypto";
54081
+ import { createHash as createHash5 } from "crypto";
54139
54082
  var MIN_CANONICAL_BYTES = 24;
54140
54083
  var MAX_TRACKED_SESSIONS3 = 256;
54141
54084
  var TTL_MS2 = 7 * 24 * 60 * 60 * 1e3;
@@ -54157,7 +54100,7 @@ function sortKeys(value) {
54157
54100
  return value;
54158
54101
  }
54159
54102
  function sha256(text) {
54160
- return createHash6("sha256").update(text, "utf8").digest("hex");
54103
+ return createHash5("sha256").update(text, "utf8").digest("hex");
54161
54104
  }
54162
54105
  function hasUserMessage(messages) {
54163
54106
  return messages.some((m2) => !!m2 && typeof m2 === "object" && m2.role === "user");
@@ -63578,6 +63521,124 @@ function mergeOmpCompactionDisabledYaml(text) {
63578
63521
  lines.splice(compactionLine + 1, 0, `${" ".repeat(childIndent)}enabled: false`);
63579
63522
  return lines.join("\n");
63580
63523
  }
63524
+ function topLevelCompactionHeader(lines) {
63525
+ for (let i = 0; i < lines.length; i++) {
63526
+ const raw = lines[i];
63527
+ const trimmed = raw.trim();
63528
+ if (!trimmed || trimmed.startsWith("#")) continue;
63529
+ if (raw.length - raw.trimStart().length !== 0) continue;
63530
+ if (/^compaction:\s*(#.*)?$/.test(trimmed)) return i;
63531
+ }
63532
+ return -1;
63533
+ }
63534
+ function findCompactionEnabledLine(lines) {
63535
+ const header = topLevelCompactionHeader(lines);
63536
+ if (header === -1) return -1;
63537
+ for (let i = header + 1; i < lines.length; i++) {
63538
+ const raw = lines[i];
63539
+ const trimmed = raw.trim();
63540
+ if (!trimmed || trimmed.startsWith("#")) continue;
63541
+ if (raw.length - raw.trimStart().length === 0) break;
63542
+ if (/^enabled:/.test(trimmed)) return i;
63543
+ }
63544
+ return -1;
63545
+ }
63546
+ function compactionBlockEmpty(lines) {
63547
+ const header = topLevelCompactionHeader(lines);
63548
+ if (header === -1) return false;
63549
+ for (let i = header + 1; i < lines.length; i++) {
63550
+ const raw = lines[i];
63551
+ const trimmed = raw.trim();
63552
+ if (!trimmed || trimmed.startsWith("#")) continue;
63553
+ return raw.length - raw.trimStart().length > 0;
63554
+ }
63555
+ return true;
63556
+ }
63557
+ function stripOmpCompactionInjection(overlayText, realText) {
63558
+ const oLines = overlayText.split("\n");
63559
+ const oe2 = findCompactionEnabledLine(oLines);
63560
+ if (oe2 === -1) return overlayText;
63561
+ const m2 = /^enabled:\s*(\S+)/.exec(oLines[oe2].trim());
63562
+ if (!m2 || m2[1] !== "false") return overlayText;
63563
+ const rLines = realText !== void 0 ? realText.split("\n") : void 0;
63564
+ const re2 = rLines ? findCompactionEnabledLine(rLines) : -1;
63565
+ if (re2 !== -1 && rLines) {
63566
+ oLines[oe2] = rLines[re2];
63567
+ return oLines.join("\n");
63568
+ }
63569
+ oLines.splice(oe2, 1);
63570
+ if (compactionBlockEmpty(oLines)) {
63571
+ const h = topLevelCompactionHeader(oLines);
63572
+ if (h !== -1) oLines.splice(h, 1);
63573
+ }
63574
+ return oLines.join("\n");
63575
+ }
63576
+ function mergeBackOmpOverlayConfig(ompHome, overlay, holderActive) {
63577
+ try {
63578
+ const overlayCfg = path15.join(overlay, "config.yml");
63579
+ let oSt;
63580
+ try {
63581
+ oSt = fs12.lstatSync(overlayCfg);
63582
+ } catch {
63583
+ return;
63584
+ }
63585
+ if (!oSt.isFile()) return;
63586
+ let target;
63587
+ for (const name of ["config.yml", "config.yaml"]) {
63588
+ try {
63589
+ const st2 = fs12.lstatSync(path15.join(ompHome, name));
63590
+ if (st2.isFile()) {
63591
+ target = path15.join(ompHome, name);
63592
+ break;
63593
+ }
63594
+ } catch {
63595
+ }
63596
+ }
63597
+ let realText;
63598
+ if (target) {
63599
+ try {
63600
+ realText = fs12.readFileSync(target, "utf8");
63601
+ } catch {
63602
+ return;
63603
+ }
63604
+ } else if (fs12.existsSync(path15.join(ompHome, "settings.json"))) {
63605
+ return;
63606
+ }
63607
+ let oText;
63608
+ try {
63609
+ oText = fs12.readFileSync(overlayCfg, "utf8");
63610
+ } catch {
63611
+ return;
63612
+ }
63613
+ const norm = (t) => t.replace(/\r\n/g, "\n");
63614
+ const stripped = norm(stripOmpCompactionInjection(oText, realText));
63615
+ if (target) {
63616
+ if (realText !== void 0 && stripped === norm(realText)) return;
63617
+ let rSt;
63618
+ try {
63619
+ rSt = fs12.lstatSync(target);
63620
+ } catch {
63621
+ return;
63622
+ }
63623
+ if (rSt.mtimeMs >= oSt.mtimeMs) {
63624
+ if (!holderActive) {
63625
+ try {
63626
+ fs12.renameSync(overlayCfg, freeConflictName(target));
63627
+ } catch {
63628
+ }
63629
+ }
63630
+ return;
63631
+ }
63632
+ const crlf = realText !== void 0 && realText.includes("\r\n");
63633
+ atomicWriteTextFile(target, crlf ? stripped.replace(/\n/g, "\r\n") : stripped);
63634
+ return;
63635
+ }
63636
+ if (stripped.trim() === "") return;
63637
+ atomicWriteTextFile(path15.join(ompHome, "config.yml"), stripped);
63638
+ } catch (err2) {
63639
+ console.error(`bili: could not merge the overlay config.yml back into ${ompHome}: ${err2.message}`);
63640
+ }
63641
+ }
63581
63642
  function writeOmpCompactionDisabledConfig(ompHome, overlay) {
63582
63643
  let base;
63583
63644
  let baseIsJson = false;
@@ -63621,6 +63682,7 @@ function prepareOmpHttpRewrite(ompHome, origin, httpRewrites, httpsRewrites) {
63621
63682
  if (!fs12.existsSync(ompHome)) return void 0;
63622
63683
  const overlay = `${ompHome}-bili`;
63623
63684
  const generated = ["config.yml"];
63685
+ const holder = livePidHoldsOverlay(overlay);
63624
63686
  const modelsPath = path15.join(ompHome, "models.yml");
63625
63687
  const unpacked = unpackDeadProxyUrlsInFile(modelsPath, liveProxyPorts());
63626
63688
  if (unpacked > 0) {
@@ -63633,6 +63695,7 @@ function prepareOmpHttpRewrite(ompHome, origin, httpRewrites, httpsRewrites) {
63633
63695
  } catch {
63634
63696
  }
63635
63697
  if (!refreshOverlayHome(ompHome, overlay, generated)) return void 0;
63698
+ mergeBackOmpOverlayConfig(ompHome, overlay, holder !== void 0);
63636
63699
  if (modelsText !== void 0) {
63637
63700
  const httpMap = new Map(httpRewrites.map((r) => [r.key, wrapUpstream(origin, r.realUpstream)]));
63638
63701
  const httpsMap = new Map(httpsRewrites.map((r) => [r.key, r.realUpstream]));
@@ -63669,7 +63732,13 @@ function prepareOmpHttpRewrite(ompHome, origin, httpRewrites, httpsRewrites) {
63669
63732
  }
63670
63733
  writeOverlayFileAtomic(overlay, "models.yml", lines.join("\n"));
63671
63734
  }
63672
- writeOmpCompactionDisabledConfig(ompHome, overlay);
63735
+ if (holder !== void 0) {
63736
+ console.error(
63737
+ `bili: another bili omp (pid ${holder}) is still running \u2014 skipping the config.yml regeneration so the shared overlay keeps its saved settings (this session inherits them; its proxy port still wins in models.yml).`
63738
+ );
63739
+ } else {
63740
+ writeOmpCompactionDisabledConfig(ompHome, overlay);
63741
+ }
63673
63742
  return overlay;
63674
63743
  }
63675
63744
  function prepareHermesHome(hermesHome, origin, rewrites) {