@fractal-solutions/qflow 1.0.84 → 2.0.0
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 +119 -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/graph.js
ADDED
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import { AsyncFlow, AsyncNode } from './qflow.js';
|
|
2
|
+
import { assertState } from './contracts.js';
|
|
3
|
+
/**
|
|
4
|
+
* Run several branches against copies of `shared`, then collect their results.
|
|
5
|
+
* Branches may be nodes or AsyncFlow instances.
|
|
6
|
+
*/
|
|
7
|
+
export class FanOutNode extends AsyncNode {
|
|
8
|
+
constructor({ branches = [], parallel = true, resultKey = 'fanOutResults' } = {}) {
|
|
9
|
+
super();
|
|
10
|
+
this.branches = [...branches];
|
|
11
|
+
this.parallel = parallel;
|
|
12
|
+
this.resultKey = resultKey;
|
|
13
|
+
}
|
|
14
|
+
async execAsync(prepRes, shared, observer = {}) {
|
|
15
|
+
const runBranch = async (branch) => {
|
|
16
|
+
const branchShared = { ...shared };
|
|
17
|
+
if (branch instanceof AsyncFlow) {
|
|
18
|
+
return await branch.runAsync(branchShared, observer);
|
|
19
|
+
}
|
|
20
|
+
return await new AsyncFlow(branch).runAsync(branchShared, observer);
|
|
21
|
+
};
|
|
22
|
+
if (this.parallel) {
|
|
23
|
+
return await Promise.all(this.branches.map(runBranch));
|
|
24
|
+
}
|
|
25
|
+
const results = [];
|
|
26
|
+
for (const branch of this.branches) {
|
|
27
|
+
results.push(await runBranch(branch));
|
|
28
|
+
}
|
|
29
|
+
return results;
|
|
30
|
+
}
|
|
31
|
+
async postAsync(shared, prepRes, execRes) {
|
|
32
|
+
shared[this.resultKey] = execRes;
|
|
33
|
+
return Object.keys(this.successors).length ? 'default' : execRes;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/** Combine `shared.fanOutResults` (or a custom key) into one result. */
|
|
37
|
+
export class FanInNode extends AsyncNode {
|
|
38
|
+
constructor({ combine, inputKey = 'fanOutResults', outputKey = 'fanInResult' } = {}) {
|
|
39
|
+
super();
|
|
40
|
+
this.combine = combine ?? (results => results);
|
|
41
|
+
this.inputKey = inputKey;
|
|
42
|
+
this.outputKey = outputKey;
|
|
43
|
+
}
|
|
44
|
+
async prepAsync(shared) {
|
|
45
|
+
return shared[this.inputKey] ?? this.params.input;
|
|
46
|
+
}
|
|
47
|
+
async execAsync(prepRes, shared, observer = {}) {
|
|
48
|
+
return typeof this.combine === 'function'
|
|
49
|
+
? await this.combine(prepRes, shared, observer)
|
|
50
|
+
: this.combine;
|
|
51
|
+
}
|
|
52
|
+
async postAsync(shared, prepRes, execRes) {
|
|
53
|
+
shared[this.outputKey] = execRes;
|
|
54
|
+
return Object.keys(this.successors).length ? 'default' : execRes;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
async function mapWithConcurrency(items, mapper, concurrency = Infinity) {
|
|
58
|
+
const results = new Array(items.length);
|
|
59
|
+
let nextIndex = 0;
|
|
60
|
+
const limit = Math.max(1, Math.min(concurrency === 0 ? Infinity : concurrency, items.length || 1));
|
|
61
|
+
const worker = async () => {
|
|
62
|
+
while (true) {
|
|
63
|
+
const index = nextIndex++;
|
|
64
|
+
if (index >= items.length)
|
|
65
|
+
return;
|
|
66
|
+
results[index] = await mapper(items[index], index);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
await Promise.all(Array.from({ length: limit }, worker));
|
|
70
|
+
return results;
|
|
71
|
+
}
|
|
72
|
+
async function runMapper(mapper, item, index, shared, observer) {
|
|
73
|
+
if (typeof mapper === 'function') {
|
|
74
|
+
return await mapper(item, index, shared, observer);
|
|
75
|
+
}
|
|
76
|
+
const target = mapper instanceof AsyncFlow ? mapper : new AsyncFlow(mapper);
|
|
77
|
+
const context = { ...shared, item, index };
|
|
78
|
+
return await target.runAsync(context, observer);
|
|
79
|
+
}
|
|
80
|
+
/** Parallel/serial collection mapping with explicit input and output contracts. */
|
|
81
|
+
export class MapNode extends AsyncNode {
|
|
82
|
+
constructor({ mapper = null, inputKey = 'items', outputKey = 'mapResults', parallel = false, concurrency = 4, inputContract = null, outputContract = null, onError = 'throw', } = {}) {
|
|
83
|
+
super();
|
|
84
|
+
this.mapper = mapper;
|
|
85
|
+
this.inputKey = inputKey;
|
|
86
|
+
this.outputKey = outputKey;
|
|
87
|
+
this.parallel = parallel;
|
|
88
|
+
this.concurrency = concurrency;
|
|
89
|
+
this.inputContract = inputContract;
|
|
90
|
+
this.outputContract = outputContract;
|
|
91
|
+
this.onError = onError;
|
|
92
|
+
}
|
|
93
|
+
async prepAsync(shared = {}) {
|
|
94
|
+
assertState(shared, this.inputContract, { phase: `${this.constructor.name}:input` });
|
|
95
|
+
const items = this.params.items ?? shared[this.inputKey] ?? [];
|
|
96
|
+
if (!Array.isArray(items))
|
|
97
|
+
throw new Error(`${this.constructor.name} expects an array from '${this.inputKey}'.`);
|
|
98
|
+
return items;
|
|
99
|
+
}
|
|
100
|
+
async execAsync(items, shared = {}, observer = {}) {
|
|
101
|
+
const mapper = this.params.mapper ?? this.mapper;
|
|
102
|
+
if (!mapper)
|
|
103
|
+
throw new Error(`${this.constructor.name} requires a mapper function or node.`);
|
|
104
|
+
const mapOne = async (item, index) => {
|
|
105
|
+
try {
|
|
106
|
+
return await runMapper(mapper, item, index, shared, observer);
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
observer.emit?.('map:item_error', { index, item, error: { message: error.message } });
|
|
110
|
+
if (this.onError === 'skip')
|
|
111
|
+
return undefined;
|
|
112
|
+
if (this.onError === 'record')
|
|
113
|
+
return { error: { message: error.message }, item, index };
|
|
114
|
+
throw error;
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
const results = this.parallel
|
|
118
|
+
? await mapWithConcurrency(items, mapOne, this.concurrency)
|
|
119
|
+
: await mapWithConcurrency(items, mapOne, 1);
|
|
120
|
+
if (this.onError === 'skip')
|
|
121
|
+
return results.filter(value => value !== undefined);
|
|
122
|
+
return results;
|
|
123
|
+
}
|
|
124
|
+
async postAsync(shared, prepRes, execRes) {
|
|
125
|
+
shared[this.outputKey] = execRes;
|
|
126
|
+
assertState(shared, this.outputContract, { phase: `${this.constructor.name}:output` });
|
|
127
|
+
return Object.keys(this.successors).length ? 'default' : execRes;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
/** Fold a collection into a single value. */
|
|
131
|
+
export class ReduceNode extends AsyncNode {
|
|
132
|
+
constructor({ reducer = null, initialValue = undefined, inputKey = 'mapResults', outputKey = 'reduceResult', inputContract = null, outputContract = null, } = {}) {
|
|
133
|
+
super();
|
|
134
|
+
this.reducer = reducer;
|
|
135
|
+
this.initialValue = initialValue;
|
|
136
|
+
this.inputKey = inputKey;
|
|
137
|
+
this.outputKey = outputKey;
|
|
138
|
+
this.inputContract = inputContract;
|
|
139
|
+
this.outputContract = outputContract;
|
|
140
|
+
}
|
|
141
|
+
async prepAsync(shared = {}) {
|
|
142
|
+
assertState(shared, this.inputContract, { phase: `${this.constructor.name}:input` });
|
|
143
|
+
const items = this.params.items ?? shared[this.inputKey] ?? [];
|
|
144
|
+
if (!Array.isArray(items))
|
|
145
|
+
throw new Error(`${this.constructor.name} expects an array from '${this.inputKey}'.`);
|
|
146
|
+
return {
|
|
147
|
+
items,
|
|
148
|
+
initialValue: this.params.initialValue ?? shared[this.params.initialKey ?? 'reduceInitial'] ?? this.initialValue,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
async execAsync(prepRes, shared = {}, observer = {}) {
|
|
152
|
+
const reducer = this.params.reducer ?? this.reducer;
|
|
153
|
+
if (typeof reducer !== 'function')
|
|
154
|
+
throw new Error(`${this.constructor.name} requires a reducer function.`);
|
|
155
|
+
let accumulator = prepRes.initialValue;
|
|
156
|
+
for (let index = 0; index < prepRes.items.length; index += 1) {
|
|
157
|
+
accumulator = await reducer(accumulator, prepRes.items[index], index, shared, observer);
|
|
158
|
+
observer.emit?.('reduce:step', { index, value: accumulator });
|
|
159
|
+
}
|
|
160
|
+
return accumulator;
|
|
161
|
+
}
|
|
162
|
+
async postAsync(shared, prepRes, execRes) {
|
|
163
|
+
shared[this.outputKey] = execRes;
|
|
164
|
+
assertState(shared, this.outputContract, { phase: `${this.constructor.name}:output` });
|
|
165
|
+
return Object.keys(this.successors).length ? 'default' : execRes;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
/** Combine several named shared-state values into one object/array. */
|
|
169
|
+
export class JoinNode extends AsyncNode {
|
|
170
|
+
constructor({ inputs = [], strategy = 'object', outputKey = 'joined', join = null, inputContract = null } = {}) {
|
|
171
|
+
super();
|
|
172
|
+
this.inputs = [...inputs];
|
|
173
|
+
this.strategy = strategy;
|
|
174
|
+
this.outputKey = outputKey;
|
|
175
|
+
this.join = join;
|
|
176
|
+
this.inputContract = inputContract;
|
|
177
|
+
}
|
|
178
|
+
async prepAsync(shared = {}) {
|
|
179
|
+
assertState(shared, this.inputContract, { phase: `${this.constructor.name}:input` });
|
|
180
|
+
const inputs = this.params.inputs ?? this.inputs;
|
|
181
|
+
const values = {};
|
|
182
|
+
for (const entry of Array.isArray(inputs) ? inputs : Object.entries(inputs)) {
|
|
183
|
+
const [key, path] = Array.isArray(entry) ? entry : [String(entry), String(entry)];
|
|
184
|
+
values[key] = path.split('.').reduce((value, part) => value?.[part], shared);
|
|
185
|
+
}
|
|
186
|
+
return values;
|
|
187
|
+
}
|
|
188
|
+
async execAsync(values, shared = {}, observer = {}) {
|
|
189
|
+
if (typeof this.join === 'function')
|
|
190
|
+
return await this.join(values, shared, observer);
|
|
191
|
+
if (this.strategy === 'array')
|
|
192
|
+
return Object.values(values);
|
|
193
|
+
if (this.strategy === 'merge')
|
|
194
|
+
return Object.assign({}, ...Object.values(values));
|
|
195
|
+
return values;
|
|
196
|
+
}
|
|
197
|
+
async postAsync(shared, prepRes, execRes) {
|
|
198
|
+
shared[this.outputKey] = execRes;
|
|
199
|
+
return Object.keys(this.successors).length ? 'default' : execRes;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
/** Route to a named successor based on predicates or a condition function. */
|
|
203
|
+
export class BranchNode extends AsyncNode {
|
|
204
|
+
constructor({ cases = {}, condition = null, defaultAction = 'default', decisionKey = 'branchDecision', inputContract = null, } = {}) {
|
|
205
|
+
super();
|
|
206
|
+
this.cases = { ...cases };
|
|
207
|
+
this.condition = condition;
|
|
208
|
+
this.defaultAction = defaultAction;
|
|
209
|
+
this.decisionKey = decisionKey;
|
|
210
|
+
this.inputContract = inputContract;
|
|
211
|
+
}
|
|
212
|
+
async prepAsync(shared = {}) {
|
|
213
|
+
assertState(shared, this.inputContract, { phase: `${this.constructor.name}:input` });
|
|
214
|
+
return shared;
|
|
215
|
+
}
|
|
216
|
+
async execAsync(shared, _sharedRef, observer = {}) {
|
|
217
|
+
const evaluate = async (value) => {
|
|
218
|
+
if (typeof value === 'function')
|
|
219
|
+
return await value(shared, observer);
|
|
220
|
+
return value;
|
|
221
|
+
};
|
|
222
|
+
const condition = this.params.condition ?? this.condition;
|
|
223
|
+
if (typeof condition === 'function') {
|
|
224
|
+
const result = await condition(shared, observer);
|
|
225
|
+
if (typeof result === 'string')
|
|
226
|
+
return result;
|
|
227
|
+
return result ? 'true' : 'false';
|
|
228
|
+
}
|
|
229
|
+
const cases = this.params.cases ?? this.cases;
|
|
230
|
+
for (const [name, predicate] of Object.entries(cases)) {
|
|
231
|
+
if (await evaluate(predicate))
|
|
232
|
+
return name;
|
|
233
|
+
}
|
|
234
|
+
return this.params.defaultAction ?? this.defaultAction;
|
|
235
|
+
}
|
|
236
|
+
async postAsync(shared, prepRes, execRes) {
|
|
237
|
+
shared[this.decisionKey] = execRes;
|
|
238
|
+
return Object.keys(this.successors).length ? execRes : execRes;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
/** Run a nested node/flow under explicit iteration and item budgets. */
|
|
242
|
+
export class CycleNode extends AsyncNode {
|
|
243
|
+
constructor({ node = null, flow = null, until = null, maxIterations = 10, maxItems = 1000, inputKey = 'items', outputKey = 'cycleResults', metaKey = 'cycleMeta', collect = true, } = {}) {
|
|
244
|
+
super();
|
|
245
|
+
this.node = node;
|
|
246
|
+
this.flow = flow;
|
|
247
|
+
this.until = until;
|
|
248
|
+
this.maxIterations = maxIterations;
|
|
249
|
+
this.maxItems = maxItems;
|
|
250
|
+
this.inputKey = inputKey;
|
|
251
|
+
this.outputKey = outputKey;
|
|
252
|
+
this.metaKey = metaKey;
|
|
253
|
+
this.collect = collect;
|
|
254
|
+
}
|
|
255
|
+
async prepAsync(shared = {}) {
|
|
256
|
+
const items = this.params.items ?? shared[this.inputKey] ?? [];
|
|
257
|
+
if (!Array.isArray(items))
|
|
258
|
+
throw new Error(`${this.constructor.name} expects an array from '${this.inputKey}'.`);
|
|
259
|
+
return [...items].slice(0, this.params.maxItems ?? this.maxItems);
|
|
260
|
+
}
|
|
261
|
+
async execAsync(items, shared = {}, observer = {}) {
|
|
262
|
+
const target = this.params.flow ?? this.flow ?? this.params.node ?? this.node;
|
|
263
|
+
if (!target && typeof this.params.run !== 'function') {
|
|
264
|
+
throw new Error(`${this.constructor.name} requires a node, flow, or run function.`);
|
|
265
|
+
}
|
|
266
|
+
const runner = typeof this.params.run === 'function'
|
|
267
|
+
? this.params.run
|
|
268
|
+
: async (item, index, context) => {
|
|
269
|
+
if (target instanceof AsyncFlow)
|
|
270
|
+
return await target.runAsync(context, observer);
|
|
271
|
+
return await new AsyncFlow(target).runAsync(context, observer);
|
|
272
|
+
};
|
|
273
|
+
const results = [];
|
|
274
|
+
let iterations = 0;
|
|
275
|
+
let stoppedBy = 'complete';
|
|
276
|
+
for (const item of items) {
|
|
277
|
+
if (iterations >= (this.params.maxIterations ?? this.maxIterations)) {
|
|
278
|
+
stoppedBy = 'maxIterations';
|
|
279
|
+
break;
|
|
280
|
+
}
|
|
281
|
+
shared.cycleInput = item;
|
|
282
|
+
shared.cycleIndex = iterations;
|
|
283
|
+
const result = await runner(item, iterations, shared);
|
|
284
|
+
iterations += 1;
|
|
285
|
+
if (this.collect)
|
|
286
|
+
results.push(result);
|
|
287
|
+
observer.emit?.('cycle:iteration', { index: iterations, item, result });
|
|
288
|
+
if (typeof this.until === 'function' && await this.until(result, shared, iterations)) {
|
|
289
|
+
stoppedBy = 'until';
|
|
290
|
+
break;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
if (items.length > (this.params.maxItems ?? this.maxItems))
|
|
294
|
+
stoppedBy = 'maxItems';
|
|
295
|
+
return { results, iterations, stoppedBy };
|
|
296
|
+
}
|
|
297
|
+
async postAsync(shared, prepRes, execRes) {
|
|
298
|
+
shared[this.outputKey] = execRes.results;
|
|
299
|
+
shared[this.metaKey] = { iterations: execRes.iterations, stoppedBy: execRes.stoppedBy };
|
|
300
|
+
return Object.keys(this.successors).length ? 'default' : execRes.results;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
/** Describe a flow as JSON nodes/edges for visual authoring or docs. */
|
|
304
|
+
export function describeGraph(startNode) {
|
|
305
|
+
const nodes = [];
|
|
306
|
+
const edges = [];
|
|
307
|
+
const seen = new Set();
|
|
308
|
+
const ids = new Map();
|
|
309
|
+
const queue = startNode ? [startNode] : [];
|
|
310
|
+
while (queue.length) {
|
|
311
|
+
const node = queue.shift();
|
|
312
|
+
if (!node || seen.has(node))
|
|
313
|
+
continue;
|
|
314
|
+
seen.add(node);
|
|
315
|
+
const id = `node-${nodes.length + 1}`;
|
|
316
|
+
ids.set(node, id);
|
|
317
|
+
nodes.push({
|
|
318
|
+
id,
|
|
319
|
+
label: node.constructor?.name ?? 'Node',
|
|
320
|
+
params: node.params ?? {},
|
|
321
|
+
successors: Object.keys(node.successors ?? {}),
|
|
322
|
+
});
|
|
323
|
+
for (const [action, successor] of Object.entries(node.successors ?? {})) {
|
|
324
|
+
edges.push({ from: id, to: ids.get(successor) ?? `node-${seen.size + 1}`, action });
|
|
325
|
+
if (!seen.has(successor))
|
|
326
|
+
queue.push(successor);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
return { nodes, edges };
|
|
330
|
+
}
|
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
export function normalizeUsage(raw: any): {
|
|
2
|
+
prompt: number;
|
|
3
|
+
completion: number;
|
|
4
|
+
total: number;
|
|
5
|
+
raw: any;
|
|
6
|
+
} | null;
|
|
7
|
+
export function normalizeCompletion(raw: any, provider?: string): {
|
|
8
|
+
text: string;
|
|
9
|
+
reasoning: string;
|
|
10
|
+
toolCalls: {
|
|
11
|
+
index: any;
|
|
12
|
+
id: any;
|
|
13
|
+
type: any;
|
|
14
|
+
function: {
|
|
15
|
+
name: any;
|
|
16
|
+
arguments: any;
|
|
17
|
+
};
|
|
18
|
+
}[];
|
|
19
|
+
usage: {
|
|
20
|
+
prompt: number;
|
|
21
|
+
completion: number;
|
|
22
|
+
total: number;
|
|
23
|
+
raw: any;
|
|
24
|
+
} | null;
|
|
25
|
+
finishReason: any;
|
|
26
|
+
cost: any;
|
|
27
|
+
raw: any;
|
|
28
|
+
};
|
|
29
|
+
export function resolveCost({ provider, model, usage, pricing, result }: {
|
|
30
|
+
provider: any;
|
|
31
|
+
model: any;
|
|
32
|
+
usage: any;
|
|
33
|
+
pricing?: null | undefined;
|
|
34
|
+
result?: null | undefined;
|
|
35
|
+
}): Promise<any>;
|
|
36
|
+
export function registerLLMAdapter(id: any, adapter: any): any;
|
|
37
|
+
export function listLLMAdapters(): any[];
|
|
38
|
+
export function resolveLLMAdapter(provider: any, options?: {}): any;
|
|
39
|
+
export function negotiateCapabilities(adapter: any, requested?: any[]): {
|
|
40
|
+
provider: any;
|
|
41
|
+
requested: any[];
|
|
42
|
+
supported: any;
|
|
43
|
+
missing: any[];
|
|
44
|
+
};
|
|
45
|
+
/** Stable cache key: explicit template/function, or a hash of the request. */
|
|
46
|
+
export function buildLLMCacheKey(request: any, shared?: {}): any;
|
|
47
|
+
export const LLM_RETRY_EVENT: "llm:retry";
|
|
48
|
+
export const LLM_FALLBACK_EVENT: "llm:fallback";
|
|
49
|
+
export const LLM_NEGOTIATED_EVENT: "llm:negotiated";
|
|
50
|
+
export const LLM_CAPABILITY_EVENT: "llm:capability";
|
|
51
|
+
export const LLM_USAGE_EVENT: "llm:usage";
|
|
52
|
+
export const LLM_TOOL_CALL_EVENT: "llm:tool_call";
|
|
53
|
+
export const LLM_CACHE_HIT_EVENT: "llm:cache_hit";
|
|
54
|
+
export namespace LLM_EVENTS {
|
|
55
|
+
export { LLM_DELTA_EVENT as delta };
|
|
56
|
+
export { LLM_REASONING_EVENT as reasoning };
|
|
57
|
+
export { LLM_DONE_EVENT as done };
|
|
58
|
+
export { LLM_RETRY_EVENT as retry };
|
|
59
|
+
export { LLM_FALLBACK_EVENT as fallback };
|
|
60
|
+
export { LLM_NEGOTIATED_EVENT as negotiated };
|
|
61
|
+
export { LLM_CAPABILITY_EVENT as capability };
|
|
62
|
+
export { LLM_USAGE_EVENT as usage };
|
|
63
|
+
export { LLM_TOOL_CALL_EVENT as toolCall };
|
|
64
|
+
export { LLM_CACHE_HIT_EVENT as cacheHit };
|
|
65
|
+
}
|
|
66
|
+
/** Base class for provider-neutral adapters. */
|
|
67
|
+
export class LLMAdapter {
|
|
68
|
+
static id: string;
|
|
69
|
+
constructor(options?: {});
|
|
70
|
+
options: {};
|
|
71
|
+
provider: any;
|
|
72
|
+
capabilities: any;
|
|
73
|
+
get id(): any;
|
|
74
|
+
requireModel(request: any): any;
|
|
75
|
+
complete(): Promise<void>;
|
|
76
|
+
stream(request: any, context?: {}): Promise<void>;
|
|
77
|
+
}
|
|
78
|
+
/** OpenAI-compatible dialect: OpenAI, DeepSeek, OpenRouter, Hugging Face, Groq, Together. */
|
|
79
|
+
export class OpenAICompatibleAdapter extends LLMAdapter {
|
|
80
|
+
baseUrl: any;
|
|
81
|
+
buildPayload(request: any): any;
|
|
82
|
+
buildHeaders(request: any): any;
|
|
83
|
+
url(request: any): any;
|
|
84
|
+
complete(request: any, context?: {}): Promise<{
|
|
85
|
+
text: string;
|
|
86
|
+
reasoning: string;
|
|
87
|
+
toolCalls: {
|
|
88
|
+
index: any;
|
|
89
|
+
id: any;
|
|
90
|
+
type: any;
|
|
91
|
+
function: {
|
|
92
|
+
name: any;
|
|
93
|
+
arguments: any;
|
|
94
|
+
};
|
|
95
|
+
}[];
|
|
96
|
+
usage: {
|
|
97
|
+
prompt: number;
|
|
98
|
+
completion: number;
|
|
99
|
+
total: number;
|
|
100
|
+
raw: any;
|
|
101
|
+
} | null;
|
|
102
|
+
finishReason: any;
|
|
103
|
+
cost: any;
|
|
104
|
+
raw: any;
|
|
105
|
+
}>;
|
|
106
|
+
stream(request: any, context?: {}): Promise<{
|
|
107
|
+
text: string;
|
|
108
|
+
reasoning: string;
|
|
109
|
+
toolCalls: {
|
|
110
|
+
index: any;
|
|
111
|
+
id: any;
|
|
112
|
+
type: any;
|
|
113
|
+
function: {
|
|
114
|
+
name: any;
|
|
115
|
+
arguments: any;
|
|
116
|
+
};
|
|
117
|
+
}[];
|
|
118
|
+
usage: {
|
|
119
|
+
prompt: number;
|
|
120
|
+
completion: number;
|
|
121
|
+
total: number;
|
|
122
|
+
raw: any;
|
|
123
|
+
} | null;
|
|
124
|
+
finishReason: any;
|
|
125
|
+
cost: any;
|
|
126
|
+
raw: any;
|
|
127
|
+
}>;
|
|
128
|
+
}
|
|
129
|
+
export class AnthropicAdapter extends LLMAdapter {
|
|
130
|
+
baseUrl: any;
|
|
131
|
+
buildPayload(request: any): any;
|
|
132
|
+
buildHeaders(request: any): any;
|
|
133
|
+
url(): any;
|
|
134
|
+
complete(request: any, context?: {}): Promise<{
|
|
135
|
+
text: string;
|
|
136
|
+
reasoning: string;
|
|
137
|
+
toolCalls: {
|
|
138
|
+
index: any;
|
|
139
|
+
id: any;
|
|
140
|
+
type: any;
|
|
141
|
+
function: {
|
|
142
|
+
name: any;
|
|
143
|
+
arguments: any;
|
|
144
|
+
};
|
|
145
|
+
}[];
|
|
146
|
+
usage: {
|
|
147
|
+
prompt: number;
|
|
148
|
+
completion: number;
|
|
149
|
+
total: number;
|
|
150
|
+
raw: any;
|
|
151
|
+
} | null;
|
|
152
|
+
finishReason: any;
|
|
153
|
+
cost: any;
|
|
154
|
+
raw: any;
|
|
155
|
+
}>;
|
|
156
|
+
stream(request: any, context?: {}): Promise<{
|
|
157
|
+
text: string;
|
|
158
|
+
reasoning: string;
|
|
159
|
+
toolCalls: {
|
|
160
|
+
index: any;
|
|
161
|
+
id: any;
|
|
162
|
+
type: any;
|
|
163
|
+
function: {
|
|
164
|
+
name: any;
|
|
165
|
+
arguments: any;
|
|
166
|
+
};
|
|
167
|
+
}[];
|
|
168
|
+
usage: {
|
|
169
|
+
prompt: number;
|
|
170
|
+
completion: number;
|
|
171
|
+
total: number;
|
|
172
|
+
raw: any;
|
|
173
|
+
} | null;
|
|
174
|
+
finishReason: any;
|
|
175
|
+
cost: any;
|
|
176
|
+
raw: any;
|
|
177
|
+
}>;
|
|
178
|
+
}
|
|
179
|
+
export class OllamaAdapter extends LLMAdapter {
|
|
180
|
+
baseUrl: any;
|
|
181
|
+
buildPayload(request: any): any;
|
|
182
|
+
url(request: any): any;
|
|
183
|
+
buildHeaders(request: any): any;
|
|
184
|
+
complete(request: any, context?: {}): Promise<{
|
|
185
|
+
text: string;
|
|
186
|
+
reasoning: string;
|
|
187
|
+
toolCalls: {
|
|
188
|
+
index: any;
|
|
189
|
+
id: any;
|
|
190
|
+
type: any;
|
|
191
|
+
function: {
|
|
192
|
+
name: any;
|
|
193
|
+
arguments: any;
|
|
194
|
+
};
|
|
195
|
+
}[];
|
|
196
|
+
usage: {
|
|
197
|
+
prompt: number;
|
|
198
|
+
completion: number;
|
|
199
|
+
total: number;
|
|
200
|
+
raw: any;
|
|
201
|
+
} | null;
|
|
202
|
+
finishReason: any;
|
|
203
|
+
cost: any;
|
|
204
|
+
raw: any;
|
|
205
|
+
}>;
|
|
206
|
+
stream(request: any, context?: {}): Promise<{
|
|
207
|
+
text: string;
|
|
208
|
+
reasoning: string;
|
|
209
|
+
toolCalls: {
|
|
210
|
+
index: any;
|
|
211
|
+
id: any;
|
|
212
|
+
type: any;
|
|
213
|
+
function: {
|
|
214
|
+
name: any;
|
|
215
|
+
arguments: any;
|
|
216
|
+
};
|
|
217
|
+
}[];
|
|
218
|
+
usage: {
|
|
219
|
+
prompt: number;
|
|
220
|
+
completion: number;
|
|
221
|
+
total: number;
|
|
222
|
+
raw: any;
|
|
223
|
+
} | null;
|
|
224
|
+
finishReason: any;
|
|
225
|
+
cost: any;
|
|
226
|
+
raw: any;
|
|
227
|
+
}>;
|
|
228
|
+
}
|
|
229
|
+
export class GeminiAdapter extends LLMAdapter {
|
|
230
|
+
baseUrl: any;
|
|
231
|
+
buildUrl(request: any, { stream }?: {
|
|
232
|
+
stream?: boolean | undefined;
|
|
233
|
+
}): string;
|
|
234
|
+
buildPayload(request: any): {
|
|
235
|
+
generationConfig?: any;
|
|
236
|
+
tools?: any;
|
|
237
|
+
systemInstruction?: {
|
|
238
|
+
parts: {
|
|
239
|
+
text: any;
|
|
240
|
+
}[];
|
|
241
|
+
} | undefined;
|
|
242
|
+
contents: any;
|
|
243
|
+
};
|
|
244
|
+
normalize(data: any): {
|
|
245
|
+
text: string;
|
|
246
|
+
reasoning: string;
|
|
247
|
+
toolCalls: {
|
|
248
|
+
index: any;
|
|
249
|
+
id: any;
|
|
250
|
+
type: any;
|
|
251
|
+
function: {
|
|
252
|
+
name: any;
|
|
253
|
+
arguments: any;
|
|
254
|
+
};
|
|
255
|
+
}[];
|
|
256
|
+
usage: {
|
|
257
|
+
prompt: number;
|
|
258
|
+
completion: number;
|
|
259
|
+
total: number;
|
|
260
|
+
raw: any;
|
|
261
|
+
} | null;
|
|
262
|
+
finishReason: any;
|
|
263
|
+
cost: any;
|
|
264
|
+
raw: any;
|
|
265
|
+
};
|
|
266
|
+
complete(request: any, context?: {}): Promise<{
|
|
267
|
+
text: string;
|
|
268
|
+
reasoning: string;
|
|
269
|
+
toolCalls: {
|
|
270
|
+
index: any;
|
|
271
|
+
id: any;
|
|
272
|
+
type: any;
|
|
273
|
+
function: {
|
|
274
|
+
name: any;
|
|
275
|
+
arguments: any;
|
|
276
|
+
};
|
|
277
|
+
}[];
|
|
278
|
+
usage: {
|
|
279
|
+
prompt: number;
|
|
280
|
+
completion: number;
|
|
281
|
+
total: number;
|
|
282
|
+
raw: any;
|
|
283
|
+
} | null;
|
|
284
|
+
finishReason: any;
|
|
285
|
+
cost: any;
|
|
286
|
+
raw: any;
|
|
287
|
+
}>;
|
|
288
|
+
stream(request: any, context?: {}): Promise<{
|
|
289
|
+
text: string;
|
|
290
|
+
reasoning: string;
|
|
291
|
+
toolCalls: {
|
|
292
|
+
index: any;
|
|
293
|
+
id: any;
|
|
294
|
+
type: any;
|
|
295
|
+
function: {
|
|
296
|
+
name: any;
|
|
297
|
+
arguments: any;
|
|
298
|
+
};
|
|
299
|
+
}[];
|
|
300
|
+
usage: {
|
|
301
|
+
prompt: number;
|
|
302
|
+
completion: number;
|
|
303
|
+
total: number;
|
|
304
|
+
raw: any;
|
|
305
|
+
} | null;
|
|
306
|
+
finishReason: any;
|
|
307
|
+
cost: any;
|
|
308
|
+
raw: any;
|
|
309
|
+
}>;
|
|
310
|
+
}
|
|
311
|
+
export const llmAdapters: Map<any, any>;
|
|
312
|
+
/** One LLM node for every provider, with retries, fallbacks and usage events. */
|
|
313
|
+
export class LLMNode extends AsyncNode {
|
|
314
|
+
static getToolDefinition(): {
|
|
315
|
+
name: string;
|
|
316
|
+
description: string;
|
|
317
|
+
parameters: {
|
|
318
|
+
type: string;
|
|
319
|
+
properties: {
|
|
320
|
+
prompt: {
|
|
321
|
+
type: string;
|
|
322
|
+
description: string;
|
|
323
|
+
};
|
|
324
|
+
messages: {
|
|
325
|
+
type: string;
|
|
326
|
+
description: string;
|
|
327
|
+
};
|
|
328
|
+
provider: {
|
|
329
|
+
type: string;
|
|
330
|
+
description: string;
|
|
331
|
+
};
|
|
332
|
+
model: {
|
|
333
|
+
type: string;
|
|
334
|
+
description: string;
|
|
335
|
+
};
|
|
336
|
+
fallbackProviders: {
|
|
337
|
+
type: string;
|
|
338
|
+
items: {
|
|
339
|
+
type: string;
|
|
340
|
+
};
|
|
341
|
+
};
|
|
342
|
+
};
|
|
343
|
+
required: string[];
|
|
344
|
+
};
|
|
345
|
+
};
|
|
346
|
+
constructor(options?: {});
|
|
347
|
+
options: {};
|
|
348
|
+
prepAsync(shared?: {}, observer?: {}): Promise<{
|
|
349
|
+
request: any;
|
|
350
|
+
shared: {};
|
|
351
|
+
}>;
|
|
352
|
+
execAsync(prepRes: any, _shared: any, observer?: {}): Promise<any>;
|
|
353
|
+
postAsync(shared: any, prepRes: any, execRes: any): Promise<any>;
|
|
354
|
+
}
|
|
355
|
+
import { LLM_DELTA_EVENT } from '../streaming.js';
|
|
356
|
+
import { LLM_REASONING_EVENT } from '../streaming.js';
|
|
357
|
+
import { LLM_DONE_EVENT } from '../streaming.js';
|
|
358
|
+
import { AsyncNode } from '../qflow.js';
|
|
359
|
+
export { LLM_DELTA_EVENT, LLM_REASONING_EVENT, LLM_DONE_EVENT };
|