@jagit/hook-copilot 0.0.5 → 0.0.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.
Files changed (2) hide show
  1. package/dist/index.js +43 -7
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -127,6 +127,9 @@ function extractTokensFromObject(obj) {
127
127
  let totalTokens = 0;
128
128
  let costUsd = 0;
129
129
  let foundAny = false;
130
+ let sawInputKey = false;
131
+ let sawCacheReadStyleKey = false;
132
+ let sawCachedSubsetKey = false;
130
133
  for (const [rawKey, rawValue] of Object.entries(obj)) {
131
134
  const key = normalizeKey(rawKey);
132
135
  const value = toFiniteNumber(rawValue);
@@ -136,11 +139,19 @@ function extractTokensFromObject(obj) {
136
139
  if (["inputtokens", "prompttokens", "requesttokens"].includes(key)) {
137
140
  inputTokens += value;
138
141
  foundAny = true;
142
+ sawInputKey = true;
139
143
  continue;
140
144
  }
141
- if (["cachedinputtokens", "cachedtokens", "cachereadinputtokens", "promptcachehitstokens"].includes(key)) {
145
+ if (["cachedinputtokens", "cachereadinputtokens"].includes(key)) {
142
146
  cachedInputTokens += value;
143
147
  foundAny = true;
148
+ sawCacheReadStyleKey = true;
149
+ continue;
150
+ }
151
+ if (["cachedtokens", "promptcachehitstokens"].includes(key)) {
152
+ cachedInputTokens += value;
153
+ foundAny = true;
154
+ sawCachedSubsetKey = true;
144
155
  continue;
145
156
  }
146
157
  if (["outputtokens", "completiontokens", "responsetokens"].includes(key)) {
@@ -162,8 +173,14 @@ function extractTokensFromObject(obj) {
162
173
  if (!foundAny) {
163
174
  return null;
164
175
  }
176
+ // Some providers report inputTokens as total prompt tokens and expose cached
177
+ // tokens as a subset. Convert to non-cached input tokens to match JaGit's
178
+ // input/cached split and avoid double-counting in aggregates.
179
+ if (sawInputKey && sawCachedSubsetKey && !sawCacheReadStyleKey) {
180
+ inputTokens = Math.max(0, inputTokens - cachedInputTokens);
181
+ }
165
182
  if (totalTokens === 0) {
166
- totalTokens = inputTokens + outputTokens;
183
+ totalTokens = inputTokens + cachedInputTokens + outputTokens;
167
184
  }
168
185
  return { inputTokens, cachedInputTokens, outputTokens, totalTokens, costUsd };
169
186
  }
@@ -293,14 +310,33 @@ export function resolveDebugUsageBySession(sessionId, hookTimestamp, baseDir = W
293
310
  * we sum the total number of individual tool requests across all turns.
294
311
  */
295
312
  function countToolCalls(entries) {
296
- let count = 0;
313
+ const toolCallIds = new Set();
314
+ let anonymousCalls = 0;
297
315
  for (const e of entries) {
298
- if (e.type !== "assistant.message")
316
+ if (e.type === "assistant.message") {
317
+ const msg = e;
318
+ for (const req of msg.data?.toolRequests ?? []) {
319
+ if (typeof req.toolCallId === "string" && req.toolCallId.length > 0) {
320
+ toolCallIds.add(req.toolCallId);
321
+ }
322
+ else {
323
+ anonymousCalls += 1;
324
+ }
325
+ }
299
326
  continue;
300
- const msg = e;
301
- count += msg.data?.toolRequests?.length ?? 0;
327
+ }
328
+ if (e.type === "tool.execution_start" || e.type === "tool.execution_complete") {
329
+ const data = isRecord(e.data) ? e.data : undefined;
330
+ const toolCallId = data?.toolCallId;
331
+ if (typeof toolCallId === "string" && toolCallId.length > 0) {
332
+ toolCallIds.add(toolCallId);
333
+ }
334
+ else {
335
+ anonymousCalls += 1;
336
+ }
337
+ }
302
338
  }
303
- return count;
339
+ return toolCallIds.size + anonymousCalls;
304
340
  }
305
341
  /**
306
342
  * Extract the earliest timestamp from the transcript.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jagit/hook-copilot",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "jagit-hook-copilot": "dist/index.js"
@@ -9,7 +9,7 @@
9
9
  "dist"
10
10
  ],
11
11
  "dependencies": {
12
- "@jagit/agent-reporter": "0.0.5"
12
+ "@jagit/agent-reporter": "0.0.6"
13
13
  },
14
14
  "devDependencies": {
15
15
  "@types/node": "^25.9.3",