feat: add macOS floating HUD popup for token metrics

This commit is contained in:
Andrew Emelyaneko
2026-10-31 22:29:14 +03:00
parent 45d06f2efb
commit 22bf9bae75
5 changed files with 398 additions and 4 deletions
+16 -3
View File
@@ -3,6 +3,7 @@ import { loadConfig } from "./config"
import { routeEvent } from "./events"
import { startServer, type SpeedServer } from "./server"
import { Tracker } from "./tracker"
import { MacOSPopup } from "./popup"
import type { SpeedSnapshot, SessionStats } from "./types"
interface SharedServer {
@@ -71,12 +72,19 @@ export const TokenSpeedPlugin: Plugin = async ({ client }) => {
})
let server: SpeedServer | null = null
let popup: MacOSPopup | null = null
try {
server = acquireServer(config.host, config.port, () => tracker.snapshot(), {
getAllSessionStats: () => tracker.getAllSessionStats(),
getSessionStats: (sessionID) => tracker.getSessionStats(sessionID),
getCurrentSessionStats: () => tracker.getCurrentSessionStats(),
})
popup = new MacOSPopup({
baseUrl: server.url,
hud: config.macosHud,
notifications: config.macosNotifications,
notifyMinIntervalMs: config.macosNotifyMinIntervalMs,
})
await client.app
.log({
body: {
@@ -98,9 +106,13 @@ export const TokenSpeedPlugin: Plugin = async ({ client }) => {
.catch(() => {})
}
const unsubscribe = server
? tracker.onChange((snapshot) => server?.publish(snapshot))
: () => {}
const onSnapshot = (snapshot: SpeedSnapshot) => {
server?.publish(snapshot)
popup?.update(snapshot)
}
const unsubscribe =
server || popup ? tracker.onChange(onSnapshot) : () => {}
if (popup) popup.start(tracker.snapshot())
const ticker = setInterval(() => tracker.refresh(), 1000)
return {
@@ -110,6 +122,7 @@ export const TokenSpeedPlugin: Plugin = async ({ client }) => {
dispose: async () => {
clearInterval(ticker)
unsubscribe()
popup?.dispose()
releaseServer(config.host, config.port)
},
}