// Memory tab: local-memory spaces, paged memory list, read-only documents, forget. import * as React from 'react' import { API, request, postJson, bodyError, messageOf } from '../api' import { formatIso, formatTime } from '../format' import { styles } from '../styles' import { Banner, Btn, Field, Pill, Section } from '../components' import { useApi } from './useApi' const h = React.createElement const PAGE_SIZE = 50 function workspaceSuffix(workspace: string | undefined): string { return workspace === undefined ? '' : '&workspace=' + encodeURIComponent(workspace) } export function MemoryTab(props: { ctx?: any; active?: boolean }): any { const active = props.active !== false 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') 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)) if (result.status !== 200 || result.body === null || result.body.ok !== true) throw new Error(bodyError(result, '读取记忆列表失败')) return result.body }, { visible: active, deps: [offset] }) const documents = useApi(async () => { const result = await request('/dsh-brain/local-memory/documents') if (result.status !== 200 || result.body === null || result.body.ok !== true) throw new Error(bodyError(result, '读取文档列表失败')) return result.body }, { visible: active }) 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 }) 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 spaces = status.data?.spaces ?? {} const globalSpace = spaces.global const projectSpace = spaces.project return h('div', { style: { display: 'flex', flexDirection: 'column', gap: '12px' } }, status.error === null ? null : h(Banner, { tone: 'bad' }, status.error), h(Section, { title: '本地记忆空间', hint: '全局空间与当前项目空间都由本机文件保存;云同步只影响上报,不改变本地存储。', aside: h(Btn, { label: '刷新', variant: 'small', onClick: () => { status.reload(); memories.reload(); documents.reload() } }) }, h('div', { style: styles.pills }, h(Pill, { label: 'provider', value: status.data?.provider ?? '—', tone: 'muted' }), h(Pill, { label: '全局空间', value: globalSpace === null || globalSpace === undefined ? '未创建' : (globalSpace.name ?? globalSpace.id ?? '就绪'), tone: globalSpace === null || globalSpace === undefined ? 'muted' : 'ok' }), h(Pill, { label: '项目空间', value: projectSpace === null || projectSpace === undefined ? '未创建' : (projectSpace.name ?? projectSpace.id ?? '就绪'), tone: projectSpace === null || projectSpace === undefined ? 'muted' : 'ok' }), h(Pill, { label: '本地记忆', value: status.data?.localMemoryEnabled === false ? '关' : '开', tone: status.data?.localMemoryEnabled === false ? 'bad' : 'ok' }), h(Pill, { label: '云同步', value: status.data?.cloudSyncEnabled === true ? '开' : '关', tone: status.data?.cloudSyncEnabled === true ? 'ok' : 'muted' }), ), status.data?.globalRoot === undefined ? null : h('p', { style: styles.hint }, '全局:' + String(status.data.globalRoot) + (status.data?.projectRoot === undefined ? '' : ' · 项目:' + String(status.data.projectRoot))), ), 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 }, '当前页没有匹配的记忆条目。') : 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) }), ), ), 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 ?? '')), )), ), ), ) }