@linxiraos/pi-stats 1.4.13 → 1.5.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 (32) hide show
  1. package/CHANGELOG.md +14 -5
  2. package/dist/client/index.js +86 -86
  3. package/dist/types/client/data/formatters.d.ts +9 -0
  4. package/dist/types/client/i18n/messages/common.d.ts +2 -2
  5. package/dist/types/client/i18n/messages/en.d.ts +2 -2
  6. package/dist/types/client/i18n/messages/models.d.ts +2 -2
  7. package/dist/types/client/i18n/messages/overview.d.ts +1 -1
  8. package/dist/types/client/ui/RequestDrawer.d.ts +2 -2
  9. package/dist/types/server.d.ts +3 -1
  10. package/dist/types/shared-types.d.ts +4 -3
  11. package/dist/types/trace.d.ts +8 -5
  12. package/package.json +4 -4
  13. package/src/client/data/formatters.ts +47 -6
  14. package/src/client/i18n/messages/common.ts +2 -2
  15. package/src/client/i18n/messages/models.ts +2 -2
  16. package/src/client/i18n/messages/overview.ts +1 -1
  17. package/src/client/routes/CostsRoute.tsx +24 -28
  18. package/src/client/routes/ErrorsRoute.tsx +57 -55
  19. package/src/client/routes/GainRoute.tsx +54 -54
  20. package/src/client/routes/ModelsRoute.tsx +7 -5
  21. package/src/client/routes/OverviewRoute.tsx +73 -62
  22. package/src/client/routes/ProvidersRoute.tsx +188 -176
  23. package/src/client/routes/RequestsRoute.tsx +98 -96
  24. package/src/client/routes/ToolsRoute.tsx +11 -10
  25. package/src/client/routes/TracesRoute.tsx +2 -1
  26. package/src/client/traces/SpanDrawer.tsx +2 -2
  27. package/src/client/traces/TimelineCanvas.tsx +16 -12
  28. package/src/client/traces/TraceView.tsx +18 -14
  29. package/src/client/ui/RequestDrawer.tsx +16 -6
  30. package/src/server.ts +7 -3
  31. package/src/shared-types.ts +4 -3
  32. package/src/trace.ts +43 -13
@@ -2,7 +2,7 @@ import { useMemo, useState } from "react";
2
2
  import { getGainDashboardStats } from "../api";
3
3
  import { t } from "../i18n";
4
4
  import { Chart, type ChartSeries, Legend } from "../charts";
5
- import { formatBytes, formatCompact, formatInteger, formatPercent } from "../data/formatters";
5
+ import { formatBytes, formatCompact, formatInteger, formatPercent, formatUtcDay } from "../data/formatters";
6
6
  import { useQuery } from "../data/query";
7
7
  import { bucketAxis, rangeMeta } from "../data/range";
8
8
  import { densify } from "../data/series";
@@ -30,8 +30,6 @@ const DAY_MS = 86_400_000;
30
30
  const SOURCE_LABEL: Record<GainSource, string> = { snapcompact: "Snapcompact" };
31
31
 
32
32
  /** The server buckets gain by UTC calendar day (`YYYY-MM-DD`). */
33
- const DAY_LABEL = new Intl.DateTimeFormat(undefined, { month: "short", day: "numeric", timeZone: "UTC" });
34
-
35
33
  interface SourceRow extends GainSourceTotals {
36
34
  source: GainSource;
37
35
  /** Share of all saved tokens (0-1). */
@@ -166,7 +164,7 @@ export function GainRoute({ active, range }: GainRouteProps) {
166
164
  >
167
165
  <Chart
168
166
  slots={series.buckets.length}
169
- tickLabel={i => DAY_LABEL.format(series.buckets[i])}
167
+ tickLabel={i => formatUtcDay(series.buckets[i])}
170
168
  series={chartSeries}
171
169
  height={260}
172
170
  formatTooltip={formatInteger}
@@ -181,7 +179,7 @@ export function GainRoute({ active, range }: GainRouteProps) {
181
179
  flush
182
180
  stale={gain.stale}
183
181
  >
184
- <Table rows={sourceRows} rowKey={row => row.source} columns={SOURCE_COLUMNS} />
182
+ <Table rows={sourceRows} rowKey={row => row.source} columns={sourceColumns()} />
185
183
  </Card>
186
184
  </>
187
185
  )}
@@ -190,52 +188,54 @@ export function GainRoute({ active, range }: GainRouteProps) {
190
188
  );
191
189
  }
192
190
 
