@vibe-cafe/vibe-usage 0.1.6 → 0.1.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/README.md +1 -1
- package/package.json +18 -5
- package/src/hooks.js +57 -4
- package/src/parsers/opencode.js +91 -3
package/README.md
CHANGED
|
@@ -30,7 +30,7 @@ npx vibe-usage status # Show config & detected tools
|
|
|
30
30
|
| Claude Code | Yes (session hook) | `~/.claude/projects/` |
|
|
31
31
|
| Codex CLI | Yes (notify hook) | `~/.codex/sessions/` |
|
|
32
32
|
| Gemini CLI | Yes (session hook) | `~/.gemini/tmp/` |
|
|
33
|
-
| OpenCode | Manual only | `~/.local/share/opencode
|
|
33
|
+
| OpenCode | Manual only | `~/.local/share/opencode/opencode.db` (SQLite) |
|
|
34
34
|
| OpenClaw | Manual only | `~/.openclaw/agents/` |
|
|
35
35
|
|
|
36
36
|
## How It Works
|
package/package.json
CHANGED
|
@@ -1,14 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vibe-cafe/vibe-usage",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8",
|
|
4
4
|
"description": "Track your AI coding tool token usage and sync to vibecafe.ai",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
7
|
-
"vibe-usage": "
|
|
7
|
+
"vibe-usage": "bin/vibe-usage.js"
|
|
8
8
|
},
|
|
9
|
-
"files": [
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
"files": [
|
|
10
|
+
"bin/",
|
|
11
|
+
"src/"
|
|
12
|
+
],
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": ">=20"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"ai",
|
|
18
|
+
"coding",
|
|
19
|
+
"usage",
|
|
20
|
+
"tokens",
|
|
21
|
+
"claude",
|
|
22
|
+
"codex",
|
|
23
|
+
"gemini"
|
|
24
|
+
],
|
|
12
25
|
"dependencies": {
|
|
13
26
|
"ccusage": "18.0.5"
|
|
14
27
|
},
|
package/src/hooks.js
CHANGED
|
@@ -4,9 +4,38 @@ import { homedir } from 'node:os';
|
|
|
4
4
|
|
|
5
5
|
const SYNC_CMD = 'npx @vibe-cafe/vibe-usage sync 2>/dev/null &';
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Check if a SessionEnd hook array (new or old format) already contains a vibe-usage hook.
|
|
9
|
+
*/
|
|
7
10
|
function hasVibeUsageHook(hooks) {
|
|
8
11
|
if (!Array.isArray(hooks)) return false;
|
|
9
|
-
return hooks.some(
|
|
12
|
+
return hooks.some(entry => {
|
|
13
|
+
// New format: { matcher?: "...", hooks: [{ type, command }] }
|
|
14
|
+
if (Array.isArray(entry.hooks)) {
|
|
15
|
+
return entry.hooks.some(h => h.command && h.command.includes('vibe-usage'));
|
|
16
|
+
}
|
|
17
|
+
// Old format: { type, command } directly
|
|
18
|
+
if (entry.command && entry.command.includes('vibe-usage')) return true;
|
|
19
|
+
return false;
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Migrate old-format hook entries to the new matcher format.
|
|
25
|
+
* Old: [{ type: "command", command: "..." }]
|
|
26
|
+
* New: [{ hooks: [{ type: "command", command: "..." }] }]
|
|
27
|
+
*/
|
|
28
|
+
function migrateOldFormatHooks(hooks) {
|
|
29
|
+
if (!Array.isArray(hooks)) return hooks;
|
|
30
|
+
return hooks.map(entry => {
|
|
31
|
+
// Already new format (has "hooks" array)
|
|
32
|
+
if (Array.isArray(entry.hooks)) return entry;
|
|
33
|
+
// Old format: bare handler → wrap in matcher group
|
|
34
|
+
if (entry.type && entry.command) {
|
|
35
|
+
return { hooks: [entry] };
|
|
36
|
+
}
|
|
37
|
+
return entry;
|
|
38
|
+
});
|
|
10
39
|
}
|
|
11
40
|
|
|
12
41
|
export function injectClaudeCode() {
|
|
@@ -21,11 +50,28 @@ export function injectClaudeCode() {
|
|
|
21
50
|
if (!settings.hooks) settings.hooks = {};
|
|
22
51
|
if (!settings.hooks.SessionEnd) settings.hooks.SessionEnd = [];
|
|
23
52
|
|
|
53
|
+
// Migrate any old-format hooks first
|
|
54
|
+
settings.hooks.SessionEnd = migrateOldFormatHooks(settings.hooks.SessionEnd);
|
|
55
|
+
|
|
24
56
|
if (hasVibeUsageHook(settings.hooks.SessionEnd)) {
|
|
25
|
-
|
|
57
|
+
// Update the command in existing hook to use latest
|
|
58
|
+
for (const group of settings.hooks.SessionEnd) {
|
|
59
|
+
if (Array.isArray(group.hooks)) {
|
|
60
|
+
for (const h of group.hooks) {
|
|
61
|
+
if (h.command && h.command.includes('vibe-usage')) {
|
|
62
|
+
h.command = SYNC_CMD;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
writeFileSync(settingsPath, JSON.stringify(settings, null, 2) + '\n', 'utf-8');
|
|
68
|
+
return { injected: false, reason: 'already installed (updated)' };
|
|
26
69
|
}
|
|
27
70
|
|
|
28
|
-
|
|
71
|
+
// New format: matcher group with hooks array
|
|
72
|
+
settings.hooks.SessionEnd.push({
|
|
73
|
+
hooks: [{ type: 'command', command: SYNC_CMD }],
|
|
74
|
+
});
|
|
29
75
|
writeFileSync(settingsPath, JSON.stringify(settings, null, 2) + '\n', 'utf-8');
|
|
30
76
|
return { injected: true };
|
|
31
77
|
}
|
|
@@ -40,7 +86,13 @@ export function injectCodex() {
|
|
|
40
86
|
}
|
|
41
87
|
|
|
42
88
|
if (content.includes('vibe-usage')) {
|
|
43
|
-
|
|
89
|
+
// Update existing command to use latest
|
|
90
|
+
content = content.replace(
|
|
91
|
+
/npx @vibe-cafe\/vibe-usage(?:@[\d.]+)? sync[^"']*/g,
|
|
92
|
+
SYNC_CMD,
|
|
93
|
+
);
|
|
94
|
+
writeFileSync(configPath, content, 'utf-8');
|
|
95
|
+
return { injected: false, reason: 'already installed (updated)' };
|
|
44
96
|
}
|
|
45
97
|
|
|
46
98
|
const notifySection = `\n[notify]\ncommand = "${SYNC_CMD}"\n`;
|
|
@@ -73,6 +125,7 @@ export function injectGeminiCli() {
|
|
|
73
125
|
return { injected: false, reason: 'already installed' };
|
|
74
126
|
}
|
|
75
127
|
|
|
128
|
+
// Gemini CLI still uses the flat format (no matcher groups)
|
|
76
129
|
settings.hooks.SessionEnd.push({ type: 'command', command: SYNC_CMD });
|
|
77
130
|
writeFileSync(settingsPath, JSON.stringify(settings, null, 2) + '\n', 'utf-8');
|
|
78
131
|
return { injected: true };
|
package/src/parsers/opencode.js
CHANGED
|
@@ -1,12 +1,103 @@
|
|
|
1
|
+
import { execFileSync } from 'node:child_process';
|
|
1
2
|
import { readdirSync, readFileSync, statSync, existsSync } from 'node:fs';
|
|
2
3
|
import { join, basename } from 'node:path';
|
|
3
4
|
import { homedir } from 'node:os';
|
|
4
5
|
import { aggregateToBuckets } from './index.js';
|
|
5
6
|
|
|
6
7
|
const DATA_DIR = join(homedir(), '.local', 'share', 'opencode');
|
|
8
|
+
const DB_PATH = join(DATA_DIR, 'opencode.db');
|
|
7
9
|
const MESSAGES_DIR = join(DATA_DIR, 'storage', 'message');
|
|
8
10
|
|
|
11
|
+
/**
|
|
12
|
+
* Parse opencode usage data.
|
|
13
|
+
* Tries SQLite database first (opencode >= v0.2), falls back to legacy JSON files.
|
|
14
|
+
*/
|
|
9
15
|
export async function parse(lastSync) {
|
|
16
|
+
if (existsSync(DB_PATH)) {
|
|
17
|
+
try {
|
|
18
|
+
return parseFromSqlite(lastSync);
|
|
19
|
+
} catch (err) {
|
|
20
|
+
process.stderr.write(`warn: opencode sqlite parse failed (${err.message}), trying legacy json...\n`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return parseFromJson(lastSync);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function parseFromSqlite(lastSync) {
|
|
27
|
+
// Build WHERE clause: only messages with token data
|
|
28
|
+
const conditions = [
|
|
29
|
+
"(json_extract(data, '$.tokens.input') > 0 OR json_extract(data, '$.tokens.output') > 0)",
|
|
30
|
+
];
|
|
31
|
+
if (lastSync) {
|
|
32
|
+
const sinceMs = new Date(lastSync).getTime();
|
|
33
|
+
conditions.push(`time_created > ${sinceMs}`);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const query = `SELECT data FROM message WHERE ${conditions.join(' AND ')}`;
|
|
37
|
+
|
|
38
|
+
let output;
|
|
39
|
+
try {
|
|
40
|
+
output = execFileSync('sqlite3', [
|
|
41
|
+
'-json',
|
|
42
|
+
DB_PATH,
|
|
43
|
+
query,
|
|
44
|
+
], { encoding: 'utf-8', maxBuffer: 100 * 1024 * 1024, timeout: 30000 });
|
|
45
|
+
} catch (err) {
|
|
46
|
+
if (err.status === 127 || (err.message && err.message.includes('ENOENT'))) {
|
|
47
|
+
throw new Error('sqlite3 CLI not found. Install sqlite3 to sync opencode data.');
|
|
48
|
+
}
|
|
49
|
+
throw err;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
output = output.trim();
|
|
53
|
+
if (!output || output === '[]') return [];
|
|
54
|
+
|
|
55
|
+
let rows;
|
|
56
|
+
try {
|
|
57
|
+
rows = JSON.parse(output);
|
|
58
|
+
} catch {
|
|
59
|
+
throw new Error('Failed to parse sqlite3 JSON output');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const entries = [];
|
|
63
|
+
for (const row of rows) {
|
|
64
|
+
let data;
|
|
65
|
+
try {
|
|
66
|
+
data = JSON.parse(row.data);
|
|
67
|
+
} catch {
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (!data.modelID) continue;
|
|
72
|
+
|
|
73
|
+
const tokens = data.tokens;
|
|
74
|
+
if (!tokens) continue;
|
|
75
|
+
if (!tokens.input && !tokens.output) continue;
|
|
76
|
+
|
|
77
|
+
const timestamp = new Date(data.time?.created);
|
|
78
|
+
if (isNaN(timestamp.getTime())) continue;
|
|
79
|
+
if (lastSync && timestamp <= new Date(lastSync)) continue;
|
|
80
|
+
|
|
81
|
+
const rootPath = data.path?.root;
|
|
82
|
+
const project = rootPath ? basename(rootPath) : 'unknown';
|
|
83
|
+
|
|
84
|
+
entries.push({
|
|
85
|
+
source: 'opencode',
|
|
86
|
+
model: data.modelID || 'unknown',
|
|
87
|
+
project,
|
|
88
|
+
timestamp,
|
|
89
|
+
inputTokens: tokens.input || 0,
|
|
90
|
+
outputTokens: tokens.output || 0,
|
|
91
|
+
cachedInputTokens: tokens.cache?.read || 0,
|
|
92
|
+
reasoningOutputTokens: tokens.reasoning || 0,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return aggregateToBuckets(entries);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Legacy parser: reads JSON files from storage/message directories. */
|
|
100
|
+
function parseFromJson(lastSync) {
|
|
10
101
|
if (!existsSync(MESSAGES_DIR)) return [];
|
|
11
102
|
|
|
12
103
|
const entries = [];
|
|
@@ -45,10 +136,8 @@ export async function parse(lastSync) {
|
|
|
45
136
|
continue;
|
|
46
137
|
}
|
|
47
138
|
|
|
48
|
-
|
|
49
139
|
if (!data.modelID) continue;
|
|
50
140
|
|
|
51
|
-
|
|
52
141
|
const tokens = data.tokens;
|
|
53
142
|
if (!tokens) continue;
|
|
54
143
|
if (!tokens.input && !tokens.output) continue;
|
|
@@ -57,7 +146,6 @@ export async function parse(lastSync) {
|
|
|
57
146
|
if (isNaN(timestamp.getTime())) continue;
|
|
58
147
|
if (lastSync && timestamp <= new Date(lastSync)) continue;
|
|
59
148
|
|
|
60
|
-
|
|
61
149
|
const rootPath = data.path?.root;
|
|
62
150
|
const project = rootPath ? basename(rootPath) : 'unknown';
|
|
63
151
|
|