@dowel-ui/react 0.4.0 → 0.5.0

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.
Files changed (146) hide show
  1. package/README.md +9 -5
  2. package/dist/components/ai-extraction-review/ai-extraction-review.d.ts +52 -0
  3. package/dist/components/ai-extraction-review/ai-extraction-review.d.ts.map +1 -0
  4. package/dist/components/ai-extraction-review/ai-extraction-review.js +249 -0
  5. package/dist/components/ai-extraction-review/ai-extraction-review.js.map +1 -0
  6. package/dist/components/ai-extraction-review/extraction-model.d.ts +94 -0
  7. package/dist/components/ai-extraction-review/extraction-model.d.ts.map +1 -0
  8. package/dist/components/ai-extraction-review/extraction-model.js +91 -0
  9. package/dist/components/ai-extraction-review/extraction-model.js.map +1 -0
  10. package/dist/components/ai-extraction-review/index.d.ts +3 -0
  11. package/dist/components/ai-extraction-review/index.js +3 -0
  12. package/dist/components/ai-extraction-review/meta.js +17 -0
  13. package/dist/components/ai-extraction-review/meta.js.map +1 -0
  14. package/dist/components/ai-suggested-value/ai-suggested-value.d.ts +72 -0
  15. package/dist/components/ai-suggested-value/ai-suggested-value.d.ts.map +1 -0
  16. package/dist/components/ai-suggested-value/ai-suggested-value.js +126 -0
  17. package/dist/components/ai-suggested-value/ai-suggested-value.js.map +1 -0
  18. package/dist/components/ai-suggested-value/index.d.ts +2 -0
  19. package/dist/components/ai-suggested-value/index.js +2 -0
  20. package/dist/components/ai-suggested-value/meta.js +17 -0
  21. package/dist/components/ai-suggested-value/meta.js.map +1 -0
  22. package/dist/components/alert/alert.d.ts +1 -1
  23. package/dist/components/avatar/avatar.d.ts +1 -1
  24. package/dist/components/badge/badge.d.ts +1 -1
  25. package/dist/components/button/button.d.ts +1 -1
  26. package/dist/components/confirm-typed/confirm-typed.d.ts +47 -0
  27. package/dist/components/confirm-typed/confirm-typed.d.ts.map +1 -0
  28. package/dist/components/confirm-typed/confirm-typed.js +106 -0
  29. package/dist/components/confirm-typed/confirm-typed.js.map +1 -0
  30. package/dist/components/confirm-typed/index.d.ts +2 -0
  31. package/dist/components/confirm-typed/index.js +2 -0
  32. package/dist/components/confirm-typed/meta.js +17 -0
  33. package/dist/components/confirm-typed/meta.js.map +1 -0
  34. package/dist/components/cron-editor/cron-editor.d.ts +36 -0
  35. package/dist/components/cron-editor/cron-editor.d.ts.map +1 -0
  36. package/dist/components/cron-editor/cron-editor.js +493 -0
  37. package/dist/components/cron-editor/cron-editor.js.map +1 -0
  38. package/dist/components/cron-editor/cron-expression.d.ts +82 -0
  39. package/dist/components/cron-editor/cron-expression.d.ts.map +1 -0
  40. package/dist/components/cron-editor/cron-expression.js +352 -0
  41. package/dist/components/cron-editor/cron-expression.js.map +1 -0
  42. package/dist/components/cron-editor/index.d.ts +3 -0
  43. package/dist/components/cron-editor/index.js +3 -0
  44. package/dist/components/cron-editor/meta.js +17 -0
  45. package/dist/components/cron-editor/meta.js.map +1 -0
  46. package/dist/components/dns-record/dns-record.d.ts +61 -0
  47. package/dist/components/dns-record/dns-record.d.ts.map +1 -0
  48. package/dist/components/dns-record/dns-record.js +227 -0
  49. package/dist/components/dns-record/dns-record.js.map +1 -0
  50. package/dist/components/dns-record/index.d.ts +2 -0
  51. package/dist/components/dns-record/index.js +2 -0
  52. package/dist/components/dns-record/meta.js +17 -0
  53. package/dist/components/dns-record/meta.js.map +1 -0
  54. package/dist/components/permission-matrix/index.d.ts +3 -0
  55. package/dist/components/permission-matrix/index.js +3 -0
  56. package/dist/components/permission-matrix/meta.js +17 -0
  57. package/dist/components/permission-matrix/meta.js.map +1 -0
  58. package/dist/components/permission-matrix/permission-matrix.d.ts +46 -0
  59. package/dist/components/permission-matrix/permission-matrix.d.ts.map +1 -0
  60. package/dist/components/permission-matrix/permission-matrix.js +199 -0
  61. package/dist/components/permission-matrix/permission-matrix.js.map +1 -0
  62. package/dist/components/permission-matrix/permission-model.d.ts +53 -0
  63. package/dist/components/permission-matrix/permission-model.d.ts.map +1 -0
  64. package/dist/components/permission-matrix/permission-model.js +64 -0
  65. package/dist/components/permission-matrix/permission-model.js.map +1 -0
  66. package/dist/components/progress/progress.d.ts +1 -1
  67. package/dist/components/secret-field/index.d.ts +2 -0
  68. package/dist/components/secret-field/index.js +2 -0
  69. package/dist/components/secret-field/meta.js +17 -0
  70. package/dist/components/secret-field/meta.js.map +1 -0
  71. package/dist/components/secret-field/secret-field.d.ts +58 -0
  72. package/dist/components/secret-field/secret-field.d.ts.map +1 -0
  73. package/dist/components/secret-field/secret-field.js +168 -0
  74. package/dist/components/secret-field/secret-field.js.map +1 -0
  75. package/dist/components/session-expiry/index.d.ts +2 -0
  76. package/dist/components/session-expiry/index.js +2 -0
  77. package/dist/components/session-expiry/meta.js +17 -0
  78. package/dist/components/session-expiry/meta.js.map +1 -0
  79. package/dist/components/session-expiry/session-expiry.d.ts +64 -0
  80. package/dist/components/session-expiry/session-expiry.d.ts.map +1 -0
  81. package/dist/components/session-expiry/session-expiry.js +185 -0
  82. package/dist/components/session-expiry/session-expiry.js.map +1 -0
  83. package/dist/components/shortcut-recorder/index.d.ts +3 -0
  84. package/dist/components/shortcut-recorder/index.js +3 -0
  85. package/dist/components/shortcut-recorder/meta.js +17 -0
  86. package/dist/components/shortcut-recorder/meta.js.map +1 -0
  87. package/dist/components/shortcut-recorder/shortcut-model.d.ts +67 -0
  88. package/dist/components/shortcut-recorder/shortcut-model.d.ts.map +1 -0
  89. package/dist/components/shortcut-recorder/shortcut-model.js +168 -0
  90. package/dist/components/shortcut-recorder/shortcut-model.js.map +1 -0
  91. package/dist/components/shortcut-recorder/shortcut-recorder.d.ts +47 -0
  92. package/dist/components/shortcut-recorder/shortcut-recorder.d.ts.map +1 -0
  93. package/dist/components/shortcut-recorder/shortcut-recorder.js +212 -0
  94. package/dist/components/shortcut-recorder/shortcut-recorder.js.map +1 -0
  95. package/dist/components/spinner/spinner.d.ts +1 -1
  96. package/dist/components/sync-status/index.d.ts +2 -0
  97. package/dist/components/sync-status/index.js +2 -0
  98. package/dist/components/sync-status/meta.js +17 -0
  99. package/dist/components/sync-status/meta.js.map +1 -0
  100. package/dist/components/sync-status/sync-status.d.ts +65 -0
  101. package/dist/components/sync-status/sync-status.d.ts.map +1 -0
  102. package/dist/components/sync-status/sync-status.js +103 -0
  103. package/dist/components/sync-status/sync-status.js.map +1 -0
  104. package/dist/components/toast/toast.d.ts +1 -1
  105. package/dist/index.d.ts +25 -1
  106. package/dist/index.js +16 -2
  107. package/dist/registry/components.d.ts.map +1 -1
  108. package/dist/registry/components.js +73 -53
  109. package/dist/registry/components.js.map +1 -1
  110. package/package.json +4 -4
  111. package/src/components/ai-extraction-review/ai-extraction-review.tsx +492 -0
  112. package/src/components/ai-extraction-review/extraction-model.ts +159 -0
  113. package/src/components/ai-extraction-review/index.ts +13 -0
  114. package/src/components/ai-extraction-review/meta.ts +24 -0
  115. package/src/components/ai-suggested-value/ai-suggested-value.tsx +295 -0
  116. package/src/components/ai-suggested-value/index.ts +6 -0
  117. package/src/components/ai-suggested-value/meta.ts +24 -0
  118. package/src/components/confirm-typed/confirm-typed.tsx +191 -0
  119. package/src/components/confirm-typed/index.ts +1 -0
  120. package/src/components/confirm-typed/meta.ts +22 -0
  121. package/src/components/cron-editor/cron-editor.tsx +676 -0
  122. package/src/components/cron-editor/cron-expression.ts +552 -0
  123. package/src/components/cron-editor/index.ts +23 -0
  124. package/src/components/cron-editor/meta.ts +23 -0
  125. package/src/components/dns-record/dns-record.tsx +315 -0
  126. package/src/components/dns-record/index.ts +7 -0
  127. package/src/components/dns-record/meta.ts +21 -0
  128. package/src/components/permission-matrix/index.ts +14 -0
  129. package/src/components/permission-matrix/meta.ts +23 -0
  130. package/src/components/permission-matrix/permission-matrix.tsx +368 -0
  131. package/src/components/permission-matrix/permission-model.ts +120 -0
  132. package/src/components/secret-field/index.ts +6 -0
  133. package/src/components/secret-field/meta.ts +21 -0
  134. package/src/components/secret-field/secret-field.tsx +284 -0
  135. package/src/components/session-expiry/index.ts +9 -0
  136. package/src/components/session-expiry/meta.ts +23 -0
  137. package/src/components/session-expiry/session-expiry.tsx +321 -0
  138. package/src/components/shortcut-recorder/index.ts +19 -0
  139. package/src/components/shortcut-recorder/meta.ts +22 -0
  140. package/src/components/shortcut-recorder/shortcut-model.ts +204 -0
  141. package/src/components/shortcut-recorder/shortcut-recorder.tsx +336 -0
  142. package/src/components/sync-status/index.ts +9 -0
  143. package/src/components/sync-status/meta.ts +21 -0
  144. package/src/components/sync-status/sync-status.tsx +217 -0
  145. package/src/index.ts +10 -0
  146. package/src/registry/components.ts +20 -0
