feat(feedback): несколько шаблонов промпта у пользователя
user_settings.feedback_prompts: JSON {active, items:[{id,name,text}]}, до 20
шаблонов, валидация normalizePromptTemplates(). Активный шаблон зеркалируется
в feedback_prompt на стороне сервера, поэтому resolveFeedbackPrompt() воркера
не меняется. UI — селект, имя и кнопки «Новый/Удалить» над #fbPromptText,
старый единственный промпт превращается в первый шаблон при первом открытии.
This commit is contained in:
@@ -3889,8 +3889,8 @@ app.put('/api/settings', requireAdmin, async (req, res) => {
|
||||
});
|
||||
|
||||
// --- User settings (персональные настройки пользователя) ---
|
||||
const USER_SETTINGS_KEYS = new Set(['feedback_prompt', 'feedback_profile', 'feedback_models']);
|
||||
const USER_SETTINGS_LIMITS = { feedback_prompt: 15000, feedback_profile: 100, feedback_models: 16000 };
|
||||
const USER_SETTINGS_KEYS = new Set(['feedback_prompt', 'feedback_profile', 'feedback_models', 'feedback_prompts']);
|
||||
const USER_SETTINGS_LIMITS = { feedback_prompt: 15000, feedback_profile: 100, feedback_models: 16000, feedback_prompts: 320000 };
|
||||
|
||||
function normalizeFeedbackModels(value) {
|
||||
let arr;
|
||||
@@ -3927,6 +3927,31 @@ function normalizeFeedbackModels(value) {
|
||||
return JSON.stringify(out);
|
||||
}
|
||||
|
||||
function normalizePromptTemplates(value) {
|
||||
let data;
|
||||
try { data = JSON.parse(value); } catch (e) { throw new Error('Некорректный JSON шаблонов'); }
|
||||
if (!data || typeof data !== 'object' || Array.isArray(data)) throw new Error('Ожидался объект шаблонов');
|
||||
if (!Array.isArray(data.items)) throw new Error('Ожидался список шаблонов');
|
||||
if (data.items.length > 20) throw new Error('Не больше 20 шаблонов');
|
||||
const out = [];
|
||||
const seen = new Set();
|
||||
for (const t of data.items) {
|
||||
if (!t || typeof t !== 'object') throw new Error('Некорректный шаблон');
|
||||
const id = String(t.id || '').trim();
|
||||
const name = String(t.name || '').trim();
|
||||
const text = String(t.text == null ? '' : t.text).trim();
|
||||
if (!id || id.length > 40 || !/^[\w-]+$/.test(id)) throw new Error('Некорректный id шаблона');
|
||||
if (!name || name.length > 100) throw new Error('Название шаблона: 1–100 символов');
|
||||
if (text.length > 15000) throw new Error('Текст шаблона длиннее 15000 символов');
|
||||
if (seen.has(id)) throw new Error('Дублирующийся id шаблона');
|
||||
seen.add(id);
|
||||
out.push({ id, name, text });
|
||||
}
|
||||
let active = String(data.active || '').trim();
|
||||
if (!seen.has(active)) active = out.length ? out[0].id : '';
|
||||
return JSON.stringify({ active, items: out });
|
||||
}
|
||||
|
||||
async function userSettingsPayload(userId) {
|
||||
const { rows } = await pool.query('SELECT key, value FROM user_settings WHERE user_id = $1', [userId]);
|
||||
const settings = {};
|
||||
@@ -3957,8 +3982,32 @@ app.put('/api/user-settings', requireAuth, async (req, res) => {
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (key === 'feedback_prompts') {
|
||||
if (value) {
|
||||
try {
|
||||
entries.push([key, normalizePromptTemplates(value)]);
|
||||
} catch (e) {
|
||||
return res.status(400).json({ error: e.message || 'Некорректный список шаблонов' });
|
||||
}
|
||||
} else {
|
||||
entries.push([key, '']);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
entries.push([key, value]);
|
||||
}
|
||||
const tplIdx = entries.findIndex(e => e[0] === 'feedback_prompts');
|
||||
if (tplIdx !== -1) {
|
||||
let mirror = '';
|
||||
if (entries[tplIdx][1]) {
|
||||
const parsed = JSON.parse(entries[tplIdx][1]);
|
||||
const act = parsed.items.find(t => t.id === parsed.active);
|
||||
mirror = act ? act.text : '';
|
||||
}
|
||||
const prev = entries.findIndex(e => e[0] === 'feedback_prompt');
|
||||
if (prev !== -1) entries[prev][1] = mirror;
|
||||
else entries.push(['feedback_prompt', mirror]);
|
||||
}
|
||||
try {
|
||||
for (const [key, value] of entries) {
|
||||
if (value === '') {
|
||||
|
||||
Reference in New Issue
Block a user