Skip to content

Commit

Permalink
fix(Chat Trigger Node): Fix Allowed Origins paramter (#11011)
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegIvaniv authored Sep 30, 2024
1 parent d2238b9 commit b5f4afe
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Node, NodeConnectionType } from 'n8n-workflow';
import type { BaseChatMemory } from '@langchain/community/memory/chat_memory';
import { pick } from 'lodash';
import { Node, NodeConnectionType, commonCORSParameters } from 'n8n-workflow';
import type {
IDataObject,
IWebhookFunctions,
Expand All @@ -10,10 +12,8 @@ import type {
INodeProperties,
} from 'n8n-workflow';

import { pick } from 'lodash';
import type { BaseChatMemory } from '@langchain/community/memory/chat_memory';
import { createPage } from './templates';
import { validateAuth } from './GenericFunctions';
import { createPage } from './templates';
import type { LoadPreviousSessionChatOption } from './types';

const CHAT_TRIGGER_PATH_IDENTIFIER = 'chat';
Expand Down Expand Up @@ -56,7 +56,6 @@ export class ChatTrigger extends Node {
],
},
},
supportsCORS: true,
maxNodes: 1,
inputs: `={{ (() => {
if (!['hostedChat', 'webhook'].includes($parameter.mode)) {
Expand Down Expand Up @@ -241,6 +240,15 @@ export class ChatTrigger extends Node {
placeholder: 'Add Field',
default: {},
options: [
// CORS parameters are only valid for when chat is used in hosted or webhook mode
...commonCORSParameters.map((p) => ({
...p,
displayOptions: {
show: {
'/mode': ['hostedChat', 'webhook'],
},
},
})),
{
...allowFileUploadsOption,
displayOptions: {
Expand Down
16 changes: 11 additions & 5 deletions packages/cli/src/webhooks/live-webhooks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Response } from 'express';
import { Workflow, NodeHelpers } from 'n8n-workflow';
import { Workflow, NodeHelpers, CHAT_TRIGGER_NODE_TYPE } from 'n8n-workflow';
import type { INode, IWebhookData, IHttpRequestMethods } from 'n8n-workflow';
import { Service } from 'typedi';

Expand Down Expand Up @@ -47,12 +47,18 @@ export class LiveWebhooks implements IWebhookManager {
select: ['nodes'],
});

const isChatWebhookNode = (type: string, webhookId?: string) =>
type === CHAT_TRIGGER_NODE_TYPE && `${webhookId}/chat` === path;

const nodes = workflowData?.nodes;
const webhookNode = nodes?.find(
({ type, parameters, typeVersion }) =>
parameters?.path === path &&
(parameters?.httpMethod ?? 'GET') === httpMethod &&
'webhook' in this.nodeTypes.getByNameAndVersion(type, typeVersion),
({ type, parameters, typeVersion, webhookId }) =>
(parameters?.path === path &&
(parameters?.httpMethod ?? 'GET') === httpMethod &&
'webhook' in this.nodeTypes.getByNameAndVersion(type, typeVersion)) ||
// Chat Trigger has doesn't have configurable path and is always using POST, so
// we need to use webhookId for matching
isChatWebhookNode(type, webhookId),
);
return webhookNode?.parameters?.options as WebhookAccessControlOptions;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/workflow/src/NodeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ const commonPollingParameters: INodeProperties[] = [
},
];

const commonCORSParameters: INodeProperties[] = [
export const commonCORSParameters: INodeProperties[] = [
{
displayName: 'Allowed Origins (CORS)',
name: 'allowedOrigins',
Expand Down

0 comments on commit b5f4afe

Please sign in to comment.