Lines 1-60javascript
1import fs from "node:fs";
2import os from "node:os";
3import path from "node:path";
4import { spawnSync } from "node:child_process";
5import { DatabaseSync } from "node:sqlite";
HighChild Process
Package source references child process execution.
install/burnboard-agent.mjsView on unpkg · L3 6async function openSqlite(file) {
7 const database = new DatabaseSync(file, { readOnly: true });
8 return { all: (sql) => database.prepare(sql).all(), close: () => database.close() };
11const home = os.homedir();
12const root = path.join(home, ".burnboard");
13const dryRun = process.argv.includes("--dry-run");
14const jsonOutput = process.argv.includes("--json");
15const onlyTool = process.argv.find((argument) => argument.startsWith("--only="))?.slice(7);
16const config = dryRun ? null : JSON.parse(fs.readFileSync(path.join(root, "config.json"), "utf8").replace(/^\uFEFF/, ""));
17const buckets = new Map();
19function showNotification(title, message, destination) {
20 title = String(title || "BurnBoard").slice(0, 100);
21 message = String(message || "").slice(0, 200);
25 const candidate = new URL(destination);
26 if (candidate.protocol === "https:" || (candidate.protocol === "http:" && ["localhost","127.0.0.1"].includes(candidate.hostname))) notificationUrl = candidate.origin;
28 if (!notificationUrl) return;
29 if (process.platform === "win32") {
31[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
32[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] > $null
33$title = [Security.SecurityElement]::Escape($env:BURNBOARD_NOTIFICATION_TITLE)
34$message = [Security.SecurityElement]::Escape($env:BURNBOARD_NOTIFICATION_MESSAGE)
35$url = [Security.SecurityElement]::Escape($env:BURNBOARD_NOTIFICATION_URL)
36$xml = New-Object Windows.Data.Xml.Dom.XmlDocument
37$xml.LoadXml("<toast activationType='protocol' launch='$url'><visual><binding template='ToastGeneric'><text>$title</text><text>$message</text></binding></visual></toast>")
38$toast = New-Object Windows.UI.Notifications.ToastNotification $xml
39[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier('BurnBoard.Desktop').Show($toast)
41 spawnSync("powershell.exe", ["-NoLogo", "-NoProfile", "-NonInteractive", "-EncodedCommand", Buffer.from(script, "utf16le").toString("base64")], {
42 stdio:"ignore", env:{ ...process.env, BURNBOARD_NOTIFICATION_TITLE:title, BURNBOARD_NOTIFICATION_MESSAGE:message, BURNBOARD_NOTIFICATION_URL:notificationUrl },
44 } else if (process.platform === "darwin") {
45 spawnSync("osascript", ["-e", "display notification (system attribute \"BURNBOARD_NOTIFICATION_MESSAGE\") with title (system attribute \"BURNBOARD_NOTIFICATION_TITLE\")"], {
46 stdio:"ignore", env:{ ...process.env, BURNBOARD_NOTIFICATION_TITLE:title, BURNBOARD_NOTIFICATION_MESSAGE:message },
49 spawnSync("notify-send", [title, message], { stdio:"ignore" });
53function localDate(value) {
54 const parts = new Intl.DateTimeFormat("en-CA", { timeZone: "Asia/Kolkata", year: "numeric", month: "2-digit", day: "2-digit" }).formatToParts(new Date(value));
55 const get = (type) => parts.find((part) => part.type === type)?.value;
56 return `${get("year")}-${get("month")}-${get("day")}`;
59function addUsage({ recordedAt, tool, model, input = 0, output = 0, cacheRead = 0, cacheCreation = 0 }) {
60 const values = [input, output, cacheRead, cacheCreation].map(Number);