@flowgram.ai/runtime-js 0.2.21 → 0.2.23

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/dist/index.js CHANGED
@@ -240,8 +240,27 @@ var FlowGramNode = /* @__PURE__ */ ((FlowGramNode22) => {
240
240
  FlowGramNode22["Loop"] = "loop";
241
241
  FlowGramNode22["Comment"] = "comment";
242
242
  FlowGramNode22["Group"] = "group";
243
+ FlowGramNode22["BlockStart"] = "block-start";
244
+ FlowGramNode22["BlockEnd"] = "block-end";
243
245
  return FlowGramNode22;
244
246
  })(FlowGramNode || {});
247
+ var ConditionOperation = /* @__PURE__ */ ((ConditionOperation2) => {
248
+ ConditionOperation2["EQ"] = "eq";
249
+ ConditionOperation2["NEQ"] = "neq";
250
+ ConditionOperation2["GT"] = "gt";
251
+ ConditionOperation2["GTE"] = "gte";
252
+ ConditionOperation2["LT"] = "lt";
253
+ ConditionOperation2["LTE"] = "lte";
254
+ ConditionOperation2["IN"] = "in";
255
+ ConditionOperation2["NIN"] = "nin";
256
+ ConditionOperation2["CONTAINS"] = "contains";
257
+ ConditionOperation2["NOT_CONTAINS"] = "not_contains";
258
+ ConditionOperation2["IS_EMPTY"] = "is_empty";
259
+ ConditionOperation2["IS_NOT_EMPTY"] = "is_not_empty";
260
+ ConditionOperation2["IS_TRUE"] = "is_true";
261
+ ConditionOperation2["IS_FALSE"] = "is_false";
262
+ return ConditionOperation2;
263
+ })(ConditionOperation || {});
245
264
  var IEngine = Symbol.for("Engine");
246
265
  var IExecutor = Symbol.for("Executor");
