@genesislcap/ai-assistant 15.12.0 → 15.13.0

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 (50) hide show
  1. package/dist/ai-assistant.api.json +245 -3
  2. package/dist/ai-assistant.d.ts +127 -7
  3. package/dist/chat-driver.cjs +79 -13
  4. package/dist/chat-driver.cjs.map +2 -2
  5. package/dist/chat-driver.mjs +76 -12
  6. package/dist/chat-driver.mjs.map +2 -2
  7. package/dist/custom-elements.json +452 -359
  8. package/dist/dts/chat-driver-node.d.ts +2 -2
  9. package/dist/dts/chat-driver-node.d.ts.map +1 -1
  10. package/dist/dts/components/chat-driver/chat-driver.d.ts +45 -1
  11. package/dist/dts/components/chat-driver/chat-driver.d.ts.map +1 -1
  12. package/dist/dts/components/chat-driver/chat-driver.turn-usage.test.d.ts +2 -0
  13. package/dist/dts/components/chat-driver/chat-driver.turn-usage.test.d.ts.map +1 -0
  14. package/dist/dts/main/main.d.ts +20 -1
  15. package/dist/dts/main/main.d.ts.map +1 -1
  16. package/dist/dts/state/debug-event-log.d.ts.map +1 -1
  17. package/dist/dts/state/persistence/diagnostics.d.ts +65 -8
  18. package/dist/dts/state/persistence/diagnostics.d.ts.map +1 -1
  19. package/dist/dts/state/persistence/index.d.ts +1 -1
  20. package/dist/dts/state/persistence/index.d.ts.map +1 -1
  21. package/dist/dts/state/persistence/session-persister.d.ts +4 -3
  22. package/dist/dts/state/persistence/session-persister.d.ts.map +1 -1
  23. package/dist/dts/utils/sum-usage.d.ts +20 -0
  24. package/dist/dts/utils/sum-usage.d.ts.map +1 -1
  25. package/dist/esm/chat-driver-node.js +12 -2
  26. package/dist/esm/components/chat-driver/chat-driver.js +60 -5
  27. package/dist/esm/components/chat-driver/chat-driver.turn-usage.test.js +268 -0
  28. package/dist/esm/main/main.js +53 -28
  29. package/dist/esm/state/debug-event-log.js +7 -2
  30. package/dist/esm/state/persistence/diagnostics.js +79 -16
  31. package/dist/esm/state/persistence/diagnostics.test.js +174 -1
  32. package/dist/esm/state/persistence/index.js +1 -1
  33. package/dist/esm/state/persistence/session-persister.js +13 -5
  34. package/dist/esm/state/persistence/session-persister.test.js +31 -0
  35. package/dist/esm/utils/sum-usage.js +43 -0
  36. package/dist/esm/utils/sum-usage.test.js +45 -1
  37. package/dist/tsconfig.tsbuildinfo +1 -1
  38. package/package.json +17 -17
  39. package/src/chat-driver-node.ts +12 -2
  40. package/src/components/chat-driver/chat-driver.ts +107 -6
  41. package/src/components/chat-driver/chat-driver.turn-usage.test.ts +362 -0
  42. package/src/main/main.ts +52 -23
  43. package/src/state/debug-event-log.ts +7 -2
  44. package/src/state/persistence/diagnostics.test.ts +208 -1
  45. package/src/state/persistence/diagnostics.ts +117 -15
  46. package/src/state/persistence/index.ts +1 -1
  47. package/src/state/persistence/session-persister.test.ts +37 -0
  48. package/src/state/persistence/session-persister.ts +13 -5
  49. package/src/utils/sum-usage.test.ts +52 -1
  50. package/src/utils/sum-usage.ts +45 -0
@@ -51,12 +51,14 @@ __export(chat_driver_node_exports, {
51
51
  geminiTokenCost: () => geminiTokenCost,
52
52
  getMetaEvents: () => getMetaEvents,
53
53
  isObservableAIProviderRegistry: () => isObservableAIProviderRegistry,
54
+ messageUsage: () => messageUsage,
54
55
  restoreMachine: () => restoreMachine,
55
56
  strictFallbackAgent: () => strictFallbackAgent,
56
57
  sumUsage: () => sumUsage,
57
58
  totalTokens: () => totalTokens,
58
59
  usageRows: () => usageRows,
59
- vendorOfModel: () => vendorOfModel
60
+ vendorOfModel: () => vendorOfModel,
61
+ withFreshMetaSnapshot: () => withFreshMetaSnapshot
60
62
  });
61
63
  module.exports = __toCommonJS(chat_driver_node_exports);
62
64
 
@@ -3617,6 +3619,24 @@ function addUsage(a, b) {
3617
3619
  outputTokens: a.outputTokens + b.outputTokens
3618
3620
  };
3619
3621
  }
