llmtop 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +114 -2
- package/bin/llmtop.mjs +101 -0
- package/dashboard/dist/apple-touch-icon.png +0 -0
- package/dashboard/dist/assets/index-BVbAwMVB.js +9 -0
- package/dashboard/dist/assets/index-Cp7Swcz5.css +1 -0
- package/dashboard/dist/assets/llmtop-lockup-DtFPJmhv.svg +1 -0
- package/dashboard/dist/assets/llmtop-lockup-reversed-D9OmQIVU.svg +1 -0
- package/dashboard/dist/favicon.ico +0 -0
- package/dashboard/dist/favicon.svg +1 -0
- package/dashboard/dist/index.html +19 -0
- package/dashboard/dist/llmtop-mark-192.png +0 -0
- package/dashboard/dist/llmtop-mark-512.png +0 -0
- package/dashboard/dist/site.webmanifest +11 -0
- package/dashboard/src/App.tsx +213 -0
- package/dashboard/src/controls/DateRange.tsx +108 -0
- package/dashboard/src/controls/Disclosure.tsx +15 -0
- package/dashboard/src/controls/Search.tsx +18 -0
- package/dashboard/src/controls/Select.tsx +48 -0
- package/dashboard/src/controls/popover.tsx +31 -0
- package/dashboard/src/dash.ts +23 -0
- package/dashboard/src/extra.test.ts +9 -0
- package/dashboard/src/extra.ts +16 -0
- package/dashboard/src/format.ts +29 -0
- package/dashboard/src/i18n.tsx +52 -0
- package/dashboard/src/index.ts +7 -0
- package/dashboard/src/main.tsx +7 -0
- package/dashboard/src/model.test.ts +110 -0
- package/dashboard/src/model.ts +255 -0
- package/dashboard/src/state.test.ts +48 -0
- package/dashboard/src/state.ts +61 -0
- package/dashboard/src/styles.css +401 -0
- package/dashboard/src/tip.ts +51 -0
- package/dashboard/src/treemap.test.ts +23 -0
- package/dashboard/src/treemap.ts +30 -0
- package/dashboard/src/ui.tsx +327 -0
- package/dashboard/src/views/Activity.tsx +177 -0
- package/dashboard/src/views/Context.tsx +183 -0
- package/dashboard/src/views/Models.tsx +155 -0
- package/dashboard/src/views/Overview.tsx +83 -0
- package/dashboard/src/views/Projects.tsx +116 -0
- package/dashboard/src/views/Sessions.tsx +54 -0
- package/dashboard/src/views/Subagents.tsx +102 -0
- package/docs/brand/README.md +91 -0
- package/docs/brand/brand.json +67 -0
- package/docs/brand/favicon.ico +0 -0
- package/docs/brand/png/apple-touch-icon.png +0 -0
- package/docs/brand/png/favicon-16.png +0 -0
- package/docs/brand/png/favicon-32.png +0 -0
- package/docs/brand/png/favicon-48.png +0 -0
- package/docs/brand/png/llmtop-lockup-reversed@2x.png +0 -0
- package/docs/brand/png/llmtop-lockup@2x.png +0 -0
- package/docs/brand/png/llmtop-mark-1024.png +0 -0
- package/docs/brand/png/llmtop-mark-192.png +0 -0
- package/docs/brand/png/llmtop-mark-512.png +0 -0
- package/docs/brand/png/llmtop-wordmark@2x.png +0 -0
- package/docs/brand/source/build_brand.py +173 -0
- package/docs/brand/svg/favicon.svg +1 -0
- package/docs/brand/svg/llmtop-glyph.svg +1 -0
- package/docs/brand/svg/llmtop-lockup-reversed.svg +1 -0
- package/docs/brand/svg/llmtop-lockup.svg +1 -0
- package/docs/brand/svg/llmtop-mark-light.svg +1 -0
- package/docs/brand/svg/llmtop-mark.svg +1 -0
- package/docs/brand/svg/llmtop-wordmark-reversed.svg +1 -0
- package/docs/brand/svg/llmtop-wordmark.svg +1 -0
- package/llmtop.config.example.json +10 -0
- package/package.json +73 -4
- package/prices.md +27 -0
- package/src/api.mjs +53 -0
- package/src/archive.mjs +142 -0
- package/src/config.mjs +55 -0
- package/src/context.mjs +151 -0
- package/src/dashboard.mjs +61 -0
- package/src/db.mjs +102 -0
- package/src/derive.mjs +346 -0
- package/src/effort.mjs +18 -0
- package/src/format.mjs +19 -0
- package/src/paths.mjs +82 -0
- package/src/prices-command.mjs +25 -0
- package/src/prices.mjs +146 -0
- package/src/providers/agy.mjs +438 -0
- package/src/providers/claude.mjs +274 -0
- package/src/providers/codex.mjs +589 -0
- package/src/providers/index.mjs +25 -0
- package/src/rebuilds.mjs +85 -0
- package/src/scan.mjs +214 -0
- package/src/serve.mjs +44 -0
- package/src/show.mjs +20 -0
- package/src/usage-command.mjs +110 -0
- package/src/usage.mjs +388 -0
- package/src/zone.mjs +15 -0
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
// Everything that knows the Antigravity CLI format under ~/.gemini/antigravity-cli:
|
|
2
|
+
// brain/<id>/.system_generated/logs/transcript.jsonl and conversation_summaries.db.
|
|
3
|
+
// input_tokens in PLANNER_RESPONSE excludes cache_read_tokens; effort is encoded as a model suffix.
|
|
4
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { join, sep } from "node:path";
|
|
6
|
+
import { DatabaseSync } from "node:sqlite";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
import { createPathTracker, extractCommandDirs } from "../paths.mjs";
|
|
9
|
+
import { zoner } from "../zone.mjs";
|
|
10
|
+
|
|
11
|
+
export const defaultPath = "~/.gemini/antigravity-cli";
|
|
12
|
+
export const label = "Antigravity";
|
|
13
|
+
|
|
14
|
+
const isDir = (p) => {
|
|
15
|
+
try {
|
|
16
|
+
return statSync(p).isDirectory();
|
|
17
|
+
} catch {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
const isFile = (p) => {
|
|
22
|
+
try {
|
|
23
|
+
return statSync(p).isFile();
|
|
24
|
+
} catch {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
const list = (p) => (isDir(p) ? readdirSync(p, { withFileTypes: true }) : []);
|
|
29
|
+
|
|
30
|
+
export function describe(rel) {
|
|
31
|
+
const parts = rel.split(sep);
|
|
32
|
+
if (parts.length === 5 && parts[0] === "brain" && parts[2] === ".system_generated" && parts[3] === "logs" && parts[4] === "transcript.jsonl") {
|
|
33
|
+
return { rel, kind: "main", slug: parts[1], sessionId: parts[1], agentId: null };
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function listHost(root) {
|
|
39
|
+
const transcripts = [];
|
|
40
|
+
const brain = join(root, "brain");
|
|
41
|
+
for (const e of list(brain)) {
|
|
42
|
+
if (!e.isDirectory()) continue;
|
|
43
|
+
const rel = join("brain", e.name, ".system_generated", "logs", "transcript.jsonl");
|
|
44
|
+
if (isFile(join(root, rel))) {
|
|
45
|
+
const d = describe(rel);
|
|
46
|
+
if (d) transcripts.push(d);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return { transcripts, others: [] };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function uriToPath(raw) {
|
|
53
|
+
if (!raw) return null;
|
|
54
|
+
let first = raw;
|
|
55
|
+
if (raw.startsWith("[")) {
|
|
56
|
+
try {
|
|
57
|
+
const arr = JSON.parse(raw);
|
|
58
|
+
first = Array.isArray(arr) ? arr[0] : null;
|
|
59
|
+
} catch {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (typeof first !== "string" || !first) return null;
|
|
64
|
+
if (first.startsWith("file://")) {
|
|
65
|
+
try {
|
|
66
|
+
return fileURLToPath(first);
|
|
67
|
+
} catch {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return first;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Snapshot conversation metadata from conversation_summaries.db into archive/agy/summaries.json (read-only on host).
|
|
75
|
+
export function syncMeta(root, archiveDir) {
|
|
76
|
+
const dbPath = join(root, "conversation_summaries.db");
|
|
77
|
+
if (!existsSync(dbPath)) return 0;
|
|
78
|
+
const outPath = join(archiveDir, "summaries.json");
|
|
79
|
+
let merged = {};
|
|
80
|
+
if (existsSync(outPath)) {
|
|
81
|
+
try {
|
|
82
|
+
merged = JSON.parse(readFileSync(outPath, "utf8"));
|
|
83
|
+
} catch {
|
|
84
|
+
merged = {};
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
let db;
|
|
88
|
+
try {
|
|
89
|
+
db = new DatabaseSync(dbPath, { readOnly: true });
|
|
90
|
+
const rows = db.prepare("SELECT conversation_id, title, workspace_uris, parent_conversation_id, agent_name FROM conversation_summaries").all();
|
|
91
|
+
const parents = new Map();
|
|
92
|
+
try {
|
|
93
|
+
for (const r of db.prepare("SELECT conversation_id, parent_id FROM conversation_parents").all()) parents.set(r.conversation_id, r.parent_id);
|
|
94
|
+
} catch { /* optional table */ }
|
|
95
|
+
for (const r of rows) {
|
|
96
|
+
const cwd = uriToPath(r.workspace_uris);
|
|
97
|
+
const parentId = r.parent_conversation_id || parents.get(r.conversation_id) || "";
|
|
98
|
+
merged[r.conversation_id] = {
|
|
99
|
+
...(r.title ? { title: r.title } : {}),
|
|
100
|
+
...(cwd ? { cwd } : {}),
|
|
101
|
+
...(parentId ? { parentId } : {}),
|
|
102
|
+
...(r.agent_name ? { agentName: r.agent_name } : {}),
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
} catch {
|
|
106
|
+
return 0;
|
|
107
|
+
} finally {
|
|
108
|
+
try { db?.close(); } catch { /* ignore */ }
|
|
109
|
+
}
|
|
110
|
+
if (!Object.keys(merged).length) return 0;
|
|
111
|
+
const next = JSON.stringify(merged, null, 1) + "\n";
|
|
112
|
+
const prev = existsSync(outPath) ? readFileSync(outPath, "utf8") : "";
|
|
113
|
+
if (next === prev) return 0;
|
|
114
|
+
mkdirSync(archiveDir, { recursive: true });
|
|
115
|
+
writeFileSync(outPath, next);
|
|
116
|
+
return 1;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const EFFORT_RE = /^(.*)-(low|medium|high|xhigh|max)$/;
|
|
120
|
+
export function splitModelEffort(raw) {
|
|
121
|
+
if (!raw) return { model: "unknown", effort: null };
|
|
122
|
+
const m = raw.match(EFFORT_RE);
|
|
123
|
+
return m ? { model: m[1], effort: m[2] } : { model: raw, effort: null };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function familyOf(model) {
|
|
127
|
+
const base = splitModelEffort(model.replace(/\[.*\]$/, "")).model;
|
|
128
|
+
const m = base.match(/^gemini-[\d.]+-([a-z][a-z0-9-]*)$/i);
|
|
129
|
+
return m ? `gemini-${m[1].toLowerCase()}` : base;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const cap = (w) => (w ? w[0].toUpperCase() + w.slice(1) : w);
|
|
133
|
+
export function displayModel(family) {
|
|
134
|
+
const v = family.match(/^gemini-[\d.]+-(.+)$/i);
|
|
135
|
+
if (v) return `Gemini ${v[1].split("-").map(cap).join(" ")}`;
|
|
136
|
+
const m = family.match(/^gemini-(.+)$/i);
|
|
137
|
+
if (m) return `Gemini ${m[1].split("-").map(cap).join(" ")}`;
|
|
138
|
+
return family;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function unquote(v) {
|
|
142
|
+
if (typeof v !== "string") return "";
|
|
143
|
+
if (v.startsWith('"')) {
|
|
144
|
+
try {
|
|
145
|
+
const p = JSON.parse(v);
|
|
146
|
+
if (typeof p === "string") return p;
|
|
147
|
+
} catch { /* raw string */ }
|
|
148
|
+
}
|
|
149
|
+
return v;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const lineCount = (s) => (s ? s.replace(/\n$/, "").split("\n").length : 0);
|
|
153
|
+
function diffPair(target, replacement) {
|
|
154
|
+
if (!target && !replacement) return { added: 0, removed: 0 };
|
|
155
|
+
if (!target) return { added: lineCount(replacement), removed: 0 };
|
|
156
|
+
if (!replacement) return { added: 0, removed: lineCount(target) };
|
|
157
|
+
const a = target.replace(/\n$/, "").split("\n");
|
|
158
|
+
const b = replacement.replace(/\n$/, "").split("\n");
|
|
159
|
+
let p = 0;
|
|
160
|
+
while (p < a.length && p < b.length && a[p] === b[p]) p++;
|
|
161
|
+
let sa = a.length;
|
|
162
|
+
let sb = b.length;
|
|
163
|
+
while (sa > p && sb > p && a[sa - 1] === b[sb - 1]) {
|
|
164
|
+
sa--;
|
|
165
|
+
sb--;
|
|
166
|
+
}
|
|
167
|
+
return { added: sb - p, removed: sa - p };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function diffLineCounts(name, args) {
|
|
171
|
+
if (!args || typeof args !== "object") return { added: 0, removed: 0 };
|
|
172
|
+
if (name === "edit_file" || name === "replace_file_content") {
|
|
173
|
+
return diffPair(unquote(args.Target ?? args.TargetContent ?? ""), unquote(args.Replacement ?? args.ReplacementContent ?? ""));
|
|
174
|
+
}
|
|
175
|
+
if (name === "multi_replace_file_content") {
|
|
176
|
+
let chunks = args.ReplacementChunks;
|
|
177
|
+
if (typeof chunks === "string") {
|
|
178
|
+
try { chunks = JSON.parse(chunks); } catch { chunks = null; }
|
|
179
|
+
}
|
|
180
|
+
if (!Array.isArray(chunks)) return { added: 0, removed: 0 };
|
|
181
|
+
let added = 0;
|
|
182
|
+
let removed = 0;
|
|
183
|
+
for (const c of chunks) {
|
|
184
|
+
const d = diffPair(String(c?.TargetContent ?? ""), String(c?.ReplacementContent ?? ""));
|
|
185
|
+
added += d.added;
|
|
186
|
+
removed += d.removed;
|
|
187
|
+
}
|
|
188
|
+
return { added, removed };
|
|
189
|
+
}
|
|
190
|
+
if (name === "write_file" || name === "write_to_file") {
|
|
191
|
+
return { added: lineCount(unquote(args.Content ?? args.CodeContent ?? "")), removed: 0 };
|
|
192
|
+
}
|
|
193
|
+
return { added: 0, removed: 0 };
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const SKILL_PATH_RE = /([^\s"'`<>()\\]*?skills\/([a-z0-9][a-z0-9._-]{0,63})\/SKILL\.md)\b/i;
|
|
197
|
+
const SLASH_CMD_RE = /^\/([a-z0-9][a-z0-9_-]{0,63})(?=\s|$)/i;
|
|
198
|
+
function skillSourceOf(path) {
|
|
199
|
+
if (/\/builtin\/skills\//i.test(path)) return "builtin";
|
|
200
|
+
if (/^~\/|\/(?:home|Users)\/[^/]+\/\.(?:agents|codex|claude|gemini)\//i.test(path)) return "personal";
|
|
201
|
+
return "project";
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function isGenericError(rec) {
|
|
205
|
+
if (rec.error) return true;
|
|
206
|
+
if (rec.status === "ERROR" || rec.status === "FAILED") return true;
|
|
207
|
+
if (typeof rec.content === "string") {
|
|
208
|
+
if (/Command exited with code [1-9]\d*|Encountered error in tool execution/i.test(rec.content)) return true;
|
|
209
|
+
}
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export function createIngester(db, timeZone) {
|
|
214
|
+
const zone = zoner(timeZone);
|
|
215
|
+
const q = {
|
|
216
|
+
record: db.prepare("INSERT OR IGNORE INTO records(uuid, file_id, line, type, ts) VALUES (?, ?, ?, ?, ?)"),
|
|
217
|
+
request: db.prepare(`INSERT OR IGNORE INTO requests(message_id, file_id, line, session_id, agent_id, ts, day, hour, model, family,
|
|
218
|
+
effort, is_sub, input, output, thinking, cache_read, cache_write, cache_write_1h) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`),
|
|
219
|
+
owner: db.prepare("INSERT OR IGNORE INTO owner_messages(uuid, file_id, line, session_id, ts, kind, text) VALUES (?, ?, ?, ?, ?, ?, ?)"),
|
|
220
|
+
call: db.prepare("INSERT OR IGNORE INTO tool_calls(id, file_id, line, session_id, agent_id, ts, name, model, is_error) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0)"),
|
|
221
|
+
callErr: db.prepare("UPDATE tool_calls SET is_error = 1 WHERE id = ?"),
|
|
222
|
+
skill: db.prepare("INSERT OR IGNORE INTO skills(id, file_id, line, session_id, ts, name, via, source, is_error) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0)"),
|
|
223
|
+
skillErr: db.prepare("UPDATE skills SET is_error = 1 WHERE id = ?"),
|
|
224
|
+
getStats: db.prepare("SELECT api_ms, tool_ms, lines_added, lines_removed, target_paths FROM file_meta WHERE file_id = ?"),
|
|
225
|
+
upsertStats: db.prepare(`INSERT INTO file_meta(file_id, api_ms, tool_ms, lines_added, lines_removed) VALUES (?, ?, ?, ?, ?)
|
|
226
|
+
ON CONFLICT(file_id) DO UPDATE SET api_ms = excluded.api_ms, tool_ms = excluded.tool_ms,
|
|
227
|
+
lines_added = excluded.lines_added, lines_removed = excluded.lines_removed`),
|
|
228
|
+
upsertTargetPaths: db.prepare(`INSERT INTO file_meta(file_id, target_paths) VALUES (?, ?)
|
|
229
|
+
ON CONFLICT(file_id) DO UPDATE SET target_paths = excluded.target_paths`),
|
|
230
|
+
};
|
|
231
|
+
const stateByFile = new Map();
|
|
232
|
+
const AGY_WRITE_TOOLS = new Set(["edit_file", "write_file", "replace_file_content", "multi_replace_file_content", "write_to_file"]);
|
|
233
|
+
const AGY_READ_TOOLS = new Set(["read_file", "view_file", "grep_search", "find_by_name", "list_dir", "run_command"]);
|
|
234
|
+
|
|
235
|
+
return function ingestLine(file, line, text) {
|
|
236
|
+
let rec;
|
|
237
|
+
try {
|
|
238
|
+
rec = JSON.parse(text);
|
|
239
|
+
} catch {
|
|
240
|
+
return { added: 0 };
|
|
241
|
+
}
|
|
242
|
+
if (!rec || typeof rec !== "object") return { added: 0 };
|
|
243
|
+
let st = stateByFile.get(file.id);
|
|
244
|
+
if (!st) {
|
|
245
|
+
const prevStats = file.lines > 0 ? q.getStats.get(file.id) : null;
|
|
246
|
+
st = {
|
|
247
|
+
lastType: null, lastTsMs: null, lastCalls: [],
|
|
248
|
+
apiMs: prevStats?.api_ms ?? 0, toolMs: prevStats?.tool_ms ?? 0,
|
|
249
|
+
linesAdded: prevStats?.lines_added ?? 0, linesRemoved: prevStats?.lines_removed ?? 0,
|
|
250
|
+
paths: createPathTracker(prevStats?.target_paths ?? null),
|
|
251
|
+
};
|
|
252
|
+
stateByFile.set(file.id, st);
|
|
253
|
+
}
|
|
254
|
+
const ownId = file.agent_id ?? file.session_id;
|
|
255
|
+
const session = file.session_id;
|
|
256
|
+
const agent = file.agent_id;
|
|
257
|
+
const isSub = file.kind === "sub" ? 1 : 0;
|
|
258
|
+
const step = rec.step_index ?? line;
|
|
259
|
+
const ts = rec.created_at ?? null;
|
|
260
|
+
const parsedTs = ts ? Date.parse(ts) : NaN;
|
|
261
|
+
const tsMs = Number.isFinite(parsedTs) ? parsedTs : null;
|
|
262
|
+
const uuid = `agy:${ownId}:${step}`;
|
|
263
|
+
let added = 0;
|
|
264
|
+
let statsDirty = false;
|
|
265
|
+
let pathsDirty = false;
|
|
266
|
+
|
|
267
|
+
if (tsMs !== null && st.lastTsMs !== null) {
|
|
268
|
+
const dt = tsMs - st.lastTsMs;
|
|
269
|
+
if (dt > 0 && dt < 600_000) {
|
|
270
|
+
if (rec.type === "PLANNER_RESPONSE" && (st.lastType === "USER_INPUT" || st.lastType === "GENERIC" || st.lastType === "SYSTEM_MESSAGE")) {
|
|
271
|
+
st.apiMs += Math.round(dt);
|
|
272
|
+
statsDirty = true;
|
|
273
|
+
} else if (rec.type === "GENERIC" && st.lastType === "PLANNER_RESPONSE") {
|
|
274
|
+
st.toolMs += Math.round(dt);
|
|
275
|
+
statsDirty = true;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
if (rec.type === "PLANNER_RESPONSE") {
|
|
281
|
+
const { model, effort } = splitModelEffort(rec.model ?? "unknown");
|
|
282
|
+
const input = rec.input_tokens ?? 0;
|
|
283
|
+
const cacheRead = rec.cache_read_tokens ?? 0;
|
|
284
|
+
const output = rec.output_tokens ?? 0;
|
|
285
|
+
if ((input > 0 || output > 0 || cacheRead > 0) && ts) {
|
|
286
|
+
const { day, hour } = zone(ts);
|
|
287
|
+
q.record.run(uuid, file.id, line, rec.type, ts);
|
|
288
|
+
q.request.run(uuid, file.id, line, session, agent, ts, day, hour, model, familyOf(model), effort, isSub,
|
|
289
|
+
input, output, 0, cacheRead, 0, null);
|
|
290
|
+
added = 1;
|
|
291
|
+
}
|
|
292
|
+
const calls = [];
|
|
293
|
+
for (const [idx, tc] of (Array.isArray(rec.tool_calls) ? rec.tool_calls : []).entries()) {
|
|
294
|
+
if (!tc?.name) continue;
|
|
295
|
+
const id = `${uuid}:${idx}`;
|
|
296
|
+
q.call.run(id, file.id, line, session, agent, ts, tc.name, model);
|
|
297
|
+
const diff = diffLineCounts(tc.name, tc.args);
|
|
298
|
+
if (diff.added || diff.removed) {
|
|
299
|
+
st.linesAdded += diff.added;
|
|
300
|
+
st.linesRemoved += diff.removed;
|
|
301
|
+
statsDirty = true;
|
|
302
|
+
}
|
|
303
|
+
if (tc.args && typeof tc.args === "object") {
|
|
304
|
+
if (AGY_WRITE_TOOLS.has(tc.name)) {
|
|
305
|
+
for (const k of ["Path", "TargetFile", "AbsolutePath"]) {
|
|
306
|
+
const val = unquote(tc.args[k]);
|
|
307
|
+
if (val && st.paths.add(val, { isFile: true, write: true })) pathsDirty = true;
|
|
308
|
+
}
|
|
309
|
+
} else if (AGY_READ_TOOLS.has(tc.name)) {
|
|
310
|
+
for (const k of ["Path", "AbsolutePath"]) {
|
|
311
|
+
const val = unquote(tc.args[k]);
|
|
312
|
+
if (val && st.paths.add(val, { isFile: true, write: false })) pathsDirty = true;
|
|
313
|
+
}
|
|
314
|
+
for (const k of ["SearchPath", "DirectoryPath", "Cwd"]) {
|
|
315
|
+
const val = unquote(tc.args[k]);
|
|
316
|
+
if (val && st.paths.add(val, { isFile: false, write: false })) pathsDirty = true;
|
|
317
|
+
}
|
|
318
|
+
if (tc.name === "run_command" && tc.args.CommandLine) {
|
|
319
|
+
const cmd = unquote(tc.args.CommandLine);
|
|
320
|
+
const cmdCwd = unquote(tc.args.Cwd) || null;
|
|
321
|
+
for (const dir of extractCommandDirs(cmd, cmdCwd)) {
|
|
322
|
+
if (st.paths.add(dir, { write: false })) pathsDirty = true;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
const skillIds = [];
|
|
328
|
+
if (tc.args && typeof tc.args === "object") {
|
|
329
|
+
for (const val of Object.values(tc.args)) {
|
|
330
|
+
const m = unquote(val).match(SKILL_PATH_RE);
|
|
331
|
+
if (m) {
|
|
332
|
+
const skName = m[2].toLowerCase();
|
|
333
|
+
const skId = `${id}:skill:${skName}`;
|
|
334
|
+
q.skill.run(skId, file.id, line, session, ts, skName, "agent", skillSourceOf(m[1]));
|
|
335
|
+
skillIds.push(skId);
|
|
336
|
+
break;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
calls.push({ id, skillIds });
|
|
341
|
+
added = 1;
|
|
342
|
+
}
|
|
343
|
+
if (calls.length) st.lastCalls = calls;
|
|
344
|
+
} else if (rec.type === "USER_INPUT") {
|
|
345
|
+
const body = typeof rec.content === "string" ? rec.content.trim() : "";
|
|
346
|
+
if ((!rec.source || rec.source === "USER_EXPLICIT") && body) {
|
|
347
|
+
q.record.run(uuid, file.id, line, rec.type, ts);
|
|
348
|
+
q.owner.run(uuid, file.id, line, session, ts, "typed", body);
|
|
349
|
+
const cmd = body.match(SLASH_CMD_RE);
|
|
350
|
+
if (cmd) q.skill.run(`${uuid}:cmd:${cmd[1].toLowerCase()}`, file.id, line, session, ts, cmd[1].toLowerCase(), "command", null);
|
|
351
|
+
added = 1;
|
|
352
|
+
}
|
|
353
|
+
} else if (rec.type === "GENERIC" && isGenericError(rec)) {
|
|
354
|
+
const last = st.lastCalls[st.lastCalls.length - 1];
|
|
355
|
+
if (last) {
|
|
356
|
+
q.callErr.run(last.id);
|
|
357
|
+
for (const skId of last.skillIds) q.skillErr.run(skId);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
if (tsMs !== null) {
|
|
362
|
+
st.lastTsMs = tsMs;
|
|
363
|
+
st.lastType = rec.type;
|
|
364
|
+
}
|
|
365
|
+
if (statsDirty && (st.apiMs > 0 || st.toolMs > 0 || st.linesAdded > 0 || st.linesRemoved > 0)) {
|
|
366
|
+
q.upsertStats.run(file.id, st.apiMs, st.toolMs, st.linesAdded, st.linesRemoved);
|
|
367
|
+
}
|
|
368
|
+
if (pathsDirty) {
|
|
369
|
+
const json = st.paths.toJSON();
|
|
370
|
+
if (json) q.upsertTargetPaths.run(file.id, json);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
return { added, cwd: null, ts, own: true };
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
export function postScan(db, archiveDir) {
|
|
378
|
+
const path = join(archiveDir, "summaries.json");
|
|
379
|
+
if (!existsSync(path)) return;
|
|
380
|
+
let meta;
|
|
381
|
+
try {
|
|
382
|
+
meta = JSON.parse(readFileSync(path, "utf8"));
|
|
383
|
+
} catch {
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const allFiles = db.prepare("SELECT id, kind, session_id, agent_id, cwd FROM files WHERE provider = 'agy'").all();
|
|
388
|
+
const byOwn = new Map();
|
|
389
|
+
for (const f of allFiles) byOwn.set(f.agent_id ?? f.session_id, f);
|
|
390
|
+
|
|
391
|
+
const rootOf = (id) => {
|
|
392
|
+
let cur = id;
|
|
393
|
+
const seen = new Set([cur]);
|
|
394
|
+
while (meta[cur]?.parentId) {
|
|
395
|
+
const next = meta[cur].parentId;
|
|
396
|
+
if (!next || seen.has(next) || !byOwn.has(next)) break;
|
|
397
|
+
seen.add(next);
|
|
398
|
+
cur = next;
|
|
399
|
+
}
|
|
400
|
+
return cur === id ? null : cur;
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
const setCwd = db.prepare("UPDATE files SET cwd = ? WHERE id = ?");
|
|
404
|
+
const setFileSub = db.prepare("UPDATE files SET kind = 'sub', session_id = ?, agent_id = ? WHERE id = ?");
|
|
405
|
+
const setReqSub = db.prepare("UPDATE requests SET session_id = ?, agent_id = ?, is_sub = 1 WHERE file_id = ?");
|
|
406
|
+
const setCallSub = db.prepare("UPDATE tool_calls SET session_id = ?, agent_id = ? WHERE file_id = ?");
|
|
407
|
+
const setSkillSess = db.prepare("UPDATE skills SET session_id = ? WHERE file_id = ?");
|
|
408
|
+
const setTitleSess = db.prepare("UPDATE titles SET session_id = ? WHERE file_id = ?");
|
|
409
|
+
const putSub = db.prepare("INSERT OR REPLACE INTO subagents(agent_id, session_id, type, tool_use_id) VALUES (?, ?, ?, NULL)");
|
|
410
|
+
const putTitle = db.prepare("INSERT OR REPLACE INTO titles(file_id, line, session_id, kind, title) VALUES (?, 0, ?, 'ai', ?)");
|
|
411
|
+
|
|
412
|
+
db.exec("BEGIN");
|
|
413
|
+
try {
|
|
414
|
+
for (const f of allFiles) {
|
|
415
|
+
const ownId = f.agent_id ?? f.session_id;
|
|
416
|
+
const m = meta[ownId];
|
|
417
|
+
if (!m) continue;
|
|
418
|
+
if (m.cwd && !f.cwd) setCwd.run(m.cwd, f.id);
|
|
419
|
+
const root = rootOf(ownId);
|
|
420
|
+
const sess = root ?? f.session_id;
|
|
421
|
+
if (root) {
|
|
422
|
+
setFileSub.run(root, ownId, f.id);
|
|
423
|
+
setReqSub.run(root, ownId, f.id);
|
|
424
|
+
setCallSub.run(root, ownId, f.id);
|
|
425
|
+
setSkillSess.run(root, f.id);
|
|
426
|
+
setTitleSess.run(root, f.id);
|
|
427
|
+
putSub.run(ownId, root, m.agentName || "subagent");
|
|
428
|
+
}
|
|
429
|
+
if (m.title) putTitle.run(f.id, sess, m.title);
|
|
430
|
+
}
|
|
431
|
+
db.exec("COMMIT");
|
|
432
|
+
} catch (err) {
|
|
433
|
+
db.exec("ROLLBACK");
|
|
434
|
+
throw err;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
export const agy = { name: "agy", label, defaultPath, describe, listHost, syncMeta, createIngester, postScan, familyOf, displayModel };
|