// Memory tab: local-memory spaces, paged memory list, read-only documents, forget. import * as React from 'react' import { request, postJson, bodyError, messageOf } from '../api' import { formatIso } from '../format' import { styles } from '../styles' import { Banner, Btn, Field, Pill, Section } from '../components' import { useApi } from './useApi' import { scopeParams, type BrainScope } from './workspace' const h = React.createElement const PAGE_SIZE = 50 export function MemoryTab(props: BrainScope & { active?: boolean }): any { const active = props.active !== false const project = props.scope === 'project' const scope = scopeParams(props) const scopeLabel = project ? '项目' : '全局' const [offset, setOffset] = React.useState(0) const [search, setSearch] = React.useState('') const [notice, setNotice] = React.useState(null) const [pendingForget, setPendingForget] = React.useState(null) const [busy, setBusy] = React.useState('') const status = useApi(async () => { const result = await request('/dsh-brain/local-memory/status' + '?' + scope.toString()) if (result.status !== 200 || result.body === null || result.body.ok !== true) throw new Error(bodyError(result, '读取本地记忆状态失败')) return result.body }, { visible: active, pollMs: 15000 }) const memories = useApi(async () => { const result = await request('/dsh-brain/local-memory/memories?limit=' + String(PAGE_SIZE) + '&offset=' + String(offset) + '&' + scope.toString()) if (result.status !== 200 || result.body === null || result.body.ok !== true) throw new Error(bodyError(result, '读取记忆列表失败')) return result.body }, { visible: active, pollMs: project ? 30000 : undefined, deps: [offset] }) const documents = useApi(async () => { const result = await request('/dsh-brain/local-memory/documents' + '?' + scope.toString()) if (result.status !== 200 || result.body === null || result.body.ok !== true) throw new Error(bodyError(result, '读取文档列表失败')) return result.body }, { visible: active && project }) const items: any[] = Array.isArray(memories.data?.items) ? memories.data!.items : [] const needle = String(search).trim().toLowerCase() const filtered = needle === '' ? items : items.filter((item) => { const hay = [item.content, item.category, item.scope, ...(Array.isArray(item.tags) ? item.tags : [])].join(' ').toLowerCase() return hay.indexOf(needle) >= 0 }) const total = memories.data?.total ?? 0 const forget = async (item: any): Promise => { setBusy(item.id) try { const result = await postJson('/dsh-brain/local-memory/forget', { id: item.id, expectedRevision: item.revision, ...Object.fromEntries(scope) }) if (result.status !== 200 || result.body === null || result.body.ok !== true) { setNotice({ tone: 'bad', text: bodyError(result, '删除失败') }) } else { setNotice({ tone: 'ok', text: '已删除记忆条目。' }) setPendingForget(null) memories.reload() } } catch (failure) { setNotice({ tone: 'bad', text: messageOf(failure) }) } finally { setBusy('') } } const space = status.data?.spaces?.[props.scope] const root = project ? status.data?.projectRoot : status.data?.globalRoot return h('div', { style: { display: 'flex', flexDirection: 'column', gap: '12px' } }, status.error === null ? null : h(Banner, { tone: 'bad' }, status.error), h(Section, { title: scopeLabel + '记忆空间', aside: h(Btn, { label: '刷新', variant: 'small', onClick: () => { status.reload(); memories.reload(); if (project) documents.reload() } }) }, h(Pill, { label: '空间状态', value: status.status === 'loading' ? '读取中' : space?.name ?? (space ? '就绪' : '未创建'), tone: space ? 'ok' : 'muted' }), root === undefined ? null : h('p', { style: styles.hint }, String(root)), ), h(Section, { title: '记忆条目(' + String(total) + ')', hint: '只显示当前页;搜索只在当前页内过滤。删除需要二次确认并带 revision 校验。', aside: h('div', { style: styles.actions }, h(Field, { label: '搜索当前页', value: search, onChange: setSearch }), h(Btn, { label: '刷新', variant: 'small', onClick: () => memories.reload() }), ) }, notice === null ? null : h(Banner, { tone: notice.tone }, notice.text), memories.error === null ? null : h(Banner, { tone: 'bad' }, memories.error), memories.status === 'loading' && items.length === 0 ? h('p', { style: styles.hint }, '正在读取记忆…') : null, filtered.length === 0 ? h('p', { style: styles.hint }, total === 0 && needle === '' ? '还没有记忆条目。对话中让助手记住,或用 memory_add 写入。' : '当前页没有匹配的记忆条目。') : h('div', { style: { display: 'flex', flexDirection: 'column', gap: '8px' } }, filtered.map((item) => h('div', { key: item.id, style: styles.row }, h('div', { style: styles.rowHead }, h('div', { style: styles.pills }, h(Pill, { label: 'scope', value: item.scope ?? '—', tone: 'muted' }), item.category === undefined ? null : h(Pill, { label: '分类', value: item.category, tone: 'muted' }), h(Pill, { label: 'rev', value: item.revision ?? '—', tone: 'muted' }), h(Pill, { label: '更新', value: formatIso(item.updatedAt), tone: 'muted' }), ), h('div', { style: styles.actions }, pendingForget === item.id ? null : h(Btn, { label: '删除…', variant: 'small', onClick: () => { setPendingForget(item.id); setNotice(null) } }), ), ), h('div', { style: styles.clamp2 }, String(item.content ?? '')), Array.isArray(item.tags) && item.tags.length > 0 ? h('p', { style: styles.hint }, '标签:' + item.tags.join('、')) : null, pendingForget === item.id ? h('div', { style: styles.nested }, h('p', { style: styles.hint }, '确认删除这条记忆?此操作不可撤销。'), h('div', { style: styles.actions }, h(Btn, { variant: 'danger', label: busy === item.id ? '删除中…' : '确认删除', disabled: busy !== '', onClick: () => { void forget(item) } }), h(Btn, { variant: 'small', label: '取消', disabled: busy !== '', onClick: () => setPendingForget(null) }), ), ) : null, )), ), h('div', { style: styles.actions }, h(Btn, { label: '上一页', variant: 'small', disabled: offset === 0 || memories.status === 'loading', onClick: () => setOffset(Math.max(0, offset - PAGE_SIZE)) }), h('span', { style: styles.hint }, '第 ' + String(Math.floor(offset / PAGE_SIZE) + 1) + ' 页 · 共 ' + String(total) + ' 条'), h(Btn, { label: '下一页', variant: 'small', disabled: offset + PAGE_SIZE >= total || memories.status === 'loading', onClick: () => setOffset(offset + PAGE_SIZE) }), ), ), project ? h(Section, { title: '文档(只读)', hint: '项目文档由本地记忆库维护;这里只展示标题与摘要。' }, documents.error === null ? null : h(Banner, { tone: 'bad' }, documents.error), (documents.data?.documents ?? []).length === 0 ? h('p', { style: styles.hint }, '当前项目还没有文档。') : h('div', { style: { display: 'flex', flexDirection: 'column', gap: '8px' } }, (documents.data?.documents ?? []).map((doc: any) => h('div', { key: doc.id, style: styles.row }, h('div', { style: styles.rowHead }, h('span', { style: styles.rowTitle }, String(doc.title ?? doc.id)), h('div', { style: styles.pills }, h(Pill, { label: 'rev', value: doc.revision ?? '—', tone: 'muted' }), h(Pill, { label: '更新', value: formatIso(doc.updatedAt), tone: 'muted' }), ), ), h('div', { style: styles.clamp2 }, String(doc.content ?? '')), )), ), ) : null, ) }