billion-context 0.1.96-pr.665.479 → 0.1.96

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,71 +44472,25 @@ var KEEP_LAST_ORPHANED = 2;
44472
44472
  function rangeKey(startRef, endRef) {
44473
44473
  return `${startRef}::${endRef}`;
44474
44474
  }
44475
- function parseCallText(text) {
44476
- const raw = text ?? "";
44477
- const start = raw.indexOf("{");
44478
- if (start < 0) return null;
44475
+ function rewriteCompressText(text, liveKeys) {
44479
44476
  let parsed;
44480
44477
  try {
44481
- parsed = JSON.parse(raw.slice(start));
44478
+ parsed = JSON.parse(text ?? "");
44482
44479
  } catch {
44483
44480
  return null;
44484
44481
  }
44485
44482
  if (!parsed || typeof parsed !== "object") return null;
44486
44483
  const obj = parsed;
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;
44484
+ const content = obj.content;
44485
+ if (!Array.isArray(content) || content.length === 0) return null;
44507
44486
  const kept = content.filter((entry) => {
44508
44487
  if (!entry || typeof entry !== "object") return false;
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));
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));
44513
44491
  });
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;
44492
+ if (kept.length === content.length || kept.length === 0) return null;
44493
+ return JSON.stringify({ ...obj, content: kept });
44540
44494
  }
44541
44495
  function hideConsumedCompressCalls(state, messages) {
44542
44496
  const allBlockCallIds = /* @__PURE__ */ new Set();
@@ -44595,11 +44549,6 @@ function hideConsumedCompressCalls(state, messages) {
44595
44549
  continue;
44596
44550
  }
44597
44551
  }
44598
- const compacted = compactCompressText(message.text);
44599
- if (compacted !== null) {
44600
- result.push({ ...message, text: compacted });
44601
- continue;
44602
- }
44603
44552
  }
44604
44553
  result.push(message);
44605
44554
  }
@@ -51872,32 +51821,16 @@ function splitChunks(messages, startIdx, endIdx, budget, minUnits, countText = d
51872
51821
  }
51873
51822
  return chunks;
51874
51823
  }
