From e1ea82475d0abc81e74cc4e5a42b1b26ef0b21a8 Mon Sep 17 00:00:00 2001 From: Joel Date: Wed, 25 Dec 2024 18:36:53 +0800 Subject: [PATCH] feat: format agent --- .../run/utils/format-log/agent/data.ts | 91 +++++++++++++++++++ .../run/utils/format-log/agent/index.spec.ts | 9 ++ .../run/utils/format-log/agent/index.ts | 36 ++++++++ .../workflow/run/utils/format-log/index.ts | 5 +- web/types/workflow.ts | 16 ++++ 5 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 web/app/components/workflow/run/utils/format-log/agent/data.ts create mode 100644 web/app/components/workflow/run/utils/format-log/agent/index.spec.ts create mode 100644 web/app/components/workflow/run/utils/format-log/agent/index.ts 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 new file mode 100644 index 0000000000..2e0c23b5fb --- /dev/null +++ b/web/app/components/workflow/run/utils/format-log/agent/data.ts @@ -0,0 +1,91 @@ +import { BlockEnum } from '@/app/components/workflow/types' + +export const agentNodeData = (() => { + const node = { + node_type: BlockEnum.Agent, + execution_metadata: { + agent_log: [ + { id: '1', label: 'Root 1' }, + { id: '2', parent_id: '1', label: 'Child 1.2' }, + { id: '3', parent_id: '1', label: 'Child 1.3' }, + { id: '4', parent_id: '2', label: 'Child 2.4' }, + { id: '5', parent_id: '2', label: 'Child 2.5' }, + { id: '6', parent_id: '3', label: 'Child 3.6' }, + { id: '7', parent_id: '4', label: 'Child 4.7' }, + { id: '8', parent_id: '4', label: 'Child 4.8' }, + { id: '9', parent_id: '5', label: 'Child 5.9' }, + { id: '10', parent_id: '5', label: 'Child 5.10' }, + { id: '11', parent_id: '7', label: 'Child 7.11' }, + { id: '12', parent_id: '7', label: 'Child 7.12' }, + { id: '13', parent_id: '9', label: 'Child 9.13' }, + { id: '14', parent_id: '9', label: 'Child 9.14' }, + { id: '15', parent_id: '9', label: 'Child 9.15' }, + ], + }, + } + + return { + in: [node], + expect: [{ + ...node, + agentLog: [ + { + id: '1', + label: 'Root 1', + children: [ + { + id: '2', + parent_id: '1', + label: 'Child 1.2', + children: [ + { + id: '4', + parent_id: '2', + label: 'Child 2.4', + children: [ + { + id: '7', + parent_id: '4', + label: 'Child 4.7', + children: [ + { id: '11', parent_id: '7', label: 'Child 7.11' }, + { id: '12', parent_id: '7', label: 'Child 7.12' }, + ], + }, + { id: '8', parent_id: '4', label: 'Child 4.8' }, + ], + }, + { + id: '5', + parent_id: '2', + label: 'Child 2.5', + children: [ + { + id: '9', + parent_id: '5', + label: 'Child 5.9', + children: [ + { id: '13', parent_id: '9', label: 'Child 9.13' }, + { id: '14', parent_id: '9', label: 'Child 9.14' }, + { id: '15', parent_id: '9', label: 'Child 9.15' }, + ], + }, + { id: '10', parent_id: '5', label: 'Child 5.10' }, + ], + }, + ], + }, + { + id: '3', + parent_id: '1', + label: 'Child 1.3', + children: [ + { id: '6', parent_id: '3', label: 'Child 3.6' }, + ], + }, + ], + }, + ], + }], + } +})() diff --git a/web/app/components/workflow/run/utils/format-log/agent/index.spec.ts b/web/app/components/workflow/run/utils/format-log/agent/index.spec.ts new file mode 100644 index 0000000000..0fd871c41d --- /dev/null +++ b/web/app/components/workflow/run/utils/format-log/agent/index.spec.ts @@ -0,0 +1,9 @@ +import format from '.' +import { agentNodeData } from './data' + +describe('agent', () => { + test('list should transform to tree', () => { + // console.log(format(agentNodeData.in as any)) + expect(format(agentNodeData.in as any)).toEqual(agentNodeData.expect) + }) +}) 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 new file mode 100644 index 0000000000..7bbe0aa57f --- /dev/null +++ b/web/app/components/workflow/run/utils/format-log/agent/index.ts @@ -0,0 +1,36 @@ +import { BlockEnum } from '@/app/components/workflow/types' +import type { AgentLogItem, AgentLogItemWithChildren, NodeTracing } from '@/types/workflow' + +const listToTree = (logs: AgentLogItem[]) => { + if (!logs || logs.length === 0) + return [] + + const tree: AgentLogItemWithChildren[] = [] + logs.forEach((log) => { + const hasParent = !!log.parent_id + if (hasParent) { + const parent = logs.find(item => item.id === log.parent_id) as AgentLogItemWithChildren + if (parent) { + if (!parent.children) + parent.children = [] + parent.children.push(log as AgentLogItemWithChildren) + } + } + else { + tree.push(log as AgentLogItemWithChildren) + } + }) + return tree +} +const format = (list: NodeTracing[]): NodeTracing[] => { + const result: NodeTracing[] = list.map((item) => { + if (item.node_type === BlockEnum.Agent && item.execution_metadata?.agent_log && item.execution_metadata?.agent_log.length > 0) + item.agentLog = listToTree(item.execution_metadata.agent_log) + + return item + }) + + return result +} + +export default format diff --git a/web/app/components/workflow/run/utils/format-log/index.ts b/web/app/components/workflow/run/utils/format-log/index.ts index a43a947791..4f996e2f26 100644 --- a/web/app/components/workflow/run/utils/format-log/index.ts +++ b/web/app/components/workflow/run/utils/format-log/index.ts @@ -1,12 +1,15 @@ import type { NodeTracing } from '@/types/workflow' import formatIterationNode from './iteration' import formatRetryNode from './retry' +import formatAgentNode from './agent' const formatToTracingNodeList = (list: NodeTracing[]) => { const allItems = [...list].reverse() const formattedIterationList = formatIterationNode(allItems) const formattedRetryList = formatRetryNode(formattedIterationList) - const result = formattedRetryList + const formattedAgentList = formatAgentNode(formattedRetryList) + + const result = formattedAgentList // console.log(allItems) // console.log(result) diff --git a/web/types/workflow.ts b/web/types/workflow.ts index cd6e9cfa5f..c89f5536a0 100644 --- a/web/types/workflow.ts +++ b/web/types/workflow.ts @@ -9,6 +9,20 @@ import type { import type { TransferMethod } from '@/types/app' import type { ErrorHandleTypeEnum } from '@/app/components/workflow/nodes/_base/components/error-handle/types' +export type AgentLogItem = { + node_execution_id: string, + id: string, + parent_id?: string, + label: string, + data: object, // debug data + error?: string, + status: string, +} + +export type AgentLogItemWithChildren = AgentLogItem & { + children: AgentLogItemWithChildren[] +} + export type NodeTracing = { id: string index: number @@ -36,6 +50,7 @@ export type NodeTracing = { parallel_mode_run_id?: string iteration_duration_map?: IterationDurationMap error_strategy?: ErrorHandleTypeEnum + agent_log?: AgentLogItem[] } metadata: { iterator_length: number @@ -53,6 +68,7 @@ export type NodeTracing = { expand?: boolean // for UI details?: NodeTracing[][] // iteration detail retryDetail?: NodeTracing[] // retry detail + agentLog?: AgentLogItemWithChildren[] parallel_id?: string parallel_start_node_id?: string parent_parallel_id?: string