Await character templates before populating group dropdowns

This commit is contained in:
Strahil Peykov
2026-06-01 08:18:32 +02:00
committed by GitHub
parent 3cbfa98c30
commit aad43a050b
+12 -17
View File
@@ -81,8 +81,7 @@ function _initGroupTab() {
} }
addBtn.addEventListener('click', async () => { addBtn.addEventListener('click', async () => {
const models = await _getModels(); const [models, characters] = await Promise.all([_getModels(), _getCharacterList()]);
const characters = _getCharacterList();
const picker = document.createElement('div'); const picker = document.createElement('div');
picker.style.cssText = 'display:flex;gap:4px;align-items:center;'; picker.style.cssText = 'display:flex;gap:4px;align-items:center;';
@@ -244,13 +243,12 @@ function _initGroupTab() {
chip.title = (g.participants || []).map(p => p.characterName || p.modelDisplay || '?').join(', '); chip.title = (g.participants || []).map(p => p.characterName || p.modelDisplay || '?').join(', ');
chip.addEventListener('click', async () => { chip.addEventListener('click', async () => {
// Load preset participants // Load preset participants
const models = await _getModels(); const [models, chars] = await Promise.all([_getModels(), _getCharacterList()]);
_groupParticipants.length = 0; _groupParticipants.length = 0;
(g.participants || []).forEach(p => { (g.participants || []).forEach(p => {
const model = models.find(m => m.mid === p.modelId) || models[0]; const model = models.find(m => m.mid === p.modelId) || models[0];
const entry = { model: model || null, character: null }; const entry = { model: model || null, character: null };
if (p.characterId) { if (p.characterId) {
const chars = _getCharacterList();
entry.character = chars.find(c => c.id === p.characterId) || null; entry.character = chars.find(c => c.id === p.characterId) || null;
} }
if (entry.model) _groupParticipants.push(entry); if (entry.model) _groupParticipants.push(entry);
@@ -284,7 +282,7 @@ function _initGroupTab() {
}); });
} }
function _getCharacterList() { async function _getCharacterList() {
// Built-in characters from PROMPT_TEMPLATES // Built-in characters from PROMPT_TEMPLATES
const chars = PROMPT_TEMPLATES.filter(t => t.isCharacter).map(t => ({ const chars = PROMPT_TEMPLATES.filter(t => t.isCharacter).map(t => ({
id: t.id, name: t.name, prompt: t.prompt, id: t.id, name: t.name, prompt: t.prompt,
@@ -300,18 +298,15 @@ function _getCharacterList() {
}); });
} }
} catch (e) {} } catch (e) {}
// Also try loading user templates // Load user templates and wait for them before returning
try { try {
fetch(API_BASE + '/api/presets/templates', { credentials: 'same-origin' }) const r = await fetch(API_BASE + '/api/presets/templates', { credentials: 'same-origin' });
.then(r => r.json()) const data = await r.json();
.then(data => { (data.templates || []).forEach(t => {
(data.templates || []).forEach(t => { if (t.isCharacter && !chars.find(c => c.id === t.id)) {
if (t.isCharacter && !chars.find(c => c.id === t.id)) { chars.push({ id: t.id, name: t.name, prompt: t.prompt || '' });
chars.push({ id: t.id, name: t.name, prompt: t.prompt || '' }); }
} });
});
})
.catch(() => {});
} catch (e) {} } catch (e) {}
return chars; return chars;
} }
@@ -475,7 +470,7 @@ export async function showModelPicker() {
body.appendChild(stepTitle); body.appendChild(stepTitle);
// Build character options // Build character options
const characters = _getCharacterList(); const characters = await _getCharacterList();
const assignments = {}; // mid -> {characterId, characterName, characterPrompt} const assignments = {}; // mid -> {characterId, characterName, characterPrompt}
for (const m of picked) { for (const m of picked) {