feat(feedback): несколько шаблонов промпта у пользователя

user_settings.feedback_prompts: JSON {active, items:[{id,name,text}]}, до 20
шаблонов, валидация normalizePromptTemplates(). Активный шаблон зеркалируется
в feedback_prompt на стороне сервера, поэтому resolveFeedbackPrompt() воркера
не меняется. UI — селект, имя и кнопки «Новый/Удалить» над #fbPromptText,
старый единственный промпт превращается в первый шаблон при первом открытии.
This commit is contained in:
dev
2026-10-08 16:25:41 +03:00
parent 355feb234b
commit 2b60a69bd4
4 changed files with 149 additions and 9 deletions
+11
View File
@@ -28,6 +28,11 @@
.fb-keys-details[open] summary { border-bottom:1px solid var(--border); }
.fb-keys-details .fb-keys-body { display:flex; flex-direction:column; gap:10px; padding:12px 14px; }
#fbPromptText { min-height:48vh; resize:vertical; }
.fb-tpl-row { display:flex; gap:8px; align-items:center; flex-wrap:wrap; margin-bottom:8px; }
.fb-tpl-row #fbPromptTpl { flex:1 1 220px; min-width:180px; }
.fb-tpl-row #fbPromptTplName { flex:1 1 180px; min-width:150px; }
.fb-tpl-row button { flex:none; }
.fb-tpl-row button:disabled { opacity:.5; cursor:default; }
#fbPromptModal { overflow:auto; align-items:flex-start; padding:4vh 16px; }
#fbPromptModal > .edit-modal { margin:auto; }
.feedback-item { position:relative; }
@@ -209,6 +214,12 @@
</div>
<div class="settings-field">
<label>Шаблон промпта</label>
<div class="fb-tpl-row">
<select id="fbPromptTpl" class="settings-input"></select>
<input type="text" id="fbPromptTplName" class="settings-input" maxlength="100" placeholder="Название шаблона">
<button type="button" class="btn-primary ghost" id="fbPromptTplAdd">+ Новый шаблон</button>
<button type="button" class="btn-primary ghost" id="fbPromptTplDel">Удалить</button>
</div>
<textarea id="fbPromptText" class="settings-input" rows="24" maxlength="15000" placeholder="Загрузка…"></textarea>
<div class="hint"><span id="fbPromptCount">0 / 15000</span> · Пусто — используется системный шаблон</div>
</div>
+86 -7
View File
@@ -1,7 +1,7 @@
(async function() {
const API = location.origin;
const PAGE_SIZE = 20;
let page = 1, total = 0, searchTimer, groupsLoaded, feedbacks = [], shortsListCache = null, shortsFetchTimer = null, lastFbAutoText = '', userSettings = { settings: {}, defaults: {} }, aiActive = 'native', myModels = [], modelEditIdx = null;
let page = 1, total = 0, searchTimer, groupsLoaded, feedbacks = [], shortsListCache = null, shortsFetchTimer = null, lastFbAutoText = '', userSettings = { settings: {}, defaults: {} }, aiActive = 'native', myModels = [], modelEditIdx = null, promptTpls = null;
const topicReportCache = new Map();
function hdr() { const t = sessionStorage.getItem('authToken'); return t ? { 'X-Auth-Token': t } : {}; }
@@ -121,6 +121,64 @@
if (el && ta) el.textContent = `${ta.value.length} / 15000`;
}
function parsePromptTemplates() {
let data = null;
try { data = JSON.parse(userSettings.settings.feedback_prompts || ''); } catch (e) {}
let items = data && Array.isArray(data.items) ? data.items : [];
items = items
.filter(t => t && typeof t === 'object' && String(t.id || '').trim())
.map(t => ({ id: String(t.id).trim(), name: String(t.name || '').trim() || 'Шаблон', text: String(t.text || '').slice(0, 15000) }));
if (!items.length) items = [{ id: 'tpl-legacy', name: 'Шаблон', text: userSettings.settings.feedback_prompt || '' }];
let active = data ? String(data.active || '').trim() : '';
if (!items.some(t => t.id === active)) active = items[0].id;
return { active, items };
}
function renderPromptTemplates() {
const sel = document.getElementById('fbPromptTpl');
if (!sel || !promptTpls) return;
const name = document.getElementById('fbPromptTplName');
const del = document.getElementById('fbPromptTplDel');
const ta = document.getElementById('fbPromptText');
sel.innerHTML = promptTpls.items.map(t => `<option value="${escapeHtml(t.id)}">${escapeHtml(t.name)}</option>`).join('');
sel.value = promptTpls.active;
const act = promptTpls.items.find(t => t.id === promptTpls.active);
if (name) name.value = act ? act.name : '';
if (del) del.disabled = promptTpls.items.length <= 1;
if (ta && act) ta.value = act.text;
updatePromptCount();
}
function promptTextSync() {
const ta = document.getElementById('fbPromptText');
const act = promptTpls && promptTpls.items.find(t => t.id === promptTpls.active);
if (ta && act) act.text = ta.value;
updatePromptCount();
}
function addPromptTemplate() {
if (!promptTpls) return;
if (promptTpls.items.length >= 20) return alert('Не больше 20 шаблонов');
promptTextSync();
const id = `tpl-${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}`;
promptTpls.items.push({ id, name: `Шаблон ${promptTpls.items.length + 1}`, text: '' });
promptTpls.active = id;
renderPromptTemplates();
const name = document.getElementById('fbPromptTplName');
if (name) { name.focus(); name.select(); }
}
function deletePromptTemplate() {
if (!promptTpls || promptTpls.items.length <= 1) return;
promptTextSync();
const act = promptTpls.items.find(t => t.id === promptTpls.active);
if (!confirm(`Удалить шаблон «${act ? act.name : ''}»?`)) return;
const idx = promptTpls.items.findIndex(t => t.id === promptTpls.active);
promptTpls.items.splice(idx, 1);
promptTpls.active = promptTpls.items[Math.min(idx, promptTpls.items.length - 1)].id;
renderPromptTemplates();
}
async function loadUserSettings() {
try {
const r = await fetch(`${API}/api/user-settings`, { headers: hdr() });
@@ -236,13 +294,13 @@
function openPromptModal() {
fillDefaultModelOptions(userSettings.settings.feedback_profile);
promptTpls = parsePromptTemplates();
const ta = document.getElementById('fbPromptText');
ta.value = userSettings.settings.feedback_prompt || '';
ta.placeholder = userSettings.defaults.feedback_prompt || 'Системный шаблон';
renderPromptTemplates();
document.getElementById('fbPromptStatus').hidden = true;
hideModelForm();
renderModelsList();
updatePromptCount();
document.getElementById('fbPromptModal').classList.add('open');
if (window.lucide) lucide.createIcons();
}
@@ -254,9 +312,13 @@
st.innerHTML = '<span class="ai-badge error">Список моделей не загружен — сохранение отменено</span>';
return;
}
promptTextSync();
if (promptTpls) promptTpls.items.forEach(t => { t.name = String(t.name || '').trim() || 'Шаблон'; });
const activeTpl = promptTpls && promptTpls.items.find(t => t.id === promptTpls.active);
const body = {
settings: {
feedback_prompt: document.getElementById('fbPromptText').value.trim(),
feedback_prompt: activeTpl ? activeTpl.text.trim() : '',
feedback_prompts: promptTpls ? JSON.stringify(promptTpls) : '',
feedback_profile: document.getElementById('fbDefaultModel').value || '',
feedback_models: myModels.length ? JSON.stringify(myModels) : '',
}
@@ -271,6 +333,7 @@
}
userSettings.settings = j.settings || {};
userSettings.defaults = j.defaults || userSettings.defaults;
promptTpls = parsePromptTemplates();
parseMyModels();
renderModelsList();
await loadAiProfiles();
@@ -570,7 +633,23 @@
document.getElementById('fbPromptBtn').addEventListener('click', openPromptModal);
document.getElementById('fbPromptClose').addEventListener('click', closePromptModal);
document.getElementById('fbPromptModal').addEventListener('click', e => { if (e.target.id === 'fbPromptModal') closePromptModal(); });
document.getElementById('fbPromptText').addEventListener('input', updatePromptCount);
document.getElementById('fbPromptText').addEventListener('input', promptTextSync);
document.getElementById('fbPromptTpl').addEventListener('change', e => {
if (!promptTpls) return;
promptTextSync();
promptTpls.active = e.target.value;
renderPromptTemplates();
});
document.getElementById('fbPromptTplName').addEventListener('input', e => {
if (!promptTpls) return;
const act = promptTpls.items.find(t => t.id === promptTpls.active);
if (!act) return;
act.name = e.target.value;
const sel = document.getElementById('fbPromptTpl');
if (sel && sel.options[sel.selectedIndex]) sel.options[sel.selectedIndex].textContent = e.target.value;
});
document.getElementById('fbPromptTplAdd').addEventListener('click', addPromptTemplate);
document.getElementById('fbPromptTplDel').addEventListener('click', deletePromptTemplate);
document.getElementById('fbPromptKeys').addEventListener('click', e => {
const key = e.target.closest('.fb-key');
const ta = document.getElementById('fbPromptText');
@@ -582,13 +661,13 @@
ta.value = ta.value.slice(0, start) + token + ta.value.slice(end);
ta.focus();
ta.setSelectionRange(start + token.length, start + token.length);
updatePromptCount();
promptTextSync();
});
document.getElementById('fbPromptReset').addEventListener('click', () => {
document.getElementById('fbPromptText').value = '';
document.getElementById('fbDefaultModel').value = '';
document.getElementById('fbPromptStatus').hidden = true;
updatePromptCount();
promptTextSync();
});
document.getElementById('fbPromptSave').addEventListener('click', savePromptSettings);
document.getElementById('fbModelAdd').addEventListener('click', () => showModelForm(null));