billion-context 0.1.86 → 0.1.87

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
@@ -43476,10 +43476,6 @@ function defaultCountTokens(text) {
43476
43476
  const cjkCount = cjk?.length ?? 0;
43477
43477
  return cjkCount + Math.ceil((text.length - cjkCount) / 4);
43478
43478
  }
43479
- function estimateTokensFast(text) {
43480
- if (!text) return 0;
43481
- return Math.ceil(text.length / 4);
43482
- }
43483
43479
  var COMPRESS_PHILOSOPHY = `Compression Philosophy:
43484
43480
  - All compression serves the primary task, but be frugal.
43485
43481
  - Context capacity is precious. Save context by compressing consumed outputs, not by avoiding tools.
@@ -46339,7 +46335,7 @@ function formatTokens2(n) {
46339
46335
  }
46340
46336
  function pct(n, total) {
46341
46337
  if (n <= 0 || total <= 0) return 0;
46342
- return Math.max(1, Math.round(n / total * 100));
46338
+ return Math.round(n / total * 100);
46343
46339
  }
46344
46340
  function numericPart2(blockId) {
46345
46341
  const match = /^b(\d+)$/.exec(blockId);
@@ -46378,12 +46374,18 @@ function collectVisible(messages, state, countTokens) {
46378
46374
  if (block.active) summaryTokens += summaryTokensOf(block, countTokens);
46379
46375
  }
46380
46376
  const visible = [];
46377
+ const toolCallNames = /* @__PURE__ */ new Map();
46378
+ for (const message of messages) {
46379
+ if (message.contentType === "tool-call" && message.toolCallId && message.toolName) {
46380
+ toolCallNames.set(message.toolCallId, message.toolName);
46381
+ }
46382
+ }
46381
46383
  messages.forEach((message, index) => {
46382
46384
  if (coveredIds.has(message.id)) return;
46383
46385
  const ref = refForRaw(state.messageRefs, message.id);
46384
46386
  if (!ref) return;
46385
46387
  const tokens = countTokens(message.text ?? "");
46386
- const tool = message.toolName ?? "text";
46388
+ const tool = isToolMessage(message) ? message.toolName ?? (message.toolCallId ? toolCallNames.get(message.toolCallId) : void 0) ?? "tool" : "text";
46387
46389
  if (tokens > 0) visible.push({ ref, tokens, tool, index });
46388
46390
  });
46389
46391
  return { visible, summaryTokens };
@@ -46471,6 +46473,17 @@ function renderUncompressedRanges(visible) {
46471
46473
  const m2 = ref.match(/\d+/);
46472
46474
  return m2 ? parseInt(m2[0], 10) : 0;
46473
46475
  };
46476
+ const dominantTool = (toolTokens) => {
46477
+ let best = "text";
46478
+ let bestN = -1;
46479
+ for (const [tool, n] of toolTokens) {
46480
+ if (n > bestN) {
46481
+ best = tool;
46482
+ bestN = n;
46483
+ }
46484
+ }
46485
+ return best;
46486
+ };
46474
46487
  const merged = [];
46475
46488
  for (const m2 of visible) {
46476
46489
  const num3 = refNum22(m2.ref);
@@ -46479,13 +46492,15 @@ function renderUncompressedRanges(visible) {
46479
46492
  last.endRef = m2.ref;
46480
46493
  last.count += 1;
46481
46494
  last.tokens += m2.tokens;
46495
+ last.toolTokens.set(m2.tool, (last.toolTokens.get(m2.tool) ?? 0) + m2.tokens);
46482
46496
  } else {
46483
- merged.push({ startRef: m2.ref, endRef: m2.ref, startNum: num3, count: 1, tokens: m2.tokens, tool: m2.tool });
46497
+ const toolTokens = /* @__PURE__ */ new Map([[m2.tool, m2.tokens]]);
46498
+ merged.push({ startRef: m2.ref, endRef: m2.ref, startNum: num3, count: 1, tokens: m2.tokens, toolTokens });
46484
46499
  }
46485
46500
  }
46486
46501
  for (const r of merged.slice(0, 30)) {
46487
46502
  const range = r.count === 1 ? r.startRef : `${r.startRef}\u2013${r.endRef}`;
46488
- lines.push(` ${range} (${r.count} msgs, ${formatTokens2(r.tokens)}${r.count > 1 ? ` (${Math.round(r.tokens / r.count)}/msg)` : ""}) ${r.tool}`);
46503
+ lines.push(` ${range} (${r.count} msgs, ${formatTokens2(r.tokens)}${r.count > 1 ? ` (${Math.round(r.tokens / r.count)}/msg)` : ""}) ${dominantTool(r.toolTokens)}`);
46489
46504
  }
46490
46505
  if (merged.length > 30) {
46491
46506
  lines.push(` ... and ${merged.length - 30} more ranges`);
@@ -50754,7 +50769,7 @@ function executeAnthropicProxyTool(toolName, args, ctx) {
50754
50769
  ${lines.join("\n\n")}`;
50755
50770
  }
50756
50771
  if (toolName === "acp_status") {
50757
- return buildStatusReport(ctx.session.state, ctx.messages, estimateTokensFast);
50772
+ return buildStatusReport(ctx.session.state, ctx.messages, defaultCountTokens);
50758
50773
  }
50759
50774
  return `[Unknown proxy tool: ${toolName}]`;
50760
50775
  }
