@cognipeer/agent-sdk 0.8.5 → 0.8.6

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.mjs CHANGED
@@ -798,6 +798,28 @@ function normalizeUsage(raw) {
798
798
  };
799
799
  return normalized;
800
800
  }
801
+ function recordUsage(state, modelName, normalized) {
802
+ const usageState = state.usage || { perRequest: [], totals: {} };
803
+ const id = `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
804
+ const cachedInput = normalized.prompt_tokens_details?.cached_tokens;
805
+ usageState.perRequest.push({
806
+ id,
807
+ modelName,
808
+ usage: normalized,
809
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
810
+ turn: usageState.perRequest.length + 1,
811
+ cachedInput
812
+ });
813
+ const previous = usageState.totals[modelName] || { input: 0, output: 0, total: 0, cachedInput: 0 };
814
+ usageState.totals[modelName] = {
815
+ input: previous.input + (Number(normalized.prompt_tokens) || 0),
816
+ output: previous.output + (Number(normalized.completion_tokens) || 0),
817
+ total: previous.total + (Number(normalized.total_tokens) || 0),
818
+ cachedInput: previous.cachedInput + (Number(cachedInput) || 0)
819
+ };
820
+ state.usage = usageState;
821
+ return id;
822
+ }
801
823
  function num(...vals) {
802
824
  for (const v of vals) {
803
825
  if (v === 0) return 0;
@@ -2936,24 +2958,7 @@ function createAgentCoreNode(opts) {
2936
2958
  messageList: messagesWithResponse
2937
2959
  });
2938
2960
  if (normalized) {
2939
- const usageState = state.usage || { perRequest: [], totals: {} };
2940
- const id = `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
2941
- const turn = usageState.perRequest.length + 1;
2942
- const timestamp = (/* @__PURE__ */ new Date()).toISOString();
2943
- const cachedInputTok = normalized.prompt_tokens_details?.cached_tokens;
2944
- usageState.perRequest.push({ id, modelName, usage: normalized, timestamp, turn, cachedInput: cachedInputTok });
2945
- const inputTok = normalized.prompt_tokens;
2946
- const outputTok = normalized.completion_tokens;
2947
- const totalTok = normalized.total_tokens;
2948
- const key = modelName;
2949
- const agg = usageState.totals[key] || { input: 0, output: 0, total: 0, cachedInput: 0 };
2950
- usageState.totals[key] = {
2951
- input: agg.input + (Number(inputTok) || 0),
2952
- output: agg.output + (Number(outputTok) || 0),
2953
- total: agg.total + (Number(totalTok) || 0),
2954
- cachedInput: agg.cachedInput + (Number(cachedInputTok) || 0)
2955
- };
2956
- state.usage = usageState;
2961
+ recordUsage(state, modelName, normalized);
2957
2962
  }
2958
2963
  return { messages: messagesWithResponse, usage: state.usage };
2959
2964
  };
@@ -5736,24 +5741,7 @@ function createAgentNode(opts) {
5736
5741
  messageList: messagesWithResponse
5737
5742
  });
5738
5743
  if (normalized) {
5739
- const usageState = state.usage || { perRequest: [], totals: {} };
5740
- const id = `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
5741
- const turn = usageState.perRequest.length + 1;
5742
- const timestamp = (/* @__PURE__ */ new Date()).toISOString();
5743
- const cachedInputTok = normalized.prompt_tokens_details.cached_tokens;
5744
- usageState.perRequest.push({ id, modelName, usage: normalized, timestamp, turn, cachedInput: cachedInputTok });
5745
- const inputTok = normalized.prompt_tokens;
5746
- const outputTok = normalized.completion_tokens;
5747
- const totalTok = normalized.total_tokens;
5748
- const key = modelName;
5749
- const agg = usageState.totals[key] || { input: 0, output: 0, total: 0, cachedInput: 0 };
5750
- usageState.totals[key] = {
5751
- input: agg.input + (Number(inputTok) || 0),
5752
- output: agg.output + (Number(outputTok) || 0),
5753
- total: agg.total + (Number(totalTok) || 0),
5754
- cachedInput: agg.cachedInput + (Number(cachedInputTok) || 0)
5755
- };
5756
- state.usage = usageState;
5744
+ recordUsage(state, modelName, normalized);
5757
5745
  }
5758
5746
  return { messages: messagesWithResponse, usage: state.usage };
5759
5747
  };
@@ -6197,6 +6185,7 @@ ${canonicalFacts.length > 0 ? canonicalFacts.map((fact) => `- ${fact.key}: ${fac
6197
6185
  outputTokensActual = normalized.completion_tokens;
6198
6186
  totalTokensActual = normalized.total_tokens;
6199
6187
  cachedInputTokens = normalized.prompt_tokens_details?.cached_tokens;
6188
+ recordUsage(state, getModelName(model) || "unknown_model", normalized);
6200
6189
  }
6201
6190
  } catch (err) {
6202
6191
  durationMs = Date.now() - startTime;