193
- const SOURCE_COLUMNS: Column<SourceRow>[] = [
194
- {
195
- key: "source",
196
- header: t("gain.source"),
197
- sort: row => row.source,
198
- render: row => <span className="cell-primary">{SOURCE_LABEL[row.source]}</span>,
199
- },
200
- {
201
- key: "tokens",
202
- header: t("gain.saved-tokens"),
203
- align: "right",
204
- sort: row => row.savedTokens,
205
- render: row => (
206
- <span title={formatInteger(row.savedTokens)}>
207
- <MeterCell value={row.share} max={1} display={formatCompact(row.savedTokens)} />
208
- </span>
209
- ),
210
- },
211
- {
212
- key: "share",
213
- header: t("common.share"),
214
- align: "right",
215
- sort: row => row.share,
216
- render: row => <span className="num muted">{formatPercent(row.share)}</span>,
217
- },
218
- {
219
- key: "bytes",
220
- header: t("gain.saved-bytes"),
221
- align: "right",
222
- sort: row => row.savedBytes,
223
- render: row => <span className="num">{formatBytes(row.savedBytes)}</span>,
224
- },
225
- {
226
- key: "hits",
227
- header: t("gain.hits"),
228
- align: "right",
229
- sort: row => row.hits,
230
- render: row => <span className="num">{formatInteger(row.hits)}</span>,
231
- },
232
- {
233
- key: "reduction",
234
- header: t("gain.reduction"),
235
- align: "right",
236
- sort: row => row.reductionPercent ?? -1,
237
- render: row => (
238
- <span className="num">{row.reductionPercent !== null ? formatPercent(row.reductionPercent) : "–"}</span>
239
- ),
240
- },
241
- ];
191
+ function sourceColumns(): Column<SourceRow>[] {
192
+ return [
193
+ {
194
+ key: "source",
195
+ header: t("gain.source"),
196
+ sort: row => row.source,
197
+ render: row => <span className="cell-primary">{SOURCE_LABEL[row.source]}</span>,
198
+ },
199
+ {
200
+ key: "tokens",
201
+ header: t("gain.saved-tokens"),
202
+ align: "right",
203
+ sort: row => row.savedTokens,
204
+ render: row => (
205
+ <span title={formatInteger(row.savedTokens)}>
206
+ <MeterCell value={row.share} max={1} display={formatCompact(row.savedTokens)} />
207
+ </span>
208
+ ),
209
+ },
210
+ {
211
+ key: "share",
212
+ header: t("common.share"),
213
+ align: "right",
214
+ sort: row => row.share,
215
+ render: row => <span className="num muted">{formatPercent(row.share)}</span>,
216
+ },
217
+ {
218
+ key: "bytes",
219
+ header: t("gain.saved-bytes"),
220
+ align: "right",
221
+ sort: row => row.savedBytes,
222
+ render: row => <span className="num">{formatBytes(row.savedBytes)}</span>,
223
+ },
224
+ {
225
+ key: "hits",
226
+ header: t("gain.hits"),
227
+ align: "right",
228
+ sort: row => row.hits,
229
+ render: row => <span className="num">{formatInteger(row.hits)}</span>,
230
+ },
231
+ {
232
+ key: "reduction",
233
+ header: t("gain.reduction"),
234
+ align: "right",
235
+ sort: row => row.reductionPercent ?? -1,
236
+ render: row => (
237
+ <span className="num">{row.reductionPercent !== null ? formatPercent(row.reductionPercent) : "–"}</span>
238
+ ),
239
+ },
240
+ ];
241
+ }
@@ -52,10 +52,12 @@ export interface ModelsRouteProps {
52
52
 
53
53
  type ShareMode = "share" | "requests";
54
54
 
55
- const SHARE_OPTIONS = [
56
- { value: "share" as const, label: t("common.share") },
57
- { value: "requests" as const, label: t("common.requests") },
58
- ];
55
+ function shareOptions() {
56
+ return [
57
+ { value: "share" as const, label: t("common.share") },
58
+ { value: "requests" as const, label: t("common.requests") },
59
+ ];
60
+ }
59
61
 
60
62
  /** Models plotted individually in the share chart; the rest fold into "Other". */
61
63
  const SHARE_LIMIT = 6;
@@ -147,7 +149,7 @@ export function ModelsRoute({ active, range }: ModelsRouteProps) {
147
149
  actions={
148
150
  <Segmented
149
151
  size="sm"
150
- options={SHARE_OPTIONS}
152
+ options={shareOptions()}
151
153
  value={mode}
152
154
  onChange={setMode}
153
155
  aria-label={t("models.share-mode-aria")}
@@ -23,6 +23,7 @@ import {
23
23
  Badge,
24
24
  Card,
25
25
  ChartSkeleton,
26
+ type Column,
26
27
  Dot,
27
28
  LabelCell,
28
29
  PageHeader,
@@ -42,29 +43,37 @@ export interface OverviewRouteProps {
42
43
 
43
44
  type ActivityMetric = "requests" | "tokens" | "cost";
44
45
 
45
- const ACTIVITY_OPTIONS = [
46
- { value: "requests" as const, label: t("common.requests") },
47
- { value: "tokens" as const, label: t("common.tokens") },
48
- { value: "cost" as const, label: t("common.cost") },
49
- ];
46
+ // Translated labels are functions, not module-level constants: `t()` reads the
47
+ // active locale, so a table built at import time would keep the boot language
48
+ // after the topbar switch (the Chinese labels in an English dashboard bug).
49
+ function activityOptions() {
50
+ return [
51
+ { value: "requests" as const, label: t("common.requests") },
52
+ { value: "tokens" as const, label: t("common.tokens") },
53
+ { value: "cost" as const, label: t("common.cost") },
54
+ ];
55
+ }
50
56
 
51
- const AGENT_LABEL: Record<AgentType, string> = {
52
- main: t("overview.main-agent"),
53
- subagent: t("overview.subagents"),
54
- advisor: t("overview.advisor"),
57
+ const AGENT_LABEL_KEY: Record<AgentType, "overview.main-agent" | "overview.subagents" | "overview.advisor"> = {
58
+ main: "overview.main-agent",
59
+ subagent: "overview.subagents",
60
+ advisor: "overview.advisor",
55
61
  };
62
+
56
63
  const AGENT_COLOR: Record<AgentType, string> = {
57
64
  main: "var(--chart-primary)",
58
65
  subagent: "var(--chart-secondary)",
59
66
  advisor: "#9d7bff",
60
67
  };
61
68
 
62
- const TOKEN_MIX = [
63
- { key: "input", label: t("overview.uncached-input"), color: "#5b8cff" },
64
- { key: "cacheRead", label: t("common.cache-read"), color: "var(--chart-primary)" },
65
- { key: "cacheWrite", label: t("common.cache-write"), color: "#f5b54a" },
66
- { key: "output", label: t("common.output"), color: "var(--chart-secondary)" },
67
- ] as const;
69
+ function tokenMix() {
70
+ return [
71
+ { key: "input", label: t("overview.uncached-input"), color: "#5b8cff" },
72
+ { key: "cacheRead", label: t("common.cache-read"), color: "var(--chart-primary)" },
73
+ { key: "cacheWrite", label: t("common.cache-write"), color: "#f5b54a" },
74
+ { key: "output", label: t("common.output"), color: "var(--chart-secondary)" },
75
+ ] as const;
76
+ }
68
77
 
69
78
  export function OverviewRoute({ active, range, onRequestClick }: OverviewRouteProps) {
70
79
  const overview = useQuery(["overview", range], ({ signal }) => getOverviewStats(range, signal), { enabled: active });
@@ -200,7 +209,7 @@ export function OverviewRoute({ active, range, onRequestClick }: OverviewRoutePr
200
209
  ? t("overview.per-hour")
201
210
  : t("overview.per-day")
202
211
  }
203
- actions={<Segmented size="sm" options={ACTIVITY_OPTIONS} value={metric} onChange={setMetric} />}
212
+ actions={<Segmented size="sm" options={activityOptions()} value={metric} onChange={setMetric} />}
204
213
  stale={overview.stale}
205
214
  >
206
215
  <QueryView query={overview} skeleton={<ChartSkeleton height={260} />}>
@@ -236,7 +245,7 @@ export function OverviewRoute({ active, range, onRequestClick }: OverviewRoutePr
236
245
  <div className="stack" style={{ gap: 18 }}>
237
246
  <div className="stack" style={{ gap: 10 }}>
238
247
  <ShareBar
239
- segments={TOKEN_MIX.map(slice => ({
248
+ segments={tokenMix().map(slice => ({
240
249
  key: slice.key,
241
250
  label: slice.label,
242
251
  value: mix[slice.key],
@@ -244,7 +253,7 @@ export function OverviewRoute({ active, range, onRequestClick }: OverviewRoutePr
244
253
  }))}
245
254
  />
246
255
  <Legend
247
- items={TOKEN_MIX.map(slice => ({
256
+ items={tokenMix().map(slice => ({
248
257
  key: slice.key,
249
258
  label: slice.label,
250
259
  color: slice.color,
@@ -259,7 +268,7 @@ export function OverviewRoute({ active, range, onRequestClick }: OverviewRoutePr
259
268
  <ShareBar
260
269
  segments={agents.segments.map(s => ({
261
270
  key: s.agentType,
262
- label: AGENT_LABEL[s.agentType],
271
+ label: t(AGENT_LABEL_KEY[s.agentType]),
263
272
  value: s.tokens,
264
273
  color: AGENT_COLOR[s.agentType],
265
274
  }))}
@@ -268,7 +277,7 @@ export function OverviewRoute({ active, range, onRequestClick }: OverviewRoutePr
268
277
  <div key={s.agentType} className="row" style={{ justifyContent: "space-between" }}>
269
278
  <span className="row">
270
279
  <span className="swatch" style={{ background: AGENT_COLOR[s.agentType] }} />
271
- {AGENT_LABEL[s.agentType]}
280
+ {t(AGENT_LABEL_KEY[s.agentType])}
272
281
  <span className="dim num">
273
282
  {t("overview.requests-short", { count: formatInteger(s.requests) })}
274
283
  </span>
@@ -310,7 +319,7 @@ export function OverviewRoute({ active, range, onRequestClick }: OverviewRoutePr
310
319
  rows={rows}
311
320
  rowKey={row => row.id ?? `${row.sessionFile}:${row.entryId}`}
312
321
  onRowClick={row => row.id !== undefined && onRequestClick(row.id)}
313
- columns={REQUEST_COLUMNS}
322
+ columns={requestColumns()}
314
323
  dense
315
324
  />
316
325
  )}
@@ -320,44 +329,46 @@ export function OverviewRoute({ active, range, onRequestClick }: OverviewRoutePr
320
329
  );
321
330
  }
322
331
 
323
- const REQUEST_COLUMNS = [
324
- {
325
- key: "model",
326
- header: t("common.model"),
327
- render: (row: MessageStats) => <LabelCell primary={row.model} secondary={row.provider} />,
328
- },
329
- {
330
- key: "time",
331
- header: t("overview.when"),
332
- render: (row: MessageStats) => <span className="muted">{formatRelativeTime(row.timestamp)}</span>,
333
- },
334
- {
335
- key: "tokens",
336
- header: t("common.tokens"),
337
- align: "right" as const,
338
- render: (row: MessageStats) => <span className="num">{formatInteger(row.usage.totalTokens)}</span>,
339
- },
340
- {
341
- key: "cost",
342
- header: t("common.cost"),
343
- align: "right" as const,
344
- render: (row: MessageStats) => <span className="num">{formatMessageCost(row, 4)}</span>,
345
- },
346
- {
347
- key: "duration",
348
- header: t("common.duration"),
349
- align: "right" as const,
350
- render: (row: MessageStats) => <span className="num">{formatDurationMs(row.duration)}</span>,
351
- },
352
- {
353
- key: "status",
354
- header: t("overview.status"),
355
- align: "right" as const,
356
- render: (row: MessageStats) =>
357
- row.errorMessage ? (
358
- <Badge tone="bad">{t("common.failed")}</Badge>
359
- ) : (
360
- <Badge tone="ok">{t("overview.ok")}</Badge>
361
- ),
362
- },
363
- ];
332
+ function requestColumns(): Column<MessageStats>[] {
333
+ return [
334
+ {
335
+ key: "model",
336
+ header: t("common.model"),
337
+ render: (row: MessageStats) => <LabelCell primary={row.model} secondary={row.provider} />,
338
+ },
339
+ {
340
+ key: "time",
341
+ header: t("overview.when"),
342
+ render: (row: MessageStats) => <span className="muted">{formatRelativeTime(row.timestamp)}</span>,
343
+ },
344
+ {
345
+ key: "tokens",
346
+ header: t("common.tokens"),
347
+ align: "right" as const,
348
+ render: (row: MessageStats) => <span className="num">{formatInteger(row.usage.totalTokens)}</span>,
349
+ },
350
+ {
351
+ key: "cost",
352
+ header: t("common.cost"),
353
+ align: "right" as const,
354
+ render: (row: MessageStats) => <span className="num">{formatMessageCost(row, 4)}</span>,
355
+ },
356
+ {
357
+ key: "duration",
358
+ header: t("common.duration"),
359
+ align: "right" as const,
360
+ render: (row: MessageStats) => <span className="num">{formatDurationMs(row.duration)}</span>,
361
+ },
362
+ {
363
+ key: "status",
364
+ header: t("overview.status"),
365
+ align: "right" as const,
366
+ render: (row: MessageStats) =>
367
+ row.errorMessage ? (
368
+ <Badge tone="bad">{t("common.failed")}</Badge>
369
+ ) : (
370
+ <Badge tone="ok">{t("overview.ok")}</Badge>
371
+ ),
372
+ },
373
+ ];
374
+ }