@arcreflex/agent-transcripts 0.1.14 → 0.1.16
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/adapters/claude-code.ts +1 -0
- package/src/adapters/index.ts +4 -0
- package/src/adapters/pi-coding-agent.ts +633 -0
- package/src/archive.ts +39 -0
- package/src/render.ts +6 -6
- package/src/serve.ts +206 -49
- package/src/types.ts +1 -0
- package/src/utils/summary.ts +3 -3
- package/test/adapters.test.ts +35 -1
- package/test/archive.test.ts +44 -1
- package/test/fixtures/claude/multiple-tools.output.md +3 -3
- package/test/fixtures/claude/skipped-message-chain.output.md +1 -1
- package/test/fixtures/claude/with-tools.output.md +1 -1
- package/test/fixtures/pi-coding-agent/basic-conversation.input.jsonl +5 -0
- package/test/fixtures/pi-coding-agent/basic-conversation.output.md +28 -0
- package/test/fixtures/pi-coding-agent/branching.input.jsonl +7 -0
- package/test/fixtures/pi-coding-agent/branching.output.md +25 -0
- package/test/fixtures/pi-coding-agent/with-compaction.input.jsonl +7 -0
- package/test/fixtures/pi-coding-agent/with-compaction.output.md +28 -0
- package/test/fixtures/pi-coding-agent/with-thinking.input.jsonl +3 -0
- package/test/fixtures/pi-coding-agent/with-thinking.output.md +21 -0
- package/test/fixtures/pi-coding-agent/with-tools.input.jsonl +5 -0
- package/test/fixtures/pi-coding-agent/with-tools.output.md +20 -0
- package/test/serve.test.ts +168 -0
- package/test/snapshots.test.ts +61 -37
- package/test/summary.test.ts +6 -0
package/src/archive.ts
CHANGED
|
@@ -10,6 +10,7 @@ import { homedir } from "os";
|
|
|
10
10
|
import { mkdir, readdir, rename, unlink } from "fs/promises";
|
|
11
11
|
import type { Adapter, DiscoveredSession, Transcript } from "./types.ts";
|
|
12
12
|
import { extractSessionId } from "./utils/naming.ts";
|
|
13
|
+
import { renderTranscript } from "./render.ts";
|
|
13
14
|
|
|
14
15
|
export const DEFAULT_ARCHIVE_DIR = join(
|
|
15
16
|
process.env.XDG_DATA_HOME || join(homedir(), ".local/share"),
|
|
@@ -119,6 +120,35 @@ export async function saveEntry(
|
|
|
119
120
|
}
|
|
120
121
|
}
|
|
121
122
|
|
|
123
|
+
function renderEntryMarkdown(entry: ArchiveEntry): string {
|
|
124
|
+
return entry.transcripts
|
|
125
|
+
.map((t, i) =>
|
|
126
|
+
renderTranscript(t, {
|
|
127
|
+
sourcePath: i === 0 ? entry.sourcePath : undefined,
|
|
128
|
+
}),
|
|
129
|
+
)
|
|
130
|
+
.join("\n\n---\n\n");
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async function saveMarkdown(
|
|
134
|
+
archiveDir: string,
|
|
135
|
+
entry: ArchiveEntry,
|
|
136
|
+
): Promise<void> {
|
|
137
|
+
const filePath = join(archiveDir, `${entry.sessionId}.md`);
|
|
138
|
+
const tmpPath = `${filePath}.${process.pid}.${Date.now()}.tmp`;
|
|
139
|
+
const content = renderEntryMarkdown(entry);
|
|
140
|
+
|
|
141
|
+
await Bun.write(tmpPath, content);
|
|
142
|
+
try {
|
|
143
|
+
await rename(tmpPath, filePath);
|
|
144
|
+
} catch (err) {
|
|
145
|
+
try {
|
|
146
|
+
await unlink(tmpPath);
|
|
147
|
+
} catch {}
|
|
148
|
+
throw err;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
122
152
|
export function isFresh(
|
|
123
153
|
entry: ArchiveEntry,
|
|
124
154
|
sourceHash: string,
|
|
@@ -146,8 +176,16 @@ export async function archiveSession(
|
|
|
146
176
|
if (session.summary && existing.title !== session.summary) {
|
|
147
177
|
existing.title = session.summary;
|
|
148
178
|
await saveEntry(archiveDir, existing);
|
|
179
|
+
await saveMarkdown(archiveDir, existing);
|
|
149
180
|
return { entry: existing, updated: true };
|
|
150
181
|
}
|
|
182
|
+
|
|
183
|
+
// Backfill .md if missing (upgrade path for pre-markdown archives)
|
|
184
|
+
const mdPath = join(archiveDir, `${sessionId}.md`);
|
|
185
|
+
if (!(await Bun.file(mdPath).exists())) {
|
|
186
|
+
await saveMarkdown(archiveDir, existing);
|
|
187
|
+
}
|
|
188
|
+
|
|
151
189
|
return { entry: existing, updated: false };
|
|
152
190
|
}
|
|
153
191
|
|
|
@@ -166,6 +204,7 @@ export async function archiveSession(
|
|
|
166
204
|
};
|
|
167
205
|
|
|
168
206
|
await saveEntry(archiveDir, entry);
|
|
207
|
+
await saveMarkdown(archiveDir, entry);
|
|
169
208
|
return { entry, updated: true };
|
|
170
209
|
}
|
|
171
210
|
|
package/src/render.ts
CHANGED
|
@@ -6,10 +6,9 @@ import type { Transcript, Message, ToolCall } from "./types.ts";
|
|
|
6
6
|
import { walkTranscriptTree } from "./utils/tree.ts";
|
|
7
7
|
|
|
8
8
|
function formatToolCall(call: ToolCall): string {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
return call.name;
|
|
9
|
+
const label = call.summary ? `${call.name} \`${call.summary}\`` : call.name;
|
|
10
|
+
const ref = call.id ? ` <!-- tool:${call.id} -->` : "";
|
|
11
|
+
return `${label}${ref}`;
|
|
13
12
|
}
|
|
14
13
|
|
|
15
14
|
function formatToolCalls(calls: ToolCall[]): string {
|
|
@@ -60,13 +59,14 @@ ${msg.thinking}
|
|
|
60
59
|
export interface RenderTranscriptOptions {
|
|
61
60
|
head?: string; // render branch ending at this message ID
|
|
62
61
|
sourcePath?: string; // absolute source path for front matter provenance
|
|
62
|
+
title?: string; // override the "# Transcript" heading
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
export function renderTranscript(
|
|
66
66
|
transcript: Transcript,
|
|
67
67
|
options: RenderTranscriptOptions = {},
|
|
68
68
|
): string {
|
|
69
|
-
const { head, sourcePath } = options;
|
|
69
|
+
const { head, sourcePath, title } = options;
|
|
70
70
|
|
|
71
71
|
const lines: string[] = [];
|
|
72
72
|
|
|
@@ -79,7 +79,7 @@ export function renderTranscript(
|
|
|
79
79
|
}
|
|
80
80
|
|
|
81
81
|
// Header
|
|
82
|
-
lines.push("
|
|
82
|
+
lines.push(`# ${title || "Transcript"}`);
|
|
83
83
|
lines.push("");
|
|
84
84
|
lines.push(`**Source**: \`${transcript.source.file}\``);
|
|
85
85
|
lines.push(`**Adapter**: ${transcript.source.adapter}`);
|
package/src/serve.ts
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Serve command: HTTP server for transcripts from the archive.
|
|
3
3
|
*
|
|
4
|
-
* Reads archive entries, renders
|
|
4
|
+
* Reads archive entries, renders on demand in multiple formats:
|
|
5
|
+
* - .html — rich HTML rendering (LRU cached, shiki highlighting)
|
|
6
|
+
* - .md — markdown (lightweight, agent-friendly)
|
|
7
|
+
* - .json — raw transcript data
|
|
5
8
|
*/
|
|
6
9
|
|
|
7
10
|
import type { ArchiveEntryHeader, TranscriptSummary } from "./archive.ts";
|
|
8
11
|
import { listEntryHeaders, loadEntry, DEFAULT_ARCHIVE_DIR } from "./archive.ts";
|
|
12
|
+
import type { Transcript } from "./types.ts";
|
|
9
13
|
import { renderTranscriptHtml } from "./render-html.ts";
|
|
14
|
+
import { renderTranscript } from "./render.ts";
|
|
10
15
|
import { renderIndexFromSessions, type SessionEntry } from "./render-index.ts";
|
|
11
16
|
import { formatDateTimePrefix } from "./utils/naming.ts";
|
|
17
|
+
import { truncate } from "./utils/text.ts";
|
|
12
18
|
|
|
13
19
|
export interface ServeOptions {
|
|
14
20
|
archiveDir?: string;
|
|
@@ -25,6 +31,14 @@ interface SessionInfo {
|
|
|
25
31
|
baseName: string;
|
|
26
32
|
}
|
|
27
33
|
|
|
34
|
+
type Format = "html" | "md" | "json";
|
|
35
|
+
|
|
36
|
+
const CONTENT_TYPES: Record<Format, string> = {
|
|
37
|
+
html: "text/html; charset=utf-8",
|
|
38
|
+
md: "text/markdown; charset=utf-8",
|
|
39
|
+
json: "application/json; charset=utf-8",
|
|
40
|
+
};
|
|
41
|
+
|
|
28
42
|
/** Simple LRU cache for rendered HTML. */
|
|
29
43
|
class HtmlCache {
|
|
30
44
|
private map = new Map<string, string>();
|
|
@@ -93,23 +107,24 @@ function buildSessionMap(
|
|
|
93
107
|
return sessions;
|
|
94
108
|
}
|
|
95
109
|
|
|
110
|
+
function sessionTitle(info: SessionInfo): string {
|
|
111
|
+
return (
|
|
112
|
+
info.title || truncate(info.segment.firstUserMessage, 80) || info.baseName
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
|
|
96
116
|
function buildIndexEntries(sessions: Map<string, SessionInfo>): SessionEntry[] {
|
|
97
117
|
const entries: SessionEntry[] = [];
|
|
98
118
|
|
|
99
119
|
for (const [baseName, info] of sessions) {
|
|
100
|
-
const { segment
|
|
120
|
+
const { segment } = info;
|
|
101
121
|
if (segment.metadata.messageCount === 0) continue;
|
|
102
122
|
|
|
103
123
|
const { messageCount, startTime, endTime, cwd } = segment.metadata;
|
|
104
124
|
|
|
105
125
|
entries.push({
|
|
106
126
|
filename: `${baseName}.html`,
|
|
107
|
-
title:
|
|
108
|
-
title ||
|
|
109
|
-
(segment.firstUserMessage.length > 80
|
|
110
|
-
? segment.firstUserMessage.slice(0, 80) + "..."
|
|
111
|
-
: segment.firstUserMessage) ||
|
|
112
|
-
baseName,
|
|
127
|
+
title: sessionTitle(info),
|
|
113
128
|
firstUserMessage: segment.firstUserMessage,
|
|
114
129
|
date: startTime,
|
|
115
130
|
endDate: endTime,
|
|
@@ -121,6 +136,128 @@ function buildIndexEntries(sessions: Map<string, SessionInfo>): SessionEntry[] {
|
|
|
121
136
|
return entries;
|
|
122
137
|
}
|
|
123
138
|
|
|
139
|
+
// ============================================================================
|
|
140
|
+
// Format-specific renderers
|
|
141
|
+
// ============================================================================
|
|
142
|
+
|
|
143
|
+
function renderTranscriptMd(
|
|
144
|
+
transcript: Transcript,
|
|
145
|
+
baseName: string,
|
|
146
|
+
title: string,
|
|
147
|
+
): string {
|
|
148
|
+
const nav = `*→ [html](${baseName}.html) · [json](${baseName}.json)*`;
|
|
149
|
+
const md = renderTranscript(transcript, { title });
|
|
150
|
+
// Insert format links after the title line
|
|
151
|
+
return md.replace(/^(# .+)\n/, `$1\n\n${nav}\n`);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function renderTranscriptJson(transcript: Transcript): string {
|
|
155
|
+
return JSON.stringify(transcript, null, 2);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function renderIndexMd(sessions: Map<string, SessionInfo>): string {
|
|
159
|
+
const sorted = [...sessions.entries()]
|
|
160
|
+
.filter(([, info]) => info.segment.metadata.messageCount > 0)
|
|
161
|
+
.sort(
|
|
162
|
+
(a, b) =>
|
|
163
|
+
new Date(b[1].segment.metadata.startTime).getTime() -
|
|
164
|
+
new Date(a[1].segment.metadata.startTime).getTime(),
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
const lines: string[] = [
|
|
168
|
+
"# Agent Transcripts",
|
|
169
|
+
"",
|
|
170
|
+
`${sorted.length} session${sorted.length !== 1 ? "s" : ""}`,
|
|
171
|
+
"",
|
|
172
|
+
"*→ [html](index.html) · [json](index.json)*",
|
|
173
|
+
"",
|
|
174
|
+
];
|
|
175
|
+
|
|
176
|
+
// Group by cwd
|
|
177
|
+
const groups = new Map<string, [string, SessionInfo][]>();
|
|
178
|
+
for (const entry of sorted) {
|
|
179
|
+
const cwd = entry[1].segment.metadata.cwd || "(unknown)";
|
|
180
|
+
const group = groups.get(cwd) || [];
|
|
181
|
+
group.push(entry);
|
|
182
|
+
groups.set(cwd, group);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
for (const [cwd, entries] of groups) {
|
|
186
|
+
lines.push(`## ${cwd}`, "");
|
|
187
|
+
for (const [baseName, info] of entries) {
|
|
188
|
+
const title = sessionTitle(info);
|
|
189
|
+
const msgs = info.segment.metadata.messageCount;
|
|
190
|
+
const preview = truncate(info.segment.firstUserMessage, 100);
|
|
191
|
+
lines.push(`- [${title}](${baseName}.md) — ${msgs} msgs`);
|
|
192
|
+
if (preview) {
|
|
193
|
+
lines.push(` > ${preview}`);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
lines.push("");
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
return lines.join("\n");
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
interface IndexJsonSession {
|
|
203
|
+
title: string;
|
|
204
|
+
links: { html: string; md: string; json: string };
|
|
205
|
+
date: string;
|
|
206
|
+
endDate: string;
|
|
207
|
+
messageCount: number;
|
|
208
|
+
cwd?: string;
|
|
209
|
+
firstUserMessage: string;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function renderIndexJson(sessions: Map<string, SessionInfo>): string {
|
|
213
|
+
const sorted = [...sessions.entries()]
|
|
214
|
+
.filter(([, info]) => info.segment.metadata.messageCount > 0)
|
|
215
|
+
.sort(
|
|
216
|
+
(a, b) =>
|
|
217
|
+
new Date(b[1].segment.metadata.startTime).getTime() -
|
|
218
|
+
new Date(a[1].segment.metadata.startTime).getTime(),
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
const result: IndexJsonSession[] = sorted.map(([baseName, info]) => ({
|
|
222
|
+
title: sessionTitle(info),
|
|
223
|
+
links: {
|
|
224
|
+
html: `${baseName}.html`,
|
|
225
|
+
md: `${baseName}.md`,
|
|
226
|
+
json: `${baseName}.json`,
|
|
227
|
+
},
|
|
228
|
+
date: info.segment.metadata.startTime,
|
|
229
|
+
endDate: info.segment.metadata.endTime,
|
|
230
|
+
messageCount: info.segment.metadata.messageCount,
|
|
231
|
+
cwd: info.segment.metadata.cwd,
|
|
232
|
+
firstUserMessage: info.segment.firstUserMessage,
|
|
233
|
+
}));
|
|
234
|
+
|
|
235
|
+
return JSON.stringify({ sessions: result }, null, 2);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// ============================================================================
|
|
239
|
+
// Server
|
|
240
|
+
// ============================================================================
|
|
241
|
+
|
|
242
|
+
/** Parse a URL path into baseName + format, or null if unrecognized. */
|
|
243
|
+
function parseRoute(
|
|
244
|
+
path: string,
|
|
245
|
+
):
|
|
246
|
+
| { type: "index"; format: Format }
|
|
247
|
+
| { type: "session"; baseName: string; format: Format }
|
|
248
|
+
| null {
|
|
249
|
+
if (path === "/") return { type: "index", format: "html" };
|
|
250
|
+
|
|
251
|
+
const match = path.match(/^\/(.+)\.(html|md|json)$/);
|
|
252
|
+
if (!match) return null;
|
|
253
|
+
|
|
254
|
+
const [, name, ext] = match;
|
|
255
|
+
const format = ext as Format;
|
|
256
|
+
|
|
257
|
+
if (name === "index") return { type: "index", format };
|
|
258
|
+
return { type: "session", baseName: name, format };
|
|
259
|
+
}
|
|
260
|
+
|
|
124
261
|
export async function serve(options: ServeOptions): Promise<void> {
|
|
125
262
|
const {
|
|
126
263
|
archiveDir = DEFAULT_ARCHIVE_DIR,
|
|
@@ -136,8 +273,10 @@ export async function serve(options: ServeOptions): Promise<void> {
|
|
|
136
273
|
const sessions = buildSessionMap(headers);
|
|
137
274
|
const htmlCache = new HtmlCache(200);
|
|
138
275
|
|
|
139
|
-
// Index is static (session map doesn't change), so build once
|
|
276
|
+
// Index HTML is static (session map doesn't change), so build once
|
|
140
277
|
const indexHtml = renderIndexFromSessions(buildIndexEntries(sessions));
|
|
278
|
+
const indexMd = renderIndexMd(sessions);
|
|
279
|
+
const indexJson = renderIndexJson(sessions);
|
|
141
280
|
|
|
142
281
|
if (!quiet) {
|
|
143
282
|
console.error(`Found ${sessions.size} transcript(s)`);
|
|
@@ -148,58 +287,76 @@ export async function serve(options: ServeOptions): Promise<void> {
|
|
|
148
287
|
port,
|
|
149
288
|
async fetch(req) {
|
|
150
289
|
const url = new URL(req.url);
|
|
151
|
-
const
|
|
290
|
+
const route = parseRoute(url.pathname);
|
|
152
291
|
|
|
153
292
|
if (!quiet) {
|
|
154
|
-
console.error(`${req.method} ${
|
|
293
|
+
console.error(`${req.method} ${url.pathname}`);
|
|
155
294
|
}
|
|
156
295
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
296
|
+
if (!route) {
|
|
297
|
+
return new Response("Not Found", { status: 404 });
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// Index
|
|
301
|
+
if (route.type === "index") {
|
|
302
|
+
const body =
|
|
303
|
+
route.format === "html"
|
|
304
|
+
? indexHtml
|
|
305
|
+
: route.format === "md"
|
|
306
|
+
? indexMd
|
|
307
|
+
: indexJson;
|
|
308
|
+
return new Response(body, {
|
|
309
|
+
headers: { "Content-Type": CONTENT_TYPES[route.format] },
|
|
161
310
|
});
|
|
162
311
|
}
|
|
163
312
|
|
|
164
|
-
// Session
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
// Load full entry from disk on demand
|
|
181
|
-
const entry = await loadEntry(archiveDir, sessionId);
|
|
182
|
-
const transcript = entry?.transcripts[segmentIndex];
|
|
183
|
-
if (!transcript) {
|
|
184
|
-
return new Response("Not Found", { status: 404 });
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
const html = await renderTranscriptHtml(transcript, { title });
|
|
188
|
-
htmlCache.set(sessionId, segmentIndex, sourceHash, html);
|
|
189
|
-
return new Response(html, {
|
|
190
|
-
headers: { "Content-Type": "text/html; charset=utf-8" },
|
|
313
|
+
// Session
|
|
314
|
+
const info = sessions.get(route.baseName);
|
|
315
|
+
if (!info) {
|
|
316
|
+
return new Response("Not Found", { status: 404 });
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
try {
|
|
320
|
+
const { sessionId, segmentIndex, sourceHash } = info;
|
|
321
|
+
const title = sessionTitle(info);
|
|
322
|
+
|
|
323
|
+
// HTML — use LRU cache (shiki rendering is expensive)
|
|
324
|
+
if (route.format === "html") {
|
|
325
|
+
const cached = htmlCache.get(sessionId, segmentIndex, sourceHash);
|
|
326
|
+
if (cached) {
|
|
327
|
+
return new Response(cached, {
|
|
328
|
+
headers: { "Content-Type": CONTENT_TYPES.html },
|
|
191
329
|
});
|
|
192
|
-
} catch (error) {
|
|
193
|
-
const message =
|
|
194
|
-
error instanceof Error ? error.message : String(error);
|
|
195
|
-
return new Response(`Error: ${message}`, { status: 500 });
|
|
196
330
|
}
|
|
331
|
+
|
|
332
|
+
const entry = await loadEntry(archiveDir, sessionId);
|
|
333
|
+
const transcript = entry?.transcripts[segmentIndex];
|
|
334
|
+
if (!transcript) return new Response("Not Found", { status: 404 });
|
|
335
|
+
|
|
336
|
+
const html = await renderTranscriptHtml(transcript, { title });
|
|
337
|
+
htmlCache.set(sessionId, segmentIndex, sourceHash, html);
|
|
338
|
+
return new Response(html, {
|
|
339
|
+
headers: { "Content-Type": CONTENT_TYPES.html },
|
|
340
|
+
});
|
|
197
341
|
}
|
|
198
342
|
|
|
199
|
-
|
|
200
|
-
|
|
343
|
+
// Markdown and JSON — render on demand (cheap)
|
|
344
|
+
const entry = await loadEntry(archiveDir, sessionId);
|
|
345
|
+
const transcript = entry?.transcripts[segmentIndex];
|
|
346
|
+
if (!transcript) return new Response("Not Found", { status: 404 });
|
|
347
|
+
|
|
348
|
+
const body =
|
|
349
|
+
route.format === "md"
|
|
350
|
+
? renderTranscriptMd(transcript, route.baseName, title)
|
|
351
|
+
: renderTranscriptJson(transcript);
|
|
201
352
|
|
|
202
|
-
|
|
353
|
+
return new Response(body, {
|
|
354
|
+
headers: { "Content-Type": CONTENT_TYPES[route.format] },
|
|
355
|
+
});
|
|
356
|
+
} catch (error) {
|
|
357
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
358
|
+
return new Response(`Error: ${message}`, { status: 500 });
|
|
359
|
+
}
|
|
203
360
|
},
|
|
204
361
|
});
|
|
205
362
|
|
package/src/types.ts
CHANGED
package/src/utils/summary.ts
CHANGED
|
@@ -7,9 +7,9 @@ import { truncate } from "./text.ts";
|
|
|
7
7
|
type ToolInput = Record<string, unknown>;
|
|
8
8
|
|
|
9
9
|
const extractors: Record<string, (input: ToolInput) => string> = {
|
|
10
|
-
Read: (i) => String(i.file_path || ""),
|
|
11
|
-
Write: (i) => String(i.file_path || ""),
|
|
12
|
-
Edit: (i) => String(i.file_path || ""),
|
|
10
|
+
Read: (i) => String(i.file_path || i.path || ""),
|
|
11
|
+
Write: (i) => String(i.file_path || i.path || ""),
|
|
12
|
+
Edit: (i) => String(i.file_path || i.path || ""),
|
|
13
13
|
Bash: (i) => {
|
|
14
14
|
if (i.description) return String(i.description);
|
|
15
15
|
if (i.command) return truncate(String(i.command), 60);
|
package/test/adapters.test.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { describe, expect, it } from "bun:test";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
getDefaultSources,
|
|
4
|
+
getAdapter,
|
|
5
|
+
detectAdapter,
|
|
6
|
+
} from "../src/adapters/index.ts";
|
|
3
7
|
import { claudeCodeAdapter } from "../src/adapters/claude-code.ts";
|
|
8
|
+
import { piCodingAgentAdapter } from "../src/adapters/pi-coding-agent.ts";
|
|
4
9
|
|
|
5
10
|
describe("getDefaultSources", () => {
|
|
6
11
|
it("returns claude-code with its defaultSource", () => {
|
|
@@ -14,6 +19,14 @@ describe("getDefaultSources", () => {
|
|
|
14
19
|
expect(cc?.source.length).toBeGreaterThan(0);
|
|
15
20
|
});
|
|
16
21
|
|
|
22
|
+
it("returns pi-coding-agent with its defaultSource", () => {
|
|
23
|
+
const specs = getDefaultSources();
|
|
24
|
+
const pi = specs.find((s) => s.adapter.name === "pi-coding-agent");
|
|
25
|
+
expect(pi).toBeDefined();
|
|
26
|
+
expect(pi?.adapter).toBe(piCodingAgentAdapter);
|
|
27
|
+
expect(pi?.source).toMatch(/\.pi[/\\]agent[/\\]sessions$/);
|
|
28
|
+
});
|
|
29
|
+
|
|
17
30
|
it("only includes adapters with defaultSource set", () => {
|
|
18
31
|
for (const spec of getDefaultSources()) {
|
|
19
32
|
expect(spec.source).toBeTruthy();
|
|
@@ -24,9 +37,30 @@ describe("getDefaultSources", () => {
|
|
|
24
37
|
describe("getAdapter", () => {
|
|
25
38
|
it("returns adapter by name", () => {
|
|
26
39
|
expect(getAdapter("claude-code")).toBe(claudeCodeAdapter);
|
|
40
|
+
expect(getAdapter("pi-coding-agent")).toBe(piCodingAgentAdapter);
|
|
27
41
|
});
|
|
28
42
|
|
|
29
43
|
it("returns undefined for unknown adapter", () => {
|
|
30
44
|
expect(getAdapter("nonexistent")).toBeUndefined();
|
|
31
45
|
});
|
|
32
46
|
});
|
|
47
|
+
|
|
48
|
+
describe("detectAdapter", () => {
|
|
49
|
+
it("detects claude-code from .claude/ paths", () => {
|
|
50
|
+
expect(detectAdapter("/home/user/.claude/projects/foo/session.jsonl")).toBe(
|
|
51
|
+
"claude-code",
|
|
52
|
+
);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("detects pi-coding-agent from .pi/agent/sessions/ paths", () => {
|
|
56
|
+
expect(
|
|
57
|
+
detectAdapter(
|
|
58
|
+
"/home/user/.pi/agent/sessions/--project--/12345_abc.jsonl",
|
|
59
|
+
),
|
|
60
|
+
).toBe("pi-coding-agent");
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("returns undefined for unrecognized paths", () => {
|
|
64
|
+
expect(detectAdapter("/tmp/random/file.jsonl")).toBeUndefined();
|
|
65
|
+
});
|
|
66
|
+
});
|
package/test/archive.test.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { describe, expect, it, beforeEach, afterEach } from "bun:test";
|
|
2
2
|
import { join } from "path";
|
|
3
|
-
import { mkdtemp, rm } from "fs/promises";
|
|
3
|
+
import { mkdtemp, readdir, rm, unlink } from "fs/promises";
|
|
4
4
|
import { tmpdir } from "os";
|
|
5
5
|
import {
|
|
6
6
|
archiveSession,
|
|
@@ -176,6 +176,49 @@ describe("archiveSession", () => {
|
|
|
176
176
|
expect(loaded?.title).toBe("New harness title");
|
|
177
177
|
});
|
|
178
178
|
|
|
179
|
+
it("writes .md alongside .json", async () => {
|
|
180
|
+
const fixturePath = join(fixturesDir, "basic-conversation.input.jsonl");
|
|
181
|
+
const session = {
|
|
182
|
+
path: fixturePath,
|
|
183
|
+
relativePath: "basic-conversation.input.jsonl",
|
|
184
|
+
mtime: Date.now(),
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
const { entry } = await archiveSession(tmpDir, session, claudeCodeAdapter);
|
|
188
|
+
|
|
189
|
+
const mdPath = join(tmpDir, `${entry.sessionId}.md`);
|
|
190
|
+
const md = await Bun.file(mdPath).text();
|
|
191
|
+
expect(md).toContain("# Transcript");
|
|
192
|
+
expect(md).toContain(fixturePath);
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it("backfills .md when json is fresh but .md is missing", async () => {
|
|
196
|
+
const fixturePath = join(fixturesDir, "basic-conversation.input.jsonl");
|
|
197
|
+
const session = {
|
|
198
|
+
path: fixturePath,
|
|
199
|
+
relativePath: "basic-conversation.input.jsonl",
|
|
200
|
+
mtime: Date.now(),
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
// Archive once (creates both .json and .md)
|
|
204
|
+
const { entry } = await archiveSession(tmpDir, session, claudeCodeAdapter);
|
|
205
|
+
|
|
206
|
+
// Delete the .md to simulate a pre-markdown archive
|
|
207
|
+
const mdPath = join(tmpDir, `${entry.sessionId}.md`);
|
|
208
|
+
await unlink(mdPath);
|
|
209
|
+
|
|
210
|
+
// Re-archive — json is fresh, but .md should be backfilled
|
|
211
|
+
const { updated } = await archiveSession(
|
|
212
|
+
tmpDir,
|
|
213
|
+
session,
|
|
214
|
+
claudeCodeAdapter,
|
|
215
|
+
);
|
|
216
|
+
expect(updated).toBe(false);
|
|
217
|
+
|
|
218
|
+
const md = await Bun.file(mdPath).text();
|
|
219
|
+
expect(md).toContain("# Transcript");
|
|
220
|
+
});
|
|
221
|
+
|
|
179
222
|
it("preserves existing title when re-archiving", async () => {
|
|
180
223
|
const fixturePath = join(fixturesDir, "basic-conversation.input.jsonl");
|
|
181
224
|
const session = {
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
{"type":"session","version":3,"id":"abc-123","timestamp":"2024-12-03T14:00:00.000Z","cwd":"/home/user/project"}
|
|
2
|
+
{"type":"message","id":"a1b2c3d4","parentId":null,"timestamp":"2024-12-03T14:00:01.000Z","message":{"role":"user","content":"Hello, can you help me with TypeScript?","timestamp":1733234401000}}
|
|
3
|
+
{"type":"message","id":"b2c3d4e5","parentId":"a1b2c3d4","timestamp":"2024-12-03T14:00:05.000Z","message":{"role":"assistant","content":[{"type":"text","text":"Of course! I'd be happy to help with TypeScript. What do you need?"}],"provider":"anthropic","model":"claude-sonnet-4-5","api":"messages","stopReason":"stop","usage":{"input":100,"output":20,"cacheRead":0,"cacheWrite":0,"totalTokens":120,"cost":{"input":0.001,"output":0.001,"cacheRead":0,"cacheWrite":0,"total":0.002}},"timestamp":1733234405000}}
|
|
4
|
+
{"type":"message","id":"c3d4e5f6","parentId":"b2c3d4e5","timestamp":"2024-12-03T14:00:30.000Z","message":{"role":"user","content":"How do I define a generic type?","timestamp":1733234430000}}
|
|
5
|
+
{"type":"message","id":"d4e5f6g7","parentId":"c3d4e5f6","timestamp":"2024-12-03T14:00:35.000Z","message":{"role":"assistant","content":[{"type":"text","text":"You can define a generic type using angle brackets:\n\n```typescript\ntype Result<T> = { ok: true; value: T } | { ok: false; error: string };\n```\n\nThe `T` is a type parameter that gets filled in when you use the type."}],"provider":"anthropic","model":"claude-sonnet-4-5","api":"messages","stopReason":"stop","usage":{"input":150,"output":50,"cacheRead":0,"cacheWrite":0,"totalTokens":200,"cost":{"input":0.002,"output":0.002,"cacheRead":0,"cacheWrite":0,"total":0.004}},"timestamp":1733234435000}}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# Transcript
|
|
2
|
+
|
|
3
|
+
**Source**: `test/fixtures/pi-coding-agent/basic-conversation.input.jsonl`
|
|
4
|
+
**Adapter**: pi-coding-agent
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## User
|
|
9
|
+
|
|
10
|
+
Hello, can you help me with TypeScript?
|
|
11
|
+
|
|
12
|
+
## Assistant
|
|
13
|
+
|
|
14
|
+
Of course! I'd be happy to help with TypeScript. What do you need?
|
|
15
|
+
|
|
16
|
+
## User
|
|
17
|
+
|
|
18
|
+
How do I define a generic type?
|
|
19
|
+
|
|
20
|
+
## Assistant
|
|
21
|
+
|
|
22
|
+
You can define a generic type using angle brackets:
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
type Result<T> = { ok: true; value: T } | { ok: false; error: string };
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
The `T` is a type parameter that gets filled in when you use the type.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{"type":"session","version":3,"id":"branch-001","timestamp":"2024-12-03T17:00:00.000Z","cwd":"/home/user/project"}
|
|
2
|
+
{"type":"message","id":"g1000001","parentId":null,"timestamp":"2024-12-03T17:00:01.000Z","message":{"role":"user","content":"Help me pick a web framework","timestamp":1733245201000}}
|
|
3
|
+
{"type":"message","id":"g2000002","parentId":"g1000001","timestamp":"2024-12-03T17:00:05.000Z","message":{"role":"assistant","content":[{"type":"text","text":"I'd recommend considering a few options. What are your priorities?"}],"provider":"anthropic","model":"claude-sonnet-4-5","api":"messages","stopReason":"stop","usage":{"input":100,"output":20,"cacheRead":0,"cacheWrite":0,"totalTokens":120,"cost":{"input":0.001,"output":0.001,"cacheRead":0,"cacheWrite":0,"total":0.002}},"timestamp":1733245205000}}
|
|
4
|
+
{"type":"message","id":"g3000003","parentId":"g2000002","timestamp":"2024-12-03T17:00:30.000Z","message":{"role":"user","content":"I want something with great TypeScript support","timestamp":1733245230000}}
|
|
5
|
+
{"type":"message","id":"g4000004","parentId":"g3000003","timestamp":"2024-12-03T17:00:35.000Z","message":{"role":"assistant","content":[{"type":"text","text":"For great TypeScript support, I'd suggest Next.js or SvelteKit."}],"provider":"anthropic","model":"claude-sonnet-4-5","api":"messages","stopReason":"stop","usage":{"input":150,"output":20,"cacheRead":0,"cacheWrite":0,"totalTokens":170,"cost":{"input":0.002,"output":0.001,"cacheRead":0,"cacheWrite":0,"total":0.003}},"timestamp":1733245235000}}
|
|
6
|
+
{"type":"message","id":"g5000005","parentId":"g2000002","timestamp":"2024-12-03T17:01:00.000Z","message":{"role":"user","content":"I want something minimal and fast","timestamp":1733245260000}}
|
|
7
|
+
{"type":"message","id":"g6000006","parentId":"g5000005","timestamp":"2024-12-03T17:01:05.000Z","message":{"role":"assistant","content":[{"type":"text","text":"For minimal and fast, check out Hono or Elysia — both are lightweight and performant."}],"provider":"anthropic","model":"claude-sonnet-4-5","api":"messages","stopReason":"stop","usage":{"input":150,"output":25,"cacheRead":0,"cacheWrite":0,"totalTokens":175,"cost":{"input":0.002,"output":0.001,"cacheRead":0,"cacheWrite":0,"total":0.003}},"timestamp":1733245265000}}
|