@luci-dev/format 0.5.8 → 0.7.1
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.
- package/dist/capture/baseline.d.ts.map +1 -1
- package/dist/capture/baseline.js +20 -3
- package/dist/capture/baseline.js.map +1 -1
- package/dist/capture/check.d.ts +2 -0
- package/dist/capture/check.d.ts.map +1 -1
- package/dist/capture/check.js +72 -7
- package/dist/capture/check.js.map +1 -1
- package/dist/capture/collection.d.ts +46 -0
- package/dist/capture/collection.d.ts.map +1 -1
- package/dist/capture/compare.d.ts.map +1 -1
- package/dist/capture/compare.js +28 -7
- package/dist/capture/compare.js.map +1 -1
- package/dist/capture/format.d.ts +6 -2
- package/dist/capture/format.d.ts.map +1 -1
- package/dist/capture/format.js +77 -33
- package/dist/capture/format.js.map +1 -1
- package/dist/capture/index.d.ts +1 -0
- package/dist/capture/index.d.ts.map +1 -1
- package/dist/capture/index.js +1 -0
- package/dist/capture/index.js.map +1 -1
- package/dist/capture/merge.js +4 -4
- package/dist/capture/merge.js.map +1 -1
- package/dist/capture/read.d.ts.map +1 -1
- package/dist/capture/read.js +7 -3
- package/dist/capture/read.js.map +1 -1
- package/dist/capture/remap.d.ts.map +1 -1
- package/dist/capture/remap.js +21 -11
- package/dist/capture/remap.js.map +1 -1
- package/dist/capture/schema.d.ts +70 -2
- package/dist/capture/schema.d.ts.map +1 -1
- package/dist/capture/schema.js +19 -4
- package/dist/capture/schema.js.map +1 -1
- package/dist/capture/spec.d.ts +368 -0
- package/dist/capture/spec.d.ts.map +1 -0
- package/dist/capture/spec.js +402 -0
- package/dist/capture/spec.js.map +1 -0
- package/dist/capture/stitch.d.ts +4 -2
- package/dist/capture/stitch.d.ts.map +1 -1
- package/dist/capture/stitch.js +169 -80
- package/dist/capture/stitch.js.map +1 -1
- package/dist/capture/tokensExport.d.ts +3 -1
- package/dist/capture/tokensExport.d.ts.map +1 -1
- package/dist/capture/tokensExport.js +198 -127
- package/dist/capture/tokensExport.js.map +1 -1
- package/dist/capture/values.d.ts +32 -4
- package/dist/capture/values.d.ts.map +1 -1
- package/dist/capture/values.js +59 -3
- package/dist/capture/values.js.map +1 -1
- package/dist/feedback/index.d.ts +1 -0
- package/dist/feedback/index.d.ts.map +1 -1
- package/dist/feedback/index.js +1 -0
- package/dist/feedback/index.js.map +1 -1
- package/dist/feedback/prompt.d.ts.map +1 -1
- package/dist/feedback/prompt.js +21 -8
- package/dist/feedback/prompt.js.map +1 -1
- package/dist/feedback/review.d.ts +178 -0
- package/dist/feedback/review.d.ts.map +1 -0
- package/dist/feedback/review.js +687 -0
- package/dist/feedback/review.js.map +1 -0
- package/dist/feedback/schema.d.ts +17 -0
- package/dist/feedback/schema.d.ts.map +1 -1
- package/dist/feedback/schema.js +17 -2
- package/dist/feedback/schema.js.map +1 -1
- package/dist/memory/brainJson.d.ts +4 -0
- package/dist/memory/brainJson.d.ts.map +1 -1
- package/dist/memory/brainJson.js +3 -1
- package/dist/memory/brainJson.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,687 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Team Feedback review format, resilient parser, duplicate merger, and untrusted-text sanitizer.
|
|
3
|
+
*
|
|
4
|
+
* Human-readable text comes first (header + numbered list), followed by one compact `luci-review`
|
|
5
|
+
* fenced NDJSON block holding anchors and measured values. If the block is edited, mangled by
|
|
6
|
+
* Slack/email, or deleted, parsing degrades cleanly to plain-text notes without anchors and never
|
|
7
|
+
* throws. Browser-safe: no Node imports.
|
|
8
|
+
*/
|
|
9
|
+
import { z } from 'zod';
|
|
10
|
+
import { REVIEWER_ROLES, routeOf } from './schema.js';
|
|
11
|
+
export const REVIEW_FORMAT_VERSION = 1;
|
|
12
|
+
export const REVIEW_FENCE = 'luci-review';
|
|
13
|
+
export const reviewMeasurementsSchema = z.object({
|
|
14
|
+
color: z.string().min(1).optional(),
|
|
15
|
+
background: z.string().min(1).optional(),
|
|
16
|
+
fontFamily: z.string().min(1).optional(),
|
|
17
|
+
fontSize: z.string().min(1).optional(),
|
|
18
|
+
fontWeight: z.string().min(1).optional(),
|
|
19
|
+
padding: z.string().min(1).optional(),
|
|
20
|
+
margin: z.string().min(1).optional(),
|
|
21
|
+
gap: z.string().min(1).optional(),
|
|
22
|
+
borderRadius: z.string().min(1).optional()
|
|
23
|
+
});
|
|
24
|
+
export const reviewAnchorSchema = z.object({
|
|
25
|
+
element: z.string().min(1).optional(),
|
|
26
|
+
text: z.string().min(1).optional(),
|
|
27
|
+
component: z.string().min(1).optional(),
|
|
28
|
+
selector: z.string().min(1).optional(),
|
|
29
|
+
position: z.tuple([z.number(), z.number()]).optional(),
|
|
30
|
+
/** Where in the element the reviewer clicked, as fractions of its width and height (0-1), for the marker. */
|
|
31
|
+
point: z.object({ x: z.number().min(0).max(1), y: z.number().min(0).max(1) }).optional()
|
|
32
|
+
});
|
|
33
|
+
export const reviewNoteSchema = z.object({
|
|
34
|
+
id: z.string().min(1),
|
|
35
|
+
path: z.string().min(1),
|
|
36
|
+
note: z.string().trim().min(1),
|
|
37
|
+
anchor: reviewAnchorSchema.optional(),
|
|
38
|
+
measured: reviewMeasurementsSchema.optional(),
|
|
39
|
+
url: z.string().min(1).optional(),
|
|
40
|
+
viewport: z.string().min(1).optional(),
|
|
41
|
+
browser: z.string().min(1).optional(),
|
|
42
|
+
createdAt: z.string().min(1).optional()
|
|
43
|
+
});
|
|
44
|
+
export const reviewerIdentitySchema = z.object({
|
|
45
|
+
name: z.string().trim().min(1).max(80),
|
|
46
|
+
role: z.enum(REVIEWER_ROLES)
|
|
47
|
+
});
|
|
48
|
+
export const teamReviewSchema = z.object({
|
|
49
|
+
version: z.literal(REVIEW_FORMAT_VERSION),
|
|
50
|
+
reviewer: reviewerIdentitySchema,
|
|
51
|
+
site: z.string().min(1),
|
|
52
|
+
createdAt: z.string().min(1),
|
|
53
|
+
viewport: z.string().min(1).optional(),
|
|
54
|
+
browser: z.string().min(1).optional(),
|
|
55
|
+
notes: z.array(reviewNoteSchema),
|
|
56
|
+
degraded: z.boolean().optional(),
|
|
57
|
+
warning: z.string().optional()
|
|
58
|
+
});
|
|
59
|
+
/** Escape untrusted reviewer words so they cannot inject markdown headings, fences, or control chars. */
|
|
60
|
+
export function sanitizeReviewerText(text) {
|
|
61
|
+
return text
|
|
62
|
+
// Strip ASCII control characters except whitespace
|
|
63
|
+
.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/g, '')
|
|
64
|
+
// Neutralize fenced code blocks so reviewer text cannot break out of prompts or blocks
|
|
65
|
+
.replace(/```+/g, "'''")
|
|
66
|
+
.replace(/~~~+/g, '---')
|
|
67
|
+
// Neutralize leading markdown headings on any line
|
|
68
|
+
.replace(/(^|\n)\s{0,3}(#{1,6}\s)/g, '$1\\$2')
|
|
69
|
+
.replace(/\r\n?/g, '\n')
|
|
70
|
+
.trim();
|
|
71
|
+
}
|
|
72
|
+
/** Single-line cleaned version of untrusted reviewer text for inline display or prompt quoting. */
|
|
73
|
+
export function sanitizeReviewerInline(text) {
|
|
74
|
+
return sanitizeReviewerText(text).replace(/\s*\n+\s*/g, ' ').replace(/\s{2,}/g, ' ').trim();
|
|
75
|
+
}
|
|
76
|
+
const shortText = (value, max = 96) => {
|
|
77
|
+
if (!value)
|
|
78
|
+
return undefined;
|
|
79
|
+
const clean = value.replace(/\s+/g, ' ').trim();
|
|
80
|
+
if (!clean)
|
|
81
|
+
return undefined;
|
|
82
|
+
return clean.length > max ? `${clean.slice(0, max - 1)}…` : clean;
|
|
83
|
+
};
|
|
84
|
+
/** Normalize a route/path: always starts with /, strips trailing slash except root, keeps hash route. */
|
|
85
|
+
export function normalizeReviewPath(pathOrUrl) {
|
|
86
|
+
// A path is untrusted reviewer input too: one line, no fences or headings (B-54)
|
|
87
|
+
const trimmed = sanitizeReviewerInline(pathOrUrl).replace(/\s+/g, '');
|
|
88
|
+
if (!trimmed)
|
|
89
|
+
return '/';
|
|
90
|
+
const raw = /^https?:\/\//i.test(trimmed) ? routeOf(trimmed) : trimmed;
|
|
91
|
+
const withoutQuery = raw.includes('#/') ? raw : raw.split('?')[0].split('#')[0];
|
|
92
|
+
const prefixed = withoutQuery.startsWith('/') ? withoutQuery : `/${withoutQuery}`;
|
|
93
|
+
return prefixed.length > 1 && prefixed.endsWith('/') ? prefixed.slice(0, -1) : prefixed;
|
|
94
|
+
}
|
|
95
|
+
const datePart = (iso) => {
|
|
96
|
+
const m = /^(\d{4}-\d{2}-\d{2})/.exec(iso.trim());
|
|
97
|
+
return m ? m[1] : iso.slice(0, 10) || 'unknown';
|
|
98
|
+
};
|
|
99
|
+
const safeJsonLine = (obj) => JSON.stringify(obj).replace(/```/g, '\\u0060\\u0060\\u0060');
|
|
100
|
+
const compactMeasured = (m) => {
|
|
101
|
+
if (!m)
|
|
102
|
+
return undefined;
|
|
103
|
+
const out = {};
|
|
104
|
+
if (m.color)
|
|
105
|
+
out.c = m.color;
|
|
106
|
+
if (m.background)
|
|
107
|
+
out.bg = m.background;
|
|
108
|
+
if (m.fontFamily)
|
|
109
|
+
out.ff = m.fontFamily;
|
|
110
|
+
if (m.fontSize)
|
|
111
|
+
out.fs = m.fontSize;
|
|
112
|
+
if (m.fontWeight)
|
|
113
|
+
out.fw = m.fontWeight;
|
|
114
|
+
if (m.padding)
|
|
115
|
+
out.pd = m.padding;
|
|
116
|
+
if (m.margin)
|
|
117
|
+
out.mg = m.margin;
|
|
118
|
+
if (m.gap)
|
|
119
|
+
out.gp = m.gap;
|
|
120
|
+
if (m.borderRadius)
|
|
121
|
+
out.br = m.borderRadius;
|
|
122
|
+
return Object.keys(out).length ? out : undefined;
|
|
123
|
+
};
|
|
124
|
+
const expandMeasured = (raw) => {
|
|
125
|
+
if (!raw || typeof raw !== 'object')
|
|
126
|
+
return undefined;
|
|
127
|
+
const r = raw;
|
|
128
|
+
const str = (k) => (typeof r[k] === 'string' && r[k].trim() ? r[k].trim() : undefined);
|
|
129
|
+
const out = {};
|
|
130
|
+
const color = str('c') ?? str('color');
|
|
131
|
+
const background = str('bg') ?? str('background');
|
|
132
|
+
const fontFamily = str('ff') ?? str('fontFamily');
|
|
133
|
+
const fontSize = str('fs') ?? str('fontSize');
|
|
134
|
+
const fontWeight = str('fw') ?? str('fontWeight');
|
|
135
|
+
const padding = str('pd') ?? str('padding');
|
|
136
|
+
const margin = str('mg') ?? str('margin');
|
|
137
|
+
const gap = str('gp') ?? str('gap');
|
|
138
|
+
const borderRadius = str('br') ?? str('borderRadius');
|
|
139
|
+
if (color)
|
|
140
|
+
out.color = color;
|
|
141
|
+
if (background)
|
|
142
|
+
out.background = background;
|
|
143
|
+
if (fontFamily)
|
|
144
|
+
out.fontFamily = fontFamily;
|
|
145
|
+
if (fontSize)
|
|
146
|
+
out.fontSize = fontSize;
|
|
147
|
+
if (fontWeight)
|
|
148
|
+
out.fontWeight = fontWeight;
|
|
149
|
+
if (padding)
|
|
150
|
+
out.padding = padding;
|
|
151
|
+
if (margin)
|
|
152
|
+
out.margin = margin;
|
|
153
|
+
if (gap)
|
|
154
|
+
out.gap = gap;
|
|
155
|
+
if (borderRadius)
|
|
156
|
+
out.borderRadius = borderRadius;
|
|
157
|
+
return Object.keys(out).length ? out : undefined;
|
|
158
|
+
};
|
|
159
|
+
/** Format a Team Review as human-readable text plus one compact `luci-review` fenced NDJSON block. */
|
|
160
|
+
export function formatTeamReview(review) {
|
|
161
|
+
const count = review.notes.length;
|
|
162
|
+
const noteWord = count === 1 ? 'note' : 'notes';
|
|
163
|
+
const name = sanitizeReviewerInline(review.reviewer.name) || 'Reviewer';
|
|
164
|
+
const role = review.reviewer.role;
|
|
165
|
+
const site = review.site.trim() || 'unknown';
|
|
166
|
+
const date = datePart(review.createdAt);
|
|
167
|
+
const lines = [
|
|
168
|
+
`Luci Review — ${name} (${role})`,
|
|
169
|
+
`Site: ${site} · Date: ${date} · ${count} ${noteWord}`,
|
|
170
|
+
''
|
|
171
|
+
];
|
|
172
|
+
review.notes.forEach((n, idx) => {
|
|
173
|
+
const p = normalizeReviewPath(n.path);
|
|
174
|
+
lines.push(`${idx + 1}. [${p}] ${sanitizeReviewerInline(n.note)}`);
|
|
175
|
+
});
|
|
176
|
+
const headerObj = {
|
|
177
|
+
v: REVIEW_FORMAT_VERSION,
|
|
178
|
+
r: name,
|
|
179
|
+
role,
|
|
180
|
+
site,
|
|
181
|
+
at: review.createdAt
|
|
182
|
+
};
|
|
183
|
+
if (review.viewport)
|
|
184
|
+
headerObj.vp = review.viewport;
|
|
185
|
+
if (review.browser)
|
|
186
|
+
headerObj.br = review.browser;
|
|
187
|
+
const blockLines = [safeJsonLine(headerObj)];
|
|
188
|
+
review.notes.forEach((n, idx) => {
|
|
189
|
+
const row = {
|
|
190
|
+
n: idx + 1,
|
|
191
|
+
p: normalizeReviewPath(n.path)
|
|
192
|
+
};
|
|
193
|
+
if (n.anchor?.element)
|
|
194
|
+
row.el = shortText(n.anchor.element, 48);
|
|
195
|
+
if (n.anchor?.text)
|
|
196
|
+
row.txt = shortText(n.anchor.text, 80);
|
|
197
|
+
if (n.anchor?.component)
|
|
198
|
+
row.cmp = shortText(n.anchor.component, 48);
|
|
199
|
+
if (n.anchor?.selector)
|
|
200
|
+
row.sel = shortText(n.anchor.selector, 160);
|
|
201
|
+
if (n.anchor?.position)
|
|
202
|
+
row.pos = [Math.round(n.anchor.position[0]), Math.round(n.anchor.position[1])];
|
|
203
|
+
const m = compactMeasured(n.measured);
|
|
204
|
+
if (m)
|
|
205
|
+
row.m = m;
|
|
206
|
+
if (n.viewport && n.viewport !== review.viewport)
|
|
207
|
+
row.vp = n.viewport;
|
|
208
|
+
if (n.createdAt && n.createdAt !== review.createdAt)
|
|
209
|
+
row.at = n.createdAt;
|
|
210
|
+
blockLines.push(safeJsonLine(row));
|
|
211
|
+
});
|
|
212
|
+
lines.push('', `\`\`\`${REVIEW_FENCE}`, ...blockLines, '```');
|
|
213
|
+
return lines.join('\n');
|
|
214
|
+
}
|
|
215
|
+
function normalizeRole(value) {
|
|
216
|
+
if (typeof value !== 'string')
|
|
217
|
+
return 'Stakeholder';
|
|
218
|
+
const clean = value.trim().toLowerCase();
|
|
219
|
+
const match = REVIEWER_ROLES.find(r => r.toLowerCase() === clean);
|
|
220
|
+
if (match)
|
|
221
|
+
return match;
|
|
222
|
+
if (clean === 'dev' || clean === 'engineer' || clean === 'eng')
|
|
223
|
+
return 'Developer';
|
|
224
|
+
if (clean === 'product' || clean === 'product manager')
|
|
225
|
+
return 'PM';
|
|
226
|
+
if (clean === 'design')
|
|
227
|
+
return 'Designer';
|
|
228
|
+
if (clean === 'customer')
|
|
229
|
+
return 'Client';
|
|
230
|
+
return 'Stakeholder';
|
|
231
|
+
}
|
|
232
|
+
/** Repair common Slack/email mangling (smart quotes, NBSP, zero-width spaces). */
|
|
233
|
+
function normalizePastedText(raw) {
|
|
234
|
+
return raw
|
|
235
|
+
.replace(/\r\n?/g, '\n')
|
|
236
|
+
.replace(/[\u200B-\u200D\uFEFF]/g, '')
|
|
237
|
+
.replace(/\u00a0/g, ' ')
|
|
238
|
+
.replace(/[\u201c\u201d]/g, '"')
|
|
239
|
+
.replace(/[\u2018\u2019]/g, "'");
|
|
240
|
+
}
|
|
241
|
+
function splitReviewChunks(text) {
|
|
242
|
+
const lines = text.split('\n');
|
|
243
|
+
const chunks = [];
|
|
244
|
+
let current = [];
|
|
245
|
+
let inFence = false;
|
|
246
|
+
const isHeader = (line) => /^Luci Review\s*[—–\-:]/i.test(line.trim());
|
|
247
|
+
for (const line of lines) {
|
|
248
|
+
const trimmed = line.trim();
|
|
249
|
+
if (/^```/.test(trimmed)) {
|
|
250
|
+
if (!inFence && /^```luci-review\b/i.test(trimmed)) {
|
|
251
|
+
// If current chunk already closed a previous luci-review block, start a new chunk.
|
|
252
|
+
if (current.some(l => /^```luci-review\b/i.test(l.trim()))) {
|
|
253
|
+
chunks.push(current);
|
|
254
|
+
current = [];
|
|
255
|
+
}
|
|
256
|
+
inFence = true;
|
|
257
|
+
}
|
|
258
|
+
else if (inFence) {
|
|
259
|
+
inFence = false;
|
|
260
|
+
}
|
|
261
|
+
current.push(line);
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
if (!inFence && isHeader(line) && current.some(l => l.trim().length > 0)) {
|
|
265
|
+
chunks.push(current);
|
|
266
|
+
current = [line];
|
|
267
|
+
continue;
|
|
268
|
+
}
|
|
269
|
+
current.push(line);
|
|
270
|
+
}
|
|
271
|
+
if (current.some(l => l.trim().length > 0))
|
|
272
|
+
chunks.push(current);
|
|
273
|
+
return chunks.map(c => c.join('\n').trim()).filter(Boolean);
|
|
274
|
+
}
|
|
275
|
+
function parseHumanSection(text) {
|
|
276
|
+
const out = { notes: [] };
|
|
277
|
+
const lines = text.split('\n');
|
|
278
|
+
for (const rawLine of lines) {
|
|
279
|
+
const line = rawLine.trim();
|
|
280
|
+
if (!line)
|
|
281
|
+
continue;
|
|
282
|
+
const headerMatch = /^Luci Review\s*[—–\-:]\s*(.+?)(?:\s*\(([^)]+)\))?\s*$/i.exec(line);
|
|
283
|
+
if (headerMatch) {
|
|
284
|
+
out.name = headerMatch[1].trim();
|
|
285
|
+
if (headerMatch[2])
|
|
286
|
+
out.role = normalizeRole(headerMatch[2]);
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
const metaMatch = /^Site:\s*(\S+)(?:.*?Date:\s*(\d{4}-\d{2}-\d{2}))?/i.exec(line);
|
|
290
|
+
if (metaMatch) {
|
|
291
|
+
out.site = sanitizeReviewerInline(metaMatch[1].replace(/[·,|]+$/, ''));
|
|
292
|
+
if (metaMatch[2])
|
|
293
|
+
out.date = metaMatch[2];
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
const numbered = /^(\d+)[.)]\s+(?:\[([^\]]+)\]\s*)?(.+)$/.exec(line);
|
|
297
|
+
if (numbered) {
|
|
298
|
+
const note = numbered[3].trim();
|
|
299
|
+
if (note) {
|
|
300
|
+
out.notes.push({
|
|
301
|
+
n: Number(numbered[1]),
|
|
302
|
+
path: numbered[2] ? normalizeReviewPath(numbered[2]) : undefined,
|
|
303
|
+
note
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
const bulleted = /^[-*•]\s+(?:\[([^\]]+)\]\s*)?(.+)$/.exec(line);
|
|
309
|
+
if (bulleted) {
|
|
310
|
+
const note = bulleted[2].trim();
|
|
311
|
+
if (note) {
|
|
312
|
+
out.notes.push({
|
|
313
|
+
n: out.notes.length + 1,
|
|
314
|
+
path: bulleted[1] ? normalizeReviewPath(bulleted[1]) : undefined,
|
|
315
|
+
note
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
return out;
|
|
321
|
+
}
|
|
322
|
+
/** A string field from a pasted luci-review line, cleaned to one safe line; undefined when empty. */
|
|
323
|
+
const inlineField = (value) => typeof value === 'string' ? sanitizeReviewerInline(value) || undefined : undefined;
|
|
324
|
+
function parseMachineBlock(blockBody, unclosed) {
|
|
325
|
+
const notes = new Map();
|
|
326
|
+
let header;
|
|
327
|
+
let damagedLines = 0;
|
|
328
|
+
let autoIndex = 1;
|
|
329
|
+
for (const rawLine of blockBody.split('\n')) {
|
|
330
|
+
const line = rawLine.trim();
|
|
331
|
+
if (!line)
|
|
332
|
+
continue;
|
|
333
|
+
try {
|
|
334
|
+
const obj = JSON.parse(line);
|
|
335
|
+
if (typeof obj !== 'object' || obj === null) {
|
|
336
|
+
damagedLines++;
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
if ('v' in obj && !('n' in obj) && !('note' in obj)) {
|
|
340
|
+
header = {
|
|
341
|
+
version: typeof obj.v === 'number' ? obj.v : REVIEW_FORMAT_VERSION,
|
|
342
|
+
name: inlineField(obj.r),
|
|
343
|
+
role: obj.role ? normalizeRole(obj.role) : undefined,
|
|
344
|
+
site: inlineField(obj.site),
|
|
345
|
+
createdAt: inlineField(obj.at),
|
|
346
|
+
viewport: inlineField(obj.vp),
|
|
347
|
+
browser: inlineField(obj.br)
|
|
348
|
+
};
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
const noteText = typeof obj.note === 'string' ? obj.note.trim() : '';
|
|
352
|
+
const hasN = typeof obj.n === 'number' && Number.isFinite(obj.n) && obj.n > 0;
|
|
353
|
+
if (!noteText && !hasN) {
|
|
354
|
+
damagedLines++;
|
|
355
|
+
continue;
|
|
356
|
+
}
|
|
357
|
+
const n = hasN ? Math.floor(obj.n) : autoIndex;
|
|
358
|
+
autoIndex = Math.max(autoIndex, n + 1);
|
|
359
|
+
// Every anchor field is untrusted reviewer input that reaches the agent prompt: one line, no
|
|
360
|
+
// fences or headings, like the note itself (B-54)
|
|
361
|
+
const anchor = {};
|
|
362
|
+
const element = inlineField(obj.el), text = inlineField(obj.txt), component = inlineField(obj.cmp), selector = inlineField(obj.sel);
|
|
363
|
+
if (element)
|
|
364
|
+
anchor.element = element;
|
|
365
|
+
if (text)
|
|
366
|
+
anchor.text = text;
|
|
367
|
+
if (component)
|
|
368
|
+
anchor.component = component;
|
|
369
|
+
if (selector)
|
|
370
|
+
anchor.selector = selector;
|
|
371
|
+
if (Array.isArray(obj.pos) && obj.pos.length === 2 && typeof obj.pos[0] === 'number' && typeof obj.pos[1] === 'number') {
|
|
372
|
+
anchor.position = [obj.pos[0], obj.pos[1]];
|
|
373
|
+
}
|
|
374
|
+
const measured = expandMeasured(obj.m);
|
|
375
|
+
const path = typeof obj.p === 'string' && obj.p.trim() ? normalizeReviewPath(obj.p) : '/';
|
|
376
|
+
notes.set(n, {
|
|
377
|
+
id: `n-${n}`,
|
|
378
|
+
path,
|
|
379
|
+
note: noteText,
|
|
380
|
+
anchor: Object.keys(anchor).length ? anchor : undefined,
|
|
381
|
+
measured,
|
|
382
|
+
viewport: inlineField(obj.vp),
|
|
383
|
+
createdAt: inlineField(obj.at)
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
catch {
|
|
387
|
+
damagedLines++;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
return { header, notes, hadBlock: true, damagedLines, unclosed };
|
|
391
|
+
}
|
|
392
|
+
function parseSingleReviewChunk(chunk, chunkIdx, fallbackDateIso) {
|
|
393
|
+
const fenceStart = /```luci-review[^\n]*\n?/i.exec(chunk);
|
|
394
|
+
let humanPart = chunk;
|
|
395
|
+
let machine = { notes: new Map(), hadBlock: false, damagedLines: 0, unclosed: false };
|
|
396
|
+
if (fenceStart) {
|
|
397
|
+
humanPart = chunk.slice(0, fenceStart.index);
|
|
398
|
+
const afterStart = chunk.slice(fenceStart.index + fenceStart[0].length);
|
|
399
|
+
const fenceEnd = /\n```(?:\s|$)/.exec(afterStart);
|
|
400
|
+
const blockBody = fenceEnd ? afterStart.slice(0, fenceEnd.index) : afterStart;
|
|
401
|
+
if (fenceEnd) {
|
|
402
|
+
const trailing = afterStart.slice(fenceEnd.index + fenceEnd[0].length).trim();
|
|
403
|
+
if (trailing)
|
|
404
|
+
humanPart = `${humanPart}\n${trailing}`;
|
|
405
|
+
}
|
|
406
|
+
machine = parseMachineBlock(blockBody, !fenceEnd);
|
|
407
|
+
}
|
|
408
|
+
const human = parseHumanSection(humanPart);
|
|
409
|
+
// Merge machine notes + human notes so human edits survive and damaged lines fall back to plain text.
|
|
410
|
+
const combined = [];
|
|
411
|
+
const usedMachine = new Set();
|
|
412
|
+
if (human.notes.length > 0) {
|
|
413
|
+
for (const hn of human.notes) {
|
|
414
|
+
const mn = machine.notes.get(hn.n);
|
|
415
|
+
if (mn) {
|
|
416
|
+
usedMachine.add(hn.n);
|
|
417
|
+
combined.push({
|
|
418
|
+
...mn,
|
|
419
|
+
id: `r${chunkIdx}-n${hn.n}`,
|
|
420
|
+
path: hn.path ?? mn.path ?? '/',
|
|
421
|
+
note: hn.note
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
else {
|
|
425
|
+
combined.push({
|
|
426
|
+
id: `r${chunkIdx}-n${hn.n}`,
|
|
427
|
+
path: hn.path ?? '/',
|
|
428
|
+
note: hn.note
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
// Include any machine notes that carry note text and weren't in the human list
|
|
433
|
+
for (const [n, mn] of [...machine.notes.entries()].sort((a, b) => a[0] - b[0])) {
|
|
434
|
+
if (!usedMachine.has(n) && mn.note) {
|
|
435
|
+
combined.push({ ...mn, id: `r${chunkIdx}-n${n}` });
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
else if (machine.notes.size > 0) {
|
|
440
|
+
for (const [n, mn] of [...machine.notes.entries()].sort((a, b) => a[0] - b[0])) {
|
|
441
|
+
if (mn.note)
|
|
442
|
+
combined.push({ ...mn, id: `r${chunkIdx}-n${n}` });
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
else {
|
|
446
|
+
// Pure freeform plain text fallback (no numbered list and no machine block)
|
|
447
|
+
const plainLines = humanPart
|
|
448
|
+
.split('\n')
|
|
449
|
+
.map(l => l.trim())
|
|
450
|
+
.filter(l => l && !/^Luci Review\b/i.test(l) && !/^Site:/i.test(l) && !/^```/.test(l));
|
|
451
|
+
plainLines.forEach((line, idx) => {
|
|
452
|
+
combined.push({ id: `r${chunkIdx}-n${idx + 1}`, path: '/', note: line });
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
if (combined.length === 0)
|
|
456
|
+
return null;
|
|
457
|
+
const site = machine.header?.site ?? human.site ?? 'unknown';
|
|
458
|
+
const name = sanitizeReviewerInline(machine.header?.name ?? human.name ?? 'Reviewer') || 'Reviewer';
|
|
459
|
+
const role = machine.header?.role ?? human.role ?? 'Stakeholder';
|
|
460
|
+
const createdAt = machine.header?.createdAt ?? (human.date ? `${human.date}T00:00:00.000Z` : fallbackDateIso);
|
|
461
|
+
const notesWithMeta = combined.map(n => ({
|
|
462
|
+
...n,
|
|
463
|
+
url: n.url ?? (site !== 'unknown' ? `${site.replace(/\/$/, '')}${n.path}` : undefined),
|
|
464
|
+
viewport: n.viewport ?? machine.header?.viewport,
|
|
465
|
+
browser: n.browser ?? machine.header?.browser,
|
|
466
|
+
createdAt: n.createdAt ?? createdAt
|
|
467
|
+
}));
|
|
468
|
+
const anyUnanchored = notesWithMeta.some(n => !n.anchor);
|
|
469
|
+
const degraded = !machine.hadBlock || machine.damagedLines > 0 || machine.unclosed || anyUnanchored;
|
|
470
|
+
let warning;
|
|
471
|
+
if (!machine.hadBlock) {
|
|
472
|
+
warning = 'Imported as plain text without element anchors (no luci-review data block found).';
|
|
473
|
+
}
|
|
474
|
+
else if (machine.damagedLines > 0 || machine.unclosed) {
|
|
475
|
+
warning = 'Part of the luci-review block was edited or damaged; affected notes were imported as plain text.';
|
|
476
|
+
}
|
|
477
|
+
return {
|
|
478
|
+
version: REVIEW_FORMAT_VERSION,
|
|
479
|
+
reviewer: { name, role },
|
|
480
|
+
site,
|
|
481
|
+
createdAt,
|
|
482
|
+
viewport: machine.header?.viewport,
|
|
483
|
+
browser: machine.header?.browser,
|
|
484
|
+
notes: notesWithMeta,
|
|
485
|
+
degraded: degraded || undefined,
|
|
486
|
+
warning
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
/**
|
|
490
|
+
* Parse one or more pasted reviews. Always returns whatever readable notes could be extracted,
|
|
491
|
+
* degrading gracefully to plain text without anchors when the machine block is missing or damaged.
|
|
492
|
+
*/
|
|
493
|
+
export function parseTeamReviews(rawText, nowIso = new Date().toISOString()) {
|
|
494
|
+
const normalized = normalizePastedText(rawText).trim();
|
|
495
|
+
if (!normalized)
|
|
496
|
+
return [];
|
|
497
|
+
const chunks = splitReviewChunks(normalized);
|
|
498
|
+
const results = [];
|
|
499
|
+
chunks.forEach((chunk, idx) => {
|
|
500
|
+
const parsed = parseSingleReviewChunk(chunk, idx + 1, nowIso);
|
|
501
|
+
if (parsed)
|
|
502
|
+
results.push(parsed);
|
|
503
|
+
});
|
|
504
|
+
return results;
|
|
505
|
+
}
|
|
506
|
+
const STOP_WORDS = new Set([
|
|
507
|
+
'the', 'a', 'an', 'is', 'are', 'was', 'were', 'be', 'been', 'being',
|
|
508
|
+
'this', 'that', 'these', 'those', 'it', 'its', 'in', 'on', 'at', 'to',
|
|
509
|
+
'of', 'for', 'with', 'from', 'by', 'about', 'as', 'into', 'here', 'there',
|
|
510
|
+
'and', 'or', 'but', 'if', 'so', 'not', 'no', 'very', 'too', 'more', 'less',
|
|
511
|
+
'just', 'really', 'quite', 'looks', 'look', 'feels', 'feel', 'seems', 'seem',
|
|
512
|
+
'should', 'could', 'would', 'can', 'need', 'needs', 'make', 'please', 'bit', 'little'
|
|
513
|
+
]);
|
|
514
|
+
const ELEMENT_NOUNS = new Set([
|
|
515
|
+
'button', 'btn', 'cta', 'card', 'cards', 'link', 'links', 'banner', 'hero',
|
|
516
|
+
'section', 'page', 'box', 'item', 'items', 'element', 'container', 'header',
|
|
517
|
+
'footer', 'nav', 'navbar', 'menu', 'modal', 'dialog', 'icon', 'icons', 'image',
|
|
518
|
+
'img', 'input', 'field', 'form', 'tab', 'tabs', 'badge', 'tag', 'row', 'column'
|
|
519
|
+
]);
|
|
520
|
+
const SYNONYMS = {
|
|
521
|
+
small: 'size-down', smaller: 'size-down', tiny: 'size-down', big: 'size-up', bigger: 'size-up', large: 'size-up', larger: 'size-up', size: 'size', sizing: 'size',
|
|
522
|
+
font: 'text', typeface: 'text', typography: 'text', copy: 'text', label: 'text', text: 'text', wording: 'text',
|
|
523
|
+
color: 'color', colour: 'color', contrast: 'color', dark: 'color-dark', darker: 'color-dark', light: 'color-light', lighter: 'color-light', faint: 'color-light', pale: 'color-light', readable: 'color', readability: 'color',
|
|
524
|
+
space: 'spacing', spacing: 'spacing', padding: 'spacing', margin: 'spacing', gap: 'spacing', tight: 'spacing', cramped: 'spacing', crowded: 'spacing', close: 'spacing', breathable: 'spacing',
|
|
525
|
+
radius: 'radius', corner: 'radius', corners: 'radius', round: 'radius-up', rounded: 'radius-up', sharp: 'radius-down', sharper: 'radius-down', square: 'radius-down',
|
|
526
|
+
align: 'align', aligned: 'align', alignment: 'align', center: 'align', centered: 'align', centred: 'align', offset: 'align', misaligned: 'align', crooked: 'align',
|
|
527
|
+
typo: 'typo', spelling: 'typo', misspelled: 'typo', mispelled: 'typo', grammar: 'typo',
|
|
528
|
+
bold: 'weight', bolder: 'weight', weight: 'weight', thin: 'weight', heavy: 'weight',
|
|
529
|
+
overlap: 'overlap', overlapping: 'overlap', clipped: 'overlap', cut: 'overlap', truncated: 'overlap', overflow: 'overlap'
|
|
530
|
+
};
|
|
531
|
+
function intentTokens(text) {
|
|
532
|
+
const words = text
|
|
533
|
+
.toLowerCase()
|
|
534
|
+
.replace(/[^a-z0-9\s]/g, ' ')
|
|
535
|
+
.split(/\s+/)
|
|
536
|
+
.filter(w => w.length > 1 && !STOP_WORDS.has(w));
|
|
537
|
+
const filtered = words.filter(w => !ELEMENT_NOUNS.has(w));
|
|
538
|
+
const base = filtered.length > 0 ? filtered : words;
|
|
539
|
+
return new Set(base.map(w => SYNONYMS[w] ?? w));
|
|
540
|
+
}
|
|
541
|
+
function sameElementAnchor(a, b) {
|
|
542
|
+
if (!a || !b)
|
|
543
|
+
return false;
|
|
544
|
+
if (a.selector && b.selector && a.selector.trim() === b.selector.trim())
|
|
545
|
+
return true;
|
|
546
|
+
const norm = (s) => s?.trim().toLowerCase().replace(/\s+/g, ' ') ?? '';
|
|
547
|
+
const elA = norm(a.element).split(' ')[0];
|
|
548
|
+
const elB = norm(b.element).split(' ')[0];
|
|
549
|
+
const txtA = norm(a.text);
|
|
550
|
+
const txtB = norm(b.text);
|
|
551
|
+
if (a.component && b.component && norm(a.component) === norm(b.component) && txtA && txtA === txtB)
|
|
552
|
+
return true;
|
|
553
|
+
if (elA && elA === elB && txtA && txtA === txtB)
|
|
554
|
+
return true;
|
|
555
|
+
if (elA && elA === elB && a.position && b.position) {
|
|
556
|
+
if (txtA && txtB && txtA !== txtB)
|
|
557
|
+
return false;
|
|
558
|
+
const dx = Math.abs(a.position[0] - b.position[0]);
|
|
559
|
+
const dy = Math.abs(a.position[1] - b.position[1]);
|
|
560
|
+
if (dx <= 8 && dy <= 8)
|
|
561
|
+
return true;
|
|
562
|
+
}
|
|
563
|
+
return false;
|
|
564
|
+
}
|
|
565
|
+
function sameMeaning(a, b, anchored) {
|
|
566
|
+
const cleanA = sanitizeReviewerInline(a).toLowerCase();
|
|
567
|
+
const cleanB = sanitizeReviewerInline(b).toLowerCase();
|
|
568
|
+
if (cleanA === cleanB)
|
|
569
|
+
return true;
|
|
570
|
+
const ta = intentTokens(a);
|
|
571
|
+
const tb = intentTokens(b);
|
|
572
|
+
if (ta.size === 0 || tb.size === 0)
|
|
573
|
+
return false;
|
|
574
|
+
let shared = 0;
|
|
575
|
+
for (const t of ta)
|
|
576
|
+
if (tb.has(t))
|
|
577
|
+
shared++;
|
|
578
|
+
if (shared === 0)
|
|
579
|
+
return false;
|
|
580
|
+
const union = ta.size + tb.size - shared;
|
|
581
|
+
const jaccard = shared / union;
|
|
582
|
+
const minSize = Math.min(ta.size, tb.size);
|
|
583
|
+
const containment = shared / minSize;
|
|
584
|
+
if (!anchored)
|
|
585
|
+
return jaccard >= 0.75;
|
|
586
|
+
// On the same element, avoid merging solely on generic "text" when one is about size and the other is about a typo.
|
|
587
|
+
if (shared === 1 && ta.has('text') && tb.has('text') && (ta.size > 1 || tb.size > 1))
|
|
588
|
+
return false;
|
|
589
|
+
return jaccard >= 0.45 || (containment >= 0.7 && shared >= 1);
|
|
590
|
+
}
|
|
591
|
+
function richestAnchor(a, b) {
|
|
592
|
+
if (!a)
|
|
593
|
+
return b;
|
|
594
|
+
if (!b)
|
|
595
|
+
return a;
|
|
596
|
+
return {
|
|
597
|
+
element: a.element ?? b.element,
|
|
598
|
+
text: a.text ?? b.text,
|
|
599
|
+
component: a.component ?? b.component,
|
|
600
|
+
selector: a.selector ?? b.selector,
|
|
601
|
+
position: a.position ?? b.position
|
|
602
|
+
};
|
|
603
|
+
}
|
|
604
|
+
function mergeMeasurements(a, b) {
|
|
605
|
+
if (!a)
|
|
606
|
+
return b;
|
|
607
|
+
if (!b)
|
|
608
|
+
return a;
|
|
609
|
+
return { ...b, ...a };
|
|
610
|
+
}
|
|
611
|
+
/**
|
|
612
|
+
* Merge duplicate notes across one or more reviews (same page, same element, same meaning).
|
|
613
|
+
* Preserves every reviewer's name and role for Builder display, and unique roles for agent hand-off.
|
|
614
|
+
*/
|
|
615
|
+
export function mergeReviewNotes(reviews) {
|
|
616
|
+
const groups = [];
|
|
617
|
+
reviews.forEach((review, reviewIndex) => {
|
|
618
|
+
for (const note of review.notes) {
|
|
619
|
+
const path = normalizeReviewPath(note.path);
|
|
620
|
+
const entry = {
|
|
621
|
+
reviewerName: review.reviewer.name,
|
|
622
|
+
role: review.reviewer.role,
|
|
623
|
+
text: note.note,
|
|
624
|
+
site: review.site,
|
|
625
|
+
createdAt: note.createdAt ?? review.createdAt,
|
|
626
|
+
reviewIndex
|
|
627
|
+
};
|
|
628
|
+
const existing = groups.find(g => {
|
|
629
|
+
if (g.path !== path)
|
|
630
|
+
return false;
|
|
631
|
+
const anchored = sameElementAnchor(g.anchor, note.anchor);
|
|
632
|
+
if (!anchored && (g.anchor || note.anchor))
|
|
633
|
+
return false;
|
|
634
|
+
return g.entries.some(e => sameMeaning(e.text, note.note, anchored));
|
|
635
|
+
});
|
|
636
|
+
if (existing) {
|
|
637
|
+
existing.entries.push(entry);
|
|
638
|
+
if (!existing.reviewers.some(r => r.name.toLowerCase() === review.reviewer.name.toLowerCase() && r.role === review.reviewer.role)) {
|
|
639
|
+
existing.reviewers.push({ ...review.reviewer });
|
|
640
|
+
}
|
|
641
|
+
if (!existing.roles.includes(review.reviewer.role)) {
|
|
642
|
+
existing.roles.push(review.reviewer.role);
|
|
643
|
+
}
|
|
644
|
+
existing.anchor = richestAnchor(existing.anchor, note.anchor);
|
|
645
|
+
existing.measured = mergeMeasurements(existing.measured, note.measured);
|
|
646
|
+
if (entry.createdAt > existing.createdAt)
|
|
647
|
+
existing.createdAt = entry.createdAt;
|
|
648
|
+
}
|
|
649
|
+
else {
|
|
650
|
+
groups.push({
|
|
651
|
+
id: `merged-${groups.length + 1}`,
|
|
652
|
+
path,
|
|
653
|
+
note: note.note,
|
|
654
|
+
entries: [entry],
|
|
655
|
+
reviewers: [{ ...review.reviewer }],
|
|
656
|
+
roles: [review.reviewer.role],
|
|
657
|
+
anchor: note.anchor ? { ...note.anchor } : undefined,
|
|
658
|
+
measured: note.measured ? { ...note.measured } : undefined,
|
|
659
|
+
site: review.site,
|
|
660
|
+
url: note.url,
|
|
661
|
+
createdAt: entry.createdAt,
|
|
662
|
+
viewport: note.viewport ?? review.viewport,
|
|
663
|
+
browser: note.browser ?? review.browser,
|
|
664
|
+
degraded: review.degraded
|
|
665
|
+
});
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
});
|
|
669
|
+
return groups;
|
|
670
|
+
}
|
|
671
|
+
/**
|
|
672
|
+
* Format a merged note's comment for saving to `.luci/feedback/` and handing off to an agent.
|
|
673
|
+
* Includes roles only (never personal names) and escapes untrusted reviewer text.
|
|
674
|
+
*/
|
|
675
|
+
export function formatAgentNoteComment(merged) {
|
|
676
|
+
const uniqueTexts = [];
|
|
677
|
+
for (const e of merged.entries) {
|
|
678
|
+
const clean = sanitizeReviewerInline(e.text);
|
|
679
|
+
if (clean && !uniqueTexts.some(t => t.toLowerCase() === clean.toLowerCase())) {
|
|
680
|
+
uniqueTexts.push(clean);
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
if (uniqueTexts.length <= 1)
|
|
684
|
+
return uniqueTexts[0] ?? sanitizeReviewerInline(merged.note);
|
|
685
|
+
return uniqueTexts.join(' / ');
|
|
686
|
+
}
|
|
687
|
+
//# sourceMappingURL=review.js.map
|