-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathvalidate.ts
More file actions
200 lines (195 loc) · 6.37 KB
/
validate.ts
File metadata and controls
200 lines (195 loc) · 6.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { WorkflowType, WorkflowMode } from '@/enums/application'
import { t } from '@/locales'
const end_nodes: Array<string> = [
WorkflowType.AiChat,
WorkflowType.Reply,
WorkflowType.ToolLib,
WorkflowType.ToolLibCustom,
WorkflowType.ImageUnderstandNode,
WorkflowType.Application,
WorkflowType.SpeechToTextNode,
WorkflowType.TextToSpeechNode,
WorkflowType.ImageGenerateNode,
WorkflowType.ImageToVideoGenerateNode,
WorkflowType.TextToVideoGenerateNode,
WorkflowType.ImageGenerateNode,
WorkflowType.LoopBodyNode,
WorkflowType.LoopNode,
WorkflowType.LoopBreakNode,
]
const loop_end_nodes: Array<string> = [
WorkflowType.AiChat,
WorkflowType.Reply,
WorkflowType.ToolLib,
WorkflowType.ToolLibCustom,
WorkflowType.ImageUnderstandNode,
WorkflowType.Application,
WorkflowType.SpeechToTextNode,
WorkflowType.TextToSpeechNode,
WorkflowType.ImageGenerateNode,
WorkflowType.ImageToVideoGenerateNode,
WorkflowType.TextToVideoGenerateNode,
WorkflowType.ImageGenerateNode,
WorkflowType.LoopBodyNode,
WorkflowType.LoopNode,
WorkflowType.LoopBreakNode,
WorkflowType.VariableAssignNode,
]
const end_nodes_dict = {
[WorkflowMode.Application]: end_nodes,
[WorkflowMode.ApplicationLoop]: loop_end_nodes,
}
export class WorkFlowInstance {
nodes
edges
workFlowNodes: Array<any>
workflowModel: WorkflowMode
constructor(workflow: { nodes: Array<any>; edges: Array<any> }, workflowModel?: WorkflowMode) {
this.nodes = workflow.nodes
this.edges = workflow.edges
this.workFlowNodes = []
this.workflowModel = workflowModel ? workflowModel : WorkflowMode.Application
}
/**
* 校验开始节点
*/
private is_valid_start_node() {
const start_node_list = this.nodes.filter((item) =>
[WorkflowType.Start, WorkflowType.LoopStartNode].includes(item.id),
)
if (start_node_list.length == 0) {
throw t('views.applicationWorkflow.validate.startNodeRequired')
} else if (start_node_list.length > 1) {
throw t('views.applicationWorkflow.validate.startNodeOnly')
}
}
/**
* 校验基本信息节点
*/
private is_valid_base_node() {
const start_node_list = this.nodes.filter((item) => item.id === WorkflowType.Base)
if (start_node_list.length == 0) {
throw t('views.applicationWorkflow.validate.baseNodeRequired')
} else if (start_node_list.length > 1) {
throw t('views.applicationWorkflow.validate.baseNodeOnly')
}
}
/**
* 校验节点
*/
is_valid() {
this.is_valid_start_node()
this.is_valid_base_node()
this.is_valid_work_flow()
this.is_valid_nodes()
}
is_loop_valid() {
this.is_valid_start_node()
this.is_valid_work_flow()
this.is_valid_nodes()
}
/**
* 获取开始节点
* @returns
*/
get_start_node() {
const start_node_list = this.nodes.filter((item) =>
[WorkflowType.Start, WorkflowType.LoopStartNode].includes(item.id),
)
return start_node_list[0]
}
/**
* 获取基本节点
* @returns 基本节点
*/
get_base_node() {
const base_node_list = this.nodes.filter((item) => item.id === WorkflowType.Base)
return base_node_list[0]
}
exist_break_node() {
return this.nodes.some((item) => item.type === WorkflowType.LoopBreakNode)
}
/**
* 校验工作流
* @param up_node 上一个节点
*/
private _is_valid_work_flow(up_node?: any) {
if (!up_node) {
up_node = this.get_start_node()
}
this.workFlowNodes.push(up_node)
this.is_valid_node(up_node)
const next_nodes = this.get_next_nodes(up_node)
for (const next_node of next_nodes) {
this._is_valid_work_flow(next_node)
}
}
private is_valid_work_flow() {
this.workFlowNodes = []
this._is_valid_work_flow()
const notInWorkFlowNodes = this.nodes
.filter((node: any) => node.id !== WorkflowType.Start && node.id !== WorkflowType.Base)
.filter((node) => !this.workFlowNodes.includes(node))
if (notInWorkFlowNodes.length > 0) {
throw `${t('views.applicationWorkflow.validate.notInWorkFlowNode')}:${notInWorkFlowNodes.map((node) => node.properties.stepName).join(',')}`
}
this.workFlowNodes = []
}
/**
* 获取流程下一个节点列表
* @param node 节点
* @returns 节点列表
*/
private get_next_nodes(node: any) {
const edge_list = this.edges.filter((edge) => edge.sourceNodeId == node.id)
const node_list = edge_list
.map((edge) => this.nodes.filter((node) => node.id == edge.targetNodeId))
.reduce((x, y) => [...x, ...y], [])
const end = end_nodes_dict[this.workflowModel]
if (node_list.length == 0 && !end.includes(node.type)) {
throw t('views.applicationWorkflow.validate.noNextNode')
}
return node_list
}
private is_valid_nodes() {
for (const node of this.nodes) {
if (
node.type !== WorkflowType.Base &&
node.type !== WorkflowType.Start &&
node.type !== WorkflowType.LoopStartNode
) {
if (!this.edges.some((edge) => edge.targetNodeId === node.id)) {
throw `${t('views.applicationWorkflow.validate.notInWorkFlowNode')}:${node.properties.stepName}`
}
}
}
}
/**
* 校验节点
* @param node 节点
*/
private is_valid_node(node: any) {
if (node.properties.status && node.properties.status === 500) {
throw `${node.properties.stepName} ${t('views.applicationWorkflow.validate.nodeUnavailable')}`
}
if (node.type === WorkflowType.Condition) {
const branch_list = node.properties.node_data.branch
for (const branch of branch_list) {
const source_anchor_id = `${node.id}_${branch.id}_right`
const edge_list = this.edges.filter((edge) => edge.sourceAnchorId == source_anchor_id)
if (edge_list.length == 0) {
throw `${node.properties.stepName} ${t('views.applicationWorkflow.validate.needConnect1')}${branch.type}${t('views.applicationWorkflow.validate.needConnect2')}`
}
}
} else {
const edge_list = this.edges.filter((edge) => edge.sourceNodeId == node.id)
const end = end_nodes_dict[this.workflowModel]
if (edge_list.length == 0 && !end.includes(node.type)) {
throw `${node.properties.stepName} ${t('views.applicationWorkflow.validate.cannotEndNode')}`
}
}
if (node.properties.status && node.properties.status !== 200) {
throw `${node.properties.stepName} ${t('views.applicationWorkflow.validate.nodeUnavailable')}`
}
}
}