@ai-setting/roy-agent-core 1.4.13 → 1.4.14

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.
Files changed (103) hide show
  1. package/dist/packages/core/src/config/index.js +32 -0
  2. package/dist/packages/core/src/env/agent/index.js +24 -0
  3. package/dist/packages/core/src/env/commands/index.js +14 -0
  4. package/dist/packages/core/src/env/debug/formatters/index.js +11 -0
  5. package/dist/packages/core/src/env/debug/index.js +26 -0
  6. package/dist/packages/core/src/env/hook/index.js +29 -0
  7. package/dist/packages/core/src/env/index.js +81 -0
  8. package/dist/packages/core/src/env/llm/index.js +40 -0
  9. package/dist/packages/core/src/env/log-trace/index.js +83 -0
  10. package/dist/packages/core/src/env/mcp/index.js +39 -0
  11. package/dist/packages/core/src/env/mcp/tool/index.js +14 -0
  12. package/dist/packages/core/src/env/memory/built-in/index.js +11 -0
  13. package/dist/packages/core/src/env/memory/index.js +56 -0
  14. package/dist/packages/core/src/env/memory/plugin/index.js +36 -0
  15. package/dist/packages/core/src/env/prompt/index.js +20 -0
  16. package/dist/packages/core/src/env/session/index.js +25 -0
  17. package/dist/packages/core/src/env/session/storage/index.js +18 -0
  18. package/dist/packages/core/src/env/skill/index.js +34 -0
  19. package/dist/packages/core/src/env/skill/tool/index.js +9 -0
  20. package/dist/packages/core/src/env/task/delegate/index.js +18 -0
  21. package/dist/packages/core/src/env/task/hooks/index.js +7 -0
  22. package/dist/packages/core/src/env/task/index.js +30 -0
  23. package/dist/packages/core/src/env/task/plugins/index.js +23 -0
  24. package/dist/packages/core/src/env/task/storage/index.js +14 -0
  25. package/dist/packages/core/src/env/task/tools/index.js +17 -0
  26. package/dist/packages/core/src/env/task/tools/operation/index.js +15 -0
  27. package/dist/{shared/chunk-1d4rwms4.js → packages/core/src/env/tool/built-in/index.js} +4 -4
  28. package/dist/packages/core/src/env/tool/index.js +39 -0
  29. package/dist/packages/core/src/env/workflow/decorators/index.js +27 -0
  30. package/dist/packages/core/src/env/workflow/engine/index.js +28 -0
  31. package/dist/packages/core/src/env/workflow/index.js +132 -0
  32. package/dist/packages/core/src/env/workflow/nodes/index.js +19 -0
  33. package/dist/packages/core/src/env/workflow/service/index.js +13 -0
  34. package/dist/packages/core/src/env/workflow/storage/index.js +27 -0
  35. package/dist/packages/core/src/env/workflow/tools/index.js +159 -0
  36. package/dist/packages/core/src/env/workflow/types/index.js +94 -0
  37. package/dist/packages/core/src/env/workflow/utils/index.js +637 -0
  38. package/dist/packages/core/src/index.js +398 -0
  39. package/dist/shared/@ai-setting/roy-agent-core-04fm8177.js +393 -0
  40. package/dist/shared/@ai-setting/roy-agent-core-04qgbjbe.js +172 -0
  41. package/dist/shared/@ai-setting/roy-agent-core-084qqd7t.js +11 -0
  42. package/dist/shared/@ai-setting/roy-agent-core-0gekht4e.js +1130 -0
  43. package/dist/shared/@ai-setting/roy-agent-core-0hdry23r.js +419 -0
  44. package/dist/shared/{chunk-ze20rksg.js → @ai-setting/roy-agent-core-0sgn3de4.js} +2 -2
  45. package/dist/shared/@ai-setting/roy-agent-core-12x57kf1.js +286 -0
  46. package/dist/shared/@ai-setting/roy-agent-core-1f3xrrm6.js +393 -0
  47. package/dist/shared/{chunk-q9j99fsm.js → @ai-setting/roy-agent-core-1k28kg7h.js} +6 -6
  48. package/dist/shared/{chunk-2b5kbhx3.js → @ai-setting/roy-agent-core-1z1zv5g8.js} +9 -117
  49. package/dist/shared/@ai-setting/roy-agent-core-2hqxnaf3.js +851 -0
  50. package/dist/shared/@ai-setting/roy-agent-core-3dfq8awb.js +587 -0
  51. package/dist/shared/@ai-setting/roy-agent-core-3takar0s.js +93 -0
  52. package/dist/shared/@ai-setting/roy-agent-core-3tnb2005.js +117 -0
  53. package/dist/shared/@ai-setting/roy-agent-core-4vmcvkav.js +14 -0
  54. package/dist/shared/{chunk-25x2pdtp.js → @ai-setting/roy-agent-core-4ws8atva.js} +2 -2
  55. package/dist/shared/@ai-setting/roy-agent-core-5fbp24se.js +603 -0
  56. package/dist/shared/@ai-setting/roy-agent-core-5my94ywp.js +66 -0
  57. package/dist/shared/{chunk-rncy3rtd.js → @ai-setting/roy-agent-core-6j0zcmwk.js} +128 -1122
  58. package/dist/shared/@ai-setting/roy-agent-core-6w4pmxc7.js +266 -0
  59. package/dist/shared/{chunk-yqmx37vm.js → @ai-setting/roy-agent-core-7vrk3add.js} +2 -2
  60. package/dist/shared/@ai-setting/roy-agent-core-8dvbn7tw.js +64 -0
  61. package/dist/shared/@ai-setting/roy-agent-core-8mbmrwzs.js +171 -0
  62. package/dist/shared/@ai-setting/roy-agent-core-8wzz66qe.js +620 -0
  63. package/dist/shared/@ai-setting/roy-agent-core-9ykq91jc.js +762 -0
  64. package/dist/shared/@ai-setting/roy-agent-core-dde19zke.js +1305 -0
  65. package/dist/shared/@ai-setting/roy-agent-core-f7g67gce.js +913 -0
  66. package/dist/shared/@ai-setting/roy-agent-core-fq5mtxsy.js +341 -0
  67. package/dist/shared/@ai-setting/roy-agent-core-fvd9g6k8.js +1205 -0
  68. package/dist/shared/@ai-setting/roy-agent-core-gv1hrn3x.js +378 -0
  69. package/dist/shared/@ai-setting/roy-agent-core-gy0wp5h7.js +213 -0
  70. package/dist/shared/{chunk-mf5xqbdh.js → @ai-setting/roy-agent-core-hyza1gm7.js} +3 -2
  71. package/dist/shared/{chunk-1qwabsm0.js → @ai-setting/roy-agent-core-j8zx62zr.js} +2 -2
  72. package/dist/shared/@ai-setting/roy-agent-core-jb2exr0d.js +442 -0
  73. package/dist/shared/@ai-setting/roy-agent-core-jv3b7v9w.js +57 -0
  74. package/dist/shared/@ai-setting/roy-agent-core-k1rxf9ya.js +513 -0
  75. package/dist/shared/@ai-setting/roy-agent-core-kydc9nwb.js +60 -0
  76. package/dist/shared/@ai-setting/roy-agent-core-m2x48hw6.js +97 -0
  77. package/dist/shared/@ai-setting/roy-agent-core-m6y668cc.js +377 -0
  78. package/dist/shared/{chunk-1aakcfp1.js → @ai-setting/roy-agent-core-nczzf0ms.js} +3 -3
  79. package/dist/shared/@ai-setting/roy-agent-core-nfj6knp5.js +36 -0
  80. package/dist/shared/@ai-setting/roy-agent-core-ntrp979d.js +204 -0
  81. package/dist/shared/@ai-setting/roy-agent-core-pd7g8z5v.js +1387 -0
  82. package/dist/shared/@ai-setting/roy-agent-core-pzk1syce.js +14 -0
  83. package/dist/shared/@ai-setting/roy-agent-core-q50tg9m2.js +862 -0
  84. package/dist/shared/{chunk-9qzt1v1p.js → @ai-setting/roy-agent-core-qg9tcaph.js} +3 -2
  85. package/dist/shared/{chunk-91bas8w5.js → @ai-setting/roy-agent-core-qhyerewk.js} +5 -5
  86. package/dist/shared/{chunk-1pf5mfgd.js → @ai-setting/roy-agent-core-qxybm159.js} +3 -3
  87. package/dist/shared/{chunk-g6j5n3gv.js → @ai-setting/roy-agent-core-rh9dpkpw.js} +10 -10
  88. package/dist/shared/{chunk-0q6s9wm6.js → @ai-setting/roy-agent-core-rr9p1g43.js} +7 -51
  89. package/dist/shared/@ai-setting/roy-agent-core-sbzvpfn7.js +284 -0
  90. package/dist/shared/@ai-setting/roy-agent-core-t22nqt4d.js +788 -0
  91. package/dist/shared/{chunk-t1rh6jtm.js → @ai-setting/roy-agent-core-tkr5ynkh.js} +8 -13
  92. package/dist/shared/@ai-setting/roy-agent-core-v4aabsf0.js +303 -0
  93. package/dist/shared/@ai-setting/roy-agent-core-w75rafhy.js +202 -0
  94. package/dist/shared/@ai-setting/roy-agent-core-w76hqkmg.js +584 -0
  95. package/dist/shared/@ai-setting/roy-agent-core-yfbgwes2.js +408 -0
  96. package/dist/shared/{chunk-a9qmy3sc.js → @ai-setting/roy-agent-core-yn761yve.js} +7 -4
  97. package/dist/shared/@ai-setting/roy-agent-core-yrzmn4m1.js +492 -0
  98. package/dist/shared/@ai-setting/roy-agent-core-yt8wdh2w.js +206 -0
  99. package/package.json +8 -7
  100. package/dist/index.d.ts +0 -7825
  101. package/dist/index.js +0 -16551
  102. package/dist/shared/chunk-hs7tbmje.js +0 -24
  103. /package/dist/shared/{chunk-wbkh7wat.js → @ai-setting/roy-agent-core-fs0mn2jk.js} +0 -0
