From bc14384ea36010d5d220381976834c387060b162 Mon Sep 17 00:00:00 2001 From: sxjeru Date: Wed, 25 Dec 2024 13:10:37 +0800 Subject: [PATCH 1/2] Update helpers.ts --- src/store/chat/helpers.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 From fc10ae14119236d6eb484ceb54bae4b405d88a5f Mon Sep 17 00:00:00 2001 From: sxjeru Date: Wed, 25 Dec 2024 13:39:13 +0800 Subject: [PATCH 2/2] Update helpers.test.ts --- src/store/chat/helpers.test.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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([]); + }); }); });