@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,45 @@
|
|
|
1
|
+
import { isAsyncFunction, isPromise } from "node:util/types";
|
|
2
|
+
// Literal workflow JavaScript runs in the host process. Only execute workflows from trusted
|
|
3
|
+
// authors. Shadowing common globals catches some mistakes; it is NOT a security
|
|
4
|
+
// boundary, an isolation mechanism, a guarantee of purity, or a deadline for arbitrary code.
|
|
5
|
+
// `import` and `eval` cannot be shadowed as strict-mode parameters. Code-node validation does
|
|
6
|
+
// not prohibit them or prevent access to globals through other expressions. Executor-specific
|
|
7
|
+
// lint is separate and does not make code nodes safe for untrusted authors.
|
|
8
|
+
const DENIED_GLOBALS = [
|
|
9
|
+
"require", "module", "exports", "process", "global", "globalThis", "__dirname", "__filename",
|
|
10
|
+
"fetch", "XMLHttpRequest", "Function", "Buffer",
|
|
11
|
+
"setTimeout", "setInterval", "setImmediate", "queueMicrotask", "Promise", "Date",
|
|
12
|
+
];
|
|
13
|
+
function createTransformFactory(src) {
|
|
14
|
+
if (typeof src !== "string" || !src.trim())
|
|
15
|
+
throw new Error("code transform source must be a non-empty string");
|
|
16
|
+
let factory;
|
|
17
|
+
try {
|
|
18
|
+
factory = new Function(...DENIED_GLOBALS, `"use strict";\nconst __fn = (${src});\nif (typeof __fn !== "function") throw new Error("code must evaluate to a single (value) => value function");\nreturn __fn;`);
|
|
19
|
+
}
|
|
20
|
+
catch (e) {
|
|
21
|
+
throw new Error(`code transform failed to compile: ${e?.message || e}`);
|
|
22
|
+
}
|
|
23
|
+
return factory;
|
|
24
|
+
}
|
|
25
|
+
/** Parse the same expression as trusted execution, without evaluating it or calling its body.
|
|
26
|
+
* Syntax validity does not prove that the expression produces a function or a valid patch. */
|
|
27
|
+
export function compileTransformSyntax(src) {
|
|
28
|
+
createTransformFactory(src);
|
|
29
|
+
}
|
|
30
|
+
export function compileTransform(src) {
|
|
31
|
+
const fn = createTransformFactory(src)(...DENIED_GLOBALS.map(() => undefined));
|
|
32
|
+
if (isAsyncFunction(fn))
|
|
33
|
+
throw new Error("code transforms are synchronous; async functions are not supported");
|
|
34
|
+
return (value, ctx) => {
|
|
35
|
+
const result = fn(value, ctx);
|
|
36
|
+
if (isPromise(result)) {
|
|
37
|
+
void Promise.prototype.then.call(result, undefined, () => { });
|
|
38
|
+
throw new Error("code transforms are synchronous; returned a Promise or thenable");
|
|
39
|
+
}
|
|
40
|
+
if (result && typeof result.then === "function") {
|
|
41
|
+
throw new Error("code transforms are synchronous; returned a Promise or thenable");
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
};
|
|
45
|
+
}
|
package/dist/demo.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { type Workflow, type WorkflowDeps } from "./workflow.js";
|
|
2
|
+
export declare const supportTriage: Workflow;
|
|
3
|
+
export declare const demoScenarios: {
|
|
4
|
+
readonly billing: {
|
|
5
|
+
readonly title: "A clear request";
|
|
6
|
+
readonly ticket: "I was charged twice for my monthly subscription. Can you refund the duplicate?";
|
|
7
|
+
readonly category: "billing";
|
|
8
|
+
readonly probabilities: {
|
|
9
|
+
readonly billing: 0.94;
|
|
10
|
+
readonly technical: 0.02;
|
|
11
|
+
readonly general: 0.04;
|
|
12
|
+
};
|
|
13
|
+
readonly confidence: 0.91;
|
|
14
|
+
readonly urgency: 0.12;
|
|
15
|
+
};
|
|
16
|
+
readonly technical: {
|
|
17
|
+
readonly title: "An urgent outage";
|
|
18
|
+
readonly ticket: "Our checkout integration is down. Every payment is failing and customers cannot order.";
|
|
19
|
+
readonly category: "technical";
|
|
20
|
+
readonly probabilities: {
|
|
21
|
+
readonly billing: 0.04;
|
|
22
|
+
readonly technical: 0.93;
|
|
23
|
+
readonly general: 0.03;
|
|
24
|
+
};
|
|
25
|
+
readonly confidence: 0.9;
|
|
26
|
+
readonly urgency: 0.96;
|
|
27
|
+
};
|
|
28
|
+
readonly ambiguous: {
|
|
29
|
+
readonly title: "An uncertain request";
|
|
30
|
+
readonly ticket: "Something is wrong with my account. It might be the plan, or the connection. Can you take a look?";
|
|
31
|
+
readonly category: "general";
|
|
32
|
+
readonly probabilities: {
|
|
33
|
+
readonly billing: 0.3;
|
|
34
|
+
readonly technical: 0.3;
|
|
35
|
+
readonly general: 0.4;
|
|
36
|
+
};
|
|
37
|
+
readonly confidence: 0.12;
|
|
38
|
+
readonly urgency: 0.25;
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
export type DemoScenario = keyof typeof demoScenarios;
|
|
42
|
+
export declare function runTriageDemo(scenario?: DemoScenario, runJudge?: WorkflowDeps["runJudge"]): Promise<{
|
|
43
|
+
scenario: "billing" | "technical" | "ambiguous";
|
|
44
|
+
mode: string;
|
|
45
|
+
input: {
|
|
46
|
+
ticket: "I was charged twice for my monthly subscription. Can you refund the duplicate?" | "Our checkout integration is down. Every payment is failing and customers cannot order." | "Something is wrong with my account. It might be the plan, or the connection. Can you take a look?";
|
|
47
|
+
};
|
|
48
|
+
result: import("./workflow.js").WorkflowRunResult;
|
|
49
|
+
events: {
|
|
50
|
+
type: string;
|
|
51
|
+
label: string;
|
|
52
|
+
detail?: unknown;
|
|
53
|
+
}[];
|
|
54
|
+
}>;
|
package/dist/demo.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { runWorkflow } from "./workflow.js";
|
|
2
|
+
export const supportTriage = {
|
|
3
|
+
v: 2,
|
|
4
|
+
name: "support-triage",
|
|
5
|
+
schemas: {
|
|
6
|
+
Input: {
|
|
7
|
+
type: "object", additionalProperties: false, required: ["ticket"],
|
|
8
|
+
properties: { ticket: { type: "string", minLength: 1 } },
|
|
9
|
+
},
|
|
10
|
+
Triage: {
|
|
11
|
+
type: "object", additionalProperties: false, required: ["category", "urgent"],
|
|
12
|
+
properties: {
|
|
13
|
+
category: {
|
|
14
|
+
type: "string", description: "Which team should handle the request in `ticket`?",
|
|
15
|
+
enum: ["billing", "technical", "general"],
|
|
16
|
+
criteria: {
|
|
17
|
+
billing: "Invoices, payments, refunds, or a duplicate charge.",
|
|
18
|
+
technical: "A broken feature, integration, or service outage.",
|
|
19
|
+
general: "Other requests or requests with no clear team.",
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
urgent: { type: "boolean", description: "Does `ticket` describe an urgent problem needing immediate attention?" },
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
Result: {
|
|
26
|
+
type: "object", additionalProperties: false, required: ["queue", "priority"],
|
|
27
|
+
properties: {
|
|
28
|
+
queue: { type: "string", enum: ["billing", "technical", "general"] },
|
|
29
|
+
priority: { type: "string", enum: ["high", "normal"] },
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
input: { schemaId: "Input" },
|
|
34
|
+
output: { schemaId: "Result", path: "result" },
|
|
35
|
+
root: {
|
|
36
|
+
node: "chain", steps: [
|
|
37
|
+
{ node: "judge", label: "understand", state: { ticket: "{ticket}" }, out: "Triage", as: "triage" },
|
|
38
|
+
{
|
|
39
|
+
node: "escalate", label: "check-confidence",
|
|
40
|
+
when: { predicate: "lt", path: "triage$answers.confidence.category", n: 0.7 },
|
|
41
|
+
kind: "human_review", stage: "triage",
|
|
42
|
+
summary: "The category is uncertain. A person should choose the queue.",
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
node: "code", label: "choose-queue",
|
|
46
|
+
code: "s => ({ result: { queue: s.triage.category, priority: s.triage.urgent ? 'high' : 'normal' } })",
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
export const demoScenarios = {
|
|
52
|
+
billing: {
|
|
53
|
+
title: "A clear request", ticket: "I was charged twice for my monthly subscription. Can you refund the duplicate?",
|
|
54
|
+
category: "billing", probabilities: { billing: 0.94, technical: 0.02, general: 0.04 }, confidence: 0.91, urgency: 0.12,
|
|
55
|
+
},
|
|
56
|
+
technical: {
|
|
57
|
+
title: "An urgent outage", ticket: "Our checkout integration is down. Every payment is failing and customers cannot order.",
|
|
58
|
+
category: "technical", probabilities: { billing: 0.04, technical: 0.93, general: 0.03 }, confidence: 0.9, urgency: 0.96,
|
|
59
|
+
},
|
|
60
|
+
ambiguous: {
|
|
61
|
+
title: "An uncertain request", ticket: "Something is wrong with my account. It might be the plan, or the connection. Can you take a look?",
|
|
62
|
+
category: "general", probabilities: { billing: 0.3, technical: 0.3, general: 0.4 }, confidence: 0.12, urgency: 0.25,
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
export async function runTriageDemo(scenario = "billing", runJudge) {
|
|
66
|
+
const fixture = demoScenarios[scenario];
|
|
67
|
+
if (!fixture)
|
|
68
|
+
throw new Error(`Unknown scenario: ${String(scenario)}`);
|
|
69
|
+
const events = [];
|
|
70
|
+
const scriptedJudge = async () => ({
|
|
71
|
+
model: "scripted-demo", cost_usd: 0,
|
|
72
|
+
answers: {
|
|
73
|
+
category: { type: "choice", choice: fixture.category, probabilities: { ...fixture.probabilities }, confidence: fixture.confidence },
|
|
74
|
+
urgent: { type: "noul", noul: fixture.urgency },
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
const input = { ticket: fixture.ticket };
|
|
78
|
+
const result = await runWorkflow(supportTriage, input, { runJudge: runJudge ?? scriptedJudge, onEvent: event => events.push(event) });
|
|
79
|
+
return { scenario, mode: runJudge ? "live" : "scripted", input, result, events };
|
|
80
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type Workflow } from "./workflow.js";
|
|
2
|
+
export type DryRunResult = {
|
|
3
|
+
ok: true;
|
|
4
|
+
skipped?: string[];
|
|
5
|
+
} | {
|
|
6
|
+
ok: false;
|
|
7
|
+
problems: string[];
|
|
8
|
+
stage?: string;
|
|
9
|
+
};
|
|
10
|
+
export declare function synthesizeInstance(schema: unknown, definitions?: Record<string, unknown>, depth?: number): unknown;
|
|
11
|
+
export type DryRunOptions = {
|
|
12
|
+
sop?: string;
|
|
13
|
+
probeContext?: {
|
|
14
|
+
references?: Record<string, string>;
|
|
15
|
+
references_parsed?: Record<string, unknown>;
|
|
16
|
+
};
|
|
17
|
+
input?: Record<string, unknown>;
|
|
18
|
+
};
|
|
19
|
+
export declare function dryRunWorkflow(workflow: Workflow, opts?: DryRunOptions): Promise<DryRunResult>;
|
package/dist/dry-run.js
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { Compile } from "typebox/compile";
|
|
2
|
+
import { runWorkflow, validateWorkflow, WorkflowInvalidError, WorkflowInputInvalidError, WorkflowOutputInvalidError } from "./workflow.js";
|
|
3
|
+
import { resolveSchemaForWorkflow } from "./schema-references.js";
|
|
4
|
+
import { synthesizeAnswers } from "./system-one.js";
|
|
5
|
+
export function synthesizeInstance(schema, definitions = {}, depth = 0) {
|
|
6
|
+
if (depth > 12 || !schema || typeof schema !== "object")
|
|
7
|
+
return null;
|
|
8
|
+
const spec = schema;
|
|
9
|
+
if (typeof spec.$ref === "string") {
|
|
10
|
+
const match = /^#\/definitions\/(.+)$/.exec(spec.$ref);
|
|
11
|
+
return match && definitions[match[1]] ? synthesizeInstance(definitions[match[1]], definitions, depth + 1) : null;
|
|
12
|
+
}
|
|
13
|
+
if (spec.const !== undefined)
|
|
14
|
+
return spec.const;
|
|
15
|
+
if (Array.isArray(spec.enum) && spec.enum.length)
|
|
16
|
+
return spec.enum[0];
|
|
17
|
+
if (Array.isArray(spec.anyOf) && spec.anyOf.length) {
|
|
18
|
+
const branch = spec.anyOf.find((candidate) => candidate?.type !== "null") ?? spec.anyOf[0];
|
|
19
|
+
return synthesizeInstance(branch, definitions, depth + 1);
|
|
20
|
+
}
|
|
21
|
+
if (Array.isArray(spec.oneOf) && spec.oneOf.length)
|
|
22
|
+
return synthesizeInstance(spec.oneOf[0], definitions, depth + 1);
|
|
23
|
+
const type = Array.isArray(spec.type) ? spec.type[0] : spec.type;
|
|
24
|
+
if (type === "object" || (!type && spec.properties)) {
|
|
25
|
+
const out = Object.create(null);
|
|
26
|
+
for (const [key, property] of Object.entries(spec.properties || {})) {
|
|
27
|
+
out[key] = synthesizeInstance(property, definitions, depth + 1);
|
|
28
|
+
}
|
|
29
|
+
return { ...out };
|
|
30
|
+
}
|
|
31
|
+
if (type === "array") {
|
|
32
|
+
if (spec.maxItems === 0)
|
|
33
|
+
return [];
|
|
34
|
+
const min = Number.isInteger(spec.minItems) ? spec.minItems : 1;
|
|
35
|
+
if (!spec.items)
|
|
36
|
+
return [];
|
|
37
|
+
return Array.from({ length: Math.max(1, min) }, () => synthesizeInstance(spec.items, definitions, depth + 1));
|
|
38
|
+
}
|
|
39
|
+
if (type === "string") {
|
|
40
|
+
if (spec.maxLength === 0)
|
|
41
|
+
return "";
|
|
42
|
+
const min = Number.isInteger(spec.minLength) ? spec.minLength : 1;
|
|
43
|
+
return "x".repeat(Math.max(1, min));
|
|
44
|
+
}
|
|
45
|
+
if (type === "integer") {
|
|
46
|
+
const lo = Math.max(Number.isFinite(spec.minimum) ? Math.ceil(spec.minimum) : -Infinity, Number.isFinite(spec.exclusiveMinimum) ? Math.floor(spec.exclusiveMinimum) + 1 : -Infinity);
|
|
47
|
+
const hi = Math.min(Number.isFinite(spec.maximum) ? Math.floor(spec.maximum) : Infinity, Number.isFinite(spec.exclusiveMaximum) ? Math.ceil(spec.exclusiveMaximum) - 1 : Infinity);
|
|
48
|
+
let value = Number.isFinite(lo) ? lo : 0;
|
|
49
|
+
if (value > hi)
|
|
50
|
+
value = hi;
|
|
51
|
+
return value;
|
|
52
|
+
}
|
|
53
|
+
if (type === "number") {
|
|
54
|
+
// A two-sided interval takes the MIDPOINT — stepping ±1 off one bound can cross the other
|
|
55
|
+
// (an exclusive (5.2, 5.5) has no room for a unit step). One-sided bounds step off by 1.
|
|
56
|
+
const lo = Math.max(Number.isFinite(spec.minimum) ? spec.minimum : -Infinity, Number.isFinite(spec.exclusiveMinimum) ? spec.exclusiveMinimum : -Infinity);
|
|
57
|
+
const hi = Math.min(Number.isFinite(spec.maximum) ? spec.maximum : Infinity, Number.isFinite(spec.exclusiveMaximum) ? spec.exclusiveMaximum : Infinity);
|
|
58
|
+
const loExclusive = Number.isFinite(spec.exclusiveMinimum) && spec.exclusiveMinimum === lo;
|
|
59
|
+
const hiExclusive = Number.isFinite(spec.exclusiveMaximum) && spec.exclusiveMaximum === hi;
|
|
60
|
+
if (Number.isFinite(lo) && Number.isFinite(hi)) {
|
|
61
|
+
if (!loExclusive && !hiExclusive)
|
|
62
|
+
return lo;
|
|
63
|
+
// Halve first to avoid overflow at float-max scale. If rounding reaches
|
|
64
|
+
// an endpoint, an inclusive upper endpoint is still a valid instance.
|
|
65
|
+
const midpoint = lo / 2 + hi / 2;
|
|
66
|
+
if (midpoint <= lo && loExclusive && !hiExclusive)
|
|
67
|
+
return hi;
|
|
68
|
+
if (midpoint >= hi && hiExclusive && !loExclusive)
|
|
69
|
+
return lo;
|
|
70
|
+
return midpoint;
|
|
71
|
+
}
|
|
72
|
+
if (Number.isFinite(lo))
|
|
73
|
+
return loExclusive ? lo + 1 : lo;
|
|
74
|
+
if (Number.isFinite(hi))
|
|
75
|
+
return hiExclusive ? hi - 1 : hi;
|
|
76
|
+
return 0;
|
|
77
|
+
}
|
|
78
|
+
if (type === "boolean")
|
|
79
|
+
return false;
|
|
80
|
+
if (type === "null")
|
|
81
|
+
return null;
|
|
82
|
+
return "x";
|
|
83
|
+
}
|
|
84
|
+
function unsupportedConstructs(schema, path, found, depth = 0) {
|
|
85
|
+
if (depth > 12 || !schema || typeof schema !== "object")
|
|
86
|
+
return found;
|
|
87
|
+
const spec = schema;
|
|
88
|
+
for (const keyword of ["pattern", "format", "not", "allOf", "if", "uniqueItems", "multipleOf", "propertyNames", "patternProperties"]) {
|
|
89
|
+
if (spec[keyword] !== undefined)
|
|
90
|
+
found.push(`${path}: ${keyword}`);
|
|
91
|
+
}
|
|
92
|
+
// An exclusive bound too large for ±1 to change its float representation cannot be stepped
|
|
93
|
+
// off — the probe would sit ON the excluded bound and falsely reject. Generator limit: stand down.
|
|
94
|
+
if (Number.isFinite(spec.exclusiveMinimum) && spec.exclusiveMinimum + 1 === spec.exclusiveMinimum)
|
|
95
|
+
found.push(`${path}: exclusiveMinimum beyond float step`);
|
|
96
|
+
if (Number.isFinite(spec.exclusiveMaximum) && spec.exclusiveMaximum - 1 === spec.exclusiveMaximum)
|
|
97
|
+
found.push(`${path}: exclusiveMaximum beyond float step`);
|
|
98
|
+
// Recurse ONLY into schema-bearing positions. `properties`/`definitions` are NAME-KEYED maps:
|
|
99
|
+
// their keys are arbitrary field names, never keywords — a property literally named "pattern"
|
|
100
|
+
// must not stand the gate down (that would silently disable it for the exact class it guards).
|
|
101
|
+
for (const mapKey of ["properties", "definitions"]) {
|
|
102
|
+
const map = spec[mapKey];
|
|
103
|
+
if (map && typeof map === "object") {
|
|
104
|
+
for (const [name, sub] of Object.entries(map))
|
|
105
|
+
unsupportedConstructs(sub, `${path}/${mapKey}/${name}`, found, depth + 1);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
for (const schemaKey of ["items", "additionalItems", "additionalProperties", "contains"]) {
|
|
109
|
+
if (spec[schemaKey] && typeof spec[schemaKey] === "object")
|
|
110
|
+
unsupportedConstructs(spec[schemaKey], `${path}/${schemaKey}`, found, depth + 1);
|
|
111
|
+
}
|
|
112
|
+
for (const listKey of ["anyOf", "oneOf", "allOf", "prefixItems"]) {
|
|
113
|
+
if (Array.isArray(spec[listKey]))
|
|
114
|
+
spec[listKey].forEach((sub, i) => unsupportedConstructs(sub, `${path}/${listKey}/${i}`, found, depth + 1));
|
|
115
|
+
}
|
|
116
|
+
return found;
|
|
117
|
+
}
|
|
118
|
+
export async function dryRunWorkflow(workflow, opts = {}) {
|
|
119
|
+
const valid = validateWorkflow(workflow, opts.input === undefined ? undefined : { input: opts.input });
|
|
120
|
+
if (!valid.ok)
|
|
121
|
+
return { ok: false, problems: [new WorkflowInvalidError(String(workflow?.name), valid.errors).message] };
|
|
122
|
+
const skipped = [];
|
|
123
|
+
const scanSchemas = (plan) => {
|
|
124
|
+
for (const [id, schema] of Object.entries(plan.schemas || {}))
|
|
125
|
+
unsupportedConstructs(schema, id, skipped);
|
|
126
|
+
const visit = (node) => {
|
|
127
|
+
if (node.node === "workflow")
|
|
128
|
+
scanSchemas(node.workflow);
|
|
129
|
+
if (node.node === "chain")
|
|
130
|
+
node.steps.forEach(visit);
|
|
131
|
+
if (node.node === "parallel")
|
|
132
|
+
node.branches.forEach(visit);
|
|
133
|
+
if (node.node === "map" || node.node === "loop")
|
|
134
|
+
visit(node.body);
|
|
135
|
+
if (node.node === "route")
|
|
136
|
+
Object.values(node.branches).forEach(branch => visit(branch.body));
|
|
137
|
+
};
|
|
138
|
+
visit(plan.root);
|
|
139
|
+
};
|
|
140
|
+
scanSchemas(workflow);
|
|
141
|
+
if (skipped.length)
|
|
142
|
+
return { ok: true, skipped };
|
|
143
|
+
let input = opts.input ?? {};
|
|
144
|
+
if (opts.input === undefined && workflow.input) {
|
|
145
|
+
const schema = resolveSchemaForWorkflow(workflow, workflow.schemas[workflow.input.schemaId]);
|
|
146
|
+
const generated = synthesizeInstance(schema, schema.definitions ?? workflow.schemas);
|
|
147
|
+
if (!generated || typeof generated !== "object" || Array.isArray(generated) || !Compile(schema).Check(generated)) {
|
|
148
|
+
return { ok: true, skipped: ["Could not synthesize a valid input object. Supply input explicitly to dryRunWorkflow or the dry-run CLI."] };
|
|
149
|
+
}
|
|
150
|
+
input = generated;
|
|
151
|
+
}
|
|
152
|
+
if (opts.probeContext && !Object.prototype.hasOwnProperty.call(input, "context")) {
|
|
153
|
+
input = { ...input, context: {
|
|
154
|
+
references: opts.probeContext?.references ?? {},
|
|
155
|
+
references_parsed: opts.probeContext?.references_parsed ?? {},
|
|
156
|
+
} };
|
|
157
|
+
}
|
|
158
|
+
let currentLabel = "";
|
|
159
|
+
try {
|
|
160
|
+
const result = await runWorkflow(workflow, input, {
|
|
161
|
+
sop: opts.sop,
|
|
162
|
+
onEvent: (event) => { if (typeof event.label === "string")
|
|
163
|
+
currentLabel = event.label; },
|
|
164
|
+
runNode: async ({ schema, review }) => {
|
|
165
|
+
const candidate = synthesizeInstance(schema, schema.definitions ?? workflow.schemas);
|
|
166
|
+
if (review)
|
|
167
|
+
await review(candidate);
|
|
168
|
+
return candidate;
|
|
169
|
+
},
|
|
170
|
+
runJudge: async ({ questions }) => ({ answers: synthesizeAnswers(questions), model: "synthetic", usage: null, cost_usd: null, request_sha256: "" }),
|
|
171
|
+
syntheticEffects: true,
|
|
172
|
+
runEffect: async ({ node, schema }) => node.via === "shell"
|
|
173
|
+
? { code: 0, stdout: "{}", stderr: "", truncated: false }
|
|
174
|
+
: synthesizeInstance(schema ?? (workflow.schemas || {})[node.out], schema?.definitions ?? workflow.schemas ?? {}),
|
|
175
|
+
});
|
|
176
|
+
if (result.status === "escalated") {
|
|
177
|
+
// Synthetic values tripping an escalate predicate proves nothing about real cases.
|
|
178
|
+
return { ok: true, skipped: [`escalated:${result.escalation.kind}@${result.escalation.stage} on synthetic values`] };
|
|
179
|
+
}
|
|
180
|
+
return { ok: true };
|
|
181
|
+
}
|
|
182
|
+
catch (error) {
|
|
183
|
+
if (error instanceof WorkflowOutputInvalidError) {
|
|
184
|
+
return { ok: false, problems: error.problems, stage: error.stage };
|
|
185
|
+
}
|
|
186
|
+
if (error instanceof WorkflowInputInvalidError)
|
|
187
|
+
return { ok: false, problems: [error.message] };
|
|
188
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
189
|
+
return { ok: false, problems: [`deterministic path threw over schema-conforming inputs${currentLabel ? ` (at "${currentLabel}")` : ""}: ${message.slice(0, 1200)}`], stage: currentLabel || undefined };
|
|
190
|
+
}
|
|
191
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { defineWorkflow, runTypedWorkflow, WorkflowSchemaConversionError } from "./authoring.js";
|
|
2
|
+
export type { StandardJSONSchema, AuthoringSchema, TypedWorkflow, TypedWorkflowRunResult, WorkflowInput, WorkflowOutput, } from "./authoring.js";
|
|
3
|
+
export { runWorkflow, validateWorkflow, workflowSha256, WorkflowInvalidError, WorkflowInputInvalidError, WorkflowOutputInvalidError, WorkflowCodeError, WorkflowStateError, WorkflowVerificationError, EffectFailure, EffectDeadlineExceededError, EffectOutcomeUnknownError, EscalationSignal, } from "./workflow.js";
|
|
4
|
+
export type { Workflow, WorkflowNode, WorkflowDeps, WorkflowRunResult, Escalation, EffectSettlement, MapItem, LlmNode, JudgeNode, PickNode, SiftNode, RouteNode, WorkflowInvocation, ArtifactNode, ArtifactState, CallNode, CallRetryClass, CallPredicate, PollClause, ModelTier, WorkflowEffort, WorkflowThinking, VerifyClause, AskPredicate, MechanicalPredicate, Predicate, } from "./workflow.js";
|
|
5
|
+
export { dryRunWorkflow, synthesizeInstance } from "./dry-run.js";
|
|
6
|
+
export type { DryRunOptions, DryRunResult } from "./dry-run.js";
|
|
7
|
+
export { compileQuestions, validateAnswers, answersToValue, answersSidecar, answerConfidence, synthesizeAnswers, SYSTEM_ONE_LIMITS, SystemOneError, isSystemOneResponseReason, } from "./system-one.js";
|
|
8
|
+
export type { SystemOneQuestion, SystemOneAnswer, SystemOneResult, SystemOneRetryClass, SystemOneResponseReason, CompiledQuestions, AnswersSidecar, } from "./system-one.js";
|
|
9
|
+
export { inspectWorkflow, formatWorkflowTree } from "./inspection.js";
|
|
10
|
+
export type { WorkflowInspection, WorkflowInspectionNode } from "./inspection.js";
|
|
11
|
+
export { runWorkflowSlice, assertWorkflowCapabilities, desugarWorkflow, stageSchemaForNode, submissionSchemaForNode, mergeStageDelta, REPORT_SCHEMA, SHELL_RESULT_SCHEMA, } from "./workflow.js";
|
|
12
|
+
export { resolveSchemaForWorkflow } from "./schema-references.js";
|
|
13
|
+
export { getPath, predicateMatches } from "./predicates.js";
|
|
14
|
+
export type { StopPredicate, AcceptPredicate } from "./predicates.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { defineWorkflow, runTypedWorkflow, WorkflowSchemaConversionError } from "./authoring.js";
|
|
2
|
+
export { runWorkflow, validateWorkflow, workflowSha256, WorkflowInvalidError, WorkflowInputInvalidError, WorkflowOutputInvalidError, WorkflowCodeError, WorkflowStateError, WorkflowVerificationError, EffectFailure, EffectDeadlineExceededError, EffectOutcomeUnknownError, EscalationSignal, } from "./workflow.js";
|
|
3
|
+
export { dryRunWorkflow, synthesizeInstance } from "./dry-run.js";
|
|
4
|
+
export { compileQuestions, validateAnswers, answersToValue, answersSidecar, answerConfidence, synthesizeAnswers, SYSTEM_ONE_LIMITS, SystemOneError, isSystemOneResponseReason, } from "./system-one.js";
|
|
5
|
+
export { inspectWorkflow, formatWorkflowTree } from "./inspection.js";
|
|
6
|
+
export { runWorkflowSlice, assertWorkflowCapabilities, desugarWorkflow, stageSchemaForNode, submissionSchemaForNode, mergeStageDelta, REPORT_SCHEMA, SHELL_RESULT_SCHEMA, } from "./workflow.js";
|
|
7
|
+
export { resolveSchemaForWorkflow } from "./schema-references.js";
|
|
8
|
+
export { getPath, predicateMatches } from "./predicates.js";
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type WorkflowNode } from "./workflow.js";
|
|
2
|
+
export type WorkflowInspectionNode = {
|
|
3
|
+
path: string;
|
|
4
|
+
parent?: string;
|
|
5
|
+
relation: string;
|
|
6
|
+
kind: WorkflowNode["node"];
|
|
7
|
+
label: string;
|
|
8
|
+
workflow: string;
|
|
9
|
+
writes?: string;
|
|
10
|
+
outputSchema?: string;
|
|
11
|
+
/** Effective selection from completed map-item state; absent means whole item state. */
|
|
12
|
+
resultPath?: string;
|
|
13
|
+
childWorkflow?: string;
|
|
14
|
+
};
|
|
15
|
+
export type WorkflowInspection = {
|
|
16
|
+
name: string;
|
|
17
|
+
version: 2;
|
|
18
|
+
sha256: string;
|
|
19
|
+
checked: "structure-only";
|
|
20
|
+
inputSchema?: string;
|
|
21
|
+
outputSchema: string;
|
|
22
|
+
outputPath?: string;
|
|
23
|
+
nodes: WorkflowInspectionNode[];
|
|
24
|
+
requires: {
|
|
25
|
+
adapters: ("runNode" | "runJudge" | "runEffect")[];
|
|
26
|
+
tools: string[];
|
|
27
|
+
sopSections: string[];
|
|
28
|
+
executableCode: boolean;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
/** Inspect a workflow's structure without running authored code, adapters, or schema probes.
|
|
32
|
+
* The input must be plain JSON data. This is a review aid, not execution admission: use
|
|
33
|
+
* validateWorkflow on trusted documents and the host's own capability gate before running. */
|
|
34
|
+
export declare function inspectWorkflow(value: unknown): WorkflowInspection;
|
|
35
|
+
export declare function formatWorkflowTree(inspection: WorkflowInspection): string;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { workflowDocumentError, workflowSha256 } from "./workflow.js";
|
|
2
|
+
import { workflowShapeErrors } from "./workflow-shape.js";
|
|
3
|
+
const pointer = (part) => part.replace(/~/g, "~0").replace(/\//g, "~1");
|
|
4
|
+
/** Inspect a workflow's structure without running authored code, adapters, or schema probes.
|
|
5
|
+
* The input must be plain JSON data. This is a review aid, not execution admission: use
|
|
6
|
+
* validateWorkflow on trusted documents and the host's own capability gate before running. */
|
|
7
|
+
export function inspectWorkflow(value) {
|
|
8
|
+
const bound = workflowDocumentError(value);
|
|
9
|
+
if (bound)
|
|
10
|
+
throw new Error(`Cannot inspect workflow: ${bound}`);
|
|
11
|
+
const errors = workflowShapeErrors(value, true);
|
|
12
|
+
if (errors.length)
|
|
13
|
+
throw new Error(`Cannot inspect workflow: ${errors.join("; ")}`);
|
|
14
|
+
const workflow = value;
|
|
15
|
+
const nodes = [];
|
|
16
|
+
const adapters = new Set();
|
|
17
|
+
const tools = new Set();
|
|
18
|
+
const sopSections = new Set();
|
|
19
|
+
let executableCode = false;
|
|
20
|
+
const visit = (node, owner, path, relation, parent) => {
|
|
21
|
+
nodes.push({ path, ...(parent ? { parent } : {}), relation, kind: node.node,
|
|
22
|
+
label: "label" in node ? node.label : node.node, workflow: owner.name,
|
|
23
|
+
...("as" in node && node.as ? { writes: node.as }
|
|
24
|
+
: ["agent", "extract", "decide"].includes(node.node) && "label" in node ? { writes: node.label }
|
|
25
|
+
: node.node === "report" ? { writes: "report_markdown" } : {}),
|
|
26
|
+
...("out" in node && node.out && node.node !== "sift" ? { outputSchema: node.out } : {}),
|
|
27
|
+
...(node.node === "map" && (node.resultPath || "as" in node.body && node.body.as)
|
|
28
|
+
? { resultPath: node.resultPath || node.body.as } : {}),
|
|
29
|
+
...(node.node === "workflow" ? { childWorkflow: node.workflow.name } : {}), });
|
|
30
|
+
if (["agent", "decide", "extract", "report"].includes(node.node) ||
|
|
31
|
+
node.node === "artifact" && ["markdown", "report"].includes(node.type))
|
|
32
|
+
adapters.add("runNode");
|
|
33
|
+
if (["judge", "pick", "sift", "route"].includes(node.node) ||
|
|
34
|
+
"verify" in node && node.verify || node.node === "loop" && node.until.predicate === "ask" ||
|
|
35
|
+
node.node === "escalate" && node.when.predicate === "ask")
|
|
36
|
+
adapters.add("runJudge");
|
|
37
|
+
if (node.node === "call") {
|
|
38
|
+
adapters.add("runEffect");
|
|
39
|
+
if (node.via === "tool" && node.tool)
|
|
40
|
+
tools.add(node.tool);
|
|
41
|
+
if (node.via === "shell" || node.via === "executor")
|
|
42
|
+
executableCode = true;
|
|
43
|
+
}
|
|
44
|
+
if (node.node === "code")
|
|
45
|
+
executableCode = true;
|
|
46
|
+
if ("tools" in node)
|
|
47
|
+
node.tools?.forEach(tool => tools.add(tool));
|
|
48
|
+
if ("sopSection" in node && node.sopSection) {
|
|
49
|
+
const sections = Array.isArray(node.sopSection) ? node.sopSection : [node.sopSection];
|
|
50
|
+
sections.forEach(section => sopSections.add(section));
|
|
51
|
+
}
|
|
52
|
+
switch (node.node) {
|
|
53
|
+
case "chain":
|
|
54
|
+
node.steps.forEach((step, index) => visit(step, owner, `${path}/steps/${index}`, `step ${index + 1}`, path));
|
|
55
|
+
break;
|
|
56
|
+
case "parallel":
|
|
57
|
+
node.branches.forEach((branch, index) => visit(branch, owner, `${path}/branches/${index}`, `parallel ${index + 1}`, path));
|
|
58
|
+
break;
|
|
59
|
+
case "map":
|
|
60
|
+
visit(node.body, owner, `${path}/body`, `each ${node.itemsPath}`, path);
|
|
61
|
+
break;
|
|
62
|
+
case "loop":
|
|
63
|
+
visit(node.body, owner, `${path}/body`, `up to ${node.maxIters} iterations`, path);
|
|
64
|
+
break;
|
|
65
|
+
case "route":
|
|
66
|
+
Object.entries(node.branches).forEach(([name, branch]) => visit(branch.body, owner, `${path}/branches/${pointer(name)}/body`, `route ${name}`, path));
|
|
67
|
+
break;
|
|
68
|
+
case "workflow":
|
|
69
|
+
visit(node.workflow.root, node.workflow, `${path}/workflow/root`, `workflow ${node.workflow.name}`, path);
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
visit(workflow.root, workflow, "/root", "root");
|
|
74
|
+
return {
|
|
75
|
+
name: workflow.name, version: workflow.v, sha256: workflowSha256(workflow), checked: "structure-only",
|
|
76
|
+
...(workflow.input ? { inputSchema: workflow.input.schemaId } : {}), outputSchema: workflow.output.schemaId,
|
|
77
|
+
...(workflow.output.path ? { outputPath: workflow.output.path } : {}),
|
|
78
|
+
nodes, requires: { adapters: [...adapters].sort(), tools: [...tools].sort(), sopSections: [...sopSections].sort(), executableCode },
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
export function formatWorkflowTree(inspection) {
|
|
82
|
+
const safe = (text) => text.replace(/[\x00-\x1f\x7f-\x9f\u2028\u2029\u202a-\u202e\u2066-\u2069]/g, character => `\\u${character.charCodeAt(0).toString(16).padStart(4, "0")}`);
|
|
83
|
+
const children = new Map();
|
|
84
|
+
for (const node of inspection.nodes) {
|
|
85
|
+
const list = children.get(node.parent) ?? [];
|
|
86
|
+
list.push(node);
|
|
87
|
+
children.set(node.parent, list);
|
|
88
|
+
}
|
|
89
|
+
const lines = [`${safe(inspection.name)} (${inspection.inputSchema ? safe(inspection.inputSchema) : "input"} → ${safe(inspection.outputSchema)})`];
|
|
90
|
+
const visit = (parent, prefix) => {
|
|
91
|
+
const list = children.get(parent) ?? [];
|
|
92
|
+
list.forEach((node, index) => {
|
|
93
|
+
const last = index === list.length - 1;
|
|
94
|
+
const relation = /^(parallel |each |up to |route |workflow )/.test(node.relation) ? `${safe(node.relation)}: ` : "";
|
|
95
|
+
const value = node.outputSchema ? ` (${safe(node.outputSchema)})` : node.kind === "map"
|
|
96
|
+
? ` (array; ${node.resultPath ? `select ${safe(node.resultPath)}` : "whole item state"})`
|
|
97
|
+
: node.kind === "code" ? " (returned value)" : "";
|
|
98
|
+
lines.push(`${prefix}${last ? "└─" : "├─"} ${relation}${safe(node.label)} [${node.kind}]${node.writes ? ` → ${safe(node.writes)}${value}` : node.kind === "code" ? " → state patch (object) or label (non-object)" : ""}`);
|
|
99
|
+
visit(node.path, `${prefix}${last ? " " : "│ "}`);
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
visit(undefined, "");
|
|
103
|
+
lines.push(`Final output: ${inspection.outputPath ? safe(inspection.outputPath) : "whole state"} (${safe(inspection.outputSchema)})`);
|
|
104
|
+
lines.push(`Adapters: ${inspection.requires.adapters.join(", ") || "none"}`);
|
|
105
|
+
lines.push("Graph preview — shows structure, not execution results.");
|
|
106
|
+
return lines.join("\n");
|
|
107
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export type StopPredicate = {
|
|
2
|
+
predicate: "no_new_items";
|
|
3
|
+
key: string;
|
|
4
|
+
} | {
|
|
5
|
+
predicate: "field_true";
|
|
6
|
+
path: string;
|
|
7
|
+
} | {
|
|
8
|
+
predicate: "count_gte";
|
|
9
|
+
path: string;
|
|
10
|
+
n: number;
|
|
11
|
+
} | {
|
|
12
|
+
predicate: "empty";
|
|
13
|
+
path: string;
|
|
14
|
+
} | {
|
|
15
|
+
predicate: "gte";
|
|
16
|
+
path: string;
|
|
17
|
+
n: number;
|
|
18
|
+
} | {
|
|
19
|
+
predicate: "lt";
|
|
20
|
+
path: string;
|
|
21
|
+
n: number;
|
|
22
|
+
};
|
|
23
|
+
export type AcceptPredicate = StopPredicate | {
|
|
24
|
+
predicate: "field_equals";
|
|
25
|
+
path: string;
|
|
26
|
+
value: string | number | boolean;
|
|
27
|
+
} | {
|
|
28
|
+
predicate: "in";
|
|
29
|
+
path: string;
|
|
30
|
+
values: Array<string | number | boolean>;
|
|
31
|
+
};
|
|
32
|
+
export declare const MECHANICAL_PREDICATE_NAMES: ReadonlySet<string>;
|
|
33
|
+
export declare function getPath(value: unknown, key: string): unknown;
|
|
34
|
+
export declare function predicateMatches(predicate: StopPredicate | AcceptPredicate, value: unknown): boolean;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export const MECHANICAL_PREDICATE_NAMES = new Set(["no_new_items", "field_true", "count_gte", "empty", "field_equals", "in", "gte", "lt"]);
|
|
2
|
+
function isRecord(value) {
|
|
3
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
4
|
+
}
|
|
5
|
+
export function getPath(value, key) {
|
|
6
|
+
if (!key)
|
|
7
|
+
return value;
|
|
8
|
+
return key.split(".").reduce((current, part) => (isRecord(current) ? current[part] : undefined), value);
|
|
9
|
+
}
|
|
10
|
+
export function predicateMatches(predicate, value) {
|
|
11
|
+
switch (predicate.predicate) {
|
|
12
|
+
case "field_equals":
|
|
13
|
+
return getPath(value, predicate.path) === predicate.value;
|
|
14
|
+
case "field_true":
|
|
15
|
+
return getPath(value, predicate.path) === true;
|
|
16
|
+
case "in": {
|
|
17
|
+
const target = getPath(value, predicate.path);
|
|
18
|
+
return (typeof target === "string" || typeof target === "number" || typeof target === "boolean") && predicate.values.includes(target);
|
|
19
|
+
}
|
|
20
|
+
case "empty": {
|
|
21
|
+
const target = getPath(value, predicate.path);
|
|
22
|
+
return Array.isArray(target) ? target.length === 0 : target === "" || target === null || target === undefined;
|
|
23
|
+
}
|
|
24
|
+
case "count_gte": {
|
|
25
|
+
const target = getPath(value, predicate.path);
|
|
26
|
+
const count = Array.isArray(target) ? target.length : typeof target === "number" ? target : 0;
|
|
27
|
+
return count >= predicate.n;
|
|
28
|
+
}
|
|
29
|
+
case "no_new_items": {
|
|
30
|
+
const target = getPath(value, predicate.key);
|
|
31
|
+
return Array.isArray(target) ? target.length === 0 : !target;
|
|
32
|
+
}
|
|
33
|
+
case "gte":
|
|
34
|
+
case "lt": {
|
|
35
|
+
const target = getPath(value, predicate.path);
|
|
36
|
+
if (typeof target !== "number" || !Number.isFinite(target))
|
|
37
|
+
return false;
|
|
38
|
+
return predicate.predicate === "gte" ? target >= predicate.n : target < predicate.n;
|
|
39
|
+
}
|
|
40
|
+
default:
|
|
41
|
+
throw new Error(`agentrun: unsupported predicate "${String(predicate?.predicate ?? "")}" — fail closed, never silently false`);
|
|
42
|
+
}
|
|
43
|
+
}
|