From f1a5f89e0facf479d1b6401a215f7e9beb471906 Mon Sep 17 00:00:00 2001 From: c4ch3c4d3 <23181631+c4ch3c4d3@users.noreply.github.com> Date: Fri, 14 Aug 2026 12:54:34 -0600 Subject: [PATCH] feat: expose usage stats via shared runtime StatsCache --- src/lib/runtime.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/lib/runtime.ts b/src/lib/runtime.ts index 2f68b0f..2257c74 100644 --- a/src/lib/runtime.ts +++ b/src/lib/runtime.ts @@ -1,6 +1,8 @@ import { EventFeed } from "./event-feed"; import { InflightTracker } from "./inflight-tracker"; import { MetricsPoller } from "./metrics-poller"; +import { StatsCache } from "./stats-cache"; +import { type UsageStats } from "./stats"; import { type LlamaSwapConfig } from "./util"; class Runtime { @@ -9,6 +11,8 @@ class Runtime { private cfg?: LlamaSwapConfig; private feed?: EventFeed; private poller?: MetricsPoller; + private statsCache?: StatsCache; + private statsUnsub?: () => void; private listeners = new Set<() => void>(); ensureConnections(cfg: LlamaSwapConfig): void { @@ -18,11 +22,15 @@ class Runtime { this.poller?.stop(); this.feed = undefined; this.poller = undefined; + this.statsUnsub?.(); + this.statsCache = undefined; + this.statsUnsub = undefined; this.cfg = cfg; } if (!this.feed) { this.feed = new EventFeed(cfg, (ev) => { this.tracker.apply(ev); + if (ev.type === "activity") this.statsCache?.scheduleRefresh(); this.emit(); }); this.feed.setStatusHandler((connected) => { @@ -36,12 +44,25 @@ class Runtime { this.poller.on(() => this.emit()); this.poller.start(); } + if (!this.statsCache) { + this.statsCache = new StatsCache(cfg); + this.statsUnsub = this.statsCache.onChange(() => this.emit()); + } } get pollerInstance(): MetricsPoller | undefined { return this.poller; } + getStats(modelId: string): UsageStats | undefined { + return this.statsCache?.get(modelId); + } + + watchStats(modelId: string): () => void { + this.statsCache?.register(modelId); + return () => this.statsCache?.unregister(modelId); + } + subscribe(listener: () => void): () => void { this.listeners.add(listener); return () => {