From 55aad3718da65c5d39f1a4e5d9c9414985db705d Mon Sep 17 00:00:00 2001 From: Joel Date: Thu, 2 Jan 2025 14:09:35 +0800 Subject: [PATCH] fix: multi loop nodes remove children error --- .../run/utils/format-log/agent/data.ts | 12 ++++---- .../run/utils/format-log/agent/index.ts | 30 ++++++++++--------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/web/app/components/workflow/run/utils/format-log/agent/data.ts b/web/app/components/workflow/run/utils/format-log/agent/data.ts index d64670a015..a1e06bf63b 100644 --- a/web/app/components/workflow/run/utils/format-log/agent/data.ts +++ b/web/app/components/workflow/run/utils/format-log/agent/data.ts @@ -1,5 +1,4 @@ import { BlockEnum } from '@/app/components/workflow/types' -import { has } from 'immer/dist/internal' export const agentNodeData = (() => { const node = { @@ -120,7 +119,6 @@ export const oneStepCircle = (() => { ], }], } - })() export const multiStepsCircle = (() => { @@ -138,13 +136,13 @@ export const multiStepsCircle = (() => { { id: '1', parent_id: '4', label: 'Node 1' }, { id: '2', parent_id: '1', label: 'Node 2' }, { id: '4', parent_id: '2', label: 'Node 4' }, - // { id: '1', parent_id: '4', label: 'Node 1' }, - // { id: '2', parent_id: '1', label: 'Node 2' }, - // { id: '4', parent_id: '2', label: 'Node 4' }, + { id: '1', parent_id: '4', label: 'Node 1' }, + { id: '2', parent_id: '1', label: 'Node 2' }, + { id: '4', parent_id: '2', label: 'Node 4' }, ], }, } - + // 1 -> [2(4(1(2(4...)))), 3] return { in: [node], expect: [{ @@ -165,7 +163,7 @@ export const multiStepsCircle = (() => { label: 'Node 4', children: [], hasCircle: true, - } + }, ], }, { diff --git a/web/app/components/workflow/run/utils/format-log/agent/index.ts b/web/app/components/workflow/run/utils/format-log/agent/index.ts index 65c7f6d36e..c1f3afc20a 100644 --- a/web/app/components/workflow/run/utils/format-log/agent/index.ts +++ b/web/app/components/workflow/run/utils/format-log/agent/index.ts @@ -5,24 +5,26 @@ import { cloneDeep } from 'lodash-es' const supportedAgentLogNodes = [BlockEnum.Agent, BlockEnum.Tool] const remove = (node: AgentLogItemWithChildren, removeId: string) => { - const { children } = node - if (!children || children.length === 0) { + let { children } = node + if (!children || children.length === 0) return + + const hasCircle = !!children.find(c => c.id === removeId) + if (hasCircle) { + node.hasCircle = true + node.children = node.children.filter(c => c.id !== removeId) + children = node.children } - children.forEach((child, index) => { - if (child.id === removeId) { - node.hasCircle = true - children.splice(index, 1) - return - } + + children.forEach((child) => { remove(child, removeId) }) } const removeRepeatedSiblings = (list: AgentLogItemWithChildren[]) => { - if (!list || list.length === 0) { + if (!list || list.length === 0) return [] - } + const result: AgentLogItemWithChildren[] = [] const addedItemIds: string[] = [] list.forEach((item) => { @@ -35,19 +37,18 @@ const removeRepeatedSiblings = (list: AgentLogItemWithChildren[]) => { } const removeCircleLogItem = (log: AgentLogItemWithChildren) => { - let newLog = cloneDeep(log) + const newLog = cloneDeep(log) newLog.children = removeRepeatedSiblings(newLog.children) let { id, children } = newLog - if (!children || children.length === 0) { + if (!children || children.length === 0) return log - } + // check one step circle const hasOneStepCircle = !!children.find(c => c.id === id) if (hasOneStepCircle) { newLog.hasCircle = true newLog.children = newLog.children.filter(c => c.id !== id) children = newLog.children - } children.forEach((child, index) => { @@ -85,6 +86,7 @@ const format = (list: NodeTracing[]): NodeTracing[] => { let removedCircleTree: AgentLogItemWithChildren[] = [] if (supportedAgentLogNodes.includes(item.node_type) && item.execution_metadata?.agent_log && item.execution_metadata?.agent_log.length > 0) treeList = listToTree(item.execution_metadata.agent_log) + // console.log(JSON.stringify(treeList)) removedCircleTree = treeList.length > 0 ? treeList.map(t => removeCircleLogItem(t)) : [] item.agentLog = removedCircleTree