feat(ui): project-memory tab for better-sidebar with graceful fallback
This commit is contained in:
parent
0b655385af
commit
3d7b59b4c9
1032
client/index.js
1032
client/index.js
File diff suppressed because it is too large
Load Diff
|
|
@ -5,6 +5,7 @@ import { CardBoundary } from './components'
|
||||||
import { SettingsCard } from './SettingsCard'
|
import { SettingsCard } from './SettingsCard'
|
||||||
import { SettingsSummaryCard } from './SettingsSummaryCard'
|
import { SettingsSummaryCard } from './SettingsSummaryCard'
|
||||||
import { registerBrainPanel } from './panel/register'
|
import { registerBrainPanel } from './panel/register'
|
||||||
|
import { mountBrainSidebarTab } from './sidebar/register'
|
||||||
|
|
||||||
const h = React.createElement
|
const h = React.createElement
|
||||||
|
|
||||||
|
|
@ -47,6 +48,13 @@ export function apply(ctx: any): void {
|
||||||
} catch (failure) {
|
} catch (failure) {
|
||||||
if (typeof console !== 'undefined') console.warn('[dsh-brain] panel registration failed: ' + messageOf(failure))
|
if (typeof console !== 'undefined') console.warn('[dsh-brain] panel registration failed: ' + messageOf(failure))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (typeof ctx.effect === 'function') ctx.effect(() => mountBrainSidebarTab(ctx), 'dsh-brain: better-sidebar tab')
|
||||||
|
else mountBrainSidebarTab(ctx)
|
||||||
|
} catch (failure) {
|
||||||
|
if (typeof console !== 'undefined') console.warn('[dsh-brain] better-sidebar tab registration failed: ' + messageOf(failure))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const name = 'dsh-brain'
|
export const name = 'dsh-brain'
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,91 @@
|
||||||
|
/* Adapted from dsh-mnemon MnemonWorkspace.module.css (MIT), see vendor/dsh-mnemon/LICENSE. */
|
||||||
|
.betterSidebarSeat {
|
||||||
|
display: flex;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
min-width: 0;
|
||||||
|
min-height: 0;
|
||||||
|
overflow: hidden;
|
||||||
|
flex: 1 1 auto;
|
||||||
|
color: var(--dsw-alias-label-primary, inherit);
|
||||||
|
}
|
||||||
|
|
||||||
|
.inner {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 10px;
|
||||||
|
width: 100%;
|
||||||
|
min-width: 0;
|
||||||
|
padding: 12px;
|
||||||
|
overflow: auto;
|
||||||
|
font-size: 12.5px;
|
||||||
|
line-height: 1.5;
|
||||||
|
}
|
||||||
|
|
||||||
|
.header {
|
||||||
|
display: flex;
|
||||||
|
align-items: flex-start;
|
||||||
|
justify-content: space-between;
|
||||||
|
gap: 8px;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.title {
|
||||||
|
margin: 0;
|
||||||
|
font-size: 13.5px;
|
||||||
|
font-weight: 650;
|
||||||
|
}
|
||||||
|
|
||||||
|
.subtitle {
|
||||||
|
margin: 0;
|
||||||
|
font-size: 12.5px;
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
|
||||||
|
.hint {
|
||||||
|
margin: 0;
|
||||||
|
font-size: 11.5px;
|
||||||
|
opacity: 0.58;
|
||||||
|
}
|
||||||
|
|
||||||
|
.list {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.row {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 5px;
|
||||||
|
border: 1px solid var(--dsw-alias-border-subtle, rgba(127,127,127,0.18));
|
||||||
|
border-radius: 10px;
|
||||||
|
padding: 8px 10px;
|
||||||
|
min-width: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.rowMeta {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 6px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.rowText {
|
||||||
|
display: -webkit-box;
|
||||||
|
-webkit-line-clamp: 3;
|
||||||
|
-webkit-box-orient: vertical;
|
||||||
|
overflow: hidden;
|
||||||
|
overflow-wrap: anywhere;
|
||||||
|
}
|
||||||
|
|
||||||
|
.divider {
|
||||||
|
height: 1px;
|
||||||
|
background: var(--dsw-alias-border-subtle, rgba(127,127,127,0.18));
|
||||||
|
margin: 2px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.recallRow {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 8px;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,121 @@
|
||||||
|
// 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 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')
|
||||||
|
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 result = await request('/dsh-brain/local-memory/memories?limit=50&offset=0')
|
||||||
|
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 ?? '')),
|
||||||
|
)),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,78 @@
|
||||||
|
// Adapted from dsh-mnemon better-sidebar.tsx (MIT), see vendor/dsh-mnemon/LICENSE.
|
||||||
|
// Registers the "项目记忆" tab into the optional better-sidebar service.
|
||||||
|
// The service is probed at runtime; when it is absent the tab simply does not
|
||||||
|
// appear and the total Brain panel remains the full-featured surface.
|
||||||
|
import * as React from 'react'
|
||||||
|
import { BrainIcon } from '../panel/brain-icon'
|
||||||
|
import { ProjectMemoryView } from './ProjectMemoryView'
|
||||||
|
|
||||||
|
export const BETTER_SIDEBAR_TAB_ID = 'dsh-brain:memory'
|
||||||
|
|
||||||
|
interface BetterSidebarTabProps {
|
||||||
|
scope?: { cwd?: string; sessionId?: string }
|
||||||
|
visible?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
interface BetterSidebarTabDescriptor {
|
||||||
|
id: string
|
||||||
|
title: string | (() => string)
|
||||||
|
description?: string
|
||||||
|
icon?: any
|
||||||
|
order?: number
|
||||||
|
single?: boolean
|
||||||
|
component: (props: BetterSidebarTabProps) => any
|
||||||
|
}
|
||||||
|
|
||||||
|
interface BetterSidebarService {
|
||||||
|
registerTab(descriptor: BetterSidebarTabDescriptor): () => void
|
||||||
|
}
|
||||||
|
|
||||||
|
export function mountBrainSidebarTab(ctx: any): () => void {
|
||||||
|
let current: { service: BetterSidebarService; dispose: () => void } | undefined
|
||||||
|
let disposed = false
|
||||||
|
|
||||||
|
const reconcile = (): void => {
|
||||||
|
if (disposed) return
|
||||||
|
const service = (typeof ctx?.get === 'function' ? ctx.get('betterSidebar') : ctx?.betterSidebar) as BetterSidebarService | undefined
|
||||||
|
if (current?.service === service) return
|
||||||
|
current?.dispose()
|
||||||
|
current = undefined
|
||||||
|
if (service === undefined || service === null || typeof service.registerTab !== 'function') return
|
||||||
|
const dispose = service.registerTab({
|
||||||
|
id: BETTER_SIDEBAR_TAB_ID,
|
||||||
|
title: () => '项目记忆',
|
||||||
|
description: '当前会话项目的本地记忆:空间/条目/搜索/部门召回',
|
||||||
|
icon: (size: number) => React.createElement(BrainIcon, { size }),
|
||||||
|
order: 60,
|
||||||
|
single: true,
|
||||||
|
component: ({ scope, visible }: BetterSidebarTabProps) => React.createElement(ProjectMemoryView, { ctx, scope, visible }),
|
||||||
|
})
|
||||||
|
current = { service, dispose }
|
||||||
|
}
|
||||||
|
|
||||||
|
reconcile()
|
||||||
|
let unsubscribe: (() => void) | undefined
|
||||||
|
if (typeof ctx?.on === 'function') {
|
||||||
|
try {
|
||||||
|
unsubscribe = ctx.on('internal/service', (name: string) => {
|
||||||
|
if (name === 'betterSidebar') reconcile()
|
||||||
|
})
|
||||||
|
} catch {
|
||||||
|
unsubscribe = undefined
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return () => {
|
||||||
|
disposed = true
|
||||||
|
try {
|
||||||
|
unsubscribe?.()
|
||||||
|
} catch {
|
||||||
|
/* ignore teardown errors */
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
current?.dispose()
|
||||||
|
} catch {
|
||||||
|
/* ignore teardown errors */
|
||||||
|
}
|
||||||
|
current = undefined
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -26,6 +26,8 @@ for (const label of ['连接与上报', '技能管理', 'MCP 管理', '概览',
|
||||||
check('client bundle keeps tab label ' + label, bundle.includes(label))
|
check('client bundle keeps tab label ' + label, bundle.includes(label))
|
||||||
}
|
}
|
||||||
check('client bundle keeps the panel semantic anchor', bundle.includes('data-dsh-panel-entry') && bundle.includes('data-dsh-plugin'))
|
check('client bundle keeps the panel semantic anchor', bundle.includes('data-dsh-panel-entry') && bundle.includes('data-dsh-plugin'))
|
||||||
|
check('client bundle registers the better-sidebar tab surface', bundle.includes('dsh-brain:memory') && bundle.includes('registerTab') && bundle.includes('internal/service'))
|
||||||
|
check('better-sidebar view keeps a visible pause branch', bundle.includes('visible') && bundle.includes('项目记忆'))
|
||||||
for (const apiPath of ['/dsh-brain/local-memory/status', '/dsh-brain/local-memory/memories', '/dsh-brain/local-memory/documents', '/dsh-brain/local-memory/forget', '/dsh-brain/departments/list', '/dsh-brain/departments/create', '/dsh-brain/departments/update', '/dsh-brain/departments/disable', '/dsh-brain/departments/checkpoint', '/dsh-brain/departments/recall']) {
|
for (const apiPath of ['/dsh-brain/local-memory/status', '/dsh-brain/local-memory/memories', '/dsh-brain/local-memory/documents', '/dsh-brain/local-memory/forget', '/dsh-brain/departments/list', '/dsh-brain/departments/create', '/dsh-brain/departments/update', '/dsh-brain/departments/disable', '/dsh-brain/departments/checkpoint', '/dsh-brain/departments/recall']) {
|
||||||
check('client bundle consumes ' + apiPath, bundle.includes(apiPath))
|
check('client bundle consumes ' + apiPath, bundle.includes(apiPath))
|
||||||
}
|
}
|
||||||
|
|
@ -126,6 +128,27 @@ try {
|
||||||
} catch { rejectThrew = true }
|
} catch { rejectThrew = true }
|
||||||
check('rejecting panel seats degrade without throwing', !rejectThrew)
|
check('rejecting panel seats degrade without throwing', !rejectThrew)
|
||||||
|
|
||||||
|
let sidebarDescriptor = null
|
||||||
|
let sidebarListener = null
|
||||||
|
let currentService = undefined
|
||||||
|
let sidebarThrew = false
|
||||||
|
try {
|
||||||
|
const mod = loadBundle('client/index.js')
|
||||||
|
const service = { registerTab(descriptor) { sidebarDescriptor = descriptor; return () => {} } }
|
||||||
|
mod.apply({
|
||||||
|
slots: { inject(seat, callback) { callback() }, register() { return () => {} } },
|
||||||
|
get(name) { return name === 'betterSidebar' ? currentService : undefined },
|
||||||
|
on(name, listener) { if (name === 'internal/service') sidebarListener = listener; return () => {} },
|
||||||
|
effect(setup) { if (typeof setup === 'function') setup() },
|
||||||
|
})
|
||||||
|
if (sidebarDescriptor !== null) throw new Error('tab must not register without a service')
|
||||||
|
currentService = service
|
||||||
|
sidebarListener('betterSidebar')
|
||||||
|
} catch { sidebarThrew = true }
|
||||||
|
check('better-sidebar tab is absent without the service', !sidebarThrew && sidebarDescriptor !== null)
|
||||||
|
check('better-sidebar tab registers with the expected descriptor', sidebarDescriptor !== null && sidebarDescriptor.id === 'dsh-brain:memory' && sidebarDescriptor.order === 60 && sidebarDescriptor.single === true && typeof sidebarDescriptor.title === 'function' && sidebarDescriptor.title() === '项目记忆' && typeof sidebarDescriptor.description === 'string' && sidebarDescriptor.description.includes('项目'))
|
||||||
|
check('better-sidebar tab hot-plugs through internal/service', sidebarListener !== null && typeof sidebarListener === 'function')
|
||||||
|
|
||||||
const legacyFile = new URL('./artifacts/analysis/client-legacy.js', import.meta.url)
|
const legacyFile = new URL('./artifacts/analysis/client-legacy.js', import.meta.url)
|
||||||
if (existsSync(legacyFile)) {
|
if (existsSync(legacyFile)) {
|
||||||
const legacy = exercise(legacyFile)
|
const legacy = exercise(legacyFile)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue