@crowandraven/evalkeeper 0.14.0 → 0.15.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.
@@ -0,0 +1,100 @@
1
+ "use client";
2
+
3
+ import { useEffect, useRef, useState, type RefObject } from "react";
4
+ import { Info, X } from "lucide-react";
5
+ import { Button } from "./pieces.js";
6
+
7
+ /**
8
+ * Closes a popup on Escape or a click outside `ref`, while it's open.
9
+ */
10
+ export function useDismiss(
11
+ open: boolean,
12
+ setOpen: (open: boolean) => void,
13
+ ref: RefObject<HTMLElement | null>,
14
+ ) {
15
+ useEffect(() => {
16
+ if (!open) return;
17
+ const onEvent = (e: MouseEvent | KeyboardEvent) => {
18
+ if (
19
+ e instanceof KeyboardEvent
20
+ ? e.key === "Escape"
21
+ : !ref.current?.contains(e.target as Node)
22
+ )
23
+ setOpen(false);
24
+ };
25
+ document.addEventListener("mousedown", onEvent);
26
+ document.addEventListener("keydown", onEvent);
27
+ return () => {
28
+ document.removeEventListener("mousedown", onEvent);
29
+ document.removeEventListener("keydown", onEvent);
30
+ };
31
+ }, [open, setOpen, ref]);
32
+ }
33
+
34
+ /** The X that closes a popup. */
35
+ export function CloseButton({ onClick }: { onClick: () => void }) {
36
+ return (
37
+ <Button
38
+ type="button"
39
+ aria-label="Close"
40
+ title="Close"
41
+ onClick={onClick}
42
+ className="-mr-1 -mt-1 inline-flex h-7 w-7 shrink-0 items-center justify-center rounded-md text-muted hover:bg-tint hover:text-ink"
43
+ >
44
+ <X aria-hidden className="h-4 w-4" />
45
+ </Button>
46
+ );
47
+ }
48
+
49
+ /**
50
+ * An info button that opens more than a tooltip holds: a list, steps, a
51
+ * guide. Opens on press, closes with its X, Escape, or a click outside.
52
+ * For a sentence or two, use InfoTip.
53
+ */
54
+ export function InfoPopover({
55
+ label,
56
+ children,
57
+ }: {
58
+ /** What the popup is, for screen readers: "About the answer key". */
59
+ label: string;
60
+ children: React.ReactNode;
61
+ }) {
62
+ const [open, setOpen] = useState(false);
63
+ const ref = useRef<HTMLDivElement>(null);
64
+ useDismiss(open, setOpen, ref);
65
+ return (
66
+ <div
67
+ ref={ref}
68
+ className="relative ml-1 inline-flex align-middle text-sm font-normal"
69
+ >
70
+ <Button
71
+ type="button"
72
+ aria-haspopup="dialog"
73
+ aria-expanded={open}
74
+ aria-label={label}
75
+ onClick={() => {
76
+ setOpen(!open);
77
+ }}
78
+ className="inline-flex h-4 w-4 items-center justify-center rounded-full text-muted hover:text-ink"
79
+ >
80
+ <Info aria-hidden className="h-3.5 w-3.5" />
81
+ </Button>
82
+ {open && (
83
+ <div
84
+ role="dialog"
85
+ aria-label={label}
86
+ className="absolute left-0 top-full z-20 mt-2 max-h-[70vh] w-[min(28rem,calc(100vw-2rem))] overflow-y-auto rounded-md border border-line bg-card p-4 shadow-lg"
87
+ >
88
+ <div className="flex items-start gap-3">
89
+ <div className="min-w-0 flex-1 space-y-3">{children}</div>
90
+ <CloseButton
91
+ onClick={() => {
92
+ setOpen(false);
93
+ }}
94
+ />
95
+ </div>
96
+ </div>
97
+ )}
98
+ </div>
99
+ );
100
+ }
@@ -1,7 +1,8 @@
1
1
  "use client";
2
2
 
3
- import { useEffect, useRef, useState } from "react";
4
- import { ChevronDown, X } from "lucide-react";
3
+ import { useRef, useState } from "react";
4
+ import { ChevronDown } from "lucide-react";
5
+ import { CloseButton, useDismiss } from "./InfoPopover.js";
5
6
  import { Button, Select, Submit, TextInput } from "./pieces.js";
6
7
  import type { FormAction } from "./app.js";
