async function renderMemory() {
const content = document.getElementById('pageContent');
content.innerHTML = `
`;
try {
const brain = await api.getBrain();
const files = Object.entries(brain);
const container = document.getElementById('memoryList');
if (files.length === 0) {
container.innerHTML = '';
return;
}
container.innerHTML = `${files.map(([name, content]) => {
const preview = content ? content.slice(0, 200) : '';
const safeName = escapeHtml(name.replace('.md', '').replace(/-/g, ' '));
return `
${safeName}
${content ? content.split('\n').length : 0} lines
${escapeHtml(preview)}${preview.length >= 200 ? '...' : ''}
`;
}).join('')}
`;
} catch (err) {
document.getElementById('memoryList').innerHTML = `âš
${escapeHtml(err.message)}
`;
}
}
async function editMemory(encodedName) {
const name = decodeURIComponent(encodedName);
const display = escapeHtml(name.replace('.md', '').replace(/-/g, ' '));
let content = '';
try {
const r = await api.getBrainFile(name);
content = r.content || '';
} catch {}
showModal(`Edit: ${display}`, `
`, `
`);
}
async function saveMemory(encodedName) {
const name = decodeURIComponent(encodedName);
const content = document.getElementById('memContent').value;
try {
await api.updateBrainFile(name, content);
closeModal();
showToast('Memory updated', 'success');
renderMemory();
} catch (err) {
showToast(`Error: ${err.message}`, 'error');
}
}