Files
apes-sidebar/src/client/session-metrics.ts
T
Menghuan1918 9ccc147e00 feat(tasks): 第二轮真机反馈——聚合条动作行、模式 chip、弹窗用量/耗时、原生侧栏转录、动效
用户在自己跑的 3080 实例上测完报回六条,逐条修:

1. 「已完成的聚合卡下方空荡荡」= 上一轮把计数收进徽标后那条只剩一个
   chevron(我的取舍造成)。小条改为动作行:组图标 + 「展开」(新词条
   tasksFoldExpandShort × 20 词典)+ 出错尾注 + 展开箭头。
2. 「展开后的子代理点不回去」= 真缺陷:expandedGroups 压过了 foldedIds,
   展开组内卡片的 chevron 失效。改成手动折叠优先(手动 → 页级规则 →
   组展开),补模型 + 组件两层判别性用例。
3. 「一次性一直显示」→ 模式词搬进徽标行成 chip(用户口径)。智能体卡不再
   有 mono 元信息行,卡片高度预算拆成 GRAPH_AGENT_NODE_H / GRAPH_NODE_H
   (只有 workflow run 卡保留元信息行),rowStrideFor(nodes) 按模型里有没有
   run 选行距。
4. 动效:接入 @formkit/auto-animate(用户选定,~3KB、无依赖无安装脚本)+
   useAutoAnimateRef 包装(WAAPI 守卫 → jsdom 空转、尊重 reduced-motion),
   挂到画布卡片层 / 树视图 / 任务抽屉;详情卡另加 140ms 入场关键帧。核心
   bundle +56KB(未压缩 CJS)。
5. 弹窗多两项:Token 用量(宿主 tokenUsage 四桶求和,悬停给明细)+ 耗时
   (宿主 subagentTiming,按 3s 轮询刷新)。SidebarProjectionSnapshot 镜像
   补两个键、模型新增 metrics 入参;**按存在性渲染**,没有测量就不显示这一
   行(0 是投影初始态,与「没工作过」不可区分)。
6. 「查看转录」改为在原生右侧栏打开:宿主 ui-subagent 的 subagentchat tab
   类型(宿主自己的子代理列表就是点行=主窗、点 chevron=侧栏)。插件照抄其
   地址公式并调 openResource(..., { kind, preferNewPane: true }),三级降级回
   主窗;页脚变「主按钮 + caret 菜单(在主窗口中打开)」。

测试 2238 passed / 17 skipped(191 文件)、tsc 0 错误、eslint 0 错误、构建
通过;新构建已装进 sidebar-test profile,:3080 上挂载正常、0 pageerror。
动效观感与真实数据的两行数值未在真机逐帧核验(右栏在无头上下文里被
transform 移出视口,合成点击不稳)——已如实记进设计文档的第二轮偏差段。
2026-10-11 23:15:40 +08:00

86 lines
3.5 KiB
TypeScript

/**
* The Tasks page's per-session METRICS: how much a node consumed (tokens) and
* how long it actually worked (its own turn timing) — the two facts the node
* detail popover adds beside the state.
*
* Both come from the host's session projections, which the client publishes in
* `SessionListState.projectionsBySession` (see the mirror in
* ../context-types.ts). This module is the pure adapter between that snapshot
* and the shape the view model carries, plus the two display rules, so the
* popover never re-derives them:
*
* - **Tokens** are a session TOTAL in four disjoint buckets. The reader wants
* one number plus a breakdown, so {@link totalTokens} sums them (input
* buckets + output) and {@link inputTokens} keeps the billed-input half.
* - **Duration** is `settledMs` (closed turns) plus the live part of an open
* turn. `active.through` only advances when an event commits, so a running
* node's elapsed time is measured to `Date.now()` and refreshed by the page's
* own poll — never by a timer of this module's own.
*
* Everything degrades silently: a session with no events, a host without the
* projection, or a key the fold omitted simply reports nothing, and the caller
* hides the row rather than printing a zero it cannot justify.
*/
import type {
SidebarProjectionSnapshot,
SidebarSubagentTiming,
SidebarTokenUsage,
} from '../context-types.ts'
/** One session's metrics, as the view model carries them on its node. */
export interface SessionMetrics {
usage?: SidebarTokenUsage
timing?: SidebarSubagentTiming
}
/** The billed input tokens: every bucket that is not output. */
export function inputTokens(usage: SidebarTokenUsage): number {
return usage.uncachedInputTokens + usage.cacheReadTokens + usage.cacheWriteTokens
}
/** The session's total tokens (input buckets + output). */
export function totalTokens(usage: SidebarTokenUsage): number {
return inputTokens(usage) + usage.outputTokens
}
/**
* The node's elapsed working time in ms, or undefined when the host published
* no timing for it.
*
* @param timing - the session's `subagentTiming`.
* @param now - the clock the caller's render is pinned to.
*/
export function elapsedMs(timing: SidebarSubagentTiming | undefined, now: number): number | undefined {
if (timing === undefined) return undefined
const active = timing.active === undefined ? 0 : Math.max(0, now - timing.active.since)
const total = timing.settledMs + active
// 0 means "no closed turn and no open one" — the projection's init state,
// which is indistinguishable from "this session never worked". Show nothing.
return total > 0 ? total : undefined
}
/**
* Fold the projection snapshot into one metrics row per session.
*
* Sessions the snapshot does not carry are simply absent (the page's own poll
* fills them in), and a session carrying neither key is omitted so the caller's
* map stays small.
*
* @param projections - `SessionListState.projectionsBySession`.
*/
export function sessionMetrics(
projections: Readonly<Record<string, SidebarProjectionSnapshot>> | undefined,
): Readonly<Record<string, SessionMetrics>> {
const out: Record<string, SessionMetrics> = {}
for (const [sessionId, snapshot] of Object.entries(projections ?? {})) {
const usage = snapshot.values.tokenUsage
const timing = snapshot.values.subagentTiming
if (usage === undefined && timing === undefined) continue
out[sessionId] = {
...(usage === undefined ? {} : { usage }),
...(timing === undefined ? {} : { timing }),
}
}
return out
}