billion-context 0.1.96 → 0.1.97-pr.666.481

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
@@ -44472,25 +44472,71 @@ var KEEP_LAST_ORPHANED = 2;
44472
44472
  function rangeKey(startRef, endRef) {
44473
44473
  return `${startRef}::${endRef}`;
44474
44474
  }
44475
- function rewriteCompressText(text, liveKeys) {
44475
+ function parseCallText(text) {
44476
+ const raw = text ?? "";
44477
+ const start = raw.indexOf("{");
44478
+ if (start < 0) return null;
44476
44479
  let parsed;
44477
44480
  try {
44478
- parsed = JSON.parse(text ?? "");
44481
+ parsed = JSON.parse(raw.slice(start));
44479
44482
  } catch {
44480
44483
  return null;
44481
44484
  }
44482
44485
  if (!parsed || typeof parsed !== "object") return null;
44483
44486
  const obj = parsed;
44484
- const content = obj.content;
44485
- if (!Array.isArray(content) || content.length === 0) return null;
44487
+ let content = null;
44488
+ let contentWasString = false;
44489
+ if (Array.isArray(obj.content)) {
44490
+ content = obj.content;
44491
+ } else if (typeof obj.content === "string") {
44492
+ contentWasString = true;
44493
+ try {
44494
+ const inner = JSON.parse(obj.content);
44495
+ if (Array.isArray(inner)) content = inner;
44496
+ } catch {
44497
+ content = null;
44498
+ }
44499
+ }
44500
+ if (!content || content.length === 0) return null;
44501
+ return { prefix: raw.slice(0, start), obj, content, contentWasString };
44502
+ }
44503
+ function rewriteCompressText(text, liveKeys) {
44504
+ const parsed = parseCallText(text);
44505
+ if (!parsed) return null;
44506
+ const { prefix, obj, content, contentWasString } = parsed;
44486
44507
  const kept = content.filter((entry) => {
44487
44508
  if (!entry || typeof entry !== "object") return false;
44488
- const s3 = typeof entry.startId === "string" ? entry.startId : typeof entry.messageId === "string" ? entry.messageId : "";
44489
- const e = typeof entry.endId === "string" ? entry.endId : typeof entry.messageId === "string" ? entry.messageId : "";
44490
- return liveKeys.has(rangeKey(s3, e));
44509
+ const e = entry;
44510
+ const s3 = typeof e.startId === "string" ? e.startId : typeof e.messageId === "string" ? e.messageId : "";
44511
+ const end = typeof e.endId === "string" ? e.endId : typeof e.messageId === "string" ? e.messageId : "";
44512
+ return liveKeys.has(rangeKey(s3, end));
44491
44513
  });
44492
- if (kept.length === content.length || kept.length === 0) return null;
44493
- return JSON.stringify({ ...obj, content: kept });
44514
+ if (kept.length === 0) return null;
44515
+ return prefix + serializeCompacted(obj, kept, contentWasString).text;
44516
+ }
44517
+ var SUMMARY_STUB_CHARS = 200;
44518
+ function compactEntry(entry) {
44519
+ if (!entry || typeof entry !== "object") return entry;
44520
+ const e = entry;
44521
+ if (typeof e.summary !== "string" || e.summary.length <= SUMMARY_STUB_CHARS) return entry;
44522
+ return { ...e, summary: `${e.summary.slice(0, SUMMARY_STUB_CHARS - 1)}\u2026` };
44523
+ }
44524
+ function serializeCompacted(obj, content, contentWasString) {
44525
+ let changed = false;
44526
+ const compacted = content.map((entry) => {
44527
+ const out = compactEntry(entry);
44528
+ if (out !== entry) changed = true;
44529
+ return out;
44530
+ });
44531
+ const outContent = contentWasString ? JSON.stringify(compacted) : compacted;
44532
+ return { text: JSON.stringify({ ...obj, content: outContent }), changed };
44533
+ }
44534
+ function compactCompressText(text) {
44535
+ const parsed = parseCallText(text);
44536
+ if (!parsed) return null;
44537
+ const { prefix, obj, content, contentWasString } = parsed;
44538
+ const { text: out, changed } = serializeCompacted(obj, content, contentWasString);
44539
+ return changed ? prefix + out : null;
44494
44540
  }
44495
44541
  function hideConsumedCompressCalls(state, messages) {
44496
44542
  const allBlockCallIds = /* @__PURE__ */ new Set();
@@ -44549,6 +44595,11 @@ function hideConsumedCompressCalls(state, messages) {
44549
44595
  continue;
44550
44596
  }
44551
44597
  }
44598
+ const compacted = compactCompressText(message.text);
44599
+ if (compacted !== null) {
44600
+ result.push({ ...message, text: compacted });
44601
+ continue;
44602
+ }
44552
44603
  }
44553
44604
  result.push(message);
44554
44605
  }
