feat: format agent
parent
08515957f1
commit
e1ea82475d
@ -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' },
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}],
|
||||
}
|
||||
})()
|
||||
@ -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)
|
||||
})
|
||||
})
|
||||
@ -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
|
||||
Loading…
Reference in New Issue