n8n/packages/cli/src/chat/utils.ts
Eugene 0b7db24070
fix(core): Iterate over all main output branches when extracting response (#19963)
Co-authored-by: Michael Kret <michael.k@radency.com>
2025-09-25 09:31:03 +02:00

58 lines
1.7 KiB
TypeScript

import type { IExecutionResponse } from '@n8n/db';
import type { INode } from 'n8n-workflow';
import { CHAT_WAIT_USER_REPLY, RESPOND_TO_WEBHOOK_NODE_TYPE } from 'n8n-workflow';
/**
* Returns the message to be sent of the last executed node
*/
export function getMessage(execution: IExecutionResponse) {
const lastNodeExecuted = execution.data.resultData.lastNodeExecuted;
if (typeof lastNodeExecuted !== 'string') return undefined;
const runIndex = execution.data.resultData.runData[lastNodeExecuted].length - 1;
const mainOutputs = execution.data.resultData.runData[lastNodeExecuted][runIndex]?.data?.main;
// Check all main output branches for a message
if (mainOutputs && Array.isArray(mainOutputs)) {
for (const branch of mainOutputs) {
if (branch && Array.isArray(branch) && branch.length > 0 && branch[0].sendMessage) {
return branch[0].sendMessage;
}
}
}
return undefined;
}
/**
* Returns the last node executed
*/
export function getLastNodeExecuted(execution: IExecutionResponse) {
const lastNodeExecuted = execution.data.resultData.lastNodeExecuted;
if (typeof lastNodeExecuted !== 'string') return undefined;
return execution.workflowData?.nodes?.find((node) => node.name === lastNodeExecuted);
}
/**
* Check if execution should be resumed immediately after receivng a message
*/
export function shouldResumeImmediately(lastNode: INode) {
if (lastNode?.type === RESPOND_TO_WEBHOOK_NODE_TYPE) {
return true;
}
if (lastNode?.parameters?.[CHAT_WAIT_USER_REPLY] === false) {
return true;
}
const options = lastNode?.parameters?.options as {
[CHAT_WAIT_USER_REPLY]?: boolean;
};
if (options && options[CHAT_WAIT_USER_REPLY] === false) {
return true;
}
return false;
}