@dudko.dev/agent-web 0.0.42 → 0.0.44
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/README.md +150 -4
- package/dist/agent-web.js +155 -155
- package/dist/agent-web.js.map +1 -1
- package/dist/{chunk-DAREMCXF.js → chunk-236RCVAZ.js} +40 -8
- package/dist/chunk-236RCVAZ.js.map +1 -0
- package/dist/{chunk-2MLHZ6LQ.js → chunk-Y4MIIBPB.js} +2 -2
- package/dist/{chunk-ZSZSSJS4.js → chunk-Y6CD3WOK.js} +176 -10
- package/dist/chunk-Y6CD3WOK.js.map +1 -0
- package/dist/{chunk-YPCHD6DV.js → chunk-YGWIOO27.js} +31 -12
- package/dist/chunk-YGWIOO27.js.map +1 -0
- package/dist/index.cjs +1248 -108
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1777 -1643
- package/dist/index.d.ts +1777 -1643
- package/dist/index.js +996 -89
- package/dist/index.js.map +1 -1
- package/dist/mcp.cjs +136 -5
- package/dist/mcp.cjs.map +1 -1
- package/dist/mcp.d.cts +5 -2
- package/dist/mcp.d.ts +5 -2
- package/dist/mcp.js +19 -8
- package/dist/mcp.js.map +1 -1
- package/dist/pipeline-engine.cjs +1130 -0
- package/dist/pipeline-engine.cjs.map +1 -0
- package/dist/pipeline-engine.d.cts +226 -0
- package/dist/pipeline-engine.d.ts +226 -0
- package/dist/pipeline-engine.js +1094 -0
- package/dist/pipeline-engine.js.map +1 -0
- package/dist/{sandbox-3VRX47UY.js → sandbox-7CGSIIIZ.js} +4 -4
- package/dist/sandbox-worker.cjs +30 -11
- package/dist/sandbox-worker.cjs.map +1 -1
- package/dist/sandbox-worker.js +1 -1
- package/dist/{elicitation-DS-84_bF.d.cts → vault-Mar2CY0J.d.cts} +526 -526
- package/dist/{elicitation-DS-84_bF.d.ts → vault-Mar2CY0J.d.ts} +526 -526
- package/package.json +13 -3
- package/dist/chunk-DAREMCXF.js.map +0 -1
- package/dist/chunk-YPCHD6DV.js.map +0 -1
- package/dist/chunk-ZSZSSJS4.js.map +0 -1
- /package/dist/{chunk-2MLHZ6LQ.js.map → chunk-Y4MIIBPB.js.map} +0 -0
- /package/dist/{sandbox-3VRX47UY.js.map → sandbox-7CGSIIIZ.js.map} +0 -0
|
@@ -0,0 +1,1130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/pipeline/engine.ts
|
|
21
|
+
var engine_exports = {};
|
|
22
|
+
__export(engine_exports, {
|
|
23
|
+
PIPELINE_LIMITS: () => PIPELINE_LIMITS,
|
|
24
|
+
PIPELINE_ROUTER_SYSTEM: () => PIPELINE_ROUTER_SYSTEM,
|
|
25
|
+
PipelineNodeError: () => PipelineNodeError,
|
|
26
|
+
PipelineValidationError: () => PipelineValidationError,
|
|
27
|
+
matchPipelineRoute: () => matchPipelineRoute,
|
|
28
|
+
normalizePipeline: () => normalizePipeline,
|
|
29
|
+
preparePipeline: () => preparePipeline,
|
|
30
|
+
renderPipelineNodeInput: () => renderPipelineNodeInput,
|
|
31
|
+
renderPipelineRoutePrompt: () => renderPipelineRoutePrompt,
|
|
32
|
+
runPipelineGraph: () => runPipelineGraph,
|
|
33
|
+
testPipelineCondition: () => testPipelineCondition,
|
|
34
|
+
validatePipeline: () => validatePipeline
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(engine_exports);
|
|
37
|
+
var PIPELINE_LIMITS = {
|
|
38
|
+
// Defaults of `limits`.
|
|
39
|
+
maxNodeVisits: 3,
|
|
40
|
+
maxRuns: 25,
|
|
41
|
+
// The largest values `limits` accepts (the smallest is 1).
|
|
42
|
+
maxNodeVisitsCeiling: 100,
|
|
43
|
+
maxRunsCeiling: 1e3,
|
|
44
|
+
// The size of a definition.
|
|
45
|
+
maxNodes: 200,
|
|
46
|
+
maxEdges: 1e3,
|
|
47
|
+
maxRoutes: 20,
|
|
48
|
+
// `matches`: the longest pattern, and the largest compiled program (what `a{50}` grows into).
|
|
49
|
+
maxPatternLength: 500,
|
|
50
|
+
maxPatternProgram: 1e3,
|
|
51
|
+
// `contains` and `matches` look at this many leading characters of an output.
|
|
52
|
+
conditionInputChars: 5e4
|
|
53
|
+
};
|
|
54
|
+
var PipelineValidationError = class extends Error {
|
|
55
|
+
errors;
|
|
56
|
+
constructor(errors) {
|
|
57
|
+
super(`Invalid pipeline: ${errors.map((e) => e.message).join("; ")}`);
|
|
58
|
+
this.name = "PipelineValidationError";
|
|
59
|
+
this.errors = errors;
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
var PipelineNodeError = class extends Error {
|
|
63
|
+
nodeId;
|
|
64
|
+
visit;
|
|
65
|
+
constructor(nodeId, visit, cause) {
|
|
66
|
+
const reason = cause instanceof Error ? cause.message : String(cause);
|
|
67
|
+
super(`Pipeline node "${nodeId}" failed (visit ${visit}): ${reason}`, { cause });
|
|
68
|
+
this.name = "PipelineNodeError";
|
|
69
|
+
this.nodeId = nodeId;
|
|
70
|
+
this.visit = visit;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
var cp = (s) => s.codePointAt(0);
|
|
74
|
+
var DIGIT = [48, 57];
|
|
75
|
+
var WORD = [48, 57, 65, 90, 95, 95, 97, 122];
|
|
76
|
+
var SPACE = [
|
|
77
|
+
9,
|
|
78
|
+
13,
|
|
79
|
+
32,
|
|
80
|
+
32,
|
|
81
|
+
160,
|
|
82
|
+
160,
|
|
83
|
+
5760,
|
|
84
|
+
5760,
|
|
85
|
+
8192,
|
|
86
|
+
8202,
|
|
87
|
+
8232,
|
|
88
|
+
8233,
|
|
89
|
+
8239,
|
|
90
|
+
8239,
|
|
91
|
+
8287,
|
|
92
|
+
8287,
|
|
93
|
+
12288,
|
|
94
|
+
12288,
|
|
95
|
+
65279,
|
|
96
|
+
65279
|
|
97
|
+
];
|
|
98
|
+
var MAX_CODE_POINT = 1114111;
|
|
99
|
+
var MAX_REPEAT = 1e3;
|
|
100
|
+
var inRanges = (r, c) => {
|
|
101
|
+
for (let i = 0; i < r.length; i += 2) {
|
|
102
|
+
if (c >= r[i] && c <= r[i + 1]) return true;
|
|
103
|
+
}
|
|
104
|
+
return false;
|
|
105
|
+
};
|
|
106
|
+
var complement = (r) => {
|
|
107
|
+
const pairs = [];
|
|
108
|
+
for (let i = 0; i < r.length; i += 2) pairs.push([r[i], r[i + 1]]);
|
|
109
|
+
pairs.sort((a, b) => a[0] - b[0]);
|
|
110
|
+
const out = [];
|
|
111
|
+
let next = 0;
|
|
112
|
+
for (const [lo, hi] of pairs) {
|
|
113
|
+
if (lo > next) out.push(next, lo - 1);
|
|
114
|
+
next = Math.max(next, hi + 1);
|
|
115
|
+
}
|
|
116
|
+
if (next <= MAX_CODE_POINT) out.push(next, MAX_CODE_POINT);
|
|
117
|
+
return out;
|
|
118
|
+
};
|
|
119
|
+
var isLineEnd = (c) => c === 10 || c === 13 || c === 8232 || c === 8233;
|
|
120
|
+
var isWordChar = (c) => c !== void 0 && inRanges(WORD, c);
|
|
121
|
+
var isHex = (c) => c !== void 0 && /^[0-9a-fA-F]$/.test(String.fromCodePoint(c));
|
|
122
|
+
var isDigit = (c) => c !== void 0 && c >= 48 && c <= 57;
|
|
123
|
+
var parsePattern = (pattern) => {
|
|
124
|
+
const src = Array.from(pattern, cp);
|
|
125
|
+
let i = 0;
|
|
126
|
+
const peek = (offset = 0) => src[i + offset];
|
|
127
|
+
const is = (ch, offset = 0) => src[i + offset] === cp(ch);
|
|
128
|
+
const fail = (message) => {
|
|
129
|
+
throw new Error(`${message} (at ${i})`);
|
|
130
|
+
};
|
|
131
|
+
const hex = (digits) => {
|
|
132
|
+
let value = 0;
|
|
133
|
+
for (let n = 0; n < digits; n++) {
|
|
134
|
+
if (!isHex(peek())) fail("a bad hexadecimal escape");
|
|
135
|
+
value = value * 16 + parseInt(String.fromCodePoint(src[i++]), 16);
|
|
136
|
+
}
|
|
137
|
+
return value;
|
|
138
|
+
};
|
|
139
|
+
const escape = (inClass) => {
|
|
140
|
+
const c = peek();
|
|
141
|
+
if (c === void 0) return fail("a pattern cannot end with a backslash");
|
|
142
|
+
i++;
|
|
143
|
+
const ch = String.fromCodePoint(c);
|
|
144
|
+
switch (ch) {
|
|
145
|
+
case "d":
|
|
146
|
+
return { r: DIGIT };
|
|
147
|
+
case "D":
|
|
148
|
+
return { r: complement(DIGIT) };
|
|
149
|
+
case "w":
|
|
150
|
+
return { r: WORD };
|
|
151
|
+
case "W":
|
|
152
|
+
return { r: complement(WORD) };
|
|
153
|
+
case "s":
|
|
154
|
+
return { r: SPACE };
|
|
155
|
+
case "S":
|
|
156
|
+
return { r: complement(SPACE) };
|
|
157
|
+
case "b":
|
|
158
|
+
return inClass ? { c: 8 } : { k: "word" };
|
|
159
|
+
case "B":
|
|
160
|
+
return inClass ? fail("\\B is not allowed in a class") : { k: "not-word" };
|
|
161
|
+
case "n":
|
|
162
|
+
return { c: 10 };
|
|
163
|
+
case "r":
|
|
164
|
+
return { c: 13 };
|
|
165
|
+
case "t":
|
|
166
|
+
return { c: 9 };
|
|
167
|
+
case "f":
|
|
168
|
+
return { c: 12 };
|
|
169
|
+
case "v":
|
|
170
|
+
return { c: 11 };
|
|
171
|
+
case "0":
|
|
172
|
+
return isDigit(peek()) ? fail("octal escapes are not supported") : { c: 0 };
|
|
173
|
+
case "x":
|
|
174
|
+
return { c: hex(2) };
|
|
175
|
+
case "u": {
|
|
176
|
+
if (!is("{")) return { c: hex(4) };
|
|
177
|
+
i++;
|
|
178
|
+
let value = 0;
|
|
179
|
+
let digits = 0;
|
|
180
|
+
while (isHex(peek())) {
|
|
181
|
+
value = value * 16 + parseInt(String.fromCodePoint(src[i++]), 16);
|
|
182
|
+
digits++;
|
|
183
|
+
if (value > MAX_CODE_POINT) fail("a code point out of range");
|
|
184
|
+
}
|
|
185
|
+
if (!digits || !is("}")) fail("a bad \\u{...} escape");
|
|
186
|
+
i++;
|
|
187
|
+
return { c: value };
|
|
188
|
+
}
|
|
189
|
+
case "k":
|
|
190
|
+
return fail("backreferences are not supported");
|
|
191
|
+
case "p":
|
|
192
|
+
case "P":
|
|
193
|
+
return fail("\\p{...} is not supported");
|
|
194
|
+
case "c":
|
|
195
|
+
return fail("\\c control escapes are not supported");
|
|
196
|
+
default:
|
|
197
|
+
if (isDigit(c)) return fail("backreferences are not supported");
|
|
198
|
+
if (/^[a-zA-Z]$/.test(ch)) return fail(`unknown escape \\${ch}`);
|
|
199
|
+
return { c };
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
const klass = () => {
|
|
203
|
+
const neg = is("^");
|
|
204
|
+
if (neg) i++;
|
|
205
|
+
const r = [];
|
|
206
|
+
for (; ; ) {
|
|
207
|
+
const c = peek();
|
|
208
|
+
if (c === void 0) return fail("an unterminated class");
|
|
209
|
+
if (is("]")) {
|
|
210
|
+
i++;
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
i++;
|
|
214
|
+
let lo;
|
|
215
|
+
if (c === cp("\\")) {
|
|
216
|
+
const e = escape(true);
|
|
217
|
+
if ("r" in e) {
|
|
218
|
+
r.push(...e.r);
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
lo = e.c;
|
|
222
|
+
} else {
|
|
223
|
+
lo = c;
|
|
224
|
+
}
|
|
225
|
+
if (is("-") && peek(1) !== void 0 && !is("]", 1)) {
|
|
226
|
+
i++;
|
|
227
|
+
let hi = src[i++];
|
|
228
|
+
if (hi === cp("\\")) {
|
|
229
|
+
const e = escape(true);
|
|
230
|
+
if (!("c" in e)) return fail("a bad range in a class");
|
|
231
|
+
hi = e.c;
|
|
232
|
+
}
|
|
233
|
+
if (hi < lo) fail("a range out of order in a class");
|
|
234
|
+
r.push(lo, hi);
|
|
235
|
+
} else {
|
|
236
|
+
r.push(lo, lo);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return { t: "set", r, neg };
|
|
240
|
+
};
|
|
241
|
+
const braces = () => {
|
|
242
|
+
let j = i + 1;
|
|
243
|
+
const number = () => {
|
|
244
|
+
const start = j;
|
|
245
|
+
while (isDigit(src[j])) j++;
|
|
246
|
+
return j > start ? Number(String.fromCodePoint(...src.slice(start, j))) : void 0;
|
|
247
|
+
};
|
|
248
|
+
const min = number();
|
|
249
|
+
if (min === void 0) return void 0;
|
|
250
|
+
let max = min;
|
|
251
|
+
if (src[j] === cp(",")) {
|
|
252
|
+
j++;
|
|
253
|
+
max = number() ?? Infinity;
|
|
254
|
+
}
|
|
255
|
+
if (src[j] !== cp("}")) return void 0;
|
|
256
|
+
i = j + 1;
|
|
257
|
+
if (max < min) fail("a quantifier out of order");
|
|
258
|
+
if (min > MAX_REPEAT || max !== Infinity && max > MAX_REPEAT) {
|
|
259
|
+
fail(`a repeat count above ${MAX_REPEAT}`);
|
|
260
|
+
}
|
|
261
|
+
return { min, max };
|
|
262
|
+
};
|
|
263
|
+
const atom = () => {
|
|
264
|
+
const c = src[i++];
|
|
265
|
+
switch (String.fromCodePoint(c)) {
|
|
266
|
+
case "(": {
|
|
267
|
+
if (is("?")) {
|
|
268
|
+
if (is(":", 1)) {
|
|
269
|
+
i += 2;
|
|
270
|
+
} else if (is("<", 1) && !is("=", 2) && !is("!", 2)) {
|
|
271
|
+
while (peek() !== void 0 && !is(">")) i++;
|
|
272
|
+
if (peek() === void 0) fail("an unterminated group name");
|
|
273
|
+
i++;
|
|
274
|
+
} else {
|
|
275
|
+
fail("lookaround is not supported");
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
const inner = alternation();
|
|
279
|
+
if (!is(")")) fail("an unterminated group");
|
|
280
|
+
i++;
|
|
281
|
+
return inner;
|
|
282
|
+
}
|
|
283
|
+
case "[":
|
|
284
|
+
return klass();
|
|
285
|
+
case ".":
|
|
286
|
+
return { t: "any" };
|
|
287
|
+
case "^":
|
|
288
|
+
return { t: "assert", k: "start" };
|
|
289
|
+
case "$":
|
|
290
|
+
return { t: "assert", k: "end" };
|
|
291
|
+
case "\\": {
|
|
292
|
+
const e = escape(false);
|
|
293
|
+
if ("k" in e) return { t: "assert", k: e.k };
|
|
294
|
+
return "r" in e ? { t: "set", r: e.r, neg: false } : { t: "char", c: e.c };
|
|
295
|
+
}
|
|
296
|
+
case "*":
|
|
297
|
+
case "+":
|
|
298
|
+
case "?":
|
|
299
|
+
return fail("nothing to repeat");
|
|
300
|
+
case "{":
|
|
301
|
+
i--;
|
|
302
|
+
if (braces()) fail("nothing to repeat");
|
|
303
|
+
i++;
|
|
304
|
+
return { t: "char", c };
|
|
305
|
+
default:
|
|
306
|
+
return { t: "char", c };
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
const repetition = () => {
|
|
310
|
+
let item = atom();
|
|
311
|
+
for (; ; ) {
|
|
312
|
+
let q;
|
|
313
|
+
if (is("*")) q = { min: 0, max: Infinity };
|
|
314
|
+
else if (is("+")) q = { min: 1, max: Infinity };
|
|
315
|
+
else if (is("?")) q = { min: 0, max: 1 };
|
|
316
|
+
if (q) i++;
|
|
317
|
+
else if (is("{")) q = braces();
|
|
318
|
+
if (!q) return item;
|
|
319
|
+
if (item.t === "assert") fail("nothing to repeat");
|
|
320
|
+
if (is("?")) i++;
|
|
321
|
+
item = { t: "rep", item, min: q.min, max: q.max };
|
|
322
|
+
}
|
|
323
|
+
};
|
|
324
|
+
const sequence = () => {
|
|
325
|
+
const items = [];
|
|
326
|
+
while (peek() !== void 0 && !is("|") && !is(")")) items.push(repetition());
|
|
327
|
+
return items.length === 1 ? items[0] : { t: "cat", items };
|
|
328
|
+
};
|
|
329
|
+
function alternation() {
|
|
330
|
+
const items = [sequence()];
|
|
331
|
+
while (is("|")) {
|
|
332
|
+
i++;
|
|
333
|
+
items.push(sequence());
|
|
334
|
+
}
|
|
335
|
+
return items.length === 1 ? items[0] : { t: "alt", items };
|
|
336
|
+
}
|
|
337
|
+
const ast = alternation();
|
|
338
|
+
if (i < src.length) fail("an unmatched )");
|
|
339
|
+
return ast;
|
|
340
|
+
};
|
|
341
|
+
var compileAst = (ast) => {
|
|
342
|
+
const prog = [];
|
|
343
|
+
const push = (inst) => {
|
|
344
|
+
if (prog.length >= PIPELINE_LIMITS.maxPatternProgram) {
|
|
345
|
+
throw new Error(
|
|
346
|
+
`the pattern is too complex (more than ${PIPELINE_LIMITS.maxPatternProgram} steps once its repeats are unrolled)`
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
prog.push(inst);
|
|
350
|
+
return prog.length - 1;
|
|
351
|
+
};
|
|
352
|
+
const emit = (node) => {
|
|
353
|
+
switch (node.t) {
|
|
354
|
+
case "char":
|
|
355
|
+
push({ op: "char", c: node.c });
|
|
356
|
+
return;
|
|
357
|
+
case "any":
|
|
358
|
+
push({ op: "any" });
|
|
359
|
+
return;
|
|
360
|
+
case "set":
|
|
361
|
+
push({ op: "set", r: node.r, neg: node.neg });
|
|
362
|
+
return;
|
|
363
|
+
case "assert":
|
|
364
|
+
push({ op: "assert", k: node.k });
|
|
365
|
+
return;
|
|
366
|
+
case "cat":
|
|
367
|
+
for (const item of node.items) emit(item);
|
|
368
|
+
return;
|
|
369
|
+
case "alt": {
|
|
370
|
+
const exits = [];
|
|
371
|
+
node.items.forEach((item, index) => {
|
|
372
|
+
if (index === node.items.length - 1) {
|
|
373
|
+
emit(item);
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
const split = { op: "split", x: 0, y: 0 };
|
|
377
|
+
split.x = push(split) + 1;
|
|
378
|
+
emit(item);
|
|
379
|
+
const exit = { op: "jmp", x: 0 };
|
|
380
|
+
push(exit);
|
|
381
|
+
exits.push(exit);
|
|
382
|
+
split.y = prog.length;
|
|
383
|
+
});
|
|
384
|
+
for (const exit of exits) exit.x = prog.length;
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
case "rep": {
|
|
388
|
+
for (let n = 0; n < node.min; n++) emit(node.item);
|
|
389
|
+
if (node.max === Infinity) {
|
|
390
|
+
const split = { op: "split", x: 0, y: 0 };
|
|
391
|
+
const at = push(split);
|
|
392
|
+
split.x = at + 1;
|
|
393
|
+
emit(node.item);
|
|
394
|
+
push({ op: "jmp", x: at });
|
|
395
|
+
split.y = prog.length;
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
const skips = [];
|
|
399
|
+
for (let n = node.min; n < node.max; n++) {
|
|
400
|
+
const split = { op: "split", x: 0, y: 0 };
|
|
401
|
+
split.x = push(split) + 1;
|
|
402
|
+
skips.push(split);
|
|
403
|
+
emit(node.item);
|
|
404
|
+
}
|
|
405
|
+
for (const split of skips) split.y = prog.length;
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
emit(ast);
|
|
411
|
+
push({ op: "match" });
|
|
412
|
+
return prog;
|
|
413
|
+
};
|
|
414
|
+
var ALLOWED_FLAGS = "imsu";
|
|
415
|
+
var compileMatcher = (pattern, flags = "") => {
|
|
416
|
+
if (typeof pattern !== "string" || pattern.length === 0) {
|
|
417
|
+
throw new Error("the pattern is empty");
|
|
418
|
+
}
|
|
419
|
+
if (pattern.length > PIPELINE_LIMITS.maxPatternLength) {
|
|
420
|
+
throw new Error(`the pattern is longer than ${PIPELINE_LIMITS.maxPatternLength} characters`);
|
|
421
|
+
}
|
|
422
|
+
if (typeof flags !== "string") throw new Error("flags must be a string");
|
|
423
|
+
for (const flag of flags) {
|
|
424
|
+
if (!ALLOWED_FLAGS.includes(flag)) {
|
|
425
|
+
throw new Error(`flag "${flag}" is not allowed (allowed: ${ALLOWED_FLAGS})`);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
if (new Set(flags).size !== flags.length) throw new Error("a flag is repeated");
|
|
429
|
+
try {
|
|
430
|
+
new RegExp(pattern, flags);
|
|
431
|
+
} catch (err) {
|
|
432
|
+
throw new Error(err instanceof Error ? err.message : String(err));
|
|
433
|
+
}
|
|
434
|
+
const prog = compileAst(parsePattern(pattern));
|
|
435
|
+
const ignoreCase = flags.includes("i");
|
|
436
|
+
const multiline = flags.includes("m");
|
|
437
|
+
const dotAll = flags.includes("s");
|
|
438
|
+
const single = (c, mapped) => mapped.length === String.fromCodePoint(cp(mapped)).length ? cp(mapped) : c;
|
|
439
|
+
const lower = (c) => single(c, String.fromCodePoint(c).toLowerCase());
|
|
440
|
+
const upper = (c) => single(c, String.fromCodePoint(c).toUpperCase());
|
|
441
|
+
return (text) => {
|
|
442
|
+
const input = Array.from(text.slice(0, PIPELINE_LIMITS.conditionInputChars), cp);
|
|
443
|
+
const n = input.length;
|
|
444
|
+
const mark = new Int32Array(prog.length);
|
|
445
|
+
let generation = 0;
|
|
446
|
+
let matched = false;
|
|
447
|
+
const stack = [];
|
|
448
|
+
const holds = (k, pos) => {
|
|
449
|
+
switch (k) {
|
|
450
|
+
case "start":
|
|
451
|
+
return pos === 0 || multiline && isLineEnd(input[pos - 1]);
|
|
452
|
+
case "end":
|
|
453
|
+
return pos === n || multiline && isLineEnd(input[pos]);
|
|
454
|
+
case "word":
|
|
455
|
+
return isWordChar(input[pos - 1]) !== isWordChar(input[pos]);
|
|
456
|
+
case "not-word":
|
|
457
|
+
return isWordChar(input[pos - 1]) === isWordChar(input[pos]);
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
const add = (list, start, pos) => {
|
|
461
|
+
stack.push(start);
|
|
462
|
+
while (stack.length) {
|
|
463
|
+
const pc = stack.pop();
|
|
464
|
+
if (mark[pc] === generation) continue;
|
|
465
|
+
mark[pc] = generation;
|
|
466
|
+
const inst = prog[pc];
|
|
467
|
+
switch (inst.op) {
|
|
468
|
+
case "jmp":
|
|
469
|
+
stack.push(inst.x);
|
|
470
|
+
break;
|
|
471
|
+
case "split":
|
|
472
|
+
stack.push(inst.y, inst.x);
|
|
473
|
+
break;
|
|
474
|
+
case "assert":
|
|
475
|
+
if (holds(inst.k, pos)) stack.push(pc + 1);
|
|
476
|
+
break;
|
|
477
|
+
case "match":
|
|
478
|
+
matched = true;
|
|
479
|
+
break;
|
|
480
|
+
default:
|
|
481
|
+
list.push(pc);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
};
|
|
485
|
+
let current = [];
|
|
486
|
+
let next = [];
|
|
487
|
+
generation++;
|
|
488
|
+
add(current, 0, 0);
|
|
489
|
+
for (let pos = 0; pos < n && !matched; pos++) {
|
|
490
|
+
const c = input[pos];
|
|
491
|
+
const lo = ignoreCase ? lower(c) : c;
|
|
492
|
+
const up = ignoreCase ? upper(c) : c;
|
|
493
|
+
generation++;
|
|
494
|
+
for (const pc of current) {
|
|
495
|
+
const inst = prog[pc];
|
|
496
|
+
let ok = false;
|
|
497
|
+
if (inst.op === "char") {
|
|
498
|
+
ok = inst.c === c || ignoreCase && (inst.c === lo || inst.c === up || lower(inst.c) === lo);
|
|
499
|
+
} else if (inst.op === "any") {
|
|
500
|
+
ok = dotAll || !isLineEnd(c);
|
|
501
|
+
} else if (inst.op === "set") {
|
|
502
|
+
const inside = inRanges(inst.r, c) || ignoreCase && (inRanges(inst.r, lo) || inRanges(inst.r, up));
|
|
503
|
+
ok = inside !== inst.neg;
|
|
504
|
+
}
|
|
505
|
+
if (ok) add(next, pc + 1, pos + 1);
|
|
506
|
+
}
|
|
507
|
+
add(next, 0, pos + 1);
|
|
508
|
+
current = next;
|
|
509
|
+
next = [];
|
|
510
|
+
}
|
|
511
|
+
return matched;
|
|
512
|
+
};
|
|
513
|
+
};
|
|
514
|
+
var CONDITION_KINDS = ["always", "contains", "matches", "route", "otherwise"];
|
|
515
|
+
var testPipelineCondition = (when, output, matcher) => {
|
|
516
|
+
if (!when || when.kind === "always") return true;
|
|
517
|
+
switch (when.kind) {
|
|
518
|
+
case "contains": {
|
|
519
|
+
const text = output.text.slice(0, PIPELINE_LIMITS.conditionInputChars);
|
|
520
|
+
return when.caseSensitive ? text.includes(when.text) : text.toLowerCase().includes(when.text.toLowerCase());
|
|
521
|
+
}
|
|
522
|
+
case "matches":
|
|
523
|
+
return (matcher ?? compileMatcher(when.pattern, when.flags))(output.text);
|
|
524
|
+
case "route":
|
|
525
|
+
return output.route !== void 0 && output.route === when.label;
|
|
526
|
+
default:
|
|
527
|
+
return false;
|
|
528
|
+
}
|
|
529
|
+
};
|
|
530
|
+
var isRecord = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
531
|
+
var isId = (v) => typeof v === "string" && v.trim().length > 0;
|
|
532
|
+
var edgeIdOf = (edge, index) => isRecord(edge) && isId(edge.id) ? edge.id : `e${index}`;
|
|
533
|
+
var edgeRef = (edge, index) => ({
|
|
534
|
+
edgeId: edgeIdOf(edge, index),
|
|
535
|
+
edgeIndex: index
|
|
536
|
+
});
|
|
537
|
+
var normalizePipeline = (definition) => {
|
|
538
|
+
const def = JSON.parse(JSON.stringify(definition));
|
|
539
|
+
if (isRecord(def) && Array.isArray(def.edges)) {
|
|
540
|
+
def.edges.forEach((edge, index) => {
|
|
541
|
+
if (isRecord(edge)) edge.id = edgeIdOf(edge, index);
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
return def;
|
|
545
|
+
};
|
|
546
|
+
var inRange = (v, max) => typeof v === "number" && Number.isInteger(v) && v >= 1 && v <= max;
|
|
547
|
+
var validatePipeline = (definition) => {
|
|
548
|
+
const errors = [];
|
|
549
|
+
const warnings = [];
|
|
550
|
+
const error = (code, message, at = {}) => {
|
|
551
|
+
errors.push({ code, message, ...at });
|
|
552
|
+
};
|
|
553
|
+
const done = () => ({ ok: errors.length === 0, errors, warnings });
|
|
554
|
+
const def = definition;
|
|
555
|
+
if (!isRecord(def)) {
|
|
556
|
+
error("definition", "a pipeline definition must be an object");
|
|
557
|
+
return done();
|
|
558
|
+
}
|
|
559
|
+
if (def.version !== 1) {
|
|
560
|
+
error("version", `unsupported pipeline version ${JSON.stringify(def.version)} (expected 1)`);
|
|
561
|
+
}
|
|
562
|
+
const nodes = Array.isArray(def.nodes) ? def.nodes : void 0;
|
|
563
|
+
const edges = Array.isArray(def.edges) ? def.edges : void 0;
|
|
564
|
+
if (!nodes || nodes.length === 0) error("nodes", "a pipeline needs at least one node");
|
|
565
|
+
if (!edges) error("edges", "`edges` must be an array");
|
|
566
|
+
if (nodes && nodes.length > PIPELINE_LIMITS.maxNodes) {
|
|
567
|
+
error("nodes", `more than ${PIPELINE_LIMITS.maxNodes} nodes`);
|
|
568
|
+
}
|
|
569
|
+
if (edges && edges.length > PIPELINE_LIMITS.maxEdges) {
|
|
570
|
+
error("edges", `more than ${PIPELINE_LIMITS.maxEdges} edges`);
|
|
571
|
+
}
|
|
572
|
+
const ids = /* @__PURE__ */ new Set();
|
|
573
|
+
const routesOf = /* @__PURE__ */ new Map();
|
|
574
|
+
for (const [index, node] of (nodes ?? []).entries()) {
|
|
575
|
+
if (!isRecord(node) || !isId(node.id)) {
|
|
576
|
+
error("node.id", `node #${index + 1} has no id`);
|
|
577
|
+
continue;
|
|
578
|
+
}
|
|
579
|
+
const nodeId = node.id;
|
|
580
|
+
if (ids.has(nodeId)) {
|
|
581
|
+
error("node.duplicate", `duplicate node id "${nodeId}"`, { nodeId });
|
|
582
|
+
continue;
|
|
583
|
+
}
|
|
584
|
+
ids.add(nodeId);
|
|
585
|
+
if (node.join !== void 0 && node.join !== "all" && node.join !== "any") {
|
|
586
|
+
error("node.join", `node "${nodeId}": join must be "all" or "any"`, { nodeId });
|
|
587
|
+
}
|
|
588
|
+
const labels = [];
|
|
589
|
+
if (node.routes !== void 0) {
|
|
590
|
+
if (!Array.isArray(node.routes) || node.routes.length > PIPELINE_LIMITS.maxRoutes) {
|
|
591
|
+
error(
|
|
592
|
+
"node.routes",
|
|
593
|
+
`node "${nodeId}": routes must be a list of at most ${PIPELINE_LIMITS.maxRoutes}`,
|
|
594
|
+
{ nodeId }
|
|
595
|
+
);
|
|
596
|
+
} else {
|
|
597
|
+
for (const route of node.routes) {
|
|
598
|
+
if (!isRecord(route) || !isId(route.label)) {
|
|
599
|
+
error("node.routes", `node "${nodeId}": a route has no label`, { nodeId });
|
|
600
|
+
} else if (labels.includes(route.label)) {
|
|
601
|
+
error("node.routes", `node "${nodeId}": duplicate route "${route.label}"`, { nodeId });
|
|
602
|
+
} else {
|
|
603
|
+
labels.push(route.label);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
routesOf.set(nodeId, labels);
|
|
609
|
+
}
|
|
610
|
+
if (!isId(def.entry)) {
|
|
611
|
+
error("entry", "the pipeline has no entry node");
|
|
612
|
+
} else if (!ids.has(def.entry)) {
|
|
613
|
+
error("entry", `the entry node "${def.entry}" does not exist`);
|
|
614
|
+
}
|
|
615
|
+
const out = /* @__PURE__ */ new Map();
|
|
616
|
+
const edgeIds = /* @__PURE__ */ new Set();
|
|
617
|
+
const otherwise = /* @__PURE__ */ new Set();
|
|
618
|
+
const routed = /* @__PURE__ */ new Set();
|
|
619
|
+
for (const [index, edge] of (edges ?? []).entries()) {
|
|
620
|
+
const at = edgeRef(edge, index);
|
|
621
|
+
const label = `edge "${at.edgeId}"`;
|
|
622
|
+
if (!isRecord(edge)) {
|
|
623
|
+
error("edge", `${label} is not an object`, at);
|
|
624
|
+
continue;
|
|
625
|
+
}
|
|
626
|
+
if (edge.id !== void 0 && !isId(edge.id)) error("edge.id", `${label} has an empty id`, at);
|
|
627
|
+
if (edgeIds.has(at.edgeId)) error("edge.duplicate", `duplicate edge id "${at.edgeId}"`, at);
|
|
628
|
+
edgeIds.add(at.edgeId);
|
|
629
|
+
const from = edge.from;
|
|
630
|
+
const to = edge.to;
|
|
631
|
+
const fromOk = isId(from) && ids.has(from);
|
|
632
|
+
const toOk = isId(to) && ids.has(to);
|
|
633
|
+
if (!fromOk)
|
|
634
|
+
error("edge.from", `${label} starts at an unknown node ${JSON.stringify(from)}`, at);
|
|
635
|
+
if (!toOk) error("edge.to", `${label} leads to an unknown node ${JSON.stringify(to)}`, at);
|
|
636
|
+
if (fromOk && toOk) out.set(from, [...out.get(from) ?? [], to]);
|
|
637
|
+
const when = edge.when;
|
|
638
|
+
if (when === void 0) continue;
|
|
639
|
+
if (!isRecord(when) || !CONDITION_KINDS.includes(when.kind)) {
|
|
640
|
+
error("edge.when", `${label} has an unknown condition`, at);
|
|
641
|
+
continue;
|
|
642
|
+
}
|
|
643
|
+
switch (when.kind) {
|
|
644
|
+
case "contains":
|
|
645
|
+
if (typeof when.text !== "string" || when.text.length === 0) {
|
|
646
|
+
error("edge.when", `${label}: "contains" needs a text`, at);
|
|
647
|
+
}
|
|
648
|
+
break;
|
|
649
|
+
case "matches":
|
|
650
|
+
try {
|
|
651
|
+
compileMatcher(when.pattern, when.flags ?? "");
|
|
652
|
+
} catch (err) {
|
|
653
|
+
error("edge.pattern", `${label}: ${err.message}`, at);
|
|
654
|
+
}
|
|
655
|
+
break;
|
|
656
|
+
case "route":
|
|
657
|
+
if (fromOk) routed.add(from);
|
|
658
|
+
if (!isId(when.label)) {
|
|
659
|
+
error("edge.route", `${label}: "route" needs a label`, at);
|
|
660
|
+
} else if (fromOk && !(routesOf.get(from) ?? []).includes(when.label)) {
|
|
661
|
+
error(
|
|
662
|
+
"edge.route",
|
|
663
|
+
`${label}: node "${from}" has no route "${when.label}"`,
|
|
664
|
+
fromOk ? { ...at, nodeId: from } : at
|
|
665
|
+
);
|
|
666
|
+
}
|
|
667
|
+
break;
|
|
668
|
+
case "otherwise":
|
|
669
|
+
if (fromOk) {
|
|
670
|
+
if (otherwise.has(from)) {
|
|
671
|
+
error("edge.otherwise", `node "${from}" has more than one "otherwise" edge`, {
|
|
672
|
+
...at,
|
|
673
|
+
nodeId: from
|
|
674
|
+
});
|
|
675
|
+
}
|
|
676
|
+
otherwise.add(from);
|
|
677
|
+
}
|
|
678
|
+
break;
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
if (def.limits !== void 0) {
|
|
682
|
+
if (!isRecord(def.limits)) {
|
|
683
|
+
error("limits", "`limits` must be an object");
|
|
684
|
+
} else {
|
|
685
|
+
const { maxNodeVisits, maxRuns } = def.limits;
|
|
686
|
+
if (maxNodeVisits !== void 0 && !inRange(maxNodeVisits, PIPELINE_LIMITS.maxNodeVisitsCeiling)) {
|
|
687
|
+
error(
|
|
688
|
+
"limits",
|
|
689
|
+
`limits.maxNodeVisits must be a whole number from 1 to ${PIPELINE_LIMITS.maxNodeVisitsCeiling}`
|
|
690
|
+
);
|
|
691
|
+
}
|
|
692
|
+
if (maxRuns !== void 0 && !inRange(maxRuns, PIPELINE_LIMITS.maxRunsCeiling)) {
|
|
693
|
+
error(
|
|
694
|
+
"limits",
|
|
695
|
+
`limits.maxRuns must be a whole number from 1 to ${PIPELINE_LIMITS.maxRunsCeiling}`
|
|
696
|
+
);
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
if (errors.length > 0 || !isId(def.entry)) return done();
|
|
701
|
+
const reached = /* @__PURE__ */ new Set([def.entry]);
|
|
702
|
+
const queue = [def.entry];
|
|
703
|
+
while (queue.length) {
|
|
704
|
+
for (const to of out.get(queue.pop()) ?? []) {
|
|
705
|
+
if (!reached.has(to)) {
|
|
706
|
+
reached.add(to);
|
|
707
|
+
queue.push(to);
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
for (const nodeId of ids) {
|
|
712
|
+
if (!reached.has(nodeId)) {
|
|
713
|
+
warnings.push({
|
|
714
|
+
code: "unreachable",
|
|
715
|
+
message: `node "${nodeId}" cannot be reached from the entry node`,
|
|
716
|
+
nodeId
|
|
717
|
+
});
|
|
718
|
+
}
|
|
719
|
+
if ((routesOf.get(nodeId) ?? []).length > 0 && !routed.has(nodeId)) {
|
|
720
|
+
warnings.push({
|
|
721
|
+
code: "routes.unused",
|
|
722
|
+
message: `node "${nodeId}" has routes but no edge with a "route" condition`,
|
|
723
|
+
nodeId
|
|
724
|
+
});
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
const state = /* @__PURE__ */ new Map();
|
|
728
|
+
const path = [];
|
|
729
|
+
const findCycle = (nodeId) => {
|
|
730
|
+
state.set(nodeId, "open");
|
|
731
|
+
path.push(nodeId);
|
|
732
|
+
for (const to of out.get(nodeId) ?? []) {
|
|
733
|
+
if (state.get(to) === "open") return [...path.slice(path.indexOf(to)), to];
|
|
734
|
+
if (!state.has(to)) {
|
|
735
|
+
const found = findCycle(to);
|
|
736
|
+
if (found) return found;
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
path.pop();
|
|
740
|
+
state.set(nodeId, "closed");
|
|
741
|
+
return void 0;
|
|
742
|
+
};
|
|
743
|
+
const cycle = findCycle(def.entry);
|
|
744
|
+
if (cycle) {
|
|
745
|
+
warnings.push({
|
|
746
|
+
code: "cycle",
|
|
747
|
+
message: `the pipeline has a loop (${cycle.join(" \u2192 ")}); limits.maxNodeVisits and limits.maxRuns bound it`,
|
|
748
|
+
nodeId: cycle[0]
|
|
749
|
+
});
|
|
750
|
+
}
|
|
751
|
+
return done();
|
|
752
|
+
};
|
|
753
|
+
var titleOf = (source) => source.name || source.nodeId;
|
|
754
|
+
var renderPipelineNodeInput = (input) => {
|
|
755
|
+
if (input.from.length === 0 && input.visit <= 1) return input.request;
|
|
756
|
+
const parts = [`Request:
|
|
757
|
+
${input.request}`];
|
|
758
|
+
for (const source of input.from) {
|
|
759
|
+
parts.push(`Output of "${titleOf(source)}":
|
|
760
|
+
${source.text}`);
|
|
761
|
+
}
|
|
762
|
+
if (input.visit > 1) parts.push(`(This is visit ${input.visit} of this step.)`);
|
|
763
|
+
return parts.join("\n\n");
|
|
764
|
+
};
|
|
765
|
+
var PIPELINE_ROUTER_SYSTEM = "You are the Router of one step in a pipeline of agents. The step has answered. Pick the ONE route label that fits its answer, from the list you are given. Answer with a label exactly as it is written in the list, and nothing else.";
|
|
766
|
+
var ROUTER_HEAD_CHARS = 2e3;
|
|
767
|
+
var ROUTER_TAIL_CHARS = 6e3;
|
|
768
|
+
var renderPipelineRoutePrompt = (node, text) => {
|
|
769
|
+
const routes = (node.routes ?? []).map((r) => `- ${r.label}${r.description ? `: ${r.description}` : ""}`).join("\n");
|
|
770
|
+
const answer = text.length > ROUTER_HEAD_CHARS + ROUTER_TAIL_CHARS ? `${text.slice(0, ROUTER_HEAD_CHARS)}
|
|
771
|
+
[...]
|
|
772
|
+
${text.slice(-ROUTER_TAIL_CHARS)}` : text;
|
|
773
|
+
return [
|
|
774
|
+
`Step: ${titleOf({ nodeId: node.id, name: node.name })}`,
|
|
775
|
+
node.instructions ? `Its role:
|
|
776
|
+
${node.instructions}` : "",
|
|
777
|
+
`Routes:
|
|
778
|
+
${routes}`,
|
|
779
|
+
`The step's answer:
|
|
780
|
+
${answer}`,
|
|
781
|
+
"Which route? Answer with one label from the list."
|
|
782
|
+
].filter(Boolean).join("\n\n");
|
|
783
|
+
};
|
|
784
|
+
var matchPipelineRoute = (node, answer) => {
|
|
785
|
+
const labels = (node.routes ?? []).map((r) => r.label);
|
|
786
|
+
let value = answer;
|
|
787
|
+
if (typeof value === "string") {
|
|
788
|
+
const text = value.trim();
|
|
789
|
+
if (text.startsWith("{")) {
|
|
790
|
+
try {
|
|
791
|
+
value = JSON.parse(text);
|
|
792
|
+
} catch {
|
|
793
|
+
value = text;
|
|
794
|
+
}
|
|
795
|
+
} else {
|
|
796
|
+
value = text;
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
if (isRecord(value)) value = value.route;
|
|
800
|
+
if (typeof value !== "string") return void 0;
|
|
801
|
+
const wanted = value.trim().replace(/^["'`]+|["'`.]+$/g, "").toLowerCase();
|
|
802
|
+
return labels.find((label) => label.toLowerCase() === wanted);
|
|
803
|
+
};
|
|
804
|
+
var preparePipeline = (definition) => {
|
|
805
|
+
const { errors } = validatePipeline(definition);
|
|
806
|
+
if (errors.length > 0) throw new PipelineValidationError(errors);
|
|
807
|
+
const def = normalizePipeline(definition);
|
|
808
|
+
const nodes = new Map(def.nodes.map((n) => [n.id, n]));
|
|
809
|
+
const outgoing = new Map(def.nodes.map((n) => [n.id, []]));
|
|
810
|
+
for (const edge of def.edges) {
|
|
811
|
+
const planned = { edge };
|
|
812
|
+
if (edge.when?.kind === "matches") {
|
|
813
|
+
planned.matcher = compileMatcher(edge.when.pattern, edge.when.flags);
|
|
814
|
+
}
|
|
815
|
+
outgoing.get(edge.from)?.push(planned);
|
|
816
|
+
}
|
|
817
|
+
const closure = /* @__PURE__ */ new Map();
|
|
818
|
+
const descendants = (from) => {
|
|
819
|
+
let seen = closure.get(from);
|
|
820
|
+
if (!seen) {
|
|
821
|
+
seen = /* @__PURE__ */ new Set();
|
|
822
|
+
const queue = [from];
|
|
823
|
+
while (queue.length) {
|
|
824
|
+
for (const { edge } of outgoing.get(queue.pop()) ?? []) {
|
|
825
|
+
if (!seen.has(edge.to)) {
|
|
826
|
+
seen.add(edge.to);
|
|
827
|
+
queue.push(edge.to);
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
closure.set(from, seen);
|
|
832
|
+
}
|
|
833
|
+
return seen;
|
|
834
|
+
};
|
|
835
|
+
return {
|
|
836
|
+
definition: def,
|
|
837
|
+
nodes,
|
|
838
|
+
outgoing,
|
|
839
|
+
maxNodeVisits: def.limits?.maxNodeVisits ?? PIPELINE_LIMITS.maxNodeVisits,
|
|
840
|
+
maxRuns: def.limits?.maxRuns ?? PIPELINE_LIMITS.maxRuns,
|
|
841
|
+
reaches: (from, to) => descendants(from).has(to)
|
|
842
|
+
};
|
|
843
|
+
};
|
|
844
|
+
var passReach = (plan) => {
|
|
845
|
+
const entry = plan.definition.entry;
|
|
846
|
+
const order = [entry];
|
|
847
|
+
const preds = /* @__PURE__ */ new Map([[entry, []]]);
|
|
848
|
+
for (let i = 0; i < order.length; i++) {
|
|
849
|
+
for (const { edge } of plan.outgoing.get(order[i]) ?? []) {
|
|
850
|
+
if (!preds.has(edge.to)) {
|
|
851
|
+
preds.set(edge.to, []);
|
|
852
|
+
order.push(edge.to);
|
|
853
|
+
}
|
|
854
|
+
;
|
|
855
|
+
preds.get(edge.to).push(order[i]);
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
const dom = /* @__PURE__ */ new Map();
|
|
859
|
+
for (const nodeId of order) dom.set(nodeId, new Set(nodeId === entry ? [entry] : order));
|
|
860
|
+
for (let changed = true; changed; ) {
|
|
861
|
+
changed = false;
|
|
862
|
+
for (const nodeId of order) {
|
|
863
|
+
if (nodeId === entry) continue;
|
|
864
|
+
const [first, ...rest] = preds.get(nodeId).map((p) => dom.get(p));
|
|
865
|
+
const next = /* @__PURE__ */ new Set([nodeId]);
|
|
866
|
+
for (const d of first) if (rest.every((set) => set.has(d))) next.add(d);
|
|
867
|
+
if (next.size < dom.get(nodeId).size) {
|
|
868
|
+
dom.set(nodeId, next);
|
|
869
|
+
changed = true;
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
const closure = /* @__PURE__ */ new Map();
|
|
874
|
+
return (from, to) => {
|
|
875
|
+
let seen = closure.get(from);
|
|
876
|
+
if (!seen) {
|
|
877
|
+
const found = /* @__PURE__ */ new Set();
|
|
878
|
+
const queue = [from];
|
|
879
|
+
while (queue.length) {
|
|
880
|
+
const at = queue.pop();
|
|
881
|
+
for (const { edge } of plan.outgoing.get(at) ?? []) {
|
|
882
|
+
if (dom.get(at)?.has(edge.to) || found.has(edge.to)) continue;
|
|
883
|
+
found.add(edge.to);
|
|
884
|
+
queue.push(edge.to);
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
closure.set(from, seen = found);
|
|
888
|
+
}
|
|
889
|
+
return seen.has(to);
|
|
890
|
+
};
|
|
891
|
+
};
|
|
892
|
+
var abortError = () => {
|
|
893
|
+
if (typeof DOMException === "function") return new DOMException("Aborted", "AbortError");
|
|
894
|
+
const error = new Error("Aborted");
|
|
895
|
+
error.name = "AbortError";
|
|
896
|
+
return error;
|
|
897
|
+
};
|
|
898
|
+
var createStopper = () => {
|
|
899
|
+
if (typeof AbortController === "function") {
|
|
900
|
+
const controller = new AbortController();
|
|
901
|
+
return { signal: controller.signal, abort: () => controller.abort() };
|
|
902
|
+
}
|
|
903
|
+
const listeners = [];
|
|
904
|
+
const signal = {
|
|
905
|
+
aborted: false,
|
|
906
|
+
reason: void 0,
|
|
907
|
+
onabort: null,
|
|
908
|
+
addEventListener(type, listener) {
|
|
909
|
+
if (type === "abort" && listener && !listeners.includes(listener)) listeners.push(listener);
|
|
910
|
+
},
|
|
911
|
+
removeEventListener(type, listener) {
|
|
912
|
+
const index = listener ? listeners.indexOf(listener) : -1;
|
|
913
|
+
if (type === "abort" && index >= 0) listeners.splice(index, 1);
|
|
914
|
+
},
|
|
915
|
+
throwIfAborted() {
|
|
916
|
+
if (signal.aborted) throw signal.reason;
|
|
917
|
+
}
|
|
918
|
+
};
|
|
919
|
+
const abort = () => {
|
|
920
|
+
if (signal.aborted) return;
|
|
921
|
+
signal.aborted = true;
|
|
922
|
+
signal.reason = abortError();
|
|
923
|
+
const event = { type: "abort", target: signal };
|
|
924
|
+
const call = (fn, thisArg) => {
|
|
925
|
+
try {
|
|
926
|
+
fn.call(thisArg, event);
|
|
927
|
+
} catch {
|
|
928
|
+
}
|
|
929
|
+
};
|
|
930
|
+
for (const listener of listeners.splice(0)) {
|
|
931
|
+
if (typeof listener === "function") call(listener, signal);
|
|
932
|
+
else call(listener.handleEvent, listener);
|
|
933
|
+
}
|
|
934
|
+
if (typeof signal.onabort === "function") call(signal.onabort, signal);
|
|
935
|
+
};
|
|
936
|
+
return { signal, abort };
|
|
937
|
+
};
|
|
938
|
+
var addUsage = (into, usage) => {
|
|
939
|
+
if (!usage) return;
|
|
940
|
+
const sum = into;
|
|
941
|
+
for (const [key, value] of Object.entries(usage)) {
|
|
942
|
+
if (typeof value === "number" && Number.isFinite(value)) sum[key] = (sum[key] ?? 0) + value;
|
|
943
|
+
}
|
|
944
|
+
};
|
|
945
|
+
var runPipelineGraph = async (plan, runNode, options) => {
|
|
946
|
+
const { definition } = plan;
|
|
947
|
+
const onAbort = options.onAbort ?? "reject";
|
|
948
|
+
const stopper = createStopper();
|
|
949
|
+
let halted;
|
|
950
|
+
let halt = () => {
|
|
951
|
+
};
|
|
952
|
+
const haltPromise = new Promise((_, reject) => {
|
|
953
|
+
halt = reject;
|
|
954
|
+
});
|
|
955
|
+
haltPromise.catch(() => {
|
|
956
|
+
});
|
|
957
|
+
const emit = (event) => {
|
|
958
|
+
try {
|
|
959
|
+
options.onEvent?.(event);
|
|
960
|
+
} catch {
|
|
961
|
+
}
|
|
962
|
+
};
|
|
963
|
+
const visits = /* @__PURE__ */ new Map();
|
|
964
|
+
const usage = { inputTokens: 0, outputTokens: 0, totalTokens: 0 };
|
|
965
|
+
const outputs = {};
|
|
966
|
+
const trace = [];
|
|
967
|
+
const terminals = [];
|
|
968
|
+
const capped = /* @__PURE__ */ new Set();
|
|
969
|
+
let runs = 0;
|
|
970
|
+
let wave = 0;
|
|
971
|
+
const pending = /* @__PURE__ */ new Map([[definition.entry, []]]);
|
|
972
|
+
const result = (stoppedBy2) => {
|
|
973
|
+
const lastWave = Math.max(-1, ...terminals.map((t) => t.wave));
|
|
974
|
+
const last = terminals.filter((t) => t.wave === lastWave);
|
|
975
|
+
const text = last.length === 1 ? last[0].text : last.map((t) => `## ${titleOf(t)}
|
|
976
|
+
|
|
977
|
+
${t.text}`).join("\n\n");
|
|
978
|
+
return { text, outputs, trace, usage, ...stoppedBy2 ? { stoppedBy: stoppedBy2 } : {}, runs };
|
|
979
|
+
};
|
|
980
|
+
const onOuterAbort = () => {
|
|
981
|
+
if (halted) return;
|
|
982
|
+
halted = "aborted";
|
|
983
|
+
stopper.abort();
|
|
984
|
+
halt(abortError());
|
|
985
|
+
};
|
|
986
|
+
const visit = async (nodeId, activations) => {
|
|
987
|
+
const node = plan.nodes.get(nodeId);
|
|
988
|
+
const number = (visits.get(nodeId) ?? 0) + 1;
|
|
989
|
+
visits.set(nodeId, number);
|
|
990
|
+
runs++;
|
|
991
|
+
const from = activations.map((a) => a.source.nodeId);
|
|
992
|
+
emit({ type: "pipeline.node.started", nodeId, visit: number, from });
|
|
993
|
+
let output;
|
|
994
|
+
try {
|
|
995
|
+
output = await runNode(
|
|
996
|
+
node,
|
|
997
|
+
{ request: options.input, from: activations.map((a) => ({ ...a.source })), visit: number },
|
|
998
|
+
{
|
|
999
|
+
signal: stopper.signal,
|
|
1000
|
+
pipeline: definition,
|
|
1001
|
+
emit: (event) => {
|
|
1002
|
+
if (!halted) emit({ ...event, nodeId, visit: number });
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
);
|
|
1006
|
+
} catch (err) {
|
|
1007
|
+
if (halted) return;
|
|
1008
|
+
halted = "failed";
|
|
1009
|
+
const failure = new PipelineNodeError(nodeId, number, err);
|
|
1010
|
+
emit({
|
|
1011
|
+
type: "pipeline.node.failed",
|
|
1012
|
+
nodeId,
|
|
1013
|
+
visit: number,
|
|
1014
|
+
error: err instanceof Error ? err.message : String(err)
|
|
1015
|
+
});
|
|
1016
|
+
stopper.abort();
|
|
1017
|
+
halt(failure);
|
|
1018
|
+
return;
|
|
1019
|
+
}
|
|
1020
|
+
if (halted) return;
|
|
1021
|
+
const text = typeof output?.text === "string" ? output.text : "";
|
|
1022
|
+
const route = (node.routes ?? []).some((r) => r.label === output?.route) ? output.route : void 0;
|
|
1023
|
+
addUsage(usage, output?.usage);
|
|
1024
|
+
const optional = {
|
|
1025
|
+
...route !== void 0 ? { route } : {},
|
|
1026
|
+
...output?.usage ? { usage: output.usage } : {}
|
|
1027
|
+
};
|
|
1028
|
+
(outputs[nodeId] ??= []).push({
|
|
1029
|
+
visit: number,
|
|
1030
|
+
text,
|
|
1031
|
+
...route !== void 0 ? { route } : {}
|
|
1032
|
+
});
|
|
1033
|
+
trace.push({ type: "node", nodeId, visit: number, wave, from, text, ...optional });
|
|
1034
|
+
emit({ type: "pipeline.node.finished", nodeId, visit: number, text, ...optional });
|
|
1035
|
+
const edges = plan.outgoing.get(nodeId) ?? [];
|
|
1036
|
+
const fired = edges.map(
|
|
1037
|
+
({ edge, matcher }) => testPipelineCondition(edge.when, { text, route }, matcher)
|
|
1038
|
+
);
|
|
1039
|
+
const anyFired = fired.some(Boolean);
|
|
1040
|
+
const record = { nodeId, name: node.name, text, wave, live: 0 };
|
|
1041
|
+
const targets = /* @__PURE__ */ new Set();
|
|
1042
|
+
edges.forEach(({ edge }, index) => {
|
|
1043
|
+
const fires = edge.when?.kind === "otherwise" ? !anyFired : fired[index];
|
|
1044
|
+
const decision = {
|
|
1045
|
+
from: nodeId,
|
|
1046
|
+
to: edge.to,
|
|
1047
|
+
edgeId: edge.id,
|
|
1048
|
+
visit: number,
|
|
1049
|
+
fired: fires
|
|
1050
|
+
};
|
|
1051
|
+
trace.push({ type: "edge", ...decision });
|
|
1052
|
+
emit({ type: "pipeline.edge", ...decision });
|
|
1053
|
+
if (!fires || targets.has(edge.to)) return;
|
|
1054
|
+
targets.add(edge.to);
|
|
1055
|
+
const waiting = pending.get(edge.to) ?? [];
|
|
1056
|
+
waiting.push({ source: { nodeId, ...node.name ? { name: node.name } : {}, text }, record });
|
|
1057
|
+
pending.set(edge.to, waiting);
|
|
1058
|
+
});
|
|
1059
|
+
record.live = targets.size;
|
|
1060
|
+
if (record.live === 0) terminals.push(record);
|
|
1061
|
+
};
|
|
1062
|
+
const drop = (nodeId, activations, reason) => {
|
|
1063
|
+
capped.add(reason);
|
|
1064
|
+
const from = activations.map((a) => a.source.nodeId);
|
|
1065
|
+
trace.push({ type: "dropped", nodeId, from, reason });
|
|
1066
|
+
emit({ type: "pipeline.node.skipped", nodeId, from, reason });
|
|
1067
|
+
for (const { record } of activations) {
|
|
1068
|
+
record.live--;
|
|
1069
|
+
if (record.live === 0) terminals.push(record);
|
|
1070
|
+
}
|
|
1071
|
+
};
|
|
1072
|
+
const deferred = (nodeId, all, reaches = plan.reaches) => (plan.nodes.get(nodeId)?.join ?? "all") === "all" && all.some((other) => other !== nodeId && reaches(other, nodeId));
|
|
1073
|
+
let inPass;
|
|
1074
|
+
emit({ type: "pipeline.started", ...definition.name ? { pipeline: definition.name } : {} });
|
|
1075
|
+
options.signal?.addEventListener("abort", onOuterAbort, { once: true });
|
|
1076
|
+
try {
|
|
1077
|
+
if (options.signal?.aborted) onOuterAbort();
|
|
1078
|
+
while (pending.size > 0 && !halted) {
|
|
1079
|
+
const waiting = [...pending.keys()];
|
|
1080
|
+
let ready = waiting.filter((nodeId) => !deferred(nodeId, waiting));
|
|
1081
|
+
if (ready.length === 0) {
|
|
1082
|
+
const reaches = inPass ??= passReach(plan);
|
|
1083
|
+
ready = waiting.filter((nodeId) => !deferred(nodeId, waiting, reaches));
|
|
1084
|
+
}
|
|
1085
|
+
if (ready.length === 0) ready = waiting;
|
|
1086
|
+
const starting = [];
|
|
1087
|
+
for (const nodeId of ready) {
|
|
1088
|
+
const activations = pending.get(nodeId);
|
|
1089
|
+
pending.delete(nodeId);
|
|
1090
|
+
if ((visits.get(nodeId) ?? 0) >= plan.maxNodeVisits) {
|
|
1091
|
+
drop(nodeId, activations, "maxNodeVisits");
|
|
1092
|
+
} else if (runs >= plan.maxRuns) {
|
|
1093
|
+
drop(nodeId, activations, "maxRuns");
|
|
1094
|
+
} else {
|
|
1095
|
+
starting.push(visit(nodeId, activations));
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
if (starting.length === 0) continue;
|
|
1099
|
+
await Promise.race([Promise.all(starting), haltPromise]);
|
|
1100
|
+
wave++;
|
|
1101
|
+
}
|
|
1102
|
+
if (halted) await haltPromise;
|
|
1103
|
+
} catch (err) {
|
|
1104
|
+
if (halted !== "aborted") throw err;
|
|
1105
|
+
emit({ type: "pipeline.finished", stoppedBy: "aborted", runs });
|
|
1106
|
+
if (onAbort === "reject") throw err;
|
|
1107
|
+
return result("aborted");
|
|
1108
|
+
} finally {
|
|
1109
|
+
options.signal?.removeEventListener("abort", onOuterAbort);
|
|
1110
|
+
}
|
|
1111
|
+
const stoppedBy = capped.has("maxRuns") ? "maxRuns" : capped.has("maxNodeVisits") ? "maxNodeVisits" : void 0;
|
|
1112
|
+
emit({ type: "pipeline.finished", ...stoppedBy ? { stoppedBy } : {}, runs });
|
|
1113
|
+
return result(stoppedBy);
|
|
1114
|
+
};
|
|
1115
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1116
|
+
0 && (module.exports = {
|
|
1117
|
+
PIPELINE_LIMITS,
|
|
1118
|
+
PIPELINE_ROUTER_SYSTEM,
|
|
1119
|
+
PipelineNodeError,
|
|
1120
|
+
PipelineValidationError,
|
|
1121
|
+
matchPipelineRoute,
|
|
1122
|
+
normalizePipeline,
|
|
1123
|
+
preparePipeline,
|
|
1124
|
+
renderPipelineNodeInput,
|
|
1125
|
+
renderPipelineRoutePrompt,
|
|
1126
|
+
runPipelineGraph,
|
|
1127
|
+
testPipelineCondition,
|
|
1128
|
+
validatePipeline
|
|
1129
|
+
});
|
|
1130
|
+
//# sourceMappingURL=pipeline-engine.cjs.map
|