Spaces:
Sleeping
Sleeping
| import { Router, type Response } from 'express'; | |
| import { verifyToken } from './auth.js'; | |
| import db from '../lib/db.js'; | |
| import crypto from 'crypto'; | |
| import { WorkflowService } from '../services/workflow.service.js'; | |
| import { addJob } from '../lib/queue.js'; | |
| const router = Router(); | |
| /** | |
| * 保存工作流 | |
| */ | |
| router.post('/save', verifyToken, (req: any, res) => { | |
| const { id, name, nodes, edges } = req.body; | |
| const userId = req.user.userId; | |
| const workflowId = id || `WF_${crypto.randomBytes(4).toString('hex').toUpperCase()}`; | |
| const data = JSON.stringify({ nodes, edges }); | |
| try { | |
| const existing = db.prepare('SELECT id FROM workflows WHERE id = ? AND user_id = ?').get(workflowId, userId); | |
| if (existing) { | |
| db.prepare(` | |
| UPDATE workflows | |
| SET name = ?, data = ?, updated_at = CURRENT_TIMESTAMP | |
| WHERE id = ? AND user_id = ? | |
| `).run(name, data, workflowId, userId); | |
| } else { | |
| db.prepare(` | |
| INSERT INTO workflows (id, user_id, name, data) | |
| VALUES (?, ?, ?, ?) | |
| `).run(workflowId, userId, name, data); | |
| } | |
| res.json({ success: true, id: workflowId }); | |
| } catch (err: any) { | |
| res.status(500).json({ success: false, error: err.message }); | |
| } | |
| }); | |
| /** | |
| * 获取工作流列表 | |
| */ | |
| router.get('/list', verifyToken, (req: any, res) => { | |
| const userId = req.user.userId; | |
| const workflows = db.prepare('SELECT id, name, updated_at FROM workflows WHERE user_id = ? ORDER BY updated_at DESC').all(userId); | |
| res.json({ success: true, workflows }); | |
| }); | |
| /** | |
| * 获取单个工作流详情 | |
| */ | |
| router.get('/:id', verifyToken, (req: any, res) => { | |
| const { id } = req.params; | |
| const userId = req.user.userId; | |
| const workflow = db.prepare('SELECT * FROM workflows WHERE id = ? AND user_id = ?').get(id, userId) as any; | |
| if (!workflow) return res.status(404).json({ success: false, error: '工作流不存在' }); | |
| res.json({ | |
| success: true, | |
| workflow: { | |
| ...workflow, | |
| data: JSON.parse(workflow.data) | |
| } | |
| }); | |
| }); | |
| /** | |
| * 同步执行工作流 (适合短任务) | |
| */ | |
| router.post('/run-sync', verifyToken, async (req: any, res) => { | |
| const { workflow, input } = req.body; | |
| try { | |
| const result = await WorkflowService.execute(workflow, input); | |
| res.json({ success: true, result }); | |
| } catch (err: any) { | |
| res.status(500).json({ success: false, error: err.message }); | |
| } | |
| }); | |
| /** | |
| * 异步执行工作流 (基于 BullMQ) | |
| */ | |
| router.post('/run-async', verifyToken, async (req: any, res) => { | |
| const { workflow, input } = req.body; | |
| const userId = req.user.userId; | |
| try { | |
| const job = await addJob('ai_workflow', { | |
| workflowId: workflow.id, | |
| workflow, | |
| input, | |
| userId | |
| }); | |
| res.json({ success: true, jobId: job.id }); | |
| } catch (err: any) { | |
| res.status(500).json({ success: false, error: err.message }); | |
| } | |
| }); | |
| /** | |
| * 智能生成工作流 (Copilot) | |
| */ | |
| router.post('/generate', verifyToken, async (req: any, res) => { | |
| const { prompt } = req.body; | |
| if (!prompt) return res.status(400).json({ success: false, error: '请描述工作流需求' }); | |
| try { | |
| const workflow = await WorkflowService.generateWorkflowFromPrompt(prompt); | |
| res.json({ success: true, workflow }); | |
| } catch (err: any) { | |
| res.status(500).json({ success: false, error: err.message }); | |
| } | |
| }); | |
| /** | |
| * 删除工作流 | |
| */ | |
| router.delete('/:id', verifyToken, (req: any, res) => { | |
| const { id } = req.params; | |
| const userId = req.user.userId; | |
| try { | |
| db.prepare('DELETE FROM workflows WHERE id = ? AND user_id = ?').run(id, userId); | |
| res.json({ success: true, message: '工作流已删除' }); | |
| } catch (err: any) { | |
| res.status(500).json({ success: false, error: err.message }); | |
| } | |
| }); | |
| export default router; | |