@baipeng139/72flow-nodejs 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,270 @@
1
+ /**
2
+ * 节点状态枚举
3
+ */
4
+ declare enum NodeStatus {
5
+ PENDING = "PENDING",
6
+ RUNNING = "RUNNING",
7
+ COMPLETED = "COMPLETED",
8
+ FAILED = "FAILED",
9
+ SKIPPED = "SKIPPED",
10
+ CANCELLED = "CANCELLED"
11
+ }
12
+ /**
13
+ * 节点类型枚举
14
+ */
15
+ declare enum NodeType {
16
+ START = "START",
17
+ END = "END",
18
+ SCRIPT = "SCRIPT",
19
+ DECISION = "DECISION",
20
+ CONDITION = "CONDITION",
21
+ PARALLEL = "PARALLEL",
22
+ LOOP = "LOOP",
23
+ API = "API",
24
+ SUBFLOW = "SUBFLOW",
25
+ LLM = "LLM",
26
+ BUSINESS = "BUSINESS"
27
+ }
28
+ /**
29
+ * 边定义
30
+ */
31
+ interface EdgeDef {
32
+ id: string;
33
+ name?: string;
34
+ from: string;
35
+ to: string;
36
+ condition?: string;
37
+ metadata?: Record<string, any>;
38
+ }
39
+ /**
40
+ * 节点配置定义
41
+ */
42
+ interface NodeConfigDef {
43
+ script?: string;
44
+ api?: {
45
+ url: string;
46
+ method: string;
47
+ headers?: Record<string, string>;
48
+ body?: string;
49
+ };
50
+ loop?: {
51
+ condition?: string;
52
+ items?: string;
53
+ endNodeId?: string;
54
+ };
55
+ inputSchema?: Record<string, any>;
56
+ outputSchema?: Record<string, any>;
57
+ error?: {
58
+ mode: 'throw' | 'fail' | string;
59
+ };
60
+ [key: string]: any;
61
+ }
62
+ /**
63
+ * 节点定义
64
+ */
65
+ interface NodeDef {
66
+ id: string;
67
+ type: string | NodeType;
68
+ name?: string;
69
+ code?: string;
70
+ config?: NodeConfigDef;
71
+ metadata?: Record<string, any>;
72
+ }
73
+ /**
74
+ * 流程定义
75
+ */
76
+ interface FlowDefinition {
77
+ id: string;
78
+ name: string;
79
+ version: string;
80
+ nodes: NodeDef[];
81
+ edges: EdgeDef[];
82
+ metadata?: Record<string, any>;
83
+ convergeMap?: Record<string, string>;
84
+ loopBodyPaths?: Record<string, string[]>;
85
+ }
86
+ /**
87
+ * 节点执行结果
88
+ */
89
+ interface NodeResult {
90
+ success: boolean;
91
+ data?: any;
92
+ message?: string;
93
+ code?: string;
94
+ }
95
+ /**
96
+ * 流程执行结果
97
+ */
98
+ interface FlowResult {
99
+ executionId: string;
100
+ status: NodeStatus;
101
+ output?: any;
102
+ error?: string;
103
+ duration: number;
104
+ startTime: number;
105
+ endTime?: number;
106
+ variables: Record<string, any>;
107
+ traces: any[];
108
+ }
109
+
110
+ interface ExecutionTrace {
111
+ nodeId: string;
112
+ /** 节点业务编码(如 DECISION_abc),用于 UI 图标和 nodeMap 查询 */
113
+ code?: string;
114
+ status: NodeStatus;
115
+ startTime: number;
116
+ endTime?: number;
117
+ /** 耗时毫秒 */
118
+ duration?: number;
119
+ /** 耗时纳秒(与 Java 后端格式对齐,供 ExecutionResultModal 使用)*/
120
+ durationNs?: number;
121
+ data?: any;
122
+ error?: string;
123
+ }
124
+ declare class FlowContext {
125
+ private executionId;
126
+ private definition;
127
+ private variables;
128
+ private status;
129
+ private startTime;
130
+ private endTime?;
131
+ private nodeMap;
132
+ private outgoingMap;
133
+ private incomingMap;
134
+ private completedNodes;
135
+ private executingNodes;
136
+ private skippedNodes;
137
+ private nodeOutputs;
138
+ private convergeStates;
139
+ private traces;
140
+ constructor(executionId: string, definition: FlowDefinition, variables: Record<string, any>);
141
+ /** 构造时预建索引,所有后续查询均为 O(1) */
142
+ private buildIndex;
143
+ getExecutionId(): string;
144
+ getDefinition(): FlowDefinition;
145
+ getVariables(): {
146
+ [x: string]: any;
147
+ };
148
+ getStatus(): NodeStatus;
149
+ getStartTime(): number;
150
+ getEndTime(): number | undefined;
151
+ getDuration(): number;
152
+ getNodes(): NodeDef[];
153
+ getNode(id: string): NodeDef | undefined;
154
+ getOutgoing(nodeId: string): EdgeDef[];
155
+ getIncoming(nodeId: string): EdgeDef[];
156
+ getCompletedNodes(): Set<string>;
157
+ getSkippedNodes(): Set<string>;
158
+ getExecutingNodes(): Set<string>;
159
+ getNodeOutputs(): {
160
+ [x: string]: any;
161
+ };
162
+ getTraces(): ExecutionTrace[];
163
+ setStatus(status: NodeStatus): void;
164
+ /** 设置单个变量(供 LoopExecutor 写入 item/index 等迭代变量) */
165
+ setVariable(key: string, value: any): void;
166
+ skipNode(nodeId: string): void;
167
+ /** 注册并行汇聚点,expected = 分支数 */
168
+ registerConvergence(nodeId: string, expected: number): void;
169
+ arePrerequisitesMet(nodeId: string): boolean;
170
+ private isEffectivelySkipped;
171
+ tryExecute(nodeId: string): boolean;
172
+ complete(nodeId: string, output: any, startTime: number): void;
173
+ fail(nodeId: string, error: Error | string, startTime: number): void;
174
+ /** 并行汇聚:返回 true 代表当前节点所有分支已全部到达 */
175
+ tryConverge(nodeId: string, fromNodeId: string): boolean;
176
+ /** 获取 LOOP_START 节点的循环体路径(来自解析器预计算的 loopBodyPaths) */
177
+ getLoopBodyPath(loopStartNodeId: string): string[];
178
+ }
179
+
180
+ type EventHandler = (...args: any[]) => void;
181
+ declare class SimpleEmitter {
182
+ private handlers;
183
+ on(event: string, handler: EventHandler): this;
184
+ once(event: string, handler: EventHandler): this;
185
+ off(event: string, handler: EventHandler): this;
186
+ emit(event: string, ...args: any[]): this;
187
+ }
188
+ declare class FlowEngine extends SimpleEmitter {
189
+ private activeContexts;
190
+ constructor();
191
+ execute(definition: FlowDefinition, variables?: Record<string, any>): Promise<FlowResult>;
192
+ private scheduleNode;
193
+ private runNode;
194
+ /**
195
+ * 触发下游节点。有汇聚时,等所有【非跳过】入边到达后才 schedule。
196
+ */
197
+ private triggerDownstream;
198
+ private handleFailure;
199
+ private buildResult;
200
+ }
201
+
202
+ interface NodeExecutor {
203
+ execute(node: NodeDef, context: FlowContext): Promise<NodeResult>;
204
+ }
205
+ declare class NodeExecutorFactory {
206
+ static execute(node: NodeDef, context: FlowContext): Promise<NodeResult>;
207
+ }
208
+
209
+ interface X6Node {
210
+ id: string;
211
+ shape: string;
212
+ position?: {
213
+ x: number;
214
+ y: number;
215
+ };
216
+ data?: {
217
+ meta?: Record<string, any>;
218
+ config?: Record<string, any>;
219
+ };
220
+ }
221
+ interface X6Edge {
222
+ id?: string;
223
+ source: string | {
224
+ cell: string;
225
+ port?: string;
226
+ };
227
+ target: string | {
228
+ cell: string;
229
+ port?: string;
230
+ };
231
+ data?: {
232
+ config?: {
233
+ branchResult?: string;
234
+ [k: string]: any;
235
+ };
236
+ priority?: number;
237
+ };
238
+ }
239
+ interface X6Graph {
240
+ nodes: X6Node[];
241
+ edges: X6Edge[];
242
+ }
243
+ declare class X6Parser {
244
+ /**
245
+ * 将前端 X6 序列化的 JSON 解析为引擎可执行的 FlowDefinition
246
+ */
247
+ parse(x6Json: X6Graph | string): FlowDefinition;
248
+ private parseNodes;
249
+ private normalizeConfig;
250
+ private normalizeApiField;
251
+ private parseEdges;
252
+ private extractCellId;
253
+ private computeConvergeMap;
254
+ /** BFS 找所有分支的第一个公共后继节点 */
255
+ private findConvergeNode;
256
+ private computeLoopBodyPaths;
257
+ private computeSingleLoopPath;
258
+ private buildOutgoingMap;
259
+ }
260
+ /** 便捷函数:直接解析 X6 JSON → FlowDefinition */
261
+ declare function parseX6(x6Json: X6Graph | string): FlowDefinition;
262
+
263
+ declare function createLogger(tag: string): {
264
+ debug: (msg: string, ...args: any[]) => void;
265
+ info: (msg: string, ...args: any[]) => void;
266
+ warn: (msg: string, ...args: any[]) => void;
267
+ error: (msg: string, ...args: any[]) => void;
268
+ };
269
+
270
+ export { type EdgeDef, type ExecutionTrace, FlowContext, type FlowDefinition, FlowEngine, type FlowResult, type NodeConfigDef, type NodeDef, type NodeExecutor, NodeExecutorFactory, type NodeResult, NodeStatus, NodeType, X6Parser, createLogger, parseX6 };
@@ -0,0 +1,270 @@
1
+ /**
2
+ * 节点状态枚举
3
+ */
4
+ declare enum NodeStatus {
5
+ PENDING = "PENDING",
6
+ RUNNING = "RUNNING",
7
+ COMPLETED = "COMPLETED",
8
+ FAILED = "FAILED",
9
+ SKIPPED = "SKIPPED",
10
+ CANCELLED = "CANCELLED"
11
+ }
12
+ /**
13
+ * 节点类型枚举
14
+ */
15
+ declare enum NodeType {
16
+ START = "START",
17
+ END = "END",
18
+ SCRIPT = "SCRIPT",
19
+ DECISION = "DECISION",
20
+ CONDITION = "CONDITION",
21
+ PARALLEL = "PARALLEL",
22
+ LOOP = "LOOP",
23
+ API = "API",
24
+ SUBFLOW = "SUBFLOW",
25
+ LLM = "LLM",
26
+ BUSINESS = "BUSINESS"
27
+ }
28
+ /**
29
+ * 边定义
30
+ */
31
+ interface EdgeDef {
32
+ id: string;
33
+ name?: string;
34
+ from: string;
35
+ to: string;
36
+ condition?: string;
37
+ metadata?: Record<string, any>;
38
+ }
39
+ /**
40
+ * 节点配置定义
41
+ */
42
+ interface NodeConfigDef {
43
+ script?: string;
44
+ api?: {
45
+ url: string;
46
+ method: string;
47
+ headers?: Record<string, string>;
48
+ body?: string;
49
+ };
50
+ loop?: {
51
+ condition?: string;
52
+ items?: string;
53
+ endNodeId?: string;
54
+ };
55
+ inputSchema?: Record<string, any>;
56
+ outputSchema?: Record<string, any>;
57
+ error?: {
58
+ mode: 'throw' | 'fail' | string;
59
+ };
60
+ [key: string]: any;
61
+ }
62
+ /**
63
+ * 节点定义
64
+ */
65
+ interface NodeDef {
66
+ id: string;
67
+ type: string | NodeType;
68
+ name?: string;
69
+ code?: string;
70
+ config?: NodeConfigDef;
71
+ metadata?: Record<string, any>;
72
+ }
73
+ /**
74
+ * 流程定义
75
+ */
76
+ interface FlowDefinition {
77
+ id: string;
78
+ name: string;
79
+ version: string;
80
+ nodes: NodeDef[];
81
+ edges: EdgeDef[];
82
+ metadata?: Record<string, any>;
83
+ convergeMap?: Record<string, string>;
84
+ loopBodyPaths?: Record<string, string[]>;
85
+ }
86
+ /**
87
+ * 节点执行结果
88
+ */
89
+ interface NodeResult {
90
+ success: boolean;
91
+ data?: any;
92
+ message?: string;
93
+ code?: string;
94
+ }
95
+ /**
96
+ * 流程执行结果
97
+ */
98
+ interface FlowResult {
99
+ executionId: string;
100
+ status: NodeStatus;
101
+ output?: any;
102
+ error?: string;
103
+ duration: number;
104
+ startTime: number;
105
+ endTime?: number;
106
+ variables: Record<string, any>;
107
+ traces: any[];
108
+ }
109
+
110
+ interface ExecutionTrace {
111
+ nodeId: string;
112
+ /** 节点业务编码(如 DECISION_abc),用于 UI 图标和 nodeMap 查询 */
113
+ code?: string;
114
+ status: NodeStatus;
115
+ startTime: number;
116
+ endTime?: number;
117
+ /** 耗时毫秒 */
118
+ duration?: number;
119
+ /** 耗时纳秒(与 Java 后端格式对齐,供 ExecutionResultModal 使用)*/
120
+ durationNs?: number;
121
+ data?: any;
122
+ error?: string;
123
+ }
124
+ declare class FlowContext {
125
+ private executionId;
126
+ private definition;
127
+ private variables;
128
+ private status;
129
+ private startTime;
130
+ private endTime?;
131
+ private nodeMap;
132
+ private outgoingMap;
133
+ private incomingMap;
134
+ private completedNodes;
135
+ private executingNodes;
136
+ private skippedNodes;
137
+ private nodeOutputs;
138
+ private convergeStates;
139
+ private traces;
140
+ constructor(executionId: string, definition: FlowDefinition, variables: Record<string, any>);
141
+ /** 构造时预建索引,所有后续查询均为 O(1) */
142
+ private buildIndex;
143
+ getExecutionId(): string;
144
+ getDefinition(): FlowDefinition;
145
+ getVariables(): {
146
+ [x: string]: any;
147
+ };
148
+ getStatus(): NodeStatus;
149
+ getStartTime(): number;
150
+ getEndTime(): number | undefined;
151
+ getDuration(): number;
152
+ getNodes(): NodeDef[];
153
+ getNode(id: string): NodeDef | undefined;
154
+ getOutgoing(nodeId: string): EdgeDef[];
155
+ getIncoming(nodeId: string): EdgeDef[];
156
+ getCompletedNodes(): Set<string>;
157
+ getSkippedNodes(): Set<string>;
158
+ getExecutingNodes(): Set<string>;
159
+ getNodeOutputs(): {
160
+ [x: string]: any;
161
+ };
162
+ getTraces(): ExecutionTrace[];
163
+ setStatus(status: NodeStatus): void;
164
+ /** 设置单个变量(供 LoopExecutor 写入 item/index 等迭代变量) */
165
+ setVariable(key: string, value: any): void;
166
+ skipNode(nodeId: string): void;
167
+ /** 注册并行汇聚点,expected = 分支数 */
168
+ registerConvergence(nodeId: string, expected: number): void;
169
+ arePrerequisitesMet(nodeId: string): boolean;
170
+ private isEffectivelySkipped;
171
+ tryExecute(nodeId: string): boolean;
172
+ complete(nodeId: string, output: any, startTime: number): void;
173
+ fail(nodeId: string, error: Error | string, startTime: number): void;
174
+ /** 并行汇聚:返回 true 代表当前节点所有分支已全部到达 */
175
+ tryConverge(nodeId: string, fromNodeId: string): boolean;
176
+ /** 获取 LOOP_START 节点的循环体路径(来自解析器预计算的 loopBodyPaths) */
177
+ getLoopBodyPath(loopStartNodeId: string): string[];
178
+ }
179
+
180
+ type EventHandler = (...args: any[]) => void;
181
+ declare class SimpleEmitter {
182
+ private handlers;
183
+ on(event: string, handler: EventHandler): this;
184
+ once(event: string, handler: EventHandler): this;
185
+ off(event: string, handler: EventHandler): this;
186
+ emit(event: string, ...args: any[]): this;
187
+ }
188
+ declare class FlowEngine extends SimpleEmitter {
189
+ private activeContexts;
190
+ constructor();
191
+ execute(definition: FlowDefinition, variables?: Record<string, any>): Promise<FlowResult>;
192
+ private scheduleNode;
193
+ private runNode;
194
+ /**
195
+ * 触发下游节点。有汇聚时,等所有【非跳过】入边到达后才 schedule。
196
+ */
197
+ private triggerDownstream;
198
+ private handleFailure;
199
+ private buildResult;
200
+ }
201
+
202
+ interface NodeExecutor {
203
+ execute(node: NodeDef, context: FlowContext): Promise<NodeResult>;
204
+ }
205
+ declare class NodeExecutorFactory {
206
+ static execute(node: NodeDef, context: FlowContext): Promise<NodeResult>;
207
+ }
208
+
209
+ interface X6Node {
210
+ id: string;
211
+ shape: string;
212
+ position?: {
213
+ x: number;
214
+ y: number;
215
+ };
216
+ data?: {
217
+ meta?: Record<string, any>;
218
+ config?: Record<string, any>;
219
+ };
220
+ }
221
+ interface X6Edge {
222
+ id?: string;
223
+ source: string | {
224
+ cell: string;
225
+ port?: string;
226
+ };
227
+ target: string | {
228
+ cell: string;
229
+ port?: string;
230
+ };
231
+ data?: {
232
+ config?: {
233
+ branchResult?: string;
234
+ [k: string]: any;
235
+ };
236
+ priority?: number;
237
+ };
238
+ }
239
+ interface X6Graph {
240
+ nodes: X6Node[];
241
+ edges: X6Edge[];
242
+ }
243
+ declare class X6Parser {
244
+ /**
245
+ * 将前端 X6 序列化的 JSON 解析为引擎可执行的 FlowDefinition
246
+ */
247
+ parse(x6Json: X6Graph | string): FlowDefinition;
248
+ private parseNodes;
249
+ private normalizeConfig;
250
+ private normalizeApiField;
251
+ private parseEdges;
252
+ private extractCellId;
253
+ private computeConvergeMap;
254
+ /** BFS 找所有分支的第一个公共后继节点 */
255
+ private findConvergeNode;
256
+ private computeLoopBodyPaths;
257
+ private computeSingleLoopPath;
258
+ private buildOutgoingMap;
259
+ }
260
+ /** 便捷函数:直接解析 X6 JSON → FlowDefinition */
261
+ declare function parseX6(x6Json: X6Graph | string): FlowDefinition;
262
+
263
+ declare function createLogger(tag: string): {
264
+ debug: (msg: string, ...args: any[]) => void;
265
+ info: (msg: string, ...args: any[]) => void;
266
+ warn: (msg: string, ...args: any[]) => void;
267
+ error: (msg: string, ...args: any[]) => void;
268
+ };
269
+
270
+ export { type EdgeDef, type ExecutionTrace, FlowContext, type FlowDefinition, FlowEngine, type FlowResult, type NodeConfigDef, type NodeDef, type NodeExecutor, NodeExecutorFactory, type NodeResult, NodeStatus, NodeType, X6Parser, createLogger, parseX6 };