@cookielab.io/oopst 0.6.1 → 0.6.2

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.js CHANGED
@@ -2519,7 +2519,7 @@ function isObject(value) {
2519
2519
  function isFiniteNumber(value) {
2520
2520
  return typeof value === "number" && Number.isFinite(value);
2521
2521
  }
2522
- function isCodexUsageDelta(value) {
2522
+ function isCodexUsageCounters(value) {
2523
2523
  if (!isObject(value)) {
2524
2524
  return false;
2525
2525
  }
@@ -2557,10 +2557,10 @@ function isCodexTokenCountEvent(value) {
2557
2557
  if (info["model"] !== void 0 && typeof info["model"] !== "string") {
2558
2558
  return false;
2559
2559
  }
2560
- if (info["last_token_usage"] !== void 0 && !isCodexUsageDelta(info["last_token_usage"])) {
2560
+ if (info["last_token_usage"] !== void 0 && !isCodexUsageCounters(info["last_token_usage"])) {
2561
2561
  return false;
2562
2562
  }
2563
- if (info["total_token_usage"] !== void 0 && !isCodexUsageDelta(info["total_token_usage"])) {
2563
+ if (info["total_token_usage"] !== void 0 && !isCodexUsageCounters(info["total_token_usage"])) {
2564
2564
  return false;
2565
2565
  }
2566
2566
  }
@@ -2622,8 +2622,11 @@ function isCodexSessionMetaEvent(value) {
2622
2622
  }
2623
2623
  return true;
2624
2624
  }
2625
- function pickCodexDelta(event) {
2626
- return event.payload.info?.last_token_usage ?? event.payload.info?.total_token_usage;
2625
+ function extractCodexUsageSnapshot(event) {
2626
+ return {
2627
+ last: event.payload.info?.last_token_usage,
2628
+ total: event.payload.info?.total_token_usage
2629
+ };
2627
2630
  }
2628
2631
  function pickCodexEventModel(event) {
2629
2632
  return event.payload.info?.model ?? event.payload.model ?? event.model;
@@ -2646,6 +2649,8 @@ function makeCodexFileAccumulator() {
2646
2649
  excluded: false,
2647
2650
  lineNo: 0,
2648
2651
  sawTurnContext: false,
2652
+ previousTotalUsage: void 0,
2653
+ pendingUsage: [],
2649
2654
  sessionLastSeenAt: void 0,
2650
2655
  sessionStartedAt: void 0
2651
2656
  };
@@ -2694,10 +2699,47 @@ function lineStreamFromFile(path) {
2694
2699
  )
2695
2700
  );
2696
2701
  }
2702
+ function normalizeUsage(usage) {
2703
+ return {
2704
+ inputTokens: Math.max(0, usage.input_tokens ?? 0),
2705
+ outputTokens: Math.max(0, usage.output_tokens ?? 0),
2706
+ cacheReadTokens: Math.max(0, usage.cached_input_tokens ?? usage.cache_read_input_tokens ?? 0)
2707
+ };
2708
+ }
2709
+ function sameUsage(a, b) {
2710
+ return a.inputTokens === b.inputTokens && a.outputTokens === b.outputTokens && a.cacheReadTokens === b.cacheReadTokens;
2711
+ }
2712
+ function positiveUsage(usage) {
2713
+ return usage.inputTokens > 0 || usage.outputTokens > 0 || usage.cacheReadTokens > 0;
2714
+ }
2715
+ function cumulativeDelta(current, previous) {
2716
+ if (previous === void 0) {
2717
+ return current;
2718
+ }
2719
+ return {
2720
+ inputTokens: current.inputTokens < previous.inputTokens ? current.inputTokens : current.inputTokens - previous.inputTokens,
2721
+ outputTokens: current.outputTokens < previous.outputTokens ? current.outputTokens : current.outputTokens - previous.outputTokens,
2722
+ cacheReadTokens: current.cacheReadTokens < previous.cacheReadTokens ? current.cacheReadTokens : current.cacheReadTokens - previous.cacheReadTokens
2723
+ };
2724
+ }
2725
+ function usageIncrement(acc, last, total) {
2726
+ const normalizedTotal = total === void 0 ? void 0 : normalizeUsage(total);
2727
+ if (normalizedTotal !== void 0) {
2728
+ if (acc.previousTotalUsage !== void 0 && sameUsage(acc.previousTotalUsage, normalizedTotal)) {
2729
+ return void 0;
2730
+ }
2731
+ const increment2 = last === void 0 ? cumulativeDelta(normalizedTotal, acc.previousTotalUsage) : normalizeUsage(last);
2732
+ acc.previousTotalUsage = normalizedTotal;
2733
+ return positiveUsage(increment2) ? increment2 : void 0;
2734
+ }
2735
+ if (last === void 0) {
2736
+ return void 0;
2737
+ }
2738
+ const increment = normalizeUsage(last);
2739
+ return positiveUsage(increment) ? increment : void 0;
2740
+ }
2697
2741
  function addToBucket(acc, hourUtc, model, delta) {
2698
- const inputTokens = Math.max(0, delta.input_tokens ?? 0);
2699
- const outputTokens = Math.max(0, delta.output_tokens ?? 0);
2700
- const cacheReadTokens = Math.max(0, delta.cached_input_tokens ?? delta.cache_read_input_tokens ?? 0);
2742
+ const { inputTokens, outputTokens, cacheReadTokens } = delta;
2701
2743
  if (inputTokens === 0 && outputTokens === 0 && cacheReadTokens === 0) {
2702
2744
  return false;
2703
2745
  }
@@ -2719,6 +2761,14 @@ function addToBucket(acc, hourUtc, model, delta) {
2719
2761
  existing.cacheReadTokens += cacheReadTokens;
2720
2762
  return true;
2721
2763
  }
2764
+ function flushPendingUsage(acc, model) {
2765
+ for (const pending of acc.pendingUsage) {
2766
+ if (addToBucket(acc, startOfUtcHour(pending.eventAt), model, pending.usage)) {
2767
+ markSessionSeen(acc, pending.eventAt);
2768
+ }
2769
+ }
2770
+ acc.pendingUsage = [];
2771
+ }
2722
2772
  function markSessionSeen(acc, eventAt) {
2723
2773
  if (acc.sessionStartedAt === void 0 || eventAt.getTime() < acc.sessionStartedAt.getTime()) {
2724
2774
  acc.sessionStartedAt = eventAt;
@@ -2748,6 +2798,7 @@ function accumulateCodexLine(context, acc, raw) {
2748
2798
  if (shouldExcludePath(parsed.payload.cwd, options.excludePath)) {
2749
2799
  acc.excluded = true;
2750
2800
  acc.buckets.clear();
2801
+ acc.pendingUsage = [];
2751
2802
  }
2752
2803
  return acc;
2753
2804
  }
@@ -2755,12 +2806,14 @@ function accumulateCodexLine(context, acc, raw) {
2755
2806
  if (shouldExcludePath(parsed.payload.cwd, options.excludePath)) {
2756
2807
  acc.excluded = true;
2757
2808
  acc.buckets.clear();
2809
+ acc.pendingUsage = [];
2758
2810
  return acc;
2759
2811
  }
2760
2812
  const model2 = parsed.payload.model ?? parsed.payload.info?.model;
2761
2813
  if (model2 !== void 0) {
2762
2814
  acc.currentModel = model2;
2763
2815
  acc.sawTurnContext = true;
2816
+ flushPendingUsage(acc, model2);
2764
2817
  }
2765
2818
  return acc;
2766
2819
  }
@@ -2771,16 +2824,18 @@ function accumulateCodexLine(context, acc, raw) {
2771
2824
  if (!Number.isFinite(tsMs)) {
2772
2825
  return acc;
2773
2826
  }
2827
+ const snapshot = extractCodexUsageSnapshot(parsed);
2828
+ const delta = usageIncrement(acc, snapshot.last, snapshot.total);
2829
+ if (delta === void 0) {
2830
+ return acc;
2831
+ }
2832
+ const eventAt = new Date(tsMs);
2774
2833
  const eventModel = pickCodexEventModel(parsed);
2775
2834
  const model = acc.sawTurnContext ? acc.currentModel ?? eventModel : eventModel ?? acc.currentModel;
2776
2835
  if (model === void 0) {
2836
+ acc.pendingUsage.push({ eventAt, usage: delta });
2777
2837
  return acc;
2778
2838
  }
2779
- const delta = pickCodexDelta(parsed);
2780
- if (delta === void 0) {
2781
- return acc;
2782
- }
2783
- const eventAt = new Date(tsMs);
2784
2839
  if (addToBucket(acc, startOfUtcHour(eventAt), model, delta)) {
2785
2840
  markSessionSeen(acc, eventAt);
2786
2841
  }