@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.cjs CHANGED
@@ -808,6 +808,28 @@ function normalizeUsage(raw) {
808
808
  };
809
809
  return normalized;
810
810
  }
811
+ function recordUsage(state, modelName, normalized) {
812
+ const usageState = state.usage || { perRequest: [], totals: {} };
813
+ const id = `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
814
+ const cachedInput = normalized.prompt_tokens_details?.cached_tokens;
815
+ usageState.perRequest.push({
816
+ id,
817
+ modelName,
818
+ usage: normalized,
819
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
820
+ turn: usageState.perRequest.length + 1,
821
+ cachedInput
822
+ });
823
+ const previous = usageState.totals[modelName] || { input: 0, output: 0, total: 0, cachedInput: 0 };
824
+ usageState.totals[modelName] = {
825
+ input: previous.input + (Number(normalized.prompt_tokens) || 0),
826
+ output: previous.output + (Number(normalized.completion_tokens) || 0),
827
+ total: previous.total + (Number(normalized.total_tokens) || 0),
828
+ cachedInput: previous.cachedInput + (Number(cachedInput) || 0)
829
+ };
830
+ state.usage = usageState;
831
+ return id;
832
+ }
811
833
  function num(...vals) {
812
834
  for (const v of vals) {
813
835
  if (v === 0) return 0;
@@ -2946,24 +2968,7 @@ function createAgentCoreNode(opts) {
2946
2968
  messageList: messagesWithResponse
2947
2969
  });
2948
2970
  if (normalized) {
2949
- const usageState = state.usage || { perRequest: [], totals: {} };
2950
- const id = `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
2951
- const turn = usageState.perRequest.length + 1;
2952
- const timestamp = (/* @__PURE__ */ new Date()).toISOString();
2953
- const cachedInputTok = normalized.prompt_tokens_details?.cached_tokens;
2954
- usageState.perRequest.push({ id, modelName, usage: normalized, timestamp, turn, cachedInput: cachedInputTok });
2955
- const inputTok = normalized.prompt_tokens;
2956
- const outputTok = normalized.completion_tokens;
2957
- const totalTok = normalized.total_tokens;
2958
- const key = modelName;
2959
- const agg = usageState.totals[key] || { input: 0, output: 0, total: 0, cachedInput: 0 };
2960
- usageState.totals[key] = {
2961
- input: agg.input + (Number(inputTok) || 0),
2962
- output: agg.output + (Number(outputTok) || 0),
2963
- total: agg.total + (Number(totalTok) || 0),
2964
- cachedInput: agg.cachedInput + (Number(cachedInputTok) || 0)
2965
- };
2966
- state.usage = usageState;
2971
+ recordUsage(state, modelName, normalized);
2967
2972
  }
2968
2973
  return { messages: messagesWithResponse, usage: state.usage };
2969
2974
  };
@@ -5746,24 +5751,7 @@ function createAgentNode(opts) {
5746
5751
  messageList: messagesWithResponse
5747
5752
  });
5748
5753
  if (normalized) {
5749
- const usageState = state.usage || { perRequest: [], totals: {} };
5750
- const id = `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
5751
- const turn = usageState.perRequest.length + 1;
5752
- const timestamp = (/* @__PURE__ */ new Date()).toISOString();
5753
- const cachedInputTok = normalized.prompt_tokens_details.cached_tokens;
5754
- usageState.perRequest.push({ id, modelName, usage: normalized, timestamp, turn, cachedInput: cachedInputTok });
5755
- const inputTok = normalized.prompt_tokens;
5756
- const outputTok = normalized.completion_tokens;
5757
- const totalTok = normalized.total_tokens;
5758
- const key = modelName;
5759
- const agg = usageState.totals[key] || { input: 0, output: 0, total: 0, cachedInput: 0 };
5760
- usageState.totals[key] = {
5761
- input: agg.input + (Number(inputTok) || 0),
5762
- output: agg.output + (Number(outputTok) || 0),
5763
- total: agg.total + (Number(totalTok) || 0),
5764
- cachedInput: agg.cachedInput + (Number(cachedInputTok) || 0)
5765
- };
5766
- state.usage = usageState;
5754
+ recordUsage(state, modelName, normalized);
5767
5755
  }
5768
5756
  return { messages: messagesWithResponse, usage: state.usage };
5769
5757
  };
@@ -6207,6 +6195,7 @@ ${canonicalFacts.length > 0 ? canonicalFacts.map((fact) => `- ${fact.key}: ${fac
6207
6195
  outputTokensActual = normalized.completion_tokens;
6208
6196
  totalTokensActual = normalized.total_tokens;
6209
6197
  cachedInputTokens = normalized.prompt_tokens_details?.cached_tokens;
6198
+ recordUsage(state, getModelName(model) || "unknown_model", normalized);
6210
6199
  }
6211
6200
  } catch (err) {
6212
6201
  durationMs = Date.now() - startTime;