/** * PersonasSection — user persona list + create/edit */ const { html } = window; const { useState, useEffect, useCallback } = hooks; function esc(s) { return s == null ? '' : String(s); } export function PersonasSection() { const [personas, setPersonas] = useState(null); const [showForm, setShowForm] = useState(false); const [editId, setEditId] = useState(null); const [form, setForm] = useState({ name: '', description: '', system_prompt: '' }); const [saving, setSaving] = useState(false); const load = useCallback(async () => { try { const resp = await sw.api.personas.list(); setPersonas(resp?.data || resp || []); } catch (e) { setPersonas([]); } }, []); useEffect(() => { load(); }, [load]); const openAdd = useCallback(() => { setEditId(null); setForm({ name: '', description: '', system_prompt: '' }); setShowForm(true); }, []); const openEdit = useCallback((p) => { setEditId(p.id); setForm({ name: p.name || '', description: p.description || '', system_prompt: p.system_prompt || '' }); setShowForm(true); }, []); const cancel = useCallback(() => { setShowForm(false); setEditId(null); }, []); const save = useCallback(async () => { if (!form.name.trim()) { sw.emit('toast', { message: 'Name is required', variant: 'error' }); return; } setSaving(true); try { if (editId) { await sw.api.personas.update(editId, form); sw.emit('toast', { message: 'Persona updated', variant: 'success' }); } else { await sw.api.personas.create(form); sw.emit('toast', { message: 'Persona created', variant: 'success' }); } setShowForm(false); setEditId(null); load(); } catch (e) { sw.emit('toast', { message: e.message, variant: 'error' }); } finally { setSaving(false); } }, [form, editId, load]); const del = useCallback(async (p) => { const ok = await sw.confirm(`Delete persona "${p.name}"?`); if (!ok) return; try { await sw.api.personas.del(p.id); sw.emit('toast', { message: 'Persona deleted', variant: 'success' }); load(); } catch (e) { sw.emit('toast', { message: e.message, variant: 'error' }); } }, [load]); const setField = useCallback((key, val) => { setForm(f => ({ ...f, [key]: val })); }, []); if (personas === null) { return html`