Repository navigation
Expand file tree
/
Copy pathworkflow.service.ts
More file actions
292 lines (254 loc) · 10.1 KB
/
Copy pathworkflow.service.ts
File metadata and controls
292 lines (254 loc) · 10.1 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import { createWorkflowEngine, type ExecutionLogEntry, type WorkflowDefinition } from '@ai-workflow/ai-engine'
import { BadRequestException, Injectable, Logger, NotFoundException } from '@nestjs/common'
import { randomUUID } from 'crypto'
import { PrismaService } from '../../prisma/prisma.service'
import type { RunWorkflowDto, WorkflowExecutionResultDto } from './dto/run-workflow.dto'
// SSE 事件类型
export interface SSEEvent {
type: 'node:start' | 'node:end' | 'log' | 'complete' | 'error'
data: unknown
}
// SSE 回调函数类型
export type SSECallback = (event: SSEEvent) => void
// 执行上下文
export interface ExecutionContext {
appId: string
activePublishedId: string | null
apiKeyId: string
}
@Injectable()
export class WorkflowService {
private readonly logger = new Logger(WorkflowService.name)
constructor(private readonly prisma: PrismaService) {}
/**
* 执行工作流(同步模式)
* 使用 PublishedApp 获取工作流定义,执行记录存入 AppExecution
*/
async runWorkflow(context: ExecutionContext, dto: RunWorkflowDto): Promise<WorkflowExecutionResultDto> {
const { appId, activePublishedId, apiKeyId } = context
// 检查是否有激活的发布版本
if (!activePublishedId) {
throw new BadRequestException({
code: 'APP_NOT_PUBLISHED',
message: '应用未发布,请先发布应用',
})
}
// 获取发布的应用版本
const publishedApp = await this.prisma.publishedApp.findUnique({
where: { id: activePublishedId },
})
if (!publishedApp) {
throw new NotFoundException({
code: 'PUBLISHED_APP_NOT_FOUND',
message: '发布版本不存在',
})
}
// 生成执行 ID
const executionId = `exec_${randomUUID().replace(/-/g, '').slice(0, 16)}`
const startTime = Date.now()
// 创建执行记录(使用 AppExecution)
const execution = await this.prisma.appExecution.create({
data: {
executionId,
publishedAppId: activePublishedId,
apiKeyId,
inputs: dto.inputs as object,
status: 'RUNNING',
},
})
try {
this.logger.log(`Starting app execution: ${executionId} (published: ${publishedApp.name} v${publishedApp.version})`)
// 创建工作流引擎
const engine = createWorkflowEngine({
ollamaBaseUrl: process.env.OLLAMA_BASE_URL || 'http://localhost:11434',
verbose: process.env.NODE_ENV === 'development',
})
// 构建工作流定义(从 PublishedApp 快照获取)
const workflowDefinition: WorkflowDefinition = {
id: publishedApp.id,
name: publishedApp.name,
nodes: publishedApp.nodes as unknown as WorkflowDefinition['nodes'],
edges: publishedApp.edges as unknown as WorkflowDefinition['edges'],
}
// 执行工作流
const result = await engine.execute(workflowDefinition, dto.inputs)
const duration = Date.now() - startTime
// 更新执行记录
await this.prisma.appExecution.update({
where: { id: execution.id },
data: {
status: result.success ? 'SUCCESS' : 'ERROR',
outputs: result.outputs as object,
error: result.error?.message,
duration,
nodeTraces: result.logs as object,
completedAt: new Date(),
},
})
return {
executionId,
status: result.success ? 'SUCCESS' : 'ERROR',
outputs: result.outputs,
error: result.error?.message,
duration,
totalTokens: 0, // TODO: 从日志中统计 token
}
} catch (error) {
const duration = Date.now() - startTime
const errorMessage = error instanceof Error ? error.message : '执行失败'
// 更新执行记录
await this.prisma.appExecution.update({
where: { id: execution.id },
data: {
status: 'ERROR',
error: errorMessage,
duration,
completedAt: new Date(),
},
})
this.logger.error(`App execution failed: ${executionId}`, error)
return {
executionId,
status: 'ERROR',
error: errorMessage,
duration,
}
}
}
/**
* 执行工作流(流式模式,支持 SSE)
* 使用 PublishedApp 获取工作流定义,执行记录存入 AppExecution
*/
async runWorkflowStream(context: ExecutionContext, dto: RunWorkflowDto, onEvent: SSECallback): Promise<void> {
const { appId, activePublishedId, apiKeyId } = context
// 检查是否有激活的发布版本
if (!activePublishedId) {
onEvent({
type: 'error',
data: { code: 'APP_NOT_PUBLISHED', message: '应用未发布,请先发布应用' },
})
return
}
// 获取发布的应用版本
const publishedApp = await this.prisma.publishedApp.findUnique({
where: { id: activePublishedId },
})
if (!publishedApp) {
onEvent({
type: 'error',
data: { code: 'PUBLISHED_APP_NOT_FOUND', message: '发布版本不存在' },
})
return
}
// 生成执行 ID
const executionId = `exec_${randomUUID().replace(/-/g, '').slice(0, 16)}`
const startTime = Date.now()
// 创建执行记录(使用 AppExecution)
const execution = await this.prisma.appExecution.create({
data: {
executionId,
publishedAppId: activePublishedId,
apiKeyId,
inputs: dto.inputs as object,
status: 'RUNNING',
},
})
try {
this.logger.log(`Starting app stream execution: ${executionId} (published: ${publishedApp.name} v${publishedApp.version})`)
// 创建工作流引擎
const engine = createWorkflowEngine({
ollamaBaseUrl: process.env.OLLAMA_BASE_URL || 'http://localhost:11434',
verbose: true,
})
// 构建工作流定义(从 PublishedApp 快照获取)
const workflowDefinition: WorkflowDefinition = {
id: publishedApp.id,
name: publishedApp.name,
nodes: publishedApp.nodes as unknown as WorkflowDefinition['nodes'],
edges: publishedApp.edges as unknown as WorkflowDefinition['edges'],
}
// 执行工作流(带实时回调)
const result = await engine.execute(workflowDefinition, dto.inputs, {
onNodeStart: (nodeId, nodeType, nodeName) => {
onEvent({
type: 'node:start',
data: { nodeId, nodeType, nodeName, executionId },
})
},
onNodeEnd: (nodeId, nodeResult) => {
onEvent({
type: 'node:end',
data: {
nodeId,
success: nodeResult.success,
outputs: nodeResult.outputs,
duration: nodeResult.duration,
executionId,
},
})
},
onLog: (entry: ExecutionLogEntry) => {
onEvent({
type: 'log',
data: {
timestamp: entry.timestamp,
level: entry.level,
phase: entry.phase,
message: entry.message,
nodeId: entry.nodeId,
executionId,
},
})
},
})
const duration = Date.now() - startTime
// 更新执行记录
await this.prisma.appExecution.update({
where: { id: execution.id },
data: {
status: result.success ? 'SUCCESS' : 'ERROR',
outputs: result.outputs as object,
error: result.error?.message,
duration,
nodeTraces: result.logs as object,
completedAt: new Date(),
},
})
// 发送完成事件
onEvent({
type: 'complete',
data: {
executionId,
status: result.success ? 'SUCCESS' : 'ERROR',
outputs: result.outputs,
error: result.error?.message,
duration,
},
})
} catch (error) {
const duration = Date.now() - startTime
const errorMessage = error instanceof Error ? error.message : '执行失败'
// 更新执行记录
await this.prisma.appExecution.update({
where: { id: execution.id },
data: {
status: 'ERROR',
error: errorMessage,
duration,
completedAt: new Date(),
},
})
this.logger.error(`App stream execution failed: ${executionId}`, error)
// 发送错误事件
onEvent({
type: 'error',
data: {
executionId,
code: 'EXECUTION_ERROR',
message: errorMessage,
duration,
},
})
}
}
}