-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.ts
More file actions
55 lines (50 loc) · 1.91 KB
/
Copy pathschema.ts
File metadata and controls
55 lines (50 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import type { EnsembleResult } from "../types.js";
/**
* Validate that a parsed JSON object has the required shape of an EnsembleResult.
* Returns null on success, or a descriptive error string on failure.
*/
export function validateResult(data: unknown): string | null {
if (data === null || typeof data !== "object") {
return "result must be a non-null object";
}
const obj = data as Record<string, unknown>;
if (typeof obj.prompt !== "string") {
return "missing or invalid field: prompt (expected string)";
}
if (typeof obj.model !== "string") {
return "missing or invalid field: model (expected string)";
}
if (typeof obj.timestamp !== "string") {
return "missing or invalid field: timestamp (expected string)";
}
if (obj.scoring !== undefined && obj.scoring !== "weighted" && obj.scoring !== "copeland") {
return 'invalid field: scoring (expected "weighted", "copeland", or omitted)';
}
if (!Array.isArray(obj.agents)) {
return "missing or invalid field: agents (expected array)";
}
if (!Array.isArray(obj.tests)) {
return "missing or invalid field: tests (expected array)";
}
if (!Array.isArray(obj.convergence)) {
return "missing or invalid field: convergence (expected array)";
}
if (obj.recommended !== null && typeof obj.recommended !== "number") {
return "missing or invalid field: recommended (expected number or null)";
}
if (obj.scores !== undefined && !Array.isArray(obj.scores)) {
return "invalid field: scores (expected array or omitted)";
}
return null;
}
/**
* Parse JSON and validate as EnsembleResult. Returns the result or throws with a descriptive message.
*/
export function parseAndValidateResult(json: string, filename: string): EnsembleResult {
const data = JSON.parse(json);
const error = validateResult(data);
if (error) {
throw new Error(`Invalid result file ${filename}: ${error}`);
}
return data as EnsembleResult;
}