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: preserve current turn with zero history (#15080)

YuTengjing7e78453ae340
diff --git a/src/server/modules/AgentRuntime/RuntimeExecutors.ts b/src/server/modules/AgentRuntime/RuntimeExecutors.ts
index dc6eb832650..a42c807ec3c 100644
--- a/src/server/modules/AgentRuntime/RuntimeExecutors.ts
+++ b/src/server/modules/AgentRuntime/RuntimeExecutors.ts
@@ -144,6 +144,16 @@ const resolveLLMMaxRetries = (provider: string) =>
// again here multiplies the same failed routed request across every channel.
provider === BRANDING_PROVIDER ? 0 : LLM_MAX_RETRIES;
+const resolveRuntimeHistoryCount = (historyCount?: number) => {
+ if (historyCount === undefined) return undefined;
+
+ // Agent config stores historical message count, excluding the current turn.
+ // Runtime executors already pass the current user/tool turn in `llmPayload.messages`;
+ // without this +1, `historyCount: 0` truncates the current message too and sends
+ // `messages: []` to providers.
+ return historyCount + 1;
+};
+
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
const getLLMRetryDelayMs = (attempt: number) =>
@@ -652,7 +662,7 @@ export const createRuntimeExecutors = (
enableHistoryCount: agentConfig.chatConfig?.enableHistoryCount ?? undefined,
evalContext: ctx.evalContext,
forceFinish: state.forceFinish,
- historyCount: agentConfig.chatConfig?.historyCount ?? undefined,
+ historyCount: resolveRuntimeHistoryCount(agentConfig.chatConfig?.historyCount),
initialContext: (state as any).initialContext?.initialContext,
knowledge: {
fileContents: agentConfig.files
diff --git a/src/server/modules/AgentRuntime/__tests__/RuntimeExecutors.test.ts b/src/server/modules/AgentRuntime/__tests__/RuntimeExecutors.test.ts
index cac0dbb17e3..bd519c76ff6 100644
--- a/src/server/modules/AgentRuntime/__tests__/RuntimeExecutors.test.ts
+++ b/src/server/modules/AgentRuntime/__tests__/RuntimeExecutors.test.ts
@@ -1072,6 +1072,46 @@ describe('RuntimeExecutors', () => {
);
});
+ it('should keep current turn when agent historyCount is 0', async () => {
+ const ctxWithConfig: RuntimeExecutorContext = {
+ ...ctx,
+ agentConfig: {
+ chatConfig: { enableHistoryCount: true, historyCount: 0 },
+ plugins: [],
+ },
+ };
+ const executors = createRuntimeExecutors(ctxWithConfig);
+ const state = createMockState();
+
+ const instruction = {
+ payload: {
+ messages: [
+ { content: 'History message', id: 'history-1', role: 'user' },
+ { content: 'History response', id: 'history-2', role: 'assistant' },
+ { content: 'Current message', id: 'current-1', role: 'user' },
+ ],
+ model: 'gpt-4',
+ provider: 'openai',
+ },
+ type: 'call_llm' as const,
+ };
+
+ await executors.call_llm!(instruction, state);
+
+ expect(engineSpy).toHaveBeenCalledWith(expect.objectContaining({ historyCount: 1 }));
+
+ const chatMessages = mockChat.mock.calls[0][0].messages;
+ expect(chatMessages).toContainEqual(
+ expect.objectContaining({ content: 'Current message', role: 'user' }),
+ );
+ expect(chatMessages).not.toContainEqual(
+ expect.objectContaining({ content: 'History message', role: 'user' }),
+ );
+ expect(chatMessages).not.toContainEqual(
+ expect.objectContaining({ content: 'History response', role: 'assistant' }),
+ );
+ });
+
it('should not call serverMessagesEngine when agentConfig is not set', async () => {
const executors = createRuntimeExecutors(ctx); // ctx without agentConfig
const state = createMockState();