diff --git a/packages/server/src/utils/buildAgentflow.ts b/packages/server/src/utils/buildAgentflow.ts index 98b1c55f2..5144eacd5 100644 --- a/packages/server/src/utils/buildAgentflow.ts +++ b/packages/server/src/utils/buildAgentflow.ts @@ -1235,7 +1235,8 @@ const executeNode = async ({ index: i, value: item, isFirst: i === 0, - isLast: i === results.input.iterationInput.length - 1 + isLast: i === results.input.iterationInput.length - 1, + sessionId: sessionId } try { @@ -1503,7 +1504,7 @@ export const executeAgentFlow = async ({ const uploads = incomingInput.uploads const userMessageDateTime = new Date() const chatflowid = chatflow.id - const sessionId = overrideConfig.sessionId || chatId + const sessionId = iterationContext?.sessionId || overrideConfig.sessionId || chatId const humanInput: IHumanInput | undefined = incomingInput.humanInput // Validate history schema if provided @@ -1655,7 +1656,8 @@ export const executeAgentFlow = async ({ } // If it is human input, find the last checkpoint and resume - if (humanInput) { + // Skip human input resumption for recursive iteration calls - they should start fresh + if (humanInput && !(isRecursive && iterationContext)) { if (!previousExecution) { throw new Error(`No previous execution found for session ${sessionId}`) } @@ -1766,7 +1768,7 @@ export const executeAgentFlow = async ({ }) if (parentExecution) { - logger.debug(` 📝 Using parent execution ID: ${parentExecutionId} for recursive call`) + logger.debug(` 📝 Using parent execution ID: ${parentExecutionId} for recursive call (iteration: ${!!iterationContext})`) newExecution = parentExecution } else { console.warn(` ⚠️ Parent execution ID ${parentExecutionId} not found, will create new execution`) @@ -1861,6 +1863,11 @@ export const executeAgentFlow = async ({ let iterations = 0 let currentHumanInput = humanInput + // For iteration calls, clear human input since they should start fresh + if (isRecursive && iterationContext && humanInput) { + currentHumanInput = undefined + } + let analyticHandlers: AnalyticHandler | undefined let parentTraceIds: ICommonObject | undefined