llmtop 0.1.4 → 0.1.5
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/api.mjs +6 -1
- package/src/db.mjs +5 -2
- package/src/providers/codex.mjs +121 -19
- package/src/serve.mjs +20 -10
package/package.json
CHANGED
package/src/api.mjs
CHANGED
|
@@ -28,7 +28,12 @@ export function createApi(config, { onScan = () => {}, routes: extra = {} } = {}
|
|
|
28
28
|
},
|
|
29
29
|
};
|
|
30
30
|
return (req, res) => {
|
|
31
|
-
|
|
31
|
+
let path;
|
|
32
|
+
try {
|
|
33
|
+
path = new URL(req.url, "http://localhost").pathname;
|
|
34
|
+
} catch {
|
|
35
|
+
return Promise.resolve(false);
|
|
36
|
+
}
|
|
32
37
|
if (!path.startsWith("/api/")) return Promise.resolve(false);
|
|
33
38
|
if (req.method === "GET" && path === "/api/scan/progress") {
|
|
34
39
|
res.writeHead(200, JSON_TYPE).end(JSON.stringify(progress));
|
package/src/db.mjs
CHANGED
|
@@ -3,7 +3,7 @@ import { dirname } from "node:path";
|
|
|
3
3
|
import { DatabaseSync } from "node:sqlite";
|
|
4
4
|
|
|
5
5
|
// Bump when the schema changes: the database is then rebuilt from the archive.
|
|
6
|
-
export const SCHEMA_VERSION =
|
|
6
|
+
export const SCHEMA_VERSION = 14;
|
|
7
7
|
|
|
8
8
|
const SCHEMA = `
|
|
9
9
|
CREATE TABLE meta(key TEXT PRIMARY KEY, value TEXT);
|
|
@@ -43,7 +43,8 @@ CREATE INDEX skills_session ON skills(session_id);
|
|
|
43
43
|
CREATE INDEX skills_file ON skills(file_id);
|
|
44
44
|
CREATE TABLE subagents(agent_id TEXT PRIMARY KEY, session_id TEXT, type TEXT, tool_use_id TEXT);
|
|
45
45
|
CREATE TABLE file_meta(file_id INTEGER PRIMARY KEY, parent_session_id TEXT, subagent_type TEXT, tool_use_id TEXT,
|
|
46
|
-
api_ms INTEGER, tool_ms INTEGER, lines_added INTEGER, lines_removed INTEGER, repo_url TEXT, target_paths TEXT
|
|
46
|
+
api_ms INTEGER, tool_ms INTEGER, lines_added INTEGER, lines_removed INTEGER, repo_url TEXT, target_paths TEXT,
|
|
47
|
+
parser_state TEXT);
|
|
47
48
|
CREATE TABLE compactions(uuid TEXT PRIMARY KEY, file_id INTEGER, line INTEGER, session_id TEXT, agent_id TEXT, ts TEXT,
|
|
48
49
|
trigger TEXT, pre_tokens INTEGER, post_tokens INTEGER, duration_ms INTEGER);
|
|
49
50
|
CREATE INDEX compactions_session ON compactions(session_id);
|
|
@@ -73,6 +74,8 @@ function configureDb(db) {
|
|
|
73
74
|
function upgradeInPlace(db) {
|
|
74
75
|
const cols = db.prepare("PRAGMA table_info(files)").all();
|
|
75
76
|
if (!cols.some((c) => c.name === "ctime_ms")) db.exec("ALTER TABLE files ADD COLUMN ctime_ms REAL");
|
|
77
|
+
const fmCols = db.prepare("PRAGMA table_info(file_meta)").all();
|
|
78
|
+
if (!fmCols.some((c) => c.name === "parser_state")) db.exec("ALTER TABLE file_meta ADD COLUMN parser_state TEXT");
|
|
76
79
|
db.exec(`
|
|
77
80
|
CREATE INDEX IF NOT EXISTS requests_file ON requests(file_id);
|
|
78
81
|
CREATE INDEX IF NOT EXISTS tool_calls_file ON tool_calls(file_id);
|
package/src/providers/codex.mjs
CHANGED
|
@@ -222,6 +222,18 @@ function extractDollarSkills(text) {
|
|
|
222
222
|
return [...seen];
|
|
223
223
|
}
|
|
224
224
|
|
|
225
|
+
const str = (v) => (typeof v === "string" && v ? v : null);
|
|
226
|
+
const modelOf = (v) => str(v) ?? str(v?.model);
|
|
227
|
+
function parseParserState(raw) {
|
|
228
|
+
if (!raw) return null;
|
|
229
|
+
try {
|
|
230
|
+
const o = JSON.parse(raw);
|
|
231
|
+
return o && typeof o === "object" ? o : null;
|
|
232
|
+
} catch {
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
225
237
|
export function createIngester(db, timeZone) {
|
|
226
238
|
const zone = zoner(timeZone);
|
|
227
239
|
const q = {
|
|
@@ -241,12 +253,17 @@ export function createIngester(db, timeZone) {
|
|
|
241
253
|
subagent_type = COALESCE(excluded.subagent_type, file_meta.subagent_type),
|
|
242
254
|
tool_use_id = COALESCE(excluded.tool_use_id, file_meta.tool_use_id),
|
|
243
255
|
repo_url = COALESCE(excluded.repo_url, file_meta.repo_url)`),
|
|
244
|
-
getStats: db.prepare("SELECT api_ms, tool_ms, lines_added, lines_removed, target_paths FROM file_meta WHERE file_id = ?"),
|
|
256
|
+
getStats: db.prepare("SELECT api_ms, tool_ms, lines_added, lines_removed, target_paths, parser_state FROM file_meta WHERE file_id = ?"),
|
|
245
257
|
upsertStats: db.prepare(`INSERT INTO file_meta(file_id, api_ms, tool_ms, lines_added, lines_removed) VALUES (?, ?, ?, ?, ?)
|
|
246
258
|
ON CONFLICT(file_id) DO UPDATE SET api_ms = excluded.api_ms, tool_ms = excluded.tool_ms,
|
|
247
259
|
lines_added = excluded.lines_added, lines_removed = excluded.lines_removed`),
|
|
248
260
|
upsertTargetPaths: db.prepare(`INSERT INTO file_meta(file_id, target_paths) VALUES (?, ?)
|
|
249
261
|
ON CONFLICT(file_id) DO UPDATE SET target_paths = excluded.target_paths`),
|
|
262
|
+
upsertParserState: db.prepare(`INSERT INTO file_meta(file_id, parser_state) VALUES (?, ?)
|
|
263
|
+
ON CONFLICT(file_id) DO UPDATE SET parser_state = excluded.parser_state`),
|
|
264
|
+
backfillReqModel: db.prepare("UPDATE requests SET model = ?, family = ? WHERE file_id = ? AND model = 'unknown'"),
|
|
265
|
+
backfillCallModel: db.prepare("UPDATE tool_calls SET model = ? WHERE file_id = ? AND model IS NULL"),
|
|
266
|
+
backfillReqEffort: db.prepare("UPDATE requests SET effort = ? WHERE file_id = ? AND effort IS NULL"),
|
|
250
267
|
};
|
|
251
268
|
const stateByFile = new Map();
|
|
252
269
|
|
|
@@ -261,17 +278,25 @@ export function createIngester(db, timeZone) {
|
|
|
261
278
|
let st = stateByFile.get(file.id);
|
|
262
279
|
if (!st) {
|
|
263
280
|
const prevStats = file.lines > 0 ? q.getStats.get(file.id) : null;
|
|
281
|
+
const ps = parseParserState(prevStats?.parser_state);
|
|
264
282
|
st = {
|
|
265
283
|
launchCwd: file.cwd ?? null,
|
|
266
|
-
cwd: file.cwd ?? null,
|
|
267
|
-
model:
|
|
284
|
+
cwd: str(ps?.cwd) ?? file.cwd ?? null,
|
|
285
|
+
model: str(ps?.model) ?? null,
|
|
286
|
+
effort: str(ps?.effort) ?? null,
|
|
287
|
+
sawTokenRec: Boolean(ps?.sawTokenRec),
|
|
288
|
+
lastTotal: str(ps?.lastTotal) ?? null,
|
|
289
|
+
lastCompactionLine: typeof ps?.lastCompactionLine === "number" ? ps.lastCompactionLine : -999,
|
|
268
290
|
callStart: new Map(), callSkills: new Map(),
|
|
269
291
|
turnStartMs: null, turnToolMs: 0, openApiMs: 0, lastWaitFromMs: null,
|
|
270
292
|
completedApiMs: prevStats?.api_ms ?? 0, toolMs: prevStats?.tool_ms ?? 0,
|
|
271
293
|
baseAdded: prevStats?.lines_added ?? 0, baseRemoved: prevStats?.lines_removed ?? 0,
|
|
272
|
-
sawFileChange:
|
|
294
|
+
sawFileChange: Boolean(ps?.sawFileChange),
|
|
295
|
+
fcAdded: 0, fcRemoved: 0, patchAdded: 0, patchRemoved: 0,
|
|
273
296
|
paths: createPathTracker(prevStats?.target_paths ?? null),
|
|
274
|
-
|
|
297
|
+
hadUnknownModel: Boolean(ps?.hadUnknownModel),
|
|
298
|
+
hadNullEffort: Boolean(ps?.hadNullEffort),
|
|
299
|
+
statsDirty: false, pathsDirty: false, stateDirty: false,
|
|
275
300
|
};
|
|
276
301
|
stateByFile.set(file.id, st);
|
|
277
302
|
}
|
|
@@ -286,6 +311,34 @@ export function createIngester(db, timeZone) {
|
|
|
286
311
|
let cwd = p.cwd ?? null;
|
|
287
312
|
let added = 0;
|
|
288
313
|
|
|
314
|
+
const setModel = (raw) => {
|
|
315
|
+
const next = modelOf(raw);
|
|
316
|
+
if (!next) return;
|
|
317
|
+
if (!st.model && st.hadUnknownModel) {
|
|
318
|
+
q.backfillReqModel.run(next, familyOf(next), file.id);
|
|
319
|
+
q.backfillCallModel.run(next, file.id);
|
|
320
|
+
st.hadUnknownModel = false;
|
|
321
|
+
st.stateDirty = true;
|
|
322
|
+
}
|
|
323
|
+
if (st.model !== next) {
|
|
324
|
+
st.model = next;
|
|
325
|
+
st.stateDirty = true;
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
const setEffort = (raw) => {
|
|
329
|
+
const next = str(raw);
|
|
330
|
+
if (!next) return;
|
|
331
|
+
if (!st.effort && st.hadNullEffort) {
|
|
332
|
+
q.backfillReqEffort.run(next, file.id);
|
|
333
|
+
st.hadNullEffort = false;
|
|
334
|
+
st.stateDirty = true;
|
|
335
|
+
}
|
|
336
|
+
if (st.effort !== next) {
|
|
337
|
+
st.effort = next;
|
|
338
|
+
st.stateDirty = true;
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
|
|
289
342
|
const consumeOpenWait = () => {
|
|
290
343
|
if (tsMs !== null && st.lastWaitFromMs !== null) {
|
|
291
344
|
const dt = tsMs - st.lastWaitFromMs;
|
|
@@ -310,6 +363,14 @@ export function createIngester(db, timeZone) {
|
|
|
310
363
|
const output = u.output_tokens ?? 0;
|
|
311
364
|
const thinking = u.reasoning_output_tokens ?? 0;
|
|
312
365
|
if (!input && !output && !cacheRead && !cacheWrite) return false;
|
|
366
|
+
if (!st.model && !st.hadUnknownModel) {
|
|
367
|
+
st.hadUnknownModel = true;
|
|
368
|
+
st.stateDirty = true;
|
|
369
|
+
}
|
|
370
|
+
if (!st.effort && !st.hadNullEffort) {
|
|
371
|
+
st.hadNullEffort = true;
|
|
372
|
+
st.stateDirty = true;
|
|
373
|
+
}
|
|
313
374
|
const model = st.model ?? "unknown";
|
|
314
375
|
const { day, hour } = zone(ts);
|
|
315
376
|
q.record.run(`codex:${ownId}:${line}`, file.id, line, rec.type, ts);
|
|
@@ -321,40 +382,52 @@ export function createIngester(db, timeZone) {
|
|
|
321
382
|
if (rec.type === "session_meta") {
|
|
322
383
|
if (p.cwd) {
|
|
323
384
|
st.launchCwd ??= p.cwd;
|
|
324
|
-
st.cwd
|
|
385
|
+
if (st.cwd !== p.cwd) {
|
|
386
|
+
st.cwd = p.cwd;
|
|
387
|
+
st.stateDirty = true;
|
|
388
|
+
}
|
|
325
389
|
}
|
|
326
390
|
const subType = subagentTypeOf(p.source);
|
|
327
391
|
const metaSub = p.parent_thread_id || subType ? (subType ?? "subagent") : null;
|
|
328
392
|
const repoUrl = p.git?.repository_url ?? null;
|
|
329
393
|
if (metaSub || repoUrl) q.fileMeta.run(file.id, p.parent_thread_id ?? null, metaSub, null, repoUrl);
|
|
330
|
-
|
|
394
|
+
setModel(p.model);
|
|
331
395
|
} else if (rec.type === "turn_context") {
|
|
332
396
|
if (p.cwd) {
|
|
333
397
|
st.launchCwd ??= p.cwd;
|
|
334
|
-
st.cwd
|
|
398
|
+
if (st.cwd !== p.cwd) {
|
|
399
|
+
st.cwd = p.cwd;
|
|
400
|
+
st.stateDirty = true;
|
|
401
|
+
}
|
|
335
402
|
if (p.cwd !== st.launchCwd && st.paths.add(p.cwd, { write: false })) st.pathsDirty = true;
|
|
336
403
|
}
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
if (eff) st.effort = eff;
|
|
404
|
+
setModel(p.model ?? p.collaboration_mode?.settings?.model);
|
|
405
|
+
setEffort(p.effort ?? p.collaboration_mode?.settings?.reasoning_effort);
|
|
340
406
|
} else if (rec.type === "world_state") {
|
|
341
|
-
|
|
342
|
-
|
|
407
|
+
setModel(modelOf(p.state?.model) ?? modelOf(p.state?.collaboration_mode?.model) ?? modelOf(p.state?.collaboration_mode?.settings?.model));
|
|
408
|
+
setEffort(p.state?.effort ?? p.state?.reasoning_effort ?? p.state?.collaboration_mode?.settings?.reasoning_effort);
|
|
343
409
|
} else if (rec.type === "token_usage_record") {
|
|
344
410
|
consumeOpenWait();
|
|
345
|
-
st.sawTokenRec
|
|
411
|
+
if (!st.sawTokenRec) {
|
|
412
|
+
st.sawTokenRec = true;
|
|
413
|
+
st.stateDirty = true;
|
|
414
|
+
}
|
|
346
415
|
const msgId = `codex:${p.response_id || `${ownId}:${line}`}`;
|
|
347
416
|
if (recordUsage(msgId, p.usage)) added = 1;
|
|
348
417
|
} else if (rec.type === "compacted") {
|
|
418
|
+
if (!st.model) setModel(p.resume_metadata?.previous_turn_settings?.model);
|
|
419
|
+
if (!st.effort) setEffort(p.resume_metadata?.previous_turn_settings?.reasoning_effort ?? p.resume_metadata?.previous_turn_settings?.effort);
|
|
349
420
|
if (line - st.lastCompactionLine > 5) {
|
|
350
421
|
st.lastCompactionLine = line;
|
|
422
|
+
st.stateDirty = true;
|
|
351
423
|
q.record.run(`codex:${ownId}:${line}`, file.id, line, rec.type, ts);
|
|
352
424
|
q.compaction.run(`codex:${ownId}:${line}`, file.id, line, session, agent, ts, "auto", null, null, null);
|
|
353
425
|
added = 1;
|
|
354
426
|
}
|
|
355
427
|
} else if (rec.type === "response_item") {
|
|
356
|
-
if (p.type === "configuration_update"
|
|
357
|
-
|
|
428
|
+
if (p.type === "configuration_update") {
|
|
429
|
+
setModel(p.model);
|
|
430
|
+
setEffort(p.reasoning?.effort ?? p.effort);
|
|
358
431
|
} else if (p.type === "message" && p.role === "user") {
|
|
359
432
|
const msg = userMessageText(p.content);
|
|
360
433
|
if (msg) {
|
|
@@ -373,6 +446,10 @@ export function createIngester(db, timeZone) {
|
|
|
373
446
|
consumeOpenWait();
|
|
374
447
|
} else if (p.type === "function_call" || p.type === "custom_tool_call") {
|
|
375
448
|
consumeOpenWait();
|
|
449
|
+
if (!st.model && !st.hadUnknownModel) {
|
|
450
|
+
st.hadUnknownModel = true;
|
|
451
|
+
st.stateDirty = true;
|
|
452
|
+
}
|
|
376
453
|
q.record.run(`codex:${ownId}:${line}`, file.id, line, p.type, ts);
|
|
377
454
|
q.call.run(`codex:${p.call_id || `${ownId}:${line}`}`, file.id, line, session, agent, ts, p.name ?? "unknown", st.model ?? null);
|
|
378
455
|
if (tsMs !== null && p.call_id) st.callStart.set(p.call_id, tsMs);
|
|
@@ -434,13 +511,18 @@ export function createIngester(db, timeZone) {
|
|
|
434
511
|
} else if (p.type === "compaction") {
|
|
435
512
|
if (line - st.lastCompactionLine > 5) {
|
|
436
513
|
st.lastCompactionLine = line;
|
|
514
|
+
st.stateDirty = true;
|
|
437
515
|
q.record.run(`codex:${ownId}:${line}`, file.id, line, p.type, ts);
|
|
438
516
|
q.compaction.run(`codex:${ownId}:${line}`, file.id, line, session, agent, ts, "auto", null, null, null);
|
|
439
517
|
added = 1;
|
|
440
518
|
}
|
|
441
519
|
}
|
|
442
520
|
} else if (rec.type === "event_msg") {
|
|
443
|
-
if (p.type === "
|
|
521
|
+
if (p.type === "thread_settings_applied") {
|
|
522
|
+
const tsSettings = p.thread_settings;
|
|
523
|
+
setModel(tsSettings?.model ?? tsSettings?.collaboration_mode?.settings?.model);
|
|
524
|
+
setEffort(tsSettings?.reasoning_effort ?? tsSettings?.effort ?? tsSettings?.collaboration_mode?.settings?.reasoning_effort);
|
|
525
|
+
} else if (p.type === "task_started" || p.type === "turn_started") {
|
|
444
526
|
if (tsMs !== null) {
|
|
445
527
|
st.turnStartMs = tsMs;
|
|
446
528
|
st.turnToolMs = 0;
|
|
@@ -486,7 +568,10 @@ export function createIngester(db, timeZone) {
|
|
|
486
568
|
st.fcRemoved += d.removed;
|
|
487
569
|
}
|
|
488
570
|
}
|
|
489
|
-
st.sawFileChange
|
|
571
|
+
if (!st.sawFileChange) {
|
|
572
|
+
st.sawFileChange = true;
|
|
573
|
+
st.stateDirty = true;
|
|
574
|
+
}
|
|
490
575
|
st.statsDirty = true;
|
|
491
576
|
}
|
|
492
577
|
} else if (p.type === "token_count" && !st.sawTokenRec && p.info?.last_token_usage) {
|
|
@@ -494,7 +579,10 @@ export function createIngester(db, timeZone) {
|
|
|
494
579
|
const tot = p.info.total_token_usage;
|
|
495
580
|
const totKey = tot ? `${tot.total_tokens}|${tot.input_tokens}|${tot.output_tokens}|${tot.cached_input_tokens}` : null;
|
|
496
581
|
if (!totKey || totKey !== st.lastTotal) {
|
|
497
|
-
st.lastTotal
|
|
582
|
+
if (st.lastTotal !== totKey) {
|
|
583
|
+
st.lastTotal = totKey;
|
|
584
|
+
st.stateDirty = true;
|
|
585
|
+
}
|
|
498
586
|
if (recordUsage(`codex:${ownId}:${line}`, p.info.last_token_usage)) added = 1;
|
|
499
587
|
}
|
|
500
588
|
}
|
|
@@ -519,6 +607,20 @@ export function createIngester(db, timeZone) {
|
|
|
519
607
|
const json = st.paths.toJSON();
|
|
520
608
|
if (json) q.upsertTargetPaths.run(fileId, json);
|
|
521
609
|
}
|
|
610
|
+
if (st.stateDirty) {
|
|
611
|
+
st.stateDirty = false;
|
|
612
|
+
q.upsertParserState.run(fileId, JSON.stringify({
|
|
613
|
+
cwd: st.cwd,
|
|
614
|
+
model: st.model,
|
|
615
|
+
effort: st.effort,
|
|
616
|
+
sawTokenRec: st.sawTokenRec,
|
|
617
|
+
lastTotal: st.lastTotal,
|
|
618
|
+
lastCompactionLine: st.lastCompactionLine,
|
|
619
|
+
sawFileChange: st.sawFileChange,
|
|
620
|
+
hadUnknownModel: st.hadUnknownModel,
|
|
621
|
+
hadNullEffort: st.hadNullEffort,
|
|
622
|
+
}));
|
|
623
|
+
}
|
|
522
624
|
};
|
|
523
625
|
ingestLine.reset = (fileId) => { stateByFile.delete(fileId); };
|
|
524
626
|
return ingestLine;
|
package/src/serve.mjs
CHANGED
|
@@ -23,18 +23,28 @@ const TEXT = { "content-type": "text/plain; charset=utf-8" };
|
|
|
23
23
|
export function serve(config, { port, onScan = () => {}, dist = DIST, routes }) {
|
|
24
24
|
const api = createApi(config, { onScan, routes });
|
|
25
25
|
const server = createServer(async (req, res) => {
|
|
26
|
-
if (await api(req, res)) return;
|
|
27
|
-
if (req.method !== "GET") return res.writeHead(405, TEXT).end("method not allowed\n");
|
|
28
|
-
const path = normalize(decodeURIComponent(new URL(req.url, "http://localhost").pathname));
|
|
29
|
-
const file = join(dist, path);
|
|
30
|
-
const asset = file.startsWith(dist + sep) && extname(path) ? file : join(dist, "index.html");
|
|
31
26
|
try {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
27
|
+
if (await api(req, res)) return;
|
|
28
|
+
if (req.method !== "GET") return res.writeHead(405, TEXT).end("method not allowed\n");
|
|
29
|
+
let path;
|
|
30
|
+
try {
|
|
31
|
+
path = normalize(decodeURIComponent(new URL(req.url, "http://localhost").pathname));
|
|
32
|
+
} catch {
|
|
33
|
+
return res.writeHead(400, TEXT).end("bad request\n");
|
|
34
|
+
}
|
|
35
|
+
const file = join(dist, path);
|
|
36
|
+
const asset = file.startsWith(dist + sep) && extname(path) ? file : join(dist, "index.html");
|
|
37
|
+
try {
|
|
38
|
+
const body = await readFile(asset);
|
|
39
|
+
const cacheControl = asset.startsWith(join(dist, "assets") + sep) ? "max-age=31536000, immutable" : "no-store";
|
|
40
|
+
res.writeHead(200, { "content-type": TYPES[extname(asset)] ?? "application/octet-stream", "cache-control": cacheControl }).end(body);
|
|
41
|
+
} catch {
|
|
42
|
+
if (asset.endsWith("index.html")) res.writeHead(503, TEXT).end("the dashboard is not built: run npm run build, or npm run dev\n");
|
|
43
|
+
else res.writeHead(404, TEXT).end("not found\n");
|
|
44
|
+
}
|
|
35
45
|
} catch {
|
|
36
|
-
if (
|
|
37
|
-
else res.
|
|
46
|
+
if (!res.headersSent) res.writeHead(500, TEXT).end("internal server error\n");
|
|
47
|
+
else if (!res.writableEnded) res.end();
|
|
38
48
|
}
|
|
39
49
|
});
|
|
40
50
|
return new Promise((resolve, reject) => {
|