@crowandraven/evalkeeper 0.10.0 → 0.11.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/README.md +1 -2
- package/dist/ai-sdk/decide.d.ts +35 -0
- package/dist/ai-sdk/decide.js +36 -0
- package/dist/ai-sdk/decide.js.map +1 -0
- package/dist/ai-sdk/hash.d.ts +50 -0
- package/dist/ai-sdk/hash.js +59 -0
- package/dist/ai-sdk/hash.js.map +1 -0
- package/dist/ai-sdk/index.d.ts +15 -0
- package/dist/ai-sdk/index.js +16 -0
- package/dist/ai-sdk/index.js.map +1 -0
- package/dist/ai-sdk/judge.d.ts +69 -0
- package/dist/ai-sdk/judge.js +105 -0
- package/dist/ai-sdk/judge.js.map +1 -0
- package/dist/ai-sdk/usage.d.ts +18 -0
- package/dist/ai-sdk/usage.js +19 -0
- package/dist/ai-sdk/usage.js.map +1 -0
- package/dist/board.d.ts +5 -0
- package/dist/cli/addPages.d.ts +6 -1
- package/dist/cli/addPages.js +14 -8
- package/dist/cli/addPages.js.map +1 -1
- package/dist/cli/main.d.ts +19 -5
- package/dist/cli/main.js +100 -4
- package/dist/cli/main.js.map +1 -1
- package/dist/cli/migration.d.ts +95 -0
- package/dist/cli/migration.js +247 -0
- package/dist/cli/migration.js.map +1 -0
- package/dist/feedback.d.ts +1 -1
- package/dist/glossary.d.ts +8 -1
- package/dist/glossary.js +8 -1
- package/dist/glossary.js.map +1 -1
- package/dist/inInstructions.d.ts +1 -1
- package/dist/inInstructions.js +1 -1
- package/dist/next/RefreshWhile.d.ts +11 -0
- package/dist/next/RefreshWhile.js +28 -0
- package/dist/next/RefreshWhile.js.map +1 -0
- package/dist/next/actions.d.ts +14 -0
- package/dist/next/actions.js +332 -0
- package/dist/next/actions.js.map +1 -0
- package/dist/next/components.d.ts +9 -0
- package/dist/next/components.js +27 -0
- package/dist/next/components.js.map +1 -0
- package/dist/next/cron.d.ts +9 -0
- package/dist/next/cron.js +17 -0
- package/dist/next/cron.js.map +1 -0
- package/dist/next/index.d.ts +15 -0
- package/dist/next/index.js +16 -0
- package/dist/next/index.js.map +1 -0
- package/dist/next/pages.d.ts +70 -0
- package/dist/next/pages.js +77 -0
- package/dist/next/pages.js.map +1 -0
- package/dist/next/route.d.ts +19 -0
- package/dist/next/route.js +31 -0
- package/dist/next/route.js.map +1 -0
- package/dist/next/weekly.d.ts +20 -0
- package/dist/next/weekly.js +55 -0
- package/dist/next/weekly.js.map +1 -0
- package/dist/next/wiring.d.ts +19 -0
- package/dist/next/wiring.js +2 -0
- package/dist/next/wiring.js.map +1 -0
- package/dist/prisma/contracts.d.ts +74 -0
- package/dist/prisma/contracts.js +178 -0
- package/dist/prisma/contracts.js.map +1 -0
- package/dist/prisma/index.d.ts +423 -0
- package/dist/prisma/index.js +622 -0
- package/dist/prisma/index.js.map +1 -0
- package/dist/prisma/mapping.d.ts +63 -0
- package/dist/prisma/mapping.js +111 -0
- package/dist/prisma/mapping.js.map +1 -0
- package/dist/prisma/moveIn.d.ts +106 -0
- package/dist/prisma/moveIn.js +299 -0
- package/dist/prisma/moveIn.js.map +1 -0
- package/dist/prune.d.ts +45 -0
- package/dist/prune.js +32 -0
- package/dist/prune.js.map +1 -1
- package/dist/react/AddCase.d.ts +10 -0
- package/dist/react/AddCase.js +19 -0
- package/dist/react/AddCase.js.map +1 -0
- package/dist/react/AnswerKey.d.ts +22 -0
- package/dist/react/AnswerKey.js +83 -0
- package/dist/react/AnswerKey.js.map +1 -0
- package/dist/react/CaseMenu.d.ts +17 -0
- package/dist/react/CaseMenu.js +41 -0
- package/dist/react/CaseMenu.js.map +1 -0
- package/dist/react/CompareView.d.ts +14 -0
- package/dist/react/CompareView.js +51 -0
- package/dist/react/CompareView.js.map +1 -0
- package/dist/react/ConfirmAnswer.d.ts +13 -0
- package/dist/react/ConfirmAnswer.js +27 -0
- package/dist/react/ConfirmAnswer.js.map +1 -0
- package/dist/react/DisagreementChip.d.ts +10 -0
- package/dist/react/DisagreementChip.js +21 -0
- package/dist/react/DisagreementChip.js.map +1 -0
- package/dist/react/FeedbackView.d.ts +10 -0
- package/dist/react/FeedbackView.js +26 -0
- package/dist/react/FeedbackView.js.map +1 -0
- package/dist/react/Fields.d.ts +32 -0
- package/dist/react/Fields.js +33 -0
- package/dist/react/Fields.js.map +1 -0
- package/dist/react/HubSections.d.ts +78 -0
- package/dist/react/HubSections.js +124 -0
- package/dist/react/HubSections.js.map +1 -0
- package/dist/react/ModelMenu.d.ts +16 -0
- package/dist/react/ModelMenu.js +38 -0
- package/dist/react/ModelMenu.js.map +1 -0
- package/dist/react/RateView.d.ts +18 -0
- package/dist/react/RateView.js +30 -0
- package/dist/react/RateView.js.map +1 -0
- package/dist/react/ReadableOutput.d.ts +3 -0
- package/dist/react/ReadableOutput.js +50 -0
- package/dist/react/ReadableOutput.js.map +1 -0
- package/dist/react/RollBack.d.ts +17 -0
- package/dist/react/RollBack.js +26 -0
- package/dist/react/RollBack.js.map +1 -0
- package/dist/react/RunView.d.ts +9 -0
- package/dist/react/RunView.js +41 -0
- package/dist/react/RunView.js.map +1 -0
- package/dist/react/SettingsHistory.d.ts +33 -0
- package/dist/react/SettingsHistory.js +79 -0
- package/dist/react/SettingsHistory.js.map +1 -0
- package/dist/react/SideBySideView.d.ts +14 -0
- package/dist/react/SideBySideView.js +67 -0
- package/dist/react/SideBySideView.js.map +1 -0
- package/dist/react/SuiteSections.d.ts +111 -0
- package/dist/react/SuiteSections.js +224 -0
- package/dist/react/SuiteSections.js.map +1 -0
- package/dist/react/Tabs.d.ts +15 -0
- package/dist/react/Tabs.js +18 -0
- package/dist/react/Tabs.js.map +1 -0
- package/dist/react/Thumbs.d.ts +13 -0
- package/dist/react/Thumbs.js +54 -0
- package/dist/react/Thumbs.js.map +1 -0
- package/dist/react/WeeklyCheck.d.ts +11 -0
- package/dist/react/WeeklyCheck.js +19 -0
- package/dist/react/WeeklyCheck.js.map +1 -0
- package/dist/react/app.d.ts +118 -0
- package/dist/react/app.js +87 -0
- package/dist/react/app.js.map +1 -0
- package/dist/react/format.d.ts +27 -0
- package/dist/react/format.js +38 -0
- package/dist/react/format.js.map +1 -0
- package/dist/react/index.d.ts +31 -0
- package/dist/react/index.js +32 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/pieces.d.ts +191 -0
- package/dist/react/pieces.js +191 -0
- package/dist/react/pieces.js.map +1 -0
- package/dist/readCheck.d.ts +1 -1
- package/dist/runner.d.ts +62 -2
- package/dist/runner.js +123 -3
- package/dist/runner.js.map +1 -1
- package/dist/settings.d.ts +1 -1
- package/dist/store.d.ts +1 -1
- package/dist/suggest.d.ts +1 -1
- package/dist/suggest.js +7 -7
- package/dist/suggest.js.map +1 -1
- package/dist/suiteContract.d.ts +3 -3
- package/dist/suiteContract.js +20 -16
- package/dist/suiteContract.js.map +1 -1
- package/dist/testing.d.ts +2 -2
- package/dist/testing.js +1 -1
- package/dist/types.js +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +56 -6
- package/schema/evalkeeper.prisma +196 -0
- package/src/ai-sdk/decide.ts +57 -0
- package/src/ai-sdk/hash.ts +74 -0
- package/src/ai-sdk/index.ts +15 -0
- package/src/ai-sdk/judge.ts +163 -0
- package/src/ai-sdk/usage.ts +31 -0
- package/src/board.ts +5 -0
- package/src/cli/addPages.ts +15 -7
- package/src/cli/main.ts +135 -9
- package/src/cli/migration.ts +349 -0
- package/src/feedback.ts +1 -1
- package/src/glossary.ts +16 -2
- package/src/inInstructions.ts +1 -1
- package/{templates/components/evalkeeper → src/next}/RefreshWhile.tsx +2 -2
- package/src/next/actions.ts +389 -0
- package/src/next/components.ts +31 -0
- package/{templates/lib/evalkeeper/cronAuth.ts → src/next/cron.ts} +3 -0
- package/src/next/index.ts +15 -0
- package/src/next/pages.tsx +106 -0
- package/src/next/route.ts +35 -0
- package/src/next/weekly.ts +70 -0
- package/src/next/wiring.ts +18 -0
- package/src/prisma/contracts.ts +249 -0
- package/src/prisma/index.ts +1169 -0
- package/src/prisma/mapping.ts +147 -0
- package/src/prisma/moveIn.ts +502 -0
- package/src/prune.ts +77 -0
- package/{templates/components/evalkeeper → src/react}/AddCase.tsx +6 -6
- package/{templates/components/evalkeeper → src/react}/AnswerKey.tsx +92 -61
- package/{templates/components/evalkeeper → src/react}/CaseMenu.tsx +9 -8
- package/src/react/CompareView.tsx +270 -0
- package/{templates/components/evalkeeper → src/react}/ConfirmAnswer.tsx +6 -3
- package/{templates/components/evalkeeper → src/react}/DisagreementChip.tsx +2 -3
- package/src/react/FeedbackView.tsx +230 -0
- package/{templates/components/evalkeeper → src/react}/Fields.tsx +6 -6
- package/src/react/HubSections.tsx +446 -0
- package/src/react/ModelMenu.tsx +89 -0
- package/src/react/RateView.tsx +143 -0
- package/{templates/components/evalkeeper → src/react}/ReadableOutput.tsx +1 -1
- package/{templates/components/evalkeeper → src/react}/RollBack.tsx +9 -10
- package/src/react/RunView.tsx +151 -0
- package/{templates/components/evalkeeper → src/react}/SettingsHistory.tsx +16 -9
- package/src/react/SideBySideView.tsx +307 -0
- package/src/react/SuiteSections.tsx +925 -0
- package/src/react/Tabs.tsx +61 -0
- package/{templates/components/evalkeeper → src/react}/Thumbs.tsx +7 -6
- package/{templates/components/evalkeeper → src/react}/WeeklyCheck.tsx +31 -20
- package/src/react/app.ts +173 -0
- package/src/react/format.ts +45 -0
- package/src/react/index.ts +31 -0
- package/src/react/pieces.tsx +412 -0
- package/src/readCheck.ts +1 -1
- package/src/runner.ts +167 -3
- package/src/settings.ts +1 -1
- package/src/store.ts +1 -1
- package/src/suggest.ts +7 -7
- package/src/suiteContract.ts +35 -28
- package/src/testing.ts +2 -2
- package/src/types.ts +2 -2
- package/starter/index.ts +22 -8
- package/templates/app/api/cron/evals-weekly/route.ts +5 -15
- package/templates/app/evals/[suite]/compare/page.tsx +6 -383
- package/templates/app/evals/[suite]/page.tsx +7 -685
- package/templates/app/evals/[suite]/rate/page.tsx +6 -151
- package/templates/app/evals/[suite]/runs/[runId]/page.tsx +6 -174
- package/templates/app/evals/[suite]/side-by-side/page.tsx +6 -318
- package/templates/app/evals/feedback/page.tsx +6 -227
- package/templates/app/evals/layout.tsx +10 -0
- package/templates/app/evals/page.tsx +7 -397
- package/templates/lib/evalkeeper/actions.ts +34 -317
- package/templates/lib/evalkeeper/appComponents.tsx +21 -0
- package/templates/lib/evalkeeper/evals.ts +14 -0
- package/templates/lib/evalkeeper/paths.ts +2 -7
- package/templates/components/evalkeeper/Submit.tsx +0 -67
- package/templates/components/evalkeeper/Tip.tsx +0 -98
- package/templates/components/evalkeeper/ui.tsx +0 -105
- package/templates/lib/evalkeeper/time.ts +0 -4
- package/templates/lib/evalkeeper/weekly.ts +0 -74
|
@@ -0,0 +1,1169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Evalkeeper's tables for Prisma, and stores on your Prisma client.
|
|
3
|
+
*
|
|
4
|
+
* @module evalkeeper/prisma
|
|
5
|
+
*/
|
|
6
|
+
import { CHANGE_KINDS, SettingsConflict, cleanRating } from "../index.js";
|
|
7
|
+
import type {
|
|
8
|
+
CaseRecord,
|
|
9
|
+
FeedbackRecord,
|
|
10
|
+
NewRating,
|
|
11
|
+
FeedbackStatus,
|
|
12
|
+
FeedbackStore,
|
|
13
|
+
CheckResult,
|
|
14
|
+
EvalSet,
|
|
15
|
+
EvalStore,
|
|
16
|
+
LabelRecord,
|
|
17
|
+
LabelStatus,
|
|
18
|
+
OutputRecord,
|
|
19
|
+
RunMetrics,
|
|
20
|
+
RunRecord,
|
|
21
|
+
RunStatus,
|
|
22
|
+
ChangeKind,
|
|
23
|
+
SettingsChange,
|
|
24
|
+
SettingsStore,
|
|
25
|
+
SettingsVersion,
|
|
26
|
+
} from "../index.js";
|
|
27
|
+
import {
|
|
28
|
+
Table,
|
|
29
|
+
isRow,
|
|
30
|
+
type ColumnMap,
|
|
31
|
+
type RawDelegate,
|
|
32
|
+
type Row,
|
|
33
|
+
} from "./mapping.js";
|
|
34
|
+
|
|
35
|
+
export type { ColumnMap } from "./mapping.js";
|
|
36
|
+
export * from "./contracts.js";
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* An EvalStore on your Prisma client, using the models in
|
|
40
|
+
* schema/evalkeeper.prisma. Pass the client you already have:
|
|
41
|
+
*
|
|
42
|
+
* const store = prismaStore(prisma);
|
|
43
|
+
*
|
|
44
|
+
* The client is typed by the rows it returns, not by Prisma's generated
|
|
45
|
+
* types, so this package works with any client generated from the schema.
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
/** A stored JSON value, as Prisma returns it. */
|
|
49
|
+
type Json = unknown;
|
|
50
|
+
|
|
51
|
+
/** The rows of Evalkeeper's tables, as the schema defines them. */
|
|
52
|
+
export type EvalCaseRow = {
|
|
53
|
+
id: string;
|
|
54
|
+
suite: string;
|
|
55
|
+
input: Json;
|
|
56
|
+
tags: Json;
|
|
57
|
+
split: string;
|
|
58
|
+
status: string;
|
|
59
|
+
source: string;
|
|
60
|
+
sourceRef: string | null;
|
|
61
|
+
note: string | null;
|
|
62
|
+
createdAt: Date;
|
|
63
|
+
updatedAt: Date;
|
|
64
|
+
};
|
|
65
|
+
/** An EvalCaseLabel row. */
|
|
66
|
+
export type EvalCaseLabelRow = {
|
|
67
|
+
id: string;
|
|
68
|
+
caseId: string;
|
|
69
|
+
kind: string;
|
|
70
|
+
status: string;
|
|
71
|
+
value: Json;
|
|
72
|
+
raterRole: string;
|
|
73
|
+
raterId: string | null;
|
|
74
|
+
note: string | null;
|
|
75
|
+
createdAt: Date;
|
|
76
|
+
updatedAt: Date;
|
|
77
|
+
};
|
|
78
|
+
/** An EvalRun row. */
|
|
79
|
+
export type EvalRunRow = {
|
|
80
|
+
id: string;
|
|
81
|
+
suite: string;
|
|
82
|
+
model: string;
|
|
83
|
+
promptVersion: string;
|
|
84
|
+
reasoningEffort: string | null;
|
|
85
|
+
temperature: number | null;
|
|
86
|
+
maxOutputTokens: number | null;
|
|
87
|
+
set: string;
|
|
88
|
+
repeats: number;
|
|
89
|
+
datasetVersion: string | null;
|
|
90
|
+
graderVersion: string;
|
|
91
|
+
status: string;
|
|
92
|
+
metrics: Json;
|
|
93
|
+
error: string | null;
|
|
94
|
+
estimatedCostUsd: number | null;
|
|
95
|
+
startedBy: string | null;
|
|
96
|
+
startedAt: Date;
|
|
97
|
+
finishedAt: Date | null;
|
|
98
|
+
};
|
|
99
|
+
/** An EvalOutput row. */
|
|
100
|
+
export type EvalOutputRow = {
|
|
101
|
+
id: string;
|
|
102
|
+
runId: string;
|
|
103
|
+
caseId: string;
|
|
104
|
+
repeat: number;
|
|
105
|
+
requestHash: string;
|
|
106
|
+
output: Json;
|
|
107
|
+
error: string | null;
|
|
108
|
+
scores: Json;
|
|
109
|
+
costUsd: number;
|
|
110
|
+
ms: number;
|
|
111
|
+
reused: boolean;
|
|
112
|
+
createdAt: Date;
|
|
113
|
+
};
|
|
114
|
+
/** An EvalJudgment row. */
|
|
115
|
+
export type EvalJudgmentRow = {
|
|
116
|
+
key: string;
|
|
117
|
+
value: Json;
|
|
118
|
+
model: string;
|
|
119
|
+
createdAt: Date;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
/** The methods of a Prisma model delegate this package calls. */
|
|
123
|
+
type Delegate<Row> = {
|
|
124
|
+
findMany(args: object): PromiseLike<Row[]>;
|
|
125
|
+
findFirst(args: object): PromiseLike<Row | null>;
|
|
126
|
+
findUnique(args: object): PromiseLike<Row | null>;
|
|
127
|
+
create(args: object): PromiseLike<Row>;
|
|
128
|
+
update(args: object): PromiseLike<Row>;
|
|
129
|
+
updateMany(args: object): PromiseLike<{ count: number }>;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
/** The part of a generated Prisma client Evalkeeper uses. */
|
|
133
|
+
export type PrismaEvalClient = {
|
|
134
|
+
evalCase: Delegate<EvalCaseRow>;
|
|
135
|
+
evalCaseLabel: Delegate<EvalCaseLabelRow>;
|
|
136
|
+
evalRun: Delegate<EvalRunRow>;
|
|
137
|
+
evalOutput: Delegate<EvalOutputRow>;
|
|
138
|
+
evalJudgment: Delegate<EvalJudgmentRow>;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/** `value` when it's one of `allowed`, else `fallback`. */
|
|
142
|
+
function oneOf<T extends string>(
|
|
143
|
+
value: unknown,
|
|
144
|
+
allowed: readonly T[],
|
|
145
|
+
fallback: T,
|
|
146
|
+
): T {
|
|
147
|
+
return typeof value === "string" &&
|
|
148
|
+
(allowed as readonly string[]).includes(value)
|
|
149
|
+
? (value as T)
|
|
150
|
+
: fallback;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Readers for a column's value. Rows come from the app's tables, which
|
|
154
|
+
// may be shaped differently (nullable where Evalkeeper's aren't), so each
|
|
155
|
+
// value is narrowed rather than trusted.
|
|
156
|
+
const str = (v: unknown): string => (typeof v === "string" ? v : "");
|
|
157
|
+
const strOrNull = (v: unknown): string | null =>
|
|
158
|
+
typeof v === "string" ? v : null;
|
|
159
|
+
const num = (v: unknown): number => (typeof v === "number" ? v : 0);
|
|
160
|
+
const numOrNull = (v: unknown): number | null =>
|
|
161
|
+
typeof v === "number" ? v : null;
|
|
162
|
+
const date = (v: unknown): Date => (v instanceof Date ? v : new Date(0));
|
|
163
|
+
const dateOrNull = (v: unknown): Date | null => (v instanceof Date ? v : null);
|
|
164
|
+
|
|
165
|
+
const LABEL_STATUSES: readonly LabelStatus[] = [
|
|
166
|
+
"PENDING",
|
|
167
|
+
"CONFIRMED",
|
|
168
|
+
"PROVISIONAL",
|
|
169
|
+
"REJECTED",
|
|
170
|
+
"SKIPPED",
|
|
171
|
+
];
|
|
172
|
+
const RUN_STATUSES: readonly RunStatus[] = ["RUNNING", "SUCCEEDED", "FAILED"];
|
|
173
|
+
const SETS: readonly EvalSet[] = ["ALL", "SMOKE", "DEV", "TEST"];
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* A skip mark has no answer, but the value column can't be null (and
|
|
177
|
+
* Prisma wants JSON null spelled with its own namespace), so a skip is
|
|
178
|
+
* written as `{}` and read back as null.
|
|
179
|
+
*/
|
|
180
|
+
const SKIP_VALUE = {};
|
|
181
|
+
|
|
182
|
+
const label = (l: Row): LabelRecord => {
|
|
183
|
+
const status = oneOf(l.status, LABEL_STATUSES, "PENDING");
|
|
184
|
+
return {
|
|
185
|
+
id: str(l.id),
|
|
186
|
+
kind: "EXPECTED",
|
|
187
|
+
status,
|
|
188
|
+
value: status === "SKIPPED" ? null : l.value,
|
|
189
|
+
raterRole: str(l.raterRole),
|
|
190
|
+
raterId: strOrNull(l.raterId),
|
|
191
|
+
note: strOrNull(l.note),
|
|
192
|
+
createdAt: date(l.createdAt),
|
|
193
|
+
updatedAt: date(l.updatedAt),
|
|
194
|
+
};
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
const run = (r: Row): RunRecord => ({
|
|
198
|
+
id: str(r.id),
|
|
199
|
+
suite: str(r.suite),
|
|
200
|
+
model: str(r.model),
|
|
201
|
+
promptVersion: str(r.promptVersion),
|
|
202
|
+
reasoningEffort: strOrNull(r.reasoningEffort),
|
|
203
|
+
temperature: numOrNull(r.temperature),
|
|
204
|
+
maxOutputTokens: numOrNull(r.maxOutputTokens),
|
|
205
|
+
set: oneOf(r.set, SETS, "ALL"),
|
|
206
|
+
repeats: num(r.repeats),
|
|
207
|
+
datasetVersion: strOrNull(r.datasetVersion),
|
|
208
|
+
graderVersion: str(r.graderVersion),
|
|
209
|
+
status: oneOf(r.status, RUN_STATUSES, "FAILED"),
|
|
210
|
+
// Written only by the runner, in this shape.
|
|
211
|
+
metrics: (r.metrics ?? null) as RunMetrics | null,
|
|
212
|
+
error: strOrNull(r.error),
|
|
213
|
+
estimatedCostUsd: numOrNull(r.estimatedCostUsd),
|
|
214
|
+
startedBy: strOrNull(r.startedBy),
|
|
215
|
+
startedAt: date(r.startedAt),
|
|
216
|
+
finishedAt: dateOrNull(r.finishedAt),
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
const output = (o: Row): OutputRecord => ({
|
|
220
|
+
id: str(o.id),
|
|
221
|
+
runId: str(o.runId),
|
|
222
|
+
caseId: str(o.caseId),
|
|
223
|
+
repeat: num(o.repeat),
|
|
224
|
+
requestHash: str(o.requestHash),
|
|
225
|
+
output: o.output ?? null,
|
|
226
|
+
error: strOrNull(o.error),
|
|
227
|
+
// Written only by the runner, in this shape.
|
|
228
|
+
scores: (o.scores ?? null) as CheckResult[] | null,
|
|
229
|
+
costUsd: num(o.costUsd),
|
|
230
|
+
ms: num(o.ms),
|
|
231
|
+
reused: o.reused === true,
|
|
232
|
+
createdAt: date(o.createdAt),
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* A value for a JSON column. Prisma reads `undefined` as "leave it alone"
|
|
237
|
+
* and wants JSON null spelled out, so a missing value stays undefined.
|
|
238
|
+
*/
|
|
239
|
+
const json = (v: unknown): object | string | number | boolean | undefined =>
|
|
240
|
+
v === undefined || v === null ? undefined : v;
|
|
241
|
+
|
|
242
|
+
/** A label's value for its JSON column; a skip's is `SKIP_VALUE`. */
|
|
243
|
+
const labelValue = (l: { status: LabelStatus; value: unknown }) =>
|
|
244
|
+
l.status === "SKIPPED" ? SKIP_VALUE : json(l.value);
|
|
245
|
+
|
|
246
|
+
/** Evalkeeper's eval tables, by the names its schema gives them. */
|
|
247
|
+
export type EvalTable =
|
|
248
|
+
"evalCase" | "evalCaseLabel" | "evalRun" | "evalOutput" | "evalJudgment";
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* How Evalkeeper's eval tables map onto an app's own, for an app that had
|
|
252
|
+
* eval tables before Evalkeeper. Pass it to `prismaStore` and
|
|
253
|
+
* `prismaJudgmentCache`; leave out anything named the same.
|
|
254
|
+
*
|
|
255
|
+
* Types convert on their own: Decimal columns read as numbers, enum
|
|
256
|
+
* columns as their names, and `tags` may be a `String[]` or a `Json`
|
|
257
|
+
* column. Columns Evalkeeper doesn't know (a creator, a legacy results
|
|
258
|
+
* column) are left alone, so they need a default or to be nullable.
|
|
259
|
+
*/
|
|
260
|
+
export type PrismaStoreMap = {
|
|
261
|
+
/**
|
|
262
|
+
* Each table's model on the Prisma client, as the client spells it
|
|
263
|
+
* (`prisma.goldCase` is `"goldCase"`).
|
|
264
|
+
*/
|
|
265
|
+
models?: Partial<Record<EvalTable, string>>;
|
|
266
|
+
/**
|
|
267
|
+
* Column names per table, keyed by Evalkeeper's field name. `null`
|
|
268
|
+
* says the table has no such column (for nullable fields only): reads
|
|
269
|
+
* give null, and writing a value fails rather than losing it.
|
|
270
|
+
*/
|
|
271
|
+
fields?: {
|
|
272
|
+
/** `labels` is the relation from a case to its answers. */
|
|
273
|
+
evalCase?: ColumnMap<EvalCaseRow & { labels: unknown }>;
|
|
274
|
+
evalCaseLabel?: ColumnMap<EvalCaseLabelRow>;
|
|
275
|
+
evalRun?: ColumnMap<EvalRunRow>;
|
|
276
|
+
evalOutput?: ColumnMap<EvalOutputRow>;
|
|
277
|
+
evalJudgment?: ColumnMap<EvalJudgmentRow>;
|
|
278
|
+
};
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
const tables = (map: PrismaStoreMap) => {
|
|
282
|
+
const t = (name: EvalTable) =>
|
|
283
|
+
new Table(name, map.models?.[name] ?? name, map.fields?.[name]);
|
|
284
|
+
return {
|
|
285
|
+
cases: t("evalCase"),
|
|
286
|
+
labels: t("evalCaseLabel"),
|
|
287
|
+
runs: t("evalRun"),
|
|
288
|
+
outputs: t("evalOutput"),
|
|
289
|
+
judgments: t("evalJudgment"),
|
|
290
|
+
};
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* An EvalStore on your Prisma client, using the models in
|
|
295
|
+
* `schema/evalkeeper.prisma`.
|
|
296
|
+
*
|
|
297
|
+
* @param db - Your Prisma client.
|
|
298
|
+
*
|
|
299
|
+
* @example
|
|
300
|
+
* ```ts
|
|
301
|
+
* import { prismaStore } from "@crowandraven/evalkeeper/prisma";
|
|
302
|
+
* const store = prismaStore(prisma);
|
|
303
|
+
* const runner = createRunner({ store, suites, models, promptVersion });
|
|
304
|
+
* ```
|
|
305
|
+
*/
|
|
306
|
+
export function prismaStore(db: PrismaEvalClient): EvalStore;
|
|
307
|
+
/**
|
|
308
|
+
* An EvalStore on your own eval tables, through a mapping (see
|
|
309
|
+
* `PrismaStoreMap`), for an app that had eval tables before Evalkeeper.
|
|
310
|
+
*
|
|
311
|
+
* @param db - Your Prisma client.
|
|
312
|
+
* @param map - Where your tables differ from Evalkeeper's.
|
|
313
|
+
*
|
|
314
|
+
* @example
|
|
315
|
+
* ```ts
|
|
316
|
+
* const store = prismaStore(prisma, {
|
|
317
|
+
* fields: {
|
|
318
|
+
* evalCaseLabel: { note: "rationale" },
|
|
319
|
+
* evalRun: { set: "split", startedBy: "startedById" },
|
|
320
|
+
* },
|
|
321
|
+
* });
|
|
322
|
+
* ```
|
|
323
|
+
*/
|
|
324
|
+
export function prismaStore(db: object, map: PrismaStoreMap): EvalStore;
|
|
325
|
+
export function prismaStore(db: object, map: PrismaStoreMap = {}): EvalStore {
|
|
326
|
+
const t = tables(map);
|
|
327
|
+
const cases = t.cases.on(db);
|
|
328
|
+
const labels = t.labels.on(db);
|
|
329
|
+
const runs = t.runs.on(db);
|
|
330
|
+
const outputs = t.outputs.on(db);
|
|
331
|
+
const labelsOf = t.cases.col("labels") ?? "labels";
|
|
332
|
+
const evalCase = (c: Row): CaseRecord => {
|
|
333
|
+
return {
|
|
334
|
+
id: str(c.id),
|
|
335
|
+
suite: str(c.suite),
|
|
336
|
+
input: c.input,
|
|
337
|
+
tags: Array.isArray(c.tags)
|
|
338
|
+
? c.tags.filter((tag): tag is string => typeof tag === "string")
|
|
339
|
+
: [],
|
|
340
|
+
split: oneOf(c.split, ["DEV", "TEST"] as const, "DEV"),
|
|
341
|
+
status: oneOf(c.status, ["DRAFT", "ACTIVE", "RETIRED"] as const, "DRAFT"),
|
|
342
|
+
source: str(c.source),
|
|
343
|
+
sourceRef: strOrNull(c.sourceRef),
|
|
344
|
+
note: strOrNull(c.note),
|
|
345
|
+
createdAt: date(c.createdAt),
|
|
346
|
+
updatedAt: date(c.updatedAt),
|
|
347
|
+
labels: Array.isArray(c.labels) ? t.labels.ins(c.labels).map(label) : [],
|
|
348
|
+
};
|
|
349
|
+
};
|
|
350
|
+
const toRun = (raw: unknown) => run(t.runs.in(raw));
|
|
351
|
+
const toOutput = (raw: unknown) => output(t.outputs.in(raw));
|
|
352
|
+
return {
|
|
353
|
+
async listCases(suite, opts) {
|
|
354
|
+
const rows = await cases.findMany({
|
|
355
|
+
where: t.cases.out(
|
|
356
|
+
opts?.includeRetired
|
|
357
|
+
? { suite }
|
|
358
|
+
: { suite, status: { not: "RETIRED" } },
|
|
359
|
+
),
|
|
360
|
+
include: { [labelsOf]: true },
|
|
361
|
+
orderBy: t.cases.out({ createdAt: "asc" }),
|
|
362
|
+
});
|
|
363
|
+
return t.cases.ins(rows).map(evalCase);
|
|
364
|
+
},
|
|
365
|
+
async createCase({ labels: first = [], ...data }) {
|
|
366
|
+
return evalCase(
|
|
367
|
+
t.cases.in(
|
|
368
|
+
await cases.create({
|
|
369
|
+
data: {
|
|
370
|
+
...t.cases.out({ ...data, input: json(data.input) }),
|
|
371
|
+
[labelsOf]: {
|
|
372
|
+
create: first.map((l) =>
|
|
373
|
+
t.labels.out({ ...l, value: labelValue(l) }),
|
|
374
|
+
),
|
|
375
|
+
},
|
|
376
|
+
},
|
|
377
|
+
include: { [labelsOf]: true },
|
|
378
|
+
}),
|
|
379
|
+
),
|
|
380
|
+
);
|
|
381
|
+
},
|
|
382
|
+
async addLabel(caseId, data) {
|
|
383
|
+
return label(
|
|
384
|
+
t.labels.in(
|
|
385
|
+
await labels.create({
|
|
386
|
+
data: t.labels.out({ ...data, caseId, value: labelValue(data) }),
|
|
387
|
+
}),
|
|
388
|
+
),
|
|
389
|
+
);
|
|
390
|
+
},
|
|
391
|
+
async setLabelStatus(id, status) {
|
|
392
|
+
await labels.update({
|
|
393
|
+
where: t.labels.out({ id }),
|
|
394
|
+
data: t.labels.out({ status }),
|
|
395
|
+
});
|
|
396
|
+
},
|
|
397
|
+
async setCaseStatus(id, status) {
|
|
398
|
+
await cases.updateMany({
|
|
399
|
+
where: t.cases.out({ id }),
|
|
400
|
+
data: t.cases.out({ status }),
|
|
401
|
+
});
|
|
402
|
+
},
|
|
403
|
+
async getRun(id) {
|
|
404
|
+
const r = await runs.findUnique({ where: t.runs.out({ id }) });
|
|
405
|
+
return r ? toRun(r) : null;
|
|
406
|
+
},
|
|
407
|
+
async listRuns(suite, opts = {}) {
|
|
408
|
+
const rows = await runs.findMany({
|
|
409
|
+
where: t.runs.out({
|
|
410
|
+
suite,
|
|
411
|
+
...(opts.status ? { status: opts.status } : {}),
|
|
412
|
+
}),
|
|
413
|
+
orderBy: t.runs.out({ startedAt: "desc" }),
|
|
414
|
+
...(opts.limit ? { take: opts.limit } : {}),
|
|
415
|
+
});
|
|
416
|
+
return t.runs.ins(rows).map(run);
|
|
417
|
+
},
|
|
418
|
+
async createRun(data) {
|
|
419
|
+
return toRun(await runs.create({ data: t.runs.out(data) }));
|
|
420
|
+
},
|
|
421
|
+
async updateRun(id, patch, onlyIf) {
|
|
422
|
+
const { metrics, ...rest } = patch;
|
|
423
|
+
await runs.updateMany({
|
|
424
|
+
where: t.runs.out({ id, ...(onlyIf ? { status: onlyIf.status } : {}) }),
|
|
425
|
+
data: t.runs.out({
|
|
426
|
+
...rest,
|
|
427
|
+
...(metrics !== undefined ? { metrics: json(metrics) } : {}),
|
|
428
|
+
}),
|
|
429
|
+
});
|
|
430
|
+
},
|
|
431
|
+
async listOutputs(runId) {
|
|
432
|
+
return t.outputs
|
|
433
|
+
.ins(await outputs.findMany({ where: t.outputs.out({ runId }) }))
|
|
434
|
+
.map(output);
|
|
435
|
+
},
|
|
436
|
+
async listOutputsFor(runIds) {
|
|
437
|
+
if (!runIds.length) return [];
|
|
438
|
+
return t.outputs
|
|
439
|
+
.ins(
|
|
440
|
+
await outputs.findMany({
|
|
441
|
+
where: t.outputs.out({ runId: { in: runIds } }),
|
|
442
|
+
}),
|
|
443
|
+
)
|
|
444
|
+
.map(output);
|
|
445
|
+
},
|
|
446
|
+
async findReusableHashes(requestHashes, since) {
|
|
447
|
+
if (!requestHashes.length) return new Set<string>();
|
|
448
|
+
const rows = await outputs.findMany({
|
|
449
|
+
where: t.outputs.out({
|
|
450
|
+
requestHash: { in: requestHashes },
|
|
451
|
+
createdAt: { gte: since },
|
|
452
|
+
error: null,
|
|
453
|
+
reused: false,
|
|
454
|
+
}),
|
|
455
|
+
select: t.outputs.out({ requestHash: true }),
|
|
456
|
+
distinct: t.outputs.names(["requestHash"]),
|
|
457
|
+
});
|
|
458
|
+
return new Set(t.outputs.ins(rows).map((r) => str(r.requestHash)));
|
|
459
|
+
},
|
|
460
|
+
async findReusableOutput(requestHash, since) {
|
|
461
|
+
const o = await outputs.findFirst({
|
|
462
|
+
where: t.outputs.out({
|
|
463
|
+
requestHash,
|
|
464
|
+
createdAt: { gte: since },
|
|
465
|
+
error: null,
|
|
466
|
+
reused: false,
|
|
467
|
+
}),
|
|
468
|
+
orderBy: t.outputs.out({ createdAt: "desc" }),
|
|
469
|
+
});
|
|
470
|
+
return o ? toOutput(o) : null;
|
|
471
|
+
},
|
|
472
|
+
async createOutput(data) {
|
|
473
|
+
return toOutput(
|
|
474
|
+
await outputs.create({
|
|
475
|
+
data: t.outputs.out({
|
|
476
|
+
...data,
|
|
477
|
+
output: json(data.output),
|
|
478
|
+
scores: json(data.scores),
|
|
479
|
+
}),
|
|
480
|
+
}),
|
|
481
|
+
);
|
|
482
|
+
},
|
|
483
|
+
async setOutputScores(id, scores) {
|
|
484
|
+
await outputs.update({
|
|
485
|
+
where: t.outputs.out({ id }),
|
|
486
|
+
data: t.outputs.out({ scores }),
|
|
487
|
+
});
|
|
488
|
+
},
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* A judgment cache on the EvalJudgment table, for
|
|
494
|
+
* `@crowandraven/evalkeeper/ai-sdk`.
|
|
495
|
+
*
|
|
496
|
+
* @param db - Your Prisma client.
|
|
497
|
+
*/
|
|
498
|
+
export function prismaJudgmentCache(db: PrismaEvalClient): PrismaJudgmentCache;
|
|
499
|
+
/**
|
|
500
|
+
* A judgment cache on your own judgment table, through a mapping.
|
|
501
|
+
*
|
|
502
|
+
* @param db - Your Prisma client.
|
|
503
|
+
* @param map - The mapping you pass `prismaStore`; only `evalJudgment` is
|
|
504
|
+
* used.
|
|
505
|
+
*/
|
|
506
|
+
export function prismaJudgmentCache(
|
|
507
|
+
db: object,
|
|
508
|
+
map: PrismaStoreMap,
|
|
509
|
+
): PrismaJudgmentCache;
|
|
510
|
+
export function prismaJudgmentCache(
|
|
511
|
+
db: object,
|
|
512
|
+
map: PrismaStoreMap = {},
|
|
513
|
+
): PrismaJudgmentCache {
|
|
514
|
+
const { judgments: t } = tables(map);
|
|
515
|
+
const judgments = t.on(db);
|
|
516
|
+
return {
|
|
517
|
+
async get(key) {
|
|
518
|
+
const j = await judgments.findUnique({ where: t.out({ key }) });
|
|
519
|
+
return j ? (t.in(j).value ?? null) : null;
|
|
520
|
+
},
|
|
521
|
+
async set(key, value, model) {
|
|
522
|
+
try {
|
|
523
|
+
await judgments.create({
|
|
524
|
+
data: t.out({ key, value: json(value), model }),
|
|
525
|
+
});
|
|
526
|
+
} catch {
|
|
527
|
+
// Already kept by a concurrent grade: the same answer either way.
|
|
528
|
+
}
|
|
529
|
+
},
|
|
530
|
+
};
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/** Where a judge model's answers are kept, so none is paid for twice. */
|
|
534
|
+
export type PrismaJudgmentCache = {
|
|
535
|
+
get(key: string): Promise<unknown>;
|
|
536
|
+
set(key: string, value: unknown, model: string): Promise<void>;
|
|
537
|
+
};
|
|
538
|
+
|
|
539
|
+
/** An EvalFeedback row. */
|
|
540
|
+
export type EvalFeedbackRow = {
|
|
541
|
+
id: string;
|
|
542
|
+
operation: string;
|
|
543
|
+
targetId: string;
|
|
544
|
+
raterId: string;
|
|
545
|
+
rating: string;
|
|
546
|
+
reason: string | null;
|
|
547
|
+
excerpt: string | null;
|
|
548
|
+
input: Json;
|
|
549
|
+
output: Json;
|
|
550
|
+
status: string;
|
|
551
|
+
caseId: string | null;
|
|
552
|
+
createdAt: Date;
|
|
553
|
+
updatedAt: Date;
|
|
554
|
+
};
|
|
555
|
+
|
|
556
|
+
/** The part of a generated Prisma client the feedback store uses. */
|
|
557
|
+
export type PrismaFeedbackClient = {
|
|
558
|
+
evalFeedback: Delegate<EvalFeedbackRow> & {
|
|
559
|
+
upsert(args: object): PromiseLike<EvalFeedbackRow>;
|
|
560
|
+
deleteMany(args: object): PromiseLike<{ count: number }>;
|
|
561
|
+
};
|
|
562
|
+
};
|
|
563
|
+
|
|
564
|
+
const FEEDBACK_STATUSES: readonly FeedbackStatus[] = [
|
|
565
|
+
"OPEN",
|
|
566
|
+
"CASED",
|
|
567
|
+
"DISMISSED",
|
|
568
|
+
];
|
|
569
|
+
|
|
570
|
+
/** What the rating column holds for a thumbs up and a thumbs down. */
|
|
571
|
+
export type RatingValues = { UP: string; DOWN: string };
|
|
572
|
+
|
|
573
|
+
const RATINGS: RatingValues = { UP: "UP", DOWN: "DOWN" };
|
|
574
|
+
|
|
575
|
+
/**
|
|
576
|
+
* Column names for the feedback table, keyed by Evalkeeper's field. Like
|
|
577
|
+
* `ColumnMap`, and `status` may be `null` too: a table without it reads
|
|
578
|
+
* every rating as OPEN.
|
|
579
|
+
*/
|
|
580
|
+
export type FeedbackColumnMap = ColumnMap<
|
|
581
|
+
Omit<EvalFeedbackRow, "status"> & { status: string | null }
|
|
582
|
+
>;
|
|
583
|
+
|
|
584
|
+
/**
|
|
585
|
+
* How Evalkeeper's feedback table maps onto an app's own thumbs table, for
|
|
586
|
+
* an app that recorded thumbs up and down before Evalkeeper. Pass it to
|
|
587
|
+
* `prismaFeedbackStore` (and `deleteScopedRows`); leave out anything
|
|
588
|
+
* named the same.
|
|
589
|
+
*
|
|
590
|
+
* A table without `input` (most thumbs tables never kept inputs) still
|
|
591
|
+
* gives the weekly summary's thumbs-down alerts and the hub's counts,
|
|
592
|
+
* which need only the rating, feature, and time; its ratings can't become
|
|
593
|
+
* cases, and `whyNotACase` says so.
|
|
594
|
+
*/
|
|
595
|
+
export type PrismaFeedbackMap = {
|
|
596
|
+
/**
|
|
597
|
+
* The table's model on the Prisma client, as the client spells it
|
|
598
|
+
* (`prisma.aiFeedback` is `"aiFeedback"`).
|
|
599
|
+
*/
|
|
600
|
+
models?: { evalFeedback?: string };
|
|
601
|
+
/**
|
|
602
|
+
* Column names, keyed by Evalkeeper's field (`raterId: "userId"`).
|
|
603
|
+
* `null` says the table has no such column (`reason`, `excerpt`,
|
|
604
|
+
* `input`, `output`, `status`, or `caseId`): reads give null (a status,
|
|
605
|
+
* OPEN), and writing a value fails rather than losing it.
|
|
606
|
+
*/
|
|
607
|
+
fields?: { evalFeedback?: FeedbackColumnMap };
|
|
608
|
+
/**
|
|
609
|
+
* What your rating column holds for each, when it isn't UP and DOWN (an
|
|
610
|
+
* enum with other names, say `{ UP: "up", DOWN: "down" }`). A value
|
|
611
|
+
* that's neither reads as a thumbs up.
|
|
612
|
+
*/
|
|
613
|
+
ratings?: RatingValues;
|
|
614
|
+
};
|
|
615
|
+
|
|
616
|
+
/** The feedback table, through a map. */
|
|
617
|
+
function feedbackTable(map: PrismaFeedbackMap = {}): Table {
|
|
618
|
+
return new Table(
|
|
619
|
+
"evalFeedback",
|
|
620
|
+
map.models?.evalFeedback ?? "evalFeedback",
|
|
621
|
+
map.fields?.evalFeedback,
|
|
622
|
+
);
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
/** A model's delegate with the two methods only the feedback store calls. */
|
|
626
|
+
type FeedbackDelegate = RawDelegate & {
|
|
627
|
+
upsert(args: object): PromiseLike<unknown>;
|
|
628
|
+
deleteMany(args: object): PromiseLike<unknown>;
|
|
629
|
+
};
|
|
630
|
+
|
|
631
|
+
function feedbackDelegate(t: Table, db: object): FeedbackDelegate {
|
|
632
|
+
const d = t.on(db);
|
|
633
|
+
if (
|
|
634
|
+
typeof Reflect.get(d, "upsert") !== "function" ||
|
|
635
|
+
typeof Reflect.get(d, "deleteMany") !== "function"
|
|
636
|
+
)
|
|
637
|
+
throw new Error(
|
|
638
|
+
`Your Prisma client's "${t.model}" model has no upsert or deleteMany, so it can't hold ratings.`,
|
|
639
|
+
);
|
|
640
|
+
// Trust boundary: a Prisma delegate, checked for both methods.
|
|
641
|
+
return d as FeedbackDelegate;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/** Fields a rating writes that a table may have no column for. */
|
|
645
|
+
const RATING_TEXT = ["reason", "excerpt", "input", "output"] as const;
|
|
646
|
+
|
|
647
|
+
/**
|
|
648
|
+
* A FeedbackStore on the EvalFeedback table.
|
|
649
|
+
*
|
|
650
|
+
* @param db - Your Prisma client.
|
|
651
|
+
*/
|
|
652
|
+
export function prismaFeedbackStore(db: PrismaFeedbackClient): FeedbackStore;
|
|
653
|
+
/**
|
|
654
|
+
* A FeedbackStore on your own thumbs table, through a map (see
|
|
655
|
+
* `PrismaFeedbackMap`), so the weekly summary's alerts and the hub's
|
|
656
|
+
* counts read the ratings your app already keeps.
|
|
657
|
+
*
|
|
658
|
+
* @param db - Your Prisma client.
|
|
659
|
+
* @param map - Where your table differs from Evalkeeper's.
|
|
660
|
+
*
|
|
661
|
+
* @example
|
|
662
|
+
* An `aiFeedback` table with a `userId` column, a rating enum of UP and
|
|
663
|
+
* DOWN, a reason, an excerpt, and times, that never kept inputs:
|
|
664
|
+
* ```ts
|
|
665
|
+
* const feedbackStore = prismaFeedbackStore(prisma, {
|
|
666
|
+
* models: { evalFeedback: "aiFeedback" },
|
|
667
|
+
* fields: {
|
|
668
|
+
* evalFeedback: {
|
|
669
|
+
* raterId: "userId",
|
|
670
|
+
* input: null,
|
|
671
|
+
* output: null,
|
|
672
|
+
* status: null,
|
|
673
|
+
* caseId: null,
|
|
674
|
+
* },
|
|
675
|
+
* },
|
|
676
|
+
* });
|
|
677
|
+
* ```
|
|
678
|
+
*/
|
|
679
|
+
export function prismaFeedbackStore(
|
|
680
|
+
db: object,
|
|
681
|
+
map: PrismaFeedbackMap,
|
|
682
|
+
): FeedbackStore;
|
|
683
|
+
export function prismaFeedbackStore(
|
|
684
|
+
db: object,
|
|
685
|
+
map?: PrismaFeedbackMap,
|
|
686
|
+
): FeedbackStore {
|
|
687
|
+
const t = feedbackTable(map);
|
|
688
|
+
const d = feedbackDelegate(t, db);
|
|
689
|
+
const ratings = map?.ratings ?? RATINGS;
|
|
690
|
+
const keepsInputs = t.col("input") !== null;
|
|
691
|
+
const hasStatus = t.col("status") !== null;
|
|
692
|
+
const toRecord = (f: Row): FeedbackRecord => {
|
|
693
|
+
return {
|
|
694
|
+
id: str(f.id),
|
|
695
|
+
operation: str(f.operation),
|
|
696
|
+
targetId: str(f.targetId),
|
|
697
|
+
raterId: str(f.raterId),
|
|
698
|
+
rating: f.rating === ratings.DOWN ? "DOWN" : "UP",
|
|
699
|
+
reason: strOrNull(f.reason),
|
|
700
|
+
excerpt: strOrNull(f.excerpt),
|
|
701
|
+
input: f.input ?? null,
|
|
702
|
+
output: f.output ?? null,
|
|
703
|
+
status: oneOf(f.status, FEEDBACK_STATUSES, "OPEN"),
|
|
704
|
+
caseId: strOrNull(f.caseId),
|
|
705
|
+
createdAt: date(f.createdAt),
|
|
706
|
+
updatedAt: date(f.updatedAt),
|
|
707
|
+
...(keepsInputs ? {} : { keepsInputs: false }),
|
|
708
|
+
};
|
|
709
|
+
};
|
|
710
|
+
const record = (raw: unknown) => toRecord(t.in(raw));
|
|
711
|
+
/**
|
|
712
|
+
* Refuses what the table can't keep, before writing anything. Said
|
|
713
|
+
* here rather than by the map, so the error never repeats a person's
|
|
714
|
+
* words or input.
|
|
715
|
+
*/
|
|
716
|
+
const refuse = (c: Required<NewRating>) => {
|
|
717
|
+
for (const field of RATING_TEXT)
|
|
718
|
+
if (c[field] !== null && t.col(field) === null)
|
|
719
|
+
throw new Error(
|
|
720
|
+
`Your feedback table has no column for a rating's ${field} (the map sets ${field} to null), so it can't keep one. Leave ${field} out of the rating, or add a nullable column and map ${field} to it.`,
|
|
721
|
+
);
|
|
722
|
+
};
|
|
723
|
+
|
|
724
|
+
if (!map)
|
|
725
|
+
return {
|
|
726
|
+
async rate(r) {
|
|
727
|
+
const c = cleanRating(r);
|
|
728
|
+
const key = {
|
|
729
|
+
raterId_operation_targetId: {
|
|
730
|
+
raterId: c.raterId,
|
|
731
|
+
operation: c.operation,
|
|
732
|
+
targetId: c.targetId,
|
|
733
|
+
},
|
|
734
|
+
};
|
|
735
|
+
const prior = await d.findUnique({ where: key });
|
|
736
|
+
const data = {
|
|
737
|
+
rating: c.rating,
|
|
738
|
+
reason: c.reason,
|
|
739
|
+
excerpt: c.excerpt,
|
|
740
|
+
input: json(c.input),
|
|
741
|
+
output: json(c.output),
|
|
742
|
+
// A changed rating needs a fresh look.
|
|
743
|
+
...(isRow(prior) && prior.rating !== c.rating
|
|
744
|
+
? { status: "OPEN", caseId: null }
|
|
745
|
+
: {}),
|
|
746
|
+
};
|
|
747
|
+
return record(
|
|
748
|
+
await d.upsert({
|
|
749
|
+
where: key,
|
|
750
|
+
create: {
|
|
751
|
+
...data,
|
|
752
|
+
operation: c.operation,
|
|
753
|
+
targetId: c.targetId,
|
|
754
|
+
raterId: c.raterId,
|
|
755
|
+
},
|
|
756
|
+
update: data,
|
|
757
|
+
}),
|
|
758
|
+
);
|
|
759
|
+
},
|
|
760
|
+
async clearRating(raterId, operation, targetId) {
|
|
761
|
+
await d.deleteMany({ where: { raterId, operation, targetId } });
|
|
762
|
+
},
|
|
763
|
+
async listFeedback(opts = {}) {
|
|
764
|
+
const rows = await d.findMany({
|
|
765
|
+
where: {
|
|
766
|
+
...(opts.operation ? { operation: opts.operation } : {}),
|
|
767
|
+
...(opts.rating ? { rating: opts.rating } : {}),
|
|
768
|
+
...(opts.status ? { status: opts.status } : {}),
|
|
769
|
+
...(opts.since ? { updatedAt: { gte: opts.since } } : {}),
|
|
770
|
+
},
|
|
771
|
+
orderBy: { updatedAt: "desc" },
|
|
772
|
+
...(opts.limit ? { take: opts.limit } : {}),
|
|
773
|
+
});
|
|
774
|
+
return t.ins(rows).map(toRecord);
|
|
775
|
+
},
|
|
776
|
+
async setFeedbackStatus(id, status, caseId = null) {
|
|
777
|
+
await d.updateMany({ where: { id }, data: { status, caseId } });
|
|
778
|
+
},
|
|
779
|
+
};
|
|
780
|
+
|
|
781
|
+
// An app's own table may have no unique key on (rater, feature,
|
|
782
|
+
// answer), or one Prisma names differently, so a rating is found first
|
|
783
|
+
// and then updated or created, rather than upserted.
|
|
784
|
+
const write = async (c: Required<NewRating>) => {
|
|
785
|
+
const who = t.out({
|
|
786
|
+
raterId: c.raterId,
|
|
787
|
+
operation: c.operation,
|
|
788
|
+
targetId: c.targetId,
|
|
789
|
+
});
|
|
790
|
+
const rating = ratings[c.rating];
|
|
791
|
+
const data = (prior: Row | null) =>
|
|
792
|
+
t.out({
|
|
793
|
+
rating,
|
|
794
|
+
reason: c.reason,
|
|
795
|
+
excerpt: c.excerpt,
|
|
796
|
+
input: json(c.input),
|
|
797
|
+
output: json(c.output),
|
|
798
|
+
// A changed rating needs a fresh look.
|
|
799
|
+
...(prior && prior.rating !== rating
|
|
800
|
+
? { ...(hasStatus ? { status: "OPEN" } : {}), caseId: null }
|
|
801
|
+
: {}),
|
|
802
|
+
});
|
|
803
|
+
const update = (prior: Row) =>
|
|
804
|
+
d.update({ where: t.out({ id: prior.id }), data: data(prior) });
|
|
805
|
+
const found = await d.findFirst({ where: who });
|
|
806
|
+
if (found) return update(t.in(found));
|
|
807
|
+
try {
|
|
808
|
+
return await d.create({ data: { ...data(null), ...who } });
|
|
809
|
+
} catch (e) {
|
|
810
|
+
// Another request by the same person got in first (a double click):
|
|
811
|
+
// update the rating it made.
|
|
812
|
+
const again = await d.findFirst({ where: who });
|
|
813
|
+
if (again) return update(t.in(again));
|
|
814
|
+
throw e;
|
|
815
|
+
}
|
|
816
|
+
};
|
|
817
|
+
|
|
818
|
+
return {
|
|
819
|
+
async rate(r) {
|
|
820
|
+
const c = cleanRating(r);
|
|
821
|
+
refuse(c);
|
|
822
|
+
return record(await write(c));
|
|
823
|
+
},
|
|
824
|
+
async clearRating(raterId, operation, targetId) {
|
|
825
|
+
await d.deleteMany({ where: t.out({ raterId, operation, targetId }) });
|
|
826
|
+
},
|
|
827
|
+
async listFeedback(opts = {}) {
|
|
828
|
+
// Without a status column every rating is open, and none is
|
|
829
|
+
// anything else.
|
|
830
|
+
if (opts.status && opts.status !== "OPEN" && !hasStatus) return [];
|
|
831
|
+
const rows = await d.findMany({
|
|
832
|
+
where: t.out({
|
|
833
|
+
...(opts.operation ? { operation: opts.operation } : {}),
|
|
834
|
+
...(opts.rating ? { rating: ratings[opts.rating] } : {}),
|
|
835
|
+
...(opts.status && hasStatus ? { status: opts.status } : {}),
|
|
836
|
+
...(opts.since ? { updatedAt: { gte: opts.since } } : {}),
|
|
837
|
+
}),
|
|
838
|
+
orderBy: t.out({ updatedAt: "desc" }),
|
|
839
|
+
...(opts.limit ? { take: opts.limit } : {}),
|
|
840
|
+
});
|
|
841
|
+
return t.ins(rows).map(toRecord);
|
|
842
|
+
},
|
|
843
|
+
async setFeedbackStatus(id, status, caseId = null) {
|
|
844
|
+
if (!hasStatus)
|
|
845
|
+
throw new Error(
|
|
846
|
+
`Your feedback table has no status column (the map sets status to null), so a rating can't be marked ${status}. Add a text column (default "OPEN") and map status to it.`,
|
|
847
|
+
);
|
|
848
|
+
await d.updateMany({
|
|
849
|
+
where: t.out({ id }),
|
|
850
|
+
data: t.out({ status, caseId }),
|
|
851
|
+
});
|
|
852
|
+
},
|
|
853
|
+
};
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
/** An EvalSettingsVersion row. */
|
|
857
|
+
export type EvalSettingsVersionRow = {
|
|
858
|
+
id: string;
|
|
859
|
+
operation: string;
|
|
860
|
+
number: number;
|
|
861
|
+
model: string;
|
|
862
|
+
fallbackModel: string | null;
|
|
863
|
+
reasoningEffort: string | null;
|
|
864
|
+
maxOutputTokens: number | null;
|
|
865
|
+
temperature: number | null;
|
|
866
|
+
promptVersion: string | null;
|
|
867
|
+
createdAt: Date;
|
|
868
|
+
};
|
|
869
|
+
/** An EvalSettingsLive row. */
|
|
870
|
+
export type EvalSettingsLiveRow = {
|
|
871
|
+
operation: string;
|
|
872
|
+
versionId: string;
|
|
873
|
+
updatedAt: Date;
|
|
874
|
+
};
|
|
875
|
+
/** An EvalSettingsChange row. */
|
|
876
|
+
export type EvalSettingsChangeRow = {
|
|
877
|
+
id: string;
|
|
878
|
+
operation: string;
|
|
879
|
+
kind: string;
|
|
880
|
+
fromVersionId: string | null;
|
|
881
|
+
toVersionId: string;
|
|
882
|
+
reason: string | null;
|
|
883
|
+
by: string | null;
|
|
884
|
+
evidence: Json;
|
|
885
|
+
createdAt: Date;
|
|
886
|
+
};
|
|
887
|
+
|
|
888
|
+
/** The settings tables, inside or outside a transaction. */
|
|
889
|
+
export type PrismaSettingsTables = {
|
|
890
|
+
evalSettingsVersion: Delegate<EvalSettingsVersionRow>;
|
|
891
|
+
evalSettingsLive: Delegate<EvalSettingsLiveRow>;
|
|
892
|
+
evalSettingsChange: Delegate<EvalSettingsChangeRow>;
|
|
893
|
+
};
|
|
894
|
+
|
|
895
|
+
/** The part of a generated Prisma client the settings store uses. */
|
|
896
|
+
export type PrismaSettingsClient = PrismaSettingsTables & {
|
|
897
|
+
$transaction<T>(fn: (tx: PrismaSettingsTables) => Promise<T>): Promise<T>;
|
|
898
|
+
};
|
|
899
|
+
|
|
900
|
+
const KINDS = CHANGE_KINDS;
|
|
901
|
+
|
|
902
|
+
/**
|
|
903
|
+
* A change read from a kind column that can't hold every kind: the real
|
|
904
|
+
* kind and evidence, when `kinds` stored them in the evidence column.
|
|
905
|
+
*/
|
|
906
|
+
function unwrapKind(c: Row): Row {
|
|
907
|
+
const ev = c.evidence;
|
|
908
|
+
if (!isRow(ev) || !("evidence" in ev)) return c;
|
|
909
|
+
const kind = ev.evalkeeperKind;
|
|
910
|
+
if (typeof kind !== "string" || !KINDS.some((k) => k === kind)) return c;
|
|
911
|
+
return { ...c, kind, evidence: ev.evidence };
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
const version = (v: Row): SettingsVersion => ({
|
|
915
|
+
id: str(v.id),
|
|
916
|
+
operation: str(v.operation),
|
|
917
|
+
number: num(v.number),
|
|
918
|
+
model: str(v.model),
|
|
919
|
+
fallbackModel: strOrNull(v.fallbackModel),
|
|
920
|
+
// Any name a provider uses; the catalog says which a model takes.
|
|
921
|
+
reasoningEffort: strOrNull(v.reasoningEffort),
|
|
922
|
+
maxOutputTokens: numOrNull(v.maxOutputTokens),
|
|
923
|
+
temperature: numOrNull(v.temperature),
|
|
924
|
+
promptVersion: strOrNull(v.promptVersion),
|
|
925
|
+
createdAt: date(v.createdAt),
|
|
926
|
+
});
|
|
927
|
+
|
|
928
|
+
const settingsChange = (c: Row, by: string | null): SettingsChange => ({
|
|
929
|
+
id: str(c.id),
|
|
930
|
+
operation: str(c.operation),
|
|
931
|
+
kind: oneOf(c.kind, KINDS, "CHANGE"),
|
|
932
|
+
fromVersionId: strOrNull(c.fromVersionId),
|
|
933
|
+
toVersionId: str(c.toVersionId),
|
|
934
|
+
reason: strOrNull(c.reason),
|
|
935
|
+
by,
|
|
936
|
+
evidence: c.evidence ?? null,
|
|
937
|
+
createdAt: date(c.createdAt),
|
|
938
|
+
});
|
|
939
|
+
|
|
940
|
+
/** Evalkeeper's settings tables, by the names its schema gives them. */
|
|
941
|
+
export type SettingsTable =
|
|
942
|
+
"evalSettingsVersion" | "evalSettingsLive" | "evalSettingsChange";
|
|
943
|
+
|
|
944
|
+
/**
|
|
945
|
+
* How Evalkeeper's settings tables map onto an app's own versions tables,
|
|
946
|
+
* so Evalkeeper's rollback and history pages read the same history as the
|
|
947
|
+
* app's own model admin page. Leave out anything named the same.
|
|
948
|
+
*/
|
|
949
|
+
export type PrismaSettingsMap = {
|
|
950
|
+
/** Each table's model on the Prisma client, as the client spells it. */
|
|
951
|
+
models?: Partial<Record<SettingsTable, string>>;
|
|
952
|
+
/**
|
|
953
|
+
* Column names per table, keyed by Evalkeeper's field name. `null`
|
|
954
|
+
* says the table has no such column (for nullable fields only): reads
|
|
955
|
+
* give null, and saving a version that sets it fails rather than losing
|
|
956
|
+
* the setting.
|
|
957
|
+
*/
|
|
958
|
+
fields?: {
|
|
959
|
+
evalSettingsVersion?: ColumnMap<EvalSettingsVersionRow>;
|
|
960
|
+
evalSettingsLive?: ColumnMap<EvalSettingsLiveRow>;
|
|
961
|
+
evalSettingsChange?: ColumnMap<EvalSettingsChangeRow>;
|
|
962
|
+
};
|
|
963
|
+
/**
|
|
964
|
+
* The kinds your change log's kind column can hold, when it's an enum
|
|
965
|
+
* made before some kinds existed (OUTSIDE and INSTRUCTIONS came later).
|
|
966
|
+
* A kind not listed is stored as CHANGE, with the real kind and the
|
|
967
|
+
* evidence kept in the evidence column (as `{ evalkeeperKind, evidence }`),
|
|
968
|
+
* and read back as itself. Leave it out for a text column or an enum
|
|
969
|
+
* with every kind.
|
|
970
|
+
*/
|
|
971
|
+
kinds?: readonly ChangeKind[];
|
|
972
|
+
/**
|
|
973
|
+
* For a table that keeps who made a change as something other than
|
|
974
|
+
* the `by` your pages pass (a user id with a foreign key, say). Each is
|
|
975
|
+
* called only for a value that isn't null.
|
|
976
|
+
*/
|
|
977
|
+
by?: {
|
|
978
|
+
/** What to store for `by` (e.g. the user id for a name). */
|
|
979
|
+
toColumn(by: string): string | null | Promise<string | null>;
|
|
980
|
+
/** What to show for a stored value (e.g. the name for a user id). */
|
|
981
|
+
fromColumn(stored: string): string | null | Promise<string | null>;
|
|
982
|
+
};
|
|
983
|
+
};
|
|
984
|
+
|
|
985
|
+
/** A Prisma client that runs a function in a transaction, as every one does. */
|
|
986
|
+
export type PrismaTransactionClient = {
|
|
987
|
+
$transaction<T>(fn: (tx: object) => Promise<T>): Promise<T>;
|
|
988
|
+
};
|
|
989
|
+
|
|
990
|
+
/**
|
|
991
|
+
* A SettingsStore on the EvalSettings tables. Each switch runs in one
|
|
992
|
+
* transaction, and moves the live pointer only if it still points at the
|
|
993
|
+
* version the switch was decided against, so concurrent saves can't
|
|
994
|
+
* silently overwrite each other.
|
|
995
|
+
*
|
|
996
|
+
* @param db - Your Prisma client.
|
|
997
|
+
*/
|
|
998
|
+
export function prismaSettingsStore(db: PrismaSettingsClient): SettingsStore;
|
|
999
|
+
/**
|
|
1000
|
+
* A SettingsStore on your own versions tables, through a mapping (see
|
|
1001
|
+
* `PrismaSettingsMap`), so Evalkeeper's pages and your own read the same
|
|
1002
|
+
* history.
|
|
1003
|
+
*
|
|
1004
|
+
* @param db - Your Prisma client.
|
|
1005
|
+
* @param map - Where your tables differ from Evalkeeper's.
|
|
1006
|
+
*
|
|
1007
|
+
* @example
|
|
1008
|
+
* Your own versions tables, with a user id for who changed what:
|
|
1009
|
+
* ```ts
|
|
1010
|
+
* const settingsStore = prismaSettingsStore(prisma, {
|
|
1011
|
+
* models: {
|
|
1012
|
+
* evalSettingsVersion: "operationVersion",
|
|
1013
|
+
* evalSettingsLive: "operationLive",
|
|
1014
|
+
* evalSettingsChange: "operationChange",
|
|
1015
|
+
* },
|
|
1016
|
+
* fields: {
|
|
1017
|
+
* evalSettingsVersion: { temperature: null },
|
|
1018
|
+
* evalSettingsChange: { by: "userId" },
|
|
1019
|
+
* },
|
|
1020
|
+
* by: {
|
|
1021
|
+
* toColumn: (by) => by, // your pages pass the admin's id
|
|
1022
|
+
* fromColumn: async (id) =>
|
|
1023
|
+
* (await prisma.user.findUnique({ where: { id } }))?.name ?? null,
|
|
1024
|
+
* },
|
|
1025
|
+
* });
|
|
1026
|
+
* ```
|
|
1027
|
+
*/
|
|
1028
|
+
export function prismaSettingsStore(
|
|
1029
|
+
db: PrismaTransactionClient,
|
|
1030
|
+
map: PrismaSettingsMap,
|
|
1031
|
+
): SettingsStore;
|
|
1032
|
+
export function prismaSettingsStore(
|
|
1033
|
+
db: PrismaTransactionClient,
|
|
1034
|
+
map: PrismaSettingsMap = {},
|
|
1035
|
+
): SettingsStore {
|
|
1036
|
+
const t = (name: SettingsTable) =>
|
|
1037
|
+
new Table(name, map.models?.[name] ?? name, map.fields?.[name]);
|
|
1038
|
+
const versions = t("evalSettingsVersion");
|
|
1039
|
+
const lives = t("evalSettingsLive");
|
|
1040
|
+
const changes = t("evalSettingsChange");
|
|
1041
|
+
const byOut = async (by: string | null) =>
|
|
1042
|
+
by === null || !map.by ? by : map.by.toColumn(by);
|
|
1043
|
+
const byIn = async (stored: unknown) => {
|
|
1044
|
+
const s = strOrNull(stored);
|
|
1045
|
+
return s === null || !map.by ? s : map.by.fromColumn(s);
|
|
1046
|
+
};
|
|
1047
|
+
return {
|
|
1048
|
+
async liveVersion(operation) {
|
|
1049
|
+
const live = await lives
|
|
1050
|
+
.on(db)
|
|
1051
|
+
.findUnique({ where: lives.out({ operation }) });
|
|
1052
|
+
if (!live) return null;
|
|
1053
|
+
const v = await versions
|
|
1054
|
+
.on(db)
|
|
1055
|
+
.findUnique({ where: versions.out({ id: lives.in(live).versionId }) });
|
|
1056
|
+
return v ? version(versions.in(v)) : null;
|
|
1057
|
+
},
|
|
1058
|
+
async listVersions(operation) {
|
|
1059
|
+
return versions
|
|
1060
|
+
.ins(
|
|
1061
|
+
await versions.on(db).findMany({
|
|
1062
|
+
where: versions.out({ operation }),
|
|
1063
|
+
orderBy: versions.out({ number: "desc" }),
|
|
1064
|
+
}),
|
|
1065
|
+
)
|
|
1066
|
+
.map(version);
|
|
1067
|
+
},
|
|
1068
|
+
async listChanges(operation, opts = {}) {
|
|
1069
|
+
const rows = changes.ins(
|
|
1070
|
+
await changes.on(db).findMany({
|
|
1071
|
+
where: changes.out({ operation }),
|
|
1072
|
+
orderBy: [
|
|
1073
|
+
changes.out({ createdAt: "desc" }),
|
|
1074
|
+
changes.out({ id: "desc" }),
|
|
1075
|
+
],
|
|
1076
|
+
...(opts.limit ? { take: opts.limit } : {}),
|
|
1077
|
+
}),
|
|
1078
|
+
);
|
|
1079
|
+
// One lookup per person, not per change.
|
|
1080
|
+
const names = new Map<unknown, Promise<string | null>>();
|
|
1081
|
+
for (const r of rows) if (!names.has(r.by)) names.set(r.by, byIn(r.by));
|
|
1082
|
+
return Promise.all(
|
|
1083
|
+
rows.map(async (r) =>
|
|
1084
|
+
settingsChange(
|
|
1085
|
+
map.kinds ? unwrapKind(r) : r,
|
|
1086
|
+
(await names.get(r.by)) ?? null,
|
|
1087
|
+
),
|
|
1088
|
+
),
|
|
1089
|
+
);
|
|
1090
|
+
},
|
|
1091
|
+
async switchTo(operation, sw) {
|
|
1092
|
+
const by = await byOut(sw.by);
|
|
1093
|
+
try {
|
|
1094
|
+
return await db.$transaction(async (tx) => {
|
|
1095
|
+
let to: Row | null;
|
|
1096
|
+
if (sw.create) {
|
|
1097
|
+
const last = await versions.on(tx).findFirst({
|
|
1098
|
+
where: versions.out({ operation }),
|
|
1099
|
+
orderBy: versions.out({ number: "desc" }),
|
|
1100
|
+
});
|
|
1101
|
+
to = versions.in(
|
|
1102
|
+
await versions.on(tx).create({
|
|
1103
|
+
data: versions.out({
|
|
1104
|
+
...sw.create.settings,
|
|
1105
|
+
operation,
|
|
1106
|
+
number: (last ? num(versions.in(last).number) : 0) + 1,
|
|
1107
|
+
promptVersion: sw.create.promptVersion,
|
|
1108
|
+
}),
|
|
1109
|
+
}),
|
|
1110
|
+
);
|
|
1111
|
+
} else {
|
|
1112
|
+
const found = await versions.on(tx).findFirst({
|
|
1113
|
+
where: versions.out({ id: sw.versionId, operation }),
|
|
1114
|
+
});
|
|
1115
|
+
if (!found) throw new Error("No such version for this feature");
|
|
1116
|
+
to = versions.in(found);
|
|
1117
|
+
}
|
|
1118
|
+
if (sw.expectLive === null) {
|
|
1119
|
+
// Fails on the primary key if another switch got in first.
|
|
1120
|
+
await lives.on(tx).create({
|
|
1121
|
+
data: lives.out({ operation, versionId: to.id }),
|
|
1122
|
+
});
|
|
1123
|
+
} else {
|
|
1124
|
+
const moved = await lives.on(tx).updateMany({
|
|
1125
|
+
where: lives.out({ operation, versionId: sw.expectLive }),
|
|
1126
|
+
data: lives.out({ versionId: to.id }),
|
|
1127
|
+
});
|
|
1128
|
+
if (!isRow(moved) || moved.count !== 1)
|
|
1129
|
+
throw new SettingsConflict(operation);
|
|
1130
|
+
}
|
|
1131
|
+
const holds = !map.kinds || map.kinds.includes(sw.kind);
|
|
1132
|
+
await changes.on(tx).create({
|
|
1133
|
+
data: changes.out({
|
|
1134
|
+
operation,
|
|
1135
|
+
kind: holds ? sw.kind : "CHANGE",
|
|
1136
|
+
fromVersionId: sw.expectLive,
|
|
1137
|
+
toVersionId: to.id,
|
|
1138
|
+
reason: sw.reason,
|
|
1139
|
+
by,
|
|
1140
|
+
evidence: holds
|
|
1141
|
+
? json(sw.evidence)
|
|
1142
|
+
: { evalkeeperKind: sw.kind, evidence: sw.evidence ?? null },
|
|
1143
|
+
}),
|
|
1144
|
+
});
|
|
1145
|
+
return version(to);
|
|
1146
|
+
});
|
|
1147
|
+
} catch (e) {
|
|
1148
|
+
if (e instanceof SettingsConflict) throw e;
|
|
1149
|
+
if (e instanceof Error && e.message.startsWith("No such version"))
|
|
1150
|
+
throw e;
|
|
1151
|
+
// A unique-key clash: another switch numbered or went live first.
|
|
1152
|
+
if (e instanceof Error && /Unique constraint|P2002/.test(e.message))
|
|
1153
|
+
throw new SettingsConflict(operation);
|
|
1154
|
+
// An enum column made before this kind existed.
|
|
1155
|
+
const col = String(changes.col("kind"));
|
|
1156
|
+
if (
|
|
1157
|
+
e instanceof Error &&
|
|
1158
|
+
e.message.includes(`Invalid value for argument \`${col}\``)
|
|
1159
|
+
)
|
|
1160
|
+
throw new Error(
|
|
1161
|
+
`Your settings change log's ${col} column can't hold ${sw.kind}. Add ${sw.kind} to its enum, or list the kinds it holds in the map's \`kinds\` (see PrismaSettingsMap).`,
|
|
1162
|
+
{ cause: e },
|
|
1163
|
+
);
|
|
1164
|
+
throw e;
|
|
1165
|
+
}
|
|
1166
|
+
},
|
|
1167
|
+
};
|
|
1168
|
+
}
|
|
1169
|
+
export * from "./moveIn.js";
|