Spaces:
Sleeping
Sleeping
const asyncHandler = require('express-async-handler'); | |
const PromptGroup = require('../models/PromptGroup'); | |
// @desc 获取所有提示词组 | |
// @route GET /api/prompt-groups | |
// @access Private | |
const getPromptGroups = asyncHandler(async (req, res) => { | |
const promptGroups = await PromptGroup.find({}) | |
.populate('category', 'name color') | |
.sort({ updatedAt: -1 }); | |
res.json(promptGroups); | |
}); | |
// @desc 获取单个提示词组 | |
// @route GET /api/prompt-groups/:id | |
// @access Private | |
const getPromptGroupById = asyncHandler(async (req, res) => { | |
const promptGroup = await PromptGroup.findById(req.params.id) | |
.populate('category', 'name color'); | |
if (promptGroup) { | |
res.json(promptGroup); | |
} else { | |
res.status(404); | |
throw new Error('提示词组未找到'); | |
} | |
}); | |
// @desc 创建提示词组 | |
// @route POST /api/prompt-groups | |
// @access Private | |
const createPromptGroup = asyncHandler(async (req, res) => { | |
const { name, description, category } = req.body; | |
const promptGroup = await PromptGroup.create({ | |
name, | |
description, | |
category, | |
prompts: [], | |
workflows: [], | |
dslFiles: [], | |
createdBy: req.user._id, | |
}); | |
if (promptGroup) { | |
res.status(201).json(promptGroup); | |
} else { | |
res.status(400); | |
throw new Error('无效的提示词组数据'); | |
} | |
}); | |
// @desc 更新提示词组 | |
// @route PUT /api/prompt-groups/:id | |
// @access Private | |
const updatePromptGroup = asyncHandler(async (req, res) => { | |
const { name, description, category } = req.body; | |
const promptGroup = await PromptGroup.findById(req.params.id); | |
if (promptGroup) { | |
promptGroup.name = name || promptGroup.name; | |
promptGroup.description = description || promptGroup.description; | |
promptGroup.category = category || promptGroup.category; | |
const updatedPromptGroup = await promptGroup.save(); | |
res.json(updatedPromptGroup); | |
} else { | |
res.status(404); | |
throw new Error('提示词组未找到'); | |
} | |
}); | |
// @desc 删除提示词组 | |
// @route DELETE /api/prompt-groups/:id | |
// @access Private | |
const deletePromptGroup = asyncHandler(async (req, res) => { | |
const promptGroup = await PromptGroup.findById(req.params.id); | |
if (promptGroup) { | |
await promptGroup.deleteOne(); | |
res.json({ message: '提示词组已删除' }); | |
} else { | |
res.status(404); | |
throw new Error('提示词组未找到'); | |
} | |
}); | |
// @desc 添加提示词到提示词组 | |
// @route POST /api/prompt-groups/:id/prompts | |
// @access Private | |
const addPromptToGroup = asyncHandler(async (req, res) => { | |
const { title, content, tags } = req.body; | |
const promptGroup = await PromptGroup.findById(req.params.id); | |
if (promptGroup) { | |
const newPrompt = { | |
title, | |
content, | |
tags: tags || [], | |
createdAt: new Date(), | |
updatedAt: new Date(), | |
}; | |
promptGroup.prompts.push(newPrompt); | |
promptGroup.updatedAt = new Date(); | |
const updatedPromptGroup = await promptGroup.save(); | |
res.status(201).json(updatedPromptGroup.prompts[updatedPromptGroup.prompts.length - 1]); | |
} else { | |
res.status(404); | |
throw new Error('提示词组未找到'); | |
} | |
}); | |
// @desc 更新提示词 | |
// @route PUT /api/prompt-groups/:id/prompts/:promptId | |
// @access Private | |
const updatePrompt = asyncHandler(async (req, res) => { | |
const { title, content, tags } = req.body; | |
const promptGroup = await PromptGroup.findById(req.params.id); | |
if (promptGroup) { | |
const promptIndex = promptGroup.prompts.findIndex( | |
(p) => p._id.toString() === req.params.promptId | |
); | |
if (promptIndex !== -1) { | |
promptGroup.prompts[promptIndex].title = title || promptGroup.prompts[promptIndex].title; | |
promptGroup.prompts[promptIndex].content = content || promptGroup.prompts[promptIndex].content; | |
promptGroup.prompts[promptIndex].tags = tags || promptGroup.prompts[promptIndex].tags; | |
promptGroup.prompts[promptIndex].updatedAt = new Date(); | |
promptGroup.updatedAt = new Date(); | |
const updatedPromptGroup = await promptGroup.save(); | |
res.json(updatedPromptGroup.prompts[promptIndex]); | |
} else { | |
res.status(404); | |
throw new Error('提示词未找到'); | |
} | |
} else { | |
res.status(404); | |
throw new Error('提示词组未找到'); | |
} | |
}); | |
// @desc 删除提示词 | |
// @route DELETE /api/prompt-groups/:id/prompts/:promptId | |
// @access Private | |
const deletePrompt = asyncHandler(async (req, res) => { | |
const promptGroup = await PromptGroup.findById(req.params.id); | |
if (promptGroup) { | |
const promptExists = promptGroup.prompts.some( | |
(p) => p._id.toString() === req.params.promptId | |
); | |
if (promptExists) { | |
promptGroup.prompts = promptGroup.prompts.filter( | |
(p) => p._id.toString() !== req.params.promptId | |
); | |
promptGroup.updatedAt = new Date(); | |
await promptGroup.save(); | |
res.json({ message: '提示词已删除' }); | |
} else { | |
res.status(404); | |
throw new Error('提示词未找到'); | |
} | |
} else { | |
res.status(404); | |
throw new Error('提示词组未找到'); | |
} | |
}); | |
// @desc 添加DSL文件到提示词组 | |
// @route POST /api/prompt-groups/:id/dsl-files | |
// @access Private | |
const addDslFileToGroup = asyncHandler(async (req, res) => { | |
const { name, fileData, mimeType } = req.body; | |
const promptGroup = await PromptGroup.findById(req.params.id); | |
if (promptGroup) { | |
const newDslFile = { | |
name, | |
fileData, | |
mimeType: mimeType || 'application/octet-stream', | |
uploadedAt: new Date(), | |
}; | |
promptGroup.dslFiles.push(newDslFile); | |
promptGroup.updatedAt = new Date(); | |
const updatedPromptGroup = await promptGroup.save(); | |
res.status(201).json(updatedPromptGroup.dslFiles[updatedPromptGroup.dslFiles.length - 1]); | |
} else { | |
res.status(404); | |
throw new Error('提示词组未找到'); | |
} | |
}); | |
// @desc 删除DSL文件 | |
// @route DELETE /api/prompt-groups/:id/dsl-files/:fileId | |
// @access Private | |
const deleteDslFile = asyncHandler(async (req, res) => { | |
const promptGroup = await PromptGroup.findById(req.params.id); | |
if (promptGroup) { | |
const fileExists = promptGroup.dslFiles.some( | |
(f) => f._id.toString() === req.params.fileId | |
); | |
if (fileExists) { | |
promptGroup.dslFiles = promptGroup.dslFiles.filter( | |
(f) => f._id.toString() !== req.params.fileId | |
); | |
promptGroup.updatedAt = new Date(); | |
await promptGroup.save(); | |
res.json({ message: 'DSL文件已删除' }); | |
} else { | |
res.status(404); | |
throw new Error('DSL文件未找到'); | |
} | |
} else { | |
res.status(404); | |
throw new Error('提示词组未找到'); | |
} | |
}); | |
module.exports = { | |
getPromptGroups, | |
getPromptGroupById, | |
createPromptGroup, | |
updatePromptGroup, | |
deletePromptGroup, | |
addPromptToGroup, | |
updatePrompt, | |
deletePrompt, | |
addDslFileToGroup, | |
deleteDslFile, | |
}; |