@@ -46262,6 +46313,9 @@ function decideNudge(input) {
46262
46313
  const growthReady = firstSightMassReady || growthSinceReference >= growthFloor;
46263
46314
  const t2Count = tiers[2]?.targetBlocks.length ?? 0;
46264
46315
  const t3Count = tiers[3]?.targetBlocks.length ?? 0;
46316
+ const tierCountUsageFloor = config.nudge.minContextLimitPct;
46317
+ const t2CountReady = t2Count >= config.tiers.tier2Trigger && usage >= tierCountUsageFloor;
46318
+ const t3CountReady = t3Count >= config.tiers.tier3Trigger && usage >= tierCountUsageFloor;
46265
46319
  if (pressure) {
46266
46320
  const candidates = [1];
46267
46321
  if (config.tiers.enabled) {
@@ -46284,19 +46338,19 @@ function decideNudge(input) {
46284
46338
  if (t1Eff >= nudgeGrowthTokens) {
46285
46339
  injectedTier = 1;
46286
46340
  injectedReason = `T1 effective ${t1Eff} >= ${nudgeGrowthTokens}, growth ${growthSinceReference}, usage ${Math.round(usage * 100)}%`;
46287
- } else if (config.tiers.enabled && (t2Count >= config.tiers.tier2Trigger || t2Pen >= tier2Threshold && t2Pen > t1Eff)) {
46341
+ } else if (config.tiers.enabled && (t2CountReady || t2Pen >= tier2Threshold && t2Pen > t1Eff)) {
46288
46342
  const lastShown = state.nudge.lastShownByTier[2] ?? 0;
46289
46343
  const cadenceMet = lastShown === 0 || tokenCount - lastShown >= growthFloor;
46290
46344
  if (cadenceMet) {
46291
46345
  injectedTier = 2;
46292
- injectedReason = t2Count >= config.tiers.tier2Trigger ? `T2 distill ready: ${t2Count} tier-1 blocks >= tier2Trigger ${config.tiers.tier2Trigger} (${t2Pen} tokens), usage ${Math.round(usage * 100)}%` : `T2 distill ready: ${tiers[2].targetBlocks.length} tier-1 blocks (${t2Pen} tokens) >= ${tier2Threshold} (1.5x) and > T1 effective ${t1Eff}, usage ${Math.round(usage * 100)}%`;
46346
+ injectedReason = t2CountReady ? `T2 distill ready: ${t2Count} tier-1 blocks >= tier2Trigger ${config.tiers.tier2Trigger} (${t2Pen} tokens), usage ${Math.round(usage * 100)}%` : `T2 distill ready: ${tiers[2].targetBlocks.length} tier-1 blocks (${t2Pen} tokens) >= ${tier2Threshold} (1.5x) and > T1 effective ${t1Eff}, usage ${Math.round(usage * 100)}%`;
46293
46347
  }
46294
- } else if (config.tiers.enabled && (t3Count >= config.tiers.tier3Trigger || t3Pen >= tier2Threshold && t3Pen > t2Pen && t3Pen > t1Eff)) {
46348
+ } else if (config.tiers.enabled && (t3CountReady || t3Pen >= tier2Threshold && t3Pen > t2Pen && t3Pen > t1Eff)) {
46295
46349
  const lastShown = state.nudge.lastShownByTier[3] ?? 0;
46296
46350
  const cadenceMet = lastShown === 0 || tokenCount - lastShown >= growthFloor;
46297
46351
  if (cadenceMet) {
46298
46352
  injectedTier = 3;
46299
- injectedReason = t3Count >= config.tiers.tier3Trigger ? `T3 condense ready: ${t3Count} tier-2 blocks >= tier3Trigger ${config.tiers.tier3Trigger} (${t3Pen} tokens), usage ${Math.round(usage * 100)}%` : `T3 condense ready: ${tiers[3].targetBlocks.length} tier-2 blocks (${t3Pen} tokens) >= ${tier2Threshold} (1.5x) and > T2 ${t2Pen} and > T1 effective ${t1Eff}, usage ${Math.round(usage * 100)}%`;
46353
+ injectedReason = t3CountReady ? `T3 condense ready: ${t3Count} tier-2 blocks >= tier3Trigger ${config.tiers.tier3Trigger} (${t3Pen} tokens), usage ${Math.round(usage * 100)}%` : `T3 condense ready: ${tiers[3].targetBlocks.length} tier-2 blocks (${t3Pen} tokens) >= ${tier2Threshold} (1.5x) and > T2 ${t2Pen} and > T1 effective ${t1Eff}, usage ${Math.round(usage * 100)}%`;
46300
46354
  }
46301
46355
  }
46302
46356
  }
@@ -46313,9 +46367,14 @@ function decideNudge(input) {
46313
46367
  } else {
46314
46368
  const tiersList = [1, 2, 3];
46315
46369
  const eligible = tiersList.filter((t) => config.tiers.enabled || t === 1);
46316
- const countReady = (t) => t === 2 ? t2Count >= config.tiers.tier2Trigger : t === 3 ? t3Count >= config.tiers.tier3Trigger : false;
46370
+ const countReadyUngated = (t) => t === 2 ? t2Count >= config.tiers.tier2Trigger : t === 3 ? t3Count >= config.tiers.tier3Trigger : false;
46371
+ const countReady = (t) => countReadyUngated(t) && usage >= tierCountUsageFloor;
46317
46372
  const ready = eligible.filter((t) => (tiers[t]?.pending ?? 0) >= nudgeGrowthTokens).map((t) => `T${t} ${tiers[t].pending}`);
46318
- const readyCount = eligible.filter((t) => (tiers[t]?.pending ?? 0) < nudgeGrowthTokens && countReady(t)).map((t) => `T${t} ${t === 2 ? t2Count : t3Count} blocks (count)`);
46373
+ const readyCount = eligible.filter(
46374
+ (t) => (tiers[t]?.pending ?? 0) < nudgeGrowthTokens && countReadyUngated(t)
46375
+ ).map(
46376
+ (t) => `T${t} ${t === 2 ? t2Count : t3Count} blocks (count${usage >= tierCountUsageFloor ? "" : ", usage-gated"})`
46377
+ );
46319
46378
  const readyAll = [...ready, ...readyCount];
46320
46379
  const readyHint = readyAll.length > 0 ? `, ready: ${readyAll.join(", ")}` : "";
46321
46380
  const blocked = eligible.filter(