@vibe-cafe/vibe-usage 0.9.2 → 0.9.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/README.md CHANGED
@@ -67,6 +67,7 @@ npx @vibe-cafe/vibe-usage status # Show config & detected tools
67
67
  | Cline | `<host>/User/globalStorage/saoudrizwan.claude-dev/{state/taskHistory.json,tasks/<id>/ui_messages.json}` (walks all VSCode-fork hosts: Code, Cursor, Windsurf, VSCodium, Trae, ...) |
68
68
  | Roo Code | `<host>/User/globalStorage/rooveterinaryinc.roo-cline/{tasks/_index.json,tasks/<id>/{history_item,ui_messages}.json}` (walks all VSCode-fork hosts) |
69
69
  | Antigravity | `~/.gemini/antigravity/conversations/*.pb` to discover cascades, then reads token usage + sessions from the running language server via Connect RPC (process discovered with `ps`/`lsof` on macOS/Linux, PowerShell CIM with a `wmic` fallback on Windows) |
70
+ | ZCode | `~/.zcode/cli/db/db.sqlite` (SQLite; reads the `message` table for per-message tokens, model, and project `cwd`/`root`, joined to `session.directory`) |
70
71
 
71
72
  ## How It Works
72
73
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vibe-cafe/vibe-usage",
3
- "version": "0.9.2",
3
+ "version": "0.9.3",
4
4
  "description": "Track your AI coding tool token usage and sync to vibecafe.ai",
5
5
  "type": "module",
