124 lines
6.9 KiB
TypeScript
124 lines
6.9 KiB
TypeScript
// Read-only project-memory view for the better-sidebar tab.
|
|
import * as React from 'react'
|
|
import { request, bodyError, messageOf } from '../api'
|
|
import { formatIso } from '../format'
|
|
import { styles } from '../styles'
|
|
import { Banner, Btn, Field, Pill } from '../components'
|
|
import { useApi } from '../panel/useApi'
|
|
import { workspaceQuery } from '../panel/workspace'
|
|
import styles2 from './ProjectMemory.module.css'
|
|
|
|
const h = React.createElement
|
|
|
|
function openTotalPanel(ctx: any): void {
|
|
const layout = ctx === undefined || ctx === null ? undefined : ctx.layout
|
|
if (layout !== undefined && layout !== null && typeof layout.selectPanel === 'function') {
|
|
layout.selectPanel('dsh-brain')
|
|
return
|
|
}
|
|
if (typeof window !== 'undefined' && typeof window.open === 'function') window.open('/dsh-brain')
|
|
}
|
|
|
|
export function ProjectMemoryView(props: { ctx?: any; scope?: { cwd?: string; sessionId?: string }; visible?: boolean }): any {
|
|
const visible = props.visible !== false
|
|
const workspace = props.scope?.cwd
|
|
const [search, setSearch] = React.useState('')
|
|
const [recallId, setRecallId] = React.useState('')
|
|
const [recallQuery, setRecallQuery] = React.useState('')
|
|
const [recallHits, setRecallHits] = React.useState<any[]>([])
|
|
const [recallBusy, setRecallBusy] = React.useState(false)
|
|
const [notice, setNotice] = React.useState<any>(null)
|
|
|
|
const status = useApi(async () => {
|
|
const result = await request('/dsh-brain/local-memory/status' + workspaceQuery(workspace))
|
|
if (result.status !== 200 || result.body === null || result.body.ok !== true) throw new Error(bodyError(result, '读取本地记忆状态失败'))
|
|
return result.body
|
|
}, { visible, pollMs: 15000 })
|
|
|
|
const memories = useApi(async () => {
|
|
const suffix = workspace === undefined ? '' : '&workspace=' + encodeURIComponent(workspace)
|
|
const result = await request('/dsh-brain/local-memory/memories?limit=50&offset=0' + suffix)
|
|
if (result.status !== 200 || result.body === null || result.body.ok !== true) throw new Error(bodyError(result, '读取记忆列表失败'))
|
|
return result.body
|
|
}, { visible, pollMs: 30000 })
|
|
|
|
const departments = useApi(async () => {
|
|
const suffix = workspace === undefined ? '' : '?workspace=' + encodeURIComponent(workspace)
|
|
const result = await request('/dsh-brain/departments/list' + suffix)
|
|
if (result.status !== 200 || result.body === null || result.body.ok !== true) throw new Error(bodyError(result, '读取部门列表失败'))
|
|
return result.body
|
|
}, { visible })
|
|
|
|
const items: any[] = Array.isArray(memories.data?.items) ? memories.data!.items : []
|
|
const needle = String(search).trim().toLowerCase()
|
|
const filtered = needle === '' ? items : items.filter((item) => String(item.content ?? '').toLowerCase().indexOf(needle) >= 0)
|
|
const departmentList: any[] = Array.isArray(departments.data?.departments) ? departments.data!.departments : []
|
|
const spaces = status.data?.spaces ?? {}
|
|
|
|
const recall = async (): Promise<void> => {
|
|
if (recallId === '') { setNotice({ tone: 'warn', text: '请选择部门。' }); return }
|
|
setRecallBusy(true)
|
|
setRecallHits([])
|
|
try {
|
|
const query = '/dsh-brain/departments/recall?id=' + encodeURIComponent(recallId) + '&query=' + encodeURIComponent(recallQuery) + '&k=5' + (workspace === undefined ? '' : '&workspace=' + encodeURIComponent(workspace))
|
|
const result = await request(query)
|
|
if (result.status !== 200 || result.body === null || result.body.ok !== true) {
|
|
setNotice({ tone: 'bad', text: bodyError(result, '召回失败') })
|
|
return
|
|
}
|
|
setRecallHits(Array.isArray(result.body.hits) ? result.body.hits : [])
|
|
} catch (failure) {
|
|
setNotice({ tone: 'bad', text: messageOf(failure) })
|
|
} finally {
|
|
setRecallBusy(false)
|
|
}
|
|
}
|
|
|
|
return h('div', { className: styles2.betterSidebarSeat, 'data-dsh-plugin': 'dsh-brain', 'data-dsh-part': 'better-sidebar-memory' },
|
|
h('div', { className: styles2.inner },
|
|
h('div', { className: styles2.header },
|
|
h('div', null,
|
|
h('h4', { className: styles2.title }, '项目记忆'),
|
|
h('p', { className: styles2.hint }, '当前会话项目的本地记忆(只读)'),
|
|
),
|
|
h(Btn, { label: '去总面板', variant: 'small', onClick: () => openTotalPanel(props.ctx) }),
|
|
),
|
|
status.error === null ? null : h(Banner, { tone: 'bad' }, status.error),
|
|
h('div', { style: styles.pills },
|
|
h(Pill, { label: '全局空间', value: spaces.global === null || spaces.global === undefined ? '未创建' : '就绪', tone: spaces.global === null || spaces.global === undefined ? 'muted' : 'ok' }),
|
|
h(Pill, { label: '项目空间', value: spaces.project === null || spaces.project === undefined ? '未创建' : '就绪', tone: spaces.project === null || spaces.project === undefined ? 'muted' : 'ok' }),
|
|
h(Pill, { label: '条目', value: memories.data?.total ?? 0, tone: 'muted' }),
|
|
),
|
|
h(Field, { label: '搜索记忆', value: search, onChange: setSearch }),
|
|
notice === null ? null : h(Banner, { tone: notice.tone }, notice.text),
|
|
memories.error === null ? null : h(Banner, { tone: 'bad' }, memories.error),
|
|
filtered.length === 0 ? h('p', { className: styles2.hint }, '没有匹配的记忆条目。') : h('div', { className: styles2.list },
|
|
filtered.map((item) => h('div', { key: item.id, className: styles2.row },
|
|
h('div', { className: styles2.rowMeta },
|
|
h(Pill, { label: 'scope', value: item.scope ?? '—', tone: 'muted' }),
|
|
item.category === undefined ? null : h(Pill, { label: '分类', value: item.category, tone: 'muted' }),
|
|
h(Pill, { label: '更新', value: formatIso(item.updatedAt), tone: 'muted' }),
|
|
),
|
|
h('div', { className: styles2.rowText }, String(item.content ?? '')),
|
|
)),
|
|
),
|
|
h('div', { className: styles2.divider }),
|
|
h('h5', { className: styles2.subtitle }, '部门召回'),
|
|
h('div', { className: styles2.recallRow },
|
|
h('select', { className: styles.input, value: recallId, onChange: (event) => setRecallId(event.target.value) },
|
|
h('option', { value: '' }, '选择部门…'),
|
|
departmentList.map((item) => h('option', { key: item.id, value: item.id }, String(item.name))),
|
|
),
|
|
h(Field, { label: '查询', value: recallQuery, onChange: setRecallQuery }),
|
|
h(Btn, { label: recallBusy ? '召回中…' : '召回', disabled: recallBusy, onClick: () => { void recall() } }),
|
|
),
|
|
recallHits.length === 0 ? null : h('div', { className: styles2.list },
|
|
recallHits.map((hit: any, index: number) => h('div', { key: String(index), className: styles2.row },
|
|
h('div', { className: styles2.rowMeta }, h(Pill, { label: '来源', value: hit.origin ?? '—', tone: 'muted' })),
|
|
h('div', { className: styles2.rowText }, String(hit.contentSnippet ?? hit.content ?? '')),
|
|
)),
|
|
),
|
|
),
|
|
)
|
|
}
|