Skip to content

Commit 08ec29f

Browse files
authored
♻️ refactor: refactor message create name (lobehub#10074)
refactor name
1 parent 54f4e18 commit 08ec29f

File tree

13 files changed

+101
-101
lines changed

13 files changed

+101
-101
lines changed

src/server/routers/lambda/__tests__/integration/message.integration.test.ts

Lines changed: 57 additions & 57 deletions
Large diffs are not rendered by default.

src/server/routers/lambda/message.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ export const messageRouter = router({
5454
return ctx.messageModel.countWords(input);
5555
}),
5656

57-
createNewMessage: messageProcedure
57+
createMessage: messageProcedure
5858
.input(CreateNewMessageParamsSchema.extend({ useGroup: z.boolean().optional() }))
5959
.mutation(async ({ input, ctx }) => {
6060
const { useGroup, ...params } = input;
61-
return ctx.messageService.createNewMessage(params as any, { useGroup });
61+
return ctx.messageService.createMessage(params as any, { useGroup });
6262
}),
6363

6464
getHeatmaps: messageProcedure.query(async ({ ctx }) => {

src/server/services/message/__tests__/index.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ describe('MessageService', () => {
255255
});
256256
});
257257

258-
describe('createNewMessage', () => {
258+
describe('createMessage', () => {
259259
it('should create message and return message list', async () => {
260260
const params = {
261261
content: 'Hello',
@@ -268,7 +268,7 @@ describe('MessageService', () => {
268268
vi.mocked(mockMessageModel.create).mockResolvedValue(createdMessage as any);
269269
vi.mocked(mockMessageModel.query).mockResolvedValue(mockMessages as any);
270270

271-
const result = await messageService.createNewMessage(params as any);
271+
const result = await messageService.createMessage(params as any);
272272

273273
expect(mockMessageModel.create).toHaveBeenCalledWith(params);
274274
expect(mockMessageModel.query).toHaveBeenCalledWith(
@@ -301,7 +301,7 @@ describe('MessageService', () => {
301301
vi.mocked(mockMessageModel.create).mockResolvedValue(createdMessage as any);
302302
vi.mocked(mockMessageModel.query).mockResolvedValue(mockMessages as any);
303303

304-
const result = await messageService.createNewMessage(params as any, { useGroup: true });
304+
const result = await messageService.createMessage(params as any, { useGroup: true });
305305

306306
expect(mockMessageModel.query).toHaveBeenCalledWith(
307307
expect.anything(),
@@ -329,7 +329,7 @@ describe('MessageService', () => {
329329
vi.mocked(mockMessageModel.create).mockResolvedValue(createdMessage as any);
330330
vi.mocked(mockMessageModel.query).mockResolvedValue(mockMessages as any);
331331

332-
const result = await messageService.createNewMessage(params as any);
332+
const result = await messageService.createMessage(params as any);
333333

334334
expect(mockMessageModel.query).toHaveBeenCalledWith(
335335
{

src/server/services/message/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export class MessageService {
128128
* This method combines message creation and querying into a single operation,
129129
* reducing the need for separate refresh calls and improving performance.
130130
*/
131-
async createNewMessage(
131+
async createMessage(
132132
params: CreateMessageParams,
133133
options?: QueryOptions,
134134
): Promise<CreateMessageResult> {

src/services/message/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ export class MessageService {
2323
return labPreferSelectors.enableAssistantMessageGroup(useUserStore.getState());
2424
}
2525

26-
createNewMessage = async ({
26+
createMessage = async ({
2727
sessionId,
2828
...params
2929
}: CreateMessageParams): Promise<CreateMessageResult> => {
30-
return lambdaClient.message.createNewMessage.mutate({
30+
return lambdaClient.message.createMessage.mutate({
3131
...params,
3232
sessionId: sessionId ? this.toDbSessionId(sessionId) : undefined,
3333
useGroup: this.useGroup,

src/store/chat/slices/aiChat/actions/__tests__/generateAIChat.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe('chatMessage actions', () => {
6161
await result.current.sendMessage({ message: TEST_CONTENT.USER_MESSAGE });
6262
});
6363

64-
expect(messageService.createNewMessage).not.toHaveBeenCalled();
64+
expect(messageService.createMessage).not.toHaveBeenCalled();
6565
expect(result.current.internal_coreProcessMessage).not.toHaveBeenCalled();
6666
});
6767

@@ -72,7 +72,7 @@ describe('chatMessage actions', () => {
7272
await result.current.sendMessage({ message: TEST_CONTENT.EMPTY });
7373
});
7474

75-
expect(messageService.createNewMessage).not.toHaveBeenCalled();
75+
expect(messageService.createMessage).not.toHaveBeenCalled();
7676
});
7777

7878
it('should not send when message is empty with empty files array', async () => {
@@ -82,7 +82,7 @@ describe('chatMessage actions', () => {
8282
await result.current.sendMessage({ message: TEST_CONTENT.EMPTY, files: [] });
8383
});
8484

85-
expect(messageService.createNewMessage).not.toHaveBeenCalled();
85+
expect(messageService.createMessage).not.toHaveBeenCalled();
8686
});
8787
});
8888

@@ -97,13 +97,13 @@ describe('chatMessage actions', () => {
9797
});
9898
});
9999

100-
expect(messageService.createNewMessage).toHaveBeenCalled();
100+
expect(messageService.createMessage).toHaveBeenCalled();
101101
expect(result.current.internal_coreProcessMessage).not.toHaveBeenCalled();
102102
});
103103

104104
it('should handle message creation errors gracefully', async () => {
105105
const { result } = renderHook(() => useChatStore());
106-
vi.spyOn(messageService, 'createNewMessage').mockRejectedValue(
106+
vi.spyOn(messageService, 'createMessage').mockRejectedValue(
107107
new Error('create message error'),
108108
);
109109

@@ -229,7 +229,7 @@ describe('chatMessage actions', () => {
229229
.mockResolvedValue({ isFunctionCall: false, content: 'AI response' });
230230

231231
const createMessageSpy = vi
232-
.spyOn(messageService, 'createNewMessage')
232+
.spyOn(messageService, 'createMessage')
233233
.mockResolvedValue({ id: TEST_IDS.ASSISTANT_MESSAGE_ID, messages: mockMessages });
234234

235235
const replaceMessagesSpy = vi.spyOn(result.current, 'replaceMessages');
@@ -272,7 +272,7 @@ describe('chatMessage actions', () => {
272272
rewriteQuery: 'rewritten query',
273273
});
274274

275-
vi.spyOn(messageService, 'createNewMessage').mockResolvedValue({
275+
vi.spyOn(messageService, 'createMessage').mockResolvedValue({
276276
id: TEST_IDS.ASSISTANT_MESSAGE_ID,
277277
messages: [],
278278
});
@@ -306,7 +306,7 @@ describe('chatMessage actions', () => {
306306
.spyOn(result.current, 'internal_fetchAIChatMessage')
307307
.mockResolvedValue({ isFunctionCall: false, content: '' });
308308

309-
vi.spyOn(messageService, 'createNewMessage').mockResolvedValue(undefined as any);
309+
vi.spyOn(messageService, 'createMessage').mockResolvedValue(undefined as any);
310310

311311
await act(async () => {
312312
await result.current.internal_coreProcessMessage([userMessage], userMessage.id);

src/store/chat/slices/aiChat/actions/__tests__/generateAIChatV2.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ describe('generateAIChatV2 actions', () => {
131131
await result.current.sendMessage({ message: TEST_CONTENT.USER_MESSAGE });
132132
});
133133

134-
expect(messageService.createNewMessage).not.toHaveBeenCalled();
134+
expect(messageService.createMessage).not.toHaveBeenCalled();
135135
expect(result.current.internal_execAgentRuntime).not.toHaveBeenCalled();
136136
});
137137

@@ -142,7 +142,7 @@ describe('generateAIChatV2 actions', () => {
142142
await result.current.sendMessage({ message: TEST_CONTENT.EMPTY });
143143
});
144144

145-
expect(messageService.createNewMessage).not.toHaveBeenCalled();
145+
expect(messageService.createMessage).not.toHaveBeenCalled();
146146
});
147147

148148
it('should not send when message is empty with empty files array', async () => {
@@ -152,7 +152,7 @@ describe('generateAIChatV2 actions', () => {
152152
await result.current.sendMessage({ message: TEST_CONTENT.EMPTY, files: [] });
153153
});
154154

155-
expect(messageService.createNewMessage).not.toHaveBeenCalled();
155+
expect(messageService.createMessage).not.toHaveBeenCalled();
156156
});
157157
});
158158

@@ -312,7 +312,7 @@ describe('generateAIChatV2 actions', () => {
312312
});
313313
});
314314

315-
expect(messageService.createNewMessage).toHaveBeenCalled();
315+
expect(messageService.createMessage).toHaveBeenCalled();
316316
expect(result.current.internal_execAgentRuntime).not.toHaveBeenCalled();
317317
});
318318

@@ -749,7 +749,7 @@ describe('generateAIChatV2 actions', () => {
749749

750750
beforeEach(() => {
751751
// Reset mocks
752-
vi.spyOn(messageService, 'createNewMessage').mockResolvedValue({
752+
vi.spyOn(messageService, 'createMessage').mockResolvedValue({
753753
id: 'new-assistant-block-id',
754754
messages: [] as any,
755755
});
@@ -814,8 +814,8 @@ describe('generateAIChatV2 actions', () => {
814814
}),
815815
);
816816

817-
// Verify that createNewMessage was called with message params
818-
expect(messageService.createNewMessage).toHaveBeenCalledWith(
817+
// Verify that createMessage was called with message params
818+
expect(messageService.createMessage).toHaveBeenCalledWith(
819819
expect.objectContaining({
820820
role: 'assistant',
821821
parentId: TOOL_RESULT_MSG_ID,
@@ -866,7 +866,7 @@ describe('generateAIChatV2 actions', () => {
866866
}),
867867
);
868868

869-
expect(messageService.createNewMessage).toHaveBeenCalledWith(
869+
expect(messageService.createMessage).toHaveBeenCalledWith(
870870
expect.objectContaining({
871871
role: 'assistant',
872872
parentId: 'non-existent-tool-result-id',

src/store/chat/slices/aiChat/actions/__tests__/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export const createMockAbortController = () => {
5858
*/
5959
export const spyOnMessageService = () => {
6060
const createMessageSpy = vi
61-
.spyOn(messageService, 'createNewMessage')
61+
.spyOn(messageService, 'createMessage')
6262
.mockResolvedValue({ id: TEST_IDS.NEW_MESSAGE_ID, messages: [] });
6363
const updateMessageSpy = vi
6464
.spyOn(messageService, 'updateMessage')

src/store/chat/slices/aiChat/actions/generateAIChatV2.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ export const generateAIChatV2: StateCreator<
646646
payload.type,
647647
);
648648

649-
// 2. 使用 createNewMessage 创建 tool 消息
649+
// 2. 使用 createMessage 创建 tool 消息
650650
const toolMessage: CreateNewMessageParams = {
651651
content: '',
652652
parentId: assistantId,

src/store/chat/slices/message/action.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ vi.mock('@/services/message', () => ({
2525
removeMessage: vi.fn(),
2626
removeMessagesByAssistant: vi.fn(),
2727
removeMessages: vi.fn(() => Promise.resolve()),
28-
createNewMessage: vi.fn(() => Promise.resolve({ id: 'new-message-id', messages: [] })),
28+
createMessage: vi.fn(() => Promise.resolve({ id: 'new-message-id', messages: [] })),
2929
updateMessage: vi.fn(),
3030
removeAllMessages: vi.fn(() => Promise.resolve()),
3131
},
@@ -71,7 +71,7 @@ describe('chatMessage actions', () => {
7171
await result.current.addAIMessage();
7272
});
7373

74-
expect(messageService.createNewMessage).not.toHaveBeenCalled();
74+
expect(messageService.createMessage).not.toHaveBeenCalled();
7575
expect(updateInputMessageSpy).not.toHaveBeenCalled();
7676
});
7777

@@ -84,7 +84,7 @@ describe('chatMessage actions', () => {
8484
await result.current.addAIMessage();
8585
});
8686

87-
expect(messageService.createNewMessage).toHaveBeenCalledWith({
87+
expect(messageService.createMessage).toHaveBeenCalledWith({
8888
content: inputMessage,
8989
role: 'assistant',
9090
sessionId: mockState.activeId,
@@ -113,7 +113,7 @@ describe('chatMessage actions', () => {
113113
await result.current.addUserMessage({ message: 'test message' });
114114
});
115115

116-
expect(messageService.createNewMessage).not.toHaveBeenCalled();
116+
expect(messageService.createMessage).not.toHaveBeenCalled();
117117
expect(updateInputMessageSpy).not.toHaveBeenCalled();
118118
});
119119

@@ -130,7 +130,7 @@ describe('chatMessage actions', () => {
130130
await result.current.addUserMessage({ message, fileList });
131131
});
132132

133-
expect(messageService.createNewMessage).toHaveBeenCalledWith({
133+
expect(messageService.createMessage).toHaveBeenCalledWith({
134134
content: message,
135135
files: fileList,
136136
role: 'user',
@@ -154,7 +154,7 @@ describe('chatMessage actions', () => {
154154
await result.current.addUserMessage({ message });
155155
});
156156

157-
expect(messageService.createNewMessage).toHaveBeenCalledWith({
157+
expect(messageService.createMessage).toHaveBeenCalledWith({
158158
content: message,
159159
files: undefined,
160160
role: 'user',
@@ -184,7 +184,7 @@ describe('chatMessage actions', () => {
184184
await result.current.addUserMessage({ message });
185185
});
186186

187-
expect(messageService.createNewMessage).toHaveBeenCalledWith({
187+
expect(messageService.createMessage).toHaveBeenCalledWith({
188188
content: message,
189189
files: undefined,
190190
role: 'user',

0 commit comments

Comments
 (0)