@flowgram.ai/runtime-js 0.1.8

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 ADDED
@@ -0,0 +1,1896 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ TaskCancelAPI: () => TaskCancelAPI,
34
+ TaskReportAPI: () => TaskReportAPI,
35
+ TaskResultAPI: () => TaskResultAPI,
36
+ TaskRunAPI: () => TaskRunAPI,
37
+ WorkflowRuntimeAPIs: () => WorkflowRuntimeAPIs
38
+ });
39
+ module.exports = __toCommonJS(src_exports);
40
+
41
+ // ../interface/dist/esm/index.js
42
+ var import_zod = __toESM(require("zod"));
43
+ var import_zod2 = __toESM(require("zod"));
44
+ var import_zod3 = __toESM(require("zod"));
45
+ var import_zod4 = __toESM(require("zod"));
46
+ var import_zod5 = __toESM(require("zod"));
47
+ var import_zod6 = __toESM(require("zod"));
48
+ var import_zod7 = __toESM(require("zod"));
49
+ var FlowGramAPIName = /* @__PURE__ */ ((FlowGramAPIName2) => {
50
+ FlowGramAPIName2["ServerInfo"] = "ServerInfo";
51
+ FlowGramAPIName2["TaskRun"] = "TaskRun";
52
+ FlowGramAPIName2["TaskReport"] = "TaskReport";
53
+ FlowGramAPIName2["TaskResult"] = "TaskResult";
54
+ FlowGramAPIName2["TaskCancel"] = "TaskCancel";
55
+ FlowGramAPIName2["Validation"] = "Validation";
56
+ return FlowGramAPIName2;
57
+ })(FlowGramAPIName || {});
58
+ var ValidationDefine = {
59
+ name: "Validation",
60
+ method: "POST",
61
+ path: "/validation",
62
+ module: "Validation",
63
+ schema: {
64
+ input: import_zod.default.object({
65
+ schema: import_zod.default.string()
66
+ }),
67
+ output: import_zod.default.object({
68
+ valid: import_zod.default.boolean(),
69
+ nodeErrors: import_zod.default.array(
70
+ import_zod.default.object({
71
+ message: import_zod.default.string(),
72
+ nodeID: import_zod.default.string()
73
+ })
74
+ ),
75
+ edgeErrors: import_zod.default.array(
76
+ import_zod.default.object({
77
+ message: import_zod.default.string(),
78
+ edge: import_zod.default.object({
79
+ sourceNodeID: import_zod.default.string(),
80
+ targetNodeID: import_zod.default.string(),
81
+ sourcePortID: import_zod.default.string().optional(),
82
+ targetPortID: import_zod.default.string().optional()
83
+ })
84
+ })
85
+ )
86
+ })
87
+ }
88
+ };
89
+ var WorkflowIOZodSchema = import_zod3.default.record(import_zod3.default.string(), import_zod3.default.any());
90
+ var WorkflowSnapshotZodSchema = import_zod3.default.object({
91
+ id: import_zod3.default.string(),
92
+ nodeID: import_zod3.default.string(),
93
+ inputs: WorkflowIOZodSchema,
94
+ outputs: WorkflowIOZodSchema.optional(),
95
+ data: WorkflowIOZodSchema,
96
+ branch: import_zod3.default.string().optional()
97
+ });
98
+ var WorkflowStatusZodShape = {
99
+ status: import_zod3.default.string(),
100
+ terminated: import_zod3.default.boolean(),
101
+ startTime: import_zod3.default.number(),
102
+ endTime: import_zod3.default.number().optional(),
103
+ timeCost: import_zod3.default.number()
104
+ };
105
+ var WorkflowStatusZodSchema = import_zod3.default.object(WorkflowStatusZodShape);
106
+ var WorkflowZodSchema = {
107
+ Inputs: WorkflowIOZodSchema,
108
+ Outputs: WorkflowIOZodSchema,
109
+ Status: WorkflowStatusZodSchema,
110
+ Snapshot: WorkflowSnapshotZodSchema,
111
+ NodeReport: import_zod3.default.object({
112
+ id: import_zod3.default.string(),
113
+ ...WorkflowStatusZodShape,
114
+ snapshots: import_zod3.default.array(WorkflowSnapshotZodSchema)
115
+ })
116
+ };
117
+ var TaskRunDefine = {
118
+ name: "TaskRun",
119
+ method: "POST",
120
+ path: "/task/run",
121
+ module: "Task",
122
+ schema: {
123
+ input: import_zod2.default.object({
124
+ schema: import_zod2.default.string(),
125
+ inputs: WorkflowZodSchema.Inputs
126
+ }),
127
+ output: import_zod2.default.object({
128
+ taskID: import_zod2.default.string()
129
+ })
130
+ }
131
+ };
132
+ var TaskResultDefine = {
133
+ name: "TaskResult",
134
+ method: "GET",
135
+ path: "/task/result",
136
+ module: "Task",
137
+ schema: {
138
+ input: import_zod4.default.object({
139
+ taskID: import_zod4.default.string()
140
+ }),
141
+ output: WorkflowZodSchema.Outputs
142
+ }
143
+ };
144
+ var TaskReportDefine = {
145
+ name: "TaskReport",
146
+ method: "GET",
147
+ path: "/task/report",
148
+ module: "Task",
149
+ schema: {
150
+ input: import_zod5.default.object({
151
+ taskID: import_zod5.default.string()
152
+ }),
153
+ output: import_zod5.default.object({
154
+ id: import_zod5.default.string(),
155
+ inputs: WorkflowZodSchema.Inputs,
156
+ outputs: WorkflowZodSchema.Outputs,
157
+ workflowStatus: WorkflowZodSchema.Status,
158
+ reports: import_zod5.default.record(import_zod5.default.string(), WorkflowZodSchema.NodeReport)
159
+ })
160
+ }
161
+ };
162
+ var TaskCancelDefine = {
163
+ name: "TaskCancel",
164
+ method: "PUT",
165
+ path: "/task/cancel",
166
+ module: "Task",
167
+ schema: {
168
+ input: import_zod6.default.object({
169
+ taskID: import_zod6.default.string()
170
+ }),
171
+ output: import_zod6.default.object({
172
+ success: import_zod6.default.boolean()
173
+ })
174
+ }
175
+ };
176
+ var ServerInfoDefine = {
177
+ name: "ServerInfo",
178
+ method: "GET",
179
+ path: "/info",
180
+ module: "Info",
181
+ schema: {
182
+ input: import_zod7.default.undefined(),
183
+ output: import_zod7.default.object({
184
+ name: import_zod7.default.string(),
185
+ runtime: import_zod7.default.string(),
186
+ version: import_zod7.default.string(),
187
+ time: import_zod7.default.string()
188
+ })
189
+ }
190
+ };
191
+ var FlowGramAPIs = {
192
+ [
193
+ "ServerInfo"
194
+ /* ServerInfo */
195
+ ]: ServerInfoDefine,
196
+ [
197
+ "TaskRun"
198
+ /* TaskRun */
199
+ ]: TaskRunDefine,
200
+ [
201
+ "TaskReport"
202
+ /* TaskReport */
203
+ ]: TaskReportDefine,
204
+ [
205
+ "TaskResult"
206
+ /* TaskResult */
207
+ ]: TaskResultDefine,
208
+ [
209
+ "TaskCancel"
210
+ /* TaskCancel */
211
+ ]: TaskCancelDefine,
212
+ [
213
+ "Validation"
214
+ /* Validation */
215
+ ]: ValidationDefine
216
+ };
217
+ var FlowGramAPINames = Object.keys(FlowGramAPIs);
218
+ var WorkflowPortType = /* @__PURE__ */ ((WorkflowPortType2) => {
219
+ WorkflowPortType2["Input"] = "input";
220
+ WorkflowPortType2["Output"] = "output";
221
+ return WorkflowPortType2;
222
+ })(WorkflowPortType || {});
223
+ var WorkflowVariableType = /* @__PURE__ */ ((WorkflowVariableType2) => {
224
+ WorkflowVariableType2["String"] = "string";
225
+ WorkflowVariableType2["Integer"] = "integer";
226
+ WorkflowVariableType2["Number"] = "number";
227
+ WorkflowVariableType2["Boolean"] = "boolean";
228
+ WorkflowVariableType2["Object"] = "object";
229
+ WorkflowVariableType2["Array"] = "array";
230
+ WorkflowVariableType2["Null"] = "null";
231
+ return WorkflowVariableType2;
232
+ })(WorkflowVariableType || {});
233
+ var FlowGramNode = /* @__PURE__ */ ((FlowGramNode22) => {
234
+ FlowGramNode22["Root"] = "root";
235
+ FlowGramNode22["Start"] = "start";
236
+ FlowGramNode22["End"] = "end";
237
+ FlowGramNode22["LLM"] = "llm";
238
+ FlowGramNode22["code"] = "code";
239
+ FlowGramNode22["Condition"] = "condition";
240
+ FlowGramNode22["Loop"] = "loop";
241
+ FlowGramNode22["Comment"] = "comment";
242
+ FlowGramNode22["Group"] = "group";
243
+ return FlowGramNode22;
244
+ })(FlowGramNode || {});
245
+ var IEngine = Symbol.for("Engine");
246
+ var IExecutor = Symbol.for("Executor");
247
+ var WorkflowStatus = /* @__PURE__ */ ((WorkflowStatus2) => {
248
+ WorkflowStatus2["Pending"] = "pending";
249
+ WorkflowStatus2["Processing"] = "processing";
250
+ WorkflowStatus2["Succeeded"] = "succeeded";
251
+ WorkflowStatus2["Failed"] = "failed";
252
+ WorkflowStatus2["Canceled"] = "canceled";
253
+ return WorkflowStatus2;
254
+ })(WorkflowStatus || {});
255
+ var IValidation = Symbol.for("Validation");
256
+
257
+ // src/nodes/start/index.ts
258
+ var StartExecutor = class {
259
+ constructor() {
260
+ this.type = FlowGramNode.Start;
261
+ }
262
+ async execute(context) {
263
+ return {
264
+ outputs: context.runtime.ioCenter.inputs
265
+ };
266
+ }
267
+ };
268
+
269
+ // src/nodes/loop/index.ts
270
+ var import_lodash_es = require("lodash-es");
271
+ var LoopExecutor = class {
272
+ constructor() {
273
+ this.type = FlowGramNode.Loop;
274
+ }
275
+ async execute(context) {
276
+ 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
+ const engine = context.container.get(IEngine);
282
+ 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
+ };
288
+ }
289
+ for (let i = 0; i < loopArray.length; i++) {
290
+ const loopItem = loopArray[i];
291
+ const subContext = context.runtime.sub();
292
+ subContext.variableStore.setVariable({
293
+ nodeID: `${loopNodeID}_locals`,
294
+ key: "item",
295
+ type: itemsType,
296
+ value: loopItem
297
+ });
298
+ await Promise.all(
299
+ startSubNodes.map(
300
+ (node) => engine.executeNode({
301
+ context: subContext,
302
+ node
303
+ })
304
+ )
305
+ );
306
+ }
307
+ return {
308
+ outputs: {}
309
+ };
310
+ }
311
+ checkLoopArray(loopArrayResult) {
312
+ const loopArray = loopArrayResult?.value;
313
+ if (!loopArray || (0, import_lodash_es.isNil)(loopArray) || !Array.isArray(loopArray)) {
314
+ throw new Error("batchFor is required");
315
+ }
316
+ const loopArrayType = loopArrayResult.type;
317
+ if (loopArrayType !== WorkflowVariableType.Array) {
318
+ throw new Error("batchFor must be an array");
319
+ }
320
+ const loopArrayItemType = loopArrayResult.itemsType;
321
+ if ((0, import_lodash_es.isNil)(loopArrayItemType)) {
322
+ throw new Error("batchFor items must be array items");
323
+ }
324
+ }
325
+ };
326
+
327
+ // src/nodes/llm/index.ts
328
+ var import_lodash_es2 = require("lodash-es");
329
+ var import_openai = require("@langchain/openai");
330
+ var import_messages = require("@langchain/core/messages");
331
+ var LLMExecutor = class {
332
+ constructor() {
333
+ this.type = FlowGramNode.LLM;
334
+ }
335
+ async execute(context) {
336
+ const inputs = context.inputs;
337
+ this.checkInputs(inputs);
338
+ const { modelName, temperature, apiKey, apiHost, systemPrompt, prompt } = inputs;
339
+ const model = new import_openai.ChatOpenAI({
340
+ modelName,
341
+ temperature,
342
+ apiKey,
343
+ configuration: {
344
+ baseURL: apiHost
345
+ }
346
+ });
347
+ const messages = [];
348
+ if (systemPrompt) {
349
+ messages.push(new import_messages.SystemMessage(systemPrompt));
350
+ }
351
+ messages.push(new import_messages.HumanMessage(prompt));
352
+ const apiMessage = await model.invoke(messages);
353
+ const result = apiMessage.content;
354
+ return {
355
+ outputs: {
356
+ result
357
+ }
358
+ };
359
+ }
360
+ checkInputs(inputs) {
361
+ const { modelName, temperature, apiKey, apiHost, prompt } = inputs;
362
+ const missingInputs = [];
363
+ if ((0, import_lodash_es2.isNil)(modelName)) missingInputs.push("modelName");
364
+ if ((0, import_lodash_es2.isNil)(temperature)) missingInputs.push("temperature");
365
+ if ((0, import_lodash_es2.isNil)(apiKey)) missingInputs.push("apiKey");
366
+ if ((0, import_lodash_es2.isNil)(apiHost)) missingInputs.push("apiHost");
367
+ if ((0, import_lodash_es2.isNil)(prompt)) missingInputs.push("prompt");
368
+ if (missingInputs.length > 0) {
369
+ throw new Error(`LLM node missing required inputs: ${missingInputs.join(", ")}`);
370
+ }
371
+ }
372
+ };
373
+
374
+ // src/nodes/end/index.ts
375
+ var EndExecutor = class {
376
+ constructor() {
377
+ this.type = FlowGramNode.End;
378
+ }
379
+ async execute(context) {
380
+ context.runtime.ioCenter.setOutputs(context.inputs);
381
+ return {
382
+ outputs: context.inputs
383
+ };
384
+ }
385
+ };
386
+
387
+ // src/nodes/condition/index.ts
388
+ var import_lodash_es9 = require("lodash-es");
389
+
390
+ // src/nodes/condition/rules.ts
391
+ var conditionRules = {
392
+ [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
401
+ },
402
+ [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
413
+ },
414
+ [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
425
+ },
426
+ [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
435
+ },
436
+ [WorkflowVariableType.Object]: {
437
+ ["is_empty" /* IS_EMPTY */]: WorkflowVariableType.Null,
438
+ ["is_not_empty" /* IS_NOT_EMPTY */]: WorkflowVariableType.Null
439
+ },
440
+ [WorkflowVariableType.Array]: {
441
+ ["is_empty" /* IS_EMPTY */]: WorkflowVariableType.Null,
442
+ ["is_not_empty" /* IS_NOT_EMPTY */]: WorkflowVariableType.Null
443
+ },
444
+ [WorkflowVariableType.Null]: {
445
+ ["eq" /* EQ */]: WorkflowVariableType.Null,
446
+ ["is_empty" /* IS_EMPTY */]: WorkflowVariableType.Null,
447
+ ["is_not_empty" /* IS_NOT_EMPTY */]: WorkflowVariableType.Null
448
+ }
449
+ };
450
+
451
+ // src/nodes/condition/handlers/string.ts
452
+ var import_lodash_es3 = require("lodash-es");
453
+ var conditionStringHandler = (condition) => {
454
+ const { operator } = condition;
455
+ const leftValue = condition.leftValue;
456
+ if (operator === "eq" /* EQ */) {
457
+ const rightValue = condition.rightValue;
458
+ return leftValue === rightValue;
459
+ }
460
+ if (operator === "neq" /* NEQ */) {
461
+ const rightValue = condition.rightValue;
462
+ return leftValue !== rightValue;
463
+ }
464
+ if (operator === "contains" /* CONTAINS */) {
465
+ const rightValue = condition.rightValue;
466
+ return leftValue.includes(rightValue);
467
+ }
468
+ if (operator === "not_contains" /* NOT_CONTAINS */) {
469
+ const rightValue = condition.rightValue;
470
+ return !leftValue.includes(rightValue);
471
+ }
472
+ if (operator === "in" /* IN */) {
473
+ const rightValue = condition.rightValue;
474
+ return rightValue.includes(leftValue);
475
+ }
476
+ if (operator === "nin" /* NIN */) {
477
+ const rightValue = condition.rightValue;
478
+ return !rightValue.includes(leftValue);
479
+ }
480
+ if (operator === "is_empty" /* IS_EMPTY */) {
481
+ return (0, import_lodash_es3.isNil)(leftValue);
482
+ }
483
+ if (operator === "is_not_empty" /* IS_NOT_EMPTY */) {
484
+ return !(0, import_lodash_es3.isNil)(leftValue);
485
+ }
486
+ return false;
487
+ };
488
+
489
+ // src/nodes/condition/handlers/object.ts
490
+ var import_lodash_es4 = require("lodash-es");
491
+ var conditionObjectHandler = (condition) => {
492
+ const { operator } = condition;
493
+ const leftValue = condition.leftValue;
494
+ if (operator === "is_empty" /* IS_EMPTY */) {
495
+ return (0, import_lodash_es4.isNil)(leftValue);
496
+ }
497
+ if (operator === "is_not_empty" /* IS_NOT_EMPTY */) {
498
+ return !(0, import_lodash_es4.isNil)(leftValue);
499
+ }
500
+ return false;
501
+ };
502
+
503
+ // src/nodes/condition/handlers/number.ts
504
+ var import_lodash_es5 = require("lodash-es");
505
+ var conditionNumberHandler = (condition) => {
506
+ const { operator } = condition;
507
+ const leftValue = condition.leftValue;
508
+ if (operator === "eq" /* EQ */) {
509
+ const rightValue = condition.rightValue;
510
+ return leftValue === rightValue;
511
+ }
512
+ if (operator === "neq" /* NEQ */) {
513
+ const rightValue = condition.rightValue;
514
+ return leftValue !== rightValue;
515
+ }
516
+ if (operator === "gt" /* GT */) {
517
+ const rightValue = condition.rightValue;
518
+ return leftValue > rightValue;
519
+ }
520
+ if (operator === "gte" /* GTE */) {
521
+ const rightValue = condition.rightValue;
522
+ return leftValue >= rightValue;
523
+ }
524
+ if (operator === "lt" /* LT */) {
525
+ const rightValue = condition.rightValue;
526
+ return leftValue < rightValue;
527
+ }
528
+ if (operator === "lte" /* LTE */) {
529
+ const rightValue = condition.rightValue;
530
+ return leftValue <= rightValue;
531
+ }
532
+ if (operator === "in" /* IN */) {
533
+ const rightValue = condition.rightValue;
534
+ return rightValue.includes(leftValue);
535
+ }
536
+ if (operator === "nin" /* NIN */) {
537
+ const rightValue = condition.rightValue;
538
+ return !rightValue.includes(leftValue);
539
+ }
540
+ if (operator === "is_empty" /* IS_EMPTY */) {
541
+ return (0, import_lodash_es5.isNil)(leftValue);
542
+ }
543
+ if (operator === "is_not_empty" /* IS_NOT_EMPTY */) {
544
+ return !(0, import_lodash_es5.isNil)(leftValue);
545
+ }
546
+ return false;
547
+ };
548
+
549
+ // src/nodes/condition/handlers/null.ts
550
+ var import_lodash_es6 = require("lodash-es");
551
+ var conditionNullHandler = (condition) => {
552
+ const { operator } = condition;
553
+ const leftValue = condition.leftValue;
554
+ if (operator === "eq" /* EQ */) {
555
+ return (0, import_lodash_es6.isNil)(leftValue) && (0, import_lodash_es6.isNil)(condition.rightValue);
556
+ }
557
+ if (operator === "is_empty" /* IS_EMPTY */) {
558
+ return (0, import_lodash_es6.isNil)(leftValue);
559
+ }
560
+ if (operator === "is_not_empty" /* IS_NOT_EMPTY */) {
561
+ return !(0, import_lodash_es6.isNil)(leftValue);
562
+ }
563
+ return false;
564
+ };
565
+
566
+ // src/nodes/condition/handlers/boolean.ts
567
+ var import_lodash_es7 = require("lodash-es");
568
+ var conditionBooleanHandler = (condition) => {
569
+ const { operator } = condition;
570
+ const leftValue = condition.leftValue;
571
+ if (operator === "eq" /* EQ */) {
572
+ const rightValue = condition.rightValue;
573
+ return leftValue === rightValue;
574
+ }
575
+ if (operator === "neq" /* NEQ */) {
576
+ const rightValue = condition.rightValue;
577
+ return leftValue !== rightValue;
578
+ }
579
+ if (operator === "is_true" /* IS_TRUE */) {
580
+ return leftValue === true;
581
+ }
582
+ if (operator === "is_false" /* IS_FALSE */) {
583
+ return leftValue === false;
584
+ }
585
+ if (operator === "in" /* IN */) {
586
+ const rightValue = condition.rightValue;
587
+ return rightValue.includes(leftValue);
588
+ }
589
+ if (operator === "nin" /* NIN */) {
590
+ const rightValue = condition.rightValue;
591
+ return !rightValue.includes(leftValue);
592
+ }
593
+ if (operator === "is_empty" /* IS_EMPTY */) {
594
+ return (0, import_lodash_es7.isNil)(leftValue);
595
+ }
596
+ if (operator === "is_not_empty" /* IS_NOT_EMPTY */) {
597
+ return !(0, import_lodash_es7.isNil)(leftValue);
598
+ }
599
+ return false;
600
+ };
601
+
602
+ // src/nodes/condition/handlers/array.ts
603
+ var import_lodash_es8 = require("lodash-es");
604
+ var conditionArrayHandler = (condition) => {
605
+ const { operator } = condition;
606
+ const leftValue = condition.leftValue;
607
+ if (operator === "is_empty" /* IS_EMPTY */) {
608
+ return (0, import_lodash_es8.isNil)(leftValue);
609
+ }
610
+ if (operator === "is_not_empty" /* IS_NOT_EMPTY */) {
611
+ return !(0, import_lodash_es8.isNil)(leftValue);
612
+ }
613
+ return false;
614
+ };
615
+
616
+ // src/nodes/condition/handlers/index.ts
617
+ var conditionHandlers = {
618
+ [WorkflowVariableType.String]: conditionStringHandler,
619
+ [WorkflowVariableType.Number]: conditionNumberHandler,
620
+ [WorkflowVariableType.Integer]: conditionNumberHandler,
621
+ [WorkflowVariableType.Boolean]: conditionBooleanHandler,
622
+ [WorkflowVariableType.Object]: conditionObjectHandler,
623
+ [WorkflowVariableType.Array]: conditionArrayHandler,
624
+ [WorkflowVariableType.Null]: conditionNullHandler
625
+ };
626
+
627
+ // src/nodes/condition/index.ts
628
+ var ConditionExecutor = class {
629
+ constructor() {
630
+ this.type = FlowGramNode.Condition;
631
+ }
632
+ async execute(context) {
633
+ const conditions = context.node.data?.conditions;
634
+ if (!conditions) {
635
+ return {
636
+ outputs: {}
637
+ };
638
+ }
639
+ const parsedConditions = conditions.map((item) => this.parseCondition(item, context)).filter((item) => this.checkCondition(item));
640
+ const activatedCondition = parsedConditions.find((item) => this.handleCondition(item));
641
+ if (!activatedCondition) {
642
+ return {
643
+ outputs: {}
644
+ };
645
+ }
646
+ return {
647
+ outputs: {},
648
+ branch: activatedCondition.key
649
+ };
650
+ }
651
+ parseCondition(item, context) {
652
+ const { key, value } = item;
653
+ const { left, operator, right } = value;
654
+ const parsedLeft = context.runtime.state.parseRef(left);
655
+ const leftValue = parsedLeft?.value ?? null;
656
+ const leftType = parsedLeft?.type ?? WorkflowVariableType.Null;
657
+ const parsedRight = Boolean(right) ? context.runtime.state.parseValue(right) : null;
658
+ const rightValue = parsedRight?.value ?? null;
659
+ const rightType = parsedRight?.type ?? WorkflowVariableType.Null;
660
+ return {
661
+ key,
662
+ leftValue,
663
+ leftType,
664
+ rightValue,
665
+ rightType,
666
+ operator
667
+ };
668
+ }
669
+ checkCondition(condition) {
670
+ const rule = conditionRules[condition.leftType];
671
+ if ((0, import_lodash_es9.isNil)(rule)) {
672
+ throw new Error(`condition left type ${condition.leftType} is not supported`);
673
+ }
674
+ const ruleType = rule[condition.operator];
675
+ if ((0, import_lodash_es9.isNil)(ruleType)) {
676
+ throw new Error(`condition operator ${condition.operator} is not supported`);
677
+ }
678
+ if (ruleType !== condition.rightType) {
679
+ return false;
680
+ }
681
+ return true;
682
+ }
683
+ handleCondition(condition) {
684
+ const handler = conditionHandlers[condition.leftType];
685
+ if (!handler) {
686
+ throw new Error(`condition left type ${condition.leftType} is not supported`);
687
+ }
688
+ const isActive = handler(condition);
689
+ return isActive;
690
+ }
691
+ };
692
+
693
+ // src/nodes/index.ts
694
+ var WorkflowRuntimeNodeExecutors = [
695
+ StartExecutor,
696
+ EndExecutor,
697
+ LLMExecutor,
698
+ ConditionExecutor,
699
+ LoopExecutor
700
+ ];
701
+
702
+ // src/domain/validation/index.ts
703
+ var WorkflowRuntimeValidation = class {
704
+ validate(schema) {
705
+ return {
706
+ valid: true
707
+ };
708
+ }
709
+ };
710
+
711
+ // src/domain/executor/index.ts
712
+ var WorkflowRuntimeExecutor = class {
713
+ constructor(nodeExecutors) {
714
+ this.nodeExecutors = /* @__PURE__ */ new Map();
715
+ nodeExecutors.forEach((executor) => {
716
+ this.register(new executor());
717
+ });
718
+ }
719
+ register(executor) {
720
+ this.nodeExecutors.set(executor.type, executor);
721
+ }
722
+ async execute(context) {
723
+ const nodeType = context.node.type;
724
+ const nodeExecutor = this.nodeExecutors.get(nodeType);
725
+ if (!nodeExecutor) {
726
+ throw new Error(`no executor found for node type ${nodeType}`);
727
+ }
728
+ const output = await nodeExecutor.execute(context);
729
+ return output;
730
+ }
731
+ };
732
+
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
+ // src/domain/task/index.ts
780
+ var WorkflowRuntimeTask = class _WorkflowRuntimeTask {
781
+ constructor(params) {
782
+ this.id = uuid();
783
+ this.context = params.context;
784
+ this.processing = params.processing;
785
+ }
786
+ cancel() {
787
+ this.context.statusCenter.workflow.cancel();
788
+ const cancelNodeIDs = this.context.statusCenter.getStatusNodeIDs(WorkflowStatus.Processing);
789
+ cancelNodeIDs.forEach((nodeID) => {
790
+ this.context.statusCenter.nodeStatus(nodeID).cancel();
791
+ });
792
+ }
793
+ static create(params) {
794
+ return new _WorkflowRuntimeTask(params);
795
+ }
796
+ };
797
+
798
+ // src/domain/variable/variable-store/index.ts
799
+ var import_lodash_es10 = require("lodash-es");
800
+
801
+ // src/domain/variable/variable-value-object/index.ts
802
+ var WorkflowRuntimeVariable;
803
+ ((WorkflowRuntimeVariable2) => {
804
+ WorkflowRuntimeVariable2.create = (params) => ({
805
+ id: uuid(),
806
+ ...params
807
+ });
808
+ })(WorkflowRuntimeVariable || (WorkflowRuntimeVariable = {}));
809
+
810
+ // src/domain/variable/variable-store/index.ts
811
+ var WorkflowRuntimeVariableStore = class {
812
+ constructor() {
813
+ this.id = uuid();
814
+ }
815
+ init() {
816
+ this.store = /* @__PURE__ */ new Map();
817
+ }
818
+ dispose() {
819
+ this.store.clear();
820
+ }
821
+ setParent(parent) {
822
+ this.parent = parent;
823
+ }
824
+ globalGet(nodeID) {
825
+ const store = this.store.get(nodeID);
826
+ if (!store && this.parent) {
827
+ return this.parent.globalGet(nodeID);
828
+ }
829
+ return store;
830
+ }
831
+ setVariable(params) {
832
+ const { nodeID, key, value, type, itemsType } = params;
833
+ if (!this.store.has(nodeID)) {
834
+ this.store.set(nodeID, /* @__PURE__ */ new Map());
835
+ }
836
+ const nodeStore = this.store.get(nodeID);
837
+ const variable = WorkflowRuntimeVariable.create({
838
+ nodeID,
839
+ key,
840
+ value,
841
+ type,
842
+ // TODO check type
843
+ itemsType
844
+ // TODO check is array
845
+ });
846
+ nodeStore.set(key, variable);
847
+ }
848
+ setValue(params) {
849
+ const { nodeID, variableKey, variablePath, value } = params;
850
+ if (!this.store.has(nodeID)) {
851
+ this.store.set(nodeID, /* @__PURE__ */ new Map());
852
+ }
853
+ const nodeStore = this.store.get(nodeID);
854
+ if (!nodeStore.has(variableKey)) {
855
+ const variable2 = WorkflowRuntimeVariable.create({
856
+ nodeID,
857
+ key: variableKey,
858
+ value: {},
859
+ type: WorkflowVariableType.Object
860
+ });
861
+ nodeStore.set(variableKey, variable2);
862
+ }
863
+ const variable = nodeStore.get(variableKey);
864
+ if (!variablePath) {
865
+ variable.value = value;
866
+ return;
867
+ }
868
+ (0, import_lodash_es10.set)(variable.value, variablePath, value);
869
+ }
870
+ getValue(params) {
871
+ const { nodeID, variableKey, variablePath } = params;
872
+ const variable = this.globalGet(nodeID)?.get(variableKey);
873
+ if (!variable) {
874
+ return null;
875
+ }
876
+ if (!variablePath || variablePath.length === 0) {
877
+ return {
878
+ value: variable.value,
879
+ type: variable.type,
880
+ itemsType: variable.itemsType
881
+ };
882
+ }
883
+ const value = (0, import_lodash_es10.get)(variable.value, variablePath);
884
+ const type = WorkflowRuntimeType.getWorkflowType(value);
885
+ if (!type) {
886
+ return null;
887
+ }
888
+ if (type === WorkflowVariableType.Array && Array.isArray(value)) {
889
+ const itemsType = WorkflowRuntimeType.getWorkflowType(value[0]);
890
+ if (!itemsType) {
891
+ return null;
892
+ }
893
+ return {
894
+ value,
895
+ type,
896
+ itemsType
897
+ };
898
+ }
899
+ return {
900
+ value,
901
+ type
902
+ };
903
+ }
904
+ };
905
+
906
+ // src/domain/status/status-entity/index.ts
907
+ var WorkflowRuntimeStatus = class _WorkflowRuntimeStatus {
908
+ constructor() {
909
+ this.id = uuid();
910
+ this._status = WorkflowStatus.Pending;
911
+ }
912
+ get status() {
913
+ return this._status;
914
+ }
915
+ get terminated() {
916
+ return [WorkflowStatus.Succeeded, WorkflowStatus.Failed, WorkflowStatus.Canceled].includes(
917
+ this.status
918
+ );
919
+ }
920
+ get startTime() {
921
+ return this._startTime;
922
+ }
923
+ get endTime() {
924
+ return this._endTime;
925
+ }
926
+ get timeCost() {
927
+ if (!this.startTime) {
928
+ return 0;
929
+ }
930
+ if (this.endTime) {
931
+ return this.endTime - this.startTime;
932
+ }
933
+ return Date.now() - this.startTime;
934
+ }
935
+ process() {
936
+ this._status = WorkflowStatus.Processing;
937
+ this._startTime = Date.now();
938
+ this._endTime = void 0;
939
+ }
940
+ success() {
941
+ if (this.terminated) {
942
+ return;
943
+ }
944
+ this._status = WorkflowStatus.Succeeded;
945
+ this._endTime = Date.now();
946
+ }
947
+ fail() {
948
+ if (this.terminated) {
949
+ return;
950
+ }
951
+ this._status = WorkflowStatus.Failed;
952
+ this._endTime = Date.now();
953
+ }
954
+ cancel() {
955
+ if (this.terminated) {
956
+ return;
957
+ }
958
+ this._status = WorkflowStatus.Canceled;
959
+ this._endTime = Date.now();
960
+ }
961
+ export() {
962
+ return {
963
+ status: this.status,
964
+ terminated: this.terminated,
965
+ startTime: this.startTime,
966
+ endTime: this.endTime,
967
+ timeCost: this.timeCost
968
+ };
969
+ }
970
+ static create() {
971
+ const status = new _WorkflowRuntimeStatus();
972
+ return status;
973
+ }
974
+ };
975
+
976
+ // src/domain/status/status-center/index.ts
977
+ var WorkflowRuntimeStatusCenter = class {
978
+ init() {
979
+ this._workflowStatus = WorkflowRuntimeStatus.create();
980
+ this._nodeStatus = /* @__PURE__ */ new Map();
981
+ }
982
+ dispose() {
983
+ }
984
+ get workflow() {
985
+ return this._workflowStatus;
986
+ }
987
+ get workflowStatus() {
988
+ return this._workflowStatus;
989
+ }
990
+ nodeStatus(nodeID) {
991
+ if (!this._nodeStatus.has(nodeID)) {
992
+ this._nodeStatus.set(nodeID, WorkflowRuntimeStatus.create());
993
+ }
994
+ const status = this._nodeStatus.get(nodeID);
995
+ return status;
996
+ }
997
+ getStatusNodeIDs(status) {
998
+ return Array.from(this._nodeStatus.entries()).filter(([, nodeStatus]) => nodeStatus.status === status).map(([nodeID]) => nodeID);
999
+ }
1000
+ exportNodeStatus() {
1001
+ return Object.fromEntries(
1002
+ Array.from(this._nodeStatus.entries()).map(([nodeID, status]) => [nodeID, status.export()])
1003
+ );
1004
+ }
1005
+ };
1006
+
1007
+ // src/domain/state/index.ts
1008
+ var import_lodash_es11 = require("lodash-es");
1009
+ var WorkflowRuntimeState = class {
1010
+ constructor(variableStore) {
1011
+ this.variableStore = variableStore;
1012
+ this.id = uuid();
1013
+ }
1014
+ init() {
1015
+ this.executedNodes = /* @__PURE__ */ new Set();
1016
+ }
1017
+ dispose() {
1018
+ this.executedNodes.clear();
1019
+ }
1020
+ getNodeInputs(node) {
1021
+ const inputsDeclare = node.declare.inputs;
1022
+ const inputsValues = node.declare.inputsValues;
1023
+ if (!inputsDeclare || !inputsValues) {
1024
+ return {};
1025
+ }
1026
+ return Object.entries(inputsValues).reduce((prev, [key, inputValue]) => {
1027
+ const typeInfo = inputsDeclare.properties?.[key];
1028
+ if (!typeInfo) {
1029
+ return prev;
1030
+ }
1031
+ const expectType = typeInfo.type;
1032
+ const result = this.parseValue(inputValue);
1033
+ if (!result) {
1034
+ return prev;
1035
+ }
1036
+ const { value, type } = result;
1037
+ if (!WorkflowRuntimeType.isTypeEqual(type, expectType)) {
1038
+ return prev;
1039
+ }
1040
+ prev[key] = value;
1041
+ return prev;
1042
+ }, {});
1043
+ }
1044
+ setNodeOutputs(params) {
1045
+ const { node, outputs } = params;
1046
+ const outputsDeclare = node.declare.outputs;
1047
+ if (!outputsDeclare) {
1048
+ return;
1049
+ }
1050
+ Object.entries(outputs).forEach(([key, value]) => {
1051
+ const typeInfo = outputsDeclare.properties?.[key];
1052
+ if (!typeInfo) {
1053
+ return;
1054
+ }
1055
+ const type = typeInfo.type;
1056
+ const itemsType = typeInfo.items?.type;
1057
+ this.variableStore.setVariable({
1058
+ nodeID: node.id,
1059
+ key,
1060
+ value,
1061
+ type,
1062
+ itemsType
1063
+ });
1064
+ });
1065
+ }
1066
+ parseRef(ref) {
1067
+ if (ref?.type !== "ref") {
1068
+ throw new Error(`invalid ref value: ${ref}`);
1069
+ }
1070
+ if (!ref.content || ref.content.length < 2) {
1071
+ return null;
1072
+ }
1073
+ const [nodeID, variableKey, ...variablePath] = ref.content;
1074
+ const result = this.variableStore.getValue({
1075
+ nodeID,
1076
+ variableKey,
1077
+ variablePath
1078
+ });
1079
+ if (!result) {
1080
+ return null;
1081
+ }
1082
+ return result;
1083
+ }
1084
+ parseValue(flowValue) {
1085
+ if (!flowValue?.type) {
1086
+ throw new Error(`invalid flow value type: ${flowValue.type}`);
1087
+ }
1088
+ if (flowValue.type === "constant") {
1089
+ const value = flowValue.content;
1090
+ const type = WorkflowRuntimeType.getWorkflowType(value);
1091
+ if ((0, import_lodash_es11.isNil)(value) || !type) {
1092
+ return null;
1093
+ }
1094
+ return {
1095
+ value,
1096
+ type
1097
+ };
1098
+ }
1099
+ if (flowValue.type === "ref") {
1100
+ return this.parseRef(flowValue);
1101
+ }
1102
+ throw new Error(`unknown flow value type: ${flowValue.type}`);
1103
+ }
1104
+ isExecutedNode(node) {
1105
+ return this.executedNodes.has(node.id);
1106
+ }
1107
+ addExecutedNode(node) {
1108
+ this.executedNodes.add(node.id);
1109
+ }
1110
+ };
1111
+
1112
+ // src/domain/snapshot/snapshot-entity/index.ts
1113
+ var WorkflowRuntimeSnapshot = class _WorkflowRuntimeSnapshot {
1114
+ constructor(data) {
1115
+ this.id = uuid();
1116
+ this.data = data;
1117
+ }
1118
+ addData(data) {
1119
+ Object.assign(this.data, data);
1120
+ }
1121
+ validate() {
1122
+ const required = ["nodeID", "inputs", "outputs", "data"];
1123
+ return required.every((key) => this.data[key] !== void 0);
1124
+ }
1125
+ export() {
1126
+ const snapshot = {
1127
+ id: this.id,
1128
+ ...this.data
1129
+ };
1130
+ return snapshot;
1131
+ }
1132
+ static create(params) {
1133
+ return new _WorkflowRuntimeSnapshot(params);
1134
+ }
1135
+ };
1136
+
1137
+ // src/domain/snapshot/snapshot-center/index.ts
1138
+ var WorkflowRuntimeSnapshotCenter = class {
1139
+ constructor() {
1140
+ this.id = uuid();
1141
+ }
1142
+ create(snapshotData) {
1143
+ const snapshot = WorkflowRuntimeSnapshot.create(snapshotData);
1144
+ this.snapshots.push(snapshot);
1145
+ return snapshot;
1146
+ }
1147
+ init() {
1148
+ this.snapshots = [];
1149
+ }
1150
+ dispose() {
1151
+ }
1152
+ exportAll() {
1153
+ return this.snapshots.slice().map((snapshot) => snapshot.export());
1154
+ }
1155
+ export() {
1156
+ const result = {};
1157
+ this.exportAll().forEach((snapshot) => {
1158
+ if (result[snapshot.nodeID]) {
1159
+ result[snapshot.nodeID].push(snapshot);
1160
+ } else {
1161
+ result[snapshot.nodeID] = [snapshot];
1162
+ }
1163
+ });
1164
+ return result;
1165
+ }
1166
+ };
1167
+
1168
+ // src/domain/report/report-value-object/index.ts
1169
+ var WorkflowRuntimeReport;
1170
+ ((WorkflowRuntimeReport2) => {
1171
+ WorkflowRuntimeReport2.create = (params) => ({
1172
+ id: uuid(),
1173
+ ...params
1174
+ });
1175
+ })(WorkflowRuntimeReport || (WorkflowRuntimeReport = {}));
1176
+
1177
+ // src/domain/report/reporter/index.ts
1178
+ var WorkflowRuntimeReporter = class {
1179
+ constructor(ioCenter, snapshotCenter, statusCenter) {
1180
+ this.ioCenter = ioCenter;
1181
+ this.snapshotCenter = snapshotCenter;
1182
+ this.statusCenter = statusCenter;
1183
+ }
1184
+ init() {
1185
+ }
1186
+ dispose() {
1187
+ }
1188
+ export() {
1189
+ const report = WorkflowRuntimeReport.create({
1190
+ inputs: this.ioCenter.inputs,
1191
+ outputs: this.ioCenter.outputs,
1192
+ workflowStatus: this.statusCenter.workflow.export(),
1193
+ reports: this.nodeReports()
1194
+ });
1195
+ return report;
1196
+ }
1197
+ nodeReports() {
1198
+ const reports = {};
1199
+ const statuses = this.statusCenter.exportNodeStatus();
1200
+ const snapshots = this.snapshotCenter.export();
1201
+ Object.keys(statuses).forEach((nodeID) => {
1202
+ const status = statuses[nodeID];
1203
+ const nodeSnapshots = snapshots[nodeID] || [];
1204
+ const nodeReport = {
1205
+ id: nodeID,
1206
+ ...status,
1207
+ snapshots: nodeSnapshots
1208
+ };
1209
+ reports[nodeID] = nodeReport;
1210
+ });
1211
+ return reports;
1212
+ }
1213
+ };
1214
+
1215
+ // src/domain/io-center/index.ts
1216
+ var WorkflowRuntimeIOCenter = class {
1217
+ init(inputs) {
1218
+ this.setInputs(inputs);
1219
+ }
1220
+ dispose() {
1221
+ }
1222
+ get inputs() {
1223
+ return this._inputs ?? {};
1224
+ }
1225
+ get outputs() {
1226
+ return this._outputs ?? {};
1227
+ }
1228
+ setInputs(inputs) {
1229
+ this._inputs = inputs;
1230
+ }
1231
+ setOutputs(outputs) {
1232
+ this._outputs = outputs;
1233
+ }
1234
+ export() {
1235
+ return {
1236
+ inputs: this._inputs,
1237
+ outputs: this._outputs
1238
+ };
1239
+ }
1240
+ };
1241
+
1242
+ // src/domain/document/entity/edge/index.ts
1243
+ var WorkflowRuntimeEdge = class {
1244
+ constructor(params) {
1245
+ const { id, from, to } = params;
1246
+ this.id = id;
1247
+ this.from = from;
1248
+ this.to = to;
1249
+ }
1250
+ get fromPort() {
1251
+ return this._fromPort;
1252
+ }
1253
+ set fromPort(port) {
1254
+ this._fromPort = port;
1255
+ }
1256
+ get toPort() {
1257
+ return this._toPort;
1258
+ }
1259
+ set toPort(port) {
1260
+ this._toPort = port;
1261
+ }
1262
+ static createID(schema) {
1263
+ const { sourceNodeID, sourcePortID, targetNodeID, targetPortID } = schema;
1264
+ const sourcePart = sourcePortID ? `${sourceNodeID}:${sourcePortID}` : sourceNodeID;
1265
+ const targetPart = targetPortID ? `${targetNodeID}:${targetPortID}` : targetNodeID;
1266
+ return `${sourcePart}-${targetPart}`;
1267
+ }
1268
+ };
1269
+
1270
+ // src/domain/document/entity/node/index.ts
1271
+ var WorkflowRuntimeNode = class {
1272
+ constructor(params) {
1273
+ const { id, type, name, position, variable, data } = params;
1274
+ this.id = id;
1275
+ this.type = type;
1276
+ this.name = name;
1277
+ this.position = position;
1278
+ this.declare = variable ?? {};
1279
+ this.data = data ?? {};
1280
+ this._parent = null;
1281
+ this._children = [];
1282
+ this._ports = [];
1283
+ this._inputEdges = [];
1284
+ this._outputEdges = [];
1285
+ this._prev = [];
1286
+ this._next = [];
1287
+ }
1288
+ get ports() {
1289
+ const inputs = this._ports.filter((port) => port.type === WorkflowPortType.Input);
1290
+ const outputs = this._ports.filter((port) => port.type === WorkflowPortType.Output);
1291
+ return {
1292
+ inputs,
1293
+ outputs
1294
+ };
1295
+ }
1296
+ get edges() {
1297
+ return {
1298
+ inputs: this._inputEdges,
1299
+ outputs: this._outputEdges
1300
+ };
1301
+ }
1302
+ get parent() {
1303
+ return this._parent;
1304
+ }
1305
+ set parent(parent) {
1306
+ this._parent = parent;
1307
+ }
1308
+ get children() {
1309
+ return this._children;
1310
+ }
1311
+ addChild(child) {
1312
+ this._children.push(child);
1313
+ }
1314
+ addPort(port) {
1315
+ this._ports.push(port);
1316
+ }
1317
+ addInputEdge(edge) {
1318
+ this._inputEdges.push(edge);
1319
+ this._prev.push(edge.from);
1320
+ }
1321
+ addOutputEdge(edge) {
1322
+ this._outputEdges.push(edge);
1323
+ this._next.push(edge.to);
1324
+ }
1325
+ get prev() {
1326
+ return this._prev;
1327
+ }
1328
+ get next() {
1329
+ return this._next;
1330
+ }
1331
+ get isBranch() {
1332
+ return this.ports.outputs.length > 1;
1333
+ }
1334
+ };
1335
+
1336
+ // src/domain/document/entity/port/index.ts
1337
+ var WorkflowRuntimePort = class {
1338
+ constructor(params) {
1339
+ const { id, node } = params;
1340
+ this.id = id;
1341
+ this.node = node;
1342
+ this.type = params.type;
1343
+ this._edges = [];
1344
+ }
1345
+ get edges() {
1346
+ return this._edges;
1347
+ }
1348
+ addEdge(edge) {
1349
+ this._edges.push(edge);
1350
+ }
1351
+ };
1352
+
1353
+ // src/domain/document/document/flat-schema.ts
1354
+ var flatLayer = (data, nodeSchema) => {
1355
+ const { blocks, edges } = nodeSchema;
1356
+ if (blocks) {
1357
+ data.flattenSchema.nodes.push(...blocks);
1358
+ const blockIDs = [];
1359
+ blocks.forEach((block) => {
1360
+ blockIDs.push(block.id);
1361
+ if (block.blocks) {
1362
+ flatLayer(data, block);
1363
+ }
1364
+ });
1365
+ data.nodeBlocks.set(nodeSchema.id, blockIDs);
1366
+ delete nodeSchema.blocks;
1367
+ }
1368
+ if (edges) {
1369
+ data.flattenSchema.edges.push(...edges);
1370
+ const edgeIDs = [];
1371
+ edges.forEach((edge) => {
1372
+ const edgeID = WorkflowRuntimeEdge.createID(edge);
1373
+ edgeIDs.push(edgeID);
1374
+ });
1375
+ data.nodeEdges.set(nodeSchema.id, edgeIDs);
1376
+ delete nodeSchema.edges;
1377
+ }
1378
+ };
1379
+ var flatSchema = (schema = { nodes: [], edges: [] }) => {
1380
+ const rootNodes = schema.nodes ?? [];
1381
+ const rootEdges = schema.edges ?? [];
1382
+ const data = {
1383
+ flattenSchema: {
1384
+ nodes: [],
1385
+ edges: []
1386
+ },
1387
+ nodeBlocks: /* @__PURE__ */ new Map(),
1388
+ nodeEdges: /* @__PURE__ */ new Map()
1389
+ };
1390
+ const root = {
1391
+ id: FlowGramNode.Root,
1392
+ type: FlowGramNode.Root,
1393
+ blocks: rootNodes,
1394
+ edges: rootEdges,
1395
+ meta: {
1396
+ position: {
1397
+ x: 0,
1398
+ y: 0
1399
+ }
1400
+ },
1401
+ data: {}
1402
+ };
1403
+ flatLayer(data, root);
1404
+ return data;
1405
+ };
1406
+
1407
+ // src/domain/document/document/create-store.ts
1408
+ var createNode = (store, params) => {
1409
+ const node = new WorkflowRuntimeNode(params);
1410
+ store.nodes.set(node.id, node);
1411
+ return node;
1412
+ };
1413
+ var createEdge = (store, params) => {
1414
+ const edge = new WorkflowRuntimeEdge(params);
1415
+ store.edges.set(edge.id, edge);
1416
+ return edge;
1417
+ };
1418
+ var getOrCreatePort = (store, params) => {
1419
+ const createdPort = store.ports.get(params.id);
1420
+ if (createdPort) {
1421
+ return createdPort;
1422
+ }
1423
+ const port = new WorkflowRuntimePort(params);
1424
+ store.ports.set(port.id, port);
1425
+ return port;
1426
+ };
1427
+ var createStore = (params) => {
1428
+ const { flattenSchema, nodeBlocks } = params;
1429
+ const { nodes, edges } = flattenSchema;
1430
+ const store = {
1431
+ nodes: /* @__PURE__ */ new Map(),
1432
+ edges: /* @__PURE__ */ new Map(),
1433
+ ports: /* @__PURE__ */ new Map()
1434
+ };
1435
+ createNode(store, {
1436
+ id: FlowGramNode.Root,
1437
+ type: FlowGramNode.Root,
1438
+ name: FlowGramNode.Root,
1439
+ position: { x: 0, y: 0 }
1440
+ });
1441
+ nodes.forEach((nodeSchema) => {
1442
+ const id = nodeSchema.id;
1443
+ const type = nodeSchema.type;
1444
+ const {
1445
+ title = `${type}-${id}-untitled`,
1446
+ inputsValues,
1447
+ inputs,
1448
+ outputs,
1449
+ ...data
1450
+ } = nodeSchema.data ?? {};
1451
+ createNode(store, {
1452
+ id,
1453
+ type,
1454
+ name: title,
1455
+ position: nodeSchema.meta.position,
1456
+ variable: { inputsValues, inputs, outputs },
1457
+ data
1458
+ });
1459
+ });
1460
+ nodeBlocks.forEach((blockIDs, parentID) => {
1461
+ const parent = store.nodes.get(parentID);
1462
+ const children = blockIDs.map((id) => store.nodes.get(id)).filter(Boolean);
1463
+ children.forEach((child) => {
1464
+ child.parent = parent;
1465
+ parent.addChild(child);
1466
+ });
1467
+ });
1468
+ edges.forEach((edgeSchema) => {
1469
+ const id = WorkflowRuntimeEdge.createID(edgeSchema);
1470
+ const {
1471
+ sourceNodeID,
1472
+ targetNodeID,
1473
+ sourcePortID = "defaultOutput",
1474
+ targetPortID = "defaultInput"
1475
+ } = edgeSchema;
1476
+ const from = store.nodes.get(sourceNodeID);
1477
+ const to = store.nodes.get(targetNodeID);
1478
+ if (!from || !to) {
1479
+ throw new Error(`invalid edge schema ID: ${id}, from: ${sourceNodeID}, to: ${targetNodeID}`);
1480
+ }
1481
+ const edge = createEdge(store, {
1482
+ id,
1483
+ from,
1484
+ to
1485
+ });
1486
+ const fromPort = getOrCreatePort(store, {
1487
+ node: from,
1488
+ id: sourcePortID,
1489
+ type: WorkflowPortType.Output
1490
+ });
1491
+ fromPort.addEdge(edge);
1492
+ edge.fromPort = fromPort;
1493
+ from.addPort(fromPort);
1494
+ from.addOutputEdge(edge);
1495
+ const toPort = getOrCreatePort(store, {
1496
+ node: to,
1497
+ id: targetPortID,
1498
+ type: WorkflowPortType.Input
1499
+ });
1500
+ toPort.addEdge(edge);
1501
+ edge.toPort = toPort;
1502
+ to.addPort(toPort);
1503
+ to.addInputEdge(edge);
1504
+ });
1505
+ return store;
1506
+ };
1507
+
1508
+ // src/domain/document/document/index.ts
1509
+ var WorkflowRuntimeDocument = class {
1510
+ constructor() {
1511
+ this.id = uuid();
1512
+ }
1513
+ get root() {
1514
+ const rootNode = this.getNode(FlowGramNode.Root);
1515
+ if (!rootNode) {
1516
+ throw new Error("Root node not found");
1517
+ }
1518
+ return rootNode;
1519
+ }
1520
+ get start() {
1521
+ const startNode = this.nodes.find((n) => n.type === FlowGramNode.Start);
1522
+ if (!startNode) {
1523
+ throw new Error("Start node not found");
1524
+ }
1525
+ return startNode;
1526
+ }
1527
+ get end() {
1528
+ const endNode = this.nodes.find((n) => n.type === FlowGramNode.End);
1529
+ if (!endNode) {
1530
+ throw new Error("End node not found");
1531
+ }
1532
+ return endNode;
1533
+ }
1534
+ getNode(id) {
1535
+ return this.store.nodes.get(id) ?? null;
1536
+ }
1537
+ getEdge(id) {
1538
+ return this.store.edges.get(id) ?? null;
1539
+ }
1540
+ get nodes() {
1541
+ return Array.from(this.store.nodes.values());
1542
+ }
1543
+ get edges() {
1544
+ return Array.from(this.store.edges.values());
1545
+ }
1546
+ init(schema) {
1547
+ const flattenSchema = flatSchema(schema);
1548
+ this.store = createStore(flattenSchema);
1549
+ }
1550
+ dispose() {
1551
+ this.store.edges.clear();
1552
+ this.store.nodes.clear();
1553
+ this.store.ports.clear();
1554
+ }
1555
+ };
1556
+
1557
+ // src/domain/context/index.ts
1558
+ var WorkflowRuntimeContext = class _WorkflowRuntimeContext {
1559
+ constructor(data) {
1560
+ this.subContexts = [];
1561
+ this.id = uuid();
1562
+ this.document = data.document;
1563
+ this.variableStore = data.variableStore;
1564
+ this.state = data.state;
1565
+ this.ioCenter = data.ioCenter;
1566
+ this.snapshotCenter = data.snapshotCenter;
1567
+ this.statusCenter = data.statusCenter;
1568
+ this.reporter = data.reporter;
1569
+ }
1570
+ init(params) {
1571
+ const { schema, inputs } = params;
1572
+ this.document.init(schema);
1573
+ this.variableStore.init();
1574
+ this.state.init();
1575
+ this.ioCenter.init(inputs);
1576
+ this.snapshotCenter.init();
1577
+ this.statusCenter.init();
1578
+ this.reporter.init();
1579
+ }
1580
+ dispose() {
1581
+ this.subContexts.forEach((subContext) => {
1582
+ subContext.dispose();
1583
+ });
1584
+ this.subContexts = [];
1585
+ this.document.dispose();
1586
+ this.variableStore.dispose();
1587
+ this.state.dispose();
1588
+ this.ioCenter.dispose();
1589
+ this.snapshotCenter.dispose();
1590
+ this.statusCenter.dispose();
1591
+ this.reporter.dispose();
1592
+ }
1593
+ sub() {
1594
+ const variableStore = new WorkflowRuntimeVariableStore();
1595
+ variableStore.setParent(this.variableStore);
1596
+ const state = new WorkflowRuntimeState(variableStore);
1597
+ const contextData = {
1598
+ document: this.document,
1599
+ ioCenter: this.ioCenter,
1600
+ snapshotCenter: this.snapshotCenter,
1601
+ statusCenter: this.statusCenter,
1602
+ reporter: this.reporter,
1603
+ variableStore,
1604
+ state
1605
+ };
1606
+ const subContext = new _WorkflowRuntimeContext(contextData);
1607
+ this.subContexts.push(subContext);
1608
+ subContext.variableStore.init();
1609
+ subContext.state.init();
1610
+ return subContext;
1611
+ }
1612
+ static create() {
1613
+ const document = new WorkflowRuntimeDocument();
1614
+ const variableStore = new WorkflowRuntimeVariableStore();
1615
+ const state = new WorkflowRuntimeState(variableStore);
1616
+ const ioCenter = new WorkflowRuntimeIOCenter();
1617
+ const snapshotCenter = new WorkflowRuntimeSnapshotCenter();
1618
+ const statusCenter = new WorkflowRuntimeStatusCenter();
1619
+ const reporter = new WorkflowRuntimeReporter(ioCenter, snapshotCenter, statusCenter);
1620
+ return new _WorkflowRuntimeContext({
1621
+ document,
1622
+ variableStore,
1623
+ state,
1624
+ ioCenter,
1625
+ snapshotCenter,
1626
+ statusCenter,
1627
+ reporter
1628
+ });
1629
+ }
1630
+ };
1631
+
1632
+ // src/domain/engine/index.ts
1633
+ var WorkflowRuntimeEngine = class {
1634
+ constructor(service) {
1635
+ this.executor = service.Executor;
1636
+ }
1637
+ invoke(params) {
1638
+ const context = WorkflowRuntimeContext.create();
1639
+ context.init(params);
1640
+ const processing = this.process(context);
1641
+ processing.then(() => {
1642
+ context.dispose();
1643
+ });
1644
+ return WorkflowRuntimeTask.create({
1645
+ processing,
1646
+ context
1647
+ });
1648
+ }
1649
+ async executeNode(params) {
1650
+ const { node, context } = params;
1651
+ if (!this.canExecuteNode({ node, context })) {
1652
+ return;
1653
+ }
1654
+ context.statusCenter.nodeStatus(node.id).process();
1655
+ try {
1656
+ const inputs = context.state.getNodeInputs(node);
1657
+ const snapshot = context.snapshotCenter.create({
1658
+ nodeID: node.id,
1659
+ data: node.data,
1660
+ inputs
1661
+ });
1662
+ const result = await this.executor.execute({
1663
+ node,
1664
+ inputs,
1665
+ runtime: context,
1666
+ container: WorkflowRuntimeContainer.instance
1667
+ });
1668
+ if (context.statusCenter.workflow.terminated) {
1669
+ return;
1670
+ }
1671
+ const { outputs, branch } = result;
1672
+ snapshot.addData({ outputs, branch });
1673
+ context.state.setNodeOutputs({ node, outputs });
1674
+ context.state.addExecutedNode(node);
1675
+ context.statusCenter.nodeStatus(node.id).success();
1676
+ const nextNodes = this.getNextNodes({ node, branch, context });
1677
+ await this.executeNext({ node, nextNodes, context });
1678
+ } catch (e) {
1679
+ context.statusCenter.nodeStatus(node.id).fail();
1680
+ console.error(e);
1681
+ return;
1682
+ }
1683
+ }
1684
+ async process(context) {
1685
+ const startNode = context.document.start;
1686
+ context.statusCenter.workflow.process();
1687
+ try {
1688
+ await this.executeNode({ node: startNode, context });
1689
+ const outputs = context.ioCenter.outputs;
1690
+ context.statusCenter.workflow.success();
1691
+ return outputs;
1692
+ } catch (e) {
1693
+ context.statusCenter.workflow.fail();
1694
+ throw e;
1695
+ }
1696
+ }
1697
+ canExecuteNode(params) {
1698
+ const { node, context } = params;
1699
+ const prevNodes = node.prev;
1700
+ if (prevNodes.length === 0) {
1701
+ return true;
1702
+ }
1703
+ return prevNodes.every((prevNode) => context.state.isExecutedNode(prevNode));
1704
+ }
1705
+ getNextNodes(params) {
1706
+ const { node, branch, context } = params;
1707
+ const allNextNodes = node.next;
1708
+ if (!branch) {
1709
+ return allNextNodes;
1710
+ }
1711
+ const targetPort = node.ports.outputs.find((port) => port.id === branch);
1712
+ if (!targetPort) {
1713
+ throw new Error(`branch ${branch} not found`);
1714
+ }
1715
+ const nextNodeIDs = new Set(targetPort.edges.map((edge) => edge.to.id));
1716
+ const nextNodes = allNextNodes.filter((nextNode) => nextNodeIDs.has(nextNode.id));
1717
+ const skipNodes = allNextNodes.filter((nextNode) => !nextNodeIDs.has(nextNode.id));
1718
+ skipNodes.forEach((skipNode) => {
1719
+ context.state.addExecutedNode(skipNode);
1720
+ });
1721
+ return nextNodes;
1722
+ }
1723
+ async executeNext(params) {
1724
+ const { context, node, nextNodes } = params;
1725
+ if (node.type === FlowGramNode.End) {
1726
+ return;
1727
+ }
1728
+ if (nextNodes.length === 0) {
1729
+ return;
1730
+ }
1731
+ await Promise.all(
1732
+ nextNodes.map(
1733
+ (nextNode) => this.executeNode({
1734
+ node: nextNode,
1735
+ context
1736
+ })
1737
+ )
1738
+ );
1739
+ }
1740
+ };
1741
+
1742
+ // src/domain/container/index.ts
1743
+ var WorkflowRuntimeContainer = class _WorkflowRuntimeContainer {
1744
+ constructor(services) {
1745
+ this.services = services;
1746
+ }
1747
+ get(key) {
1748
+ return this.services[key];
1749
+ }
1750
+ static get instance() {
1751
+ if (this._instance) {
1752
+ return this._instance;
1753
+ }
1754
+ const services = this.create();
1755
+ this._instance = new _WorkflowRuntimeContainer(services);
1756
+ return this._instance;
1757
+ }
1758
+ static create() {
1759
+ const Validation = new WorkflowRuntimeValidation();
1760
+ const Executor = new WorkflowRuntimeExecutor(WorkflowRuntimeNodeExecutors);
1761
+ const Engine = new WorkflowRuntimeEngine({
1762
+ Executor
1763
+ });
1764
+ return {
1765
+ [IValidation]: Validation,
1766
+ [IExecutor]: Executor,
1767
+ [IEngine]: Engine
1768
+ };
1769
+ }
1770
+ };
1771
+
1772
+ // src/application/workflow.ts
1773
+ var WorkflowApplication = class _WorkflowApplication {
1774
+ constructor() {
1775
+ this.container = WorkflowRuntimeContainer.instance;
1776
+ this.tasks = /* @__PURE__ */ new Map();
1777
+ }
1778
+ run(params) {
1779
+ const engine = this.container.get(IEngine);
1780
+ const task = engine.invoke(params);
1781
+ this.tasks.set(task.id, task);
1782
+ console.log("> POST TaskRun - taskID: ", task.id);
1783
+ console.log(params.inputs);
1784
+ task.processing.then((output) => {
1785
+ console.log("> LOG Task finished: ", task.id);
1786
+ console.log(output);
1787
+ });
1788
+ return task.id;
1789
+ }
1790
+ cancel(taskID) {
1791
+ console.log("> PUT TaskCancel - taskID: ", taskID);
1792
+ const task = this.tasks.get(taskID);
1793
+ if (!task) {
1794
+ return false;
1795
+ }
1796
+ task.cancel();
1797
+ return true;
1798
+ }
1799
+ report(taskID) {
1800
+ const task = this.tasks.get(taskID);
1801
+ console.log("> GET TaskReport - taskID: ", taskID);
1802
+ if (!task) {
1803
+ return;
1804
+ }
1805
+ return task.context.reporter.export();
1806
+ }
1807
+ result(taskID) {
1808
+ console.log("> GET TaskResult - taskID: ", taskID);
1809
+ const task = this.tasks.get(taskID);
1810
+ if (!task) {
1811
+ return;
1812
+ }
1813
+ if (!task.context.statusCenter.workflow.terminated) {
1814
+ return;
1815
+ }
1816
+ return task.context.ioCenter.outputs;
1817
+ }
1818
+ static get instance() {
1819
+ if (this._instance) {
1820
+ return this._instance;
1821
+ }
1822
+ this._instance = new _WorkflowApplication();
1823
+ return this._instance;
1824
+ }
1825
+ };
1826
+
1827
+ // src/api/task-run.ts
1828
+ var TaskRunAPI = async (input) => {
1829
+ const app = WorkflowApplication.instance;
1830
+ const { schema: stringSchema, inputs } = input;
1831
+ const schema = JSON.parse(stringSchema);
1832
+ const taskID = app.run({
1833
+ schema,
1834
+ inputs
1835
+ });
1836
+ const output = {
1837
+ taskID
1838
+ };
1839
+ return output;
1840
+ };
1841
+
1842
+ // src/api/task-result.ts
1843
+ var TaskResultAPI = async (input) => {
1844
+ const app = WorkflowApplication.instance;
1845
+ const { taskID } = input;
1846
+ const output = app.result(taskID);
1847
+ return output;
1848
+ };
1849
+
1850
+ // src/api/task-report.ts
1851
+ var TaskReportAPI = async (input) => {
1852
+ const app = WorkflowApplication.instance;
1853
+ const { taskID } = input;
1854
+ const output = app.report(taskID);
1855
+ try {
1856
+ TaskReportDefine.schema.output.parse(output);
1857
+ } catch (e) {
1858
+ console.log("> TaskReportAPI - output: ", JSON.stringify(output));
1859
+ console.error(e);
1860
+ }
1861
+ return output;
1862
+ };
1863
+
1864
+ // src/api/task-cancel.ts
1865
+ var TaskCancelAPI = async (input) => {
1866
+ const app = WorkflowApplication.instance;
1867
+ const { taskID } = input;
1868
+ const success = app.cancel(taskID);
1869
+ const output = {
1870
+ success
1871
+ };
1872
+ return output;
1873
+ };
1874
+
1875
+ // src/api/index.ts
1876
+ var WorkflowRuntimeAPIs = {
1877
+ [FlowGramAPIName.TaskRun]: TaskRunAPI,
1878
+ [FlowGramAPIName.TaskReport]: TaskReportAPI,
1879
+ [FlowGramAPIName.TaskResult]: TaskResultAPI,
1880
+ [FlowGramAPIName.TaskCancel]: TaskCancelAPI,
1881
+ [FlowGramAPIName.ServerInfo]: () => {
1882
+ },
1883
+ // TODO
1884
+ [FlowGramAPIName.Validation]: () => {
1885
+ }
1886
+ // TODO
1887
+ };
1888
+ // Annotate the CommonJS export names for ESM import in node:
1889
+ 0 && (module.exports = {
1890
+ TaskCancelAPI,
1891
+ TaskReportAPI,
1892
+ TaskResultAPI,
1893
+ TaskRunAPI,
1894
+ WorkflowRuntimeAPIs
1895
+ });
1896
+ //# sourceMappingURL=index.js.map