claude-friends 0.3.2 → 0.3.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.
- package/hooks/update-tokens.js +41 -40
- package/package.json +1 -1
package/hooks/update-tokens.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
// Hook script: reads REAL token usage from Claude Code session files
|
|
4
|
-
//
|
|
4
|
+
// Only counts tokens from today. Pushes to PartyKit.
|
|
5
5
|
|
|
6
6
|
import { readFileSync, readdirSync, statSync, existsSync } from "fs";
|
|
7
7
|
import { join } from "path";
|
|
@@ -17,14 +17,17 @@ try {
|
|
|
17
17
|
process.exit(0);
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
//
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
// Today at midnight
|
|
21
|
+
const todayStart = new Date();
|
|
22
|
+
todayStart.setHours(0, 0, 0, 0);
|
|
23
|
+
const todayISO = todayStart.toISOString();
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
// Find all session files modified today
|
|
26
|
+
function getSessionFilesModifiedToday() {
|
|
27
|
+
const projectsDir = join(homedir(), ".claude", "projects");
|
|
28
|
+
if (!existsSync(projectsDir)) return [];
|
|
27
29
|
|
|
30
|
+
const files = [];
|
|
28
31
|
try {
|
|
29
32
|
for (const dir of readdirSync(projectsDir)) {
|
|
30
33
|
const dirPath = join(projectsDir, dir);
|
|
@@ -33,50 +36,48 @@ function getLatestSessionFile() {
|
|
|
33
36
|
if (!file.endsWith(".jsonl")) continue;
|
|
34
37
|
const filePath = join(dirPath, file);
|
|
35
38
|
const mtime = statSync(filePath).mtimeMs;
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
+
// Only files modified today
|
|
40
|
+
if (mtime >= todayStart.getTime()) {
|
|
41
|
+
files.push(filePath);
|
|
39
42
|
}
|
|
40
43
|
}
|
|
41
44
|
} catch {}
|
|
42
45
|
}
|
|
43
46
|
} catch {}
|
|
44
47
|
|
|
45
|
-
return
|
|
48
|
+
return files;
|
|
46
49
|
}
|
|
47
50
|
|
|
48
|
-
// Sum token usage from session
|
|
49
|
-
function
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return totalTokens;
|
|
73
|
-
} catch {
|
|
74
|
-
return 0;
|
|
51
|
+
// Sum today's token usage from session files
|
|
52
|
+
function getTodayTokens(files) {
|
|
53
|
+
let totalTokens = 0;
|
|
54
|
+
|
|
55
|
+
for (const filePath of files) {
|
|
56
|
+
try {
|
|
57
|
+
const content = readFileSync(filePath, "utf-8");
|
|
58
|
+
for (const line of content.trim().split("\n")) {
|
|
59
|
+
try {
|
|
60
|
+
const entry = JSON.parse(line);
|
|
61
|
+
const usage = entry?.message?.usage;
|
|
62
|
+
const timestamp = entry?.timestamp;
|
|
63
|
+
|
|
64
|
+
// Only count entries from today
|
|
65
|
+
if (usage && timestamp && timestamp >= todayISO) {
|
|
66
|
+
totalTokens +=
|
|
67
|
+
(usage.input_tokens || 0) +
|
|
68
|
+
(usage.cache_creation_input_tokens || 0) +
|
|
69
|
+
(usage.output_tokens || 0);
|
|
70
|
+
}
|
|
71
|
+
} catch {}
|
|
72
|
+
}
|
|
73
|
+
} catch {}
|
|
75
74
|
}
|
|
75
|
+
|
|
76
|
+
return totalTokens;
|
|
76
77
|
}
|
|
77
78
|
|
|
78
|
-
const
|
|
79
|
-
const totalTokens =
|
|
79
|
+
const files = getSessionFilesModifiedToday();
|
|
80
|
+
const totalTokens = getTodayTokens(files);
|
|
80
81
|
|
|
81
82
|
if (totalTokens === 0) process.exit(0);
|
|
82
83
|
|