Skip to content

Commit

Permalink
fix(editor): Reduce cases for Auto-Add of ChatTrigger for AI Agents (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlieKolb authored Dec 12, 2024
1 parent 614aad5 commit 365e82d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand All @@ -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();

Expand Down

0 comments on commit 365e82d

Please sign in to comment.