Spaces:
Sleeping
Sleeping
Upload 18 files
Browse files
controllers/promptGroupController.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
const asyncHandler = require('express-async-handler');
|
| 2 |
const PromptGroup = require('../models/PromptGroup');
|
|
|
|
| 3 |
|
| 4 |
// @desc 获取所有提示词组
|
| 5 |
// @route GET /api/prompt-groups
|
|
@@ -180,15 +181,40 @@ const deletePrompt = asyncHandler(async (req, res) => {
|
|
| 180 |
// @route POST /api/prompt-groups/:id/dsl-files
|
| 181 |
// @access Private
|
| 182 |
const addDslFileToGroup = asyncHandler(async (req, res) => {
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
-
if (promptGroup) {
|
| 188 |
const newDslFile = {
|
| 189 |
-
name,
|
| 190 |
fileData,
|
| 191 |
-
mimeType: mimeType || 'application/
|
| 192 |
uploadedAt: new Date(),
|
| 193 |
};
|
| 194 |
|
|
@@ -196,10 +222,67 @@ const addDslFileToGroup = asyncHandler(async (req, res) => {
|
|
| 196 |
promptGroup.updatedAt = new Date();
|
| 197 |
|
| 198 |
const updatedPromptGroup = await promptGroup.save();
|
|
|
|
|
|
|
| 199 |
res.status(201).json(updatedPromptGroup.dslFiles[updatedPromptGroup.dslFiles.length - 1]);
|
| 200 |
-
}
|
| 201 |
-
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
}
|
| 204 |
});
|
| 205 |
|
|
@@ -242,5 +325,6 @@ module.exports = {
|
|
| 242 |
updatePrompt,
|
| 243 |
deletePrompt,
|
| 244 |
addDslFileToGroup,
|
|
|
|
| 245 |
deleteDslFile,
|
| 246 |
};
|
|
|
|
| 1 |
const asyncHandler = require('express-async-handler');
|
| 2 |
const PromptGroup = require('../models/PromptGroup');
|
| 3 |
+
const logger = require('../utils/logger');
|
| 4 |
|
| 5 |
// @desc 获取所有提示词组
|
| 6 |
// @route GET /api/prompt-groups
|
|
|
|
| 181 |
// @route POST /api/prompt-groups/:id/dsl-files
|
| 182 |
// @access Private
|
| 183 |
const addDslFileToGroup = asyncHandler(async (req, res) => {
|
| 184 |
+
try {
|
| 185 |
+
const { name, fileData, mimeType } = req.body;
|
| 186 |
+
|
| 187 |
+
logger.info(`正在添加DSL文件: ${name}`);
|
| 188 |
+
|
| 189 |
+
// 验证请求数据
|
| 190 |
+
if (!name || !fileData) {
|
| 191 |
+
res.status(400);
|
| 192 |
+
throw new Error('文件名和文件数据是必需的');
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
const promptGroup = await PromptGroup.findById(req.params.id);
|
| 196 |
|
| 197 |
+
if (!promptGroup) {
|
| 198 |
+
res.status(404);
|
| 199 |
+
throw new Error('提示词组未找到');
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
+
// 验证文件数据格式
|
| 203 |
+
if (typeof fileData !== 'string') {
|
| 204 |
+
res.status(400);
|
| 205 |
+
throw new Error('文件数据必须是字符串');
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
// 确保文件名以 .yml 结尾
|
| 209 |
+
let fileName = name;
|
| 210 |
+
if (!fileName.toLowerCase().endsWith('.yml')) {
|
| 211 |
+
fileName = fileName.replace(/\.[^/.]+$/, '') + '.yml';
|
| 212 |
+
}
|
| 213 |
|
|
|
|
| 214 |
const newDslFile = {
|
| 215 |
+
name: fileName,
|
| 216 |
fileData,
|
| 217 |
+
mimeType: mimeType || 'application/x-yaml',
|
| 218 |
uploadedAt: new Date(),
|
| 219 |
};
|
| 220 |
|
|
|
|
| 222 |
promptGroup.updatedAt = new Date();
|
| 223 |
|
| 224 |
const updatedPromptGroup = await promptGroup.save();
|
| 225 |
+
logger.info(`DSL文件添加成功: ${fileName}`);
|
| 226 |
+
|
| 227 |
res.status(201).json(updatedPromptGroup.dslFiles[updatedPromptGroup.dslFiles.length - 1]);
|
| 228 |
+
} catch (error) {
|
| 229 |
+
logger.error(`添加DSL文件时出错: ${error.message}`);
|
| 230 |
+
if (!res.statusCode || res.statusCode === 200) {
|
| 231 |
+
res.status(500);
|
| 232 |
+
}
|
| 233 |
+
throw error;
|
| 234 |
+
}
|
| 235 |
+
});
|
| 236 |
+
|
| 237 |
+
// @desc 更新DSL文件
|
| 238 |
+
// @route PUT /api/prompt-groups/:id/dsl-files/:fileId
|
| 239 |
+
// @access Private
|
| 240 |
+
const updateDslFile = asyncHandler(async (req, res) => {
|
| 241 |
+
try {
|
| 242 |
+
const { name, fileData, mimeType } = req.body;
|
| 243 |
+
|
| 244 |
+
logger.info(`正在更新DSL文件: ${req.params.fileId}`);
|
| 245 |
+
|
| 246 |
+
const promptGroup = await PromptGroup.findById(req.params.id);
|
| 247 |
+
|
| 248 |
+
if (!promptGroup) {
|
| 249 |
+
res.status(404);
|
| 250 |
+
throw new Error('提示词组未找到');
|
| 251 |
+
}
|
| 252 |
+
|
| 253 |
+
const fileIndex = promptGroup.dslFiles.findIndex(
|
| 254 |
+
(f) => f._id.toString() === req.params.fileId
|
| 255 |
+
);
|
| 256 |
+
|
| 257 |
+
if (fileIndex === -1) {
|
| 258 |
+
res.status(404);
|
| 259 |
+
throw new Error('DSL文件未找到');
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
// 确保文件名以 .yml 结尾
|
| 263 |
+
let fileName = name || promptGroup.dslFiles[fileIndex].name;
|
| 264 |
+
if (!fileName.toLowerCase().endsWith('.yml')) {
|
| 265 |
+
fileName = fileName.replace(/\.[^/.]+$/, '') + '.yml';
|
| 266 |
+
}
|
| 267 |
+
|
| 268 |
+
// 更新文件属性
|
| 269 |
+
if (name) promptGroup.dslFiles[fileIndex].name = fileName;
|
| 270 |
+
if (fileData) promptGroup.dslFiles[fileIndex].fileData = fileData;
|
| 271 |
+
if (mimeType) promptGroup.dslFiles[fileIndex].mimeType = mimeType;
|
| 272 |
+
|
| 273 |
+
// 更新时间
|
| 274 |
+
promptGroup.updatedAt = new Date();
|
| 275 |
+
|
| 276 |
+
const updatedPromptGroup = await promptGroup.save();
|
| 277 |
+
logger.info(`DSL文件更新成功: ${fileName}`);
|
| 278 |
+
|
| 279 |
+
res.json(updatedPromptGroup.dslFiles[fileIndex]);
|
| 280 |
+
} catch (error) {
|
| 281 |
+
logger.error(`更新DSL文件时出错: ${error.message}`);
|
| 282 |
+
if (!res.statusCode || res.statusCode === 200) {
|
| 283 |
+
res.status(500);
|
| 284 |
+
}
|
| 285 |
+
throw error;
|
| 286 |
}
|
| 287 |
});
|
| 288 |
|
|
|
|
| 325 |
updatePrompt,
|
| 326 |
deletePrompt,
|
| 327 |
addDslFileToGroup,
|
| 328 |
+
updateDslFile,
|
| 329 |
deleteDslFile,
|
| 330 |
};
|
routes/promptGroupRoutes.js
CHANGED
|
@@ -10,6 +10,7 @@ const {
|
|
| 10 |
updatePrompt,
|
| 11 |
deletePrompt,
|
| 12 |
addDslFileToGroup,
|
|
|
|
| 13 |
deleteDslFile,
|
| 14 |
} = require('../controllers/promptGroupController');
|
| 15 |
const { protect } = require('../middleware/auth');
|
|
@@ -37,6 +38,7 @@ router.route('/:id/dsl-files')
|
|
| 37 |
.post(protect, addDslFileToGroup);
|
| 38 |
|
| 39 |
router.route('/:id/dsl-files/:fileId')
|
|
|
|
| 40 |
.delete(protect, deleteDslFile);
|
| 41 |
|
| 42 |
module.exports = router;
|
|
|
|
| 10 |
updatePrompt,
|
| 11 |
deletePrompt,
|
| 12 |
addDslFileToGroup,
|
| 13 |
+
updateDslFile,
|
| 14 |
deleteDslFile,
|
| 15 |
} = require('../controllers/promptGroupController');
|
| 16 |
const { protect } = require('../middleware/auth');
|
|
|
|
| 38 |
.post(protect, addDslFileToGroup);
|
| 39 |
|
| 40 |
router.route('/:id/dsl-files/:fileId')
|
| 41 |
+
.put(protect, updateDslFile)
|
| 42 |
.delete(protect, deleteDslFile);
|
| 43 |
|
| 44 |
module.exports = router;
|