fix(editor): Reduce cases for Auto-Add of ChatTrigger for AI Agents (#12154)

This commit is contained in:
Charlie Kolb 2024-12-12 08:35:08 +01:00 committed by GitHub
parent 614aad55f0
commit 365e82d200
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 28 deletions

View file

@ -212,24 +212,10 @@ export const useActions = () => {
]; ];
const isCompatibleNode = addedNodes.some((node) => COMPATIBLE_CHAT_NODES.includes(node.type)); const isCompatibleNode = addedNodes.some((node) => COMPATIBLE_CHAT_NODES.includes(node.type));
if (!isCompatibleNode) return false; if (!isCompatibleNode) return false;
const { allNodes, getNodeTypes } = useWorkflowsStore(); const { allNodes } = useWorkflowsStore();
const { getByNameAndVersion } = getNodeTypes(); return allNodes.filter((x) => x.type !== MANUAL_TRIGGER_NODE_TYPE).length === 0;
// 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;
} }
// AI-226: Prepend LLM Chain node when adding a language model // AI-226: Prepend LLM Chain node when adding a language model

View file

@ -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(); const workflowsStore = useWorkflowsStore();
vi.spyOn(workflowsStore, 'workflowTriggerNodes', 'get').mockReturnValue([]); vi.spyOn(workflowsStore, 'workflowTriggerNodes', 'get').mockReturnValue([]);
vi.spyOn(workflowsStore, 'allNodes', 'get').mockReturnValue([]);
const { getAddedNodesAndConnections } = useActions(); 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(); const workflowsStore = useWorkflowsStore();
vi.spyOn(workflowsStore, 'workflowTriggerNodes', 'get').mockReturnValue([ vi.spyOn(workflowsStore, 'workflowTriggerNodes', 'get').mockReturnValue([
{ type: MANUAL_TRIGGER_NODE_TYPE } as never, { type: MANUAL_TRIGGER_NODE_TYPE } as never,
]); ]);
vi.spyOn(workflowsStore, 'getNodeTypes').mockReturnValue({ vi.spyOn(workflowsStore, 'allNodes', 'get').mockReturnValue([
getByNameAndVersion: () => ({ description: { group: ['trigger'] } }), { type: MANUAL_TRIGGER_NODE_TYPE } as never,
} as never); ]);
const { getAddedNodesAndConnections } = useActions(); 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(); const workflowsStore = useWorkflowsStore();
vi.spyOn(workflowsStore, 'workflowTriggerNodes', 'get').mockReturnValue([
{ type: GITHUB_TRIGGER_NODE_TYPE } as never,
]);
vi.spyOn(workflowsStore, 'allNodes', 'get').mockReturnValue([ vi.spyOn(workflowsStore, 'allNodes', 'get').mockReturnValue([
{ type: GITHUB_TRIGGER_NODE_TYPE } as never, { 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(); const { getAddedNodesAndConnections } = useActions();
@ -155,9 +158,6 @@ describe('useActions', () => {
vi.spyOn(workflowsStore, 'allNodes', 'get').mockReturnValue([ vi.spyOn(workflowsStore, 'allNodes', 'get').mockReturnValue([
{ type: CHAT_TRIGGER_NODE_TYPE } as never, { type: CHAT_TRIGGER_NODE_TYPE } as never,
]); ]);
vi.spyOn(workflowsStore, 'getNodeTypes').mockReturnValue({
getByNameAndVersion: () => ({ description: { group: ['trigger'] } }),
} as never);
const { getAddedNodesAndConnections } = useActions(); const { getAddedNodesAndConnections } = useActions();