@crowandraven/evalkeeper 0.16.0 → 0.17.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.
- package/dist/react/AnswerKey.js +46 -15
- package/dist/react/AnswerKey.js.map +1 -1
- package/dist/react/ConfirmAnswer.d.ts +25 -4
- package/dist/react/ConfirmAnswer.js +66 -13
- package/dist/react/ConfirmAnswer.js.map +1 -1
- package/dist/react/Fields.d.ts +8 -2
- package/dist/react/Fields.js +22 -6
- package/dist/react/Fields.js.map +1 -1
- package/dist/react/Folded.d.ts +13 -0
- package/dist/react/Folded.js +15 -0
- package/dist/react/Folded.js.map +1 -0
- package/dist/react/Readable.d.ts +39 -0
- package/dist/react/Readable.js +136 -0
- package/dist/react/Readable.js.map +1 -0
- package/dist/react/pieces.d.ts +10 -0
- package/dist/react/pieces.js +19 -6
- package/dist/react/pieces.js.map +1 -1
- package/dist/types.d.ts +22 -0
- package/dist/types.js.map +1 -1
- package/package.json +4 -4
- package/src/react/AnswerKey.tsx +237 -93
- package/src/react/ConfirmAnswer.tsx +120 -30
- package/src/react/Fields.tsx +28 -20
- package/src/react/Folded.tsx +38 -0
- package/src/react/Readable.tsx +266 -0
- package/src/react/pieces.tsx +46 -6
- package/src/types.ts +23 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useState, type ReactNode } from "react";
|
|
4
|
+
import { Button } from "./pieces.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Long content, folded: `short` shows until the person opens the rest.
|
|
8
|
+
* For a posting's text or a long list, so a case reads at a glance.
|
|
9
|
+
*/
|
|
10
|
+
export function Folded({
|
|
11
|
+
short,
|
|
12
|
+
children,
|
|
13
|
+
more,
|
|
14
|
+
}: {
|
|
15
|
+
/** What shows while folded. */
|
|
16
|
+
short: ReactNode;
|
|
17
|
+
/** The whole of it. */
|
|
18
|
+
children: ReactNode;
|
|
19
|
+
/** The button's label: "Show all 24" or "Show the whole text". */
|
|
20
|
+
more: string;
|
|
21
|
+
}) {
|
|
22
|
+
const [open, setOpen] = useState(false);
|
|
23
|
+
return (
|
|
24
|
+
<>
|
|
25
|
+
{open ? children : short}
|
|
26
|
+
<Button
|
|
27
|
+
type="button"
|
|
28
|
+
aria-expanded={open}
|
|
29
|
+
onClick={() => {
|
|
30
|
+
setOpen(!open);
|
|
31
|
+
}}
|
|
32
|
+
className="mt-1 text-xs text-accent hover:underline"
|
|
33
|
+
>
|
|
34
|
+
{open ? "Show less" : more}
|
|
35
|
+
</Button>
|
|
36
|
+
</>
|
|
37
|
+
);
|
|
38
|
+
}
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import { asText } from "../index.js";
|
|
3
|
+
import { Folded } from "./Folded.js";
|
|
4
|
+
|
|
5
|
+
/** Longer than this, text is folded to its start. */
|
|
6
|
+
const LONG_TEXT = 320;
|
|
7
|
+
/** More items than this, a list is folded to its first ones. */
|
|
8
|
+
const LONG_LIST = 6;
|
|
9
|
+
/** Deeper than this, a value shows as data. */
|
|
10
|
+
const MAX_DEPTH = 3;
|
|
11
|
+
|
|
12
|
+
/** "linkStatus" or "link_status" → "Link status". */
|
|
13
|
+
export function humanize(key: string): string {
|
|
14
|
+
const words = key
|
|
15
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
16
|
+
.replace(/[_-]+/g, " ")
|
|
17
|
+
.trim()
|
|
18
|
+
.toLowerCase();
|
|
19
|
+
return words.charAt(0).toUpperCase() + words.slice(1);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** `text` with each of `marks` highlighted, ignoring case. */
|
|
23
|
+
export function Marked({
|
|
24
|
+
text,
|
|
25
|
+
marks = [],
|
|
26
|
+
}: {
|
|
27
|
+
text: string;
|
|
28
|
+
marks?: string[] | undefined;
|
|
29
|
+
}): ReactNode {
|
|
30
|
+
const terms = marks.map((m) => m.trim()).filter(Boolean);
|
|
31
|
+
if (!terms.length) return text;
|
|
32
|
+
const escaped = terms
|
|
33
|
+
.sort((a, b) => b.length - a.length)
|
|
34
|
+
.map((m) => m.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
|
|
35
|
+
const parts = text.split(new RegExp(`(${escaped.join("|")})`, "gi"));
|
|
36
|
+
if (parts.length === 1) return text;
|
|
37
|
+
const lower = new Set(terms.map((t) => t.toLowerCase()));
|
|
38
|
+
return parts.map((p, i) =>
|
|
39
|
+
lower.has(p.toLowerCase()) ? (
|
|
40
|
+
<mark key={i} className="rounded-sm bg-warn/20 px-0.5 text-ink">
|
|
41
|
+
{p}
|
|
42
|
+
</mark>
|
|
43
|
+
) : (
|
|
44
|
+
p
|
|
45
|
+
),
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** "VERIFIED" → "Verified", "NOT_SURE" → "Not sure"; other text as is. */
|
|
50
|
+
export function enumText(text: string): string {
|
|
51
|
+
if (!/^[A-Z][A-Z0-9]*(?:_[A-Z0-9]+)*$/.test(text)) return text;
|
|
52
|
+
const words = text.replace(/_/g, " ").toLowerCase();
|
|
53
|
+
return words.charAt(0).toUpperCase() + words.slice(1);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Whether `value` contains any of `marks` anywhere, ignoring case. */
|
|
57
|
+
export function hasMark(value: unknown, marks: string[] = []): boolean {
|
|
58
|
+
if (!marks.length) return false;
|
|
59
|
+
const text = (typeof value === "string" ? value : asText(value))
|
|
60
|
+
.toLowerCase()
|
|
61
|
+
.replace(/\\n/g, "\n");
|
|
62
|
+
return marks.some((m) => m.trim() && text.includes(m.trim().toLowerCase()));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
type Block =
|
|
66
|
+
| { kind: "heading"; text: string }
|
|
67
|
+
| { kind: "list"; items: string[] }
|
|
68
|
+
| { kind: "text"; text: string };
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Text as blocks a person reads: Markdown-style headings ("### Wants")
|
|
72
|
+
* and lists ("- item") as such, paragraphs at blank lines, other line
|
|
73
|
+
* breaks kept.
|
|
74
|
+
*/
|
|
75
|
+
export function blocksOf(text: string): Block[] {
|
|
76
|
+
const out: Block[] = [];
|
|
77
|
+
let para: string[] = [];
|
|
78
|
+
const endPara = () => {
|
|
79
|
+
const t = para.join("\n").trim();
|
|
80
|
+
if (t) out.push({ kind: "text", text: t });
|
|
81
|
+
para = [];
|
|
82
|
+
};
|
|
83
|
+
for (const raw of text.split("\n")) {
|
|
84
|
+
const line = raw.trimEnd();
|
|
85
|
+
const heading = /^#{1,6}\s+(.+)$/.exec(line);
|
|
86
|
+
const item = /^\s*[-*•]\s+(.+)$/.exec(line);
|
|
87
|
+
if (heading?.[1]) {
|
|
88
|
+
endPara();
|
|
89
|
+
out.push({ kind: "heading", text: heading[1] });
|
|
90
|
+
} else if (item?.[1]) {
|
|
91
|
+
endPara();
|
|
92
|
+
const last = out.at(-1);
|
|
93
|
+
if (last?.kind === "list") last.items.push(item[1]);
|
|
94
|
+
else out.push({ kind: "list", items: [item[1]] });
|
|
95
|
+
} else if (!line.trim()) endPara();
|
|
96
|
+
else para.push(line);
|
|
97
|
+
}
|
|
98
|
+
endPara();
|
|
99
|
+
return out;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Text in blocks: headings, lists, and paragraphs, with marks. */
|
|
103
|
+
function Paragraphs({
|
|
104
|
+
text,
|
|
105
|
+
marks,
|
|
106
|
+
}: {
|
|
107
|
+
text: string;
|
|
108
|
+
marks?: string[] | undefined;
|
|
109
|
+
}) {
|
|
110
|
+
return (
|
|
111
|
+
<div className="readable space-y-2">
|
|
112
|
+
{blocksOf(text).map((b, i) =>
|
|
113
|
+
b.kind === "heading" ? (
|
|
114
|
+
<p key={i} className="text-xs font-semibold text-muted">
|
|
115
|
+
<Marked text={b.text} marks={marks} />
|
|
116
|
+
</p>
|
|
117
|
+
) : b.kind === "list" ? (
|
|
118
|
+
<ul key={i} className="list-disc space-y-1 pl-5">
|
|
119
|
+
{b.items.map((t, j) => (
|
|
120
|
+
<li key={j}>
|
|
121
|
+
<Marked text={t} marks={marks} />
|
|
122
|
+
</li>
|
|
123
|
+
))}
|
|
124
|
+
</ul>
|
|
125
|
+
) : (
|
|
126
|
+
<p key={i} className="whitespace-pre-wrap">
|
|
127
|
+
<Marked text={b.text} marks={marks} />
|
|
128
|
+
</p>
|
|
129
|
+
),
|
|
130
|
+
)}
|
|
131
|
+
</div>
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Text as a person reads it: short text inline, long text as paragraphs
|
|
137
|
+
* folded to its start, unless a highlight sits in the rest.
|
|
138
|
+
*/
|
|
139
|
+
function ReadableText({
|
|
140
|
+
text,
|
|
141
|
+
marks,
|
|
142
|
+
}: {
|
|
143
|
+
text: string;
|
|
144
|
+
marks?: string[] | undefined;
|
|
145
|
+
}) {
|
|
146
|
+
if (/^https?:\/\/\S+$/.test(text))
|
|
147
|
+
return (
|
|
148
|
+
<a
|
|
149
|
+
href={text}
|
|
150
|
+
target="_blank"
|
|
151
|
+
rel="noreferrer"
|
|
152
|
+
className="break-all text-accent hover:underline"
|
|
153
|
+
>
|
|
154
|
+
<Marked text={text} marks={marks} />
|
|
155
|
+
</a>
|
|
156
|
+
);
|
|
157
|
+
if (text.length <= LONG_TEXT && !text.includes("\n"))
|
|
158
|
+
return <Marked text={enumText(text)} marks={marks} />;
|
|
159
|
+
const whole = (
|
|
160
|
+
<div className="rounded-md bg-tint px-3 py-2">
|
|
161
|
+
<Paragraphs text={text} marks={marks} />
|
|
162
|
+
</div>
|
|
163
|
+
);
|
|
164
|
+
if (text.length <= LONG_TEXT) return whole;
|
|
165
|
+
const cut = text.slice(0, LONG_TEXT).replace(/\s+\S*$/, "");
|
|
166
|
+
const markedLater = hasMark(text, marks) && !hasMark(cut, marks);
|
|
167
|
+
return (
|
|
168
|
+
<div>
|
|
169
|
+
<Folded
|
|
170
|
+
more="Show the whole text"
|
|
171
|
+
short={
|
|
172
|
+
<div className="rounded-md bg-tint px-3 py-2">
|
|
173
|
+
<Paragraphs text={`${cut}…`} marks={marks} />
|
|
174
|
+
{markedLater && (
|
|
175
|
+
<p className="mt-1 text-xs text-muted">
|
|
176
|
+
More highlighted further on.
|
|
177
|
+
</p>
|
|
178
|
+
)}
|
|
179
|
+
</div>
|
|
180
|
+
}
|
|
181
|
+
>
|
|
182
|
+
{whole}
|
|
183
|
+
</Folded>
|
|
184
|
+
</div>
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* A stored value, read like a page rather than data: an object as
|
|
190
|
+
* labeled rows, a list as a list (folded when long), text as text, with
|
|
191
|
+
* `marks` highlighted.
|
|
192
|
+
*/
|
|
193
|
+
export function ReadableValue({
|
|
194
|
+
value,
|
|
195
|
+
marks,
|
|
196
|
+
depth = 0,
|
|
197
|
+
}: {
|
|
198
|
+
value: unknown;
|
|
199
|
+
marks?: string[] | undefined;
|
|
200
|
+
depth?: number;
|
|
201
|
+
}): ReactNode {
|
|
202
|
+
if (value === undefined || value === null || value === "")
|
|
203
|
+
return <span className="text-muted">—</span>;
|
|
204
|
+
if (typeof value === "string")
|
|
205
|
+
return <ReadableText text={value} marks={marks} />;
|
|
206
|
+
if (typeof value === "number" || typeof value === "boolean")
|
|
207
|
+
return String(value);
|
|
208
|
+
if (depth >= MAX_DEPTH)
|
|
209
|
+
return (
|
|
210
|
+
<pre className="whitespace-pre-wrap text-xs">
|
|
211
|
+
{JSON.stringify(value, null, 2)}
|
|
212
|
+
</pre>
|
|
213
|
+
);
|
|
214
|
+
if (Array.isArray(value)) {
|
|
215
|
+
if (!value.length) return <span className="text-muted">None</span>;
|
|
216
|
+
// Records (résumés, facts with fields): each its own tinted block.
|
|
217
|
+
const records = value.every(
|
|
218
|
+
(x) => x !== null && typeof x === "object" && !Array.isArray(x),
|
|
219
|
+
);
|
|
220
|
+
const items = (xs: unknown[]) =>
|
|
221
|
+
records ? (
|
|
222
|
+
<ul className="space-y-2">
|
|
223
|
+
{xs.map((x, i) => (
|
|
224
|
+
<li key={i} className="rounded-md bg-tint px-3 py-2">
|
|
225
|
+
<ReadableValue value={x} marks={marks} depth={depth + 1} />
|
|
226
|
+
</li>
|
|
227
|
+
))}
|
|
228
|
+
</ul>
|
|
229
|
+
) : (
|
|
230
|
+
<ul className="list-disc space-y-1 pl-5">
|
|
231
|
+
{xs.map((x, i) => (
|
|
232
|
+
<li key={i}>
|
|
233
|
+
<ReadableValue value={x} marks={marks} depth={depth + 1} />
|
|
234
|
+
</li>
|
|
235
|
+
))}
|
|
236
|
+
</ul>
|
|
237
|
+
);
|
|
238
|
+
if (value.length <= LONG_LIST) return items(value);
|
|
239
|
+
return (
|
|
240
|
+
<div>
|
|
241
|
+
<Folded
|
|
242
|
+
more={`Show all ${value.length}`}
|
|
243
|
+
short={items(value.slice(0, LONG_LIST - 1))}
|
|
244
|
+
>
|
|
245
|
+
{items(value)}
|
|
246
|
+
</Folded>
|
|
247
|
+
</div>
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
const entries = Object.entries(value as Record<string, unknown>).filter(
|
|
251
|
+
([, v]) => v !== undefined,
|
|
252
|
+
);
|
|
253
|
+
if (!entries.length) return <span className="text-muted">—</span>;
|
|
254
|
+
return (
|
|
255
|
+
<dl className="grid grid-cols-[minmax(6rem,auto)_minmax(0,1fr)] gap-x-3 gap-y-1.5">
|
|
256
|
+
{entries.map(([k, v]) => (
|
|
257
|
+
<div key={k} className="contents">
|
|
258
|
+
<dt className="text-xs text-muted">{humanize(k)}</dt>
|
|
259
|
+
<dd className="min-w-0 break-words">
|
|
260
|
+
<ReadableValue value={v} marks={marks} depth={depth + 1} />
|
|
261
|
+
</dd>
|
|
262
|
+
</div>
|
|
263
|
+
))}
|
|
264
|
+
</dl>
|
|
265
|
+
);
|
|
266
|
+
}
|
package/src/react/pieces.tsx
CHANGED
|
@@ -9,7 +9,7 @@ import {
|
|
|
9
9
|
type ReactNode,
|
|
10
10
|
} from "react";
|
|
11
11
|
import { useFormStatus } from "react-dom";
|
|
12
|
-
import { ArrowLeft, Info, LoaderCircle } from "lucide-react";
|
|
12
|
+
import { ArrowLeft, CircleAlert, Info, LoaderCircle } from "lucide-react";
|
|
13
13
|
import { GLOSSARY, type GlossaryTerm } from "../glossary.js";
|
|
14
14
|
import {
|
|
15
15
|
STANDING_LABEL,
|
|
@@ -120,8 +120,22 @@ export type EvalComponents = {
|
|
|
120
120
|
TextArea: ComponentType<TextAreaProps>;
|
|
121
121
|
Select: ComponentType<SelectProps>;
|
|
122
122
|
RefreshWhile: ComponentType<RefreshWhileProps>;
|
|
123
|
+
/**
|
|
124
|
+
* Your app's toast, for what happened after a quick action (an answer
|
|
125
|
+
* confirmed, or a save that failed). Without it, the screens say so in
|
|
126
|
+
* place, and say nothing on success.
|
|
127
|
+
*/
|
|
128
|
+
notify: Notify;
|
|
123
129
|
};
|
|
124
130
|
|
|
131
|
+
/** Shows a short message as your app does (`EvalComponents.notify`). */
|
|
132
|
+
export type Notify = (type: "success" | "error", message: string) => void;
|
|
133
|
+
|
|
134
|
+
/** Your app's toast, if it gave one (`EvalComponents.notify`). */
|
|
135
|
+
export function useNotify(): Notify | undefined {
|
|
136
|
+
return useContext(Swapped).notify;
|
|
137
|
+
}
|
|
138
|
+
|
|
125
139
|
const Swapped = createContext<Partial<EvalComponents>>({});
|
|
126
140
|
|
|
127
141
|
/**
|
|
@@ -271,12 +285,38 @@ export function StandingBadge({ standing }: { standing: HealthStanding }) {
|
|
|
271
285
|
* footnote instead.
|
|
272
286
|
*/
|
|
273
287
|
export function HealthReasons({ flags }: { flags: readonly HealthFlag[] }) {
|
|
274
|
-
const
|
|
288
|
+
const reasons = flags
|
|
275
289
|
.filter((f) => f.kind !== "provisional" && f.kind !== "good")
|
|
276
|
-
.flatMap(flagChips)
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
290
|
+
.flatMap(flagChips);
|
|
291
|
+
if (!reasons.length) return null;
|
|
292
|
+
// Each in its standing's color with an icon, so it reads at a glance
|
|
293
|
+
// without another row of chips.
|
|
294
|
+
return (
|
|
295
|
+
<ul className="flex flex-wrap gap-x-4 gap-y-1 text-sm font-medium">
|
|
296
|
+
{reasons.map((r) => {
|
|
297
|
+
const urgent = r.tone === "serious" || r.tone === "warn";
|
|
298
|
+
return (
|
|
299
|
+
<li
|
|
300
|
+
key={r.text}
|
|
301
|
+
className={`inline-flex items-center gap-1.5 ${
|
|
302
|
+
r.tone === "serious"
|
|
303
|
+
? "text-bad"
|
|
304
|
+
: r.tone === "warn"
|
|
305
|
+
? "text-warn"
|
|
306
|
+
: "text-ink"
|
|
307
|
+
}`}
|
|
308
|
+
>
|
|
309
|
+
{urgent ? (
|
|
310
|
+
<CircleAlert aria-hidden className="h-4 w-4 shrink-0" />
|
|
311
|
+
) : (
|
|
312
|
+
<Info aria-hidden className="h-4 w-4 shrink-0 text-muted" />
|
|
313
|
+
)}
|
|
314
|
+
{r.text}
|
|
315
|
+
</li>
|
|
316
|
+
);
|
|
317
|
+
})}
|
|
318
|
+
</ul>
|
|
319
|
+
);
|
|
280
320
|
}
|
|
281
321
|
|
|
282
322
|
/**
|
package/src/types.ts
CHANGED
|
@@ -286,6 +286,27 @@ export type Suite<Input, Output, Label> = {
|
|
|
286
286
|
* the model, so it doesn't change the instructions hash.
|
|
287
287
|
*/
|
|
288
288
|
answerGuide?: AnswerGuide;
|
|
289
|
+
/**
|
|
290
|
+
* What a person should look at to check this case's answer: shown with
|
|
291
|
+
* the answer to confirm ("Look at: pay is below their floor"), each
|
|
292
|
+
* note's `marks` highlighted wherever they appear in the input. Pick
|
|
293
|
+
* them by rule from the case (a dealbreaker beside what it rules on),
|
|
294
|
+
* never from the draft answer's own reasoning: that steers a person to
|
|
295
|
+
* agree with it. When models' tests disagree, the answer key says so on
|
|
296
|
+
* its own. For people only: it never reaches the model.
|
|
297
|
+
*
|
|
298
|
+
* @param input - The case's input, already checked against `input`.
|
|
299
|
+
* @param answer - The answer being checked, or null without one.
|
|
300
|
+
*/
|
|
301
|
+
lookAt?(input: Input, answer: Label | null): LookAt[];
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
/** One thing to look at when checking an answer (`Suite.lookAt`). */
|
|
305
|
+
export type LookAt = {
|
|
306
|
+
/** A short note: "pay is below their $70K floor". */
|
|
307
|
+
note: string;
|
|
308
|
+
/** Text to highlight in the case's input: "$55,000–$61,000". */
|
|
309
|
+
marks?: string[];
|
|
289
310
|
};
|
|
290
311
|
|
|
291
312
|
/** How to judge a suite's answers: a lead-in, the points, and a note. */
|
|
@@ -333,6 +354,8 @@ export type FormField = {
|
|
|
333
354
|
kind: "text" | "textarea" | "select" | "lines" | "json";
|
|
334
355
|
/** For "select". */
|
|
335
356
|
options?: readonly string[];
|
|
357
|
+
/** For "select": how each option reads to a person ("YES" → "Would apply"). */
|
|
358
|
+
optionLabels?: Readonly<Record<string, string>>;
|
|
336
359
|
required?: boolean;
|
|
337
360
|
};
|
|
338
361
|
|