personal-eval 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +26 -2
- package/bin/personal-eval.js +10 -0
- package/fonts/GeistMono-Medium.ttf +0 -0
- package/fonts/GeistMono-Regular.ttf +0 -0
- package/fonts/OFL.txt +92 -0
- package/package.json +42 -4
- package/src/classify.js +90 -0
- package/src/cli.js +219 -0
- package/src/judge.js +177 -0
- package/src/lines.js +89 -0
- package/src/metrics.js +203 -0
- package/src/models.js +42 -0
- package/src/pipeline.js +93 -0
- package/src/readers/claude.js +138 -0
- package/src/readers/codex.js +246 -0
- package/src/readers/fs.js +30 -0
- package/src/readers/fx.js +171 -0
- package/src/readers/gemini.js +191 -0
- package/src/readers/index.js +13 -0
- package/src/readers/opencode.js +243 -0
- package/src/render.js +110 -0
- package/src/text.js +204 -0
- package/src/turns.js +157 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
// Gemini CLI: ~/.gemini/tmp/<project>/chats/session-*.jsonl (v0.39+) or .json (older).
|
|
2
|
+
//
|
|
3
|
+
// JSONL is an append-only log that is replayed: message records (the same id is
|
|
4
|
+
// re-appended in full on every update; last wins), {$set}, {$patch}, and
|
|
5
|
+
// {$rewindTo}. A cancelled turn is *rewound* out of history, so we keep rewound
|
|
6
|
+
// messages and treat the rewind as the user cancelling.
|
|
7
|
+
import fs from 'node:fs/promises';
|
|
8
|
+
import os from 'node:os';
|
|
9
|
+
import path from 'node:path';
|
|
10
|
+
import { readLines, tryJSON } from '../lines.js';
|
|
11
|
+
import { toolKind, classifyToolError, diffLines, toMs } from '../text.js';
|
|
12
|
+
|
|
13
|
+
export const id = 'gemini';
|
|
14
|
+
export const label = 'Gemini CLI';
|
|
15
|
+
|
|
16
|
+
export function roots() {
|
|
17
|
+
const home = process.env.GEMINI_CLI_HOME || os.homedir();
|
|
18
|
+
return [path.join(home, '.gemini', 'tmp'), path.join(os.homedir(), '.cache', '.gemini', 'tmp')];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function discover() {
|
|
22
|
+
const units = [];
|
|
23
|
+
for (const root of roots()) {
|
|
24
|
+
let projects = [];
|
|
25
|
+
try {
|
|
26
|
+
projects = await fs.readdir(root, { withFileTypes: true });
|
|
27
|
+
} catch {
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
for (const p of projects) {
|
|
31
|
+
if (!p.isDirectory()) continue;
|
|
32
|
+
const chats = path.join(root, p.name, 'chats');
|
|
33
|
+
let files = [];
|
|
34
|
+
try {
|
|
35
|
+
files = await fs.readdir(chats, { withFileTypes: true });
|
|
36
|
+
} catch {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
const names = new Set(files.filter((f) => f.isFile()).map((f) => f.name));
|
|
40
|
+
for (const name of names) {
|
|
41
|
+
if (!/^session-.*\.jsonl?$/.test(name)) continue;
|
|
42
|
+
// A resumed legacy session gets a .jsonl next to the old .json; read only the .jsonl.
|
|
43
|
+
if (name.endsWith('.json') && names.has(`${name}l`)) continue;
|
|
44
|
+
units.push(path.join(chats, name));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return units;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function partsText(content) {
|
|
52
|
+
if (typeof content === 'string') return content;
|
|
53
|
+
if (!Array.isArray(content)) return content?.text || '';
|
|
54
|
+
return content.map((p) => (typeof p === 'string' ? p : p?.text || '')).join('');
|
|
55
|
+
}
|
|
56
|
+
const hasFunctionResponse = (content) => Array.isArray(content) && content.some((p) => p && p.functionResponse);
|
|
57
|
+
|
|
58
|
+
const IGNORED_PREFIX = ['/', '?', '<session_context>', '<hook_context>'];
|
|
59
|
+
|
|
60
|
+
export async function parse(file) {
|
|
61
|
+
let meta = {};
|
|
62
|
+
const entries = []; // { rec, removed }
|
|
63
|
+
const byId = new Map();
|
|
64
|
+
|
|
65
|
+
const put = (rec) => {
|
|
66
|
+
const e = byId.get(rec.id);
|
|
67
|
+
if (e && !e.removed) e.rec = rec;
|
|
68
|
+
else {
|
|
69
|
+
const ne = { rec, removed: false };
|
|
70
|
+
entries.push(ne);
|
|
71
|
+
byId.set(rec.id, ne);
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
if (file.endsWith('.json')) {
|
|
76
|
+
const obj = tryJSON(await fs.readFile(file, 'utf8'));
|
|
77
|
+
if (!obj) return [];
|
|
78
|
+
meta = obj;
|
|
79
|
+
for (const m of obj.messages || []) if (m?.id) put(m);
|
|
80
|
+
} else {
|
|
81
|
+
for await (const line of readLines(file)) {
|
|
82
|
+
const d = tryJSON(line);
|
|
83
|
+
if (!d || typeof d !== 'object') continue;
|
|
84
|
+
if (d.$rewindTo) {
|
|
85
|
+
const e = byId.get(d.$rewindTo);
|
|
86
|
+
const idx = e ? entries.indexOf(e) : -1;
|
|
87
|
+
if (idx >= 0) for (let i = idx; i < entries.length; i++) markRemoved(entries[i], byId);
|
|
88
|
+
} else if (d.$set) {
|
|
89
|
+
if (Array.isArray(d.$set.messages)) {
|
|
90
|
+
entries.length = 0;
|
|
91
|
+
byId.clear();
|
|
92
|
+
for (const m of d.$set.messages) if (m?.id) put(m);
|
|
93
|
+
}
|
|
94
|
+
meta = { ...meta, ...d.$set, messages: undefined };
|
|
95
|
+
} else if (d.$patch) {
|
|
96
|
+
applyPatch(d.$patch, byId, entries);
|
|
97
|
+
} else if (d.id && d.type) {
|
|
98
|
+
put(d);
|
|
99
|
+
} else if (d.sessionId) {
|
|
100
|
+
meta = { ...meta, ...d };
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (meta.kind === 'subagent') return [];
|
|
105
|
+
|
|
106
|
+
const events = [];
|
|
107
|
+
let lastModel = null;
|
|
108
|
+
for (const { rec, removed } of entries) {
|
|
109
|
+
const t = toMs(rec.timestamp);
|
|
110
|
+
if (rec.type === 'user') {
|
|
111
|
+
if (hasFunctionResponse(rec.content)) continue;
|
|
112
|
+
const text = partsText(rec.displayContent ?? rec.content).trim();
|
|
113
|
+
if (!text || text === 'Please continue.' || IGNORED_PREFIX.some((p) => text.startsWith(p))) continue;
|
|
114
|
+
events.push({ type: 'prompt', t, text, key: `gemini:${meta.sessionId || file}:${rec.id}` });
|
|
115
|
+
if (removed) events.push({ type: 'interrupt', t });
|
|
116
|
+
} else if (rec.type === 'gemini') {
|
|
117
|
+
if (rec.model) lastModel = rec.model;
|
|
118
|
+
const model = rec.model || lastModel;
|
|
119
|
+
if (!model) continue;
|
|
120
|
+
const calls = Array.isArray(rec.toolCalls) ? rec.toolCalls : [];
|
|
121
|
+
events.push({ type: 'assistant', t, model, text: partsText(rec.content), tools: calls.length });
|
|
122
|
+
for (const c of calls) {
|
|
123
|
+
const ev = toolEvent(c, model, toMs(c.timestamp) ?? t);
|
|
124
|
+
if (ev) events.push(ev);
|
|
125
|
+
}
|
|
126
|
+
} else if (rec.type === 'info' && /^Request cancelled/.test(partsText(rec.content))) {
|
|
127
|
+
events.push({ type: 'interrupt', t });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return [{ agent: id, id: meta.sessionId || path.basename(file), cwd: null, file, events }];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function markRemoved(entry, byId) {
|
|
134
|
+
entry.removed = true;
|
|
135
|
+
if (byId.get(entry.rec.id) === entry) byId.delete(entry.rec.id);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function applyPatch(p, byId, entries) {
|
|
139
|
+
const updates = [...(p.updates || [])];
|
|
140
|
+
if (p.id) updates.push(p);
|
|
141
|
+
for (const u of updates) {
|
|
142
|
+
const e = byId.get(u.id);
|
|
143
|
+
if (!e) continue;
|
|
144
|
+
const rec = { ...e.rec };
|
|
145
|
+
if (u.content !== undefined) rec.content = u.content;
|
|
146
|
+
if (Array.isArray(u.toolCalls) && Array.isArray(rec.toolCalls)) {
|
|
147
|
+
const byCall = new Map(u.toolCalls.map((c) => [c.id, c]));
|
|
148
|
+
rec.toolCalls = rec.toolCalls.map((c) => (byCall.has(c.id) ? { ...c, ...byCall.get(c.id) } : c));
|
|
149
|
+
}
|
|
150
|
+
e.rec = rec;
|
|
151
|
+
}
|
|
152
|
+
for (const rid of p.removeIds || []) {
|
|
153
|
+
const e = byId.get(rid);
|
|
154
|
+
if (e) markRemoved(e, byId);
|
|
155
|
+
}
|
|
156
|
+
if (Array.isArray(p.orderIds)) {
|
|
157
|
+
const pos = new Map(p.orderIds.map((x, i) => [x, i]));
|
|
158
|
+
const live = entries.filter((e) => !e.removed && pos.has(e.rec.id));
|
|
159
|
+
live.sort((a, b) => pos.get(a.rec.id) - pos.get(b.rec.id));
|
|
160
|
+
let k = 0;
|
|
161
|
+
for (let i = 0; i < entries.length; i++) if (!entries[i].removed && pos.has(entries[i].rec.id)) entries[i] = live[k++];
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function responseOf(call) {
|
|
166
|
+
const r = Array.isArray(call.result) ? call.result.find((p) => p?.functionResponse) : null;
|
|
167
|
+
return r?.functionResponse?.response || {};
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function toolEvent(call, model, t) {
|
|
171
|
+
const kind = toolKind(call.name);
|
|
172
|
+
let status;
|
|
173
|
+
if (call.status === 'success') status = 'ok';
|
|
174
|
+
else if (call.status === 'error') status = classifyToolError(responseOf(call).error || call.resultDisplay);
|
|
175
|
+
else if (call.status === 'cancelled') status = 'rejected';
|
|
176
|
+
else return null; // still running / awaiting approval
|
|
177
|
+
if (status === 'ok' && kind === 'shell') {
|
|
178
|
+
const out = String(responseOf(call).output || '');
|
|
179
|
+
const m = out.match(/^Exit Code: (-?\d+)$/im);
|
|
180
|
+
if (m && Number(m[1]) !== 0) status = 'error';
|
|
181
|
+
}
|
|
182
|
+
const ev = { type: 'tool', t, model, name: call.name, kind, status };
|
|
183
|
+
if (kind === 'edit' && status === 'ok') {
|
|
184
|
+
const a = call.args || {};
|
|
185
|
+
const p = a.file_path || a.path;
|
|
186
|
+
if (p && call.name === 'write_file') ev.edits = [{ path: p, mode: 'write', removed: [], added: diffLines('', a.content).added }];
|
|
187
|
+
else if (p) ev.edits = [{ path: p, mode: 'patch', ...diffLines(a.old_string, a.new_string) }];
|
|
188
|
+
}
|
|
189
|
+
return ev;
|
|
190
|
+
}
|
|
191
|
+
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import * as claude from './claude.js';
|
|
2
|
+
import * as codex from './codex.js';
|
|
3
|
+
import * as gemini from './gemini.js';
|
|
4
|
+
import * as opencode from './opencode.js';
|
|
5
|
+
import * as fx from './fx.js';
|
|
6
|
+
|
|
7
|
+
export const READERS = { claude, codex, gemini, opencode, fx };
|
|
8
|
+
|
|
9
|
+
export function getReader(id) {
|
|
10
|
+
const r = READERS[id];
|
|
11
|
+
if (!r) throw new Error(`unknown agent "${id}"`);
|
|
12
|
+
return r;
|
|
13
|
+
}
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
// opencode: data dir $XDG_DATA_HOME/opencode or ~/.local/share/opencode.
|
|
2
|
+
// Three storage generations, all handled:
|
|
3
|
+
// A JSON files storage/{session,message,part}/… (≤ v1.1)
|
|
4
|
+
// B sqlite v1 opencode*.db: session / message / part (v1.2 – v1.19)
|
|
5
|
+
// C sqlite v2 opencode*.db: session_v2 / session_message / session_part (v2.x)
|
|
6
|
+
// v2 copies v1 rows forward and v1.2 copied the JSON files, so the same session
|
|
7
|
+
// can appear more than once; turns are de-duplicated by message id downstream.
|
|
8
|
+
import fs from 'node:fs/promises';
|
|
9
|
+
import os from 'node:os';
|
|
10
|
+
import path from 'node:path';
|
|
11
|
+
import { tryJSON } from '../lines.js';
|
|
12
|
+
import { toolKind, classifyToolError, applyPatchEdits, diffLines } from '../text.js';
|
|
13
|
+
|
|
14
|
+
export const id = 'opencode';
|
|
15
|
+
export const label = 'opencode';
|
|
16
|
+
|
|
17
|
+
export function dataDir() {
|
|
18
|
+
return path.join(process.env.XDG_DATA_HOME || path.join(os.homedir(), '.local', 'share'), 'opencode');
|
|
19
|
+
}
|
|
20
|
+
export function roots() {
|
|
21
|
+
return [dataDir()];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function discover() {
|
|
25
|
+
const dir = dataDir();
|
|
26
|
+
const units = [];
|
|
27
|
+
let entries = [];
|
|
28
|
+
try {
|
|
29
|
+
entries = await fs.readdir(dir);
|
|
30
|
+
} catch {
|
|
31
|
+
return units;
|
|
32
|
+
}
|
|
33
|
+
const custom = process.env.OPENCODE_DB && path.resolve(dir, process.env.OPENCODE_DB);
|
|
34
|
+
for (const e of entries) if (/^opencode.*\.db$/.test(e)) units.push(path.join(dir, e));
|
|
35
|
+
if (custom && !units.includes(custom)) units.push(custom);
|
|
36
|
+
// Generation A: one unit per session's message directory.
|
|
37
|
+
const msgRoot = path.join(dir, 'storage', 'message');
|
|
38
|
+
try {
|
|
39
|
+
for (const s of await fs.readdir(msgRoot)) units.push(path.join(msgRoot, s));
|
|
40
|
+
} catch {}
|
|
41
|
+
return units;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function parse(unit) {
|
|
45
|
+
if (unit.endsWith('.db')) return parseDb(unit);
|
|
46
|
+
return parseJsonSession(unit);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// ---------- sqlite ----------
|
|
50
|
+
|
|
51
|
+
async function openDb(file) {
|
|
52
|
+
let sqlite;
|
|
53
|
+
try {
|
|
54
|
+
process.removeAllListeners('warning'); // node:sqlite prints an ExperimentalWarning on some versions
|
|
55
|
+
sqlite = await import('node:sqlite');
|
|
56
|
+
} catch {
|
|
57
|
+
throw new Error('reading opencode needs Node 22.5+ (node:sqlite)');
|
|
58
|
+
}
|
|
59
|
+
return new sqlite.DatabaseSync(file, { readOnly: true });
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function parseDb(file) {
|
|
63
|
+
const db = await openDb(file);
|
|
64
|
+
try {
|
|
65
|
+
const tables = new Set(db.prepare("SELECT name FROM sqlite_master WHERE type='table'").all().map((r) => r.name));
|
|
66
|
+
const out = [];
|
|
67
|
+
if (tables.has('session_v2') && tables.has('session_message')) out.push(...parseV2(db, tables));
|
|
68
|
+
if (tables.has('session') && tables.has('message') && tables.has('part')) out.push(...parseV1(db));
|
|
69
|
+
return out.map((s) => ({ ...s, file }));
|
|
70
|
+
} finally {
|
|
71
|
+
db.close();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function parseV1(db) {
|
|
76
|
+
const sessions = db.prepare('SELECT id, directory FROM session WHERE parent_id IS NULL').all();
|
|
77
|
+
const msgStmt = db.prepare('SELECT id, time_created, data FROM message WHERE session_id = ? ORDER BY time_created, id');
|
|
78
|
+
const partStmt = db.prepare('SELECT message_id, data FROM part WHERE session_id = ? ORDER BY id');
|
|
79
|
+
const out = [];
|
|
80
|
+
for (const s of sessions) {
|
|
81
|
+
const parts = new Map();
|
|
82
|
+
for (const p of partStmt.all(s.id)) {
|
|
83
|
+
if (!parts.has(p.message_id)) parts.set(p.message_id, []);
|
|
84
|
+
parts.get(p.message_id).push(tryJSON(p.data) || {});
|
|
85
|
+
}
|
|
86
|
+
const messages = msgStmt.all(s.id).map((m) => ({ id: m.id, ...(tryJSON(m.data) || {}), _t: m.time_created }));
|
|
87
|
+
out.push({ agent: id, id: s.id, cwd: s.directory, events: v1Events(messages, parts) });
|
|
88
|
+
}
|
|
89
|
+
return out;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function parseV2(db, tables) {
|
|
93
|
+
const sessions = db.prepare('SELECT id, directory FROM session_v2 WHERE parent_id IS NULL').all();
|
|
94
|
+
const msgStmt = db.prepare('SELECT id, type, time_created, data FROM session_message WHERE session_id = ? ORDER BY seq');
|
|
95
|
+
const partStmt = tables.has('session_part')
|
|
96
|
+
? db.prepare('SELECT message_id, type, data FROM session_part WHERE session_id = ? ORDER BY message_id, ordinal')
|
|
97
|
+
: null;
|
|
98
|
+
const out = [];
|
|
99
|
+
for (const s of sessions) {
|
|
100
|
+
const parts = new Map();
|
|
101
|
+
if (partStmt) {
|
|
102
|
+
for (const p of partStmt.all(s.id)) {
|
|
103
|
+
if (!parts.has(p.message_id)) parts.set(p.message_id, []);
|
|
104
|
+
parts.get(p.message_id).push({ type: p.type, ...(tryJSON(p.data) || {}) });
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
const events = [];
|
|
108
|
+
for (const m of msgStmt.all(s.id)) {
|
|
109
|
+
const data = tryJSON(m.data) || {};
|
|
110
|
+
const t = data.time?.created ?? m.time_created;
|
|
111
|
+
if (m.type === 'user') {
|
|
112
|
+
const text = String(data.text || '').trim();
|
|
113
|
+
if (text) events.push({ type: 'prompt', t, text, key: `opencode:${m.id}` });
|
|
114
|
+
} else if (m.type === 'idle') {
|
|
115
|
+
if (data.outcome === 'interrupted') events.push({ type: 'interrupt', t });
|
|
116
|
+
} else if (m.type === 'assistant') {
|
|
117
|
+
const model = data.model?.id || null;
|
|
118
|
+
if (!model) continue;
|
|
119
|
+
const content = [...(parts.get(m.id) || []), ...(Array.isArray(data.content) ? data.content : [])];
|
|
120
|
+
const text = content.filter((c) => c.type === 'text').map((c) => c.text || '').join('');
|
|
121
|
+
const tools = content.filter((c) => c.type === 'tool');
|
|
122
|
+
const tEnd = data.time?.completed ?? t;
|
|
123
|
+
events.push({ type: 'assistant', t: tEnd, model, text, tools: tools.length });
|
|
124
|
+
for (const c of tools) {
|
|
125
|
+
const ev = v2Tool(c, model, tEnd);
|
|
126
|
+
if (ev) events.push(ev);
|
|
127
|
+
}
|
|
128
|
+
if (data.error?.type === 'aborted') events.push({ type: 'interrupt', t: tEnd });
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
out.push({ agent: id, id: s.id, cwd: s.directory, events });
|
|
132
|
+
}
|
|
133
|
+
return out;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function v2Tool(c, model, t) {
|
|
137
|
+
const st = c.state || {};
|
|
138
|
+
const kind = toolKind(c.name);
|
|
139
|
+
let status;
|
|
140
|
+
if (st.status === 'completed') status = 'ok';
|
|
141
|
+
else if (st.status === 'error') {
|
|
142
|
+
const type = st.error?.type || '';
|
|
143
|
+
const msg = st.error?.message || '';
|
|
144
|
+
if (type === 'tool.input-json' || type === 'tool.input-incomplete') status = 'malformed';
|
|
145
|
+
else if (type === 'aborted') {
|
|
146
|
+
if (/declined/i.test(msg)) status = 'rejected';
|
|
147
|
+
else return null; // interrupted mid-run; the turn is marked cancelled instead
|
|
148
|
+
} else status = classifyToolError(msg) === 'malformed' ? 'malformed' : 'error';
|
|
149
|
+
} else return null;
|
|
150
|
+
if (status === 'ok' && kind === 'shell' && typeof st.metadata?.exit === 'number' && st.metadata.exit !== 0) status = 'error';
|
|
151
|
+
const ev = { type: 'tool', t, model, name: c.name, kind, status };
|
|
152
|
+
if (status === 'ok' && kind === 'edit') ev.edits = editsFor(c.name, st.input);
|
|
153
|
+
return ev;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// ---------- generation A + v1 share message/part shapes ----------
|
|
157
|
+
|
|
158
|
+
function v1Events(messages, parts) {
|
|
159
|
+
const events = [];
|
|
160
|
+
for (const m of messages) {
|
|
161
|
+
const ps = parts.get(m.id) || [];
|
|
162
|
+
const t = m.time?.created ?? m._t;
|
|
163
|
+
if (m.role === 'user') {
|
|
164
|
+
const text = ps.filter((p) => p.type === 'text' && !p.synthetic && !p.ignored).map((p) => p.text || '').join('\n').trim();
|
|
165
|
+
if (text) events.push({ type: 'prompt', t, text, key: `opencode:${m.id}` });
|
|
166
|
+
} else if (m.role === 'assistant') {
|
|
167
|
+
const model = m.modelID || null;
|
|
168
|
+
if (!model) continue;
|
|
169
|
+
const tEnd = m.time?.completed ?? t;
|
|
170
|
+
const text = ps.filter((p) => p.type === 'text' && !p.synthetic).map((p) => p.text || '').join('');
|
|
171
|
+
const tools = ps.filter((p) => p.type === 'tool');
|
|
172
|
+
events.push({ type: 'assistant', t: tEnd, model, text, tools: tools.length });
|
|
173
|
+
for (const p of tools) {
|
|
174
|
+
const ev = v1Tool(p, model, tEnd);
|
|
175
|
+
if (ev) events.push(ev);
|
|
176
|
+
}
|
|
177
|
+
if (m.error?.name === 'MessageAbortedError') events.push({ type: 'interrupt', t: tEnd });
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return events;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function v1Tool(p, model, t) {
|
|
184
|
+
const st = p.state || {};
|
|
185
|
+
if (p.tool === 'invalid') return { type: 'tool', t, model, name: 'invalid', kind: 'other', status: 'malformed' };
|
|
186
|
+
const kind = toolKind(p.tool);
|
|
187
|
+
let status;
|
|
188
|
+
if (st.status === 'completed') status = 'ok';
|
|
189
|
+
else if (st.status === 'error') {
|
|
190
|
+
if (/Tool execution aborted/i.test(st.error || '') || st.metadata?.interrupted) return null;
|
|
191
|
+
status = classifyToolError(st.error);
|
|
192
|
+
} else return null;
|
|
193
|
+
if (status === 'ok' && kind === 'shell' && typeof st.metadata?.exit === 'number' && st.metadata.exit !== 0) status = 'error';
|
|
194
|
+
const ev = { type: 'tool', t, model, name: p.tool, kind, status };
|
|
195
|
+
if (status === 'ok' && kind === 'edit') ev.edits = editsFor(p.tool, st.input);
|
|
196
|
+
return ev;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function editsFor(name, input = {}) {
|
|
200
|
+
const n = String(name).toLowerCase();
|
|
201
|
+
const file = input.filePath || input.path;
|
|
202
|
+
if (n === 'apply_patch' || n === 'patch') return applyPatchEdits(input.patchText || input.patch || '');
|
|
203
|
+
if (n === 'write' && file) return [{ path: file, mode: 'write', removed: [], added: diffLines('', input.content).added }];
|
|
204
|
+
if (n === 'multiedit') return (input.edits || []).map((e) => ({ path: e.filePath || file, mode: 'patch', ...diffLines(e.oldString, e.newString) }));
|
|
205
|
+
if (n === 'edit' && file) return [{ path: file, mode: 'patch', ...diffLines(input.oldString, input.newString) }];
|
|
206
|
+
return [];
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
async function parseJsonSession(msgDir) {
|
|
210
|
+
const sessionId = path.basename(msgDir);
|
|
211
|
+
const storage = path.dirname(path.dirname(msgDir));
|
|
212
|
+
// Skip sub-agent sessions: their info file has a parentID.
|
|
213
|
+
const info = await findSessionInfo(storage, sessionId);
|
|
214
|
+
if (info?.parentID) return [];
|
|
215
|
+
const messages = [];
|
|
216
|
+
for (const f of await fs.readdir(msgDir).catch(() => [])) {
|
|
217
|
+
if (!f.endsWith('.json')) continue;
|
|
218
|
+
const m = tryJSON(await fs.readFile(path.join(msgDir, f), 'utf8'));
|
|
219
|
+
if (m?.id) messages.push(m);
|
|
220
|
+
}
|
|
221
|
+
messages.sort((a, b) => (a.time?.created ?? 0) - (b.time?.created ?? 0) || String(a.id).localeCompare(String(b.id)));
|
|
222
|
+
const parts = new Map();
|
|
223
|
+
for (const m of messages) {
|
|
224
|
+
const dir = path.join(storage, 'part', m.id);
|
|
225
|
+
const list = [];
|
|
226
|
+
for (const f of (await fs.readdir(dir).catch(() => [])).sort()) {
|
|
227
|
+
const p = tryJSON(await fs.readFile(path.join(dir, f), 'utf8'));
|
|
228
|
+
if (p) list.push(p);
|
|
229
|
+
}
|
|
230
|
+
parts.set(m.id, list);
|
|
231
|
+
}
|
|
232
|
+
return [{ agent: id, id: sessionId, cwd: info?.directory || null, file: msgDir, events: v1Events(messages, parts) }];
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
async function findSessionInfo(storage, sessionId) {
|
|
236
|
+
const root = path.join(storage, 'session');
|
|
237
|
+
for (const project of await fs.readdir(root).catch(() => [])) {
|
|
238
|
+
const f = path.join(root, project, `${sessionId}.json`);
|
|
239
|
+
const info = tryJSON(await fs.readFile(f, 'utf8').catch(() => 'null'));
|
|
240
|
+
if (info) return info;
|
|
241
|
+
}
|
|
242
|
+
return null;
|
|
243
|
+
}
|
package/src/render.js
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// Render the scorecard as a terminal table and as a PNG that mirrors the
|
|
2
|
+
// black, monospace, hairline-grid look of the original screenshot.
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import { createRequire } from 'node:module';
|
|
7
|
+
|
|
8
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
9
|
+
const FONT_DIR = path.join(here, '..', 'fonts');
|
|
10
|
+
|
|
11
|
+
function tableRows(card) {
|
|
12
|
+
const header = ['Dimension', ...card.columns, ...(card.single ? [] : ['Edge'])];
|
|
13
|
+
const body = card.rows
|
|
14
|
+
.filter((r) => !(card.single && r.overall))
|
|
15
|
+
.map((r) => [r.label, ...r.cells, ...(card.single ? [] : [r.edge])]);
|
|
16
|
+
return { header, body };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const width = (s) => [...s].length;
|
|
20
|
+
|
|
21
|
+
export function renderTerminal(card, { color = false } = {}) {
|
|
22
|
+
const { header, body } = tableRows(card);
|
|
23
|
+
const all = [header, ...body];
|
|
24
|
+
const w = header.map((_, c) => Math.max(...all.map((r) => width(r[c]))));
|
|
25
|
+
const bold = (s) => (color ? `\x1b[1m${s}\x1b[22m` : s);
|
|
26
|
+
const dim = (s) => (color ? `\x1b[2m${s}\x1b[22m` : s);
|
|
27
|
+
const line = (l, m, r) => dim(l + w.map((x) => '─'.repeat(x + 2)).join(m) + r);
|
|
28
|
+
const row = (cells, fmt = (s) => s) =>
|
|
29
|
+
dim('│') + cells.map((c, i) => ` ${fmt(c)}${' '.repeat(w[i] - width(c))} `).join(dim('│')) + dim('│');
|
|
30
|
+
const out = [line('┌', '┬', '┐'), row(header, bold)];
|
|
31
|
+
for (const r of body) {
|
|
32
|
+
out.push(line('├', '┼', '┤'));
|
|
33
|
+
out.push(row(r));
|
|
34
|
+
}
|
|
35
|
+
out.push(line('└', '┴', '┘'));
|
|
36
|
+
return out.join('\n');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const esc = (s) => String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
40
|
+
|
|
41
|
+
export function renderSVG(card, { footer = 'npx personal-eval' } = {}) {
|
|
42
|
+
const { header, body } = tableRows(card);
|
|
43
|
+
const FS = 26; // font size
|
|
44
|
+
const CW = FS * 0.6; // Geist Mono advance width
|
|
45
|
+
const PADX = 24;
|
|
46
|
+
const ROWH = 68;
|
|
47
|
+
const MX = 96;
|
|
48
|
+
const MT = 62;
|
|
49
|
+
const MB = footer ? 96 : 75;
|
|
50
|
+
const all = [header, ...body];
|
|
51
|
+
const colW = header.map((_, c) => Math.ceil(Math.max(...all.map((r) => width(r[c]))) * CW + PADX * 2));
|
|
52
|
+
const tableW = colW.reduce((a, b) => a + b, 0);
|
|
53
|
+
const tableH = ROWH * all.length;
|
|
54
|
+
const W = Math.round(tableW + MX * 2);
|
|
55
|
+
const H = Math.round(tableH + MT + MB);
|
|
56
|
+
const xs = [MX];
|
|
57
|
+
for (const cw of colW) xs.push(xs[xs.length - 1] + cw);
|
|
58
|
+
|
|
59
|
+
const parts = [];
|
|
60
|
+
parts.push(`<svg xmlns="http://www.w3.org/2000/svg" width="${W}" height="${H}" viewBox="0 0 ${W} ${H}">`);
|
|
61
|
+
parts.push(`<rect width="100%" height="100%" fill="#000"/>`);
|
|
62
|
+
const stroke = 'stroke="#d4d4d4" stroke-width="1.5" shape-rendering="crispEdges"';
|
|
63
|
+
parts.push(`<rect x="${MX}" y="${MT}" width="${tableW}" height="${tableH}" fill="none" ${stroke}/>`);
|
|
64
|
+
for (let r = 1; r < all.length; r++) {
|
|
65
|
+
const y = MT + r * ROWH;
|
|
66
|
+
parts.push(`<line x1="${MX}" y1="${y}" x2="${MX + tableW}" y2="${y}" ${stroke}/>`);
|
|
67
|
+
}
|
|
68
|
+
for (let c = 1; c < colW.length; c++) {
|
|
69
|
+
parts.push(`<line x1="${xs[c]}" y1="${MT}" x2="${xs[c]}" y2="${MT + tableH}" ${stroke}/>`);
|
|
70
|
+
}
|
|
71
|
+
all.forEach((cells, r) => {
|
|
72
|
+
const y = MT + r * ROWH + ROWH / 2 + FS * 0.36;
|
|
73
|
+
const weight = r === 0 ? 500 : 400;
|
|
74
|
+
cells.forEach((text, c) => {
|
|
75
|
+
parts.push(
|
|
76
|
+
`<text x="${xs[c] + PADX}" y="${y}" font-family="Geist Mono" font-size="${FS}" font-weight="${weight}" fill="#fff" xml:space="preserve">${esc(text)}</text>`,
|
|
77
|
+
);
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
if (footer) {
|
|
81
|
+
parts.push(
|
|
82
|
+
`<text x="${MX + tableW}" y="${MT + tableH + 52}" text-anchor="end" font-family="Geist Mono" font-size="18" fill="#6b6b6b">${esc(footer)}</text>`,
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
parts.push('</svg>');
|
|
86
|
+
return parts.join('\n');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Write a PNG (via resvg) or fall back to SVG. Returns the path written. */
|
|
90
|
+
export function writeImage(card, outPath, opts = {}) {
|
|
91
|
+
const svg = renderSVG(card, opts);
|
|
92
|
+
let Resvg;
|
|
93
|
+
try {
|
|
94
|
+
Resvg = createRequire(import.meta.url)('@resvg/resvg-js').Resvg;
|
|
95
|
+
} catch {
|
|
96
|
+
Resvg = null;
|
|
97
|
+
}
|
|
98
|
+
if (Resvg && !outPath.endsWith('.svg')) {
|
|
99
|
+
const fontFiles = ['GeistMono-Regular.ttf', 'GeistMono-Medium.ttf'].map((f) => path.join(FONT_DIR, f));
|
|
100
|
+
const png = new Resvg(svg, {
|
|
101
|
+
font: { fontFiles, loadSystemFonts: false, defaultFontFamily: 'Geist Mono' },
|
|
102
|
+
fitTo: { mode: 'zoom', value: opts.scale || 1 },
|
|
103
|
+
}).render().asPng();
|
|
104
|
+
fs.writeFileSync(outPath, png);
|
|
105
|
+
return outPath;
|
|
106
|
+
}
|
|
107
|
+
const svgPath = outPath.replace(/\.png$/i, '') + (outPath.endsWith('.svg') ? '' : '.svg');
|
|
108
|
+
fs.writeFileSync(svgPath, svg);
|
|
109
|
+
return svgPath;
|
|
110
|
+
}
|