@hype/clod 1.3.1 → 1.4.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 +27 -11
- package/dist/git.js +170 -0
- package/dist/harnesses/claude.js +445 -0
- package/dist/harnesses/index.js +27 -0
- package/dist/harnesses/omp.js +477 -0
- package/dist/index.js +7 -3
- package/dist/list.js +37 -17
- package/dist/proc.js +51 -0
- package/dist/sessions.js +5 -352
- package/dist/ui.js +91 -39
- package/package.json +8 -6
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
import { appendFile, chmod, mkdir, readdir, readFile, stat, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { basename, dirname, join } from 'node:path';
|
|
4
|
+
import { processStartedAt } from '../proc.js';
|
|
5
|
+
import { modelKey, throughputOf } from '../sessions.js';
|
|
6
|
+
import { trashPaths } from '../trash.js';
|
|
7
|
+
// Resolved lazily so tests can point $HOME at a fixture tree; os.homedir()
|
|
8
|
+
// honors $HOME on posix, read afresh each call.
|
|
9
|
+
const claudeDir = () => join(homedir(), '.claude');
|
|
10
|
+
const projectsDir = () => join(claudeDir(), 'projects');
|
|
11
|
+
// Claude Code's per-session task output and scratchpads, outside ~/.claude.
|
|
12
|
+
// getuid exists on every platform clod runs on (macOS, Linux, WSL).
|
|
13
|
+
const claudeTmpDir = () => join(process.env.CLAUDE_CODE_TMPDIR || '/tmp', `claude-${process.getuid?.()}`);
|
|
14
|
+
// Claude Code injects system content as XML-tagged blocks; any message starting
|
|
15
|
+
// with an opening tag is injected content, not a real user message
|
|
16
|
+
const INJECTED = /^<[a-z]/i;
|
|
17
|
+
// Claude Code writes "[Request interrupted by user]" and the like as user records
|
|
18
|
+
const NOTICE = /^\[[^\]]*\]$/;
|
|
19
|
+
// A slash command arrives as one injected block, with what the user typed inside
|
|
20
|
+
// <command-args>; without this the whole prompt reads as system content.
|
|
21
|
+
const COMMAND = /<command-name>([^<]*)<\/command-name>(?:\s*<command-args>([\s\S]*?)<\/command-args>)?/;
|
|
22
|
+
function rawText(content) {
|
|
23
|
+
if (typeof content === 'string')
|
|
24
|
+
return content;
|
|
25
|
+
if (Array.isArray(content))
|
|
26
|
+
return content.find((b) => b.type === 'text')?.text ?? '';
|
|
27
|
+
return '';
|
|
28
|
+
}
|
|
29
|
+
function userText(record) {
|
|
30
|
+
if (record.type !== 'user' || record.isMeta)
|
|
31
|
+
return null;
|
|
32
|
+
const content = record.message?.content;
|
|
33
|
+
if (hasRealText(content))
|
|
34
|
+
return extractText(content);
|
|
35
|
+
const m = COMMAND.exec(rawText(content));
|
|
36
|
+
if (!m)
|
|
37
|
+
return null;
|
|
38
|
+
const args = m[2]?.trim();
|
|
39
|
+
return args ? `${m[1].trim()} ${args}` : m[1].trim();
|
|
40
|
+
}
|
|
41
|
+
function hasRealText(content) {
|
|
42
|
+
if (typeof content === 'string')
|
|
43
|
+
return !INJECTED.test(content);
|
|
44
|
+
if (Array.isArray(content))
|
|
45
|
+
return content.some((b) => b.type === 'text' && !INJECTED.test(b.text ?? ''));
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
export function extractText(content) {
|
|
49
|
+
if (typeof content === 'string')
|
|
50
|
+
return content;
|
|
51
|
+
if (Array.isArray(content)) {
|
|
52
|
+
return content.find((b) => b.type === 'text' && !INJECTED.test(b.text ?? ''))?.text ?? '';
|
|
53
|
+
}
|
|
54
|
+
return '';
|
|
55
|
+
}
|
|
56
|
+
// All text blocks of a message, joined; used to build the searchable content blob.
|
|
57
|
+
export function textBlocks(content) {
|
|
58
|
+
if (typeof content === 'string')
|
|
59
|
+
return content;
|
|
60
|
+
if (Array.isArray(content))
|
|
61
|
+
return content
|
|
62
|
+
.filter((b) => b.type === 'text')
|
|
63
|
+
.map((b) => b.text ?? '')
|
|
64
|
+
.join(' ');
|
|
65
|
+
return '';
|
|
66
|
+
}
|
|
67
|
+
// Fold one assistant turn's usage into the per-model accumulator. cacheRead is
|
|
68
|
+
// tracked apart: it re-counts the cached context every turn and dwarfs the rest
|
|
69
|
+
// (~10x). Returns this turn's context size (input + cache), or null if no usage.
|
|
70
|
+
function accumulateUsage(message, acc) {
|
|
71
|
+
const u = message?.usage;
|
|
72
|
+
if (!u)
|
|
73
|
+
return null;
|
|
74
|
+
const input = u.input_tokens || 0;
|
|
75
|
+
const output = u.output_tokens || 0;
|
|
76
|
+
const cacheRead = u.cache_read_input_tokens || 0;
|
|
77
|
+
const cacheWrite = u.cache_creation_input_tokens || 0;
|
|
78
|
+
const key = modelKey(message?.model);
|
|
79
|
+
if (key) {
|
|
80
|
+
acc[key] ??= { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 };
|
|
81
|
+
const m = acc[key];
|
|
82
|
+
m.input += input;
|
|
83
|
+
m.output += output;
|
|
84
|
+
m.cacheRead += cacheRead;
|
|
85
|
+
m.cacheWrite += cacheWrite;
|
|
86
|
+
}
|
|
87
|
+
return input + cacheRead + cacheWrite;
|
|
88
|
+
}
|
|
89
|
+
// last capture of re in str; re must have the g flag
|
|
90
|
+
function matchLast(re, str) {
|
|
91
|
+
let last = null;
|
|
92
|
+
for (const m of str.matchAll(re))
|
|
93
|
+
last = m[1]; // single capture group, always present on a match
|
|
94
|
+
return last;
|
|
95
|
+
}
|
|
96
|
+
export async function parseSession(filePath, fileStats) {
|
|
97
|
+
const sessionId = basename(filePath, '.jsonl');
|
|
98
|
+
try {
|
|
99
|
+
// Read as a Buffer and decode per line only as far as the sniff needs; skipped
|
|
100
|
+
// lines (most of the bytes) are never decoded, which is the big startup cost.
|
|
101
|
+
const buf = await readFile(filePath);
|
|
102
|
+
let title = null;
|
|
103
|
+
let aiTitle = null;
|
|
104
|
+
let cwd = null;
|
|
105
|
+
let gitBranch = null;
|
|
106
|
+
let firstUserMessage = null;
|
|
107
|
+
let lastUserMessage = null;
|
|
108
|
+
let lastTimestamp = null;
|
|
109
|
+
let contextTokens = null; // input context of the most recent assistant turn
|
|
110
|
+
const models = {};
|
|
111
|
+
const searchParts = []; // prose for content search, lowercased at the end
|
|
112
|
+
const setEnvelope = (ts, branch, dir) => {
|
|
113
|
+
if (ts)
|
|
114
|
+
lastTimestamp = ts;
|
|
115
|
+
if (branch && branch !== 'HEAD')
|
|
116
|
+
gitBranch = branch; // last-write: captures branch switches
|
|
117
|
+
if (dir && !cwd)
|
|
118
|
+
cwd = dir; // first-write: cwd doesn't change mid-session
|
|
119
|
+
};
|
|
120
|
+
// Most bytes are tool results and attachments clod never shows. Sniff each line's
|
|
121
|
+
// head to decide whether to JSON.parse it; the sniff only gates parsing, so a
|
|
122
|
+
// false positive costs one wasted parse, never wrong data. Assistant records
|
|
123
|
+
// serialize "message" before "type", so their early marker is the message "role";
|
|
124
|
+
// skipped lines get their envelope (timestamp/branch/cwd) from a tail regex.
|
|
125
|
+
const NL = 10;
|
|
126
|
+
for (let pos = 0, lineEnd = 0; pos < buf.length; pos = lineEnd + 1) {
|
|
127
|
+
lineEnd = buf.indexOf(NL, pos);
|
|
128
|
+
if (lineEnd === -1)
|
|
129
|
+
lineEnd = buf.length;
|
|
130
|
+
if (lineEnd === pos)
|
|
131
|
+
continue;
|
|
132
|
+
const head = buf.toString('utf8', pos, Math.min(pos + 600, lineEnd));
|
|
133
|
+
const isAssistant = head.includes('"role":"assistant"') || head.includes('"type":"assistant"');
|
|
134
|
+
const isUser = !isAssistant && (head.includes('"type":"user"') || head.includes('"role":"user"'));
|
|
135
|
+
const isProse = isAssistant ||
|
|
136
|
+
(isUser &&
|
|
137
|
+
!head.includes('"isMeta":true') &&
|
|
138
|
+
!head.includes('"tool_use_id"') &&
|
|
139
|
+
!head.includes('"type":"tool_result"'));
|
|
140
|
+
if (isProse) {
|
|
141
|
+
try {
|
|
142
|
+
const r = JSON.parse(buf.toString('utf8', pos, lineEnd));
|
|
143
|
+
setEnvelope(r.timestamp, r.gitBranch, r.cwd);
|
|
144
|
+
if (r.type === 'assistant') {
|
|
145
|
+
const ctx = accumulateUsage(r.message, models); // usage on every turn, incl. tool-only
|
|
146
|
+
if (ctx != null)
|
|
147
|
+
contextTokens = ctx;
|
|
148
|
+
searchParts.push(textBlocks(r.message?.content));
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
const text = userText(r);
|
|
152
|
+
if (text !== null) {
|
|
153
|
+
lastUserMessage = text;
|
|
154
|
+
searchParts.push(text);
|
|
155
|
+
const trimmed = text.trim();
|
|
156
|
+
if (!firstUserMessage && trimmed && !NOTICE.test(trimmed))
|
|
157
|
+
firstUserMessage = trimmed;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
catch { }
|
|
162
|
+
}
|
|
163
|
+
else if (isUser ||
|
|
164
|
+
head.includes('"type":"system"') ||
|
|
165
|
+
/^\{"type":"(attachment|pr-link|queue-operation)"/.test(head)) {
|
|
166
|
+
const tail = buf.toString('utf8', Math.max(pos, lineEnd - 400), lineEnd);
|
|
167
|
+
setEnvelope(matchLast(/"timestamp":"([^"]+)"/g, tail), matchLast(/"gitBranch":"([^"]*)"/g, tail), matchLast(/"cwd":"([^"]*)"/g, tail));
|
|
168
|
+
}
|
|
169
|
+
else if (head.includes('"type":"custom-title"')) {
|
|
170
|
+
try {
|
|
171
|
+
const r = JSON.parse(buf.toString('utf8', pos, lineEnd));
|
|
172
|
+
if (r.customTitle)
|
|
173
|
+
title = r.customTitle;
|
|
174
|
+
}
|
|
175
|
+
catch { }
|
|
176
|
+
}
|
|
177
|
+
else if (head.startsWith('{"type":"ai-title"')) {
|
|
178
|
+
try {
|
|
179
|
+
const r = JSON.parse(buf.toString('utf8', pos, lineEnd));
|
|
180
|
+
if (r.aiTitle)
|
|
181
|
+
aiTitle = r.aiTitle;
|
|
182
|
+
}
|
|
183
|
+
catch { }
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
if (!cwd)
|
|
187
|
+
return null;
|
|
188
|
+
const totals = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 };
|
|
189
|
+
for (const k in models) {
|
|
190
|
+
totals.input += models[k].input;
|
|
191
|
+
totals.output += models[k].output;
|
|
192
|
+
totals.cacheRead += models[k].cacheRead;
|
|
193
|
+
totals.cacheWrite += models[k].cacheWrite;
|
|
194
|
+
}
|
|
195
|
+
const throughput = throughputOf(totals);
|
|
196
|
+
return {
|
|
197
|
+
harness: 'claude',
|
|
198
|
+
sessionId,
|
|
199
|
+
title,
|
|
200
|
+
aiTitle,
|
|
201
|
+
cwd,
|
|
202
|
+
gitBranch,
|
|
203
|
+
firstUserMessage,
|
|
204
|
+
lastUserMessage,
|
|
205
|
+
contextTokens,
|
|
206
|
+
models,
|
|
207
|
+
totals,
|
|
208
|
+
throughput,
|
|
209
|
+
searchText: searchParts.join(' ').toLowerCase(),
|
|
210
|
+
mtime: fileStats.mtimeMs,
|
|
211
|
+
lastTimestamp: lastTimestamp ? new Date(lastTimestamp).getTime() : fileStats.mtimeMs,
|
|
212
|
+
filePath
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
catch {
|
|
216
|
+
return null;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
// All session files with stats, newest first. mtime proxies the last record's
|
|
220
|
+
// timestamp (files are append-only), so the head is what a recency sort needs first.
|
|
221
|
+
export async function listSessionFiles() {
|
|
222
|
+
let projects;
|
|
223
|
+
try {
|
|
224
|
+
projects = await readdir(projectsDir());
|
|
225
|
+
}
|
|
226
|
+
catch {
|
|
227
|
+
return [];
|
|
228
|
+
}
|
|
229
|
+
const files = (await Promise.all(projects.map(async (project) => {
|
|
230
|
+
const projectPath = join(projectsDir(), project);
|
|
231
|
+
try {
|
|
232
|
+
const names = await readdir(projectPath);
|
|
233
|
+
return names.filter((f) => f.endsWith('.jsonl')).map((f) => join(projectPath, f));
|
|
234
|
+
}
|
|
235
|
+
catch {
|
|
236
|
+
return [];
|
|
237
|
+
}
|
|
238
|
+
}))).flat();
|
|
239
|
+
const stats = await Promise.all(files.map(async (filePath) => {
|
|
240
|
+
try {
|
|
241
|
+
return { harness: 'claude', filePath, stats: await stat(filePath) };
|
|
242
|
+
}
|
|
243
|
+
catch {
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
}));
|
|
247
|
+
return stats
|
|
248
|
+
.filter((s) => s !== null)
|
|
249
|
+
.sort((a, b) => b.stats.mtimeMs - a.stats.mtimeMs);
|
|
250
|
+
}
|
|
251
|
+
export async function getSessionMessages(filePath) {
|
|
252
|
+
try {
|
|
253
|
+
const raw = await readFile(filePath, 'utf8');
|
|
254
|
+
const messages = [];
|
|
255
|
+
for (const line of raw.split('\n').filter(Boolean)) {
|
|
256
|
+
try {
|
|
257
|
+
const r = JSON.parse(line);
|
|
258
|
+
const text = userText(r);
|
|
259
|
+
if (text !== null) {
|
|
260
|
+
messages.push({ role: 'user', text, timestamp: r.timestamp });
|
|
261
|
+
}
|
|
262
|
+
else if (r.type === 'assistant') {
|
|
263
|
+
const content = r.message?.content;
|
|
264
|
+
let text = '';
|
|
265
|
+
if (Array.isArray(content))
|
|
266
|
+
text = content
|
|
267
|
+
.filter((b) => b.type === 'text')
|
|
268
|
+
.map((b) => b.text ?? '')
|
|
269
|
+
.join('');
|
|
270
|
+
else if (typeof content === 'string')
|
|
271
|
+
text = content;
|
|
272
|
+
if (text.trim())
|
|
273
|
+
messages.push({ role: 'assistant', text: text.trim(), timestamp: r.timestamp });
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
catch { }
|
|
277
|
+
}
|
|
278
|
+
return messages;
|
|
279
|
+
}
|
|
280
|
+
catch {
|
|
281
|
+
return [];
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
// Claude Code writes sessions/<pid>.json at startup and rewrites its sessionId on
|
|
285
|
+
// /clear, /resume, and forks. A crash leaves the file behind, so the pid has to be
|
|
286
|
+
// alive and started when the file says: procStart is ps's lstart, in UTC.
|
|
287
|
+
export async function liveSessionIds(startedAt = processStartedAt) {
|
|
288
|
+
const live = new Set();
|
|
289
|
+
const dir = join(claudeDir(), 'sessions');
|
|
290
|
+
let names;
|
|
291
|
+
try {
|
|
292
|
+
names = await readdir(dir);
|
|
293
|
+
}
|
|
294
|
+
catch {
|
|
295
|
+
return live;
|
|
296
|
+
}
|
|
297
|
+
for (const name of names) {
|
|
298
|
+
if (!/^\d+\.json$/.test(name))
|
|
299
|
+
continue;
|
|
300
|
+
let r;
|
|
301
|
+
try {
|
|
302
|
+
r = JSON.parse(await readFile(join(dir, name), 'utf8'));
|
|
303
|
+
}
|
|
304
|
+
catch {
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
if (!r.sessionId || !r.pid)
|
|
308
|
+
continue;
|
|
309
|
+
try {
|
|
310
|
+
process.kill(r.pid, 0);
|
|
311
|
+
}
|
|
312
|
+
catch (err) {
|
|
313
|
+
if (err.code !== 'EPERM')
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
const started = r.procStart ? await startedAt(r.pid) : null;
|
|
317
|
+
// unreadable start time: assume the session is open rather than risk a write
|
|
318
|
+
if (!r.procStart ||
|
|
319
|
+
started === null ||
|
|
320
|
+
started.split(/\s+/).join(' ') === r.procStart.split(/\s+/).join(' '))
|
|
321
|
+
live.add(r.sessionId);
|
|
322
|
+
}
|
|
323
|
+
return live;
|
|
324
|
+
}
|
|
325
|
+
export async function isSessionLive(sessionId, startedAt = processStartedAt) {
|
|
326
|
+
return (await liveSessionIds(startedAt)).has(sessionId);
|
|
327
|
+
}
|
|
328
|
+
const LIVE = 'session is open in Claude Code';
|
|
329
|
+
// Rename by appending the records Claude Code's /rename writes; the last one
|
|
330
|
+
// wins on parse. custom-title feeds the resume picker, agent-name the title
|
|
331
|
+
// shown inside a resumed session. The picker falls back to custom-title.json
|
|
332
|
+
// when no custom-title sits in the transcript's tail, so it has to match too.
|
|
333
|
+
// An open session would write its own title back over this one.
|
|
334
|
+
export async function renameSession(filePath, title, isLive = isSessionLive) {
|
|
335
|
+
const sessionId = basename(filePath, '.jsonl');
|
|
336
|
+
if (await isLive(sessionId))
|
|
337
|
+
throw new Error(LIVE);
|
|
338
|
+
// the title file first: if the append then fails, the transcript keeps its old
|
|
339
|
+
// custom-title, which the picker reads ahead of the file. Modes match Claude
|
|
340
|
+
// Code's, chmod'd too since a mode option only applies when the path is created.
|
|
341
|
+
const sessionDir = filePath.replace(/\.jsonl$/, '');
|
|
342
|
+
const titleFile = join(sessionDir, 'custom-title.json');
|
|
343
|
+
await mkdir(sessionDir, { recursive: true, mode: 0o700 });
|
|
344
|
+
await chmod(sessionDir, 0o700);
|
|
345
|
+
await writeFile(titleFile, JSON.stringify({ customTitle: title }), { mode: 0o600 });
|
|
346
|
+
await chmod(titleFile, 0o600);
|
|
347
|
+
const records = [
|
|
348
|
+
{ type: 'custom-title', customTitle: title, sessionId },
|
|
349
|
+
{ type: 'agent-name', agentName: title, sessionId }
|
|
350
|
+
];
|
|
351
|
+
await appendFile(filePath, records.map((r) => `${JSON.stringify(r)}\n`).join(''));
|
|
352
|
+
}
|
|
353
|
+
// Trash everything Claude Code keys by session id, plus the session's
|
|
354
|
+
// history.jsonl lines. Goes to Trash (recoverable). Skips plans/ and paste-cache/
|
|
355
|
+
// (shared) and sessions/<pid>.json (ephemeral). An open session would write the
|
|
356
|
+
// transcript back, so it is refused.
|
|
357
|
+
export async function deleteSession(filePath, trashFn = trashPaths, { isLive = isSessionLive, tmpDir = claudeTmpDir() } = {}) {
|
|
358
|
+
const sessionId = basename(filePath, '.jsonl');
|
|
359
|
+
if (await isLive(sessionId))
|
|
360
|
+
throw new Error(LIVE);
|
|
361
|
+
const dir = claudeDir();
|
|
362
|
+
const projectDir = dirname(filePath);
|
|
363
|
+
const candidates = [
|
|
364
|
+
join(dir, 'tasks', sessionId),
|
|
365
|
+
join(dir, 'file-history', sessionId),
|
|
366
|
+
join(dir, 'session-env', sessionId),
|
|
367
|
+
join(dir, 'uploads', sessionId),
|
|
368
|
+
join(dir, 'debug', `${sessionId}.txt`),
|
|
369
|
+
join(tmpDir, basename(projectDir), sessionId)
|
|
370
|
+
];
|
|
371
|
+
// In the project dir, the transcript, its same-named dir, and every sidecar
|
|
372
|
+
// (<id>.jsonl.superseded-*, <id>.ccr-tip.json, <id>-<stamp>.cast, ...) share the
|
|
373
|
+
// id as a prefix, and a uuid can't prefix another session's.
|
|
374
|
+
try {
|
|
375
|
+
for (const name of await readdir(projectDir)) {
|
|
376
|
+
if (name === sessionId ||
|
|
377
|
+
name.startsWith(`${sessionId}.`) ||
|
|
378
|
+
name.startsWith(`${sessionId}-`))
|
|
379
|
+
candidates.push(join(projectDir, name));
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
catch { }
|
|
383
|
+
// telemetry is keyed by a filename prefix too
|
|
384
|
+
const telemetryDir = join(dir, 'telemetry');
|
|
385
|
+
try {
|
|
386
|
+
const prefix = `1p_failed_events.${sessionId}.`;
|
|
387
|
+
for (const name of await readdir(telemetryDir)) {
|
|
388
|
+
if (name.startsWith(prefix))
|
|
389
|
+
candidates.push(join(telemetryDir, name));
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
catch { }
|
|
393
|
+
const toTrash = [];
|
|
394
|
+
for (const p of candidates) {
|
|
395
|
+
try {
|
|
396
|
+
await stat(p);
|
|
397
|
+
toTrash.push(p);
|
|
398
|
+
}
|
|
399
|
+
catch { }
|
|
400
|
+
}
|
|
401
|
+
await trashFn(toTrash);
|
|
402
|
+
await scrubHistory(sessionId, trashFn);
|
|
403
|
+
}
|
|
404
|
+
// Remove the session's prompt lines from history.jsonl, writing the removed lines
|
|
405
|
+
// to a file that's trashed too, so even this stays recoverable.
|
|
406
|
+
async function scrubHistory(sessionId, trashFn) {
|
|
407
|
+
const historyPath = join(claudeDir(), 'history.jsonl');
|
|
408
|
+
let raw;
|
|
409
|
+
try {
|
|
410
|
+
raw = await readFile(historyPath, 'utf8');
|
|
411
|
+
}
|
|
412
|
+
catch {
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
const needle = `"sessionId":"${sessionId}"`;
|
|
416
|
+
if (!raw.includes(needle))
|
|
417
|
+
return;
|
|
418
|
+
const kept = [];
|
|
419
|
+
const removed = [];
|
|
420
|
+
for (const line of raw.split('\n')) {
|
|
421
|
+
if (line.includes(needle))
|
|
422
|
+
removed.push(line);
|
|
423
|
+
else
|
|
424
|
+
kept.push(line);
|
|
425
|
+
}
|
|
426
|
+
const removedPath = join(claudeDir(), `history-removed-${sessionId}.jsonl`);
|
|
427
|
+
await writeFile(removedPath, `${removed.join('\n')}\n`);
|
|
428
|
+
await writeFile(historyPath, kept.join('\n'));
|
|
429
|
+
await trashFn([removedPath]);
|
|
430
|
+
}
|
|
431
|
+
export const claude = {
|
|
432
|
+
id: 'claude',
|
|
433
|
+
name: 'Claude Code',
|
|
434
|
+
mark: '✻',
|
|
435
|
+
listFiles: listSessionFiles,
|
|
436
|
+
parse: (f) => parseSession(f.filePath, f.stats),
|
|
437
|
+
messages: (s) => getSessionMessages(s.filePath),
|
|
438
|
+
resume: (s) => ({ command: 'claude', args: ['--resume', s.sessionId] }),
|
|
439
|
+
liveIds: () => liveSessionIds(),
|
|
440
|
+
rename: async (s, title) => {
|
|
441
|
+
await renameSession(s.filePath, title);
|
|
442
|
+
return title;
|
|
443
|
+
},
|
|
444
|
+
delete: (s) => deleteSession(s.filePath)
|
|
445
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { claude } from './claude.js';
|
|
2
|
+
import { omp } from './omp.js';
|
|
3
|
+
export const HARNESSES = [claude, omp];
|
|
4
|
+
const byId = Object.fromEntries(HARNESSES.map((h) => [h.id, h]));
|
|
5
|
+
export const harnessOf = (s) => byId[s.harness];
|
|
6
|
+
// Every harness's session files, merged newest-first so the first batch the
|
|
7
|
+
// UI parses is the most recent across all of them.
|
|
8
|
+
export async function listSessionFiles(harnesses = HARNESSES) {
|
|
9
|
+
const lists = await Promise.all(harnesses.map((h) => h.listFiles()));
|
|
10
|
+
return lists.flat().sort((a, b) => b.stats.mtimeMs - a.stats.mtimeMs);
|
|
11
|
+
}
|
|
12
|
+
export async function parseSessions(files) {
|
|
13
|
+
const sessions = await Promise.all(files.map((f) => harnessOf(f).parse(f)));
|
|
14
|
+
return sessions.filter((s) => s !== null);
|
|
15
|
+
}
|
|
16
|
+
export async function loadSessions() {
|
|
17
|
+
const sessions = await parseSessions(await listSessionFiles());
|
|
18
|
+
return sessions.sort((a, b) => b.lastTimestamp - a.lastTimestamp);
|
|
19
|
+
}
|
|
20
|
+
export const getSessionMessages = (s) => harnessOf(s).messages(s);
|
|
21
|
+
export const renameSession = (s, title) => harnessOf(s).rename(s, title);
|
|
22
|
+
// Ids of every session a running harness has open, one probe per harness.
|
|
23
|
+
export async function liveSessions(sessions) {
|
|
24
|
+
const sets = await Promise.all(HARNESSES.map((h) => h.liveIds(sessions.filter((s) => s.harness === h.id))));
|
|
25
|
+
return new Set(sets.flatMap((s) => [...s]));
|
|
26
|
+
}
|
|
27
|
+
export const deleteSession = (s) => harnessOf(s).delete(s);
|