useful-pi-extensions 1.3.0 → 1.4.0

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "useful-pi-extensions",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "A small collection of pi extensions, installed with one command — a labelled status line with context pressure, cache, cost, effort, TTFT and tokens/sec.",
5
5
  "keywords": [
6
6
  "bun",
@@ -30,6 +30,7 @@ import type {
30
30
  import { truncateToWidth, visibleWidth } from '@earendil-works/pi-tui'
31
31
 
32
32
  import {
33
+ avgTokPerSec,
33
34
  contextRow,
34
35
  currencyFromConfig,
35
36
  formatCwd,
@@ -37,10 +38,10 @@ import {
37
38
  formatTps,
38
39
  isQuietStatus,
39
40
  pair,
40
- rateFromPayload,
41
- cachedRate,
41
+ cachedRates,
42
+ ratesFromPayload,
42
43
  cacheIsFresh,
43
- withCachedRate,
44
+ withCachedRates,
44
45
  row,
45
46
  shortenPath,
46
47
  ttftDisplay,
@@ -59,7 +60,7 @@ const FALLBACK_TOKENS_PER_CHAR = 0.25
59
60
 
60
61
  /** The status line's own settings file, alongside pi's other per-tool config. */
61
62
  const CONFIG_PATH = join(homedir(), '.pi', 'agent', 'statusline.json')
62
- /** Free, keyless, updated once a day which is exactly the freshness a daily rate wants. */
63
+ /** Free, keyless, and one request returns every currency so the cache serves instant switching. */
63
64
  const RATES_URL = 'https://open.er-api.com/v6/latest/USD'
64
65
  const FETCH_TIMEOUT_MS = 5000
65
66
 
@@ -70,13 +71,14 @@ function today(): string {
70
71
  return `${now.getFullYear()}-${month}-${day}`
71
72
  }
72
73
 
73
- async function fetchRate(code: string): Promise<number | null> {
74
+ async function fetchRates(): Promise<Record<string, number> | null> {
74
75
  const controller = new AbortController()
75
76
  const timer = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS)
76
77
  try {
77
78
  const response = await fetch(RATES_URL, { signal: controller.signal })
78
79
  if (!response.ok) return null
79
- return rateFromPayload(await response.json(), code)
80
+ const rates = ratesFromPayload(await response.json())
81
+ return Object.keys(rates).length === 0 ? null : rates
80
82
  } catch {
81
83
  return null
82
84
  } finally {
@@ -87,6 +89,11 @@ async function fetchRate(code: string): Promise<number | null> {
87
89
  /**
88
90
  * Reads the display currency.
89
91
  *
92
+ * `perUsd` is a pinned rate and wins untouched; a code without one resolves through the cached rate
93
+ * table — today's table answers instantly, a stale one answers while a fresh one is fetched, and
94
+ * only a first-ever failure speaks up. The whole table is cached because one request returns every
95
+ * currency, which is also what makes switching codes instant and offline.
96
+ *
90
97
  * Called once per session on purpose: a footer that stat'ed a file on every frame would be its own
91
98
  * bug. Editing the file therefore takes effect on `/reload`.
92
99
  */
@@ -102,21 +109,22 @@ async function loadCurrency(notify: (message: string) => void): Promise<Currency
102
109
  if (problem !== null) notify(problem)
103
110
  if (pending === null) return currency
104
111
 
105
- const cached = cachedRate(text)
106
- if (cached !== null && cacheIsFresh(cached.fetchedAt, today())) {
107
- return { symbol: pending.symbol, perUsd: cached.perUsd }
112
+ const cached = cachedRates(text)
113
+ const known = cached?.rates[pending.code]
114
+ if (cached !== null && known !== undefined && cacheIsFresh(cached.fetchedAt, today())) {
115
+ return { symbol: pending.symbol, perUsd: known }
108
116
  }
109
117
 
110
- const fetched = await fetchRate(pending.code)
118
+ const fetched = await fetchRates()
111
119
  if (fetched !== null) {
112
120
  try {
113
- await writeFile(CONFIG_PATH, withCachedRate(text, fetched, today()))
121
+ await writeFile(CONFIG_PATH, withCachedRates(text, fetched, today()))
114
122
  } catch {
115
123
  // The session still runs on the fetched rate; only tomorrow's warm start is lost.
116
124
  }
117
- return { symbol: pending.symbol, perUsd: fetched }
125
+ return { symbol: pending.symbol, perUsd: fetched[pending.code] ?? USD.perUsd }
118
126
  }
119
- if (cached !== null) return { symbol: pending.symbol, perUsd: cached.perUsd }
127
+ if (known !== undefined) return { symbol: pending.symbol, perUsd: known }
120
128
  notify(`could not fetch a ${pending.code} rate, showing USD`)
121
129
  return USD
122
130
  }
@@ -231,6 +239,7 @@ export default function (pi: ExtensionAPI) {
231
239
  let chars = 0
232
240
  let requestAt: number | null = null
233
241
  let firstTokenAt: number | null = null
242
+ let totalDecodeMs = 0
234
243
  let ticker: ReturnType<typeof setInterval> | null = null
235
244
  let windowAt = 0
236
245
  let windowTokens = 0
@@ -281,6 +290,7 @@ export default function (pi: ExtensionAPI) {
281
290
  render(width: number): string[] {
282
291
  const usage = ctx.getContextUsage()
283
292
  const totals = collectTotals(ctx)
293
+ const avg = avgTokPerSec(totals.output, totalDecodeMs)
284
294
  const row1 = contextRow(
285
295
  theme,
286
296
  width,
@@ -309,12 +319,15 @@ export default function (pi: ExtensionAPI) {
309
319
  if (reading.ttftMs !== null)
310
320
  row2Parts.push(pair(theme, 'TTFT', formatLatency(reading.ttftMs), 'muted'))
311
321
  row2Parts.push(
312
- theme.fg(
313
- reading.exact ? 'success' : 'dim',
322
+ pair(
323
+ theme,
324
+ 'Last',
314
325
  `${reading.exact ? '' : '~'}${formatTps(reading.rate)} tok/s`,
326
+ reading.exact ? 'success' : 'dim',
315
327
  ),
316
328
  )
317
329
  }
330
+ if (avg !== null) row2Parts.push(pair(theme, 'Avg', `${formatTps(avg)} tok/s`, 'muted'))
318
331
  const row2Right = row2Parts.join(theme.fg('dim', ' · '))
319
332
 
320
333
  const branch = footerData.getGitBranch()
@@ -374,6 +387,7 @@ export default function (pi: ExtensionAPI) {
374
387
  ratio = seedRatio(ctx)
375
388
  reading = null
376
389
  requestAt = null
390
+ totalDecodeMs = 0
377
391
  stopTicker()
378
392
  resetStream()
379
393
  currency = await loadCurrency((message) => ctx.ui.notify(message, 'warning'))
@@ -455,8 +469,11 @@ export default function (pi: ExtensionAPI) {
455
469
  const decodeMs = firstTokenAt !== null ? Date.now() - firstTokenAt : 0
456
470
  const measured = ttftMs(requestAt, firstTokenAt)
457
471
  const tokens = output > 0 ? output : totalChars * (ratio ?? FALLBACK_TOKENS_PER_CHAR)
458
- if (measured !== null && decodeMs >= MIN_SAMPLE_MS)
472
+ if (measured !== null && decodeMs >= MIN_SAMPLE_MS) {
473
+ // Session-average numerator lives in collectTotals; this is its denominator.
474
+ totalDecodeMs += decodeMs
459
475
  publish((tokens / decodeMs) * 1000, output > 0, measured)
476
+ }
460
477
  // Null it with the stream: a request that has produced its message is no longer in flight, and
461
478
  // a stale anchor would let the live branch count against nothing until the next turn.
462
479
  requestAt = null
@@ -250,26 +250,34 @@ export function currencyFromConfig(text: string | null): CurrencyConfig {
250
250
  }
251
251
  }
252
252
 
253
- /** One rate out of an open.er-api.com payload (`{"rates": {"CNY": 7.12}}`), read defensively. */
254
- export function rateFromPayload(payload: unknown, code: string): number | null {
255
- if (!isRecord(payload) || !isRecord(payload.rates)) return null
256
- const rate = payload.rates[code]
257
- return typeof rate === 'number' && Number.isFinite(rate) && rate > 0 ? rate : null
253
+ /** The day's rates out of an open.er-api.com payload, positive and finite only. */
254
+ export function ratesFromPayload(payload: unknown): Record<string, number> {
255
+ if (!isRecord(payload) || !isRecord(payload.rates)) return {}
256
+ const rates: Record<string, number> = {}
257
+ for (const [code, rate] of Object.entries(payload.rates)) {
258
+ if (typeof rate === 'number' && Number.isFinite(rate) && rate > 0) rates[code] = rate
259
+ }
260
+ return rates
258
261
  }
259
262
 
260
- /** The rate a config file cached from an earlier fetch, and the day it was fetched. */
261
- export function cachedRate(text: string): { perUsd: number; fetchedAt: string } | null {
263
+ /** The rates a config file cached from an earlier fetch, and the day they were fetched. */
264
+ export function cachedRates(
265
+ text: string,
266
+ ): { rates: Record<string, number>; fetchedAt: string } | null {
262
267
  let config: unknown
263
268
  try {
264
269
  config = JSON.parse(text)
265
270
  } catch {
266
271
  return null
267
272
  }
268
- if (!isRecord(config)) return null
269
- const perUsd = config.fetchedPerUsd
273
+ if (!isRecord(config) || !isRecord(config.rates)) return null
270
274
  const fetchedAt = config.fetchedAt
271
- if (typeof perUsd !== 'number' || !Number.isFinite(perUsd) || perUsd <= 0) return null
272
- return typeof fetchedAt === 'string' ? { perUsd, fetchedAt } : null
275
+ if (typeof fetchedAt !== 'string') return null
276
+ const rates: Record<string, number> = {}
277
+ for (const [code, rate] of Object.entries(config.rates)) {
278
+ if (typeof rate === 'number' && Number.isFinite(rate) && rate > 0) rates[code] = rate
279
+ }
280
+ return Object.keys(rates).length === 0 ? null : { rates, fetchedAt }
273
281
  }
274
282
 
275
283
  /** Whether a cached rate was fetched today, both dates as `YYYY-MM-DD`. */
@@ -277,13 +285,23 @@ export function cacheIsFresh(fetchedAt: string, today: string): boolean {
277
285
  return fetchedAt.slice(0, 10) === today
278
286
  }
279
287
 
288
+ /** Session-average output tokens per second of measured decode time; null before any measurement. */
289
+ export function avgTokPerSec(outputTokens: number, decodeMs: number): number | null {
290
+ if (decodeMs <= 0) return null
291
+ return outputTokens / (decodeMs / 1000)
292
+ }
293
+
280
294
  /**
281
- * The config file's text with a fetched rate recorded in it, so the next session starts warm.
295
+ * The config file's text with the day's rates recorded in it, so the next session starts warm.
282
296
  *
283
297
  * Unknown keys are kept, and a file that does not parse is returned untouched: the extension has no
284
298
  * business replacing a config it could not read with one it wrote.
285
299
  */
286
- export function withCachedRate(text: string, perUsd: number, fetchedAt: string): string {
300
+ export function withCachedRates(
301
+ text: string,
302
+ rates: Record<string, number>,
303
+ fetchedAt: string,
304
+ ): string {
287
305
  let config: unknown
288
306
  try {
289
307
  config = JSON.parse(text)
@@ -291,7 +309,7 @@ export function withCachedRate(text: string, perUsd: number, fetchedAt: string):
291
309
  return text
292
310
  }
293
311
  if (!isRecord(config)) return text
294
- return `${JSON.stringify({ ...config, fetchedPerUsd: perUsd, fetchedAt }, null, 2)}\n`
312
+ return `${JSON.stringify({ ...config, rates, fetchedAt }, null, 2)}\n`
295
313
  }
296
314
 
297
315
  /** Three decimals, with one trailing zero trimmed so `$0.380` renders as `$0.38`. */