@nomad-e/bluma-cli 0.1.64 → 0.1.65

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.
Files changed (2) hide show
  1. package/dist/main.js +32 -3
  2. package/package.json +1 -1
package/dist/main.js CHANGED
@@ -8530,15 +8530,30 @@ async function task_output(params) {
8530
8530
 
8531
8531
  // src/app/agent/core/context-api/token_counter.ts
8532
8532
  import { getEncoding } from "js-tiktoken";
8533
+ import { createHash } from "crypto";
8533
8534
  var MESSAGE_OVERHEAD_TOKENS = 4;
8534
8535
  var CONVERSATION_BASE_OVERHEAD = 3;
8535
8536
  var cachedEncoding = null;
8537
+ var tokenCountCache = null;
8536
8538
  function getO200kEncoding() {
8537
8539
  if (!cachedEncoding) {
8538
8540
  cachedEncoding = getEncoding("o200k_base");
8539
8541
  }
8540
8542
  return cachedEncoding;
8541
8543
  }
8544
+ function hashHistory(messages) {
8545
+ const hash = createHash("sha256");
8546
+ hash.update(`len:${messages.length}:`);
8547
+ for (const msg of messages) {
8548
+ const m = msg;
8549
+ hash.update(`role:${m.role ?? ""}:`);
8550
+ hash.update(`content:${JSON.stringify(m.content ?? "")}:`);
8551
+ hash.update(`tool_calls:${JSON.stringify(m.tool_calls ?? "")}:`);
8552
+ hash.update(`tool_call_id:${m.tool_call_id ?? ""}:`);
8553
+ hash.update(`name:${m.name ?? ""}:`);
8554
+ }
8555
+ return hash.digest("hex");
8556
+ }
8542
8557
  function messageBodyForTokens(msg) {
8543
8558
  const c = msg.content;
8544
8559
  if (c == null) {
@@ -8563,10 +8578,16 @@ function messageExtraForTokens(msg) {
8563
8578
  }
8564
8579
  return parts.join("\0");
8565
8580
  }
8566
- function countTokens(messages) {
8581
+ function countTokens(messages, useCache = true) {
8567
8582
  if (messages.length === 0) {
8568
8583
  return CONVERSATION_BASE_OVERHEAD;
8569
8584
  }
8585
+ if (useCache) {
8586
+ const currentHash = hashHistory(messages);
8587
+ if (tokenCountCache && tokenCountCache.hash === currentHash) {
8588
+ return tokenCountCache.count;
8589
+ }
8590
+ }
8570
8591
  const enc = getO200kEncoding();
8571
8592
  let total = CONVERSATION_BASE_OVERHEAD;
8572
8593
  for (const msg of messages) {
@@ -8576,8 +8597,14 @@ function countTokens(messages) {
8576
8597
  const nExtra = extra ? enc.encode(extra).length : 0;
8577
8598
  total += nBody + nExtra + MESSAGE_OVERHEAD_TOKENS;
8578
8599
  }
8600
+ if (useCache) {
8601
+ tokenCountCache = { hash: hashHistory(messages), count: total };
8602
+ }
8579
8603
  return total;
8580
8604
  }
8605
+ function clearTokenCountCache() {
8606
+ tokenCountCache = null;
8607
+ }
8581
8608
 
8582
8609
  // src/app/agent/tools/natives/ctx_inspect.ts
8583
8610
  async function ctx_inspect(args = {}) {
@@ -13429,8 +13456,9 @@ async function createApiContextWindow(fullHistory, currentAnchor, compressedTurn
13429
13456
  const thresholdTokens = tokenBudget * compressThreshold;
13430
13457
  let pendingSlices = turnSlices.slice(sliceCount, recentStart);
13431
13458
  let pendingFlat = pendingSlices.flat();
13459
+ clearTokenCountCache();
13432
13460
  let messages = buildContextMessages(systemMessages, anchor, pendingFlat, recentFlat);
13433
- let tokens = countTokens(messages);
13461
+ let tokens = countTokens(messages, true);
13434
13462
  while (tokens >= thresholdTokens && pendingSlices.length > 0) {
13435
13463
  try {
13436
13464
  anchor = await compressToAnchor(
@@ -13448,8 +13476,9 @@ async function createApiContextWindow(fullHistory, currentAnchor, compressedTurn
13448
13476
  pendingSlices = [];
13449
13477
  pendingFlat = [];
13450
13478
  }
13479
+ clearTokenCountCache();
13451
13480
  messages = buildContextMessages(systemMessages, anchor, pendingFlat, recentFlat);
13452
- tokens = countTokens(messages);
13481
+ tokens = countTokens(messages, true);
13453
13482
  }
13454
13483
  return {
13455
13484
  messages,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nomad-e/bluma-cli",
3
- "version": "0.1.64",
3
+ "version": "0.1.65",
4
4
  "description": "BluMa independent agent for automation and advanced software engineering.",
5
5
  "author": "Alex Fonseca",
6
6
  "license": "Apache-2.0",