@vibe-cafe/vibe-usage 0.1.8 → 0.1.10
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/package.json +1 -1
- package/src/init.js +1 -1
- package/src/parsers/codex.js +50 -15
- package/src/parsers/gemini-cli.js +29 -11
- package/src/sync.js +22 -0
package/package.json
CHANGED
package/src/init.js
CHANGED
package/src/parsers/codex.js
CHANGED
|
@@ -1,23 +1,39 @@
|
|
|
1
1
|
import { readdirSync, readFileSync, statSync, existsSync } from 'node:fs';
|
|
2
|
-
import { join
|
|
2
|
+
import { join } from 'node:path';
|
|
3
3
|
import { homedir } from 'node:os';
|
|
4
4
|
import { aggregateToBuckets } from './index.js';
|
|
5
5
|
|
|
6
6
|
const SESSIONS_DIR = join(homedir(), '.codex', 'sessions');
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Recursively find all .jsonl files under a directory.
|
|
10
|
+
* Codex CLI stores sessions as: ~/.codex/sessions/YYYY/MM/DD/rollout-*.jsonl
|
|
11
|
+
*/
|
|
12
|
+
function findJsonlFiles(dir) {
|
|
13
|
+
const results = [];
|
|
14
|
+
if (!existsSync(dir)) return results;
|
|
13
15
|
try {
|
|
14
|
-
|
|
16
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
17
|
+
const fullPath = join(dir, entry.name);
|
|
18
|
+
if (entry.isDirectory()) {
|
|
19
|
+
results.push(...findJsonlFiles(fullPath));
|
|
20
|
+
} else if (entry.name.endsWith('.jsonl')) {
|
|
21
|
+
results.push(fullPath);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
15
24
|
} catch {
|
|
16
|
-
|
|
25
|
+
// ignore unreadable directories
|
|
17
26
|
}
|
|
27
|
+
return results;
|
|
28
|
+
}
|
|
18
29
|
|
|
19
|
-
|
|
20
|
-
|
|
30
|
+
export async function parse(lastSync) {
|
|
31
|
+
if (!existsSync(SESSIONS_DIR)) return [];
|
|
32
|
+
|
|
33
|
+
const entries = [];
|
|
34
|
+
const files = findJsonlFiles(SESSIONS_DIR);
|
|
35
|
+
if (files.length === 0) return [];
|
|
36
|
+
for (const filePath of files) {
|
|
21
37
|
if (lastSync) {
|
|
22
38
|
try {
|
|
23
39
|
const stat = statSync(filePath);
|
|
@@ -27,8 +43,6 @@ export async function parse(lastSync) {
|
|
|
27
43
|
}
|
|
28
44
|
}
|
|
29
45
|
|
|
30
|
-
const project = basename(file, '.jsonl');
|
|
31
|
-
|
|
32
46
|
let content;
|
|
33
47
|
try {
|
|
34
48
|
content = readFileSync(filePath, 'utf-8');
|
|
@@ -36,9 +50,30 @@ export async function parse(lastSync) {
|
|
|
36
50
|
continue;
|
|
37
51
|
}
|
|
38
52
|
|
|
53
|
+
// Extract project name and model from session_meta line
|
|
54
|
+
let sessionProject = 'unknown';
|
|
55
|
+
let sessionModel = 'unknown';
|
|
56
|
+
for (const line of content.split('\n')) {
|
|
57
|
+
if (!line.trim()) continue;
|
|
58
|
+
try {
|
|
59
|
+
const obj = JSON.parse(line);
|
|
60
|
+
if (obj.type === 'session_meta' && obj.payload) {
|
|
61
|
+
const meta = obj.payload;
|
|
62
|
+
if (meta.cwd) {
|
|
63
|
+
sessionProject = meta.cwd.split('/').pop() || 'unknown';
|
|
64
|
+
}
|
|
65
|
+
if (meta.git?.repository_url) {
|
|
66
|
+
// e.g. https://github.com/org/repo.git → org/repo
|
|
67
|
+
const match = meta.git.repository_url.match(/([^/]+\/[^/]+?)(?:\.git)?$/);
|
|
68
|
+
if (match) sessionProject = match[1];
|
|
69
|
+
}
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
} catch { break; }
|
|
73
|
+
}
|
|
74
|
+
|
|
39
75
|
// Track previous cumulative totals per model to compute deltas when only total_token_usage is available
|
|
40
76
|
const prevTotal = new Map();
|
|
41
|
-
|
|
42
77
|
for (const line of content.split('\n')) {
|
|
43
78
|
if (!line.trim()) continue;
|
|
44
79
|
try {
|
|
@@ -78,12 +113,12 @@ export async function parse(lastSync) {
|
|
|
78
113
|
}
|
|
79
114
|
if (!usage) continue;
|
|
80
115
|
|
|
81
|
-
const model = info.model || payload.model ||
|
|
116
|
+
const model = info.model || payload.model || sessionModel;
|
|
82
117
|
|
|
83
118
|
entries.push({
|
|
84
119
|
source: 'codex',
|
|
85
120
|
model,
|
|
86
|
-
project,
|
|
121
|
+
project: sessionProject,
|
|
87
122
|
timestamp,
|
|
88
123
|
inputTokens: usage.input_tokens || 0,
|
|
89
124
|
outputTokens: usage.output_tokens || 0,
|
|
@@ -55,8 +55,11 @@ export async function parse(lastSync) {
|
|
|
55
55
|
|
|
56
56
|
const messages = data.messages || data.history || [];
|
|
57
57
|
for (const msg of messages) {
|
|
58
|
+
// New format: tokens on type=gemini messages (ChatRecordingService)
|
|
59
|
+
// Old format: usage/usageMetadata on any message
|
|
60
|
+
const tokens = msg.tokens;
|
|
58
61
|
const usage = msg.usage || msg.usageMetadata || msg.token_count;
|
|
59
|
-
if (!usage) continue;
|
|
62
|
+
if (!tokens && !usage) continue;
|
|
60
63
|
|
|
61
64
|
const timestamp = msg.timestamp || msg.createTime || data.createTime;
|
|
62
65
|
if (!timestamp) continue;
|
|
@@ -64,16 +67,31 @@ export async function parse(lastSync) {
|
|
|
64
67
|
if (isNaN(ts.getTime())) continue;
|
|
65
68
|
if (lastSync && ts <= new Date(lastSync)) continue;
|
|
66
69
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
70
|
+
if (tokens) {
|
|
71
|
+
// New format: { input, output, cached, thoughts, tool, total }
|
|
72
|
+
entries.push({
|
|
73
|
+
source: 'gemini-cli',
|
|
74
|
+
model: msg.model || data.model || 'unknown',
|
|
75
|
+
project: 'unknown',
|
|
76
|
+
timestamp: ts,
|
|
77
|
+
inputTokens: tokens.input || 0,
|
|
78
|
+
outputTokens: tokens.output || 0,
|
|
79
|
+
cachedInputTokens: tokens.cached || 0,
|
|
80
|
+
reasoningOutputTokens: tokens.thoughts || 0,
|
|
81
|
+
});
|
|
82
|
+
} else {
|
|
83
|
+
// Old format: { promptTokenCount, candidatesTokenCount, ... }
|
|
84
|
+
entries.push({
|
|
85
|
+
source: 'gemini-cli',
|
|
86
|
+
model: msg.model || data.model || 'unknown',
|
|
87
|
+
project: 'unknown',
|
|
88
|
+
timestamp: ts,
|
|
89
|
+
inputTokens: usage.promptTokenCount || usage.input_tokens || 0,
|
|
90
|
+
outputTokens: usage.candidatesTokenCount || usage.output_tokens || 0,
|
|
91
|
+
cachedInputTokens: usage.cachedContentTokenCount || 0,
|
|
92
|
+
reasoningOutputTokens: usage.thoughtsTokenCount || 0,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
77
95
|
}
|
|
78
96
|
}
|
|
79
97
|
|
package/src/sync.js
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import { loadConfig, saveConfig } from './config.js';
|
|
2
2
|
import { ingest } from './api.js';
|
|
3
3
|
import { parsers } from './parsers/index.js';
|
|
4
|
+
import { TOOLS } from './hooks.js';
|
|
4
5
|
|
|
5
6
|
const BATCH_SIZE = 500;
|
|
6
7
|
|
|
7
8
|
export async function runSync() {
|
|
9
|
+
// Self-heal: re-inject any missing hooks before syncing
|
|
10
|
+
ensureHooks();
|
|
11
|
+
|
|
8
12
|
const config = loadConfig();
|
|
9
13
|
if (!config?.apiKey) {
|
|
10
14
|
console.error('Not configured. Run `npx @vibe-cafe/vibe-usage init` first.');
|
|
@@ -73,3 +77,21 @@ export async function runSync() {
|
|
|
73
77
|
process.exit(1);
|
|
74
78
|
}
|
|
75
79
|
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Re-inject hooks for any installed tool whose hook is missing.
|
|
83
|
+
* Runs silently — meant as a self-healing side effect of sync.
|
|
84
|
+
*/
|
|
85
|
+
function ensureHooks() {
|
|
86
|
+
for (const tool of TOOLS) {
|
|
87
|
+
if (!tool.inject) continue;
|
|
88
|
+
try {
|
|
89
|
+
const result = tool.inject();
|
|
90
|
+
if (result.injected) {
|
|
91
|
+
process.stderr.write(`hook: re-installed ${tool.name} hook\n`);
|
|
92
|
+
}
|
|
93
|
+
} catch {
|
|
94
|
+
// ignore — best effort
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|