@@ -0,0 +1,492 @@
1
+ "use client";
2
+
3
+ import {
4
+ useEffect,
5
+ useId,
6
+ useMemo,
7
+ useRef,
8
+ useState,
9
+ type ComponentPropsWithRef,
10
+ type KeyboardEvent,
11
+ type ReactNode,
12
+ } from "react";
13
+
14
+ import { disabledStyles, focusRing } from "@/lib/styles";
15
+ import { cn } from "@/lib/utils";
16
+
17
+ import {
18
+ evidenceOf,
19
+ isSourced,
20
+ segmentSource,
21
+ summarizeReview,
22
+ type ExtractionField,
23
+ type FieldDecision,
24
+ type SourceRun,
25
+ } from "./extraction-model";
26
+
27
+ /**
28
+ * The check after extraction: the document on one side, what the model read
29
+ * out of it on the other, and a decision about every field.
30
+ *
31
+ * Every extraction demo shows the filled object and stops. The products that
32
+ * run extraction for a living — invoice capture, KYC onboarding, claims intake —
33
+ * all have this screen, each built from scratch, because a value that cannot
34
+ * be checked can only be trusted, and nobody who is paying an invoice trusts a
35
+ * model that far. No component library ships it.
36
+ *
37
+ * The link is the component. Each field carries where in the source it was
38
+ * read from, quoted in text under the value as well as highlighted in the
39
+ * document, so a reviewer who cannot see the highlight still has the evidence.
40
+ * And a value with no evidence is said outright — "the model supplied this
41
+ * without evidence" — because that is the case a review exists to catch, and
42
+ * the one every filled-object view renders identically to a good value.
43
+ *
44
+ * Evidence is a text offset, not a bounding box. A language model reads text,
45
+ * and a text layer with offsets is what every OCR pipeline already yields.
46
+ * Boxes over a rendered page need page rendering, zoom and geometry, and are a
47
+ * different component; this one does not pretend at them.
48
+ *
49
+ * Decisions are controlled and the component writes nothing. It reports that
50
+ * a field was accepted as proposed, corrected from what was proposed, or
51
+ * rejected — a richer record than a form's submitted values, and the one an
52
+ * audit later needs — and leaves what to do with it to the application.
53
+ */
54
+
55
+ export interface ExtractionReviewProps extends Omit<
56
+ ComponentPropsWithRef<"section">,
57
+ "children"
58
+ > {
59
+ /** What the model read. Every span indexes into it. */
60
+ source: string;
61
+ /** Names the source — "Invoice 4471.pdf, page 1". */
62
+ sourceLabel?: string;
63
+ /** What is being reviewed, as the heading. */
64
+ heading: string;
65
+ fields: ExtractionField[];
66
+ /** Per-field decisions, keyed by field name. Controlled. */
67
+ decisions?: Record<string, FieldDecision>;
68
+ /** Omit to render the review as a record rather than something to operate. */
69
+ onDecision?: (name: string, decision: FieldDecision) => void;
70
+ /** True while fields are still arriving. Missing values wait instead of reading as not found. */
71
+ streaming?: boolean;
72
+ /** Confidence below this is called low, in words. */
73
+ lowConfidenceBelow?: number;
74
+ children?: ReactNode;
75
+ }
76
+
77
+ export function ExtractionReview({
78
+ className,
79
+ source,
80
+ sourceLabel = "Source",
81
+ heading,
82
+ fields,
83
+ decisions = {},
84
+ onDecision,
85
+ streaming = false,
86
+ lowConfidenceBelow = 0.7,
87
+ children,
88
+ ...props
89
+ }: ExtractionReviewProps) {
90
+ const headingId = useId();
91
+ const [activeField, setActiveField] = useState<string | null>(null);
92
+
93
+ const runs = useMemo(() => segmentSource(source, fields), [source, fields]);
94
+ const summary = useMemo(
95
+ () => summarizeReview(source, fields, decisions),
96
+ [source, fields, decisions],
97
+ );
98
+
99
+ return (
100
+ <section
101
+ data-slot="extraction-review"
102
+ data-complete={summary.complete || undefined}
103
+ aria-labelledby={headingId}
104
+ aria-busy={streaming || undefined}
105
+ className={cn("flex flex-col gap-3", className)}
106
+ {...props}
107
+ >
108
+ <div className="flex flex-wrap items-baseline justify-between gap-2">
109
+ <h3 id={headingId} className="text-sm font-medium">
110
+ {heading}
111
+ </h3>
112
+ {/* Live from the start and short, so each decision is followed by
113
+ where the review stands rather than by silence. Evidence gaps are
114
+ counted here too: they are the reason to read carefully, and worth
115
+ knowing before starting rather than discovering on field six. */}
116
+ <p
117
+ data-slot="extraction-review-summary"
118
+ aria-live="polite"
119
+ className="text-xs text-muted-foreground"
120
+ >
121
+ {streaming
122
+ ? "Fields are still arriving"
123
+ : `${String(summary.reviewed)} of ${String(summary.total)} reviewed`}
124
+ {summary.unsourced.length > 0
125
+ ? ` · ${String(summary.unsourced.length)} without evidence`
126
+ : null}
127
+ </p>
128
+ </div>
129
+
130
+ {children}
131
+
132
+ <div className="grid gap-4 md:grid-cols-2">
133
+ <SourcePanel
134
+ label={sourceLabel}
135
+ runs={runs}
136
+ active={activeField}
137
+ decisions={decisions}
138
+ />
139
+
140
+ <ol data-slot="extraction-fields" className="m-0 flex list-none flex-col gap-3 p-0">
141
+ {fields.map((field) => (
142
+ <ReviewField
143
+ key={field.name}
144
+ field={field}
145
+ source={source}
146
+ decision={decisions[field.name]}
147
+ streaming={streaming}
148
+ lowConfidenceBelow={lowConfidenceBelow}
149
+ active={activeField === field.name}
150
+ onActivate={() => {
151
+ setActiveField(field.name);
152
+ }}
153
+ onDecision={onDecision}
154
+ />
155
+ ))}
156
+ </ol>
157
+ </div>
158
+ </section>
159
+ );
160
+ }
161
+
162
+ function SourcePanel({
163
+ label,
164
+ runs,
165
+ active,
166
+ decisions,
167
+ }: {
168
+ label: string;
169
+ runs: SourceRun[];
170
+ active: string | null;
171
+ decisions: Record<string, FieldDecision>;
172
+ }) {
173
+ const ref = useRef<HTMLDivElement>(null);
174
+
175
+ // Bring the active field's evidence into view. Focus stays where it is —
176
+ // in the field — so a keyboard user is shown the source without being
177
+ // moved into it.
178
+ useEffect(() => {
179
+ if (!active || !ref.current) return;
180
+ const mark = ref.current.querySelector(`mark[data-active]`);
181
+ mark?.scrollIntoView({ block: "nearest" });
182
+ }, [active]);
183
+
184
+ return (
185
+ <div
186
+ ref={ref}
187
+ data-slot="extraction-source"
188
+ // A scrolling box has to be focusable to be scrolled by keyboard, and a
189
+ // focusable box needs a name.
190
+ role="region"
191
+ aria-label={label}
192
+ tabIndex={0}
193
+ // Sticks beside the fields on a wide screen, so the evidence for field
194
+ // nine is still in view when the reviewer reaches it.
195
+ className={cn(
196
+ "max-h-96 overflow-auto rounded-lg border border-border bg-muted/30 p-3 md:sticky md:top-0 md:self-start",
197
+ focusRing,
198
+ )}
199
+ >
200
+ {runs.length === 0 ? (
201
+ <p className="text-xs text-muted-foreground">No source text.</p>
202
+ ) : (
203
+ <pre className="m-0 font-sans text-sm leading-relaxed break-words whitespace-pre-wrap">
204
+ {runs.map((run, index) =>
205
+ run.fields.length === 0 ? (
206
+ <span key={index}>{run.text}</span>
207
+ ) : (
208
+ // A mark element, so the evidence survives as structure rather
209
+ // than only as a tint. No label is inserted into the text: a
210
+ // document read aloud with field names spliced into it is not
211
+ // the document, and the field already quotes its evidence.
212
+ <mark
213
+ key={index}
214
+ data-fields={run.fields.join(" ")}
215
+ data-active={active !== null && run.fields.includes(active) ? "" : undefined}
216
+ className={cn(
217
+ "rounded-[2px] text-inherit",
218
+ run.fields.every((name) => decisions[name]?.kind === "rejected")
219
+ ? "bg-muted line-through decoration-muted-foreground"
220
+ : "bg-primary/15",
221
+ active !== null &&
222
+ run.fields.includes(active) &&
223
+ "bg-primary/30 ring-1 ring-primary ring-inset",
224
+ )}
225
+ >
226
+ {run.text}
227
+ </mark>
228
+ ),
229
+ )}
230
+ </pre>
231
+ )}
232
+ </div>
233
+ );
234
+ }
235
+
236
+ /** Whitespace collapsed and cut short, for quoting evidence under a field. */
237
+ function quote(text: string, limit = 80): string {
238
+ const collapsed = text.replace(/\s+/g, " ").trim();
239
+ return collapsed.length > limit ? `${collapsed.slice(0, limit - 1)}…` : collapsed;
240
+ }
241
+
242
+ function ReviewField({
243
+ field,
244
+ source,
245
+ decision,
246
+ streaming,
247
+ lowConfidenceBelow,
248
+ active,
249
+ onActivate,
250
+ onDecision,
251
+ }: {
252
+ field: ExtractionField;
253
+ source: string;
254
+ decision?: FieldDecision;
255
+ streaming: boolean;
256
+ lowConfidenceBelow: number;
257
+ active: boolean;
258
+ onActivate: () => void;
259
+ onDecision?: (name: string, decision: FieldDecision) => void;
260
+ }) {
261
+ const inputId = useId();
262
+ const evidenceId = useId();
263
+ const statusId = useId();
264
+ const confidenceId = useId();
265
+
266
+ // The reviewer's typing, kept apart from the model's value so the decision
267
+ // can say which is which.
268
+ const [draft, setDraft] = useState<string | null>(null);
269
+
270
+ const proposed = field.value ?? "";
271
+ const decided = decision && decision.kind !== "rejected" ? decision.value : undefined;
272
+ const current = draft ?? decided ?? proposed;
273
+ const edited = current !== proposed;
274
+ const changedSince = decided !== undefined && current !== decided;
275
+
276
+ const arriving = streaming && field.value === undefined;
277
+ const sourced = isSourced(source, field);
278
+ const evidence = sourced ? evidenceOf(source, field.span) : "";
279
+ const unsourced = !arriving && field.value !== undefined && !sourced;
280
+
281
+ const accept = () => {
282
+ onDecision?.(
283
+ field.name,
284
+ edited
285
+ ? { kind: "corrected", value: current, proposed }
286
+ : { kind: "accepted", value: current },
287
+ );
288
+ };
289
+
290
+ const onKeyDown = (event: KeyboardEvent<HTMLInputElement | HTMLTextAreaElement>) => {
291
+ if (event.key !== "Enter") return;
292
+ // Enter confirms an IME candidate before it means anything else. Without
293
+ // this, a reviewer typing a Japanese correction accepts a fragment of it.
294
+ if (event.nativeEvent.isComposing) return;
295
+ if (field.multiline && !(event.ctrlKey || event.metaKey)) return;
296
+ event.preventDefault();
297
+ accept();
298
+ };
299
+
300
+ const status = arriving
301
+ ? "Arriving"
302
+ : !decision
303
+ ? "Not reviewed"
304
+ : decision.kind === "rejected"
305
+ ? "Rejected"
306
+ : changedSince
307
+ ? `Changed since it was ${decision.kind}`
308
+ : decision.kind === "accepted"
309
+ ? "Accepted"
310
+ : decision.proposed
311
+ ? `Corrected from “${quote(decision.proposed, 40)}”`
312
+ : "Supplied by the reviewer";
313
+
314
+ const low = field.confidence !== undefined && field.confidence < lowConfidenceBelow;
315
+ const percent =
316
+ field.confidence === undefined
317
+ ? null
318
+ : new Intl.NumberFormat(undefined, { style: "percent", maximumFractionDigits: 0 }).format(
319
+ field.confidence,
320
+ );
321
+
322
+ const describedBy = [evidenceId, statusId, percent !== null ? confidenceId : null]
323
+ .filter(Boolean)
324
+ .join(" ");
325
+
326
+ const controlClass = cn(
327
+ "w-full rounded-md border border-input bg-background px-2 py-1.5 text-sm",
328
+ edited && "border-warning",
329
+ focusRing,
330
+ );
331
+
332
+ return (
333
+ <li
334
+ data-slot="extraction-field"
335
+ data-field={field.name}
336
+ data-state={arriving ? "arriving" : (decision?.kind ?? "undecided")}
337
+ data-active={active || undefined}
338
+ data-unsourced={unsourced || undefined}
339
+ // Focus anywhere in the field — value, either button — is the reviewer
340
+ // looking at it, so the source follows without a separate step.
341
+ onFocus={onActivate}
342
+ className={cn(
343
+ "flex flex-col gap-1 rounded-lg border p-3",
344
+ active ? "border-primary" : "border-border",
345
+ decision?.kind === "rejected" && "bg-muted/40",
346
+ )}
347
+ >
348
+ <div className="flex flex-wrap items-baseline justify-between gap-2">
349
+ {onDecision && !arriving ? (
350
+ <label htmlFor={inputId} className="text-xs font-medium text-muted-foreground">
351
+ {field.label}
352
+ </label>
353
+ ) : (
354
+ <span className="text-xs font-medium text-muted-foreground">{field.label}</span>
355
+ )}
356
+ {/* Status as a word, outside the label so editing never renames the
357
+ control under a screen reader user. */}
358
+ <span
359
+ id={statusId}
360
+ data-slot="extraction-field-status"
361
+ className={cn(
362
+ "text-xs",
363
+ decision?.kind === "rejected" ? "text-destructive" : "text-muted-foreground",
364
+ changedSince && "text-warning",
365
+ )}
366
+ >
367
+ {status}
368
+ </span>
369
+ </div>
370
+
371
+ {arriving ? (
372
+ <span
373
+ aria-hidden="true"
374
+ className="block h-8 w-full animate-pulse-soft rounded-md bg-muted"
375
+ />
376
+ ) : !onDecision ? (
377
+ <p className="rounded-md bg-muted px-2 py-1.5 text-sm break-words">
378
+ {current || <span className="text-muted-foreground">Empty</span>}
379
+ </p>
380
+ ) : field.multiline ? (
381
+ <textarea
382
+ id={inputId}
383
+ rows={3}
384
+ value={current}
385
+ aria-describedby={describedBy}
386
+ onChange={(event) => {
387
+ setDraft(event.target.value);
388
+ }}
389
+ onKeyDown={onKeyDown}
390
+ className={cn(controlClass, "resize-none")}
391
+ />
392
+ ) : (
393
+ <input
394
+ id={inputId}
395
+ type="text"
396
+ value={current}
397
+ aria-describedby={describedBy}
398
+ onChange={(event) => {
399
+ setDraft(event.target.value);
400
+ }}
401
+ onKeyDown={onKeyDown}
402
+ className={controlClass}
403
+ />
404
+ )}
405
+
406
+ {/* The evidence in text. This is the accessible path to the source, and
407
+ for a sighted reviewer it is the comparison itself — "1 March 2026"
408
+ beside "2026-03-01" is a normalisation, not an error, and only reads
409
+ that way with both in view. */}
410
+ <p
411
+ id={evidenceId}
412
+ data-slot="extraction-field-evidence"
413
+ className={cn("text-xs", unsourced ? "text-warning" : "text-muted-foreground")}
414
+ >
415
+ {arriving
416
+ ? "Waiting for the model"
417
+ : sourced
418
+ ? `In the source: “${quote(evidence)}”`
419
+ : field.value === undefined
420
+ ? "The model found nothing for this field."
421
+ : "Not in the source — the model supplied this without evidence."}
422
+ </p>
423
+
424
+ {percent !== null ? (
425
+ <p
426
+ id={confidenceId}
427
+ data-slot="extraction-field-confidence"
428
+ data-low={low || undefined}
429
+ className={cn("text-xs", low ? "text-warning" : "text-muted-foreground")}
430
+ >
431
+ {low ? `Low confidence, ${percent} — worth checking` : `${percent} confidence`}
432
+ </p>
433
+ ) : null}
434
+
435
+ {onDecision && !arriving ? (
436
+ <div className="mt-1 flex flex-wrap items-center gap-2">
437
+ <FieldButton
438
+ variant="primary"
439
+ aria-label={`${edited ? "Accept correction" : "Accept"} — ${field.label}`}
440
+ aria-pressed={
441
+ decision !== undefined && decision.kind !== "rejected" && !changedSince
442
+ }
443
+ onClick={accept}
444
+ >
445
+ {edited ? "Accept correction" : "Accept"}
446
+ </FieldButton>
447
+ <FieldButton
448
+ aria-label={`Reject — ${field.label}`}
449
+ aria-pressed={decision?.kind === "rejected"}
450
+ onClick={() => {
451
+ onDecision(field.name, { kind: "rejected" });
452
+ }}
453
+ >
454
+ Reject
455
+ </FieldButton>
456
+ {sourced ? (
457
+ <FieldButton
458
+ aria-label={`Show in source — ${field.label}`}
459
+ className="ml-auto"
460
+ onClick={onActivate}
461
+ >
462
+ Show in source
463
+ </FieldButton>
464
+ ) : null}
465
+ </div>
466
+ ) : null}
467
+ </li>
468
+ );
469
+ }
470
+
471
+ function FieldButton({
472
+ className,
473
+ variant = "default",
474
+ ...props
475
+ }: ComponentPropsWithRef<"button"> & { variant?: "default" | "primary" }) {
476
+ return (
477
+ <button
478
+ type="button"
479
+ className={cn(
480
+ "rounded-md border px-2.5 py-1 text-xs font-medium transition-colors",
481
+ variant === "primary" &&
482
+ "border-primary bg-primary text-primary-foreground hover:bg-primary-hover aria-pressed:bg-primary-active",
483
+ variant === "default" &&
484
+ "border-input bg-background hover:bg-accent hover:text-accent-foreground aria-pressed:bg-secondary aria-pressed:text-secondary-foreground",
485
+ focusRing,
486
+ disabledStyles,
487
+ className,
488
+ )}
489
+ {...props}
490
+ />
491
+ );
492
+ }
@@ -0,0 +1,159 @@
1
+ /**
2
+ * The model behind a review of extracted fields: where each value was read
3
+ * from, what the reviewer decided about it, and how far through the review
4
+ * they are.
5
+ *
6
+ * Pure, so overlap handling and the completion rule are tested without
7
+ * rendering anything — and so an application can answer "is this review
8
+ * finished" on the server from the same decisions the component reports,
9
+ * rather than trusting a flag the client sent.
10
+ */
11
+
12
+ export interface SourceSpan {
13
+ /** Offset into the source text in UTF-16 code units. Inclusive. */
14
+ start: number;
15
+ /** Exclusive. */
16
+ end: number;
17
+ }
18
+
19
+ export interface ExtractionField {
20
+ /** Key in the extracted object. */
21
+ name: string;
22
+ label: string;
23
+ /**
24
+ * What the model extracted. Absent while streaming means it has not arrived;
25
+ * absent once streaming has ended means the model found nothing.
26
+ */
27
+ value?: string;
28
+ /**
29
+ * Where in the source the value was read from. Absent means the model
30
+ * supplied the value without evidence, which the review says outright.
31
+ */
32
+ span?: SourceSpan;
33
+ /** 0 to 1. Shown as a number, never only as a colour. */
34
+ confidence?: number;
35
+ /** A textarea instead of a single line. For addresses and descriptions. */
36
+ multiline?: boolean;
37
+ }
38
+
39
+ export type FieldDecision =
40
+ /** The model's value, taken as it was. */
41
+ | { kind: "accepted"; value: string }
42
+ /** The reviewer's value, with the model's kept so a record can tell them apart. */
43
+ | { kind: "corrected"; value: string; proposed: string }
44
+ /** Wrong and not corrected — the field does not apply, or cannot be known. */
45
+ | { kind: "rejected" };
46
+
47
+ /** A run of source text and the fields whose evidence covers it. */
48
+ export interface SourceRun {
49
+ text: string;
50
+ /** Field names, in declaration order. Empty for text nobody cites. */
51
+ fields: string[];
52
+ }
53
+
54
+ /**
55
+ * A span brought within the source, or null if nothing of it is.
56
+ *
57
+ * Offsets come from a model or an OCR layer and are wrong often enough that
58
+ * refusing to render on a bad one would leave the whole review blank. A span
59
+ * past the end is clamped; an inverted or empty one is treated as no evidence,
60
+ * which is what it is.
61
+ */
62
+ export function clampSpan(span: SourceSpan, length: number): SourceSpan | null {
63
+ const start = Math.max(0, Math.min(span.start, length));
64
+ const end = Math.max(0, Math.min(span.end, length));
65
+ return end > start ? { start, end } : null;
66
+ }
67
+
68
+ /** The text a span points at, or an empty string if it points at nothing. */
69
+ export function evidenceOf(source: string, span: SourceSpan | undefined): string {
70
+ if (!span) return "";
71
+ const clamped = clampSpan(span, source.length);
72
+ return clamped ? source.slice(clamped.start, clamped.end) : "";
73
+ }
74
+
75
+ /** Whether a field's value can be checked against the source at all. */
76
+ export function isSourced(source: string, field: ExtractionField): boolean {
77
+ return field.span !== undefined && clampSpan(field.span, source.length) !== null;
78
+ }
79
+
80
+ /**
81
+ * Cuts the source into runs at every span boundary, so overlapping evidence —
82
+ * a total that sits inside the line that contains it — renders as nested
83
+ * coverage rather than as two marks fighting over the same characters.
84
+ */
85
+ export function segmentSource(source: string, fields: ExtractionField[]): SourceRun[] {
86
+ const spans = fields.flatMap((field) => {
87
+ const clamped = field.span ? clampSpan(field.span, source.length) : null;
88
+ return clamped ? [{ name: field.name, ...clamped }] : [];
89
+ });
90
+
91
+ if (source.length === 0) return [];
92
+ if (spans.length === 0) return [{ text: source, fields: [] }];
93
+
94
+ const cuts = new Set<number>([0, source.length]);
95
+ for (const span of spans) {
96
+ cuts.add(span.start);
97
+ cuts.add(span.end);
98
+ }
99
+ const points = [...cuts].sort((a, b) => a - b);
100
+
101
+ const runs: SourceRun[] = [];
102
+ for (let index = 0; index < points.length - 1; index += 1) {
103
+ const start = points[index] ?? 0;
104
+ const end = points[index + 1] ?? start;
105
+ if (end <= start) continue;
106
+ runs.push({
107
+ text: source.slice(start, end),
108
+ fields: spans.filter((span) => span.start <= start && span.end >= end).map((s) => s.name),
109
+ });
110
+ }
111
+ return runs;
112
+ }
113
+
114
+ export interface ReviewSummary {
115
+ total: number;
116
+ reviewed: number;
117
+ accepted: number;
118
+ corrected: number;
119
+ rejected: number;
120
+ /** Fields with a value the model could not point to in the source. */
121
+ unsourced: string[];
122
+ /** Fields still awaiting a decision. */
123
+ remaining: string[];
124
+ /** Every field has a decision. The only completion signal worth trusting. */
125
+ complete: boolean;
126
+ }
127
+
128
+ export function summarizeReview(
129
+ source: string,
130
+ fields: ExtractionField[],
131
+ decisions: Record<string, FieldDecision> = {},
132
+ ): ReviewSummary {
133
+ const summary: ReviewSummary = {
134
+ total: fields.length,
135
+ reviewed: 0,
136
+ accepted: 0,
137
+ corrected: 0,
138
+ rejected: 0,
139
+ unsourced: [],
140
+ remaining: [],
141
+ complete: false,
142
+ };
143
+
144
+ for (const field of fields) {
145
+ if (field.value !== undefined && !isSourced(source, field)) {
146
+ summary.unsourced.push(field.name);
147
+ }
148
+ const decision = decisions[field.name];
149
+ if (!decision) {
150
+ summary.remaining.push(field.name);
151
+ continue;
152
+ }
153
+ summary.reviewed += 1;
154
+ summary[decision.kind] += 1;
155
+ }
156
+
157
+ summary.complete = fields.length > 0 && summary.remaining.length === 0;
158
+ return summary;
159
+ }
@@ -0,0 +1,13 @@
1
+ export { ExtractionReview, type ExtractionReviewProps } from "./ai-extraction-review";
2
+ export {
3
+ clampSpan,
4
+ evidenceOf,
5
+ isSourced,
6
+ segmentSource,
7
+ summarizeReview,
8
+ type ExtractionField,
9
+ type FieldDecision,
10
+ type ReviewSummary,
11
+ type SourceRun,
12
+ type SourceSpan,
13
+ } from "./extraction-model";
@@ -0,0 +1,24 @@
1
+ import { defineMeta } from "@/registry/schema";
2
+
3
+ export const meta = defineMeta({
4
+ name: "ai-extraction-review",
5
+ title: "AI Extraction Review",
6
+ description:
7
+ "Check extracted fields against the document they came from, and decide each one.",
8
+ category: "ai",
9
+ status: "stable",
10
+ dependencies: [],
11
+ registryDependencies: [],
12
+ files: ["extraction-model.ts", "ai-extraction-review.tsx"],
13
+ a11y:
14
+ "Every field quotes its evidence in text under the value, so a reviewer who cannot see the " +
15
+ "highlight in the source still has what the model read. A value with no evidence is said in " +
16
+ "words — never left to look like a good one. The source is a focusable named region because " +
17
+ "it scrolls, and its highlights are mark elements with nothing spliced into the text, since a " +
18
+ "document read aloud with field names inserted is no longer the document. Focus anywhere in a " +
19
+ "field brings its evidence into view without moving focus, so a keyboard user is shown the " +
20
+ "source rather than sent into it. Each control's name carries the field it belongs to, so " +
21
+ "ten Accept buttons are ten different buttons. Status is a word, outside the label so editing " +
22
+ "never renames the control, and the running count is a polite live region present from the " +
23
+ "start. Enter accepts, except while an IME is composing.",
24
+ });