-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
338 lines (310 loc) · 10.9 KB
/
server.ts
File metadata and controls
338 lines (310 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import express from "express";
import cors from "cors";
import fs from "fs";
import path from "path";
import os from "os";
const app = express();
app.use(cors());
const CLAUDE_DIR = process.env.CLAUDE_DIR || path.join(os.homedir(), ".claude");
const PROJECTS_DIR = path.join(CLAUDE_DIR, "projects");
const IMAGE_MIME: Record<string, string> = {
".png": "image/png",
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".gif": "image/gif",
".webp": "image/webp",
".svg": "image/svg+xml",
".bmp": "image/bmp",
};
app.get("/api/image", (req, res) => {
try {
const raw = String(req.query.path || "");
if (!raw) { res.status(400).end(); return; }
// The trace file stores the host's absolute path (e.g. /Users/<user>/.claude/...),
// but inside a container CLAUDE_DIR points to a mounted copy. Normalize the
// request to a path that is relative to whatever CLAUDE_DIR is here.
const marker = "/.claude/";
const ix = raw.indexOf(marker);
const rel = ix >= 0 ? raw.slice(ix + marker.length) : raw.replace(/^\/+/, "");
const abs = path.resolve(CLAUDE_DIR, rel);
const within = path.relative(CLAUDE_DIR, abs);
if (within.startsWith("..") || path.isAbsolute(within)) { res.status(403).end(); return; }
const mime = IMAGE_MIME[path.extname(abs).toLowerCase()];
if (!mime) { res.status(415).end(); return; }
fs.stat(abs, (err, stat) => {
if (err || !stat.isFile()) { res.status(404).end(); return; }
res.setHeader("Content-Type", mime);
res.setHeader("Cache-Control", "public, max-age=86400, immutable");
fs.createReadStream(abs).pipe(res);
});
} catch {
res.status(500).end();
}
});
app.get("/api/projects", (_req, res) => {
try {
const projects = fs.readdirSync(PROJECTS_DIR)
.filter((d) => fs.statSync(path.join(PROJECTS_DIR, d)).isDirectory())
.map((d) => {
const projectPath = path.join(PROJECTS_DIR, d);
let latestMtime = 0;
let sessionCount = 0;
try {
for (const f of fs.readdirSync(projectPath)) {
if (f.endsWith(".jsonl")) {
sessionCount++;
const mt = fs.statSync(path.join(projectPath, f)).mtimeMs;
if (mt > latestMtime) latestMtime = mt;
}
}
} catch {}
return { name: d, sessionCount, mtime: latestMtime };
})
.sort((a, b) => b.mtime - a.mtime);
res.json(projects);
} catch {
res.json([]);
}
});
app.get("/api/projects/:project/sessions", (req, res) => {
try {
const projectDir = path.join(PROJECTS_DIR, req.params.project);
const files = fs
.readdirSync(projectDir)
.filter((f) => f.endsWith(".jsonl"))
.map((f) => {
const stat = fs.statSync(path.join(projectDir, f));
const lines = fs
.readFileSync(path.join(projectDir, f), "utf-8")
.split("\n")
.filter(Boolean);
let slug = "";
let firstUserMsg = "";
for (const line of lines) {
try {
const obj = JSON.parse(line);
if (obj.slug && !slug) slug = obj.slug;
if (
obj.type === "user" &&
!firstUserMsg &&
obj.message?.content
) {
const content =
typeof obj.message.content === "string"
? obj.message.content
: obj.message.content
.filter((b: any) => b.type === "text")
.map((b: any) => b.text)
.join(" ");
firstUserMsg = content.slice(0, 120);
}
if (slug && firstUserMsg) break;
} catch {}
}
return {
id: f.replace(".jsonl", ""),
file: f,
size: stat.size,
modified: stat.mtime.toISOString(),
lineCount: lines.length,
slug,
preview: firstUserMsg,
};
})
.sort(
(a, b) =>
new Date(b.modified).getTime() - new Date(a.modified).getTime()
);
res.json(files);
} catch {
res.json([]);
}
});
app.get("/api/projects/:project/sessions/:session", (req, res) => {
try {
const filePath = path.join(
PROJECTS_DIR,
req.params.project,
req.params.session + ".jsonl"
);
// Conditional GET: poll fires every few seconds; when the .jsonl file
// hasn't been touched we return 304 so the browser tab loader barely
// flickers and the client skips its re-normalize pass entirely.
const stat = fs.statSync(filePath);
const lastModified = stat.mtime.toUTCString();
const etag = `"${stat.mtimeMs.toString(36)}-${stat.size.toString(36)}"`;
res.set("Cache-Control", "no-cache");
res.set("Last-Modified", lastModified);
res.set("ETag", etag);
const inm = req.header("if-none-match");
const ims = req.header("if-modified-since");
if ((inm && inm === etag) || (ims && new Date(ims).getTime() >= Math.floor(stat.mtimeMs))) {
res.status(304).end();
return;
}
const lines = fs.readFileSync(filePath, "utf-8").split("\n").filter(Boolean);
const records = lines.map((line) => {
try {
return JSON.parse(line);
} catch {
return null;
}
}).filter(Boolean);
res.json(records);
} catch {
res.status(404).json({ error: "Session not found" });
}
});
// Resolve a subagent's persona (the .md file the model reads as its system prompt).
// Covers the standard Claude Code agent layouts. Note that paths under <cwd>
// (project-local agents) only resolve if the server has filesystem access to
// the project directory — true when running via `npm start`, but not in the
// default Docker setup which only mounts ~/.claude. Custom --plugin-dir
// sources are not covered.
// Returns the first match plus its resolved path, or null.
function resolvePersona(cwd: string, agentType: string): { content: string; resolvedPath: string } | null {
const tryRead = (p: string) => {
try {
const stat = fs.statSync(p);
if (stat.isFile()) return { content: fs.readFileSync(p, "utf-8"), resolvedPath: p };
} catch {}
return null;
};
const candidates: string[] = [];
// 1. Project-local standard agents (cwd-reachable only).
if (cwd) candidates.push(path.join(cwd, ".claude/agents", `${agentType}.md`));
// 2. User-level standard agents.
candidates.push(path.join(CLAUDE_DIR, "agents", `${agentType}.md`));
// 3. Namespaced types like "qa:poc-writer" → ns="qa", name="poc-writer".
const m = agentType.match(/^([^:]+):(.+)$/);
if (m) {
const ns = m[1];
const name = m[2];
// 3a. Project-local plugins (cwd-reachable only).
if (cwd) {
candidates.push(path.join(cwd, ".claude/plugins", ns, "agents", `${name}.md`));
candidates.push(path.join(cwd, ".claude/skills", ns, "agents", `${name}.md`));
}
// 3b. Marketplace-installed plugin agents.
const cacheRoot = path.join(CLAUDE_DIR, "plugins/cache");
try {
for (const market of fs.readdirSync(cacheRoot)) {
const pluginDir = path.join(cacheRoot, market, ns);
try {
for (const ver of fs.readdirSync(pluginDir)) {
candidates.push(path.join(pluginDir, ver, "agents", `${name}.md`));
}
} catch {}
}
} catch {}
}
for (const c of candidates) {
const hit = tryRead(c);
if (hit) return hit;
}
return null;
}
// Get subagent conversation
app.get("/api/projects/:project/sessions/:session/agents/:agentId", (req, res) => {
try {
const subagentsDir = path.join(
PROJECTS_DIR,
req.params.project,
req.params.session,
"subagents"
);
const agentFile = path.join(subagentsDir, `agent-${req.params.agentId}.jsonl`);
const metaFile = path.join(subagentsDir, `agent-${req.params.agentId}.meta.json`);
const lines = fs.readFileSync(agentFile, "utf-8").split("\n").filter(Boolean);
const records = lines.map((line) => {
try { return JSON.parse(line); } catch { return null; }
}).filter(Boolean);
let meta = null;
try { meta = JSON.parse(fs.readFileSync(metaFile, "utf-8")); } catch {}
let cwd = "";
for (const r of records) {
if (r?.cwd) { cwd = r.cwd; break; }
}
const persona = meta?.agentType ? resolvePersona(cwd, meta.agentType) : null;
res.json({ meta, records, persona });
} catch {
res.status(404).json({ error: "Agent not found" });
}
});
// List available subagents for a session
app.get("/api/projects/:project/sessions/:session/agents", (req, res) => {
try {
const subagentsDir = path.join(
PROJECTS_DIR,
req.params.project,
req.params.session,
"subagents"
);
if (!fs.existsSync(subagentsDir)) { res.json([]); return; }
const agents = fs.readdirSync(subagentsDir)
.filter((f) => f.endsWith(".meta.json"))
.map((f) => {
const id = f.replace("agent-", "").replace(".meta.json", "");
let meta = null;
try { meta = JSON.parse(fs.readFileSync(path.join(subagentsDir, f), "utf-8")); } catch {}
const jsonlFile = path.join(subagentsDir, `agent-${id}.jsonl`);
let lineCount = 0;
try {
lineCount = fs.readFileSync(jsonlFile, "utf-8").split("\n").filter(Boolean).length;
} catch {}
return { id, ...meta, lineCount };
});
res.json(agents);
} catch {
res.json([]);
}
});
app.delete("/api/projects/:project/sessions", (req, res) => {
try {
const projectDir = path.join(PROJECTS_DIR, req.params.project);
const files = fs.readdirSync(projectDir);
for (const f of files) {
const fp = path.join(projectDir, f);
if (f.endsWith(".jsonl")) {
fs.unlinkSync(fp);
const companionDir = path.join(projectDir, f.replace(".jsonl", ""));
if (fs.existsSync(companionDir) && fs.statSync(companionDir).isDirectory()) {
fs.rmSync(companionDir, { recursive: true });
}
}
}
res.json({ ok: true });
} catch {
res.status(500).json({ error: "Failed to delete sessions" });
}
});
app.delete("/api/projects/:project/sessions/:session", (req, res) => {
try {
const filePath = path.join(
PROJECTS_DIR,
req.params.project,
req.params.session + ".jsonl"
);
fs.unlinkSync(filePath);
// Also remove companion directory if exists
const dirPath = path.join(PROJECTS_DIR, req.params.project, req.params.session);
if (fs.existsSync(dirPath) && fs.statSync(dirPath).isDirectory()) {
fs.rmSync(dirPath, { recursive: true });
}
res.json({ ok: true });
} catch {
res.status(404).json({ error: "Session not found" });
}
});
// Serve static frontend in production
const distPath = path.join(import.meta.dirname, "dist");
if (fs.existsSync(distPath)) {
app.use(express.static(distPath));
app.get("/{*splat}", (_req, res) => {
res.sendFile(path.join(distPath, "index.html"));
});
}
const PORT = parseInt(process.env.PORT || "3099");
app.listen(PORT, "0.0.0.0", () => {
console.log(`Claude Trace Viewer running on http://localhost:${PORT}`);
});