Spaces:
Sleeping
Sleeping
Upload 18 files
Browse files- controllers/promptGroupController.js +117 -10
- models/PromptGroup.js +4 -4
- routes/promptGroupRoutes.js +2 -0
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
|
|
@@ -176,19 +177,60 @@ const deletePrompt = asyncHandler(async (req, res) => {
|
|
| 176 |
}
|
| 177 |
});
|
| 178 |
|
| 179 |
-
// @desc 添加DSL文件到提示词组
|
| 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 |
-
|
| 191 |
-
mimeType: mimeType || 'application/
|
| 192 |
uploadedAt: new Date(),
|
| 193 |
};
|
| 194 |
|
|
@@ -196,10 +238,74 @@ 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 +348,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
|
|
|
|
| 177 |
}
|
| 178 |
});
|
| 179 |
|
| 180 |
+
// @desc 添加DSL文件到提示词组 - 修改为支持文本内容
|
| 181 |
// @route POST /api/prompt-groups/:id/dsl-files
|
| 182 |
// @access Private
|
| 183 |
const addDslFileToGroup = asyncHandler(async (req, res) => {
|
| 184 |
+
try {
|
| 185 |
+
const { name, content, fileData, mimeType } = req.body;
|
| 186 |
+
logger.info(`正在添加DSL文件: ${name}`);
|
| 187 |
+
|
| 188 |
+
// 验证请求数据
|
| 189 |
+
if (!name) {
|
| 190 |
+
res.status(400);
|
| 191 |
+
throw new Error('文件名是必需的');
|
| 192 |
+
}
|
| 193 |
|
| 194 |
+
// 确保至少提供了文本内容或文件数据中的一个
|
| 195 |
+
if (!content && !fileData) {
|
| 196 |
+
res.status(400);
|
| 197 |
+
throw new Error('请提供YAML内容或上传文件');
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
+
const promptGroup = await PromptGroup.findById(req.params.id);
|
| 201 |
+
|
| 202 |
+
if (!promptGroup) {
|
| 203 |
+
res.status(404);
|
| 204 |
+
throw new Error('提示词组未找到');
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
// 确保文件名以 .yml 结尾
|
| 208 |
+
let fileName = name;
|
| 209 |
+
if (!fileName.toLowerCase().endsWith('.yml')) {
|
| 210 |
+
fileName = fileName.replace(/\.[^/.]+$/, '') + '.yml';
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
// 处理内容: 优先使用提供的内容,如果没有则尝试解码文件数据
|
| 214 |
+
let yamlContent = content;
|
| 215 |
+
|
| 216 |
+
// 如果提供了fileData但没有提供content,尝试将fileData转换为文本
|
| 217 |
+
if (!yamlContent && fileData) {
|
| 218 |
+
try {
|
| 219 |
+
// 尝试将Base64解码为文本
|
| 220 |
+
const buffer = Buffer.from(fileData, 'base64');
|
| 221 |
+
yamlContent = buffer.toString('utf-8');
|
| 222 |
+
logger.info(`成功从文件数据提取YAML内容`);
|
| 223 |
+
} catch (error) {
|
| 224 |
+
logger.error(`从文件数据提取内容失败: ${error.message}`);
|
| 225 |
+
res.status(400);
|
| 226 |
+
throw new Error('无法从上传的文件中提取内容');
|
| 227 |
+
}
|
| 228 |
+
}
|
| 229 |
|
|
|
|
| 230 |
const newDslFile = {
|
| 231 |
+
name: fileName,
|
| 232 |
+
content: yamlContent,
|
| 233 |
+
mimeType: mimeType || 'application/x-yaml',
|
| 234 |
uploadedAt: new Date(),
|
| 235 |
};
|
| 236 |
|
|
|
|
| 238 |
promptGroup.updatedAt = new Date();
|
| 239 |
|
| 240 |
const updatedPromptGroup = await promptGroup.save();
|
| 241 |
+
logger.info(`DSL文件添加成功: ${fileName}`);
|
| 242 |
+
|
| 243 |
res.status(201).json(updatedPromptGroup.dslFiles[updatedPromptGroup.dslFiles.length - 1]);
|
| 244 |
+
} catch (error) {
|
| 245 |
+
logger.error(`添加DSL文件时出错: ${error.message}`);
|
| 246 |
+
if (!res.statusCode || res.statusCode === 200) {
|
| 247 |
+
res.status(500);
|
| 248 |
+
}
|
| 249 |
+
throw error;
|
| 250 |
+
}
|
| 251 |
+
});
|
| 252 |
+
|
| 253 |
+
// @desc 更新DSL文件
|
| 254 |
+
// @route PUT /api/prompt-groups/:id/dsl-files/:fileId
|
| 255 |
+
// @access Private
|
| 256 |
+
const updateDslFile = asyncHandler(async (req, res) => {
|
| 257 |
+
try {
|
| 258 |
+
const { name, content } = req.body;
|
| 259 |
+
|
| 260 |
+
if (!name && !content) {
|
| 261 |
+
res.status(400);
|
| 262 |
+
throw new Error('请提供要更新的文件名或内容');
|
| 263 |
+
}
|
| 264 |
+
|
| 265 |
+
const promptGroup = await PromptGroup.findById(req.params.id);
|
| 266 |
+
|
| 267 |
+
if (!promptGroup) {
|
| 268 |
+
res.status(404);
|
| 269 |
+
throw new Error('提示词组未找到');
|
| 270 |
+
}
|
| 271 |
+
|
| 272 |
+
const fileIndex = promptGroup.dslFiles.findIndex(
|
| 273 |
+
file => file._id.toString() === req.params.fileId
|
| 274 |
+
);
|
| 275 |
+
|
| 276 |
+
if (fileIndex === -1) {
|
| 277 |
+
res.status(404);
|
| 278 |
+
throw new Error('DSL文件未找到');
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
// 更新文件名 - 确保以.yml结尾
|
| 282 |
+
if (name) {
|
| 283 |
+
let updatedName = name;
|
| 284 |
+
if (!updatedName.toLowerCase().endsWith('.yml')) {
|
| 285 |
+
updatedName = updatedName.replace(/\.[^/.]+$/, '') + '.yml';
|
| 286 |
+
}
|
| 287 |
+
promptGroup.dslFiles[fileIndex].name = updatedName;
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
// 更新内容
|
| 291 |
+
if (content) {
|
| 292 |
+
promptGroup.dslFiles[fileIndex].content = content;
|
| 293 |
+
}
|
| 294 |
+
|
| 295 |
+
// 更新时间戳
|
| 296 |
+
promptGroup.dslFiles[fileIndex].uploadedAt = new Date();
|
| 297 |
+
promptGroup.updatedAt = new Date();
|
| 298 |
+
|
| 299 |
+
const updatedPromptGroup = await promptGroup.save();
|
| 300 |
+
logger.info(`DSL文件更新成功: ${promptGroup.dslFiles[fileIndex].name}`);
|
| 301 |
+
|
| 302 |
+
res.status(200).json(updatedPromptGroup.dslFiles[fileIndex]);
|
| 303 |
+
} catch (error) {
|
| 304 |
+
logger.error(`更新DSL文件时出错: ${error.message}`);
|
| 305 |
+
if (!res.statusCode || res.statusCode === 200) {
|
| 306 |
+
res.status(500);
|
| 307 |
+
}
|
| 308 |
+
throw error;
|
| 309 |
}
|
| 310 |
});
|
| 311 |
|
|
|
|
| 348 |
updatePrompt,
|
| 349 |
deletePrompt,
|
| 350 |
addDslFileToGroup,
|
| 351 |
+
updateDslFile,
|
| 352 |
deleteDslFile,
|
| 353 |
};
|
models/PromptGroup.js
CHANGED
|
@@ -24,7 +24,7 @@ const promptSchema = new mongoose.Schema(
|
|
| 24 |
}
|
| 25 |
);
|
| 26 |
|
| 27 |
-
// DSL 文件 Schema
|
| 28 |
const dslFileSchema = new mongoose.Schema(
|
| 29 |
{
|
| 30 |
name: {
|
|
@@ -32,13 +32,13 @@ const dslFileSchema = new mongoose.Schema(
|
|
| 32 |
required: [true, '请输入文件名称'],
|
| 33 |
trim: true,
|
| 34 |
},
|
| 35 |
-
|
| 36 |
type: String,
|
| 37 |
-
required: [true, '请提供
|
| 38 |
},
|
| 39 |
mimeType: {
|
| 40 |
type: String,
|
| 41 |
-
default: 'application/
|
| 42 |
},
|
| 43 |
uploadedAt: {
|
| 44 |
type: Date,
|
|
|
|
| 24 |
}
|
| 25 |
);
|
| 26 |
|
| 27 |
+
// DSL 文件 Schema - 修改以存储文本内容
|
| 28 |
const dslFileSchema = new mongoose.Schema(
|
| 29 |
{
|
| 30 |
name: {
|
|
|
|
| 32 |
required: [true, '请输入文件名称'],
|
| 33 |
trim: true,
|
| 34 |
},
|
| 35 |
+
content: { // 添加: 存储 YAML 文本内容
|
| 36 |
type: String,
|
| 37 |
+
required: [true, '请提供 YAML 内容'],
|
| 38 |
},
|
| 39 |
mimeType: {
|
| 40 |
type: String,
|
| 41 |
+
default: 'application/x-yaml',
|
| 42 |
},
|
| 43 |
uploadedAt: {
|
| 44 |
type: Date,
|
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;
|