index.ts
6.2 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* Iteration history lookup tool for judger agent.
*
* Provides methods to query the history of component positions and fixes
* across validation iterations.
*/
import { tools } from 'evoltagent';
import type { IterationSummaryChange } from './types';
import type { ComponentHistory } from '../../types/validation-types';
/**
* Iteration history lookup tool.
*
* This tool allows agents to understand the history of component positions
* and fixes across validation iterations, helping to avoid repeating failed
* fixes and detecting regressions.
*/
@tools({
getComponentHistory: {
description:
"Get position and fix history for a component. Shows how the component's position and error evolved across iterations, and what fixes were applied. Helps identify if previous fixes helped or hurt",
params: [{ name: 'componentId', type: 'string', description: 'Component ID to get history for' }],
returns: { type: 'string', description: 'Formatted string with complete history of positions, errors, and fixes' },
examples: [
`<HistoryTool.getComponentHistory>
<componentId>HeroSection</componentId>
</HistoryTool.getComponentHistory>`,
],
},
getIterationSummary: {
description:
'Get summary of what was changed in a specific iteration. Shows all components that were fixed in that iteration and what fixes were applied. Useful for understanding what went wrong in a previous iteration',
params: [{ name: 'iteration', type: 'number', description: 'Iteration number (1-indexed)' }],
returns: { type: 'string', description: 'Formatted string with summary of changes in that iteration' },
examples: [
`<HistoryTool.getIterationSummary>
<iteration>2</iteration>
</HistoryTool.getIterationSummary>`,
],
},
})
export class HistoryTool {
private _history: ComponentHistory = {};
/**
* Initialize with component history from previous iterations.
*
* @param history - Dict mapping component_id to list of iteration records
*/
setContext(history: ComponentHistory): void {
this._history = history;
}
/**
* Get position and fix history for a component.
*
* Shows how the component's position and error evolved across iterations,
* and what fixes were applied. Helps identify if previous fixes helped or hurt.
*
* @param componentId - Component ID to get history for
* @returns Formatted string with complete history of positions, errors, and fixes
*/
getComponentHistory(componentId: string): string {
if (!this._history || Object.keys(this._history).length === 0) {
return 'No history available (this is iteration 1)';
}
if (!(componentId in this._history)) {
return `No history found for component '${componentId}' (first time misaligned)`;
}
const historyEntries = this._history[componentId];
if (!historyEntries || historyEntries.length === 0) {
return `Empty history for component '${componentId}'`;
}
const lines: string[] = [`History for ${componentId}:`, ''];
for (let i = 0; i < historyEntries.length; i++) {
const entry = historyEntries[i];
if (!entry) continue;
const iteration = entry.iteration ?? '?';
const position = entry.position ?? [0, 0];
const error = entry.error ?? [0, 0];
const fixApplied = entry.fixApplied ? entry.fixApplied.join('\n ') : 'None';
lines.push(`Iteration ${iteration}:`);
lines.push(` Position: (${position[0].toFixed(1)}, ${position[1].toFixed(1)}) px`);
lines.push(` Error: (${error[0].toFixed(1)}, ${error[1].toFixed(1)}) px`);
lines.push(` Fix Applied:\n ${fixApplied}`);
if (i > 0) {
const prevEntry = historyEntries[i - 1];
if (prevEntry) {
const prevErrorMagnitude = prevEntry.error[0] + prevEntry.error[1];
const currErrorMagnitude = error[0] + error[1];
if (currErrorMagnitude > prevErrorMagnitude) {
lines.push(
` Warning: REGRESSION: Error increased from ${prevErrorMagnitude.toFixed(1)}px to ${currErrorMagnitude.toFixed(1)}px`
);
}
}
}
lines.push('');
}
return lines.join('\n');
}
/**
* Get summary of what was changed in a specific iteration.
*
* Shows all components that were fixed in that iteration and what fixes
* were applied. Useful for understanding what went wrong in a previous iteration.
*
* @param iteration - Iteration number (1-indexed)
* @returns Formatted string with summary of changes in that iteration
*/
getIterationSummary(iteration: number): string {
if (!this._history || Object.keys(this._history).length === 0) {
return `No history available (iteration ${iteration} has not completed yet)`;
}
const changes: IterationSummaryChange[] = [];
for (const [componentId, entries] of Object.entries(this._history)) {
for (const entry of entries) {
if (entry.iteration === iteration) {
changes.push({
componentId,
position: entry.position ?? [0, 0],
error: entry.error ?? [0, 0],
fixApplied: entry.fixApplied ?? ['None'],
});
break;
}
}
}
if (changes.length === 0) {
return `No changes recorded for iteration ${iteration}`;
}
const lines: string[] = [`Iteration ${iteration} Summary:`, `Total components modified: ${changes.length}`, ''];
for (const change of changes) {
lines.push(`${change.componentId}:`);
lines.push(` Error: (${change.error[0].toFixed(1)}, ${change.error[1].toFixed(1)}) px`);
lines.push(` Fix:\n ${change.fixApplied.join('\n ')}`);
lines.push('');
}
return lines.join('\n');
}
}