validation-types.ts
6.91 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* Shared validation types.
*
* Types used across multiple subsystems in the validation pipeline:
* - Validation node (src/nodes/validation/)
* - Position tool (src/tools/position-tool/)
* - Report tool (src/tools/report-tool/)
* - History tool (src/tools/history-tool/)
* - Judger agent (src/agents/judger-agent/)
*
* Per type organization hierarchy: shared types go in src/types/
*/
import type { FigmaFrameInfo } from './figma-types.js';
// ============================================================================
// Utility Types
// ============================================================================
/**
* Generic dictionary/object type with string keys and unknown values.
* Used for flexible data structures like Figma JSON trees, structure trees, etc.
*/
export type Dict = Record<string, unknown>;
// ============================================================================
// Validation Context Types (extracted from protocol)
// ============================================================================
/**
* Figma position with offset already applied (CSS coordinates).
*/
export interface FigmaPosition {
x: number;
y: number;
w: number;
h: number;
}
/**
* Component information from protocol structure.
*/
export interface ComponentInfo {
id: string;
name: string;
path: string;
}
/**
* Complete metadata for a single element (Figma node) in context.
*/
export interface ElementInfo {
id: string;
name: string;
type: string;
/** Position with offset already applied (CSS coordinates) */
position: FigmaPosition;
/** Raw Figma layout properties */
layoutMode?: string;
primaryAxisAlignItems?: string;
counterAxisAlignItems?: string;
itemSpacing?: number;
paddingTop?: number;
paddingRight?: number;
paddingBottom?: number;
paddingLeft?: number;
constraints?: Record<string, unknown>;
/** Parent component ID */
parentComponentId: string;
/** Parent component name */
parentComponentName: string;
/** Parent component path */
parentComponentPath: string;
/** Parent item type */
parentItemType: 'frame' | 'component';
}
/**
* Extended Figma frame information for validation.
* Includes layout properties needed for position validation.
*/
export interface ValidationFrameInfo extends FigmaFrameInfo {
layoutMode?: string; // 'HORIZONTAL' | 'VERTICAL' | 'NONE'
primaryAxisAlignItems?: string; // Auto-layout: main axis alignment
counterAxisAlignItems?: string; // Auto-layout: cross axis alignment
itemSpacing?: number; // Auto-layout: gap between items
paddingTop?: number; // Auto-layout padding
paddingRight?: number;
paddingBottom?: number;
paddingLeft?: number;
}
/**
* Unified validation context extracted from protocol.
* Built once at validation start and reused across all iterations.
*/
export interface ValidationContext {
/** Coordinate offset from Figma canvas to CSS (0,0) origin */
offset: { x: number; y: number };
/** All elements indexed by Figma node ID */
elements: Map<string, ElementInfo>;
/** All components indexed by component ID */
components: Map<string, ComponentInfo>;
/** Element ID to parent component mapping (for compatibility) */
elementToComponent: Map<string, ComponentInfo>;
/** All Figma positions indexed by node ID (offset already applied) */
figmaPositions: Record<string, FigmaPosition | undefined>;
}
/**
* Element metadata registry for compatibility with existing APIs.
*/
export interface ElementMetadataRegistry {
elements: Map<
string,
{
id: string;
name: string;
parentComponentId: string;
parentComponentName: string;
parentComponentPath: string;
parentItemType: 'frame' | 'component';
}
>;
components: Map<string, ComponentInfo>;
}
// ============================================================================
// Validation Reports
// ============================================================================
/**
* User-facing validation report with annotated screenshots and analysis
* Generated by ValidationNode after position capture and annotation
*/
export interface UserReport {
design: {
snap: string; // Figma thumbnail URL
markedSnap: string; // Annotated design image (base64 data URI)
};
page: {
url: string; // Local server URL
snap: string; // Page screenshot (base64 data URI)
markedSnap: string; // Annotated page screenshot (base64 data URI)
};
report: {
heatmap: string; // Reserved for future heatmap implementation (empty for now)
detail: {
metrics: {
mae: number;
sae: number;
misalignedCount: number;
};
// Grouped component type for report visualization
components: {
componentId: string;
componentPath: string;
elementId?: string;
validationInfo?: {
x: number;
y: number;
};
elements?: {
elementId: string;
elementIndex: number;
validationInfo: {
x: number; // Horizontal deviation in px
y: number; // Vertical deviation in px
};
}[];
}[];
};
};
}
/**
* Structured validation error report
* Contains position comparison data for a single component
*/
export interface ValidationReport {
/** [x, y] of rendered position */
currentPosition: [number, number];
/** [x, y] of expected Figma position */
targetPosition: [number, number];
/** [abs_error_x, abs_error_y] absolute values of position error */
absoluteError: [number, number];
}
/**
* Misaligned component data
* Contains all information about a component that needs fixing
*/
export interface MisalignedComponent {
name: string;
componentId: string;
elementIds: string[];
path: string;
validationReport: ValidationReport;
currentX: number;
currentY: number;
targetX: number;
targetY: number;
currentWidth: number;
currentHeight: number;
targetWidth: number;
targetHeight: number;
distance: number;
}
// ============================================================================
// Component History
// ============================================================================
/**
* Component history entry
* Tracks position and fix across iterations for HistoryTool
*/
export interface ComponentHistoryEntry {
iteration: number;
position: [number, number];
error: [number, number];
fixApplied: string[] | null;
}
/**
* Component history map
* Maps component IDs to their history entries
*/
export type ComponentHistory = Record<string, ComponentHistoryEntry[]>;