PR Feedback

This commit is contained in:
Charlie Kolb 2024-11-18 16:39:34 +01:00
parent 4117b5298b
commit 144f9fb44f
No known key found for this signature in database
3 changed files with 90 additions and 88 deletions

View file

@ -658,6 +658,7 @@ export function useCanvasOperations({ router }: { router: ReturnType<typeof useR
const lastInteractedWithNodeConnection = uiStore.lastInteractedWithNodeConnection;
const lastInteractedWithNodeHandle = uiStore.lastInteractedWithNodeHandle;
let connectedNodes: ReturnType<typeof createConnection> | undefined;
// If we have a specific endpoint to connect to
if (lastInteractedWithNodeHandle) {
const { type: connectionType, mode } = parseCanvasConnectionHandleString(
@ -672,46 +673,37 @@ export function useCanvasOperations({ router }: { router: ReturnType<typeof useR
});
if (mode === CanvasConnectionMode.Input) {
createConnection(
{
source: nodeId,
sourceHandle: nodeHandle,
target: lastInteractedWithNodeId,
targetHandle: lastInteractedWithNodeHandle,
},
{ parentIsNew: true },
);
connectedNodes = createConnection({
source: nodeId,
sourceHandle: nodeHandle,
target: lastInteractedWithNodeId,
targetHandle: lastInteractedWithNodeHandle,
});
} else {
createConnection(
{
source: lastInteractedWithNodeId,
sourceHandle: lastInteractedWithNodeHandle,
target: nodeId,
targetHandle: nodeHandle,
},
{ childIsNew: true },
);
connectedNodes = createConnection({
source: lastInteractedWithNodeId,
sourceHandle: lastInteractedWithNodeHandle,
target: nodeId,
targetHandle: nodeHandle,
});
}
} else {
// If a node is last selected then connect between the active and its child ones
// Connect active node to the newly created one
createConnection(
{
source: lastInteractedWithNodeId,
sourceHandle: createCanvasConnectionHandleString({
mode: CanvasConnectionMode.Output,
type: NodeConnectionType.Main,
index: 0,
}),
target: node.id,
targetHandle: createCanvasConnectionHandleString({
mode: CanvasConnectionMode.Input,
type: NodeConnectionType.Main,
index: 0,
}),
},
{ childIsNew: true },
);
connectedNodes = createConnection({
source: lastInteractedWithNodeId,
sourceHandle: createCanvasConnectionHandleString({
mode: CanvasConnectionMode.Output,
type: NodeConnectionType.Main,
index: 0,
}),
target: node.id,
targetHandle: createCanvasConnectionHandleString({
mode: CanvasConnectionMode.Input,
type: NodeConnectionType.Main,
index: 0,
}),
});
}
if (lastInteractedWithNodeConnection) {
@ -719,21 +711,26 @@ export function useCanvasOperations({ router }: { router: ReturnType<typeof useR
const targetNode = workflowsStore.getNodeById(lastInteractedWithNodeConnection.target);
if (targetNode) {
createConnection(
{
source: node.id,
sourceHandle: createCanvasConnectionHandleString({
mode: CanvasConnectionMode.Input,
type: NodeConnectionType.Main,
index: 0,
}),
target: lastInteractedWithNodeConnection.target,
targetHandle: lastInteractedWithNodeConnection.targetHandle,
},
{ parentIsNew: true },
);
connectedNodes = createConnection({
source: node.id,
sourceHandle: createCanvasConnectionHandleString({
mode: CanvasConnectionMode.Input,
type: NodeConnectionType.Main,
index: 0,
}),
target: lastInteractedWithNodeConnection.target,
targetHandle: lastInteractedWithNodeConnection.targetHandle,
});
}
}
if (connectedNodes) {
const { sourceNode, targetNode } = connectedNodes;
adjustNewNodes(sourceNode, targetNode, {
sourceIsNew: sourceNode.id === node.id,
targetIsNew: targetNode.id === node.id,
});
}
}
function trackAddNode(nodeData: INodeUi, options: AddNodeOptions) {
@ -1089,7 +1086,7 @@ export function useCanvasOperations({ router }: { router: ReturnType<typeof useR
function createConnection(
connection: Connection,
{ trackHistory = false, keepPristine = false, parentIsNew = false, childIsNew = false } = {},
{ trackHistory = false, keepPristine = false } = {},
) {
const sourceNode = workflowsStore.getNodeById(connection.source);
const targetNode = workflowsStore.getNodeById(connection.target);
@ -1115,7 +1112,6 @@ export function useCanvasOperations({ router }: { router: ReturnType<typeof useR
return;
}
adjustNewNodes(sourceNode, targetNode, { parentIsNew, childIsNew });
workflowsStore.addConnection({
connection: mappedConnection,
});
@ -1128,6 +1124,8 @@ export function useCanvasOperations({ router }: { router: ReturnType<typeof useR
if (!keepPristine) {
uiStore.stateIsDirty = true;
}
return { sourceNode, targetNode };
}
function revertCreateConnection(connection: [IConnection, IConnection]) {

View file

@ -2,11 +2,11 @@ import { AGENT_NODE_TYPE, CHAT_TRIGGER_NODE_TYPE, MANUAL_TRIGGER_NODE_TYPE } fro
import { adjustNewNodes } from '@/utils/connectionNodeUtils';
import { createPinia, setActivePinia } from 'pinia';
const getParentNodesByDepth = vi.fn();
const getsourceNodesByDepth = vi.fn();
const getNode = vi.fn();
vi.mock('@/stores/workflow.store', () => ({
useWorkflowsStore: vi.fn(() => ({
getParentNodesByDepth,
getsourceNodesByDepth,
getNode,
})),
}));
@ -21,12 +21,12 @@ describe('adjustNewlyConnectedNodes', () => {
});
it('modifies promptType with ChatTrigger->new Agent', () => {
const parent = { type: CHAT_TRIGGER_NODE_TYPE };
const child = { type: AGENT_NODE_TYPE };
const source = { type: CHAT_TRIGGER_NODE_TYPE };
const target = { type: AGENT_NODE_TYPE };
adjustNewNodes(parent, child, { parentIsNew: false });
adjustNewNodes(source, target, { sourceIsNew: false });
expect(child).toEqual({
expect(target).toEqual({
type: AGENT_NODE_TYPE,
parameters: {
promptType: 'auto',
@ -36,12 +36,12 @@ describe('adjustNewlyConnectedNodes', () => {
});
it('modifies promptType with new ChatTrigger->new Agent', () => {
const parent = { type: CHAT_TRIGGER_NODE_TYPE };
const child = { type: AGENT_NODE_TYPE };
const source = { type: CHAT_TRIGGER_NODE_TYPE };
const target = { type: AGENT_NODE_TYPE };
adjustNewNodes(parent, child);
adjustNewNodes(source, target);
expect(child).toEqual({
expect(target).toEqual({
type: AGENT_NODE_TYPE,
parameters: {
promptType: 'auto',
@ -51,42 +51,42 @@ describe('adjustNewlyConnectedNodes', () => {
});
it('does not modify promptType with ManualTrigger->new Agent', () => {
const parent = { type: MANUAL_TRIGGER_NODE_TYPE };
const child = { type: AGENT_NODE_TYPE };
const source = { type: MANUAL_TRIGGER_NODE_TYPE };
const target = { type: AGENT_NODE_TYPE };
adjustNewNodes(parent, child, { parentIsNew: false });
adjustNewNodes(source, target, { sourceIsNew: false });
expect(child).toEqual({
expect(target).toEqual({
type: AGENT_NODE_TYPE,
});
});
it('modifies sessionId with ChatTrigger->(new Memory->Agent)', () => {
const trigger = { type: CHAT_TRIGGER_NODE_TYPE, name: 'trigger' };
getParentNodesByDepth.mockReturnValue([{ name: trigger.name }]);
getsourceNodesByDepth.mockReturnValue([{ name: trigger.name }]);
getNode.mockReturnValue({ type: trigger.type });
const child = { type: AGENT_NODE_TYPE };
const parent = { type: '@n8n/n8n-nodes-langchain.memoryBufferWindow' };
const target = { type: AGENT_NODE_TYPE };
const source = { type: '@n8n/n8n-nodes-langchain.memoryBufferWindow' };
adjustNewNodes(parent, child, { childIsNew: false });
adjustNewNodes(source, target, { targetIsNew: false });
expect(parent).toEqual({
expect(source).toEqual({
type: '@n8n/n8n-nodes-langchain.memoryBufferWindow',
});
});
it('does not modify sessionId with ManualTrigger->(new Memory->Agent)', () => {
const trigger = { type: MANUAL_TRIGGER_NODE_TYPE, name: 'trigger' };
getParentNodesByDepth.mockReturnValue([{ name: trigger.name }]);
getsourceNodesByDepth.mockReturnValue([{ name: trigger.name }]);
getNode.mockReturnValue({ type: trigger.type });
const child = { type: AGENT_NODE_TYPE, name: 'myAgent' };
const parent = { type: '@n8n/n8n-nodes-langchain.memoryBufferWindow' };
const target = { type: AGENT_NODE_TYPE, name: 'myAgent' };
const source = { type: '@n8n/n8n-nodes-langchain.memoryBufferWindow' };
adjustNewNodes(parent, child, { childIsNew: false });
adjustNewNodes(source, target, { targetIsNew: false });
expect(parent).toEqual({
expect(source).toEqual({
type: '@n8n/n8n-nodes-langchain.memoryBufferWindow',
parameters: { sessionIdType: 'customKey' },
});

View file

@ -5,37 +5,41 @@ import type { AddedNode } from '@/Interface';
const PROMPT_PROVIDER_NODE_NAMES = [CHAT_TRIGGER_NODE_TYPE];
/** Adjust new Source->Target connection */
export function adjustNewNodes(
parent: AddedNode,
child: AddedNode,
{ parentIsNew = true, childIsNew = true } = {},
source: AddedNode,
target: AddedNode,
{ sourceIsNew = true, targetIsNew = true } = {},
) {
if (childIsNew) adjustNewChild(parent, child);
if (parentIsNew) adjustNewParent(parent, child);
if (sourceIsNew) adjustNewSource(source, target);
if (targetIsNew) adjustNewTarget(source, target);
}
function adjustNewChild(parent: AddedNode, child: AddedNode) {
if (AI_ROOT_NODE_TYPES.includes(child.type) && PROMPT_PROVIDER_NODE_NAMES.includes(parent.type)) {
function adjustNewTarget(source: AddedNode, target: AddedNode) {
if (
AI_ROOT_NODE_TYPES.includes(target.type) &&
PROMPT_PROVIDER_NODE_NAMES.includes(source.type)
) {
// Need to re-set text to support disabled parameter value for prompt text.
Object.assign<AddedNode, Partial<INode>>(child, {
parameters: { ...child.parameters, promptType: 'auto', text: '={{ $json.chatInput }}' },
Object.assign<AddedNode, Partial<INode>>(target, {
parameters: { ...target.parameters, promptType: 'auto', text: '={{ $json.chatInput }}' },
});
}
}
function adjustNewParent(parent: AddedNode, child: AddedNode) {
if (AI_MEMORY_NODE_TYPES.includes(parent.type) && child.name) {
function adjustNewSource(source: AddedNode, target: AddedNode) {
if (AI_MEMORY_NODE_TYPES.includes(source.type) && target.name) {
const { getCurrentWorkflow } = useWorkflowsStore();
const workflow = getCurrentWorkflow();
// If a memory node is added to an Agent, the memory node is actually a parent since it provides input
// So we need to look for the Agent's (other) parents to determine if there is a sessionId provider
const ps = workflow.getParentNodesByDepth(child.name, 1);
const ps = workflow.getParentNodesByDepth(target.name, 1);
if (
!ps.some((x) => PROMPT_PROVIDER_NODE_NAMES.includes(workflow.getNode(x.name)?.type ?? ''))
) {
Object.assign<AddedNode, Partial<INode>>(parent, {
parameters: { ...parent.parameters, sessionIdType: 'customKey' },
Object.assign<AddedNode, Partial<INode>>(source, {
parameters: { ...source.parameters, sessionIdType: 'customKey' },
});
}
}