@@ -51872,6 +51887,23 @@ function isLoopThinking(m2) {
51872
51887
  function stripLoopThinking(messages) {
51873
51888
  return messages.filter((m2) => !isLoopThinking(m2));
51874
51889
  }
51890
+ function canonicalArgs(args) {
51891
+ try {
51892
+ return JSON.stringify(sortKeys(JSON.parse(args)));
51893
+ } catch {
51894
+ return args;
51895
+ }
51896
+ }
51897
+ function sortKeys(v2) {
51898
+ if (Array.isArray(v2)) return v2.map(sortKeys);
51899
+ if (v2 !== null && typeof v2 === "object") {
51900
+ const src = v2;
51901
+ const out = {};
51902
+ for (const k2 of Object.keys(src).sort()) out[k2] = sortKeys(src[k2]);
51903
+ return out;
51904
+ }
51905
+ return v2;
51906
+ }
51875
51907
  function executeProxyTool(toolName, args, ctx, callId) {
51876
51908
  if (toolName === "compress") {
51877
51909
  return applyRanges(parseCompressInput(args, callId), ctx);
@@ -51906,7 +51938,7 @@ function handleAcpStatus(args, ctx) {
51906
51938
  const tool = typeof args.tool === "string" ? args.tool : void 0;
51907
51939
  const sort = typeof args.sort === "string" ? args.sort : void 0;
51908
51940
  const limit = typeof args.limit === "number" ? args.limit : void 0;
51909
- const base = buildStatusReport(ctx.session.state, ctx.messages, estimateTokensFast, { scope, view, tool, sort, limit });
51941
+ const base = buildStatusReport(ctx.session.state, ctx.messages, defaultCountTokens, { scope, view, tool, sort, limit });
51910
51942
  if (scope) return base;
51911
51943
  const nudge = ctx.nudge;
51912
51944
  const ranges = nudge?.compressibleRanges ?? [];
@@ -51974,6 +52006,7 @@ async function* runCompressLoop(upstream, ctx, requestBody, requestOptions, adap
51974
52006
  log("warn", `[acp-loop] upstream rejected replay (HTTP ${info.status}); retrying in ${info.delayMs}ms (attempt ${info.attempt}/${info.maxAttempts})${lc}`);
51975
52007
  }
51976
52008
  );
52009
+ const failedSignatures = /* @__PURE__ */ new Set();
51977
52010
  try {
51978
52011
  for (let round = 1; round <= MAX_LOOP_ROUNDS; round++) {
51979
52012
  if (signal?.aborted) break;
@@ -52229,6 +52262,19 @@ async function* runCompressLoop(upstream, ctx, requestBody, requestOptions, adap
52229
52262
  yield adapter.emitCompletion({ finishReason: "length", usage });
52230
52263
  return;
52231
52264
  }
52265
+ const failedMutating = proxyResults.filter(
52266
+ (pr2) => (pr2.name === "compress" || pr2.name === "decompress") && pr2.result.includes("FAILED")
52267
+ );
52268
+ if (reRequest && failedMutating.length > 0 && failedMutating.length === proxyResults.length) {
52269
+ const repeated = failedMutating.some((pr2) => failedSignatures.has(`${pr2.name}\0${canonicalArgs(pr2.arguments)}`));
52270
+ if (repeated) {
52271
+ ctx.log(`[acp-loop] round ${round}: model re-submitted an already-failed compress with identical arguments; stopping after ${round} round(s) instead of ${MAX_LOOP_ROUNDS}`);
52272
+ log("warn", `[acp-loop] repeated identical compress failure at round ${round}; breaking early (#156)`);
52273
+ yield adapter.emitCompletion({ finishReason: "length", usage });
52274
+ return;
52275
+ }
52276
+ }
52277
+ for (const pr2 of failedMutating) failedSignatures.add(`${pr2.name}\0${canonicalArgs(pr2.arguments)}`);
52232
52278
  ctx.log(`[acp-loop] round ${round}: proxy tool executed; re-requesting so the model sees the result`);
52233
52279
  if (signal?.aborted) break;
52234
52280
  let newBody = adapter.buildRequest(coreMessages, systemPrompt, requestBody);
@@ -53755,7 +53801,7 @@ function executeProxyTool2(toolName, args, ctx) {
53755
53801
  ${lines.join("\n\n")}`;
53756
53802
  }
53757
53803
  if (toolName === "acp_status") {
53758
- return buildStatusReport(ctx.session.state, ctx.messages, estimateTokensFast);
53804
+ return buildStatusReport(ctx.session.state, ctx.messages, defaultCountTokens);
53759
53805
  }
53760
53806
  return `[Unknown proxy tool: ${toolName}]`;
53761
53807
  }
@@ -54334,14 +54380,14 @@ var MIN_TAIL_MATCH = 3;
54334
54380
  var MAX_STORED_ITEMS = 128;
54335
54381
  var MIN_FORK_PREFIX = 3;
54336
54382
  function stableStringify(value) {
54337
- return JSON.stringify(sortKeys(value));
54383
+ return JSON.stringify(sortKeys2(value));
54338
54384
  }
54339
- function sortKeys(value) {
54340
- if (Array.isArray(value)) return value.map(sortKeys);
54385
+ function sortKeys2(value) {
54386
+ if (Array.isArray(value)) return value.map(sortKeys2);
54341
54387
  if (value && typeof value === "object") {
54342
54388
  const record = value;
54343
54389
  const out = {};
54344
- for (const key of Object.keys(record).sort()) out[key] = sortKeys(record[key]);
54390
+ for (const key of Object.keys(record).sort()) out[key] = sortKeys2(record[key]);
54345
54391
  return out;
54346
54392
  }
54347
54393
  return value;