6
6
  "bin": {
@@ -16,6 +16,7 @@ import { parse as parseAntigravity } from './antigravity.js';
16
16
  import { parse as parseHermes } from './hermes.js';
17
17
  import { parse as parseKiro } from './kiro.js';
18
18
  import { parse as parsePiCodingAgent } from './pi-coding-agent.js';
19
+ import { parse as parseZcode } from './zcode.js';
19
20
 
20
21
  export const parsers = {
21
22
  'claude-code': parseClaudeCode,
@@ -35,6 +36,7 @@ export const parsers = {
35
36
  'hermes': parseHermes,
36
37
  'kiro': parseKiro,
37
38
  'pi-coding-agent': parsePiCodingAgent,
39
+ 'zcode': parseZcode,
38
40
  };
39
41
 
40
42
 
@@ -0,0 +1,104 @@
1
+ import { existsSync } from 'node:fs';
2
+ import { join, basename } from 'node:path';
3
+ import { homedir } from 'node:os';
4
+ import { aggregateToBuckets, extractSessions } from './index.js';
5
+ import { queryDbJson } from './sqlite.js';
6
+
7
+ // ZCode (z.ai / Zhipu's coding agent) stores everything in a SQLite database
8
+ // at ~/.zcode/cli/db/db.sqlite. The `message` table is the canonical source:
9
+ // each row is one user or assistant message, with an assistant message carrying
10
+ // per-request token usage and the working directory. We read it directly rather
11
+ // than the parallel `model_usage` ledger because `message` gives us BOTH session
12
+ // timing (user + assistant rows) and token usage in one pass, with the project
13
+ // path attached to each message.
14
+ const DB_PATH = join(homedir(), '.zcode', 'cli', 'db', 'db.sqlite');
15
+
16
+ /**
17
+ * Project name from a ZCode message's path. ZCode records both `cwd` and `root`;
18
+ * prefer `root` (the workspace root) and fall back to `cwd`, then to the
19
+ * session's `directory` column (joined in via the query) — taking the last path
20
+ * component, matching how every other parser names projects.
21
+ */
22
+ function projectName(root, cwd, sessionDir) {
23
+ const p = root || cwd || sessionDir;
24
+ if (!p) return 'unknown';
25
+ return basename(String(p).replace(/[/\\]+$/, '')) || 'unknown';
26
+ }
27
+
28
+ export async function parse() {
29
+ if (!existsSync(DB_PATH)) return { buckets: [], sessions: [] };
30
+
31
+ // Join each message to its session so we can fall back to the session's
32
+ // directory when an individual message has no path (older rows, lite agents).
33
+ const query = `SELECT
34
+ m.session_id AS sessionId,
35
+ m.time_created AS created,
36
+ json_extract(m.data, '$.role') AS role,
37
+ json_extract(m.data, '$.modelID') AS modelID,
38
+ json_extract(m.data, '$.tokens') AS tokens,
39
+ json_extract(m.data, '$.path.root') AS pathRoot,
40
+ json_extract(m.data, '$.path.cwd') AS pathCwd,
41
+ s.directory AS sessionDir
42
+ FROM message m
43
+ LEFT JOIN session s ON s.id = m.session_id`;
44
+
45
+ let rows;
46
+ try {
47
+ rows = queryDbJson(DB_PATH, query);
48
+ } catch (err) {
49
+ if (err.status === 127 || (err.message && err.message.includes('ENOENT'))) {
50
+ throw new Error('sqlite3 CLI not found. Install sqlite3 (or use Node >= 22.5) to sync ZCode data.');
51
+ }
52
+ throw err;
53
+ }
54
+ if (!rows.length) return { buckets: [], sessions: [] };
55
+
56
+ const entries = [];
57
+ const sessionEvents = [];
58
+
59
+ for (const row of rows) {
60
+ const timestamp = new Date(row.created);
61
+ if (isNaN(timestamp.getTime())) continue;
62
+
63
+ const project = projectName(row.pathRoot, row.pathCwd, row.sessionDir);
64
+ const sessionId = row.sessionId || 'unknown';
65
+
66
+ sessionEvents.push({
67
+ sessionId,
68
+ source: 'zcode',
69
+ project,
70
+ timestamp,
71
+ role: row.role === 'user' ? 'user' : 'assistant',
72
+ });
73
+
74
+ if (row.role !== 'assistant') continue;
75
+
76
+ let tokens;
77
+ try {
78
+ tokens = typeof row.tokens === 'string' ? JSON.parse(row.tokens) : row.tokens;
79
+ } catch {
80
+ continue;
81
+ }
82
+ if (!tokens || (!tokens.input && !tokens.output)) continue;
83
+
84
+ // ZCode follows Anthropic-style usage where `input` INCLUDES the cache-read
85
+ // tokens and `output` INCLUDES reasoning (verified: input + output == total).
86
+ // Normalize to this codebase's non-overlapping fields so cached/reasoning
87
+ // tokens aren't double-counted inside input/output.
88
+ const cachedInput = tokens.cache?.read || 0;
89
+ const reasoning = tokens.reasoning || 0;
90
+
91
+ entries.push({
92
+ source: 'zcode',
93
+ model: row.modelID || 'unknown',
94
+ project,
95
+ timestamp,
96
+ inputTokens: (tokens.input || 0) - cachedInput,
97
+ outputTokens: (tokens.output || 0) - reasoning,
98
+ cachedInputTokens: cachedInput,
99
+ reasoningOutputTokens: reasoning,
100
+ });
101
+ }
102
+
103
+ return { buckets: aggregateToBuckets(entries), sessions: extractSessions(sessionEvents) };
104
+ }
package/src/tools.js CHANGED
@@ -195,6 +195,11 @@ export const TOOLS = [
195
195
  dataDir: join(homedir(), 'Library', 'Application Support', 'Code', 'User', 'globalStorage', 'rooveterinaryinc.roo-cline'),
196
196
  detectDataDirs: findRooCodeDataDirs,
197
197
  },
198
+ {
199
+ name: 'ZCode',
200
+ id: 'zcode',
201
+ dataDir: join(homedir(), '.zcode', 'cli', 'db', 'db.sqlite'),
202
+ },
198
203
  ];
199
204
 
200
205
  export function detectInstalledTools() {