tinker-token-usage 0.1.1 → 0.1.3

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.
@@ -1,13 +1,13 @@
1
- import { contextBridge as H } from "electron";
2
- import { loadDailyUsageData as P, loadSessionData as X } from "ccusage/data-loader";
3
- import { stat as G, readFile as J } from "node:fs/promises";
1
+ import { contextBridge as B } from "electron";
2
+ import { loadDailyUsageData as H, loadSessionData as P } from "ccusage/data-loader";
3
+ import { stat as X, readFile as G } from "node:fs/promises";
4
4
  import * as g from "node:path";
5
- import * as K from "node:os";
6
- const V = "CODEX_HOME", W = g.join(K.homedir(), ".codex"), Y = "sessions";
5
+ import * as J from "node:os";
6
+ const K = "CODEX_HOME", V = g.join(J.homedir(), ".codex"), W = "sessions";
7
7
  function y(e) {
8
8
  return typeof e == "number" && Number.isFinite(e) ? e : 0;
9
9
  }
10
- function z(e) {
10
+ function F(e) {
11
11
  if (e == null || typeof e != "object")
12
12
  return null;
13
13
  const t = e, o = y(t.input_tokens), l = y(
@@ -21,7 +21,7 @@ function z(e) {
21
21
  total_tokens: u > 0 ? u : o + s
22
22
  };
23
23
  }
24
- function $(e, t) {
24
+ function Y(e, t) {
25
25
  return {
26
26
  input_tokens: Math.max(
27
27
  e.input_tokens - ((t == null ? void 0 : t.input_tokens) ?? 0),
@@ -45,7 +45,7 @@ function $(e, t) {
45
45
  )
46
46
  };
47
47
  }
48
- function q(e) {
48
+ function $(e) {
49
49
  const t = e.total_tokens > 0 ? e.total_tokens : e.input_tokens + e.output_tokens, o = Math.min(e.cached_input_tokens, e.input_tokens);
50
50
  return {
51
51
  inputTokens: e.input_tokens,
@@ -55,7 +55,7 @@ function q(e) {
55
55
  totalTokens: t
56
56
  };
57
57
  }
58
- function L(e) {
58
+ function A(e) {
59
59
  if (e == null || typeof e != "object")
60
60
  return;
61
61
  const t = e;
@@ -78,7 +78,7 @@ function L(e) {
78
78
  return o.model.trim();
79
79
  }
80
80
  }
81
- async function Q(e) {
81
+ async function q(e) {
82
82
  try {
83
83
  const { glob: t } = await import("./index-CYePjqCs.mjs");
84
84
  return await t("**/*.jsonl", {
@@ -89,63 +89,65 @@ async function Q(e) {
89
89
  return [];
90
90
  }
91
91
  }
92
- async function Z() {
92
+ async function Q() {
93
93
  var u;
94
- const e = (u = process.env[V]) == null ? void 0 : u.trim(), t = e != null && e !== "" ? g.resolve(e) : W, o = g.join(t, Y);
94
+ const e = (u = process.env[K]) == null ? void 0 : u.trim(), t = e != null && e !== "" ? g.resolve(e) : V, o = g.join(t, W);
95
95
  try {
96
- if (!(await G(o)).isDirectory())
96
+ if (!(await X(o)).isDirectory())
97
97
  return [];
98
98
  } catch {
99
99
  return [];
100
100
  }
101
- const l = await Q(o), s = [], r = "gpt-5";
101
+ const l = await q(o), s = [], r = "gpt-5";
102
102
  for (const d of l) {
103
103
  const n = g.relative(o, d).split(g.sep).join("/").replace(/\.jsonl$/i, "");
104
104
  try {
105
- const i = await J(d, "utf8");
106
- let k = null, p, f = !1;
107
- const S = i.split(/\r?\n/);
108
- for (const m of S) {
109
- const C = m.trim();
105
+ const i = await G(d, "utf8");
106
+ let k = null, p, m = !1;
107
+ const M = i.split(/\r?\n/);
108
+ for (const T of M) {
109
+ const C = T.trim();
110
110
  if (C === "") continue;
111
- let T;
111
+ let _;
112
112
  try {
113
- T = JSON.parse(C);
113
+ _ = JSON.parse(C);
114
114
  } catch {
115
115
  continue;
116
116
  }
117
- if (T == null || typeof T != "object") continue;
118
- const j = T, O = j.type, _ = j.payload, E = j.timestamp;
119
- if (O === "turn_context") {
120
- if (_ != null && typeof _ == "object") {
121
- const A = L(_);
122
- A != null && (p = A, f = !1);
117
+ if (_ == null || typeof _ != "object") continue;
118
+ const S = _, w = S.type, h = S.payload, O = S.timestamp;
119
+ if (w === "turn_context") {
120
+ if (h != null && typeof h == "object") {
121
+ const R = A(h);
122
+ R != null && (p = R, m = !1);
123
123
  }
124
124
  continue;
125
125
  }
126
- if (O !== "event_msg" || _ == null || typeof _ != "object") continue;
127
- const U = _;
128
- if (U.type !== "token_count" || typeof E != "string") continue;
129
- const b = U.info;
126
+ if (w !== "event_msg" || h == null || typeof h != "object") continue;
127
+ const j = h;
128
+ if (j.type !== "token_count" || typeof O != "string") continue;
129
+ const b = j.info;
130
130
  if (b == null || typeof b != "object") continue;
131
- const F = b, N = z(F.last_token_usage), D = z(F.total_token_usage);
132
- let I = N;
133
- if (I == null && D != null && (I = $(D, k)), D != null && (k = D), I == null) continue;
134
- const h = q(I);
135
- if (h.inputTokens === 0 && h.cachedInputTokens === 0 && h.outputTokens === 0 && h.reasoningOutputTokens === 0)
131
+ const E = b, z = F(E.last_token_usage), D = F(E.total_token_usage);
132
+ let I = z;
133
+ if (I == null && D != null && (I = Y(D, k)), D != null && (k = D), I == null) continue;
134
+ const f = $(I);
135
+ if (f.inputTokens === 0 && f.cachedInputTokens === 0 && f.outputTokens === 0 && f.reasoningOutputTokens === 0)
136
136
  continue;
137
- const B = Object.assign({}, U, { info: b }), M = L(B);
138
- let w = !1;
139
- M != null && (p = M, f = !1);
140
- let x = M ?? p;
141
- x == null ? (x = r, w = !0, p = x, f = !0) : M == null && f && (w = !0);
142
- const R = {
137
+ const L = Object.assign({}, j, { info: b }), U = A(L);
138
+ U != null && (p = U, m = !1);
139
+ let x = U ?? p;
140
+ x == null && (x = r, p = x, m = !0);
141
+ const N = {
143
142
  sessionId: n,
144
- timestamp: E,
143
+ timestamp: O,
145
144
  model: x,
146
- ...h
145
+ inputTokens: f.inputTokens,
146
+ cachedInputTokens: f.cachedInputTokens,
147
+ outputTokens: f.outputTokens,
148
+ totalTokens: f.totalTokens
147
149
  };
148
- w && (R.isFallbackModel = !0), s.push(R);
150
+ s.push(N);
149
151
  }
150
152
  } catch (i) {
151
153
  console.debug("Failed to read Codex session file", i);
@@ -156,31 +158,31 @@ async function Z() {
156
158
  (d, c) => new Date(d.timestamp).getTime() - new Date(c.timestamp).getTime()
157
159
  ), s;
158
160
  }
159
- const v = {
161
+ const Z = {
160
162
  getUsage: async (e = "claude-code") => {
161
163
  try {
162
- return e === "codex" ? await et() : await tt();
164
+ return e === "codex" ? await tt() : await v();
163
165
  } catch (t) {
164
166
  throw console.error("Failed to get token usage:", t), t;
165
167
  }
166
168
  }
167
169
  };
168
- async function tt() {
169
- const e = await P({}), t = await X({}), o = /* @__PURE__ */ new Map();
170
+ async function v() {
171
+ const e = await H({}), t = await P({}), o = /* @__PURE__ */ new Map();
170
172
  for (const n of t) {
171
173
  const i = n.lastActivity.split("T")[0];
172
174
  o.has(i) || o.set(i, /* @__PURE__ */ new Set()), o.get(i).add(n.sessionId);
173
175
  }
174
176
  let l = 0, s = 0, r = 0, u = 0;
175
177
  const d = /* @__PURE__ */ new Set(), c = e.map((n) => {
176
- const i = n.inputTokens || 0, k = n.outputTokens || 0, p = n.cacheCreationTokens || 0, f = n.cacheReadTokens || 0, S = i + k + p + f, m = o.get(n.date), C = m ? m.size : 0;
177
- return l += i, s += k, r += p, u += f, m && m.forEach((T) => d.add(T)), {
178
+ const i = n.inputTokens || 0, k = n.outputTokens || 0, p = n.cacheCreationTokens || 0, m = n.cacheReadTokens || 0, M = i + k + p + m, T = o.get(n.date), C = T ? T.size : 0;
179
+ return l += i, s += k, r += p, u += m, T && T.forEach((_) => d.add(_)), {
178
180
  date: n.date,
179
181
  inputTokens: i,
180
182
  outputTokens: k,
181
183
  cacheCreationTokens: p,
182
- cacheReadTokens: f,
183
- totalTokens: S,
184
+ cacheReadTokens: m,
185
+ totalTokens: M,
184
186
  sessionCount: C,
185
187
  modelsUsed: n.modelsUsed || []
186
188
  };
@@ -195,11 +197,10 @@ async function tt() {
195
197
  sessionCount: d.size
196
198
  },
197
199
  byDay: c.sort((n, i) => n.date.localeCompare(i.date))
198
- // Sort by date ascending
199
200
  };
200
201
  }
201
- async function et() {
202
- const e = await Z(), t = /* @__PURE__ */ new Map();
202
+ async function tt() {
203
+ const e = await Q(), t = /* @__PURE__ */ new Map();
203
204
  for (const c of e) {
204
205
  const a = c.timestamp.split("T")[0];
205
206
  t.has(a) || t.set(a, {
@@ -222,12 +223,9 @@ async function et() {
222
223
  return o += n, l += a.outputTokens, s += i, a.sessions.forEach((k) => r.add(k)), {
223
224
  date: c,
224
225
  inputTokens: n,
225
- // Non-cached input tokens (to match Claude Code)
226
226
  outputTokens: a.outputTokens,
227
227
  cacheCreationTokens: 0,
228
- // Codex doesn't track cache creation separately
229
228
  cacheReadTokens: i,
230
- // Cached input tokens
231
229
  totalTokens: a.totalTokens,
232
230
  sessionCount: a.sessions.size,
233
231
  modelsUsed: Array.from(a.models)
@@ -236,15 +234,13 @@ async function et() {
236
234
  return {
237
235
  total: {
238
236
  inputTokens: o,
239
- // Non-cached input tokens
240
237
  outputTokens: l,
241
238
  cacheCreationTokens: 0,
242
239
  cacheReadTokens: s,
243
- // Cached input tokens
244
240
  totalTokens: o + l + s,
245
241
  sessionCount: r.size
246
242
  },
247
243
  byDay: u
248
244
  };
249
245
  }
250
- H.exposeInMainWorld("tokenUsage", v);
246
+ B.exposeInMainWorld("tokenUsage", Z);