abidlabs HF Staff commited on
Commit
3e72ea6
·
verified ·
1 Parent(s): ee3107e

Deploy ML Intern Slack worker (5)

Browse files
Files changed (34) hide show
  1. src/lib/utils/review/anchors.spec.ts +100 -0
  2. src/lib/utils/review/anchors.ts +80 -0
  3. src/lib/utils/review/answers.spec.ts +110 -0
  4. src/lib/utils/review/answers.ts +103 -0
  5. src/lib/utils/review/hash.spec.ts +36 -0
  6. src/lib/utils/review/hash.ts +25 -0
  7. src/lib/utils/review/json.spec.ts +51 -0
  8. src/lib/utils/review/json.ts +70 -0
  9. src/lib/utils/review/progress.spec.ts +146 -0
  10. src/lib/utils/review/progress.ts +193 -0
  11. src/lib/utils/review/records.spec.ts +124 -0
  12. src/lib/utils/review/records.ts +176 -0
  13. src/lib/utils/review/schema.spec.ts +127 -0
  14. src/lib/utils/review/schema.ts +254 -0
  15. src/lib/utils/review/view.spec.ts +139 -0
  16. src/lib/utils/review/view.ts +191 -0
  17. src/lib/utils/sendElicitationAnswer.ts +1 -1
  18. src/lib/utils/slackRequest.spec.ts +25 -0
  19. src/lib/utils/slackRequest.ts +15 -0
  20. src/routes/api/conversations/+server.ts +2 -0
  21. src/routes/api/v2/conversations/+server.ts +2 -0
  22. src/routes/api/v2/conversations/[id]/+server.ts +7 -6
  23. src/routes/api/v2/conversations/[id]/export/+server.ts +3 -5
  24. src/routes/api/v2/conversations/[id]/files/[...name]/+server.ts +3 -5
  25. src/routes/api/v2/conversations/[id]/message/[messageId]/+server.ts +2 -2
  26. src/routes/api/v2/conversations/[id]/registry/+server.ts +3 -5
  27. src/routes/api/v2/conversations/[id]/registry/[serviceId]/stop/+server.ts +3 -4
  28. src/routes/api/v2/conversations/[id]/reviews/[reviewId]/+server.ts +23 -0
  29. src/routes/api/v2/conversations/[id]/reviews/[reviewId]/media/[index]/[field]/+server.ts +61 -0
  30. src/routes/api/v2/conversations/[id]/reviews/[reviewId]/status/+server.ts +8 -0
  31. src/routes/api/v2/conversations/[id]/reviews/[reviewId]/submit/+server.ts +14 -0
  32. src/routes/api/v2/conversations/[id]/runs/[runId]/+server.ts +3 -4
  33. src/routes/conversation/[id]/+server.ts +2 -0
  34. src/styles/main.css +12 -0
