Skip to content

Commit

Permalink
🚸 style: add delete and regenerate for function message (lobehub#992)
Browse files Browse the repository at this point in the history
* 🚸 style: add delete and regenerate for function message

* πŸ› fix: fix plugin state update error

* βœ… test: fix test
  • Loading branch information
arvinxx authored Jan 10, 2024
1 parent c0f1ca7 commit 7f8c717
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/database/models/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ class _MessageModel extends BaseModel {
return super._update(id, data);
}

async updatePluginState(id: string, key: string, value: any) {
const item = await this.findById(id);

return this.update(id, { pluginState: { ...item.pluginState, [key]: value } });
}

/**
* Batch updates multiple fields of the specified messages.
*
Expand Down
4 changes: 2 additions & 2 deletions src/features/Conversation/Actions/Function.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { useChatListActionsBar } from '../hooks/useChatListActionsBar';
import { RenderAction } from '../types';

export const FunctionActionsBar: RenderAction = memo(({ onActionClick }) => {
const { regenerate, divider, del } = useChatListActionsBar();
const { regenerate, divider, delAndRegenerate, del } = useChatListActionsBar();
return (
<ActionIconGroup
dropdownMenu={[regenerate, divider, del]}
dropdownMenu={[regenerate, divider, delAndRegenerate, del]}
items={[regenerate]}
onActionClick={onActionClick}
type="ghost"
Expand Down
7 changes: 3 additions & 4 deletions src/services/__tests__/message.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ vi.mock('@/database/models/message', () => {
clearTable: vi.fn(),
batchUpdate: vi.fn(),
queryAll: vi.fn(),
updatePluginState: vi.fn(),
},
};
});
Expand Down Expand Up @@ -255,7 +256,7 @@ describe('MessageService', () => {
const key = 'stateKey';
const value = 'stateValue';
const newPluginState = { [key]: value };
(MessageModel.update as Mock).mockResolvedValue({
(MessageModel.updatePluginState as Mock).mockResolvedValue({
...mockMessage,
pluginState: newPluginState,
});
Expand All @@ -264,9 +265,7 @@ describe('MessageService', () => {
const result = await messageService.updateMessagePluginState(mockMessageId, key, value);

// Assert
expect(MessageModel.update).toHaveBeenCalledWith(mockMessageId, {
pluginState: newPluginState,
});
expect(MessageModel.updatePluginState).toHaveBeenCalledWith(mockMessageId, key, value);
expect(result).toEqual({ ...mockMessage, pluginState: newPluginState });
});
});
Expand Down
3 changes: 2 additions & 1 deletion src/services/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ export class MessageService {
async updateMessagePlugin(id: string, plugin: ChatPluginPayload) {
return MessageModel.update(id, { plugin });
}

async updateMessagePluginState(id: string, key: string, value: any) {
return MessageModel.update(id, { pluginState: { [key]: value } });
return MessageModel.updatePluginState(id, key, value);
}

async getAllMessages() {
Expand Down

0 comments on commit 7f8c717

Please sign in to comment.