From da8d0f12d6e20c8df59642727ac49d39b05fb302 Mon Sep 17 00:00:00 2001 From: Henry Heng Date: Wed, 28 May 2025 15:04:45 +0100 Subject: [PATCH] Bugfix/Disable default user input if memory is disabled for first node (#4530) disable default user input if memory is disabled for first node --- .../components/nodes/agentflow/Agent/Agent.ts | 18 +++++++++++++++--- packages/components/nodes/agentflow/LLM/LLM.ts | 18 +++++++++++++++--- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/packages/components/nodes/agentflow/Agent/Agent.ts b/packages/components/nodes/agentflow/Agent/Agent.ts index 5abe10fb..55f565d0 100644 --- a/packages/components/nodes/agentflow/Agent/Agent.ts +++ b/packages/components/nodes/agentflow/Agent/Agent.ts @@ -756,7 +756,7 @@ class Agent_Agentflow implements INode { /* * If this is the first node: * - Add images to messages if exist - * - Add user message + * - Add user message if it does not exist in the agentMessages array */ if (options.uploads) { const imageContents = await getUniqueImageMessages(options, messages, modelConfig) @@ -767,7 +767,7 @@ class Agent_Agentflow implements INode { } } - if (input && typeof input === 'string') { + if (input && typeof input === 'string' && !agentMessages.some((msg) => msg.role === 'user')) { messages.push({ role: 'user', content: input @@ -977,7 +977,19 @@ class Agent_Agentflow implements INode { inputMessages.push(...runtimeImageMessagesWithFileRef) } if (input && typeof input === 'string') { - inputMessages.push({ role: 'user', content: input }) + if (!enableMemory) { + if (!agentMessages.some((msg) => msg.role === 'user')) { + inputMessages.push({ role: 'user', content: input }) + } else { + agentMessages.map((msg) => { + if (msg.role === 'user') { + inputMessages.push({ role: 'user', content: msg.content }) + } + }) + } + } else { + inputMessages.push({ role: 'user', content: input }) + } } } diff --git a/packages/components/nodes/agentflow/LLM/LLM.ts b/packages/components/nodes/agentflow/LLM/LLM.ts index 18f8d187..8b400e7e 100644 --- a/packages/components/nodes/agentflow/LLM/LLM.ts +++ b/packages/components/nodes/agentflow/LLM/LLM.ts @@ -410,7 +410,7 @@ class LLM_Agentflow implements INode { /* * If this is the first node: * - Add images to messages if exist - * - Add user message + * - Add user message if it does not exist in the llmMessages array */ if (options.uploads) { const imageContents = await getUniqueImageMessages(options, messages, modelConfig) @@ -421,7 +421,7 @@ class LLM_Agentflow implements INode { } } - if (input && typeof input === 'string') { + if (input && typeof input === 'string' && !llmMessages.some((msg) => msg.role === 'user')) { messages.push({ role: 'user', content: input @@ -545,7 +545,19 @@ class LLM_Agentflow implements INode { inputMessages.push(...runtimeImageMessagesWithFileRef) } if (input && typeof input === 'string') { - inputMessages.push({ role: 'user', content: input }) + if (!enableMemory) { + if (!llmMessages.some((msg) => msg.role === 'user')) { + inputMessages.push({ role: 'user', content: input }) + } else { + llmMessages.map((msg) => { + if (msg.role === 'user') { + inputMessages.push({ role: 'user', content: msg.content }) + } + }) + } + } else { + inputMessages.push({ role: 'user', content: input }) + } } }