Skip to content

refactor: 优化账户列表状态刷新与渲染 - #585

Open
Qihuanxishini wants to merge 1 commit into
aimod-cc:betafrom
Qihuanxishini:perf/accounts-list
Open

Qihuanxishini wants to merge 1 commit into
aimod-cc:betafrom
Qihuanxishini:perf/accounts-list

Conversation

@Qihuanxishini

Copy link
Copy Markdown

目的

减少 Web 账户页中重复的状态读取和列表计算;本次不做 Token 刷新并发化。

改动

  1. Web 写操作通知改为状态失效,由主页面统一读取最新状态。Token 刷新由单号和批量调用方统一补读,避免桥层重复触发整表刷新。
  2. 单账号维护前后的快照只读取目标账号,复用现有公开字段投影,不再为单号刷新读取全部账号。
  3. 预计算账号索引和组内序号,缓存分组、筛选计数和账号行;勾选、查询加载态等界面变更不再触发无关的整组重算。
  4. 数据缓存失效覆盖余额、Token 读数、余额陈旧标记、排序和共享分钟显示时钟,并同步重建 desktop-tauri/ui/islands/ui.js。

保持不变

  • 批量 Token 刷新仍串行执行,保留现有可刷新判据及操作后的状态补读。
  • 管理 API 响应结构和账号持久化数据格式不变。

验证

  • npm --prefix desktop-tauri/ui-islands run build:TypeScript 检查和生产构建通过。
  • Web 应用脚本与 Rust 内嵌 Web shim 脚本的 JavaScript 语法检查通过。
  • 服务端库测试(scripts/cargo-test.mjs --lib):388 通过、0 失败、11 跳过;跳过项需要配置 AGENT2API_TEST_PLUGIN_EXE。
  • git diff --check 通过;GitHub 比较范围为本批 1 个提交、12 个相关文件。

验证边界

未做真实 Web 交互验收或优化前后的性能对照,性能收益尚未量化。

🤖 本条回复由 AI Agent 生成

Generated by an AI agent.

- 合并 Web 状态读取,由 token 刷新调用方统一补读
- 单账号维护复用公开字段投影,避免全量读取
- 预计算账号索引和组内序号,缓存未变化的分组与行
- 覆盖余额失效与共享显示时钟,保留串行刷新及现有数据格式
@coderabbitai

coderabbitai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Explain Change →

Summary

1. 📌 通俗总结(这个 PR 做了什么?)

这个 PR 是一次性能和刷新逻辑的优化,目标是让 Web 账号页更省资源、更快。它不新增功能。

核心目的:减少重复读取状态和重复计算列表的次数。

主要改动:

  • 写操作后的刷新方式变了。 写操作成功后,系统仍会发出 accounts:state-changed 通知。但通知不再先请求 /api/session。页面收到通知后,自己决定何时读取状态。通知仍有 300ms 防抖。refreshAccountToken 改用 rawCall,不再经过写请求通知层。
  • 单账号维护只读目标账号。 后端新增 AccountStore::public_account_by_id。refresh_single 只读取目标账号的快照,不再读取全部账号。
  • 列表计算有了缓存。 页面预先计算账号索引和分组位置。分组、筛选计数、可见行和列数据都按依赖项缓存。这样切换选择或查询时,不会重新计算无关的分组。
  • 缓存失效规则更细。 余额、token 读数、过期标记、排序和分钟级显示时钟变化时,缓存会失效。
  • 账号行拆成独立组件。 新增 AccountRow,原来的单元格分发函数被移除。GroupBandRow 改为 React.memo,filtered 改为必传属性。
  • 批量刷新仍是串行。 刷新条件和刷新后的状态读取方式不变。

未改变的内容: 管理 API 的响应结构和持久化的账号数据格式不变。

验证情况: 以下结果是作者报告的,本次总结未独立核实。

  • npm --prefix desktop-tauri/ui-islands run build 通过。
  • Web 脚本和 Rust 内嵌 Web shim 的 JavaScript 语法检查通过。
  • scripts/cargo-test.mjs --lib:388 通过,0 失败,11 跳过。跳过的测试需要 AGENT2API_TEST_PLUGIN_EXE。
  • git diff --check 通过。
  • 作者未做真实 Web 交互测试,也未做前后性能对比。因此性能收益未被量化。
  • CI 结果:not available。

2. 💡 产品与技术方案评估

产品视角

  • 对用户来说,本次改动几乎不可见。主要收益是页面响应速度和稳定性。这个收益合理,但目前没有数据支撑。
  • 文档中写的是"优化",没有给出具体指标。建议在改动前后记录页面切换、筛选和批量刷新的耗时。
  • 批量刷新仍是串行。如果账号很多,用户等待时间仍会很长。本 PR 没有改善这一点,也没有说明这是有意保留。
  • 需要确认的交互死角:
    • 单账号刷新后,列表行是否正确更新。
    • 批量弹窗中,被跳过的账号列表是否和刷新后的数据一致。
    • 分钟级显示时钟变化后,"过期时间"类文案是否及时更新。

技术视角

  • 刷新策略需要确认。 app.js 现在对任何状态变更通知都调用 refresh()。之前只在 accounts、session 或 health 变化时刷新。如果 health 事件频繁发出,整页刷新次数可能增加。这与"减少重复读取"的目标相反。建议只在相关字段变化时刷新,或对 refresh() 做合并。
  • 缓存失效依赖人工规则。 dataVersion 只在 usageChanged 为真时递增,且仅在记录存在且未过期时生效。如果漏掉某个写入路径,列表会显示旧数据。建议为缓存失效增加针对性测试,覆盖余额、token、排序和时钟四类场景。
  • findAccount 的索引依赖数组引用。 索引只在数组引用变化时重建。如果有代码原地修改数组,索引会失效。需要确认所有写入路径都会替换数组引用。
  • ui/islands/ui.js 是构建产物。 该文件已重新生成。审查时应以源码为准,不应逐行审查这个文件。
  • 更根本的做法。 当前方案仍是"写后整页刷新"加"前端缓存"。如果后端能返回变更的账号 ID,前端只需局部更新该行。这样可以同时减少网络读取和前端重算,也更容易验证正确性。本 PR 没有采用这种方式,且没有说明原因。

本次改动调整了单账号快照读取和状态变更通知,并更新账号列表的数据追踪、批量选择及表格行渲染。

Changes: 账号状态与列表更新

Layer / File(s) Summary
单账号快照读取
desktop-tauri/src-tauri/server/src/server/core/account_store/store_view.rs, desktop-tauri/src-tauri/server/src/server/core/maintenance/types.rs, desktop-tauri/src-tauri/server/src/server/core/maintenance/mod.rs
新增按 ID 获取公开账号及单账号快照的逻辑。refresh_single 改为只读取指定账号的刷新前后快照。
状态变更通知与界面刷新
desktop-tauri/src-tauri/server/src/web_shim.rs, desktop-tauri/ui/app.js
管理 API 写请求成功后,防抖通知监听器,不再先读取 /api/session。界面收到通知后调用 refresh();refreshAccountToken 改用 rawCall。
账号数据版本与选择状态
desktop-tauri/ui-islands/src/islands/accounts-store.ts, desktop-tauri/ui-islands/src/islands/accounts-usage-tasks.ts, desktop-tauri/ui-islands/src/islands/accounts-batch-dialog.tsx
账号数据版本随数据变更或用量记录失效而更新。批量对话框在账号列表变化时重新解析所选账号,并按目标账号 ID 判断跳过项。
账号表格行与渲染
desktop-tauri/ui-islands/src/islands/accounts-row.tsx, desktop-tauri/ui-islands/src/islands/accounts-panels.tsx, desktop-tauri/ui-islands/src/islands/accounts-page.tsx
新增 AccountRow,并将组头改为接收 filtered 属性的记忆化组件。账号页记忆分组和列数据,按分页行构建提供商映射并使用新组件渲染。

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Sequence Diagram(s)

