Commit 035994f
♻️ refactor: refactor chat message model to speed up (lobehub#10053)
* refactor new chat message
* ♻️ refactor: Unify message creation methods into single `internal_createMessage`
## Changes
### Method Consolidation
- Merged `internal_createMessage` and `internal_createNewMessage` into a single unified method
- All message creation now returns `{ id: string, messages: UIChatMessage[] }`
- Eliminated redundant API calls by always using `createNewMessage` backend endpoint
### Updated Call Sites (11 locations)
**Store Actions:**
- `addAIMessage` & `addUserMessage` - Added result validation
**AI Chat:**
- `generateAIChat.ts` - Extract `result.id` from response
- `generateAIChatV2.ts` - Renamed from `internal_createNewMessage` to `internal_createMessage`
**Group Chat:**
- `generateAIGroupChat.ts` - Extract `result.id` in 3 locations
**Thread & Tools:**
- `thread/action.ts` - Extract `result.id`
- `builtinTool/actions/search.ts` - Extract `result.id`
- `plugin/action.ts` - Extract `result.id`
### Test Updates
- Updated mocks to return `{ id, messages }` structure
- `thread/action.test.ts` - 4 mock updates
- `plugin/action.test.ts` - 2 mock updates
## Benefits
- **Performance**: All message creation now uses single-request pattern
- **Consistency**: Unified return type across all creation flows
- **Maintainability**: Single method to maintain instead of two similar ones
## Testing
- ✅ Type check: 0 errors
- ✅ Unit tests: 175/175 passed
- message/action.test.ts: 33/33
- plugin/action.test.ts: 26/26
- thread/action.test.ts: 39/39
- generateAIChat.test.ts: 41/41
- generateAIChatV2.test.ts: 36/36
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* ♻️ refactor: optimize message update operations to reduce API calls
Optimized two message update operations to reduce network requests:
1. **updatePluginState**: Modified to return updated messages
- Backend: `MessageModel.updatePluginState` now accepts options and returns `UpdateMessageResult`
- Router: Added `sessionId`, `topicId`, and `useGroup` parameters
- Frontend: Service layer passes lab preferences, store uses `replaceMessages` instead of `refreshMessages`
- Reduction: 2 requests → 1 request
2. **message.update**: Added `groupAssistantMessages` support
- Service: `updateMessage` now passes `useGroup` flag based on lab preferences
- Backend model already had infrastructure for returning messages
- Reduction: Ensures consistent 1-request pattern
Tests passing (26/26 plugin tests, 14/14 integration tests).
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* ♻️ refactor: optimize all internal message methods to reduce API calls
Optimized 6 internal message methods and 3 error handling scenarios to reduce API calls from 2 requests (update + refresh) to 1 request (update with messages returned):
**Internal methods optimized:**
- internal_updateMessagePluginError
- internal_updateMessageRAG
- internal_deleteMessage
- internal_refreshToUpdateMessageTools
- internal_updatePluginError
**Error handling optimized:**
- internal_callPluginApi error scenarios (2 locations)
- invokeStandaloneTypePlugin invalid settings
**Changes:**
- Backend: Updated message routers to accept sessionId/topicId/useGroup and return messages
- Service: Added getUseGroupPreference() getter to simplify lab preference checks
- Service: Updated methods to use getter and return UpdateMessageResult
- Store: Changed from refreshMessages() to replaceMessages(result.messages)
- Tests: Updated 4 plugin tests to verify replaceMessages instead of refreshMessages
**Performance impact:**
Each optimized method now makes 1 request instead of 2, reducing network overhead and improving UI responsiveness.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* ♻️ refactor: optimize message deletion to reduce API calls and fix group message children deletion
**Problem 1 - Group message children not deleted:**
- When deleting a `role: 'group'` message, children messages (linked via `parentId`) were not deleted
- Tool result messages were also not included in deletion
**Problem 2 - Delete operations using refresh pattern:**
- `deleteMessage`, `clearMessage`, `clearAllMessages` all used refreshMessages after deletion
- This resulted in 2 requests: delete + refresh
**Solutions:**
1. **Enhanced deleteMessage in UI layer:**
- Added logic to find all children messages via `parentId` for group role messages
- Combined with existing tool message deletion logic
- All related message IDs are collected and passed to backend in one call
- Business logic stays in UI layer, model layer remains simple
2. **Optimized delete operations:**
- Backend: `removeMessages` now accepts sessionId/topicId/useGroup and returns messages
- Service: `removeMessages` updated to pass options and return UpdateMessageResult
- Store: `deleteMessage` now uses replaceMessages with returned data (2 requests → 1 request)
- Store: `clearMessage` and `clearAllMessages` directly replace with empty array
3. **Updated tests:**
- Fixed 4 tests to verify replaceMessages instead of refreshMessages
- Added mock for service to return messages in delete operations
- All 33 message action tests passing
- All 14 integration tests passing
**Performance impact:**
- deleteMessage: 2 requests → 1 request
- clearMessage/clearAllMessages: 1 delete + 1 refresh → 1 delete + direct clear
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
* ✅ test: add comprehensive tests for group message deletion with children
Added 2 new test cases to verify group message deletion behavior:
1. **Basic group message with children deletion:**
- Verifies that deleting a `role: 'group'` message also deletes all children (via `parentId`)
- Tests that unrelated messages are preserved
2. **Group message with children that have tool calls:**
- Verifies that deleting a group message also deletes:
- The group message itself
- All children messages (via `parentId`)
- Tool result messages from children (via `tool_call_id`)
- Ensures complete cleanup of the entire message tree
**Implementation enhancement:**
- Updated `deleteMessage` to also collect and delete tool results from children messages
- Ensures no orphaned tool result messages remain after group deletion
**Test results:**
- All 35 message action tests passing (2 new tests added)
- Verifies complete cascading deletion of group message trees
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent c7b7998 commit 035994f
File tree
19 files changed
+644
-314
lines changed- packages
- database
- src/models
- types/src/message/ui
- src
- server/routers/lambda
- __tests__/integration
- services/message
- store/chat/slices
- aiChat/actions
- __tests__
- builtinTool/actions
- message
- plugin
- thread
19 files changed
+644
-314
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
| 27 | + | |
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
603 | 603 | | |
604 | 604 | | |
605 | 605 | | |
| 606 | + | |
606 | 607 | | |
607 | 608 | | |
608 | 609 | | |
| |||
633 | 634 | | |
634 | 635 | | |
635 | 636 | | |
| 637 | + | |
636 | 638 | | |
637 | 639 | | |
638 | 640 | | |
| |||
660 | 662 | | |
661 | 663 | | |
662 | 664 | | |
663 | | - | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
664 | 675 | | |
665 | 676 | | |
666 | 677 | | |
667 | 678 | | |
668 | 679 | | |
669 | | - | |
| 680 | + | |
670 | 681 | | |
671 | 682 | | |
672 | 683 | | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
673 | 700 | | |
674 | 701 | | |
675 | 702 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | | - | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | 123 | | |
173 | 124 | | |
174 | 125 | | |
| |||
0 commit comments