@cognipeer/agent-sdk 0.8.7 → 0.8.8

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
@@ -812,6 +812,28 @@ function normalizeUsage(raw) {
812
812
  };
813
813
  return normalized;
814
814
  }
815
+ function recordUsage(state, modelName, normalized) {
816
+ const usageState = state.usage || { perRequest: [], totals: {} };
817
+ const id = `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
818
+ const cachedInput = normalized.prompt_tokens_details?.cached_tokens;
819
+ usageState.perRequest.push({
820
+ id,
821
+ modelName,
822
+ usage: normalized,
823
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
824
+ turn: usageState.perRequest.length + 1,
825
+ cachedInput
826
+ });
827
+ const previous = usageState.totals[modelName] || { input: 0, output: 0, total: 0, cachedInput: 0 };
828
+ usageState.totals[modelName] = {
829
+ input: previous.input + (Number(normalized.prompt_tokens) || 0),
830
+ output: previous.output + (Number(normalized.completion_tokens) || 0),
831
+ total: previous.total + (Number(normalized.total_tokens) || 0),
832
+ cachedInput: previous.cachedInput + (Number(cachedInput) || 0)
833
+ };
834
+ state.usage = usageState;
835
+ return id;
836
+ }
815
837
  function num(...vals) {
816
838
  for (const v of vals) {
817
839
  if (v === 0) return 0;
@@ -3181,24 +3203,7 @@ function createAgentCoreNode(opts) {
3181
3203
  messageList: messagesWithResponse
3182
3204
  });
3183
3205
  if (normalized) {
3184
- const usageState = state.usage || { perRequest: [], totals: {} };
3185
- const id = `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
3186
- const turn = usageState.perRequest.length + 1;
3187
- const timestamp = (/* @__PURE__ */ new Date()).toISOString();
3188
- const cachedInputTok = normalized.prompt_tokens_details?.cached_tokens;
3189
- usageState.perRequest.push({ id, modelName, usage: normalized, timestamp, turn, cachedInput: cachedInputTok });
3190
- const inputTok = normalized.prompt_tokens;
3191
- const outputTok = normalized.completion_tokens;
3192
- const totalTok = normalized.total_tokens;
3193
- const key = modelName;
3194
- const agg = usageState.totals[key] || { input: 0, output: 0, total: 0, cachedInput: 0 };
3195
- usageState.totals[key] = {
3196
- input: agg.input + (Number(inputTok) || 0),
3197
- output: agg.output + (Number(outputTok) || 0),
3198
- total: agg.total + (Number(totalTok) || 0),
3199
- cachedInput: agg.cachedInput + (Number(cachedInputTok) || 0)
3200
- };
3201
- state.usage = usageState;
3206
+ recordUsage(state, modelName, normalized);
3202
3207
  }
3203
3208
  return { messages: messagesWithResponse, usage: state.usage };
3204
3209
  };
@@ -5891,24 +5896,7 @@ function createAgentNode(opts) {
5891
5896
  messageList: messagesWithResponse
5892
5897
  });
5893
5898
  if (normalized) {
5894
- const usageState = state.usage || { perRequest: [], totals: {} };
5895
- const id = `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
5896
- const turn = usageState.perRequest.length + 1;
5897
- const timestamp = (/* @__PURE__ */ new Date()).toISOString();
5898
- const cachedInputTok = normalized.prompt_tokens_details.cached_tokens;
5899
- usageState.perRequest.push({ id, modelName, usage: normalized, timestamp, turn, cachedInput: cachedInputTok });
5900
- const inputTok = normalized.prompt_tokens;
5901
- const outputTok = normalized.completion_tokens;
5902
- const totalTok = normalized.total_tokens;
5903
- const key = modelName;
5904
- const agg = usageState.totals[key] || { input: 0, output: 0, total: 0, cachedInput: 0 };
5905
- usageState.totals[key] = {
5906
- input: agg.input + (Number(inputTok) || 0),
5907
- output: agg.output + (Number(outputTok) || 0),
5908
- total: agg.total + (Number(totalTok) || 0),
5909
- cachedInput: agg.cachedInput + (Number(cachedInputTok) || 0)
5910
- };
5911
- state.usage = usageState;
5899
+ recordUsage(state, modelName, normalized);
5912
5900
  }
5913
5901
  return { messages: messagesWithResponse, usage: state.usage };
5914
5902
  };
@@ -6358,6 +6346,7 @@ ${canonicalFacts.length > 0 ? canonicalFacts.map((fact) => `- ${fact.key}: ${fac
6358
6346
  outputTokensActual = normalized.completion_tokens;
6359
6347
  totalTokensActual = normalized.total_tokens;
6360
6348
  cachedInputTokens = normalized.prompt_tokens_details?.cached_tokens;
6349
+ recordUsage(state, getModelName(model) || "unknown_model", normalized);
6361
6350
  }
6362
6351
  } catch (err) {
6363
6352
  durationMs = Date.now() - startTime;