{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 811
Expand file tree
/
Copy pathweb-dashboard.ts
More file actions
446 lines (412 loc) · 20.2 KB
/
Copy pathweb-dashboard.ts
File metadata and controls
446 lines (412 loc) · 20.2 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
import { createServer, type Server } from 'http'
import { exec } from 'child_process'
import { readFile } from 'fs/promises'
import { existsSync } from 'fs'
import { join, normalize, extname, dirname, sep } from 'path'
import { fileURLToPath } from 'url'
import { AddressInfo } from 'net'
import { hostname } from 'os'
import { loadPricing } from './models.js'
import { buildMenubarPayloadForRange } from './usage-aggregator.js'
import type { MenubarPayload } from './menubar-json.js'
import { periodInfoFromQuery, UsageQueryError } from './cli-date.js'
import { pullDevices, linkRemote } from './sharing/host.js'
import { browse } from './sharing/discovery.js'
import { loadOrCreateIdentity } from './sharing/identity.js'
import { pairingCode } from './sharing/pairing.js'
import { getSharingDir, loadRemotes, loadShareAlways, saveShareAlways } from './sharing/store.js'
import { ShareController } from './sharing/share-controller.js'
import { sanitizeForSharing } from './sharing/sanitize.js'
import { buildContextTree, findClaudeSession, listRecentTitledSessions, snapshotRows, type ContextTreeResult, type SessionRef } from './context-tree.js'
import { buildCodexContextTree, findCodexSession, listRecentCodexSessions } from './context-tree-codex.js'
function readBody(req: import('http').IncomingMessage): Promise<string> {
return new Promise((resolve, reject) => {
let body = ''
req.on('data', (c) => {
body += c
if (body.length > 1_000_000) reject(new Error('request body too large'))
})
req.on('end', () => resolve(body))
req.on('error', reject)
})
}
function writeJsonError(res: import('http').ServerResponse, status: number, error: string): void {
res.writeHead(status, { 'content-type': 'application/json; charset=utf-8' })
res.end(JSON.stringify({ error }))
}
// Cap on the cached local payload, matched to the parser's own session cache
// (parser.ts) so the assembled payload is never staler than its source data.
const LOCAL_PAYLOAD_TTL_MS = 180_000
const HERE = dirname(fileURLToPath(import.meta.url))
// Locate the built React dashboard (dist/dash). Works both when running from a
// published package (dist/dash next to the bundled CLI) and from source.
function resolveDashDir(): string | null {
const candidates = [
process.env['CODEBURN_DASH_DIR'],
join(HERE, 'dash'),
join(HERE, '..', 'dist', 'dash'),
join(HERE, '..', 'dash', 'dist'),
].filter(Boolean) as string[]
for (const dir of candidates) {
if (existsSync(join(dir, 'index.html'))) return dir
}
return null
}
const CONTENT_TYPES: Record<string, string> = {
'.html': 'text/html; charset=utf-8',
'.js': 'text/javascript; charset=utf-8',
'.css': 'text/css; charset=utf-8',
'.svg': 'image/svg+xml',
'.json': 'application/json; charset=utf-8',
'.woff2': 'font/woff2',
'.woff': 'font/woff',
'.png': 'image/png',
'.ico': 'image/x-icon',
'.map': 'application/json',
}
const NOT_BUILT_PAGE =
'<!doctype html><meta charset="utf-8">' +
'<body style="font-family:system-ui;background:#0a0a0b;color:#e7e7ea;padding:48px;line-height:1.6">' +
'<h2>Dashboard not built yet</h2>' +
'<p>Build the web UI once, then reload:</p>' +
'<pre style="background:#141417;padding:12px 16px;border-radius:8px;color:#ff8c42">cd dash && npm install && npm run build</pre>' +
'<p>The CLI keeps serving the live data API in the meantime.</p></body>'
function openBrowser(url: string): void {
const cmd = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'start ""' : 'xdg-open'
try {
exec(`${cmd} ${url}`)
} catch {
/* user can open it manually */
}
}
export function injectDashboardBootstrap(html: string, payload: unknown): string {
const json = JSON.stringify(payload)
if (json === undefined) throw new TypeError('dashboard bootstrap payload is not serializable')
// Keep the JSON safe at the boundary where it enters an HTML script. This is
// deliberately inside the helper so callers cannot forget the escape.
const safeJson = json.replace(/</g, String.fromCharCode(92) + 'u003c')
return html.replace('<script type="module"', () => `<script>window.__CODEBURN_BOOTSTRAP__=${safeJson}</script>\n <script type="module"`)
}
export async function runWebDashboard(opts: {
period: string
provider: string
from?: string
to?: string
project: string[]
exclude: string[]
port: number
open: boolean
}): Promise<Server> {
await loadPricing()
const dashDir = resolveDashDir()
// Sharing this device serves the SANITIZED aggregate (no project names/paths
// or per-session detail), unlike the local /api/usage which shows everything.
const shareGetUsage = async (q: { period?: string; from?: string; to?: string }) => {
const periodInfo = periodInfoFromQuery(q, opts.period)
return sanitizeForSharing(await buildMenubarPayloadForRange(periodInfo, { provider: 'all', optimize: false }))
}
const share = new ShareController(shareGetUsage)
if (await loadShareAlways()) await share.start(true).catch(() => {})
// The server is long-lived, so cache this machine's parsed payload (the CLI
// process cannot). Store the promise before any await so concurrent identical
// requests collapse into one parse instead of racing.
const localPayloadCache = new Map<string, { at: number; payload: Promise<MenubarPayload> }>()
const rebuildPayload = (key: string, period: string, provider: string, from?: string, to?: string): Promise<MenubarPayload> => {
const periodInfo = periodInfoFromQuery({ period, from, to }, opts.period)
const payload = buildMenubarPayloadForRange(periodInfo, { provider, project: opts.project, exclude: opts.exclude, optimize: false })
const now = Date.now()
localPayloadCache.set(key, { at: now, payload })
for (const [k, v] of localPayloadCache) if (now - v.at >= LOCAL_PAYLOAD_TTL_MS) localPayloadCache.delete(k)
void payload.catch(() => localPayloadCache.delete(key))
return payload
}
const getLocalPayload = (period: string, provider: string, from?: string, to?: string): Promise<MenubarPayload> => {
const key = `${period}|${provider}|${from ?? ''}|${to ?? ''}`
const hit = localPayloadCache.get(key)
if (hit && Date.now() - hit.at < LOCAL_PAYLOAD_TTL_MS) {
// Stale-while-revalidate: past 75% of the TTL, hand back the cached
// payload instantly and rebuild behind it, so the TTL expiry never
// lands its multi-second parse on a user's click.
if (Date.now() - hit.at > LOCAL_PAYLOAD_TTL_MS * 0.75) void rebuildPayload(key, period, provider, from, to).catch(() => {})
return hit.payload
}
return rebuildPayload(key, period, provider, from, to)
}
// Warm every period tab shortly after startup, sequentially, so the first
// click on 7d/30d/Month answers from the payload cache instead of paying a
// full parse. Lifetime is deliberately last-and-optional: it is the rarest
// tab and the costliest parse. Failures are ignored - a prefetch is never
// load-bearing.
const prefetchPeriods = async (): Promise<void> => {
for (const period of ['today', 'week', '30days', 'month', 'all', 'lifetime']) {
try { await getLocalPayload(period, opts.provider, opts.from, opts.to) } catch { /* not load-bearing */ }
}
}
setTimeout(() => { void prefetchPeriods() }, 500)
// Context trees re-read a whole transcript (up to 100MB), so cache each by
// file version. Keyed on mtime: an active session invalidates itself.
const contextTreeCache = new Map<string, Promise<ContextTreeResult>>()
const getContextTree = (provider: 'claude' | 'codex', ref: SessionRef): Promise<ContextTreeResult> => {
const key = `${provider}:${ref.sessionId}:${ref.mtimeMs}`
const hit = contextTreeCache.get(key)
if (hit) {
// Re-insert so eviction is LRU rather than insertion order.
contextTreeCache.delete(key)
contextTreeCache.set(key, hit)
return hit
}
const tree = provider === 'claude' ? buildContextTree(ref) : buildCodexContextTree(ref)
contextTreeCache.set(key, tree)
void tree.catch(() => contextTreeCache.delete(key))
while (contextTreeCache.size > 12) {
const oldest = contextTreeCache.keys().next().value
if (oldest === undefined) break
contextTreeCache.delete(oldest)
}
return tree
}
// Embed this machine's prewarmed payload in index.html for an instant first
// paint with no data round-trip. Only the local device is inlined: no remote
// network wait, and paired devices stream in via the live fetch right after.
const serveIndexHtml = async (res: import('http').ServerResponse, filePath: string): Promise<void> => {
const html = await readFile(filePath, 'utf8')
const payload = await getLocalPayload(opts.period, opts.provider, opts.from, opts.to)
const devices = [{ id: 'local', name: hostname(), local: true, payload }]
const injected = injectDashboardBootstrap(html, { devices })
res.writeHead(200, { 'content-type': 'text/html; charset=utf-8', 'cache-control': 'no-store' })
res.end(injected)
}
const server = createServer(async (req, res) => {
try {
const url = new URL(req.url ?? '/', 'http://localhost')
// Loopback-only server. Reject any request not addressed to localhost
// (defeats DNS rebinding, which would otherwise let a website you visit
// read your local usage) and any cross-origin request (CSRF). The local
// payload is unsanitized, so this guard is what keeps it on your machine.
const reqHost = (req.headers.host ?? '').replace(/:\d+$/, '')
const loopback = reqHost === '127.0.0.1' || reqHost === 'localhost' || reqHost === '::1' || reqHost === '[::1]'
const origin = req.headers.origin
const originOk = !origin || /^https?:\/\/(127\.0\.0\.1|localhost|\[::1\])(:\d+)?$/.test(origin)
if (!loopback || !originOk) {
res.writeHead(403, { 'content-type': 'text/plain' })
res.end('Forbidden')
return
}
if (url.pathname === '/api/usage') {
const period = url.searchParams.get('period') ?? opts.period
const provider = url.searchParams.get('provider') ?? opts.provider
const from = url.searchParams.get('from') ?? opts.from
const to = url.searchParams.get('to') ?? opts.to
let payload
try {
payload = await getLocalPayload(period, provider, from, to)
} catch (err) {
if (!(err instanceof UsageQueryError)) throw err
writeJsonError(res, 400, err.message)
return
}
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
res.end(JSON.stringify(payload))
return
}
// This machine plus every paired device, each kept separate. Remote
// payloads arrive already sanitized (aggregate numbers only).
if (url.pathname === '/api/devices') {
const period = url.searchParams.get('period') ?? opts.period
const provider = url.searchParams.get('provider') ?? opts.provider
const from = url.searchParams.get('from') ?? opts.from
const to = url.searchParams.get('to') ?? opts.to
let results
try {
results = await pullDevices(() => getLocalPayload(period, provider, from, to), { period, from, to }, hostname(), {})
} catch (err) {
if (!(err instanceof UsageQueryError)) throw err
writeJsonError(res, 400, err.message)
return
}
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
res.end(JSON.stringify({ devices: results }))
return
}
// This device's own identity (name + fingerprint) for the pairing UI.
if (url.pathname === '/api/identity') {
const id = await loadOrCreateIdentity(getSharingDir())
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
res.end(JSON.stringify({ name: id.name, fingerprint: id.fingerprint }))
return
}
// Discover devices currently sharing on the local network (mDNS). Each
// carries the confirm code to match, and whether it is already paired.
if (url.pathname === '/api/devices/scan') {
const dir = getSharingDir()
const id = await loadOrCreateIdentity(dir)
const pairedFps = new Set((await loadRemotes(dir)).map((r) => r.fingerprint))
const found = await browse(2500)
const list = found
.filter((d) => d.fingerprint !== id.fingerprint)
.map((d) => ({
name: d.name,
host: d.host,
port: d.port,
fingerprint: d.fingerprint,
code: pairingCode(id.fingerprint, d.fingerprint),
paired: pairedFps.has(d.fingerprint),
}))
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
res.end(JSON.stringify({ found: list }))
return
}
// Pair with a chosen discovered device. Blocks until the other device
// approves (or declines / times out), then stores the link.
if (url.pathname === '/api/devices/pair' && req.method === 'POST') {
if (!(req.headers['content-type'] ?? '').includes('application/json')) {
res.writeHead(415, { 'content-type': 'application/json' })
res.end(JSON.stringify({ ok: false, error: 'content-type must be application/json' }))
return
}
const body = JSON.parse((await readBody(req)) || '{}') as { name: string; host: string; port: number; fingerprint: string }
try {
const device = await linkRemote(body)
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8' })
res.end(JSON.stringify({ ok: true, name: device.name }))
} catch (err) {
res.writeHead(409, { 'content-type': 'application/json; charset=utf-8' })
res.end(JSON.stringify({ ok: false, error: err instanceof Error ? err.message : String(err) }))
}
return
}
// Share-this-device controls. Status carries the pending pairing requests
// so the SPA can poll one endpoint and surface approvals in the browser.
if (url.pathname === '/api/share/status') {
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
res.end(JSON.stringify(await share.status()))
return
}
if (url.pathname === '/api/share/start' && req.method === 'POST') {
const body = JSON.parse((await readBody(req)) || '{}') as { always?: boolean }
let startError: string | undefined
try {
await share.start(!!body.always)
await saveShareAlways(!!body.always)
} catch (err) {
// e.g. EADDRINUSE when a CLI `codeburn share` already holds the port.
startError = err instanceof Error ? err.message : String(err)
}
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8' })
res.end(JSON.stringify({ ...(await share.status()), error: startError }))
return
}
if (url.pathname === '/api/share/stop' && req.method === 'POST') {
await share.stop()
await saveShareAlways(false)
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8' })
res.end(JSON.stringify(await share.status()))
return
}
// Session ids are resolved against the discovered session files only,
// never joined into a path from user input, and responses never carry
// local file paths.
if (url.pathname === '/api/context/sessions') {
const provider = url.searchParams.get('provider') ?? 'claude'
if (provider !== 'claude' && provider !== 'codex') {
writeJsonError(res, 400, 'provider must be claude or codex')
return
}
const refs = provider === 'claude' ? await listRecentTitledSessions(15) : await listRecentCodexSessions(15)
const sessions = refs.map((r) => ({ provider, sessionId: r.sessionId, project: r.project, title: r.title, mtimeMs: r.mtimeMs, sizeBytes: r.sizeBytes }))
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
res.end(JSON.stringify({ sessions }))
return
}
if (url.pathname === '/api/context/tree') {
const provider = url.searchParams.get('provider') ?? 'claude'
const id = url.searchParams.get('id') ?? ''
if ((provider !== 'claude' && provider !== 'codex') || !id) {
writeJsonError(res, 400, 'provider (claude|codex) and id are required')
return
}
// findClaudeSession/findCodexSession resolve an id prefix the same way
// `codeburn context <session>` does (see context-tree.ts's resolveSession) -
// trust that match instead of re-requiring the full id here, so a prefix
// that works on the CLI works through the API too.
const ref = provider === 'claude' ? await findClaudeSession(id) : await findCodexSession(id)
if (!ref) {
writeJsonError(res, 404, `no ${provider} session ${id}`)
return
}
const tree = await getContextTree(provider, ref)
const payload = {
...tree,
session: { sessionId: tree.session.sessionId, project: tree.session.project, mtimeMs: tree.session.mtimeMs, sizeBytes: tree.session.sizeBytes },
effectiveRows: snapshotRows(tree.effective),
fullRows: snapshotRows(tree.full),
}
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
res.end(JSON.stringify(payload))
return
}
if (url.pathname === '/api/share/approve' && req.method === 'POST') {
const body = JSON.parse((await readBody(req)) || '{}') as { id?: string; approve?: boolean }
const ok = typeof body.id === 'string' && share.resolvePending(body.id, !!body.approve)
res.writeHead(200, { 'content-type': 'application/json; charset=utf-8' })
res.end(JSON.stringify({ ok }))
return
}
if (!dashDir) {
res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' })
res.end(NOT_BUILT_PAGE)
return
}
let pathname = decodeURIComponent(url.pathname)
if (pathname === '/' || pathname === '') pathname = '/index.html'
const filePath = normalize(join(dashDir, pathname))
if (filePath !== dashDir && !filePath.startsWith(dashDir + sep)) {
res.writeHead(403)
res.end('Forbidden')
return
}
try {
if (extname(filePath) === '.html') {
await serveIndexHtml(res, filePath)
} else {
const buf = await readFile(filePath)
res.writeHead(200, { 'content-type': CONTENT_TYPES[extname(filePath)] ?? 'application/octet-stream' })
res.end(buf)
}
} catch {
// Unknown path: serve index.html so the SPA can route it.
await serveIndexHtml(res, join(dashDir, 'index.html'))
}
} catch (err) {
res.writeHead(500, { 'content-type': 'application/json' })
res.end(JSON.stringify({ error: err instanceof Error ? err.message : String(err) }))
}
})
const port = await new Promise<number>((resolve, reject) => {
server.once('error', (err: NodeJS.ErrnoException) => {
if (err.code === 'EADDRINUSE') {
server.listen(0, '127.0.0.1', () => resolve((server.address() as AddressInfo).port))
} else {
reject(err)
}
})
server.listen(opts.port, '127.0.0.1', () => resolve((server.address() as AddressInfo).port))
})
// Durable handler so a post-bind socket error never crashes the process.
server.on('error', () => {})
void Promise.resolve().then(() => getLocalPayload(opts.period, opts.provider, opts.from, opts.to)).catch(() => {})
const url = `http://127.0.0.1:${port}`
if (!dashDir) {
process.stdout.write(`\n Dashboard UI is not built. Run: cd dash && npm install && npm run build\n`)
}
process.stdout.write(`\n CodeBurn dashboard at ${url}\n Press Ctrl+C to stop.\n\n`)
if (opts.open) openBrowser(url)
// Withdraw the mDNS advertisement and close the share server cleanly on exit.
process.on('SIGINT', () => {
void share.stop().finally(() => process.exit(0))
})
return server
await new Promise<never>(() => {
/* run until interrupted */
})
}
You can’t perform that action at this time.
