pi-ext
bugabingas pi extensions
owned by bugabinga
refactor(tune): isolate dashboard server
b668205fc54926dcc10f5a1c81842a2613b7596d
Oliver Jan Krylow <oliver@bugabinga.net> ·
unsigned
Browse filesCompare with HEAD
Changed files (1)
extensions/tune/index.ts
Modified 11 11 import { randomBytes }
from "node:crypto";
12 12 import * as fs
from "node:fs";
13 13 import { createWriteStream }
from "node:fs";
14 -import { createServer, type Server, type ServerResponse }
from "node:http";
15 14 import { tmpdir }
from "node:os";
16 15 import * as path
from "node:path";
17 16 import { fileURLToPath }
from "node:url";
41 40 buildTuneCompactionSummary,
42 41 tuneSummaryPathsFor,
43 42 } from
"./compaction.ts";
43 +import { startTuneDashboard, type TuneDashboard }
from "./dashboard.ts";
45 45 appendHookLogEntryIfConfigured,
3026 - let dashboardServer
: Server | null = null;
3027 - let dashboardServerPort
: number | null = null;
3028 - let dashboardIndexPath
: string | null = null;
3029 - let dashboardJsonlPath
: string | null = null;
3030 - const dashboardSseClients
= new Set<
ServerResponse>();
3026 + let dashboardServer
: TuneDashboard | null = null;
3032 3028 const TITLE_PLACEHOLDER
= "__TUNE_TITLE__";
3044 3040 function broadcastDashboardUpdate(_cwd
: string)
: void {
3045 - if (
!dashboardServer)
return;
3046 - for (
const res
of dashboardSseClients) {
3048 - res.
write(
"event: jsonl-updated\n");
3049 - res.
write(
`data: ${Date.now()}\n\n`);
3051 - dashboardSseClients.
delete(res);
3041 + dashboardServer?.
broadcast();
3056 3044 function stopDashboardServer()
: void {
3057 - for (
const client
of dashboardSseClients) {
3064 - dashboardSseClients.
clear();
3065 - if (dashboardServer) {
3067 - dashboardServer.
close();
3045 + dashboardServer?.
close();
3072 3046 dashboardServer
= null;
3073 - dashboardServerPort
= null;
3074 - dashboardIndexPath
= null;
3075 - dashboardJsonlPath
= null;
3078 3049 function openInBrowser(url
: string)
: void {
3111 3082 const jsonlContent
= fs.
readFileSync(jsonlPath,
"utf-8").
trim();
3112 3083 const sessionName
= extractTuneSessionName(jsonlContent);
3113 3084 const html
= injectDataIntoTemplate(template, sessionName);
3114 - const exportDir
= fs.
mkdtempSync(
3115 - path.
join(
tmpdir(),
"pi-tune-dashboard-"),
3085 + if (dashboardServer) dashboardServer.
update(html, jsonlPath);
3086 + else dashboardServer
= await startTuneDashboard(html, jsonlPath);
3088 + openInBrowser(dashboardServer.url);
3090 + `Dashboard at ${dashboardServer.url} (live updates)`,
3117 - const dest
= path.
join(exportDir,
"index.html");
3118 - fs.
writeFileSync(dest, html);
3119 - dashboardIndexPath
= dest;
3120 - dashboardJsonlPath
= jsonlPath;
3122 - if (
!dashboardServer) {
3123 - const server
= createServer((req, res)
=> {
3124 - const url
= new URL(req.url
?? "/",
"http://127.0.0.1");
3125 - if (url.
pathname === "/events") {
3126 - res.
writeHead(
200, {
3127 - "Content-Type":
"text/event-stream",
3128 - "Cache-Control":
"no-cache",
3129 - Connection:
"keep-alive",
3131 - res.
write(
"retry: 1000\n\n");
3132 - dashboardSseClients.
add(res);
3133 - res.
on(
"close", ()
=> dashboardSseClients.
delete(res));
3137 - url.
pathname === "/" 3138 - ? dashboardIndexPath
3139 - : url.
pathname === "/session.jsonl" 3140 - ? dashboardJsonlPath
3143 - res.
writeHead(
404);
3147 - fs.
readFile(filePath, (err, data)
=> {
3149 - res.
writeHead(
404);
3153 - res.
writeHead(
200, {
"Content-Type":
"text/html; charset=utf-8" });
3158 - const port
= await new Promise<
number>((resolve, reject)
=> {
3159 - server.
listen(
0,
"127.0.0.1", ()
=> {
3160 - const addr
= server.
address();
3161 - if (
!addr
|| typeof addr
=== "string") {
3162 - reject(
new Error(
"Failed to bind"));
3165 - resolve(addr.
port);
3167 - server.
on(
"error", reject);
3170 - dashboardServer
= server;
3171 - dashboardServerPort
= port;
3174 - const url
= `http://127.0.0.1:${dashboardServerPort}`;
3175 - openInBrowser(url);
3176 - ctx.ui.
notify(
`Dashboard at ${url} (live updates)`,
"info");
3177 3093 }
catch (error) {
3179 3095 `Export failed: ${error instanceof Error ? error.message : String(error)}`,