sonance-brand-mcp 1.3.103 → 1.3.105
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -137,6 +137,132 @@ function debugLog(message: string, data?: unknown) {
|
|
|
137
137
|
}
|
|
138
138
|
}
|
|
139
139
|
|
|
140
|
+
/**
|
|
141
|
+
* Sanitize a JSON string by finding the correct end point using bracket balancing.
|
|
142
|
+
* Handles cases where LLM outputs trailing garbage like extra ]} characters.
|
|
143
|
+
*/
|
|
144
|
+
function sanitizeJsonString(text: string): string {
|
|
145
|
+
const trimmed = text.trim();
|
|
146
|
+
|
|
147
|
+
debugLog("[sanitizeJsonString] Starting", {
|
|
148
|
+
inputLength: trimmed.length,
|
|
149
|
+
first100: trimmed.substring(0, 100),
|
|
150
|
+
last100: trimmed.substring(trimmed.length - 100)
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
// Try parsing as-is first
|
|
154
|
+
try {
|
|
155
|
+
JSON.parse(trimmed);
|
|
156
|
+
debugLog("[sanitizeJsonString] Parsed as-is successfully");
|
|
157
|
+
return trimmed;
|
|
158
|
+
} catch (e) {
|
|
159
|
+
const error = e as Error;
|
|
160
|
+
debugLog("[sanitizeJsonString] Initial parse failed", {
|
|
161
|
+
error: error.message,
|
|
162
|
+
// Try to find where the error is
|
|
163
|
+
errorPosition: error.message.match(/position (\d+)/)?.[1]
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Find the correct end using balanced bracket counting
|
|
168
|
+
let braceCount = 0;
|
|
169
|
+
let bracketCount = 0;
|
|
170
|
+
let inString = false;
|
|
171
|
+
let escapeNext = false;
|
|
172
|
+
let endIndex = -1;
|
|
173
|
+
|
|
174
|
+
for (let i = 0; i < trimmed.length; i++) {
|
|
175
|
+
const char = trimmed[i];
|
|
176
|
+
|
|
177
|
+
if (escapeNext) {
|
|
178
|
+
escapeNext = false;
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (char === '\\' && inString) {
|
|
183
|
+
escapeNext = true;
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (char === '"') {
|
|
188
|
+
inString = !inString;
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (inString) continue;
|
|
193
|
+
|
|
194
|
+
if (char === '{') braceCount++;
|
|
195
|
+
if (char === '}') braceCount--;
|
|
196
|
+
if (char === '[') bracketCount++;
|
|
197
|
+
if (char === ']') bracketCount--;
|
|
198
|
+
|
|
199
|
+
// Found the end of the root object
|
|
200
|
+
if (braceCount === 0 && bracketCount === 0 && char === '}') {
|
|
201
|
+
endIndex = i;
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
debugLog("[sanitizeJsonString] Bracket counting complete", {
|
|
207
|
+
endIndex,
|
|
208
|
+
finalBraceCount: braceCount,
|
|
209
|
+
finalBracketCount: bracketCount,
|
|
210
|
+
inString
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
if (endIndex !== -1) {
|
|
214
|
+
const sanitized = trimmed.slice(0, endIndex + 1);
|
|
215
|
+
const removed = trimmed.slice(endIndex + 1);
|
|
216
|
+
debugLog("[sanitizeJsonString] Attempting parse of balanced portion", {
|
|
217
|
+
sanitizedLength: sanitized.length,
|
|
218
|
+
removedLength: removed.length,
|
|
219
|
+
removedChars: removed.substring(0, 50)
|
|
220
|
+
});
|
|
221
|
+
try {
|
|
222
|
+
JSON.parse(sanitized);
|
|
223
|
+
debugLog("[sanitizeJsonString] SUCCESS: Removed trailing garbage", {
|
|
224
|
+
originalLength: trimmed.length,
|
|
225
|
+
sanitizedLength: sanitized.length,
|
|
226
|
+
removedChars: removed
|
|
227
|
+
});
|
|
228
|
+
return sanitized;
|
|
229
|
+
} catch (e) {
|
|
230
|
+
const error = e as Error;
|
|
231
|
+
debugLog("[sanitizeJsonString] Balanced portion still invalid", {
|
|
232
|
+
error: error.message
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Last resort: progressively remove trailing characters
|
|
238
|
+
debugLog("[sanitizeJsonString] Starting progressive trimming");
|
|
239
|
+
let attempt = trimmed;
|
|
240
|
+
let iterations = 0;
|
|
241
|
+
const maxIterations = Math.min(1000, trimmed.length); // Safety limit
|
|
242
|
+
|
|
243
|
+
while (attempt.length > 1 && iterations < maxIterations) {
|
|
244
|
+
iterations++;
|
|
245
|
+
try {
|
|
246
|
+
JSON.parse(attempt);
|
|
247
|
+
debugLog("[sanitizeJsonString] SUCCESS via progressive trimming", {
|
|
248
|
+
originalLength: trimmed.length,
|
|
249
|
+
sanitizedLength: attempt.length,
|
|
250
|
+
iterations
|
|
251
|
+
});
|
|
252
|
+
return attempt;
|
|
253
|
+
} catch {
|
|
254
|
+
attempt = attempt.slice(0, -1).trim();
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
debugLog("[sanitizeJsonString] FAILED: Could not sanitize JSON", {
|
|
259
|
+
originalLength: trimmed.length,
|
|
260
|
+
iterations
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
return trimmed; // Return original if all else fails
|
|
264
|
+
}
|
|
265
|
+
|
|
140
266
|
/**
|
|
141
267
|
* Extract JSON from LLM response that may contain preamble text
|
|
142
268
|
* Handles: pure JSON, markdown code fences, and text with embedded JSON
|
|
@@ -150,14 +276,14 @@ function debugLog(message: string, data?: unknown) {
|
|
|
150
276
|
function extractJsonFromResponse(text: string): string {
|
|
151
277
|
// Try direct parse first - if it starts with {, it's likely pure JSON
|
|
152
278
|
const trimmed = text.trim();
|
|
153
|
-
if (trimmed.startsWith('{')) return trimmed;
|
|
279
|
+
if (trimmed.startsWith('{')) return sanitizeJsonString(trimmed);
|
|
154
280
|
|
|
155
281
|
// PRIORITY 1: Look specifically for ```json fence (most reliable)
|
|
156
282
|
const jsonFenceMatch = text.match(/```json\s*([\s\S]*?)```/);
|
|
157
283
|
if (jsonFenceMatch) {
|
|
158
284
|
const extracted = jsonFenceMatch[1].trim();
|
|
159
285
|
debugLog("Extracted JSON from explicit json fence", { previewLength: extracted.length });
|
|
160
|
-
return extracted;
|
|
286
|
+
return sanitizeJsonString(extracted);
|
|
161
287
|
}
|
|
162
288
|
|
|
163
289
|
// PRIORITY 2: Find raw JSON object in text (handles prose + JSON at end)
|
|
@@ -165,7 +291,7 @@ function extractJsonFromResponse(text: string): string {
|
|
|
165
291
|
const modMatch = text.match(/(\{"modifications"\s*:\s*\[[\s\S]*\](?:\s*,\s*"explanation"\s*:\s*"[^"]*")?\s*\})/);
|
|
166
292
|
if (modMatch) {
|
|
167
293
|
debugLog("Extracted JSON via modifications pattern", { length: modMatch[1].length });
|
|
168
|
-
return modMatch[1];
|
|
294
|
+
return sanitizeJsonString(modMatch[1]);
|
|
169
295
|
}
|
|
170
296
|
|
|
171
297
|
// PRIORITY 3: Find any JSON object starting with {"
|
|
@@ -177,7 +303,7 @@ function extractJsonFromResponse(text: string): string {
|
|
|
177
303
|
preambleLength: jsonStart,
|
|
178
304
|
jsonLength: extracted.length
|
|
179
305
|
});
|
|
180
|
-
return extracted;
|
|
306
|
+
return sanitizeJsonString(extracted);
|
|
181
307
|
}
|
|
182
308
|
|
|
183
309
|
// PRIORITY 4 (last resort): Try generic code fence
|
|
@@ -188,12 +314,12 @@ function extractJsonFromResponse(text: string): string {
|
|
|
188
314
|
// Only use if it looks like JSON
|
|
189
315
|
if (extracted.startsWith('{')) {
|
|
190
316
|
debugLog("Extracted JSON from generic fence", { previewLength: extracted.length });
|
|
191
|
-
return extracted;
|
|
317
|
+
return sanitizeJsonString(extracted);
|
|
192
318
|
}
|
|
193
319
|
}
|
|
194
320
|
|
|
195
|
-
// Return as-is if no JSON structure found
|
|
196
|
-
return text;
|
|
321
|
+
// Return as-is if no JSON structure found (sanitizeJsonString will handle validation)
|
|
322
|
+
return sanitizeJsonString(text);
|
|
197
323
|
}
|
|
198
324
|
|
|
199
325
|
/**
|
|
@@ -324,9 +450,19 @@ function findElementLineInFile(
|
|
|
324
450
|
}
|
|
325
451
|
}
|
|
326
452
|
|
|
453
|
+
// Helper: Normalize text for searching (handle bullets, whitespace, special chars)
|
|
454
|
+
const normalizeTextForSearch = (text: string): string => {
|
|
455
|
+
return text
|
|
456
|
+
.replace(/[•·●○◦‣⁃▪▫→←↓↑]/g, '') // Remove bullet/arrow characters
|
|
457
|
+
.replace(/\s+/g, ' ') // Collapse whitespace
|
|
458
|
+
.replace(/[""'']/g, '"') // Normalize quotes
|
|
459
|
+
.trim();
|
|
460
|
+
};
|
|
461
|
+
|
|
327
462
|
// PRIORITY 2: Exact text content in JSX
|
|
328
463
|
if (focusedElement.textContent && focusedElement.textContent.trim().length >= 2) {
|
|
329
464
|
const text = focusedElement.textContent.trim();
|
|
465
|
+
const normalizedText = normalizeTextForSearch(text);
|
|
330
466
|
// Escape special regex characters in the text
|
|
331
467
|
const escapedText = text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
332
468
|
|
|
@@ -366,6 +502,34 @@ function findElementLineInFile(
|
|
|
366
502
|
}
|
|
367
503
|
}
|
|
368
504
|
}
|
|
505
|
+
|
|
506
|
+
// PRIORITY 2c: Word-based matching for text with special characters (bullets, etc.)
|
|
507
|
+
// Extract significant words and find lines containing multiple of them
|
|
508
|
+
if (normalizedText.length > 10) {
|
|
509
|
+
const commonWords = ['the', 'this', 'that', 'with', 'from', 'have', 'will', 'your', 'for', 'and', 'use'];
|
|
510
|
+
const significantWords = normalizedText.split(' ')
|
|
511
|
+
.filter(word => word.length >= 4 && !commonWords.includes(word.toLowerCase()))
|
|
512
|
+
.slice(0, 5); // Take up to 5 significant words
|
|
513
|
+
|
|
514
|
+
if (significantWords.length >= 2) {
|
|
515
|
+
for (let i = 0; i < lines.length; i++) {
|
|
516
|
+
const lineLower = lines[i].toLowerCase();
|
|
517
|
+
const matchCount = significantWords.filter(word =>
|
|
518
|
+
lineLower.includes(word.toLowerCase())
|
|
519
|
+
).length;
|
|
520
|
+
|
|
521
|
+
// Require at least 2 word matches for confidence
|
|
522
|
+
if (matchCount >= 2) {
|
|
523
|
+
return {
|
|
524
|
+
lineNumber: i + 1,
|
|
525
|
+
snippet: lines.slice(Math.max(0, i - 3), i + 5).join('\n'),
|
|
526
|
+
confidence: 'medium',
|
|
527
|
+
matchedBy: `word match: ${significantWords.slice(0, 3).join(', ')}... (${matchCount} words)`
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
369
533
|
}
|
|
370
534
|
|
|
371
535
|
// PRIORITY 2b: Look for icon patterns if type is button and no text (like X buttons)
|
|
@@ -386,159 +550,72 @@ function findElementLineInFile(
|
|
|
386
550
|
}
|
|
387
551
|
}
|
|
388
552
|
|
|
389
|
-
// PRIORITY 3: Distinctive className patterns (
|
|
553
|
+
// PRIORITY 3: Distinctive className patterns (semantic classes from design system)
|
|
390
554
|
if (focusedElement.className) {
|
|
391
|
-
//
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
/^
|
|
398
|
-
|
|
399
|
-
/^
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
/^
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
/^col-/, // grid columns
|
|
417
|
-
/^row-/, // grid rows
|
|
418
|
-
/^gap-/, // gap
|
|
419
|
-
/^items-/, // align-items
|
|
420
|
-
/^justify-/, // justify-content
|
|
421
|
-
/^self-/, // align-self
|
|
422
|
-
/^place-/, // place utilities
|
|
423
|
-
/^space-/, // space-x, space-y
|
|
424
|
-
/^overflow/, // overflow
|
|
425
|
-
/^scroll/, // scroll utilities
|
|
426
|
-
/^cursor-/, // cursor
|
|
427
|
-
/^pointer-/, // pointer-events
|
|
428
|
-
/^select-/, // user-select
|
|
429
|
-
/^opacity-/, // opacity
|
|
430
|
-
/^visible/, // visibility
|
|
431
|
-
/^invisible/, // visibility
|
|
432
|
-
/^z-/, // z-index
|
|
433
|
-
/^inset/, // inset utilities
|
|
434
|
-
/^top-/, // positioning
|
|
435
|
-
/^right-/, // positioning
|
|
436
|
-
/^bottom-/, // positioning
|
|
437
|
-
/^left-/, // positioning
|
|
438
|
-
/^static/, // position
|
|
439
|
-
/^fixed/, // position
|
|
440
|
-
/^absolute/, // position
|
|
441
|
-
/^relative/, // position
|
|
442
|
-
/^sticky/, // position
|
|
443
|
-
/^transition/, // transitions
|
|
444
|
-
/^duration-/, // duration
|
|
445
|
-
/^ease-/, // timing function
|
|
446
|
-
/^delay-/, // delay
|
|
447
|
-
/^animate-/, // animations
|
|
448
|
-
/^transform/, // transform
|
|
449
|
-
/^scale-/, // scale
|
|
450
|
-
/^rotate-/, // rotate
|
|
451
|
-
/^translate-/, // translate
|
|
452
|
-
/^skew-/, // skew
|
|
453
|
-
/^origin-/, // transform-origin
|
|
454
|
-
/^appearance-/, // appearance
|
|
455
|
-
/^accent-/, // accent-color
|
|
456
|
-
/^caret-/, // caret-color
|
|
457
|
-
/^fill-/, // SVG fill
|
|
458
|
-
/^stroke-/, // SVG stroke
|
|
459
|
-
/^object-/, // object-fit/position
|
|
460
|
-
/^aspect-/, // aspect-ratio
|
|
461
|
-
/^container/, // container
|
|
462
|
-
/^columns-/, // columns
|
|
463
|
-
/^break-/, // word-break
|
|
464
|
-
/^truncate/, // text-overflow
|
|
465
|
-
/^whitespace-/, // whitespace
|
|
466
|
-
/^list-/, // list-style
|
|
467
|
-
/^decoration-/, // text-decoration
|
|
468
|
-
/^underline/, // underline
|
|
469
|
-
/^overline/, // overline
|
|
470
|
-
/^line-through/, // line-through
|
|
471
|
-
/^no-underline/, // no underline
|
|
472
|
-
/^antialiased/, // font-smoothing
|
|
473
|
-
/^subpixel/, // subpixel-antialiased
|
|
474
|
-
/^italic/, // font-style
|
|
475
|
-
/^not-italic/, // font-style
|
|
476
|
-
/^uppercase/, // text-transform
|
|
477
|
-
/^lowercase/, // text-transform
|
|
478
|
-
/^capitalize/, // text-transform
|
|
479
|
-
/^normal-case/, // text-transform
|
|
480
|
-
/^align-/, // vertical-align
|
|
481
|
-
/^indent-/, // text-indent
|
|
482
|
-
/^content-/, // content
|
|
483
|
-
/^will-change/, // will-change
|
|
484
|
-
/^hover:/, // hover states
|
|
485
|
-
/^focus:/, // focus states
|
|
486
|
-
/^focus-within:/, // focus-within states
|
|
487
|
-
/^focus-visible:/, // focus-visible states
|
|
488
|
-
/^active:/, // active states
|
|
489
|
-
/^visited:/, // visited states
|
|
490
|
-
/^disabled:/, // disabled states
|
|
491
|
-
/^checked:/, // checked states
|
|
492
|
-
/^group-/, // group utilities
|
|
493
|
-
/^peer-/, // peer utilities
|
|
494
|
-
/^dark:/, // dark mode
|
|
495
|
-
/^light:/, // light mode
|
|
496
|
-
/^motion-/, // motion utilities
|
|
497
|
-
/^print:/, // print utilities
|
|
498
|
-
/^portrait:/, // orientation
|
|
499
|
-
/^landscape:/, // orientation
|
|
500
|
-
/^sm:|^md:|^lg:|^xl:|^2xl:/, // responsive prefixes
|
|
501
|
-
/^data-\[/, // data attribute variants
|
|
502
|
-
/^aria-/, // aria variants
|
|
503
|
-
/^supports-/, // supports variants
|
|
504
|
-
/^has-/, // has variants
|
|
505
|
-
/^group$/, // group class itself
|
|
506
|
-
/^peer$/, // peer class itself
|
|
507
|
-
/^sr-only/, // screen reader only
|
|
508
|
-
/^not-sr-only/, // not screen reader only
|
|
509
|
-
/^isolate/, // isolation
|
|
510
|
-
/^block$/, // display
|
|
511
|
-
/^inline/, // display
|
|
512
|
-
/^hidden$/, // display
|
|
513
|
-
/^table/, // display table
|
|
555
|
+
// SEMANTIC CLASS DETECTION: Instead of filtering OUT utilities, filter IN semantics
|
|
556
|
+
// These patterns identify classes that reference CSS variables (design tokens)
|
|
557
|
+
const semanticClassPatterns = [
|
|
558
|
+
// Background colors referencing CSS variables
|
|
559
|
+
/^bg-(card|background|foreground|primary|secondary|accent|destructive|muted|popover|input|sidebar)/,
|
|
560
|
+
// Text colors referencing CSS variables
|
|
561
|
+
/^text-(foreground|muted|primary|secondary|accent|destructive|card)/,
|
|
562
|
+
// Border colors referencing CSS variables
|
|
563
|
+
/^border-(border|card|input|primary|accent|destructive|muted)/,
|
|
564
|
+
// Any class containing these semantic tokens (catches hover:bg-card-hover, etc.)
|
|
565
|
+
/-card($|-)/,
|
|
566
|
+
/-foreground($|-)/,
|
|
567
|
+
/-background($|-)/,
|
|
568
|
+
/-border($|-)/,
|
|
569
|
+
/-primary($|-)/,
|
|
570
|
+
/-secondary($|-)/,
|
|
571
|
+
/-accent($|-)/,
|
|
572
|
+
/-muted($|-)/,
|
|
573
|
+
/-destructive($|-)/,
|
|
574
|
+
/-popover($|-)/,
|
|
575
|
+
/-sidebar($|-)/,
|
|
576
|
+
// Component-specific classes (likely custom)
|
|
577
|
+
/^(btn|card|modal|dialog|panel|sidebar|nav|menu|dropdown|tooltip|badge|chip|tag|avatar|icon)-/,
|
|
578
|
+
// Data attribute classes (often semantic)
|
|
579
|
+
/^\[data-/,
|
|
514
580
|
];
|
|
515
581
|
|
|
516
|
-
const
|
|
517
|
-
|
|
582
|
+
const isSemanticClass = (cls: string): boolean => {
|
|
583
|
+
// Remove variant prefixes for checking (hover:, focus:, dark:, etc.)
|
|
584
|
+
const baseClass = cls.replace(/^(hover:|focus:|active:|dark:|light:|md:|lg:|xl:|2xl:|sm:)+/, '');
|
|
585
|
+
return semanticClassPatterns.some(pattern => pattern.test(baseClass));
|
|
586
|
+
};
|
|
518
587
|
|
|
519
|
-
//
|
|
588
|
+
// Find semantic classes
|
|
520
589
|
const semanticClasses = focusedElement.className.split(/\s+/)
|
|
521
|
-
.filter(c => c.length > 3 &&
|
|
590
|
+
.filter(c => c.length > 3 && isSemanticClass(c));
|
|
522
591
|
|
|
523
592
|
// Only proceed if we found semantic classes
|
|
524
593
|
if (semanticClasses.length > 0) {
|
|
594
|
+
debugLog("Found semantic classes for element search", {
|
|
595
|
+
semanticClasses,
|
|
596
|
+
originalClasses: focusedElement.className.substring(0, 80)
|
|
597
|
+
});
|
|
598
|
+
|
|
525
599
|
for (const cls of semanticClasses) {
|
|
600
|
+
// Remove variant prefix for searching (hover:bg-card -> bg-card)
|
|
601
|
+
const searchClass = cls.replace(/^(hover:|focus:|active:|dark:|light:|md:|lg:|xl:|2xl:|sm:)+/, '');
|
|
602
|
+
|
|
526
603
|
for (let i = 0; i < lines.length; i++) {
|
|
527
|
-
if (lines[i].includes(
|
|
604
|
+
if (lines[i].includes(searchClass)) {
|
|
528
605
|
return {
|
|
529
606
|
lineNumber: i + 1,
|
|
530
607
|
snippet: lines.slice(Math.max(0, i - 3), i + 5).join('\n'),
|
|
531
608
|
confidence: 'medium',
|
|
532
|
-
matchedBy: `semantic className "${
|
|
609
|
+
matchedBy: `semantic className "${searchClass}"`
|
|
533
610
|
};
|
|
534
611
|
}
|
|
535
612
|
}
|
|
536
613
|
}
|
|
537
614
|
}
|
|
538
615
|
|
|
539
|
-
// Log when we
|
|
616
|
+
// Log when we couldn't find semantic classes (helpful for debugging)
|
|
540
617
|
if (semanticClasses.length === 0 && focusedElement.className.trim().length > 0) {
|
|
541
|
-
console.log('[findElementLineInFile]
|
|
618
|
+
console.log('[findElementLineInFile] No semantic classes found in:',
|
|
542
619
|
focusedElement.className.substring(0, 100));
|
|
543
620
|
}
|
|
544
621
|
}
|
|
@@ -647,39 +724,105 @@ function scoreFilesForTextContent(
|
|
|
647
724
|
*/
|
|
648
725
|
function findElementInImportedFiles(
|
|
649
726
|
focusedElement: VisionFocusedElement,
|
|
650
|
-
importedFiles: { path: string; content: string }[]
|
|
727
|
+
importedFiles: { path: string; content: string }[],
|
|
728
|
+
pageFilePath?: string // Pass page file for proximity scoring
|
|
651
729
|
): { path: string; lineNumber: number; matchedBy: string; content: string; confidence: string } | null {
|
|
652
730
|
debugLog("Searching imported components for element", {
|
|
653
731
|
elementType: focusedElement.type,
|
|
654
732
|
textContent: focusedElement.textContent?.substring(0, 30),
|
|
655
|
-
filesCount: importedFiles.length
|
|
733
|
+
filesCount: importedFiles.length,
|
|
734
|
+
pageFilePath
|
|
656
735
|
});
|
|
657
736
|
|
|
737
|
+
// Collect ALL matches with scores instead of returning first match
|
|
738
|
+
const matches: Array<{
|
|
739
|
+
path: string;
|
|
740
|
+
lineNumber: number;
|
|
741
|
+
matchedBy: string;
|
|
742
|
+
content: string;
|
|
743
|
+
confidence: string;
|
|
744
|
+
score: number;
|
|
745
|
+
}> = [];
|
|
746
|
+
|
|
747
|
+
// Extract route/directory info from page file for proximity scoring
|
|
748
|
+
const pageDir = pageFilePath ? path.dirname(pageFilePath) : '';
|
|
749
|
+
const routeName = pageDir.split('/').pop() || ''; // e.g., "typography"
|
|
750
|
+
|
|
658
751
|
for (const file of importedFiles) {
|
|
659
752
|
// Focus on component files (where UI elements live)
|
|
660
753
|
// Skip types, stores, utils, hooks - they don't contain JSX elements
|
|
661
|
-
if (!file.path.includes('components/') && !file.path.includes('/ui/')) continue;
|
|
754
|
+
if (!file.path.includes('components/') && !file.path.includes('/ui/') && !file.path.includes('/_components/')) continue;
|
|
662
755
|
|
|
663
756
|
const result = findElementLineInFile(file.content, focusedElement);
|
|
664
757
|
if (result && result.confidence !== 'low') {
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
}
|
|
671
|
-
|
|
758
|
+
let score = 0;
|
|
759
|
+
|
|
760
|
+
// Text content match is highest priority (+100)
|
|
761
|
+
if (result.matchedBy.includes('textContent') || result.matchedBy.includes('word match')) {
|
|
762
|
+
score += 100;
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
// Directory proximity - same directory tree as page file (+50)
|
|
766
|
+
// e.g., for page src/app/typography/page.tsx, prefer src/app/typography/_components/
|
|
767
|
+
if (pageDir && file.path.includes(pageDir)) {
|
|
768
|
+
score += 50;
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
// Route name in filename (+30)
|
|
772
|
+
// e.g., for /typography route, prefer DigitalTypography.tsx
|
|
773
|
+
if (routeName && routeName.length > 2 && file.path.toLowerCase().includes(routeName.toLowerCase())) {
|
|
774
|
+
score += 30;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
// Confidence bonus
|
|
778
|
+
score += result.confidence === 'high' ? 20 : 10;
|
|
779
|
+
|
|
780
|
+
matches.push({
|
|
672
781
|
path: file.path,
|
|
673
782
|
lineNumber: result.lineNumber,
|
|
674
783
|
matchedBy: result.matchedBy,
|
|
675
784
|
content: file.content,
|
|
676
|
-
confidence: result.confidence
|
|
677
|
-
|
|
785
|
+
confidence: result.confidence,
|
|
786
|
+
score
|
|
787
|
+
});
|
|
678
788
|
}
|
|
679
789
|
}
|
|
680
790
|
|
|
681
|
-
|
|
682
|
-
|
|
791
|
+
// Return highest-scoring match
|
|
792
|
+
if (matches.length === 0) {
|
|
793
|
+
debugLog("Element not found in any imported component files");
|
|
794
|
+
return null;
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
// Sort by score descending
|
|
798
|
+
matches.sort((a, b) => b.score - a.score);
|
|
799
|
+
|
|
800
|
+
debugLog("Scored imported component matches", {
|
|
801
|
+
matchCount: matches.length,
|
|
802
|
+
topMatches: matches.slice(0, 3).map(m => ({
|
|
803
|
+
path: m.path,
|
|
804
|
+
score: m.score,
|
|
805
|
+
matchedBy: m.matchedBy,
|
|
806
|
+
confidence: m.confidence
|
|
807
|
+
}))
|
|
808
|
+
});
|
|
809
|
+
|
|
810
|
+
const bestMatch = matches[0];
|
|
811
|
+
debugLog("Found element in imported component (best match)", {
|
|
812
|
+
path: bestMatch.path,
|
|
813
|
+
lineNumber: bestMatch.lineNumber,
|
|
814
|
+
matchedBy: bestMatch.matchedBy,
|
|
815
|
+
confidence: bestMatch.confidence,
|
|
816
|
+
score: bestMatch.score
|
|
817
|
+
});
|
|
818
|
+
|
|
819
|
+
return {
|
|
820
|
+
path: bestMatch.path,
|
|
821
|
+
lineNumber: bestMatch.lineNumber,
|
|
822
|
+
matchedBy: bestMatch.matchedBy,
|
|
823
|
+
content: bestMatch.content,
|
|
824
|
+
confidence: bestMatch.confidence
|
|
825
|
+
};
|
|
683
826
|
}
|
|
684
827
|
|
|
685
828
|
/**
|
|
@@ -1528,6 +1671,27 @@ DESIGN ISSUES TO FIX (${problems.length} problems identified)
|
|
|
1528
1671
|
|
|
1529
1672
|
${problemsList}
|
|
1530
1673
|
|
|
1674
|
+
BEFORE GENERATING PATCHES, analyze each problem:
|
|
1675
|
+
|
|
1676
|
+
For EACH problem, determine:
|
|
1677
|
+
1. What is the current DOM structure for this element?
|
|
1678
|
+
2. Can this be fixed by adjusting className values (spacing, alignment, colors)?
|
|
1679
|
+
3. Or does the DOM structure itself prevent the fix?
|
|
1680
|
+
|
|
1681
|
+
DECISION RULES:
|
|
1682
|
+
- If a problem is about SPACING: adjust margin/padding classes (mt-X, mb-X, gap-X, p-X)
|
|
1683
|
+
- If a problem is about ALIGNMENT: adjust flex/grid alignment classes (items-center, justify-between)
|
|
1684
|
+
- If a problem is about HIERARCHY: adjust font-size, font-weight, or color classes
|
|
1685
|
+
- If a problem is about GROUPING: check if elements are in the same container first
|
|
1686
|
+
→ If yes: adjust gap/spacing classes
|
|
1687
|
+
→ If no: consider minimal restructuring
|
|
1688
|
+
|
|
1689
|
+
ONLY restructure elements when:
|
|
1690
|
+
- Elements are in separate containers that prevent proper grouping
|
|
1691
|
+
- The current container type (div, flex, grid) fundamentally can't achieve the layout
|
|
1692
|
+
|
|
1693
|
+
PREFER the minimal change. A good designer makes surgical fixes, not rewrites.
|
|
1694
|
+
|
|
1531
1695
|
YOUR TASK: Generate AT LEAST ONE PATCH for EACH problem listed above.
|
|
1532
1696
|
Expected minimum patches: ${problems.length}
|
|
1533
1697
|
|
|
@@ -1546,12 +1710,14 @@ DO NOT:
|
|
|
1546
1710
|
- Add or remove classes/attributes
|
|
1547
1711
|
- Invent code that "should" exist
|
|
1548
1712
|
- Guess at the structure
|
|
1713
|
+
- Restructure when a className change would work
|
|
1549
1714
|
|
|
1550
1715
|
DO:
|
|
1551
1716
|
- Copy the exact text from the numbered lines
|
|
1552
1717
|
- Make small, targeted changes in the "replace" field
|
|
1553
1718
|
- Keep patches focused on one change each
|
|
1554
1719
|
- Generate one patch per problem minimum
|
|
1720
|
+
- Prefer adjusting existing classes over moving elements
|
|
1555
1721
|
`;
|
|
1556
1722
|
}
|
|
1557
1723
|
|
|
@@ -1763,6 +1929,7 @@ export async function POST(request: Request) {
|
|
|
1763
1929
|
let smartSearchFiles: { path: string; content: string }[] = [];
|
|
1764
1930
|
let recommendedFile: { path: string; reason: string } | null = null;
|
|
1765
1931
|
let deterministicMatch: { path: string; lineNumber: number } | null = null;
|
|
1932
|
+
let phase1VisibleText: string[] = []; // Store Phase 1 visible text for use in file scoring
|
|
1766
1933
|
|
|
1767
1934
|
// ========================================================================
|
|
1768
1935
|
// PHASE 0: Deterministic Element ID Search (Cursor-style explicit selection)
|
|
@@ -1828,6 +1995,9 @@ export async function POST(request: Request) {
|
|
|
1828
1995
|
if (!deterministicMatch && screenshot) {
|
|
1829
1996
|
debugLog("Starting Phase 1: LLM screenshot analysis");
|
|
1830
1997
|
const analysis = await analyzeScreenshotForSearch(screenshot, userPrompt, apiKey);
|
|
1998
|
+
|
|
1999
|
+
// Store visible text for use in file scoring when no element is focused
|
|
2000
|
+
phase1VisibleText = analysis.visibleText || [];
|
|
1831
2001
|
|
|
1832
2002
|
const hasSearchTerms = analysis.visibleText.length > 0 ||
|
|
1833
2003
|
analysis.componentNames.length > 0 ||
|
|
@@ -2138,7 +2308,8 @@ export async function POST(request: Request) {
|
|
|
2138
2308
|
|
|
2139
2309
|
const importedMatch = findElementInImportedFiles(
|
|
2140
2310
|
focusedElements[0],
|
|
2141
|
-
pageContext.componentSources
|
|
2311
|
+
pageContext.componentSources,
|
|
2312
|
+
actualTargetFile?.path ?? undefined // Pass page file for proximity scoring
|
|
2142
2313
|
);
|
|
2143
2314
|
|
|
2144
2315
|
if (importedMatch) {
|
|
@@ -2157,6 +2328,48 @@ export async function POST(request: Request) {
|
|
|
2157
2328
|
}
|
|
2158
2329
|
}
|
|
2159
2330
|
}
|
|
2331
|
+
// ========== AUTO TEXT SCORING (no focused element) ==========
|
|
2332
|
+
// When no element is clicked but we have Phase 1 visible text, use it to find the best file
|
|
2333
|
+
else if (actualTargetFile && (!focusedElements || focusedElements.length === 0) && phase1VisibleText.length > 0) {
|
|
2334
|
+
debugLog("No focused element - using Phase 1 visible text for file scoring", {
|
|
2335
|
+
textCount: phase1VisibleText.length,
|
|
2336
|
+
texts: phase1VisibleText.slice(0, 5)
|
|
2337
|
+
});
|
|
2338
|
+
|
|
2339
|
+
// Score all imported files based on visible text from Phase 1
|
|
2340
|
+
const bestMatch = scoreFilesForTextContent(
|
|
2341
|
+
phase1VisibleText,
|
|
2342
|
+
pageContext.componentSources
|
|
2343
|
+
);
|
|
2344
|
+
|
|
2345
|
+
// Check how well the current target file matches
|
|
2346
|
+
const currentFileScore = phase1VisibleText.filter(text =>
|
|
2347
|
+
actualTargetFile!.content.includes(text.substring(0, 20))
|
|
2348
|
+
).length;
|
|
2349
|
+
|
|
2350
|
+
debugLog("Phase 1 text scoring comparison", {
|
|
2351
|
+
currentFile: actualTargetFile.path,
|
|
2352
|
+
currentScore: currentFileScore,
|
|
2353
|
+
bestImport: bestMatch?.path || 'none',
|
|
2354
|
+
bestImportScore: bestMatch?.score || 0
|
|
2355
|
+
});
|
|
2356
|
+
|
|
2357
|
+
// Redirect if an imported file has significantly more text matches
|
|
2358
|
+
if (bestMatch && bestMatch.score > currentFileScore && bestMatch.score >= 2) {
|
|
2359
|
+
debugLog("TEXT REDIRECT (no element): Phase 1 visible text found better file", {
|
|
2360
|
+
originalFile: actualTargetFile.path,
|
|
2361
|
+
originalScore: currentFileScore,
|
|
2362
|
+
redirectTo: bestMatch.path,
|
|
2363
|
+
redirectScore: bestMatch.score,
|
|
2364
|
+
matchedTexts: bestMatch.matchedTexts
|
|
2365
|
+
});
|
|
2366
|
+
|
|
2367
|
+
actualTargetFile = {
|
|
2368
|
+
path: bestMatch.path,
|
|
2369
|
+
content: bestMatch.content
|
|
2370
|
+
};
|
|
2371
|
+
}
|
|
2372
|
+
}
|
|
2160
2373
|
|
|
2161
2374
|
debugLog("File redirect complete", {
|
|
2162
2375
|
originalRecommended: recommendedFileContent?.path || 'none',
|
|
@@ -2539,11 +2752,24 @@ This is better than generating patches with made-up code.`,
|
|
|
2539
2752
|
try {
|
|
2540
2753
|
// Use robust JSON extraction that handles preamble text, code fences, etc.
|
|
2541
2754
|
const jsonText = extractJsonFromResponse(textResponse.text);
|
|
2755
|
+
debugLog("[JSON Parse] Attempting to parse extracted JSON", {
|
|
2756
|
+
extractedLength: jsonText.length,
|
|
2757
|
+
extractedFirst100: jsonText.substring(0, 100),
|
|
2758
|
+
extractedLast100: jsonText.substring(jsonText.length - 100)
|
|
2759
|
+
});
|
|
2542
2760
|
aiResponse = JSON.parse(jsonText);
|
|
2543
|
-
|
|
2761
|
+
debugLog("[JSON Parse] Successfully parsed AI response");
|
|
2762
|
+
} catch (parseError) {
|
|
2763
|
+
const error = parseError as Error;
|
|
2544
2764
|
console.error("Failed to parse AI response:", textResponse.text);
|
|
2765
|
+
debugLog("[JSON Parse] FAILED to parse", {
|
|
2766
|
+
error: error.message,
|
|
2767
|
+
originalLength: textResponse.text.length,
|
|
2768
|
+
originalFirst100: textResponse.text.substring(0, 100),
|
|
2769
|
+
originalLast100: textResponse.text.substring(textResponse.text.length - 100)
|
|
2770
|
+
});
|
|
2545
2771
|
return NextResponse.json(
|
|
2546
|
-
{ error:
|
|
2772
|
+
{ error: `Failed to parse AI response: ${error.message}` },
|
|
2547
2773
|
{ status: 500 }
|
|
2548
2774
|
);
|
|
2549
2775
|
}
|