7
8
  import type { SuitePriority } from "../types.js";
@@ -42,23 +43,7 @@ export function PriorityPicker({
42
43
  const [open, setOpen] = useState(false);
43
44
  const ref = useRef<HTMLDivElement>(null);
44
45
 
45
- useEffect(() => {
46
- if (!open) return;
47
- const close = (e: MouseEvent | KeyboardEvent) => {
48
- if (
49
- e instanceof KeyboardEvent
50
- ? e.key === "Escape"
51
- : !ref.current?.contains(e.target as Node)
52
- )
53
- setOpen(false);
54
- };
55
- document.addEventListener("mousedown", close);
56
- document.addEventListener("keydown", close);
57
- return () => {
58
- document.removeEventListener("mousedown", close);
59
- document.removeEventListener("keydown", close);
60
- };
61
- }, [open]);
46
+ useDismiss(open, setOpen, ref);
62
47
 
63
48
  return (
64
49
  <div ref={ref} className="relative text-sm font-normal">
@@ -82,17 +67,11 @@ export function PriorityPicker({
82
67
  >
83
68
  <div className="flex items-start justify-between gap-3">
84
69
  <p className="text-muted">{why}</p>
85
- <Button
86
- type="button"
87
- aria-label="Close"
88
- title="Close"
70
+ <CloseButton
89
71
  onClick={() => {
90
72
  setOpen(false);
91
73
  }}
92
- className="-mr-1 -mt-1 inline-flex h-7 w-7 shrink-0 items-center justify-center rounded-md text-muted hover:bg-tint hover:text-ink"
93
- >
94
- <X aria-hidden className="h-4 w-4" />
95
- </Button>
74
+ />
96
75
  </div>
97
76
  <form
98
77
  action={action}
@@ -7,6 +7,7 @@ import {
7
7
  type Settings,
8
8
  } from "../index.js";
9
9
  import { RollBack } from "./RollBack.js";
10
+ import { ShowEarlier } from "./ShowEarlier.js";
10
11
  import { Link, Chip, InfoTip, Tip, LocalDate } from "./pieces.js";
11
12
  import { evalsPath, type EvalAppConfig } from "./app.js";
12
13
 
@@ -46,12 +47,16 @@ const KIND: Record<
46
47
  },
47
48
  };
48
49
 
50
+ /** A version's card, on the page's background. */
51
+ const BOX = "space-y-2 rounded-md border border-line bg-card p-3 text-sm";
52
+
49
53
  type Evidence = { runId?: string | null; live?: string | null };
50
54
 
51
55
  /**
52
56
  * A feature's settings history: every switch, newest first, with what
53
57
  * changed, who, why, and the evidence; any earlier version can be made
54
- * live again, with a reason.
58
+ * live again, with a reason. The newest and live versions show; earlier
59
+ * ones open with a toggle.
55
60
  */
56
61
  export function SettingsHistory({
57
62
  app,
@@ -70,12 +75,33 @@ export function SettingsHistory({
70
75
  /** The settings in code, shown as v1 until the first change. */
71
76
  defaults: Settings;
72
77
  }) {
78
+ const heading = (
79
+ <h2 id="history" className="text-lg font-semibold">
80
+ Versions
81
+ <InfoTip term="versions" />
82
+ </h2>
83
+ );
84
+ // The newest version shows, and the live one when it's older (the first
85
+ // that points at it: a rollback points at the version it restored).
86
+ const live = entries.findIndex((e) => e.to.id === liveId);
87
+ const recent = (_e: HistoryEntry, i: number) => i === 0 || i === live;
88
+ const entry = (e: HistoryEntry) => (
89
+ <VersionEntry key={e.id} {...{ app, suite, e, liveId, label, box: BOX }} />
90
+ );
91
+ if (entries.some((e, i) => !recent(e, i)))
92
+ return (
93
+ <section className="space-y-3" aria-labelledby="history">
94
+ <ShowEarlier
95
+ heading={heading}
96
+ noun="versions"
97
+ recent={entries.filter(recent).map(entry)}
98
+ earlier={entries.filter((e, i) => !recent(e, i)).map(entry)}
99
+ />
100
+ </section>
101
+ );
73
102
  return (
74
103
  <section className="space-y-3" aria-labelledby="history">
75
- <h2 id="history" className="text-lg font-semibold">
76
- Versions
77
- <InfoTip term="versions" />
78
- </h2>
104
+ {heading}
79
105
  <HistoryEntries
80
106
  app={app}
81
107
  suite={suite}
@@ -112,7 +138,7 @@ export function HistoryEntries({
112
138
  /** No tint behind each version, for a list already on a tint. */
113
139
  plain?: boolean;
114
140
  }) {
115
- const box = `space-y-2 text-sm ${plain ? "py-2" : "rounded-md border border-line bg-card p-3"}`;
141
+ const box = plain ? "space-y-2 py-2 text-sm" : BOX;
116
142
  if (entries.length === 0)
117
143
  return (
118
144
  <article aria-label="Version 1: Defaults" className={box}>
@@ -135,74 +161,86 @@ export function HistoryEntries({
135
161
  );
136
162
  return (
137
163
  <div className="space-y-2">
138
- {entries.map((e) => {
139
- const kind = KIND[e.kind];
140
- const ev = (e.evidence ?? {}) as Evidence;
141
- const testId = ev.runId ?? ev.live ?? null;
142
- const isLive = e.to.id === liveId;
143
- // Said outright: "same models" read as the app switching models.
144
- const unchanged = (v: typeof e.to) =>
145
- `Models unchanged: ${label(v.model)} live${v.fallbackModel ? `, ${label(v.fallbackModel)} fallback` : ""}`;
146
- return (
147
- <article
148
- key={e.id}
149
- aria-label={`Version ${e.to.number}: ${kind.text}`}
150
- className={box}
151
- >
152
- <div className="flex flex-wrap items-center justify-between gap-2">
153
- <span className="flex flex-wrap items-center gap-2">
154
- <span className="font-medium">v{e.to.number}</span>
155
- <Tip text={kind.help} id={`kind-${e.id}`}>
156
- <Chip tone={kind.tone}>{kind.text}</Chip>
157
- </Tip>
158
- {isLive && <Chip tone="live">Live now</Chip>}
159
- </span>
160
- <span className="text-xs text-muted">
161
- {e.by ??
162
- (e.kind === "OUTSIDE"
163
- ? "Found by the system"
164
- : "System")}{" "}
165
- · <LocalDate date={e.createdAt} time />
166
- </span>
167
- </div>
168
- <ul className="readable list-disc pl-5">
169
- {(e.kind === "ROLLBACK"
170
- ? [`Back to v${e.to.number}: ${label(e.to.model)} live`]
171
- : e.kind === "DEPLOY"
172
- ? [unchanged(e.to), "The instructions in code changed"]
173
- : e.kind === "INSTRUCTIONS"
174
- ? [
175
- unchanged(e.to),
176
- "The instructions were edited at runtime",
177
- ]
178
- : describeChange(e.from, e.to, label)
179
- ).map((line) => (
180
- <li key={line}>{line}</li>
181
- ))}
182
- </ul>
183
- {e.reason && <p className="readable text-muted">{e.reason}</p>}
184
- <div className="flex flex-wrap items-center gap-3">
185
- {testId && (
186
- <Link
187
- href={evalsPath(app, suite, "runs", testId)}
188
- className={`text-xs ${LINK}`}
189
- >
190
- The test behind it
191
- </Link>
192
- )}
193
- {!isLive && (
194
- <RollBack
195
- action={app.actions.rollBackSettings}
196
- suite={suite}
197
- versionId={e.to.id}
198
- number={e.to.number}
199
- kind={e.kind}
200
- />
201
- )}
202
- </div>
203
- </article>
204
- );
205
- })}
164
+ {entries.map((e) => (
165
+ <VersionEntry key={e.id} {...{ app, suite, e, liveId, label, box }} />
166
+ ))}
206
167
  </div>
207
168
  );
208
169
  }
170
+
171
+ /** One version: what changed, who and when, why, its evidence, and rollback. */
172
+ function VersionEntry({
173
+ app,
174
+ suite,
175
+ e,
176
+ liveId,
177
+ label,
178
+ box,
179
+ }: {
180
+ app: EvalAppConfig;
181
+ suite: string;
182
+ e: HistoryEntry;
183
+ liveId: string | null;
184
+ label: (model: string) => string;
185
+ box: string;
186
+ }) {
187
+ const kind = KIND[e.kind];
188
+ const ev = (e.evidence ?? {}) as Evidence;
189
+ const testId = ev.runId ?? ev.live ?? null;
190
+ const isLive = e.to.id === liveId;
191
+ // Said outright: "same models" read as the app switching models.
192
+ const unchanged = (v: typeof e.to) =>
193
+ `Models unchanged: ${label(v.model)} live${v.fallbackModel ? `, ${label(v.fallbackModel)} fallback` : ""}`;
194
+ return (
195
+ <article
196
+ aria-label={`Version ${e.to.number}: ${kind.text}`}
197
+ className={box}
198
+ >
199
+ <div className="flex flex-wrap items-center justify-between gap-2">
200
+ <span className="flex flex-wrap items-center gap-2">
201
+ <span className="font-medium">v{e.to.number}</span>
202
+ <Tip text={kind.help} id={`kind-${e.id}`}>
203
+ <Chip tone={kind.tone}>{kind.text}</Chip>
204
+ </Tip>
205
+ {isLive && <Chip tone="live">Live now</Chip>}
206
+ </span>
207
+ <span className="text-xs text-muted">
208
+ {e.by ?? (e.kind === "OUTSIDE" ? "Found by the system" : "System")} ·{" "}
209
+ <LocalDate date={e.createdAt} time />
210
+ </span>
211
+ </div>
212
+ <ul className="readable list-disc pl-5">
213
+ {(e.kind === "ROLLBACK"
214
+ ? [`Back to v${e.to.number}: ${label(e.to.model)} live`]
215
+ : e.kind === "DEPLOY"
216
+ ? [unchanged(e.to), "The instructions in code changed"]
217
+ : e.kind === "INSTRUCTIONS"
218
+ ? [unchanged(e.to), "The instructions were edited at runtime"]
219
+ : describeChange(e.from, e.to, label)
220
+ ).map((line) => (
221
+ <li key={line}>{line}</li>
222
+ ))}
223
+ </ul>
224
+ {e.reason && <p className="readable text-muted">{e.reason}</p>}
225
+ <div className="flex flex-wrap items-center gap-3">
226
+ {testId && (
227
+ <Link
228
+ href={evalsPath(app, suite, "runs", testId)}
229
+ className={`text-xs ${LINK}`}
230
+ >
231
+ The test behind it
232
+ </Link>
233
+ )}
234
+ {!isLive && (
235
+ <RollBack
236
+ action={app.actions.rollBackSettings}
237
+ suite={suite}
238
+ versionId={e.to.id}
239
+ number={e.to.number}
240
+ kind={e.kind}
241
+ />
242
+ )}
243
+ </div>
244
+ </article>
245
+ );
246
+ }
@@ -0,0 +1,57 @@
1
+ "use client";
2
+
3
+ import { useId, useState } from "react";
4
+ import { ChevronDown } from "lucide-react";
5
+ import { Button } from "./pieces.js";
6
+
7
+ /**
8
+ * A section whose earlier items stay folded until asked for: the heading
9
+ * with a toggle across from it, the recent items, then the rest when open.
10
+ */
11
+ export function ShowEarlier({
12
+ heading,
13
+ recent,
14
+ earlier,
15
+ noun,
16
+ }: {
17
+ heading: React.ReactNode;
18
+ recent: React.ReactNode;
19
+ earlier: React.ReactNode[];
20
+ /** What the items are, plural: "versions". */
21
+ noun: string;
22
+ }) {
23
+ const [open, setOpen] = useState(false);
24
+ const id = useId();
25
+ return (
26
+ <>
27
+ <div className="flex items-center justify-between gap-4">
28
+ {heading}
29
+ <Button
30
+ type="button"
31
+ aria-expanded={open}
32
+ aria-controls={open ? id : undefined}
33
+ onClick={() => {
34
+ setOpen(!open);
35
+ }}
36
+ className="inline-flex items-center gap-1 text-sm text-muted hover:text-ink"
37
+ >
38
+ {open
39
+ ? `Hide earlier ${noun}`
40
+ : `Show ${earlier.length} earlier ${earlier.length === 1 ? noun.replace(/s$/, "") : noun}`}
41
+ <ChevronDown
42
+ aria-hidden
43
+ className={`h-4 w-4 transition-transform ${open ? "rotate-180" : ""}`}
44
+ />
45
+ </Button>
46
+ </div>
47
+ <div className="space-y-2">
48
+ {recent}
49
+ {open && (
50
+ <div id={id} className="space-y-2">
51
+ {earlier}
52
+ </div>
53
+ )}
54
+ </div>
55
+ </>
56
+ );
57
+ }