fix: restore plugin integration after popup refactor
This commit is contained in:
@@ -0,0 +1,131 @@
|
|||||||
|
import type { Plugin } from "@opencode-ai/plugin"
|
||||||
|
import { loadConfig } from "./config"
|
||||||
|
import { routeEvent } from "./events"
|
||||||
|
import { startServer, type SpeedServer } from "./server"
|
||||||
|
import { Tracker } from "./tracker"
|
||||||
|
import { acquirePopup, releasePopup } from "./popup"
|
||||||
|
import type { SpeedSnapshot, SessionStats } from "./types"
|
||||||
|
|
||||||
|
interface SharedServer {
|
||||||
|
server: SpeedServer
|
||||||
|
refs: number
|
||||||
|
}
|
||||||
|
|
||||||
|
const SHARED_KEY = Symbol.for("plagin-tokens.sharedServers")
|
||||||
|
|
||||||
|
function registry(): Map<string, SharedServer> {
|
||||||
|
const g = globalThis as unknown as Record<PropertyKey, unknown>
|
||||||
|
let reg = g[SHARED_KEY] as Map<string, SharedServer> | undefined
|
||||||
|
if (!reg) {
|
||||||
|
reg = new Map()
|
||||||
|
g[SHARED_KEY] = reg
|
||||||
|
}
|
||||||
|
return reg
|
||||||
|
}
|
||||||
|
|
||||||
|
function acquireServer(
|
||||||
|
host: string,
|
||||||
|
port: number,
|
||||||
|
getSnapshot: () => SpeedSnapshot,
|
||||||
|
providers: {
|
||||||
|
getAllSessionStats?: () => SessionStats[]
|
||||||
|
getSessionStats?: (sessionID: string) => SessionStats | null
|
||||||
|
getCurrentSessionStats?: () => SessionStats | null
|
||||||
|
} = {},
|
||||||
|
): SpeedServer {
|
||||||
|
const reg = registry()
|
||||||
|
const key = `${host}:${port}`
|
||||||
|
const existing = reg.get(key)
|
||||||
|
if (existing) {
|
||||||
|
existing.refs += 1
|
||||||
|
return existing.server
|
||||||
|
}
|
||||||
|
const server = startServer({
|
||||||
|
host,
|
||||||
|
port,
|
||||||
|
getSnapshot,
|
||||||
|
getAllSessionStats: providers.getAllSessionStats,
|
||||||
|
getSessionStats: providers.getSessionStats,
|
||||||
|
getCurrentSessionStats: providers.getCurrentSessionStats,
|
||||||
|
})
|
||||||
|
reg.set(key, { server, refs: 1 })
|
||||||
|
return server
|
||||||
|
}
|
||||||
|
|
||||||
|
function releaseServer(host: string, port: number): void {
|
||||||
|
const reg = registry()
|
||||||
|
const key = `${host}:${port}`
|
||||||
|
const entry = reg.get(key)
|
||||||
|
if (!entry) return
|
||||||
|
entry.refs -= 1
|
||||||
|
if (entry.refs <= 0) {
|
||||||
|
entry.server.stop()
|
||||||
|
reg.delete(key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const TokenSpeedPlugin: Plugin = async ({ client }) => {
|
||||||
|
const config = loadConfig()
|
||||||
|
const tracker = new Tracker({
|
||||||
|
windowMs: config.windowMs,
|
||||||
|
idleTimeoutMs: config.idleTimeoutMs,
|
||||||
|
})
|
||||||
|
|
||||||
|
let server: SpeedServer | null = null
|
||||||
|
let popup: any = null
|
||||||
|
try {
|
||||||
|
server = acquireServer(config.host, config.port, () => tracker.snapshot(), {
|
||||||
|
getAllSessionStats: () => tracker.getAllSessionStats(),
|
||||||
|
getSessionStats: (sessionID) => tracker.getSessionStats(sessionID),
|
||||||
|
getCurrentSessionStats: () => tracker.getCurrentSessionStats(),
|
||||||
|
})
|
||||||
|
popup = acquirePopup({
|
||||||
|
baseUrl: server.url,
|
||||||
|
hud: config.macosHud,
|
||||||
|
notifications: config.macosNotifications,
|
||||||
|
notifyMinIntervalMs: config.macosNotifyMinIntervalMs,
|
||||||
|
})
|
||||||
|
await client.app
|
||||||
|
.log({
|
||||||
|
body: {
|
||||||
|
service: "plagin-tokens",
|
||||||
|
level: "info",
|
||||||
|
message: `token speed overlay on ${server.url}`,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.catch(() => {})
|
||||||
|
} catch (error) {
|
||||||
|
await client.app
|
||||||
|
.log({
|
||||||
|
body: {
|
||||||
|
service: "plagin-tokens",
|
||||||
|
level: "error",
|
||||||
|
message: `failed to start overlay server: ${String(error)}`,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
.catch(() => {})
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
event: async ({ event }) => {
|
||||||
|
routeEvent(tracker, event)
|
||||||
|
},
|
||||||
|
dispose: async () => {
|
||||||
|
clearInterval(ticker)
|
||||||
|
unsubscribe()
|
||||||
|
releasePopup()
|
||||||
|
releaseServer(config.host, config.port)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default TokenSpeedPlugin
|
||||||
|
|||||||
Reference in New Issue
Block a user