dsh-brain/src-client/panel/Overview.tsx

115 lines
6.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Overview tab: status + capabilities aggregation.
import * as React from 'react'
import { API, request, messageOf } from '../api'
import { formatTime } from '../format'
import styles from './BrainPanel.module.css'
const h = React.createElement
const BUNDLE_LABELS: Record<string, string> = {
cloudsync: '云同步',
memory: '记忆',
skills: '技能',
mcp: 'MCP',
'settings-ui': '设置 UI',
ui: '设置 UI',
}
function Pill(props: { label: string; value: any; tone?: 'ok' | 'bad' | 'warn' | 'muted' }): any {
const tone = props.tone === undefined ? styles.muted : props.tone === 'ok' ? styles.ok : props.tone === 'bad' ? styles.bad : props.tone === 'warn' ? styles.warn : styles.muted
return h('span', { className: styles.pill + ' ' + tone },
h('span', { className: styles.pillLabel }, props.label),
h('span', { className: styles.pillValue }, String(props.value)),
)
}
function Card(props: { title: string; children?: any }): any {
return h('div', { className: styles.card },
h('div', { className: styles.cardHead }, h('h4', { className: styles.cardTitle }, props.title)),
props.children,
)
}
export function Overview(props: { active?: boolean }): any {
const active = props.active !== false
const [status, setStatus] = React.useState<any>(null)
const [capabilities, setCapabilities] = React.useState<any>(null)
const [memory, setMemory] = React.useState<any>(null)
const [error, setError] = React.useState<string | null>(null)
React.useEffect(() => {
if (!active) return
let cancelled = false
const load = async (): Promise<void> => {
try {
const [statusResult, capabilitiesResult, memoryResult] = await Promise.all([
request(API.status),
request(API.capabilities),
request('/dsh-brain/local-memory/status'),
])
if (cancelled) return
if (statusResult.status === 200 && statusResult.body !== null && statusResult.body.ok === true) {
setStatus(statusResult.body)
setError(null)
} else {
setError('读取插件状态失败(HTTP ' + String(statusResult.status) + ')')
}
if (capabilitiesResult.status === 200 && capabilitiesResult.body !== null && capabilitiesResult.body.ok === true) setCapabilities(capabilitiesResult.body)
if (memoryResult.status === 200 && memoryResult.body !== null && memoryResult.body.ok === true) setMemory(memoryResult.body)
else setMemory(null)
} catch (failure) {
if (!cancelled) setError(messageOf(failure))
}
}
void load()
const timer = setInterval(() => { void load() }, 15000)
return () => {
cancelled = true
clearInterval(timer)
}
}, [active])
const bundles: Record<string, boolean> = status?.bundles ?? {}
const bundleKeys = Object.keys(bundles).length === 0 ? ['cloudsync', 'memory', 'skills', 'mcp', 'settings-ui'] : Object.keys(bundles)
const settings = status?.settings ?? {}
const reporter = status?.reporter ?? {}
const skills = status?.skills ?? {}
const memorySpaces = memory?.spaces ?? {}
const version = capabilities?.version ?? status?.capabilities?.version ?? '—'
return h('div', { className: styles.overviewGrid },
error === null ? null : h('div', { className: styles.error }, error),
h(Card, { title: '版本与构建' },
h('div', { className: styles.pills },
h(Pill, { label: '版本', value: version }),
h(Pill, { label: '服务', value: settings.baseUrlConfigured === false ? '未配置' : (settings.baseUrlOrigin || '已配置'), tone: settings.baseUrlConfigured === false ? 'warn' : 'muted' }),
h(Pill, { label: '上报', value: settings.activityEnabled === true ? '开' : '关', tone: settings.activityEnabled === true ? 'ok' : 'muted' }),
h(Pill, { label: '记忆', value: settings.memoryEnabled === true ? '开' : '关', tone: settings.memoryEnabled === true ? 'ok' : 'muted' }),
),
status?.lastCheck === null || status?.lastCheck === undefined ? null : h('p', { className: styles.hint }, '最近检查:' + formatTime(status.lastCheck.at) + ' · ' + String(status.lastCheck.status ?? '')),
),
h(Card, { title: '五个 bundle 开关态' },
h('div', { className: styles.pills },
bundleKeys.map((key) => h(Pill, { key, label: BUNDLE_LABELS[key] ?? key, value: bundles[key] === true ? '启用' : bundles[key] === false ? '停用' : '未知', tone: bundles[key] === true ? 'ok' : bundles[key] === false ? 'bad' : 'muted' })),
),
),
h(Card, { title: '本地记忆' },
h('div', { className: styles.pills },
h(Pill, { label: '状态', value: memory === null ? '不可用' : '已启用', tone: memory === null ? 'warn' : 'ok' }),
h(Pill, { label: '全局空间', value: memorySpaces.global === undefined ? '—' : (memorySpaces.global.exists === false ? '未创建' : '就绪'), tone: memorySpaces.global?.exists === false ? 'muted' : 'ok' }),
h(Pill, { label: '项目空间', value: memorySpaces.project === undefined ? '—' : (memorySpaces.project.exists === false ? '未创建' : '就绪'), tone: memorySpaces.project?.exists === false ? 'muted' : 'ok' }),
h(Pill, { label: '云同步', value: memory?.cloudSyncEnabled === true ? '开' : '关', tone: memory?.cloudSyncEnabled === true ? 'ok' : 'muted' }),
),
memory === null ? h('p', { className: styles.hint }, '本地记忆状态路由不可用;启用「本地记忆」后此处会显示空间状态。') : null,
),
h(Card, { title: '同步统计' },
h('div', { className: styles.statRow },
h('span', { className: styles.stat }, h('span', { className: styles.statNum }, String(reporter.reportedTurns ?? 0)), h('span', { className: styles.hint }, '已上报回合')),
h('span', { className: styles.stat }, h('span', { className: styles.statNum }, String(skills.downloaded?.length ?? 0)), h('span', { className: styles.hint }, '技能下载')),
h('span', { className: styles.stat }, h('span', { className: styles.statNum }, String(skills.uploaded?.length ?? 0)), h('span', { className: styles.hint }, '技能上传')),
h('span', { className: styles.statLast }, h('span', { className: styles.statNum }, String(skills.conflicts?.length ?? 0)), h('span', { className: styles.hint }, '待处理冲突')),
),
),
)
}