247
266
  var WorkflowStatus = /* @__PURE__ */ ((WorkflowStatus2) => {
@@ -253,6 +272,14 @@ var WorkflowStatus = /* @__PURE__ */ ((WorkflowStatus2) => {
253
272
  return WorkflowStatus2;
254
273
  })(WorkflowStatus || {});
255
274
  var IValidation = Symbol.for("Validation");
275
+ var WorkflowMessageType = /* @__PURE__ */ ((WorkflowMessageType2) => {
276
+ WorkflowMessageType2["Log"] = "log";
277
+ WorkflowMessageType2["Info"] = "info";
278
+ WorkflowMessageType2["Debug"] = "debug";
279
+ WorkflowMessageType2["Error"] = "error";
280
+ WorkflowMessageType2["Warn"] = "warning";
281
+ return WorkflowMessageType2;
282
+ })(WorkflowMessageType || {});
256
283
 
257
284
  // src/nodes/start/index.ts
258
285
  var StartExecutor = class {
@@ -268,26 +295,130 @@ var StartExecutor = class {
268
295
 
269
296
  // src/nodes/loop/index.ts
270
297
  var import_lodash_es = require("lodash-es");
298
+
299
+ // src/infrastructure/utils/uuid.ts
300
+ var import_uuid = require("uuid");
301
+ var uuid = import_uuid.v4;
302
+
303
+ // src/infrastructure/utils/runtime-type.ts
304
+ var WorkflowRuntimeType;
305
+ ((WorkflowRuntimeType2) => {
306
+ WorkflowRuntimeType2.getWorkflowType = (value) => {
307
+ if (value === null || value === void 0) {
308
+ return WorkflowVariableType.Null;
309
+ }
310
+ if (typeof value === "string") {
311
+ return WorkflowVariableType.String;
312
+ }
313
+ if (typeof value === "boolean") {
314
+ return WorkflowVariableType.Boolean;
315
+ }
316
+ if (typeof value === "number") {
317
+ if (Number.isInteger(value)) {
318
+ return WorkflowVariableType.Integer;
319
+ }
320
+ return WorkflowVariableType.Number;
321
+ }
322
+ if (Array.isArray(value)) {
323
+ return WorkflowVariableType.Array;
324
+ }
325
+ if (typeof value === "object") {
326
+ return WorkflowVariableType.Object;
327
+ }
328
+ return null;
329
+ };
330
+ WorkflowRuntimeType2.isMatchWorkflowType = (value, type) => {
331
+ const workflowType = (0, WorkflowRuntimeType2.getWorkflowType)(value);
332
+ if (!workflowType) {
333
+ return false;
334
+ }
335
+ return workflowType === type;
336
+ };
337
+ WorkflowRuntimeType2.isTypeEqual = (leftType, rightType) => {
338
+ if (leftType === WorkflowVariableType.Number && rightType === WorkflowVariableType.Integer || leftType === WorkflowVariableType.Integer && rightType === WorkflowVariableType.Number) {
339
+ return true;
340
+ }
341
+ return leftType === rightType;
342
+ };
343
+ WorkflowRuntimeType2.getArrayItemsType = (types) => {
344
+ const expectedType = types[0];
345
+ types.forEach((type) => {
346
+ if (type !== expectedType) {
347
+ throw new Error(`array items type must be same, expect ${expectedType}, but got ${type}`);
348
+ }
349
+ });
350
+ return expectedType;
351
+ };
352
+ })(WorkflowRuntimeType || (WorkflowRuntimeType = {}));
353
+
354
+ // src/infrastructure/utils/traverse-nodes.ts
355
+ function traverseNodes(startNode, getConnectedNodes) {
356
+ const visited = /* @__PURE__ */ new Set();
357
+ const result = [];
358
+ const traverse = (node) => {
359
+ for (const connectedNode of getConnectedNodes(node)) {
360
+ if (!visited.has(connectedNode.id)) {
361
+ visited.add(connectedNode.id);
362
+ result.push(connectedNode);
363
+ traverse(connectedNode);
364
+ }
365
+ }
366
+ };
367
+ traverse(startNode);
368
+ return result;
369
+ }
370
+
371
+ // src/infrastructure/utils/compare-node-groups.ts
372
+ function compareNodeGroups(groupA, groupB) {
373
+ const flatA = groupA.flat();
374
+ const setA = /* @__PURE__ */ new Map();
375
+ flatA.forEach((node) => {
376
+ setA.set(node.id, node);
377
+ });
378
+ const flatB = groupB.flat();
379
+ const setB = /* @__PURE__ */ new Map();
380
+ flatB.forEach((node) => {
381
+ setB.set(node.id, node);
382
+ });
383
+ const common = [];
384
+ const uniqueToA = [];
385
+ const uniqueToB = [];
386
+ setA.forEach((node, id) => {
387
+ if (setB.has(id)) {
388
+ common.push(node);
389
+ } else {
390
+ uniqueToA.push(node);
391
+ }
392
+ });
393
+ setB.forEach((node, id) => {
394
+ if (!setA.has(id)) {
395
+ uniqueToB.push(node);
396
+ }
397
+ });
398
+ return {
399
+ common,
400
+ uniqueToA,
401
+ uniqueToB
402
+ };
403
+ }
404
+
405
+ // src/nodes/loop/index.ts
271
406
  var LoopExecutor = class {
272
407
  constructor() {
273
408
  this.type = FlowGramNode.Loop;
274
409
  }
275
410
  async execute(context) {
276
411
  const loopNodeID = context.node.id;
277
- const loopArrayResult = context.runtime.state.parseRef(context.node.data.batchFor);
278
- this.checkLoopArray(loopArrayResult);
279
- const loopArray = loopArrayResult.value;
280
- const itemsType = loopArrayResult.itemsType;
281
412
  const engine = context.container.get(IEngine);
413
+ const { value: loopArray, itemsType } = this.getLoopArrayVariable(context);
282
414
  const subNodes = context.node.children;
283
- const startSubNodes = subNodes.filter((node) => node.prev.length === 0);
284
- if (loopArray.length === 0 || startSubNodes.length === 0) {
285
- return {
286
- outputs: {}
287
- };
415
+ const blockStartNode = subNodes.find((node) => node.type === FlowGramNode.BlockStart);
416
+ if (!blockStartNode) {
417
+ throw new Error("block start node not found");
288
418
  }
289
- for (let i = 0; i < loopArray.length; i++) {
290
- const loopItem = loopArray[i];
419
+ const blockOutputs = [];
420
+ for (let index = 0; index < loopArray.length; index++) {
421
+ const loopItem = loopArray[index];
291
422
  const subContext = context.runtime.sub();
292
423
  subContext.variableStore.setVariable({
293
424
  nodeID: `${loopNodeID}_locals`,
@@ -295,33 +426,99 @@ var LoopExecutor = class {
295
426
  type: itemsType,
296
427
  value: loopItem
297
428
  });
298
- await Promise.all(
299
- startSubNodes.map(
300
- (node) => engine.executeNode({
301
- context: subContext,
302
- node
303
- })
304
- )
305
- );
429
+ subContext.variableStore.setVariable({
430
+ nodeID: `${loopNodeID}_locals`,
431
+ key: "index",
432
+ type: WorkflowVariableType.Number,
433
+ value: index
434
+ });
435
+ await engine.executeNode({
436
+ context: subContext,
437
+ node: blockStartNode
438
+ });
439
+ const blockOutput = this.getBlockOutput(context, subContext);
440
+ blockOutputs.push(blockOutput);
306
441
  }
442
+ this.setLoopNodeOutputs(context, blockOutputs);
443
+ const outputs = this.combineBlockOutputs(context, blockOutputs);
307
444
  return {
308
- outputs: {}
445
+ outputs
309
446
  };
310
447
  }
311
- checkLoopArray(loopArrayResult) {
312
- const loopArray = loopArrayResult?.value;
448
+ getLoopArrayVariable(executionContext) {
449
+ const loopNodeData = executionContext.node.data;
450
+ const LoopArrayVariable = executionContext.runtime.state.parseRef(
451
+ loopNodeData.loopFor
452
+ );
453
+ this.checkLoopArray(LoopArrayVariable);
454
+ return LoopArrayVariable;
455
+ }
456
+ checkLoopArray(LoopArrayVariable) {
457
+ const loopArray = LoopArrayVariable?.value;
313
458
  if (!loopArray || (0, import_lodash_es.isNil)(loopArray) || !Array.isArray(loopArray)) {
314
- throw new Error("batchFor is required");
459
+ throw new Error("loopFor is required");
315
460
  }
316
- const loopArrayType = loopArrayResult.type;
461
+ const loopArrayType = LoopArrayVariable.type;
317
462
  if (loopArrayType !== WorkflowVariableType.Array) {
318
- throw new Error("batchFor must be an array");
463
+ throw new Error("loopFor must be an array");
319
464
  }
320
- const loopArrayItemType = loopArrayResult.itemsType;
465
+ const loopArrayItemType = LoopArrayVariable.itemsType;
321
466
  if ((0, import_lodash_es.isNil)(loopArrayItemType)) {
322
- throw new Error("batchFor items must be array items");
467
+ throw new Error("loopFor items must be array items");
323
468
  }
324
469
  }
470
+ getBlockOutput(executionContext, subContext) {
471
+ const loopOutputsDeclare = this.getLoopOutputsDeclare(executionContext);
472
+ const blockOutput = Object.entries(loopOutputsDeclare).reduce(
473
+ (acc, [outputName, outputRef]) => {
474
+ const outputVariable = subContext.state.parseRef(outputRef);
475
+ if (!outputVariable) {
476
+ return acc;
477
+ }
478
+ return {
479
+ ...acc,
480
+ [outputName]: outputVariable
481
+ };
482
+ },
483
+ {}
484
+ );
485
+ return blockOutput;
486
+ }
487
+ setLoopNodeOutputs(executionContext, blockOutputs) {
488
+ const loopNode = executionContext.node;
489
+ const loopOutputsDeclare = this.getLoopOutputsDeclare(executionContext);
490
+ const loopOutputNames = Object.keys(loopOutputsDeclare);
491
+ loopOutputNames.forEach((outputName) => {
492
+ const outputVariables = blockOutputs.map((blockOutput) => blockOutput[outputName]);
493
+ const outputTypes = outputVariables.map((fieldVariable) => fieldVariable.type);
494
+ const itemsType = WorkflowRuntimeType.getArrayItemsType(outputTypes);
495
+ const value = outputVariables.map((fieldVariable) => fieldVariable.value);
496
+ executionContext.runtime.variableStore.setVariable({
497
+ nodeID: loopNode.id,
498
+ key: outputName,
499
+ type: WorkflowVariableType.Array,
500
+ itemsType,
501
+ value
502
+ });
503
+ });
504
+ }
505
+ combineBlockOutputs(executionContext, blockOutputs) {
506
+ const loopOutputsDeclare = this.getLoopOutputsDeclare(executionContext);
507
+ const loopOutputNames = Object.keys(loopOutputsDeclare);
508
+ const loopOutput = loopOutputNames.reduce(
509
+ (outputs, outputName) => ({
510
+ ...outputs,
511
+ [outputName]: blockOutputs.map((blockOutput) => blockOutput[outputName].value)
512
+ }),
513
+ {}
514
+ );
515
+ return loopOutput;
516
+ }
517
+ getLoopOutputsDeclare(executionContext) {
518
+ const loopNodeData = executionContext.node.data;
519
+ const loopOutputsDeclare = loopNodeData.loopOutputs ?? {};
520
+ return loopOutputsDeclare;
521
+ }
325
522
  };
326
523
 
327
524
  // src/nodes/llm/index.ts
@@ -384,67 +581,89 @@ var EndExecutor = class {
384
581
  }
385
582
  };
386
583
 
584
+ // src/nodes/empty/index.ts
585
+ var BlockStartExecutor = class {
586
+ constructor() {
587
+ this.type = FlowGramNode.BlockStart;
588
+ }
589
+ async execute(context) {
590
+ return {
591
+ outputs: {}
592
+ };
593
+ }
594
+ };
595
+ var BlockEndExecutor = class {
596
+ constructor() {
597
+ this.type = FlowGramNode.BlockEnd;
598
+ }
599
+ async execute(context) {
600
+ return {
601
+ outputs: {}
602
+ };
603
+ }
604
+ };
605
+
387
606
  // src/nodes/condition/index.ts
388
607
  var import_lodash_es9 = require("lodash-es");
389
608
 
390
609
  // src/nodes/condition/rules.ts
391
610
  var conditionRules = {
392
611
  [WorkflowVariableType.String]: {
393
- ["eq" /* EQ */]: WorkflowVariableType.String,
394
- ["neq" /* NEQ */]: WorkflowVariableType.String,
395
- ["contains" /* CONTAINS */]: WorkflowVariableType.String,
396
- ["not_contains" /* NOT_CONTAINS */]: WorkflowVariableType.String,
397
- ["in" /* IN */]: WorkflowVariableType.Array,
398
- ["nin" /* NIN */]: WorkflowVariableType.Array,
399
- ["is_empty" /* IS_EMPTY */]: WorkflowVariableType.String,
400
- ["is_not_empty" /* IS_NOT_EMPTY */]: WorkflowVariableType.String
612
+ [ConditionOperation.EQ]: WorkflowVariableType.String,
613
+ [ConditionOperation.NEQ]: WorkflowVariableType.String,
614
+ [ConditionOperation.CONTAINS]: WorkflowVariableType.String,
615
+ [ConditionOperation.NOT_CONTAINS]: WorkflowVariableType.String,
616
+ [ConditionOperation.IN]: WorkflowVariableType.Array,
617
+ [ConditionOperation.NIN]: WorkflowVariableType.Array,
618
+ [ConditionOperation.IS_EMPTY]: WorkflowVariableType.String,
619
+ [ConditionOperation.IS_NOT_EMPTY]: WorkflowVariableType.String
401
620
  },
402
621
  [WorkflowVariableType.Number]: {
403
- ["eq" /* EQ */]: WorkflowVariableType.Number,
404
- ["neq" /* NEQ */]: WorkflowVariableType.Number,
405
- ["gt" /* GT */]: WorkflowVariableType.Number,
406
- ["gte" /* GTE */]: WorkflowVariableType.Number,
407
- ["lt" /* LT */]: WorkflowVariableType.Number,
408
- ["lte" /* LTE */]: WorkflowVariableType.Number,
409
- ["in" /* IN */]: WorkflowVariableType.Array,
410
- ["nin" /* NIN */]: WorkflowVariableType.Array,
411
- ["is_empty" /* IS_EMPTY */]: WorkflowVariableType.Null,
412
- ["is_not_empty" /* IS_NOT_EMPTY */]: WorkflowVariableType.Null
622
+ [ConditionOperation.EQ]: WorkflowVariableType.Number,
623
+ [ConditionOperation.NEQ]: WorkflowVariableType.Number,
624
+ [ConditionOperation.GT]: WorkflowVariableType.Number,
625
+ [ConditionOperation.GTE]: WorkflowVariableType.Number,
626
+ [ConditionOperation.LT]: WorkflowVariableType.Number,
627
+ [ConditionOperation.LTE]: WorkflowVariableType.Number,
628
+ [ConditionOperation.IN]: WorkflowVariableType.Array,
629
+ [ConditionOperation.NIN]: WorkflowVariableType.Array,
630
+ [ConditionOperation.IS_EMPTY]: WorkflowVariableType.Null,
631
+ [ConditionOperation.IS_NOT_EMPTY]: WorkflowVariableType.Null
413
632
  },
414
633
  [WorkflowVariableType.Integer]: {
415
- ["eq" /* EQ */]: WorkflowVariableType.Integer,
416
- ["neq" /* NEQ */]: WorkflowVariableType.Integer,
417
- ["gt" /* GT */]: WorkflowVariableType.Integer,
418
- ["gte" /* GTE */]: WorkflowVariableType.Integer,
419
- ["lt" /* LT */]: WorkflowVariableType.Integer,
420
- ["lte" /* LTE */]: WorkflowVariableType.Integer,
421
- ["in" /* IN */]: WorkflowVariableType.Array,
422
- ["nin" /* NIN */]: WorkflowVariableType.Array,
423
- ["is_empty" /* IS_EMPTY */]: WorkflowVariableType.Null,
424
- ["is_not_empty" /* IS_NOT_EMPTY */]: WorkflowVariableType.Null
634
+ [ConditionOperation.EQ]: WorkflowVariableType.Integer,
635
+ [ConditionOperation.NEQ]: WorkflowVariableType.Integer,
636
+ [ConditionOperation.GT]: WorkflowVariableType.Integer,
637
+ [ConditionOperation.GTE]: WorkflowVariableType.Integer,
638
+ [ConditionOperation.LT]: WorkflowVariableType.Integer,
639
+ [ConditionOperation.LTE]: WorkflowVariableType.Integer,
640
+ [ConditionOperation.IN]: WorkflowVariableType.Array,
641
+ [ConditionOperation.NIN]: WorkflowVariableType.Array,
642
+ [ConditionOperation.IS_EMPTY]: WorkflowVariableType.Null,
643
+ [ConditionOperation.IS_NOT_EMPTY]: WorkflowVariableType.Null
425
644
  },
426
645
  [WorkflowVariableType.Boolean]: {
427
- ["eq" /* EQ */]: WorkflowVariableType.Boolean,
428
- ["neq" /* NEQ */]: WorkflowVariableType.Boolean,
429
- ["is_true" /* IS_TRUE */]: WorkflowVariableType.Null,
430
- ["is_false" /* IS_FALSE */]: WorkflowVariableType.Null,
431
- ["in" /* IN */]: WorkflowVariableType.Array,
432
- ["nin" /* NIN */]: WorkflowVariableType.Array,
433
- ["is_empty" /* IS_EMPTY */]: WorkflowVariableType.Null,
434
- ["is_not_empty" /* IS_NOT_EMPTY */]: WorkflowVariableType.Null
646
+ [ConditionOperation.EQ]: WorkflowVariableType.Boolean,
647
+ [ConditionOperation.NEQ]: WorkflowVariableType.Boolean,
648
+ [ConditionOperation.IS_TRUE]: WorkflowVariableType.Null,
649
+ [ConditionOperation.IS_FALSE]: WorkflowVariableType.Null,
650
+ [ConditionOperation.IN]: WorkflowVariableType.Array,
651
+ [ConditionOperation.NIN]: WorkflowVariableType.Array,
652
+ [ConditionOperation.IS_EMPTY]: WorkflowVariableType.Null,
653
+ [ConditionOperation.IS_NOT_EMPTY]: WorkflowVariableType.Null
435
654
  },
436
655
  [WorkflowVariableType.Object]: {
437
- ["is_empty" /* IS_EMPTY */]: WorkflowVariableType.Null,
438
- ["is_not_empty" /* IS_NOT_EMPTY */]: WorkflowVariableType.Null
656
+ [ConditionOperation.IS_EMPTY]: WorkflowVariableType.Null,
657
+ [ConditionOperation.IS_NOT_EMPTY]: WorkflowVariableType.Null
439
658
  },
440
659
  [WorkflowVariableType.Array]: {
441
- ["is_empty" /* IS_EMPTY */]: WorkflowVariableType.Null,
442
- ["is_not_empty" /* IS_NOT_EMPTY */]: WorkflowVariableType.Null
660
+ [ConditionOperation.IS_EMPTY]: WorkflowVariableType.Null,
661
+ [ConditionOperation.IS_NOT_EMPTY]: WorkflowVariableType.Null
443
662
  },
444
663
  [WorkflowVariableType.Null]: {
445
- ["eq" /* EQ */]: WorkflowVariableType.Null,
446
- ["is_empty" /* IS_EMPTY */]: WorkflowVariableType.Null,
447
- ["is_not_empty" /* IS_NOT_EMPTY */]: WorkflowVariableType.Null
664
+ [ConditionOperation.EQ]: WorkflowVariableType.Null,
665
+ [ConditionOperation.IS_EMPTY]: WorkflowVariableType.Null,
666
+ [ConditionOperation.IS_NOT_EMPTY]: WorkflowVariableType.Null
448
667
  }
449
668
  };
450
669
 
@@ -453,34 +672,34 @@ var import_lodash_es3 = require("lodash-es");
453
672
  var conditionStringHandler = (condition) => {
454
673
  const { operator } = condition;
455
674
  const leftValue = condition.leftValue;
456
- if (operator === "eq" /* EQ */) {
675
+ if (operator === ConditionOperation.EQ) {
457
676
  const rightValue = condition.rightValue;
458
677
  return leftValue === rightValue;
459
678
  }
460
- if (operator === "neq" /* NEQ */) {
679
+ if (operator === ConditionOperation.NEQ) {
461
680
  const rightValue = condition.rightValue;
462
681
  return leftValue !== rightValue;
463
682
  }
464
- if (operator === "contains" /* CONTAINS */) {
683
+ if (operator === ConditionOperation.CONTAINS) {
465
684
  const rightValue = condition.rightValue;
466
685
  return leftValue.includes(rightValue);
467
686
  }
468
- if (operator === "not_contains" /* NOT_CONTAINS */) {
687
+ if (operator === ConditionOperation.NOT_CONTAINS) {
469
688
  const rightValue = condition.rightValue;
470
689
  return !leftValue.includes(rightValue);
471
690
  }
472
- if (operator === "in" /* IN */) {
691
+ if (operator === ConditionOperation.IN) {
473
692
  const rightValue = condition.rightValue;
474
693
  return rightValue.includes(leftValue);
475
694
  }
476
- if (operator === "nin" /* NIN */) {
695
+ if (operator === ConditionOperation.NIN) {
477
696
  const rightValue = condition.rightValue;
478
697
  return !rightValue.includes(leftValue);
479
698
  }
480
- if (operator === "is_empty" /* IS_EMPTY */) {
699
+ if (operator === ConditionOperation.IS_EMPTY) {
481
700
  return (0, import_lodash_es3.isNil)(leftValue);
482
701
  }
483
- if (operator === "is_not_empty" /* IS_NOT_EMPTY */) {
702
+ if (operator === ConditionOperation.IS_NOT_EMPTY) {
484
703
  return !(0, import_lodash_es3.isNil)(leftValue);
485
704
  }
486
705
  return false;
@@ -491,10 +710,10 @@ var import_lodash_es4 = require("lodash-es");
491
710
  var conditionObjectHandler = (condition) => {
492
711
  const { operator } = condition;
493
712
  const leftValue = condition.leftValue;
494
- if (operator === "is_empty" /* IS_EMPTY */) {
713
+ if (operator === ConditionOperation.IS_EMPTY) {
495
714
  return (0, import_lodash_es4.isNil)(leftValue);
496
715
  }
497
- if (operator === "is_not_empty" /* IS_NOT_EMPTY */) {
716
+ if (operator === ConditionOperation.IS_NOT_EMPTY) {
498
717
  return !(0, import_lodash_es4.isNil)(leftValue);
499
718
  }
500
719
  return false;
@@ -505,42 +724,42 @@ var import_lodash_es5 = require("lodash-es");
505
724
  var conditionNumberHandler = (condition) => {
506
725
  const { operator } = condition;
507
726
  const leftValue = condition.leftValue;
508
- if (operator === "eq" /* EQ */) {
727
+ if (operator === ConditionOperation.EQ) {
509
728
  const rightValue = condition.rightValue;
510
729
  return leftValue === rightValue;
511
730
  }
512
- if (operator === "neq" /* NEQ */) {
731
+ if (operator === ConditionOperation.NEQ) {
513
732
  const rightValue = condition.rightValue;
514
733
  return leftValue !== rightValue;
515
734
  }
516
- if (operator === "gt" /* GT */) {
735
+ if (operator === ConditionOperation.GT) {
517
736
  const rightValue = condition.rightValue;
518
737
  return leftValue > rightValue;
519
738
  }
520
- if (operator === "gte" /* GTE */) {
739
+ if (operator === ConditionOperation.GTE) {
521
740
  const rightValue = condition.rightValue;
522
741
  return leftValue >= rightValue;
523
742
  }
524
- if (operator === "lt" /* LT */) {
743
+ if (operator === ConditionOperation.LT) {
525
744
  const rightValue = condition.rightValue;
526
745
  return leftValue < rightValue;
527
746
  }
528
- if (operator === "lte" /* LTE */) {
747
+ if (operator === ConditionOperation.LTE) {
529
748
  const rightValue = condition.rightValue;
530
749
  return leftValue <= rightValue;
531
750
  }
532
- if (operator === "in" /* IN */) {
751
+ if (operator === ConditionOperation.IN) {
533
752
  const rightValue = condition.rightValue;
534
753
  return rightValue.includes(leftValue);
535
754
  }
536
- if (operator === "nin" /* NIN */) {
755
+ if (operator === ConditionOperation.NIN) {
537
756
  const rightValue = condition.rightValue;
538
757
  return !rightValue.includes(leftValue);
539
758
  }
540
- if (operator === "is_empty" /* IS_EMPTY */) {
759
+ if (operator === ConditionOperation.IS_EMPTY) {
541
760
  return (0, import_lodash_es5.isNil)(leftValue);
542
761
  }
543
- if (operator === "is_not_empty" /* IS_NOT_EMPTY */) {
762
+ if (operator === ConditionOperation.IS_NOT_EMPTY) {
544
763
  return !(0, import_lodash_es5.isNil)(leftValue);
545
764
  }
546
765
  return false;
@@ -551,13 +770,13 @@ var import_lodash_es6 = require("lodash-es");
551
770
  var conditionNullHandler = (condition) => {
552
771
  const { operator } = condition;
553
772
  const leftValue = condition.leftValue;
554
- if (operator === "eq" /* EQ */) {
773
+ if (operator === ConditionOperation.EQ) {
555
774
  return (0, import_lodash_es6.isNil)(leftValue) && (0, import_lodash_es6.isNil)(condition.rightValue);
556
775
  }
557
- if (operator === "is_empty" /* IS_EMPTY */) {
776
+ if (operator === ConditionOperation.IS_EMPTY) {
558
777
  return (0, import_lodash_es6.isNil)(leftValue);
559
778
  }
560
- if (operator === "is_not_empty" /* IS_NOT_EMPTY */) {
779
+ if (operator === ConditionOperation.IS_NOT_EMPTY) {
561
780
  return !(0, import_lodash_es6.isNil)(leftValue);
562
781
  }
563
782
  return false;
@@ -568,32 +787,32 @@ var import_lodash_es7 = require("lodash-es");
568
787
  var conditionBooleanHandler = (condition) => {
569
788
  const { operator } = condition;
570
789
  const leftValue = condition.leftValue;
571
- if (operator === "eq" /* EQ */) {
790
+ if (operator === ConditionOperation.EQ) {
572
791
  const rightValue = condition.rightValue;
573
792
  return leftValue === rightValue;
574
793
  }
575
- if (operator === "neq" /* NEQ */) {
794
+ if (operator === ConditionOperation.NEQ) {
576
795
  const rightValue = condition.rightValue;
577
796
  return leftValue !== rightValue;
578
797
  }
579
- if (operator === "is_true" /* IS_TRUE */) {
798
+ if (operator === ConditionOperation.IS_TRUE) {
580
799
  return leftValue === true;
581
800
  }
582
- if (operator === "is_false" /* IS_FALSE */) {
801
+ if (operator === ConditionOperation.IS_FALSE) {
583
802
  return leftValue === false;
584
803
  }
585
- if (operator === "in" /* IN */) {
804
+ if (operator === ConditionOperation.IN) {
586
805
  const rightValue = condition.rightValue;
587
806
  return rightValue.includes(leftValue);
588
807
  }
589
- if (operator === "nin" /* NIN */) {
808
+ if (operator === ConditionOperation.NIN) {
590
809
  const rightValue = condition.rightValue;
591
810
  return !rightValue.includes(leftValue);
592
811
  }
593
- if (operator === "is_empty" /* IS_EMPTY */) {
812
+ if (operator === ConditionOperation.IS_EMPTY) {
594
813
  return (0, import_lodash_es7.isNil)(leftValue);
595
814
  }
596
- if (operator === "is_not_empty" /* IS_NOT_EMPTY */) {
815
+ if (operator === ConditionOperation.IS_NOT_EMPTY) {
597
816
  return !(0, import_lodash_es7.isNil)(leftValue);
598
817
  }
599
818
  return false;
@@ -604,10 +823,10 @@ var import_lodash_es8 = require("lodash-es");
604
823
  var conditionArrayHandler = (condition) => {
605
824
  const { operator } = condition;
606
825
  const leftValue = condition.leftValue;
607
- if (operator === "is_empty" /* IS_EMPTY */) {
826
+ if (operator === ConditionOperation.IS_EMPTY) {
608
827
  return (0, import_lodash_es8.isNil)(leftValue);
609
828
  }
610
- if (operator === "is_not_empty" /* IS_NOT_EMPTY */) {
829
+ if (operator === ConditionOperation.IS_NOT_EMPTY) {
611
830
  return !(0, import_lodash_es8.isNil)(leftValue);
612
831
  }
613
832
  return false;
@@ -639,9 +858,7 @@ var ConditionExecutor = class {
639
858
  const parsedConditions = conditions.map((item) => this.parseCondition(item, context)).filter((item) => this.checkCondition(item));
640
859
  const activatedCondition = parsedConditions.find((item) => this.handleCondition(item));
641
860
  if (!activatedCondition) {
642
- return {
643
- outputs: {}
644
- };
861
+ throw new Error("no condition is activated");
645
862
  }
646
863
  return {
647
864
  outputs: {},
@@ -696,7 +913,9 @@ var WorkflowRuntimeNodeExecutors = [
696
913
  EndExecutor,
697
914
  LLMExecutor,
698
915
  ConditionExecutor,
699
- LoopExecutor
916
+ LoopExecutor,
917
+ BlockStartExecutor,
918
+ BlockEndExecutor
700
919
  ];
701
920
 
702
921
  // src/domain/validation/index.ts
@@ -730,52 +949,6 @@ var WorkflowRuntimeExecutor = class {
730
949
  }
731
950
  };
732
951
 
733
- // src/infrastructure/utils/uuid.ts
734
- var import_uuid = require("uuid");
735
- var uuid = import_uuid.v4;
736
-
737
- // src/infrastructure/utils/runtime-type.ts
738
- var WorkflowRuntimeType;
739
- ((WorkflowRuntimeType2) => {
740
- WorkflowRuntimeType2.getWorkflowType = (value) => {
741
- if (value === null || value === void 0) {
742
- return WorkflowVariableType.Null;
743
- }
744
- if (typeof value === "string") {
745
- return WorkflowVariableType.String;
746
- }
747
- if (typeof value === "boolean") {
748
- return WorkflowVariableType.Boolean;
749
- }
750
- if (typeof value === "number") {
751
- if (Number.isInteger(value)) {
752
- return WorkflowVariableType.Integer;
753
- }
754
- return WorkflowVariableType.Number;
755
- }
756
- if (Array.isArray(value)) {
757
- return WorkflowVariableType.Array;
758
- }
759
- if (typeof value === "object") {
760
- return WorkflowVariableType.Object;
761
- }
762
- return null;
763
- };
764
- WorkflowRuntimeType2.isMatchWorkflowType = (value, type) => {
765
- const workflowType = (0, WorkflowRuntimeType2.getWorkflowType)(value);
766
- if (!workflowType) {
767
- return false;
768
- }
769
- return workflowType === type;
770
- };
771
- WorkflowRuntimeType2.isTypeEqual = (leftType, rightType) => {
772
- if (leftType === WorkflowVariableType.Number && rightType === WorkflowVariableType.Integer || leftType === WorkflowVariableType.Integer && rightType === WorkflowVariableType.Number) {
773
- return true;
774
- }
775
- return leftType === rightType;
776
- };
777
- })(WorkflowRuntimeType || (WorkflowRuntimeType = {}));
778
-
779
952
  // src/domain/task/index.ts
780
953
  var WorkflowRuntimeTask = class _WorkflowRuntimeTask {
781
954
  constructor(params) {
@@ -795,6 +968,85 @@ var WorkflowRuntimeTask = class _WorkflowRuntimeTask {
795
968
  }
796
969
  };
797
970
 
971
+ // src/domain/message/message-value-object/index.ts
972
+ var WorkflowRuntimeMessage;
973
+ ((WorkflowRuntimeMessage2) => {
974
+ WorkflowRuntimeMessage2.create = (params) => {
975
+ const message = {
976
+ id: uuid(),
977
+ ...params
978
+ };
979
+ if (!params.timestamp) {
980
+ message.timestamp = Date.now();
981
+ }
982
+ return message;
983
+ };
984
+ })(WorkflowRuntimeMessage || (WorkflowRuntimeMessage = {}));
985
+
986
+ // src/domain/message/message-center/index.ts
987
+ var WorkflowRuntimeMessageCenter = class {
988
+ init() {
989
+ this.messages = {
990
+ [WorkflowMessageType.Log]: [],
991
+ [WorkflowMessageType.Info]: [],
992
+ [WorkflowMessageType.Debug]: [],
993
+ [WorkflowMessageType.Error]: [],
994
+ [WorkflowMessageType.Warn]: []
995
+ };
996
+ }
997
+ dispose() {
998
+ }
999
+ log(data) {
1000
+ const message = WorkflowRuntimeMessage.create({
1001
+ type: WorkflowMessageType.Log,
1002
+ ...data
1003
+ });
1004
+ this.messages[WorkflowMessageType.Log].push(message);
1005
+ return message;
1006
+ }
1007
+ info(data) {
1008
+ const message = WorkflowRuntimeMessage.create({
1009
+ type: WorkflowMessageType.Info,
1010
+ ...data
1011
+ });
1012
+ this.messages[WorkflowMessageType.Info].push(message);
1013
+ return message;
1014
+ }
1015
+ debug(data) {
1016
+ const message = WorkflowRuntimeMessage.create({
1017
+ type: WorkflowMessageType.Debug,
1018
+ ...data
1019
+ });
1020
+ this.messages[WorkflowMessageType.Debug].push(message);
1021
+ return message;
1022
+ }
1023
+ error(data) {
1024
+ const message = WorkflowRuntimeMessage.create({
1025
+ type: WorkflowMessageType.Error,
1026
+ ...data
1027
+ });
1028
+ this.messages[WorkflowMessageType.Error].push(message);
1029
+ return message;
1030
+ }
1031
+ warn(data) {
1032
+ const message = WorkflowRuntimeMessage.create({
1033
+ type: WorkflowMessageType.Warn,
1034
+ ...data
1035
+ });
1036
+ this.messages[WorkflowMessageType.Warn].push(message);
1037
+ return message;
1038
+ }
1039
+ export() {
1040
+ return {
1041
+ [WorkflowMessageType.Log]: this.messages[WorkflowMessageType.Log].slice(),
1042
+ [WorkflowMessageType.Info]: this.messages[WorkflowMessageType.Info].slice(),
1043
+ [WorkflowMessageType.Debug]: this.messages[WorkflowMessageType.Debug].slice(),
1044
+ [WorkflowMessageType.Error]: this.messages[WorkflowMessageType.Error].slice(),
1045
+ [WorkflowMessageType.Warn]: this.messages[WorkflowMessageType.Warn].slice()
1046
+ };
1047
+ }
1048
+ };
1049
+
798
1050
  // src/domain/variable/variable-store/index.ts
799
1051
  var import_lodash_es10 = require("lodash-es");
800
1052
 
@@ -1144,7 +1396,7 @@ var WorkflowRuntimeSnapshot = class _WorkflowRuntimeSnapshot {
1144
1396
  this.id = uuid();
1145
1397
  this.data = data;
1146
1398
  }
1147
- addData(data) {
1399
+ update(data) {
1148
1400
  Object.assign(this.data, data);
1149
1401
  }
1150
1402
  validate() {
@@ -1205,10 +1457,11 @@ var WorkflowRuntimeReport;
1205
1457
 
1206
1458
  // src/domain/report/reporter/index.ts
1207
1459
  var WorkflowRuntimeReporter = class {
1208
- constructor(ioCenter, snapshotCenter, statusCenter) {
1460
+ constructor(ioCenter, snapshotCenter, statusCenter, messageCenter) {
1209
1461
  this.ioCenter = ioCenter;
1210
1462
  this.snapshotCenter = snapshotCenter;
1211
1463
  this.statusCenter = statusCenter;
1464
+ this.messageCenter = messageCenter;
1212
1465
  }
1213
1466
  init() {
1214
1467
  }
@@ -1219,7 +1472,8 @@ var WorkflowRuntimeReporter = class {
1219
1472
  inputs: this.ioCenter.inputs,
1220
1473
  outputs: this.ioCenter.outputs,
1221
1474
  workflowStatus: this.statusCenter.workflow.export(),
1222
- reports: this.nodeReports()
1475
+ reports: this.nodeReports(),
1476
+ messages: this.messageCenter.export()
1223
1477
  });
1224
1478
  return report;
1225
1479
  }
@@ -1357,6 +1611,12 @@ var WorkflowRuntimeNode = class {
1357
1611
  get next() {
1358
1612
  return this._next;
1359
1613
  }
1614
+ get successors() {
1615
+ return traverseNodes(this, (node) => node.next);
1616
+ }
1617
+ get predecessors() {
1618
+ return traverseNodes(this, (node) => node.prev);
1619
+ }
1360
1620
  get isBranch() {
1361
1621
  return this.ports.outputs.length > 1;
1362
1622
  }
@@ -1594,6 +1854,7 @@ var WorkflowRuntimeContext = class _WorkflowRuntimeContext {
1594
1854
  this.ioCenter = data.ioCenter;
1595
1855
  this.snapshotCenter = data.snapshotCenter;
1596
1856
  this.statusCenter = data.statusCenter;
1857
+ this.messageCenter = data.messageCenter;
1597
1858
  this.reporter = data.reporter;
1598
1859
  }
1599
1860
  init(params) {
@@ -1604,6 +1865,7 @@ var WorkflowRuntimeContext = class _WorkflowRuntimeContext {
1604
1865
  this.ioCenter.init(inputs);
1605
1866
  this.snapshotCenter.init();
1606
1867
  this.statusCenter.init();
1868
+ this.messageCenter.init();
1607
1869
  this.reporter.init();
1608
1870
  }
1609
1871
  dispose() {
@@ -1617,6 +1879,7 @@ var WorkflowRuntimeContext = class _WorkflowRuntimeContext {
1617
1879
  this.ioCenter.dispose();
1618
1880
  this.snapshotCenter.dispose();
1619
1881
  this.statusCenter.dispose();
1882
+ this.messageCenter.dispose();
1620
1883
  this.reporter.dispose();
1621
1884
  }
1622
1885
  sub() {
@@ -1628,6 +1891,7 @@ var WorkflowRuntimeContext = class _WorkflowRuntimeContext {
1628
1891
  ioCenter: this.ioCenter,
1629
1892
  snapshotCenter: this.snapshotCenter,
1630
1893
  statusCenter: this.statusCenter,
1894
+ messageCenter: this.messageCenter,
1631
1895
  reporter: this.reporter,
1632
1896
  variableStore,
1633
1897
  state
@@ -1645,7 +1909,13 @@ var WorkflowRuntimeContext = class _WorkflowRuntimeContext {
1645
1909
  const ioCenter = new WorkflowRuntimeIOCenter();
1646
1910
  const snapshotCenter = new WorkflowRuntimeSnapshotCenter();
1647
1911
  const statusCenter = new WorkflowRuntimeStatusCenter();
1648
- const reporter = new WorkflowRuntimeReporter(ioCenter, snapshotCenter, statusCenter);
1912
+ const messageCenter = new WorkflowRuntimeMessageCenter();
1913
+ const reporter = new WorkflowRuntimeReporter(
1914
+ ioCenter,
1915
+ snapshotCenter,
1916
+ statusCenter,
1917
+ messageCenter
1918
+ );
1649
1919
  return new _WorkflowRuntimeContext({
1650
1920
  document,
1651
1921
  variableStore,
@@ -1653,6 +1923,7 @@ var WorkflowRuntimeContext = class _WorkflowRuntimeContext {
1653
1923
  ioCenter,
1654
1924
  snapshotCenter,
1655
1925
  statusCenter,
1926
+ messageCenter,
1656
1927
  reporter
1657
1928
  });
1658
1929
  }
@@ -1681,11 +1952,14 @@ var WorkflowRuntimeEngine = class {
1681
1952
  return;
1682
1953
  }
1683
1954
  context.statusCenter.nodeStatus(node.id).process();
1955
+ const snapshot = context.snapshotCenter.create({
1956
+ nodeID: node.id,
1957
+ data: node.data
1958
+ });
1959
+ let nextNodes = [];
1684
1960
  try {
1685
1961
  const inputs = context.state.getNodeInputs(node);
1686
- const snapshot = context.snapshotCenter.create({
1687
- nodeID: node.id,
1688
- data: node.data,
1962
+ snapshot.update({
1689
1963
  inputs
1690
1964
  });
1691
1965
  const result = await this.executor.execute({
@@ -1698,17 +1972,23 @@ var WorkflowRuntimeEngine = class {
1698
1972
  return;
1699
1973
  }
1700
1974
  const { outputs, branch } = result;
1701
- snapshot.addData({ outputs, branch });
1975
+ snapshot.update({ outputs, branch });
1702
1976
  context.state.setNodeOutputs({ node, outputs });
1703
1977
  context.state.addExecutedNode(node);
1704
1978
  context.statusCenter.nodeStatus(node.id).success();
1705
- const nextNodes = this.getNextNodes({ node, branch, context });
1706
- await this.executeNext({ node, nextNodes, context });
1979
+ nextNodes = this.getNextNodes({ node, branch, context });
1707
1980
  } catch (e) {
1981
+ const errorMessage = e instanceof Error ? e.message : "An unknown error occurred";
1982
+ snapshot.update({ error: errorMessage });
1983
+ context.messageCenter.error({
1984
+ nodeID: node.id,
1985
+ message: errorMessage
1986
+ });
1708
1987
  context.statusCenter.nodeStatus(node.id).fail();
1709
1988
  console.error(e);
1710
- return;
1989
+ throw e;
1711
1990
  }
1991
+ await this.executeNext({ node, nextNodes, context });
1712
1992
  }
1713
1993
  async process(context) {
1714
1994
  const startNode = context.document.start;
@@ -1720,7 +2000,7 @@ var WorkflowRuntimeEngine = class {
1720
2000
  return outputs;
1721
2001
  } catch (e) {
1722
2002
  context.statusCenter.workflow.fail();
1723
- throw e;
2003
+ return {};
1724
2004
  }
1725
2005
  }
1726
2006
  canExecuteNode(params) {
@@ -1744,8 +2024,11 @@ var WorkflowRuntimeEngine = class {
1744
2024
  const nextNodeIDs = new Set(targetPort.edges.map((edge) => edge.to.id));
1745
2025
  const nextNodes = allNextNodes.filter((nextNode) => nextNodeIDs.has(nextNode.id));
1746
2026
  const skipNodes = allNextNodes.filter((nextNode) => !nextNodeIDs.has(nextNode.id));
1747
- skipNodes.forEach((skipNode) => {
1748
- context.state.addExecutedNode(skipNode);
2027
+ const nextGroups = nextNodes.map((nextNode) => [nextNode, ...nextNode.successors]);
2028
+ const skipGroups = skipNodes.map((skipNode) => [skipNode, ...skipNode.successors]);
2029
+ const { uniqueToB: skippedNodes } = compareNodeGroups(nextGroups, skipGroups);
2030
+ skippedNodes.forEach((node2) => {
2031
+ context.state.addExecutedNode(node2);
1749
2032
  });
1750
2033
  return nextNodes;
1751
2034
  }