Skip to content

feat(agents): 统计支持拆分维度、筛选,并给出每组速度中位数与首字平均耗时 - #245

Merged
xiaoyumuxi merged 2 commits into
mainfrom
feat/agent-usage-split
Dec 1, 2026
Merged

xiaoyumuxi merged 2 commits into
mainfrom
feat/agent-usage-split

Conversation

@xiaoyumuxi

Copy link
Copy Markdown
Contributor

背景

桌面端 AI 管理窗口的「用量 → 概览」要对照 magpie 的用量页做到:每日图表按模型/供应商/Agent 拆分堆叠、指标可切换(令牌/费用/请求/速度/首字耗时)、顶部有模型/供应商/成败/搜索筛选。现在的 agentSessions.stats 一次只能按一个维度分组,没有筛选,也没有每组的速度和首字耗时,这些逻辑属于可复用的统计逻辑,按仓库规则放在 Core,桌面端只负责显示(桌面端 PR:Lithe-IDEA #2597)。

改动

agentSessions.stats(宿主命令):

  • splitBy(agent / model / provider / modelProvider):在 groupBy 之后再拆一层,例如 groupBy: "day" + splitBy: "model" 得到每天每个模型一条 Summary,第二个键放在 summary.split。不带 splitBy 时回答和以前一样。非法值以 agentSessions.invalidSplit 拒绝。
  • groupBy 新增 provider、modelProvider、all(全部调用合成一组,用于不能由其他分组相加得到的总数,如速度中位数、缓存命中率)。这三个名字只给宿主,agents/usage(插件)的可用分组不变。
  • 筛选 models、providers、failed、search:同时作用于 summaries 和 speeds。models/providers 里的空字符串表示“没有写明模型/供应商的调用”;search 按空白分词,每个词都要在模型、请求的模型、供应商、Agent、会话 ID、工作目录之一里出现(不区分大小写)。最多 64 个模型、64 个供应商,每个名字和搜索文字最多 256 字节,超出以 agentSessions.invalidFilter 拒绝。
  • detail: true 时,每个 Summary 增加 speed {calls, medianTokensPerSecond, lowerBound}(该组中有速度且输出至少 100 令牌的调用的速度中位数,规则和 speeds 一致)和 firstToken {calls, averageMs}(成功且记录了首字时间的调用的平均首字耗时)。不带 detail 时两者都不出现,所以宿主和插件两条路径的 summaries 仍逐字节一致(既有的“小时统计与共享 fixture 一致”测试继续有效)。

agentSessions.calls:新增 models、providers、search,规则同上;原有的 model、failed 不变,所有给出的筛选必须同时满足。

实现上新增 stats::CallSelector(stats 与 calls 共用同一份筛选判断),Summarizer 增加 split_by()、detailed(),速度排名与分组速度共用 ranked_speed。

契约

  • shared/plugins/contract/v1/README.md 的 agentSessions.stats、agentSessions.calls 两段补充了新参数、新分组名、新返回字段、错误码 agentSessions.invalidSplit / agentSessions.invalidFilter,以及旧 Core 的表现(把新字段当未知字段拒绝)。
  • 都在 agents 0 内补充,不升能力版本;没有新增或修改插件宿主方法、通知、能力版本,所以 API 总表和“宿主方法(按 API 编号)”索引不需要加行。

验证

  • cargo test -p lithe-agent-sessions stats(12 个,新增 4 个:拆分、供应商分组、每组速度/首字、筛选)
  • cargo test -p lithe-agent-runtime(含新增 stats_split_filter_and_detail_the_calls:拆分键、detail、各类筛选在 stats 与 calls 上结果一致、非法 splitBy/筛选的拒绝、插件路径不变)
  • cargo fmt --all -- --check、cargo clippy -p lithe-agent-sessions -p lithe-agent-runtime --all-targets -- -D warnings
  • node shared/plugins/scripts/verify-docs.mjs
  • 没有改插件后端协议,所以没有跑 sdk-e2e.mjs。

后续

桌面端 Draft #2597 临时固定这个 PR 的 head 提交;合入后换成 main 提交。

agentSessions.stats 新增可选 splitBy(agent/model/provider/modelProvider)、models、providers、failed、search、detail,groupBy 新增 provider、modelProvider、all;agentSessions.calls 新增 models、providers、search。agents/usage 的分组和回答保持不变。
@xiaoyumuxi
xiaoyumuxi marked this pull request as ready for review December 1, 2026 13:51
@xiaoyumuxi
xiaoyumuxi merged commit 614c9e4 into main Dec 1, 2026
11 of 12 checks passed

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

Some(call.session_id.as_str()),
entry.cwd.as_deref(),
]
.map(|field| field.map(str::to_lowercase));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Usage search misses Greek directory matches

When users search for οσ, CallSelector drops calls from /work/ΟΣ because lowercasing turns the final Σ into ς. The usage totals and request table both omit those calls.

Learn more

Usage statistics and the request table both use the same search filter through summarize and calls. Session validation accepts Unicode working directories, which for_each_call supplies to the filter. with_search lowercases each query word separately. Lowercasing a complete directory uses a contextual final sigma, so the substring comparison can reject spellings that differ only in case.

Example: With calls recorded in /work/ΟΣ, a search for οσ compares οσ against /work/ος and drops the calls. Both commands must keep them under the case-insensitive search contract.

Recommended fix: Apply Unicode case folding to both query words and candidate fields, as the recent-project search does with caseless::default_case_fold_str. Keep the existing whitespace splitting and all-words matching, and add a regression test with a Unicode working directory.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant