@troed/oc-ls-stats 1.1.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +11 -6
  2. package/package.json +1 -1
  3. package/tui.tsx +49 -76
package/README.md CHANGED
@@ -55,15 +55,20 @@ Every 500ms, the plugin polls `GET /slots?model=<model>` on each discovered serv
55
55
 
56
56
  ### State Classification
57
57
 
58
- Each slot is classified as prefill or generation based on the `n_decoded` counter in `next_token[0]`. The plugin tracks a per-slot baseline value:
58
+ Each processing slot is classified as prefill (PP) or generation (TG) by comparing `n_decoded` and `n_prompt_tokens` against per-slot/per-task baselines. The plugin tracks two baseline maps keyed by slot ID:
59
59
 
60
- 1. When a slot first appears as processing, the current `n_decoded` is recorded as the baseline with `hasIncreased = false`.
61
- 2. If `n_decoded <= baseline` and `hasIncreased` is false, the slot is classified as prefilling.
62
- 3. If `n_decoded > baseline`, `hasIncreased` is set to true and the slot is classified as generating.
60
+ - `prevNdBySlot[slotId]` — stores `{ prevNd, idTask, classification }`
61
+ - `prevPromptTokensBySlot[slotId]` — stores `{ tokens, idTask }`
63
62
 
64
- This approach handles the case where `n_decoded` drops when a new request starts on a reused slot, and prevents generation stalls (where `n_decoded` plateaus) from being misclassified as prefill.
63
+ The `idTask` field ensures baselines are invalidated when a slot starts a new task (task reuse).
65
64
 
66
- When no slots are processing, all tracked state for those slots is cleared.
65
+ Classification logic:
66
+
67
+ 1. **First time or after task change/reset**: If we have no baseline for this slot+task, or `n_decoded` or `n_prompt_tokens` dropped below baseline (indicating a new request), use a heuristic: `n_decoded > 0` → TG, otherwise → PP.
68
+ 2. **Compare against baseline**: If `n_decoded > baselineNd` → TG. If `n_prompt_tokens > baselineNt` → PP.
69
+ 3. **Neither increased**: Persist the previous classification. This handles frequent polling where neither field increases on every poll cycle.
70
+
71
+ When a slot stops being processed, all tracked state for that slot is cleared.
67
72
 
68
73
  ### Prefill Rate (PP)