@@ -0,0 +1,637 @@
1
+ import {
2
+ TracedAs,
3
+ init_decorator
4
+ } from "../../../../../../shared/@ai-setting/roy-agent-core-04qgbjbe.js";
5
+ import"../../../../../../shared/@ai-setting/roy-agent-core-yn761yve.js";
6
+ import"../../../../../../shared/@ai-setting/roy-agent-core-1k28kg7h.js";
7
+ import"../../../../../../shared/@ai-setting/roy-agent-core-j8zx62zr.js";
8
+ import {
9
+ __legacyDecorateClassTS
10
+ } from "../../../../../../shared/@ai-setting/roy-agent-core-fs0mn2jk.js";
11
+
12
+ // packages/core/src/env/workflow/utils/template-resolver.ts
13
+ class TemplateResolver {
14
+ input;
15
+ previousOutputs;
16
+ constructor(options = {}) {
17
+ this.input = options.input ?? {};
18
+ this.previousOutputs = options.previousOutputs ?? new Map;
19
+ }
20
+ static fromContext(context) {
21
+ return new TemplateResolver({
22
+ input: context.input,
23
+ previousOutputs: context.previousOutputs
24
+ });
25
+ }
26
+ resolveValue(input) {
27
+ if (input === null || input === undefined) {
28
+ return input;
29
+ }
30
+ if (typeof input === "string") {
31
+ const pureTemplate = this.extractPureTemplate(input);
32
+ if (pureTemplate) {
33
+ return this.resolvePureTemplate(pureTemplate);
34
+ }
35
+ return this.resolveString(input);
36
+ }
37
+ if (Array.isArray(input)) {
38
+ return input.map((item) => this.resolveValue(item));
39
+ }
40
+ if (typeof input === "object") {
41
+ const resolved = {};
42
+ for (const [key, value] of Object.entries(input)) {
43
+ resolved[key] = this.resolveValue(value);
44
+ }
45
+ return resolved;
46
+ }
47
+ return input;
48
+ }
49
+ extractPureTemplate(str) {
50
+ const trimmed = str.trim();
51
+ if (trimmed.startsWith("{{") && trimmed.endsWith("}}") && trimmed.length > 4) {
52
+ const content = trimmed.slice(2, -2).trim();
53
+ if (!content.includes("{{") && !content.includes("}}")) {
54
+ return content;
55
+ }
56
+ }
57
+ return null;
58
+ }
59
+ resolvePureTemplate(templateContent) {
60
+ const value = this.resolveTemplateContent(templateContent);
61
+ return value;
62
+ }
63
+ resolveTemplateContent(templateContent) {
64
+ const trimmed = templateContent.trim();
65
+ if (trimmed.startsWith("input.")) {
66
+ return this.getNestedValue(this.input, trimmed.slice(6));
67
+ }
68
+ if (trimmed.startsWith("nodes.")) {
69
+ return this.resolveNodePath(trimmed.slice(6));
70
+ }
71
+ if (trimmed !== "input" && trimmed !== "nodes") {
72
+ const segments = trimmed.split(".");
73
+ const nodeId = segments[0];
74
+ const restPath = segments.slice(1).join(".") || undefined;
75
+ return this.resolveNodePath(`${nodeId}${restPath ? "." + restPath : ""}`);
76
+ }
77
+ return;
78
+ }
79
+ resolveString(template) {
80
+ if (!template || typeof template !== "string") {
81
+ return template;
82
+ }
83
+ let resolved = template;
84
+ resolved = resolved.replace(/\{\{input\.([^}]+)\}\}/g, (match, path) => {
85
+ const value = this.getNestedValue(this.input, path);
86
+ return this.stringify(value, match);
87
+ });
88
+ resolved = resolved.replace(/\{\{nodes\.([^}]+)\}\}/g, (match, path) => {
89
+ const value = this.resolveNodePath(path);
90
+ return this.stringify(value, match);
91
+ });
92
+ resolved = resolved.replace(/\{\{([a-zA-Z_][a-zA-Z0-9_-]*)(?:\.([^}]+))?\}\}/g, (match, nodeId, path) => {
93
+ if (nodeId === "input") {
94
+ return match;
95
+ }
96
+ if (nodeId === "nodes") {
97
+ return match;
98
+ }
99
+ const value = this.resolveNodePath(`${nodeId}${path ? "." + path : ""}`);
100
+ return this.stringify(value, match);
101
+ });
102
+ return resolved;
103
+ }
104
+ resolveNodePath(path) {
105
+ const segments = path.split(".");
106
+ const nodeId = segments[0];
107
+ if (!nodeId) {
108
+ return;
109
+ }
110
+ let value = this.previousOutputs.get(nodeId);
111
+ if (value === undefined) {
112
+ const normalizedUnderscore = nodeId.replace(/-/g, "_");
113
+ const normalizedHyphen = nodeId.replace(/_/g, "-");
114
+ if (normalizedUnderscore !== nodeId) {
115
+ value = this.previousOutputs.get(normalizedUnderscore);
116
+ }
117
+ if (value === undefined && normalizedHyphen !== nodeId) {
118
+ value = this.previousOutputs.get(normalizedHyphen);
119
+ }
120
+ }
121
+ if (value === undefined) {
122
+ return;
123
+ }
124
+ if (segments.length === 1) {
125
+ if (value && typeof value === "object" && "result" in value && !("nodes" in value)) {
126
+ value = value.result;
127
+ }
128
+ return value;
129
+ }
130
+ const secondSegment = segments[1];
131
+ if (secondSegment === "result" || secondSegment === "output") {
132
+ if (value && typeof value === "object" && "result" in value) {
133
+ value = value.result;
134
+ if (segments.length === 2) {
135
+ return value;
136
+ }
137
+ for (let i = 2;i < segments.length; i++) {
138
+ if (value === null || value === undefined) {
139
+ return;
140
+ }
141
+ value = value[segments[i]];
142
+ }
143
+ return value;
144
+ }
145
+ }
146
+ for (let i = 1;i < segments.length; i++) {
147
+ if (value === null || value === undefined) {
148
+ return;
149
+ }
150
+ value = value[segments[i]];
151
+ }
152
+ return value;
153
+ }
154
+ getNestedValue(obj, path) {
155
+ if (!obj || !path) {
156
+ return;
157
+ }
158
+ const segments = path.split(".");
159
+ let current = obj;
160
+ for (const segment of segments) {
161
+ if (current === null || current === undefined) {
162
+ return;
163
+ }
164
+ if (typeof current !== "object") {
165
+ return;
166
+ }
167
+ current = current[segment];
168
+ }
169
+ return current;
170
+ }
171
+ stringify(value, originalTemplate) {
172
+ if (value === undefined) {
173
+ return originalTemplate;
174
+ }
175
+ if (typeof value === "string") {
176
+ return value;
177
+ }
178
+ if (value === null) {
179
+ return "null";
180
+ }
181
+ return JSON.stringify(value);
182
+ }
183
+ containsTemplates(str) {
184
+ if (typeof str !== "string") {
185
+ return false;
186
+ }
187
+ return /\{\{[^}]+\}\}/.test(str);
188
+ }
189
+ extractTemplates(str) {
190
+ if (typeof str !== "string") {
191
+ return [];
192
+ }
193
+ const matches = str.match(/\{\{[^}]+\}\}/g);
194
+ return matches ?? [];
195
+ }
196
+ }
197
+ function resolveTemplates(input, context) {
198
+ const resolver = TemplateResolver.fromContext(context);
199
+ return resolver.resolveValue(input);
200
+ }
201
+ function resolveTemplateString(template, context) {
202
+ const resolver = TemplateResolver.fromContext(context);
203
+ return resolver.resolveString(template);
204
+ }
205
+ // packages/core/src/env/workflow/utils/recovery-validator.ts
206
+ init_decorator();
207
+
208
+ // packages/core/src/env/workflow/utils/validation-rules.ts
209
+ var ValidationErrorType;
210
+ ((ValidationErrorType2) => {
211
+ ValidationErrorType2["MISSING_VARIABLE"] = "MISSING_VARIABLE";
212
+ ValidationErrorType2["INCOMPLETE_PARALLEL"] = "INCOMPLETE_PARALLEL";
213
+ ValidationErrorType2["AMBIGUOUS_BRANCH"] = "AMBIGUOUS_BRANCH";
214
+ ValidationErrorType2["LOOP_STATE_LOST"] = "LOOP_STATE_LOST";
215
+ ValidationErrorType2["NESTED_WORKFLOW_INVALID"] = "NESTED_WORKFLOW_INVALID";
216
+ ValidationErrorType2["NODE_STATE_INCONSISTENT"] = "NODE_STATE_INCONSISTENT";
217
+ ValidationErrorType2["MISSING_DEPENDENCY"] = "MISSING_DEPENDENCY";
218
+ })(ValidationErrorType ||= {});
219
+ var SuggestedAction;
220
+ ((SuggestedAction2) => {
221
+ SuggestedAction2["RE_RUN"] = "RE_RUN";
222
+ SuggestedAction2["CONFIRM"] = "CONFIRM";
223
+ SuggestedAction2["SKIP"] = "SKIP";
224
+ SuggestedAction2["PROVIDE_VALUE"] = "PROVIDE_VALUE";
225
+ SuggestedAction2["IGNORE"] = "IGNORE";
226
+ })(SuggestedAction ||= {});
227
+
228
+ // packages/core/src/env/workflow/utils/execution-state.ts
229
+ init_decorator();
230
+ class ExecutionStateBuilder {
231
+ build(messages, definition) {
232
+ const state = {
233
+ completedNodes: new Set,
234
+ inProgressNodes: new Set,
235
+ pendingNodes: new Set,
236
+ nodeOutputs: new Map,
237
+ nodeInputs: new Map,
238
+ variables: new Map,
239
+ loopCounts: new Map
240
+ };
241
+ this.parseMessages(messages, state);
242
+ this.buildPendingNodes(definition, state);
243
+ return state;
244
+ }
245
+ parseMessages(messages, state) {
246
+ let lastCallNodeId = null;
247
+ let lastCallInput = null;
248
+ for (const msg of messages) {
249
+ const metadata = msg.metadata;
250
+ const msgType = metadata?.type;
251
+ switch (msgType) {
252
+ case "workflow.node.call": {
253
+ const nodeId = metadata?.workflowNodeId;
254
+ if (nodeId) {
255
+ lastCallNodeId = nodeId;
256
+ lastCallInput = this.extractInput(msg);
257
+ state.inProgressNodes.add(nodeId);
258
+ state.nodeInputs.set(nodeId, lastCallInput);
259
+ }
260
+ break;
261
+ }
262
+ case "workflow.node.result": {
263
+ if (lastCallNodeId) {
264
+ state.inProgressNodes.delete(lastCallNodeId);
265
+ state.completedNodes.add(lastCallNodeId);
266
+ const output = this.extractOutput(msg);
267
+ state.nodeOutputs.set(lastCallNodeId, output);
268
+ this.extractVariables(output, state);
269
+ lastCallNodeId = null;
270
+ lastCallInput = null;
271
+ }
272
+ break;
273
+ }
274
+ case "workflow.node.interrupt": {
275
+ if (lastCallNodeId) {
276
+ state.inProgressNodes.add(lastCallNodeId);
277
+ }
278
+ break;
279
+ }
280
+ case "workflow.node.resume": {
281
+ if (lastCallNodeId) {
282
+ state.inProgressNodes.add(lastCallNodeId);
283
+ }
284
+ break;
285
+ }
286
+ }
287
+ }
288
+ }
289
+ buildPendingNodes(definition, state) {
290
+ const nodeIds = new Set((definition.nodes || []).map((n) => n.id));
291
+ const allNodeIds = Array.from(nodeIds);
292
+ const entryNodes = this.getEntryNodes(definition);
293
+ for (const entryNode of entryNodes) {
294
+ if (!state.completedNodes.has(entryNode)) {
295
+ this.markPendingFromNode(entryNode, definition, state);
296
+ }
297
+ }
298
+ for (const completedId of state.completedNodes) {
299
+ const children = this.getDirectChildren(completedId, definition);
300
+ for (const childId of children) {
301
+ if (!state.completedNodes.has(childId) && !state.inProgressNodes.has(childId)) {
302
+ state.pendingNodes.add(childId);
303
+ }
304
+ }
305
+ }
306
+ }
307
+ getEntryNodes(definition) {
308
+ const entry = definition.entry;
309
+ if (!entry) {
310
+ return [];
311
+ }
312
+ if (Array.isArray(entry)) {
313
+ return entry;
314
+ }
315
+ return [entry];
316
+ }
317
+ markPendingFromNode(startNodeId, definition, state) {
318
+ const visited = new Set;
319
+ const queue = [startNodeId];
320
+ while (queue.length > 0) {
321
+ const nodeId = queue.shift();
322
+ if (visited.has(nodeId)) {
323
+ continue;
324
+ }
325
+ visited.add(nodeId);
326
+ if (!state.completedNodes.has(nodeId)) {
327
+ state.pendingNodes.add(nodeId);
328
+ }
329
+ const children = this.getDirectChildren(nodeId, definition);
330
+ for (const childId of children) {
331
+ if (!visited.has(childId)) {
332
+ queue.push(childId);
333
+ }
334
+ }
335
+ }
336
+ }
337
+ getDirectChildren(nodeId, definition) {
338
+ const edges = definition.edges || definition.connections || [];
339
+ return edges.filter((edge) => edge.source === nodeId || edge.from === nodeId).map((edge) => edge.target || edge.to);
340
+ }
341
+ extractInput(msg) {
342
+ try {
343
+ return JSON.parse(msg.content);
344
+ } catch {
345
+ return msg.content;
346
+ }
347
+ }
348
+ extractOutput(msg) {
349
+ try {
350
+ return JSON.parse(msg.content);
351
+ } catch {
352
+ return msg.content;
353
+ }
354
+ }
355
+ extractVariables(output, state) {
356
+ if (!output || typeof output !== "object") {
357
+ return;
358
+ }
359
+ const outputObj = output;
360
+ for (const [key, value] of Object.entries(outputObj)) {
361
+ if (key.startsWith("_") || key.startsWith("$")) {
362
+ continue;
363
+ }
364
+ if (!state.variables.has(key)) {
365
+ state.variables.set(key, value);
366
+ }
367
+ }
368
+ }
369
+ }
370
+ __legacyDecorateClassTS([
371
+ TracedAs("recovery.builder.build", { recordParams: true, recordResult: true, log: true })
372
+ ], ExecutionStateBuilder.prototype, "build", null);
373
+ __legacyDecorateClassTS([
374
+ TracedAs("recovery.builder.parseMessages", { recordParams: true, recordResult: true, log: true })
375
+ ], ExecutionStateBuilder.prototype, "parseMessages", null);
376
+ __legacyDecorateClassTS([
377
+ TracedAs("recovery.builder.buildPendingNodes", { recordParams: true, recordResult: true, log: true })
378
+ ], ExecutionStateBuilder.prototype, "buildPendingNodes", null);
379
+
380
+ // packages/core/src/env/workflow/utils/recovery-validator.ts
381
+ class RecoveryValidator {
382
+ stateBuilder;
383
+ constructor() {
384
+ this.stateBuilder = new ExecutionStateBuilder;
385
+ }
386
+ async validate(messages, definition) {
387
+ const state = this.stateBuilder.build(messages, definition);
388
+ const errors = [];
389
+ const warnings = [];
390
+ errors.push(...this.validateStateIntegrity(state, definition));
391
+ errors.push(...this.validateParallelBranches(state, definition));
392
+ errors.push(...this.validateConditionBranches(state, definition));
393
+ errors.push(...this.validateLoopState(state, definition));
394
+ errors.push(...this.validateNestedWorkflows(state, definition));
395
+ return {
396
+ isValid: errors.length === 0,
397
+ errors,
398
+ warnings
399
+ };
400
+ }
401
+ async generatePlan(messages, definition) {
402
+ const state = this.stateBuilder.build(messages, definition);
403
+ const validationResult = await this.validate(messages, definition);
404
+ const pendingNodes = Array.from(state.pendingNodes);
405
+ const completedNodes = Array.from(state.completedNodes);
406
+ const resumePoint = this.determineResumePoint(state, definition);
407
+ const summary = this.generateSummary(state, validationResult);
408
+ return {
409
+ canResume: validationResult.isValid,
410
+ resumePoint,
411
+ completedNodes,
412
+ pendingNodes,
413
+ skippedNodes: [],
414
+ validationErrors: validationResult.errors,
415
+ summary
416
+ };
417
+ }
418
+ validateStateIntegrity(state, definition) {
419
+ const errors = [];
420
+ for (const nodeId of state.pendingNodes) {
421
+ const node = definition.nodes?.find((n) => n.id === nodeId);
422
+ if (!node?.config)
423
+ continue;
424
+ const variableRefs = this.extractVariableRefs(node.config);
425
+ for (const varRef of variableRefs) {
426
+ if (!state.nodeOutputs.has(varRef) && !state.variables.has(varRef)) {
427
+ errors.push({
428
+ type: "MISSING_VARIABLE" /* MISSING_VARIABLE */,
429
+ nodeId,
430
+ description: `变量 ${varRef} 不存在,需要重新运行`,
431
+ suggestedAction: "RE_RUN" /* RE_RUN */,
432
+ context: { variable: varRef, nodeConfig: node.config }
433
+ });
434
+ }
435
+ }
436
+ }
437
+ return errors;
438
+ }
439
+ validateParallelBranches(state, definition) {
440
+ const errors = [];
441
+ const parallelGroups = this.identifyParallelGroups(definition);
442
+ for (const group of parallelGroups) {
443
+ const completedInGroup = group.filter((id) => state.completedNodes.has(id));
444
+ const inProgressInGroup = group.filter((id) => state.inProgressNodes.has(id));
445
+ if (inProgressInGroup.length > 0 || completedInGroup.length < group.length) {
446
+ const mergeNode = this.findMergeNodeForParallel(group, definition);
447
+ if (mergeNode && !state.completedNodes.has(mergeNode)) {
448
+ const incomplete = group.filter((id) => !state.completedNodes.has(id));
449
+ errors.push({
450
+ type: "INCOMPLETE_PARALLEL" /* INCOMPLETE_PARALLEL */,
451
+ nodeId: mergeNode,
452
+ description: `并行分支 ${incomplete.join(", ")} 未完成`,
453
+ suggestedAction: "RE_RUN" /* RE_RUN */,
454
+ context: { incompleteBranches: incomplete, mergeNode }
455
+ });
456
+ }
457
+ }
458
+ }
459
+ return errors;
460
+ }
461
+ validateConditionBranches(state, definition) {
462
+ const errors = [];
463
+ const edges = definition.edges || definition.connections || [];
464
+ const conditionalEdges = edges.filter((e) => e.condition);
465
+ for (const edge of conditionalEdges) {
466
+ const conditionVars = this.extractVariableRefs(edge.condition);
467
+ for (const varRef of conditionVars) {
468
+ if (!state.nodeOutputs.has(varRef) && !state.variables.has(varRef)) {
469
+ errors.push({
470
+ type: "AMBIGUOUS_BRANCH" /* AMBIGUOUS_BRANCH */,
471
+ nodeId: edge.target || edge.to,
472
+ description: `条件分支无法确定:变量 ${varRef} 不存在`,
473
+ suggestedAction: "CONFIRM" /* CONFIRM */,
474
+ context: { variable: varRef, condition: edge.condition }
475
+ });
476
+ }
477
+ }
478
+ }
479
+ return errors;
480
+ }
481
+ validateLoopState(state, definition) {
482
+ const errors = [];
483
+ const loops = this.identifyLoops(definition);
484
+ for (const loop of loops) {
485
+ const loopCounter = `loop_count_${loop.header}`;
486
+ if (!state.nodeOutputs.has(loopCounter) && state.pendingNodes.has(loop.header)) {
487
+ errors.push({
488
+ type: "LOOP_STATE_LOST" /* LOOP_STATE_LOST */,
489
+ nodeId: loop.header,
490
+ description: `循环状态丢失,循环计数器 ${loopCounter} 不存在`,
491
+ suggestedAction: "RE_RUN" /* RE_RUN */,
492
+ context: { loopHeader: loop.header }
493
+ });
494
+ }
495
+ }
496
+ return errors;
497
+ }
498
+ validateNestedWorkflows(state, definition) {
499
+ const errors = [];
500
+ const workflowNodes = (definition.nodes || []).filter((n) => n.type === "workflow");
501
+ for (const node of workflowNodes) {
502
+ const nodeOutput = state.nodeOutputs.get(node.id);
503
+ const subSessionId = nodeOutput?.sessionId;
504
+ if (subSessionId && state.pendingNodes.has(node.id)) {}
505
+ }
506
+ return errors;
507
+ }
508
+ extractVariableRefs(obj) {
509
+ const refs = [];
510
+ if (!obj)
511
+ return refs;
512
+ const str = JSON.stringify(obj);
513
+ const regex = /\{\{([^}]+)\}\}/g;
514
+ let match;
515
+ while ((match = regex.exec(str)) !== null) {
516
+ refs.push(match[1].trim());
517
+ }
518
+ return refs;
519
+ }
520
+ identifyParallelGroups(definition) {
521
+ const groups = [];
522
+ const sources = new Map;
523
+ const edges = definition.edges || definition.connections || [];
524
+ for (const edge of edges) {
525
+ const source = edge.source || edge.from;
526
+ const target = edge.target || edge.to;
527
+ const targets = sources.get(source) || [];
528
+ targets.push(target);
529
+ sources.set(source, targets);
530
+ }
531
+ for (const [_source, targets] of sources) {
532
+ if (targets.length > 1) {
533
+ groups.push(targets);
534
+ }
535
+ }
536
+ return groups;
537
+ }
538
+ findMergeNodeForParallel(parallelIds, definition) {
539
+ const edges = definition.edges || definition.connections || [];
540
+ for (const edge of edges) {
541
+ const target = edge.target || edge.to;
542
+ const source = edge.source || edge.from;
543
+ const allSourcesFromParallel = parallelIds.includes(source);
544
+ const hasIncomingFromParallel = parallelIds.some((id) => edges.some((e) => (e.source || e.from) === id && (e.target || e.to) === target));
545
+ if (allSourcesFromParallel && hasIncomingFromParallel) {
546
+ return target;
547
+ }
548
+ }
549
+ return null;
550
+ }
551
+ identifyLoops(definition) {
552
+ const loops = [];
553
+ const visited = new Set;
554
+ const recStack = new Set;
555
+ const edges = definition.edges || definition.connections || [];
556
+ const getChildren = (nodeId) => {
557
+ return edges.filter((e) => (e.source || e.from) === nodeId).map((e) => e.target || e.to);
558
+ };
559
+ const dfs = (nodeId, path) => {
560
+ visited.add(nodeId);
561
+ recStack.add(nodeId);
562
+ const outgoing = getChildren(nodeId);
563
+ for (const childId of outgoing) {
564
+ if (!visited.has(childId)) {
565
+ dfs(childId, [...path, childId]);
566
+ } else if (recStack.has(childId)) {
567
+ const loopStart = path.indexOf(childId);
568
+ if (loopStart !== -1) {
569
+ loops.push({
570
+ header: childId,
571
+ body: path.slice(loopStart)
572
+ });
573
+ }
574
+ }
575
+ }
576
+ recStack.delete(nodeId);
577
+ };
578
+ for (const node of definition.nodes || []) {
579
+ if (!visited.has(node.id)) {
580
+ dfs(node.id, [node.id]);
581
+ }
582
+ }
583
+ return loops;
584
+ }
585
+ determineResumePoint(state, definition) {
586
+ const inProgress = Array.from(state.inProgressNodes);
587
+ if (inProgress.length > 0) {
588
+ return inProgress[0];
589
+ }
590
+ const pending = Array.from(state.pendingNodes);
591
+ if (pending.length > 0) {
592
+ return pending[0];
593
+ }
594
+ const entry = definition.entry;
595
+ if (Array.isArray(entry)) {
596
+ return entry[0] || "";
597
+ }
598
+ return entry || "";
599
+ }
600
+ generateSummary(state, result) {
601
+ if (result.isValid) {
602
+ return `恢复验证通过。已完成 ${state.completedNodes.size} 个节点,待执行 ${state.pendingNodes.size} 个节点。`;
603
+ }
604
+ const errorTypes = result.errors.map((e) => e.type).join(", ");
605
+ return `恢复验证失败:${errorTypes}。${result.errors.length} 个错误需要处理。`;
606
+ }
607
+ }
608
+ __legacyDecorateClassTS([
609
+ TracedAs("recovery.validator.validate", { recordParams: true, recordResult: true, log: true })
610
+ ], RecoveryValidator.prototype, "validate", null);
611
+ __legacyDecorateClassTS([
612
+ TracedAs("recovery.generate.plan", { recordParams: true, recordResult: true, log: true })
613
+ ], RecoveryValidator.prototype, "generatePlan", null);
614
+ __legacyDecorateClassTS([
615
+ TracedAs("recovery.validator.validateStateIntegrity", { recordParams: true, recordResult: true, log: true })
616
+ ], RecoveryValidator.prototype, "validateStateIntegrity", null);
617
+ __legacyDecorateClassTS([
618
+ TracedAs("recovery.validator.validateParallel", { recordParams: true, recordResult: true, log: true })
619
+ ], RecoveryValidator.prototype, "validateParallelBranches", null);
620
+ __legacyDecorateClassTS([
621
+ TracedAs("recovery.validator.validateCondition", { recordParams: true, recordResult: true, log: true })
622
+ ], RecoveryValidator.prototype, "validateConditionBranches", null);
623
+ __legacyDecorateClassTS([
624
+ TracedAs("recovery.validator.validateLoop", { recordParams: true, recordResult: true, log: true })
625
+ ], RecoveryValidator.prototype, "validateLoopState", null);
626
+ __legacyDecorateClassTS([
627
+ TracedAs("recovery.validator.validateNested", { recordParams: true, recordResult: true, log: true })
628
+ ], RecoveryValidator.prototype, "validateNestedWorkflows", null);
629
+ export {
630
+ resolveTemplates,
631
+ resolveTemplateString,
632
+ ValidationErrorType,
633
+ TemplateResolver,
634
+ SuggestedAction,
635
+ RecoveryValidator,
636
+ ExecutionStateBuilder
637
+ };