diff --git a/src/store/chat/helpers.test.ts b/src/store/chat/helpers.test.ts index 97fd58bdc0a1..40070a0f63d7 100644 --- a/src/store/chat/helpers.test.ts +++ b/src/store/chat/helpers.test.ts @@ -74,7 +74,7 @@ describe('chatHelpers', () => { ] as ChatMessage[]; it('returns all messages if history is disabled', () => { - const config = { enableHistoryCount: false, historyCount: 0 } as LobeAgentChatConfig; + const config = { enableHistoryCount: false, historyCount: undefined } as LobeAgentChatConfig; const slicedMessages = chatHelpers.getSlicedMessagesWithConfig(messages, config); expect(slicedMessages).toEqual(messages); }); @@ -105,5 +105,11 @@ describe('chatHelpers', () => { const slicedMessages = chatHelpers.getSlicedMessagesWithConfig([], config); expect(slicedMessages).toEqual([]); }); + + it('returns an empty array when historyCount is zero', () => { + const config = { enableHistoryCount: true, historyCount: 0 } as LobeAgentChatConfig; + const slicedMessages = chatHelpers.getSlicedMessagesWithConfig(messages, config); + expect(slicedMessages).toEqual([]); + }); }); }); diff --git a/src/store/chat/helpers.ts b/src/store/chat/helpers.ts index cd0a5e5af644..3648d776c0f6 100644 --- a/src/store/chat/helpers.ts +++ b/src/store/chat/helpers.ts @@ -14,13 +14,13 @@ const getSlicedMessagesWithConfig = ( config: LobeAgentChatConfig, includeNewUserMessage?: boolean, ): ChatMessage[] => { - // if historyCount is not enabled or set to 0, return all messages - if (!config.enableHistoryCount || !config.historyCount) return messages; + // if historyCount is not enabled, return all messages + if (!config.enableHistoryCount || config.historyCount === undefined) return messages; // if user send message, history will include this message so the total length should +1 const messagesCount = !!includeNewUserMessage ? config.historyCount + 1 : config.historyCount; - // if historyCount is negative, return empty array + // if historyCount is negative or set to 0, return empty array if (messagesCount <= 0) return []; // if historyCount is positive, return last N messages