69
74
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@troed/oc-ls-stats",
4
- "version": "1.1.0",
4
+ "version": "1.2.1",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  "./tui": {
package/tui.tsx CHANGED
@@ -1,19 +1,9 @@
1
1
  /** @jsxImportSource @opentui/solid */
2
+ import type { TextRenderable } from "@opentui/core"
2
3
  import type { TuiPlugin, TuiPluginModule } from "@opencode-ai/plugin/tui"
3
- import { createMemo, createSignal } from "solid-js"
4
- import { appendFileSync } from "fs"
4
+ import { onCleanup } from "solid-js"
5
5
  import { processPoll } from "./src/stats.ts"
6
6
 
7
- const DEBUG_ENABLED = false
8
- const DEBUG_LOG = "/tmp/oc-ls-stats-debug.log"
9
- function debug(...args: unknown[]) {
10
- if (!DEBUG_ENABLED) return
11
- try {
12
- const line = `${args.map(a => typeof a === "object" ? JSON.stringify(a) : String(a)).join(" ")}\n`
13
- appendFileSync(DEBUG_LOG, line)
14
- } catch {}
15
- }
16
-
17
7
  type StreamSample = {
18
8
  at: number
19
9
  tokens: number
@@ -61,32 +51,23 @@ function stripBaseUrlPath(baseURL: string): string {
61
51
  }
62
52
 
63
53
  function extractProviderUrls(config: unknown): string[] {
64
- if (!config || typeof config !== "object") { debug("extractProviderUrls: config is null/undefined or not object"); return [] }
54
+ if (!config || typeof config !== "object") return []
65
55
  const obj = config as Record<string, unknown>
66
56
  const provider = obj.provider
67
- if (!provider || typeof provider !== "object") { debug("extractProviderUrls: no provider in config"); return [] }
57
+ if (!provider || typeof provider !== "object") return []
68
58
 
69
59
  const urls: string[] = []
70
- let total = 0
71
- let matched = 0
72
60
  for (const [key, val] of Object.entries(provider as Record<string, unknown>)) {
73
- total++
74
61
  if (val && typeof val === "object") {
75
62
  const opts = (val as Record<string, unknown>).options
76
63
  if (opts && typeof opts === "object") {
77
64
  const baseURL = (opts as Record<string, unknown>).baseURL || (opts as Record<string, unknown>).base_url
78
- debug(`extractProviderUrls: provider="${key}" baseURL=${baseURL}`)
79
65
  if (typeof baseURL === "string" && key.toLowerCase().includes("llama") && !key.toLowerCase().includes("ollama")) {
80
66
  urls.push(stripBaseUrlPath(baseURL))
81
- matched++
82
- debug(`extractProviderUrls: MATCH provider="${key}" -> ${urls[urls.length-1]}`)
83
- } else {
84
- debug(`extractProviderUrls: SKIP provider="${key}" (llama=${key.toLowerCase().includes("llama")} ollama=${key.toLowerCase().includes("ollama")})`)
85
67
  }
86
68
  }
87
69
  }
88
70
  }
89
- debug(`extractProviderUrls: total=${total} matched=${matched} urls=${JSON.stringify(urls)}`)
90
71
  return urls
91
72
  }
92
73
 
@@ -123,39 +104,46 @@ function SessionPromptRight(props: {
123
104
  api: Parameters<TuiPlugin>[0]
124
105
  sessionID: string
125
106
  tracker: TrackerState
126
- version: () => number
127
- clock: () => number
107
+ subscribe: (listener: () => void) => () => void
128
108
  }) {
129
- const liveTps = createMemo(() => {
130
- props.version()
131
- props.clock()
132
- const rate = props.tracker.lastGeneratedTps
133
- if (!Number.isFinite(rate) || rate <= 0) return undefined
134
- return rate
135
- })
109
+ let textRef: TextRenderable | undefined
136
110
 
137
- const prefillRate = createMemo(() => {
138
- props.version()
139
- if (!props.tracker.isPrefilling) return undefined
140
- const rate = props.tracker.lastPrefillRate
141
- if (!Number.isFinite(rate) || rate <= 0) return undefined
142
- return rate
143
- })
111
+ const sync = () => {
112
+ if (!textRef) return
113
+ textRef.content = getStatusText()
114
+ props.api.renderer.requestRender()
115
+ }
144
116
 
145
- const text = createMemo(() => {
146
- if (props.tracker.failure) return props.tracker.failure
147
- const pp = prefillRate()
148
- const tps = liveTps()
149
- if (props.tracker.isPrefilling && pp) {
150
- return `${formatPps(pp)} tps (PP)`
117
+ const unsubscribe = props.subscribe(sync)
118
+ onCleanup(unsubscribe)
119
+
120
+ return (
121
+ <text
122
+ ref={(ref: TextRenderable) => {
123
+ textRef = ref
124
+ sync()
125
+ }}
126
+ fg={props.api.theme.current.textMuted}
127
+ >
128
+ {getStatusText()}
129
+ </text>
130
+ )
131
+
132
+ function getStatusText() {
133
+ const s = props.tracker
134
+ if (s.failure) {
135
+ return s.failure
151
136
  }
152
- if (props.tracker.isGenerating && tps) {
153
- return `${formatTps(tps)} tps (TG)`
137
+ const pp = s.isPrefilling && s.lastPrefillRate > 0 ? formatPps(s.lastPrefillRate) : undefined
138
+ const tps = s.isGenerating && s.lastGeneratedTps > 0 ? formatTps(s.lastGeneratedTps) : undefined
139
+ let result = "- tps (TG)"
140
+ if (s.isPrefilling && pp) {
141
+ result = `${pp} tps (PP)`
142
+ } else if (s.isGenerating && tps) {
143
+ result = `${tps} tps (TG)`
154
144
  }
155
- return `- tps (TG)`
156
- })
157
-
158
- return <>{text() ? <text fg={props.api.theme.current.textMuted}>{text()}</text> : null}</>
145
+ return result
146
+ }
159
147
  }
160
148
 
161
149
  const tui: TuiPlugin = async (api) => {
@@ -177,10 +165,11 @@ const tui: TuiPlugin = async (api) => {
177
165
  prevPromptTokensBySlot: {},
178
166
  failure: null,
179
167
  }
180
- const [version, setVersion] = createSignal(0)
181
- const [clock, setClock] = createSignal(Date.now())
168
+ const listeners = new Set<() => void>()
182
169
 
183
- const bump = () => setVersion((value) => value + 1)
170
+ const bump = () => {
171
+ for (const listener of listeners) listener()
172
+ }
184
173
 
185
174
  const pruneSamples = (now = Date.now()) => {
186
175
  let changed = false
@@ -240,13 +229,9 @@ const tui: TuiPlugin = async (api) => {
240
229
  }
241
230
  }
242
231
 
243
- debug(`pollMetrics: urls=${JSON.stringify(llamaServerUrls)} model=${model}`)
244
232
  for (const baseUrl of llamaServerUrls) {
245
- debug(`pollMetrics: polling ${baseUrl}`)
246
233
  const slots = await fetchSlots(baseUrl, model)
247
- debug(`pollMetrics: ${baseUrl} response=${JSON.stringify(slots)}`)
248
234
  const slotList = Array.isArray(slots) ? slots : slots ? Object.values(slots) : []
249
- debug(`pollMetrics: ${baseUrl} slotList.length=${slotList.length}`)
250
235
  if (slotList.length === 0) {
251
236
  tracker.failure = "n/a"
252
237
  bump()
@@ -256,22 +241,6 @@ const tui: TuiPlugin = async (api) => {
256
241
  bump()
257
242
 
258
243
  const result = processPoll(slotList, tracker)
259
-
260
- if (result.isPrefilling) {
261
- debug(`PP rate=${Math.round(result.lastPrefillRate)} tps`)
262
- }
263
- if (result.isGenerating) {
264
- debug(`TG rate=${Math.round(result.lastGeneratedTps)} tps`)
265
- }
266
- if (result.ppEnded) {
267
- debug(`PP ended rate=${Math.round(result.lastPrefillRate)} tps`)
268
- }
269
- if (result.tgEnded) {
270
- debug(`TG ended rate=${Math.round(result.lastGeneratedTps)} tps`)
271
- }
272
- }
273
-
274
- if (tracker.lastPrefillRate > 0 || tracker.lastGeneratedTps > 0) {
275
244
  bump()
276
245
  }
277
246
  }
@@ -333,7 +302,6 @@ const tui: TuiPlugin = async (api) => {
333
302
  })
334
303
 
335
304
  const timer = setInterval(() => {
336
- setClock(Date.now())
337
305
  pruneSamples()
338
306
  }, 1000)
339
307
 
@@ -360,7 +328,12 @@ const tui: TuiPlugin = async (api) => {
360
328
  api.slots.register({
361
329
  slots: {
362
330
  session_prompt_right(_ctx, value) {
363
- return <SessionPromptRight api={api} sessionID={value.session_id} tracker={tracker} version={version} clock={clock} />
331
+ return <SessionPromptRight api={api} sessionID={value.session_id} tracker={tracker} subscribe={(listener) => {
332
+ listeners.add(listener)
333
+ return () => {
334
+ listeners.delete(listener)
335
+ }
336
+ }} />
364
337
  },
365
338
  },
366
339
  })