From 365e82d2008dff2f9c91664ee04d7a78363a8b30 Mon Sep 17 00:00:00 2001 From: Charlie Kolb Date: Thu, 12 Dec 2024 08:35:08 +0100 Subject: [PATCH] fix(editor): Reduce cases for Auto-Add of ChatTrigger for AI Agents (#12154) --- .../NodeCreator/composables/useActions.ts | 18 ++------------ .../Node/NodeCreator/useActions.test.ts | 24 +++++++++---------- 2 files changed, 14 insertions(+), 28 deletions(-) diff --git a/packages/editor-ui/src/components/Node/NodeCreator/composables/useActions.ts b/packages/editor-ui/src/components/Node/NodeCreator/composables/useActions.ts index 92a5563d80d8f..e92097bbddae1 100644 --- a/packages/editor-ui/src/components/Node/NodeCreator/composables/useActions.ts +++ b/packages/editor-ui/src/components/Node/NodeCreator/composables/useActions.ts @@ -212,24 +212,10 @@ export const useActions = () => { ]; const isCompatibleNode = addedNodes.some((node) => COMPATIBLE_CHAT_NODES.includes(node.type)); - if (!isCompatibleNode) return false; - const { allNodes, getNodeTypes } = useWorkflowsStore(); - const { getByNameAndVersion } = getNodeTypes(); - - // We want to add a trigger if there are no triggers other than Manual Triggers - // Performance here should be fine as `getByNameAndVersion` fetches nodeTypes once in bulk - // and `every` aborts on first `false` - const shouldAddChatTrigger = allNodes.every((node) => { - const nodeType = getByNameAndVersion(node.type, node.typeVersion); - - return ( - !nodeType.description.group.includes('trigger') || node.type === MANUAL_TRIGGER_NODE_TYPE - ); - }); - - return shouldAddChatTrigger; + const { allNodes } = useWorkflowsStore(); + return allNodes.filter((x) => x.type !== MANUAL_TRIGGER_NODE_TYPE).length === 0; } // AI-226: Prepend LLM Chain node when adding a language model diff --git a/packages/editor-ui/src/components/Node/NodeCreator/useActions.test.ts b/packages/editor-ui/src/components/Node/NodeCreator/useActions.test.ts index cf8fa94014ba1..386ff58c6eefe 100644 --- a/packages/editor-ui/src/components/Node/NodeCreator/useActions.test.ts +++ b/packages/editor-ui/src/components/Node/NodeCreator/useActions.test.ts @@ -73,10 +73,11 @@ describe('useActions', () => { }); }); - test('should insert a ChatTrigger node when an AI Agent is added without trigger', () => { + test('should insert a ChatTrigger node when an AI Agent is added on an empty canvas', () => { const workflowsStore = useWorkflowsStore(); vi.spyOn(workflowsStore, 'workflowTriggerNodes', 'get').mockReturnValue([]); + vi.spyOn(workflowsStore, 'allNodes', 'get').mockReturnValue([]); const { getAddedNodesAndConnections } = useActions(); @@ -98,15 +99,15 @@ describe('useActions', () => { }); }); - test('should insert a ChatTrigger node when an AI Agent is added with only a Manual Trigger', () => { + test('should insert a ChatTrigger node when an AI Agent is added with only a Manual Trigger present', () => { const workflowsStore = useWorkflowsStore(); vi.spyOn(workflowsStore, 'workflowTriggerNodes', 'get').mockReturnValue([ { type: MANUAL_TRIGGER_NODE_TYPE } as never, ]); - vi.spyOn(workflowsStore, 'getNodeTypes').mockReturnValue({ - getByNameAndVersion: () => ({ description: { group: ['trigger'] } }), - } as never); + vi.spyOn(workflowsStore, 'allNodes', 'get').mockReturnValue([ + { type: MANUAL_TRIGGER_NODE_TYPE } as never, + ]); const { getAddedNodesAndConnections } = useActions(); @@ -128,15 +129,17 @@ describe('useActions', () => { }); }); - test('should not insert a ChatTrigger node when an AI Agent is added with a trigger already present', () => { + test('should not insert a ChatTrigger node when an AI Agent is added with a non-trigger node prseent', () => { const workflowsStore = useWorkflowsStore(); + vi.spyOn(workflowsStore, 'workflowTriggerNodes', 'get').mockReturnValue([ + { type: GITHUB_TRIGGER_NODE_TYPE } as never, + ]); + vi.spyOn(workflowsStore, 'allNodes', 'get').mockReturnValue([ { type: GITHUB_TRIGGER_NODE_TYPE } as never, + { type: HTTP_REQUEST_NODE_TYPE } as never, ]); - vi.spyOn(workflowsStore, 'getNodeTypes').mockReturnValue({ - getByNameAndVersion: () => ({ description: { group: ['trigger'] } }), - } as never); const { getAddedNodesAndConnections } = useActions(); @@ -155,9 +158,6 @@ describe('useActions', () => { vi.spyOn(workflowsStore, 'allNodes', 'get').mockReturnValue([ { type: CHAT_TRIGGER_NODE_TYPE } as never, ]); - vi.spyOn(workflowsStore, 'getNodeTypes').mockReturnValue({ - getByNameAndVersion: () => ({ description: { group: ['trigger'] } }), - } as never); const { getAddedNodesAndConnections } = useActions();