billion-context 0.1.140 → 0.1.141

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
@@ -43467,6 +43467,10 @@ var require_lib = __commonJS({
43467
43467
  }
43468
43468
  });
43469
43469
 
43470
+ // src/index.ts
43471
+ import { realpathSync as realpathSync2 } from "fs";
43472
+ import { fileURLToPath as fileURLToPath8 } from "url";
43473
+
43470
43474
  // node_modules/acp-kernel/dist/chunk-6RNVHY67.js
43471
43475
  import { createRequire } from "module";
43472
43476
  var require2 = createRequire(import.meta.url);
@@ -73243,6 +73247,10 @@ async function startServer(opts) {
73243
73247
  const log2 = (level, msg) => logMsg(opts, level, msg);
73244
73248
  const instanceId = randomUUID4();
73245
73249
  const instanceStartedAt = Date.now();
73250
+ const parentWatchPid = Number.parseInt(process.env.BILI_PARENT_PID ?? "", 10);
73251
+ const initialWatcherPid = Number.isInteger(parentWatchPid) && parentWatchPid > 0 && parentWatchPid !== process.pid ? parentWatchPid : null;
73252
+ const proxyWatchers = /* @__PURE__ */ new Set();
73253
+ if (initialWatcherPid !== null) proxyWatchers.add(initialWatcherPid);
73246
73254
  await initSessions();
73247
73255
  loadConversations();
73248
73256
  log2("info", `[persist] ${getStore().enabled ? "enabled" : "disabled"}`);
@@ -73263,7 +73271,7 @@ async function startServer(opts) {
73263
73271
  const server = http.createServer(async (req, res) => {
73264
73272
  armRequestWatchdog(req, res, log2);
73265
73273
  try {
73266
- await handle(req, res, opts, core, config, log2, instanceId, instanceStartedAt);
73274
+ await handle(req, res, opts, core, config, log2, instanceId, instanceStartedAt, proxyWatchers, initialWatcherPid);
73267
73275
  } catch (err2) {
73268
73276
  const msg = String(err2);
73269
73277
  const e = err2;
@@ -73419,10 +73427,30 @@ Content-Length: ${Buffer.byteLength(body)}\r
73419
73427
  if (process.platform === "win32") {
73420
73428
  process.on("SIGBREAK", () => shutdown("SIGBREAK"));
73421
73429
  }
73422
- const parentPid = Number.parseInt(process.env.BILI_PARENT_PID ?? "", 10);
73423
- if (Number.isInteger(parentPid) && parentPid > 0 && parentPid !== process.pid) {
73430
+ if (initialWatcherPid !== null) {
73431
+ let idleSince = null;
73432
+ let lastDead = [];
73424
73433
  const watcher = setInterval(() => {
73425
- if (!isPidAlive(parentPid)) shutdown(`parent-gone (pid ${parentPid})`);
73434
+ const dead = [];
73435
+ for (const pid of proxyWatchers) {
73436
+ if (!isPidAlive(pid)) {
73437
+ proxyWatchers.delete(pid);
73438
+ dead.push(pid);
73439
+ }
73440
+ }
73441
+ if (dead.length > 0) lastDead = dead;
73442
+ if (proxyWatchers.size > 0) {
73443
+ idleSince = null;
73444
+ return;
73445
+ }
73446
+ if (idleSince === null) {
73447
+ idleSince = Date.now();
73448
+ return;
73449
+ }
73450
+ if (Date.now() - idleSince >= WATCHER_IDLE_GRACE_MS) {
73451
+ const reason = lastDead.length === 1 && lastDead[0] === initialWatcherPid ? `parent-gone (pid ${lastDead[0]})` : `watchers-gone (pids: ${lastDead.join(", ")})`;
73452
+ shutdown(reason);
73453
+ }
73426
73454
  }, 2e3);
73427
73455
  watcher.unref?.();
73428
73456
  }
@@ -73458,7 +73486,8 @@ function adminTrustedHosts(bindHost, port) {
73458
73486
  return set;
73459
73487
  }
73460
73488
  var headroomFallbackLogged = /* @__PURE__ */ new Set();
73461
- async function handle(req, res, opts, core, config, log2, instanceId, instanceStartedAt) {
73489
+ var WATCHER_IDLE_GRACE_MS = 5e3;
73490
+ async function handle(req, res, opts, core, config, log2, instanceId, instanceStartedAt, proxyWatchers, initialWatcherPid) {
73462
73491
  const selfProbePath = selfAdminProbePath(req.url ?? "", req.socket.localPort);
73463
73492
  if (selfProbePath !== void 0) req.url = selfProbePath;
73464
73493
  const isAdminPath = req.url === "/__bili/" || req.url?.startsWith("/__bili/") || req.url === "/__acp/" || req.url?.startsWith("/__acp/");
@@ -73587,6 +73616,29 @@ async function handle(req, res, opts, core, config, log2, instanceId, instanceSt
73587
73616
  }
73588
73617
  return handlePluginStatus(conversationId2, res, { core, config, log: log2 }, params.get("fallback") === "latest");
73589
73618
  }
73619
+ if (req.method === "POST" && req.url === "/__bili/watcher") {
73620
+ try {
73621
+ const body = await readBody(req);
73622
+ const parsed2 = JSON.parse(body.toString("utf8"));
73623
+ const pid = parsed2.pid;
73624
+ if (typeof pid !== "number" || !Number.isInteger(pid) || pid <= 1 || pid === process.pid) {
73625
+ res.writeHead(400, { "content-type": "application/json" });
73626
+ res.end(JSON.stringify({ ok: false, error: "expected { pid: <integer> }" }));
73627
+ } else if (initialWatcherPid === null) {
73628
+ res.writeHead(409, { "content-type": "application/json" });
73629
+ res.end(JSON.stringify({ ok: false, error: "watchdog not armed (no parent pid) \u2014 this proxy does not take watchers" }));
73630
+ } else {
73631
+ proxyWatchers.add(pid);
73632
+ res.writeHead(200, { "content-type": "application/json" });
73633
+ res.end(JSON.stringify({ ok: true, watchers: proxyWatchers.size }));
73634
+ }
73635
+ return;
73636
+ } catch (err2) {
73637
+ res.writeHead(err2 instanceof BodyTooLargeError ? 413 : 400, { "content-type": "application/json" });
73638
+ res.end(JSON.stringify({ ok: false, error: String(err2) }));
73639
+ return;
73640
+ }
73641
+ }
73590
73642
  if (req.method === "POST" && req.url === "/__bili/plugin/tool") {
73591
73643
  try {
73592
73644
  const body = await readBody(req);
@@ -81073,10 +81125,26 @@ async function main() {
81073
81125
  }
81074
81126
 
81075
81127
  // src/index.ts
81076
- main().catch((err2) => {
81077
- console.error("bili: failed to start:", err2);
81078
- process.exit(1);
81079
- });
81128
+ function invokedAsScript() {
81129
+ const argv1 = process.argv[1];
81130
+ if (!argv1) return false;
81131
+ let self2;
81132
+ let invoked;
81133
+ try {
81134
+ self2 = realpathSync2(fileURLToPath8(import.meta.url));
81135
+ invoked = realpathSync2(argv1);
81136
+ } catch {
81137
+ return false;
81138
+ }
81139
+ if (process.platform === "win32") return self2.toLowerCase() === invoked.toLowerCase();
81140
+ return self2 === invoked;
81141
+ }
81142
+ if (invokedAsScript()) {
81143
+ main().catch((err2) => {
81144
+ console.error("bili: failed to start:", err2);
81145
+ process.exit(1);
81146
+ });
81147
+ }
81080
81148
  /*! Bundled license information:
81081
81149
 
81082
81150
  undici/lib/web/fetch/body.js: