Skip to content

fix(x-sdk): preserve message and conversation snapshots - #2077

Open
Aminark wants to merge 1 commit into
ant-design:mainfrom
Aminark:fix/sdk-store-snapshots
Open

Aminark wants to merge 1 commit into
ant-design:mainfrom
Aminark:fix/sdk-store-snapshots

Conversation

@Aminark

@Aminark Aminark commented Nov 5, 2026 •

Copy link
Copy Markdown

🤔 This is a ...

  • 🐞 Bug fix
  • ✅ Test Case

🔗 Related Issues

Found while checking message and conversation updates.

💡 Background and Solution

setMessage and setConversation mutate the existing item before copying the array. A React.memo row receiving that item therefore keeps the same prop reference and displays the old content. The mutation also changes earlier snapshots and throws when the caller provides a frozen item. Removing an item similarly splices the array from the previous snapshot.

Create a new object for the updated item and copy the array before removal. Unchanged items keep their references, and partial updates still preserve existing fields.

Added store regressions for previous snapshots, frozen data, updater callbacks and unknown keys, plus hook tests covering memoized message and conversation rows. The existing setMessage test now waits for React to render the update.

Validation:

  • All 146 x-sdk tests pass, including 10 new cases. Eight of the new cases fail on the original implementation.
  • npm run tsc --workspace packages/x-sdk
  • npm run lint:script --workspace packages/x-sdk
  • Biome formatting check on the changed files.

📝 Change Log

Language Changelog
🇺🇸 English Fix useXChat and useXConversations updates leaving memoized rows stale and mutating previous state snapshots.
🇨🇳 Chinese 修复 useXChat 和 useXConversations 更新后记忆化列表项不刷新、修改旧状态快照的问题。

Summary by CodeRabbit

  • 优化
    • 消息和会话更新、删除时不再修改原有数据,旧快照及未变更项目保持不变。
    • 消息或会话更新后,使用记忆化组件展示的内容也能及时刷新。
    • 对不存在的消息或会话执行更新、删除时,操作会返回失败且数据保持不变。

@coderabbitai

coderabbitai Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

消息和会话存储的更新与删除操作改为使用新对象或新数组。新增测试检查旧快照和对象引用,并验证通过 setMessage 或 setConversation 更新时,memo 行会显示新内容。

Changes: 消息与会话存储更新

Layer / File(s) Summary
消息存储更新与界面验证
packages/x-sdk/src/x-chat/store.ts, packages/x-sdk/src/x-chat/__test__/*
setMessage 为目标消息创建新对象,并提交新数组。removeMessage 先复制消息数组再删除目标项。测试覆盖旧快照、对象引用、冻结消息、未知 ID,以及 memo 行更新。
会话存储更新与界面验证
packages/x-sdk/src/x-conversations/store.ts, packages/x-sdk/src/x-conversations/__test__/*
setConversation 为目标会话创建新对象,并提交新列表。removeConversation 先复制列表再删除目标项。测试覆盖旧快照、对象引用、未知 key,以及 memo 行更新。

Estimated code review effort: 2 (Simple) | ~12 minutes

Change: Bug fix

Suggested labels: bug

Suggested reviewers: kimteayon

Merge Risk: Minimal · up to 22934

The change is intended to preserve snapshots and refresh memoized rows. No merge-blocking risk is identified from the supplied evidence.

Pre-merge checks: Passed 5

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 标题“fix(x-sdk): preserve message and conversation snapshots”与变更内容一致。变更将 ChatMessagesStore.setMessage、removeMessage 以及 ConversationStore.setConversation、removeConversation 改为不原地修改旧对象,而是生成新对象和新数组,并新增快照与 …
Docstring Coverage Passed No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
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.
Full details: Title check

Explanation

标题“fix(x-sdk): preserve message and conversation snapshots”与变更内容一致。变更将 ChatMessagesStore.setMessage、removeMessage 以及 ConversationStore.setConversation、removeConversation 改为不原地修改旧对象,而是生成新对象和新数组,并新增快照与 memo 行组件相关的测试。标题指出了 message 与 conversation 两部分的快照保护,覆盖了主要变更。标题符合 Conventional Commits 格式,长度适中,无噪声词汇。

Full details: Docstring Coverage

Explanation

No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 0 functions across 6 files.

Finishing Touches

Generate unit tests (beta)

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

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

我是兔子,捧着新快照,
旧消息安稳,不被改写。
会话换新对象,列表轻轻更新,
memo 行也读到了新内容。
我蹦过测试,耳朵摇摇,
为清晰的状态唱一支小调。

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: 5f42ff45-8481-4a85-86ad-350c320abd74
Commits

Reviewing files that changed from the base of the PR and between da08db1 and 22934f9.

Files selected for processing: 6

  • packages/x-sdk/src/x-chat/__test__/index.test.tsx
  • packages/x-sdk/src/x-chat/__test__/store.test.ts
  • packages/x-sdk/src/x-chat/store.ts
  • packages/x-sdk/src/x-conversations/__test__/index.test.tsx
  • packages/x-sdk/src/x-conversations/__test__/store.test.ts
  • packages/x-sdk/src/x-conversations/store.ts

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

This branch has not been deployed

No deployments
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