3622
+ function messageUsage(m) {
3623
+ if (m.cost == null && m.externalCostUsd == null && m.inputTokens == null && m.outputTokens == null && m.cacheReadTokens == null && m.cacheWriteTokens == null) {
3624
+ return void 0;
3625
+ }
3626
+ const cacheReadTokens = m.cacheReadTokens ?? 0;
3627
+ const cacheWriteTokens = m.cacheWriteTokens ?? 0;
3628
+ return {
3629
+ costUsd: (m.cost ?? 0) + (m.externalCostUsd ?? 0),
3630
+ // The uncached REMAINDER, clamped — the same arithmetic `accumulate` below applies,
3631
+ // for the same reasons. The two are deliberately separate implementations (that walk
3632
+ // mutates one accumulator rather than allocating per message) and must agree;
3633
+ // `sum-usage.test.ts` pins them together.
3634
+ uncachedInputTokens: Math.max(0, (m.inputTokens ?? 0) - cacheReadTokens - cacheWriteTokens),
3635
+ cacheReadTokens,
3636
+ cacheWriteTokens,
3637
+ outputTokens: m.outputTokens ?? 0
3638
+ };
3639
+ }
3620
3640
  function sumUsage(messages) {
3621
3641
  const total = emptyUsage();
3622
3642
  accumulate(messages, total);
@@ -4190,7 +4210,14 @@ var ChatDriver = class _ChatDriver extends EventTarget {
4190
4210
  if (resolvedName !== this.lastDispatchedProviderName) {
4191
4211
  this.lastDispatchedProviderName = resolvedName;
4192
4212
  recordMetaEvent(this.sessionKey, "provider.selected", {
4213
+ // `provider` is the registry SLOT (a tier name like 'high'), kept under that key
4214
+ // for compatibility; `model` and `vendor` are what it resolved to. Recording all
4215
+ // three is the difference between "the agent switched to its high tier" and
4216
+ // knowing which model that actually was — a tier can be repointed mid-session,
4217
+ // and a slot name alone cannot distinguish anthropic from gemini.
4193
4218
  provider: resolvedName,
4219
+ model: status.model,
4220
+ vendor: status.provider,
4194
4221
  agent: this.activeAgentName
4195
4222
  });
4196
4223
  this.dispatchEvent(
@@ -4329,6 +4356,10 @@ var ChatDriver = class _ChatDriver extends EventTarget {
4329
4356
  * Push one snapshot to the ring buffer. Called inside `runToolLoop` just
4330
4357
  * before each LLM call — that's the latest point where the prompt, tool
4331
4358
  * surface, and agent state line up with what the model is about to see.
4359
+ *
4360
+ * Returns the pushed object so the caller can back-fill what only the response
4361
+ * knows (`usage`). Mutating it after the fact is safe whether or not the ring
4362
+ * buffer has since evicted it — an evicted snapshot is simply no longer exported.
4332
4363
  */
4333
4364
  recordTurnSnapshot(resolvedSystemPrompt, temperature, toolChoice, tailContext) {
4334
4365
  let agentSnapshot;
@@ -4341,7 +4372,7 @@ var ChatDriver = class _ChatDriver extends EventTarget {
4341
4372
  }
4342
4373
  const turnIndex = String(this.globalTurnIndex);
4343
4374
  this.globalTurnIndex += 1;
4344
- this.turnSnapshots.push({
4375
+ const snapshot = {
4345
4376
  turnIndex,
4346
4377
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
4347
4378
  agentName: this.activeAgentName,
@@ -4352,10 +4383,12 @@ var ChatDriver = class _ChatDriver extends EventTarget {
4352
4383
  temperature,
4353
4384
  toolChoice,
4354
4385
  agentSnapshot
4355
- });
4386
+ };
4387
+ this.turnSnapshots.push(snapshot);
4356
4388
  if (this.turnSnapshots.length > this.maxTurnSnapshots) {
4357
4389
  this.turnSnapshots.shift();
4358
4390
  }
4391
+ return snapshot;
4359
4392
  }
4360
4393
  /**
4361
4394
  * Optional transform applied to conversation history immediately before each LLM request.
@@ -5283,7 +5316,12 @@ Output format (strict):
5283
5316
  ${tailBody}
5284
5317
  </system-reminder>` : void 0;
5285
5318
  const effectiveToolChoice = resolvedToolChoice ?? (this.isSubAgent ? "required" : void 0);
5286
- this.recordTurnSnapshot(systemPrompt, resolvedTemperature, effectiveToolChoice, tailContext);
5319
+ const turnSnapshot = this.recordTurnSnapshot(
5320
+ systemPrompt,
5321
+ resolvedTemperature,
5322
+ effectiveToolChoice,
5323
+ tailContext
5324
+ );
5287
5325
  const userInputForCall = currentInput;
5288
5326
  const attachmentsForCall = currentAttachments;
5289
5327
  currentInput = "";
@@ -5319,6 +5357,12 @@ ${tailBody}
5319
5357
  fallbacks: this.activeFallbacks
5320
5358
  };
5321
5359
  const activeProvider = await this.resolveProviderForTurn(promptCtx);
5360
+ if (this.lastResolvedProviderName !== void 0) {
5361
+ turnSnapshot.providerName = this.lastResolvedProviderName;
5362
+ }
5363
+ if (this.lastResolvedProvider !== void 0)
5364
+ turnSnapshot.provider = this.lastResolvedProvider;
5365
+ if (this.lastResolvedModel !== void 0) turnSnapshot.model = this.lastResolvedModel;
5322
5366
  let response;
5323
5367
  try {
5324
5368
  response = await activeProvider.chat(historyForCall, userInputForCall, options);
@@ -5436,6 +5480,8 @@ ${tailBody}
5436
5480
  if (this.lastResolvedProviderName !== void 0) {
5437
5481
  response.providerName = this.lastResolvedProviderName;
5438
5482
  }
5483
+ turnSnapshot.usage = messageUsage(response);
5484
+ if (response.model !== void 0) turnSnapshot.model = response.model;
5439
5485
  const isThinkingStep = response.content && response.toolCalls?.length;
5440
5486
  const isEmptyResponse = !response.content?.trim() && !response.toolCalls?.length;
5441
5487
  const isRefusal = response.responseMeta?.finishReason === "refusal";
@@ -5481,7 +5527,14 @@ ${tailBody}
5481
5527
  return this.turnDone(failureReason2);
5482
5528
  } else {
5483
5529
  const { reasoning, ...rest } = response;
5484
- const displayOnly = { cost: void 0, inputTokens: void 0, outputTokens: void 0 };
5530
+ const displayOnly = {
5531
+ cost: void 0,
5532
+ externalCostUsd: void 0,
5533
+ inputTokens: void 0,
5534
+ outputTokens: void 0,
5535
+ cacheReadTokens: void 0,
5536
+ cacheWriteTokens: void 0
5537
+ };
5485
5538
  if (reasoning) {
5486
5539
  this.appendToHistory({
5487
5540
  ...rest,
@@ -6679,23 +6732,30 @@ function buildTimelineEntries(input) {
6679
6732
  }
6680
6733
 
6681
6734
  // src/state/persistence/diagnostics.ts
6735
+ function turnIdentity(entry) {
6736
+ return `turn::${String(entry.turnIndex ?? "")}::${entry.timestamp ?? ""}::${String(entry.agentName ?? "")}`;
6737
+ }
6682
6738
  var KIND_RANK = { event: 0, turn: 1, message: 2 };
6683
6739
  var UNKNOWN_KIND_RANK = 99;
6684
6740
  function assembleDebugLog(entries, readme) {
6685
- const seen = /* @__PURE__ */ new Set();
6741
+ const seenAt = /* @__PURE__ */ new Map();
6686
6742
  let latestMeta;
6687
6743
  const timeline = [];
6688
6744
  for (const entry of entries) {
6689
- const id = JSON.stringify(entry);
6690
- if (seen.has(id)) continue;
6691
- seen.add(id);
6692
6745
  if (entry.kind === "meta-snapshot") {
6693
6746
  if (!latestMeta || (entry.timestamp ?? "") >= (latestMeta.timestamp ?? "")) {
6694
6747
  latestMeta = entry;
6695
6748
  }
6696
- } else {
6697
- timeline.push(entry);
6749
+ continue;
6698
6750
  }
6751
+ const id = entry.kind === "turn" ? turnIdentity(entry) : JSON.stringify(entry);
6752
+ const at = seenAt.get(id);
6753
+ if (at !== void 0) {
6754
+ if (entry.usage && !timeline[at].usage) timeline[at] = entry;
6755
+ continue;
6756
+ }
6757
+ seenAt.set(id, timeline.length);
6758
+ timeline.push(entry);
6699
6759
  }
6700
6760
  timeline.sort((a, b) => {
6701
6761
  const ta = a.timestamp ?? "";
@@ -6704,7 +6764,11 @@ function assembleDebugLog(entries, readme) {
6704
6764
  if (ta > tb) return 1;
6705
6765
  return (KIND_RANK[a.kind] ?? UNKNOWN_KIND_RANK) - (KIND_RANK[b.kind] ?? UNKNOWN_KIND_RANK);
6706
6766
  });
6707
- return { readme, timeline, meta: latestMeta?.meta };
6767
+ const context = latestMeta?.meta?.context;
6768
+ return { readme, sessionUsage: context?.sessionUsage, timeline, meta: latestMeta?.meta };
6769
+ }
6770
+ function withFreshMetaSnapshot(stored, fresh) {
6771
+ return [...stored.filter((e) => e.kind !== "meta-snapshot"), fresh];
6708
6772
  }
6709
6773
  // Annotate the CommonJS export names for ESM import in node:
6710
6774
  0 && (module.exports = {
@@ -6740,11 +6804,13 @@ function assembleDebugLog(entries, readme) {
6740
6804
  geminiTokenCost,
6741
6805
  getMetaEvents,
6742
6806
  isObservableAIProviderRegistry,
6807
+ messageUsage,
6743
6808
  restoreMachine,
6744
6809
  strictFallbackAgent,
6745
6810
  sumUsage,
6746
6811
  totalTokens,
6747
6812
  usageRows,
6748
- vendorOfModel
6813
+ vendorOfModel,
6814
+ withFreshMetaSnapshot
6749
6815
  });
6750
6816
  //# sourceMappingURL=chat-driver.cjs.map