@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.mjs CHANGED
@@ -802,6 +802,28 @@ function normalizeUsage(raw) {
802
802
  };
803
803
  return normalized;
804
804
  }
805
+ function recordUsage(state, modelName, normalized) {
806
+ const usageState = state.usage || { perRequest: [], totals: {} };
807
+ const id = `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
808
+ const cachedInput = normalized.prompt_tokens_details?.cached_tokens;
809
+ usageState.perRequest.push({
810
+ id,
811
+ modelName,
812
+ usage: normalized,
813
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
814
+ turn: usageState.perRequest.length + 1,
815
+ cachedInput
816
+ });
817
+ const previous = usageState.totals[modelName] || { input: 0, output: 0, total: 0, cachedInput: 0 };
818
+ usageState.totals[modelName] = {
819
+ input: previous.input + (Number(normalized.prompt_tokens) || 0),
820
+ output: previous.output + (Number(normalized.completion_tokens) || 0),
821
+ total: previous.total + (Number(normalized.total_tokens) || 0),
822
+ cachedInput: previous.cachedInput + (Number(cachedInput) || 0)
823
+ };
824
+ state.usage = usageState;
825
+ return id;
826
+ }
805
827
  function num(...vals) {
806
828
  for (const v of vals) {
807
829
  if (v === 0) return 0;
@@ -3171,24 +3193,7 @@ function createAgentCoreNode(opts) {
3171
3193
  messageList: messagesWithResponse
3172
3194
  });
3173
3195
  if (normalized) {
3174
- const usageState = state.usage || { perRequest: [], totals: {} };
3175
- const id = `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
3176
- const turn = usageState.perRequest.length + 1;
3177
- const timestamp = (/* @__PURE__ */ new Date()).toISOString();
3178
- const cachedInputTok = normalized.prompt_tokens_details?.cached_tokens;
3179
- usageState.perRequest.push({ id, modelName, usage: normalized, timestamp, turn, cachedInput: cachedInputTok });
3180
- const inputTok = normalized.prompt_tokens;
3181
- const outputTok = normalized.completion_tokens;
3182
- const totalTok = normalized.total_tokens;
3183
- const key = modelName;
3184
- const agg = usageState.totals[key] || { input: 0, output: 0, total: 0, cachedInput: 0 };
3185
- usageState.totals[key] = {
3186
- input: agg.input + (Number(inputTok) || 0),
3187
- output: agg.output + (Number(outputTok) || 0),
3188
- total: agg.total + (Number(totalTok) || 0),
3189
- cachedInput: agg.cachedInput + (Number(cachedInputTok) || 0)
3190
- };
3191
- state.usage = usageState;
3196
+ recordUsage(state, modelName, normalized);
3192
3197
  }
3193
3198
  return { messages: messagesWithResponse, usage: state.usage };
3194
3199
  };
@@ -5881,24 +5886,7 @@ function createAgentNode(opts) {
5881
5886
  messageList: messagesWithResponse
5882
5887
  });
5883
5888
  if (normalized) {
5884
- const usageState = state.usage || { perRequest: [], totals: {} };
5885
- const id = `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
5886
- const turn = usageState.perRequest.length + 1;
5887
- const timestamp = (/* @__PURE__ */ new Date()).toISOString();
5888
- const cachedInputTok = normalized.prompt_tokens_details.cached_tokens;
5889
- usageState.perRequest.push({ id, modelName, usage: normalized, timestamp, turn, cachedInput: cachedInputTok });
5890
- const inputTok = normalized.prompt_tokens;
5891
- const outputTok = normalized.completion_tokens;
5892
- const totalTok = normalized.total_tokens;
5893
- const key = modelName;
5894
- const agg = usageState.totals[key] || { input: 0, output: 0, total: 0, cachedInput: 0 };
5895
- usageState.totals[key] = {
5896
- input: agg.input + (Number(inputTok) || 0),
5897
- output: agg.output + (Number(outputTok) || 0),
5898
- total: agg.total + (Number(totalTok) || 0),
5899
- cachedInput: agg.cachedInput + (Number(cachedInputTok) || 0)
5900
- };
5901
- state.usage = usageState;
5889
+ recordUsage(state, modelName, normalized);
5902
5890
  }
5903
5891
  return { messages: messagesWithResponse, usage: state.usage };
5904
5892
  };
@@ -6348,6 +6336,7 @@ ${canonicalFacts.length > 0 ? canonicalFacts.map((fact) => `- ${fact.key}: ${fac
6348
6336
  outputTokensActual = normalized.completion_tokens;
6349
6337
  totalTokensActual = normalized.total_tokens;
6350
6338
  cachedInputTokens = normalized.prompt_tokens_details?.cached_tokens;
6339
+ recordUsage(state, getModelName(model) || "unknown_model", normalized);
6351
6340
  }
6352
6341
  } catch (err) {
6353
6342
  durationMs = Date.now() - startTime;