sequenceDiagram
  participant WriteCaller
  participant web_shim
  participant StateListeners as accounts:state-changed listeners
  participant App as ui/app.js
  WriteCaller->>web_shim: 发起管理 API 写请求
  web_shim->>StateListeners: 写入成功后经防抖发送通知
  StateListeners->>App: 触发状态变更回调
  App->>App: 调用 refresh()
Loading

Suggested reviewers: aimod-cc

Security architecture risk: Minimal · up to 6ca6d

The inspected changes preserve existing account-management permissions and credential-refresh execution. No introduced or worsened issue materially affecting security, state ownership, or failure containment was established.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The inspected change remains within the existing deployment's account-management authority. A caller-controlled account ID reaches the existing refresh handler; the new internal lookup does not add a route, broaden the returned account list, or introduce another data-store boundary.

Trust Boundaries and Controls

  • observed — The rawCall alias retains the original HTTP call path and removes only the success-notification wrapper. Account-management routes remain under the existing management gate, including its rejection of restricted model keys for management access and its deployment-dependent session/key rules; this PR does not tighten or weaken those rules.

Resilience and Maintainability Implications

  • observed — The refresh coordinator retains generation-based ordering: an invalidation during an in-flight read triggers another read rather than applying the superseded response. Failed reads retain the previous state and publish an error. Batch token refresh remains serial, validates returned identity and provider, records per-account failures, and subsequently attempts state reconciliation; visible-page polling provides another recovery path.
  • observed — Usage invalidation preserves request-scoped cleanup: it marks readings stale, settles waiters, marks tasks config_changed, and requests cancellation of running work. Revision and request-ID checks continue to reject superseded responses. The added dataVersion increment makes a newly stale reading invalidate memoized displays without changing cancellation authority or persistent state.
Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed 标题“refactor: 优化账户列表状态刷新与渲染”与变更内容相符。变更涵盖 Web 写操作通知改为状态失效、单账号快照只读目标账号、以及账户列表的分组、计数和行渲染缓存。标题点明了主要方向,属于概括性描述,没有涉及无关内容。
Description check Passed 描述说明了目的、改动点、保持不变的部分和验证结果,与变更集直接相关(Web 通知、单账号快照、预计算索引与缓存、ui.js 重建等)。内容详细度足够,不属于偏题或空泛描述。
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

ast-grep (0.51.1)

desktop-tauri/ui/islands/ui.js

ast-grep timed out on this file

Clippy (1.99.1)

Clippy execution failed

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 5607f06c-8d69-4792-ba4b-34a491ec8197
Commits

Reviewing files that changed from the base of the PR and between aba5885 and 6ca6db6.

Files selected for processing: 12

  • desktop-tauri/src-tauri/server/src/server/core/account_store/store_view.rs
  • desktop-tauri/src-tauri/server/src/server/core/maintenance/mod.rs
  • desktop-tauri/src-tauri/server/src/server/core/maintenance/types.rs
  • desktop-tauri/src-tauri/server/src/web_shim.rs
  • desktop-tauri/ui-islands/src/islands/accounts-batch-dialog.tsx
  • desktop-tauri/ui-islands/src/islands/accounts-page.tsx
  • desktop-tauri/ui-islands/src/islands/accounts-panels.tsx
  • desktop-tauri/ui-islands/src/islands/accounts-row.tsx
  • desktop-tauri/ui-islands/src/islands/accounts-store.ts
  • desktop-tauri/ui-islands/src/islands/accounts-usage-tasks.ts
  • desktop-tauri/ui/app.js
  • desktop-tauri/ui/islands/ui.js

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

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