@n8n/n8n-nodes-langchain
Version:
1,077 lines (1,075 loc) • 48.3 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChatTrigger = void 0;
const pick_1 = __importDefault(require("lodash/pick"));
const highlightedData_1 = require("n8n-nodes-base/dist/utils/highlightedData");
const n8n_workflow_1 = require("n8n-workflow");
const a = __importStar(require("node:assert"));
const config_1 = require("@n8n/config");
const di_1 = require("@n8n/di");
const connect_panel_1 = require("./connect-panel");
const constants_1 = require("./constants");
const GenericFunctions_1 = require("./GenericFunctions");
const shell_1 = require("./shell");
const templates_1 = require("./templates");
const types_1 = require("./types");
const isPublicChatTriggerDisabled = () => di_1.Container.get(config_1.ChatTriggerConfig).disablePublicChat;
function isPlainObject(value) {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}
function withAuthenticatedUser(json, user, serverOwnsUserKey) {
if (!serverOwnsUserKey || !isPlainObject(json))
return json;
const { user: claimedUser, ...rest } = json;
if (!user)
return rest;
return {
...rest,
user: {
id: user.id,
email: user.email,
firstName: user.firstName,
lastName: user.lastName,
},
};
}
const allowFileUploadsOption = {
displayName: 'Allow File Uploads',
name: 'allowFileUploads',
type: 'boolean',
default: false,
description: 'Whether to allow file uploads in the chat',
};
const includeUserInOutputOption = {
displayName: 'Include User in Output',
name: 'includeUserInOutput',
type: 'boolean',
default: true,
description: "Whether to include the logged-in user's ID, email and name in the trigger output",
};
const allowedFileMimeTypeOption = {
displayName: 'Allowed File Mime Types',
name: 'allowedFilesMimeTypes',
type: 'string',
default: '*',
placeholder: 'e.g. image/*, text/*, application/pdf',
description: 'Allowed file types for upload. Comma-separated list of <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types" target="_blank">MIME types</a>.',
};
const respondToWebhookResponseMode = {
name: "Using 'Respond to Webhook' Node",
value: 'responseNode',
description: 'Response defined in that node',
};
const lastNodeResponseMode = {
name: 'When Last Node Finishes',
value: 'lastNode',
description: 'Returns data of the last-executed node',
};
const streamingResponseMode = {
name: 'Streaming',
value: 'streaming',
description: 'Streaming response from specified nodes (e.g. Agents)',
};
const respondNodesResponseMode = {
name: 'Using Response Nodes',
value: 'responseNodes',
description: 'Send responses to the chat by using one or more Chat nodes',
};
const responseModeBuilderHint = "'streaming' (preferred for Agent-backed chats): the connected Agent streams its reply to the widget directly — no extra wiring. Place logging or side-effects on a PARALLEL branch off the trigger or Agent, never inline after the Agent. 'lastNode': the last-executed node's output is sent to the widget — that node MUST emit `{ output: '<reply text>' }` (typically the Agent itself, or a Set node re-shaping data). NEVER terminate the chain with a Data Table insert, HTTP Request, or other side-effect node — their output is not a chat reply and the widget will error. 'responseNodes' / 'responseNode': requires explicit response nodes inside the flow (`@n8n/n8n-nodes-langchain.chat` for chat-hub mode, `n8n-nodes-base.respondToWebhook` for webhook mode).";
const commonOptionsFields = [
{
displayName: 'Allowed Origins (CORS)',
name: 'allowedOrigins',
type: 'string',
default: '*',
description: 'Comma-separated list of URLs allowed for cross-origin non-preflight requests. Use * (default) to allow all origins.',
displayOptions: {
show: {
'/mode': ['hostedChat', 'webhook'],
},
},
},
{
...allowFileUploadsOption,
displayOptions: {
show: {
'/mode': ['hostedChat'],
},
},
},
{
...allowedFileMimeTypeOption,
displayOptions: {
show: {
'/mode': ['hostedChat'],
},
},
},
{
displayName: 'Input Placeholder',
name: 'inputPlaceholder',
type: 'string',
displayOptions: {
show: {
'/mode': ['hostedChat'],
},
},
default: 'Type your question..',
placeholder: 'e.g. Type your message here',
description: 'Shown as placeholder text in the chat input field',
},
{
displayName: 'Load Previous Session',
name: 'loadPreviousSession',
type: 'options',
options: [
{
name: 'Off',
value: 'notSupported',
description: 'Loading messages of previous session is turned off',
},
{
name: 'From Memory',
value: 'memory',
description: 'Load session messages from memory',
},
{
name: 'Manually',
value: 'manually',
description: 'Manually return messages of session',
},
],
default: 'notSupported',
description: 'If loading messages of a previous session should be enabled',
builderHint: {
propertyHint: "This ONLY rehydrates the chat widget UI when the user reopens it — it does NOT give the Agent memory. The Agent gets memory from its own memory subnode regardless of this setting. Only set to 'memory' if the user wants the widget to restore visible history on reload; if so, you MUST also attach a memory subnode to this trigger (use the same memory node as the Agent so widget history matches what the Agent remembers). Otherwise leave as 'notSupported'.",
},
},
{
displayName: 'Require Button Click to Start Chat',
name: 'showWelcomeScreen',
type: 'boolean',
displayOptions: {
show: {
'/mode': ['hostedChat'],
},
},
default: false,
description: 'Whether to show the welcome screen at the start of the chat',
},
{
displayName: 'Start Conversation Button Text',
name: 'getStarted',
type: 'string',
displayOptions: {
show: {
showWelcomeScreen: [true],
'/mode': ['hostedChat'],
},
},
default: 'New Conversation',
placeholder: 'e.g. New Conversation',
description: 'Shown as part of the welcome screen, in the middle of the chat window',
},
{
displayName: 'Subtitle',
name: 'subtitle',
type: 'string',
displayOptions: {
show: {
'/mode': ['hostedChat'],
},
},
default: "Start a chat. We're here to help you 24/7.",
placeholder: "e.g. We're here for you",
description: 'Shown at the top of the chat, under the title',
},
{
displayName: 'Title',
name: 'title',
type: 'string',
displayOptions: {
show: {
'/mode': ['hostedChat'],
},
},
default: 'Hi there! 👋',
placeholder: 'e.g. Welcome',
description: 'Shown at the top of the chat',
},
{
displayName: 'Custom Chat Styling',
name: 'customCss',
type: 'string',
typeOptions: {
rows: 10,
editor: 'cssEditor',
},
displayOptions: {
show: {
'/mode': ['hostedChat'],
},
},
default: `
${constants_1.cssVariables}
/* You can override any class styles, too. Right-click inspect in Chat UI to find class to override. */
.chat-message {
max-width: 50%;
}
`.trim(),
description: 'Override default styling of the public chat interface with CSS',
},
];
class ChatTrigger extends n8n_workflow_1.Node {
constructor() {
super(...arguments);
this.description = {
displayName: 'Chat Trigger',
name: 'chatTrigger',
icon: 'node:chat-trigger',
iconColor: 'black',
group: ['trigger'],
version: [1, 1.1, 1.2, 1.3, 1.4, 1.5],
defaultVersion: 1.5,
description: 'Runs the workflow when an n8n generated webchat is submitted',
defaults: {
name: 'When chat message received',
},
codex: {
categories: ['Core Nodes'],
resources: {
primaryDocumentation: [
{
url: 'https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-langchain.chattrigger/',
},
],
},
},
maxNodes: 1,
inputs: `={{ (() => {
if (!['hostedChat', 'webhook'].includes($parameter.mode)) {
return [];
}
if ($parameter.options?.loadPreviousSession !== 'memory') {
return [];
}
return [
{
displayName: 'Memory',
maxConnections: 1,
type: '${n8n_workflow_1.NodeConnectionTypes.AiMemory}',
required: true,
}
];
})() }}`,
outputs: [n8n_workflow_1.NodeConnectionTypes.Main],
builderHint: {
searchHint: "Pair with `@n8n/n8n-nodes-langchain.agent` for chatbot workflows. Reply delivery is controlled by `options.responseMode` — `streaming` (Agent streams directly to widget) is simplest and preferred. For `lastNode` mode, the workflow's last-executed node MUST output `{ output: '<reply>' }` — typically the Agent itself or a Set node re-shaping data; ending the chain with a Data Table insert, HTTP Request, or other side-effect node will fail. Put logging or persistence on a parallel branch, not inline after the Agent.",
relatedNodes: [
{
nodeType: '@n8n/n8n-nodes-langchain.agent',
relationHint: "Main reply producer; use `responseMode: 'streaming'` so the Agent streams directly to the widget.",
},
{
nodeType: 'n8n-nodes-base.set',
relationHint: "Append at the end of a `responseMode: 'lastNode'` chain to re-shape the last node's output into `{ output: '<reply text>' }` when the natural last step (e.g. a Data Table insert) doesn't produce chat-shaped data.",
},
{
nodeType: '@n8n/n8n-nodes-langchain.chat',
relationHint: "Required for `responseMode: 'responseNodes'`. Place inside the flow wherever you want to emit a reply chunk.",
},
],
inputs: {
ai_memory: {
required: true,
displayOptions: {
show: {
mode: ['hostedChat', 'webhook'],
'options.loadPreviousSession': ['memory'],
},
},
},
},
},
credentials: [
{
name: 'httpBasicAuth',
required: true,
displayOptions: {
show: {
authentication: ['basicAuth'],
},
},
},
],
webhooks: [
{
name: 'setup',
httpMethod: 'GET',
responseMode: 'onReceived',
path: n8n_workflow_1.CHAT_TRIGGER_PATH_SUFFIX,
ndvHideUrl: true,
},
{
name: 'default',
httpMethod: 'POST',
responseMode: '={{$parameter.options?.["responseMode"] ?? ($parameter.availableInChat ? "streaming" : "lastNode") }}',
path: n8n_workflow_1.CHAT_TRIGGER_PATH_SUFFIX,
ndvHideMethod: true,
ndvHideUrl: isPublicChatTriggerDisabled() ? true : '={{ !$parameter.public }}',
},
],
eventTriggerDescription: 'Waiting for you to submit the chat',
activationMessage: isPublicChatTriggerDisabled()
? 'Public chat is disabled by instance policy.'
: 'You can now make calls to your production chat URL.',
triggerPanel: false,
properties: [
{
displayName: 'Make Chat Publicly Available',
name: 'public',
type: 'boolean',
default: false,
description: 'Whether the chat should be publicly available or only accessible through the manual chat interface',
},
{
displayName: 'Mode',
name: 'mode',
type: 'options',
options: [
{
name: 'Hosted Chat',
value: 'hostedChat',
description: 'Chat on a page served by n8n',
},
{
name: 'Embedded Chat',
value: 'webhook',
description: 'Chat through a widget embedded in another page, or by calling a webhook',
},
],
default: 'hostedChat',
displayOptions: {
show: {
public: [true],
},
},
},
{
displayName: 'Chat will be live at the URL above once this workflow is published. Live executions will show up in the ‘executions’ tab',
name: 'hostedChatNotice',
type: 'notice',
displayOptions: {
show: {
mode: ['hostedChat'],
public: [true],
},
},
default: '',
},
{
displayName: 'Follow the instructions <a href="https://www.npmjs.com/package/@n8n/chat" target="_blank">here</a> to embed chat in a webpage (or just call the webhook URL at the top of this section). Chat will be live once you publish this workflow',
name: 'embeddedChatNotice',
type: 'notice',
displayOptions: {
show: {
mode: ['webhook'],
public: [true],
},
},
default: '',
},
{
displayName: 'Authentication',
name: 'authentication',
type: 'options',
displayOptions: {
show: {
public: [true],
},
},
options: [
{
name: 'Basic Auth',
value: 'basicAuth',
description: 'Simple username and password (the same one for all users)',
},
{
name: 'n8n User Auth',
value: 'n8nUserAuth',
description: 'Require user to be logged in with their n8n account',
},
{
name: 'None',
value: 'none',
},
],
default: 'none',
description: 'The way to authenticate',
builderHint: {
propertyHint: "Default to 'none'. n8n exposes inbound trigger URLs publicly by design. Only select an authentication method when the user explicitly asks to authenticate inbound traffic.",
},
},
{
displayName: 'Require Workflow Execute Permission',
name: 'requireExecuteAccess',
type: 'boolean',
default: false,
displayOptions: {
show: {
authentication: ['n8nUserAuth'],
mode: ['hostedChat'],
public: [true],
},
},
description: 'Whether the triggering user must also have permission to execute the workflow in the project it belongs to',
},
{
...includeUserInOutputOption,
displayOptions: {
show: {
authentication: ['n8nUserAuth'],
public: [true],
'@version': [{ _cnd: { gte: 1.5 } }],
},
},
},
{
...includeUserInOutputOption,
default: false,
displayOptions: {
show: {
authentication: ['n8nUserAuth'],
public: [true],
'@version': [{ _cnd: { lt: 1.5 } }],
},
},
},
{
displayName: 'Initial Message(s)',
name: 'initialMessages',
type: 'string',
displayOptions: {
show: {
mode: ['hostedChat'],
public: [true],
},
},
typeOptions: {
rows: 3,
},
default: 'Hi there! 👋\nMy name is Nathan. How can I assist you today?',
description: 'Default messages shown at the start of the chat, one per line',
},
{
displayName: 'Make Available in n8n Chat Hub',
name: 'availableInChat',
type: 'boolean',
default: false,
noDataExpression: true,
description: 'Whether to make the agent available in n8n Chat Hub for n8n instance users to chat with',
},
{
displayName: 'Your Chat Trigger node is out of date. To update, delete this node and insert a new Chat Trigger node.',
name: 'availableInChatNotice',
type: 'notice',
displayOptions: {
show: {
availableInChat: [true],
'@version': [{ _cnd: { lt: 1.2 } }],
},
},
default: '',
},
{
displayName: 'Your n8n users will be able to use this agent in <a href="/home/chat/" target="_blank">Chat</a> once this workflow is published. Make sure to share this workflow with at least Project Chat User access to all users who should use it.',
name: 'availableInChatNotice',
type: 'notice',
displayOptions: {
show: {
availableInChat: [true],
'@version': [{ _cnd: { gte: 1.2 } }],
},
},
default: '',
},
{
displayName: 'Agent Icon',
name: 'agentIcon',
type: 'icon',
default: { type: 'icon', value: 'bot' },
noDataExpression: true,
description: 'The icon of the agent on n8n Chat',
displayOptions: {
show: {
availableInChat: [true],
'@version': [{ _cnd: { gte: 1.2 } }],
},
},
},
{
displayName: 'Agent Name',
name: 'agentName',
type: 'string',
default: '',
noDataExpression: true,
description: 'The name of the agent on n8n Chat. Name of the workflow is used if left empty.',
displayOptions: {
show: {
availableInChat: [true],
'@version': [{ _cnd: { gte: 1.2 } }],
},
},
},
{
displayName: 'Agent Description',
name: 'agentDescription',
type: 'string',
typeOptions: {
rows: 2,
},
default: '',
noDataExpression: true,
description: 'The description of the agent on n8n Chat',
displayOptions: {
show: {
availableInChat: [true],
'@version': [{ _cnd: { gte: 1.2 } }],
},
},
},
{
displayName: 'Suggestions',
name: 'suggestedPrompts',
type: 'fixedCollection',
typeOptions: { multipleValues: true, fixedCollection: { layout: 'inline' } },
default: {},
noDataExpression: true,
placeholder: 'Add Prompt',
description: 'Suggested prompts shown to users in n8n Chat Hub to start a conversation with the agent',
displayOptions: {
show: {
availableInChat: [true],
'@version': [{ _cnd: { gte: 1.2 } }],
},
},
options: [
{
name: 'prompts',
displayName: 'Prompts',
values: [
{
displayName: 'Icon',
name: 'icon',
type: 'icon',
noDataExpression: true,
default: { type: 'icon', value: 'comment' },
},
{
displayName: 'Prompt Text',
name: 'text',
type: 'string',
default: '',
noDataExpression: true,
required: true,
},
],
},
],
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
displayOptions: {
show: {
public: [false],
'@version': [1, 1.1],
},
},
placeholder: 'Add Field',
default: {},
options: [allowFileUploadsOption, allowedFileMimeTypeOption],
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
displayOptions: {
show: {
mode: ['hostedChat', 'webhook'],
public: [true],
'@version': [1, 1.1],
},
},
placeholder: 'Add Field',
default: {},
options: [
...commonOptionsFields,
{
displayName: 'Response Mode',
name: 'responseMode',
type: 'options',
options: [lastNodeResponseMode, respondToWebhookResponseMode],
default: 'lastNode',
description: 'When and how to respond to the webhook',
builderHint: { propertyHint: responseModeBuilderHint },
},
highlightedData_1.autoSaveHighlightedDataProperty,
],
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
displayOptions: {
show: {
mode: ['hostedChat', 'webhook'],
public: [true],
'@version': [1.2],
},
},
placeholder: 'Add Field',
default: {},
options: [
...commonOptionsFields,
{
displayName: 'Response Mode',
name: 'responseMode',
type: 'options',
options: [lastNodeResponseMode, respondToWebhookResponseMode, streamingResponseMode],
default: 'lastNode',
description: 'When and how to respond to the webhook',
displayOptions: { show: { '/availableInChat': [false] } },
builderHint: { propertyHint: responseModeBuilderHint },
},
{
displayName: 'Response Mode',
name: 'responseMode',
type: 'options',
options: [streamingResponseMode, lastNodeResponseMode],
default: 'streaming',
description: 'When and how to respond to the webhook',
displayOptions: { show: { '/availableInChat': [true] } },
builderHint: { propertyHint: responseModeBuilderHint },
},
highlightedData_1.autoSaveHighlightedDataProperty,
],
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
displayOptions: {
show: {
public: [false],
'@version': [{ _cnd: { gte: 1.3 } }],
},
},
placeholder: 'Add Field',
default: {},
options: [
allowFileUploadsOption,
allowedFileMimeTypeOption,
{
displayName: 'Response Mode',
name: 'responseMode',
type: 'options',
options: [lastNodeResponseMode, respondNodesResponseMode, streamingResponseMode],
default: 'lastNode',
description: 'When and how to respond to the chat',
displayOptions: { show: { '/availableInChat': [false] } },
builderHint: { propertyHint: responseModeBuilderHint },
},
{
displayName: 'Response Mode',
name: 'responseMode',
type: 'options',
options: [streamingResponseMode, lastNodeResponseMode, respondNodesResponseMode],
default: 'streaming',
description: 'When and how to respond to the chat',
displayOptions: { show: { '/availableInChat': [true] } },
builderHint: { propertyHint: responseModeBuilderHint },
},
highlightedData_1.autoSaveHighlightedDataProperty,
],
},
{
displayName: 'Options',
name: 'options',
type: 'collection',
displayOptions: {
show: {
mode: ['hostedChat', 'webhook'],
public: [true],
'@version': [{ _cnd: { gte: 1.3 } }],
},
},
placeholder: 'Add Field',
default: {},
options: [
...commonOptionsFields,
{
displayName: 'Response Mode',
name: 'responseMode',
type: 'options',
options: [lastNodeResponseMode, streamingResponseMode, respondToWebhookResponseMode],
default: 'lastNode',
description: 'When and how to respond to the chat',
displayOptions: { show: { '/mode': ['webhook'], '/availableInChat': [false] } },
builderHint: { propertyHint: responseModeBuilderHint },
},
{
displayName: 'Response Mode',
name: 'responseMode',
type: 'options',
options: [streamingResponseMode, lastNodeResponseMode],
default: 'streaming',
description: 'When and how to respond to the chat',
displayOptions: { show: { '/mode': ['webhook'], '/availableInChat': [true] } },
builderHint: { propertyHint: responseModeBuilderHint },
},
{
displayName: 'Response Mode',
name: 'responseMode',
type: 'options',
options: [lastNodeResponseMode, streamingResponseMode, respondNodesResponseMode],
default: 'lastNode',
description: 'When and how to respond to the chat',
displayOptions: { show: { '/mode': ['hostedChat'], '/availableInChat': [false] } },
builderHint: { propertyHint: responseModeBuilderHint },
},
{
displayName: 'Response Mode',
name: 'responseMode',
type: 'options',
options: [streamingResponseMode, lastNodeResponseMode, respondNodesResponseMode],
default: 'streaming',
description: 'When and how to respond to the chat',
displayOptions: { show: { '/mode': ['hostedChat'], '/availableInChat': [true] } },
builderHint: { propertyHint: responseModeBuilderHint },
},
highlightedData_1.autoSaveHighlightedDataProperty,
],
},
],
};
}
async handleFormData(context) {
const req = context.getRequestObject();
a.ok(req.contentType === 'multipart/form-data', 'Expected multipart/form-data');
const options = context.getNodeParameter('options', {});
const { data, files } = req.body;
const returnItem = {
json: data,
};
if (files && Object.keys(files).length) {
returnItem.json.files = [];
returnItem.binary = {};
const count = 0;
for (const fileKey of Object.keys(files)) {
const processedFiles = [];
if (Array.isArray(files[fileKey])) {
processedFiles.push(...files[fileKey]);
}
else {
processedFiles.push(files[fileKey]);
}
let fileIndex = 0;
for (const file of processedFiles) {
let binaryPropertyName = 'data';
if (binaryPropertyName.endsWith('[]')) {
binaryPropertyName = binaryPropertyName.slice(0, -2);
}
if (options.binaryPropertyName) {
binaryPropertyName = `${options.binaryPropertyName.toString()}${count}`;
}
const binaryFile = await context.nodeHelpers.copyBinaryFile(file.filepath, file.originalFilename ?? file.newFilename, file.mimetype);
const binaryKey = `${binaryPropertyName}${fileIndex}`;
const binaryInfo = {
...(0, pick_1.default)(binaryFile, ['fileName', 'fileSize', 'fileType', 'mimeType', 'fileExtension']),
binaryKey,
};
returnItem.binary = Object.assign(returnItem.binary ?? {}, {
[`${binaryKey}`]: binaryFile,
});
returnItem.json.files = [
...returnItem.json.files,
binaryInfo,
];
fileIndex += 1;
}
}
}
return returnItem;
}
async webhook(ctx) {
const res = ctx.getResponseObject();
const isPublic = isPublicChatTriggerDisabled() ? false : ctx.getNodeParameter('public', false);
(0, n8n_workflow_1.assertParamIsBoolean)('public', isPublic, ctx.getNode());
const nodeMode = ctx.getNodeParameter('mode', 'hostedChat');
(0, n8n_workflow_1.assertParamIsString)('mode', nodeMode, ctx.getNode());
const mode = ctx.getMode() === 'manual' ? 'test' : 'production';
if (!isPublic && (mode !== 'test' || !ctx.isChatSessionTest())) {
res.status(404).end();
return {
noWebhookResponse: true,
};
}
const availableInChat = ctx.getNodeParameter('availableInChat', false);
const options = ctx.getNodeParameter('options', {});
(0, n8n_workflow_1.validateNodeParameters)(options, {
getStarted: { type: 'string' },
inputPlaceholder: { type: 'string' },
loadPreviousSession: { type: 'string' },
showWelcomeScreen: { type: 'boolean' },
subtitle: { type: 'string' },
title: { type: 'string' },
allowFileUploads: { type: 'boolean' },
allowedFilesMimeTypes: { type: 'string' },
customCss: { type: 'string' },
responseMode: { type: 'string' },
[highlightedData_1.autoSaveHighlightedDataProperty.name]: { type: 'boolean' },
}, ctx.getNode());
const loadPreviousSession = options.loadPreviousSession;
(0, types_1.assertValidLoadPreviousSessionOption)(loadPreviousSession, ctx.getNode());
const enableStreaming = availableInChat
? !options.responseMode || options.responseMode === 'streaming'
: options.responseMode === 'streaming';
const req = ctx.getRequestObject();
const webhookName = ctx.getWebhookName();
const bodyData = ctx.getBodyData() ?? {};
const authentication = ctx.getNodeParameter('authentication', 'none');
let authedUser;
if (mode === 'test' && ctx.isChatSessionTest()) {
if (authentication === 'n8nUserAuth') {
authedUser = await ctx.getTestWebhookUser?.();
}
}
else {
try {
authedUser = await (0, GenericFunctions_1.validateAuth)(ctx);
}
catch (error) {
if (error) {
const webhookId = ctx.getNode().webhookId;
const realm = webhookId ? `Webhook ${webhookId}` : 'Webhook';
res.writeHead(error.responseCode, {
'www-authenticate': `Basic realm="${realm}"`,
});
res.end(error.message);
return { noWebhookResponse: true };
}
throw error;
}
}
if (nodeMode === 'hostedChat') {
if (webhookName === 'setup') {
const webhookUrlRaw = ctx.getNodeWebhookUrl('default');
if (!webhookUrlRaw) {
throw new n8n_workflow_1.NodeOperationError(ctx.getNode(), 'Default webhook url not set');
}
const webhookUrl = mode === 'test' ? webhookUrlRaw.replace('/webhook', '/webhook-test') : webhookUrlRaw;
const authentication = ctx.getNodeParameter('authentication');
const initialMessagesRaw = ctx.getNodeParameter('initialMessages', '');
(0, n8n_workflow_1.assertParamIsString)('initialMessage', initialMessagesRaw, ctx.getNode());
const instanceId = ctx.getInstanceId();
const i18nConfig = {};
const keys = ['getStarted', 'inputPlaceholder', 'subtitle', 'title'];
for (const key of keys) {
if (options[key] !== undefined) {
i18nConfig[key] = options[key];
}
}
let frameIdentity;
if (authentication === 'n8nUserAuth') {
const resourceUrl = ctx.getWebhookResourceUrl('default');
if (!resourceUrl) {
throw new n8n_workflow_1.NodeOperationError(ctx.getNode(), 'Default webhook url not set');
}
if ((0, shell_1.isChatRefreshRequest)(req)) {
await (0, GenericFunctions_1.handleChatTokenRefresh)(ctx, resourceUrl);
return { noWebhookResponse: true };
}
if (!(0, shell_1.isShellInnerRequest)(req)) {
const outerIdentity = await (0, GenericFunctions_1.establishChatSessionIdentity)(ctx, resourceUrl);
if (!outerIdentity) {
return { noWebhookResponse: true };
}
let credentialStatus;
try {
credentialStatus = await ctx.checkTriggerCredentialStatus();
}
catch {
ctx.logger.error('Chat trigger credential readiness check failed');
res.status(503).send('Chat is unavailable right now. Please try again later.');
return { noWebhookResponse: true };
}
const connect = credentialStatus?.credentials.length
? (0, connect_panel_1.buildChatShellViewModel)(credentialStatus.credentials, outerIdentity.visitor.email)
: undefined;
res.setHeader('Content-Security-Policy', "frame-ancestors 'none'");
res.status(200).render('chat-shell', {
iframeSrc: (0, shell_1.buildInnerFrameSrc)(req),
sandbox: shell_1.CHAT_FRAME_SANDBOX,
refreshUrl: (0, shell_1.buildChatRefreshUrl)(req),
refreshExpiresIn: Math.max(0, Math.round(outerIdentity.expiresIn)),
testMode: mode === 'test',
visitorEmail: outerIdentity.visitor.email,
hasCredentials: !!connect,
ready: connect ? connect.connectedCount >= connect.total : false,
barText: connect ? (0, connect_panel_1.connectBarText)(connect, mode === 'test') : '',
...connect,
});
return { noWebhookResponse: true };
}
const identity = await (0, GenericFunctions_1.resolveInnerFrameIdentity)(ctx, resourceUrl);
if (!identity) {
res.status(401).send('Session expired. Please reload the page.');
res.end();
return { noWebhookResponse: true };
}
frameIdentity = identity;
res.setHeader('Content-Security-Policy', `sandbox ${shell_1.CHAT_FRAME_SANDBOX}`);
}
const page = (0, templates_1.createPage)({
i18n: {
en: i18nConfig,
},
showWelcomeScreen: options.showWelcomeScreen,
loadPreviousSession,
initialMessages: initialMessagesRaw,
webhookUrl,
mode,
instanceId,
authentication,
allowFileUploads: options.allowFileUploads,
allowedFilesMimeTypes: options.allowedFilesMimeTypes,
customCss: options.customCss,
enableStreaming,
frameIdentity,
});
res.status(200).send(page).end();
return {
noWebhookResponse: true,
};
}
}
if (bodyData.action === 'loadPreviousSession') {
if (options?.loadPreviousSession === 'memory') {
const memory = (await ctx.getInputConnectionData(n8n_workflow_1.NodeConnectionTypes.AiMemory, 0));
const messages = ((await memory?.chatHistory.getMessages()) ?? [])
.filter((message) => !message?.additional_kwargs?.hideFromUI)
.map((message) => message?.toJSON());
return {
webhookResponse: { data: messages },
};
}
else if (!options?.loadPreviousSession || options?.loadPreviousSession === 'notSupported') {
return {
webhookResponse: { data: [] },
};
}
}
else {
let readiness;
try {
readiness = await ctx.checkTriggerCredentialStatus();
}
catch {
ctx.logger.error('Chat trigger credential readiness check failed');
res.status(503).json({ status: 'credential_readiness_check_failed' });
return { noWebhookResponse: true };
}
if (readiness && !readiness.readyToExecute) {
res.status(428).json((0, n8n_workflow_1.buildCredentialConnectionsRequiredResponse)(readiness));
return { noWebhookResponse: true };
}
}
if (ctx.getNodeParameter('options.autoSaveHighlightedData', true) !== false) {
if (typeof bodyData.chatInput === 'string') {
ctx.customData.set((0, n8n_workflow_1.getHighlightedInputKey)(ctx.getNode().name), bodyData.chatInput);
}
if (typeof bodyData.sessionId === 'string') {
ctx.customData.set(n8n_workflow_1.HIGHLIGHTED_SESSION_KEY, bodyData.sessionId);
}
}
const webhookResponse = { status: 200 };
if (enableStreaming) {
req.socket.setTimeout(0);
req.socket.setNoDelay(true);
req.socket.setKeepAlive(true);
res.writeHead(200, {
'Content-Type': 'application/json; charset=utf-8',
'Transfer-Encoding': 'chunked',
'Cache-Control': 'no-cache, no-transform',
Connection: 'keep-alive',
});
res.flushHeaders();
}
const isMultipart = req.contentType === 'multipart/form-data';
const item = isMultipart ? await this.handleFormData(ctx) : { json: bodyData };
const serverOwnsUserKey = authentication === 'n8nUserAuth';
const includeUser = serverOwnsUserKey &&
ctx.getNodeParameter('includeUserInOutput', ctx.getNode().typeVersion >= 1.5) !== false;
const returnItem = {
...item,
json: withAuthenticatedUser(item.json, includeUser ? authedUser : undefined, serverOwnsUserKey),
};
const returnData = [returnItem];
if (enableStreaming) {
return {
workflowData: [ctx.helpers.returnJsonArray(returnData)],
noWebhookResponse: true,
};
}
if (isMultipart) {
return {
webhookResponse,
workflowData: [returnData],
};
}
return {
webhookResponse,
workflowData: [ctx.helpers.returnJsonArray(returnData)],
};
}
}
exports.ChatTrigger = ChatTrigger;
//# sourceMappingURL=ChatTrigger.node.js.map