@parcha/agentrun-dsl 0.1.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +2 -0
- package/README.md +67 -0
- package/dist/authoring.d.ts +72 -0
- package/dist/authoring.js +138 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +135 -0
- package/dist/code-exec.d.ts +4 -0
- package/dist/code-exec.js +45 -0
- package/dist/demo.d.ts +54 -0
- package/dist/demo.js +80 -0
- package/dist/dry-run.d.ts +19 -0
- package/dist/dry-run.js +191 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +8 -0
- package/dist/inspection.d.ts +35 -0
- package/dist/inspection.js +107 -0
- package/dist/predicates.d.ts +34 -0
- package/dist/predicates.js +43 -0
- package/dist/schema-references.d.ts +3 -0
- package/dist/schema-references.js +66 -0
- package/dist/system-one.d.ts +78 -0
- package/dist/system-one.js +193 -0
- package/dist/workflow-shape.d.ts +1 -0
- package/dist/workflow-shape.js +181 -0
- package/dist/workflow.d.ts +541 -0
- package/dist/workflow.js +2294 -0
- package/dist/zod-json-contract.d.ts +1 -0
- package/dist/zod-json-contract.js +106 -0
- package/package.json +52 -0
- package/schema/workflow.schema.json +1850 -0
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
function collectRefNames(schema, out) {
|
|
2
|
+
if (Array.isArray(schema)) {
|
|
3
|
+
for (const item of schema)
|
|
4
|
+
collectRefNames(item, out);
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
if (!schema || typeof schema !== "object")
|
|
8
|
+
return;
|
|
9
|
+
for (const [key, value] of Object.entries(schema)) {
|
|
10
|
+
if (key === "$ref" && typeof value === "string") {
|
|
11
|
+
const match = value.match(/^#\/definitions\/(.+)$/);
|
|
12
|
+
if (match)
|
|
13
|
+
out.add(match[1]);
|
|
14
|
+
}
|
|
15
|
+
else
|
|
16
|
+
collectRefNames(value, out);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export function resolveSchemaForWorkflow(workflow, schema) {
|
|
20
|
+
const needed = new Set();
|
|
21
|
+
collectRefNames(schema, needed);
|
|
22
|
+
if (!needed.size)
|
|
23
|
+
return schema;
|
|
24
|
+
const definitions = Object.assign(Object.create(null), schema.definitions || {});
|
|
25
|
+
const queue = [...needed];
|
|
26
|
+
while (queue.length) {
|
|
27
|
+
const name = queue.pop();
|
|
28
|
+
if (definitions[name] !== undefined)
|
|
29
|
+
continue;
|
|
30
|
+
const target = Object.hasOwn(workflow.schemas, name) ? workflow.schemas[name] : undefined;
|
|
31
|
+
if (target === undefined)
|
|
32
|
+
continue;
|
|
33
|
+
definitions[name] = target;
|
|
34
|
+
const transitive = new Set();
|
|
35
|
+
collectRefNames(target, transitive);
|
|
36
|
+
for (const next of transitive)
|
|
37
|
+
if (definitions[next] === undefined)
|
|
38
|
+
queue.push(next);
|
|
39
|
+
}
|
|
40
|
+
// TypeBox's reference cache treats Object.prototype names specially. Alias only those
|
|
41
|
+
// declared names at the compiler boundary; retain original definitions for diagnostics.
|
|
42
|
+
const aliases = new Map();
|
|
43
|
+
for (const name of Object.keys(definitions)) {
|
|
44
|
+
if (!Object.hasOwn(Object.prototype, name))
|
|
45
|
+
continue;
|
|
46
|
+
let alias = `agentrun_definition_${Buffer.from(name).toString("hex")}`;
|
|
47
|
+
while (Object.hasOwn(definitions, alias))
|
|
48
|
+
alias += "_";
|
|
49
|
+
aliases.set(name, alias);
|
|
50
|
+
definitions[alias] = definitions[name];
|
|
51
|
+
}
|
|
52
|
+
const resolved = { ...schema, definitions: { ...definitions } };
|
|
53
|
+
if (!aliases.size)
|
|
54
|
+
return resolved;
|
|
55
|
+
const rewrite = (value) => {
|
|
56
|
+
if (Array.isArray(value))
|
|
57
|
+
return value.map(rewrite);
|
|
58
|
+
if (!value || typeof value !== "object")
|
|
59
|
+
return value;
|
|
60
|
+
return Object.fromEntries(Object.entries(value).map(([key, item]) => {
|
|
61
|
+
const name = key === "$ref" && typeof item === "string" ? item.match(/^#\/definitions\/(.+)$/)?.[1] : undefined;
|
|
62
|
+
return [key, name && aliases.has(name) ? `#/definitions/${aliases.get(name)}` : rewrite(item)];
|
|
63
|
+
}));
|
|
64
|
+
};
|
|
65
|
+
return rewrite(resolved);
|
|
66
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
export type SystemOneQuestion = {
|
|
2
|
+
type: "choice";
|
|
3
|
+
instructions: string;
|
|
4
|
+
criteria: Record<string, string | null>;
|
|
5
|
+
} | {
|
|
6
|
+
type: "noul";
|
|
7
|
+
instructions: string;
|
|
8
|
+
criteria?: {
|
|
9
|
+
true?: string;
|
|
10
|
+
false?: string;
|
|
11
|
+
};
|
|
12
|
+
} | {
|
|
13
|
+
type: "score";
|
|
14
|
+
instructions: string;
|
|
15
|
+
criteria: string[];
|
|
16
|
+
};
|
|
17
|
+
export type SystemOneAnswer = {
|
|
18
|
+
type: "choice";
|
|
19
|
+
choice: string;
|
|
20
|
+
probabilities: Record<string, number>;
|
|
21
|
+
confidence: number;
|
|
22
|
+
} | {
|
|
23
|
+
type: "noul";
|
|
24
|
+
noul: number;
|
|
25
|
+
} | {
|
|
26
|
+
type: "score";
|
|
27
|
+
score: number;
|
|
28
|
+
legend: Record<string, string>;
|
|
29
|
+
probabilities: Record<string, number>;
|
|
30
|
+
confidence: number;
|
|
31
|
+
};
|
|
32
|
+
export type SystemOneResult = {
|
|
33
|
+
answers: Record<string, SystemOneAnswer>;
|
|
34
|
+
model: string | null;
|
|
35
|
+
usage: {
|
|
36
|
+
input_tokens: number;
|
|
37
|
+
output_tokens: number;
|
|
38
|
+
} | null;
|
|
39
|
+
cost_usd: number | null;
|
|
40
|
+
latency_ms: number;
|
|
41
|
+
request_sha256: string;
|
|
42
|
+
};
|
|
43
|
+
export declare const SYSTEM_ONE_LIMITS: {
|
|
44
|
+
readonly maxChoiceOptions: 240;
|
|
45
|
+
readonly minScoreLevels: 2;
|
|
46
|
+
readonly maxScoreLevels: 10;
|
|
47
|
+
};
|
|
48
|
+
export declare const SYSTEM_ONE_DEFAULT_MODEL = "jev-latest";
|
|
49
|
+
export type CompiledQuestions = Record<string, SystemOneQuestion>;
|
|
50
|
+
export declare function compileQuestions(schema: Record<string, unknown>, definitions?: Record<string, unknown>): {
|
|
51
|
+
ok: true;
|
|
52
|
+
questions: CompiledQuestions;
|
|
53
|
+
} | {
|
|
54
|
+
ok: false;
|
|
55
|
+
errors: string[];
|
|
56
|
+
};
|
|
57
|
+
export declare function answersToValue(answers: Record<string, SystemOneAnswer>, questions: CompiledQuestions): Record<string, unknown>;
|
|
58
|
+
export declare function answerConfidence(answer: SystemOneAnswer): number;
|
|
59
|
+
export type AnswersSidecar = {
|
|
60
|
+
answers: Record<string, SystemOneAnswer>;
|
|
61
|
+
confidence: Record<string, number>;
|
|
62
|
+
weakest: string | null;
|
|
63
|
+
min_confidence: number | null;
|
|
64
|
+
};
|
|
65
|
+
export declare function answersSidecar(answers: Record<string, SystemOneAnswer>): AnswersSidecar;
|
|
66
|
+
export declare function synthesizeAnswers(questions: Record<string, SystemOneQuestion>): Record<string, SystemOneAnswer>;
|
|
67
|
+
export type SystemOneRetryClass = "http_429" | "http_5xx" | "connection" | "timeout";
|
|
68
|
+
declare const responseReasons: readonly ["answers_shape", "answer_keys", "answer_shape", "answer_type", "noul_probability", "confidence", "probabilities", "probability_keys", "probability_mass", "score_legend", "score_consistency", "choice_option", "score_range"];
|
|
69
|
+
export type SystemOneResponseReason = typeof responseReasons[number];
|
|
70
|
+
export declare function isSystemOneResponseReason(value: unknown): value is SystemOneResponseReason;
|
|
71
|
+
export declare class SystemOneError extends Error {
|
|
72
|
+
readonly retryClass: SystemOneRetryClass | null;
|
|
73
|
+
readonly status?: number | undefined;
|
|
74
|
+
readonly responseReason?: SystemOneResponseReason;
|
|
75
|
+
constructor(message: string, retryClass: SystemOneRetryClass | null, status?: number | undefined, responseReason?: SystemOneResponseReason);
|
|
76
|
+
}
|
|
77
|
+
export declare function validateAnswers(questions: Record<string, SystemOneQuestion>, answers: unknown): asserts answers is Record<string, SystemOneAnswer>;
|
|
78
|
+
export {};
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
export const SYSTEM_ONE_LIMITS = { maxChoiceOptions: 240, minScoreLevels: 2, maxScoreLevels: 10 };
|
|
2
|
+
export const SYSTEM_ONE_DEFAULT_MODEL = "jev-latest";
|
|
3
|
+
const CRITERIA = "criteria";
|
|
4
|
+
export function compileQuestions(schema, definitions = {}) {
|
|
5
|
+
const errors = [];
|
|
6
|
+
// Question IDs and choice labels are arbitrary JSON keys, including "__proto__".
|
|
7
|
+
// Null-prototype construction prevents setters; object spread returns ordinary public records.
|
|
8
|
+
const questions = Object.create(null);
|
|
9
|
+
const resolve = (spec) => {
|
|
10
|
+
const seen = new Set();
|
|
11
|
+
while (spec && typeof spec.$ref === "string") {
|
|
12
|
+
if (seen.has(spec))
|
|
13
|
+
return undefined;
|
|
14
|
+
seen.add(spec);
|
|
15
|
+
const m = /^#\/definitions\/(.+)$/.exec(spec.$ref);
|
|
16
|
+
const inline = schema.definitions;
|
|
17
|
+
spec = m ? (inline && Object.hasOwn(inline, m[1]) ? inline[m[1]] : Object.hasOwn(definitions, m[1]) ? definitions[m[1]] : undefined) : undefined;
|
|
18
|
+
}
|
|
19
|
+
return spec;
|
|
20
|
+
};
|
|
21
|
+
const root = resolve(schema);
|
|
22
|
+
const type = Array.isArray(root?.type) ? root.type[0] : root?.type;
|
|
23
|
+
if (!root || (type !== "object" && !root.properties))
|
|
24
|
+
return { ok: false, errors: ["a question schema is a flat object schema"] };
|
|
25
|
+
const properties = (root.properties || {});
|
|
26
|
+
if (!Object.keys(properties).length)
|
|
27
|
+
return { ok: false, errors: ["a question schema declares at least one property"] };
|
|
28
|
+
for (const [id, rawSpec] of Object.entries(properties)) {
|
|
29
|
+
const spec = resolve(rawSpec);
|
|
30
|
+
const instructions = typeof spec?.description === "string" && spec.description.trim() ? spec.description.trim() : "";
|
|
31
|
+
if (!instructions) {
|
|
32
|
+
errors.push(`property "${id}": a question needs a description (its instructions)`);
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const types = (Array.isArray(spec.type) ? spec.type : [spec.type]).filter((t) => t !== "null");
|
|
36
|
+
const criteria = spec[CRITERIA];
|
|
37
|
+
if (Array.isArray(spec.enum) && spec.enum.length) {
|
|
38
|
+
if (types.some((t) => t !== "string") || spec.enum.some((v) => typeof v !== "string")) {
|
|
39
|
+
errors.push(`property "${id}": a choice is a string enum`);
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
if (spec.enum.length > SYSTEM_ONE_LIMITS.maxChoiceOptions) {
|
|
43
|
+
errors.push(`property "${id}": a choice takes at most ${SYSTEM_ONE_LIMITS.maxChoiceOptions} options (${spec.enum.length} given)`);
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const map = Object.create(null);
|
|
47
|
+
for (const option of spec.enum) {
|
|
48
|
+
const text = criteria && typeof criteria === "object" && !Array.isArray(criteria) ? criteria[option] : undefined;
|
|
49
|
+
map[option] = typeof text === "string" && text.trim() ? text.trim() : null;
|
|
50
|
+
}
|
|
51
|
+
if (criteria && typeof criteria === "object" && !Array.isArray(criteria)) {
|
|
52
|
+
const unknown = Object.keys(criteria).filter((k) => !spec.enum.includes(k));
|
|
53
|
+
if (unknown.length)
|
|
54
|
+
errors.push(`property "${id}": criteria name options the enum does not have: ${unknown.join(", ")}`);
|
|
55
|
+
}
|
|
56
|
+
questions[id] = { type: "choice", instructions, criteria: { ...map } };
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if (types.length === 1 && types[0] === "boolean") {
|
|
60
|
+
const c = criteria && typeof criteria === "object" && !Array.isArray(criteria) ? criteria : undefined;
|
|
61
|
+
questions[id] = { type: "noul", instructions, ...(c ? { criteria: { ...(typeof c.true === "string" ? { true: c.true } : {}), ...(typeof c.false === "string" ? { false: c.false } : {}) } } : {}) };
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (types.length === 1 && types[0] === "integer") {
|
|
65
|
+
const levels = Array.isArray(criteria) ? criteria : null;
|
|
66
|
+
if (!levels || levels.some((l) => typeof l !== "string" || !l.trim())) {
|
|
67
|
+
errors.push(`property "${id}": a score is an integer with criteria: [level descriptions] (${SYSTEM_ONE_LIMITS.minScoreLevels}..${SYSTEM_ONE_LIMITS.maxScoreLevels} levels)`);
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (levels.length < SYSTEM_ONE_LIMITS.minScoreLevels || levels.length > SYSTEM_ONE_LIMITS.maxScoreLevels) {
|
|
71
|
+
errors.push(`property "${id}": a score takes ${SYSTEM_ONE_LIMITS.minScoreLevels}..${SYSTEM_ONE_LIMITS.maxScoreLevels} levels (${levels.length} given)`);
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (spec.minimum !== undefined && spec.minimum !== 0)
|
|
75
|
+
errors.push(`property "${id}": a score's minimum is 0 (level 0)`);
|
|
76
|
+
if (spec.maximum !== undefined && spec.maximum !== levels.length - 1)
|
|
77
|
+
errors.push(`property "${id}": a score's maximum is its last level index (${levels.length - 1})`);
|
|
78
|
+
questions[id] = { type: "score", instructions, criteria: levels };
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
errors.push(`property "${id}": not a question — a question is a string enum (choice), a boolean (noul), or an integer with level criteria (score); prose and lists belong to a decide node`);
|
|
82
|
+
}
|
|
83
|
+
return errors.length ? { ok: false, errors } : { ok: true, questions: { ...questions } };
|
|
84
|
+
}
|
|
85
|
+
export function answersToValue(answers, questions) {
|
|
86
|
+
const out = Object.create(null);
|
|
87
|
+
for (const [id, q] of Object.entries(questions)) {
|
|
88
|
+
const a = answers[id];
|
|
89
|
+
if (!a)
|
|
90
|
+
throw new Error(`System One returned no answer for question "${id}"`);
|
|
91
|
+
if (q.type === "choice" && a.type === "choice")
|
|
92
|
+
out[id] = a.choice;
|
|
93
|
+
else if (q.type === "noul" && a.type === "noul")
|
|
94
|
+
out[id] = a.noul >= 0.5;
|
|
95
|
+
else if (q.type === "score" && a.type === "score")
|
|
96
|
+
out[id] = Math.max(0, Math.round(a.score));
|
|
97
|
+
else
|
|
98
|
+
throw new Error(`System One answered question "${id}" with type "${a.type}", expected ${q.type}`);
|
|
99
|
+
}
|
|
100
|
+
return { ...out };
|
|
101
|
+
}
|
|
102
|
+
export function answerConfidence(answer) {
|
|
103
|
+
if (!answer || typeof answer !== "object")
|
|
104
|
+
throw new SystemOneError("System One answer missing for a question that was asked", null);
|
|
105
|
+
if (answer.type === "noul")
|
|
106
|
+
return Math.abs(answer.noul - 0.5) * 2;
|
|
107
|
+
return typeof answer.confidence === "number" ? answer.confidence : 0;
|
|
108
|
+
}
|
|
109
|
+
export function answersSidecar(answers) {
|
|
110
|
+
const confidence = Object.create(null);
|
|
111
|
+
let weakest = null;
|
|
112
|
+
for (const [id, a] of Object.entries(answers)) {
|
|
113
|
+
const c = +answerConfidence(a).toFixed(4);
|
|
114
|
+
confidence[id] = c;
|
|
115
|
+
if (!weakest || c < weakest.confidence)
|
|
116
|
+
weakest = { question: id, confidence: c };
|
|
117
|
+
}
|
|
118
|
+
return { answers, confidence: { ...confidence }, weakest: weakest?.question ?? null, min_confidence: weakest?.confidence ?? null };
|
|
119
|
+
}
|
|
120
|
+
export function synthesizeAnswers(questions) {
|
|
121
|
+
const out = Object.create(null);
|
|
122
|
+
for (const [id, q] of Object.entries(questions)) {
|
|
123
|
+
if (q.type === "choice") {
|
|
124
|
+
const options = Object.keys(q.criteria);
|
|
125
|
+
out[id] = { type: "choice", choice: options[0], probabilities: Object.fromEntries(options.map((o) => [o, 1 / options.length])), confidence: 0 };
|
|
126
|
+
}
|
|
127
|
+
else if (q.type === "noul")
|
|
128
|
+
out[id] = { type: "noul", noul: 0.5 };
|
|
129
|
+
else
|
|
130
|
+
out[id] = { type: "score", score: (q.criteria.length - 1) / 2, legend: Object.fromEntries(q.criteria.map((l, i) => [String(i), l])), probabilities: Object.fromEntries(q.criteria.map((_, i) => [String(i), 1 / q.criteria.length])), confidence: 0 };
|
|
131
|
+
}
|
|
132
|
+
return { ...out };
|
|
133
|
+
}
|
|
134
|
+
const responseReasons = ["answers_shape", "answer_keys", "answer_shape", "answer_type", "noul_probability",
|
|
135
|
+
"confidence", "probabilities", "probability_keys", "probability_mass", "score_legend", "score_consistency", "choice_option", "score_range"];
|
|
136
|
+
export function isSystemOneResponseReason(value) {
|
|
137
|
+
return typeof value === "string" && responseReasons.includes(value);
|
|
138
|
+
}
|
|
139
|
+
export class SystemOneError extends Error {
|
|
140
|
+
retryClass;
|
|
141
|
+
status;
|
|
142
|
+
responseReason;
|
|
143
|
+
constructor(message, retryClass, status, responseReason) {
|
|
144
|
+
super(message);
|
|
145
|
+
this.retryClass = retryClass;
|
|
146
|
+
this.status = status;
|
|
147
|
+
this.name = "SystemOneError";
|
|
148
|
+
if (isSystemOneResponseReason(responseReason))
|
|
149
|
+
this.responseReason = responseReason;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
const unit = (v) => typeof v === "number" && Number.isFinite(v) && v >= 0 && v <= 1;
|
|
153
|
+
export function validateAnswers(questions, answers) {
|
|
154
|
+
if (!answers || typeof answers !== "object" || Array.isArray(answers))
|
|
155
|
+
throw new SystemOneError("System One response carries no answers", null, undefined, "answers_shape");
|
|
156
|
+
const answerKeys = Object.keys(answers);
|
|
157
|
+
if (answerKeys.length !== Object.keys(questions).length || answerKeys.some(id => !Object.hasOwn(questions, id)))
|
|
158
|
+
throw new SystemOneError("System One answer keys do not match questions", null, undefined, "answer_keys");
|
|
159
|
+
for (const [id, q] of Object.entries(questions)) {
|
|
160
|
+
const a = answers[id];
|
|
161
|
+
if (!a || typeof a !== "object")
|
|
162
|
+
throw new SystemOneError("System One returned no answer for a question", null, undefined, "answer_shape");
|
|
163
|
+
if (a.type !== q.type)
|
|
164
|
+
throw new SystemOneError("System One answer type does not match its question", null, undefined, "answer_type");
|
|
165
|
+
if (q.type === "noul") {
|
|
166
|
+
if (!unit(a.noul))
|
|
167
|
+
throw new SystemOneError("System One answer: noul is not a probability", null, undefined, "noul_probability");
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
if (!unit(a.confidence))
|
|
171
|
+
throw new SystemOneError("System One answer: confidence is not in [0, 1]", null, undefined, "confidence");
|
|
172
|
+
if (!a.probabilities || typeof a.probabilities !== "object" || !Object.values(a.probabilities).every(unit))
|
|
173
|
+
throw new SystemOneError("System One answer: probabilities are not a distribution", null, undefined, "probabilities");
|
|
174
|
+
const expectedKeys = q.type === "choice" ? Object.keys(q.criteria) : q.criteria.map((_, index) => String(index));
|
|
175
|
+
const probabilityKeys = Object.keys(a.probabilities);
|
|
176
|
+
if (Array.isArray(a.probabilities) || probabilityKeys.length !== expectedKeys.length || expectedKeys.some(key => !Object.hasOwn(a.probabilities, key)))
|
|
177
|
+
throw new SystemOneError("System One answer: probabilities must contain exactly the declared options", null, undefined, "probability_keys");
|
|
178
|
+
const mass = Object.values(a.probabilities).reduce((sum, p) => sum + p, 0);
|
|
179
|
+
if (Math.abs(mass - 1) > 1e-5)
|
|
180
|
+
throw new SystemOneError("System One answer: probabilities must sum to 1", null, undefined, "probability_mass");
|
|
181
|
+
if (q.type === "score") {
|
|
182
|
+
if (!a.legend || typeof a.legend !== "object" || Array.isArray(a.legend) || Object.keys(a.legend).length !== expectedKeys.length || q.criteria.some((text, index) => a.legend[String(index)] !== text))
|
|
183
|
+
throw new SystemOneError("System One answer: legend must match the score criteria", null, undefined, "score_legend");
|
|
184
|
+
const weighted = expectedKeys.reduce((sum, key) => sum + Number(key) * a.probabilities[key], 0);
|
|
185
|
+
if (Math.abs(weighted - a.score) > 1e-5)
|
|
186
|
+
throw new SystemOneError("System One answer: score must match its weighted distribution", null, undefined, "score_consistency");
|
|
187
|
+
}
|
|
188
|
+
if (q.type === "choice" && !(typeof a.choice === "string" && Object.prototype.hasOwnProperty.call(q.criteria, a.choice)))
|
|
189
|
+
throw new SystemOneError("System One answer: choice is not an option", null, undefined, "choice_option");
|
|
190
|
+
if (q.type === "score" && !(typeof a.score === "number" && Number.isFinite(a.score) && a.score >= 0 && a.score <= q.criteria.length - 1))
|
|
191
|
+
throw new SystemOneError("System One answer: score is outside its levels", null, undefined, "score_range");
|
|
192
|
+
}
|
|
193
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function workflowShapeErrors(value: unknown, complete?: boolean): string[];
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { Compile } from "typebox/compile";
|
|
3
|
+
const documentSchema = createRequire(import.meta.url)("../schema/workflow.schema.json");
|
|
4
|
+
const fullValidator = Compile(documentSchema);
|
|
5
|
+
const isRecord = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
6
|
+
// Diagnostics only: the full validator below remains the acceptance authority.
|
|
7
|
+
// Its union errors can fill the entire error limit with unrelated node kinds.
|
|
8
|
+
// Select the declared discriminator's schema, checking nested nodes separately so
|
|
9
|
+
// their own discriminators also get useful errors. Never evaluate authored code.
|
|
10
|
+
const diagnosticDefinitions = { ...documentSchema.definitions, WorkflowNode: {}, Workflow: {} };
|
|
11
|
+
const nodeSchemas = new Map(documentSchema.definitions.WorkflowNode.anyOf.map((schema) => [schema.properties.node.const, schema]));
|
|
12
|
+
const diagnosticValidators = new Map();
|
|
13
|
+
for (const schema of [documentSchema.definitions.Workflow, ...nodeSchemas.values()]) {
|
|
14
|
+
diagnosticValidators.set(schema, Compile({ ...schema, definitions: diagnosticDefinitions }));
|
|
15
|
+
}
|
|
16
|
+
const pointer = (key) => key.replace(/~/g, "~0").replace(/\//g, "~1");
|
|
17
|
+
function discriminatedErrors(value) {
|
|
18
|
+
const errors = [];
|
|
19
|
+
const check = (schema, entry, path) => {
|
|
20
|
+
if (errors.length >= 8)
|
|
21
|
+
return;
|
|
22
|
+
const validator = diagnosticValidators.get(schema);
|
|
23
|
+
if (!validator.Check(entry))
|
|
24
|
+
for (const error of validator.Errors(entry)) {
|
|
25
|
+
errors.push(`workflow${path}${error.instancePath || (path ? "" : "/")}: ${error.message}`);
|
|
26
|
+
if (errors.length >= 8)
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
visit(schema, entry, path);
|
|
30
|
+
};
|
|
31
|
+
const visit = (schema, entry, path) => {
|
|
32
|
+
if (!schema || typeof schema !== "object" || errors.length >= 8)
|
|
33
|
+
return;
|
|
34
|
+
if (schema.$ref === "#/definitions/WorkflowNode") {
|
|
35
|
+
const selected = isRecord(entry) && typeof entry.node === "string" ? nodeSchemas.get(entry.node) : undefined;
|
|
36
|
+
if (selected)
|
|
37
|
+
check(selected, entry, path);
|
|
38
|
+
return; // Unknown/missing discriminators retain the full validator's fallback.
|
|
39
|
+
}
|
|
40
|
+
if (schema.$ref === "#/definitions/Workflow") {
|
|
41
|
+
check(documentSchema.definitions.Workflow, entry, path);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (isRecord(entry)) {
|
|
45
|
+
for (const [key, child] of Object.entries(schema.properties ?? {})) {
|
|
46
|
+
if (Object.hasOwn(entry, key))
|
|
47
|
+
visit(child, entry[key], `${path}/${pointer(key)}`);
|
|
48
|
+
}
|
|
49
|
+
if (isRecord(schema.additionalProperties))
|
|
50
|
+
for (const key of Object.keys(entry)) {
|
|
51
|
+
if (!Object.hasOwn(schema.properties ?? {}, key))
|
|
52
|
+
visit(schema.additionalProperties, entry[key], `${path}/${pointer(key)}`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (Array.isArray(entry) && schema.items)
|
|
56
|
+
entry.forEach((child, index) => visit(schema.items, child, `${path}/${index}`));
|
|
57
|
+
};
|
|
58
|
+
check(documentSchema.definitions.Workflow, value, "");
|
|
59
|
+
return errors;
|
|
60
|
+
}
|
|
61
|
+
export function workflowShapeErrors(value, complete = false) {
|
|
62
|
+
if (complete) {
|
|
63
|
+
if (fullValidator.Check(value))
|
|
64
|
+
return [];
|
|
65
|
+
const selected = discriminatedErrors(value);
|
|
66
|
+
if (selected.length)
|
|
67
|
+
return selected;
|
|
68
|
+
return [...fullValidator.Errors(value)].slice(0, 8).map(error => `workflow${error.instancePath || "/"}: ${error.message}`);
|
|
69
|
+
}
|
|
70
|
+
const errors = [];
|
|
71
|
+
const fail = (path, expected) => { errors.push(`${path} must be ${expected}`); };
|
|
72
|
+
const record = (entry, path) => {
|
|
73
|
+
if (isRecord(entry))
|
|
74
|
+
return true;
|
|
75
|
+
fail(path, "an object");
|
|
76
|
+
return false;
|
|
77
|
+
};
|
|
78
|
+
const strings = (entry, path, keys) => {
|
|
79
|
+
for (const key of keys)
|
|
80
|
+
if (entry[key] !== undefined && typeof entry[key] !== "string")
|
|
81
|
+
fail(`${path}.${key}`, "a string");
|
|
82
|
+
};
|
|
83
|
+
const stringList = (entry, path) => {
|
|
84
|
+
if (entry === undefined)
|
|
85
|
+
return;
|
|
86
|
+
if (!Array.isArray(entry)) {
|
|
87
|
+
fail(path, "an array of strings");
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
entry.forEach((item, index) => { if (typeof item !== "string")
|
|
91
|
+
fail(`${path}[${index}]`, "a string"); });
|
|
92
|
+
};
|
|
93
|
+
// Preserve existing diagnostics for missing, non-finite and out-of-range numbers, and
|
|
94
|
+
// primitive strings such as poll.deadline_s="soon". Objects can throw during coercion.
|
|
95
|
+
const numeric = (entry, path, keys) => {
|
|
96
|
+
for (const key of keys) {
|
|
97
|
+
const item = entry[key];
|
|
98
|
+
if (item !== null && item !== undefined && !["number", "string", "boolean"].includes(typeof item))
|
|
99
|
+
fail(`${path}.${key}`, "a number");
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
const objectField = (entry, path, key, visit) => {
|
|
103
|
+
if (entry[key] === undefined)
|
|
104
|
+
return;
|
|
105
|
+
if (record(entry[key], `${path}.${key}`))
|
|
106
|
+
visit?.(entry[key], `${path}.${key}`);
|
|
107
|
+
};
|
|
108
|
+
const predicate = (entry, path) => {
|
|
109
|
+
strings(entry, path, ["predicate", "path", "key", "instructions"]);
|
|
110
|
+
numeric(entry, path, ["n", "gte"]);
|
|
111
|
+
objectField(entry, path, "state");
|
|
112
|
+
objectField(entry, path, "criteria", (criteria, at) => strings(criteria, at, ["true", "false"]));
|
|
113
|
+
};
|
|
114
|
+
const node = (entry, path) => {
|
|
115
|
+
if (entry === undefined)
|
|
116
|
+
return; // missing bodies already receive semantic diagnostics
|
|
117
|
+
if (!record(entry, path))
|
|
118
|
+
return;
|
|
119
|
+
strings(entry, path, ["node", "label", "code", "as", "out", "instructions", "itemsPath", "resultPath", "kind", "stage", "summary", "describe", "type", "path", "tool", "command", "via", "where", "effort", "thinking", "tier"]);
|
|
120
|
+
numeric(entry, path, ["maxConcurrency", "maxIters", "deadline_s"]);
|
|
121
|
+
for (const key of ["requires", "tools", "produces"])
|
|
122
|
+
stringList(entry[key], `${path}.${key}`);
|
|
123
|
+
if (entry.sopSection !== undefined && typeof entry.sopSection !== "string")
|
|
124
|
+
stringList(entry.sopSection, `${path}.sopSection`);
|
|
125
|
+
for (const key of ["state", "args", "input", "env"])
|
|
126
|
+
objectField(entry, path, key);
|
|
127
|
+
for (const key of ["until", "when"])
|
|
128
|
+
objectField(entry, path, key, predicate);
|
|
129
|
+
objectField(entry, path, "verify", (verify, at) => {
|
|
130
|
+
strings(verify, at, ["out"]);
|
|
131
|
+
numeric(verify, at, ["maxDrives"]);
|
|
132
|
+
objectField(verify, at, "state");
|
|
133
|
+
objectField(verify, at, "override", (override, here) => numeric(override, here, ["below"]));
|
|
134
|
+
});
|
|
135
|
+
objectField(entry, path, "keep", (keep, at) => { strings(keep, at, ["path"]); numeric(keep, at, ["gte"]); });
|
|
136
|
+
objectField(entry, path, "unsure", (unsure, at) => { strings(unsure, at, ["branch"]); numeric(unsure, at, ["gte"]); });
|
|
137
|
+
objectField(entry, path, "retry", (retry, at) => {
|
|
138
|
+
numeric(retry, at, ["attempts", "backoff_s"]);
|
|
139
|
+
stringList(retry.on, `${at}.on`);
|
|
140
|
+
});
|
|
141
|
+
objectField(entry, path, "poll", (poll, at) => {
|
|
142
|
+
numeric(poll, at, ["interval_s", "deadline_s"]);
|
|
143
|
+
for (const key of ["until", "fail_when"])
|
|
144
|
+
objectField(poll, at, key, predicate);
|
|
145
|
+
});
|
|
146
|
+
if (entry.node === "chain" || entry.node === "parallel") {
|
|
147
|
+
const key = entry.node === "chain" ? "steps" : "branches";
|
|
148
|
+
if (entry[key] !== undefined) {
|
|
149
|
+
if (!Array.isArray(entry[key]))
|
|
150
|
+
fail(`${path}.${key}`, "an array of nodes");
|
|
151
|
+
else
|
|
152
|
+
entry[key].forEach((child, index) => node(child, `${path}.${key}[${index}]`));
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (entry.node === "map" || entry.node === "loop")
|
|
156
|
+
node(entry.body, `${path}.body`);
|
|
157
|
+
if (entry.node === "route")
|
|
158
|
+
objectField(entry, path, "branches", (branches, at) => {
|
|
159
|
+
for (const [name, branch] of Object.entries(branches))
|
|
160
|
+
if (record(branch, `${at}.${name}`)) {
|
|
161
|
+
strings(branch, `${at}.${name}`, ["criteria"]);
|
|
162
|
+
node(branch.body, `${at}.${name}.body`);
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
if (entry.node === "workflow")
|
|
166
|
+
workflow(entry.workflow, `${path}.workflow`);
|
|
167
|
+
};
|
|
168
|
+
const workflow = (entry, path) => {
|
|
169
|
+
if (!record(entry, path))
|
|
170
|
+
return;
|
|
171
|
+
strings(entry, path, ["name"]);
|
|
172
|
+
record(entry.schemas, `${path}.schemas`);
|
|
173
|
+
objectField(entry, path, "input", (input, at) => strings(input, at, ["schemaId"]));
|
|
174
|
+
if (record(entry.output, `${path}.output`))
|
|
175
|
+
strings(entry.output, `${path}.output`, ["schemaId", "path"]);
|
|
176
|
+
if (record(entry.root, `${path}.root`))
|
|
177
|
+
node(entry.root, `${path}.root`);
|
|
178
|
+
};
|
|
179
|
+
workflow(value, "workflow");
|
|
180
|
+
return errors.slice(0, 8);
|
|
181
|
+
}
|