@fractal-solutions/qflow 1.0.84 → 2.0.0-canary.4
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/CHANGELOG.md +107 -0
- package/LICENSE +21 -504
- package/MIGRATION.md +1130 -0
- package/README.md +1432 -823
- package/dist/agent/agent.d.ts +4 -22
- package/dist/agent/agent.js +4 -364
- package/dist/agent/base_agent.d.ts +8 -20
- package/dist/agent/base_agent.js +8 -205
- package/dist/agent/control_nodes.d.ts +61 -0
- package/dist/agent/control_nodes.js +164 -0
- package/dist/agent/engine.d.ts +45 -0
- package/dist/agent/engine.js +415 -0
- package/dist/agent/flow.d.ts +77 -0
- package/dist/agent/flow.js +148 -0
- package/dist/agent/index.d.ts +12 -0
- package/dist/agent/index.js +12 -0
- package/dist/agent/lpavrl_agent.d.ts +1 -2
- package/dist/agent/lpavrl_agent.js +2 -175
- package/dist/agent/ooda_agent.d.ts +1 -2
- package/dist/agent/ooda_agent.js +2 -164
- package/dist/agent/pema_agent.d.ts +1 -2
- package/dist/agent/pema_agent.js +2 -168
- package/dist/agent/raor_agent.d.ts +1 -2
- package/dist/agent/raor_agent.js +2 -161
- package/dist/agent/strategies.d.ts +126 -0
- package/dist/agent/strategies.js +159 -0
- package/dist/agent/tools.d.ts +1 -0
- package/dist/agent/tools.js +42 -24
- package/dist/approval.d.ts +42 -0
- package/dist/approval.js +86 -0
- package/dist/budget.d.ts +103 -0
- package/dist/budget.js +133 -0
- package/dist/bun_nodes.d.ts +119 -0
- package/dist/bun_nodes.js +331 -0
- package/dist/contracts.d.ts +30 -0
- package/dist/contracts.js +103 -0
- package/dist/durable.d.ts +278 -0
- package/dist/durable.js +730 -0
- package/dist/eval.d.ts +165 -0
- package/dist/eval.js +263 -0
- package/dist/graph.d.ts +157 -0
- package/dist/graph.js +330 -0
- package/dist/llm/index.d.ts +359 -0
- package/dist/llm/index.js +924 -0
- package/dist/logger.js +7 -3
- package/dist/mcp/client.d.ts +55 -0
- package/dist/mcp/client.js +145 -0
- package/dist/mcp/index.d.ts +2 -0
- package/dist/mcp/index.js +2 -0
- package/dist/mcp/server.d.ts +106 -0
- package/dist/mcp/server.js +154 -0
- package/dist/mcp/stdio.d.ts +2 -0
- package/dist/mcp/stdio.js +3 -0
- package/dist/memory.d.ts +101 -0
- package/dist/memory.js +217 -0
- package/dist/nodes/browser_control.js +5 -4
- package/dist/nodes/data_extractor.js +2 -1
- package/dist/nodes/data_validation.d.ts +3 -3
- package/dist/nodes/data_validation.js +19 -5
- package/dist/nodes/database.d.ts +2 -1
- package/dist/nodes/database.js +71 -58
- package/dist/nodes/gis.js +1 -1
- package/dist/nodes/git.d.ts +1 -1
- package/dist/nodes/git.js +65 -51
- package/dist/nodes/github.d.ts +1 -0
- package/dist/nodes/hardware_interaction.d.ts +2 -4
- package/dist/nodes/hardware_interaction.js +4 -3
- package/dist/nodes/http.d.ts +1 -7
- package/dist/nodes/http.js +46 -33
- package/dist/nodes/image_gallery.js +45 -45
- package/dist/nodes/index.d.ts +8 -0
- package/dist/nodes/index.js +8 -0
- package/dist/nodes/interactive_webview.d.ts +1 -2
- package/dist/nodes/interactive_webview.js +217 -216
- package/dist/nodes/multimedia_processing.js +1 -1
- package/dist/nodes/navigator.d.ts +7 -0
- package/dist/nodes/navigator.js +48 -8
- package/dist/nodes/pdf_processor.js +2 -1
- package/dist/nodes/remote_execution.js +2 -2
- package/dist/nodes/s3.d.ts +1 -0
- package/dist/nodes/scheduler.js +2 -2
- package/dist/nodes/search.d.ts +1 -1
- package/dist/nodes/search.js +3 -2
- package/dist/nodes/speech_synthesis.js +2 -1
- package/dist/nodes/spreadsheet.d.ts +1 -7
- package/dist/nodes/spreadsheet.js +2 -1
- package/dist/nodes/streaming_llm.d.ts +51 -0
- package/dist/nodes/streaming_llm.js +120 -0
- package/dist/nodes/webhook.d.ts +1 -5
- package/dist/nodes/webhook.js +106 -93
- package/dist/nodes/websockets.js +5 -3
- package/dist/nodes/webview.js +3 -2
- package/dist/optional.d.ts +32 -0
- package/dist/optional.js +61 -0
- package/dist/qflow.d.ts +25 -14
- package/dist/qflow.js +140 -49
- package/dist/schedule.d.ts +41 -0
- package/dist/schedule.js +149 -0
- package/dist/side_effects.d.ts +50 -0
- package/dist/side_effects.js +94 -0
- package/dist/streaming.d.ts +35 -0
- package/dist/streaming.js +199 -0
- package/dist/testing.d.ts +47 -0
- package/dist/testing.js +130 -0
- package/dist/tool_spec.d.ts +76 -0
- package/dist/tool_spec.js +321 -0
- package/dist/trace.d.ts +28 -0
- package/dist/trace.js +190 -0
- package/dist/util.d.ts +57 -0
- package/dist/util.js +122 -0
- package/package.json +153 -67
- package/qflow.config.js +18 -18
- package/scripts/postinstall.js +50 -0
- package/scripts/verify-package.js +114 -0
package/dist/eval.d.ts
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
export function deepDiff(expected: any, actual: any, { path, ignoreKeys, tolerance }?: {
|
|
2
|
+
path?: string | undefined;
|
|
3
|
+
ignoreKeys?: never[] | undefined;
|
|
4
|
+
tolerance?: number | undefined;
|
|
5
|
+
}): any[];
|
|
6
|
+
export function compareTraces(expected: any, actual: any, options?: {}): {
|
|
7
|
+
equal: boolean;
|
|
8
|
+
differences: any[];
|
|
9
|
+
expected: any;
|
|
10
|
+
actual: any;
|
|
11
|
+
};
|
|
12
|
+
export function createEvalSuite(flow: any, cases?: any[]): {
|
|
13
|
+
cases: any[];
|
|
14
|
+
run(): Promise<{
|
|
15
|
+
name: any;
|
|
16
|
+
result: any;
|
|
17
|
+
shared: any;
|
|
18
|
+
}[]>;
|
|
19
|
+
};
|
|
20
|
+
/** Deterministic pass/fail check for regression flows. */
|
|
21
|
+
export class AssertNode extends AsyncNode {
|
|
22
|
+
constructor({ condition, operator, expected, actualKey, outputKey, throwOnFailure, }?: {
|
|
23
|
+
condition?: null | undefined;
|
|
24
|
+
operator?: string | undefined;
|
|
25
|
+
expected?: boolean | undefined;
|
|
26
|
+
actualKey?: string | undefined;
|
|
27
|
+
outputKey?: string | undefined;
|
|
28
|
+
throwOnFailure?: boolean | undefined;
|
|
29
|
+
});
|
|
30
|
+
condition: any;
|
|
31
|
+
operator: string;
|
|
32
|
+
expected: boolean;
|
|
33
|
+
actualKey: string;
|
|
34
|
+
outputKey: string;
|
|
35
|
+
throwOnFailure: boolean;
|
|
36
|
+
prepAsync(shared?: {}): Promise<{
|
|
37
|
+
actual: any;
|
|
38
|
+
expected: any;
|
|
39
|
+
}>;
|
|
40
|
+
execAsync(prepRes: any, shared?: {}, observer?: {}): Promise<{
|
|
41
|
+
passed: boolean;
|
|
42
|
+
operator: any;
|
|
43
|
+
expected: any;
|
|
44
|
+
actual: any;
|
|
45
|
+
message: any;
|
|
46
|
+
}>;
|
|
47
|
+
postAsync(shared: any, prepRes: any, execRes: any): Promise<any>;
|
|
48
|
+
}
|
|
49
|
+
/** Numeric scorer with a threshold and structured output. */
|
|
50
|
+
export class ScoreNode extends AsyncNode {
|
|
51
|
+
constructor({ scorer, threshold, outputKey }?: {
|
|
52
|
+
scorer?: null | undefined;
|
|
53
|
+
threshold?: number | undefined;
|
|
54
|
+
outputKey?: string | undefined;
|
|
55
|
+
});
|
|
56
|
+
scorer: any;
|
|
57
|
+
threshold: number;
|
|
58
|
+
outputKey: string;
|
|
59
|
+
prepAsync(shared?: {}): Promise<{
|
|
60
|
+
actual: any;
|
|
61
|
+
expected: any;
|
|
62
|
+
}>;
|
|
63
|
+
execAsync(prepRes: any, shared?: {}, observer?: {}): Promise<{
|
|
64
|
+
score: any;
|
|
65
|
+
threshold: any;
|
|
66
|
+
passed: boolean;
|
|
67
|
+
details: any;
|
|
68
|
+
}>;
|
|
69
|
+
postAsync(shared: any, prepRes: any, execRes: any): Promise<any>;
|
|
70
|
+
}
|
|
71
|
+
/** Diff two payloads or two normalized traces. */
|
|
72
|
+
export class CompareNode extends AsyncNode {
|
|
73
|
+
constructor({ expected, actual, actualKey, outputKey, fields }?: {
|
|
74
|
+
expected?: null | undefined;
|
|
75
|
+
actual?: null | undefined;
|
|
76
|
+
actualKey?: string | undefined;
|
|
77
|
+
outputKey?: string | undefined;
|
|
78
|
+
fields?: null | undefined;
|
|
79
|
+
});
|
|
80
|
+
expected: any;
|
|
81
|
+
actual: any;
|
|
82
|
+
actualKey: string;
|
|
83
|
+
outputKey: string;
|
|
84
|
+
fields: any;
|
|
85
|
+
prepAsync(shared?: {}): Promise<{
|
|
86
|
+
expected: any;
|
|
87
|
+
actual: any;
|
|
88
|
+
}>;
|
|
89
|
+
execAsync(prepRes: any, shared?: {}, observer?: {}): Promise<{
|
|
90
|
+
equal: boolean;
|
|
91
|
+
differences: any[];
|
|
92
|
+
expected: any;
|
|
93
|
+
actual: any;
|
|
94
|
+
}>;
|
|
95
|
+
postAsync(shared: any, prepRes: any, execRes: any): Promise<any>;
|
|
96
|
+
}
|
|
97
|
+
/** Replay a saved trace into a flow, then compare the resulting trace. */
|
|
98
|
+
export class ReplayNode extends AsyncNode {
|
|
99
|
+
constructor({ trace, tracePath, flow, flowFactory, outputKey }?: {
|
|
100
|
+
trace?: null | undefined;
|
|
101
|
+
tracePath?: null | undefined;
|
|
102
|
+
flow?: null | undefined;
|
|
103
|
+
flowFactory?: null | undefined;
|
|
104
|
+
outputKey?: string | undefined;
|
|
105
|
+
});
|
|
106
|
+
trace: any;
|
|
107
|
+
tracePath: any;
|
|
108
|
+
flow: any;
|
|
109
|
+
flowFactory: any;
|
|
110
|
+
outputKey: string;
|
|
111
|
+
loadTrace(shared?: {}): Promise<{
|
|
112
|
+
schema: string;
|
|
113
|
+
runId: any;
|
|
114
|
+
flowName: any;
|
|
115
|
+
status: any;
|
|
116
|
+
steps: any;
|
|
117
|
+
startedAt: any;
|
|
118
|
+
endedAt: any;
|
|
119
|
+
events: any[];
|
|
120
|
+
checkpoints: any;
|
|
121
|
+
spans: any[];
|
|
122
|
+
}>;
|
|
123
|
+
prepAsync(shared?: {}): Promise<{
|
|
124
|
+
trace: {
|
|
125
|
+
schema: string;
|
|
126
|
+
runId: any;
|
|
127
|
+
flowName: any;
|
|
128
|
+
status: any;
|
|
129
|
+
steps: any;
|
|
130
|
+
startedAt: any;
|
|
131
|
+
endedAt: any;
|
|
132
|
+
events: any[];
|
|
133
|
+
checkpoints: any;
|
|
134
|
+
spans: any[];
|
|
135
|
+
};
|
|
136
|
+
initialShared: any;
|
|
137
|
+
}>;
|
|
138
|
+
execAsync(prepRes: any, shared?: {}, observer?: {}): Promise<{
|
|
139
|
+
result: any;
|
|
140
|
+
actual: {
|
|
141
|
+
schema: string;
|
|
142
|
+
runId: any;
|
|
143
|
+
flowName: any;
|
|
144
|
+
status: any;
|
|
145
|
+
steps: any;
|
|
146
|
+
startedAt: any;
|
|
147
|
+
endedAt: any;
|
|
148
|
+
events: any[];
|
|
149
|
+
checkpoints: any;
|
|
150
|
+
spans: any[];
|
|
151
|
+
};
|
|
152
|
+
comparison: {
|
|
153
|
+
equal: boolean;
|
|
154
|
+
differences: any[];
|
|
155
|
+
expected: any;
|
|
156
|
+
actual: any;
|
|
157
|
+
};
|
|
158
|
+
expectedTrace: string;
|
|
159
|
+
actualTrace: string;
|
|
160
|
+
}>;
|
|
161
|
+
postAsync(shared: any, prepRes: any, execRes: any): Promise<any>;
|
|
162
|
+
}
|
|
163
|
+
export { AsyncFlow };
|
|
164
|
+
import { AsyncNode } from './qflow.js';
|
|
165
|
+
import { AsyncFlow } from './qflow.js';
|
package/dist/eval.js
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { AsyncFlow, AsyncNode } from './qflow.js';
|
|
2
|
+
import { normalizeTrace, serializeTrace } from './trace.js';
|
|
3
|
+
const DEFAULT_IGNORED_KEYS = new Set([
|
|
4
|
+
'seq', 'startTime', 'endTime', 'duration', 'timestamp', 'stack', 'flowId', 'parentFlowId', 'updatedAt', 'createdAt',
|
|
5
|
+
]);
|
|
6
|
+
export function deepDiff(expected, actual, { path = '', ignoreKeys = [], tolerance = 0 } = {}) {
|
|
7
|
+
const differences = [];
|
|
8
|
+
const ignored = new Set([...DEFAULT_IGNORED_KEYS, ...ignoreKeys]);
|
|
9
|
+
const compare = (left, right, currentPath) => {
|
|
10
|
+
if (Object.prototype.hasOwnProperty.call(left ?? {}, '$ignore'))
|
|
11
|
+
return;
|
|
12
|
+
if (typeof left === 'number' && typeof right === 'number') {
|
|
13
|
+
if (Math.abs(left - right) > tolerance)
|
|
14
|
+
differences.push({ path: currentPath, expected: left, actual: right });
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
if (left === right)
|
|
18
|
+
return;
|
|
19
|
+
if (Array.isArray(left) && Array.isArray(right)) {
|
|
20
|
+
if (left.length !== right.length) {
|
|
21
|
+
differences.push({ path: `${currentPath}.length`, expected: left.length, actual: right.length });
|
|
22
|
+
}
|
|
23
|
+
const length = Math.min(left.length, right.length);
|
|
24
|
+
for (let index = 0; index < length; index += 1)
|
|
25
|
+
compare(left[index], right[index], `${currentPath}[${index}]`);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (left && right && typeof left === 'object' && typeof right === 'object') {
|
|
29
|
+
const keys = new Set([...Object.keys(left), ...Object.keys(right)]);
|
|
30
|
+
for (const key of keys) {
|
|
31
|
+
if (ignored.has(key))
|
|
32
|
+
continue;
|
|
33
|
+
compare(left[key], right[key], currentPath ? `${currentPath}.${key}` : key);
|
|
34
|
+
}
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
differences.push({ path: currentPath || 'root', expected: left, actual: right });
|
|
38
|
+
};
|
|
39
|
+
compare(expected, actual, path);
|
|
40
|
+
return differences;
|
|
41
|
+
}
|
|
42
|
+
function looksLikeTrace(value) {
|
|
43
|
+
return Boolean(value && typeof value === 'object' && (Array.isArray(value.events) || Array.isArray(value.spans)));
|
|
44
|
+
}
|
|
45
|
+
export function compareTraces(expected, actual, options = {}) {
|
|
46
|
+
const left = looksLikeTrace(expected) ? normalizeTrace(expected) : expected;
|
|
47
|
+
const right = looksLikeTrace(actual) ? normalizeTrace(actual) : actual;
|
|
48
|
+
const fields = options.fields ?? ['status', 'events', 'spans', 'checkpoints'];
|
|
49
|
+
const projected = trace => Object.fromEntries(fields.map(field => [field, trace?.[field]]));
|
|
50
|
+
const differences = deepDiff(projected(left), projected(right), options);
|
|
51
|
+
return {
|
|
52
|
+
equal: differences.length === 0,
|
|
53
|
+
differences,
|
|
54
|
+
expected: left,
|
|
55
|
+
actual: right,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function applyOperator(operator, actual, expected) {
|
|
59
|
+
switch (operator) {
|
|
60
|
+
case 'eq': return actual === expected;
|
|
61
|
+
case 'ne': return actual !== expected;
|
|
62
|
+
case 'deepEqual': return JSON.stringify(actual) === JSON.stringify(expected);
|
|
63
|
+
case 'gt': return actual > expected;
|
|
64
|
+
case 'gte': return actual >= expected;
|
|
65
|
+
case 'lt': return actual < expected;
|
|
66
|
+
case 'lte': return actual <= expected;
|
|
67
|
+
case 'contains': return String(actual ?? '').includes(String(expected));
|
|
68
|
+
case 'truthy': return Boolean(actual);
|
|
69
|
+
case 'falsy': return !actual;
|
|
70
|
+
default: throw new Error(`Unknown assertion operator '${operator}'.`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function actionForNode(node, action) {
|
|
74
|
+
return Object.keys(node.successors).length ? action : action === 'pass' || action === 'equal' ? true : false;
|
|
75
|
+
}
|
|
76
|
+
/** Deterministic pass/fail check for regression flows. */
|
|
77
|
+
export class AssertNode extends AsyncNode {
|
|
78
|
+
constructor({ condition = null, operator = 'eq', expected = true, actualKey = 'actual', outputKey = 'assertions', throwOnFailure = false, } = {}) {
|
|
79
|
+
super();
|
|
80
|
+
this.condition = condition;
|
|
81
|
+
this.operator = operator;
|
|
82
|
+
this.expected = expected;
|
|
83
|
+
this.actualKey = actualKey;
|
|
84
|
+
this.outputKey = outputKey;
|
|
85
|
+
this.throwOnFailure = throwOnFailure;
|
|
86
|
+
}
|
|
87
|
+
async prepAsync(shared = {}) {
|
|
88
|
+
const actual = this.params.actual ?? shared[this.params.actualKey ?? this.actualKey];
|
|
89
|
+
const expected = this.params.expected ?? this.expected;
|
|
90
|
+
return { actual, expected };
|
|
91
|
+
}
|
|
92
|
+
async execAsync(prepRes, shared = {}, observer = {}) {
|
|
93
|
+
const condition = this.params.condition ?? this.condition;
|
|
94
|
+
let passed;
|
|
95
|
+
if (typeof condition === 'function')
|
|
96
|
+
passed = Boolean(await condition(prepRes.actual, shared, observer));
|
|
97
|
+
else if (typeof condition === 'boolean')
|
|
98
|
+
passed = condition;
|
|
99
|
+
else
|
|
100
|
+
passed = applyOperator(this.params.operator ?? this.operator, prepRes.actual, prepRes.expected);
|
|
101
|
+
const result = {
|
|
102
|
+
passed,
|
|
103
|
+
operator: this.params.operator ?? this.operator,
|
|
104
|
+
expected: prepRes.expected,
|
|
105
|
+
actual: prepRes.actual,
|
|
106
|
+
message: this.params.message ?? `${passed ? 'Assertion passed' : 'Assertion failed'}`,
|
|
107
|
+
};
|
|
108
|
+
observer.emit?.('eval:assert', result);
|
|
109
|
+
if (!passed && (this.params.throwOnFailure ?? this.throwOnFailure)) {
|
|
110
|
+
throw new Error(result.message);
|
|
111
|
+
}
|
|
112
|
+
return result;
|
|
113
|
+
}
|
|
114
|
+
async postAsync(shared, prepRes, execRes) {
|
|
115
|
+
shared[this.outputKey] = [...(shared[this.outputKey] ?? []), execRes];
|
|
116
|
+
return actionForNode(this, execRes.passed ? 'pass' : 'fail');
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/** Numeric scorer with a threshold and structured output. */
|
|
120
|
+
export class ScoreNode extends AsyncNode {
|
|
121
|
+
constructor({ scorer = null, threshold = 0.8, outputKey = 'score' } = {}) {
|
|
122
|
+
super();
|
|
123
|
+
this.scorer = scorer;
|
|
124
|
+
this.threshold = threshold;
|
|
125
|
+
this.outputKey = outputKey;
|
|
126
|
+
}
|
|
127
|
+
async prepAsync(shared = {}) {
|
|
128
|
+
return {
|
|
129
|
+
actual: this.params.actual ?? shared.actual ?? shared.llmResult,
|
|
130
|
+
expected: this.params.expected ?? shared.expected,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
async execAsync(prepRes, shared = {}, observer = {}) {
|
|
134
|
+
const scorer = this.params.scorer ?? this.scorer;
|
|
135
|
+
if (typeof scorer !== 'function')
|
|
136
|
+
throw new Error('ScoreNode requires a scorer function.');
|
|
137
|
+
const score = await scorer(prepRes.actual, prepRes.expected, shared, observer);
|
|
138
|
+
const threshold = this.params.threshold ?? this.threshold;
|
|
139
|
+
const result = {
|
|
140
|
+
score: typeof score === 'number' ? score : score?.score ?? 0,
|
|
141
|
+
threshold,
|
|
142
|
+
passed: (typeof score === 'number' ? score : score?.score ?? 0) >= threshold,
|
|
143
|
+
details: typeof score === 'number' ? null : score?.details ?? null,
|
|
144
|
+
};
|
|
145
|
+
observer.emit?.('eval:score', result);
|
|
146
|
+
return result;
|
|
147
|
+
}
|
|
148
|
+
async postAsync(shared, prepRes, execRes) {
|
|
149
|
+
shared[this.outputKey] = execRes;
|
|
150
|
+
return actionForNode(this, execRes.passed ? 'pass' : 'fail');
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
/** Diff two payloads or two normalized traces. */
|
|
154
|
+
export class CompareNode extends AsyncNode {
|
|
155
|
+
constructor({ expected = null, actual = null, actualKey = 'actual', outputKey = 'comparison', fields = null } = {}) {
|
|
156
|
+
super();
|
|
157
|
+
this.expected = expected;
|
|
158
|
+
this.actual = actual;
|
|
159
|
+
this.actualKey = actualKey;
|
|
160
|
+
this.outputKey = outputKey;
|
|
161
|
+
this.fields = fields;
|
|
162
|
+
}
|
|
163
|
+
async prepAsync(shared = {}) {
|
|
164
|
+
return {
|
|
165
|
+
expected: this.params.expected ?? this.expected ?? shared.expected,
|
|
166
|
+
actual: this.params.actual ?? this.actual ?? shared[this.params.actualKey ?? this.actualKey],
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
async execAsync(prepRes, shared = {}, observer = {}) {
|
|
170
|
+
const result = compareTraces(prepRes.expected, prepRes.actual, {
|
|
171
|
+
...(this.params.compareOptions ?? {}),
|
|
172
|
+
...(this.fields ? { fields: this.params.fields ?? this.fields } : {}),
|
|
173
|
+
});
|
|
174
|
+
observer.emit?.('eval:compare', { equal: result.equal, differences: result.differences });
|
|
175
|
+
return result;
|
|
176
|
+
}
|
|
177
|
+
async postAsync(shared, prepRes, execRes) {
|
|
178
|
+
shared[this.outputKey] = execRes;
|
|
179
|
+
return actionForNode(this, execRes.equal ? 'equal' : 'different');
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
/** Replay a saved trace into a flow, then compare the resulting trace. */
|
|
183
|
+
export class ReplayNode extends AsyncNode {
|
|
184
|
+
constructor({ trace = null, tracePath = null, flow = null, flowFactory = null, outputKey = 'replay' } = {}) {
|
|
185
|
+
super();
|
|
186
|
+
this.trace = trace;
|
|
187
|
+
this.tracePath = tracePath;
|
|
188
|
+
this.flow = flow;
|
|
189
|
+
this.flowFactory = flowFactory;
|
|
190
|
+
this.outputKey = outputKey;
|
|
191
|
+
}
|
|
192
|
+
async loadTrace(shared = {}) {
|
|
193
|
+
const source = this.params.trace ?? this.trace ?? shared.trace;
|
|
194
|
+
if (source)
|
|
195
|
+
return normalizeTrace(source);
|
|
196
|
+
const path = this.params.tracePath ?? this.tracePath ?? shared.tracePath;
|
|
197
|
+
if (path) {
|
|
198
|
+
if (!globalThis.Bun?.file)
|
|
199
|
+
throw new Error('ReplayNode trace loading requires the Bun runtime.');
|
|
200
|
+
const text = await globalThis.Bun.file(path).text();
|
|
201
|
+
return normalizeTrace(JSON.parse(text));
|
|
202
|
+
}
|
|
203
|
+
throw new Error('ReplayNode requires a trace object or tracePath.');
|
|
204
|
+
}
|
|
205
|
+
async prepAsync(shared = {}) {
|
|
206
|
+
const trace = await this.loadTrace(shared);
|
|
207
|
+
const initialShared = this.params.shared
|
|
208
|
+
?? trace.checkpoints?.[0]?.shared
|
|
209
|
+
?? trace.events?.find(event => event.shared)?.shared
|
|
210
|
+
?? {};
|
|
211
|
+
return { trace, initialShared };
|
|
212
|
+
}
|
|
213
|
+
async execAsync(prepRes, shared = {}, observer = {}) {
|
|
214
|
+
const factory = this.params.flowFactory ?? this.flowFactory;
|
|
215
|
+
const target = factory
|
|
216
|
+
? await factory({ ...prepRes.initialShared }, { trace: prepRes.trace, observer })
|
|
217
|
+
: this.params.flow ?? this.flow;
|
|
218
|
+
if (!target || typeof target.runAsync !== 'function') {
|
|
219
|
+
throw new Error('ReplayNode requires an AsyncFlow or a flowFactory.');
|
|
220
|
+
}
|
|
221
|
+
const events = [];
|
|
222
|
+
target.onAny?.(entry => events.push({ event: entry.event, payload: entry.payload }));
|
|
223
|
+
const result = await target.runAsync({ ...prepRes.initialShared }, observer);
|
|
224
|
+
const actual = normalizeTrace({
|
|
225
|
+
runId: prepRes.trace.runId,
|
|
226
|
+
flowName: prepRes.trace.flowName,
|
|
227
|
+
events,
|
|
228
|
+
});
|
|
229
|
+
// Checkpoints are metadata of the saved run, not an output of replaying a
|
|
230
|
+
// fresh flow, so compare observed behaviour by default.
|
|
231
|
+
const comparison = compareTraces(prepRes.trace, actual, {
|
|
232
|
+
fields: ['status', 'events', 'spans'],
|
|
233
|
+
...(this.params.compareOptions ?? {}),
|
|
234
|
+
});
|
|
235
|
+
observer.emit?.('eval:replay', { equal: comparison.equal, differences: comparison.differences.length });
|
|
236
|
+
return {
|
|
237
|
+
result,
|
|
238
|
+
actual,
|
|
239
|
+
comparison,
|
|
240
|
+
expectedTrace: serializeTrace(prepRes.trace),
|
|
241
|
+
actualTrace: serializeTrace(actual),
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
async postAsync(shared, prepRes, execRes) {
|
|
245
|
+
shared[this.outputKey] = execRes;
|
|
246
|
+
return actionForNode(this, execRes.comparison.equal ? 'equal' : 'different');
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
export function createEvalSuite(flow, cases = []) {
|
|
250
|
+
return {
|
|
251
|
+
cases,
|
|
252
|
+
async run() {
|
|
253
|
+
const results = [];
|
|
254
|
+
for (const testCase of cases) {
|
|
255
|
+
const shared = { ...(testCase.shared ?? {}) };
|
|
256
|
+
const result = await flow.runAsync(shared);
|
|
257
|
+
results.push({ name: testCase.name ?? `case-${results.length + 1}`, result, shared });
|
|
258
|
+
}
|
|
259
|
+
return results;
|
|
260
|
+
},
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
export { AsyncFlow };
|
package/dist/graph.d.ts
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/** Describe a flow as JSON nodes/edges for visual authoring or docs. */
|
|
2
|
+
export function describeGraph(startNode: any): {
|
|
3
|
+
nodes: {
|
|
4
|
+
id: any;
|
|
5
|
+
label: any;
|
|
6
|
+
params: any;
|
|
7
|
+
successors: string[];
|
|
8
|
+
}[];
|
|
9
|
+
edges: {
|
|
10
|
+
from: any;
|
|
11
|
+
to: any;
|
|
12
|
+
action: string;
|
|
13
|
+
}[];
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Run several branches against copies of `shared`, then collect their results.
|
|
17
|
+
* Branches may be nodes or AsyncFlow instances.
|
|
18
|
+
*/
|
|
19
|
+
export class FanOutNode extends AsyncNode {
|
|
20
|
+
constructor({ branches, parallel, resultKey }?: {
|
|
21
|
+
branches?: never[] | undefined;
|
|
22
|
+
parallel?: boolean | undefined;
|
|
23
|
+
resultKey?: string | undefined;
|
|
24
|
+
});
|
|
25
|
+
branches: any[];
|
|
26
|
+
parallel: boolean;
|
|
27
|
+
resultKey: string;
|
|
28
|
+
execAsync(prepRes: any, shared: any, observer?: {}): Promise<any[]>;
|
|
29
|
+
postAsync(shared: any, prepRes: any, execRes: any): Promise<any>;
|
|
30
|
+
}
|
|
31
|
+
/** Combine `shared.fanOutResults` (or a custom key) into one result. */
|
|
32
|
+
export class FanInNode extends AsyncNode {
|
|
33
|
+
constructor({ combine, inputKey, outputKey }?: {
|
|
34
|
+
inputKey?: string | undefined;
|
|
35
|
+
outputKey?: string | undefined;
|
|
36
|
+
});
|
|
37
|
+
combine: any;
|
|
38
|
+
inputKey: string;
|
|
39
|
+
outputKey: string;
|
|
40
|
+
prepAsync(shared: any): Promise<any>;
|
|
41
|
+
execAsync(prepRes: any, shared: any, observer?: {}): Promise<any>;
|
|
42
|
+
postAsync(shared: any, prepRes: any, execRes: any): Promise<any>;
|
|
43
|
+
}
|
|
44
|
+
/** Parallel/serial collection mapping with explicit input and output contracts. */
|
|
45
|
+
export class MapNode extends AsyncNode {
|
|
46
|
+
constructor({ mapper, inputKey, outputKey, parallel, concurrency, inputContract, outputContract, onError, }?: {
|
|
47
|
+
mapper?: null | undefined;
|
|
48
|
+
inputKey?: string | undefined;
|
|
49
|
+
outputKey?: string | undefined;
|
|
50
|
+
parallel?: boolean | undefined;
|
|
51
|
+
concurrency?: number | undefined;
|
|
52
|
+
inputContract?: null | undefined;
|
|
53
|
+
outputContract?: null | undefined;
|
|
54
|
+
onError?: string | undefined;
|
|
55
|
+
});
|
|
56
|
+
mapper: any;
|
|
57
|
+
inputKey: string;
|
|
58
|
+
outputKey: string;
|
|
59
|
+
parallel: boolean;
|
|
60
|
+
concurrency: number;
|
|
61
|
+
inputContract: any;
|
|
62
|
+
outputContract: any;
|
|
63
|
+
onError: string;
|
|
64
|
+
prepAsync(shared?: {}): Promise<any[]>;
|
|
65
|
+
execAsync(items: any, shared?: {}, observer?: {}): Promise<any[]>;
|
|
66
|
+
postAsync(shared: any, prepRes: any, execRes: any): Promise<any>;
|
|
67
|
+
}
|
|
68
|
+
/** Fold a collection into a single value. */
|
|
69
|
+
export class ReduceNode extends AsyncNode {
|
|
70
|
+
constructor({ reducer, initialValue, inputKey, outputKey, inputContract, outputContract, }?: {
|
|
71
|
+
reducer?: null | undefined;
|
|
72
|
+
initialValue?: undefined;
|
|
73
|
+
inputKey?: string | undefined;
|
|
74
|
+
outputKey?: string | undefined;
|
|
75
|
+
inputContract?: null | undefined;
|
|
76
|
+
outputContract?: null | undefined;
|
|
77
|
+
});
|
|
78
|
+
reducer: any;
|
|
79
|
+
initialValue: any;
|
|
80
|
+
inputKey: string;
|
|
81
|
+
outputKey: string;
|
|
82
|
+
inputContract: any;
|
|
83
|
+
outputContract: any;
|
|
84
|
+
prepAsync(shared?: {}): Promise<{
|
|
85
|
+
items: any[];
|
|
86
|
+
initialValue: any;
|
|
87
|
+
}>;
|
|
88
|
+
execAsync(prepRes: any, shared?: {}, observer?: {}): Promise<any>;
|
|
89
|
+
postAsync(shared: any, prepRes: any, execRes: any): Promise<any>;
|
|
90
|
+
}
|
|
91
|
+
/** Combine several named shared-state values into one object/array. */
|
|
92
|
+
export class JoinNode extends AsyncNode {
|
|
93
|
+
constructor({ inputs, strategy, outputKey, join, inputContract }?: {
|
|
94
|
+
inputs?: never[] | undefined;
|
|
95
|
+
strategy?: string | undefined;
|
|
96
|
+
outputKey?: string | undefined;
|
|
97
|
+
join?: null | undefined;
|
|
98
|
+
inputContract?: null | undefined;
|
|
99
|
+
});
|
|
100
|
+
inputs: any[];
|
|
101
|
+
strategy: string;
|
|
102
|
+
outputKey: string;
|
|
103
|
+
join: any;
|
|
104
|
+
inputContract: any;
|
|
105
|
+
prepAsync(shared?: {}): Promise<{}>;
|
|
106
|
+
execAsync(values: any, shared?: {}, observer?: {}): Promise<any>;
|
|
107
|
+
postAsync(shared: any, prepRes: any, execRes: any): Promise<any>;
|
|
108
|
+
}
|
|
109
|
+
/** Route to a named successor based on predicates or a condition function. */
|
|
110
|
+
export class BranchNode extends AsyncNode {
|
|
111
|
+
constructor({ cases, condition, defaultAction, decisionKey, inputContract, }?: {
|
|
112
|
+
cases?: {} | undefined;
|
|
113
|
+
condition?: null | undefined;
|
|
114
|
+
defaultAction?: string | undefined;
|
|
115
|
+
decisionKey?: string | undefined;
|
|
116
|
+
inputContract?: null | undefined;
|
|
117
|
+
});
|
|
118
|
+
cases: {};
|
|
119
|
+
condition: any;
|
|
120
|
+
defaultAction: string;
|
|
121
|
+
decisionKey: string;
|
|
122
|
+
inputContract: any;
|
|
123
|
+
prepAsync(shared?: {}): Promise<{}>;
|
|
124
|
+
execAsync(shared: any, _sharedRef: any, observer?: {}): Promise<any>;
|
|
125
|
+
postAsync(shared: any, prepRes: any, execRes: any): Promise<any>;
|
|
126
|
+
}
|
|
127
|
+
/** Run a nested node/flow under explicit iteration and item budgets. */
|
|
128
|
+
export class CycleNode extends AsyncNode {
|
|
129
|
+
constructor({ node, flow, until, maxIterations, maxItems, inputKey, outputKey, metaKey, collect, }?: {
|
|
130
|
+
node?: null | undefined;
|
|
131
|
+
flow?: null | undefined;
|
|
132
|
+
until?: null | undefined;
|
|
133
|
+
maxIterations?: number | undefined;
|
|
134
|
+
maxItems?: number | undefined;
|
|
135
|
+
inputKey?: string | undefined;
|
|
136
|
+
outputKey?: string | undefined;
|
|
137
|
+
metaKey?: string | undefined;
|
|
138
|
+
collect?: boolean | undefined;
|
|
139
|
+
});
|
|
140
|
+
node: any;
|
|
141
|
+
flow: any;
|
|
142
|
+
until: any;
|
|
143
|
+
maxIterations: number;
|
|
144
|
+
maxItems: number;
|
|
145
|
+
inputKey: string;
|
|
146
|
+
outputKey: string;
|
|
147
|
+
metaKey: string;
|
|
148
|
+
collect: boolean;
|
|
149
|
+
prepAsync(shared?: {}): Promise<any[]>;
|
|
150
|
+
execAsync(items: any, shared?: {}, observer?: {}): Promise<{
|
|
151
|
+
results: any[];
|
|
152
|
+
iterations: number;
|
|
153
|
+
stoppedBy: string;
|
|
154
|
+
}>;
|
|
155
|
+
postAsync(shared: any, prepRes: any, execRes: any): Promise<any>;
|
|
156
|
+
}
|
|
157
|
+
import { AsyncNode } from './qflow.js';
|