src/lib/utils/review/anchors.spec.ts ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, it } from "vitest";
2
+ import type { ReviewComment } from "$lib/types/MlReview";
3
+ import {
4
+ anchorLabel,
5
+ areaBetween,
6
+ formatClock,
7
+ formatTime,
8
+ pinNumbers,
9
+ timeCovers,
10
+ } from "./anchors";
11
+
12
+ const comment = (id: string, extra: Partial<ReviewComment> = {}): ReviewComment => ({
13
+ id,
14
+ body: "x",
15
+ createdAt: new Date(0),
16
+ ...extra,
17
+ });
18
+
19
+ describe("formatClock", () => {
20
+ it("prints m:ss under an hour and h:mm:ss above", () => {
21
+ expect(formatClock(12.7)).toBe("0:12");
22
+ expect(formatClock(75)).toBe("1:15");
23
+ expect(formatClock(3723)).toBe("1:02:03");
24
+ });
25
+
26
+ it("adds tenths for the player clock", () => {
27
+ expect(formatClock(8.14, { tenths: true })).toBe("0:08.1");
28
+ });
29
+
30
+ it("treats a missing duration as zero", () => {
31
+ expect(formatClock(Number.NaN)).toBe("0:00");
32
+ });
33
+ });
34
+
35
+ describe("anchorLabel", () => {
36
+ it("matches the result text the server writes", () => {
37
+ expect(anchorLabel({ area: { x: 0.31, y: 0.06, w: 0.49, h: 0.12 } })).toBe(
38
+ "area 31%,6% 49%x12%"
39
+ );
40
+ expect(anchorLabel({ time: { start: 12, end: 18 } })).toBe("0:12-0:18");
41
+ expect(anchorLabel({ time: { start: 12 } })).toBe("0:12");
42
+ expect(anchorLabel({ time: { start: 12 }, area: { x: 0.31, y: 0.06, w: 0.49, h: 0.12 } })).toBe(
43
+ "0:12 area 31%,6% 49%x12%"
44
+ );
45
+ expect(anchorLabel({})).toBeUndefined();
46
+ });
47
+
48
+ it("prints a range", () => {
49
+ expect(formatTime({ start: 61, end: 3601 })).toBe("1:01-1:00:01");
50
+ });
51
+ });
52
+
53
+ describe("areaBetween", () => {
54
+ it("spans two corners in either direction and stays inside the stage", () => {
55
+ expect(areaBetween({ x: 0.8, y: 0.5 }, { x: 0.2, y: 1.4 })).toEqual({
56
+ x: 0.2,
57
+ y: 0.5,
58
+ w: 0.6,
59
+ h: 0.5,
60
+ });
61
+ });
62
+
63
+ it("rounds to 4 decimals", () => {
64
+ expect(areaBetween({ x: 0.123456, y: 0 }, { x: 0.5, y: 0.333333 })).toEqual({
65
+ x: 0.1235,
66
+ y: 0,
67
+ w: 0.3765,
68
+ h: 0.3333,
69
+ });
70
+ });
71
+
72
+ it("never reaches past the edge once rounded", () => {
73
+ const area = areaBetween({ x: 0.12345, y: 0.00005 }, { x: 1, y: 1 });
74
+ expect(area.x + area.w).toBeLessThanOrEqual(1);
75
+ expect(area.y + area.h).toBeLessThanOrEqual(1);
76
+ });
77
+ });
78
+
79
+ describe("timeCovers", () => {
80
+ it("holds a moment briefly and a range throughout", () => {
81
+ expect(timeCovers({ start: 2 }, 2.1)).toBe(true);
82
+ expect(timeCovers({ start: 2 }, 3)).toBe(false);
83
+ expect(timeCovers({ start: 2, end: 4 }, 3)).toBe(true);
84
+ expect(timeCovers(undefined, 99)).toBe(true);
85
+ });
86
+ });
87
+
88
+ describe("pinNumbers", () => {
89
+ it("numbers only anchored comments, in order", () => {
90
+ const numbers = pinNumbers([
91
+ comment("a", { time: { start: 1 } }),
92
+ comment("b"),
93
+ comment("c", { area: { x: 0, y: 0, w: 0.5, h: 0.5 } }),
94
+ ]);
95
+ expect([...numbers]).toEqual([
96
+ ["a", 1],
97
+ ["c", 2],
98
+ ]);
99
+ });
100
+ });
src/lib/utils/review/anchors.ts ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { ReviewArea, ReviewComment, ReviewField, ReviewTime } from "$lib/types/MlReview";
2
+
3
+ /** the server keeps 4 decimals, rounding here keeps a resave from changing the draft */
4
+ const AREA_DECIMALS = 4;
5
+ /** a drag smaller than this, as a fraction of the stage, is a click */
6
+ export const MIN_AREA = 0.01;
7
+
8
+ export type MediaFieldType = "audio" | "video";
9
+
10
+ export const isMediaField = (field: ReviewField): field is ReviewField & { type: MediaFieldType } =>
11
+ field.type === "audio" || field.type === "video";
12
+
13
+ const round = (n: number, decimals: number) => {
14
+ const scale = 10 ** decimals;
15
+ return Math.round(n * scale) / scale;
16
+ };
17
+
18
+ const clamp01 = (n: number) => Math.min(1, Math.max(0, n));
19
+
20
+ /** the box two corners span, in either drag direction, kept inside the stage */
21
+ export function areaBetween(a: { x: number; y: number }, b: { x: number; y: number }): ReviewArea {
22
+ const edge = (n: number) => round(clamp01(n), AREA_DECIMALS);
23
+ const x = edge(Math.min(a.x, b.x));
24
+ const y = edge(Math.min(a.y, b.y));
25
+ // widths from rounded edges, rounding x and w apart can push x + w past 1 and the server refuses it
26
+ return {
27
+ x,
28
+ y,
29
+ w: round(edge(Math.max(a.x, b.x)) - x, AREA_DECIMALS),
30
+ h: round(edge(Math.max(a.y, b.y)) - y, AREA_DECIMALS),
31
+ };
32
+ }
33
+
34
+ export const timeAt = (seconds: number) => round(Math.max(0, seconds), 2);
35
+
36
+ /** m:ss under an hour and h:mm:ss above, tenths for the player clock */
37
+ export function formatClock(seconds: number, { tenths = false } = {}): string {
38
+ const total = Math.max(0, Number.isFinite(seconds) ? seconds : 0);
39
+ const whole = Math.floor(total);
40
+ const h = Math.floor(whole / 3600);
41
+ const m = Math.floor((whole % 3600) / 60);
42
+ const s = whole % 60;
43
+ const ss = String(s).padStart(2, "0");
44
+ const clock = h > 0 ? `${h}:${String(m).padStart(2, "0")}:${ss}` : `${m}:${ss}`;
45
+ return tenths ? `${clock}.${Math.floor((total - whole) * 10)}` : clock;
46
+ }
47
+
48
+ export const formatTime = (time: ReviewTime): string =>
49
+ time.end === undefined
50
+ ? formatClock(time.start)
51
+ : `${formatClock(time.start)}-${formatClock(time.end)}`;
52
+
53
+ const percent = (n: number) => `${Math.round(n * 100)}%`;
54
+
55
+ export const formatArea = (area: ReviewArea): string =>
56
+ `${percent(area.x)},${percent(area.y)} ${percent(area.w)}x${percent(area.h)}`;
57
+
58
+ /** the anchor as the result text prints it, so the pane and the agent read the same */
59
+ export function anchorLabel(comment: Pick<ReviewComment, "area" | "time">): string | undefined {
60
+ const parts = [
61
+ comment.time && formatTime(comment.time),
62
+ comment.area && `area ${formatArea(comment.area)}`,
63
+ ].filter(Boolean);
64
+ return parts.length > 0 ? parts.join(" ") : undefined;
65
+ }
66
+
67
+ /** a frame box shows while the clip sits at its moment or inside its range */
68
+ export function timeCovers(time: ReviewTime | undefined, at: number, slack = 0.25): boolean {
69
+ if (!time) return true;
70
+ return at >= time.start - slack && at <= (time.end ?? time.start) + slack;
71
+ }
72
+
73
+ /** numbered in the order they were left, the pins on the stage use the same numbers */
74
+ export function pinNumbers(comments: readonly ReviewComment[]): Map<string, number> {
75
+ const numbers = new Map<string, number>();
76
+ for (const comment of comments) {
77
+ if (comment.area || comment.time) numbers.set(comment.id, numbers.size + 1);
78
+ }
79
+ return numbers;
80
+ }
src/lib/utils/review/answers.spec.ts ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, it } from "vitest";
2
+ import type { ReviewDraft, ReviewQuestion } from "$lib/types/MlReview";
3
+ import { blockers, invalidAnswers, recordStatus, validateAnswer } from "./answers";
4
+
5
+ const label: ReviewQuestion = {
6
+ name: "kind",
7
+ type: "label",
8
+ required: true,
9
+ options: [{ value: "bug" }, { value: "feature" }],
10
+ };
11
+ const multi: ReviewQuestion = {
12
+ name: "areas",
13
+ type: "multi_label",
14
+ required: false,
15
+ options: [{ value: "ui" }, { value: "api" }, { value: "docs" }],
16
+ min: 1,
17
+ max: 2,
18
+ };
19
+ const rating: ReviewQuestion = { name: "sev", type: "rating", required: false, min: 1, max: 5 };
20
+ const yesNo: ReviewQuestion = { name: "dup", type: "boolean", required: false };
21
+ const text: ReviewQuestion = { name: "note", type: "text", required: false, maxLength: 5 };
22
+
23
+ const draft = (answers: ReviewDraft["answers"], status: ReviewDraft["status"] = "answered") => ({
24
+ status,
25
+ answers,
26
+ comments: [],
27
+ updatedAt: new Date(),
28
+ });
29
+
30
+ describe("validateAnswer", () => {
31
+ it("checks each question type", () => {
32
+ expect(validateAnswer(label, "bug")).toBeUndefined();
33
+ expect(validateAnswer(label, "nope")).toBe('"nope" is not an option (bug, feature)');
34
+ expect(validateAnswer(label, ["bug"])).toBe("must be one option value");
35
+
36
+ expect(validateAnswer(multi, ["ui", "api"])).toBeUndefined();
37
+ expect(validateAnswer(multi, ["ui", "api", "docs"])).toBe("pick at most 2");
38
+ expect(validateAnswer(multi, [])).toBe("pick at least 1");
39
+ expect(validateAnswer(multi, ["ui", "ui"])).toBe("lists an option twice");
40
+ expect(validateAnswer(multi, "ui")).toBe("must be a list of option values");
41
+
42
+ expect(validateAnswer(rating, 3)).toBeUndefined();
43
+ expect(validateAnswer(rating, 6)).toBe("must be between 1 and 5");
44
+ expect(validateAnswer(rating, 2.5)).toBe("must be a whole number");
45
+
46
+ expect(validateAnswer(yesNo, false)).toBeUndefined();
47
+ expect(validateAnswer(yesNo, "yes")).toBe("must be true or false");
48
+
49
+ expect(validateAnswer(text, "short")).toBeUndefined();
50
+ expect(validateAnswer(text, "too long")).toBe("is 8 characters, over 5");
51
+ });
52
+ });
53
+
54
+ describe("blockers", () => {
55
+ const schema = { questions: [label, rating] };
56
+
57
+ it("blocks a missing required answer on every record not skipped or discarded", () => {
58
+ expect(
59
+ blockers(
60
+ schema,
61
+ {
62
+ "0": draft({ kind: "bug" }),
63
+ "1": draft({}, "skipped"),
64
+ "2": draft({}, "discarded"),
65
+ "3": draft({ kind: "" }),
66
+ },
67
+ 5
68
+ )
69
+ ).toEqual([
70
+ { record: 3, question: "kind", reason: "required" },
71
+ { record: 4, question: "kind", reason: "required" },
72
+ ]);
73
+ });
74
+
75
+ it("blocks an invalid answer even where nothing is required, skipped or not", () => {
76
+ expect(
77
+ blockers(
78
+ schema,
79
+ { "0": draft({ kind: "bug", sev: 9 }), "1": draft({ sev: 0 }, "skipped") },
80
+ 2
81
+ )
82
+ ).toEqual([
83
+ { record: 0, question: "sev", reason: "must be between 1 and 5" },
84
+ { record: 1, question: "sev", reason: "must be between 1 and 5" },
85
+ ]);
86
+ });
87
+
88
+ it("lets a review with no required question through untouched", () => {
89
+ expect(blockers({ questions: [rating] }, {}, 50)).toEqual([]);
90
+ });
91
+ });
92
+
93
+ describe("partial submits", () => {
94
+ const schema = { questions: [label, rating] };
95
+
96
+ it("keeps only answers that are there and wrong", () => {
97
+ expect(
98
+ invalidAnswers(schema, { "0": draft({ kind: "bug", sev: 9 }), "1": draft({}) }, 3)
99
+ ).toEqual([{ record: 0, question: "sev", reason: "must be between 1 and 5" }]);
100
+ });
101
+
102
+ it("counts an answered record missing a required answer as pending", () => {
103
+ expect(recordStatus(schema, draft({ kind: "bug" }))).toBe("answered");
104
+ expect(recordStatus(schema, draft({ sev: 3 }))).toBe("pending");
105
+ expect(recordStatus(schema, draft({}, "skipped"))).toBe("skipped");
106
+ expect(recordStatus(schema, draft({}, "discarded"))).toBe("discarded");
107
+ expect(recordStatus(schema, undefined)).toBe("pending");
108
+ expect(recordStatus({ questions: [] }, draft({}))).toBe("answered");
109
+ });
110
+ });
src/lib/utils/review/answers.ts ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type {
2
+ ReviewDraft,
3
+ ReviewDraftStatus,
4
+ ReviewQuestion,
5
+ ReviewSchema,
6
+ } from "$lib/types/MlReview";
7
+
8
+ const optionValues = (question: { options: { value: string }[] }) =>
9
+ question.options.map((option) => option.value);
10
+
11
+ export function validateAnswer(question: ReviewQuestion, value: unknown): string | undefined {
12
+ switch (question.type) {
13
+ case "label":
14
+ if (typeof value !== "string") return "must be one option value";
15
+ return optionValues(question).includes(value)
16
+ ? undefined
17
+ : `"${value}" is not an option (${optionValues(question).join(", ")})`;
18
+ case "multi_label": {
19
+ if (!Array.isArray(value) || value.some((v) => typeof v !== "string")) {
20
+ return "must be a list of option values";
21
+ }
22
+ const allowed = optionValues(question);
23
+ const unknown = value.find((v) => !allowed.includes(v as string));
24
+ if (unknown !== undefined) return `"${unknown}" is not an option (${allowed.join(", ")})`;
25
+ if (new Set(value).size !== value.length) return "lists an option twice";
26
+ if (question.min !== undefined && value.length < question.min) {
27
+ return `pick at least ${question.min}`;
28
+ }
29
+ if (question.max !== undefined && value.length > question.max) {
30
+ return `pick at most ${question.max}`;
31
+ }
32
+ return undefined;
33
+ }
34
+ case "rating":
35
+ if (typeof value !== "number" || !Number.isInteger(value)) return "must be a whole number";
36
+ return value < question.min || value > question.max
37
+ ? `must be between ${question.min} and ${question.max}`
38
+ : undefined;
39
+ case "boolean":
40
+ return typeof value === "boolean" ? undefined : "must be true or false";
41
+ case "text":
42
+ if (typeof value !== "string") return "must be text";
43
+ return value.length > question.maxLength
44
+ ? `is ${value.length} characters, over ${question.maxLength}`
45
+ : undefined;
46
+ }
47
+ }
48
+
49
+ /** an empty text or an empty pick is no answer, so a required question stays open on it */
50
+ export function isAnswered(value: unknown): boolean {
51
+ if (value === undefined || value === null) return false;
52
+ if (typeof value === "string") return value.trim().length > 0;
53
+ if (Array.isArray(value)) return value.length > 0;
54
+ return true;
55
+ }
56
+
57
+ export interface ReviewBlocker {
58
+ /** record position, 0 based */
59
+ record: number;
60
+ question: string;
61
+ reason: string;
62
+ }
63
+
64
+ export function blockers(
65
+ schema: Pick<ReviewSchema, "questions">,
66
+ drafts: Record<string, ReviewDraft | undefined>,
67
+ recordCount: number
68
+ ): ReviewBlocker[] {
69
+ const found: ReviewBlocker[] = [];
70
+ for (let record = 0; record < recordCount; record += 1) {
71
+ const draft = drafts[String(record)];
72
+ const settled = draft?.status === "skipped" || draft?.status === "discarded";
73
+ for (const question of schema.questions) {
74
+ const value = draft?.answers?.[question.name];
75
+ if (!isAnswered(value)) {
76
+ if (question.required && !settled) {
77
+ found.push({ record, question: question.name, reason: "required" });
78
+ }
79
+ continue;
80
+ }
81
+ const problem = validateAnswer(question, value);
82
+ if (problem) found.push({ record, question: question.name, reason: problem });
83
+ }
84
+ }
85
+ return found;
86
+ }
87
+
88
+ /** a submit may leave records unfinished, so only an answer that is there and wrong refuses it */
89
+ export const invalidAnswers = (...args: Parameters<typeof blockers>): ReviewBlocker[] =>
90
+ blockers(...args).filter((found) => found.reason !== "required");
91
+
92
+ /** an answered record missing a required answer is not finished, whatever status was stored */
93
+ export function recordStatus(
94
+ schema: Pick<ReviewSchema, "questions">,
95
+ draft: ReviewDraft | undefined
96
+ ): ReviewDraftStatus {
97
+ const status = draft?.status ?? "pending";
98
+ if (status !== "answered") return status;
99
+ const missing = schema.questions.some(
100
+ (question) => question.required && !isAnswered(draft?.answers?.[question.name])
101
+ );
102
+ return missing ? "pending" : status;
103
+ }
src/lib/utils/review/hash.spec.ts ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, it } from "vitest";
2
+ import type { ReviewSchema } from "$lib/types/MlReview";
3
+ import { reviewSchemaHash } from "./hash";
4
+
5
+ const schema: Pick<ReviewSchema, "fields" | "questions"> = {
6
+ fields: [{ name: "body", type: "text", title: "Body" }],
7
+ questions: [
8
+ { name: "kind", type: "label", required: true, options: [{ value: "a" }, { value: "b" }] },
9
+ ],
10
+ };
11
+
12
+ describe("reviewSchemaHash", () => {
13
+ it("is 8 hex characters and stable under key order", async () => {
14
+ const hash = await reviewSchemaHash(schema);
15
+ expect(hash).toMatch(/^[0-9a-f]{8}$/);
16
+ const reordered = {
17
+ questions: [
18
+ { options: [{ value: "a" }, { value: "b" }], required: true, type: "label", name: "kind" },
19
+ ],
20
+ fields: [{ title: "Body", type: "text", name: "body" }],
21
+ } as unknown as typeof schema;
22
+ expect(await reviewSchemaHash(reordered)).toBe(hash);
23
+ });
24
+
25
+ it("changes when the schema changes, including option order", async () => {
26
+ const hash = await reviewSchemaHash(schema);
27
+ const swapped = {
28
+ ...schema,
29
+ questions: [{ ...schema.questions[0], options: [{ value: "b" }, { value: "a" }] }],
30
+ } as typeof schema;
31
+ expect(await reviewSchemaHash(swapped)).not.toBe(hash);
32
+ expect(
33
+ await reviewSchemaHash({ ...schema, fields: [{ name: "body", type: "markdown" }] })
34
+ ).not.toBe(hash);
35
+ });
36
+ });
src/lib/utils/review/hash.ts ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type { ReviewSchema } from "$lib/types/MlReview";
2
+
3
+ function canonical(value: unknown): string {
4
+ if (Array.isArray(value)) return `[${value.map(canonical).join(",")}]`;
5
+ if (value !== null && typeof value === "object") {
6
+ const entries = Object.entries(value as Record<string, unknown>)
7
+ .filter(([, v]) => v !== undefined)
8
+ .sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
9
+ return `{${entries.map(([k, v]) => `${JSON.stringify(k)}:${canonical(v)}`).join(",")}}`;
10
+ }
11
+ return JSON.stringify(value);
12
+ }
13
+
14
+ /** stamps every result, so answers under one schema never mix silently with another schema */
15
+ export async function reviewSchemaHash(
16
+ schema: Pick<ReviewSchema, "fields" | "questions">
17
+ ): Promise<string> {
18
+ const bytes = new TextEncoder().encode(
19
+ canonical({ fields: schema.fields, questions: schema.questions })
20
+ );
21
+ const digest = await globalThis.crypto.subtle.digest("SHA-256", bytes);
22
+ return Array.from(new Uint8Array(digest).slice(0, 4), (b) =>
23
+ b.toString(16).padStart(2, "0")
24
+ ).join("");
25
+ }
src/lib/utils/review/json.spec.ts ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, it } from "vitest";
2
+ import { isFlatObject, parseJsonText, unpackJson } from "./json";
3
+
4
+ describe("parseJsonText", () => {
5
+ it("parses an object or array and nothing else", () => {
6
+ expect(parseJsonText('{"a": 1}')).toEqual({ a: 1 });
7
+ expect(parseJsonText(" [1, 2]")).toEqual([1, 2]);
8
+ expect(parseJsonText('"just a string"')).toBeUndefined();
9
+ expect(parseJsonText("42")).toBeUndefined();
10
+ expect(parseJsonText("{not json")).toBeUndefined();
11
+ expect(parseJsonText("plain words")).toBeUndefined();
12
+ });
13
+
14
+ it("leaves a huge cell as text", () => {
15
+ expect(parseJsonText(`[${"1,".repeat(60_000)}1]`)).toBeUndefined();
16
+ });
17
+ });
18
+
19
+ describe("unpackJson", () => {
20
+ it("opens a string holding JSON, and JSON nested in its strings", () => {
21
+ const inner = JSON.stringify({ true: "At least one span.", false: "No span." });
22
+ const outer = JSON.stringify({ criteria: inner, n: 2 });
23
+ expect(unpackJson(outer)).toEqual({
24
+ criteria: { true: "At least one span.", false: "No span." },
25
+ n: 2,
26
+ });
27
+ });
28
+
29
+ it("keeps strings that are not JSON and does not change the input", () => {
30
+ const value = { a: "{nope", b: ["x"] };
31
+ expect(unpackJson(value)).toEqual(value);
32
+ expect(value).toEqual({ a: "{nope", b: ["x"] });
33
+ });
34
+
35
+ it("stops opening strings past the depth cap", () => {
36
+ let value = JSON.stringify({ leaf: true });
37
+ for (let i = 0; i < 10; i += 1) value = JSON.stringify({ next: value });
38
+ const text = JSON.stringify(unpackJson(value));
39
+ expect(text).toContain('\\"next\\"');
40
+ });
41
+ });
42
+
43
+ describe("isFlatObject", () => {
44
+ it("is true for a few short scalars only", () => {
45
+ expect(isFlatObject({ true: "yes", false: "no" })).toBe(true);
46
+ expect(isFlatObject({ a: { b: 1 } })).toBe(false);
47
+ expect(isFlatObject({ a: "x".repeat(300) })).toBe(false);
48
+ expect(isFlatObject([1, 2])).toBe(false);
49
+ expect(isFlatObject({})).toBe(false);
50
+ });
51
+ });
src/lib/utils/review/json.ts ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /** past these a cell renders as it is stored, so one huge value can not hang the pane */
2
+ const MAX_DEPTH = 6;
3
+ const MAX_PARSE_CHARS = 100_000;
4
+ const MAX_NODES = 2_000;
5
+
6
+ const looksLikeJson = (text: string) => {
7
+ const start = text.trimStart()[0];
8
+ return start === "{" || start === "[";
9
+ };
10
+
11
+ /** an object or array the string serializes, undefined for anything else */
12
+ export function parseJsonText(text: string): object | undefined {
13
+ if (text.length > MAX_PARSE_CHARS || !looksLikeJson(text)) return undefined;
14
+ try {
15
+ const parsed: unknown = JSON.parse(text);
16
+ return parsed !== null && typeof parsed === "object" ? parsed : undefined;
17
+ } catch {
18
+ return undefined;
19
+ }
20
+ }
21
+
22
+ /**
23
+ * strings holding serialized JSON become the value they hold, at any depth, for display only,
24
+ * the stored value is never replaced
25
+ */
26
+ export function unpackJson(value: unknown): unknown {
27
+ let nodes = 0;
28
+ const walk = (node: unknown, depth: number): unknown => {
29
+ nodes += 1;
30
+ if (nodes > MAX_NODES || depth > MAX_DEPTH) return node;
31
+ if (typeof node === "string") {
32
+ const parsed = parseJsonText(node);
33
+ return parsed === undefined ? node : walk(parsed, depth + 1);
34
+ }
35
+ if (Array.isArray(node)) return node.map((item) => walk(item, depth + 1));
36
+ if (node !== null && typeof node === "object") {
37
+ return Object.fromEntries(
38
+ Object.entries(node).map(([key, item]) => [key, walk(item, depth + 1)])
39
+ );
40
+ }
41
+ return node;
42
+ };
43
+ return walk(value, 0);
44
+ }
45
+
46
+ export const isContainer = (value: unknown): value is object =>
47
+ value !== null && typeof value === "object";
48
+
49
+ const FLAT_KEYS = 8;
50
+ const FLAT_CHARS = 200;
51
+
52
+ /** a few short scalar entries, shown as a key and value list rather than a tree */
53
+ export function isFlatObject(value: unknown): value is Record<string, unknown> {
54
+ if (!isContainer(value) || Array.isArray(value)) return false;
55
+ const entries = Object.entries(value);
56
+ return (
57
+ entries.length > 0 &&
58
+ entries.length <= FLAT_KEYS &&
59
+ entries.every(([, item]) => !isContainer(item) && String(item).length <= FLAT_CHARS)
60
+ );
61
+ }
62
+
63
+ export function jsonSummary(value: unknown): string {
64
+ if (Array.isArray(value)) return `${value.length} item${value.length === 1 ? "" : "s"}`;
65
+ if (isContainer(value)) {
66
+ const keys = Object.keys(value);
67
+ return `${keys.length} key${keys.length === 1 ? "" : "s"}: ${keys.slice(0, 4).join(", ")}${keys.length > 4 ? ", …" : ""}`;
68
+ }
69
+ return JSON.stringify(value) ?? String(value);
70
+ }
src/lib/utils/review/progress.spec.ts ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, it } from "vitest";
2
+ import type { ReviewDraft, ReviewRecord, ReviewSchema } from "$lib/types/MlReview";
3
+ import {
4
+ canDecide,
5
+ headingField,
6
+ isDecided,
7
+ isTabular,
8
+ keyRange,
9
+ modelDisagrees,
10
+ plainText,
11
+ progressRuns,
12
+ recordDecision,
13
+ recordState,
14
+ } from "./progress";
15
+
16
+ const schema = (fields: ReviewSchema["fields"]): Pick<ReviewSchema, "fields" | "idField"> => ({
17
+ fields,
18
+ idField: "id",
19
+ });
20
+ const record = (values: Record<string, unknown>, suggestions?: ReviewRecord["suggestions"]) => ({
21
+ id: "r",
22
+ values,
23
+ ...(suggestions ? { suggestions } : {}),
24
+ });
25
+
26
+ it("merges neighbouring records in the same state into runs", () => {
27
+ expect(progressRuns(["answered", "answered", "skipped", "todo", "todo", "answered"])).toEqual([
28
+ { state: "answered", count: 2 },
29
+ { state: "skipped", count: 1 },
30
+ { state: "todo", count: 2 },
31
+ { state: "answered", count: 1 },
32
+ ]);
33
+ expect(recordState({ status: "discarded" })).toBe("skipped");
34
+ expect(recordState(undefined)).toBe("todo");
35
+ });
36
+
37
+ describe("headingField", () => {
38
+ it("takes the first short text field, never the id", () => {
39
+ const fields: ReviewSchema["fields"] = [
40
+ { name: "id", type: "text" },
41
+ { name: "body", type: "markdown" },
42
+ { name: "title", type: "text" },
43
+ ];
44
+ expect(
45
+ headingField(schema(fields), [record({ id: "1", title: "Crash", body: "long" })])?.name
46
+ ).toBe("title");
47
+ });
48
+
49
+ it("skips a text field too long to be a title", () => {
50
+ const fields: ReviewSchema["fields"] = [{ name: "text", type: "text" }];
51
+ expect(headingField(schema(fields), [record({ text: "x".repeat(400) })])).toBeUndefined();
52
+ });
53
+ });
54
+
55
+ it("previews markdown and JSON as plain words, keeping snake_case", () => {
56
+ expect(
57
+ plainText("### Steps\n\n1. Set `OPENAI_BASE_URL`\n2. **Run** it, see [docs](https://x)")
58
+ ).toBe("Steps Set OPENAI_BASE_URL Run it, see docs");
59
+ expect(plainText({ tokens: 4 })).toBe('{"tokens":4}');
60
+ expect(
61
+ plainText([
62
+ { role: "user", content: "hi" },
63
+ { role: "assistant", content: "hello" },
64
+ ])
65
+ ).toBe("hi hello");
66
+ });
67
+
68
+ it("calls rows tabular only when every field is short and typed", () => {
69
+ const short: ReviewSchema["fields"] = [
70
+ { name: "utterance", type: "text" },
71
+ { name: "intent", type: "category" },
72
+ ];
73
+ expect(isTabular(schema(short), [record({ utterance: "refund", intent: "billing" })])).toBe(true);
74
+ expect(isTabular(schema([...short, { name: "body", type: "markdown" }]), [])).toBe(false);
75
+ });
76
+
77
+ it("names the keys a question answers to", () => {
78
+ expect(
79
+ keyRange({
80
+ name: "k",
81
+ type: "label",
82
+ required: true,
83
+ options: [{ value: "a" }, { value: "b" }],
84
+ })
85
+ ).toBe("1–2");
86
+ expect(keyRange({ name: "s", type: "rating", required: false, min: 0, max: 10 })).toBe("1–9");
87
+ expect(keyRange({ name: "t", type: "text", required: false, maxLength: 10 })).toBeUndefined();
88
+ });
89
+
90
+ it("finds where the user answered differently from the model", () => {
91
+ const questions: ReviewSchema["questions"] = [
92
+ {
93
+ name: "kind",
94
+ type: "label",
95
+ required: true,
96
+ options: [{ value: "bug" }, { value: "feature" }],
97
+ },
98
+ ];
99
+ const guessed = record({}, { kind: { value: "bug" } });
100
+ const draft = (kind: string) => ({
101
+ status: "answered" as const,
102
+ answers: { kind },
103
+ comments: [],
104
+ updatedAt: new Date(),
105
+ });
106
+ expect(modelDisagrees({ questions }, guessed, draft("feature"))).toBe(true);
107
+ expect(modelDisagrees({ questions }, guessed, draft("bug"))).toBe(false);
108
+ expect(modelDisagrees({ questions }, guessed, undefined)).toBe(false);
109
+ });
110
+
111
+ describe("record decisions", () => {
112
+ const draft = (status: ReviewDraft["status"], answers: ReviewDraft["answers"] = {}) => ({
113
+ status,
114
+ answers,
115
+ });
116
+ const yesNo = { questions: [{ name: "ok", type: "boolean" as const, required: false }] };
117
+
118
+ it("reads the status when the review has no questions", () => {
119
+ expect(recordDecision({ questions: [] }, draft("answered"))).toBe("approved");
120
+ expect(recordDecision({ questions: [] }, draft("discarded"))).toBe("rejected");
121
+ expect(recordDecision({ questions: [] }, draft("skipped"))).toBeUndefined();
122
+ });
123
+
124
+ it("reads the yes/no question when there is one", () => {
125
+ expect(recordDecision(yesNo, draft("answered", { ok: false }))).toBe("rejected");
126
+ expect(recordDecision(yesNo, draft("answered", { ok: true }))).toBe("approved");
127
+ expect(recordDecision(yesNo, draft("pending"))).toBeUndefined();
128
+ });
129
+
130
+ it("offers approve and reject only where they mean something", () => {
131
+ expect(canDecide({ questions: [] })).toBe(true);
132
+ expect(canDecide(yesNo)).toBe(true);
133
+ expect(
134
+ canDecide({
135
+ questions: [{ name: "kind", type: "label", required: true, options: [{ value: "a" }] }],
136
+ })
137
+ ).toBe(false);
138
+ });
139
+
140
+ it("counts answered and rejected records as decided, never skipped ones", () => {
141
+ expect(isDecided({ status: "answered" })).toBe(true);
142
+ expect(isDecided({ status: "discarded" })).toBe(true);
143
+ expect(isDecided({ status: "skipped" })).toBe(false);
144
+ expect(isDecided(undefined)).toBe(false);
145
+ });
146
+ });
src/lib/utils/review/progress.ts ADDED
@@ -0,0 +1,193 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type {
2
+ ReviewDraft,
3
+ ReviewField,
4
+ ReviewQuestion,
5
+ ReviewRecord,
6
+ ReviewSchema,
7
+ } from "$lib/types/MlReview";
8
+ import { isAnswered } from "./answers";
9
+
10
+ export type RecordState = "answered" | "skipped" | "todo";
11
+
12
+ export function recordState(draft: Pick<ReviewDraft, "status"> | undefined): RecordState {
13
+ if (draft?.status === "answered") return "answered";
14
+ if (draft?.status === "skipped" || draft?.status === "discarded") return "skipped";
15
+ return "todo";
16
+ }
17
+
18
+ export interface ProgressRun<T extends string> {
19
+ state: T;
20
+ count: number;
21
+ }
22
+
23
+ /** neighbours in the same state merge, so 500 records draw as a handful of runs */
24
+ export function progressRuns<T extends string>(states: readonly T[]): ProgressRun<T>[] {
25
+ const runs: ProgressRun<T>[] = [];
26
+ for (const state of states) {
27
+ const last = runs.at(-1);
28
+ if (last && last.state === state) last.count += 1;
29
+ else runs.push({ state, count: 1 });
30
+ }
31
+ return runs;
32
+ }
33
+
34
+ /** the fields worth showing, the id already sits in the record nav */
35
+ export const shownFields = (schema: Pick<ReviewSchema, "fields" | "idField">): ReviewField[] =>
36
+ schema.fields.filter((field) => field.name !== schema.idField);
37
+
38
+ const HEADING_CHARS = 160;
39
+ const HEADING_SAMPLE = 20;
40
+
41
+ /** the first text field short enough to read as a title across the first records */
42
+ export function headingField(
43
+ schema: Pick<ReviewSchema, "fields" | "idField">,
44
+ records: readonly ReviewRecord[]
45
+ ): ReviewField | undefined {
46
+ const sample = records.slice(0, HEADING_SAMPLE);
47
+ return shownFields(schema).find(
48
+ (field) =>
49
+ field.type === "text" &&
50
+ sample.some((record) => typeof record.values[field.name] === "string") &&
51
+ sample.every((record) => {
52
+ const value = record.values[field.name];
53
+ return value === undefined || value === null || String(value).length <= HEADING_CHARS;
54
+ })
55
+ );
56
+ }
57
+
58
+ /** markdown and JSON as one line of plain words, for previews */
59
+ export function plainText(value: unknown): string {
60
+ if (value === undefined || value === null) return "";
61
+ if (typeof value !== "string") {
62
+ if (Array.isArray(value) && value.every((turn) => turn && typeof turn === "object")) {
63
+ return value
64
+ .map((turn) => String((turn as { content?: unknown }).content ?? ""))
65
+ .join(" ")
66
+ .replace(/\s+/g, " ")
67
+ .trim();
68
+ }
69
+ return JSON.stringify(value);
70
+ }
71
+ return value
72
+ .replace(/```[^\n]*\n?/g, " ")
73
+ .replace(/!\[[^\]]*\]\([^)]*\)/g, " ")
74
+ .replace(/\[([^\]]*)\]\([^)]*\)/g, "$1")
75
+ .replace(/<[^>]+>/g, " ")
76
+ .replace(/^\s{0,3}(#{1,6}|>|[-*+]|\d+\.)\s+/gm, "")
77
+ .replace(/(\*\*|__|`|~~)/g, "")
78
+ .replace(/(^|[\s(])[*_](\S(?:[^*_]*\S)?)[*_](?=[\s).,;:!?]|$)/g, "$1$2")
79
+ .replace(/\s+/g, " ")
80
+ .trim();
81
+ }
82
+
83
+ const sameAnswer = (a: unknown, b: unknown) => JSON.stringify(a) === JSON.stringify(b);
84
+
85
+ /** a question the user answered differently from the model guess */
86
+ export function modelDisagrees(
87
+ schema: Pick<ReviewSchema, "questions">,
88
+ record: ReviewRecord,
89
+ draft: ReviewDraft | undefined
90
+ ): boolean {
91
+ return schema.questions.some((question) => {
92
+ const guess = record.suggestions?.[question.name];
93
+ const answer = draft?.answers[question.name];
94
+ return guess !== undefined && isAnswered(answer) && !sameAnswer(guess.value, answer);
95
+ });
96
+ }
97
+
98
+ export function modelAgrees(
99
+ question: ReviewQuestion,
100
+ record: ReviewRecord,
101
+ answer: unknown
102
+ ): boolean | undefined {
103
+ const guess = record.suggestions?.[question.name];
104
+ if (guess === undefined || !isAnswered(answer)) return undefined;
105
+ return sameAnswer(guess.value, answer);
106
+ }
107
+
108
+ const TABLE_TYPES = new Set(["text", "number", "category"]);
109
+
110
+ /** rows that read well as a grid, short typed values, no prose or media */
111
+ export function isTabular(
112
+ schema: Pick<ReviewSchema, "fields" | "idField">,
113
+ records: readonly ReviewRecord[]
114
+ ): boolean {
115
+ const fields = shownFields(schema);
116
+ if (fields.length < 2 || !fields.every((field) => TABLE_TYPES.has(field.type))) return false;
117
+ return records
118
+ .slice(0, HEADING_SAMPLE)
119
+ .every((record) =>
120
+ fields.every((field) => String(record.values[field.name] ?? "").length <= 280)
121
+ );
122
+ }
123
+
124
+ /** the image a record is about, when the image comes first */
125
+ export function leadImageField(
126
+ schema: Pick<ReviewSchema, "fields" | "idField">
127
+ ): ReviewField | undefined {
128
+ const first = shownFields(schema)[0];
129
+ return first?.type === "image" ? first : undefined;
130
+ }
131
+
132
+ /** the first yes/no question, which colours the bar pass and fail */
133
+ export const passFailQuestion = (
134
+ schema: Pick<ReviewSchema, "questions">
135
+ ): Extract<ReviewQuestion, { type: "boolean" }> | undefined =>
136
+ schema.questions.find(
137
+ (question): question is Extract<ReviewQuestion, { type: "boolean" }> =>
138
+ question.type === "boolean"
139
+ );
140
+
141
+ export type RecordDecision = "approved" | "rejected";
142
+
143
+ /** a review with no questions, or with a yes/no one, lets the user approve or reject records */
144
+ export const canDecide = (schema: Pick<ReviewSchema, "questions">): boolean =>
145
+ schema.questions.length === 0 || passFailQuestion(schema) !== undefined;
146
+
147
+ /** the yes/no question carries the decision when there is one, otherwise the record status does */
148
+ export function recordDecision(
149
+ schema: Pick<ReviewSchema, "questions">,
150
+ draft: Pick<ReviewDraft, "status" | "answers"> | undefined
151
+ ): RecordDecision | undefined {
152
+ if (schema.questions.length === 0) {
153
+ if (draft?.status === "answered") return "approved";
154
+ if (draft?.status === "discarded") return "rejected";
155
+ return undefined;
156
+ }
157
+ const question = passFailQuestion(schema);
158
+ const value = question ? draft?.answers[question.name] : undefined;
159
+ return value === true ? "approved" : value === false ? "rejected" : undefined;
160
+ }
161
+
162
+ /** answered or rejected, a submit with none of these is refused */
163
+ export const isDecided = (draft: Pick<ReviewDraft, "status"> | undefined): boolean =>
164
+ draft?.status === "answered" || draft?.status === "discarded";
165
+
166
+ export function questionSummary(question: ReviewQuestion): string {
167
+ switch (question.type) {
168
+ case "label":
169
+ return question.options.map((option) => option.title ?? option.value).join(", ");
170
+ case "multi_label":
171
+ return `any of ${question.options.map((option) => option.title ?? option.value).join(", ")}`;
172
+ case "rating":
173
+ return `rating ${question.min} to ${question.max}`;
174
+ case "boolean":
175
+ return "yes or no";
176
+ case "text":
177
+ return "free text";
178
+ }
179
+ }
180
+
181
+ /** the number keys a question answers to, a text question has none */
182
+ export function keyRange(question: ReviewQuestion): string | undefined {
183
+ const count =
184
+ question.type === "label" || question.type === "multi_label"
185
+ ? question.options.length
186
+ : question.type === "rating"
187
+ ? question.max - question.min + 1
188
+ : question.type === "boolean"
189
+ ? 2
190
+ : 0;
191
+ if (count === 0) return undefined;
192
+ return `1–${Math.min(count, 9)}`;
193
+ }
src/lib/utils/review/records.spec.ts ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, it } from "vitest";
2
+ import type { ReviewSchema } from "$lib/types/MlReview";
3
+ import { compileRecordValidator } from "./records";
4
+
5
+ const schema: Pick<ReviewSchema, "idField" | "fields" | "questions"> = {
6
+ idField: "id",
7
+ fields: [
8
+ { name: "body", type: "text" },
9
+ { name: "meta", type: "json" },
10
+ { name: "turns", type: "chat" },
11
+ { name: "img", type: "image" },
12
+ { name: "voice", type: "audio" },
13
+ { name: "clip", type: "video" },
14
+ { name: "score", type: "number" },
15
+ ],
16
+ questions: [
17
+ {
18
+ name: "kind",
19
+ type: "label",
20
+ required: true,
21
+ options: [{ value: "bug" }, { value: "feature" }],
22
+ },
23
+ ],
24
+ };
25
+
26
+ const validate = compileRecordValidator(schema);
27
+
28
+ describe("compileRecordValidator", () => {
29
+ it("keeps typed values, stringifies ids and stores suggestions apart", () => {
30
+ const result = validate([
31
+ {
32
+ id: 7,
33
+ body: "crash",
34
+ meta: { a: [1] },
35
+ turns: [{ role: "user", content: "hi" }],
36
+ img: "hf://datasets/o/r/a.png",
37
+ score: 0.5,
38
+ suggestions: { kind: { value: "bug", score: 0.7, rationale: "stack trace" } },
39
+ },
40
+ { id: "b", body: null },
41
+ ]);
42
+ expect(result).toEqual({
43
+ ok: true,
44
+ records: [
45
+ {
46
+ id: "7",
47
+ values: {
48
+ body: "crash",
49
+ meta: { a: [1] },
50
+ turns: [{ role: "user", content: "hi" }],
51
+ img: "hf://datasets/o/r/a.png",
52
+ score: 0.5,
53
+ },
54
+ suggestions: { kind: { value: "bug", score: 0.7, rationale: "stack trace" } },
55
+ },
56
+ { id: "b", values: {} },
57
+ ],
58
+ });
59
+ });
60
+
61
+ it("reports wrong types by 1-based record position", () => {
62
+ const result = validate([
63
+ { id: "a", body: "ok" },
64
+ { id: "b", body: 3 },
65
+ { id: "c", turns: [{ role: "user" }], img: "http://x/y.png", score: "1" },
66
+ { id: "d", body: ["bug"] },
67
+ ]);
68
+ expect(result).toEqual({
69
+ ok: false,
70
+ errors: [
71
+ "record 2: body must be a string",
72
+ "record 3: turns must be a list of {role, content} turns",
73
+ "record 3: img must be an https:// or hf:// URL",
74
+ "record 3: score must be a number",
75
+ "record 4: body must be a string; declare the field as json to show a list or an object",
76
+ ],
77
+ });
78
+ });
79
+
80
+ it("takes audio and video as https or hf urls", () => {
81
+ expect(
82
+ validate([
83
+ { id: "a", voice: "hf://datasets/o/r/a.wav", clip: "https://x.test/c.mp4" },
84
+ { id: "b", voice: "file:///a.wav" },
85
+ ])
86
+ ).toEqual({ ok: false, errors: ["record 2: voice must be an https:// or hf:// URL"] });
87
+ });
88
+
89
+ it("needs present, unique ids", () => {
90
+ const result = validate([{ id: "a" }, { body: "x" }, { id: "a" }, { id: " " }]);
91
+ expect(result.ok).toBe(false);
92
+ if (result.ok) return;
93
+ expect(result.errors).toEqual([
94
+ "record 2: id is missing; every record needs a string or number id",
95
+ 'record 3: id "a" repeats record 1',
96
+ "record 4: id is missing; every record needs a string or number id",
97
+ ]);
98
+ });
99
+
100
+ it("refuses unknown keys except suggestions, and bad suggestions", () => {
101
+ const result = validate([
102
+ { id: "a", extra: 1, suggestions: { kind: { value: "bug" } } },
103
+ { id: "b", suggestions: { kind: { value: "nope" } } },
104
+ { id: "c", suggestions: { other: { value: "bug" } } },
105
+ { id: "d", suggestions: { kind: { value: "bug", score: 2 } } },
106
+ ]);
107
+ expect(result.ok).toBe(false);
108
+ if (result.ok) return;
109
+ expect(result.errors).toEqual([
110
+ 'record 1: unknown key "extra"; declare it as a field or drop it',
111
+ 'record 2: suggestions.kind.value "nope" is not an option (bug, feature)',
112
+ "record 3: suggestions.other names no declared question",
113
+ "record 4: suggestions.kind.score must be a number from 0 to 1",
114
+ ]);
115
+ });
116
+
117
+ it("stops at ten errors", () => {
118
+ const result = validate(Array.from({ length: 30 }, (_, i) => ({ id: String(i), body: i })));
119
+ expect(result.ok).toBe(false);
120
+ if (result.ok) return;
121
+ expect(result.errors).toHaveLength(10);
122
+ expect(result.errors[9]).toBe("record 10: body must be a string");
123
+ });
124
+ });
src/lib/utils/review/records.ts ADDED
@@ -0,0 +1,176 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type {
2
+ ReviewField,
3
+ ReviewRecord,
4
+ ReviewSchema,
5
+ ReviewSuggestion,
6
+ } from "$lib/types/MlReview";
7
+ import { validateAnswer } from "./answers";
8
+ import { SUGGESTIONS_KEY } from "./schema";
9
+
10
+ const MAX_ERRORS = 10;
11
+ const MAX_RATIONALE_CHARS = 1_000;
12
+ const MAX_ID_CHARS = 200;
13
+
14
+ const isRecord = (value: unknown): value is Record<string, unknown> =>
15
+ typeof value === "object" && value !== null && !Array.isArray(value);
16
+
17
+ type FieldCheck = (value: unknown) => string | undefined;
18
+
19
+ function checkFor(field: ReviewField): FieldCheck {
20
+ switch (field.type) {
21
+ case "text":
22
+ case "markdown":
23
+ case "code":
24
+ case "category":
25
+ return (value) =>
26
+ typeof value === "string"
27
+ ? undefined
28
+ : typeof value === "object"
29
+ ? "must be a string; declare the field as json to show a list or an object"
30
+ : "must be a string";
31
+ case "number":
32
+ return (value) =>
33
+ typeof value === "number" && Number.isFinite(value) ? undefined : "must be a number";
34
+ case "json":
35
+ return () => undefined;
36
+ case "image":
37
+ case "audio":
38
+ case "video":
39
+ return (value) =>
40
+ typeof value === "string" && /^(https:\/\/|hf:\/\/)\S+$/.test(value)
41
+ ? undefined
42
+ : "must be an https:// or hf:// URL";
43
+ case "chat":
44
+ return (value) =>
45
+ Array.isArray(value) &&
46
+ value.every(
47
+ (turn) =>
48
+ isRecord(turn) && typeof turn.role === "string" && typeof turn.content === "string"
49
+ )
50
+ ? undefined
51
+ : "must be a list of {role, content} turns";
52
+ }
53
+ }
54
+
55
+ export type ValidatedRecords =
56
+ { ok: true; records: ReviewRecord[] } | { ok: false; errors: string[] };
57
+
58
+ /** a null or absent field shows empty, so a sparse dataset row is not refused */
59
+ export function compileRecordValidator(
60
+ schema: Pick<ReviewSchema, "idField" | "fields" | "questions">
61
+ ) {
62
+ const checks = new Map(schema.fields.map((field) => [field.name, checkFor(field)]));
63
+ const questions = new Map(schema.questions.map((question) => [question.name, question]));
64
+ const known = new Set([schema.idField, SUGGESTIONS_KEY, ...checks.keys()]);
65
+
66
+ return function validateRecords(raw: unknown[]): ValidatedRecords {
67
+ const errors: string[] = [];
68
+ const records: ReviewRecord[] = [];
69
+ const seen = new Map<string, number>();
70
+ const fail = (position: number, message: string) => {
71
+ if (errors.length < MAX_ERRORS) errors.push(`record ${position}: ${message}`);
72
+ };
73
+
74
+ for (const [index, item] of raw.entries()) {
75
+ const position = index + 1;
76
+ if (!isRecord(item)) {
77
+ fail(position, "must be an object");
78
+ continue;
79
+ }
80
+ const before = errors.length;
81
+ const rawId = item[schema.idField];
82
+ let id: string | undefined;
83
+ if (typeof rawId === "string" && rawId.trim().length > 0) id = rawId.trim();
84
+ else if (typeof rawId === "number" && Number.isFinite(rawId)) id = String(rawId);
85
+ if (id === undefined) {
86
+ fail(position, `${schema.idField} is missing; every record needs a string or number id`);
87
+ } else if (id.length > MAX_ID_CHARS) {
88
+ fail(position, `${schema.idField} is over ${MAX_ID_CHARS} characters`);
89
+ } else if (seen.has(id)) {
90
+ fail(position, `${schema.idField} "${id}" repeats record ${seen.get(id)}`);
91
+ } else {
92
+ seen.set(id, position);
93
+ }
94
+
95
+ const unknown = Object.keys(item).filter((key) => !known.has(key));
96
+ if (unknown.length > 0) {
97
+ fail(
98
+ position,
99
+ `unknown key${unknown.length > 1 ? "s" : ""} ${unknown.map((k) => `"${k}"`).join(", ")}; declare ${
100
+ unknown.length > 1 ? "them" : "it"
101
+ } as a field or drop ${unknown.length > 1 ? "them" : "it"}`
102
+ );
103
+ }
104
+
105
+ const values: Record<string, unknown> = {};
106
+ for (const [name, check] of checks) {
107
+ const value = item[name];
108
+ if (value === undefined || value === null) continue;
109
+ const problem = check(value);
110
+ if (problem) fail(position, `${name} ${problem}`);
111
+ else values[name] = value;
112
+ }
113
+
114
+ let suggestions: Record<string, ReviewSuggestion> | undefined;
115
+ const rawSuggestions = item[SUGGESTIONS_KEY];
116
+ if (rawSuggestions !== undefined) {
117
+ if (!isRecord(rawSuggestions)) {
118
+ fail(position, `${SUGGESTIONS_KEY} must be an object keyed by question name`);
119
+ } else {
120
+ suggestions = {};
121
+ for (const [questionName, rawSuggestion] of Object.entries(rawSuggestions)) {
122
+ const question = questions.get(questionName);
123
+ const at = `${SUGGESTIONS_KEY}.${questionName}`;
124
+ if (!question) {
125
+ fail(position, `${at} names no declared question`);
126
+ continue;
127
+ }
128
+ if (!isRecord(rawSuggestion) || !("value" in rawSuggestion)) {
129
+ fail(position, `${at} must be {value, score?, rationale?}`);
130
+ continue;
131
+ }
132
+ const problem = validateAnswer(question, rawSuggestion.value);
133
+ if (problem) {
134
+ fail(position, `${at}.value ${problem}`);
135
+ continue;
136
+ }
137
+ const { score, rationale } = rawSuggestion;
138
+ if (
139
+ score !== undefined &&
140
+ (typeof score !== "number" || !Number.isFinite(score) || score < 0 || score > 1)
141
+ ) {
142
+ fail(position, `${at}.score must be a number from 0 to 1`);
143
+ continue;
144
+ }
145
+ if (
146
+ rationale !== undefined &&
147
+ (typeof rationale !== "string" || rationale.length > MAX_RATIONALE_CHARS)
148
+ ) {
149
+ fail(
150
+ position,
151
+ `${at}.rationale must be text of at most ${MAX_RATIONALE_CHARS} characters`
152
+ );
153
+ continue;
154
+ }
155
+ suggestions[questionName] = {
156
+ value: rawSuggestion.value as ReviewSuggestion["value"],
157
+ ...(score !== undefined ? { score } : {}),
158
+ ...(rationale !== undefined ? { rationale } : {}),
159
+ };
160
+ }
161
+ }
162
+ }
163
+
164
+ if (errors.length === before && id !== undefined) {
165
+ records.push({
166
+ id,
167
+ values,
168
+ ...(suggestions && Object.keys(suggestions).length > 0 ? { suggestions } : {}),
169
+ });
170
+ }
171
+ }
172
+
173
+ if (errors.length > 0) return { ok: false, errors };
174
+ return { ok: true, records };
175
+ };
176
+ }
src/lib/utils/review/schema.spec.ts ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, it } from "vitest";
2
+ import { parseReviewSchema, REVIEW_LIMITS } from "./schema";
3
+
4
+ const base = {
5
+ title: "Issue triage, batch 1",
6
+ fields: [
7
+ { name: "title", type: "text" },
8
+ { name: "body", type: "markdown" },
9
+ ],
10
+ };
11
+
12
+ const options = (n: number) => Array.from({ length: n }, (_, i) => ({ value: `o${i}` }));
13
+
14
+ const error = (input: unknown) => {
15
+ const parsed = parseReviewSchema(input);
16
+ if (parsed.ok) throw new Error("expected a refusal");
17
+ return parsed.error;
18
+ };
19
+
20
+ describe("parseReviewSchema", () => {
21
+ it("normalises a declaration and fills the defaults", () => {
22
+ const parsed = parseReviewSchema({
23
+ ...base,
24
+ questions: [
25
+ { name: "kind", type: "label", required: true, options: options(3) },
26
+ { name: "severity", type: "rating" },
27
+ { name: "note", type: "text", target: "body" },
28
+ ],
29
+ });
30
+ expect(parsed).toEqual({
31
+ ok: true,
32
+ schema: {
33
+ title: "Issue triage, batch 1",
34
+ idField: "id",
35
+ fields: base.fields,
36
+ questions: [
37
+ { name: "kind", type: "label", required: true, options: options(3) },
38
+ { name: "severity", type: "rating", required: false, min: 1, max: 5 },
39
+ { name: "note", type: "text", required: false, target: "body", maxLength: 4_000 },
40
+ ],
41
+ suggestions: "shown",
42
+ },
43
+ });
44
+ });
45
+
46
+ it("holds every limit, naming the path to fix", () => {
47
+ const field = (i: number) => ({ name: `f${i}`, type: "text" });
48
+ expect(
49
+ error({
50
+ ...base,
51
+ fields: Array.from({ length: REVIEW_LIMITS.fields + 1 }, (_, i) => field(i)),
52
+ })
53
+ ).toBe("fields: at most 30 fields");
54
+ expect(
55
+ error({
56
+ ...base,
57
+ questions: Array.from({ length: REVIEW_LIMITS.questions + 1 }, (_, i) => ({
58
+ name: `q${i}`,
59
+ type: "boolean",
60
+ })),
61
+ })
62
+ ).toBe("questions: at most 20 questions");
63
+ expect(
64
+ error({ ...base, questions: [{ name: "kind", type: "label", options: options(1) }] })
65
+ ).toBe("questions[0].options: give 2 to 20 options");
66
+ expect(
67
+ error({ ...base, questions: [{ name: "kind", type: "label", options: options(21) }] })
68
+ ).toBe("questions[0].options: give 2 to 20 options");
69
+ expect(
70
+ error({ ...base, questions: [{ name: "r", type: "rating", min: 0, max: 11 }] })
71
+ ).toContain("at most 11 steps");
72
+ expect(
73
+ parseReviewSchema({ ...base, questions: [{ name: "r", type: "rating", min: 0, max: 10 }] }).ok
74
+ ).toBe(true);
75
+ expect(
76
+ error({ ...base, questions: [{ name: "t", type: "text", max_length: 4_001 }] })
77
+ ).toContain("questions[0].max_length");
78
+ expect(error({ ...base, fields: [{ name: "Body", type: "text" }] })).toContain(
79
+ "lowercase snake_case"
80
+ );
81
+ expect(error({ ...base, fields: [{ name: "a".repeat(41), type: "text" }] })).toContain(
82
+ "lowercase snake_case"
83
+ );
84
+ });
85
+
86
+ it("refuses a target that names no field, and names the fields there are", () => {
87
+ expect(error({ ...base, questions: [{ name: "fix", type: "text", target: "bdy" }] })).toBe(
88
+ 'questions[0].target: "bdy" is not a declared field (fields: title, body)'
89
+ );
90
+ });
91
+
92
+ it("refuses repeated names and options, and the reserved suggestions key", () => {
93
+ expect(error({ ...base, fields: [...base.fields, { name: "title", type: "json" }] })).toContain(
94
+ '"title" is declared twice'
95
+ );
96
+ expect(
97
+ error({
98
+ ...base,
99
+ questions: [{ name: "k", type: "label", options: [{ value: "a" }, { value: "a" }] }],
100
+ })
101
+ ).toContain('"a" is listed twice');
102
+ expect(error({ ...base, fields: [{ name: "suggestions", type: "json" }] })).toContain(
103
+ "reserved"
104
+ );
105
+ });
106
+
107
+ it("checks multi_label pick counts against the options", () => {
108
+ expect(
109
+ error({
110
+ ...base,
111
+ questions: [{ name: "a", type: "multi_label", options: options(3), max: 4 }],
112
+ })
113
+ ).toContain("max: must be between 1 and 3");
114
+ expect(
115
+ error({
116
+ ...base,
117
+ questions: [{ name: "a", type: "multi_label", options: options(3), min: 3, max: 2 }],
118
+ })
119
+ ).toContain("min: must not exceed max");
120
+ });
121
+
122
+ it("refuses an unknown question type", () => {
123
+ expect(error({ ...base, questions: [{ name: "a", type: "span" }] })).toContain(
124
+ "questions[0].type"
125
+ );
126
+ });
127
+ });
src/lib/utils/review/schema.ts ADDED
@@ -0,0 +1,254 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { z } from "zod";
2
+ import type { ReviewSchema } from "$lib/types/MlReview";
3
+
4
+ export const REVIEW_LIMITS = {
5
+ fields: 30,
6
+ questions: 20,
7
+ minOptions: 2,
8
+ maxOptions: 20,
9
+ ratingSteps: 11,
10
+ textChars: 4_000,
11
+ titleChars: 200,
12
+ instructionsChars: 4_000,
13
+ optionChars: 200,
14
+ descriptionChars: 1_000,
15
+ idFieldChars: 64,
16
+ comments: 200,
17
+ commentsPerRecord: 20,
18
+ commentChars: 2_000,
19
+ } as const;
20
+
21
+ export const REVIEW_NAME_PATTERN = /^[a-z][a-z0-9_]{0,39}$/;
22
+
23
+ export const REVIEW_FIELD_TYPES = [
24
+ "text",
25
+ "markdown",
26
+ "code",
27
+ "json",
28
+ "chat",
29
+ "image",
30
+ "audio",
31
+ "video",
32
+ "number",
33
+ "category",
34
+ ] as const;
35
+
36
+ export const REVIEW_QUESTION_TYPES = ["label", "multi_label", "rating", "boolean", "text"] as const;
37
+
38
+ export const REVIEW_SUGGESTION_MODES = ["shown", "after_answer", "none"] as const;
39
+
40
+ /** a record carries its model guesses under this key, so no field may take the name */
41
+ export const SUGGESTIONS_KEY = "suggestions";
42
+
43
+ const name = z
44
+ .string()
45
+ .regex(
46
+ REVIEW_NAME_PATTERN,
47
+ "names are lowercase snake_case, start with a letter and are at most 40 characters"
48
+ )
49
+ .refine((value) => value !== SUGGESTIONS_KEY, `"${SUGGESTIONS_KEY}" is reserved`);
50
+
51
+ const shortText = (max: number) => z.string().trim().min(1).max(max, `at most ${max} characters`);
52
+
53
+ const field = z.object({
54
+ name,
55
+ type: z.enum(REVIEW_FIELD_TYPES),
56
+ title: shortText(REVIEW_LIMITS.titleChars).optional(),
57
+ language: z.string().trim().max(40).optional(),
58
+ column: z.string().trim().min(1).max(200).optional(),
59
+ });
60
+
61
+ const option = z.object({
62
+ value: shortText(REVIEW_LIMITS.optionChars),
63
+ title: shortText(REVIEW_LIMITS.optionChars).optional(),
64
+ description: shortText(REVIEW_LIMITS.descriptionChars).optional(),
65
+ });
66
+
67
+ const options = z
68
+ .array(option)
69
+ .min(
70
+ REVIEW_LIMITS.minOptions,
71
+ `give ${REVIEW_LIMITS.minOptions} to ${REVIEW_LIMITS.maxOptions} options`
72
+ )
73
+ .max(
74
+ REVIEW_LIMITS.maxOptions,
75
+ `give ${REVIEW_LIMITS.minOptions} to ${REVIEW_LIMITS.maxOptions} options`
76
+ );
77
+
78
+ const questionBase = {
79
+ name,
80
+ title: shortText(REVIEW_LIMITS.titleChars).optional(),
81
+ description: shortText(REVIEW_LIMITS.descriptionChars).optional(),
82
+ required: z.boolean().optional(),
83
+ target: z.string().optional(),
84
+ };
85
+
86
+ const count = z.number().int().nonnegative();
87
+
88
+ const question = z.discriminatedUnion("type", [
89
+ z.object({ ...questionBase, type: z.literal("label"), options }),
90
+ z.object({
91
+ ...questionBase,
92
+ type: z.literal("multi_label"),
93
+ options,
94
+ min: count.optional(),
95
+ max: count.optional(),
96
+ }),
97
+ z.object({
98
+ ...questionBase,
99
+ type: z.literal("rating"),
100
+ min: z.number().int().optional(),
101
+ max: z.number().int().optional(),
102
+ }),
103
+ z.object({ ...questionBase, type: z.literal("boolean") }),
104
+ z.object({
105
+ ...questionBase,
106
+ type: z.literal("text"),
107
+ max_length: z.number().int().positive().max(REVIEW_LIMITS.textChars).optional(),
108
+ }),
109
+ ]);
110
+
111
+ export const reviewDeclarationSchema = z.object({
112
+ title: shortText(REVIEW_LIMITS.titleChars),
113
+ instructions: shortText(REVIEW_LIMITS.instructionsChars).optional(),
114
+ id_field: z
115
+ .string()
116
+ .trim()
117
+ .min(1)
118
+ .max(REVIEW_LIMITS.idFieldChars)
119
+ .refine((value) => value !== SUGGESTIONS_KEY, `"${SUGGESTIONS_KEY}" is reserved`)
120
+ .optional(),
121
+ fields: z
122
+ .array(field)
123
+ .min(1, "declare at least one field")
124
+ .max(REVIEW_LIMITS.fields, `at most ${REVIEW_LIMITS.fields} fields`),
125
+ questions: z
126
+ .array(question)
127
+ .max(REVIEW_LIMITS.questions, `at most ${REVIEW_LIMITS.questions} questions`)
128
+ .optional(),
129
+ suggestions: z.enum(REVIEW_SUGGESTION_MODES).optional(),
130
+ });
131
+
132
+ export type ReviewDeclaration = z.input<typeof reviewDeclarationSchema>;
133
+
134
+ export type ParsedReviewSchema = { ok: true; schema: ReviewSchema } | { ok: false; error: string };
135
+
136
+ function issueText(error: z.ZodError): string {
137
+ const issue = error.issues[0];
138
+ const path = issue?.path
139
+ .map((part, i) => (typeof part === "number" ? `[${part}]` : i === 0 ? part : `.${part}`))
140
+ .join("");
141
+ return `${path ? `${path}: ` : ""}${issue?.message ?? "invalid"}`;
142
+ }
143
+
144
+ const DEFAULT_RATING = { min: 1, max: 5 };
145
+
146
+ export function parseReviewSchema(input: unknown): ParsedReviewSchema {
147
+ const parsed = reviewDeclarationSchema.safeParse(input);
148
+ if (!parsed.success) return { ok: false, error: issueText(parsed.error) };
149
+ const declared = parsed.data;
150
+
151
+ const fieldNames = new Set<string>();
152
+ for (const [i, f] of declared.fields.entries()) {
153
+ if (fieldNames.has(f.name)) {
154
+ return { ok: false, error: `fields[${i}].name: "${f.name}" is declared twice` };
155
+ }
156
+ fieldNames.add(f.name);
157
+ }
158
+
159
+ const questions: ReviewSchema["questions"] = [];
160
+ const questionNames = new Set<string>();
161
+ for (const [i, q] of (declared.questions ?? []).entries()) {
162
+ const at = `questions[${i}]`;
163
+ if (questionNames.has(q.name)) {
164
+ return { ok: false, error: `${at}.name: "${q.name}" is declared twice` };
165
+ }
166
+ questionNames.add(q.name);
167
+ if (q.target !== undefined && !fieldNames.has(q.target)) {
168
+ return {
169
+ ok: false,
170
+ error: `${at}.target: "${q.target}" is not a declared field (fields: ${[...fieldNames].join(", ")})`,
171
+ };
172
+ }
173
+ const base = {
174
+ name: q.name,
175
+ required: q.required === true,
176
+ ...(q.title ? { title: q.title } : {}),
177
+ ...(q.description ? { description: q.description } : {}),
178
+ ...(q.target !== undefined ? { target: q.target } : {}),
179
+ };
180
+ if (q.type === "label" || q.type === "multi_label") {
181
+ const values = new Set<string>();
182
+ for (const [j, o] of q.options.entries()) {
183
+ if (values.has(o.value)) {
184
+ return { ok: false, error: `${at}.options[${j}].value: "${o.value}" is listed twice` };
185
+ }
186
+ values.add(o.value);
187
+ }
188
+ }
189
+ switch (q.type) {
190
+ case "label":
191
+ questions.push({ ...base, type: "label", options: q.options });
192
+ break;
193
+ case "multi_label": {
194
+ const min = q.min;
195
+ const max = q.max;
196
+ if (max !== undefined && (max < 1 || max > q.options.length)) {
197
+ return { ok: false, error: `${at}.max: must be between 1 and ${q.options.length}` };
198
+ }
199
+ if (min !== undefined && min > (max ?? q.options.length)) {
200
+ return { ok: false, error: `${at}.min: must not exceed max` };
201
+ }
202
+ questions.push({
203
+ ...base,
204
+ type: "multi_label",
205
+ options: q.options,
206
+ ...(min !== undefined ? { min } : {}),
207
+ ...(max !== undefined ? { max } : {}),
208
+ });
209
+ break;
210
+ }
211
+ case "rating": {
212
+ const min = q.min ?? DEFAULT_RATING.min;
213
+ const max = q.max ?? (q.min !== undefined ? q.min + 4 : DEFAULT_RATING.max);
214
+ if (max <= min) return { ok: false, error: `${at}: max must be greater than min` };
215
+ if (max - min + 1 > REVIEW_LIMITS.ratingSteps) {
216
+ return {
217
+ ok: false,
218
+ error: `${at}: a rating has at most ${REVIEW_LIMITS.ratingSteps} steps, ${min} to ${max} is ${max - min + 1}`,
219
+ };
220
+ }
221
+ questions.push({ ...base, type: "rating", min, max });
222
+ break;
223
+ }
224
+ case "boolean":
225
+ questions.push({ ...base, type: "boolean" });
226
+ break;
227
+ case "text":
228
+ questions.push({
229
+ ...base,
230
+ type: "text",
231
+ maxLength: q.max_length ?? REVIEW_LIMITS.textChars,
232
+ });
233
+ break;
234
+ }
235
+ }
236
+
237
+ return {
238
+ ok: true,
239
+ schema: {
240
+ title: declared.title,
241
+ ...(declared.instructions ? { instructions: declared.instructions } : {}),
242
+ idField: declared.id_field ?? "id",
243
+ fields: declared.fields.map((f) => ({
244
+ name: f.name,
245
+ type: f.type,
246
+ ...(f.title ? { title: f.title } : {}),
247
+ ...(f.type === "code" && f.language ? { language: f.language } : {}),
248
+ ...(f.column && f.column !== f.name ? { column: f.column } : {}),
249
+ })) as ReviewSchema["fields"],
250
+ questions,
251
+ suggestions: declared.suggestions ?? "shown",
252
+ },
253
+ };
254
+ }
src/lib/utils/review/view.spec.ts ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { describe, expect, it } from "vitest";
2
+ import type { ReviewSchema } from "$lib/types/MlReview";
3
+ import {
4
+ draftStatus,
5
+ findQuote,
6
+ imageUrl,
7
+ mergeDraft,
8
+ pick,
9
+ quoteAt,
10
+ suggestionVisible,
11
+ } from "./view";
12
+
13
+ const schema = (required: boolean): Pick<ReviewSchema, "questions"> => ({
14
+ questions: [
15
+ { name: "kind", type: "label", required, options: [{ value: "a" }, { value: "b" }] },
16
+ { name: "note", type: "text", required: false, maxLength: 100 },
17
+ ],
18
+ });
19
+
20
+ describe("draftStatus", () => {
21
+ it("is answered once every required question is", () => {
22
+ expect(draftStatus(schema(true), { note: "x" }, undefined, { answering: true })).toBe(
23
+ "pending"
24
+ );
25
+ expect(draftStatus(schema(true), { kind: "a" }, undefined, { answering: true })).toBe(
26
+ "answered"
27
+ );
28
+ });
29
+
30
+ it("is answered by any answer when nothing is required", () => {
31
+ expect(draftStatus(schema(false), { note: " " }, undefined, { answering: true })).toBe(
32
+ "pending"
33
+ );
34
+ expect(draftStatus(schema(false), { note: "x" }, undefined, { answering: true })).toBe(
35
+ "answered"
36
+ );
37
+ });
38
+
39
+ it("keeps a skip until the user answers again", () => {
40
+ expect(draftStatus(schema(true), {}, "skipped", { answering: false })).toBe("skipped");
41
+ expect(draftStatus(schema(true), { kind: "a" }, "skipped", { answering: true })).toBe(
42
+ "answered"
43
+ );
44
+ });
45
+ });
46
+
47
+ describe("quotes", () => {
48
+ const text = "the cat sat on the mat, the cat ran";
49
+
50
+ it("finds a repeated quote by its context", () => {
51
+ const second = text.lastIndexOf("the cat");
52
+ const quote = quoteAt(text, second, second + "the cat".length);
53
+ expect(quote.prefix).toBe(text.slice(0, second));
54
+ expect(findQuote(text, quote)).toEqual({ start: second, end: second + 7 });
55
+ });
56
+
57
+ it("loses a quote the text no longer holds", () => {
58
+ expect(findQuote(text, { exact: "the dog", prefix: "", suffix: "" })).toBeUndefined();
59
+ });
60
+ });
61
+
62
+ it("toggles a multi label pick and replaces any other", () => {
63
+ const multi = {
64
+ name: "tags",
65
+ type: "multi_label" as const,
66
+ required: false,
67
+ options: [{ value: "a" }, { value: "b" }],
68
+ };
69
+ expect(pick(multi, ["a"], "b")).toEqual(["a", "b"]);
70
+ expect(pick(multi, ["a", "b"], "a")).toEqual(["b"]);
71
+ expect(pick(schema(true).questions[0], "a", "b")).toBe("b");
72
+ });
73
+
74
+ it("shows an after_answer suggestion only once answered", () => {
75
+ expect(suggestionVisible("after_answer", undefined)).toBe(false);
76
+ expect(suggestionVisible("after_answer", "a")).toBe(true);
77
+ expect(suggestionVisible("shown", undefined)).toBe(true);
78
+ expect(suggestionVisible("none", "a")).toBe(false);
79
+ });
80
+
81
+ it("turns hf:// images into public resolve URLs", () => {
82
+ expect(imageUrl("hf://datasets/org/set/img/1.png")).toBe(
83
+ "https://huggingface.co/datasets/org/set/resolve/main/img/1.png"
84
+ );
85
+ expect(imageUrl("hf://org/model@v2/a.jpg")).toBe(
86
+ "https://huggingface.co/org/model/resolve/v2/a.jpg"
87
+ );
88
+ expect(imageUrl("https://example.com/a.png")).toBe("https://example.com/a.png");
89
+ expect(imageUrl("hf://buckets/org")).toBeUndefined();
90
+ });
91
+
92
+ describe("mergeDraft", () => {
93
+ const schema = {
94
+ questions: [
95
+ { name: "a", type: "boolean" as const, required: false },
96
+ { name: "b", type: "boolean" as const, required: false },
97
+ ],
98
+ };
99
+ const comment = (id: string) => ({ id, body: id, createdAt: new Date(0) });
100
+ const base = {
101
+ status: "answered" as const,
102
+ answers: { a: true, b: true },
103
+ comments: [comment("x")],
104
+ };
105
+
106
+ it("keeps the other tab's change to an answer this tab left alone", () => {
107
+ const ours = { ...base, answers: { a: true, b: false } };
108
+ const theirs = { ...base, answers: { a: false, b: true } };
109
+ expect(mergeDraft(schema, base, ours, theirs).answers).toEqual({ a: false, b: false });
110
+ });
111
+
112
+ it("clears an answer this tab cleared", () => {
113
+ const ours = { ...base, answers: { a: true } };
114
+ const theirs = { ...base, answers: { a: true, b: true } };
115
+ expect(mergeDraft(schema, base, ours, theirs).answers).toEqual({ a: true });
116
+ });
117
+
118
+ it("drops a comment this tab deleted and keeps both tabs' new ones", () => {
119
+ const ours = { ...base, comments: [comment("mine")] };
120
+ const theirs = { ...base, comments: [comment("x"), comment("theirs")] };
121
+ expect(mergeDraft(schema, base, ours, theirs).comments.map((c) => c.id)).toEqual([
122
+ "theirs",
123
+ "mine",
124
+ ]);
125
+ });
126
+
127
+ it("keeps a status this tab set on a review with no questions", () => {
128
+ const none = { questions: [] };
129
+ const was = { status: "pending" as const, answers: {}, comments: [] };
130
+ const merged = mergeDraft(
131
+ none,
132
+ was,
133
+ { ...was, status: "discarded" },
134
+ { ...was, comments: [comment("t")] }
135
+ );
136
+ expect(merged.status).toBe("discarded");
137
+ expect(merged.comments).toHaveLength(1);
138
+ });
139
+ });
src/lib/utils/review/view.ts ADDED
@@ -0,0 +1,191 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type {
2
+ ReviewAnswer,
3
+ ReviewDraft,
4
+ ReviewDraftStatus,
5
+ ReviewQuestion,
6
+ ReviewQuote,
7
+ ReviewSchema,
8
+ ReviewSuggestionsMode,
9
+ } from "$lib/types/MlReview";
10
+ import { isAnswered } from "./answers";
11
+
12
+ /** long enough to see the pick land before the record changes */
13
+ export const AUTO_ADVANCE_MS = 250;
14
+
15
+ /** what the comment anchor keeps on each side of the selection */
16
+ export const QUOTE_CONTEXT_CHARS = 32;
17
+ /** the draft endpoint refuses a longer quote */
18
+ export const QUOTE_MAX_CHARS = 2_000;
19
+
20
+ /** a skip or discard stands until the user answers again */
21
+ export function draftStatus(
22
+ schema: Pick<ReviewSchema, "questions">,
23
+ answers: ReviewDraft["answers"],
24
+ previous: ReviewDraftStatus | undefined,
25
+ { answering }: { answering: boolean }
26
+ ): ReviewDraftStatus {
27
+ if (!answering && (previous === "skipped" || previous === "discarded")) return previous;
28
+ const required = schema.questions.filter((question) => question.required);
29
+ const done =
30
+ required.length > 0
31
+ ? required.every((question) => isAnswered(answers[question.name]))
32
+ : schema.questions.some((question) => isAnswered(answers[question.name]));
33
+ return done ? "answered" : "pending";
34
+ }
35
+
36
+ export type DraftCore = Pick<ReviewDraft, "status" | "answers" | "comments">;
37
+
38
+ const sameValue = (a: unknown, b: unknown) => JSON.stringify(a) === JSON.stringify(b);
39
+
40
+ /** this tab's changes since `base`, set or cleared answers, added or deleted comments, over theirs */
41
+ export function mergeDraft(
42
+ schema: Pick<ReviewSchema, "questions">,
43
+ base: DraftCore | undefined,
44
+ ours: DraftCore,
45
+ theirs: DraftCore
46
+ ): DraftCore {
47
+ const was: DraftCore = base ?? { status: "pending", answers: {}, comments: [] };
48
+ const answers = { ...theirs.answers };
49
+ let answering = false;
50
+ for (const name of new Set([...Object.keys(ours.answers), ...Object.keys(was.answers)])) {
51
+ const mine = ours.answers[name];
52
+ if (sameValue(mine, was.answers[name])) continue;
53
+ answering = true;
54
+ if (mine === undefined) delete answers[name];
55
+ else answers[name] = mine;
56
+ }
57
+ const before = new Set(was.comments.map((comment) => comment.id));
58
+ const kept = new Set(ours.comments.map((comment) => comment.id));
59
+ const known = new Set(theirs.comments.map((comment) => comment.id));
60
+ const comments = [
61
+ ...theirs.comments.filter((comment) => !before.has(comment.id) || kept.has(comment.id)),
62
+ ...ours.comments.filter((comment) => !before.has(comment.id) && !known.has(comment.id)),
63
+ ];
64
+ const moved = ours.status !== was.status;
65
+ const settled = ours.status === "skipped" || ours.status === "discarded";
66
+ const status =
67
+ moved && (settled || schema.questions.length === 0)
68
+ ? ours.status
69
+ : schema.questions.length === 0
70
+ ? theirs.status
71
+ : draftStatus(schema, answers, theirs.status, { answering });
72
+ return { status, answers, comments };
73
+ }
74
+
75
+ export function suggestionVisible(mode: ReviewSuggestionsMode, value: unknown): boolean {
76
+ if (mode === "none") return false;
77
+ if (mode === "after_answer") return isAnswered(value);
78
+ return true;
79
+ }
80
+
81
+ export interface KeyOption {
82
+ value: ReviewAnswer;
83
+ label: string;
84
+ }
85
+
86
+ /** the picks number keys reach, in order, a text question has none */
87
+ export function keyOptions(question: ReviewQuestion): KeyOption[] {
88
+ switch (question.type) {
89
+ case "label":
90
+ case "multi_label":
91
+ return question.options.map((option) => ({
92
+ value: option.value,
93
+ label: option.title ?? option.value,
94
+ }));
95
+ case "rating":
96
+ return Array.from({ length: question.max - question.min + 1 }, (_, i) => ({
97
+ value: question.min + i,
98
+ label: String(question.min + i),
99
+ }));
100
+ case "boolean":
101
+ return [
102
+ { value: true, label: "Yes" },
103
+ { value: false, label: "No" },
104
+ ];
105
+ case "text":
106
+ return [];
107
+ }
108
+ }
109
+
110
+ /** a multi label pick toggles, every other pick replaces */
111
+ export function pick(
112
+ question: ReviewQuestion,
113
+ current: unknown,
114
+ value: ReviewAnswer
115
+ ): ReviewAnswer {
116
+ if (question.type !== "multi_label") return value;
117
+ const list = Array.isArray(current) ? (current as string[]) : [];
118
+ return list.includes(value as string)
119
+ ? list.filter((v) => v !== value)
120
+ : [...list, value as string];
121
+ }
122
+
123
+ export function formatAnswer(question: ReviewQuestion, value: unknown): string {
124
+ if (!isAnswered(value)) return "";
125
+ if (question.type === "boolean") return value ? "Yes" : "No";
126
+ const options = keyOptions(question);
127
+ const label = (v: unknown) => options.find((option) => option.value === v)?.label ?? String(v);
128
+ return Array.isArray(value) ? value.map(label).join(", ") : label(value);
129
+ }
130
+
131
+ export function quoteAt(text: string, start: number, end: number): ReviewQuote {
132
+ return {
133
+ exact: text.slice(start, Math.min(end, start + QUOTE_MAX_CHARS)),
134
+ prefix: text.slice(Math.max(0, start - QUOTE_CONTEXT_CHARS), start),
135
+ suffix: text.slice(end, end + QUOTE_CONTEXT_CHARS),
136
+ };
137
+ }
138
+
139
+ const commonSuffix = (a: string, b: string) => {
140
+ let n = 0;
141
+ while (n < a.length && n < b.length && a[a.length - 1 - n] === b[b.length - 1 - n]) n += 1;
142
+ return n;
143
+ };
144
+
145
+ const commonPrefix = (a: string, b: string) => {
146
+ let n = 0;
147
+ while (n < a.length && n < b.length && a[n] === b[n]) n += 1;
148
+ return n;
149
+ };
150
+
151
+ /** found again by the quote, the context picks between repeats, undefined is an anchor lost */
152
+ export function findQuote(
153
+ text: string,
154
+ quote: ReviewQuote
155
+ ): { start: number; end: number } | undefined {
156
+ if (!quote.exact) return undefined;
157
+ let best: { start: number; score: number } | undefined;
158
+ for (let at = text.indexOf(quote.exact); at !== -1; at = text.indexOf(quote.exact, at + 1)) {
159
+ const score =
160
+ commonSuffix(text.slice(0, at), quote.prefix) +
161
+ commonPrefix(text.slice(at + quote.exact.length), quote.suffix);
162
+ if (!best || score > best.score) best = { start: at, score };
163
+ }
164
+ return best && { start: best.start, end: best.start + quote.exact.length };
165
+ }
166
+
167
+ /** a public resolve url, a private repo image fails to load like any broken link */
168
+ export function imageUrl(uri: string): string | undefined {
169
+ if (uri.startsWith("https://")) return uri;
170
+ const match =
171
+ /^hf:\/\/(?:(datasets|spaces|models)\/)?([^/\s@]+\/[^/\s@]+)(?:@([^/\s]+))?\/(.+)$/.exec(uri);
172
+ if (!match) return undefined;
173
+ const [, kind, repo, revision, path] = match;
174
+ const prefix = kind === "datasets" || kind === "spaces" ? `${kind}/` : "";
175
+ return `https://huggingface.co/${prefix}${repo}/resolve/${encodeURIComponent(
176
+ revision ?? "main"
177
+ )}/${path}`;
178
+ }
179
+
180
+ export type DeliveryStatus = "held" | "pending" | "sending" | "sent" | "failed";
181
+
182
+ const DELIVERY_LABEL: Record<DeliveryStatus, string> = {
183
+ held: "Posts as your message once you answer the publish question",
184
+ pending: "Posts as your message once the current turn ends",
185
+ sending: "Posting as your message",
186
+ sent: "Posted as your message",
187
+ failed: "Could not post this review to the chat",
188
+ };
189
+
190
+ /** a submission the turn had moved past goes out as the user's next message */
191
+ export const deliveryLabel = (status: DeliveryStatus): string => DELIVERY_LABEL[status];
src/lib/utils/sendElicitationAnswer.ts CHANGED
@@ -15,7 +15,7 @@ import type {
15
  * store reads public env at load, and this module is reached from components that render
16
  * without one.
17
  */
18
- async function mcpSelection() {
19
  try {
20
  const { enabledServers, mcpServersLoaded } = await import("$lib/stores/mcpServers");
21
  if (!get(mcpServersLoaded)) return {};
 
15
  * store reads public env at load, and this module is reached from components that render
16
  * without one.
17
  */
18
+ export async function mcpSelection() {
19
  try {
20
  const { enabledServers, mcpServersLoaded } = await import("$lib/stores/mcpServers");
21
  if (!get(mcpServersLoaded)) return {};
src/lib/utils/slackRequest.spec.ts ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { expect, it } from "vitest";
2
+ import { threadPrompt } from "$lib/server/slack/context";
3
+ import { splitSlackRequest } from "./slackRequest";
4
+
5
+ const prompt = (text: string, ts = "101.0") =>
6
+ threadPrompt(
7
+ [{ user: "U2", ts: "100.0", text: "Use the 4x model we discussed" }],
8
+ { user: "U1", ts, text },
9
+ "UBOT"
10
+ );
11
+
12
+ it("separates requests, including joined follow-ups, from their Slack framing", () => {
13
+ const { request, context } = splitSlackRequest(
14
+ `${prompt("Hey <@UBOT> can you pick an upscaler?")}\n\n${prompt("and push it", "102.0")}`
15
+ );
16
+ expect(request).toBe("Hey can you pick an upscaler?\n\nand push it");
17
+ expect(context?.match(/The invoking Slack user is U1\./g)).toHaveLength(2);
18
+ expect(context).toContain("Use the 4x model we discussed");
19
+ expect(splitSlackRequest("Plain message").context).toBeUndefined();
20
+ });
21
+
22
+ it("keeps the request's own whitespace", () => {
23
+ const code = "fix:\n```\ndef f():\n return 1\n```";
24
+ expect(splitSlackRequest(prompt(`<@UBOT> ${code}`)).request).toBe(code);
25
+ });
src/lib/utils/slackRequest.ts ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ export const SLACK_FRAMING_START = "The invoking Slack user is ";
2
+ export const SLACK_REQUEST_MARKER = "Invoking user's request:\n";
3
+
4
+ const framing = new RegExp(`${SLACK_FRAMING_START}[\\s\\S]*?${SLACK_REQUEST_MARKER}`, "g");
5
+
6
+ export function splitSlackRequest(content: string): { request: string; context?: string } {
7
+ const blocks = content.match(framing);
8
+ if (!blocks) return { request: content };
9
+ return {
10
+ request: content.replace(framing, "").trim(),
11
+ context: blocks
12
+ .map((block) => block.slice(0, -SLACK_REQUEST_MARKER.length).trim())
13
+ .join("\n\n"),
14
+ };
15
+ }
src/routes/api/conversations/+server.ts CHANGED
@@ -3,6 +3,7 @@ import { collections } from "$lib/server/database";
3
  import { deleteMlFilesOf } from "$lib/server/mlFiles/store";
4
  import { deleteStoredFilesOf } from "$lib/server/files/toolImages";
5
  import { deleteMlRegistry } from "$lib/server/mlRegistry/store";
 
6
  import type { ObjectId } from "mongodb";
7
  import { authCondition } from "$lib/server/auth";
8
  import type { Conversation } from "$lib/types/Conversation";
@@ -53,6 +54,7 @@ export async function DELETE({ locals }) {
53
  );
54
  await collections.conversations.deleteMany({ _id: { $in: ids } });
55
  await deleteMlRegistry(ids);
 
56
  await deleteMlFilesOf(ids);
57
  await deleteStoredFilesOf(ids);
58
  }
 
3
  import { deleteMlFilesOf } from "$lib/server/mlFiles/store";
4
  import { deleteStoredFilesOf } from "$lib/server/files/toolImages";
5
  import { deleteMlRegistry } from "$lib/server/mlRegistry/store";
6
+ import { deleteMlReviewsOf } from "$lib/server/mlReviews/store";
7
  import type { ObjectId } from "mongodb";
8
  import { authCondition } from "$lib/server/auth";
9
  import type { Conversation } from "$lib/types/Conversation";
 
54
  );
55
  await collections.conversations.deleteMany({ _id: { $in: ids } });
56
  await deleteMlRegistry(ids);
57
+ await deleteMlReviewsOf(ids);
58
  await deleteMlFilesOf(ids);
59
  await deleteStoredFilesOf(ids);
60
  }
src/routes/api/v2/conversations/+server.ts CHANGED
@@ -7,6 +7,7 @@ import { collections } from "$lib/server/database";
7
  import { deleteMlFilesOf } from "$lib/server/mlFiles/store";
8
  import { deleteStoredFilesOf } from "$lib/server/files/toolImages";
9
  import { deleteMlRegistry } from "$lib/server/mlRegistry/store";
 
10
  import { authCondition } from "$lib/server/auth";
11
  import type { Conversation } from "$lib/types/Conversation";
12
  import { CONV_NUM_PER_PAGE } from "$lib/constants/pagination";
@@ -60,6 +61,7 @@ export const DELETE: RequestHandler = async ({ locals }) => {
60
  );
61
  const res = await collections.conversations.deleteMany({ _id: { $in: ids } });
62
  await deleteMlRegistry(ids);
 
63
  await deleteMlFilesOf(ids);
64
  await deleteStoredFilesOf(ids);
65
 
 
7
  import { deleteMlFilesOf } from "$lib/server/mlFiles/store";
8
  import { deleteStoredFilesOf } from "$lib/server/files/toolImages";
9
  import { deleteMlRegistry } from "$lib/server/mlRegistry/store";
10
+ import { deleteMlReviewsOf } from "$lib/server/mlReviews/store";
11
  import { authCondition } from "$lib/server/auth";
12
  import type { Conversation } from "$lib/types/Conversation";
13
  import { CONV_NUM_PER_PAGE } from "$lib/constants/pagination";
 
61
  );
62
  const res = await collections.conversations.deleteMany({ _id: { $in: ids } });
63
  await deleteMlRegistry(ids);
64
+ await deleteMlReviewsOf(ids);
65
  await deleteMlFilesOf(ids);
66
  await deleteStoredFilesOf(ids);
67
 
src/routes/api/v2/conversations/[id]/+server.ts CHANGED
@@ -6,6 +6,7 @@ import { collections } from "$lib/server/database";
6
  import { deleteMlFilesOf } from "$lib/server/mlFiles/store";
7
  import { deleteStoredFilesOf } from "$lib/server/files/toolImages";
8
  import { deleteMlRegistry } from "$lib/server/mlRegistry/store";
 
9
  import { authCondition } from "$lib/server/auth";
10
  import { ObjectId } from "mongodb";
11
  import { validModelIdSchema } from "$lib/server/models";
@@ -29,12 +30,11 @@ export const GET: RequestHandler = async ({ locals, params, url }) => {
29
  const lastAssistant = [...conversation.messages]
30
  .reverse()
31
  .find((message) => message.from === "assistant");
32
- // Share snapshots are keyed by a 7-char string id, which `new ObjectId` rejects
33
- // synchronously, and they carry no turn state of their own.
34
  const turnStateDoc =
35
- lastAssistant && typeof conversation._id !== "string"
36
  ? await collections.turnStates
37
- .findOne({ conversationId: conversation._id, messageId: lastAssistant.id })
38
  .catch(() => null)
39
  : null;
40
  const turnState: TurnStateSnapshot | undefined = turnStateDoc
@@ -49,7 +49,7 @@ export const GET: RequestHandler = async ({ locals, params, url }) => {
49
  : undefined;
50
 
51
  const slackThread = !conversation.shared
52
- ? await collections.slackThreads.findOne({ conversationId: new ObjectId(conversation._id) })
53
  : null;
54
  return superjsonResponse({
55
  slackThreadUrl: slackThread
@@ -60,7 +60,7 @@ export const GET: RequestHandler = async ({ locals, params, url }) => {
60
  model: conversation.model,
61
  preprompt: conversation.preprompt,
62
  rootMessageId: conversation.rootMessageId,
63
- id: conversation._id.toString(),
64
  updatedAt: conversation.updatedAt,
65
  modelId: conversation.model,
66
  shared: conversation.shared,
@@ -90,6 +90,7 @@ export const DELETE: RequestHandler = async ({ locals, params }) => {
90
  await deleteMlFilesOf([new ObjectId(id)]);
91
  await deleteStoredFilesOf([new ObjectId(id)]);
92
  await deleteMlRegistry([new ObjectId(id)]);
 
93
 
94
  return superjsonResponse({ success: true });
95
  };
 
6
  import { deleteMlFilesOf } from "$lib/server/mlFiles/store";
7
  import { deleteStoredFilesOf } from "$lib/server/files/toolImages";
8
  import { deleteMlRegistry } from "$lib/server/mlRegistry/store";
9
+ import { deleteMlReviewsOf } from "$lib/server/mlReviews/store";
10
  import { authCondition } from "$lib/server/auth";
11
  import { ObjectId } from "mongodb";
12
  import { validModelIdSchema } from "$lib/server/models";
 
30
  const lastAssistant = [...conversation.messages]
31
  .reverse()
32
  .find((message) => message.from === "assistant");
33
+ // share readers must not see the source conversation turn state
 
34
  const turnStateDoc =
35
+ lastAssistant && conversation.kind === "owned"
36
  ? await collections.turnStates
37
+ .findOne({ conversationId: conversation.objectId, messageId: lastAssistant.id })
38
  .catch(() => null)
39
  : null;
40
  const turnState: TurnStateSnapshot | undefined = turnStateDoc
 
49
  : undefined;
50
 
51
  const slackThread = !conversation.shared
52
+ ? await collections.slackThreads.findOne({ conversationId: conversation.objectId })
53
  : null;
54
  return superjsonResponse({
55
  slackThreadUrl: slackThread
 
60
  model: conversation.model,
61
  preprompt: conversation.preprompt,
62
  rootMessageId: conversation.rootMessageId,
63
+ id: conversation.id,
64
  updatedAt: conversation.updatedAt,
65
  modelId: conversation.model,
66
  shared: conversation.shared,
 
90
  await deleteMlFilesOf([new ObjectId(id)]);
91
  await deleteStoredFilesOf([new ObjectId(id)]);
92
  await deleteMlRegistry([new ObjectId(id)]);
93
+ await deleteMlReviewsOf([new ObjectId(id)]);
94
 
95
  return superjsonResponse({ success: true });
96
  };
src/routes/api/v2/conversations/[id]/export/+server.ts CHANGED
@@ -1,5 +1,4 @@
1
  import { error, type RequestHandler } from "@sveltejs/kit";
2
- import { ObjectId } from "mongodb";
3
  import { requireAuth } from "$lib/server/api/utils/requireAuth";
4
  import { resolveConversation } from "$lib/server/api/utils/resolveConversation";
5
  import { buildConversationTrace, traceFilename } from "$lib/server/conversationTrace";
@@ -8,11 +7,10 @@ export const GET: RequestHandler = async ({ locals, params }) => {
8
  requireAuth(locals);
9
 
10
  const id = params.id ?? "";
11
- // a share snapshot has no registry of its own, and its reader is not the owner
12
- if (id.length === 7) error(404, "Conversation not found");
13
-
14
  const conversation = await resolveConversation(id, locals);
15
- const trace = await buildConversationTrace(new ObjectId(conversation._id), conversation);
 
 
16
  const filename = traceFilename(trace.conversation.id, conversation.title);
17
 
18
  return new Response(JSON.stringify(trace, null, 2), {
 
1
  import { error, type RequestHandler } from "@sveltejs/kit";
 
2
  import { requireAuth } from "$lib/server/api/utils/requireAuth";
3
  import { resolveConversation } from "$lib/server/api/utils/resolveConversation";
4
  import { buildConversationTrace, traceFilename } from "$lib/server/conversationTrace";
 
7
  requireAuth(locals);
8
 
9
  const id = params.id ?? "";
 
 
 
10
  const conversation = await resolveConversation(id, locals);
11
+ // a share snapshot has no registry of its own, and its reader is not the owner
12
+ if (conversation.kind === "share") error(404, "Conversation not found");
13
+ const trace = await buildConversationTrace(conversation.objectId, conversation);
14
  const filename = traceFilename(trace.conversation.id, conversation.title);
15
 
16
  return new Response(JSON.stringify(trace, null, 2), {
src/routes/api/v2/conversations/[id]/files/[...name]/+server.ts CHANGED
@@ -1,5 +1,4 @@
1
  import { error, type RequestHandler } from "@sveltejs/kit";
2
- import { ObjectId } from "mongodb";
3
  import { superjsonResponse } from "$lib/server/api/utils/superjsonResponse";
4
  import { requireAuth } from "$lib/server/api/utils/requireAuth";
5
  import { resolveConversation } from "$lib/server/api/utils/resolveConversation";
@@ -12,11 +11,10 @@ export const GET: RequestHandler = async ({ locals, params, url }) => {
12
  requireAuth(locals);
13
 
14
  const id = params.id ?? "";
15
- // a share snapshot has no files of its own, and its reader is not the owner
16
- if (id.length === 7) error(404, "Conversation not found");
17
-
18
  const conversation = await resolveConversation(id, locals);
19
- const conversationId = new ObjectId(conversation._id);
 
 
20
  const name = params.name ?? "";
21
 
22
  const version = url.searchParams.get("version");
 
1
  import { error, type RequestHandler } from "@sveltejs/kit";
 
2
  import { superjsonResponse } from "$lib/server/api/utils/superjsonResponse";
3
  import { requireAuth } from "$lib/server/api/utils/requireAuth";
4
  import { resolveConversation } from "$lib/server/api/utils/resolveConversation";
 
11
  requireAuth(locals);
12
 
13
  const id = params.id ?? "";
 
 
 
14
  const conversation = await resolveConversation(id, locals);
15
+ // a share snapshot has no files of its own, and its reader is not the owner
16
+ if (conversation.kind === "share") error(404, "Conversation not found");
17
+ const { objectId: conversationId } = conversation;
18
  const name = params.name ?? "";
19
 
20
  const version = url.searchParams.get("version");
src/routes/api/v2/conversations/[id]/message/[messageId]/+server.ts CHANGED
@@ -4,7 +4,6 @@ import { requireAuth } from "$lib/server/api/utils/requireAuth";
4
  import { resolveConversation } from "$lib/server/api/utils/resolveConversation";
5
  import { collections } from "$lib/server/database";
6
  import { authCondition } from "$lib/server/auth";
7
- import { ObjectId } from "mongodb";
8
 
9
  export const DELETE: RequestHandler = async ({ locals, params }) => {
10
  requireAuth(locals);
@@ -13,6 +12,7 @@ export const DELETE: RequestHandler = async ({ locals, params }) => {
13
  const messageId = params.messageId ?? "";
14
 
15
  const conversation = await resolveConversation(id, locals);
 
16
 
17
  if (!conversation.messages.map((m) => m.id).includes(messageId)) {
18
  error(404, "Message not found");
@@ -31,7 +31,7 @@ export const DELETE: RequestHandler = async ({ locals, params }) => {
31
  });
32
 
33
  const res = await collections.conversations.updateOne(
34
- { _id: new ObjectId(conversation._id), ...authCondition(locals) },
35
  { $set: { messages: filteredMessages } }
36
  );
37
 
 
4
  import { resolveConversation } from "$lib/server/api/utils/resolveConversation";
5
  import { collections } from "$lib/server/database";
6
  import { authCondition } from "$lib/server/auth";
 
7
 
8
  export const DELETE: RequestHandler = async ({ locals, params }) => {
9
  requireAuth(locals);
 
12
  const messageId = params.messageId ?? "";
13
 
14
  const conversation = await resolveConversation(id, locals);
15
+ if (conversation.kind === "share") error(404, "Conversation not found");
16
 
17
  if (!conversation.messages.map((m) => m.id).includes(messageId)) {
18
  error(404, "Message not found");
 
31
  });
32
 
33
  const res = await collections.conversations.updateOne(
34
+ { _id: conversation.objectId, ...authCondition(locals) },
35
  { $set: { messages: filteredMessages } }
36
  );
37
 
src/routes/api/v2/conversations/[id]/registry/+server.ts CHANGED
@@ -1,5 +1,4 @@
1
  import { error, type RequestHandler } from "@sveltejs/kit";
2
- import { ObjectId } from "mongodb";
3
  import { superjsonResponse } from "$lib/server/api/utils/superjsonResponse";
4
  import { requireAuth } from "$lib/server/api/utils/requireAuth";
5
  import { resolveConversation } from "$lib/server/api/utils/resolveConversation";
@@ -28,11 +27,10 @@ export const GET: RequestHandler = async ({ locals, params }) => {
28
  requireAuth(locals);
29
 
30
  const id = params.id ?? "";
31
- // a share snapshot has no registry of its own, and its reader is not the owner
32
- if (id.length === 7) error(404, "Conversation not found");
33
-
34
  const conversation = await resolveConversation(id, locals);
35
- const conversationId = new ObjectId(conversation._id);
 
 
36
  const [services, agentRuns, artefacts, files, sources] = await Promise.all([
37
  listMlServices(conversationId),
38
  listMlAgentRuns(conversationId),
 
1
  import { error, type RequestHandler } from "@sveltejs/kit";
 
2
  import { superjsonResponse } from "$lib/server/api/utils/superjsonResponse";
3
  import { requireAuth } from "$lib/server/api/utils/requireAuth";
4
  import { resolveConversation } from "$lib/server/api/utils/resolveConversation";
 
27
  requireAuth(locals);
28
 
29
  const id = params.id ?? "";
 
 
 
30
  const conversation = await resolveConversation(id, locals);
31
+ // a share snapshot has no registry of its own, and its reader is not the owner
32
+ if (conversation.kind === "share") error(404, "Conversation not found");
33
+ const { objectId: conversationId } = conversation;
34
  const [services, agentRuns, artefacts, files, sources] = await Promise.all([
35
  listMlServices(conversationId),
36
  listMlAgentRuns(conversationId),
src/routes/api/v2/conversations/[id]/registry/[serviceId]/stop/+server.ts CHANGED
@@ -8,15 +8,14 @@ export const POST: RequestHandler = async ({ locals, params }) => {
8
  requireAuth(locals);
9
 
10
  const id = params.id ?? "";
11
- // a share snapshot has no registry of its own, and its reader is not the owner
12
- if (id.length === 7) error(404, "Conversation not found");
13
-
14
  const conversation = await resolveConversation(id, locals);
 
 
15
  const serviceId = params.serviceId ?? "";
16
  if (!ObjectId.isValid(serviceId)) error(404, "No such sandbox in this conversation.");
17
 
18
  const outcome = await stopSandbox({
19
- conversationId: new ObjectId(conversation._id),
20
  serviceId: new ObjectId(serviceId),
21
  requestToken: locals.token,
22
  });
 
8
  requireAuth(locals);
9
 
10
  const id = params.id ?? "";
 
 
 
11
  const conversation = await resolveConversation(id, locals);
12
+ // a share snapshot has no registry of its own, and its reader is not the owner
13
+ if (conversation.kind === "share") error(404, "Conversation not found");
14
  const serviceId = params.serviceId ?? "";
15
  if (!ObjectId.isValid(serviceId)) error(404, "No such sandbox in this conversation.");
16
 
17
  const outcome = await stopSandbox({
18
+ conversationId: conversation.objectId,
19
  serviceId: new ObjectId(serviceId),
20
  requestToken: locals.token,
21
  });
src/routes/api/v2/conversations/[id]/reviews/[reviewId]/+server.ts ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { json, type RequestHandler } from "@sveltejs/kit";
2
+ import { superjsonResponse } from "$lib/server/api/utils/superjsonResponse";
3
+ import { ownedReview, patchDraft, reviewPayload } from "$lib/server/mlReviews/api";
4
+
5
+ export const GET: RequestHandler = async ({ locals, params }) => {
6
+ const review = await ownedReview(locals, params.id ?? "", params.reviewId ?? "");
7
+ return superjsonResponse(reviewPayload(review));
8
+ };
9
+
10
+ export const PATCH: RequestHandler = async ({ locals, params, request }) => {
11
+ const review = await ownedReview(locals, params.id ?? "", params.reviewId ?? "");
12
+ const saved = await patchDraft(review, await request.json().catch(() => null));
13
+ if ("conflict" in saved) {
14
+ return superjsonResponse(
15
+ {
16
+ message: "This record was changed elsewhere, its current draft is attached.",
17
+ draft: saved.conflict,
18
+ },
19
+ { status: 409 }
20
+ );
21
+ }
22
+ return json({ ok: true, ...saved });
23
+ };
src/routes/api/v2/conversations/[id]/reviews/[reviewId]/media/[index]/[field]/+server.ts ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { error, type RequestHandler } from "@sveltejs/kit";
2
+ import { ownedReview } from "$lib/server/mlReviews/api";
3
+ import { logger } from "$lib/server/logger";
4
+ import { reviewMedia } from "$lib/server/mlReviews/media";
5
+ import { keepMediaLinks } from "$lib/server/mlReviews/store";
6
+
7
+ /** the bytes come from a dataset row, nothing in them may run or be sniffed into something that does */
8
+ const SAFE_HEADERS = {
9
+ "X-Content-Type-Options": "nosniff",
10
+ "Content-Security-Policy": "default-src 'none'; sandbox",
11
+ "Content-Disposition": "inline",
12
+ };
13
+
14
+ function byteRange(header: string | null, size: number): [number, number] | undefined {
15
+ const match = header ? /^bytes=(\d*)-(\d*)$/.exec(header.trim()) : null;
16
+ if (!match || (!match[1] && !match[2])) return undefined;
17
+ const start = match[1] ? Number(match[1]) : Math.max(0, size - Number(match[2]));
18
+ const end = match[1] && match[2] ? Math.min(size - 1, Number(match[2])) : size - 1;
19
+ return start <= end && start < size ? [start, end] : undefined;
20
+ }
21
+
22
+ export const GET: RequestHandler = async ({ locals, params, request }) => {
23
+ const review = await ownedReview(locals, params.id ?? "", params.reviewId ?? "");
24
+ const index = Number(params.index);
25
+ const outcome = await reviewMedia(review, index, params.field ?? "", { token: locals.token });
26
+ if ("status" in outcome) error(outcome.status, outcome.message);
27
+ if ("redirect" in outcome) {
28
+ if (outcome.links) {
29
+ await keepMediaLinks(review._id, outcome.links).catch((err) =>
30
+ logger.warn({ err, reviewId: review._id.toString() }, "[review] could not keep media links")
31
+ );
32
+ }
33
+ // the viewer signs its links for about an hour, so this answer must not be kept longer
34
+ return new Response(null, {
35
+ status: 302,
36
+ headers: { Location: outcome.redirect, "Cache-Control": "private, max-age=600" },
37
+ });
38
+ }
39
+ const { bytes, contentType } = outcome;
40
+ const headers = {
41
+ ...SAFE_HEADERS,
42
+ "Content-Type": contentType,
43
+ "Cache-Control": "private, max-age=3600",
44
+ "Accept-Ranges": "bytes",
45
+ };
46
+ const range = byteRange(request.headers.get("range"), bytes.byteLength);
47
+ if (range) {
48
+ const [start, end] = range;
49
+ return new Response(bytes.slice(start, end + 1), {
50
+ status: 206,
51
+ headers: {
52
+ ...headers,
53
+ "Content-Range": `bytes ${start}-${end}/${bytes.byteLength}`,
54
+ "Content-Length": String(end - start + 1),
55
+ },
56
+ });
57
+ }
58
+ return new Response(new Uint8Array(bytes), {
59
+ headers: { ...headers, "Content-Length": String(bytes.byteLength) },
60
+ });
61
+ };
src/routes/api/v2/conversations/[id]/reviews/[reviewId]/status/+server.ts ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ import type { RequestHandler } from "@sveltejs/kit";
2
+ import { superjsonResponse } from "$lib/server/api/utils/superjsonResponse";
3
+ import { ownedReviewStatus, reviewStatusPayload } from "$lib/server/mlReviews/api";
4
+
5
+ export const GET: RequestHandler = async ({ locals, params }) => {
6
+ const review = await ownedReviewStatus(locals, params.id ?? "", params.reviewId ?? "");
7
+ return superjsonResponse(reviewStatusPayload(review));
8
+ };
src/routes/api/v2/conversations/[id]/reviews/[reviewId]/submit/+server.ts ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { json, type RequestHandler } from "@sveltejs/kit";
2
+ import { ownedReview, submitReviewRequest } from "$lib/server/mlReviews/api";
3
+
4
+ export const POST: RequestHandler = async ({ locals, params, request }) => {
5
+ const review = await ownedReview(locals, params.id ?? "", params.reviewId ?? "");
6
+ const outcome = await submitReviewRequest(review, await request.json().catch(() => null));
7
+ if ("blockers" in outcome) {
8
+ return json(
9
+ { message: "Some answers are not valid.", blockers: outcome.blockers },
10
+ { status: 400 }
11
+ );
12
+ }
13
+ return json(outcome);
14
+ };
src/routes/api/v2/conversations/[id]/runs/[runId]/+server.ts CHANGED
@@ -11,14 +11,13 @@ export const GET: RequestHandler = async ({ locals, params }) => {
11
  requireAuth(locals);
12
 
13
  const id = params.id ?? "";
14
- // a share snapshot has no runs of its own, and its reader is not the owner
15
- if (id.length === 7) error(404, "Conversation not found");
16
-
17
  const conversation = await resolveConversation(id, locals);
 
 
18
  const runId = params.runId ?? "";
19
  if (!ObjectId.isValid(runId) || runId.length !== 24) error(404, "Run not found");
20
 
21
- const run = await readMlAgentRun(new ObjectId(conversation._id), new ObjectId(runId));
22
  if (!run) error(404, "Run not found");
23
  return superjsonResponse(toDetail(run));
24
  };
 
11
  requireAuth(locals);
12
 
13
  const id = params.id ?? "";
 
 
 
14
  const conversation = await resolveConversation(id, locals);
15
+ // a share snapshot has no runs of its own, and its reader is not the owner
16
+ if (conversation.kind === "share") error(404, "Conversation not found");
17
  const runId = params.runId ?? "";
18
  if (!ObjectId.isValid(runId) || runId.length !== 24) error(404, "Run not found");
19
 
20
+ const run = await readMlAgentRun(conversation.objectId, new ObjectId(runId));
21
  if (!run) error(404, "Run not found");
22
  return superjsonResponse(toDetail(run));
23
  };
src/routes/conversation/[id]/+server.ts CHANGED
@@ -3,6 +3,7 @@ import { authCondition } from "$lib/server/auth";
3
  import { collections } from "$lib/server/database";
4
  import { deleteMlFilesOf } from "$lib/server/mlFiles/store";
5
  import { deleteMlRegistry } from "$lib/server/mlRegistry/store";
 
6
  import { validModelIdSchema } from "$lib/server/models";
7
  import { applyConversationSettings } from "$lib/server/conversationSettings";
8
  import { ObjectId } from "mongodb";
@@ -26,6 +27,7 @@ export async function DELETE({ locals, params }) {
26
  await deleteMlFilesOf([conv._id]);
27
  await deleteStoredFilesOf([conv._id]);
28
  await deleteMlRegistry([conv._id]);
 
29
 
30
  return new Response();
31
  }
 
3
  import { collections } from "$lib/server/database";
4
  import { deleteMlFilesOf } from "$lib/server/mlFiles/store";
5
  import { deleteMlRegistry } from "$lib/server/mlRegistry/store";
6
+ import { deleteMlReviewsOf } from "$lib/server/mlReviews/store";
7
  import { validModelIdSchema } from "$lib/server/models";
8
  import { applyConversationSettings } from "$lib/server/conversationSettings";
9
  import { ObjectId } from "mongodb";
 
27
  await deleteMlFilesOf([conv._id]);
28
  await deleteStoredFilesOf([conv._id]);
29
  await deleteMlRegistry([conv._id]);
30
+ await deleteMlReviewsOf([conv._id]);
31
 
32
  return new Response();
33
  }
src/styles/main.css CHANGED
@@ -310,3 +310,15 @@ body {
310
  background-position: -200% 0;
311
  }
312
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
310
  background-position: -200% 0;
311
  }
312
  }
313
+
314
+ /* review comments anchored on a quote, set by quoteAnchors */
315
+ ::highlight(review-quote) {
316
+ text-decoration-line: underline;
317
+ text-decoration-color: #fde68a;
318
+ text-decoration-thickness: 3px;
319
+ text-underline-offset: 2px;
320
+ }
321
+
322
+ .dark ::highlight(review-quote) {
323
+ text-decoration-color: rgb(251 191 36 / 0.45);
324
+ }