51875
- function summaryPayload(protocol, model, system, content, stream2, includeMaxOutputTokens) {
51824
+ function summaryPayload(protocol, model, system, content, stream2) {
51876
51825
  if (protocol === "anthropic") {
51877
51826
  return { model, max_tokens: MAX_SUMMARY_OUTPUT_TOKENS, system, messages: [{ role: "user", content }], stream: stream2 };
51878
51827
  }
51879
51828
  if (protocol === "openai") {
51880
51829
  return { model, max_tokens: MAX_SUMMARY_OUTPUT_TOKENS, messages: [{ role: "system", content: system }, { role: "user", content }], stream: stream2 };
51881
51830
  }
51882
- const payload = { model, instructions: system, input: [{ role: "user", content }], stream: stream2, store: false };
51883
- if (includeMaxOutputTokens) payload.max_output_tokens = MAX_SUMMARY_OUTPUT_TOKENS;
51884
- return payload;
51831
+ return { model, max_output_tokens: MAX_SUMMARY_OUTPUT_TOKENS, instructions: system, input: [{ role: "user", content }], stream: stream2, store: false };
51885
51832
  }
51886
51833
  var STREAM_REQUIRED_RE = /\bstream\b[^\n]{0,60}\btrue\b/i;
51887
- var MAX_OUTPUT_TOKENS_REJECTED_RE = /\bmax_output_tokens\b/i;
51888
- function noMaxOutputTokensKey(deps) {
51889
- return `${deps.url}\0${deps.model}`;
51890
- }
51891
- function hasLearnedNoMaxOutputTokens(deps) {
51892
- const learned = deps.session.metadata.preflightNoMaxOutputTokens;
51893
- return typeof learned === "object" && learned !== null && learned[noMaxOutputTokensKey(deps)] === true;
51894
- }
51895
- function rememberNoMaxOutputTokens(deps) {
51896
- const learned = deps.session.metadata.preflightNoMaxOutputTokens;
51897
- const map = typeof learned === "object" && learned !== null ? learned : {};
51898
- map[noMaxOutputTokensKey(deps)] = true;
51899
- deps.session.metadata.preflightNoMaxOutputTokens = map;
51900
- }
51901
51834
  function extractSummaryFromSse(protocol, text) {
51902
51835
  let out = "";
51903
51836
  for (const line of text.split("\n")) {
@@ -51961,39 +51894,25 @@ async function summarizeRange(deps, content, startRef, endRef) {
51961
51894
  const system = buildCompressSystemPrompt(deps.prompts) + `
51962
51895
 
51963
51896
  TASK: The conversation segment below (messages ${startRef}\u2013${endRef}) must be compressed because the session context exceeds the current model's window. Write a tier-1 compression summary of the segment following every rule above. Output ONLY the summary text \u2014 no preamble, no closing remarks, no tool calls.`;
51964
- let stream2 = deps.session.metadata.preflightStreamSummary === true;
51965
- let includeMaxOutputTokens = !(deps.protocol === "responses" && hasLearnedNoMaxOutputTokens(deps));
51966
- for (; ; ) {
51967
- try {
51968
- return await requestSummary(deps, system, content, stream2, includeMaxOutputTokens);
51969
- } catch (err2) {
51970
- if (err2 instanceof UpstreamHttpError && err2.status === 400) {
51971
- let adapted = false;
51972
- if (!stream2 && STREAM_REQUIRED_RE.test(err2.body)) {
51973
- deps.session.metadata.preflightStreamSummary = true;
51974
- stream2 = true;
51975
- adapted = true;
51976
- deps.log("info", "[preflight] upstream requires stream for summaries; retrying with SSE (learned for this session)");
51977
- }
51978
- if (deps.protocol === "responses" && includeMaxOutputTokens && MAX_OUTPUT_TOKENS_REJECTED_RE.test(err2.body)) {
51979
- rememberNoMaxOutputTokens(deps);
51980
- includeMaxOutputTokens = false;
51981
- adapted = true;
51982
- deps.log("info", `[preflight] upstream rejects max_output_tokens for summaries (model=${deps.model}); retrying without it (learned for this session+upstream+model)`);
51983
- }
51984
- if (adapted) continue;
51985
- }
51986
- throw err2;
51897
+ const learned = deps.session.metadata.preflightStreamSummary === true;
51898
+ try {
51899
+ return await requestSummary(deps, system, content, learned);
51900
+ } catch (err2) {
51901
+ if (err2 instanceof UpstreamHttpError && err2.status === 400 && !learned && STREAM_REQUIRED_RE.test(err2.body)) {
51902
+ deps.session.metadata.preflightStreamSummary = true;
51903
+ deps.log("info", "[preflight] upstream requires stream for summaries; retrying with SSE (learned for this session)");
51904
+ return await requestSummary(deps, system, content, true);
51987
51905
  }
51906
+ throw err2;
51988
51907
  }
51989
51908
  }
51990
- async function requestSummary(deps, system, content, stream2, includeMaxOutputTokens) {
51909
+ async function requestSummary(deps, system, content, stream2) {
51991
51910
  const { response, clearTimer } = await fetchWithRetry(
51992
51911
  deps.url,
51993
51912
  {
51994
51913
  method: "POST",
51995
51914
  headers: { "content-type": "application/json", ...deps.headers },
51996
- body: JSON.stringify(summaryPayload(deps.protocol, deps.model, system, content, stream2, includeMaxOutputTokens)),
51915
+ body: JSON.stringify(summaryPayload(deps.protocol, deps.model, system, content, stream2)),
51997
51916
  dispatcher: proxyDispatcher(deps.proxyUrl)
51998
51917
  },
51999
51918
  void 0,