File size: 7,161 Bytes
4c025e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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,
};