import * as http from "node:http";
import { getActiveAccount, listAccounts, loadConfigV2 } from "../config/v2";
const DASHBOARD_HTML = `
ImBIOS Dashboard
ImBIOS Dashboard v2.0
Connected
Quick Actions
`;
export function startDashboard(): void {
const config = loadConfigV2();
if (!config.dashboard.enabled) {
console.log("Dashboard is disabled. Enable with: imbios dashboard start");
return;
}
const server = http.createServer((req, res) => {
if (req.url === "/" || req.url === "/index.html") {
res.writeHead(200, { "Content-Type": "text/html" });
res.end(DASHBOARD_HTML);
return;
}
if (req.url === "/api/status") {
const activeAccount = getActiveAccount();
const accounts = listAccounts();
res.writeHead(200, { "Content-Type": "application/json" });
const usage = { used: 0, limit: 1000, remaining: 1000 };
if (activeAccount) {
res.end(
JSON.stringify({
activeAccount: activeAccount.id,
usage,
accounts: accounts.map((a) => ({
id: a.id,
name: a.name,
provider: a.provider,
})),
alerts: [],
})
);
return;
}
res.end(JSON.stringify({ error: "No active account" }));
}
if (req.url === "/api/rotate" && req.method === "POST") {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ success: true, message: "Key rotated" }));
return;
}
res.writeHead(404);
res.end("Not found");
});
const { host, port } = config.dashboard;
server.listen(port, host, () => {
console.log(`ImBIOS Dashboard running at http://${host}:${port}`);
console.log(`Auth token: ${config.dashboard.authToken}`);
});
}
export function stopDashboard(): void {
console.log("Dashboard stopped.");
}