Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: adjust historyCount calculation to include accurate user messages (#13051)

sxjeru12280badbdf2
diff --git a/src/services/agentRuntime/index.ts b/src/services/agentRuntime/index.ts
index 623ce2b22b8..9b3bc8f6744 100644
--- a/src/services/agentRuntime/index.ts
+++ b/src/services/agentRuntime/index.ts
@@ -45,8 +45,8 @@ class AgentRuntimeService {
const llmMessages = await contextEngineering({
agentId: agentStoreState.activeAgentId,
enableHistoryCount: agentChatConfigSelectors.enableHistoryCount(agentStoreState),
- // include user messages
- historyCount: agentChatConfigSelectors.historyCount(agentStoreState) + 2,
+ // historyCount is number of history messages; add 1 for current user message
+ historyCount: agentChatConfigSelectors.historyCount(agentStoreState) + 1,
inputTemplate: chatConfig.inputTemplate,
messages: data.messages as any,
...modelRuntimeConfig,
diff --git a/src/services/chat/chat.test.ts b/src/services/chat/chat.test.ts
index eaad6938ba7..9fc1d8557fe 100644
--- a/src/services/chat/chat.test.ts
+++ b/src/services/chat/chat.test.ts
@@ -160,6 +160,182 @@ describe('ChatService', () => {
);
});
+ describe('historyCount functionality', () => {
+ it('should include historyCount + 1 messages when historyCount is enabled', async () => {
+ const getChatCompletionSpy = vi.spyOn(chatService, 'getChatCompletion');
+
+ const messages = [
+ {
+ content: 'History 1',
+ createdAt: Date.now(),
+ id: '1',
+ role: 'user',
+ updatedAt: Date.now(),
+ },
+ {
+ content: 'Response 1',
+ createdAt: Date.now(),
+ id: '2',
+ role: 'assistant',
+ updatedAt: Date.now(),
+ },
+ {
+ content: 'History 2',
+ createdAt: Date.now(),
+ id: '3',
+ role: 'user',
+ updatedAt: Date.now(),
+ },
+ {
+ content: 'Response 2',
+ createdAt: Date.now(),
+ id: '4',
+ role: 'assistant',
+ updatedAt: Date.now(),
+ },
+ {
+ content: 'Current message',
+ createdAt: Date.now(),
+ id: '5',
+ role: 'user',
+ updatedAt: Date.now(),
+ },
+ ] as UIChatMessage[];
+
+ await chatService.createAssistantMessage({
+ messages,
+ model: 'gpt-4',
+ provider: 'openai',
+ resolvedAgentConfig: createMockResolvedConfig({
+ agentConfig: { model: 'gpt-4', provider: 'openai' },
+ chatConfig: { enableHistoryCount: true, historyCount: 2, searchMode: 'off' },
+ }),
+ });
+
+ const calledMessages = getChatCompletionSpy.mock.calls[0][0].messages as any[];
+
+ // System date + (2 history messages + 1 current user message)
+ expect(calledMessages).toHaveLength(4);
+ expect(calledMessages[0]).toEqual(
+ expect.objectContaining({
+ content: expect.stringContaining(getCurrentDateContent()),
+ role: 'system',
+ }),
+ );
+ expect(calledMessages.slice(1)).toEqual([
+ expect.objectContaining({ content: 'History 2', role: 'user' }),
+ expect.objectContaining({ content: 'Response 2', role: 'assistant' }),
+ expect.objectContaining({ content: 'Current message', role: 'user' }),
+ ]);
+ });
+
+ it('should include only current message when historyCount is 0 and enabled', async () => {
+ const getChatCompletionSpy = vi.spyOn(chatService, 'getChatCompletion');
+
+ const messages = [
+ {
+ content: 'History 1',
+ createdAt: Date.now(),
+ id: '1',
+ role: 'user',
+ updatedAt: Date.now(),
+ },
+ {
+ content: 'Response 1',
+ createdAt: Date.now(),
+ id: '2',
+ role: 'assistant',
+ updatedAt: Date.now(),
+ },
+ {
+ content: 'Current message',
+ createdAt: Date.now(),
+ id: '3',
+ role: 'user',
+ updatedAt: Date.now(),
+ },
+ ] as UIChatMessage[];
+
+ await chatService.createAssistantMessage({
+ messages,
+ model: 'gpt-4',
+ provider: 'openai',
+ resolvedAgentConfig: createMockResolvedConfig({
+ agentConfig: { model: 'gpt-4', provider: 'openai' },
+ chatConfig: { enableHistoryCount: true, historyCount: 0, searchMode: 'off' },
+ }),
+ });
+
+ const calledMessages = getChatCompletionSpy.mock.calls[0][0].messages as any[];
+
+ // System date + current user message only
+ expect(calledMessages).toHaveLength(2);
+ expect(calledMessages[0]).toEqual(
+ expect.objectContaining({
+ content: expect.stringContaining(getCurrentDateContent()),
+ role: 'system',
+ }),
+ );
+ expect(calledMessages[1]).toEqual(
+ expect.objectContaining({ content: 'Current message', role: 'user' }),
+ );
+ });
+
+ it('should include all messages when historyCount is disabled', async () => {
+ const getChatCompletionSpy = vi.spyOn(chatService, 'getChatCompletion');
+
+ const messages = [
+ {
+ content: 'History 1',
+ createdAt: Date.now(),
+ id: '1',
+ role: 'user',
+ updatedAt: Date.now(),
+ },
+ {
+ content: 'Response 1',
+ createdAt: Date.now(),
+ id: '2',
+ role: 'assistant',
+ updatedAt: Date.now(),
+ },
+ {
+ content: 'Current message',
+ createdAt: Date.now(),
+ id: '3',
+ role: 'user',
+ updatedAt: Date.now(),
+ },
+ ] as UIChatMessage[];
+
+ await chatService.createAssistantMessage({
+ messages,
+ model: 'gpt-4',
+ provider: 'openai',
+ resolvedAgentConfig: createMockResolvedConfig({
+ agentConfig: { model: 'gpt-4', provider: 'openai' },
+ chatConfig: { enableHistoryCount: false, historyCount: 0, searchMode: 'off' },
+ }),
+ });
+
+ const calledMessages = getChatCompletionSpy.mock.calls[0][0].messages as any[];
+
+ // System date + all original messages
+ expect(calledMessages).toHaveLength(4);
+ expect(calledMessages[0]).toEqual(
+ expect.objectContaining({
+ content: expect.stringContaining(getCurrentDateContent()),
+ role: 'system',
+ }),
+ );
+ expect(calledMessages.slice(1)).toEqual([
+ expect.objectContaining({ content: 'History 1', role: 'user' }),
+ expect.objectContaining({ content: 'Response 1', role: 'assistant' }),
+ expect.objectContaining({ content: 'Current message', role: 'user' }),
+ ]);
+ });
+ });
+
describe('extendParams functionality', () => {
it('should add reasoning parameters when model supports enableReasoning and user enables it', async () => {
const getChatCompletionSpy = vi.spyOn(chatService, 'getChatCompletion');
diff --git a/src/services/chat/index.ts b/src/services/chat/index.ts
index d4ae56f2686..e160fd93a5b 100644
--- a/src/services/chat/index.ts
+++ b/src/services/chat/index.ts
@@ -244,8 +244,8 @@ class ChatService {
enableHistoryCount: chatConfig.enableHistoryCount,
enableUserMemories,
groupId,
- // historyCount + 2 accounts for: 1 user message + 1 assistant response being added
- historyCount: (chatConfig.historyCount ?? 20) + 2,
+ // historyCount is number of history messages; add 1 for current user message
+ historyCount: (chatConfig.historyCount ?? 20) + 1,
// Page editor context from agent runtime
initialContext: options?.initialContext,
inputTemplate: chatConfig.inputTemplate,