index.ts
5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* Component hierarchy lookup tool for judger agent.
*
* Provides methods to query the component structure tree for parent-child
* relationships, siblings, and shared component instances.
*/
import { tools } from 'evoltagent';
import { findInTree, getNodeId } from '../../nodes/validation/utils/tree/tree-traversal';
import type { ComponentPaths, HierarchyNode, ParentInfo } from './types';
/**
* Component hierarchy lookup tool.
*
* This tool allows agents to understand the component tree structure,
* find parents, siblings, and children, and identify shared component instances.
*/
@tools({
lookup: {
description:
'Get file path, parent, siblings, and children for a component. Use this to find the correct file path before reading component files.',
params: [{ name: 'componentId', type: 'string', description: 'Component ID to look up' }],
returns: { type: 'string', description: 'Formatted string with file path, parent (with its file path), siblings, and children' },
examples: [
`<HierarchyTool.lookup>
<componentId>HeroSection</componentId>
</HierarchyTool.lookup>`,
],
},
getSharedInstances: {
description:
'Find all component instances using a specific file path. Useful for identifying which components share the same implementation file (e.g., StartButton and ApiButton both using button/index.tsx). ALWAYS check this before editing a shared file.',
params: [{ name: 'filePath', type: 'string', description: 'Filesystem path to search for (can be relative or absolute)' }],
returns: { type: 'string', description: 'Formatted string with all component instances using the file' },
examples: [
`<HierarchyTool.getSharedInstances>
<filePath>button/index.tsx</filePath>
</HierarchyTool.getSharedInstances>`,
],
},
})
export class HierarchyTool {
private _structureTree: HierarchyNode = {};
private _componentPaths: ComponentPaths = {};
constructor() {
this._structureTree = {};
this._componentPaths = {};
}
/**
* Initialize with page structure and path mapping.
*/
setContext(structureTree: HierarchyNode, componentPaths: ComponentPaths): void {
this._structureTree = structureTree;
this._componentPaths = componentPaths;
}
lookup(componentId: string): string {
const node = findInTree(this._structureTree, componentId);
if (!node) {
return `Component '${componentId}' not found in structure tree`;
}
const parentInfo = this._findParent(this._structureTree, componentId);
const parentStr = parentInfo ? `Parent: ${parentInfo.id}` : 'Parent: None (root component)';
// Include parent file path if available (helps agent read parent files without guessing paths)
const parentPathStr =
parentInfo && this._componentPaths[parentInfo.id] ? `Parent File: ${this._componentPaths[parentInfo.id]}` : '';
let siblings: string[] = [];
if (parentInfo) {
const parentNode = findInTree(this._structureTree, parentInfo.id);
if (parentNode) {
const children = (parentNode.children as HierarchyNode[]) || [];
siblings = children.map(child => getNodeId(child)).filter((id): id is string => id !== undefined && id !== componentId);
}
}
const siblingsStr = siblings.length > 0 ? `Siblings: ${siblings.join(', ')}` : 'Siblings: None';
const nodeChildren = (node.children as HierarchyNode[]) || [];
const children = nodeChildren.map(child => getNodeId(child)).filter((id): id is string => id !== undefined);
const childrenStr = children.length > 0 ? `Children: ${children.join(', ')}` : 'Children: None';
// Include file path for this component
const filePath = this._componentPaths[componentId];
const fileStr = filePath ? `File: ${filePath}` : '';
const lines = [`Component: ${componentId}`, fileStr, parentStr, parentPathStr, siblingsStr, childrenStr].filter(Boolean);
return lines.join('\n');
}
getSharedInstances(filePath: string): string {
const instances: string[] = [];
for (const [compId, compPath] of Object.entries(this._componentPaths)) {
if (compPath.includes(filePath) || compPath.endsWith(filePath)) {
instances.push(compId);
}
}
if (instances.length === 0) {
return `No components found using file: ${filePath}`;
}
const instancesList = instances.map(inst => ` - ${inst}`).join('\n');
return `Components using ${filePath}:
${instancesList}
Warning: Changes to this file will affect ALL ${instances.length} instance(s)`;
}
private _findParent(node: HierarchyNode, targetId: string, parent: HierarchyNode | null = null): ParentInfo | undefined {
const nodeId = getNodeId(node);
if (nodeId === targetId) {
if (parent) {
const parentId = getNodeId(parent);
if (parentId) {
return { id: parentId };
}
}
return undefined;
}
const children = (node.children as HierarchyNode[]) || [];
for (const child of children) {
const result = this._findParent(child, targetId, node);
if (result !== undefined) {
return result;
}
}
return undefined;
}
}