feat: add macOS floating HUD popup for token metrics
This commit is contained in:
@@ -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)
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user