blun-king-cli 9.1.243 → 9.1.245

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.
@@ -11,6 +11,17 @@ const STEP_END_LIVE_ONLY_FIELDS = [
11
11
  ];
12
12
  const STEP_END_ROUTINE_FINISH_REASONS = new Set(['tool_use', 'end_turn']);
13
13
 
14
+ function usageTotal(usage) {
15
+ return usage.inputCacheRead + usage.inputCacheCreation + usage.inputOther + usage.output;
16
+ }
17
+
18
+ function resolveCompletedStepUuid(openSteps, event) {
19
+ if (event.uuid !== undefined) return event.uuid;
20
+ let latest;
21
+ for (const uuid of openSteps.keys()) latest = uuid;
22
+ return latest;
23
+ }
24
+
14
25
  function hasOwn(object, key) {
15
26
  return Object.prototype.hasOwnProperty.call(object, key);
16
27
  }
@@ -56,11 +67,14 @@ function projectLoopEventForRecord(event) {
56
67
  }
57
68
 
58
69
  const hasRoutineFinishReason = STEP_END_ROUTINE_FINISH_REASONS.has(event?.finishReason);
70
+ const hasUsage = event?.type === 'step.end' && hasOwn(event, 'usage');
59
71
  if (
60
72
  event?.type !== 'step.end' ||
61
73
  (
74
+ !hasOwn(event, 'uuid') &&
62
75
  !hasOwn(event, 'turnId') &&
63
76
  !hasOwn(event, 'step') &&
77
+ !hasUsage &&
64
78
  !hasRoutineFinishReason &&
65
79
  !STEP_END_LIVE_ONLY_FIELDS.some((field) => hasOwn(event, field))
66
80
  )
@@ -69,6 +83,7 @@ function projectLoopEventForRecord(event) {
69
83
  }
70
84
 
71
85
  const {
86
+ uuid: _uuid,
72
87
  turnId: _turnId,
73
88
  step: _step,
74
89
  llmFirstTokenLatencyMs: _llmFirstTokenLatencyMs,
@@ -78,11 +93,15 @@ function projectLoopEventForRecord(event) {
78
93
  llmServerDecodeMs: _llmServerDecodeMs,
79
94
  llmClientConsumeMs: _llmClientConsumeMs,
80
95
  messageId: _messageId,
96
+ usage: _usage,
81
97
  ...recordedEvent
82
98
  } = event;
83
- if (!hasRoutineFinishReason) return recordedEvent;
84
- const { finishReason: _finishReason, ...withoutRoutineFinishReason } = recordedEvent;
99
+ const compactUsageEvent = hasUsage
100
+ ? { ...recordedEvent, contextTokens: usageTotal(event.usage) }
101
+ : recordedEvent;
102
+ if (!hasRoutineFinishReason) return compactUsageEvent;
103
+ const { finishReason: _finishReason, ...withoutRoutineFinishReason } = compactUsageEvent;
85
104
  return withoutRoutineFinishReason;
86
105
  }
87
106
 
88
- module.exports = { projectLoopEventForRecord };
107
+ module.exports = { projectLoopEventForRecord, resolveCompletedStepUuid };
package/blun.mjs CHANGED
@@ -75992,7 +75992,7 @@ var init_full = __esmMin((() => {
75992
75992
  }));
75993
75993
  //#endregion
75994
75994
  //#region ../../packages/agent-core/src/agent/compaction/micro.ts
75995
- var selectMicroCompactionCutoff, MICRO_COMPACTION_RECENT_MESSAGES, MICRO_COMPACTION_TOOL_ARGUMENTS_ENABLED, MICRO_COMPACTION_TOOL_ARGUMENT_MIN_TOKENS, isPersistedToolResultReference, projectHistoricalUnaddressedTelegramMessages, dedupeRepeatedUserMessages, projectRepeatedAssistantResponses, compactHistoricalSkillActivations, dedupeRecurringCronWakeups, dedupeRepeatedInjections, projectLoopEventForRecord, DEFAULT_CONFIG, MicroCompaction;
75995
+ var selectMicroCompactionCutoff, MICRO_COMPACTION_RECENT_MESSAGES, MICRO_COMPACTION_TOOL_ARGUMENTS_ENABLED, MICRO_COMPACTION_TOOL_ARGUMENT_MIN_TOKENS, isPersistedToolResultReference, projectHistoricalUnaddressedTelegramMessages, dedupeRepeatedUserMessages, projectRepeatedAssistantResponses, compactHistoricalSkillActivations, dedupeRecurringCronWakeups, dedupeRepeatedInjections, projectLoopEventForRecord, resolveCompletedStepUuid, DEFAULT_CONFIG, MicroCompaction;
75996
75996
  var init_micro = __esmMin((() => {
75997
75997
  ({ selectMicroCompactionCutoff, MICRO_COMPACTION_RECENT_MESSAGES, MICRO_COMPACTION_TOOL_ARGUMENTS_ENABLED, MICRO_COMPACTION_TOOL_ARGUMENT_MIN_TOKENS } = createRequire(import.meta.url)("./bin/micro-compaction-policy.cjs"));
75998
75998
  ({ isPersistedToolResultReference } = createRequire(import.meta.url)("./bin/tool-result-offload-policy.cjs"));
@@ -76001,7 +76001,7 @@ var init_micro = __esmMin((() => {
76001
76001
  ({ projectRepeatedAssistantResponses } = createRequire(import.meta.url)("./bin/repeated-assistant-response-policy.cjs"));
76002
76002
  ({ compactHistoricalSkillActivations } = createRequire(import.meta.url)("./bin/skill-activation-performance-policy.cjs"));
76003
76003
  ({ dedupeRecurringCronWakeups, dedupeRepeatedInjections } = createRequire(import.meta.url)("./bin/repeated-injection-projection.cjs"));
76004
- ({ projectLoopEventForRecord } = createRequire(import.meta.url)("./bin/loop-event-record-policy.cjs"));
76004
+ ({ projectLoopEventForRecord, resolveCompletedStepUuid } = createRequire(import.meta.url)("./bin/loop-event-record-policy.cjs"));
76005
76005
  init_tokens();
76006
76006
  DEFAULT_CONFIG = {
76007
76007
  keepRecentMessages: MICRO_COMPACTION_RECENT_MESSAGES,
@@ -79130,12 +79130,13 @@ var init_context$2 = __esmMin((() => {
79130
79130
  return;
79131
79131
  }
79132
79132
  case "step.end": {
79133
- const openStep = this.openSteps.get(event.uuid);
79134
- this.openSteps.delete(event.uuid);
79135
- if (event.usage !== void 0) {
79133
+ const completedStepUuid = resolveCompletedStepUuid(this.openSteps, event);
79134
+ const openStep = this.openSteps.get(completedStepUuid);
79135
+ this.openSteps.delete(completedStepUuid);
79136
+ if (event.usage !== void 0 || event.contextTokens !== void 0) {
79136
79137
  const openStepIndex = openStep === void 0 ? -1 : this._history.indexOf(openStep);
79137
79138
  const coveredCount = openStepIndex === -1 ? this._history.length : openStepIndex + 1;
79138
- const totalUsage = event.usage.inputCacheRead + event.usage.inputCacheCreation + event.usage.inputOther + event.usage.output;
79139
+ const totalUsage = event.contextTokens ?? event.usage.inputCacheRead + event.usage.inputCacheCreation + event.usage.inputOther + event.usage.output;
79139
79140
  if (totalUsage > 0) this._tokenCount = totalUsage;
79140
79141
  else {
79141
79142
  const previousCoveredCount = this.tokenCountCoveredMessageCount;
@@ -324048,10 +324049,12 @@ function reduceWireRecords(records) {
324048
324049
  openSteps.set(event.uuid, entry);
324049
324050
  return;
324050
324051
  }
324051
- case "step.end":
324052
- openSteps.delete(event.uuid);
324052
+ case "step.end": {
324053
+ const completedStepUuid = resolveCompletedStepUuid(openSteps, event);
324054
+ openSteps.delete(completedStepUuid);
324053
324055
  flushDeferredIfToolExchangeClosed();
324054
324056
  return;
324057
+ }
324055
324058
  case "content.part":
324056
324059
  openSteps.get(event.stepUuid)?.message.content.push(event.part);
324057
324060
  return;
@@ -398968,11 +398971,12 @@ function projectContext(entries, mode = "model") {
398968
398971
  });
398969
398972
  }
398970
398973
  } else if (ev.type === "step.end") {
398971
- if ("usage" in ev && ev.usage !== void 0) {
398972
- const fill = ev.usage.inputCacheRead + ev.usage.inputCacheCreation + ev.usage.inputOther + ev.usage.output;
398974
+ if ("usage" in ev && ev.usage !== void 0 || ev.contextTokens !== void 0) {
398975
+ const fill = ev.contextTokens ?? ev.usage.inputCacheRead + ev.usage.inputCacheCreation + ev.usage.inputOther + ev.usage.output;
398973
398976
  if (fill > 0) contextTokens = fill;
398974
398977
  }
398975
- openSteps.delete(ev.uuid);
398978
+ const completedStepUuid = resolveCompletedStepUuid(openSteps, ev);
398979
+ openSteps.delete(completedStepUuid);
398976
398980
  } else if (ev.type === "tool.result") {
398977
398981
  const toolMsg = {
398978
398982
  role: "tool",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "blun-king-cli",
3
- "version": "9.1.243",
3
+ "version": "9.1.245",
4
4
  "description": "BLUN CLI - your own AI agent with a Telegram channel. Get it done. With BLUN.",
5
5
  "license": "MIT",
6
6
  "bin": {