feat: format agent

This commit is contained in:
Joel 2024-12-25 18:36:53 +08:00
parent 08515957f1
commit e1ea82475d
5 changed files with 156 additions and 1 deletions

View File

@ -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' },
],
},
],
},
],
}],
}
})()

View File

@ -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)
})
})

View File

@ -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

View File

@ -1,12 +1,15 @@
import type { NodeTracing } from '@/types/workflow' import type { NodeTracing } from '@/types/workflow'
import formatIterationNode from './iteration' import formatIterationNode from './iteration'
import formatRetryNode from './retry' import formatRetryNode from './retry'
import formatAgentNode from './agent'
const formatToTracingNodeList = (list: NodeTracing[]) => { const formatToTracingNodeList = (list: NodeTracing[]) => {
const allItems = [...list].reverse() const allItems = [...list].reverse()
const formattedIterationList = formatIterationNode(allItems) const formattedIterationList = formatIterationNode(allItems)
const formattedRetryList = formatRetryNode(formattedIterationList) const formattedRetryList = formatRetryNode(formattedIterationList)
const result = formattedRetryList const formattedAgentList = formatAgentNode(formattedRetryList)
const result = formattedAgentList
// console.log(allItems) // console.log(allItems)
// console.log(result) // console.log(result)

View File

@ -9,6 +9,20 @@ import type {
import type { TransferMethod } from '@/types/app' import type { TransferMethod } from '@/types/app'
import type { ErrorHandleTypeEnum } from '@/app/components/workflow/nodes/_base/components/error-handle/types' 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 = { export type NodeTracing = {
id: string id: string
index: number index: number
@ -36,6 +50,7 @@ export type NodeTracing = {
parallel_mode_run_id?: string parallel_mode_run_id?: string
iteration_duration_map?: IterationDurationMap iteration_duration_map?: IterationDurationMap
error_strategy?: ErrorHandleTypeEnum error_strategy?: ErrorHandleTypeEnum
agent_log?: AgentLogItem[]
} }
metadata: { metadata: {
iterator_length: number iterator_length: number
@ -53,6 +68,7 @@ export type NodeTracing = {
expand?: boolean // for UI expand?: boolean // for UI
details?: NodeTracing[][] // iteration detail details?: NodeTracing[][] // iteration detail
retryDetail?: NodeTracing[] // retry detail retryDetail?: NodeTracing[] // retry detail
agentLog?: AgentLogItemWithChildren[]
parallel_id?: string parallel_id?: string
parallel_start_node_id?: string parallel_start_node_id?: string
parent_parallel_id?: string parent_parallel_id?: string