mirror of
https://github.com/n8n-io/n8n.git
synced 2026-06-01 01:07:04 +02:00
58 lines
1.7 KiB
TypeScript
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;
|
|
}
|