@statforge/claudestat 1.4.0 → 1.5.0
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 +130 -547
- package/dist/cost-projector.d.ts +24 -0
- package/dist/cost-projector.js +133 -0
- package/dist/daemon.js +1 -1
- package/dist/db.d.ts +4 -0
- package/dist/db.js +14 -6
- package/dist/enricher.d.ts +18 -26
- package/dist/enricher.js +113 -333
- package/dist/index.js +23 -2
- package/dist/insights.js +0 -2
- package/dist/meta-stats.js +1 -1
- package/dist/middleware/rate-limiter.js +1 -1
- package/dist/paths.d.ts +17 -0
- package/dist/paths.js +44 -0
- package/dist/quota-tracker.js +0 -1
- package/dist/roast.js +0 -2
- package/dist/routes/events.js +5 -5
- package/dist/routes/misc.js +3 -21
- package/dist/routes/stream.d.ts +1 -1
- package/dist/routes/stream.js +3 -3
- package/dist/service.js +11 -7
- package/dist/watchers/adapter.d.ts +37 -0
- package/dist/watchers/adapter.js +31 -0
- package/dist/watchers/amp.d.ts +8 -0
- package/dist/watchers/amp.js +42 -0
- package/dist/watchers/claude-code.d.ts +17 -0
- package/dist/watchers/claude-code.js +300 -0
- package/dist/watchers/codebuff.d.ts +8 -0
- package/dist/watchers/codebuff.js +42 -0
- package/dist/watchers/codex.d.ts +9 -0
- package/dist/watchers/codex.js +43 -0
- package/dist/watchers/droid.d.ts +8 -0
- package/dist/watchers/droid.js +42 -0
- package/dist/watchers/opencode.d.ts +9 -0
- package/dist/watchers/opencode.js +43 -0
- package/package.json +10 -2
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export interface CostProjection {
|
|
2
|
+
trend: 'up' | 'down' | 'stable';
|
|
3
|
+
slope: number;
|
|
4
|
+
rSquared: number;
|
|
5
|
+
weekly: PeriodProjection;
|
|
6
|
+
monthly: PeriodProjection;
|
|
7
|
+
dailyHistory: DayPoint[];
|
|
8
|
+
}
|
|
9
|
+
export interface PeriodProjection {
|
|
10
|
+
projected: number;
|
|
11
|
+
lower80: number;
|
|
12
|
+
upper80: number;
|
|
13
|
+
daysWithData: number;
|
|
14
|
+
costSoFar: number;
|
|
15
|
+
avgPerDay: number;
|
|
16
|
+
}
|
|
17
|
+
export interface DayPoint {
|
|
18
|
+
date: string;
|
|
19
|
+
dayIndex: number;
|
|
20
|
+
cost: number;
|
|
21
|
+
fitted: number | null;
|
|
22
|
+
}
|
|
23
|
+
export declare function computeProjection(days?: number): CostProjection;
|
|
24
|
+
export declare function formatProjection(p: CostProjection): string;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.computeProjection = computeProjection;
|
|
4
|
+
exports.formatProjection = formatProjection;
|
|
5
|
+
const db_1 = require("./db");
|
|
6
|
+
function leastSquares(xs, ys) {
|
|
7
|
+
const n = xs.length;
|
|
8
|
+
if (n < 2)
|
|
9
|
+
return { slope: 0, intercept: 0, rSquared: 0 };
|
|
10
|
+
const sumX = xs.reduce((a, b) => a + b, 0);
|
|
11
|
+
const sumY = ys.reduce((a, b) => a + b, 0);
|
|
12
|
+
const sumX2 = xs.reduce((a, b) => a + b * b, 0);
|
|
13
|
+
const sumXY = xs.reduce((a, b, i) => a + b * ys[i], 0);
|
|
14
|
+
const denom = n * sumX2 - sumX * sumX;
|
|
15
|
+
if (Math.abs(denom) < 1e-12)
|
|
16
|
+
return { slope: 0, intercept: 0, rSquared: 0 };
|
|
17
|
+
const slope = (n * sumXY - sumX * sumY) / denom;
|
|
18
|
+
const intercept = (sumY - slope * sumX) / n;
|
|
19
|
+
const yMean = sumY / n;
|
|
20
|
+
const ssRes = ys.reduce((a, y, i) => a + (y - (slope * xs[i] + intercept)) ** 2, 0);
|
|
21
|
+
const ssTot = ys.reduce((a, y) => a + (y - yMean) ** 2, 0);
|
|
22
|
+
const rSquared = ssTot > 1e-12 ? 1 - ssRes / ssTot : 0;
|
|
23
|
+
return { slope, intercept, rSquared };
|
|
24
|
+
}
|
|
25
|
+
function standardError(xs, ys, slope, intercept) {
|
|
26
|
+
const n = xs.length;
|
|
27
|
+
if (n < 3)
|
|
28
|
+
return 0;
|
|
29
|
+
const residuals = xs.map((x, i) => (ys[i] - (slope * x + intercept)) ** 2);
|
|
30
|
+
const sse = residuals.reduce((a, b) => a + b, 0);
|
|
31
|
+
const mse = sse / (n - 2);
|
|
32
|
+
const sumX = xs.reduce((a, b) => a + b, 0);
|
|
33
|
+
const sumX2 = xs.reduce((a, b) => a + b * b, 0);
|
|
34
|
+
const denom = n * sumX2 - sumX * sumX;
|
|
35
|
+
if (Math.abs(denom) < 1e-12)
|
|
36
|
+
return 0;
|
|
37
|
+
const seSlope = Math.sqrt(mse * n / denom);
|
|
38
|
+
return seSlope;
|
|
39
|
+
}
|
|
40
|
+
function tCrit80(n) {
|
|
41
|
+
if (n <= 2)
|
|
42
|
+
return 0;
|
|
43
|
+
const df = n - 2;
|
|
44
|
+
if (df >= 120)
|
|
45
|
+
return 1.289;
|
|
46
|
+
if (df >= 60)
|
|
47
|
+
return 1.296;
|
|
48
|
+
if (df >= 40)
|
|
49
|
+
return 1.303;
|
|
50
|
+
if (df >= 30)
|
|
51
|
+
return 1.310;
|
|
52
|
+
if (df >= 20)
|
|
53
|
+
return 1.325;
|
|
54
|
+
if (df >= 15)
|
|
55
|
+
return 1.341;
|
|
56
|
+
if (df >= 10)
|
|
57
|
+
return 1.372;
|
|
58
|
+
if (df >= 8)
|
|
59
|
+
return 1.397;
|
|
60
|
+
if (df >= 6)
|
|
61
|
+
return 1.440;
|
|
62
|
+
if (df >= 5)
|
|
63
|
+
return 1.476;
|
|
64
|
+
return 0;
|
|
65
|
+
}
|
|
66
|
+
function computeProjection(days = 90) {
|
|
67
|
+
const since = Date.now() - days * 86400000;
|
|
68
|
+
const daily = db_1.dbOps.getAnalyticsDaily(since);
|
|
69
|
+
const dayPoints = daily.map((d, i) => ({
|
|
70
|
+
date: d.date,
|
|
71
|
+
dayIndex: i,
|
|
72
|
+
cost: d.cost,
|
|
73
|
+
fitted: null,
|
|
74
|
+
}));
|
|
75
|
+
const costs = dayPoints.map(d => d.cost);
|
|
76
|
+
const indices = dayPoints.map(d => d.dayIndex);
|
|
77
|
+
const { slope, intercept, rSquared } = leastSquares(indices, costs);
|
|
78
|
+
const se = standardError(indices, costs, slope, intercept);
|
|
79
|
+
const t80 = tCrit80(indices.length);
|
|
80
|
+
dayPoints.forEach((d, i) => {
|
|
81
|
+
d.fitted = slope * i + intercept;
|
|
82
|
+
});
|
|
83
|
+
const daysWithData = dayPoints.length;
|
|
84
|
+
const costSoFar = costs.reduce((a, b) => a + b, 0);
|
|
85
|
+
const avgPerDay = daysWithData > 0 ? costSoFar / daysWithData : 0;
|
|
86
|
+
const next7 = indices.length;
|
|
87
|
+
const next30 = indices.length + 23;
|
|
88
|
+
const weeklyProj = slope * next7 + intercept;
|
|
89
|
+
const monthlyProj = slope * next30 + intercept;
|
|
90
|
+
const weeklyProjected = Math.max(0, weeklyProj * 7);
|
|
91
|
+
const monthlyProjected = Math.max(0, monthlyProj * 30);
|
|
92
|
+
const weeklySE = se * Math.sqrt(7);
|
|
93
|
+
const monthlySE = se * Math.sqrt(30);
|
|
94
|
+
const weeklyCI = t80 > 0 ? t80 * weeklySE * 7 : weeklyProjected * 0.5;
|
|
95
|
+
const monthlyCI = t80 > 0 ? t80 * monthlySE * 30 : monthlyProjected * 0.5;
|
|
96
|
+
const trend = slope > 0.001 ? 'up' : slope < -0.001 ? 'down' : 'stable';
|
|
97
|
+
return {
|
|
98
|
+
trend,
|
|
99
|
+
slope,
|
|
100
|
+
rSquared: Math.round(rSquared * 10000) / 10000,
|
|
101
|
+
weekly: {
|
|
102
|
+
projected: Math.round(weeklyProjected * 100) / 100,
|
|
103
|
+
lower80: Math.max(0, Math.round((weeklyProjected - weeklyCI) * 100) / 100),
|
|
104
|
+
upper80: Math.round((weeklyProjected + weeklyCI) * 100) / 100,
|
|
105
|
+
daysWithData,
|
|
106
|
+
costSoFar: Math.round(costSoFar * 100) / 100,
|
|
107
|
+
avgPerDay: Math.round(avgPerDay * 10000) / 10000,
|
|
108
|
+
},
|
|
109
|
+
monthly: {
|
|
110
|
+
projected: Math.round(monthlyProjected * 100) / 100,
|
|
111
|
+
lower80: Math.max(0, Math.round((monthlyProjected - monthlyCI) * 100) / 100),
|
|
112
|
+
upper80: Math.round((monthlyProjected + monthlyCI) * 100) / 100,
|
|
113
|
+
daysWithData,
|
|
114
|
+
costSoFar: Math.round(costSoFar * 100) / 100,
|
|
115
|
+
avgPerDay: Math.round(avgPerDay * 10000) / 10000,
|
|
116
|
+
},
|
|
117
|
+
dailyHistory: dayPoints,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
function formatProjection(p) {
|
|
121
|
+
const trendChar = p.trend === 'up' ? '↑' : p.trend === 'down' ? '↓' : '→';
|
|
122
|
+
const trendLabel = p.trend === 'up' ? 'increasing' : p.trend === 'down' ? 'decreasing' : 'stable';
|
|
123
|
+
let out = `Cost Projection (R²=${p.rSquared.toFixed(3)}, trend=${trendChar} ${trendLabel})\n`;
|
|
124
|
+
out += `${'─'.repeat(42)}\n`;
|
|
125
|
+
out += ` Weekly | projected: $${p.weekly.projected.toFixed(2)} `;
|
|
126
|
+
out += `(80% CI: $${p.weekly.lower80.toFixed(2)}–$${p.weekly.upper80.toFixed(2)})\n`;
|
|
127
|
+
out += ` | avg $${p.weekly.avgPerDay.toFixed(4)}/day over ${p.weekly.daysWithData} days\n`;
|
|
128
|
+
out += ` Monthly | projected: $${p.monthly.projected.toFixed(2)} `;
|
|
129
|
+
out += `(80% CI: $${p.monthly.lower80.toFixed(2)}–$${p.monthly.upper80.toFixed(2)})\n`;
|
|
130
|
+
out += ` | avg $${p.monthly.avgPerDay.toFixed(4)}/day over ${p.monthly.daysWithData} days\n`;
|
|
131
|
+
out += `${'─'.repeat(42)}`;
|
|
132
|
+
return out;
|
|
133
|
+
}
|
package/dist/daemon.js
CHANGED
|
@@ -94,7 +94,7 @@ app.get('/health', (_req, res) => {
|
|
|
94
94
|
res.json({ status: 'ok', port: PORT, clients: (0, stream_1.getSseClientsSize)() });
|
|
95
95
|
});
|
|
96
96
|
// ─── Dashboard React (servir estáticos del build de Vite) ────────────────────
|
|
97
|
-
const DASHBOARD_DIST =
|
|
97
|
+
const DASHBOARD_DIST = (0, paths_1.getDashboardDir)();
|
|
98
98
|
app.use(express_1.default.static(DASHBOARD_DIST, {
|
|
99
99
|
setHeaders(res, filePath) {
|
|
100
100
|
if (filePath.endsWith('.html')) {
|
package/dist/db.d.ts
CHANGED
|
@@ -23,6 +23,9 @@ export interface SessionRow {
|
|
|
23
23
|
efficiency_score?: number;
|
|
24
24
|
loops_detected?: number;
|
|
25
25
|
ai_summary?: string;
|
|
26
|
+
dominant_model?: string;
|
|
27
|
+
parent_session_id?: string;
|
|
28
|
+
source?: string;
|
|
26
29
|
}
|
|
27
30
|
export interface EventRow {
|
|
28
31
|
id?: number;
|
|
@@ -35,6 +38,7 @@ export interface EventRow {
|
|
|
35
38
|
cwd?: string;
|
|
36
39
|
duration_ms?: number;
|
|
37
40
|
skill_parent?: string;
|
|
41
|
+
source?: string;
|
|
38
42
|
}
|
|
39
43
|
export interface BlockCostEntry {
|
|
40
44
|
inputUsd: number;
|
package/dist/db.js
CHANGED
|
@@ -111,11 +111,19 @@ try {
|
|
|
111
111
|
db.exec(`ALTER TABLE sessions ADD COLUMN parent_session_id TEXT`);
|
|
112
112
|
}
|
|
113
113
|
catch { }
|
|
114
|
+
try {
|
|
115
|
+
db.exec(`ALTER TABLE sessions ADD COLUMN source TEXT DEFAULT 'claude-code'`);
|
|
116
|
+
}
|
|
117
|
+
catch { }
|
|
118
|
+
try {
|
|
119
|
+
db.exec(`ALTER TABLE events ADD COLUMN source TEXT DEFAULT 'claude-code'`);
|
|
120
|
+
}
|
|
121
|
+
catch { }
|
|
114
122
|
// ─── Prepared statements (se compilan una vez al iniciar) ─────────────────────
|
|
115
123
|
const stmts = {
|
|
116
124
|
upsertSession: db.prepare(`
|
|
117
|
-
INSERT INTO sessions (id, cwd, started_at, last_event_at)
|
|
118
|
-
VALUES (?, ?, ?,
|
|
125
|
+
INSERT INTO sessions (id, cwd, started_at, last_event_at, source)
|
|
126
|
+
VALUES (?, ?, ?, ?, COALESCE(?, 'claude-code'))
|
|
119
127
|
ON CONFLICT(id) DO UPDATE SET last_event_at = excluded.last_event_at
|
|
120
128
|
`),
|
|
121
129
|
updateSessionCost: db.prepare(`
|
|
@@ -131,8 +139,8 @@ const stmts = {
|
|
|
131
139
|
WHERE id = ?
|
|
132
140
|
`),
|
|
133
141
|
insertEvent: db.prepare(`
|
|
134
|
-
INSERT INTO events (session_id, type, tool_name, tool_input, ts, cwd, skill_parent)
|
|
135
|
-
VALUES (?, ?, ?, ?, ?, ?,
|
|
142
|
+
INSERT INTO events (session_id, type, tool_name, tool_input, ts, cwd, skill_parent, source)
|
|
143
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, COALESCE(?, 'claude-code'))
|
|
136
144
|
`),
|
|
137
145
|
pairPost: db.prepare(`
|
|
138
146
|
UPDATE events SET type = 'Done', tool_response = ?, duration_ms = ?
|
|
@@ -471,10 +479,10 @@ const stmts = {
|
|
|
471
479
|
// ─── Operaciones públicas ─────────────────────────────────────────────────────
|
|
472
480
|
exports.dbOps = {
|
|
473
481
|
upsertSession(s) {
|
|
474
|
-
stmts.upsertSession.run(s.id, s.cwd ?? null, s.started_at, s.last_event_at ?? s.started_at);
|
|
482
|
+
stmts.upsertSession.run(s.id, s.cwd ?? null, s.started_at, s.last_event_at ?? s.started_at, s.source ?? null);
|
|
475
483
|
},
|
|
476
484
|
insertEvent(e) {
|
|
477
|
-
const res = stmts.insertEvent.run(e.session_id, e.type, e.tool_name ?? null, e.tool_input ?? null, e.ts, e.cwd ?? null, e.skill_parent ?? null);
|
|
485
|
+
const res = stmts.insertEvent.run(e.session_id, e.type, e.tool_name ?? null, e.tool_input ?? null, e.ts, e.cwd ?? null, e.skill_parent ?? null, e.source ?? null);
|
|
478
486
|
return Number(res.lastInsertRowid);
|
|
479
487
|
},
|
|
480
488
|
/**
|
package/dist/enricher.d.ts
CHANGED
|
@@ -1,34 +1,26 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* enricher.ts —
|
|
2
|
+
* enricher.ts — Watcher multi-CLI usando adapters
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* En lugar de hardcodear la lógica de Claude Code, usa el adapter pattern
|
|
5
|
+
* para soportar múltiples coding CLIs (Claude Code, Codex, OpenCode, etc.).
|
|
6
6
|
*
|
|
7
|
-
* Cada
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* message.model
|
|
13
|
-
*
|
|
14
|
-
* El enricher observa cambios en esos archivos (con chokidar),
|
|
15
|
-
* calcula el coste acumulado por sesión y llama al callback
|
|
16
|
-
* para que el daemon actualice la DB y haga broadcast via SSE.
|
|
7
|
+
* Cada adapter implementa WatcherAdapter (src/watchers/adapter.ts):
|
|
8
|
+
* - detect() → si el CLI está instalado
|
|
9
|
+
* - getWatchPaths() → qué archivos observar
|
|
10
|
+
* - parseEvent() → parsear una línea de trace
|
|
11
|
+
* - getSessionCost() → calcular costos acumulados
|
|
17
12
|
*/
|
|
13
|
+
import './watchers/claude-code';
|
|
14
|
+
import './watchers/codex';
|
|
15
|
+
import './watchers/opencode';
|
|
16
|
+
import './watchers/amp';
|
|
17
|
+
import './watchers/droid';
|
|
18
|
+
import './watchers/codebuff';
|
|
18
19
|
import type { CostUpdate } from './db';
|
|
19
|
-
export
|
|
20
|
-
|
|
21
|
-
export declare function getAllBlockCostsForSession(sessionId: string): Promise<BlockCostEntry[]>;
|
|
22
|
-
export interface SessionPrompt {
|
|
23
|
-
index: number;
|
|
24
|
-
ts: number;
|
|
25
|
-
text: string;
|
|
26
|
-
}
|
|
27
|
-
export declare function getSessionPrompts(sessionId: string): Promise<SessionPrompt[]>;
|
|
28
|
-
export type CostUpdateCallback = (sessionId: string, cost: CostUpdate) => void;
|
|
20
|
+
export { getAllBlockCostsForSession, getContextWindow, getSessionPrompts } from './watchers/claude-code';
|
|
21
|
+
export type CostUpdateCallback = (sessionId: string, cost: CostUpdate, source?: string) => void;
|
|
29
22
|
export type CompactDetectedCallback = (sessionId: string) => void;
|
|
30
|
-
export
|
|
31
|
-
export declare function startEnricher(onUpdate: CostUpdateCallback, onCompact?: CompactDetectedCallback, onSessionEnd?: SessionEndCallback): void;
|
|
23
|
+
export declare function startEnricher(onUpdate: CostUpdateCallback, onCompact?: CompactDetectedCallback): void;
|
|
32
24
|
export declare function stopEnricher(): void;
|
|
33
25
|
export declare function cleanupSession(sessionId: string): void;
|
|
34
|
-
export declare function processLatestForSession(sessionId: string, onUpdate: CostUpdateCallback): Promise<void>;
|
|
26
|
+
export declare function processLatestForSession(sessionId: string, onUpdate: CostUpdateCallback, source?: string): Promise<void>;
|