diff --git a/client/index.js b/client/index.js index 6d7de1a..8c43c17 100644 --- a/client/index.js +++ b/client/index.js @@ -1,3734 +1,4139 @@ -// dsh-brain client bundle. -// -// Hand-written on purpose: the host loads this file through `require` shims it -// provides itself (react / react/jsx-runtime), so no bundler is involved. The -// module wrapper below is the contract the web loader expects — `id` has to -// match the package name, otherwise the module graph registers a second copy. -// -// Three tabs: 连接与上报 / 技能管理 / MCP 管理. Nothing here invents data: every -// number and state comes from the plugin's own routes or, for MCP, from the -// host `dsh-mcp-connector` plugin's public bridge on the same origin. window.__ModuleLoader__.load({ - id: 'dsh-brain', - factory: (require) => { - var module = { exports: {} } - var exports = module.exports - - 'use strict' - - var React = require('react') - var h = React.createElement - - var API = { - status: '/dsh-brain/status', - capabilities: '/dsh-brain/capabilities', - config: '/dsh-brain/config', - login: '/dsh-brain/login', - test: '/dsh-brain/test', - flush: '/dsh-brain/flush', - skills: '/dsh-brain/skills', - market: '/dsh-brain/market', - sync: '/dsh-brain/sync', - clients: '/dsh-brain/clients', - migrate: '/dsh-brain/migrate', - } - - // ---- MCP bundle (browser bridge) --------------------------------------- - // The MCP bridge is another plugin's public surface, not ours: it is probed - // at runtime and the tab reports a capability gap when it is missing. - // This bundle owns the tab, the request envelope, the probe timeout and - // the host-unavailable state; connections, credentials, tool management - // and any cloud-side sync belong to the host dsh-mcp-connector plugin. - var MCP_BRIDGE = '/mcp-connector/api' - var MCP_PANEL = '/mcp-connector/ui' - var POLL_MS = 10000 - var PROBE_TIMEOUT_MS = 5000 - - var inject = ['slots'] - - function request(url, init) { - var options = init || {} - options.cache = 'no-store' - options.headers = options.body === undefined - ? { accept: 'application/json' } - : { accept: 'application/json', 'content-type': 'application/json' } - return fetch(url, options).then(function (response) { - return response.json().then( - function (body) { - return { status: response.status, body: body } - }, - function () { - return { status: response.status, body: null } - }, - ) - }) - } - - function postJson(url, payload) { - return request(url, { method: 'POST', body: JSON.stringify(payload === undefined ? {} : payload) }) - } - - function messageOf(error) { - if (error === null || error === undefined) return '未知错误' - if (typeof error === 'string') return error - return typeof error.message === 'string' ? error.message : String(error) - } - - // One POST to the host MCP bridge. Used by the self-contained MCP panels - // below; the dispatcher whitelist lives in dsh-mcp-connector. - function bridgeCall(method, params) { - var controller = typeof AbortController === 'function' ? new AbortController() : null - var timer = controller === null ? null : setTimeout(function () { controller.abort() }, 20000) - return fetch(MCP_BRIDGE, { - method: 'POST', - cache: 'no-store', - headers: { accept: 'application/json', 'content-type': 'application/json' }, - body: JSON.stringify({ method: method, params: params === undefined ? {} : params }), - signal: controller === null ? undefined : controller.signal, - }).then(function (response) { - if (timer !== null) clearTimeout(timer) - return response.json().then( - function (body) { return { status: response.status, body: body } }, - function () { return { status: response.status, body: null } }, - ) - }, function (failure) { - if (timer !== null) clearTimeout(timer) - return { status: 0, body: null, error: messageOf(failure) } - }) - } - - function bridgeMessage(result) { - if (result.body !== null && typeof result.body.message === 'string' && result.body.message !== '') return result.body.message - return 'HTTP ' + String(result.status) + (result.error === undefined ? '' : ':' + result.error) - } - - // A 404/405 here means the host half on disk is newer than the one this - // window is talking to: the module is loaded into the running process and - // does not hot-reload, so the answer is a restart, not a retry. - function statusText(status) { - if (status === 404 || status === 405) { - return '插件后端还没加载这个接口(HTTP ' + String(status) + '):请完全退出并重启 DSH 后再试。' - } - return 'HTTP ' + String(status) - } - - function bodyError(result, fallback) { - if (result.body !== null && result.body !== undefined && typeof result.body.error === 'string' && result.body.error !== '') { - return result.body.error - } - return fallback === undefined ? statusText(result.status) : fallback - } - - // ---- settings-ui bundle (configuration surface) ---- - // pickSettings + the SettingsCard draft/save/login/test/flush logic and the - // 连接与上报 tab belong to the settings-ui bundle; the skills/mcp tabs - // belong to their own bundles. The card only talks to the public HTTP API — - // it never touches SettingsStore/CloudClient/Reporter/SkillSync directly. - function pickSettings(settings) { - var source = settings === undefined || settings === null ? {} : settings - return { - enabled: source.enabled === true, - baseUrl: String(source.baseUrl === undefined ? '' : source.baseUrl), - baseUrlConfigured: source.baseUrlConfigured !== false, - baseUrlOrigin: String(source.baseUrlOrigin === undefined ? '' : source.baseUrlOrigin), - clientId: String(source.clientId === undefined ? '' : source.clientId), - requestTimeoutMs: String(source.requestTimeoutMs === undefined ? 8000 : source.requestTimeoutMs), - defaultK: String(source.defaultK === undefined ? 10 : source.defaultK), - summaryEnabled: source.summaryEnabled === true, - activityEnabled: source.activityEnabled === true, - memoryEnabled: source.memoryEnabled === true, - skillsDir: String(source.skillsDir === undefined ? '' : source.skillsDir), - skillsSyncEnabled: source.skillsSyncEnabled === true, - } - } - - function emptyTokens() { - return { activity: '', memory: '', summary: '' } - } - - function formatTime(value) { - if (typeof value !== 'number' || value <= 0) return '—' - var date = new Date(value) - var pad = function (n) { - return n < 10 ? '0' + String(n) : String(n) - } - return pad(date.getMonth() + 1) + '-' + pad(date.getDate()) + ' ' + pad(date.getHours()) + ':' + pad(date.getMinutes()) + ':' + pad(date.getSeconds()) - } - - function formatIso(value) { - if (typeof value !== 'string' || value === '') return '—' - var stamp = Date.parse(value) - if (!Number.isFinite(stamp)) return value - return formatTime(stamp) - } - - function formatBytes(value) { - if (typeof value !== 'number' || value < 0) return '—' - if (value < 1024) return String(value) + ' B' - if (value < 1024 * 1024) return (value / 1024).toFixed(1) + ' KB' - return (value / (1024 * 1024)).toFixed(2) + ' MB' - } - - function shortHash(value) { - if (typeof value !== 'string' || value === '') return '—' - return value.slice(0, 12) - } - - function readStored(key, fallback) { - try { - var raw = window.localStorage.getItem('dsh-brain:' + key) - if (raw === null || raw === '') raw = window.localStorage.getItem('dsh-cloud-memory:' + key) - return raw === null || raw === '' ? fallback : raw - } catch { - return fallback - } - } - - function store(key, value) { - try { - window.localStorage.setItem('dsh-brain:' + key, String(value)) - } catch { - /* a locked-down window keeps the value in memory only */ - } - } - - var OUTCOME_LABELS = { - recorded: '已上报', - duplicate: '重复忽略', - failed: '失败', - queued: '待重试', - skipped: '已跳过', - } - - var OUTCOME_TONES = { - recorded: 'ok', - duplicate: 'muted', - failed: 'bad', - queued: 'warn', - skipped: 'muted', - } - - var ORIGIN_LABELS = { - cloud: '云端下发', - uploaded: '本地已上传', - local: '本机安装', - unknown: '来源未知', - } - - var CHECK_TONES = { - ok: 'ok', - unconfigured: 'muted', - invalid: 'bad', - forbidden: 'bad', - unreachable: 'bad', - error: 'bad', - } - - var CHECK_LABELS = { - ok: '可用', - unconfigured: '未配置', - invalid: '令牌失效', - forbidden: '权限不足', - unreachable: '不可达', - error: '出错', - } - - var MCP_STATE_LABELS = { - connected: '已连接', - connecting: '连接中', - disabled: '已停用', - error: '异常', - expired: '授权过期', - unknown: '未知', - needs_auth: '待授权', - } - - var styles = { - root: { - display: 'flex', - flexDirection: 'column', - gap: '18px', - padding: '4px 2px 20px', - fontSize: '13px', - lineHeight: 1.55, - minWidth: 0, - }, - tabs: { - display: 'flex', - gap: '4px', - padding: '3px', - borderRadius: '13px', - border: '1px solid rgba(127,127,127,0.22)', - background: 'rgba(127,127,127,0.06)', - flexWrap: 'wrap', - }, - tab: { - flex: '1 1 auto', - minWidth: '88px', - font: 'inherit', - fontSize: '12.5px', - padding: '7px 12px', - borderRadius: '10px', - border: '1px solid transparent', - background: 'transparent', - color: 'inherit', - cursor: 'pointer', - whiteSpace: 'nowrap', - }, - tabActive: { - border: '1px solid rgba(127,127,127,0.30)', - background: 'rgba(127,127,127,0.14)', - boxShadow: '0 1px 3px rgba(0,0,0,0.10)', - fontWeight: 600, - }, - section: { - display: 'flex', - flexDirection: 'column', - gap: '12px', - border: '1px solid rgba(127,127,127,0.22)', - borderRadius: '16px', - padding: '18px 20px', - minWidth: 0, - }, - sectionHead: { - display: 'flex', - alignItems: 'center', - justifyContent: 'space-between', - gap: '10px', - flexWrap: 'wrap', - }, - sectionTitle: { - margin: 0, - fontSize: '13.5px', - fontWeight: 650, - letterSpacing: '0.02em', - }, - hint: { - margin: 0, - fontSize: '11.5px', - opacity: 0.58, - }, - grid: { - display: 'flex', - flexWrap: 'wrap', - gap: '12px 18px', - }, - field: { - display: 'flex', - flexDirection: 'column', - gap: '4px', - flex: '1 1 auto', - minWidth: '220px', - }, - label: { - fontSize: '12px', - opacity: 0.75, - }, - input: { - font: 'inherit', - fontSize: '13px', - padding: '8px 11px', - borderRadius: '10px', - border: '1px solid rgba(127,127,127,0.35)', - background: 'rgba(127,127,127,0.05)', - color: 'inherit', - minWidth: 0, - width: '100%', - boxSizing: 'border-box', - }, - checkboxRow: { - display: 'flex', - alignItems: 'flex-start', - gap: '8px', - }, - actions: { - display: 'flex', - gap: '10px', - flexWrap: 'wrap', - alignItems: 'center', - }, - button: { - font: 'inherit', - fontSize: '13px', - padding: '7px 16px', - borderRadius: '10px', - border: '1px solid rgba(127,127,127,0.4)', - background: 'transparent', - color: 'inherit', - cursor: 'pointer', - }, - buttonPrimary: { - font: 'inherit', - fontSize: '13px', - padding: '7px 18px', - borderRadius: '10px', - border: '1px solid transparent', - background: '#3f3f46', - boxShadow: '0 2px 6px -2px rgba(0,0,0,0.35)', - color: '#ffffff', - cursor: 'pointer', - fontWeight: 600, - }, - buttonDanger: { - font: 'inherit', - fontSize: '13px', - padding: '7px 16px', - borderRadius: '10px', - border: '1px solid rgba(220,38,38,0.5)', - background: 'rgba(220,38,38,0.14)', - color: 'inherit', - cursor: 'pointer', - }, - buttonSmall: { - font: 'inherit', - fontSize: '12px', - padding: '3px 8px', - borderRadius: '8px', - border: '1px solid transparent', - background: 'transparent', - opacity: 0.85, - color: 'inherit', - cursor: 'pointer', - }, - pills: { - display: 'flex', - flexWrap: 'wrap', - gap: '8px', - }, - pill: { - display: 'inline-flex', - alignItems: 'center', - gap: '6px', - padding: '3px 10px', - borderRadius: '999px', - border: '1px solid rgba(127,127,127,0.35)', - fontSize: '12px', - maxWidth: '100%', - whiteSpace: 'nowrap', - flex: '0 0 auto', - }, - table: { - width: '100%', - borderCollapse: 'collapse', - fontSize: '12px', - tableLayout: 'fixed', - }, - th: { - textAlign: 'left', - fontWeight: 600, - padding: '4px 8px 4px 0', - borderBottom: '1px solid rgba(127,127,127,0.28)', - whiteSpace: 'nowrap', - }, - td: { - padding: '4px 8px 4px 0', - borderBottom: '1px solid rgba(127,127,127,0.14)', - verticalAlign: 'top', - overflowWrap: 'anywhere', - }, - mono: { - fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Consolas, monospace', - fontSize: '11px', - opacity: 0.72, - wordBreak: 'break-all', - }, - ok: { borderColor: 'rgba(22,163,74,0.55)', background: 'rgba(22,163,74,0.12)' }, - bad: { borderColor: 'rgba(220,38,38,0.55)', background: 'rgba(220,38,38,0.12)' }, - warn: { borderColor: 'rgba(217,119,6,0.55)', background: 'rgba(217,119,6,0.12)' }, - muted: {}, - iconBtn: { - width: '28px', - height: '28px', - display: 'inline-flex', - alignItems: 'center', - justifyContent: 'center', - fontSize: '13px', - lineHeight: 1, - padding: 0, - borderRadius: '8px', - border: '1px solid rgba(127,127,127,0.25)', - background: 'rgba(127,127,127,0.05)', - color: 'inherit', - cursor: 'pointer', - flex: '0 0 auto', - }, - iconBtnDanger: { - border: '1px solid rgba(220,38,38,0.40)', - background: 'rgba(220,38,38,0.08)', - }, - statsBar: { - display: 'flex', - alignItems: 'center', - flexWrap: 'wrap', - }, - stat: { - display: 'inline-flex', - alignItems: 'baseline', - gap: '6px', - paddingRight: '18px', - marginRight: '18px', - borderRight: '1px solid rgba(127,127,127,0.20)', - }, - statLast: { - display: 'inline-flex', - alignItems: 'baseline', - gap: '6px', - }, - statNum: { - fontSize: '17px', - fontWeight: 700, - }, - banner: { - padding: '10px 13px', - borderRadius: '11px', - border: '1px solid transparent', - fontSize: '12.5px', - }, - row: { - display: 'flex', - flexDirection: 'column', - gap: '6px', - border: '1px solid rgba(127,127,127,0.15)', - borderRadius: '11px', - padding: '10px 12px', - minWidth: 0, - }, - rowHead: { - display: 'flex', - alignItems: 'baseline', - justifyContent: 'space-between', - gap: '8px', - flexWrap: 'wrap', - }, - rowTitle: { - fontWeight: 600, - overflowWrap: 'anywhere', - }, - code: { - fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Consolas, monospace', - fontSize: '11.5px', - whiteSpace: 'pre-wrap', - wordBreak: 'break-word', - maxHeight: '260px', - overflow: 'auto', - margin: 0, - padding: '10px', - borderRadius: '8px', - background: 'rgba(127,127,127,0.1)', - }, - chips: { - display: 'flex', - gap: '6px', - flexWrap: 'wrap', - }, - chip: { - font: 'inherit', - fontSize: '12px', - padding: '2px 10px', - borderRadius: '999px', - border: '1px solid rgba(127,127,127,0.35)', - background: 'transparent', - color: 'inherit', - cursor: 'pointer', - }, - chipActive: { - border: '1px solid rgba(99,102,241,0.6)', - background: 'rgba(99,102,241,0.16)', - }, - nested: { - display: 'flex', - flexDirection: 'column', - gap: '6px', - paddingLeft: '10px', - borderLeft: '2px solid rgba(127,127,127,0.18)', - minWidth: 0, - }, - sourceCard: { - display: 'flex', - flexDirection: 'column', - gap: '6px', - border: '1px solid rgba(127,127,127,0.17)', - borderRadius: '14px', - padding: '12px 14px', - minWidth: 0, - }, - groupRow: { - display: 'flex', - flexDirection: 'column', - gap: '6px', - border: '1px solid transparent', - borderRadius: '11px', - padding: '7px 9px', - background: 'transparent', - minWidth: 0, - }, - skillRow: { - display: 'flex', - alignItems: 'center', - justifyContent: 'space-between', - gap: '8px', - padding: '5px 8px', - borderRadius: '8px', - border: '1px solid transparent', - minWidth: 0, - flexWrap: 'nowrap', - }, - clamp1: { - display: '-webkit-box', - WebkitLineClamp: '1', - WebkitBoxOrient: 'vertical', - overflow: 'hidden', - }, - clamp2: { - display: '-webkit-box', - WebkitLineClamp: '2', - WebkitBoxOrient: 'vertical', - overflow: 'hidden', - }, - sourceGlyph: { - width: '20px', - height: '20px', - borderRadius: '6px', - display: 'inline-flex', - alignItems: 'center', - justifyContent: 'center', - fontSize: '11px', - fontWeight: 700, - border: '1px solid rgba(127,127,127,0.22)', - background: 'rgba(127,127,127,0.10)', - flex: '0 0 auto', - }, - folderGlyph: { - fontSize: '12px', - opacity: 0.85, - flex: '0 0 auto', - }, - sourceTitle: { - fontWeight: 600, - fontSize: '13px', - overflowWrap: 'anywhere', - }, - } - - function Pill(props) { - var tone = styles[props.tone] === undefined ? styles.muted : styles[props.tone] - return h( - 'span', - { style: Object.assign({}, styles.pill, tone) }, - h('span', { style: { opacity: 0.75 } }, props.label), - h('span', { style: { fontWeight: 600 } }, String(props.value)), - ) - } - - function Banner(props) { - var tone = styles[props.tone] === undefined ? styles.muted : styles[props.tone] - return h('div', { style: Object.assign({}, styles.banner, tone), role: props.role }, props.children) - } - - function Field(props) { - return h( - 'label', - { style: styles.field }, - h('span', { style: styles.label }, props.label), - h('input', { - style: styles.input, - type: props.type === undefined ? 'text' : props.type, - value: props.value, - placeholder: props.placeholder === undefined ? '' : props.placeholder, - spellCheck: false, - autoComplete: 'off', - disabled: props.disabled === true, - onChange: function (event) { - props.onChange(event.target.value) - }, - }), - props.hint === undefined ? null : h('span', { style: styles.hint }, props.hint), - ) - } - - function Btn(props) { - var base = props.variant === 'primary' ? styles.buttonPrimary : props.variant === 'danger' ? styles.buttonDanger : props.variant === 'small' ? styles.buttonSmall : styles.button - var style = props.disabled === true ? Object.assign({}, base, { opacity: 0.45, cursor: 'not-allowed' }) : base - return h( - 'button', - { - type: 'button', - style: style, - disabled: props.disabled === true, - onClick: props.onClick, - 'aria-label': props.ariaLabel, - }, - props.label, - ) - } - - function IconBtn(props) { - var style = Object.assign({}, styles.iconBtn, props.danger === true ? styles.iconBtnDanger : null, props.disabled === true ? { opacity: 0.45, cursor: 'not-allowed' } : null) - return h( - 'button', - { - type: 'button', - style: style, - disabled: props.disabled === true, - onClick: props.disabled === true ? undefined : props.onClick, - 'aria-label': props.ariaLabel, - title: props.ariaLabel, - }, - props.glyph, - ) - } - - function Switch(props) { return h( - 'div', - { style: styles.tabs, role: 'tablist', 'aria-label': props.ariaLabel }, - props.options.map(function (option) { - var active = option.value === props.value - return h( - 'button', - { - key: option.value, - type: 'button', - role: 'tab', - 'aria-selected': active, - style: Object.assign({}, styles.tab, active ? styles.tabActive : null), - onClick: function () { - props.onChange(option.value) - }, - }, - option.label, - ) - }), - ) - } - - // A real switch, not a checkbox: the tree rows are dense and the state has - // to be readable at a glance in both themes. - function Toggle(props) { - var on = props.checked === true - var disabled = props.disabled === true - return h( - 'button', - { - type: 'button', - role: 'switch', - 'aria-checked': on, - 'aria-label': props.label, - title: props.label, - disabled: disabled, - style: { - width: '38px', - height: '22px', - flex: '0 0 auto', - padding: '2px', - borderRadius: '999px', - border: '1px solid ' + (on ? 'rgba(0,0,0,0.25)' : 'rgba(127,127,127,0.45)'), - background: on ? '#3f3f46' : 'rgba(127,127,127,0.28)', - display: 'flex', - alignItems: 'center', - justifyContent: on ? 'flex-end' : 'flex-start', - cursor: disabled ? 'not-allowed' : 'pointer', - opacity: disabled ? 0.45 : 1, - transition: 'background 120ms ease', - }, - onClick: disabled ? undefined : props.onChange, - }, - h('span', { - style: { - width: '16px', - height: '16px', - borderRadius: '999px', - background: 'rgba(255,255,255,0.96)', - boxShadow: '0 1px 2px rgba(0,0,0,0.25)', - flex: '0 0 auto', - }, - }), - ) - } - - function Section(props) { - return h( - 'div', - { style: styles.section }, - h( - 'div', - { style: styles.sectionHead }, - h('h4', { style: styles.sectionTitle }, props.title), - props.aside === undefined ? null : h('div', { style: styles.actions }, props.aside), - ), - props.hint === undefined ? null : h('p', { style: styles.hint }, props.hint), - props.children, - ) - } - - function findCheck(source, id) { - if (source === null || source === undefined || !Array.isArray(source.checks)) return undefined - for (var index = 0; index < source.checks.length; index += 1) { - if (source.checks[index] !== null && typeof source.checks[index] === 'object' && source.checks[index].id === id) { - return source.checks[index] - } - } - return undefined - } - - // ---- P3: skill market ------------------------------------------------- - function MarketSection(props) { - var capabilities = props.capabilities || {} - var tuple = React.useState({ - providers: null, provider: '', query: '', entries: [], nextCursor: null, cursor: 0, - status: 'idle', error: null, detail: null, plan: null, selected: {}, busy: '', notice: null, - }) - var state = tuple[0] - var setState = tuple[1] - function patch(next) { setState(function (prev) { return Object.assign({}, prev, next) }) } - var available = capabilities.features !== undefined && capabilities.features.skillsMarketplace === true - - React.useEffect(function () { - if (available) loadProviders() - }, []) - - function loadProviders() { - request(API.market + '/providers', { method: 'GET' }).then(function (res) { - if (res.body !== null && res.body !== undefined && res.body.ok === true) { - var list = Array.isArray(res.body.providers) ? res.body.providers : [] - patch({ providers: list, provider: state.provider !== '' ? state.provider : (list.length > 0 ? list[0].id : '') }) - } else { - patch({ error: bodyError(res, '读取市场来源失败') }) - } - }, function (error) { patch({ error: messageOf(error) }) }) - } - function search(append) { - if (state.provider === '') return - var cursor = append === true ? state.cursor : 0 - patch({ busy: 'search', error: null, status: 'loading' }) - var url = API.market + '/search?provider=' + encodeURIComponent(state.provider) + '&q=' + encodeURIComponent(state.query) + '&cursor=' + String(cursor) - request(url, { method: 'GET' }).then(function (res) { - if (res.body !== null && res.body !== undefined && res.body.ok === true) { - var entries = append === true ? state.entries.concat(res.body.entries || []) : (res.body.entries || []) - patch({ entries: entries, nextCursor: res.body.nextCursor === undefined ? null : res.body.nextCursor, cursor: res.body.nextCursor === undefined ? cursor : res.body.nextCursor, status: 'ready', busy: '', detail: null, plan: null }) - } else { - patch({ status: 'error', busy: '', error: bodyError(res, '市场搜索失败') }) - } - }, function (error) { patch({ status: 'error', busy: '', error: messageOf(error) }) }) - } - function openDetail(entry) { - patch({ busy: 'detail', detail: null, plan: null, notice: null }) - postJson(API.market + '/detail', { provider: state.provider, id: entry.id }).then(function (res) { - if (res.body !== null && res.body !== undefined && res.body.ok === true) patch({ detail: res.body, busy: '' }) - else patch({ busy: '', error: bodyError(res, '读取条目详情失败') }) - }, function (error) { patch({ busy: '', error: messageOf(error) }) }) - } - function makePlan() { - if (state.detail === null) return - patch({ busy: 'plan', plan: null, notice: null }) - postJson(API.market + '/plan', { provider: state.provider, id: state.detail.id }).then(function (res) { - if (res.body !== null && res.body !== undefined && res.body.ok === true) { - var selected = {} - ;(res.body.candidates || []).forEach(function (candidate) { - if (candidate.conflict !== true && candidate.problem === undefined) selected[candidate.folder] = true - }) - patch({ plan: res.body, selected: selected, busy: '' }) - } else patch({ busy: '', error: bodyError(res, '无法生成安装计划') }) - }, function (error) { patch({ busy: '', error: messageOf(error) }) }) - } - function toggleFolder(folder) { - var next = Object.assign({}, state.selected) - next[folder] = next[folder] !== true - patch({ selected: next }) - } - function commitPlan() { - if (state.plan === null) return - var folders = Object.keys(state.selected).filter(function (folder) { return state.selected[folder] === true }) - if (folders.length === 0) { patch({ notice: { tone: 'warn', text: '请至少选中一个技能。' } }); return } - patch({ busy: 'commit' }) - postJson(API.market + '/commit', { planId: state.plan.planId, folders: folders }).then(function (res) { - if (res.body !== null && res.body !== undefined && res.body.ok === true) { - var skipped = Array.isArray(res.body.skipped) ? res.body.skipped.length : 0 - patch({ busy: '', plan: null, detail: null, notice: { tone: 'ok', text: '已安装:' + ((res.body.installed || []).join('、') || '无') + (skipped > 0 ? ';跳过 ' + skipped + ' 项' : '') } }) - if (typeof props.onInstalled === 'function') props.onInstalled() - } else patch({ busy: '', notice: { tone: 'bad', text: bodyError(res, '安装失败') } }) - }, function (error) { patch({ busy: '', notice: { tone: 'bad', text: messageOf(error) } }) }) - } - - if (!available) { - return h(Section, { title: '技能市场', hint: '本版没有配置经验证的市场来源。' }, - h('p', { style: styles.hint }, '能力缺口:capabilities.features.skillsMarketplace = false,因此不显示任何搜索框或示例条目。')) - } - var providerLabel = state.provider - if (state.providers !== null) { - state.providers.forEach(function (provider) { if (provider.id === state.provider) providerLabel = provider.label }) - } - var resultRows = state.entries.map(function (entry) { - return h('div', { key: entry.id, style: styles.row }, - h('div', { style: styles.rowHead }, - h('span', { style: styles.rowTitle }, entry.name), - h('div', { style: styles.pills }, - entry.version === undefined ? null : h(Pill, { label: '版本', value: String(entry.version), tone: 'muted' }), - h(Pill, { label: '技能', value: (entry.skills || []).length, tone: 'muted' }), - entry.author === undefined ? null : h(Pill, { label: '作者', value: String(entry.author), tone: 'muted' }), - ), - ), - h('p', { style: styles.hint }, entry.description === '' ? '(无描述)' : entry.description), - h('div', { style: styles.actions }, h(Btn, { label: '查看详情', variant: 'small', disabled: state.busy !== '', onClick: function () { openDetail(entry) } })), - ) - }) - var detailBox = null - if (state.detail !== null) { - var details = Array.isArray(state.detail.skillDetails) ? state.detail.skillDetails : [] - detailBox = h('div', { style: styles.nested }, - h('p', { style: styles.hint }, '来源:' + String(state.detail.sourceRepo) + ' · ' + (state.detail.description || '')), - h('div', { style: { display: 'flex', flexDirection: 'column', gap: '4px' } }, - details.map(function (skill) { - return h('div', { key: skill.folder, style: styles.rowHead }, - h('span', { style: styles.rowTitle }, skill.name === undefined ? skill.folder : skill.name), - h('span', { style: styles.hint }, skill.ok === false ? '(无法读取 SKILL.md)' : String(skill.description || '')), - ) - }), - ), - h('div', { style: styles.actions }, h(Btn, { variant: 'primary', label: state.busy === 'plan' ? '拉取中…' : '拉取安装计划', disabled: state.busy !== '', onClick: makePlan })), - ) - } - var planBox = null - if (state.plan !== null) { - var candidates = Array.isArray(state.plan.candidates) ? state.plan.candidates : [] - planBox = h('div', { style: styles.nested }, - h('p', { style: styles.hint }, '已固定提交:' + (state.plan.commit === undefined ? '克隆时记录实际提交' : String(state.plan.commit).slice(0, 12)) + ';只安装勾选项,冲突项默认不勾,不执行仓库脚本。'), - candidates.length === 0 ? h('p', { style: styles.hint }, '该条目没有可安装的技能。') : null, - h('div', { style: { display: 'flex', flexDirection: 'column', gap: '6px' } }, - candidates.map(function (candidate) { - return h('label', { key: candidate.folder, style: styles.checkboxRow }, - h('input', { type: 'checkbox', checked: state.selected[candidate.folder] === true, disabled: candidate.conflict === true || candidate.problem !== undefined, onChange: function () { toggleFolder(candidate.folder) } }), - h('span', null, candidate.folder + ' · ' + String(candidate.files) + ' 个文件 · ' + formatBytes(candidate.bytes) + (candidate.conflict === true ? ' · 已存在,跳过' : '') + (candidate.problem === undefined ? '' : ' · ' + candidate.problem)), - ) - }), - ), - h('div', { style: styles.actions }, h(Btn, { variant: 'primary', label: state.busy === 'commit' ? '安装中…' : '安装选中', disabled: state.busy !== '', onClick: commitPlan })), - ) - } - return h(Section, { title: '技能市场', hint: '来自经验证的公开市场清单;安装走暂存计划、只装勾选项并固定提交。' }, - h('div', { style: { display: 'flex', flexDirection: 'column', gap: '10px' } }, - h('div', { style: styles.pills }, - h(Pill, { label: '来源', value: String(providerLabel || '未配置'), tone: 'muted' }), - state.providers === null ? null : h(Pill, { label: '已接入', value: state.providers.length, tone: 'ok' }), - ), - h('div', { style: { display: 'flex', gap: '8px', alignItems: 'flex-end', flexWrap: 'wrap' } }, - h('div', { style: { flex: '1 1 220px', minWidth: 0 } }, h(Field, { label: '搜索市场', value: state.query, placeholder: '例如 xlsx / pdf / design', onChange: function (value) { patch({ query: value }) } })), - h(Btn, { variant: 'primary', label: state.busy === 'search' ? '搜索中…' : '搜索', disabled: state.busy !== '', onClick: function () { search(false) } }), - ), - state.error === null ? null : h(Banner, { tone: 'bad' }, state.error), - state.notice === null ? null : h(Banner, { tone: state.notice.tone }, state.notice.text), - state.status === 'ready' && state.entries.length === 0 ? h('p', { style: styles.hint }, '没有匹配的市场条目。') : null, - h('div', { style: { display: 'flex', flexDirection: 'column', gap: '8px' } }, resultRows), - state.nextCursor === null ? null : h('div', { style: styles.actions }, h(Btn, { label: '加载更多', variant: 'small', disabled: state.busy !== '', onClick: function () { search(true) } })), - detailBox, - planBox, - ), - ) - } - - // ---- P3: interactive sync conflict resolution ------------------------- - function SyncResolveSection(props) { - var tuple = React.useState({ status: 'idle', data: null, busy: '', error: null, notice: null, pendingAll: null }) - var state = tuple[0] - var setState = tuple[1] - function patch(next) { setState(function (prev) { return Object.assign({}, prev, next) }) } - React.useEffect(function () { load() }, []) - function load() { - patch({ status: 'loading', error: null }) - request(API.sync + '/status', { method: 'GET' }).then(function (res) { - if (res.body !== null && res.body !== undefined && res.body.ok === true) patch({ status: 'ready', data: res.body }) - else patch({ status: 'error', error: bodyError(res, '读取同步状态失败') }) - }, function (error) { patch({ status: 'error', error: messageOf(error) }) }) - } - function resolve(name, action, conflictId) { - patch({ busy: name + ':' + action, notice: null }) - postJson(API.sync + '/resolve', { name: name, action: action, conflictId: conflictId }).then(function (res) { - if (res.body !== null && res.body !== undefined && res.body.ok === true) { - var stats = state.data === null ? null : state.data.stats - patch({ busy: '', data: { stats: stats, conflicts: res.body.conflicts || [] }, notice: { tone: 'ok', text: name + ':' + (res.body.note || '已处理') } }) - if (typeof props.onResolved === 'function') props.onResolved() - } else patch({ busy: '', notice: { tone: 'bad', text: bodyError(res, '处理冲突失败') } }) - }, function (error) { patch({ busy: '', notice: { tone: 'bad', text: messageOf(error) } }) }) - } - function runNow() { - patch({ busy: 'run' }) - postJson(API.sync + '/run', {}).then(function (res) { - if (res.body !== null && res.body !== undefined && res.body.ok === true) patch({ busy: '', data: { stats: res.body.stats, conflicts: res.body.conflicts || [] }, notice: { tone: 'ok', text: '已执行一次同步。' } }) - else patch({ busy: '', notice: { tone: 'bad', text: bodyError(res, '同步失败') } }) - }, function (error) { patch({ busy: '', notice: { tone: 'bad', text: messageOf(error) } }) }) - } - function resolveAll(action) { - if (conflicts.length === 0) { patch({ pendingAll: null }); return } - patch({ busy: 'all:' + action, notice: null }) - postJson(API.sync + '/resolve-all', { action: action, conflictIds: conflicts.map(function (item) { return item.conflictId }) }).then(function (res) { - if (res.body !== null && res.body !== undefined && res.body.ok === true) { - var resolved = Array.isArray(res.body.resolved) ? res.body.resolved : [] - var failed = Array.isArray(res.body.failed) ? res.body.failed : [] - var text = '批量处理完成:成功 ' + resolved.length + ' 项' + (failed.length === 0 ? '。' : ',失败 ' + failed.length + ' 项(' + failed.map(function (item) { return item.name + ':' + item.error }).join('、') + ')。') - patch({ busy: '', pendingAll: null, data: { stats: res.body.stats, conflicts: res.body.conflicts || [] }, notice: { tone: failed.length === 0 ? 'ok' : 'warn', text: text } }) - if (typeof props.onResolved === 'function') props.onResolved() - } else patch({ busy: '', notice: { tone: 'bad', text: bodyError(res, '批量处理冲突失败') } }) - }, function (error) { patch({ busy: '', notice: { tone: 'bad', text: messageOf(error) } }) }) - } - var data = state.data - var stats = data === null || data === undefined ? null : data.stats - var conflicts = data === null || data === undefined || !Array.isArray(data.conflicts) ? [] : data.conflicts - var statPills = stats === null ? h('p', { style: styles.hint }, '同步引擎未启用或尚未运行。') : h('div', { style: styles.pills }, - h(Pill, { label: '运行中', value: stats.running === true ? '是' : '否', tone: stats.running === true ? 'warn' : 'muted' }), - h(Pill, { label: '上次运行', value: formatIso(stats.lastRunAt), tone: 'muted' }), - h(Pill, { label: '上传', value: (stats.uploaded || []).length, tone: 'muted' }), - h(Pill, { label: '下载', value: (stats.downloaded || []).length, tone: 'muted' }), - h(Pill, { label: '冲突', value: conflicts.length, tone: conflicts.length > 0 ? 'warn' : 'muted' }), - ) - var conflictRows = conflicts.map(function (conflict) { - var local = conflict.local || {} - var cloud = conflict.cloud - return h('div', { key: conflict.name, style: styles.row }, - h('div', { style: styles.rowHead }, - h('span', { style: styles.rowTitle }, conflict.name), - h('div', { style: styles.pills }, - h(Pill, { label: '原因', value: String(conflict.reason), tone: 'warn' }), - h(Pill, { label: '本地', value: local.exists === true ? (local.files + ' 文件') : '缺失', tone: local.exists === true ? 'muted' : 'bad' }), - h(Pill, { label: '云端', value: cloud === null ? '无' : ('rev ' + String(cloud.revision === undefined ? '?' : cloud.revision)), tone: cloud === null ? 'muted' : 'ok' }), - ), - ), - h('p', { style: styles.hint }, conflict.note), - h('div', { style: styles.actions }, - h(Btn, { label: '保留本地', variant: 'small', disabled: state.busy !== '' || conflict.canKeepLocal !== true, onClick: function () { resolve(conflict.name, 'keep_local', conflict.conflictId) } }), - h(Btn, { label: '采用云端', variant: 'small', disabled: state.busy !== '' || conflict.canTakeCloud !== true, onClick: function () { resolve(conflict.name, 'take_cloud', conflict.conflictId) } }), - h(Btn, { label: '手动合并', variant: 'small', disabled: state.busy !== '', onClick: function () { resolve(conflict.name, 'manual', conflict.conflictId) } }), - ), - ) - }) - var batchRow = null - if (conflicts.length > 0) { - batchRow = h('div', { style: styles.actions }, - h(Btn, { label: '全部保留本地', variant: 'small', disabled: state.busy !== '' || !conflicts.some(function (item) { return item.canKeepLocal === true }), onClick: function () { patch({ pendingAll: 'keep_local', notice: null }) } }), - h(Btn, { label: '全部采用云端', variant: 'small', disabled: state.busy !== '' || !conflicts.some(function (item) { return item.canTakeCloud === true }), onClick: function () { patch({ pendingAll: 'take_cloud', notice: null }) } }), - ) - } - var confirmAllRow = null - if (state.pendingAll !== null && conflicts.length > 0) { - var pendingText = state.pendingAll === 'keep_local' ? '把全部本地版本上传为新版本' : '用云端版本覆盖全部本地(覆盖前自动备份)' - confirmAllRow = h('div', { style: styles.nested }, - h('p', { style: styles.hint }, '确认' + pendingText + '?共 ' + conflicts.length + ' 项。'), - h('div', { style: styles.actions }, - h(Btn, { variant: 'primary', label: state.busy === 'all:keep_local' || state.busy === 'all:take_cloud' ? '处理中…' : '确认批量', disabled: state.busy !== '', onClick: function () { resolveAll(state.pendingAll) } }), - h(Btn, { variant: 'small', label: '取消', disabled: state.busy !== '', onClick: function () { patch({ pendingAll: null }) } }), - ), - ) - } - return h(Section, { title: '云端同步与冲突', hint: '同步先比内容与更新时间:一致直接认领,不同则自动采用较新的一边(覆盖云端前先备份本地);只有时间相同/缺失或目录结构不支持时才逐项询问,也可用批量按钮一次处理。', aside: h(Btn, { label: state.busy === 'run' ? '同步中…' : '立即同步', variant: 'small', disabled: state.busy !== '', onClick: runNow }) }, - h('div', { style: { display: 'flex', flexDirection: 'column', gap: '10px' } }, - state.error === null ? null : h(Banner, { tone: 'bad' }, state.error), - state.notice === null ? null : h(Banner, { tone: state.notice.tone }, state.notice.text), - statPills, - batchRow, - confirmAllRow, - conflicts.length === 0 ? h('p', { style: styles.hint }, '当前没有待处理冲突。') : h('div', { style: { display: 'flex', flexDirection: 'column', gap: '8px' } }, conflictRows), - ), - ) - } - - // ---- P4: custom connection / edit / scope / policy --------------------- - // The MCP marketplace section was removed in 0.9.6: the host's `catalog` - // method answers 405 on current dsh-mcp-connector builds, so the card only - // rendered an error banner. Discover/install stays in the host's own - // plugin market instead of a broken embedded panel. - - function McpCustomSection(props) { - var open = React.useState(false) - var tuple = React.useState({ transport: 'streamable-http', name: '', serverName: '', url: '', command: '', args: '', cwd: '', authMode: 'none', bearerToken: '', apiKeyHeader: 'X-Api-Key', apiKeyValue: '', importJson: '', busy: '', notice: null }) - var s = tuple[0] - var set = tuple[1] - function patch(next) { set(function (prev) { return Object.assign({}, prev, next) }) } - function submit() { - if (String(s.name).trim() === '') { patch({ notice: { tone: 'warn', text: '请填写连接名称。' } }); return } - var params = { name: String(s.name).trim(), serverName: String(s.serverName).trim() || undefined, transport: s.transport, authMode: s.authMode } - if (s.transport === 'stdio') { - if (String(s.command).trim() === '') { patch({ notice: { tone: 'warn', text: 'stdio 连接需要启动命令。' } }); return } - params.command = String(s.command).trim() - params.args = String(s.args).split(/\s+/).filter(function (x) { return x !== '' }) - if (String(s.cwd).trim() !== '') params.cwd = String(s.cwd).trim() - } else { - if (String(s.url).trim() === '') { patch({ notice: { tone: 'warn', text: 'HTTP 连接需要 URL。' } }); return } - params.url = String(s.url).trim() - if (s.authMode === 'bearer') params.bearerToken = String(s.bearerToken) - if (s.authMode === 'api-key') { params.apiKeyHeader = String(s.apiKeyHeader); params.apiKeyValue = String(s.apiKeyValue) } - } - patch({ busy: 'configure', notice: null }) - bridgeCall('configure', params).then(function (res) { - var ok = res.status === 200 && res.body !== null && res.body.ok === true - patch({ busy: '', notice: ok ? { tone: 'ok', text: bridgeMessage(res) } : { tone: 'bad', text: '新增失败:' + bridgeMessage(res) } }) - if (ok && typeof props.onChanged === 'function') props.onChanged() - }) - } - function importJson() { - if (String(s.importJson).trim() === '') { patch({ notice: { tone: 'warn', text: '请粘贴 mcpServers JSON。' } }); return } - patch({ busy: 'import', notice: null }) - bridgeCall('importJson', { json: s.importJson }).then(function (res) { - var ok = res.status === 200 && res.body !== null && res.body.ok === true - patch({ busy: '', notice: ok ? { tone: 'ok', text: bridgeMessage(res) } : { tone: 'bad', text: '导入失败:' + bridgeMessage(res) } }) - if (ok && typeof props.onChanged === 'function') props.onChanged() - }) - } - return h(Section, { - title: '新增 / 导入连接', - hint: 'HTTP 与 stdio 都由宿主校验并保存凭据;本卡不回显已有密钥。stdio 的启动命令由你显式填写,云端不会执行任何本机命令。', - aside: h(Btn, { - label: open[0] ? '收起' : '展开', - ariaLabel: '展开新增连接', - variant: 'small', - onClick: function () { open[1](!open[0]) }, - }), - }, - open[0] ? h('div', { style: { display: 'flex', flexDirection: 'column', gap: '10px' } }, - h('div', { style: styles.grid }, - h(Field, { label: '名称', value: s.name, onChange: function (v) { patch({ name: v }) } }), - h(Field, { label: 'Server 名(可选)', value: s.serverName, onChange: function (v) { patch({ serverName: v }) } }), - h('div', { style: styles.field }, - h('span', { style: styles.label }, '传输方式'), - h('select', { value: s.transport, style: styles.input, onChange: function (e) { patch({ transport: e.target.value }) } }, - h('option', { value: 'streamable-http' }, 'Streamable HTTP'), - h('option', { value: 'stdio' }, 'stdio(本机进程)'), - ), - ), - ), - s.transport === 'stdio' - ? h('div', { style: styles.grid }, - h(Field, { label: '启动命令', value: s.command, placeholder: 'npx / uvx / 本机可执行文件', onChange: function (v) { patch({ command: v }) } }), - h(Field, { label: '参数(空格分隔)', value: s.args, onChange: function (v) { patch({ args: v }) } }), - h(Field, { label: '工作目录(可选)', value: s.cwd, onChange: function (v) { patch({ cwd: v }) } }), - ) - : h('div', { style: { display: 'flex', flexDirection: 'column', gap: '10px' } }, - h(Field, { label: 'URL', value: s.url, placeholder: 'https://…', onChange: function (v) { patch({ url: v }) } }), - h('div', { style: styles.grid }, - h('div', { style: styles.field }, - h('span', { style: styles.label }, '鉴权'), - h('select', { value: s.authMode, style: styles.input, onChange: function (e) { patch({ authMode: e.target.value }) } }, - h('option', { value: 'none' }, '无'), - h('option', { value: 'bearer' }, 'Bearer Token'), - h('option', { value: 'api-key' }, 'API Key'), - ), - ), - s.authMode === 'bearer' ? h(Field, { label: 'Bearer Token', type: 'password', value: s.bearerToken, onChange: function (v) { patch({ bearerToken: v }) } }) : null, - s.authMode === 'api-key' ? h(Field, { label: 'API Key 头', value: s.apiKeyHeader, onChange: function (v) { patch({ apiKeyHeader: v }) } }) : null, - s.authMode === 'api-key' ? h(Field, { label: 'API Key 值', type: 'password', value: s.apiKeyValue, onChange: function (v) { patch({ apiKeyValue: v }) } }) : null, - ), - ), - h('div', { style: styles.actions }, h(Btn, { variant: 'primary', label: s.busy === 'configure' ? '保存中…' : '校验并保存连接', disabled: s.busy !== '', onClick: submit })), - h('p', { style: styles.hint }, '或粘贴 mcpServers JSON 批量导入:'), - h('textarea', { value: s.importJson, rows: 5, style: Object.assign({}, styles.input, { fontFamily: 'monospace', resize: 'vertical' }), placeholder: '{"mcpServers":{"name":{"url":"…"}}}', onChange: function (e) { patch({ importJson: e.target.value }) } }), - h('div', { style: styles.actions }, h(Btn, { label: s.busy === 'import' ? '导入中…' : '导入 JSON', disabled: s.busy !== '', onClick: importJson })), - s.notice === null ? null : h(Banner, { tone: s.notice.tone }, s.notice.text), - ) : null, - ) - } - - function McpConfigSection(props) { - var items = Array.isArray(props.items) ? props.items : [] - var open = React.useState(false) - var tuple = React.useState({ key: '', json: '', busy: '', notice: null, scopePreview: null, targetScope: 'global', workspaceId: '', gov: null, policyPreview: null }) - var s = tuple[0] - var set = tuple[1] - function patch(next) { set(function (prev) { return Object.assign({}, prev, next) }) } - var selected = null - for (var i = 0; i < items.length; i += 1) if (items[i].key === s.key) selected = items[i] - React.useEffect(function () { - bridgeCall('governance', {}).then(function (res) { - if (res.body !== null && res.body.ok === true) patch({ gov: res.body.detail }) - }) - }, []) - function choose(key) { - patch({ key: key, json: '', scopePreview: null, policyPreview: null, notice: null }) - if (key === '') return - bridgeCall('editableConnectionConfig', { key: key }).then(function (res) { - if (res.body !== null && res.body.ok === true && res.body.detail !== undefined) patch({ json: String(res.body.detail.json === undefined ? '' : res.body.detail.json) }) - else patch({ json: '', notice: { tone: 'warn', text: bridgeMessage(res) } }) - }) - } - function save() { - if (s.key === '') return - patch({ busy: 'save' }) - bridgeCall('reconfigureConnection', { key: s.key, json: s.json }).then(function (res) { - var ok = res.body !== null && res.body.ok === true - patch({ busy: '', notice: ok ? { tone: 'ok', text: bridgeMessage(res) } : { tone: 'bad', text: '保存失败:' + bridgeMessage(res) } }) - if (ok && typeof props.onChanged === 'function') props.onChanged() - }) - } - function previewScope() { - if (s.key === '') return - patch({ busy: 'scope-preview' }) - var params = { key: s.key, targetScope: s.targetScope, mode: 'move' } - if (s.targetScope === 'project') params.workspaceId = s.workspaceId - bridgeCall('previewConnectionScope', params).then(function (res) { - var ok = res.body !== null && res.body.ok === true - patch({ busy: '', scopePreview: ok ? res.body.detail : null, notice: ok ? null : { tone: 'bad', text: bridgeMessage(res) } }) - if (ok && res.body.detail.canApply !== true) patch({ notice: { tone: 'warn', text: bridgeMessage(res) } }) - }) - } - function applyScope() { - if (s.key === '' || s.scopePreview === null) return - patch({ busy: 'scope-apply' }) - var params = { key: s.key, targetScope: s.targetScope, mode: 'move', expectedRevision: s.scopePreview.baseRevision } - if (s.targetScope === 'project') params.workspaceId = s.workspaceId - bridgeCall('applyConnectionScope', params).then(function (res) { - var ok = res.body !== null && res.body.ok === true - patch({ busy: '', notice: ok ? { tone: 'ok', text: bridgeMessage(res) } : { tone: 'bad', text: '作用域应用失败:' + bridgeMessage(res) }, scopePreview: null }) - if (ok && typeof props.onChanged === 'function') props.onChanged() - }) - } - function previewPolicy(effect) { - if (selected === null) return - patch({ busy: 'policy-preview' }) - bridgeCall('previewPolicy', { scope: 'connection', effect: effect, connectorId: selected.connectorId }).then(function (res) { - var ok = res.body !== null && res.body.ok === true - patch({ busy: '', policyPreview: ok ? res.body.detail : null, notice: ok ? { tone: 'ok', text: bridgeMessage(res) } : { tone: 'bad', text: '策略预览失败:' + bridgeMessage(res) } }) - }) - } - function applyPolicy() { - if (selected === null || s.policyPreview === null) return - patch({ busy: 'policy-apply' }) - bridgeCall('applyPolicy', { scope: 'connection', effect: s.policyPreview.mutation.effect, connectorId: selected.connectorId, expectedRevision: s.policyPreview.baseRevision }).then(function (res) { - var ok = res.body !== null && res.body.ok === true - patch({ busy: '', policyPreview: null, notice: ok ? { tone: 'ok', text: bridgeMessage(res) } : { tone: 'bad', text: '策略应用失败:' + bridgeMessage(res) } }) - if (ok) bridgeCall('governance', {}).then(function (g) { if (g.body !== null && g.body.ok === true) patch({ gov: g.body.detail }) }) - }) - } - var gov = s.gov - return h(Section, { - title: '连接配置、范围与治理', - hint: '配置编辑使用宿主的保留标记合并,空输入不会清空已有密钥;作用域与策略都带 revision 预览后再提交。', - aside: h(Btn, { - label: open[0] ? '收起' : '展开', - ariaLabel: '展开连接配置', - variant: 'small', - onClick: function () { open[1](!open[0]) }, - }), - }, - open[0] ? h('div', { style: { display: 'flex', flexDirection: 'column', gap: '12px' } }, - itemSelect(items, s.key, choose), - s.key === '' ? h('p', { style: styles.hint }, '选择一条连接后可编辑配置、调整范围或设置治理策略。') : h('div', { style: styles.nested }, - h('p', { style: styles.hint }, '可编辑配置(保留标记 表示保留原密钥):'), - h('textarea', { value: s.json, rows: 8, style: Object.assign({}, styles.input, { fontFamily: 'monospace', resize: 'vertical' }), onChange: function (e) { patch({ json: e.target.value }) } }), - h('div', { style: styles.actions }, h(Btn, { variant: 'primary', label: s.busy === 'save' ? '保存中…' : '保存配置', disabled: s.busy !== '' || s.json === '', onClick: save })), - h('p', { style: styles.hint }, '作用域(目标范围 + mode=move,凭据不复制):'), - h('div', { style: styles.grid }, - h('div', { style: styles.field }, - h('span', { style: styles.label }, '目标范围'), - h('select', { value: s.targetScope, style: styles.input, onChange: function (e) { patch({ targetScope: e.target.value }) } }, - h('option', { value: 'global' }, '全局'), - h('option', { value: 'project' }, '指定工作区'), - ), - ), - s.targetScope === 'project' ? h(Field, { label: '工作区 id', value: s.workspaceId, onChange: function (v) { patch({ workspaceId: v }) } }) : null, - ), - h('div', { style: styles.actions }, - h(Btn, { label: s.busy === 'scope-preview' ? '预览中…' : '预览作用域', disabled: s.busy !== '', onClick: previewScope }), - s.scopePreview === null ? null : h(Btn, { variant: 'primary', label: s.busy === 'scope-apply' ? '应用中…' : '应用作用域(rev ' + String(s.scopePreview.baseRevision) + ')', disabled: s.busy !== '' || s.scopePreview.canApply !== true, onClick: applyScope }), - ), - s.scopePreview === null ? null : h('p', { style: styles.hint }, '影响:' + String(s.scopePreview.impact === undefined ? '' : JSON.stringify(s.scopePreview.impact)) + ';可应用:' + (s.scopePreview.canApply === true ? '是' : '否') + ';凭据复制:否'), - h('p', { style: styles.hint }, '治理(当前 connection 级):'), - gov === null ? h('p', { style: styles.hint }, '治理文档未读取。') : h('div', { style: styles.pills }, - h(Pill, { label: 'revision', value: gov.revision, tone: 'muted' }), - h(Pill, { label: '规则', value: (gov.rules || []).length, tone: 'muted' }), - h(Pill, { label: '优先级', value: String(gov.precedenceLabel || '—'), tone: 'muted' }), - ), - h('div', { style: styles.actions }, - h(Btn, { label: '预览 allow', variant: 'small', disabled: s.busy !== '' || selected === null, onClick: function () { previewPolicy('allow') } }), - h(Btn, { label: '预览 deny', variant: 'small', disabled: s.busy !== '' || selected === null, onClick: function () { previewPolicy('deny') } }), - h(Btn, { label: '预览 inherit', variant: 'small', disabled: s.busy !== '' || selected === null, onClick: function () { previewPolicy('inherit') } }), - s.policyPreview === null ? null : h(Btn, { variant: 'primary', label: s.busy === 'policy-apply' ? '应用中…' : '应用策略(rev ' + String(s.policyPreview.baseRevision) + ')', disabled: s.busy !== '', onClick: applyPolicy }), - ), - ), - s.notice === null ? null : h(Banner, { tone: s.notice.tone }, s.notice.text), - ) : null, - ) - } - - function itemSelect(items, value, onChange) { - return h('div', { style: styles.field }, - h('span', { style: styles.label }, '连接'), - h('select', { value: value, style: styles.input, onChange: function (e) { onChange(e.target.value) } }, - h('option', { value: '' }, '选择连接…'), - items.map(function (item) { return h('option', { key: item.key, value: item.key }, String(item.name || item.key) + '(' + String(item.transport || '') + ')') }), - ), - ) - } - - // ---- import skills from other tools ------------------------------- - function ImportSection(props) { - var onImported = props && typeof props.onImported === 'function' ? props.onImported : null - var tuple = React.useState({ expanded: false, status: 'idle', sources: [], open: {}, busy: '', notice: null, imported: {}, search: '', selection: [], progress: null }) - var s = tuple[0] - var set = tuple[1] - function patch(next) { set(function (prev) { return Object.assign({}, prev, next) }) } - function load() { - patch({ status: 'loading', notice: null }) - request(API.skills + '/sources', { method: 'GET' }).then(function (res) { - if (res.body !== null && res.body !== undefined && res.body.ok === true) { - patch({ status: 'ready', sources: Array.isArray(res.body.sources) ? res.body.sources : [] }) - } else patch({ status: 'error', notice: { tone: 'bad', text: bodyError(res, '读取来源失败') } }) - }, function (error) { patch({ status: 'error', notice: { tone: 'bad', text: messageOf(error) } }) }) - } - function openPanel() { - patch({ expanded: true }) - if (s.status === 'idle' || s.status === 'error') load() - } - function closePanel() { - patch({ expanded: false }) - } - function dshFolders() { - var found = {} - s.sources.forEach(function (source) { - if (!source.writable) return - var groups = source.groups || [] - groups.forEach(function (group) { - var skills = group.skills || [] - skills.forEach(function (skill) { found[skill.folder] = true }) - }) - }) - return found - } - function toggleSource(sourceId) { - var next = Object.assign({}, s.open) - var open = s.open[sourceId] !== false - next[sourceId] = !open - patch({ open: next }) - } - var mine = dshFolders() - var others = s.sources.filter(function (source) { return !source.writable }) - var needle = String(s.search).trim().toLowerCase() - function skillsOf(source) { - var list = [] - var groups = source.groups || [] - groups.forEach(function (group) { - var items = group.skills || [] - items.forEach(function (skill) { list.push(skill) }) - }) - return list - } - function importedYet(skill) { - return mine[skill.folder] === true || s.imported[skill.folder] === true - } - var candidates = [] - others.forEach(function (source) { - if (!source.exists) return - skillsOf(source).forEach(function (skill) { - candidates.push({ source: source, skill: skill }) - }) - }) - var matches = needle === '' ? candidates : candidates.filter(function (entry) { - var hay = String(entry.skill.name || '') + ' ' + String(entry.skill.folder || '') + ' ' + String(entry.skill.description || '') - return hay.toLowerCase().indexOf(needle) >= 0 - }) - var importable = matches.filter(function (entry) { return !importedYet(entry.skill) }) - var alreadyCount = candidates.length - candidates.filter(function (entry) { return !importedYet(entry.skill) }).length - function keyOf(entry) { - return String(entry.source.id) + ':' + String(entry.skill.folder) - } - var chosen = s.selection - function isChosen(entry) { - return chosen.indexOf(keyOf(entry)) >= 0 - } - function chooseMany(list, on) { - var next = chosen.slice() - list.forEach(function (entry) { - var key = keyOf(entry) - var at = next.indexOf(key) - if (on === true && at < 0) next.push(key) - if (on !== true && at >= 0) next.splice(at, 1) - }) - patch({ selection: next }) - } - var chosenEntries = candidates.filter(function (entry) { - return chosen.indexOf(keyOf(entry)) >= 0 && !importedYet(entry.skill) - }) - function importSelected() { - var targets = chosenEntries - if (targets.length === 0) return Promise.resolve() - patch({ busy: 'batch', notice: null, progress: { done: 0, total: targets.length } }) - var done = 0 - var failures = [] - var importedMap = Object.assign({}, s.imported) - var chain = Promise.resolve() - targets.forEach(function (entry) { - chain = chain.then(function () { - return postJson(API.skills + '/import', { sourceId: entry.source.id, folder: entry.skill.folder }).then( - function (res) { - if (res.body !== null && res.body !== undefined && res.body.ok === true) { - done += 1 - importedMap[entry.skill.folder] = true - } else failures.push(String(entry.skill.name) + '(' + bodyError(res, '失败') + ')') - patch({ progress: { done: done + failures.length, total: targets.length } }) - }, - function (error) { - failures.push(String(entry.skill.name) + '(' + messageOf(error) + ')') - patch({ progress: { done: done + failures.length, total: targets.length } }) - }, - ) - }) - }) - return chain.then(function () { - patch({ - busy: '', - progress: null, - selection: [], - imported: importedMap, - notice: { - tone: failures.length === 0 ? 'ok' : 'warn', - text: '已导入 ' + String(done) + ' / ' + String(targets.length) + ' 个技能到 DSH' + (failures.length === 0 ? '。' : ';失败:' + failures.join('、')), - }, - }) - if (onImported !== null) onImported() - load() - }) - } - if (!s.expanded) { - return h( - 'div', - { style: { display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: '8px', flexWrap: 'wrap' } }, - h(Btn, { - label: '从其他软件导入', - ariaLabel: '从其他软件导入', - onClick: openPanel, - }), - s.notice === null ? null : h(Banner, { tone: s.notice.tone }, s.notice.text), - ) - } - function describe(skill) { - var text = String(skill.description === undefined || skill.description === null ? '' : skill.description).trim() - if (/^[>|][+-]?$/.test(text)) return '' - return text - } - var blocks = others.map(function (source) { - var all = skillsOf(source) - var visible = matches.filter(function (entry) { return entry.source.id === source.id }) - var open = s.open[source.id] !== false - var selectable = visible.filter(function (entry) { return !importedYet(entry.skill) }) - return h('div', { key: source.id, style: styles.row }, - h('div', { style: styles.rowHead }, - h('span', { style: { display: 'inline-flex', alignItems: 'center', gap: '8px', minWidth: 0 } }, - h('span', { style: styles.sourceGlyph }, String(source.label).slice(0, 1)), - h('span', { style: styles.sourceTitle }, String(source.label)), - h( - 'span', - { style: styles.hint }, - source.exists - ? String(all.length) + ' 个技能' + (needle === '' ? '' : ' · 匹配 ' + String(visible.length)) + ' · 可导入 ' + String(selectable.length) - : '未安装', - ), - ), - source.exists - ? h('div', { style: styles.actions }, - h(Btn, { - label: '全选本来源(' + String(selectable.length) + ')', - ariaLabel: '全选本来源 ' + String(source.label), - variant: 'small', - disabled: selectable.length === 0 || s.busy !== '', - onClick: function () { chooseMany(visible, true) }, - }), - h(Btn, { label: open ? '收起' : '展开', variant: 'small', onClick: function () { toggleSource(source.id) } }), - ) - : null, - ), - h('span', { style: styles.mono }, String(source.root)), - !source.exists || !open - ? null - : visible.length === 0 - ? h('p', { style: styles.hint }, needle === '' ? '这个来源里还没有技能。' : '没有匹配「' + s.search + '」的技能。') - : h( - 'div', - { style: { display: 'flex', flexDirection: 'column', gap: '2px', maxHeight: '320px', overflow: 'auto' } }, - visible.map(function (entry) { - var skill = entry.skill - var has = importedYet(skill) - return h( - 'label', - { key: String(skill.folder), style: styles.checkboxRow }, - h('input', { - type: 'checkbox', - checked: has || isChosen(entry), - disabled: has || s.busy !== '', - 'aria-label': '选择技能 ' + String(skill.name), - onChange: function (event) { chooseMany([entry], event.target.checked === true) }, - }), - h( - 'span', - { style: { minWidth: 0, flex: '1 1 auto' } }, - h('span', { style: styles.rowTitle }, String(skill.name)), - describe(skill) === '' ? null : h('span', { style: Object.assign({}, styles.hint, styles.clamp1) }, describe(skill)), - ), - has ? h(Pill, { label: 'DSH', value: '已导入', tone: 'muted' }) : null, - ) - }), - ), - ) - }) - return h( - Section, - { - title: '从其他软件导入', - hint: '把其他软件技能目录里的技能复制一份到 DSH,原目录不动;同名技能不能重复导入。', - aside: h( - 'div', - { style: styles.actions }, - h(Btn, { label: '刷新', variant: 'small', onClick: load }), - h(Btn, { label: '收起', variant: 'small', onClick: closePanel }), - ), - }, - s.status === 'loading' ? h('p', { style: styles.hint }, '正在读取来源…') - : others.length === 0 ? h('p', { style: styles.hint }, '没有检测到其他软件的技能目录。') - : h( - 'div', - { style: styles.root }, - h( - 'div', - { style: styles.pills }, - h(Pill, { label: '来源', value: String(others.length), tone: 'muted' }), - h(Pill, { label: '可导入', value: String(candidates.length - alreadyCount), tone: 'muted' }), - s.search === '' ? null : h(Pill, { label: '匹配', value: String(matches.length), tone: matches.length === 0 ? 'muted' : 'ok' }), - h(Pill, { label: '已选', value: String(chosenEntries.length), tone: chosenEntries.length === 0 ? 'muted' : 'ok' }), - alreadyCount === 0 ? null : h(Pill, { label: '已在 DSH', value: String(alreadyCount), tone: 'muted' }), - ), - h( - 'div', - { style: { display: 'flex', gap: '8px', alignItems: 'center', flexWrap: 'wrap' } }, - h('input', { - type: 'search', - style: Object.assign({}, styles.input, { flex: '1 1 220px', minWidth: 0 }), - value: s.search, - placeholder: '搜索技能:名称 / 目录 / 描述', - 'aria-label': '搜索要导入的技能', - onChange: function (event) { patch({ search: event.target.value }) }, - }), - s.search === '' ? null : h(Btn, { - label: '清空搜索', - variant: 'small', - onClick: function () { patch({ search: '' }) }, - }), - ), - h( - 'div', - { style: Object.assign({}, styles.actions, { flexWrap: 'wrap' }) }, - h(Btn, { - label: '全选匹配可导入(' + String(importable.length) + ')', - ariaLabel: '全选匹配可导入', - variant: 'small', - disabled: importable.length === 0 || s.busy !== '', - onClick: function () { chooseMany(importable, true) }, - }), - h(Btn, { - label: '全选全部(' + String(candidates.length - alreadyCount) + ')', - ariaLabel: '全选全部', - variant: 'small', - disabled: candidates.length - alreadyCount === 0 || s.busy !== '', - onClick: function () { - chooseMany(candidates.filter(function (entry) { return !importedYet(entry.skill) }), true) - }, - }), - h(Btn, { - label: '清空已选', - ariaLabel: '清空已选', - variant: 'small', - disabled: chosenEntries.length === 0, - onClick: function () { patch({ selection: [] }) }, - }), - h(Btn, { - variant: 'primary', - label: s.busy === 'batch' ? '导入中…' : '导入所选(' + String(chosenEntries.length) + ')', - ariaLabel: '导入所选', - disabled: s.busy !== '' || chosenEntries.length === 0, - onClick: importSelected, - }), - ), - s.progress === null ? null : h('p', { style: styles.hint }, '正在导入:' + String(s.progress.done) + ' / ' + String(s.progress.total)), - h('div', { style: { display: 'flex', flexDirection: 'column', gap: '10px' } }, blocks), - ), - s.notice === null ? null : h(Banner, { tone: s.notice.tone }, s.notice.text), - ) - } - - function SettingsCard() { - var snapshotState = React.useState(null) - var snapshot = snapshotState[0] - var setSnapshot = snapshotState[1] - - var draftState = React.useState(null) - var draft = draftState[0] - var setDraft = draftState[1] - - var tokenState = React.useState(emptyTokens()) - var tokenDraft = tokenState[0] - var setTokenDraft = tokenState[1] - - var identityState = React.useState(readStored('identity', 'account')) - var identity = identityState[0] - var setIdentity = identityState[1] - - var loginState = React.useState({ username: '', password: '' }) - var loginDraft = loginState[0] - var setLoginDraft = loginState[1] - - var loginResultState = React.useState(null) - var loginResult = loginResultState[0] - var setLoginResult = loginResultState[1] - - var errorState = React.useState(null) - var error = errorState[0] - var setError = errorState[1] - - var noticeState = React.useState(null) - var notice = noticeState[0] - var setNotice = noticeState[1] - - var testState = React.useState(null) - var testResult = testState[0] - var setTestResult = testState[1] - - var busyState = React.useState('') - var busy = busyState[0] - var setBusy = busyState[1] - - var tabState = React.useState(readStored('tab', 'connection')) - var tab = tabState[0] - var setTab = tabState[1] - - var advancedState = React.useState(readStored('advanced', 'closed') === 'open') - var advanced = advancedState[0] - var setAdvanced = advancedState[1] - - var skillsState = React.useState({ status: 'idle', data: null, error: null, detail: null, uninstall: null, plan: null, selection: [], installUrl: '', search: '', filter: 'all', busy: '' }) - var skills = skillsState[0] - var setSkills = skillsState[1] - - var mcpState = React.useState({ status: 'idle', items: [], error: null, tools: {}, busy: '', confirm: null, search: '', filter: 'all', selection: [], bulk: null, confirmBulk: false, details: {} }) - var mcp = mcpState[0] - var setMcp = mcpState[1] - - var treeState = React.useState({ status: 'idle', data: null, error: null, busy: '', expanded: {}, openGroups: {}, edit: null, confirm: null, assign: null }) - var tree = treeState[0] - var setTree = treeState[1] - - function patchSkills(patch) { - setSkills(function (previous) { - return Object.assign({}, previous, patch) - }) - } - - function patchMcp(patch) { - setMcp(function (previous) { - return Object.assign({}, previous, patch) - }) - } - - function patchTree(patch) { - setTree(function (previous) { - return Object.assign({}, previous, patch) - }) - } - - var load = React.useCallback( - function (keepDraft) { - return request(API.status) - .then(function (result) { - if (result.status !== 200 || result.body === null || result.body.ok !== true) { - setError('读取插件状态失败(HTTP ' + String(result.status) + '),请重启 DSH 后再试。') - return - } - setError(null) - setSnapshot(result.body) - if (keepDraft !== true) setDraft(pickSettings(result.body.settings)) - }) - .catch(function (failure) { - setError('无法连接插件:' + messageOf(failure)) - }) - }, - [], - ) - - React.useEffect( - function () { - load(false) - var timer = setInterval(function () { - if (typeof document !== 'undefined' && document.visibilityState === 'hidden') return - load(true) - }, POLL_MS) - return function () { - clearInterval(timer) - } - }, - [load], - ) - - var dirty = snapshot !== null && draft !== null && JSON.stringify(draft) !== JSON.stringify(pickSettings(snapshot.settings)) - - React.useEffect( - function () { - if (!dirty) return undefined - var guard = function (event) { - event.preventDefault() - event.returnValue = '有未保存的改动' - return event.returnValue - } - window.addEventListener('beforeunload', guard) - return function () { - window.removeEventListener('beforeunload', guard) - } - }, - [dirty], - ) - - function patchDraft(patch) { - setDraft(function (previous) { - return Object.assign({}, previous, patch) - }) - } - - function patchTokens(patch) { - setTokenDraft(function (previous) { - return Object.assign({}, previous, patch) - }) - } - - function run(label, task) { - setBusy(label) - setNotice(null) - return Promise.resolve() - .then(task) - .catch(function (failure) { - setNotice({ tone: 'bad', text: messageOf(failure) }) - }) - .then(function () { - setBusy('') - }) - } - - function save() { - return run('save', function () { - var payload = { - enabled: draft.enabled, - baseUrl: draft.baseUrl, - clientId: draft.clientId, - requestTimeoutMs: Number(draft.requestTimeoutMs), - defaultK: Number(draft.defaultK), - summaryEnabled: draft.summaryEnabled, - activityEnabled: draft.activityEnabled, - memoryEnabled: draft.memoryEnabled, - skillsSyncEnabled: draft.skillsSyncEnabled, - skillsDir: String(draft.skillsDir).trim(), - } - if (tokenDraft.activity.trim() !== '') payload.activityToken = tokenDraft.activity.trim() - if (tokenDraft.memory.trim() !== '') payload.memoryToken = tokenDraft.memory.trim() - if (tokenDraft.summary.trim() !== '') payload.summaryToken = tokenDraft.summary.trim() - return request(API.config, { method: 'PUT', body: JSON.stringify(payload) }).then(function (result) { - if (result.status !== 200 || result.body === null || result.body.ok !== true) { - throw new Error('保存失败:' + bodyError(result)) - } - setTokenDraft(emptyTokens()) - var cleared = Array.isArray(result.body.credentialsCleared) ? result.body.credentialsCleared : [] - setNotice( - cleared.length === 0 - ? { tone: 'ok', text: '已保存到本机配置文件。' } - : { tone: 'warn', text: '服务地址的 origin 已变更,旧地址的令牌已清除(' + cleared.join('、') + '),请重新登录或填写新令牌。' }, - ) - setSnapshot(result.body) - setDraft(pickSettings(result.body.settings)) - }) - }) - } - - function clearToken(which) { - return run('clear-' + which, function () { - var payload = which === 'activity' ? { clearActivityToken: true } : which === 'memory' ? { clearMemoryToken: true } : { clearSummaryToken: true } - return request(API.config, { method: 'PUT', body: JSON.stringify(payload) }).then(function (result) { - if (result.status !== 200 || result.body === null || result.body.ok !== true) { - throw new Error('清除令牌失败:' + bodyError(result)) - } - setNotice({ tone: 'ok', text: '令牌已清除。' }) - setSnapshot(result.body) - setDraft(pickSettings(result.body.settings)) - }) - }) - } - - function test() { - return run('test', function () { - setTestResult(null) - return request(API.test, { method: 'POST', body: '{}' }).then(function (result) { - if (result.status !== 200 || result.body === null || result.body.ok !== true) { - throw new Error('测试失败(HTTP ' + String(result.status) + ')') - } - setTestResult(result.body) - load(true) - }) - }) - } - - function flush() { - return run('flush', function () { - return request(API.flush, { method: 'POST', body: '{}' }).then(function (result) { - if (result.status !== 200 || result.body === null || result.body.ok !== true) { - throw new Error('重试失败(HTTP ' + String(result.status) + ')') - } - setNotice({ tone: 'ok', text: '已重试 ' + String(result.body.sent) + ' 条,剩余 ' + String(result.body.remaining) + ' 条。' }) - load(true) - }) - }) - } - - function login() { - return run('login', function () { - setLoginResult(null) - var payload = { username: loginDraft.username.trim(), password: loginDraft.password } - if (payload.username === '' || payload.password === '') { - throw new Error('请填写用户名和密码') - } - return request(API.login, { method: 'POST', body: JSON.stringify(payload) }).then(function (result) { - if (result.status !== 200 || result.body === null || result.body.ok !== true) { - throw new Error('登录失败:' + bodyError(result)) - } - // Drop the password the moment the exchange succeeded. - setLoginDraft({ username: payload.username, password: '' }) - setLoginResult(result.body.login || null) - setNotice({ - tone: result.body.login !== null && result.body.login !== undefined && result.body.login.partial === true ? 'warn' : 'ok', - text: '登录成功,已写入可用的令牌。', - }) - setSnapshot(result.body) - setDraft(pickSettings(result.body.settings)) - }) - }) - } - - function switchIdentity(next) { - setIdentity(next) - store('identity', next) - setLoginResult(null) - setNotice(null) - } - - function switchTab(next) { - setTab(next) - store('tab', next) - if (next === 'skills' && skills.status === 'idle') loadSkills() - if (next === 'skills' && tree.status === 'idle') loadTree() - if (next === 'mcp' && mcp.status === 'idle') probeMcp() - } - - function toggleAdvanced() { - setAdvanced(function (previous) { - store('advanced', previous ? 'closed' : 'open') - return !previous - }) - } - - // A panel that opens (or reopens) on the skills/MCP tab must fetch on its - // own: relying on the click handler alone would leave a stale empty view - // after a reload, when the tab is restored from local storage. - React.useEffect( - function () { - if (tab === 'skills' && skills.status === 'idle') void loadSkills() - if (tab === 'skills' && tree.status === 'idle') void loadTree() - if (tab === 'mcp' && mcp.status === 'idle') void probeMcp() - }, - [tab, skills.status, mcp.status, tree.status], - ) - - // ---- skills ------------------------------------------------------- - - function loadSkills() { - patchSkills({ status: 'loading', error: null }) - return request(API.skills + '/api') - .then(function (result) { - if (result.status !== 200 || result.body === null || result.body.ok !== true) { - patchSkills({ status: 'error', error: '读取技能列表失败:' + statusText(result.status) }) - return - } - patchSkills({ status: 'ready', data: result.body, error: null }) - }) - .catch(function (failure) { - patchSkills({ status: 'error', error: '无法读取技能列表:' + messageOf(failure) }) - }) - } - - function skillAction(path, payload, label) { - patchSkills({ busy: label }) - return postJson(API.skills + path, payload) - .then(function (result) { - patchSkills({ busy: '' }) - if (result.status !== 200 || result.body === null || result.body.ok !== true) { - throw new Error(bodyError(result, statusText(result.status))) - } - return result.body - }) - .catch(function (failure) { - patchSkills({ busy: '' }) - patchSkills({ error: messageOf(failure) }) - setNotice({ tone: 'bad', text: messageOf(failure) }) - throw failure - }) - } - - // ---- skill tree (sources → categories → skills) -------------------- - - function loadTree() { - patchTree({ status: 'loading', error: null }) - return request(API.skills + '/sources') - .then(function (result) { - if (result.status !== 200 || result.body === null || result.body.ok !== true) { - patchTree({ status: 'error', error: '读取技能来源失败:' + statusText(result.status) }) - return - } - patchTree({ status: 'ready', data: result.body, error: null }) - }) - .catch(function (failure) { - patchTree({ status: 'error', error: '无法读取技能来源:' + messageOf(failure) }) - }) - } - - function treeAction(path, payload, label) { - patchTree({ busy: label }) - return postJson(API.skills + path, payload) - .then(function (result) { - patchTree({ busy: '' }) - if (result.status !== 200 || result.body === null || result.body.ok !== true) { - throw new Error(bodyError(result, statusText(result.status))) - } - return result.body - }) - .catch(function (failure) { - patchTree({ busy: '' }) - setNotice({ tone: 'bad', text: messageOf(failure) }) - return null - }) - } - - function createGroup() { - if (tree.edit === null) return Promise.resolve() - var name = String(tree.edit.value === undefined ? '' : tree.edit.value).trim() - if (name === '') { - setNotice({ tone: 'bad', text: '请填写分类名。' }) - return Promise.resolve() - } - return treeAction('/groups/create', { sourceId: tree.edit.sourceId, name: name }, 'create').then(function (body) { - if (body === null) return - patchTree({ edit: null }) - setNotice({ tone: 'ok', text: '已创建分类「' + String(body.name) + '」。' }) - return loadTree() - }) - } - - function saveRename() { - if (tree.edit === null) return Promise.resolve() - return treeAction('/groups/rename', { sourceId: tree.edit.sourceId, groupId: tree.edit.groupId, name: String(tree.edit.value).trim() }, 'rename').then(function (body) { - if (body === null) return - patchTree({ edit: null }) - return loadTree() - }) - } - - function deleteGroup(sourceId, groupId) { - return treeAction('/groups/delete', { sourceId: sourceId, groupId: groupId }, 'delete').then(function (body) { - if (body === null) return - patchTree({ confirm: null }) - setNotice({ tone: 'ok', text: '分类已删除,' + String(body.released) + ' 个技能回到「未分类」;没有删除任何文件。' }) - return loadTree() - }) - } - - function openAssign(source, group) { - var renameKey = String(source.id) + ':' + group.id - if (tree.assign !== null && tree.assign.sourceId === source.id && tree.assign.groupId === group.id) { - patchTree({ assign: null }) - return - } - patchTree({ - openGroups: Object.assign({}, tree.openGroups, { [renameKey]: true }), - assign: { - sourceId: source.id, - groupId: group.id, - search: '', - selection: (Array.isArray(group.skills) ? group.skills : []).map(function (skill) { - return skill.folder - }), - }, - }) - } - - function saveAssign() { - if (tree.assign === null) return Promise.resolve() - return treeAction('/groups/assign', { sourceId: tree.assign.sourceId, groupId: tree.assign.groupId, folders: tree.assign.selection }, 'assign').then(function (body) { - if (body === null) return - patchTree({ assign: null }) - return loadTree() - }) - } - - function toggleGroup(sourceId, groupId, enabled) { - return treeAction('/groups/toggle', { sourceId: sourceId, groupId: groupId, enabled: enabled }, 'toggle-group').then(function (body) { - if (body === null) return - if (body.cardOnly === true) { - setNotice({ tone: 'warn', text: '这个来源由对应软件自行加载:开关只记录在本卡,不影响它自己的加载。' }) - } else { - var failed = Array.isArray(body.failed) ? body.failed : [] - setNotice({ - tone: failed.length === 0 ? 'ok' : 'warn', - text: '已' + (enabled ? '启用' : '停用') + ' ' + String(Array.isArray(body.applied) ? body.applied.length : 0) + ' 项' + (failed.length === 0 ? '(新会话生效)。' : ',' + String(failed.length) + ' 项失败:' + failed.map(function (item) { return String(item.folder) + '(' + String(item.reason) + ')' }).join('、')), - }) - } - return loadTree() - }) - } - - function toggleSource(sourceId, enabled) { - return treeAction('/source/toggle', { sourceId: sourceId, enabled: enabled }, 'toggle-source').then(function (body) { - if (body === null) return - setNotice( - body.cardOnly === true - ? { tone: 'warn', text: '这个来源由对应软件自行加载:开关只记录在本卡。' } - : { tone: 'ok', text: '已' + (enabled ? '启用' : '停用') + ' ' + String(body.applied) + ' 个技能(新会话生效)。' }, - ) - return loadTree() - }) - } - - function revealSource(sourceId) { - return treeAction('/source/reveal', { sourceId: sourceId }, 'reveal') - } - - function openDetail(folder) { - patchSkills({ detail: { folder: folder, data: null, error: null } }) - return skillAction('/detail', { folder: folder }, 'detail').then( - function (body) { - patchSkills({ detail: { folder: folder, data: body, error: null } }) - }, - function () { - patchSkills({ detail: { folder: folder, data: null, error: '无法读取详情' } }) - }, - ) - } - - function toggleSkill(folder, enabled) { - return skillAction('/toggle', { folder: folder, enabled: enabled }, 'toggle').then(function (body) { - setNotice({ tone: 'ok', text: body.note === undefined ? '已更新。' : String(body.note) }) - return loadSkills() - }, function () {}) - } - - function planUninstall(folder) { - return skillAction('/uninstall/plan', { folder: folder }, 'uninstall').then(function (body) { - patchSkills({ uninstall: body }) - }, function () {}) - } - - function confirmUninstall() { - if (skills.uninstall === null) return Promise.resolve() - return skillAction('/remove', { folder: skills.uninstall.folder, token: skills.uninstall.token, confirmed: true }, 'remove').then(function (body) { - patchSkills({ uninstall: null }) - setNotice({ tone: 'ok', text: '已删除本机技能 ' + String(body.folder) + '(' + String(body.files) + ' 个文件)。' }) - return loadSkills() - }, function () {}) - } - - function prepareInstall() { - var url = String(skills.installUrl).trim() - if (url === '') { - setNotice({ tone: 'bad', text: '请填写 git 仓库地址。' }) - return Promise.resolve() - } - return skillAction('/install/plan', { url: url }, 'plan').then(function (body) { - var selection = (body.candidates || []) - .filter(function (candidate) { - return candidate.conflict !== true && candidate.problem === undefined - }) - .map(function (candidate) { - return candidate.folder - }) - patchSkills({ plan: body, selection: selection }) - }, function () {}) - } - - function commitInstall() { - if (skills.plan === null) return Promise.resolve() - return skillAction('/install/commit', { planId: skills.plan.planId, folders: skills.selection }, 'commit').then(function (body) { - var installed = Array.isArray(body.installed) ? body.installed : [] - var skipped = Array.isArray(body.skipped) ? body.skipped : [] - patchSkills({ plan: null, selection: [] }) - setNotice({ - tone: skipped.length === 0 ? 'ok' : 'warn', - text: '已安装 ' + String(installed.length) + ' 个技能' + (skipped.length === 0 ? '。' : ',跳过 ' + String(skipped.length) + ' 个:' + skipped.map(function (item) { return String(item.folder) + '(' + String(item.reason) + ')' }).join('、')), - }) - return loadSkills() - }, function () {}) - } - - function cancelInstall() { - if (skills.plan === null) return Promise.resolve() - return skillAction('/install/cancel', { planId: skills.plan.planId }, 'cancel').then(function () { - patchSkills({ plan: null, selection: [] }) - }, function () {}) - } - - // ---- mcp ---------------------------------------------------------- - - function mcpCall(method, params) { - var controller = typeof AbortController === 'function' ? new AbortController() : null - var timer = controller === null ? null : setTimeout(function () { - controller.abort() - }, PROBE_TIMEOUT_MS) - return fetch(MCP_BRIDGE, { - method: 'POST', - cache: 'no-store', - headers: { accept: 'application/json', 'content-type': 'application/json' }, - body: JSON.stringify({ method: method, params: params === undefined ? {} : params }), - signal: controller === null ? undefined : controller.signal, - }) - .then(function (response) { - if (timer !== null) clearTimeout(timer) - return response.json().then( - function (body) { - return { status: response.status, body: body } - }, - function () { - return { status: response.status, body: null } - }, - ) - }) - .catch(function (failure) { - if (timer !== null) clearTimeout(timer) - return { status: 0, body: null, error: messageOf(failure) } - }) - } - - function probeMcp() { - patchMcp({ status: 'probe', error: null }) - return mcpCall('status', {}).then(function (result) { - if (result.status === 200 && result.body !== null && result.body.ok === true && result.body.detail !== undefined) { - patchMcp({ status: 'ok', items: Array.isArray(result.body.detail.items) ? result.body.detail.items : [], error: null }) - return - } - patchMcp({ - status: 'missing', - items: [], - error: result.status === 0 ? '探测超时或连接被拒绝' : '接口返回 HTTP ' + String(result.status) + (result.body !== null && typeof result.body.message === 'string' ? ':' + result.body.message : ''), - }) - }) - } - - function mcpAction(label, method, params) { - patchMcp({ busy: label }) - return mcpCall(method, params).then(function (result) { - patchMcp({ busy: '' }) - if (result.status !== 200 || result.body === null || result.body.ok !== true) { - var reason = result.body !== null && typeof result.body.message === 'string' ? result.body.message : 'HTTP ' + String(result.status) - setNotice({ tone: 'bad', text: 'MCP 操作失败:' + reason }) - return null - } - setNotice({ tone: 'ok', text: 'MCP 操作已完成:' + label }) - return result.body - }) - } - - function loadTools(item) { - return mcpAction('读取工具列表', 'toolsList', { connectorId: item.connectorId, connectionKey: item.key, cachedOnly: true }).then(function (body) { - if (body === null) return - var list = body.detail !== undefined && Array.isArray(body.detail.tools) ? body.detail.tools : Array.isArray(body.tools) ? body.tools : [] - patchMcp(function (previous) { - var tools = Object.assign({}, previous.tools) - tools[item.key] = { items: list, at: Date.now() } - return Object.assign({}, previous, { tools: tools }) - }) - }) - } - - function mcpSelected() { - return Array.isArray(mcp.selection) ? mcp.selection : [] - } - - function selectMcpMany(list, on) { - var next = mcpSelected().slice() - list.forEach(function (item) { - var at = next.indexOf(item.key) - if (on === true && at < 0) next.push(item.key) - if (on !== true && at >= 0) next.splice(at, 1) - }) - patchMcp({ selection: next, confirmBulk: false }) - } - - function runMcpBulk(actionLabel, method, paramsOf) { - var chosen = mcpSelected() - var targets = (Array.isArray(mcp.items) ? mcp.items : []).filter(function (item) { - return chosen.indexOf(item.key) >= 0 - }) - if (targets.length === 0) return Promise.resolve() - patchMcp({ busy: 'bulk', confirm: null, confirmBulk: false, bulk: { label: actionLabel, done: 0, total: targets.length } }) - var failures = [] - var chain = Promise.resolve() - targets.forEach(function (item) { - chain = chain.then(function () { - return mcpCall(method, paramsOf(item)).then( - function (result) { - if (!(result.status === 200 && result.body !== null && result.body.ok === true)) { - var reason = result.body !== null && typeof result.body.message === 'string' ? result.body.message : 'HTTP ' + String(result.status) - failures.push(String(item.name === undefined ? item.key : item.name) + '(' + reason + ')') - } - patchMcp(function (previous) { - var bulk = previous.bulk === null || previous.bulk === undefined ? { label: actionLabel, done: 0, total: targets.length } : Object.assign({}, previous.bulk) - bulk.done = (bulk.done === undefined ? 0 : bulk.done) + 1 - return Object.assign({}, previous, { bulk: bulk }) - }) - }, - function (failure) { - failures.push(String(item.name === undefined ? item.key : item.name) + '(' + messageOf(failure) + ')') - }, - ) - }) - }) - return chain.then(function () { - patchMcp({ busy: '', bulk: null, selection: [], confirmBulk: false }) - setNotice({ - tone: failures.length === 0 ? 'ok' : 'warn', - text: actionLabel + '完成:成功 ' + String(targets.length - failures.length) + ' / ' + String(targets.length) + ' 项' + (failures.length === 0 ? '。' : ';失败:' + failures.join('、')), - }) - return probeMcp() - }) - } - - // ---- render ------------------------------------------------------- - - if (error !== null && snapshot === null) { - return h( - 'div', - { style: styles.root }, - h(Banner, { tone: 'bad' }, error), - h( - 'div', - { style: styles.actions }, - h(Btn, { - label: '重新加载', - onClick: function () { - load(false) - }, - }), - ), - ) - } - - if (snapshot === null || draft === null) { - return h('div', { style: styles.root }, h('p', { style: styles.hint }, '正在读取云端记忆插件状态…')) - } - - var reporter = snapshot.reporter === undefined || snapshot.reporter === null ? {} : snapshot.reporter - var tokens = snapshot.tokens === undefined || snapshot.tokens === null ? { activity: {}, memory: {}, summary: {} } : snapshot.tokens - var caps = snapshot.capabilities === undefined || snapshot.capabilities === null ? { features: {} } : snapshot.capabilities - var features = caps.features === undefined ? {} : caps.features - var checkSource = testResult !== null ? testResult : snapshot.lastCheck === null || snapshot.lastCheck === undefined ? null : snapshot.lastCheck - var recent = Array.isArray(reporter.recent) ? reporter.recent.slice(0, 8) : [] - var activityToken = tokens.activity === undefined ? {} : tokens.activity - var memoryToken = tokens.memory === undefined ? {} : tokens.memory - var summaryToken = tokens.summary === undefined ? {} : tokens.summary - // Token source label: where the effective credential comes from. An env - // token only applies when its origin binding matches the service origin. - var tokenHint = function (view) { - if (view.configured !== true) { - if (view.source === 'env') return '来自环境变量(DSH_BRAIN_*_TOKEN,绑定当前 origin)' - return undefined - } - if (view.source === 'env') return '当前:' + String(view.masked) + '(来自环境变量,绑定当前 origin)' - return '当前:' + String(view.masked) - } - var checkedAt = checkSource === null ? '—' : formatTime(checkSource.at === undefined ? checkSource.checkedAt : checkSource.at) - - var connectivity = !draft.baseUrlConfigured - ? { tone: 'warn', text: '未配置服务' } - : checkSource === null - ? { tone: 'muted', text: '未测试' } - : findCheck(checkSource, 'network') === undefined - ? { tone: 'muted', text: '未测试' } - : findCheck(checkSource, 'network').state === 'ok' - ? { tone: 'ok', text: '可达' } - : { tone: 'bad', text: '不可达' } - - var header = h( - 'div', - { style: styles.section, key: 'header' }, - h( - 'div', - { style: styles.sectionHead }, - h('h4', { style: styles.sectionTitle }, h('span', { style: { display: 'inline-block', width: '9px', height: '9px', borderRadius: '3px', background: 'linear-gradient(135deg, #6366f1, #8b5cf6)', marginRight: '8px', verticalAlign: '1px' } }), '云端记忆 · v' + String(caps.version === undefined ? '—' : caps.version)), - h( - 'div', - { style: styles.pills }, - h(Pill, { label: '连接', value: connectivity.text, tone: connectivity.tone }), - h(Pill, { label: '最后检查', value: checkedAt, tone: 'muted' }), - dirty ? h(Pill, { label: '草稿', value: '未保存', tone: 'warn' }) : null, - ), - ), - h( - 'div', - { style: styles.pills }, - h(Pill, { label: '已上报', value: reporter.recorded === undefined ? 0 : reporter.recorded, tone: 'ok' }), - h(Pill, { label: '待重试', value: reporter.queued === undefined ? 0 : reporter.queued, tone: reporter.queued > 0 ? 'warn' : 'muted' }), - h(Pill, { label: '失败', value: reporter.failed === undefined ? 0 : reporter.failed, tone: reporter.failed > 0 ? 'bad' : 'muted' }), - ), - ) - - var tabBar = h(Switch, { - key: 'tabs', - ariaLabel: '云端记忆管理页签', - value: tab, - onChange: switchTab, - options: [ - { value: 'connection', label: '连接与上报' }, - { value: 'skills', label: '技能管理' }, - { value: 'mcp', label: 'MCP 管理' }, - ], - }) - - var noticeBox = notice === null ? null : h(Banner, { tone: notice.tone, role: 'status', key: 'notice' }, notice.text) - - var connectionTab = null - if (tab === 'connection') { - var changedOrigin = draft.baseUrlConfigured && draft.baseUrlOrigin !== '' && String(draft.baseUrl) !== '' && String(draft.baseUrl).indexOf(draft.baseUrlOrigin) !== 0 - connectionTab = h( - 'div', - { style: styles.root, key: 'connection' }, - - h( - Section, - { - title: '身份验证', - hint: '两种方式最终更新同一组配置;切换不会清除任何一种方式已保存的凭据,也不会自动发起请求。', - aside: h(Switch, { - ariaLabel: '身份验证方式', - value: identity, - onChange: switchIdentity, - options: [ - { value: 'account', label: '账号登录' }, - { value: 'token', label: '手动令牌' }, - ], - }), - }, - - identity === 'account' - ? h( - 'div', - { style: styles.nested }, - h( - 'p', - { style: styles.hint }, - '用云端面板的普通用户账号登录,插件会用一次性会话换取活动与记忆令牌并写入本机。密码只在这一次请求里使用:不保存、不写日志、不回显。', - ), - h( - 'div', - { style: styles.grid }, - h(Field, { - label: '用户名', - value: loginDraft.username, - placeholder: '云端面板的账号', - onChange: function (value) { - setLoginDraft(function (previous) { - return Object.assign({}, previous, { username: value }) - }) - }, - }), - h(Field, { - label: '密码', - type: 'password', - value: loginDraft.password, - placeholder: '只用于本次换取令牌', - hint: '成功后自动清空', - onChange: function (value) { - setLoginDraft(function (previous) { - return Object.assign({}, previous, { password: value }) - }) - }, - }), - ), - h( - 'div', - { style: styles.actions }, - h(Btn, { - variant: 'primary', - label: busy === 'login' ? '登录中…' : '登录并获取令牌', - disabled: busy !== '', - onClick: login, - }), - loginResult === null - ? null - : h( - 'span', - { style: styles.hint }, - '当前账号:' + String(loginResult.user !== undefined && loginResult.user !== null && loginResult.user.username !== undefined ? loginResult.user.username : '未知') + (loginResult.warning === undefined ? '' : '(' + String(loginResult.warning) + ')'), - ), - ), - ) - : h( - 'div', - { style: styles.nested }, - h( - 'p', - { style: styles.hint }, - '三个令牌各有独立权限:activity:write 只用于上报,memory:write 同时允许检索与写入,summary:write 用于自动摘要(可选)。空值表示不修改;清除是独立动作。', - ), - h( - 'div', - { style: styles.grid }, - h(Field, { - label: '活动上报令牌(activity:write)', - type: 'password', - value: tokenDraft.activity, - placeholder: activityToken.configured === true ? '已配置 ' + String(activityToken.masked) + ',留空表示不修改' : '未配置', - hint: tokenHint(activityToken), - onChange: function (value) { - patchTokens({ activity: value }) - }, - }), - h(Field, { - label: '记忆读写令牌(memory:write)', - type: 'password', - value: tokenDraft.memory, - placeholder: memoryToken.configured === true ? '已配置 ' + String(memoryToken.masked) + ',留空表示不修改' : '未配置', - hint: tokenHint(memoryToken), - onChange: function (value) { - patchTokens({ memory: value }) - }, - }), - h(Field, { - label: '摘要令牌(summary:write,可选)', - type: 'password', - value: tokenDraft.summary, - placeholder: summaryToken.configured === true ? '已配置 ' + String(summaryToken.masked) + ',留空表示不修改' : '未配置', - hint: tokenHint(summaryToken), - onChange: function (value) { - patchTokens({ summary: value }) - }, - }), - ), - h( - 'div', - { style: styles.actions }, - h(Btn, { - label: '清除上报令牌', - variant: 'small', - disabled: activityToken.configured !== true || busy !== '', - onClick: function () { - clearToken('activity') - }, - }), - h(Btn, { - label: '清除记忆令牌', - variant: 'small', - disabled: memoryToken.configured !== true || busy !== '', - onClick: function () { - clearToken('memory') - }, - }), - h(Btn, { - label: '清除摘要令牌', - variant: 'small', - disabled: summaryToken.configured !== true || busy !== '', - onClick: function () { - clearToken('summary') - }, - }), - ), - ), - ), - - h( - Section, - { - title: '服务地址', - hint: '账号登录会把密码发到该地址:仅接受 HTTPS(本机回环除外)。不同 origin 不会复用旧令牌。', - }, - h(Field, { - label: '服务地址(baseUrl)', - value: draft.baseUrl, - placeholder: 'https://<你的域名>/api/longmemory', - hint: changedOrigin ? '地址已改成另一个 origin(当前 ' + draft.baseUrlOrigin + '):保存后旧令牌会被清除,需要重新认证。' : '当前 origin:' + (draft.baseUrlOrigin === '' ? '未配置' : draft.baseUrlOrigin), - onChange: function (value) { - patchDraft({ baseUrl: value }) - }, - }), - advanced - ? h( - 'div', - { style: styles.nested }, - h( - 'div', - { style: styles.grid }, - h(Field, { - label: '客户端标识(client_id)', - value: draft.clientId, - onChange: function (value) { - patchDraft({ clientId: value }) - }, - }), - h(Field, { - label: '请求超时(毫秒)', - type: 'number', - value: draft.requestTimeoutMs, - onChange: function (value) { - patchDraft({ requestTimeoutMs: value }) - }, - }), - - ), - h( - 'div', - { style: styles.checkboxRow }, - h('input', { - type: 'checkbox', - checked: draft.enabled, - id: 'dsh-brain-enabled', - onChange: function (event) { - patchDraft({ enabled: event.target.checked }) - }, - }), - h('label', { htmlFor: 'dsh-brain-enabled', style: styles.label }, '启用每轮活动上报(只上报时间戳、会话 id 与状态,不上传对话内容)'), - ), - h( - 'div', - { style: styles.checkboxRow }, - h('input', { - type: 'checkbox', - checked: draft.summaryEnabled, - id: 'dsh-brain-summary', - onChange: function (event) { - patchDraft({ summaryEnabled: event.target.checked }) - }, - }), - h('label', { htmlFor: 'dsh-brain-summary', style: styles.label }, '启用自动摘要(会把该轮对话文本上传为待确认提案;与上面的元数据上报是两件事)'), - ), - h( - 'div', - { style: styles.checkboxRow }, - h('input', { - type: 'checkbox', - checked: draft.activityEnabled, - id: 'dsh-brain-activity', - onChange: function (event) { - patchDraft({ activityEnabled: event.target.checked }) - }, - }), - h('label', { htmlFor: 'dsh-brain-activity', style: styles.label }, '启用活动上报(细粒度开关;未显式设置时跟随总开关)'), - ), - ) - : h( - 'div', - { style: styles.actions }, - h(Btn, { label: '高级设置', variant: 'small', onClick: toggleAdvanced }), - ), - ), - - h( - Section, - { - title: '记忆配置', - hint: '只影响记忆工具:开关未显式设置时跟随总开关,检索上限只限制单次返回条数。', - }, - - h('div', { style: styles.grid }, - h(Field, { - label: '单次搜索返回上限(defaultK)', - type: 'number', - value: draft.defaultK, - hint: '只限制一次搜索返回多少条,不限制可搜索的历史范围。默认 10。', - onChange: function (value) { - patchDraft({ defaultK: value }) - }, - }), - ), - h('div', { style: styles.checkboxRow }, - h('input', { - type: 'checkbox', - checked: draft.memoryEnabled, - id: 'dsh-brain-memory-enabled', - onChange: function (event) { - patchDraft({ memoryEnabled: event.target.checked }) - }, - }), - h('label', { htmlFor: 'dsh-brain-memory-enabled', style: styles.label }, '启用记忆工具(memory_search / memory_read / memory_add)'), - ), - ), - - features.skillsSync === false - ? h('div', null) - : h( - 'div', - null, - h( - Section, - { - title: 'Skill 同步', - hint: '同步由后台定时执行,这里只保存开关与目录;目录留空使用默认位置。', - }, - - h(Field, { - label: '技能目录(skillsDir,可选)', - value: draft.skillsDir, - placeholder: '默认 $DSH_HOME/skills', - hint: '保存后下一次同步生效,无需重启。', - onChange: function (value) { - patchDraft({ skillsDir: value }) - }, - }), - h('div', { style: styles.checkboxRow }, - h('input', { - type: 'checkbox', - checked: draft.skillsSyncEnabled, - id: 'dsh-brain-skills-sync', - onChange: function (event) { - patchDraft({ skillsSyncEnabled: event.target.checked }) - }, - }), - h('label', { htmlFor: 'dsh-brain-skills-sync', style: styles.label }, '启用云端技能同步(下载云端技能库、上传本机新技能)'), - ), - ), - ), - - h( - Section, - { - title: '连接与权限检查', - aside: h( - 'div', - { style: styles.actions }, - h(Btn, { variant: 'primary', label: busy === 'save' ? '保存中…' : '保存', disabled: busy !== '', onClick: save }), - h(Btn, { label: busy === 'test' ? '测试中…' : '测试连接', disabled: busy !== '', onClick: test }), - h(Btn, { - label: '重试上报队列', - disabled: busy !== '' || reporter.queued === undefined || reporter.queued === 0, - onClick: flush, - }), - ), - }, - checkSource === null - ? h('p', { style: styles.hint }, '还没有检查记录。点「测试连接」做一次只读检查:网络 → 活动权限 → 记忆权限 → 摘要权限。') - : h( - 'div', - { style: styles.pills }, - (checkSource.checks || []).map(function (check) { - return h(Pill, { - key: String(check.id), - label: String(check.label === undefined ? check.id : check.label), - value: CHECK_LABELS[check.state] === undefined ? String(check.state) : CHECK_LABELS[check.state], - tone: CHECK_TONES[check.state] === undefined ? 'muted' : CHECK_TONES[check.state], - }) - }), - ), - checkSource === null - ? null - : h( - 'div', - { style: { display: 'flex', flexDirection: 'column', gap: '6px' } }, - (checkSource.checks || []).map(function (check) { - return h( - 'div', - { - key: 'detail-' + String(check.id), - style: Object.assign({}, styles.banner, styles[CHECK_TONES[check.state]] === undefined ? styles.muted : styles[CHECK_TONES[check.state]]), - }, - String(check.label === undefined ? check.id : check.label) + ':' + String(check.message), - ) - }), - ), - h('p', { style: styles.hint }, '鉴权通过只代表令牌有效,不代表已经成功写入一条上报;两者在下面分开显示。'), - ), - - h( - Section, - { title: '最近上报' }, - recent.length === 0 - ? h('p', { style: styles.hint }, '还没有记录。发一条消息让它跑完一轮,这里就会出现一条。') - : h( - 'table', - { style: styles.table }, - h('thead', null, h('tr', null, h('th', { style: styles.th }, '时间'), h('th', { style: styles.th }, '结果'), h('th', { style: styles.th }, 'turn_id'))), - h( - 'tbody', - null, - recent.map(function (row, index) { - var outcome = String(row.outcome === undefined ? '' : row.outcome) - return h( - 'tr', - { key: String(row.turnId) + ':' + String(index) }, - h('td', { style: styles.td }, formatTime(row.at)), - h( - 'td', - { style: styles.td }, - h( - 'span', - { style: Object.assign({}, styles.pill, styles[OUTCOME_TONES[outcome]] === undefined ? styles.muted : styles[OUTCOME_TONES[outcome]]) }, - OUTCOME_LABELS[outcome] === undefined ? outcome : OUTCOME_LABELS[outcome], - ), - ), - h('td', { style: Object.assign({}, styles.td, styles.mono) }, String(row.turnId === undefined ? '' : row.turnId)), - ) - }), - ), - ), - reporter.lastError === undefined ? null : h('p', { style: styles.hint }, '最近一次错误:' + String(reporter.lastError)), - ), - - h( - 'p', - { style: styles.hint }, - '配置保存在 ' + String(snapshot.settingsPath === undefined ? '本机配置文件' : snapshot.settingsPath) + '。页面地址 /dsh-brain 仍然可用。', - ), - ) - } - - var skillsTab = null - if (tab === 'skills') { - var treeSources = tree.data !== null && Array.isArray(tree.data.sources) ? tree.data.sources : [] - var keyword = String(skills.search).trim().toLowerCase() - - var matchesSkill = function (skill) { - if (keyword === '') return true - return (String(skill.folder) + ' ' + String(skill.name) + ' ' + String(skill.description)).toLowerCase().indexOf(keyword) >= 0 - } - - var visibleGroupsOf = function (source) { - return (Array.isArray(source.groups) ? source.groups : []) - .map(function (group) { - var visible = (Array.isArray(group.skills) ? group.skills : []).filter(matchesSkill) - return Object.assign({}, group, { visible: visible }) - }) - .filter(function (group) { - return group.visible.length > 0 || keyword === '' - }) - } - - var detailBox = null - if (skills.detail !== null) { - var detail = skills.detail.data - detailBox = h( - 'div', - { style: styles.row }, - h( - 'div', - { style: styles.rowHead }, - h('span', { style: styles.rowTitle }, '详情:' + String(skills.detail.folder)), - h(Btn, { - label: '关闭', - variant: 'small', - onClick: function () { - patchSkills({ detail: null }) - }, - }), - ), - detail === null - ? h('p', { style: styles.hint }, skills.detail.error === null ? '正在读取…' : String(skills.detail.error)) - : h( - 'div', - { style: styles.nested }, - h( - 'div', - { style: styles.grid }, - h(Field, { label: '目录', value: String(detail.dir), onChange: function () {} }), - h(Field, { label: 'SKILL.md 哈希', value: String(detail.skill !== undefined ? detail.skill.skillSha256 : ''), onChange: function () {} }), - ), - h('p', { style: styles.hint }, '文件:' + String(Array.isArray(detail.files) ? detail.files.length : 0) + ' 个,共 ' + formatBytes(detail.totalBytes) + (detail.truncated ? '(SKILL.md 过长,已截断显示)' : '')), - h('pre', { style: styles.code }, String(detail.skillMd === undefined ? '' : detail.skillMd)), - h( - 'p', - { style: styles.hint }, - '文件清单:' + (Array.isArray(detail.files) ? detail.files.map(function (file) { return String(file.path) + (file.script ? '(脚本)' : '') }).join('、') : ''), - ), - ), - ) - } - - var uninstallBox = null - if (skills.uninstall !== null) { - uninstallBox = h( - 'div', - { style: Object.assign({}, styles.row, styles.warn) }, - h('div', { style: styles.rowHead }, h('span', { style: styles.rowTitle }, '卸载确认:' + String(skills.uninstall.folder))), - h( - 'p', - { style: styles.hint }, - '将实际删除:' + String(skills.uninstall.dir) + ';影响:' + String(skills.uninstall.files) + ' 个文件 / ' + formatBytes(skills.uninstall.bytes) + (Array.isArray(skills.uninstall.scripts) && skills.uninstall.scripts.length > 0 ? ';含脚本:' + skills.uninstall.scripts.join('、') : ''), - ), - h('p', { style: styles.hint }, String(skills.uninstall.note)), - h( - 'div', - { style: styles.actions }, - h(Btn, { variant: 'danger', label: tree.busy === 'uninstall' ? '删除中…' : '确认删除(不可恢复)', disabled: tree.busy !== '', onClick: confirmUninstall }), - h(Btn, { - label: '取消', - disabled: tree.busy !== '', - onClick: function () { - patchSkills({ uninstall: null }) - }, - }), - ), - ) - } - - var planBox = null - if (skills.plan !== null) { - var candidates = Array.isArray(skills.plan.candidates) ? skills.plan.candidates : [] - planBox = h( - 'div', - { style: styles.row }, - h('div', { style: styles.rowHead }, h('span', { style: styles.rowTitle }, '安装预览:' + String(skills.plan.url))), - h( - 'p', - { style: styles.hint }, - '计划 ' + String(skills.plan.planId) + ' 有效到 ' + formatTime(skills.plan.expiresAt) + '。默认不勾选冲突项;只安装勾选的技能,来源中的安装脚本不会执行。', - ), - h( - 'div', - { style: { display: 'flex', flexDirection: 'column', gap: '6px' } }, - candidates.map(function (candidate) { - var selected = skills.selection.indexOf(candidate.folder) >= 0 - var blocked = candidate.conflict === true || candidate.problem !== undefined - return h( - 'label', - { key: String(candidate.id), style: styles.checkboxRow }, - h('input', { - type: 'checkbox', - checked: selected, - disabled: blocked, - onChange: function (event) { - var next = skills.selection.slice() - var at = next.indexOf(candidate.folder) - if (event.target.checked && at < 0) next.push(candidate.folder) - if (!event.target.checked && at >= 0) next.splice(at, 1) - patchSkills({ selection: next }) - }, - }), - h( - 'span', - { style: { minWidth: 0 } }, - h('span', { style: { fontWeight: 600 } }, String(candidate.name)), - h('span', { style: styles.hint }, ' · ' + String(candidate.folder) + ' · ' + String(candidate.files) + ' 个文件 · ' + formatBytes(candidate.bytes) + (candidate.conflict === true ? ' · 已存在(' + String(candidate.conflictLocation === 'holding' ? '已停用目录' : '技能目录') + '),不覆盖' : '') + (candidate.problem === undefined ? '' : ' · ' + String(candidate.problem)) + (Array.isArray(candidate.scripts) && candidate.scripts.length > 0 ? ' · 含脚本:' + candidate.scripts.join('、') : '')), - ), - ) - }), - ), - h( - 'div', - { style: styles.actions }, - h(Btn, { - variant: 'primary', - label: tree.busy === 'commit' ? '安装中…' : '安装所选(' + String(skills.selection.length) + ')', - disabled: tree.busy !== '' || skills.selection.length === 0, - onClick: commitInstall, - }), - h(Btn, { label: '放弃计划', variant: 'small', disabled: tree.busy !== '', onClick: cancelInstall }), - ), - ) - } - - var assignBox = null - if (tree.assign !== null) { - var assignSource = treeSources.find(function (source) { - return source.id === tree.assign.sourceId - }) - var assignGroup = assignSource === undefined - ? undefined - : (assignSource.groups || []).find(function (group) { - return group.id === tree.assign.groupId - }) - var assignable = [] - if (assignSource !== undefined) { - (assignSource.groups || []).forEach(function (group) { - (group.skills || []).forEach(function (skill) { - if (!assignable.some(function (item) { return item.folder === skill.folder })) assignable.push(skill) - }) - }) - } - var assignSearch = String(tree.assign.search === undefined ? '' : tree.assign.search) - var assignNeedle = assignSearch.trim().toLowerCase() - var filteredAssignable = assignNeedle === '' - ? assignable - : assignable.filter(function (skill) { - var hay = (String(skill.name || '') + ' ' + String(skill.folder || '') + ' ' + String(skill.description || '')).toLowerCase() - return hay.indexOf(assignNeedle) >= 0 - }) - var selectedCount = tree.assign.selection.length - var matchedSelected = 0 - filteredAssignable.forEach(function (skill) { - if (tree.assign.selection.indexOf(skill.folder) >= 0) matchedSelected += 1 - }) - function setSelection(next) { - patchTree({ assign: Object.assign({}, tree.assign, { selection: next }) }) - } - function setSelectionFilter(value) { - patchTree({ assign: Object.assign({}, tree.assign, { search: value }) }) - } - function selectMany(list, on) { - var next = tree.assign.selection.slice() - list.forEach(function (skill) { - var at = next.indexOf(skill.folder) - if (on && at < 0) next.push(skill.folder) - if (!on && at >= 0) next.splice(at, 1) - }) - setSelection(next) - } - assignBox = h( - 'div', - { style: styles.row }, - h( - 'div', - { style: styles.rowHead }, - h('span', { style: styles.rowTitle }, '加入分类:' + String(assignGroup === undefined ? '' : assignGroup.name)), - h( - 'div', - { style: styles.pills }, - h(Pill, { label: '可分配', value: String(assignable.length), tone: 'muted' }), - h(Pill, { label: '匹配', value: String(filteredAssignable.length), tone: filteredAssignable.length === 0 ? 'muted' : 'ok' }), - h(Pill, { label: '已选', value: String(selectedCount), tone: selectedCount === 0 ? 'muted' : 'ok' }), - ), - ), - h('p', { style: styles.hint }, '先搜索再勾选,或直接「全选匹配结果」一次性加入;一个技能只会属于一个分类,取消勾选即回到「未分类」。'), - h( - 'div', - { style: { display: 'flex', gap: '8px', alignItems: 'center', flexWrap: 'wrap' } }, - h('input', { - type: 'search', - style: Object.assign({}, styles.input, { flex: '1 1 200px', minWidth: 0 }), - value: assignSearch, - autoFocus: true, - placeholder: '搜索技能:名称 / 目录 / 描述', - 'aria-label': '搜索要加入分类的技能', - onChange: function (event) { - setSelectionFilter(event.target.value) - }, - }), - assignSearch !== '' - ? h(Btn, { - label: '清空搜索', - variant: 'small', - onClick: function () { - setSelectionFilter('') - }, - }) - : null, - ), - h( - 'div', - { style: Object.assign({}, styles.actions, { flexWrap: 'wrap' }) }, - h(Btn, { - label: '全选匹配结果(' + String(filteredAssignable.length) + ')', - variant: 'small', - disabled: tree.busy !== '' || filteredAssignable.length === 0, - onClick: function () { - selectMany(filteredAssignable, true) - }, - }), - h(Btn, { - label: '全选全部(' + String(assignable.length) + ')', - variant: 'small', - disabled: tree.busy !== '' || assignable.length === 0 || selectedCount === assignable.length, - onClick: function () { - selectMany(assignable, true) - }, - }), - h(Btn, { - label: '取消勾选匹配项(' + String(matchedSelected) + ')', - variant: 'small', - disabled: tree.busy !== '' || matchedSelected === 0, - onClick: function () { - selectMany(filteredAssignable, false) - }, - }), - h(Btn, { - label: '清空已选', - variant: 'small', - disabled: tree.busy !== '' || selectedCount === 0, - onClick: function () { - setSelection([]) - }, - }), - ), - filteredAssignable.length === 0 - ? h('p', { style: styles.hint }, assignable.length === 0 ? '当前来源还没有可分配的技能。' : '没有匹配「' + assignSearch + '」的技能。换个关键词,或点「清空搜索」看全部。') - : h( - 'div', - { style: { display: 'flex', flexDirection: 'column', gap: '4px', maxHeight: '260px', overflow: 'auto' } }, - filteredAssignable.map(function (skill) { - var checked = tree.assign.selection.indexOf(skill.folder) >= 0 - return h( - 'label', - { key: String(skill.folder), style: styles.checkboxRow }, - h('input', { - type: 'checkbox', - checked: checked, - onChange: function (event) { - selectMany([skill], event.target.checked === true) - }, - }), - h( - 'span', - { style: { minWidth: 0 } }, - String(skill.name) + ' ', - h('span', { style: styles.hint }, '· ' + String(skill.folder) + (skill.description ? ' · ' + String(skill.description) : '')), - ), - ) - }), - ), - h( - 'div', - { style: styles.actions }, - h(Btn, { - variant: 'primary', - label: '保存分类(' + String(selectedCount) + ')', - disabled: tree.busy !== '', - onClick: function () { - saveAssign() - }, - }), - h(Btn, { - label: '取消', - disabled: tree.busy !== '', - onClick: function () { - patchTree({ assign: null }) - }, - }), - ), - ) - } - - var renderSkillRow = function (source, skill) { - var writable = source.writable === true - return h( - 'div', - { key: String(source.id) + ':' + String(skill.folder), style: styles.skillRow }, - h( - 'div', - { - style: { display: 'flex', alignItems: 'baseline', gap: '8px', minWidth: 0, flex: '1 1 auto', cursor: writable ? 'pointer' : 'default' }, - title: writable ? '查看详情' : undefined, - onClick: writable - ? function () { - openDetail(skill.folder) - } - : undefined, - }, - h('span', { style: Object.assign({}, styles.rowTitle, { flex: '0 0 auto' }) }, String(skill.name)), - skill.description === '' ? null : h('span', { style: Object.assign({}, styles.hint, styles.clamp1, { minWidth: 0 }) }, String(skill.description)), - ), - h( - 'div', - { style: { display: 'flex', alignItems: 'center', gap: '6px', flex: '0 0 auto' } }, - skill.enabledEditable !== true ? h('span', { style: styles.hint }, '只读') : null, - writable - ? h(IconBtn, { - glyph: '🗑', - danger: true, - ariaLabel: '卸载 ' + String(skill.name), - disabled: tree.busy !== '', - onClick: function () { - planUninstall(skill.folder) - }, - }) - : null, - skill.enabledEditable !== true - ? null - : h(Toggle, { - checked: skill.enabled === true, - disabled: tree.busy !== '', - label: '技能 ' + String(skill.name) + ' 启停', - onChange: function () { - toggleSkill(skill.folder, skill.enabled !== true) - }, - }), - ), - ) - } - - var renderGroup = function (source, group) { - var open = tree.openGroups[String(source.id) + ':' + group.id] === true - var renameKey = String(source.id) + ':' + group.id - var renaming = tree.edit !== null && tree.edit.kind === 'rename' && tree.edit.key === renameKey - var confirming = tree.confirm === renameKey - var assigning = tree.assign !== null && tree.assign.sourceId === source.id && tree.assign.groupId === group.id - var groupSkills = group.visible === undefined ? group.skills || [] : group.visible - return h( - 'div', - { key: renameKey, style: styles.groupRow }, - h( - 'div', - { style: styles.rowHead }, - h( - 'div', - { style: { display: 'flex', alignItems: 'center', gap: '6px', minWidth: 0, flexWrap: 'wrap' } }, - h(Btn, { - label: open ? '▾' : '▸', - variant: 'small', - ariaLabel: '展开或收起 ' + String(group.name), - onClick: function () { - patchTree({ openGroups: Object.assign({}, tree.openGroups, { [renameKey]: !open }) }) - }, - }), - h('span', { style: styles.folderGlyph }, '📁'), - renaming - ? h('input', { - style: Object.assign({}, styles.input, { width: '160px' }), - value: tree.edit.value, - autoFocus: true, - onChange: function (event) { - patchTree({ edit: Object.assign({}, tree.edit, { value: event.target.value }) }) - }, - }) - : h('span', { style: styles.rowTitle }, String(group.name)), - h('span', { style: styles.hint }, String(group.total) + ' 项 · ' + String(group.enabledCount) + '/' + String(group.total) + ' 启用'), - group.cardOnly === true ? h(Pill, { label: '开关', value: '仅本卡', tone: 'muted' }) : null, - ), - h( - 'div', - { style: styles.actions }, - renaming - ? h(Btn, { label: '保存', variant: 'small', disabled: tree.busy !== '', onClick: saveRename }) - : h(IconBtn, { - glyph: '+', - ariaLabel: '把技能加入 ' + String(group.name), - disabled: tree.busy !== '' || group.id === 'unassigned', - onClick: function () { - openAssign(source, group) - }, - }), - renaming - ? h(Btn, { label: '取消', variant: 'small', onClick: function () { patchTree({ edit: null }) } }) - : h(IconBtn, { - glyph: '✎', - ariaLabel: '重命名 ' + String(group.name), - disabled: tree.busy !== '' || group.id === 'unassigned', - onClick: function () { - patchTree({ edit: { kind: 'rename', key: renameKey, sourceId: source.id, groupId: group.id, value: String(group.name) } }) - }, - }), - confirming - ? h(Btn, { label: '确认删除分类', variant: 'danger', disabled: tree.busy !== '', onClick: function () { deleteGroup(source.id, group.id) } }) - : h(IconBtn, { - glyph: '🗑', - danger: true, - ariaLabel: '删除分类 ' + String(group.name) + '(技能回到未分类)', - disabled: tree.busy !== '' || group.id === 'unassigned', - onClick: function () { - patchTree({ confirm: renameKey }) - setNotice({ tone: 'warn', text: '删除分类只删除分组记录,里面的技能会回到「未分类」,文件不会被删除。再点一次确认。' }) - }, - }), - group.enabledEditable === false - ? null - : h(Toggle, { - checked: group.enabled !== false, - disabled: tree.busy !== '', - label: '分类 ' + String(group.name) + ' 启停', - onChange: function () { - toggleGroup(source.id, group.id, group.enabled === false) - }, - }), - ), - ), - assigning ? h('div', { style: styles.nested }, assignBox) : null, - open - ? h( - 'div', - { style: styles.nested }, - groupSkills.length === 0 - ? assigning - ? null - : h('p', { style: styles.hint }, group.id === 'unassigned' ? '没有未分类的技能。' : '这个分类还没有技能,点「+」加入。') - : groupSkills.map(function (skill) { - return renderSkillRow(source, skill) - }), - ) - : null, - ) - } - - var dshSource = treeSources.find(function (src) { - return src.writable === true - }) - - skillsTab = h( - 'div', - { style: styles.root, key: 'skills' }, - - h(ImportSection, { - onImported: function () { - loadTree() - loadSkills() - }, - }), - - h( - Section, - { - title: '技能文件夹', - hint: '按软件来源分组;展开来源与分类查看技能。', - }, - dshSource === undefined - ? null - : h( - 'div', - { style: styles.statsBar }, - h('span', { style: styles.stat }, h('span', { style: styles.statNum }, String(dshSource.total || 0)), h('span', { style: styles.hint }, '个技能')), - h('span', { style: styles.stat }, h('span', { style: styles.statNum }, String(dshSource.enabledCount || 0)), h('span', { style: styles.hint }, '个已启用')), - h('span', { style: styles.statLast }, h('span', { style: styles.statNum }, String((dshSource.total || 0) - (dshSource.enabledCount || 0))), h('span', { style: styles.hint }, '个已停用')), - ), - h( - 'div', - { style: { display: 'flex', gap: '8px', alignItems: 'flex-end', flexWrap: 'wrap' } }, - h( - 'div', - { style: { flex: '1 1 220px', minWidth: 0 } }, - h(Field, { - label: '搜索', - value: skills.search, - placeholder: '按名称、目录或描述筛选', - onChange: function (value) { - patchSkills({ search: value }) - }, - }), - ), - h(Btn, { - label: tree.status === 'loading' ? '刷新中…' : '刷新', - variant: 'small', - disabled: tree.status === 'loading', - onClick: function () { - loadTree() - loadSkills() - }, - }), - ), - tree.error === null ? null : h(Banner, { tone: 'bad' }, tree.error), - tree.status === 'loading' && tree.data === null ? h('p', { style: styles.hint }, '正在读取技能来源…') : null, - tree.status === 'ready' && treeSources.length === 0 ? h('p', { style: styles.hint }, '没有探测到任何技能来源。') : null, - dshSource === undefined - ? null - : h( - 'div', - { style: { display: 'flex', flexDirection: 'column', gap: '2px' } }, - visibleGroupsOf(dshSource).map(function (group) { - return renderGroup(dshSource, group) - }), - tree.edit !== null && tree.edit.kind === 'create' && tree.edit.sourceId === dshSource.id - ? h( - 'div', - { style: Object.assign({}, styles.row, { flexDirection: 'row', alignItems: 'center', flexWrap: 'wrap', marginTop: '6px' }) }, - h('input', { - style: Object.assign({}, styles.input, { width: '180px' }), - placeholder: '分类名称,例如 QQ-Skill', - value: tree.edit.value, - autoFocus: true, - onChange: function (event) { - patchTree({ edit: Object.assign({}, tree.edit, { value: event.target.value }) }) - }, - }), - h(Btn, { label: '创建分类', variant: 'primary', disabled: tree.busy !== '', onClick: createGroup }), - h(Btn, { label: '取消', onClick: function () { patchTree({ edit: null }) } }), - ) - : h( - 'div', - { style: { marginTop: '6px' } }, - h(Btn, { - label: '+ 新建分类', - variant: 'small', - disabled: tree.busy !== '', - onClick: function () { - patchTree({ edit: { kind: 'create', sourceId: dshSource.id, value: '' } }) - }, - }), - ), - ), - tree.data !== null && tree.data.note !== undefined ? h('p', { style: styles.hint }, String(tree.data.note)) : null, - ), - - detailBox, - uninstallBox, - - h( - Section, - { - title: '从 Git 仓库安装', - hint: '先克隆到隔离暂存目录并列出候选技能,确认后才写入 DSH 技能目录;不执行仓库里的任何脚本,符号链接不会跟随。仓库地址只接受 https:// 或 file://。', - }, - h( - 'div', - { style: styles.grid }, - h(Field, { - label: '仓库地址', - value: skills.installUrl, - placeholder: 'https://github.com//.git', - onChange: function (value) { - patchSkills({ installUrl: value }) - }, - }), - ), - h( - 'div', - { style: styles.actions }, - h(Btn, { - variant: 'primary', - label: skills.busy === 'plan' ? '拉取中…' : '拉取并预览', - disabled: skills.busy !== '', - onClick: prepareInstall, - }), - ), - planBox, - ), - - h(MarketSection, { - capabilities: caps, - onInstalled: function () { - loadTree() - loadSkills() - }, - }), - - h(SyncResolveSection, { - onResolved: function () { - loadSkills() - }, - }), - ) - } - - var mcpTab = null - if (tab === 'mcp') { - var items = Array.isArray(mcp.items) ? mcp.items : [] - var mcpSearch = String(mcp.search === undefined ? '' : mcp.search) - var mcpNeedle = mcpSearch.trim().toLowerCase() - var mcpFilter = String(mcp.filter === undefined ? 'all' : mcp.filter) - var mcpChosen = Array.isArray(mcp.selection) ? mcp.selection : [] - var mcpBulk = mcp.bulk === null || mcp.bulk === undefined ? null : mcp.bulk - var stateOf = function (item) { - return String(item.connectionState === undefined ? 'unknown' : item.connectionState) - } - var isProblem = function (item) { - var state = stateOf(item) - return state !== 'connected' && state !== 'disabled' - } - var haystackOf = function (item) { - var state = stateOf(item) - return [ - item.name, item.key, item.transport, item.authMode, item.injectionLabel, - MCP_STATE_LABELS[state] === undefined ? state : MCP_STATE_LABELS[state], - item.lastError, - item.scope === undefined || item.scope === null ? '' : item.scope.visibility, - ] - .map(function (value) { return String(value === undefined || value === null ? '' : value) }) - .join(' ') - .toLowerCase() - } - var matchesMcp = function (item) { - if (mcpFilter === 'connected' && stateOf(item) !== 'connected') return false - if (mcpFilter === 'disabled' && !(item.enabled === false || stateOf(item) === 'disabled')) return false - if (mcpFilter === 'problem' && !isProblem(item)) return false - if (mcpNeedle === '') return true - return haystackOf(item).indexOf(mcpNeedle) >= 0 - } - var visibleItems = items.filter(matchesMcp) - var mcpCounts = { - connected: items.filter(function (item) { return stateOf(item) === 'connected' }).length, - disabled: items.filter(function (item) { return item.enabled === false || stateOf(item) === 'disabled' }).length, - problem: items.filter(isProblem).length, - } - var matchedSelected = visibleItems.filter(function (item) { return mcpChosen.indexOf(item.key) >= 0 }).length - var bridgeBox = null - if (mcp.status === 'missing') { - bridgeBox = h( - Banner, - { tone: 'warn' }, - 'MCP 管理当前不可用:没有探测到宿主 dsh-mcp-connector 的公开接口(' + MCP_BRIDGE + ')。' + (mcp.error === null ? '' : '原因:' + String(mcp.error)) + '。自定义 MCP Server 的权威位置仍是「设置 → 插件配置 → MCP 服务器配置」,本卡不会修改它的托管配置。', - ) - } - - mcpTab = h( - 'div', - { style: styles.root, key: 'mcp' }, - h( - Section, - { - title: '我的连接', - hint: '数据来自宿主 dsh-mcp-connector 插件(同一 origin 的公开接口);本卡不保存任何 MCP 凭据,也不回显启动命令与密钥。', - aside: h(Btn, { label: mcp.status === 'probe' ? '探测中…' : '重新探测', variant: 'small', disabled: mcp.busy !== '', onClick: probeMcp }), - }, - bridgeBox, - mcp.status === 'ok' && items.length === 0 ? h('p', { style: styles.hint }, '宿主还没有登记任何 MCP 连接。') : null, - mcp.status === 'ok' && items.length > 0 - ? h( - 'div', - { style: { display: 'flex', flexDirection: 'column', gap: '8px' } }, - h( - 'div', - { style: styles.pills }, - h(Pill, { label: '共', value: String(items.length), tone: 'muted' }), - h(Pill, { label: '匹配', value: String(visibleItems.length), tone: visibleItems.length === 0 ? 'muted' : 'ok' }), - h(Pill, { label: '已选', value: String(mcpChosen.length), tone: mcpChosen.length === 0 ? 'muted' : 'ok' }), - h(Pill, { label: '已连接', value: String(mcpCounts.connected), tone: 'ok' }), - h(Pill, { label: '异常', value: String(mcpCounts.problem), tone: mcpCounts.problem === 0 ? 'muted' : 'warn' }), - ), - h( - 'div', - { style: { display: 'flex', gap: '8px', alignItems: 'center', flexWrap: 'wrap' } }, - h('input', { - type: 'search', - style: Object.assign({}, styles.input, { flex: '1 1 220px', minWidth: 0 }), - value: mcpSearch, - placeholder: '搜索连接:名称 / key / 传输 / 鉴权 / 状态', - 'aria-label': '搜索 MCP 连接', - onChange: function (event) { - patchMcp({ search: event.target.value }) - }, - }), - mcpSearch !== '' - ? h(Btn, { - label: '清空搜索', - variant: 'small', - onClick: function () { - patchMcp({ search: '' }) - }, - }) - : null, - ), - h( - 'div', - { style: Object.assign({}, styles.actions, { flexWrap: 'wrap' }) }, - h(Btn, { - label: '筛选:全部(' + String(items.length) + ')', - variant: mcpFilter === 'all' ? 'primary' : 'small', - onClick: function () { - patchMcp({ filter: 'all' }) - }, - }), - h(Btn, { - label: '已连接(' + String(mcpCounts.connected) + ')', - variant: mcpFilter === 'connected' ? 'primary' : 'small', - onClick: function () { - patchMcp({ filter: 'connected' }) - }, - }), - h(Btn, { - label: '已停用(' + String(mcpCounts.disabled) + ')', - variant: mcpFilter === 'disabled' ? 'primary' : 'small', - onClick: function () { - patchMcp({ filter: 'disabled' }) - }, - }), - h(Btn, { - label: '异常(' + String(mcpCounts.problem) + ')', - variant: mcpFilter === 'problem' ? 'primary' : 'small', - onClick: function () { - patchMcp({ filter: 'problem' }) - }, - }), - ), - h( - 'div', - { style: Object.assign({}, styles.actions, { flexWrap: 'wrap' }) }, - h(Btn, { - label: '全选匹配结果(' + String(visibleItems.length) + ')', - ariaLabel: '全选匹配结果', - variant: 'small', - disabled: visibleItems.length === 0, - onClick: function () { - selectMcpMany(visibleItems, true) - }, - }), - h(Btn, { - label: '全选全部(' + String(items.length) + ')', - ariaLabel: '全选全部', - variant: 'small', - disabled: items.length === 0 || mcpChosen.length === items.length, - onClick: function () { - selectMcpMany(items, true) - }, - }), - h(Btn, { - label: '取消勾选匹配项(' + String(matchedSelected) + ')', - ariaLabel: '取消勾选匹配项', - variant: 'small', - disabled: matchedSelected === 0, - onClick: function () { - selectMcpMany(visibleItems, false) - }, - }), - h(Btn, { - label: '清空已选', - ariaLabel: '清空已选', - variant: 'small', - disabled: mcpChosen.length === 0, - onClick: function () { - patchMcp({ selection: [], confirmBulk: false }) - }, - }), - ), - mcpChosen.length === 0 - ? h('p', { style: styles.hint }, '勾选左侧连接后,这里会出现批量健康检查、启停、重连与移除。') - : h( - 'div', - { style: Object.assign({}, styles.actions, { flexWrap: 'wrap' }) }, - h('span', { style: styles.hint }, '批量操作(已选 ' + String(mcpChosen.length) + '):'), - h(Btn, { - label: '健康检查', - ariaLabel: '批量健康检查', - variant: 'small', - disabled: mcp.busy !== '', - onClick: function () { - runMcpBulk('批量健康检查', 'healthCheck', function (item) { return { connectorId: item.connectorId } }) - }, - }), - h(Btn, { - label: '启用', - ariaLabel: '批量启用', - variant: 'small', - disabled: mcp.busy !== '', - onClick: function () { - runMcpBulk('批量启用', 'setEnabled', function (item) { return { key: item.key, enabled: true } }) - }, - }), - h(Btn, { - label: '停用', - ariaLabel: '批量停用', - variant: 'small', - disabled: mcp.busy !== '', - onClick: function () { - runMcpBulk('批量停用', 'setEnabled', function (item) { return { key: item.key, enabled: false } }) - }, - }), - h(Btn, { - label: '重连', - ariaLabel: '批量重连', - variant: 'small', - disabled: mcp.busy !== '', - onClick: function () { - runMcpBulk('批量重连', 'reconnect', function (item) { return { key: item.key } }) - }, - }), - mcp.confirmBulk === true - ? h(Btn, { - label: '确认移除(' + String(mcpChosen.length) + ')', - ariaLabel: '确认批量移除', - variant: 'danger', - disabled: mcp.busy !== '', - onClick: function () { - runMcpBulk('批量移除', 'disconnect', function (item) { return { key: item.key } }) - }, - }) - : h(Btn, { - label: '移除…', - ariaLabel: '批量移除', - variant: 'small', - disabled: mcp.busy !== '', - onClick: function () { - patchMcp({ confirmBulk: true }) - setNotice({ tone: 'warn', text: '批量移除会断开所选 ' + String(mcpChosen.length) + ' 条连接定义;再点一次「确认移除」才执行。' }) - }, - }), - ), - mcpBulk === null ? null : h('p', { style: styles.hint }, String(mcpBulk.label) + ' 进行中:' + String(mcpBulk.done) + ' / ' + String(mcpBulk.total)), - visibleItems.length === 0 ? h('p', { style: styles.hint }, '没有匹配「' + mcpSearch + '」的连接。换个关键词或切回「全部」筛选。') : null, - visibleItems.map(function (item) { - var state = stateOf(item) - var tools = mcp.tools[item.key] - var details = mcp.details === null || mcp.details === undefined ? {} : mcp.details - var open = details[item.key] === true - var label = String(item.name === undefined ? item.key : item.name) - return h( - 'div', - { key: String(item.key), style: styles.row }, - h( - 'div', - { style: styles.rowHead }, - h( - 'label', - { style: { display: 'inline-flex', alignItems: 'center', gap: '8px', minWidth: 0, flex: '1 1 auto' } }, - h('input', { - type: 'checkbox', - checked: mcpChosen.indexOf(item.key) >= 0, - 'aria-label': '选择连接 ' + label, - onChange: function (event) { - selectMcpMany([item], event.target.checked === true) - }, - }), - h('span', { style: styles.rowTitle }, label), - h(Pill, { label: '状态', value: MCP_STATE_LABELS[state] === undefined ? state : MCP_STATE_LABELS[state], tone: state === 'connected' ? 'ok' : state === 'disabled' ? 'muted' : 'warn' }), - h(Pill, { label: '传输', value: String(item.transport === undefined ? '—' : item.transport), tone: 'muted' }), - ), - h(Btn, { - label: open ? '收起' : '详情', - ariaLabel: (open ? '收起详情 ' : '详情 ') + label, - variant: 'small', - onClick: function () { - patchMcp({ details: Object.assign({}, details, { [String(item.key)]: !open }) }) - }, - }), - ), - open - ? h( - 'div', - { style: { display: 'flex', flexDirection: 'column', gap: '6px' } }, - h( - 'div', - { style: styles.pills }, - h(Pill, { label: '鉴权', value: String(item.authMode === undefined ? 'none' : item.authMode), tone: 'muted' }), - h(Pill, { label: '注入', value: String(item.injectionLabel === undefined ? '—' : item.injectionLabel), tone: 'muted' }), - h(Pill, { label: '范围', value: item.scope === undefined || item.scope === null ? '未知' : String(item.scope.visibility === undefined ? JSON.stringify(item.scope) : item.scope.visibility), tone: 'muted' }), - h(Pill, { label: '健康检查', value: formatIso(item.healthCheckedAt), tone: 'muted' }), - ), - item.lastError === null || item.lastError === undefined - ? null - : h('p', { style: styles.hint }, '最近错误:' + String(item.lastError)), - h( - 'div', - { style: styles.actions }, - h(Btn, { - label: '健康检查', - ariaLabel: '健康检查 ' + label, - variant: 'small', - disabled: mcp.busy !== '', - onClick: function () { - mcpAction('健康检查 ' + label, 'healthCheck', { connectorId: item.connectorId }).then(function () { - probeMcp() - }) - }, - }), - h(Btn, { - label: '工具列表', - ariaLabel: '工具列表 ' + label, - variant: 'small', - disabled: mcp.busy !== '', - onClick: function () { - loadTools(item) - }, - }), - h(Btn, { - label: item.enabled === false ? '启用' : '停用', - ariaLabel: '启停 ' + label, - variant: 'small', - disabled: mcp.busy !== '', - onClick: function () { - mcpAction(item.enabled === false ? '启用连接' : '停用连接', 'setEnabled', { key: item.key, enabled: item.enabled === false }).then(function () { - probeMcp() - }) - }, - }), - h(Btn, { - label: '重连', - ariaLabel: '重连 ' + label, - variant: 'small', - disabled: mcp.busy !== '', - onClick: function () { - mcpAction('重连', 'reconnect', { key: item.key }).then(function () { - probeMcp() - }) - }, - }), - mcp.confirm === item.key - ? h(Btn, { - label: '确认移除', - ariaLabel: '确认移除 ' + label, - variant: 'danger', - disabled: mcp.busy !== '', - onClick: function () { - mcpAction('移除连接', 'disconnect', { key: item.key }).then(function () { - patchMcp({ confirm: null }) - probeMcp() - }) - }, - }) - : h(Btn, { - label: '移除连接…', - ariaLabel: '移除连接 ' + label, - variant: 'small', - disabled: mcp.busy !== '', - onClick: function () { - patchMcp({ confirm: item.key }) - setNotice({ tone: 'warn', text: '再次点击「确认移除」才会断开该连接定义。' }) - }, - }), - ), - tools === undefined - ? null - : h('p', { style: styles.hint }, '工具缓存(' + formatTime(tools.at) + '):' + (tools.items.length === 0 ? '空' : tools.items.map(function (tool) { return String(tool.name === undefined ? tool : tool.name) }).join('、'))), - ) - : null, - ) - }), - h('p', { style: styles.hint }, '工具可见不等于业务可调用:连接状态与工具缓存分开显示。'), - ) - : null, - ), - - h(McpCustomSection, { onChanged: probeMcp }), - h(McpConfigSection, { items: items, onChanged: probeMcp }), - ) - } - - return h( - 'div', - { style: styles.root }, - header, - tabBar, - noticeBox, - error === null ? null : h(Banner, { tone: 'bad' }, error), - tab === 'connection' ? connectionTab : tab === 'skills' ? skillsTab : mcpTab, - ) - } - - // A thrown render error must not take the settings page down with it, so the - // card is wrapped in a minimal boundary that renders the message instead. - function CardBoundary(props) { - React.Component.call(this, props) - this.state = { failure: null } - } - - CardBoundary.prototype = Object.create(React.Component.prototype) - CardBoundary.prototype.constructor = CardBoundary - - CardBoundary.getDerivedStateFromError = function (failure) { - return { failure: failure } - } - - CardBoundary.prototype.render = function () { - if (this.state.failure !== null) { - return h( - 'div', - { style: Object.assign({}, styles.root) }, - h('div', { style: Object.assign({}, styles.banner, styles.bad) }, '云端记忆设置卡渲染失败:' + messageOf(this.state.failure)), - h('p', { style: styles.hint }, '插件本身仍在运行,可改用 /dsh-brain 页面配置。'), - ) - } - return this.props.children - } - - function apply(ctx) { - var card = function () { - return h(CardBoundary, null, h(SettingsCard, null)) - } - - var register = function (seat, options) { - try { - ctx.slots.inject(seat, function () { - return ctx.slots.register(options, card) - }) - } catch (failure) { - if (typeof console !== 'undefined') console.warn('[dsh-brain] slot ' + seat + ' unavailable: ' + messageOf(failure)) - } - } - - register('settings.section', { - name: 'settings.section', - id: 'dsh-brain', - order: 100, - label: function () { - return '云端记忆' - }, - }) - - register('plugins.bundle.config', { name: 'plugins.bundle.config', key: 'dsh-brain' }) - } - - module.exports = { apply: apply, inject: inject, name: 'dsh-brain' } - return module.exports - }, -}) + id: "dsh-brain", + factory: (require) => { + var module = { exports: {} }; + var exports = module.exports; + Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); + // \0rolldown/runtime.js + var __create = Object.create; + var __defProp = Object.defineProperty; + var __getOwnPropDesc = Object.getOwnPropertyDescriptor; + var __getOwnPropNames = Object.getOwnPropertyNames; + var __getProtoOf = Object.getPrototypeOf; + var __hasOwnProp = Object.prototype.hasOwnProperty; + var __copyProps = (to, from, except, desc) => { + if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) { + key = keys[i]; + if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { + get: ((k) => from[k]).bind(null, key), + enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable + }); + } + return to; + }; + var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", { + value: mod, + enumerable: true + }) : target, mod)); + let react = require("react"); + react = __toESM(react, 1); + // src-client/api.ts + const API = { + status: "/dsh-brain/status", + capabilities: "/dsh-brain/capabilities", + config: "/dsh-brain/config", + login: "/dsh-brain/login", + test: "/dsh-brain/test", + flush: "/dsh-brain/flush", + skills: "/dsh-brain/skills", + market: "/dsh-brain/market", + sync: "/dsh-brain/sync", + clients: "/dsh-brain/clients", + migrate: "/dsh-brain/migrate" + }; + function request(url, init) { + var options = init || {}; + options.cache = "no-store"; + options.headers = options.body === void 0 ? { accept: "application/json" } : { + accept: "application/json", + "content-type": "application/json" + }; + return fetch(url, options).then(function(response) { + return response.json().then(function(body) { + return { + status: response.status, + body + }; + }, function() { + return { + status: response.status, + body: null + }; + }); + }); + } + function postJson(url, payload) { + return request(url, { + method: "POST", + body: JSON.stringify(payload === void 0 ? {} : payload) + }); + } + function messageOf(error) { + if (error === null || error === void 0) return "未知错误"; + if (typeof error === "string") return error; + return typeof error.message === "string" ? error.message : String(error); + } + function statusText(status) { + if (status === 404 || status === 405) return "插件后端还没加载这个接口(HTTP " + String(status) + "):请完全退出并重启 DSH 后再试。"; + return "HTTP " + String(status); + } + function bodyError(result, fallback) { + if (result.body !== null && result.body !== void 0 && typeof result.body.error === "string" && result.body.error !== "") return result.body.error; + return fallback === void 0 ? statusText(result.status) : fallback; + } + function pickSettings(settings) { + var source = settings === void 0 || settings === null ? {} : settings; + return { + enabled: source.enabled === true, + baseUrl: String(source.baseUrl === void 0 ? "" : source.baseUrl), + baseUrlConfigured: source.baseUrlConfigured !== false, + baseUrlOrigin: String(source.baseUrlOrigin === void 0 ? "" : source.baseUrlOrigin), + clientId: String(source.clientId === void 0 ? "" : source.clientId), + requestTimeoutMs: String(source.requestTimeoutMs === void 0 ? 8e3 : source.requestTimeoutMs), + defaultK: String(source.defaultK === void 0 ? 10 : source.defaultK), + summaryEnabled: source.summaryEnabled === true, + activityEnabled: source.activityEnabled === true, + memoryEnabled: source.memoryEnabled === true, + skillsDir: String(source.skillsDir === void 0 ? "" : source.skillsDir), + skillsSyncEnabled: source.skillsSyncEnabled === true + }; + } + function emptyTokens() { + return { + activity: "", + memory: "", + summary: "" + }; + } + // src-client/styles.ts + const styles = { + root: { + display: "flex", + flexDirection: "column", + gap: "18px", + padding: "4px 2px 20px", + fontSize: "13px", + lineHeight: 1.55, + minWidth: 0 + }, + tabs: { + display: "flex", + gap: "4px", + padding: "3px", + borderRadius: "13px", + border: "1px solid rgba(127,127,127,0.22)", + background: "rgba(127,127,127,0.06)", + flexWrap: "wrap" + }, + tab: { + flex: "1 1 auto", + minWidth: "88px", + font: "inherit", + fontSize: "12.5px", + padding: "7px 12px", + borderRadius: "10px", + border: "1px solid transparent", + background: "transparent", + color: "inherit", + cursor: "pointer", + whiteSpace: "nowrap" + }, + tabActive: { + border: "1px solid rgba(127,127,127,0.30)", + background: "rgba(127,127,127,0.14)", + boxShadow: "0 1px 3px rgba(0,0,0,0.10)", + fontWeight: 600 + }, + section: { + display: "flex", + flexDirection: "column", + gap: "12px", + border: "1px solid rgba(127,127,127,0.22)", + borderRadius: "16px", + padding: "18px 20px", + minWidth: 0 + }, + sectionHead: { + display: "flex", + alignItems: "center", + justifyContent: "space-between", + gap: "10px", + flexWrap: "wrap" + }, + sectionTitle: { + margin: 0, + fontSize: "13.5px", + fontWeight: 650, + letterSpacing: "0.02em" + }, + hint: { + margin: 0, + fontSize: "11.5px", + opacity: .58 + }, + grid: { + display: "flex", + flexWrap: "wrap", + gap: "12px 18px" + }, + field: { + display: "flex", + flexDirection: "column", + gap: "4px", + flex: "1 1 auto", + minWidth: "220px" + }, + label: { + fontSize: "12px", + opacity: .75 + }, + input: { + font: "inherit", + fontSize: "13px", + padding: "8px 11px", + borderRadius: "10px", + border: "1px solid rgba(127,127,127,0.35)", + background: "rgba(127,127,127,0.05)", + color: "inherit", + minWidth: 0, + width: "100%", + boxSizing: "border-box" + }, + checkboxRow: { + display: "flex", + alignItems: "flex-start", + gap: "8px" + }, + actions: { + display: "flex", + gap: "10px", + flexWrap: "wrap", + alignItems: "center" + }, + button: { + font: "inherit", + fontSize: "13px", + padding: "7px 16px", + borderRadius: "10px", + border: "1px solid rgba(127,127,127,0.4)", + background: "transparent", + color: "inherit", + cursor: "pointer" + }, + buttonPrimary: { + font: "inherit", + fontSize: "13px", + padding: "7px 18px", + borderRadius: "10px", + border: "1px solid transparent", + background: "#3f3f46", + boxShadow: "0 2px 6px -2px rgba(0,0,0,0.35)", + color: "#ffffff", + cursor: "pointer", + fontWeight: 600 + }, + buttonDanger: { + font: "inherit", + fontSize: "13px", + padding: "7px 16px", + borderRadius: "10px", + border: "1px solid rgba(220,38,38,0.5)", + background: "rgba(220,38,38,0.14)", + color: "inherit", + cursor: "pointer" + }, + buttonSmall: { + font: "inherit", + fontSize: "12px", + padding: "3px 8px", + borderRadius: "8px", + border: "1px solid transparent", + background: "transparent", + opacity: .85, + color: "inherit", + cursor: "pointer" + }, + pills: { + display: "flex", + flexWrap: "wrap", + gap: "8px" + }, + pill: { + display: "inline-flex", + alignItems: "center", + gap: "6px", + padding: "3px 10px", + borderRadius: "999px", + border: "1px solid rgba(127,127,127,0.35)", + fontSize: "12px", + maxWidth: "100%", + whiteSpace: "nowrap", + flex: "0 0 auto" + }, + table: { + width: "100%", + borderCollapse: "collapse", + fontSize: "12px", + tableLayout: "fixed" + }, + th: { + textAlign: "left", + fontWeight: 600, + padding: "4px 8px 4px 0", + borderBottom: "1px solid rgba(127,127,127,0.28)", + whiteSpace: "nowrap" + }, + td: { + padding: "4px 8px 4px 0", + borderBottom: "1px solid rgba(127,127,127,0.14)", + verticalAlign: "top", + overflowWrap: "anywhere" + }, + mono: { + fontFamily: "ui-monospace, SFMono-Regular, Menlo, Consolas, monospace", + fontSize: "11px", + opacity: .72, + wordBreak: "break-all" + }, + ok: { + borderColor: "rgba(22,163,74,0.55)", + background: "rgba(22,163,74,0.12)" + }, + bad: { + borderColor: "rgba(220,38,38,0.55)", + background: "rgba(220,38,38,0.12)" + }, + warn: { + borderColor: "rgba(217,119,6,0.55)", + background: "rgba(217,119,6,0.12)" + }, + muted: {}, + iconBtn: { + width: "28px", + height: "28px", + display: "inline-flex", + alignItems: "center", + justifyContent: "center", + fontSize: "13px", + lineHeight: 1, + padding: 0, + borderRadius: "8px", + border: "1px solid rgba(127,127,127,0.25)", + background: "rgba(127,127,127,0.05)", + color: "inherit", + cursor: "pointer", + flex: "0 0 auto" + }, + iconBtnDanger: { + border: "1px solid rgba(220,38,38,0.40)", + background: "rgba(220,38,38,0.08)" + }, + statsBar: { + display: "flex", + alignItems: "center", + flexWrap: "wrap" + }, + stat: { + display: "inline-flex", + alignItems: "baseline", + gap: "6px", + paddingRight: "18px", + marginRight: "18px", + borderRight: "1px solid rgba(127,127,127,0.20)" + }, + statLast: { + display: "inline-flex", + alignItems: "baseline", + gap: "6px" + }, + statNum: { + fontSize: "17px", + fontWeight: 700 + }, + banner: { + padding: "10px 13px", + borderRadius: "11px", + border: "1px solid transparent", + fontSize: "12.5px" + }, + row: { + display: "flex", + flexDirection: "column", + gap: "6px", + border: "1px solid rgba(127,127,127,0.15)", + borderRadius: "11px", + padding: "10px 12px", + minWidth: 0 + }, + rowHead: { + display: "flex", + alignItems: "baseline", + justifyContent: "space-between", + gap: "8px", + flexWrap: "wrap" + }, + rowTitle: { + fontWeight: 600, + overflowWrap: "anywhere" + }, + code: { + fontFamily: "ui-monospace, SFMono-Regular, Menlo, Consolas, monospace", + fontSize: "11.5px", + whiteSpace: "pre-wrap", + wordBreak: "break-word", + maxHeight: "260px", + overflow: "auto", + margin: 0, + padding: "10px", + borderRadius: "8px", + background: "rgba(127,127,127,0.1)" + }, + chips: { + display: "flex", + gap: "6px", + flexWrap: "wrap" + }, + chip: { + font: "inherit", + fontSize: "12px", + padding: "2px 10px", + borderRadius: "999px", + border: "1px solid rgba(127,127,127,0.35)", + background: "transparent", + color: "inherit", + cursor: "pointer" + }, + chipActive: { + border: "1px solid rgba(99,102,241,0.6)", + background: "rgba(99,102,241,0.16)" + }, + nested: { + display: "flex", + flexDirection: "column", + gap: "6px", + paddingLeft: "10px", + borderLeft: "2px solid rgba(127,127,127,0.18)", + minWidth: 0 + }, + sourceCard: { + display: "flex", + flexDirection: "column", + gap: "6px", + border: "1px solid rgba(127,127,127,0.17)", + borderRadius: "14px", + padding: "12px 14px", + minWidth: 0 + }, + groupRow: { + display: "flex", + flexDirection: "column", + gap: "6px", + border: "1px solid transparent", + borderRadius: "11px", + padding: "7px 9px", + background: "transparent", + minWidth: 0 + }, + skillRow: { + display: "flex", + alignItems: "center", + justifyContent: "space-between", + gap: "8px", + padding: "5px 8px", + borderRadius: "8px", + border: "1px solid transparent", + minWidth: 0, + flexWrap: "nowrap" + }, + clamp1: { + display: "-webkit-box", + WebkitLineClamp: "1", + WebkitBoxOrient: "vertical", + overflow: "hidden" + }, + clamp2: { + display: "-webkit-box", + WebkitLineClamp: "2", + WebkitBoxOrient: "vertical", + overflow: "hidden" + }, + sourceGlyph: { + width: "20px", + height: "20px", + borderRadius: "6px", + display: "inline-flex", + alignItems: "center", + justifyContent: "center", + fontSize: "11px", + fontWeight: 700, + border: "1px solid rgba(127,127,127,0.22)", + background: "rgba(127,127,127,0.10)", + flex: "0 0 auto" + }, + folderGlyph: { + fontSize: "12px", + opacity: .85, + flex: "0 0 auto" + }, + sourceTitle: { + fontWeight: 600, + fontSize: "13px", + overflowWrap: "anywhere" + } + }; + // src-client/components.tsx + const h$7 = react.createElement; + function Pill(props) { + var tone = styles[props.tone] === void 0 ? styles.muted : styles[props.tone]; + return h$7("span", { style: Object.assign({}, styles.pill, tone) }, h$7("span", { style: { opacity: .75 } }, props.label), h$7("span", { style: { fontWeight: 600 } }, String(props.value))); + } + function Banner(props) { + var tone = styles[props.tone] === void 0 ? styles.muted : styles[props.tone]; + return h$7("div", { + style: Object.assign({}, styles.banner, tone), + role: props.role + }, props.children); + } + function Field(props) { + return h$7("label", { style: styles.field }, h$7("span", { style: styles.label }, props.label), h$7("input", { + style: styles.input, + type: props.type === void 0 ? "text" : props.type, + value: props.value, + placeholder: props.placeholder === void 0 ? "" : props.placeholder, + spellCheck: false, + autoComplete: "off", + disabled: props.disabled === true, + onChange: function(event) { + props.onChange(event.target.value); + } + }), props.hint === void 0 ? null : h$7("span", { style: styles.hint }, props.hint)); + } + function Btn(props) { + var base = props.variant === "primary" ? styles.buttonPrimary : props.variant === "danger" ? styles.buttonDanger : props.variant === "small" ? styles.buttonSmall : styles.button; + var style = props.disabled === true ? Object.assign({}, base, { + opacity: .45, + cursor: "not-allowed" + }) : base; + return h$7("button", { + type: "button", + style, + disabled: props.disabled === true, + onClick: props.onClick, + "aria-label": props.ariaLabel + }, props.label); + } + function IconBtn(props) { + var style = Object.assign({}, styles.iconBtn, props.danger === true ? styles.iconBtnDanger : null, props.disabled === true ? { + opacity: .45, + cursor: "not-allowed" + } : null); + return h$7("button", { + type: "button", + style, + disabled: props.disabled === true, + onClick: props.disabled === true ? void 0 : props.onClick, + "aria-label": props.ariaLabel, + title: props.ariaLabel + }, props.glyph); + } + function Switch(props) { + return h$7("div", { + style: styles.tabs, + role: "tablist", + "aria-label": props.ariaLabel + }, props.options.map(function(option) { + var active = option.value === props.value; + return h$7("button", { + key: option.value, + type: "button", + role: "tab", + "aria-selected": active, + style: Object.assign({}, styles.tab, active ? styles.tabActive : null), + onClick: function() { + props.onChange(option.value); + } + }, option.label); + })); + } + function Toggle(props) { + var on = props.checked === true; + var disabled = props.disabled === true; + return h$7("button", { + type: "button", + role: "switch", + "aria-checked": on, + "aria-label": props.label, + title: props.label, + disabled, + style: { + width: "38px", + height: "22px", + flex: "0 0 auto", + padding: "2px", + borderRadius: "999px", + border: "1px solid " + (on ? "rgba(0,0,0,0.25)" : "rgba(127,127,127,0.45)"), + background: on ? "#3f3f46" : "rgba(127,127,127,0.28)", + display: "flex", + alignItems: "center", + justifyContent: on ? "flex-end" : "flex-start", + cursor: disabled ? "not-allowed" : "pointer", + opacity: disabled ? .45 : 1, + transition: "background 120ms ease" + }, + onClick: disabled ? void 0 : props.onChange + }, h$7("span", { style: { + width: "16px", + height: "16px", + borderRadius: "999px", + background: "rgba(255,255,255,0.96)", + boxShadow: "0 1px 2px rgba(0,0,0,0.25)", + flex: "0 0 auto" + } })); + } + function Section(props) { + return h$7("div", { style: styles.section }, h$7("div", { style: styles.sectionHead }, h$7("h4", { style: styles.sectionTitle }, props.title), props.aside === void 0 ? null : h$7("div", { style: styles.actions }, props.aside)), props.hint === void 0 ? null : h$7("p", { style: styles.hint }, props.hint), props.children); + } + function CardBoundary(props) { + react.Component.call(this, props); + this.state = { failure: null }; + } + CardBoundary.prototype = Object.create(react.Component.prototype); + CardBoundary.prototype.constructor = CardBoundary; + CardBoundary.getDerivedStateFromError = function(failure) { + return { failure }; + }; + CardBoundary.prototype.render = function() { + if (this.state.failure !== null) return h$7("div", { style: Object.assign({}, styles.root) }, h$7("div", { style: Object.assign({}, styles.banner, styles.bad) }, "云端记忆设置卡渲染失败:" + messageOf(this.state.failure)), h$7("p", { style: styles.hint }, "插件本身仍在运行,可改用 /dsh-brain 页面配置。")); + return this.props.children; + }; + // src-client/format.ts + function formatTime(value) { + if (typeof value !== "number" || value <= 0) return "—"; + var date = new Date(value); + var pad = function(n) { + return n < 10 ? "0" + String(n) : String(n); + }; + return pad(date.getMonth() + 1) + "-" + pad(date.getDate()) + " " + pad(date.getHours()) + ":" + pad(date.getMinutes()) + ":" + pad(date.getSeconds()); + } + function formatIso(value) { + if (typeof value !== "string" || value === "") return "—"; + var stamp = Date.parse(value); + if (!Number.isFinite(stamp)) return value; + return formatTime(stamp); + } + function formatBytes(value) { + if (typeof value !== "number" || value < 0) return "—"; + if (value < 1024) return String(value) + " B"; + if (value < 1048576) return (value / 1024).toFixed(1) + " KB"; + return (value / 1048576).toFixed(2) + " MB"; + } + function readStored(key, fallback) { + try { + var raw = window.localStorage.getItem("dsh-brain:" + key); + if (raw === null || raw === "") raw = window.localStorage.getItem("dsh-cloud-memory:" + key); + return raw === null || raw === "" ? fallback : raw; + } catch { + return fallback; + } + } + function store(key, value) { + try { + window.localStorage.setItem("dsh-brain:" + key, String(value)); + } catch {} + } + const OUTCOME_LABELS = { + recorded: "已上报", + duplicate: "重复忽略", + failed: "失败", + queued: "待重试", + skipped: "已跳过" + }; + const OUTCOME_TONES = { + recorded: "ok", + duplicate: "muted", + failed: "bad", + queued: "warn", + skipped: "muted" + }; + const CHECK_TONES = { + ok: "ok", + unconfigured: "muted", + invalid: "bad", + forbidden: "bad", + unreachable: "bad", + error: "bad" + }; + const CHECK_LABELS = { + ok: "可用", + unconfigured: "未配置", + invalid: "令牌失效", + forbidden: "权限不足", + unreachable: "不可达", + error: "出错" + }; + const MCP_STATE_LABELS = { + connected: "已连接", + connecting: "连接中", + disabled: "已停用", + error: "异常", + expired: "授权过期", + unknown: "未知", + needs_auth: "待授权" + }; + // src-client/bridge.ts + const MCP_BRIDGE = "/mcp-connector/api"; + const POLL_MS = 1e4; + const PROBE_TIMEOUT_MS = 5e3; + function bridgeCall(method, params) { + var controller = typeof AbortController === "function" ? new AbortController() : null; + var timer = controller === null ? null : setTimeout(function() { + controller.abort(); + }, 2e4); + return fetch(MCP_BRIDGE, { + method: "POST", + cache: "no-store", + headers: { + accept: "application/json", + "content-type": "application/json" + }, + body: JSON.stringify({ + method, + params: params === void 0 ? {} : params + }), + signal: controller === null ? void 0 : controller.signal + }).then(function(response) { + if (timer !== null) clearTimeout(timer); + return response.json().then(function(body) { + return { + status: response.status, + body + }; + }, function() { + return { + status: response.status, + body: null + }; + }); + }, function(failure) { + if (timer !== null) clearTimeout(timer); + return { + status: 0, + body: null, + error: messageOf(failure) + }; + }); + } + function bridgeMessage(result) { + if (result.body !== null && typeof result.body.message === "string" && result.body.message !== "") return result.body.message; + return "HTTP " + String(result.status) + (result.error === void 0 ? "" : ":" + result.error); + } + // src-client/sections/MarketSection.tsx + const h$6 = react.createElement; + function MarketSection(props) { + var capabilities = props.capabilities || {}; + var tuple = react.useState({ + providers: null, + provider: "", + query: "", + entries: [], + nextCursor: null, + cursor: 0, + status: "idle", + error: null, + detail: null, + plan: null, + selected: {}, + busy: "", + notice: null + }); + var state = tuple[0]; + var setState = tuple[1]; + function patch(next) { + setState(function(prev) { + return Object.assign({}, prev, next); + }); + } + var available = capabilities.features !== void 0 && capabilities.features.skillsMarketplace === true; + react.useEffect(function() { + if (available) loadProviders(); + }, []); + function loadProviders() { + request(API.market + "/providers", { method: "GET" }).then(function(res) { + if (res.body !== null && res.body !== void 0 && res.body.ok === true) { + var list = Array.isArray(res.body.providers) ? res.body.providers : []; + patch({ + providers: list, + provider: state.provider !== "" ? state.provider : list.length > 0 ? list[0].id : "" + }); + } else patch({ error: bodyError(res, "读取市场来源失败") }); + }, function(error) { + patch({ error: messageOf(error) }); + }); + } + function search(append) { + if (state.provider === "") return; + var cursor = append === true ? state.cursor : 0; + patch({ + busy: "search", + error: null, + status: "loading" + }); + request(API.market + "/search?provider=" + encodeURIComponent(state.provider) + "&q=" + encodeURIComponent(state.query) + "&cursor=" + String(cursor), { method: "GET" }).then(function(res) { + if (res.body !== null && res.body !== void 0 && res.body.ok === true) patch({ + entries: append === true ? state.entries.concat(res.body.entries || []) : res.body.entries || [], + nextCursor: res.body.nextCursor === void 0 ? null : res.body.nextCursor, + cursor: res.body.nextCursor === void 0 ? cursor : res.body.nextCursor, + status: "ready", + busy: "", + detail: null, + plan: null + }); + else patch({ + status: "error", + busy: "", + error: bodyError(res, "市场搜索失败") + }); + }, function(error) { + patch({ + status: "error", + busy: "", + error: messageOf(error) + }); + }); + } + function openDetail(entry) { + patch({ + busy: "detail", + detail: null, + plan: null, + notice: null + }); + postJson(API.market + "/detail", { + provider: state.provider, + id: entry.id + }).then(function(res) { + if (res.body !== null && res.body !== void 0 && res.body.ok === true) patch({ + detail: res.body, + busy: "" + }); + else patch({ + busy: "", + error: bodyError(res, "读取条目详情失败") + }); + }, function(error) { + patch({ + busy: "", + error: messageOf(error) + }); + }); + } + function makePlan() { + if (state.detail === null) return; + patch({ + busy: "plan", + plan: null, + notice: null + }); + postJson(API.market + "/plan", { + provider: state.provider, + id: state.detail.id + }).then(function(res) { + if (res.body !== null && res.body !== void 0 && res.body.ok === true) { + var selected = {}; + (res.body.candidates || []).forEach(function(candidate) { + if (candidate.conflict !== true && candidate.problem === void 0) selected[candidate.folder] = true; + }); + patch({ + plan: res.body, + selected, + busy: "" + }); + } else patch({ + busy: "", + error: bodyError(res, "无法生成安装计划") + }); + }, function(error) { + patch({ + busy: "", + error: messageOf(error) + }); + }); + } + function toggleFolder(folder) { + var next = Object.assign({}, state.selected); + next[folder] = next[folder] !== true; + patch({ selected: next }); + } + function commitPlan() { + if (state.plan === null) return; + var folders = Object.keys(state.selected).filter(function(folder) { + return state.selected[folder] === true; + }); + if (folders.length === 0) { + patch({ notice: { + tone: "warn", + text: "请至少选中一个技能。" + } }); + return; + } + patch({ busy: "commit" }); + postJson(API.market + "/commit", { + planId: state.plan.planId, + folders + }).then(function(res) { + if (res.body !== null && res.body !== void 0 && res.body.ok === true) { + var skipped = Array.isArray(res.body.skipped) ? res.body.skipped.length : 0; + patch({ + busy: "", + plan: null, + detail: null, + notice: { + tone: "ok", + text: "已安装:" + ((res.body.installed || []).join("、") || "无") + (skipped > 0 ? ";跳过 " + skipped + " 项" : "") + } + }); + if (typeof props.onInstalled === "function") props.onInstalled(); + } else patch({ + busy: "", + notice: { + tone: "bad", + text: bodyError(res, "安装失败") + } + }); + }, function(error) { + patch({ + busy: "", + notice: { + tone: "bad", + text: messageOf(error) + } + }); + }); + } + if (!available) return h$6(Section, { + title: "技能市场", + hint: "本版没有配置经验证的市场来源。" + }, h$6("p", { style: styles.hint }, "能力缺口:capabilities.features.skillsMarketplace = false,因此不显示任何搜索框或示例条目。")); + var providerLabel = state.provider; + if (state.providers !== null) state.providers.forEach(function(provider) { + if (provider.id === state.provider) providerLabel = provider.label; + }); + var resultRows = state.entries.map(function(entry) { + return h$6("div", { + key: entry.id, + style: styles.row + }, h$6("div", { style: styles.rowHead }, h$6("span", { style: styles.rowTitle }, entry.name), h$6("div", { style: styles.pills }, entry.version === void 0 ? null : h$6(Pill, { + label: "版本", + value: String(entry.version), + tone: "muted" + }), h$6(Pill, { + label: "技能", + value: (entry.skills || []).length, + tone: "muted" + }), entry.author === void 0 ? null : h$6(Pill, { + label: "作者", + value: String(entry.author), + tone: "muted" + }))), h$6("p", { style: styles.hint }, entry.description === "" ? "(无描述)" : entry.description), h$6("div", { style: styles.actions }, h$6(Btn, { + label: "查看详情", + variant: "small", + disabled: state.busy !== "", + onClick: function() { + openDetail(entry); + } + }))); + }); + var detailBox = null; + if (state.detail !== null) { + var details = Array.isArray(state.detail.skillDetails) ? state.detail.skillDetails : []; + detailBox = h$6("div", { style: styles.nested }, h$6("p", { style: styles.hint }, "来源:" + String(state.detail.sourceRepo) + " · " + (state.detail.description || "")), h$6("div", { style: { + display: "flex", + flexDirection: "column", + gap: "4px" + } }, details.map(function(skill) { + return h$6("div", { + key: skill.folder, + style: styles.rowHead + }, h$6("span", { style: styles.rowTitle }, skill.name === void 0 ? skill.folder : skill.name), h$6("span", { style: styles.hint }, skill.ok === false ? "(无法读取 SKILL.md)" : String(skill.description || ""))); + })), h$6("div", { style: styles.actions }, h$6(Btn, { + variant: "primary", + label: state.busy === "plan" ? "拉取中…" : "拉取安装计划", + disabled: state.busy !== "", + onClick: makePlan + }))); + } + var planBox = null; + if (state.plan !== null) { + var candidates = Array.isArray(state.plan.candidates) ? state.plan.candidates : []; + planBox = h$6("div", { style: styles.nested }, h$6("p", { style: styles.hint }, "已固定提交:" + (state.plan.commit === void 0 ? "克隆时记录实际提交" : String(state.plan.commit).slice(0, 12)) + ";只安装勾选项,冲突项默认不勾,不执行仓库脚本。"), candidates.length === 0 ? h$6("p", { style: styles.hint }, "该条目没有可安装的技能。") : null, h$6("div", { style: { + display: "flex", + flexDirection: "column", + gap: "6px" + } }, candidates.map(function(candidate) { + return h$6("label", { + key: candidate.folder, + style: styles.checkboxRow + }, h$6("input", { + type: "checkbox", + checked: state.selected[candidate.folder] === true, + disabled: candidate.conflict === true || candidate.problem !== void 0, + onChange: function() { + toggleFolder(candidate.folder); + } + }), h$6("span", null, candidate.folder + " · " + String(candidate.files) + " 个文件 · " + formatBytes(candidate.bytes) + (candidate.conflict === true ? " · 已存在,跳过" : "") + (candidate.problem === void 0 ? "" : " · " + candidate.problem))); + })), h$6("div", { style: styles.actions }, h$6(Btn, { + variant: "primary", + label: state.busy === "commit" ? "安装中…" : "安装选中", + disabled: state.busy !== "", + onClick: commitPlan + }))); + } + return h$6(Section, { + title: "技能市场", + hint: "来自经验证的公开市场清单;安装走暂存计划、只装勾选项并固定提交。" + }, h$6("div", { style: { + display: "flex", + flexDirection: "column", + gap: "10px" + } }, h$6("div", { style: styles.pills }, h$6(Pill, { + label: "来源", + value: String(providerLabel || "未配置"), + tone: "muted" + }), state.providers === null ? null : h$6(Pill, { + label: "已接入", + value: state.providers.length, + tone: "ok" + })), h$6("div", { style: { + display: "flex", + gap: "8px", + alignItems: "flex-end", + flexWrap: "wrap" + } }, h$6("div", { style: { + flex: "1 1 220px", + minWidth: 0 + } }, h$6(Field, { + label: "搜索市场", + value: state.query, + placeholder: "例如 xlsx / pdf / design", + onChange: function(value) { + patch({ query: value }); + } + })), h$6(Btn, { + variant: "primary", + label: state.busy === "search" ? "搜索中…" : "搜索", + disabled: state.busy !== "", + onClick: function() { + search(false); + } + })), state.error === null ? null : h$6(Banner, { tone: "bad" }, state.error), state.notice === null ? null : h$6(Banner, { tone: state.notice.tone }, state.notice.text), state.status === "ready" && state.entries.length === 0 ? h$6("p", { style: styles.hint }, "没有匹配的市场条目。") : null, h$6("div", { style: { + display: "flex", + flexDirection: "column", + gap: "8px" + } }, resultRows), state.nextCursor === null ? null : h$6("div", { style: styles.actions }, h$6(Btn, { + label: "加载更多", + variant: "small", + disabled: state.busy !== "", + onClick: function() { + search(true); + } + })), detailBox, planBox)); + } + // src-client/sections/SyncResolveSection.tsx + const h$5 = react.createElement; + function SyncResolveSection(props) { + var tuple = react.useState({ + status: "idle", + data: null, + busy: "", + error: null, + notice: null, + pendingAll: null + }); + var state = tuple[0]; + var setState = tuple[1]; + function patch(next) { + setState(function(prev) { + return Object.assign({}, prev, next); + }); + } + react.useEffect(function() { + load(); + }, []); + function load() { + patch({ + status: "loading", + error: null + }); + request(API.sync + "/status", { method: "GET" }).then(function(res) { + if (res.body !== null && res.body !== void 0 && res.body.ok === true) patch({ + status: "ready", + data: res.body + }); + else patch({ + status: "error", + error: bodyError(res, "读取同步状态失败") + }); + }, function(error) { + patch({ + status: "error", + error: messageOf(error) + }); + }); + } + function resolve(name, action, conflictId) { + patch({ + busy: name + ":" + action, + notice: null + }); + postJson(API.sync + "/resolve", { + name, + action, + conflictId + }).then(function(res) { + if (res.body !== null && res.body !== void 0 && res.body.ok === true) { + patch({ + busy: "", + data: { + stats: state.data === null ? null : state.data.stats, + conflicts: res.body.conflicts || [] + }, + notice: { + tone: "ok", + text: name + ":" + (res.body.note || "已处理") + } + }); + if (typeof props.onResolved === "function") props.onResolved(); + } else patch({ + busy: "", + notice: { + tone: "bad", + text: bodyError(res, "处理冲突失败") + } + }); + }, function(error) { + patch({ + busy: "", + notice: { + tone: "bad", + text: messageOf(error) + } + }); + }); + } + function runNow() { + patch({ busy: "run" }); + postJson(API.sync + "/run", {}).then(function(res) { + if (res.body !== null && res.body !== void 0 && res.body.ok === true) patch({ + busy: "", + data: { + stats: res.body.stats, + conflicts: res.body.conflicts || [] + }, + notice: { + tone: "ok", + text: "已执行一次同步。" + } + }); + else patch({ + busy: "", + notice: { + tone: "bad", + text: bodyError(res, "同步失败") + } + }); + }, function(error) { + patch({ + busy: "", + notice: { + tone: "bad", + text: messageOf(error) + } + }); + }); + } + function resolveAll(action) { + if (conflicts.length === 0) { + patch({ pendingAll: null }); + return; + } + patch({ + busy: "all:" + action, + notice: null + }); + postJson(API.sync + "/resolve-all", { + action, + conflictIds: conflicts.map(function(item) { + return item.conflictId; + }) + }).then(function(res) { + if (res.body !== null && res.body !== void 0 && res.body.ok === true) { + var resolved = Array.isArray(res.body.resolved) ? res.body.resolved : []; + var failed = Array.isArray(res.body.failed) ? res.body.failed : []; + var text = "批量处理完成:成功 " + resolved.length + " 项" + (failed.length === 0 ? "。" : ",失败 " + failed.length + " 项(" + failed.map(function(item) { + return item.name + ":" + item.error; + }).join("、") + ")。"); + patch({ + busy: "", + pendingAll: null, + data: { + stats: res.body.stats, + conflicts: res.body.conflicts || [] + }, + notice: { + tone: failed.length === 0 ? "ok" : "warn", + text + } + }); + if (typeof props.onResolved === "function") props.onResolved(); + } else patch({ + busy: "", + notice: { + tone: "bad", + text: bodyError(res, "批量处理冲突失败") + } + }); + }, function(error) { + patch({ + busy: "", + notice: { + tone: "bad", + text: messageOf(error) + } + }); + }); + } + var data = state.data; + var stats = data === null || data === void 0 ? null : data.stats; + var conflicts = data === null || data === void 0 || !Array.isArray(data.conflicts) ? [] : data.conflicts; + var statPills = stats === null ? h$5("p", { style: styles.hint }, "同步引擎未启用或尚未运行。") : h$5("div", { style: styles.pills }, h$5(Pill, { + label: "运行中", + value: stats.running === true ? "是" : "否", + tone: stats.running === true ? "warn" : "muted" + }), h$5(Pill, { + label: "上次运行", + value: formatIso(stats.lastRunAt), + tone: "muted" + }), h$5(Pill, { + label: "上传", + value: (stats.uploaded || []).length, + tone: "muted" + }), h$5(Pill, { + label: "下载", + value: (stats.downloaded || []).length, + tone: "muted" + }), h$5(Pill, { + label: "冲突", + value: conflicts.length, + tone: conflicts.length > 0 ? "warn" : "muted" + })); + var conflictRows = conflicts.map(function(conflict) { + var local = conflict.local || {}; + var cloud = conflict.cloud; + return h$5("div", { + key: conflict.name, + style: styles.row + }, h$5("div", { style: styles.rowHead }, h$5("span", { style: styles.rowTitle }, conflict.name), h$5("div", { style: styles.pills }, h$5(Pill, { + label: "原因", + value: String(conflict.reason), + tone: "warn" + }), h$5(Pill, { + label: "本地", + value: local.exists === true ? local.files + " 文件" : "缺失", + tone: local.exists === true ? "muted" : "bad" + }), h$5(Pill, { + label: "云端", + value: cloud === null ? "无" : "rev " + String(cloud.revision === void 0 ? "?" : cloud.revision), + tone: cloud === null ? "muted" : "ok" + }))), h$5("p", { style: styles.hint }, conflict.note), h$5("div", { style: styles.actions }, h$5(Btn, { + label: "保留本地", + variant: "small", + disabled: state.busy !== "" || conflict.canKeepLocal !== true, + onClick: function() { + resolve(conflict.name, "keep_local", conflict.conflictId); + } + }), h$5(Btn, { + label: "采用云端", + variant: "small", + disabled: state.busy !== "" || conflict.canTakeCloud !== true, + onClick: function() { + resolve(conflict.name, "take_cloud", conflict.conflictId); + } + }), h$5(Btn, { + label: "手动合并", + variant: "small", + disabled: state.busy !== "", + onClick: function() { + resolve(conflict.name, "manual", conflict.conflictId); + } + }))); + }); + var batchRow = null; + if (conflicts.length > 0) batchRow = h$5("div", { style: styles.actions }, h$5(Btn, { + label: "全部保留本地", + variant: "small", + disabled: state.busy !== "" || !conflicts.some(function(item) { + return item.canKeepLocal === true; + }), + onClick: function() { + patch({ + pendingAll: "keep_local", + notice: null + }); + } + }), h$5(Btn, { + label: "全部采用云端", + variant: "small", + disabled: state.busy !== "" || !conflicts.some(function(item) { + return item.canTakeCloud === true; + }), + onClick: function() { + patch({ + pendingAll: "take_cloud", + notice: null + }); + } + })); + var confirmAllRow = null; + if (state.pendingAll !== null && conflicts.length > 0) { + var pendingText = state.pendingAll === "keep_local" ? "把全部本地版本上传为新版本" : "用云端版本覆盖全部本地(覆盖前自动备份)"; + confirmAllRow = h$5("div", { style: styles.nested }, h$5("p", { style: styles.hint }, "确认" + pendingText + "?共 " + conflicts.length + " 项。"), h$5("div", { style: styles.actions }, h$5(Btn, { + variant: "primary", + label: state.busy === "all:keep_local" || state.busy === "all:take_cloud" ? "处理中…" : "确认批量", + disabled: state.busy !== "", + onClick: function() { + resolveAll(state.pendingAll); + } + }), h$5(Btn, { + variant: "small", + label: "取消", + disabled: state.busy !== "", + onClick: function() { + patch({ pendingAll: null }); + } + }))); + } + return h$5(Section, { + title: "云端同步与冲突", + hint: "同步先比内容与更新时间:一致直接认领,不同则自动采用较新的一边(覆盖云端前先备份本地);只有时间相同/缺失或目录结构不支持时才逐项询问,也可用批量按钮一次处理。", + aside: h$5(Btn, { + label: state.busy === "run" ? "同步中…" : "立即同步", + variant: "small", + disabled: state.busy !== "", + onClick: runNow + }) + }, h$5("div", { style: { + display: "flex", + flexDirection: "column", + gap: "10px" + } }, state.error === null ? null : h$5(Banner, { tone: "bad" }, state.error), state.notice === null ? null : h$5(Banner, { tone: state.notice.tone }, state.notice.text), statPills, batchRow, confirmAllRow, conflicts.length === 0 ? h$5("p", { style: styles.hint }, "当前没有待处理冲突。") : h$5("div", { style: { + display: "flex", + flexDirection: "column", + gap: "8px" + } }, conflictRows))); + } + // src-client/sections/McpCustomSection.tsx + const h$4 = react.createElement; + function McpCustomSection(props) { + var open = react.useState(false); + var tuple = react.useState({ + transport: "streamable-http", + name: "", + serverName: "", + url: "", + command: "", + args: "", + cwd: "", + authMode: "none", + bearerToken: "", + apiKeyHeader: "X-Api-Key", + apiKeyValue: "", + importJson: "", + busy: "", + notice: null + }); + var s = tuple[0]; + var set = tuple[1]; + function patch(next) { + set(function(prev) { + return Object.assign({}, prev, next); + }); + } + function submit() { + if (String(s.name).trim() === "") { + patch({ notice: { + tone: "warn", + text: "请填写连接名称。" + } }); + return; + } + var params = { + name: String(s.name).trim(), + serverName: String(s.serverName).trim() || void 0, + transport: s.transport, + authMode: s.authMode + }; + if (s.transport === "stdio") { + if (String(s.command).trim() === "") { + patch({ notice: { + tone: "warn", + text: "stdio 连接需要启动命令。" + } }); + return; + } + params.command = String(s.command).trim(); + params.args = String(s.args).split(/\s+/).filter(function(x) { + return x !== ""; + }); + if (String(s.cwd).trim() !== "") params.cwd = String(s.cwd).trim(); + } else { + if (String(s.url).trim() === "") { + patch({ notice: { + tone: "warn", + text: "HTTP 连接需要 URL。" + } }); + return; + } + params.url = String(s.url).trim(); + if (s.authMode === "bearer") params.bearerToken = String(s.bearerToken); + if (s.authMode === "api-key") { + params.apiKeyHeader = String(s.apiKeyHeader); + params.apiKeyValue = String(s.apiKeyValue); + } + } + patch({ + busy: "configure", + notice: null + }); + bridgeCall("configure", params).then(function(res) { + var ok = res.status === 200 && res.body !== null && res.body.ok === true; + patch({ + busy: "", + notice: ok ? { + tone: "ok", + text: bridgeMessage(res) + } : { + tone: "bad", + text: "新增失败:" + bridgeMessage(res) + } + }); + if (ok && typeof props.onChanged === "function") props.onChanged(); + }); + } + function importJson() { + if (String(s.importJson).trim() === "") { + patch({ notice: { + tone: "warn", + text: "请粘贴 mcpServers JSON。" + } }); + return; + } + patch({ + busy: "import", + notice: null + }); + bridgeCall("importJson", { json: s.importJson }).then(function(res) { + var ok = res.status === 200 && res.body !== null && res.body.ok === true; + patch({ + busy: "", + notice: ok ? { + tone: "ok", + text: bridgeMessage(res) + } : { + tone: "bad", + text: "导入失败:" + bridgeMessage(res) + } + }); + if (ok && typeof props.onChanged === "function") props.onChanged(); + }); + } + return h$4(Section, { + title: "新增 / 导入连接", + hint: "HTTP 与 stdio 都由宿主校验并保存凭据;本卡不回显已有密钥。stdio 的启动命令由你显式填写,云端不会执行任何本机命令。", + aside: h$4(Btn, { + label: open[0] ? "收起" : "展开", + ariaLabel: "展开新增连接", + variant: "small", + onClick: function() { + open[1](!open[0]); + } + }) + }, open[0] ? h$4("div", { style: { + display: "flex", + flexDirection: "column", + gap: "10px" + } }, h$4("div", { style: styles.grid }, h$4(Field, { + label: "名称", + value: s.name, + onChange: function(v) { + patch({ name: v }); + } + }), h$4(Field, { + label: "Server 名(可选)", + value: s.serverName, + onChange: function(v) { + patch({ serverName: v }); + } + }), h$4("div", { style: styles.field }, h$4("span", { style: styles.label }, "传输方式"), h$4("select", { + value: s.transport, + style: styles.input, + onChange: function(e) { + patch({ transport: e.target.value }); + } + }, h$4("option", { value: "streamable-http" }, "Streamable HTTP"), h$4("option", { value: "stdio" }, "stdio(本机进程)")))), s.transport === "stdio" ? h$4("div", { style: styles.grid }, h$4(Field, { + label: "启动命令", + value: s.command, + placeholder: "npx / uvx / 本机可执行文件", + onChange: function(v) { + patch({ command: v }); + } + }), h$4(Field, { + label: "参数(空格分隔)", + value: s.args, + onChange: function(v) { + patch({ args: v }); + } + }), h$4(Field, { + label: "工作目录(可选)", + value: s.cwd, + onChange: function(v) { + patch({ cwd: v }); + } + })) : h$4("div", { style: { + display: "flex", + flexDirection: "column", + gap: "10px" + } }, h$4(Field, { + label: "URL", + value: s.url, + placeholder: "https://…", + onChange: function(v) { + patch({ url: v }); + } + }), h$4("div", { style: styles.grid }, h$4("div", { style: styles.field }, h$4("span", { style: styles.label }, "鉴权"), h$4("select", { + value: s.authMode, + style: styles.input, + onChange: function(e) { + patch({ authMode: e.target.value }); + } + }, h$4("option", { value: "none" }, "无"), h$4("option", { value: "bearer" }, "Bearer Token"), h$4("option", { value: "api-key" }, "API Key"))), s.authMode === "bearer" ? h$4(Field, { + label: "Bearer Token", + type: "password", + value: s.bearerToken, + onChange: function(v) { + patch({ bearerToken: v }); + } + }) : null, s.authMode === "api-key" ? h$4(Field, { + label: "API Key 头", + value: s.apiKeyHeader, + onChange: function(v) { + patch({ apiKeyHeader: v }); + } + }) : null, s.authMode === "api-key" ? h$4(Field, { + label: "API Key 值", + type: "password", + value: s.apiKeyValue, + onChange: function(v) { + patch({ apiKeyValue: v }); + } + }) : null)), h$4("div", { style: styles.actions }, h$4(Btn, { + variant: "primary", + label: s.busy === "configure" ? "保存中…" : "校验并保存连接", + disabled: s.busy !== "", + onClick: submit + })), h$4("p", { style: styles.hint }, "或粘贴 mcpServers JSON 批量导入:"), h$4("textarea", { + value: s.importJson, + rows: 5, + style: Object.assign({}, styles.input, { + fontFamily: "monospace", + resize: "vertical" + }), + placeholder: "{\"mcpServers\":{\"name\":{\"url\":\"…\"}}}", + onChange: function(e) { + patch({ importJson: e.target.value }); + } + }), h$4("div", { style: styles.actions }, h$4(Btn, { + label: s.busy === "import" ? "导入中…" : "导入 JSON", + disabled: s.busy !== "", + onClick: importJson + })), s.notice === null ? null : h$4(Banner, { tone: s.notice.tone }, s.notice.text)) : null); + } + // src-client/sections/McpConfigSection.tsx + const h$3 = react.createElement; + function McpConfigSection(props) { + var items = Array.isArray(props.items) ? props.items : []; + var open = react.useState(false); + var tuple = react.useState({ + key: "", + json: "", + busy: "", + notice: null, + scopePreview: null, + targetScope: "global", + workspaceId: "", + gov: null, + policyPreview: null + }); + var s = tuple[0]; + var set = tuple[1]; + function patch(next) { + set(function(prev) { + return Object.assign({}, prev, next); + }); + } + var selected = null; + for (var i = 0; i < items.length; i += 1) if (items[i].key === s.key) selected = items[i]; + react.useEffect(function() { + bridgeCall("governance", {}).then(function(res) { + if (res.body !== null && res.body.ok === true) patch({ gov: res.body.detail }); + }); + }, []); + function choose(key) { + patch({ + key, + json: "", + scopePreview: null, + policyPreview: null, + notice: null + }); + if (key === "") return; + bridgeCall("editableConnectionConfig", { key }).then(function(res) { + if (res.body !== null && res.body.ok === true && res.body.detail !== void 0) patch({ json: String(res.body.detail.json === void 0 ? "" : res.body.detail.json) }); + else patch({ + json: "", + notice: { + tone: "warn", + text: bridgeMessage(res) + } + }); + }); + } + function save() { + if (s.key === "") return; + patch({ busy: "save" }); + bridgeCall("reconfigureConnection", { + key: s.key, + json: s.json + }).then(function(res) { + var ok = res.body !== null && res.body.ok === true; + patch({ + busy: "", + notice: ok ? { + tone: "ok", + text: bridgeMessage(res) + } : { + tone: "bad", + text: "保存失败:" + bridgeMessage(res) + } + }); + if (ok && typeof props.onChanged === "function") props.onChanged(); + }); + } + function previewScope() { + if (s.key === "") return; + patch({ busy: "scope-preview" }); + var params = { + key: s.key, + targetScope: s.targetScope, + mode: "move" + }; + if (s.targetScope === "project") params.workspaceId = s.workspaceId; + bridgeCall("previewConnectionScope", params).then(function(res) { + var ok = res.body !== null && res.body.ok === true; + patch({ + busy: "", + scopePreview: ok ? res.body.detail : null, + notice: ok ? null : { + tone: "bad", + text: bridgeMessage(res) + } + }); + if (ok && res.body.detail.canApply !== true) patch({ notice: { + tone: "warn", + text: bridgeMessage(res) + } }); + }); + } + function applyScope() { + if (s.key === "" || s.scopePreview === null) return; + patch({ busy: "scope-apply" }); + var params = { + key: s.key, + targetScope: s.targetScope, + mode: "move", + expectedRevision: s.scopePreview.baseRevision + }; + if (s.targetScope === "project") params.workspaceId = s.workspaceId; + bridgeCall("applyConnectionScope", params).then(function(res) { + var ok = res.body !== null && res.body.ok === true; + patch({ + busy: "", + notice: ok ? { + tone: "ok", + text: bridgeMessage(res) + } : { + tone: "bad", + text: "作用域应用失败:" + bridgeMessage(res) + }, + scopePreview: null + }); + if (ok && typeof props.onChanged === "function") props.onChanged(); + }); + } + function previewPolicy(effect) { + if (selected === null) return; + patch({ busy: "policy-preview" }); + bridgeCall("previewPolicy", { + scope: "connection", + effect, + connectorId: selected.connectorId + }).then(function(res) { + var ok = res.body !== null && res.body.ok === true; + patch({ + busy: "", + policyPreview: ok ? res.body.detail : null, + notice: ok ? { + tone: "ok", + text: bridgeMessage(res) + } : { + tone: "bad", + text: "策略预览失败:" + bridgeMessage(res) + } + }); + }); + } + function applyPolicy() { + if (selected === null || s.policyPreview === null) return; + patch({ busy: "policy-apply" }); + bridgeCall("applyPolicy", { + scope: "connection", + effect: s.policyPreview.mutation.effect, + connectorId: selected.connectorId, + expectedRevision: s.policyPreview.baseRevision + }).then(function(res) { + var ok = res.body !== null && res.body.ok === true; + patch({ + busy: "", + policyPreview: null, + notice: ok ? { + tone: "ok", + text: bridgeMessage(res) + } : { + tone: "bad", + text: "策略应用失败:" + bridgeMessage(res) + } + }); + if (ok) bridgeCall("governance", {}).then(function(g) { + if (g.body !== null && g.body.ok === true) patch({ gov: g.body.detail }); + }); + }); + } + var gov = s.gov; + return h$3(Section, { + title: "连接配置、范围与治理", + hint: "配置编辑使用宿主的保留标记合并,空输入不会清空已有密钥;作用域与策略都带 revision 预览后再提交。", + aside: h$3(Btn, { + label: open[0] ? "收起" : "展开", + ariaLabel: "展开连接配置", + variant: "small", + onClick: function() { + open[1](!open[0]); + } + }) + }, open[0] ? h$3("div", { style: { + display: "flex", + flexDirection: "column", + gap: "12px" + } }, itemSelect(items, s.key, choose), s.key === "" ? h$3("p", { style: styles.hint }, "选择一条连接后可编辑配置、调整范围或设置治理策略。") : h$3("div", { style: styles.nested }, h$3("p", { style: styles.hint }, "可编辑配置(保留标记 表示保留原密钥):"), h$3("textarea", { + value: s.json, + rows: 8, + style: Object.assign({}, styles.input, { + fontFamily: "monospace", + resize: "vertical" + }), + onChange: function(e) { + patch({ json: e.target.value }); + } + }), h$3("div", { style: styles.actions }, h$3(Btn, { + variant: "primary", + label: s.busy === "save" ? "保存中…" : "保存配置", + disabled: s.busy !== "" || s.json === "", + onClick: save + })), h$3("p", { style: styles.hint }, "作用域(目标范围 + mode=move,凭据不复制):"), h$3("div", { style: styles.grid }, h$3("div", { style: styles.field }, h$3("span", { style: styles.label }, "目标范围"), h$3("select", { + value: s.targetScope, + style: styles.input, + onChange: function(e) { + patch({ targetScope: e.target.value }); + } + }, h$3("option", { value: "global" }, "全局"), h$3("option", { value: "project" }, "指定工作区"))), s.targetScope === "project" ? h$3(Field, { + label: "工作区 id", + value: s.workspaceId, + onChange: function(v) { + patch({ workspaceId: v }); + } + }) : null), h$3("div", { style: styles.actions }, h$3(Btn, { + label: s.busy === "scope-preview" ? "预览中…" : "预览作用域", + disabled: s.busy !== "", + onClick: previewScope + }), s.scopePreview === null ? null : h$3(Btn, { + variant: "primary", + label: s.busy === "scope-apply" ? "应用中…" : "应用作用域(rev " + String(s.scopePreview.baseRevision) + ")", + disabled: s.busy !== "" || s.scopePreview.canApply !== true, + onClick: applyScope + })), s.scopePreview === null ? null : h$3("p", { style: styles.hint }, "影响:" + String(s.scopePreview.impact === void 0 ? "" : JSON.stringify(s.scopePreview.impact)) + ";可应用:" + (s.scopePreview.canApply === true ? "是" : "否") + ";凭据复制:否"), h$3("p", { style: styles.hint }, "治理(当前 connection 级):"), gov === null ? h$3("p", { style: styles.hint }, "治理文档未读取。") : h$3("div", { style: styles.pills }, h$3(Pill, { + label: "revision", + value: gov.revision, + tone: "muted" + }), h$3(Pill, { + label: "规则", + value: (gov.rules || []).length, + tone: "muted" + }), h$3(Pill, { + label: "优先级", + value: String(gov.precedenceLabel || "—"), + tone: "muted" + })), h$3("div", { style: styles.actions }, h$3(Btn, { + label: "预览 allow", + variant: "small", + disabled: s.busy !== "" || selected === null, + onClick: function() { + previewPolicy("allow"); + } + }), h$3(Btn, { + label: "预览 deny", + variant: "small", + disabled: s.busy !== "" || selected === null, + onClick: function() { + previewPolicy("deny"); + } + }), h$3(Btn, { + label: "预览 inherit", + variant: "small", + disabled: s.busy !== "" || selected === null, + onClick: function() { + previewPolicy("inherit"); + } + }), s.policyPreview === null ? null : h$3(Btn, { + variant: "primary", + label: s.busy === "policy-apply" ? "应用中…" : "应用策略(rev " + String(s.policyPreview.baseRevision) + ")", + disabled: s.busy !== "", + onClick: applyPolicy + }))), s.notice === null ? null : h$3(Banner, { tone: s.notice.tone }, s.notice.text)) : null); + } + function itemSelect(items, value, onChange) { + return h$3("div", { style: styles.field }, h$3("span", { style: styles.label }, "连接"), h$3("select", { + value, + style: styles.input, + onChange: function(e) { + onChange(e.target.value); + } + }, h$3("option", { value: "" }, "选择连接…"), items.map(function(item) { + return h$3("option", { + key: item.key, + value: item.key + }, String(item.name || item.key) + "(" + String(item.transport || "") + ")"); + }))); + } + // src-client/sections/ImportSection.tsx + const h$2 = react.createElement; + function ImportSection(props) { + var onImported = props && typeof props.onImported === "function" ? props.onImported : null; + var tuple = react.useState({ + expanded: false, + status: "idle", + sources: [], + open: {}, + busy: "", + notice: null, + imported: {}, + search: "", + selection: [], + progress: null + }); + var s = tuple[0]; + var set = tuple[1]; + function patch(next) { + set(function(prev) { + return Object.assign({}, prev, next); + }); + } + function load() { + patch({ + status: "loading", + notice: null + }); + request(API.skills + "/sources", { method: "GET" }).then(function(res) { + if (res.body !== null && res.body !== void 0 && res.body.ok === true) patch({ + status: "ready", + sources: Array.isArray(res.body.sources) ? res.body.sources : [] + }); + else patch({ + status: "error", + notice: { + tone: "bad", + text: bodyError(res, "读取来源失败") + } + }); + }, function(error) { + patch({ + status: "error", + notice: { + tone: "bad", + text: messageOf(error) + } + }); + }); + } + function openPanel() { + patch({ expanded: true }); + if (s.status === "idle" || s.status === "error") load(); + } + function closePanel() { + patch({ expanded: false }); + } + function dshFolders() { + var found = {}; + s.sources.forEach(function(source) { + if (!source.writable) return; + (source.groups || []).forEach(function(group) { + (group.skills || []).forEach(function(skill) { + found[skill.folder] = true; + }); + }); + }); + return found; + } + function toggleSource(sourceId) { + var next = Object.assign({}, s.open); + next[sourceId] = !(s.open[sourceId] !== false); + patch({ open: next }); + } + var mine = dshFolders(); + var others = s.sources.filter(function(source) { + return !source.writable; + }); + var needle = String(s.search).trim().toLowerCase(); + function skillsOf(source) { + var list = []; + (source.groups || []).forEach(function(group) { + (group.skills || []).forEach(function(skill) { + list.push(skill); + }); + }); + return list; + } + function importedYet(skill) { + return mine[skill.folder] === true || s.imported[skill.folder] === true; + } + var candidates = []; + others.forEach(function(source) { + if (!source.exists) return; + skillsOf(source).forEach(function(skill) { + candidates.push({ + source, + skill + }); + }); + }); + var matches = needle === "" ? candidates : candidates.filter(function(entry) { + return (String(entry.skill.name || "") + " " + String(entry.skill.folder || "") + " " + String(entry.skill.description || "")).toLowerCase().indexOf(needle) >= 0; + }); + var importable = matches.filter(function(entry) { + return !importedYet(entry.skill); + }); + var alreadyCount = candidates.length - candidates.filter(function(entry) { + return !importedYet(entry.skill); + }).length; + function keyOf(entry) { + return String(entry.source.id) + ":" + String(entry.skill.folder); + } + var chosen = s.selection; + function isChosen(entry) { + return chosen.indexOf(keyOf(entry)) >= 0; + } + function chooseMany(list, on) { + var next = chosen.slice(); + list.forEach(function(entry) { + var key = keyOf(entry); + var at = next.indexOf(key); + if (on === true && at < 0) next.push(key); + if (on !== true && at >= 0) next.splice(at, 1); + }); + patch({ selection: next }); + } + var chosenEntries = candidates.filter(function(entry) { + return chosen.indexOf(keyOf(entry)) >= 0 && !importedYet(entry.skill); + }); + function importSelected() { + var targets = chosenEntries; + if (targets.length === 0) return Promise.resolve(); + patch({ + busy: "batch", + notice: null, + progress: { + done: 0, + total: targets.length + } + }); + var done = 0; + var failures = []; + var importedMap = Object.assign({}, s.imported); + var chain = Promise.resolve(); + targets.forEach(function(entry) { + chain = chain.then(function() { + return postJson(API.skills + "/import", { + sourceId: entry.source.id, + folder: entry.skill.folder + }).then(function(res) { + if (res.body !== null && res.body !== void 0 && res.body.ok === true) { + done += 1; + importedMap[entry.skill.folder] = true; + } else failures.push(String(entry.skill.name) + "(" + bodyError(res, "失败") + ")"); + patch({ progress: { + done: done + failures.length, + total: targets.length + } }); + }, function(error) { + failures.push(String(entry.skill.name) + "(" + messageOf(error) + ")"); + patch({ progress: { + done: done + failures.length, + total: targets.length + } }); + }); + }); + }); + return chain.then(function() { + patch({ + busy: "", + progress: null, + selection: [], + imported: importedMap, + notice: { + tone: failures.length === 0 ? "ok" : "warn", + text: "已导入 " + String(done) + " / " + String(targets.length) + " 个技能到 DSH" + (failures.length === 0 ? "。" : ";失败:" + failures.join("、")) + } + }); + if (onImported !== null) onImported(); + load(); + }); + } + if (!s.expanded) return h$2("div", { style: { + display: "flex", + alignItems: "center", + justifyContent: "space-between", + gap: "8px", + flexWrap: "wrap" + } }, h$2(Btn, { + label: "从其他软件导入", + ariaLabel: "从其他软件导入", + onClick: openPanel + }), s.notice === null ? null : h$2(Banner, { tone: s.notice.tone }, s.notice.text)); + function describe(skill) { + var text = String(skill.description === void 0 || skill.description === null ? "" : skill.description).trim(); + if (/^[>|][+-]?$/.test(text)) return ""; + return text; + } + var blocks = others.map(function(source) { + var all = skillsOf(source); + var visible = matches.filter(function(entry) { + return entry.source.id === source.id; + }); + var open = s.open[source.id] !== false; + var selectable = visible.filter(function(entry) { + return !importedYet(entry.skill); + }); + return h$2("div", { + key: source.id, + style: styles.row + }, h$2("div", { style: styles.rowHead }, h$2("span", { style: { + display: "inline-flex", + alignItems: "center", + gap: "8px", + minWidth: 0 + } }, h$2("span", { style: styles.sourceGlyph }, String(source.label).slice(0, 1)), h$2("span", { style: styles.sourceTitle }, String(source.label)), h$2("span", { style: styles.hint }, source.exists ? String(all.length) + " 个技能" + (needle === "" ? "" : " · 匹配 " + String(visible.length)) + " · 可导入 " + String(selectable.length) : "未安装")), source.exists ? h$2("div", { style: styles.actions }, h$2(Btn, { + label: "全选本来源(" + String(selectable.length) + ")", + ariaLabel: "全选本来源 " + String(source.label), + variant: "small", + disabled: selectable.length === 0 || s.busy !== "", + onClick: function() { + chooseMany(visible, true); + } + }), h$2(Btn, { + label: open ? "收起" : "展开", + variant: "small", + onClick: function() { + toggleSource(source.id); + } + })) : null), h$2("span", { style: styles.mono }, String(source.root)), !source.exists || !open ? null : visible.length === 0 ? h$2("p", { style: styles.hint }, needle === "" ? "这个来源里还没有技能。" : "没有匹配「" + s.search + "」的技能。") : h$2("div", { style: { + display: "flex", + flexDirection: "column", + gap: "2px", + maxHeight: "320px", + overflow: "auto" + } }, visible.map(function(entry) { + var skill = entry.skill; + var has = importedYet(skill); + return h$2("label", { + key: String(skill.folder), + style: styles.checkboxRow + }, h$2("input", { + type: "checkbox", + checked: has || isChosen(entry), + disabled: has || s.busy !== "", + "aria-label": "选择技能 " + String(skill.name), + onChange: function(event) { + chooseMany([entry], event.target.checked === true); + } + }), h$2("span", { style: { + minWidth: 0, + flex: "1 1 auto" + } }, h$2("span", { style: styles.rowTitle }, String(skill.name)), describe(skill) === "" ? null : h$2("span", { style: Object.assign({}, styles.hint, styles.clamp1) }, describe(skill))), has ? h$2(Pill, { + label: "DSH", + value: "已导入", + tone: "muted" + }) : null); + }))); + }); + return h$2(Section, { + title: "从其他软件导入", + hint: "把其他软件技能目录里的技能复制一份到 DSH,原目录不动;同名技能不能重复导入。", + aside: h$2("div", { style: styles.actions }, h$2(Btn, { + label: "刷新", + variant: "small", + onClick: load + }), h$2(Btn, { + label: "收起", + variant: "small", + onClick: closePanel + })) + }, s.status === "loading" ? h$2("p", { style: styles.hint }, "正在读取来源…") : others.length === 0 ? h$2("p", { style: styles.hint }, "没有检测到其他软件的技能目录。") : h$2("div", { style: styles.root }, h$2("div", { style: styles.pills }, h$2(Pill, { + label: "来源", + value: String(others.length), + tone: "muted" + }), h$2(Pill, { + label: "可导入", + value: String(candidates.length - alreadyCount), + tone: "muted" + }), s.search === "" ? null : h$2(Pill, { + label: "匹配", + value: String(matches.length), + tone: matches.length === 0 ? "muted" : "ok" + }), h$2(Pill, { + label: "已选", + value: String(chosenEntries.length), + tone: chosenEntries.length === 0 ? "muted" : "ok" + }), alreadyCount === 0 ? null : h$2(Pill, { + label: "已在 DSH", + value: String(alreadyCount), + tone: "muted" + })), h$2("div", { style: { + display: "flex", + gap: "8px", + alignItems: "center", + flexWrap: "wrap" + } }, h$2("input", { + type: "search", + style: Object.assign({}, styles.input, { + flex: "1 1 220px", + minWidth: 0 + }), + value: s.search, + placeholder: "搜索技能:名称 / 目录 / 描述", + "aria-label": "搜索要导入的技能", + onChange: function(event) { + patch({ search: event.target.value }); + } + }), s.search === "" ? null : h$2(Btn, { + label: "清空搜索", + variant: "small", + onClick: function() { + patch({ search: "" }); + } + })), h$2("div", { style: Object.assign({}, styles.actions, { flexWrap: "wrap" }) }, h$2(Btn, { + label: "全选匹配可导入(" + String(importable.length) + ")", + ariaLabel: "全选匹配可导入", + variant: "small", + disabled: importable.length === 0 || s.busy !== "", + onClick: function() { + chooseMany(importable, true); + } + }), h$2(Btn, { + label: "全选全部(" + String(candidates.length - alreadyCount) + ")", + ariaLabel: "全选全部", + variant: "small", + disabled: candidates.length - alreadyCount === 0 || s.busy !== "", + onClick: function() { + chooseMany(candidates.filter(function(entry) { + return !importedYet(entry.skill); + }), true); + } + }), h$2(Btn, { + label: "清空已选", + ariaLabel: "清空已选", + variant: "small", + disabled: chosenEntries.length === 0, + onClick: function() { + patch({ selection: [] }); + } + }), h$2(Btn, { + variant: "primary", + label: s.busy === "batch" ? "导入中…" : "导入所选(" + String(chosenEntries.length) + ")", + ariaLabel: "导入所选", + disabled: s.busy !== "" || chosenEntries.length === 0, + onClick: importSelected + })), s.progress === null ? null : h$2("p", { style: styles.hint }, "正在导入:" + String(s.progress.done) + " / " + String(s.progress.total)), h$2("div", { style: { + display: "flex", + flexDirection: "column", + gap: "10px" + } }, blocks)), s.notice === null ? null : h$2(Banner, { tone: s.notice.tone }, s.notice.text)); + } + // src-client/SettingsCard.tsx + const h$1 = react.createElement; + function findCheck(source, id) { + if (source === null || source === void 0 || !Array.isArray(source.checks)) return void 0; + for (var index = 0; index < source.checks.length; index += 1) if (source.checks[index] !== null && typeof source.checks[index] === "object" && source.checks[index].id === id) return source.checks[index]; + } + function SettingsCard() { + var snapshotState = react.useState(null); + var snapshot = snapshotState[0]; + var setSnapshot = snapshotState[1]; + var draftState = react.useState(null); + var draft = draftState[0]; + var setDraft = draftState[1]; + var tokenState = react.useState(emptyTokens()); + var tokenDraft = tokenState[0]; + var setTokenDraft = tokenState[1]; + var identityState = react.useState(readStored("identity", "account")); + var identity = identityState[0]; + var setIdentity = identityState[1]; + var loginState = react.useState({ + username: "", + password: "" + }); + var loginDraft = loginState[0]; + var setLoginDraft = loginState[1]; + var loginResultState = react.useState(null); + var loginResult = loginResultState[0]; + var setLoginResult = loginResultState[1]; + var errorState = react.useState(null); + var error = errorState[0]; + var setError = errorState[1]; + var noticeState = react.useState(null); + var notice = noticeState[0]; + var setNotice = noticeState[1]; + var testState = react.useState(null); + var testResult = testState[0]; + var setTestResult = testState[1]; + var busyState = react.useState(""); + var busy = busyState[0]; + var setBusy = busyState[1]; + var tabState = react.useState(readStored("tab", "connection")); + var tab = tabState[0]; + var setTab = tabState[1]; + var advancedState = react.useState(readStored("advanced", "closed") === "open"); + var advanced = advancedState[0]; + var setAdvanced = advancedState[1]; + var skillsState = react.useState({ + status: "idle", + data: null, + error: null, + detail: null, + uninstall: null, + plan: null, + selection: [], + installUrl: "", + search: "", + filter: "all", + busy: "" + }); + var skills = skillsState[0]; + var setSkills = skillsState[1]; + var mcpState = react.useState({ + status: "idle", + items: [], + error: null, + tools: {}, + busy: "", + confirm: null, + search: "", + filter: "all", + selection: [], + bulk: null, + confirmBulk: false, + details: {} + }); + var mcp = mcpState[0]; + var setMcp = mcpState[1]; + var treeState = react.useState({ + status: "idle", + data: null, + error: null, + busy: "", + expanded: {}, + openGroups: {}, + edit: null, + confirm: null, + assign: null + }); + var tree = treeState[0]; + var setTree = treeState[1]; + function patchSkills(patch) { + setSkills(function(previous) { + return Object.assign({}, previous, patch); + }); + } + function patchMcp(patch) { + setMcp(function(previous) { + return Object.assign({}, previous, patch); + }); + } + function patchTree(patch) { + setTree(function(previous) { + return Object.assign({}, previous, patch); + }); + } + var load = react.useCallback(function(keepDraft) { + return request(API.status).then(function(result) { + if (result.status !== 200 || result.body === null || result.body.ok !== true) { + setError("读取插件状态失败(HTTP " + String(result.status) + "),请重启 DSH 后再试。"); + return; + } + setError(null); + setSnapshot(result.body); + if (keepDraft !== true) setDraft(pickSettings(result.body.settings)); + }).catch(function(failure) { + setError("无法连接插件:" + messageOf(failure)); + }); + }, []); + react.useEffect(function() { + load(false); + var timer = setInterval(function() { + if (typeof document !== "undefined" && document.visibilityState === "hidden") return; + load(true); + }, POLL_MS); + return function() { + clearInterval(timer); + }; + }, [load]); + var dirty = snapshot !== null && draft !== null && JSON.stringify(draft) !== JSON.stringify(pickSettings(snapshot.settings)); + react.useEffect(function() { + if (!dirty) return void 0; + var guard = function(event) { + event.preventDefault(); + event.returnValue = "有未保存的改动"; + return event.returnValue; + }; + window.addEventListener("beforeunload", guard); + return function() { + window.removeEventListener("beforeunload", guard); + }; + }, [dirty]); + function patchDraft(patch) { + setDraft(function(previous) { + return Object.assign({}, previous, patch); + }); + } + function patchTokens(patch) { + setTokenDraft(function(previous) { + return Object.assign({}, previous, patch); + }); + } + function run(label, task) { + setBusy(label); + setNotice(null); + return Promise.resolve().then(task).catch(function(failure) { + setNotice({ + tone: "bad", + text: messageOf(failure) + }); + }).then(function() { + setBusy(""); + }); + } + function save() { + return run("save", function() { + var payload = { + enabled: draft.enabled, + baseUrl: draft.baseUrl, + clientId: draft.clientId, + requestTimeoutMs: Number(draft.requestTimeoutMs), + defaultK: Number(draft.defaultK), + summaryEnabled: draft.summaryEnabled, + activityEnabled: draft.activityEnabled, + memoryEnabled: draft.memoryEnabled, + skillsSyncEnabled: draft.skillsSyncEnabled, + skillsDir: String(draft.skillsDir).trim() + }; + if (tokenDraft.activity.trim() !== "") payload.activityToken = tokenDraft.activity.trim(); + if (tokenDraft.memory.trim() !== "") payload.memoryToken = tokenDraft.memory.trim(); + if (tokenDraft.summary.trim() !== "") payload.summaryToken = tokenDraft.summary.trim(); + return request(API.config, { + method: "PUT", + body: JSON.stringify(payload) + }).then(function(result) { + if (result.status !== 200 || result.body === null || result.body.ok !== true) throw new Error("保存失败:" + bodyError(result)); + setTokenDraft(emptyTokens()); + var cleared = Array.isArray(result.body.credentialsCleared) ? result.body.credentialsCleared : []; + setNotice(cleared.length === 0 ? { + tone: "ok", + text: "已保存到本机配置文件。" + } : { + tone: "warn", + text: "服务地址的 origin 已变更,旧地址的令牌已清除(" + cleared.join("、") + "),请重新登录或填写新令牌。" + }); + setSnapshot(result.body); + setDraft(pickSettings(result.body.settings)); + }); + }); + } + function clearToken(which) { + return run("clear-" + which, function() { + var payload = which === "activity" ? { clearActivityToken: true } : which === "memory" ? { clearMemoryToken: true } : { clearSummaryToken: true }; + return request(API.config, { + method: "PUT", + body: JSON.stringify(payload) + }).then(function(result) { + if (result.status !== 200 || result.body === null || result.body.ok !== true) throw new Error("清除令牌失败:" + bodyError(result)); + setNotice({ + tone: "ok", + text: "令牌已清除。" + }); + setSnapshot(result.body); + setDraft(pickSettings(result.body.settings)); + }); + }); + } + function test() { + return run("test", function() { + setTestResult(null); + return request(API.test, { + method: "POST", + body: "{}" + }).then(function(result) { + if (result.status !== 200 || result.body === null || result.body.ok !== true) throw new Error("测试失败(HTTP " + String(result.status) + ")"); + setTestResult(result.body); + load(true); + }); + }); + } + function flush() { + return run("flush", function() { + return request(API.flush, { + method: "POST", + body: "{}" + }).then(function(result) { + if (result.status !== 200 || result.body === null || result.body.ok !== true) throw new Error("重试失败(HTTP " + String(result.status) + ")"); + setNotice({ + tone: "ok", + text: "已重试 " + String(result.body.sent) + " 条,剩余 " + String(result.body.remaining) + " 条。" + }); + load(true); + }); + }); + } + function login() { + return run("login", function() { + setLoginResult(null); + var payload = { + username: loginDraft.username.trim(), + password: loginDraft.password + }; + if (payload.username === "" || payload.password === "") throw new Error("请填写用户名和密码"); + return request(API.login, { + method: "POST", + body: JSON.stringify(payload) + }).then(function(result) { + if (result.status !== 200 || result.body === null || result.body.ok !== true) throw new Error("登录失败:" + bodyError(result)); + setLoginDraft({ + username: payload.username, + password: "" + }); + setLoginResult(result.body.login || null); + setNotice({ + tone: result.body.login !== null && result.body.login !== void 0 && result.body.login.partial === true ? "warn" : "ok", + text: "登录成功,已写入可用的令牌。" + }); + setSnapshot(result.body); + setDraft(pickSettings(result.body.settings)); + }); + }); + } + function switchIdentity(next) { + setIdentity(next); + store("identity", next); + setLoginResult(null); + setNotice(null); + } + function switchTab(next) { + setTab(next); + store("tab", next); + if (next === "skills" && skills.status === "idle") loadSkills(); + if (next === "skills" && tree.status === "idle") loadTree(); + if (next === "mcp" && mcp.status === "idle") probeMcp(); + } + function toggleAdvanced() { + setAdvanced(function(previous) { + store("advanced", previous ? "closed" : "open"); + return !previous; + }); + } + react.useEffect(function() { + if (tab === "skills" && skills.status === "idle") loadSkills(); + if (tab === "skills" && tree.status === "idle") loadTree(); + if (tab === "mcp" && mcp.status === "idle") probeMcp(); + }, [ + tab, + skills.status, + mcp.status, + tree.status + ]); + function loadSkills() { + patchSkills({ + status: "loading", + error: null + }); + return request(API.skills + "/api").then(function(result) { + if (result.status !== 200 || result.body === null || result.body.ok !== true) { + patchSkills({ + status: "error", + error: "读取技能列表失败:" + statusText(result.status) + }); + return; + } + patchSkills({ + status: "ready", + data: result.body, + error: null + }); + }).catch(function(failure) { + patchSkills({ + status: "error", + error: "无法读取技能列表:" + messageOf(failure) + }); + }); + } + function skillAction(path, payload, label) { + patchSkills({ busy: label }); + return postJson(API.skills + path, payload).then(function(result) { + patchSkills({ busy: "" }); + if (result.status !== 200 || result.body === null || result.body.ok !== true) throw new Error(bodyError(result, statusText(result.status))); + return result.body; + }).catch(function(failure) { + patchSkills({ busy: "" }); + patchSkills({ error: messageOf(failure) }); + setNotice({ + tone: "bad", + text: messageOf(failure) + }); + throw failure; + }); + } + function loadTree() { + patchTree({ + status: "loading", + error: null + }); + return request(API.skills + "/sources").then(function(result) { + if (result.status !== 200 || result.body === null || result.body.ok !== true) { + patchTree({ + status: "error", + error: "读取技能来源失败:" + statusText(result.status) + }); + return; + } + patchTree({ + status: "ready", + data: result.body, + error: null + }); + }).catch(function(failure) { + patchTree({ + status: "error", + error: "无法读取技能来源:" + messageOf(failure) + }); + }); + } + function treeAction(path, payload, label) { + patchTree({ busy: label }); + return postJson(API.skills + path, payload).then(function(result) { + patchTree({ busy: "" }); + if (result.status !== 200 || result.body === null || result.body.ok !== true) throw new Error(bodyError(result, statusText(result.status))); + return result.body; + }).catch(function(failure) { + patchTree({ busy: "" }); + setNotice({ + tone: "bad", + text: messageOf(failure) + }); + return null; + }); + } + function createGroup() { + if (tree.edit === null) return Promise.resolve(); + var name = String(tree.edit.value === void 0 ? "" : tree.edit.value).trim(); + if (name === "") { + setNotice({ + tone: "bad", + text: "请填写分类名。" + }); + return Promise.resolve(); + } + return treeAction("/groups/create", { + sourceId: tree.edit.sourceId, + name + }, "create").then(function(body) { + if (body === null) return; + patchTree({ edit: null }); + setNotice({ + tone: "ok", + text: "已创建分类「" + String(body.name) + "」。" + }); + return loadTree(); + }); + } + function saveRename() { + if (tree.edit === null) return Promise.resolve(); + return treeAction("/groups/rename", { + sourceId: tree.edit.sourceId, + groupId: tree.edit.groupId, + name: String(tree.edit.value).trim() + }, "rename").then(function(body) { + if (body === null) return; + patchTree({ edit: null }); + return loadTree(); + }); + } + function deleteGroup(sourceId, groupId) { + return treeAction("/groups/delete", { + sourceId, + groupId + }, "delete").then(function(body) { + if (body === null) return; + patchTree({ confirm: null }); + setNotice({ + tone: "ok", + text: "分类已删除," + String(body.released) + " 个技能回到「未分类」;没有删除任何文件。" + }); + return loadTree(); + }); + } + function openAssign(source, group) { + var renameKey = String(source.id) + ":" + group.id; + if (tree.assign !== null && tree.assign.sourceId === source.id && tree.assign.groupId === group.id) { + patchTree({ assign: null }); + return; + } + patchTree({ + openGroups: Object.assign({}, tree.openGroups, { [renameKey]: true }), + assign: { + sourceId: source.id, + groupId: group.id, + search: "", + selection: (Array.isArray(group.skills) ? group.skills : []).map(function(skill) { + return skill.folder; + }) + } + }); + } + function saveAssign() { + if (tree.assign === null) return Promise.resolve(); + return treeAction("/groups/assign", { + sourceId: tree.assign.sourceId, + groupId: tree.assign.groupId, + folders: tree.assign.selection + }, "assign").then(function(body) { + if (body === null) return; + patchTree({ assign: null }); + return loadTree(); + }); + } + function toggleGroup(sourceId, groupId, enabled) { + return treeAction("/groups/toggle", { + sourceId, + groupId, + enabled + }, "toggle-group").then(function(body) { + if (body === null) return; + if (body.cardOnly === true) setNotice({ + tone: "warn", + text: "这个来源由对应软件自行加载:开关只记录在本卡,不影响它自己的加载。" + }); + else { + var failed = Array.isArray(body.failed) ? body.failed : []; + setNotice({ + tone: failed.length === 0 ? "ok" : "warn", + text: "已" + (enabled ? "启用" : "停用") + " " + String(Array.isArray(body.applied) ? body.applied.length : 0) + " 项" + (failed.length === 0 ? "(新会话生效)。" : "," + String(failed.length) + " 项失败:" + failed.map(function(item) { + return String(item.folder) + "(" + String(item.reason) + ")"; + }).join("、")) + }); + } + return loadTree(); + }); + } + function openDetail(folder) { + patchSkills({ detail: { + folder, + data: null, + error: null + } }); + return skillAction("/detail", { folder }, "detail").then(function(body) { + patchSkills({ detail: { + folder, + data: body, + error: null + } }); + }, function() { + patchSkills({ detail: { + folder, + data: null, + error: "无法读取详情" + } }); + }); + } + function toggleSkill(folder, enabled) { + return skillAction("/toggle", { + folder, + enabled + }, "toggle").then(function(body) { + setNotice({ + tone: "ok", + text: body.note === void 0 ? "已更新。" : String(body.note) + }); + return loadSkills(); + }, function() {}); + } + function planUninstall(folder) { + return skillAction("/uninstall/plan", { folder }, "uninstall").then(function(body) { + patchSkills({ uninstall: body }); + }, function() {}); + } + function confirmUninstall() { + if (skills.uninstall === null) return Promise.resolve(); + return skillAction("/remove", { + folder: skills.uninstall.folder, + token: skills.uninstall.token, + confirmed: true + }, "remove").then(function(body) { + patchSkills({ uninstall: null }); + setNotice({ + tone: "ok", + text: "已删除本机技能 " + String(body.folder) + "(" + String(body.files) + " 个文件)。" + }); + return loadSkills(); + }, function() {}); + } + function prepareInstall() { + var url = String(skills.installUrl).trim(); + if (url === "") { + setNotice({ + tone: "bad", + text: "请填写 git 仓库地址。" + }); + return Promise.resolve(); + } + return skillAction("/install/plan", { url }, "plan").then(function(body) { + patchSkills({ + plan: body, + selection: (body.candidates || []).filter(function(candidate) { + return candidate.conflict !== true && candidate.problem === void 0; + }).map(function(candidate) { + return candidate.folder; + }) + }); + }, function() {}); + } + function commitInstall() { + if (skills.plan === null) return Promise.resolve(); + return skillAction("/install/commit", { + planId: skills.plan.planId, + folders: skills.selection + }, "commit").then(function(body) { + var installed = Array.isArray(body.installed) ? body.installed : []; + var skipped = Array.isArray(body.skipped) ? body.skipped : []; + patchSkills({ + plan: null, + selection: [] + }); + setNotice({ + tone: skipped.length === 0 ? "ok" : "warn", + text: "已安装 " + String(installed.length) + " 个技能" + (skipped.length === 0 ? "。" : ",跳过 " + String(skipped.length) + " 个:" + skipped.map(function(item) { + return String(item.folder) + "(" + String(item.reason) + ")"; + }).join("、")) + }); + return loadSkills(); + }, function() {}); + } + function cancelInstall() { + if (skills.plan === null) return Promise.resolve(); + return skillAction("/install/cancel", { planId: skills.plan.planId }, "cancel").then(function() { + patchSkills({ + plan: null, + selection: [] + }); + }, function() {}); + } + function mcpCall(method, params) { + var controller = typeof AbortController === "function" ? new AbortController() : null; + var timer = controller === null ? null : setTimeout(function() { + controller.abort(); + }, PROBE_TIMEOUT_MS); + return fetch(MCP_BRIDGE, { + method: "POST", + cache: "no-store", + headers: { + accept: "application/json", + "content-type": "application/json" + }, + body: JSON.stringify({ + method, + params: params === void 0 ? {} : params + }), + signal: controller === null ? void 0 : controller.signal + }).then(function(response) { + if (timer !== null) clearTimeout(timer); + return response.json().then(function(body) { + return { + status: response.status, + body + }; + }, function() { + return { + status: response.status, + body: null + }; + }); + }).catch(function(failure) { + if (timer !== null) clearTimeout(timer); + return { + status: 0, + body: null, + error: messageOf(failure) + }; + }); + } + function probeMcp() { + patchMcp({ + status: "probe", + error: null + }); + return mcpCall("status", {}).then(function(result) { + if (result.status === 200 && result.body !== null && result.body.ok === true && result.body.detail !== void 0) { + patchMcp({ + status: "ok", + items: Array.isArray(result.body.detail.items) ? result.body.detail.items : [], + error: null + }); + return; + } + patchMcp({ + status: "missing", + items: [], + error: result.status === 0 ? "探测超时或连接被拒绝" : "接口返回 HTTP " + String(result.status) + (result.body !== null && typeof result.body.message === "string" ? ":" + result.body.message : "") + }); + }); + } + function mcpAction(label, method, params) { + patchMcp({ busy: label }); + return mcpCall(method, params).then(function(result) { + patchMcp({ busy: "" }); + if (result.status !== 200 || result.body === null || result.body.ok !== true) { + setNotice({ + tone: "bad", + text: "MCP 操作失败:" + (result.body !== null && typeof result.body.message === "string" ? result.body.message : "HTTP " + String(result.status)) + }); + return null; + } + setNotice({ + tone: "ok", + text: "MCP 操作已完成:" + label + }); + return result.body; + }); + } + function loadTools(item) { + return mcpAction("读取工具列表", "toolsList", { + connectorId: item.connectorId, + connectionKey: item.key, + cachedOnly: true + }).then(function(body) { + if (body === null) return; + var list = body.detail !== void 0 && Array.isArray(body.detail.tools) ? body.detail.tools : Array.isArray(body.tools) ? body.tools : []; + patchMcp(function(previous) { + var tools = Object.assign({}, previous.tools); + tools[item.key] = { + items: list, + at: Date.now() + }; + return Object.assign({}, previous, { tools }); + }); + }); + } + function mcpSelected() { + return Array.isArray(mcp.selection) ? mcp.selection : []; + } + function selectMcpMany(list, on) { + var next = mcpSelected().slice(); + list.forEach(function(item) { + var at = next.indexOf(item.key); + if (on === true && at < 0) next.push(item.key); + if (on !== true && at >= 0) next.splice(at, 1); + }); + patchMcp({ + selection: next, + confirmBulk: false + }); + } + function runMcpBulk(actionLabel, method, paramsOf) { + var chosen = mcpSelected(); + var targets = (Array.isArray(mcp.items) ? mcp.items : []).filter(function(item) { + return chosen.indexOf(item.key) >= 0; + }); + if (targets.length === 0) return Promise.resolve(); + patchMcp({ + busy: "bulk", + confirm: null, + confirmBulk: false, + bulk: { + label: actionLabel, + done: 0, + total: targets.length + } + }); + var failures = []; + var chain = Promise.resolve(); + targets.forEach(function(item) { + chain = chain.then(function() { + return mcpCall(method, paramsOf(item)).then(function(result) { + if (!(result.status === 200 && result.body !== null && result.body.ok === true)) { + var reason = result.body !== null && typeof result.body.message === "string" ? result.body.message : "HTTP " + String(result.status); + failures.push(String(item.name === void 0 ? item.key : item.name) + "(" + reason + ")"); + } + patchMcp(function(previous) { + var bulk = previous.bulk === null || previous.bulk === void 0 ? { + label: actionLabel, + done: 0, + total: targets.length + } : Object.assign({}, previous.bulk); + bulk.done = (bulk.done === void 0 ? 0 : bulk.done) + 1; + return Object.assign({}, previous, { bulk }); + }); + }, function(failure) { + failures.push(String(item.name === void 0 ? item.key : item.name) + "(" + messageOf(failure) + ")"); + }); + }); + }); + return chain.then(function() { + patchMcp({ + busy: "", + bulk: null, + selection: [], + confirmBulk: false + }); + setNotice({ + tone: failures.length === 0 ? "ok" : "warn", + text: actionLabel + "完成:成功 " + String(targets.length - failures.length) + " / " + String(targets.length) + " 项" + (failures.length === 0 ? "。" : ";失败:" + failures.join("、")) + }); + return probeMcp(); + }); + } + if (error !== null && snapshot === null) return h$1("div", { style: styles.root }, h$1(Banner, { tone: "bad" }, error), h$1("div", { style: styles.actions }, h$1(Btn, { + label: "重新加载", + onClick: function() { + load(false); + } + }))); + if (snapshot === null || draft === null) return h$1("div", { style: styles.root }, h$1("p", { style: styles.hint }, "正在读取云端记忆插件状态…")); + var reporter = snapshot.reporter === void 0 || snapshot.reporter === null ? {} : snapshot.reporter; + var tokens = snapshot.tokens === void 0 || snapshot.tokens === null ? { + activity: {}, + memory: {}, + summary: {} + } : snapshot.tokens; + var caps = snapshot.capabilities === void 0 || snapshot.capabilities === null ? { features: {} } : snapshot.capabilities; + var features = caps.features === void 0 ? {} : caps.features; + var checkSource = testResult !== null ? testResult : snapshot.lastCheck === null || snapshot.lastCheck === void 0 ? null : snapshot.lastCheck; + var recent = Array.isArray(reporter.recent) ? reporter.recent.slice(0, 8) : []; + var activityToken = tokens.activity === void 0 ? {} : tokens.activity; + var memoryToken = tokens.memory === void 0 ? {} : tokens.memory; + var summaryToken = tokens.summary === void 0 ? {} : tokens.summary; + var tokenHint = function(view) { + if (view.configured !== true) { + if (view.source === "env") return "来自环境变量(DSH_BRAIN_*_TOKEN,绑定当前 origin)"; + return; + } + if (view.source === "env") return "当前:" + String(view.masked) + "(来自环境变量,绑定当前 origin)"; + return "当前:" + String(view.masked); + }; + var checkedAt = checkSource === null ? "—" : formatTime(checkSource.at === void 0 ? checkSource.checkedAt : checkSource.at); + var connectivity = !draft.baseUrlConfigured ? { + tone: "warn", + text: "未配置服务" + } : checkSource === null ? { + tone: "muted", + text: "未测试" + } : findCheck(checkSource, "network") === void 0 ? { + tone: "muted", + text: "未测试" + } : findCheck(checkSource, "network").state === "ok" ? { + tone: "ok", + text: "可达" + } : { + tone: "bad", + text: "不可达" + }; + var header = h$1("div", { + style: styles.section, + key: "header" + }, h$1("div", { style: styles.sectionHead }, h$1("h4", { style: styles.sectionTitle }, h$1("span", { style: { + display: "inline-block", + width: "9px", + height: "9px", + borderRadius: "3px", + background: "linear-gradient(135deg, #6366f1, #8b5cf6)", + marginRight: "8px", + verticalAlign: "1px" + } }), "云端记忆 · v" + String(caps.version === void 0 ? "—" : caps.version)), h$1("div", { style: styles.pills }, h$1(Pill, { + label: "连接", + value: connectivity.text, + tone: connectivity.tone + }), h$1(Pill, { + label: "最后检查", + value: checkedAt, + tone: "muted" + }), dirty ? h$1(Pill, { + label: "草稿", + value: "未保存", + tone: "warn" + }) : null)), h$1("div", { style: styles.pills }, h$1(Pill, { + label: "已上报", + value: reporter.recorded === void 0 ? 0 : reporter.recorded, + tone: "ok" + }), h$1(Pill, { + label: "待重试", + value: reporter.queued === void 0 ? 0 : reporter.queued, + tone: reporter.queued > 0 ? "warn" : "muted" + }), h$1(Pill, { + label: "失败", + value: reporter.failed === void 0 ? 0 : reporter.failed, + tone: reporter.failed > 0 ? "bad" : "muted" + }))); + var tabBar = h$1(Switch, { + key: "tabs", + ariaLabel: "云端记忆管理页签", + value: tab, + onChange: switchTab, + options: [ + { + value: "connection", + label: "连接与上报" + }, + { + value: "skills", + label: "技能管理" + }, + { + value: "mcp", + label: "MCP 管理" + } + ] + }); + var noticeBox = notice === null ? null : h$1(Banner, { + tone: notice.tone, + role: "status", + key: "notice" + }, notice.text); + var connectionTab = null; + if (tab === "connection") { + var changedOrigin = draft.baseUrlConfigured && draft.baseUrlOrigin !== "" && String(draft.baseUrl) !== "" && String(draft.baseUrl).indexOf(draft.baseUrlOrigin) !== 0; + connectionTab = h$1("div", { + style: styles.root, + key: "connection" + }, h$1(Section, { + title: "身份验证", + hint: "两种方式最终更新同一组配置;切换不会清除任何一种方式已保存的凭据,也不会自动发起请求。", + aside: h$1(Switch, { + ariaLabel: "身份验证方式", + value: identity, + onChange: switchIdentity, + options: [{ + value: "account", + label: "账号登录" + }, { + value: "token", + label: "手动令牌" + }] + }) + }, identity === "account" ? h$1("div", { style: styles.nested }, h$1("p", { style: styles.hint }, "用云端面板的普通用户账号登录,插件会用一次性会话换取活动与记忆令牌并写入本机。密码只在这一次请求里使用:不保存、不写日志、不回显。"), h$1("div", { style: styles.grid }, h$1(Field, { + label: "用户名", + value: loginDraft.username, + placeholder: "云端面板的账号", + onChange: function(value) { + setLoginDraft(function(previous) { + return Object.assign({}, previous, { username: value }); + }); + } + }), h$1(Field, { + label: "密码", + type: "password", + value: loginDraft.password, + placeholder: "只用于本次换取令牌", + hint: "成功后自动清空", + onChange: function(value) { + setLoginDraft(function(previous) { + return Object.assign({}, previous, { password: value }); + }); + } + })), h$1("div", { style: styles.actions }, h$1(Btn, { + variant: "primary", + label: busy === "login" ? "登录中…" : "登录并获取令牌", + disabled: busy !== "", + onClick: login + }), loginResult === null ? null : h$1("span", { style: styles.hint }, "当前账号:" + String(loginResult.user !== void 0 && loginResult.user !== null && loginResult.user.username !== void 0 ? loginResult.user.username : "未知") + (loginResult.warning === void 0 ? "" : "(" + String(loginResult.warning) + ")")))) : h$1("div", { style: styles.nested }, h$1("p", { style: styles.hint }, "三个令牌各有独立权限:activity:write 只用于上报,memory:write 同时允许检索与写入,summary:write 用于自动摘要(可选)。空值表示不修改;清除是独立动作。"), h$1("div", { style: styles.grid }, h$1(Field, { + label: "活动上报令牌(activity:write)", + type: "password", + value: tokenDraft.activity, + placeholder: activityToken.configured === true ? "已配置 " + String(activityToken.masked) + ",留空表示不修改" : "未配置", + hint: tokenHint(activityToken), + onChange: function(value) { + patchTokens({ activity: value }); + } + }), h$1(Field, { + label: "记忆读写令牌(memory:write)", + type: "password", + value: tokenDraft.memory, + placeholder: memoryToken.configured === true ? "已配置 " + String(memoryToken.masked) + ",留空表示不修改" : "未配置", + hint: tokenHint(memoryToken), + onChange: function(value) { + patchTokens({ memory: value }); + } + }), h$1(Field, { + label: "摘要令牌(summary:write,可选)", + type: "password", + value: tokenDraft.summary, + placeholder: summaryToken.configured === true ? "已配置 " + String(summaryToken.masked) + ",留空表示不修改" : "未配置", + hint: tokenHint(summaryToken), + onChange: function(value) { + patchTokens({ summary: value }); + } + })), h$1("div", { style: styles.actions }, h$1(Btn, { + label: "清除上报令牌", + variant: "small", + disabled: activityToken.configured !== true || busy !== "", + onClick: function() { + clearToken("activity"); + } + }), h$1(Btn, { + label: "清除记忆令牌", + variant: "small", + disabled: memoryToken.configured !== true || busy !== "", + onClick: function() { + clearToken("memory"); + } + }), h$1(Btn, { + label: "清除摘要令牌", + variant: "small", + disabled: summaryToken.configured !== true || busy !== "", + onClick: function() { + clearToken("summary"); + } + })))), h$1(Section, { + title: "服务地址", + hint: "账号登录会把密码发到该地址:仅接受 HTTPS(本机回环除外)。不同 origin 不会复用旧令牌。" + }, h$1(Field, { + label: "服务地址(baseUrl)", + value: draft.baseUrl, + placeholder: "https://<你的域名>/api/longmemory", + hint: changedOrigin ? "地址已改成另一个 origin(当前 " + draft.baseUrlOrigin + "):保存后旧令牌会被清除,需要重新认证。" : "当前 origin:" + (draft.baseUrlOrigin === "" ? "未配置" : draft.baseUrlOrigin), + onChange: function(value) { + patchDraft({ baseUrl: value }); + } + }), advanced ? h$1("div", { style: styles.nested }, h$1("div", { style: styles.grid }, h$1(Field, { + label: "客户端标识(client_id)", + value: draft.clientId, + onChange: function(value) { + patchDraft({ clientId: value }); + } + }), h$1(Field, { + label: "请求超时(毫秒)", + type: "number", + value: draft.requestTimeoutMs, + onChange: function(value) { + patchDraft({ requestTimeoutMs: value }); + } + })), h$1("div", { style: styles.checkboxRow }, h$1("input", { + type: "checkbox", + checked: draft.enabled, + id: "dsh-brain-enabled", + onChange: function(event) { + patchDraft({ enabled: event.target.checked }); + } + }), h$1("label", { + htmlFor: "dsh-brain-enabled", + style: styles.label + }, "启用每轮活动上报(只上报时间戳、会话 id 与状态,不上传对话内容)")), h$1("div", { style: styles.checkboxRow }, h$1("input", { + type: "checkbox", + checked: draft.summaryEnabled, + id: "dsh-brain-summary", + onChange: function(event) { + patchDraft({ summaryEnabled: event.target.checked }); + } + }), h$1("label", { + htmlFor: "dsh-brain-summary", + style: styles.label + }, "启用自动摘要(会把该轮对话文本上传为待确认提案;与上面的元数据上报是两件事)")), h$1("div", { style: styles.checkboxRow }, h$1("input", { + type: "checkbox", + checked: draft.activityEnabled, + id: "dsh-brain-activity", + onChange: function(event) { + patchDraft({ activityEnabled: event.target.checked }); + } + }), h$1("label", { + htmlFor: "dsh-brain-activity", + style: styles.label + }, "启用活动上报(细粒度开关;未显式设置时跟随总开关)"))) : h$1("div", { style: styles.actions }, h$1(Btn, { + label: "高级设置", + variant: "small", + onClick: toggleAdvanced + }))), h$1(Section, { + title: "记忆配置", + hint: "只影响记忆工具:开关未显式设置时跟随总开关,检索上限只限制单次返回条数。" + }, h$1("div", { style: styles.grid }, h$1(Field, { + label: "单次搜索返回上限(defaultK)", + type: "number", + value: draft.defaultK, + hint: "只限制一次搜索返回多少条,不限制可搜索的历史范围。默认 10。", + onChange: function(value) { + patchDraft({ defaultK: value }); + } + })), h$1("div", { style: styles.checkboxRow }, h$1("input", { + type: "checkbox", + checked: draft.memoryEnabled, + id: "dsh-brain-memory-enabled", + onChange: function(event) { + patchDraft({ memoryEnabled: event.target.checked }); + } + }), h$1("label", { + htmlFor: "dsh-brain-memory-enabled", + style: styles.label + }, "启用记忆工具(memory_search / memory_read / memory_add)"))), features.skillsSync === false ? h$1("div", null) : h$1("div", null, h$1(Section, { + title: "Skill 同步", + hint: "同步由后台定时执行,这里只保存开关与目录;目录留空使用默认位置。" + }, h$1(Field, { + label: "技能目录(skillsDir,可选)", + value: draft.skillsDir, + placeholder: "默认 $DSH_HOME/skills", + hint: "保存后下一次同步生效,无需重启。", + onChange: function(value) { + patchDraft({ skillsDir: value }); + } + }), h$1("div", { style: styles.checkboxRow }, h$1("input", { + type: "checkbox", + checked: draft.skillsSyncEnabled, + id: "dsh-brain-skills-sync", + onChange: function(event) { + patchDraft({ skillsSyncEnabled: event.target.checked }); + } + }), h$1("label", { + htmlFor: "dsh-brain-skills-sync", + style: styles.label + }, "启用云端技能同步(下载云端技能库、上传本机新技能)")))), h$1(Section, { + title: "连接与权限检查", + aside: h$1("div", { style: styles.actions }, h$1(Btn, { + variant: "primary", + label: busy === "save" ? "保存中…" : "保存", + disabled: busy !== "", + onClick: save + }), h$1(Btn, { + label: busy === "test" ? "测试中…" : "测试连接", + disabled: busy !== "", + onClick: test + }), h$1(Btn, { + label: "重试上报队列", + disabled: busy !== "" || reporter.queued === void 0 || reporter.queued === 0, + onClick: flush + })) + }, checkSource === null ? h$1("p", { style: styles.hint }, "还没有检查记录。点「测试连接」做一次只读检查:网络 → 活动权限 → 记忆权限 → 摘要权限。") : h$1("div", { style: styles.pills }, (checkSource.checks || []).map(function(check) { + return h$1(Pill, { + key: String(check.id), + label: String(check.label === void 0 ? check.id : check.label), + value: CHECK_LABELS[check.state] === void 0 ? String(check.state) : CHECK_LABELS[check.state], + tone: CHECK_TONES[check.state] === void 0 ? "muted" : CHECK_TONES[check.state] + }); + })), checkSource === null ? null : h$1("div", { style: { + display: "flex", + flexDirection: "column", + gap: "6px" + } }, (checkSource.checks || []).map(function(check) { + return h$1("div", { + key: "detail-" + String(check.id), + style: Object.assign({}, styles.banner, styles[CHECK_TONES[check.state]] === void 0 ? styles.muted : styles[CHECK_TONES[check.state]]) + }, String(check.label === void 0 ? check.id : check.label) + ":" + String(check.message)); + })), h$1("p", { style: styles.hint }, "鉴权通过只代表令牌有效,不代表已经成功写入一条上报;两者在下面分开显示。")), h$1(Section, { title: "最近上报" }, recent.length === 0 ? h$1("p", { style: styles.hint }, "还没有记录。发一条消息让它跑完一轮,这里就会出现一条。") : h$1("table", { style: styles.table }, h$1("thead", null, h$1("tr", null, h$1("th", { style: styles.th }, "时间"), h$1("th", { style: styles.th }, "结果"), h$1("th", { style: styles.th }, "turn_id"))), h$1("tbody", null, recent.map(function(row, index) { + var outcome = String(row.outcome === void 0 ? "" : row.outcome); + return h$1("tr", { key: String(row.turnId) + ":" + String(index) }, h$1("td", { style: styles.td }, formatTime(row.at)), h$1("td", { style: styles.td }, h$1("span", { style: Object.assign({}, styles.pill, styles[OUTCOME_TONES[outcome]] === void 0 ? styles.muted : styles[OUTCOME_TONES[outcome]]) }, OUTCOME_LABELS[outcome] === void 0 ? outcome : OUTCOME_LABELS[outcome])), h$1("td", { style: Object.assign({}, styles.td, styles.mono) }, String(row.turnId === void 0 ? "" : row.turnId))); + }))), reporter.lastError === void 0 ? null : h$1("p", { style: styles.hint }, "最近一次错误:" + String(reporter.lastError))), h$1("p", { style: styles.hint }, "配置保存在 " + String(snapshot.settingsPath === void 0 ? "本机配置文件" : snapshot.settingsPath) + "。页面地址 /dsh-brain 仍然可用。")); + } + var skillsTab = null; + if (tab === "skills") { + var treeSources = tree.data !== null && Array.isArray(tree.data.sources) ? tree.data.sources : []; + var keyword = String(skills.search).trim().toLowerCase(); + var matchesSkill = function(skill) { + if (keyword === "") return true; + return (String(skill.folder) + " " + String(skill.name) + " " + String(skill.description)).toLowerCase().indexOf(keyword) >= 0; + }; + var visibleGroupsOf = function(source) { + return (Array.isArray(source.groups) ? source.groups : []).map(function(group) { + var visible = (Array.isArray(group.skills) ? group.skills : []).filter(matchesSkill); + return Object.assign({}, group, { visible }); + }).filter(function(group) { + return group.visible.length > 0 || keyword === ""; + }); + }; + var detailBox = null; + if (skills.detail !== null) { + var detail = skills.detail.data; + detailBox = h$1("div", { style: styles.row }, h$1("div", { style: styles.rowHead }, h$1("span", { style: styles.rowTitle }, "详情:" + String(skills.detail.folder)), h$1(Btn, { + label: "关闭", + variant: "small", + onClick: function() { + patchSkills({ detail: null }); + } + })), detail === null ? h$1("p", { style: styles.hint }, skills.detail.error === null ? "正在读取…" : String(skills.detail.error)) : h$1("div", { style: styles.nested }, h$1("div", { style: styles.grid }, h$1(Field, { + label: "目录", + value: String(detail.dir), + onChange: function() {} + }), h$1(Field, { + label: "SKILL.md 哈希", + value: String(detail.skill !== void 0 ? detail.skill.skillSha256 : ""), + onChange: function() {} + })), h$1("p", { style: styles.hint }, "文件:" + String(Array.isArray(detail.files) ? detail.files.length : 0) + " 个,共 " + formatBytes(detail.totalBytes) + (detail.truncated ? "(SKILL.md 过长,已截断显示)" : "")), h$1("pre", { style: styles.code }, String(detail.skillMd === void 0 ? "" : detail.skillMd)), h$1("p", { style: styles.hint }, "文件清单:" + (Array.isArray(detail.files) ? detail.files.map(function(file) { + return String(file.path) + (file.script ? "(脚本)" : ""); + }).join("、") : "")))); + } + var uninstallBox = null; + if (skills.uninstall !== null) uninstallBox = h$1("div", { style: Object.assign({}, styles.row, styles.warn) }, h$1("div", { style: styles.rowHead }, h$1("span", { style: styles.rowTitle }, "卸载确认:" + String(skills.uninstall.folder))), h$1("p", { style: styles.hint }, "将实际删除:" + String(skills.uninstall.dir) + ";影响:" + String(skills.uninstall.files) + " 个文件 / " + formatBytes(skills.uninstall.bytes) + (Array.isArray(skills.uninstall.scripts) && skills.uninstall.scripts.length > 0 ? ";含脚本:" + skills.uninstall.scripts.join("、") : "")), h$1("p", { style: styles.hint }, String(skills.uninstall.note)), h$1("div", { style: styles.actions }, h$1(Btn, { + variant: "danger", + label: tree.busy === "uninstall" ? "删除中…" : "确认删除(不可恢复)", + disabled: tree.busy !== "", + onClick: confirmUninstall + }), h$1(Btn, { + label: "取消", + disabled: tree.busy !== "", + onClick: function() { + patchSkills({ uninstall: null }); + } + }))); + var planBox = null; + if (skills.plan !== null) { + var candidates = Array.isArray(skills.plan.candidates) ? skills.plan.candidates : []; + planBox = h$1("div", { style: styles.row }, h$1("div", { style: styles.rowHead }, h$1("span", { style: styles.rowTitle }, "安装预览:" + String(skills.plan.url))), h$1("p", { style: styles.hint }, "计划 " + String(skills.plan.planId) + " 有效到 " + formatTime(skills.plan.expiresAt) + "。默认不勾选冲突项;只安装勾选的技能,来源中的安装脚本不会执行。"), h$1("div", { style: { + display: "flex", + flexDirection: "column", + gap: "6px" + } }, candidates.map(function(candidate) { + var selected = skills.selection.indexOf(candidate.folder) >= 0; + var blocked = candidate.conflict === true || candidate.problem !== void 0; + return h$1("label", { + key: String(candidate.id), + style: styles.checkboxRow + }, h$1("input", { + type: "checkbox", + checked: selected, + disabled: blocked, + onChange: function(event) { + var next = skills.selection.slice(); + var at = next.indexOf(candidate.folder); + if (event.target.checked && at < 0) next.push(candidate.folder); + if (!event.target.checked && at >= 0) next.splice(at, 1); + patchSkills({ selection: next }); + } + }), h$1("span", { style: { minWidth: 0 } }, h$1("span", { style: { fontWeight: 600 } }, String(candidate.name)), h$1("span", { style: styles.hint }, " · " + String(candidate.folder) + " · " + String(candidate.files) + " 个文件 · " + formatBytes(candidate.bytes) + (candidate.conflict === true ? " · 已存在(" + String(candidate.conflictLocation === "holding" ? "已停用目录" : "技能目录") + "),不覆盖" : "") + (candidate.problem === void 0 ? "" : " · " + String(candidate.problem)) + (Array.isArray(candidate.scripts) && candidate.scripts.length > 0 ? " · 含脚本:" + candidate.scripts.join("、") : "")))); + })), h$1("div", { style: styles.actions }, h$1(Btn, { + variant: "primary", + label: tree.busy === "commit" ? "安装中…" : "安装所选(" + String(skills.selection.length) + ")", + disabled: tree.busy !== "" || skills.selection.length === 0, + onClick: commitInstall + }), h$1(Btn, { + label: "放弃计划", + variant: "small", + disabled: tree.busy !== "", + onClick: cancelInstall + }))); + } + var assignBox = null; + if (tree.assign !== null) { + var assignSource = treeSources.find(function(source) { + return source.id === tree.assign.sourceId; + }); + var assignGroup = assignSource === void 0 ? void 0 : (assignSource.groups || []).find(function(group) { + return group.id === tree.assign.groupId; + }); + var assignable = []; + if (assignSource !== void 0) (assignSource.groups || []).forEach(function(group) { + (group.skills || []).forEach(function(skill) { + if (!assignable.some(function(item) { + return item.folder === skill.folder; + })) assignable.push(skill); + }); + }); + var assignSearch = String(tree.assign.search === void 0 ? "" : tree.assign.search); + var assignNeedle = assignSearch.trim().toLowerCase(); + var filteredAssignable = assignNeedle === "" ? assignable : assignable.filter(function(skill) { + return (String(skill.name || "") + " " + String(skill.folder || "") + " " + String(skill.description || "")).toLowerCase().indexOf(assignNeedle) >= 0; + }); + var selectedCount = tree.assign.selection.length; + var matchedSelected = 0; + filteredAssignable.forEach(function(skill) { + if (tree.assign.selection.indexOf(skill.folder) >= 0) matchedSelected += 1; + }); + function setSelection(next) { + patchTree({ assign: Object.assign({}, tree.assign, { selection: next }) }); + } + function setSelectionFilter(value) { + patchTree({ assign: Object.assign({}, tree.assign, { search: value }) }); + } + function selectMany(list, on) { + var next = tree.assign.selection.slice(); + list.forEach(function(skill) { + var at = next.indexOf(skill.folder); + if (on && at < 0) next.push(skill.folder); + if (!on && at >= 0) next.splice(at, 1); + }); + setSelection(next); + } + assignBox = h$1("div", { style: styles.row }, h$1("div", { style: styles.rowHead }, h$1("span", { style: styles.rowTitle }, "加入分类:" + String(assignGroup === void 0 ? "" : assignGroup.name)), h$1("div", { style: styles.pills }, h$1(Pill, { + label: "可分配", + value: String(assignable.length), + tone: "muted" + }), h$1(Pill, { + label: "匹配", + value: String(filteredAssignable.length), + tone: filteredAssignable.length === 0 ? "muted" : "ok" + }), h$1(Pill, { + label: "已选", + value: String(selectedCount), + tone: selectedCount === 0 ? "muted" : "ok" + }))), h$1("p", { style: styles.hint }, "先搜索再勾选,或直接「全选匹配结果」一次性加入;一个技能只会属于一个分类,取消勾选即回到「未分类」。"), h$1("div", { style: { + display: "flex", + gap: "8px", + alignItems: "center", + flexWrap: "wrap" + } }, h$1("input", { + type: "search", + style: Object.assign({}, styles.input, { + flex: "1 1 200px", + minWidth: 0 + }), + value: assignSearch, + autoFocus: true, + placeholder: "搜索技能:名称 / 目录 / 描述", + "aria-label": "搜索要加入分类的技能", + onChange: function(event) { + setSelectionFilter(event.target.value); + } + }), assignSearch !== "" ? h$1(Btn, { + label: "清空搜索", + variant: "small", + onClick: function() { + setSelectionFilter(""); + } + }) : null), h$1("div", { style: Object.assign({}, styles.actions, { flexWrap: "wrap" }) }, h$1(Btn, { + label: "全选匹配结果(" + String(filteredAssignable.length) + ")", + variant: "small", + disabled: tree.busy !== "" || filteredAssignable.length === 0, + onClick: function() { + selectMany(filteredAssignable, true); + } + }), h$1(Btn, { + label: "全选全部(" + String(assignable.length) + ")", + variant: "small", + disabled: tree.busy !== "" || assignable.length === 0 || selectedCount === assignable.length, + onClick: function() { + selectMany(assignable, true); + } + }), h$1(Btn, { + label: "取消勾选匹配项(" + String(matchedSelected) + ")", + variant: "small", + disabled: tree.busy !== "" || matchedSelected === 0, + onClick: function() { + selectMany(filteredAssignable, false); + } + }), h$1(Btn, { + label: "清空已选", + variant: "small", + disabled: tree.busy !== "" || selectedCount === 0, + onClick: function() { + setSelection([]); + } + })), filteredAssignable.length === 0 ? h$1("p", { style: styles.hint }, assignable.length === 0 ? "当前来源还没有可分配的技能。" : "没有匹配「" + assignSearch + "」的技能。换个关键词,或点「清空搜索」看全部。") : h$1("div", { style: { + display: "flex", + flexDirection: "column", + gap: "4px", + maxHeight: "260px", + overflow: "auto" + } }, filteredAssignable.map(function(skill) { + var checked = tree.assign.selection.indexOf(skill.folder) >= 0; + return h$1("label", { + key: String(skill.folder), + style: styles.checkboxRow + }, h$1("input", { + type: "checkbox", + checked, + onChange: function(event) { + selectMany([skill], event.target.checked === true); + } + }), h$1("span", { style: { minWidth: 0 } }, String(skill.name) + " ", h$1("span", { style: styles.hint }, "· " + String(skill.folder) + (skill.description ? " · " + String(skill.description) : "")))); + })), h$1("div", { style: styles.actions }, h$1(Btn, { + variant: "primary", + label: "保存分类(" + String(selectedCount) + ")", + disabled: tree.busy !== "", + onClick: function() { + saveAssign(); + } + }), h$1(Btn, { + label: "取消", + disabled: tree.busy !== "", + onClick: function() { + patchTree({ assign: null }); + } + }))); + } + var renderSkillRow = function(source, skill) { + var writable = source.writable === true; + return h$1("div", { + key: String(source.id) + ":" + String(skill.folder), + style: styles.skillRow + }, h$1("div", { + style: { + display: "flex", + alignItems: "baseline", + gap: "8px", + minWidth: 0, + flex: "1 1 auto", + cursor: writable ? "pointer" : "default" + }, + title: writable ? "查看详情" : void 0, + onClick: writable ? function() { + openDetail(skill.folder); + } : void 0 + }, h$1("span", { style: Object.assign({}, styles.rowTitle, { flex: "0 0 auto" }) }, String(skill.name)), skill.description === "" ? null : h$1("span", { style: Object.assign({}, styles.hint, styles.clamp1, { minWidth: 0 }) }, String(skill.description))), h$1("div", { style: { + display: "flex", + alignItems: "center", + gap: "6px", + flex: "0 0 auto" + } }, skill.enabledEditable !== true ? h$1("span", { style: styles.hint }, "只读") : null, writable ? h$1(IconBtn, { + glyph: "🗑", + danger: true, + ariaLabel: "卸载 " + String(skill.name), + disabled: tree.busy !== "", + onClick: function() { + planUninstall(skill.folder); + } + }) : null, skill.enabledEditable !== true ? null : h$1(Toggle, { + checked: skill.enabled === true, + disabled: tree.busy !== "", + label: "技能 " + String(skill.name) + " 启停", + onChange: function() { + toggleSkill(skill.folder, skill.enabled !== true); + } + }))); + }; + var renderGroup = function(source, group) { + var open = tree.openGroups[String(source.id) + ":" + group.id] === true; + var renameKey = String(source.id) + ":" + group.id; + var renaming = tree.edit !== null && tree.edit.kind === "rename" && tree.edit.key === renameKey; + var confirming = tree.confirm === renameKey; + var assigning = tree.assign !== null && tree.assign.sourceId === source.id && tree.assign.groupId === group.id; + var groupSkills = group.visible === void 0 ? group.skills || [] : group.visible; + return h$1("div", { + key: renameKey, + style: styles.groupRow + }, h$1("div", { style: styles.rowHead }, h$1("div", { style: { + display: "flex", + alignItems: "center", + gap: "6px", + minWidth: 0, + flexWrap: "wrap" + } }, h$1(Btn, { + label: open ? "▾" : "▸", + variant: "small", + ariaLabel: "展开或收起 " + String(group.name), + onClick: function() { + patchTree({ openGroups: Object.assign({}, tree.openGroups, { [renameKey]: !open }) }); + } + }), h$1("span", { style: styles.folderGlyph }, "📁"), renaming ? h$1("input", { + style: Object.assign({}, styles.input, { width: "160px" }), + value: tree.edit.value, + autoFocus: true, + onChange: function(event) { + patchTree({ edit: Object.assign({}, tree.edit, { value: event.target.value }) }); + } + }) : h$1("span", { style: styles.rowTitle }, String(group.name)), h$1("span", { style: styles.hint }, String(group.total) + " 项 · " + String(group.enabledCount) + "/" + String(group.total) + " 启用"), group.cardOnly === true ? h$1(Pill, { + label: "开关", + value: "仅本卡", + tone: "muted" + }) : null), h$1("div", { style: styles.actions }, renaming ? h$1(Btn, { + label: "保存", + variant: "small", + disabled: tree.busy !== "", + onClick: saveRename + }) : h$1(IconBtn, { + glyph: "+", + ariaLabel: "把技能加入 " + String(group.name), + disabled: tree.busy !== "" || group.id === "unassigned", + onClick: function() { + openAssign(source, group); + } + }), renaming ? h$1(Btn, { + label: "取消", + variant: "small", + onClick: function() { + patchTree({ edit: null }); + } + }) : h$1(IconBtn, { + glyph: "✎", + ariaLabel: "重命名 " + String(group.name), + disabled: tree.busy !== "" || group.id === "unassigned", + onClick: function() { + patchTree({ edit: { + kind: "rename", + key: renameKey, + sourceId: source.id, + groupId: group.id, + value: String(group.name) + } }); + } + }), confirming ? h$1(Btn, { + label: "确认删除分类", + variant: "danger", + disabled: tree.busy !== "", + onClick: function() { + deleteGroup(source.id, group.id); + } + }) : h$1(IconBtn, { + glyph: "🗑", + danger: true, + ariaLabel: "删除分类 " + String(group.name) + "(技能回到未分类)", + disabled: tree.busy !== "" || group.id === "unassigned", + onClick: function() { + patchTree({ confirm: renameKey }); + setNotice({ + tone: "warn", + text: "删除分类只删除分组记录,里面的技能会回到「未分类」,文件不会被删除。再点一次确认。" + }); + } + }), group.enabledEditable === false ? null : h$1(Toggle, { + checked: group.enabled !== false, + disabled: tree.busy !== "", + label: "分类 " + String(group.name) + " 启停", + onChange: function() { + toggleGroup(source.id, group.id, group.enabled === false); + } + }))), assigning ? h$1("div", { style: styles.nested }, assignBox) : null, open ? h$1("div", { style: styles.nested }, groupSkills.length === 0 ? assigning ? null : h$1("p", { style: styles.hint }, group.id === "unassigned" ? "没有未分类的技能。" : "这个分类还没有技能,点「+」加入。") : groupSkills.map(function(skill) { + return renderSkillRow(source, skill); + })) : null); + }; + var dshSource = treeSources.find(function(src) { + return src.writable === true; + }); + skillsTab = h$1("div", { + style: styles.root, + key: "skills" + }, h$1(ImportSection, { onImported: function() { + loadTree(); + loadSkills(); + } }), h$1(Section, { + title: "技能文件夹", + hint: "按软件来源分组;展开来源与分类查看技能。" + }, dshSource === void 0 ? null : h$1("div", { style: styles.statsBar }, h$1("span", { style: styles.stat }, h$1("span", { style: styles.statNum }, String(dshSource.total || 0)), h$1("span", { style: styles.hint }, "个技能")), h$1("span", { style: styles.stat }, h$1("span", { style: styles.statNum }, String(dshSource.enabledCount || 0)), h$1("span", { style: styles.hint }, "个已启用")), h$1("span", { style: styles.statLast }, h$1("span", { style: styles.statNum }, String((dshSource.total || 0) - (dshSource.enabledCount || 0))), h$1("span", { style: styles.hint }, "个已停用"))), h$1("div", { style: { + display: "flex", + gap: "8px", + alignItems: "flex-end", + flexWrap: "wrap" + } }, h$1("div", { style: { + flex: "1 1 220px", + minWidth: 0 + } }, h$1(Field, { + label: "搜索", + value: skills.search, + placeholder: "按名称、目录或描述筛选", + onChange: function(value) { + patchSkills({ search: value }); + } + })), h$1(Btn, { + label: tree.status === "loading" ? "刷新中…" : "刷新", + variant: "small", + disabled: tree.status === "loading", + onClick: function() { + loadTree(); + loadSkills(); + } + })), tree.error === null ? null : h$1(Banner, { tone: "bad" }, tree.error), tree.status === "loading" && tree.data === null ? h$1("p", { style: styles.hint }, "正在读取技能来源…") : null, tree.status === "ready" && treeSources.length === 0 ? h$1("p", { style: styles.hint }, "没有探测到任何技能来源。") : null, dshSource === void 0 ? null : h$1("div", { style: { + display: "flex", + flexDirection: "column", + gap: "2px" + } }, visibleGroupsOf(dshSource).map(function(group) { + return renderGroup(dshSource, group); + }), tree.edit !== null && tree.edit.kind === "create" && tree.edit.sourceId === dshSource.id ? h$1("div", { style: Object.assign({}, styles.row, { + flexDirection: "row", + alignItems: "center", + flexWrap: "wrap", + marginTop: "6px" + }) }, h$1("input", { + style: Object.assign({}, styles.input, { width: "180px" }), + placeholder: "分类名称,例如 QQ-Skill", + value: tree.edit.value, + autoFocus: true, + onChange: function(event) { + patchTree({ edit: Object.assign({}, tree.edit, { value: event.target.value }) }); + } + }), h$1(Btn, { + label: "创建分类", + variant: "primary", + disabled: tree.busy !== "", + onClick: createGroup + }), h$1(Btn, { + label: "取消", + onClick: function() { + patchTree({ edit: null }); + } + })) : h$1("div", { style: { marginTop: "6px" } }, h$1(Btn, { + label: "+ 新建分类", + variant: "small", + disabled: tree.busy !== "", + onClick: function() { + patchTree({ edit: { + kind: "create", + sourceId: dshSource.id, + value: "" + } }); + } + }))), tree.data !== null && tree.data.note !== void 0 ? h$1("p", { style: styles.hint }, String(tree.data.note)) : null), detailBox, uninstallBox, h$1(Section, { + title: "从 Git 仓库安装", + hint: "先克隆到隔离暂存目录并列出候选技能,确认后才写入 DSH 技能目录;不执行仓库里的任何脚本,符号链接不会跟随。仓库地址只接受 https:// 或 file://。" + }, h$1("div", { style: styles.grid }, h$1(Field, { + label: "仓库地址", + value: skills.installUrl, + placeholder: "https://github.com//.git", + onChange: function(value) { + patchSkills({ installUrl: value }); + } + })), h$1("div", { style: styles.actions }, h$1(Btn, { + variant: "primary", + label: skills.busy === "plan" ? "拉取中…" : "拉取并预览", + disabled: skills.busy !== "", + onClick: prepareInstall + })), planBox), h$1(MarketSection, { + capabilities: caps, + onInstalled: function() { + loadTree(); + loadSkills(); + } + }), h$1(SyncResolveSection, { onResolved: function() { + loadSkills(); + } })); + } + var mcpTab = null; + if (tab === "mcp") { + var items = Array.isArray(mcp.items) ? mcp.items : []; + var mcpSearch = String(mcp.search === void 0 ? "" : mcp.search); + var mcpNeedle = mcpSearch.trim().toLowerCase(); + var mcpFilter = String(mcp.filter === void 0 ? "all" : mcp.filter); + var mcpChosen = Array.isArray(mcp.selection) ? mcp.selection : []; + var mcpBulk = mcp.bulk === null || mcp.bulk === void 0 ? null : mcp.bulk; + var stateOf = function(item) { + return String(item.connectionState === void 0 ? "unknown" : item.connectionState); + }; + var isProblem = function(item) { + var state = stateOf(item); + return state !== "connected" && state !== "disabled"; + }; + var haystackOf = function(item) { + var state = stateOf(item); + return [ + item.name, + item.key, + item.transport, + item.authMode, + item.injectionLabel, + MCP_STATE_LABELS[state] === void 0 ? state : MCP_STATE_LABELS[state], + item.lastError, + item.scope === void 0 || item.scope === null ? "" : item.scope.visibility + ].map(function(value) { + return String(value === void 0 || value === null ? "" : value); + }).join(" ").toLowerCase(); + }; + var matchesMcp = function(item) { + if (mcpFilter === "connected" && stateOf(item) !== "connected") return false; + if (mcpFilter === "disabled" && !(item.enabled === false || stateOf(item) === "disabled")) return false; + if (mcpFilter === "problem" && !isProblem(item)) return false; + if (mcpNeedle === "") return true; + return haystackOf(item).indexOf(mcpNeedle) >= 0; + }; + var visibleItems = items.filter(matchesMcp); + var mcpCounts = { + connected: items.filter(function(item) { + return stateOf(item) === "connected"; + }).length, + disabled: items.filter(function(item) { + return item.enabled === false || stateOf(item) === "disabled"; + }).length, + problem: items.filter(isProblem).length + }; + var matchedSelected = visibleItems.filter(function(item) { + return mcpChosen.indexOf(item.key) >= 0; + }).length; + var bridgeBox = null; + if (mcp.status === "missing") bridgeBox = h$1(Banner, { tone: "warn" }, "MCP 管理当前不可用:没有探测到宿主 dsh-mcp-connector 的公开接口(" + MCP_BRIDGE + ")。" + (mcp.error === null ? "" : "原因:" + String(mcp.error)) + "。自定义 MCP Server 的权威位置仍是「设置 → 插件配置 → MCP 服务器配置」,本卡不会修改它的托管配置。"); + mcpTab = h$1("div", { + style: styles.root, + key: "mcp" + }, h$1(Section, { + title: "我的连接", + hint: "数据来自宿主 dsh-mcp-connector 插件(同一 origin 的公开接口);本卡不保存任何 MCP 凭据,也不回显启动命令与密钥。", + aside: h$1(Btn, { + label: mcp.status === "probe" ? "探测中…" : "重新探测", + variant: "small", + disabled: mcp.busy !== "", + onClick: probeMcp + }) + }, bridgeBox, mcp.status === "ok" && items.length === 0 ? h$1("p", { style: styles.hint }, "宿主还没有登记任何 MCP 连接。") : null, mcp.status === "ok" && items.length > 0 ? h$1("div", { style: { + display: "flex", + flexDirection: "column", + gap: "8px" + } }, h$1("div", { style: styles.pills }, h$1(Pill, { + label: "共", + value: String(items.length), + tone: "muted" + }), h$1(Pill, { + label: "匹配", + value: String(visibleItems.length), + tone: visibleItems.length === 0 ? "muted" : "ok" + }), h$1(Pill, { + label: "已选", + value: String(mcpChosen.length), + tone: mcpChosen.length === 0 ? "muted" : "ok" + }), h$1(Pill, { + label: "已连接", + value: String(mcpCounts.connected), + tone: "ok" + }), h$1(Pill, { + label: "异常", + value: String(mcpCounts.problem), + tone: mcpCounts.problem === 0 ? "muted" : "warn" + })), h$1("div", { style: { + display: "flex", + gap: "8px", + alignItems: "center", + flexWrap: "wrap" + } }, h$1("input", { + type: "search", + style: Object.assign({}, styles.input, { + flex: "1 1 220px", + minWidth: 0 + }), + value: mcpSearch, + placeholder: "搜索连接:名称 / key / 传输 / 鉴权 / 状态", + "aria-label": "搜索 MCP 连接", + onChange: function(event) { + patchMcp({ search: event.target.value }); + } + }), mcpSearch !== "" ? h$1(Btn, { + label: "清空搜索", + variant: "small", + onClick: function() { + patchMcp({ search: "" }); + } + }) : null), h$1("div", { style: Object.assign({}, styles.actions, { flexWrap: "wrap" }) }, h$1(Btn, { + label: "筛选:全部(" + String(items.length) + ")", + variant: mcpFilter === "all" ? "primary" : "small", + onClick: function() { + patchMcp({ filter: "all" }); + } + }), h$1(Btn, { + label: "已连接(" + String(mcpCounts.connected) + ")", + variant: mcpFilter === "connected" ? "primary" : "small", + onClick: function() { + patchMcp({ filter: "connected" }); + } + }), h$1(Btn, { + label: "已停用(" + String(mcpCounts.disabled) + ")", + variant: mcpFilter === "disabled" ? "primary" : "small", + onClick: function() { + patchMcp({ filter: "disabled" }); + } + }), h$1(Btn, { + label: "异常(" + String(mcpCounts.problem) + ")", + variant: mcpFilter === "problem" ? "primary" : "small", + onClick: function() { + patchMcp({ filter: "problem" }); + } + })), h$1("div", { style: Object.assign({}, styles.actions, { flexWrap: "wrap" }) }, h$1(Btn, { + label: "全选匹配结果(" + String(visibleItems.length) + ")", + ariaLabel: "全选匹配结果", + variant: "small", + disabled: visibleItems.length === 0, + onClick: function() { + selectMcpMany(visibleItems, true); + } + }), h$1(Btn, { + label: "全选全部(" + String(items.length) + ")", + ariaLabel: "全选全部", + variant: "small", + disabled: items.length === 0 || mcpChosen.length === items.length, + onClick: function() { + selectMcpMany(items, true); + } + }), h$1(Btn, { + label: "取消勾选匹配项(" + String(matchedSelected) + ")", + ariaLabel: "取消勾选匹配项", + variant: "small", + disabled: matchedSelected === 0, + onClick: function() { + selectMcpMany(visibleItems, false); + } + }), h$1(Btn, { + label: "清空已选", + ariaLabel: "清空已选", + variant: "small", + disabled: mcpChosen.length === 0, + onClick: function() { + patchMcp({ + selection: [], + confirmBulk: false + }); + } + })), mcpChosen.length === 0 ? h$1("p", { style: styles.hint }, "勾选左侧连接后,这里会出现批量健康检查、启停、重连与移除。") : h$1("div", { style: Object.assign({}, styles.actions, { flexWrap: "wrap" }) }, h$1("span", { style: styles.hint }, "批量操作(已选 " + String(mcpChosen.length) + "):"), h$1(Btn, { + label: "健康检查", + ariaLabel: "批量健康检查", + variant: "small", + disabled: mcp.busy !== "", + onClick: function() { + runMcpBulk("批量健康检查", "healthCheck", function(item) { + return { connectorId: item.connectorId }; + }); + } + }), h$1(Btn, { + label: "启用", + ariaLabel: "批量启用", + variant: "small", + disabled: mcp.busy !== "", + onClick: function() { + runMcpBulk("批量启用", "setEnabled", function(item) { + return { + key: item.key, + enabled: true + }; + }); + } + }), h$1(Btn, { + label: "停用", + ariaLabel: "批量停用", + variant: "small", + disabled: mcp.busy !== "", + onClick: function() { + runMcpBulk("批量停用", "setEnabled", function(item) { + return { + key: item.key, + enabled: false + }; + }); + } + }), h$1(Btn, { + label: "重连", + ariaLabel: "批量重连", + variant: "small", + disabled: mcp.busy !== "", + onClick: function() { + runMcpBulk("批量重连", "reconnect", function(item) { + return { key: item.key }; + }); + } + }), mcp.confirmBulk === true ? h$1(Btn, { + label: "确认移除(" + String(mcpChosen.length) + ")", + ariaLabel: "确认批量移除", + variant: "danger", + disabled: mcp.busy !== "", + onClick: function() { + runMcpBulk("批量移除", "disconnect", function(item) { + return { key: item.key }; + }); + } + }) : h$1(Btn, { + label: "移除…", + ariaLabel: "批量移除", + variant: "small", + disabled: mcp.busy !== "", + onClick: function() { + patchMcp({ confirmBulk: true }); + setNotice({ + tone: "warn", + text: "批量移除会断开所选 " + String(mcpChosen.length) + " 条连接定义;再点一次「确认移除」才执行。" + }); + } + })), mcpBulk === null ? null : h$1("p", { style: styles.hint }, String(mcpBulk.label) + " 进行中:" + String(mcpBulk.done) + " / " + String(mcpBulk.total)), visibleItems.length === 0 ? h$1("p", { style: styles.hint }, "没有匹配「" + mcpSearch + "」的连接。换个关键词或切回「全部」筛选。") : null, visibleItems.map(function(item) { + var state = stateOf(item); + var tools = mcp.tools[item.key]; + var details = mcp.details === null || mcp.details === void 0 ? {} : mcp.details; + var open = details[item.key] === true; + var label = String(item.name === void 0 ? item.key : item.name); + return h$1("div", { + key: String(item.key), + style: styles.row + }, h$1("div", { style: styles.rowHead }, h$1("label", { style: { + display: "inline-flex", + alignItems: "center", + gap: "8px", + minWidth: 0, + flex: "1 1 auto" + } }, h$1("input", { + type: "checkbox", + checked: mcpChosen.indexOf(item.key) >= 0, + "aria-label": "选择连接 " + label, + onChange: function(event) { + selectMcpMany([item], event.target.checked === true); + } + }), h$1("span", { style: styles.rowTitle }, label), h$1(Pill, { + label: "状态", + value: MCP_STATE_LABELS[state] === void 0 ? state : MCP_STATE_LABELS[state], + tone: state === "connected" ? "ok" : state === "disabled" ? "muted" : "warn" + }), h$1(Pill, { + label: "传输", + value: String(item.transport === void 0 ? "—" : item.transport), + tone: "muted" + })), h$1(Btn, { + label: open ? "收起" : "详情", + ariaLabel: (open ? "收起详情 " : "详情 ") + label, + variant: "small", + onClick: function() { + patchMcp({ details: Object.assign({}, details, { [String(item.key)]: !open }) }); + } + })), open ? h$1("div", { style: { + display: "flex", + flexDirection: "column", + gap: "6px" + } }, h$1("div", { style: styles.pills }, h$1(Pill, { + label: "鉴权", + value: String(item.authMode === void 0 ? "none" : item.authMode), + tone: "muted" + }), h$1(Pill, { + label: "注入", + value: String(item.injectionLabel === void 0 ? "—" : item.injectionLabel), + tone: "muted" + }), h$1(Pill, { + label: "范围", + value: item.scope === void 0 || item.scope === null ? "未知" : String(item.scope.visibility === void 0 ? JSON.stringify(item.scope) : item.scope.visibility), + tone: "muted" + }), h$1(Pill, { + label: "健康检查", + value: formatIso(item.healthCheckedAt), + tone: "muted" + })), item.lastError === null || item.lastError === void 0 ? null : h$1("p", { style: styles.hint }, "最近错误:" + String(item.lastError)), h$1("div", { style: styles.actions }, h$1(Btn, { + label: "健康检查", + ariaLabel: "健康检查 " + label, + variant: "small", + disabled: mcp.busy !== "", + onClick: function() { + mcpAction("健康检查 " + label, "healthCheck", { connectorId: item.connectorId }).then(function() { + probeMcp(); + }); + } + }), h$1(Btn, { + label: "工具列表", + ariaLabel: "工具列表 " + label, + variant: "small", + disabled: mcp.busy !== "", + onClick: function() { + loadTools(item); + } + }), h$1(Btn, { + label: item.enabled === false ? "启用" : "停用", + ariaLabel: "启停 " + label, + variant: "small", + disabled: mcp.busy !== "", + onClick: function() { + mcpAction(item.enabled === false ? "启用连接" : "停用连接", "setEnabled", { + key: item.key, + enabled: item.enabled === false + }).then(function() { + probeMcp(); + }); + } + }), h$1(Btn, { + label: "重连", + ariaLabel: "重连 " + label, + variant: "small", + disabled: mcp.busy !== "", + onClick: function() { + mcpAction("重连", "reconnect", { key: item.key }).then(function() { + probeMcp(); + }); + } + }), mcp.confirm === item.key ? h$1(Btn, { + label: "确认移除", + ariaLabel: "确认移除 " + label, + variant: "danger", + disabled: mcp.busy !== "", + onClick: function() { + mcpAction("移除连接", "disconnect", { key: item.key }).then(function() { + patchMcp({ confirm: null }); + probeMcp(); + }); + } + }) : h$1(Btn, { + label: "移除连接…", + ariaLabel: "移除连接 " + label, + variant: "small", + disabled: mcp.busy !== "", + onClick: function() { + patchMcp({ confirm: item.key }); + setNotice({ + tone: "warn", + text: "再次点击「确认移除」才会断开该连接定义。" + }); + } + })), tools === void 0 ? null : h$1("p", { style: styles.hint }, "工具缓存(" + formatTime(tools.at) + "):" + (tools.items.length === 0 ? "空" : tools.items.map(function(tool) { + return String(tool.name === void 0 ? tool : tool.name); + }).join("、")))) : null); + }), h$1("p", { style: styles.hint }, "工具可见不等于业务可调用:连接状态与工具缓存分开显示。")) : null), h$1(McpCustomSection, { onChanged: probeMcp }), h$1(McpConfigSection, { + items, + onChanged: probeMcp + })); + } + return h$1("div", { style: styles.root }, header, tabBar, noticeBox, error === null ? null : h$1(Banner, { tone: "bad" }, error), tab === "connection" ? connectionTab : tab === "skills" ? skillsTab : mcpTab); + } + // src-client/index.ts + const h = react.createElement; + const inject = ["slots"]; + function apply(ctx) { + const card = function() { + return h(CardBoundary, null, h(SettingsCard, null)); + }; + const register = function(seat, options) { + try { + ctx.slots.inject(seat, function() { + return ctx.slots.register(options, card); + }); + } catch (failure) { + if (typeof console !== "undefined") console.warn("[dsh-brain] slot " + seat + " unavailable: " + messageOf(failure)); + } + }; + register("settings.section", { + name: "settings.section", + id: "dsh-brain", + order: 100, + label: function() { + return "云端记忆"; + } + }); + register("plugins.bundle.config", { + name: "plugins.bundle.config", + key: "dsh-brain" + }); + } + const name = "dsh-brain"; + exports.apply = apply; + exports.inject = inject; + exports.name = name; + return module.exports; + } +}); diff --git a/package.json b/package.json index bce0598..a87a2ff 100644 --- a/package.json +++ b/package.json @@ -38,10 +38,11 @@ } }, "scripts": { - "build": "tsc -p tsconfig.build.json", + "build": "tsc -p tsconfig.build.json && tsdown", + "build:client": "tsdown", "verify": "node scripts/verify-all.mjs", - "typecheck": "tsc -p tsconfig.json --noEmit", - "prepack": "tsc -p tsconfig.build.json", + "typecheck": "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.client.json --noEmit", + "prepack": "tsc -p tsconfig.build.json && tsdown", "prepublishOnly": "node scripts/pack-guard.mjs", "test": "node scripts/verify-all.mjs" }, diff --git a/src-client/SettingsCard.tsx b/src-client/SettingsCard.tsx new file mode 100644 index 0000000..26afe73 --- /dev/null +++ b/src-client/SettingsCard.tsx @@ -0,0 +1,2156 @@ +// dsh-brain SettingsCard — the original settings surface, kept behavior-equivalent. +import * as React from 'react' +import { API, request, postJson, messageOf, bodyError, statusText, pickSettings, emptyTokens } from './api' +import { formatTime, formatIso, formatBytes, shortHash, readStored, store, OUTCOME_LABELS, OUTCOME_TONES, ORIGIN_LABELS, CHECK_TONES, CHECK_LABELS, MCP_STATE_LABELS } from './format' +import { styles } from './styles' +import { Pill, Banner, Field, Btn, IconBtn, Switch, Toggle, Section, CardBoundary } from './components' +import { MCP_BRIDGE, PROBE_TIMEOUT_MS, POLL_MS } from './bridge' +import { MarketSection } from './sections/MarketSection' +import { SyncResolveSection } from './sections/SyncResolveSection' +import { McpCustomSection } from './sections/McpCustomSection' +import { McpConfigSection } from './sections/McpConfigSection' +import { ImportSection } from './sections/ImportSection' + +const h = React.createElement + +export function findCheck(source, id) { + if (source === null || source === undefined || !Array.isArray(source.checks)) return undefined + for (var index = 0; index < source.checks.length; index += 1) { + if (source.checks[index] !== null && typeof source.checks[index] === 'object' && source.checks[index].id === id) { + return source.checks[index] + } + } + return undefined +} + +// ---- P3: skill market ------------------------------------------------- + +export function SettingsCard() { + var snapshotState = React.useState(null) + var snapshot = snapshotState[0] + var setSnapshot = snapshotState[1] + + var draftState = React.useState(null) + var draft = draftState[0] + var setDraft = draftState[1] + + var tokenState = React.useState(emptyTokens()) + var tokenDraft = tokenState[0] + var setTokenDraft = tokenState[1] + + var identityState = React.useState(readStored('identity', 'account')) + var identity = identityState[0] + var setIdentity = identityState[1] + + var loginState = React.useState({ username: '', password: '' }) + var loginDraft = loginState[0] + var setLoginDraft = loginState[1] + + var loginResultState = React.useState(null) + var loginResult = loginResultState[0] + var setLoginResult = loginResultState[1] + + var errorState = React.useState(null) + var error = errorState[0] + var setError = errorState[1] + + var noticeState = React.useState(null) + var notice = noticeState[0] + var setNotice = noticeState[1] + + var testState = React.useState(null) + var testResult = testState[0] + var setTestResult = testState[1] + + var busyState = React.useState('') + var busy = busyState[0] + var setBusy = busyState[1] + + var tabState = React.useState(readStored('tab', 'connection')) + var tab = tabState[0] + var setTab = tabState[1] + + var advancedState = React.useState(readStored('advanced', 'closed') === 'open') + var advanced = advancedState[0] + var setAdvanced = advancedState[1] + + var skillsState = React.useState({ status: 'idle', data: null, error: null, detail: null, uninstall: null, plan: null, selection: [], installUrl: '', search: '', filter: 'all', busy: '' }) + var skills = skillsState[0] + var setSkills = skillsState[1] + + var mcpState = React.useState({ status: 'idle', items: [], error: null, tools: {}, busy: '', confirm: null, search: '', filter: 'all', selection: [], bulk: null, confirmBulk: false, details: {} }) + var mcp = mcpState[0] + var setMcp = mcpState[1] + + var treeState = React.useState({ status: 'idle', data: null, error: null, busy: '', expanded: {}, openGroups: {}, edit: null, confirm: null, assign: null }) + var tree = treeState[0] + var setTree = treeState[1] + + function patchSkills(patch) { + setSkills(function (previous) { + return Object.assign({}, previous, patch) + }) + } + + function patchMcp(patch) { + setMcp(function (previous) { + return Object.assign({}, previous, patch) + }) + } + + function patchTree(patch) { + setTree(function (previous) { + return Object.assign({}, previous, patch) + }) + } + + var load = React.useCallback( + function (keepDraft) { + return request(API.status) + .then(function (result) { + if (result.status !== 200 || result.body === null || result.body.ok !== true) { + setError('读取插件状态失败(HTTP ' + String(result.status) + '),请重启 DSH 后再试。') + return + } + setError(null) + setSnapshot(result.body) + if (keepDraft !== true) setDraft(pickSettings(result.body.settings)) + }) + .catch(function (failure) { + setError('无法连接插件:' + messageOf(failure)) + }) + }, + [], + ) + + React.useEffect( + function () { + load(false) + var timer = setInterval(function () { + if (typeof document !== 'undefined' && document.visibilityState === 'hidden') return + load(true) + }, POLL_MS) + return function () { + clearInterval(timer) + } + }, + [load], + ) + + var dirty = snapshot !== null && draft !== null && JSON.stringify(draft) !== JSON.stringify(pickSettings(snapshot.settings)) + + React.useEffect( + function () { + if (!dirty) return undefined + var guard = function (event) { + event.preventDefault() + event.returnValue = '有未保存的改动' + return event.returnValue + } + window.addEventListener('beforeunload', guard) + return function () { + window.removeEventListener('beforeunload', guard) + } + }, + [dirty], + ) + + function patchDraft(patch) { + setDraft(function (previous) { + return Object.assign({}, previous, patch) + }) + } + + function patchTokens(patch) { + setTokenDraft(function (previous) { + return Object.assign({}, previous, patch) + }) + } + + function run(label, task) { + setBusy(label) + setNotice(null) + return Promise.resolve() + .then(task) + .catch(function (failure) { + setNotice({ tone: 'bad', text: messageOf(failure) }) + }) + .then(function () { + setBusy('') + }) + } + + function save() { + return run('save', function () { + var payload: any = { + enabled: draft.enabled, + baseUrl: draft.baseUrl, + clientId: draft.clientId, + requestTimeoutMs: Number(draft.requestTimeoutMs), + defaultK: Number(draft.defaultK), + summaryEnabled: draft.summaryEnabled, + activityEnabled: draft.activityEnabled, + memoryEnabled: draft.memoryEnabled, + skillsSyncEnabled: draft.skillsSyncEnabled, + skillsDir: String(draft.skillsDir).trim(), + } + if (tokenDraft.activity.trim() !== '') payload.activityToken = tokenDraft.activity.trim() + if (tokenDraft.memory.trim() !== '') payload.memoryToken = tokenDraft.memory.trim() + if (tokenDraft.summary.trim() !== '') payload.summaryToken = tokenDraft.summary.trim() + return request(API.config, { method: 'PUT', body: JSON.stringify(payload) }).then(function (result) { + if (result.status !== 200 || result.body === null || result.body.ok !== true) { + throw new Error('保存失败:' + bodyError(result)) + } + setTokenDraft(emptyTokens()) + var cleared = Array.isArray(result.body.credentialsCleared) ? result.body.credentialsCleared : [] + setNotice( + cleared.length === 0 + ? { tone: 'ok', text: '已保存到本机配置文件。' } + : { tone: 'warn', text: '服务地址的 origin 已变更,旧地址的令牌已清除(' + cleared.join('、') + '),请重新登录或填写新令牌。' }, + ) + setSnapshot(result.body) + setDraft(pickSettings(result.body.settings)) + }) + }) + } + + function clearToken(which) { + return run('clear-' + which, function () { + var payload = which === 'activity' ? { clearActivityToken: true } : which === 'memory' ? { clearMemoryToken: true } : { clearSummaryToken: true } + return request(API.config, { method: 'PUT', body: JSON.stringify(payload) }).then(function (result) { + if (result.status !== 200 || result.body === null || result.body.ok !== true) { + throw new Error('清除令牌失败:' + bodyError(result)) + } + setNotice({ tone: 'ok', text: '令牌已清除。' }) + setSnapshot(result.body) + setDraft(pickSettings(result.body.settings)) + }) + }) + } + + function test() { + return run('test', function () { + setTestResult(null) + return request(API.test, { method: 'POST', body: '{}' }).then(function (result) { + if (result.status !== 200 || result.body === null || result.body.ok !== true) { + throw new Error('测试失败(HTTP ' + String(result.status) + ')') + } + setTestResult(result.body) + load(true) + }) + }) + } + + function flush() { + return run('flush', function () { + return request(API.flush, { method: 'POST', body: '{}' }).then(function (result) { + if (result.status !== 200 || result.body === null || result.body.ok !== true) { + throw new Error('重试失败(HTTP ' + String(result.status) + ')') + } + setNotice({ tone: 'ok', text: '已重试 ' + String(result.body.sent) + ' 条,剩余 ' + String(result.body.remaining) + ' 条。' }) + load(true) + }) + }) + } + + function login() { + return run('login', function () { + setLoginResult(null) + var payload = { username: loginDraft.username.trim(), password: loginDraft.password } + if (payload.username === '' || payload.password === '') { + throw new Error('请填写用户名和密码') + } + return request(API.login, { method: 'POST', body: JSON.stringify(payload) }).then(function (result) { + if (result.status !== 200 || result.body === null || result.body.ok !== true) { + throw new Error('登录失败:' + bodyError(result)) + } + // Drop the password the moment the exchange succeeded. + setLoginDraft({ username: payload.username, password: '' }) + setLoginResult(result.body.login || null) + setNotice({ + tone: result.body.login !== null && result.body.login !== undefined && result.body.login.partial === true ? 'warn' : 'ok', + text: '登录成功,已写入可用的令牌。', + }) + setSnapshot(result.body) + setDraft(pickSettings(result.body.settings)) + }) + }) + } + + function switchIdentity(next) { + setIdentity(next) + store('identity', next) + setLoginResult(null) + setNotice(null) + } + + function switchTab(next) { + setTab(next) + store('tab', next) + if (next === 'skills' && skills.status === 'idle') loadSkills() + if (next === 'skills' && tree.status === 'idle') loadTree() + if (next === 'mcp' && mcp.status === 'idle') probeMcp() + } + + function toggleAdvanced() { + setAdvanced(function (previous) { + store('advanced', previous ? 'closed' : 'open') + return !previous + }) + } + + // A panel that opens (or reopens) on the skills/MCP tab must fetch on its + // own: relying on the click handler alone would leave a stale empty view + // after a reload, when the tab is restored from local storage. + React.useEffect( + function () { + if (tab === 'skills' && skills.status === 'idle') void loadSkills() + if (tab === 'skills' && tree.status === 'idle') void loadTree() + if (tab === 'mcp' && mcp.status === 'idle') void probeMcp() + }, + [tab, skills.status, mcp.status, tree.status], + ) + + // ---- skills ------------------------------------------------------- + + function loadSkills() { + patchSkills({ status: 'loading', error: null }) + return request(API.skills + '/api') + .then(function (result) { + if (result.status !== 200 || result.body === null || result.body.ok !== true) { + patchSkills({ status: 'error', error: '读取技能列表失败:' + statusText(result.status) }) + return + } + patchSkills({ status: 'ready', data: result.body, error: null }) + }) + .catch(function (failure) { + patchSkills({ status: 'error', error: '无法读取技能列表:' + messageOf(failure) }) + }) + } + + function skillAction(path, payload, label) { + patchSkills({ busy: label }) + return postJson(API.skills + path, payload) + .then(function (result) { + patchSkills({ busy: '' }) + if (result.status !== 200 || result.body === null || result.body.ok !== true) { + throw new Error(bodyError(result, statusText(result.status))) + } + return result.body + }) + .catch(function (failure) { + patchSkills({ busy: '' }) + patchSkills({ error: messageOf(failure) }) + setNotice({ tone: 'bad', text: messageOf(failure) }) + throw failure + }) + } + + // ---- skill tree (sources → categories → skills) -------------------- + + function loadTree() { + patchTree({ status: 'loading', error: null }) + return request(API.skills + '/sources') + .then(function (result) { + if (result.status !== 200 || result.body === null || result.body.ok !== true) { + patchTree({ status: 'error', error: '读取技能来源失败:' + statusText(result.status) }) + return + } + patchTree({ status: 'ready', data: result.body, error: null }) + }) + .catch(function (failure) { + patchTree({ status: 'error', error: '无法读取技能来源:' + messageOf(failure) }) + }) + } + + function treeAction(path, payload, label) { + patchTree({ busy: label }) + return postJson(API.skills + path, payload) + .then(function (result) { + patchTree({ busy: '' }) + if (result.status !== 200 || result.body === null || result.body.ok !== true) { + throw new Error(bodyError(result, statusText(result.status))) + } + return result.body + }) + .catch(function (failure) { + patchTree({ busy: '' }) + setNotice({ tone: 'bad', text: messageOf(failure) }) + return null + }) + } + + function createGroup() { + if (tree.edit === null) return Promise.resolve() + var name = String(tree.edit.value === undefined ? '' : tree.edit.value).trim() + if (name === '') { + setNotice({ tone: 'bad', text: '请填写分类名。' }) + return Promise.resolve() + } + return treeAction('/groups/create', { sourceId: tree.edit.sourceId, name: name }, 'create').then(function (body) { + if (body === null) return + patchTree({ edit: null }) + setNotice({ tone: 'ok', text: '已创建分类「' + String(body.name) + '」。' }) + return loadTree() + }) + } + + function saveRename() { + if (tree.edit === null) return Promise.resolve() + return treeAction('/groups/rename', { sourceId: tree.edit.sourceId, groupId: tree.edit.groupId, name: String(tree.edit.value).trim() }, 'rename').then(function (body) { + if (body === null) return + patchTree({ edit: null }) + return loadTree() + }) + } + + function deleteGroup(sourceId, groupId) { + return treeAction('/groups/delete', { sourceId: sourceId, groupId: groupId }, 'delete').then(function (body) { + if (body === null) return + patchTree({ confirm: null }) + setNotice({ tone: 'ok', text: '分类已删除,' + String(body.released) + ' 个技能回到「未分类」;没有删除任何文件。' }) + return loadTree() + }) + } + + function openAssign(source, group) { + var renameKey = String(source.id) + ':' + group.id + if (tree.assign !== null && tree.assign.sourceId === source.id && tree.assign.groupId === group.id) { + patchTree({ assign: null }) + return + } + patchTree({ + openGroups: Object.assign({}, tree.openGroups, { [renameKey]: true }), + assign: { + sourceId: source.id, + groupId: group.id, + search: '', + selection: (Array.isArray(group.skills) ? group.skills : []).map(function (skill) { + return skill.folder + }), + }, + }) + } + + function saveAssign() { + if (tree.assign === null) return Promise.resolve() + return treeAction('/groups/assign', { sourceId: tree.assign.sourceId, groupId: tree.assign.groupId, folders: tree.assign.selection }, 'assign').then(function (body) { + if (body === null) return + patchTree({ assign: null }) + return loadTree() + }) + } + + function toggleGroup(sourceId, groupId, enabled) { + return treeAction('/groups/toggle', { sourceId: sourceId, groupId: groupId, enabled: enabled }, 'toggle-group').then(function (body) { + if (body === null) return + if (body.cardOnly === true) { + setNotice({ tone: 'warn', text: '这个来源由对应软件自行加载:开关只记录在本卡,不影响它自己的加载。' }) + } else { + var failed = Array.isArray(body.failed) ? body.failed : [] + setNotice({ + tone: failed.length === 0 ? 'ok' : 'warn', + text: '已' + (enabled ? '启用' : '停用') + ' ' + String(Array.isArray(body.applied) ? body.applied.length : 0) + ' 项' + (failed.length === 0 ? '(新会话生效)。' : ',' + String(failed.length) + ' 项失败:' + failed.map(function (item) { return String(item.folder) + '(' + String(item.reason) + ')' }).join('、')), + }) + } + return loadTree() + }) + } + + function toggleSource(sourceId, enabled) { + return treeAction('/source/toggle', { sourceId: sourceId, enabled: enabled }, 'toggle-source').then(function (body) { + if (body === null) return + setNotice( + body.cardOnly === true + ? { tone: 'warn', text: '这个来源由对应软件自行加载:开关只记录在本卡。' } + : { tone: 'ok', text: '已' + (enabled ? '启用' : '停用') + ' ' + String(body.applied) + ' 个技能(新会话生效)。' }, + ) + return loadTree() + }) + } + + function revealSource(sourceId) { + return treeAction('/source/reveal', { sourceId: sourceId }, 'reveal') + } + + function openDetail(folder) { + patchSkills({ detail: { folder: folder, data: null, error: null } }) + return skillAction('/detail', { folder: folder }, 'detail').then( + function (body) { + patchSkills({ detail: { folder: folder, data: body, error: null } }) + }, + function () { + patchSkills({ detail: { folder: folder, data: null, error: '无法读取详情' } }) + }, + ) + } + + function toggleSkill(folder, enabled) { + return skillAction('/toggle', { folder: folder, enabled: enabled }, 'toggle').then(function (body) { + setNotice({ tone: 'ok', text: body.note === undefined ? '已更新。' : String(body.note) }) + return loadSkills() + }, function () {}) + } + + function planUninstall(folder) { + return skillAction('/uninstall/plan', { folder: folder }, 'uninstall').then(function (body) { + patchSkills({ uninstall: body }) + }, function () {}) + } + + function confirmUninstall() { + if (skills.uninstall === null) return Promise.resolve() + return skillAction('/remove', { folder: skills.uninstall.folder, token: skills.uninstall.token, confirmed: true }, 'remove').then(function (body) { + patchSkills({ uninstall: null }) + setNotice({ tone: 'ok', text: '已删除本机技能 ' + String(body.folder) + '(' + String(body.files) + ' 个文件)。' }) + return loadSkills() + }, function () {}) + } + + function prepareInstall() { + var url = String(skills.installUrl).trim() + if (url === '') { + setNotice({ tone: 'bad', text: '请填写 git 仓库地址。' }) + return Promise.resolve() + } + return skillAction('/install/plan', { url: url }, 'plan').then(function (body) { + var selection = (body.candidates || []) + .filter(function (candidate) { + return candidate.conflict !== true && candidate.problem === undefined + }) + .map(function (candidate) { + return candidate.folder + }) + patchSkills({ plan: body, selection: selection }) + }, function () {}) + } + + function commitInstall() { + if (skills.plan === null) return Promise.resolve() + return skillAction('/install/commit', { planId: skills.plan.planId, folders: skills.selection }, 'commit').then(function (body) { + var installed = Array.isArray(body.installed) ? body.installed : [] + var skipped = Array.isArray(body.skipped) ? body.skipped : [] + patchSkills({ plan: null, selection: [] }) + setNotice({ + tone: skipped.length === 0 ? 'ok' : 'warn', + text: '已安装 ' + String(installed.length) + ' 个技能' + (skipped.length === 0 ? '。' : ',跳过 ' + String(skipped.length) + ' 个:' + skipped.map(function (item) { return String(item.folder) + '(' + String(item.reason) + ')' }).join('、')), + }) + return loadSkills() + }, function () {}) + } + + function cancelInstall() { + if (skills.plan === null) return Promise.resolve() + return skillAction('/install/cancel', { planId: skills.plan.planId }, 'cancel').then(function () { + patchSkills({ plan: null, selection: [] }) + }, function () {}) + } + + // ---- mcp ---------------------------------------------------------- + + function mcpCall(method, params) { + var controller = typeof AbortController === 'function' ? new AbortController() : null + var timer = controller === null ? null : setTimeout(function () { + controller.abort() + }, PROBE_TIMEOUT_MS) + return fetch(MCP_BRIDGE, { + method: 'POST', + cache: 'no-store', + headers: { accept: 'application/json', 'content-type': 'application/json' }, + body: JSON.stringify({ method: method, params: params === undefined ? {} : params }), + signal: controller === null ? undefined : controller.signal, + }) + .then(function (response) { + if (timer !== null) clearTimeout(timer) + return response.json().then( + function (body) { + return { status: response.status, body: body } + }, + function () { + return { status: response.status, body: null } + }, + ) + }) + .catch(function (failure) { + if (timer !== null) clearTimeout(timer) + return { status: 0, body: null, error: messageOf(failure) } + }) + } + + function probeMcp() { + patchMcp({ status: 'probe', error: null }) + return mcpCall('status', {}).then(function (result) { + if (result.status === 200 && result.body !== null && result.body.ok === true && result.body.detail !== undefined) { + patchMcp({ status: 'ok', items: Array.isArray(result.body.detail.items) ? result.body.detail.items : [], error: null }) + return + } + patchMcp({ + status: 'missing', + items: [], + error: result.status === 0 ? '探测超时或连接被拒绝' : '接口返回 HTTP ' + String(result.status) + (result.body !== null && typeof result.body.message === 'string' ? ':' + result.body.message : ''), + }) + }) + } + + function mcpAction(label, method, params) { + patchMcp({ busy: label }) + return mcpCall(method, params).then(function (result) { + patchMcp({ busy: '' }) + if (result.status !== 200 || result.body === null || result.body.ok !== true) { + var reason = result.body !== null && typeof result.body.message === 'string' ? result.body.message : 'HTTP ' + String(result.status) + setNotice({ tone: 'bad', text: 'MCP 操作失败:' + reason }) + return null + } + setNotice({ tone: 'ok', text: 'MCP 操作已完成:' + label }) + return result.body + }) + } + + function loadTools(item) { + return mcpAction('读取工具列表', 'toolsList', { connectorId: item.connectorId, connectionKey: item.key, cachedOnly: true }).then(function (body) { + if (body === null) return + var list = body.detail !== undefined && Array.isArray(body.detail.tools) ? body.detail.tools : Array.isArray(body.tools) ? body.tools : [] + patchMcp(function (previous) { + var tools = Object.assign({}, previous.tools) + tools[item.key] = { items: list, at: Date.now() } + return Object.assign({}, previous, { tools: tools }) + }) + }) + } + + function mcpSelected() { + return Array.isArray(mcp.selection) ? mcp.selection : [] + } + + function selectMcpMany(list, on) { + var next = mcpSelected().slice() + list.forEach(function (item) { + var at = next.indexOf(item.key) + if (on === true && at < 0) next.push(item.key) + if (on !== true && at >= 0) next.splice(at, 1) + }) + patchMcp({ selection: next, confirmBulk: false }) + } + + function runMcpBulk(actionLabel, method, paramsOf) { + var chosen = mcpSelected() + var targets = (Array.isArray(mcp.items) ? mcp.items : []).filter(function (item) { + return chosen.indexOf(item.key) >= 0 + }) + if (targets.length === 0) return Promise.resolve() + patchMcp({ busy: 'bulk', confirm: null, confirmBulk: false, bulk: { label: actionLabel, done: 0, total: targets.length } }) + var failures = [] + var chain = Promise.resolve() + targets.forEach(function (item) { + chain = chain.then(function () { + return mcpCall(method, paramsOf(item)).then( + function (result) { + if (!(result.status === 200 && result.body !== null && result.body.ok === true)) { + var reason = result.body !== null && typeof result.body.message === 'string' ? result.body.message : 'HTTP ' + String(result.status) + failures.push(String(item.name === undefined ? item.key : item.name) + '(' + reason + ')') + } + patchMcp(function (previous) { + var bulk = previous.bulk === null || previous.bulk === undefined ? { label: actionLabel, done: 0, total: targets.length } : Object.assign({}, previous.bulk) + bulk.done = (bulk.done === undefined ? 0 : bulk.done) + 1 + return Object.assign({}, previous, { bulk: bulk }) + }) + }, + function (failure) { + failures.push(String(item.name === undefined ? item.key : item.name) + '(' + messageOf(failure) + ')') + }, + ) + }) + }) + return chain.then(function () { + patchMcp({ busy: '', bulk: null, selection: [], confirmBulk: false }) + setNotice({ + tone: failures.length === 0 ? 'ok' : 'warn', + text: actionLabel + '完成:成功 ' + String(targets.length - failures.length) + ' / ' + String(targets.length) + ' 项' + (failures.length === 0 ? '。' : ';失败:' + failures.join('、')), + }) + return probeMcp() + }) + } + + // ---- render ------------------------------------------------------- + + if (error !== null && snapshot === null) { + return h( + 'div', + { style: styles.root }, + h(Banner, { tone: 'bad' }, error), + h( + 'div', + { style: styles.actions }, + h(Btn, { + label: '重新加载', + onClick: function () { + load(false) + }, + }), + ), + ) + } + + if (snapshot === null || draft === null) { + return h('div', { style: styles.root }, h('p', { style: styles.hint }, '正在读取云端记忆插件状态…')) + } + + var reporter = snapshot.reporter === undefined || snapshot.reporter === null ? {} : snapshot.reporter + var tokens = snapshot.tokens === undefined || snapshot.tokens === null ? { activity: {}, memory: {}, summary: {} } : snapshot.tokens + var caps = snapshot.capabilities === undefined || snapshot.capabilities === null ? { features: {} } : snapshot.capabilities + var features = caps.features === undefined ? {} : caps.features + var checkSource = testResult !== null ? testResult : snapshot.lastCheck === null || snapshot.lastCheck === undefined ? null : snapshot.lastCheck + var recent = Array.isArray(reporter.recent) ? reporter.recent.slice(0, 8) : [] + var activityToken = tokens.activity === undefined ? {} : tokens.activity + var memoryToken = tokens.memory === undefined ? {} : tokens.memory + var summaryToken = tokens.summary === undefined ? {} : tokens.summary + // Token source label: where the effective credential comes from. An env + // token only applies when its origin binding matches the service origin. + var tokenHint = function (view) { + if (view.configured !== true) { + if (view.source === 'env') return '来自环境变量(DSH_BRAIN_*_TOKEN,绑定当前 origin)' + return undefined + } + if (view.source === 'env') return '当前:' + String(view.masked) + '(来自环境变量,绑定当前 origin)' + return '当前:' + String(view.masked) + } + var checkedAt = checkSource === null ? '—' : formatTime(checkSource.at === undefined ? checkSource.checkedAt : checkSource.at) + + var connectivity = !draft.baseUrlConfigured + ? { tone: 'warn', text: '未配置服务' } + : checkSource === null + ? { tone: 'muted', text: '未测试' } + : findCheck(checkSource, 'network') === undefined + ? { tone: 'muted', text: '未测试' } + : findCheck(checkSource, 'network').state === 'ok' + ? { tone: 'ok', text: '可达' } + : { tone: 'bad', text: '不可达' } + + var header = h( + 'div', + { style: styles.section, key: 'header' }, + h( + 'div', + { style: styles.sectionHead }, + h('h4', { style: styles.sectionTitle }, h('span', { style: { display: 'inline-block', width: '9px', height: '9px', borderRadius: '3px', background: 'linear-gradient(135deg, #6366f1, #8b5cf6)', marginRight: '8px', verticalAlign: '1px' } }), '云端记忆 · v' + String(caps.version === undefined ? '—' : caps.version)), + h( + 'div', + { style: styles.pills }, + h(Pill, { label: '连接', value: connectivity.text, tone: connectivity.tone }), + h(Pill, { label: '最后检查', value: checkedAt, tone: 'muted' }), + dirty ? h(Pill, { label: '草稿', value: '未保存', tone: 'warn' }) : null, + ), + ), + h( + 'div', + { style: styles.pills }, + h(Pill, { label: '已上报', value: reporter.recorded === undefined ? 0 : reporter.recorded, tone: 'ok' }), + h(Pill, { label: '待重试', value: reporter.queued === undefined ? 0 : reporter.queued, tone: reporter.queued > 0 ? 'warn' : 'muted' }), + h(Pill, { label: '失败', value: reporter.failed === undefined ? 0 : reporter.failed, tone: reporter.failed > 0 ? 'bad' : 'muted' }), + ), + ) + + var tabBar = h(Switch, { + key: 'tabs', + ariaLabel: '云端记忆管理页签', + value: tab, + onChange: switchTab, + options: [ + { value: 'connection', label: '连接与上报' }, + { value: 'skills', label: '技能管理' }, + { value: 'mcp', label: 'MCP 管理' }, + ], + }) + + var noticeBox = notice === null ? null : h(Banner, { tone: notice.tone, role: 'status', key: 'notice' }, notice.text) + + var connectionTab = null + if (tab === 'connection') { + var changedOrigin = draft.baseUrlConfigured && draft.baseUrlOrigin !== '' && String(draft.baseUrl) !== '' && String(draft.baseUrl).indexOf(draft.baseUrlOrigin) !== 0 + connectionTab = h( + 'div', + { style: styles.root, key: 'connection' }, + + h( + Section, + { + title: '身份验证', + hint: '两种方式最终更新同一组配置;切换不会清除任何一种方式已保存的凭据,也不会自动发起请求。', + aside: h(Switch, { + ariaLabel: '身份验证方式', + value: identity, + onChange: switchIdentity, + options: [ + { value: 'account', label: '账号登录' }, + { value: 'token', label: '手动令牌' }, + ], + }), + }, + + identity === 'account' + ? h( + 'div', + { style: styles.nested }, + h( + 'p', + { style: styles.hint }, + '用云端面板的普通用户账号登录,插件会用一次性会话换取活动与记忆令牌并写入本机。密码只在这一次请求里使用:不保存、不写日志、不回显。', + ), + h( + 'div', + { style: styles.grid }, + h(Field, { + label: '用户名', + value: loginDraft.username, + placeholder: '云端面板的账号', + onChange: function (value) { + setLoginDraft(function (previous) { + return Object.assign({}, previous, { username: value }) + }) + }, + }), + h(Field, { + label: '密码', + type: 'password', + value: loginDraft.password, + placeholder: '只用于本次换取令牌', + hint: '成功后自动清空', + onChange: function (value) { + setLoginDraft(function (previous) { + return Object.assign({}, previous, { password: value }) + }) + }, + }), + ), + h( + 'div', + { style: styles.actions }, + h(Btn, { + variant: 'primary', + label: busy === 'login' ? '登录中…' : '登录并获取令牌', + disabled: busy !== '', + onClick: login, + }), + loginResult === null + ? null + : h( + 'span', + { style: styles.hint }, + '当前账号:' + String(loginResult.user !== undefined && loginResult.user !== null && loginResult.user.username !== undefined ? loginResult.user.username : '未知') + (loginResult.warning === undefined ? '' : '(' + String(loginResult.warning) + ')'), + ), + ), + ) + : h( + 'div', + { style: styles.nested }, + h( + 'p', + { style: styles.hint }, + '三个令牌各有独立权限:activity:write 只用于上报,memory:write 同时允许检索与写入,summary:write 用于自动摘要(可选)。空值表示不修改;清除是独立动作。', + ), + h( + 'div', + { style: styles.grid }, + h(Field, { + label: '活动上报令牌(activity:write)', + type: 'password', + value: tokenDraft.activity, + placeholder: activityToken.configured === true ? '已配置 ' + String(activityToken.masked) + ',留空表示不修改' : '未配置', + hint: tokenHint(activityToken), + onChange: function (value) { + patchTokens({ activity: value }) + }, + }), + h(Field, { + label: '记忆读写令牌(memory:write)', + type: 'password', + value: tokenDraft.memory, + placeholder: memoryToken.configured === true ? '已配置 ' + String(memoryToken.masked) + ',留空表示不修改' : '未配置', + hint: tokenHint(memoryToken), + onChange: function (value) { + patchTokens({ memory: value }) + }, + }), + h(Field, { + label: '摘要令牌(summary:write,可选)', + type: 'password', + value: tokenDraft.summary, + placeholder: summaryToken.configured === true ? '已配置 ' + String(summaryToken.masked) + ',留空表示不修改' : '未配置', + hint: tokenHint(summaryToken), + onChange: function (value) { + patchTokens({ summary: value }) + }, + }), + ), + h( + 'div', + { style: styles.actions }, + h(Btn, { + label: '清除上报令牌', + variant: 'small', + disabled: activityToken.configured !== true || busy !== '', + onClick: function () { + clearToken('activity') + }, + }), + h(Btn, { + label: '清除记忆令牌', + variant: 'small', + disabled: memoryToken.configured !== true || busy !== '', + onClick: function () { + clearToken('memory') + }, + }), + h(Btn, { + label: '清除摘要令牌', + variant: 'small', + disabled: summaryToken.configured !== true || busy !== '', + onClick: function () { + clearToken('summary') + }, + }), + ), + ), + ), + + h( + Section, + { + title: '服务地址', + hint: '账号登录会把密码发到该地址:仅接受 HTTPS(本机回环除外)。不同 origin 不会复用旧令牌。', + }, + h(Field, { + label: '服务地址(baseUrl)', + value: draft.baseUrl, + placeholder: 'https://<你的域名>/api/longmemory', + hint: changedOrigin ? '地址已改成另一个 origin(当前 ' + draft.baseUrlOrigin + '):保存后旧令牌会被清除,需要重新认证。' : '当前 origin:' + (draft.baseUrlOrigin === '' ? '未配置' : draft.baseUrlOrigin), + onChange: function (value) { + patchDraft({ baseUrl: value }) + }, + }), + advanced + ? h( + 'div', + { style: styles.nested }, + h( + 'div', + { style: styles.grid }, + h(Field, { + label: '客户端标识(client_id)', + value: draft.clientId, + onChange: function (value) { + patchDraft({ clientId: value }) + }, + }), + h(Field, { + label: '请求超时(毫秒)', + type: 'number', + value: draft.requestTimeoutMs, + onChange: function (value) { + patchDraft({ requestTimeoutMs: value }) + }, + }), + + ), + h( + 'div', + { style: styles.checkboxRow }, + h('input', { + type: 'checkbox', + checked: draft.enabled, + id: 'dsh-brain-enabled', + onChange: function (event) { + patchDraft({ enabled: event.target.checked }) + }, + }), + h('label', { htmlFor: 'dsh-brain-enabled', style: styles.label }, '启用每轮活动上报(只上报时间戳、会话 id 与状态,不上传对话内容)'), + ), + h( + 'div', + { style: styles.checkboxRow }, + h('input', { + type: 'checkbox', + checked: draft.summaryEnabled, + id: 'dsh-brain-summary', + onChange: function (event) { + patchDraft({ summaryEnabled: event.target.checked }) + }, + }), + h('label', { htmlFor: 'dsh-brain-summary', style: styles.label }, '启用自动摘要(会把该轮对话文本上传为待确认提案;与上面的元数据上报是两件事)'), + ), + h( + 'div', + { style: styles.checkboxRow }, + h('input', { + type: 'checkbox', + checked: draft.activityEnabled, + id: 'dsh-brain-activity', + onChange: function (event) { + patchDraft({ activityEnabled: event.target.checked }) + }, + }), + h('label', { htmlFor: 'dsh-brain-activity', style: styles.label }, '启用活动上报(细粒度开关;未显式设置时跟随总开关)'), + ), + ) + : h( + 'div', + { style: styles.actions }, + h(Btn, { label: '高级设置', variant: 'small', onClick: toggleAdvanced }), + ), + ), + + h( + Section, + { + title: '记忆配置', + hint: '只影响记忆工具:开关未显式设置时跟随总开关,检索上限只限制单次返回条数。', + }, + + h('div', { style: styles.grid }, + h(Field, { + label: '单次搜索返回上限(defaultK)', + type: 'number', + value: draft.defaultK, + hint: '只限制一次搜索返回多少条,不限制可搜索的历史范围。默认 10。', + onChange: function (value) { + patchDraft({ defaultK: value }) + }, + }), + ), + h('div', { style: styles.checkboxRow }, + h('input', { + type: 'checkbox', + checked: draft.memoryEnabled, + id: 'dsh-brain-memory-enabled', + onChange: function (event) { + patchDraft({ memoryEnabled: event.target.checked }) + }, + }), + h('label', { htmlFor: 'dsh-brain-memory-enabled', style: styles.label }, '启用记忆工具(memory_search / memory_read / memory_add)'), + ), + ), + + features.skillsSync === false + ? h('div', null) + : h( + 'div', + null, + h( + Section, + { + title: 'Skill 同步', + hint: '同步由后台定时执行,这里只保存开关与目录;目录留空使用默认位置。', + }, + + h(Field, { + label: '技能目录(skillsDir,可选)', + value: draft.skillsDir, + placeholder: '默认 $DSH_HOME/skills', + hint: '保存后下一次同步生效,无需重启。', + onChange: function (value) { + patchDraft({ skillsDir: value }) + }, + }), + h('div', { style: styles.checkboxRow }, + h('input', { + type: 'checkbox', + checked: draft.skillsSyncEnabled, + id: 'dsh-brain-skills-sync', + onChange: function (event) { + patchDraft({ skillsSyncEnabled: event.target.checked }) + }, + }), + h('label', { htmlFor: 'dsh-brain-skills-sync', style: styles.label }, '启用云端技能同步(下载云端技能库、上传本机新技能)'), + ), + ), + ), + + h( + Section, + { + title: '连接与权限检查', + aside: h( + 'div', + { style: styles.actions }, + h(Btn, { variant: 'primary', label: busy === 'save' ? '保存中…' : '保存', disabled: busy !== '', onClick: save }), + h(Btn, { label: busy === 'test' ? '测试中…' : '测试连接', disabled: busy !== '', onClick: test }), + h(Btn, { + label: '重试上报队列', + disabled: busy !== '' || reporter.queued === undefined || reporter.queued === 0, + onClick: flush, + }), + ), + }, + checkSource === null + ? h('p', { style: styles.hint }, '还没有检查记录。点「测试连接」做一次只读检查:网络 → 活动权限 → 记忆权限 → 摘要权限。') + : h( + 'div', + { style: styles.pills }, + (checkSource.checks || []).map(function (check) { + return h(Pill, { + key: String(check.id), + label: String(check.label === undefined ? check.id : check.label), + value: CHECK_LABELS[check.state] === undefined ? String(check.state) : CHECK_LABELS[check.state], + tone: CHECK_TONES[check.state] === undefined ? 'muted' : CHECK_TONES[check.state], + }) + }), + ), + checkSource === null + ? null + : h( + 'div', + { style: { display: 'flex', flexDirection: 'column', gap: '6px' } }, + (checkSource.checks || []).map(function (check) { + return h( + 'div', + { + key: 'detail-' + String(check.id), + style: Object.assign({}, styles.banner, styles[CHECK_TONES[check.state]] === undefined ? styles.muted : styles[CHECK_TONES[check.state]]), + }, + String(check.label === undefined ? check.id : check.label) + ':' + String(check.message), + ) + }), + ), + h('p', { style: styles.hint }, '鉴权通过只代表令牌有效,不代表已经成功写入一条上报;两者在下面分开显示。'), + ), + + h( + Section, + { title: '最近上报' }, + recent.length === 0 + ? h('p', { style: styles.hint }, '还没有记录。发一条消息让它跑完一轮,这里就会出现一条。') + : h( + 'table', + { style: styles.table }, + h('thead', null, h('tr', null, h('th', { style: styles.th }, '时间'), h('th', { style: styles.th }, '结果'), h('th', { style: styles.th }, 'turn_id'))), + h( + 'tbody', + null, + recent.map(function (row, index) { + var outcome = String(row.outcome === undefined ? '' : row.outcome) + return h( + 'tr', + { key: String(row.turnId) + ':' + String(index) }, + h('td', { style: styles.td }, formatTime(row.at)), + h( + 'td', + { style: styles.td }, + h( + 'span', + { style: Object.assign({}, styles.pill, styles[OUTCOME_TONES[outcome]] === undefined ? styles.muted : styles[OUTCOME_TONES[outcome]]) }, + OUTCOME_LABELS[outcome] === undefined ? outcome : OUTCOME_LABELS[outcome], + ), + ), + h('td', { style: Object.assign({}, styles.td, styles.mono) }, String(row.turnId === undefined ? '' : row.turnId)), + ) + }), + ), + ), + reporter.lastError === undefined ? null : h('p', { style: styles.hint }, '最近一次错误:' + String(reporter.lastError)), + ), + + h( + 'p', + { style: styles.hint }, + '配置保存在 ' + String(snapshot.settingsPath === undefined ? '本机配置文件' : snapshot.settingsPath) + '。页面地址 /dsh-brain 仍然可用。', + ), + ) + } + + var skillsTab = null + if (tab === 'skills') { + var treeSources = tree.data !== null && Array.isArray(tree.data.sources) ? tree.data.sources : [] + var keyword = String(skills.search).trim().toLowerCase() + + var matchesSkill = function (skill) { + if (keyword === '') return true + return (String(skill.folder) + ' ' + String(skill.name) + ' ' + String(skill.description)).toLowerCase().indexOf(keyword) >= 0 + } + + var visibleGroupsOf = function (source) { + return (Array.isArray(source.groups) ? source.groups : []) + .map(function (group) { + var visible = (Array.isArray(group.skills) ? group.skills : []).filter(matchesSkill) + return Object.assign({}, group, { visible: visible }) + }) + .filter(function (group) { + return group.visible.length > 0 || keyword === '' + }) + } + + var detailBox = null + if (skills.detail !== null) { + var detail = skills.detail.data + detailBox = h( + 'div', + { style: styles.row }, + h( + 'div', + { style: styles.rowHead }, + h('span', { style: styles.rowTitle }, '详情:' + String(skills.detail.folder)), + h(Btn, { + label: '关闭', + variant: 'small', + onClick: function () { + patchSkills({ detail: null }) + }, + }), + ), + detail === null + ? h('p', { style: styles.hint }, skills.detail.error === null ? '正在读取…' : String(skills.detail.error)) + : h( + 'div', + { style: styles.nested }, + h( + 'div', + { style: styles.grid }, + h(Field, { label: '目录', value: String(detail.dir), onChange: function () {} }), + h(Field, { label: 'SKILL.md 哈希', value: String(detail.skill !== undefined ? detail.skill.skillSha256 : ''), onChange: function () {} }), + ), + h('p', { style: styles.hint }, '文件:' + String(Array.isArray(detail.files) ? detail.files.length : 0) + ' 个,共 ' + formatBytes(detail.totalBytes) + (detail.truncated ? '(SKILL.md 过长,已截断显示)' : '')), + h('pre', { style: styles.code }, String(detail.skillMd === undefined ? '' : detail.skillMd)), + h( + 'p', + { style: styles.hint }, + '文件清单:' + (Array.isArray(detail.files) ? detail.files.map(function (file) { return String(file.path) + (file.script ? '(脚本)' : '') }).join('、') : ''), + ), + ), + ) + } + + var uninstallBox = null + if (skills.uninstall !== null) { + uninstallBox = h( + 'div', + { style: Object.assign({}, styles.row, styles.warn) }, + h('div', { style: styles.rowHead }, h('span', { style: styles.rowTitle }, '卸载确认:' + String(skills.uninstall.folder))), + h( + 'p', + { style: styles.hint }, + '将实际删除:' + String(skills.uninstall.dir) + ';影响:' + String(skills.uninstall.files) + ' 个文件 / ' + formatBytes(skills.uninstall.bytes) + (Array.isArray(skills.uninstall.scripts) && skills.uninstall.scripts.length > 0 ? ';含脚本:' + skills.uninstall.scripts.join('、') : ''), + ), + h('p', { style: styles.hint }, String(skills.uninstall.note)), + h( + 'div', + { style: styles.actions }, + h(Btn, { variant: 'danger', label: tree.busy === 'uninstall' ? '删除中…' : '确认删除(不可恢复)', disabled: tree.busy !== '', onClick: confirmUninstall }), + h(Btn, { + label: '取消', + disabled: tree.busy !== '', + onClick: function () { + patchSkills({ uninstall: null }) + }, + }), + ), + ) + } + + var planBox = null + if (skills.plan !== null) { + var candidates = Array.isArray(skills.plan.candidates) ? skills.plan.candidates : [] + planBox = h( + 'div', + { style: styles.row }, + h('div', { style: styles.rowHead }, h('span', { style: styles.rowTitle }, '安装预览:' + String(skills.plan.url))), + h( + 'p', + { style: styles.hint }, + '计划 ' + String(skills.plan.planId) + ' 有效到 ' + formatTime(skills.plan.expiresAt) + '。默认不勾选冲突项;只安装勾选的技能,来源中的安装脚本不会执行。', + ), + h( + 'div', + { style: { display: 'flex', flexDirection: 'column', gap: '6px' } }, + candidates.map(function (candidate) { + var selected = skills.selection.indexOf(candidate.folder) >= 0 + var blocked = candidate.conflict === true || candidate.problem !== undefined + return h( + 'label', + { key: String(candidate.id), style: styles.checkboxRow }, + h('input', { + type: 'checkbox', + checked: selected, + disabled: blocked, + onChange: function (event) { + var next = skills.selection.slice() + var at = next.indexOf(candidate.folder) + if (event.target.checked && at < 0) next.push(candidate.folder) + if (!event.target.checked && at >= 0) next.splice(at, 1) + patchSkills({ selection: next }) + }, + }), + h( + 'span', + { style: { minWidth: 0 } }, + h('span', { style: { fontWeight: 600 } }, String(candidate.name)), + h('span', { style: styles.hint }, ' · ' + String(candidate.folder) + ' · ' + String(candidate.files) + ' 个文件 · ' + formatBytes(candidate.bytes) + (candidate.conflict === true ? ' · 已存在(' + String(candidate.conflictLocation === 'holding' ? '已停用目录' : '技能目录') + '),不覆盖' : '') + (candidate.problem === undefined ? '' : ' · ' + String(candidate.problem)) + (Array.isArray(candidate.scripts) && candidate.scripts.length > 0 ? ' · 含脚本:' + candidate.scripts.join('、') : '')), + ), + ) + }), + ), + h( + 'div', + { style: styles.actions }, + h(Btn, { + variant: 'primary', + label: tree.busy === 'commit' ? '安装中…' : '安装所选(' + String(skills.selection.length) + ')', + disabled: tree.busy !== '' || skills.selection.length === 0, + onClick: commitInstall, + }), + h(Btn, { label: '放弃计划', variant: 'small', disabled: tree.busy !== '', onClick: cancelInstall }), + ), + ) + } + + var assignBox = null + if (tree.assign !== null) { + var assignSource = treeSources.find(function (source) { + return source.id === tree.assign.sourceId + }) + var assignGroup = assignSource === undefined + ? undefined + : (assignSource.groups || []).find(function (group) { + return group.id === tree.assign.groupId + }) + var assignable = [] + if (assignSource !== undefined) { + (assignSource.groups || []).forEach(function (group) { + (group.skills || []).forEach(function (skill) { + if (!assignable.some(function (item) { return item.folder === skill.folder })) assignable.push(skill) + }) + }) + } + var assignSearch = String(tree.assign.search === undefined ? '' : tree.assign.search) + var assignNeedle = assignSearch.trim().toLowerCase() + var filteredAssignable = assignNeedle === '' + ? assignable + : assignable.filter(function (skill) { + var hay = (String(skill.name || '') + ' ' + String(skill.folder || '') + ' ' + String(skill.description || '')).toLowerCase() + return hay.indexOf(assignNeedle) >= 0 + }) + var selectedCount = tree.assign.selection.length + var matchedSelected = 0 + filteredAssignable.forEach(function (skill) { + if (tree.assign.selection.indexOf(skill.folder) >= 0) matchedSelected += 1 + }) + function setSelection(next) { + patchTree({ assign: Object.assign({}, tree.assign, { selection: next }) }) + } + function setSelectionFilter(value) { + patchTree({ assign: Object.assign({}, tree.assign, { search: value }) }) + } + function selectMany(list, on) { + var next = tree.assign.selection.slice() + list.forEach(function (skill) { + var at = next.indexOf(skill.folder) + if (on && at < 0) next.push(skill.folder) + if (!on && at >= 0) next.splice(at, 1) + }) + setSelection(next) + } + assignBox = h( + 'div', + { style: styles.row }, + h( + 'div', + { style: styles.rowHead }, + h('span', { style: styles.rowTitle }, '加入分类:' + String(assignGroup === undefined ? '' : assignGroup.name)), + h( + 'div', + { style: styles.pills }, + h(Pill, { label: '可分配', value: String(assignable.length), tone: 'muted' }), + h(Pill, { label: '匹配', value: String(filteredAssignable.length), tone: filteredAssignable.length === 0 ? 'muted' : 'ok' }), + h(Pill, { label: '已选', value: String(selectedCount), tone: selectedCount === 0 ? 'muted' : 'ok' }), + ), + ), + h('p', { style: styles.hint }, '先搜索再勾选,或直接「全选匹配结果」一次性加入;一个技能只会属于一个分类,取消勾选即回到「未分类」。'), + h( + 'div', + { style: { display: 'flex', gap: '8px', alignItems: 'center', flexWrap: 'wrap' } }, + h('input', { + type: 'search', + style: Object.assign({}, styles.input, { flex: '1 1 200px', minWidth: 0 }), + value: assignSearch, + autoFocus: true, + placeholder: '搜索技能:名称 / 目录 / 描述', + 'aria-label': '搜索要加入分类的技能', + onChange: function (event) { + setSelectionFilter(event.target.value) + }, + }), + assignSearch !== '' + ? h(Btn, { + label: '清空搜索', + variant: 'small', + onClick: function () { + setSelectionFilter('') + }, + }) + : null, + ), + h( + 'div', + { style: Object.assign({}, styles.actions, { flexWrap: 'wrap' }) }, + h(Btn, { + label: '全选匹配结果(' + String(filteredAssignable.length) + ')', + variant: 'small', + disabled: tree.busy !== '' || filteredAssignable.length === 0, + onClick: function () { + selectMany(filteredAssignable, true) + }, + }), + h(Btn, { + label: '全选全部(' + String(assignable.length) + ')', + variant: 'small', + disabled: tree.busy !== '' || assignable.length === 0 || selectedCount === assignable.length, + onClick: function () { + selectMany(assignable, true) + }, + }), + h(Btn, { + label: '取消勾选匹配项(' + String(matchedSelected) + ')', + variant: 'small', + disabled: tree.busy !== '' || matchedSelected === 0, + onClick: function () { + selectMany(filteredAssignable, false) + }, + }), + h(Btn, { + label: '清空已选', + variant: 'small', + disabled: tree.busy !== '' || selectedCount === 0, + onClick: function () { + setSelection([]) + }, + }), + ), + filteredAssignable.length === 0 + ? h('p', { style: styles.hint }, assignable.length === 0 ? '当前来源还没有可分配的技能。' : '没有匹配「' + assignSearch + '」的技能。换个关键词,或点「清空搜索」看全部。') + : h( + 'div', + { style: { display: 'flex', flexDirection: 'column', gap: '4px', maxHeight: '260px', overflow: 'auto' } }, + filteredAssignable.map(function (skill) { + var checked = tree.assign.selection.indexOf(skill.folder) >= 0 + return h( + 'label', + { key: String(skill.folder), style: styles.checkboxRow }, + h('input', { + type: 'checkbox', + checked: checked, + onChange: function (event) { + selectMany([skill], event.target.checked === true) + }, + }), + h( + 'span', + { style: { minWidth: 0 } }, + String(skill.name) + ' ', + h('span', { style: styles.hint }, '· ' + String(skill.folder) + (skill.description ? ' · ' + String(skill.description) : '')), + ), + ) + }), + ), + h( + 'div', + { style: styles.actions }, + h(Btn, { + variant: 'primary', + label: '保存分类(' + String(selectedCount) + ')', + disabled: tree.busy !== '', + onClick: function () { + saveAssign() + }, + }), + h(Btn, { + label: '取消', + disabled: tree.busy !== '', + onClick: function () { + patchTree({ assign: null }) + }, + }), + ), + ) + } + + var renderSkillRow = function (source, skill) { + var writable = source.writable === true + return h( + 'div', + { key: String(source.id) + ':' + String(skill.folder), style: styles.skillRow }, + h( + 'div', + { + style: { display: 'flex', alignItems: 'baseline', gap: '8px', minWidth: 0, flex: '1 1 auto', cursor: writable ? 'pointer' : 'default' }, + title: writable ? '查看详情' : undefined, + onClick: writable + ? function () { + openDetail(skill.folder) + } + : undefined, + }, + h('span', { style: Object.assign({}, styles.rowTitle, { flex: '0 0 auto' }) }, String(skill.name)), + skill.description === '' ? null : h('span', { style: Object.assign({}, styles.hint, styles.clamp1, { minWidth: 0 }) }, String(skill.description)), + ), + h( + 'div', + { style: { display: 'flex', alignItems: 'center', gap: '6px', flex: '0 0 auto' } }, + skill.enabledEditable !== true ? h('span', { style: styles.hint }, '只读') : null, + writable + ? h(IconBtn, { + glyph: '🗑', + danger: true, + ariaLabel: '卸载 ' + String(skill.name), + disabled: tree.busy !== '', + onClick: function () { + planUninstall(skill.folder) + }, + }) + : null, + skill.enabledEditable !== true + ? null + : h(Toggle, { + checked: skill.enabled === true, + disabled: tree.busy !== '', + label: '技能 ' + String(skill.name) + ' 启停', + onChange: function () { + toggleSkill(skill.folder, skill.enabled !== true) + }, + }), + ), + ) + } + + var renderGroup = function (source, group) { + var open = tree.openGroups[String(source.id) + ':' + group.id] === true + var renameKey = String(source.id) + ':' + group.id + var renaming = tree.edit !== null && tree.edit.kind === 'rename' && tree.edit.key === renameKey + var confirming = tree.confirm === renameKey + var assigning = tree.assign !== null && tree.assign.sourceId === source.id && tree.assign.groupId === group.id + var groupSkills = group.visible === undefined ? group.skills || [] : group.visible + return h( + 'div', + { key: renameKey, style: styles.groupRow }, + h( + 'div', + { style: styles.rowHead }, + h( + 'div', + { style: { display: 'flex', alignItems: 'center', gap: '6px', minWidth: 0, flexWrap: 'wrap' } }, + h(Btn, { + label: open ? '▾' : '▸', + variant: 'small', + ariaLabel: '展开或收起 ' + String(group.name), + onClick: function () { + patchTree({ openGroups: Object.assign({}, tree.openGroups, { [renameKey]: !open }) }) + }, + }), + h('span', { style: styles.folderGlyph }, '📁'), + renaming + ? h('input', { + style: Object.assign({}, styles.input, { width: '160px' }), + value: tree.edit.value, + autoFocus: true, + onChange: function (event) { + patchTree({ edit: Object.assign({}, tree.edit, { value: event.target.value }) }) + }, + }) + : h('span', { style: styles.rowTitle }, String(group.name)), + h('span', { style: styles.hint }, String(group.total) + ' 项 · ' + String(group.enabledCount) + '/' + String(group.total) + ' 启用'), + group.cardOnly === true ? h(Pill, { label: '开关', value: '仅本卡', tone: 'muted' }) : null, + ), + h( + 'div', + { style: styles.actions }, + renaming + ? h(Btn, { label: '保存', variant: 'small', disabled: tree.busy !== '', onClick: saveRename }) + : h(IconBtn, { + glyph: '+', + ariaLabel: '把技能加入 ' + String(group.name), + disabled: tree.busy !== '' || group.id === 'unassigned', + onClick: function () { + openAssign(source, group) + }, + }), + renaming + ? h(Btn, { label: '取消', variant: 'small', onClick: function () { patchTree({ edit: null }) } }) + : h(IconBtn, { + glyph: '✎', + ariaLabel: '重命名 ' + String(group.name), + disabled: tree.busy !== '' || group.id === 'unassigned', + onClick: function () { + patchTree({ edit: { kind: 'rename', key: renameKey, sourceId: source.id, groupId: group.id, value: String(group.name) } }) + }, + }), + confirming + ? h(Btn, { label: '确认删除分类', variant: 'danger', disabled: tree.busy !== '', onClick: function () { deleteGroup(source.id, group.id) } }) + : h(IconBtn, { + glyph: '🗑', + danger: true, + ariaLabel: '删除分类 ' + String(group.name) + '(技能回到未分类)', + disabled: tree.busy !== '' || group.id === 'unassigned', + onClick: function () { + patchTree({ confirm: renameKey }) + setNotice({ tone: 'warn', text: '删除分类只删除分组记录,里面的技能会回到「未分类」,文件不会被删除。再点一次确认。' }) + }, + }), + group.enabledEditable === false + ? null + : h(Toggle, { + checked: group.enabled !== false, + disabled: tree.busy !== '', + label: '分类 ' + String(group.name) + ' 启停', + onChange: function () { + toggleGroup(source.id, group.id, group.enabled === false) + }, + }), + ), + ), + assigning ? h('div', { style: styles.nested }, assignBox) : null, + open + ? h( + 'div', + { style: styles.nested }, + groupSkills.length === 0 + ? assigning + ? null + : h('p', { style: styles.hint }, group.id === 'unassigned' ? '没有未分类的技能。' : '这个分类还没有技能,点「+」加入。') + : groupSkills.map(function (skill) { + return renderSkillRow(source, skill) + }), + ) + : null, + ) + } + + var dshSource = treeSources.find(function (src) { + return src.writable === true + }) + + skillsTab = h( + 'div', + { style: styles.root, key: 'skills' }, + + h(ImportSection, { + onImported: function () { + loadTree() + loadSkills() + }, + }), + + h( + Section, + { + title: '技能文件夹', + hint: '按软件来源分组;展开来源与分类查看技能。', + }, + dshSource === undefined + ? null + : h( + 'div', + { style: styles.statsBar }, + h('span', { style: styles.stat }, h('span', { style: styles.statNum }, String(dshSource.total || 0)), h('span', { style: styles.hint }, '个技能')), + h('span', { style: styles.stat }, h('span', { style: styles.statNum }, String(dshSource.enabledCount || 0)), h('span', { style: styles.hint }, '个已启用')), + h('span', { style: styles.statLast }, h('span', { style: styles.statNum }, String((dshSource.total || 0) - (dshSource.enabledCount || 0))), h('span', { style: styles.hint }, '个已停用')), + ), + h( + 'div', + { style: { display: 'flex', gap: '8px', alignItems: 'flex-end', flexWrap: 'wrap' } }, + h( + 'div', + { style: { flex: '1 1 220px', minWidth: 0 } }, + h(Field, { + label: '搜索', + value: skills.search, + placeholder: '按名称、目录或描述筛选', + onChange: function (value) { + patchSkills({ search: value }) + }, + }), + ), + h(Btn, { + label: tree.status === 'loading' ? '刷新中…' : '刷新', + variant: 'small', + disabled: tree.status === 'loading', + onClick: function () { + loadTree() + loadSkills() + }, + }), + ), + tree.error === null ? null : h(Banner, { tone: 'bad' }, tree.error), + tree.status === 'loading' && tree.data === null ? h('p', { style: styles.hint }, '正在读取技能来源…') : null, + tree.status === 'ready' && treeSources.length === 0 ? h('p', { style: styles.hint }, '没有探测到任何技能来源。') : null, + dshSource === undefined + ? null + : h( + 'div', + { style: { display: 'flex', flexDirection: 'column', gap: '2px' } }, + visibleGroupsOf(dshSource).map(function (group) { + return renderGroup(dshSource, group) + }), + tree.edit !== null && tree.edit.kind === 'create' && tree.edit.sourceId === dshSource.id + ? h( + 'div', + { style: Object.assign({}, styles.row, { flexDirection: 'row', alignItems: 'center', flexWrap: 'wrap', marginTop: '6px' }) }, + h('input', { + style: Object.assign({}, styles.input, { width: '180px' }), + placeholder: '分类名称,例如 QQ-Skill', + value: tree.edit.value, + autoFocus: true, + onChange: function (event) { + patchTree({ edit: Object.assign({}, tree.edit, { value: event.target.value }) }) + }, + }), + h(Btn, { label: '创建分类', variant: 'primary', disabled: tree.busy !== '', onClick: createGroup }), + h(Btn, { label: '取消', onClick: function () { patchTree({ edit: null }) } }), + ) + : h( + 'div', + { style: { marginTop: '6px' } }, + h(Btn, { + label: '+ 新建分类', + variant: 'small', + disabled: tree.busy !== '', + onClick: function () { + patchTree({ edit: { kind: 'create', sourceId: dshSource.id, value: '' } }) + }, + }), + ), + ), + tree.data !== null && tree.data.note !== undefined ? h('p', { style: styles.hint }, String(tree.data.note)) : null, + ), + + detailBox, + uninstallBox, + + h( + Section, + { + title: '从 Git 仓库安装', + hint: '先克隆到隔离暂存目录并列出候选技能,确认后才写入 DSH 技能目录;不执行仓库里的任何脚本,符号链接不会跟随。仓库地址只接受 https:// 或 file://。', + }, + h( + 'div', + { style: styles.grid }, + h(Field, { + label: '仓库地址', + value: skills.installUrl, + placeholder: 'https://github.com//.git', + onChange: function (value) { + patchSkills({ installUrl: value }) + }, + }), + ), + h( + 'div', + { style: styles.actions }, + h(Btn, { + variant: 'primary', + label: skills.busy === 'plan' ? '拉取中…' : '拉取并预览', + disabled: skills.busy !== '', + onClick: prepareInstall, + }), + ), + planBox, + ), + + h(MarketSection, { + capabilities: caps, + onInstalled: function () { + loadTree() + loadSkills() + }, + }), + + h(SyncResolveSection, { + onResolved: function () { + loadSkills() + }, + }), + ) + } + + var mcpTab = null + if (tab === 'mcp') { + var items = Array.isArray(mcp.items) ? mcp.items : [] + var mcpSearch = String(mcp.search === undefined ? '' : mcp.search) + var mcpNeedle = mcpSearch.trim().toLowerCase() + var mcpFilter = String(mcp.filter === undefined ? 'all' : mcp.filter) + var mcpChosen = Array.isArray(mcp.selection) ? mcp.selection : [] + var mcpBulk = mcp.bulk === null || mcp.bulk === undefined ? null : mcp.bulk + var stateOf = function (item) { + return String(item.connectionState === undefined ? 'unknown' : item.connectionState) + } + var isProblem = function (item) { + var state = stateOf(item) + return state !== 'connected' && state !== 'disabled' + } + var haystackOf = function (item) { + var state = stateOf(item) + return [ + item.name, item.key, item.transport, item.authMode, item.injectionLabel, + MCP_STATE_LABELS[state] === undefined ? state : MCP_STATE_LABELS[state], + item.lastError, + item.scope === undefined || item.scope === null ? '' : item.scope.visibility, + ] + .map(function (value) { return String(value === undefined || value === null ? '' : value) }) + .join(' ') + .toLowerCase() + } + var matchesMcp = function (item) { + if (mcpFilter === 'connected' && stateOf(item) !== 'connected') return false + if (mcpFilter === 'disabled' && !(item.enabled === false || stateOf(item) === 'disabled')) return false + if (mcpFilter === 'problem' && !isProblem(item)) return false + if (mcpNeedle === '') return true + return haystackOf(item).indexOf(mcpNeedle) >= 0 + } + var visibleItems = items.filter(matchesMcp) + var mcpCounts = { + connected: items.filter(function (item) { return stateOf(item) === 'connected' }).length, + disabled: items.filter(function (item) { return item.enabled === false || stateOf(item) === 'disabled' }).length, + problem: items.filter(isProblem).length, + } + var matchedSelected = visibleItems.filter(function (item) { return mcpChosen.indexOf(item.key) >= 0 }).length + var bridgeBox = null + if (mcp.status === 'missing') { + bridgeBox = h( + Banner, + { tone: 'warn' }, + 'MCP 管理当前不可用:没有探测到宿主 dsh-mcp-connector 的公开接口(' + MCP_BRIDGE + ')。' + (mcp.error === null ? '' : '原因:' + String(mcp.error)) + '。自定义 MCP Server 的权威位置仍是「设置 → 插件配置 → MCP 服务器配置」,本卡不会修改它的托管配置。', + ) + } + + mcpTab = h( + 'div', + { style: styles.root, key: 'mcp' }, + h( + Section, + { + title: '我的连接', + hint: '数据来自宿主 dsh-mcp-connector 插件(同一 origin 的公开接口);本卡不保存任何 MCP 凭据,也不回显启动命令与密钥。', + aside: h(Btn, { label: mcp.status === 'probe' ? '探测中…' : '重新探测', variant: 'small', disabled: mcp.busy !== '', onClick: probeMcp }), + }, + bridgeBox, + mcp.status === 'ok' && items.length === 0 ? h('p', { style: styles.hint }, '宿主还没有登记任何 MCP 连接。') : null, + mcp.status === 'ok' && items.length > 0 + ? h( + 'div', + { style: { display: 'flex', flexDirection: 'column', gap: '8px' } }, + h( + 'div', + { style: styles.pills }, + h(Pill, { label: '共', value: String(items.length), tone: 'muted' }), + h(Pill, { label: '匹配', value: String(visibleItems.length), tone: visibleItems.length === 0 ? 'muted' : 'ok' }), + h(Pill, { label: '已选', value: String(mcpChosen.length), tone: mcpChosen.length === 0 ? 'muted' : 'ok' }), + h(Pill, { label: '已连接', value: String(mcpCounts.connected), tone: 'ok' }), + h(Pill, { label: '异常', value: String(mcpCounts.problem), tone: mcpCounts.problem === 0 ? 'muted' : 'warn' }), + ), + h( + 'div', + { style: { display: 'flex', gap: '8px', alignItems: 'center', flexWrap: 'wrap' } }, + h('input', { + type: 'search', + style: Object.assign({}, styles.input, { flex: '1 1 220px', minWidth: 0 }), + value: mcpSearch, + placeholder: '搜索连接:名称 / key / 传输 / 鉴权 / 状态', + 'aria-label': '搜索 MCP 连接', + onChange: function (event) { + patchMcp({ search: event.target.value }) + }, + }), + mcpSearch !== '' + ? h(Btn, { + label: '清空搜索', + variant: 'small', + onClick: function () { + patchMcp({ search: '' }) + }, + }) + : null, + ), + h( + 'div', + { style: Object.assign({}, styles.actions, { flexWrap: 'wrap' }) }, + h(Btn, { + label: '筛选:全部(' + String(items.length) + ')', + variant: mcpFilter === 'all' ? 'primary' : 'small', + onClick: function () { + patchMcp({ filter: 'all' }) + }, + }), + h(Btn, { + label: '已连接(' + String(mcpCounts.connected) + ')', + variant: mcpFilter === 'connected' ? 'primary' : 'small', + onClick: function () { + patchMcp({ filter: 'connected' }) + }, + }), + h(Btn, { + label: '已停用(' + String(mcpCounts.disabled) + ')', + variant: mcpFilter === 'disabled' ? 'primary' : 'small', + onClick: function () { + patchMcp({ filter: 'disabled' }) + }, + }), + h(Btn, { + label: '异常(' + String(mcpCounts.problem) + ')', + variant: mcpFilter === 'problem' ? 'primary' : 'small', + onClick: function () { + patchMcp({ filter: 'problem' }) + }, + }), + ), + h( + 'div', + { style: Object.assign({}, styles.actions, { flexWrap: 'wrap' }) }, + h(Btn, { + label: '全选匹配结果(' + String(visibleItems.length) + ')', + ariaLabel: '全选匹配结果', + variant: 'small', + disabled: visibleItems.length === 0, + onClick: function () { + selectMcpMany(visibleItems, true) + }, + }), + h(Btn, { + label: '全选全部(' + String(items.length) + ')', + ariaLabel: '全选全部', + variant: 'small', + disabled: items.length === 0 || mcpChosen.length === items.length, + onClick: function () { + selectMcpMany(items, true) + }, + }), + h(Btn, { + label: '取消勾选匹配项(' + String(matchedSelected) + ')', + ariaLabel: '取消勾选匹配项', + variant: 'small', + disabled: matchedSelected === 0, + onClick: function () { + selectMcpMany(visibleItems, false) + }, + }), + h(Btn, { + label: '清空已选', + ariaLabel: '清空已选', + variant: 'small', + disabled: mcpChosen.length === 0, + onClick: function () { + patchMcp({ selection: [], confirmBulk: false }) + }, + }), + ), + mcpChosen.length === 0 + ? h('p', { style: styles.hint }, '勾选左侧连接后,这里会出现批量健康检查、启停、重连与移除。') + : h( + 'div', + { style: Object.assign({}, styles.actions, { flexWrap: 'wrap' }) }, + h('span', { style: styles.hint }, '批量操作(已选 ' + String(mcpChosen.length) + '):'), + h(Btn, { + label: '健康检查', + ariaLabel: '批量健康检查', + variant: 'small', + disabled: mcp.busy !== '', + onClick: function () { + runMcpBulk('批量健康检查', 'healthCheck', function (item) { return { connectorId: item.connectorId } }) + }, + }), + h(Btn, { + label: '启用', + ariaLabel: '批量启用', + variant: 'small', + disabled: mcp.busy !== '', + onClick: function () { + runMcpBulk('批量启用', 'setEnabled', function (item) { return { key: item.key, enabled: true } }) + }, + }), + h(Btn, { + label: '停用', + ariaLabel: '批量停用', + variant: 'small', + disabled: mcp.busy !== '', + onClick: function () { + runMcpBulk('批量停用', 'setEnabled', function (item) { return { key: item.key, enabled: false } }) + }, + }), + h(Btn, { + label: '重连', + ariaLabel: '批量重连', + variant: 'small', + disabled: mcp.busy !== '', + onClick: function () { + runMcpBulk('批量重连', 'reconnect', function (item) { return { key: item.key } }) + }, + }), + mcp.confirmBulk === true + ? h(Btn, { + label: '确认移除(' + String(mcpChosen.length) + ')', + ariaLabel: '确认批量移除', + variant: 'danger', + disabled: mcp.busy !== '', + onClick: function () { + runMcpBulk('批量移除', 'disconnect', function (item) { return { key: item.key } }) + }, + }) + : h(Btn, { + label: '移除…', + ariaLabel: '批量移除', + variant: 'small', + disabled: mcp.busy !== '', + onClick: function () { + patchMcp({ confirmBulk: true }) + setNotice({ tone: 'warn', text: '批量移除会断开所选 ' + String(mcpChosen.length) + ' 条连接定义;再点一次「确认移除」才执行。' }) + }, + }), + ), + mcpBulk === null ? null : h('p', { style: styles.hint }, String(mcpBulk.label) + ' 进行中:' + String(mcpBulk.done) + ' / ' + String(mcpBulk.total)), + visibleItems.length === 0 ? h('p', { style: styles.hint }, '没有匹配「' + mcpSearch + '」的连接。换个关键词或切回「全部」筛选。') : null, + visibleItems.map(function (item) { + var state = stateOf(item) + var tools = mcp.tools[item.key] + var details = mcp.details === null || mcp.details === undefined ? {} : mcp.details + var open = details[item.key] === true + var label = String(item.name === undefined ? item.key : item.name) + return h( + 'div', + { key: String(item.key), style: styles.row }, + h( + 'div', + { style: styles.rowHead }, + h( + 'label', + { style: { display: 'inline-flex', alignItems: 'center', gap: '8px', minWidth: 0, flex: '1 1 auto' } }, + h('input', { + type: 'checkbox', + checked: mcpChosen.indexOf(item.key) >= 0, + 'aria-label': '选择连接 ' + label, + onChange: function (event) { + selectMcpMany([item], event.target.checked === true) + }, + }), + h('span', { style: styles.rowTitle }, label), + h(Pill, { label: '状态', value: MCP_STATE_LABELS[state] === undefined ? state : MCP_STATE_LABELS[state], tone: state === 'connected' ? 'ok' : state === 'disabled' ? 'muted' : 'warn' }), + h(Pill, { label: '传输', value: String(item.transport === undefined ? '—' : item.transport), tone: 'muted' }), + ), + h(Btn, { + label: open ? '收起' : '详情', + ariaLabel: (open ? '收起详情 ' : '详情 ') + label, + variant: 'small', + onClick: function () { + patchMcp({ details: Object.assign({}, details, { [String(item.key)]: !open }) }) + }, + }), + ), + open + ? h( + 'div', + { style: { display: 'flex', flexDirection: 'column', gap: '6px' } }, + h( + 'div', + { style: styles.pills }, + h(Pill, { label: '鉴权', value: String(item.authMode === undefined ? 'none' : item.authMode), tone: 'muted' }), + h(Pill, { label: '注入', value: String(item.injectionLabel === undefined ? '—' : item.injectionLabel), tone: 'muted' }), + h(Pill, { label: '范围', value: item.scope === undefined || item.scope === null ? '未知' : String(item.scope.visibility === undefined ? JSON.stringify(item.scope) : item.scope.visibility), tone: 'muted' }), + h(Pill, { label: '健康检查', value: formatIso(item.healthCheckedAt), tone: 'muted' }), + ), + item.lastError === null || item.lastError === undefined + ? null + : h('p', { style: styles.hint }, '最近错误:' + String(item.lastError)), + h( + 'div', + { style: styles.actions }, + h(Btn, { + label: '健康检查', + ariaLabel: '健康检查 ' + label, + variant: 'small', + disabled: mcp.busy !== '', + onClick: function () { + mcpAction('健康检查 ' + label, 'healthCheck', { connectorId: item.connectorId }).then(function () { + probeMcp() + }) + }, + }), + h(Btn, { + label: '工具列表', + ariaLabel: '工具列表 ' + label, + variant: 'small', + disabled: mcp.busy !== '', + onClick: function () { + loadTools(item) + }, + }), + h(Btn, { + label: item.enabled === false ? '启用' : '停用', + ariaLabel: '启停 ' + label, + variant: 'small', + disabled: mcp.busy !== '', + onClick: function () { + mcpAction(item.enabled === false ? '启用连接' : '停用连接', 'setEnabled', { key: item.key, enabled: item.enabled === false }).then(function () { + probeMcp() + }) + }, + }), + h(Btn, { + label: '重连', + ariaLabel: '重连 ' + label, + variant: 'small', + disabled: mcp.busy !== '', + onClick: function () { + mcpAction('重连', 'reconnect', { key: item.key }).then(function () { + probeMcp() + }) + }, + }), + mcp.confirm === item.key + ? h(Btn, { + label: '确认移除', + ariaLabel: '确认移除 ' + label, + variant: 'danger', + disabled: mcp.busy !== '', + onClick: function () { + mcpAction('移除连接', 'disconnect', { key: item.key }).then(function () { + patchMcp({ confirm: null }) + probeMcp() + }) + }, + }) + : h(Btn, { + label: '移除连接…', + ariaLabel: '移除连接 ' + label, + variant: 'small', + disabled: mcp.busy !== '', + onClick: function () { + patchMcp({ confirm: item.key }) + setNotice({ tone: 'warn', text: '再次点击「确认移除」才会断开该连接定义。' }) + }, + }), + ), + tools === undefined + ? null + : h('p', { style: styles.hint }, '工具缓存(' + formatTime(tools.at) + '):' + (tools.items.length === 0 ? '空' : tools.items.map(function (tool) { return String(tool.name === undefined ? tool : tool.name) }).join('、'))), + ) + : null, + ) + }), + h('p', { style: styles.hint }, '工具可见不等于业务可调用:连接状态与工具缓存分开显示。'), + ) + : null, + ), + + h(McpCustomSection, { onChanged: probeMcp }), + h(McpConfigSection, { items: items, onChanged: probeMcp }), + ) + } + + return h( + 'div', + { style: styles.root }, + header, + tabBar, + noticeBox, + error === null ? null : h(Banner, { tone: 'bad' }, error), + tab === 'connection' ? connectionTab : tab === 'skills' ? skillsTab : mcpTab, + ) +} + +// A thrown render error must not take the settings page down with it, so the +// card is wrapped in a minimal boundary that renders the message instead. diff --git a/src-client/api.ts b/src-client/api.ts new file mode 100644 index 0000000..4ca3dd1 --- /dev/null +++ b/src-client/api.ts @@ -0,0 +1,95 @@ +// dsh-brain client API helpers. +// Adapted from the original hand-written client bundle (dsh-brain). +export const API = { + status: '/dsh-brain/status', + capabilities: '/dsh-brain/capabilities', + config: '/dsh-brain/config', + login: '/dsh-brain/login', + test: '/dsh-brain/test', + flush: '/dsh-brain/flush', + skills: '/dsh-brain/skills', + market: '/dsh-brain/market', + sync: '/dsh-brain/sync', + clients: '/dsh-brain/clients', + migrate: '/dsh-brain/migrate', +} + +// ---- MCP bundle (browser bridge) --------------------------------------- +// The MCP bridge is another plugin's public surface, not ours: it is probed +// at runtime and the tab reports a capability gap when it is missing. +// This bundle owns the tab, the request envelope, the probe timeout and +// the host-unavailable state; connections, credentials, tool management +// and any cloud-side sync belong to the host dsh-mcp-connector plugin. + +export function request(url, init?) { + var options = init || {} + options.cache = 'no-store' + options.headers = options.body === undefined + ? { accept: 'application/json' } + : { accept: 'application/json', 'content-type': 'application/json' } + return fetch(url, options).then(function (response) { + return response.json().then( + function (body) { + return { status: response.status, body: body } + }, + function () { + return { status: response.status, body: null } + }, + ) + }) +} + +export function postJson(url, payload?) { + return request(url, { method: 'POST', body: JSON.stringify(payload === undefined ? {} : payload) }) +} + +export function messageOf(error) { + if (error === null || error === undefined) return '未知错误' + if (typeof error === 'string') return error + return typeof error.message === 'string' ? error.message : String(error) +} + +// One POST to the host MCP bridge. Used by the self-contained MCP panels +// below; the dispatcher whitelist lives in dsh-mcp-connector. + +export function statusText(status) { + if (status === 404 || status === 405) { + return '插件后端还没加载这个接口(HTTP ' + String(status) + '):请完全退出并重启 DSH 后再试。' + } + return 'HTTP ' + String(status) +} + +export function bodyError(result, fallback?) { + if (result.body !== null && result.body !== undefined && typeof result.body.error === 'string' && result.body.error !== '') { + return result.body.error + } + return fallback === undefined ? statusText(result.status) : fallback +} + +// ---- settings-ui bundle (configuration surface) ---- +// pickSettings + the SettingsCard draft/save/login/test/flush logic and the +// 连接与上报 tab belong to the settings-ui bundle; the skills/mcp tabs +// belong to their own bundles. The card only talks to the public HTTP API — +// it never touches SettingsStore/CloudClient/Reporter/SkillSync directly. +export function pickSettings(settings) { + var source = settings === undefined || settings === null ? {} : settings + return { + enabled: source.enabled === true, + baseUrl: String(source.baseUrl === undefined ? '' : source.baseUrl), + baseUrlConfigured: source.baseUrlConfigured !== false, + baseUrlOrigin: String(source.baseUrlOrigin === undefined ? '' : source.baseUrlOrigin), + clientId: String(source.clientId === undefined ? '' : source.clientId), + requestTimeoutMs: String(source.requestTimeoutMs === undefined ? 8000 : source.requestTimeoutMs), + defaultK: String(source.defaultK === undefined ? 10 : source.defaultK), + summaryEnabled: source.summaryEnabled === true, + activityEnabled: source.activityEnabled === true, + memoryEnabled: source.memoryEnabled === true, + skillsDir: String(source.skillsDir === undefined ? '' : source.skillsDir), + skillsSyncEnabled: source.skillsSyncEnabled === true, + } +} + +export function emptyTokens() { + return { activity: '', memory: '', summary: '' } +} + diff --git a/src-client/bridge.ts b/src-client/bridge.ts new file mode 100644 index 0000000..318211a --- /dev/null +++ b/src-client/bridge.ts @@ -0,0 +1,37 @@ +// Host MCP bridge helpers (dsh-mcp-connector public surface). +import { messageOf } from './api' +export const MCP_BRIDGE = '/mcp-connector/api' +export const MCP_PANEL = '/mcp-connector/ui' +export const POLL_MS = 10000 +export const PROBE_TIMEOUT_MS = 5000 + + +export function bridgeCall(method, params) { + var controller = typeof AbortController === 'function' ? new AbortController() : null + var timer = controller === null ? null : setTimeout(function () { controller.abort() }, 20000) + return fetch(MCP_BRIDGE, { + method: 'POST', + cache: 'no-store', + headers: { accept: 'application/json', 'content-type': 'application/json' }, + body: JSON.stringify({ method: method, params: params === undefined ? {} : params }), + signal: controller === null ? undefined : controller.signal, + }).then(function (response) { + if (timer !== null) clearTimeout(timer) + return response.json().then( + function (body) { return { status: response.status, body: body } }, + function () { return { status: response.status, body: null } }, + ) + }, function (failure) { + if (timer !== null) clearTimeout(timer) + return { status: 0, body: null, error: messageOf(failure) } + }) +} + +export function bridgeMessage(result) { + if (result.body !== null && typeof result.body.message === 'string' && result.body.message !== '') return result.body.message + return 'HTTP ' + String(result.status) + (result.error === undefined ? '' : ':' + result.error) +} + +// A 404/405 here means the host half on disk is newer than the one this +// window is talking to: the module is loaded into the running process and +// does not hot-reload, so the answer is a restart, not a retry. diff --git a/src-client/components.tsx b/src-client/components.tsx new file mode 100644 index 0000000..2c1894e --- /dev/null +++ b/src-client/components.tsx @@ -0,0 +1,181 @@ +// dsh-brain client basic components. +import * as React from 'react' +import { messageOf } from './api' +import { styles } from './styles' + +const h = React.createElement + +export function Pill(props) { + var tone = styles[props.tone] === undefined ? styles.muted : styles[props.tone] + return h( + 'span', + { style: Object.assign({}, styles.pill, tone) }, + h('span', { style: { opacity: 0.75 } }, props.label), + h('span', { style: { fontWeight: 600 } }, String(props.value)), + ) +} + +export function Banner(props) { + var tone = styles[props.tone] === undefined ? styles.muted : styles[props.tone] + return h('div', { style: Object.assign({}, styles.banner, tone), role: props.role }, props.children) +} + +export function Field(props) { + return h( + 'label', + { style: styles.field }, + h('span', { style: styles.label }, props.label), + h('input', { + style: styles.input, + type: props.type === undefined ? 'text' : props.type, + value: props.value, + placeholder: props.placeholder === undefined ? '' : props.placeholder, + spellCheck: false, + autoComplete: 'off', + disabled: props.disabled === true, + onChange: function (event) { + props.onChange(event.target.value) + }, + }), + props.hint === undefined ? null : h('span', { style: styles.hint }, props.hint), + ) +} + +export function Btn(props) { + var base = props.variant === 'primary' ? styles.buttonPrimary : props.variant === 'danger' ? styles.buttonDanger : props.variant === 'small' ? styles.buttonSmall : styles.button + var style = props.disabled === true ? Object.assign({}, base, { opacity: 0.45, cursor: 'not-allowed' }) : base + return h( + 'button', + { + type: 'button', + style: style, + disabled: props.disabled === true, + onClick: props.onClick, + 'aria-label': props.ariaLabel, + }, + props.label, + ) +} + +export function IconBtn(props) { + var style = Object.assign({}, styles.iconBtn, props.danger === true ? styles.iconBtnDanger : null, props.disabled === true ? { opacity: 0.45, cursor: 'not-allowed' } : null) + return h( + 'button', + { + type: 'button', + style: style, + disabled: props.disabled === true, + onClick: props.disabled === true ? undefined : props.onClick, + 'aria-label': props.ariaLabel, + title: props.ariaLabel, + }, + props.glyph, + ) +} + +export function Switch(props) { return h( + 'div', + { style: styles.tabs, role: 'tablist', 'aria-label': props.ariaLabel }, + props.options.map(function (option) { + var active = option.value === props.value + return h( + 'button', + { + key: option.value, + type: 'button', + role: 'tab', + 'aria-selected': active, + style: Object.assign({}, styles.tab, active ? styles.tabActive : null), + onClick: function () { + props.onChange(option.value) + }, + }, + option.label, + ) + }), + ) +} + +// A real switch, not a checkbox: the tree rows are dense and the state has +// to be readable at a glance in both themes. +export function Toggle(props) { + var on = props.checked === true + var disabled = props.disabled === true + return h( + 'button', + { + type: 'button', + role: 'switch', + 'aria-checked': on, + 'aria-label': props.label, + title: props.label, + disabled: disabled, + style: { + width: '38px', + height: '22px', + flex: '0 0 auto', + padding: '2px', + borderRadius: '999px', + border: '1px solid ' + (on ? 'rgba(0,0,0,0.25)' : 'rgba(127,127,127,0.45)'), + background: on ? '#3f3f46' : 'rgba(127,127,127,0.28)', + display: 'flex', + alignItems: 'center', + justifyContent: on ? 'flex-end' : 'flex-start', + cursor: disabled ? 'not-allowed' : 'pointer', + opacity: disabled ? 0.45 : 1, + transition: 'background 120ms ease', + }, + onClick: disabled ? undefined : props.onChange, + }, + h('span', { + style: { + width: '16px', + height: '16px', + borderRadius: '999px', + background: 'rgba(255,255,255,0.96)', + boxShadow: '0 1px 2px rgba(0,0,0,0.25)', + flex: '0 0 auto', + }, + }), + ) +} + +export function Section(props) { + return h( + 'div', + { style: styles.section }, + h( + 'div', + { style: styles.sectionHead }, + h('h4', { style: styles.sectionTitle }, props.title), + props.aside === undefined ? null : h('div', { style: styles.actions }, props.aside), + ), + props.hint === undefined ? null : h('p', { style: styles.hint }, props.hint), + props.children, + ) +} + + +export function CardBoundary(props) { + React.Component.call(this, props) + this.state = { failure: null } +} + +CardBoundary.prototype = Object.create(React.Component.prototype) +CardBoundary.prototype.constructor = CardBoundary + +CardBoundary.getDerivedStateFromError = function (failure) { + return { failure: failure } +} + +CardBoundary.prototype.render = function () { + if (this.state.failure !== null) { + return h( + 'div', + { style: Object.assign({}, styles.root) }, + h('div', { style: Object.assign({}, styles.banner, styles.bad) }, '云端记忆设置卡渲染失败:' + messageOf(this.state.failure)), + h('p', { style: styles.hint }, '插件本身仍在运行,可改用 /dsh-brain 页面配置。'), + ) + } + return this.props.children +} diff --git a/src-client/format.ts b/src-client/format.ts new file mode 100644 index 0000000..b3aa7c4 --- /dev/null +++ b/src-client/format.ts @@ -0,0 +1,98 @@ +// dsh-brain client formatting helpers and label tables. +export function formatTime(value) { + if (typeof value !== 'number' || value <= 0) return '—' + var date = new Date(value) + var pad = function (n) { + return n < 10 ? '0' + String(n) : String(n) + } + return pad(date.getMonth() + 1) + '-' + pad(date.getDate()) + ' ' + pad(date.getHours()) + ':' + pad(date.getMinutes()) + ':' + pad(date.getSeconds()) +} + +export function formatIso(value) { + if (typeof value !== 'string' || value === '') return '—' + var stamp = Date.parse(value) + if (!Number.isFinite(stamp)) return value + return formatTime(stamp) +} + +export function formatBytes(value) { + if (typeof value !== 'number' || value < 0) return '—' + if (value < 1024) return String(value) + ' B' + if (value < 1024 * 1024) return (value / 1024).toFixed(1) + ' KB' + return (value / (1024 * 1024)).toFixed(2) + ' MB' +} + +export function shortHash(value) { + if (typeof value !== 'string' || value === '') return '—' + return value.slice(0, 12) +} + +export function readStored(key, fallback) { + try { + var raw = window.localStorage.getItem('dsh-brain:' + key) + if (raw === null || raw === '') raw = window.localStorage.getItem('dsh-cloud-memory:' + key) + return raw === null || raw === '' ? fallback : raw + } catch { + return fallback + } +} + +export function store(key, value) { + try { + window.localStorage.setItem('dsh-brain:' + key, String(value)) + } catch { + /* a locked-down window keeps the value in memory only */ + } +} + +export const OUTCOME_LABELS = { + recorded: '已上报', + duplicate: '重复忽略', + failed: '失败', + queued: '待重试', + skipped: '已跳过', +} + +export const OUTCOME_TONES = { + recorded: 'ok', + duplicate: 'muted', + failed: 'bad', + queued: 'warn', + skipped: 'muted', +} + +export const ORIGIN_LABELS = { + cloud: '云端下发', + uploaded: '本地已上传', + local: '本机安装', + unknown: '来源未知', +} + +export const CHECK_TONES = { + ok: 'ok', + unconfigured: 'muted', + invalid: 'bad', + forbidden: 'bad', + unreachable: 'bad', + error: 'bad', +} + +export const CHECK_LABELS = { + ok: '可用', + unconfigured: '未配置', + invalid: '令牌失效', + forbidden: '权限不足', + unreachable: '不可达', + error: '出错', +} + +export const MCP_STATE_LABELS = { + connected: '已连接', + connecting: '连接中', + disabled: '已停用', + error: '异常', + expired: '授权过期', + unknown: '未知', + needs_auth: '待授权', +} + diff --git a/src-client/index.ts b/src-client/index.ts new file mode 100644 index 0000000..a422bf3 --- /dev/null +++ b/src-client/index.ts @@ -0,0 +1,38 @@ +// dsh-brain client bundle entry — the __ModuleLoader__ module contract. +import * as React from 'react' +import { messageOf } from './api' +import { CardBoundary } from './components' +import { SettingsCard } from './SettingsCard' + +const h = React.createElement + +export const inject = ['slots'] + +export function apply(ctx: any): void { + const card = function () { + return h(CardBoundary as any, null, h(SettingsCard as any, null)) + } + + const register = function (seat: string, options: any) { + try { + ctx.slots.inject(seat, function () { + return ctx.slots.register(options, card) + }) + } catch (failure) { + if (typeof console !== 'undefined') console.warn('[dsh-brain] slot ' + seat + ' unavailable: ' + messageOf(failure)) + } + } + + register('settings.section', { + name: 'settings.section', + id: 'dsh-brain', + order: 100, + label: function () { + return '云端记忆' + }, + }) + + register('plugins.bundle.config', { name: 'plugins.bundle.config', key: 'dsh-brain' }) +} + +export const name = 'dsh-brain' diff --git a/src-client/sections/ImportSection.tsx b/src-client/sections/ImportSection.tsx new file mode 100644 index 0000000..ae71434 --- /dev/null +++ b/src-client/sections/ImportSection.tsx @@ -0,0 +1,301 @@ +// dsh-brain client section component. +import * as React from 'react' +import { API, request, postJson, messageOf, bodyError } from '../api' +import { formatTime, formatIso, formatBytes, shortHash, readStored, store, OUTCOME_LABELS, OUTCOME_TONES, ORIGIN_LABELS, CHECK_TONES, CHECK_LABELS, MCP_STATE_LABELS } from '../format' +import { styles } from '../styles' +import { Pill, Banner, Field, Btn, IconBtn, Switch, Toggle, Section, CardBoundary } from '../components' + +const h = React.createElement + +export function ImportSection(props) { + var onImported = props && typeof props.onImported === 'function' ? props.onImported : null + var tuple = React.useState({ expanded: false, status: 'idle', sources: [], open: {}, busy: '', notice: null, imported: {}, search: '', selection: [], progress: null }) + var s = tuple[0] + var set = tuple[1] + function patch(next) { set(function (prev) { return Object.assign({}, prev, next) }) } + function load() { + patch({ status: 'loading', notice: null }) + request(API.skills + '/sources', { method: 'GET' }).then(function (res) { + if (res.body !== null && res.body !== undefined && res.body.ok === true) { + patch({ status: 'ready', sources: Array.isArray(res.body.sources) ? res.body.sources : [] }) + } else patch({ status: 'error', notice: { tone: 'bad', text: bodyError(res, '读取来源失败') } }) + }, function (error) { patch({ status: 'error', notice: { tone: 'bad', text: messageOf(error) } }) }) + } + function openPanel() { + patch({ expanded: true }) + if (s.status === 'idle' || s.status === 'error') load() + } + function closePanel() { + patch({ expanded: false }) + } + function dshFolders() { + var found = {} + s.sources.forEach(function (source) { + if (!source.writable) return + var groups = source.groups || [] + groups.forEach(function (group) { + var skills = group.skills || [] + skills.forEach(function (skill) { found[skill.folder] = true }) + }) + }) + return found + } + function toggleSource(sourceId) { + var next = Object.assign({}, s.open) + var open = s.open[sourceId] !== false + next[sourceId] = !open + patch({ open: next }) + } + var mine = dshFolders() + var others = s.sources.filter(function (source) { return !source.writable }) + var needle = String(s.search).trim().toLowerCase() + function skillsOf(source) { + var list = [] + var groups = source.groups || [] + groups.forEach(function (group) { + var items = group.skills || [] + items.forEach(function (skill) { list.push(skill) }) + }) + return list + } + function importedYet(skill) { + return mine[skill.folder] === true || s.imported[skill.folder] === true + } + var candidates = [] + others.forEach(function (source) { + if (!source.exists) return + skillsOf(source).forEach(function (skill) { + candidates.push({ source: source, skill: skill }) + }) + }) + var matches = needle === '' ? candidates : candidates.filter(function (entry) { + var hay = String(entry.skill.name || '') + ' ' + String(entry.skill.folder || '') + ' ' + String(entry.skill.description || '') + return hay.toLowerCase().indexOf(needle) >= 0 + }) + var importable = matches.filter(function (entry) { return !importedYet(entry.skill) }) + var alreadyCount = candidates.length - candidates.filter(function (entry) { return !importedYet(entry.skill) }).length + function keyOf(entry) { + return String(entry.source.id) + ':' + String(entry.skill.folder) + } + var chosen = s.selection + function isChosen(entry) { + return chosen.indexOf(keyOf(entry)) >= 0 + } + function chooseMany(list, on) { + var next = chosen.slice() + list.forEach(function (entry) { + var key = keyOf(entry) + var at = next.indexOf(key) + if (on === true && at < 0) next.push(key) + if (on !== true && at >= 0) next.splice(at, 1) + }) + patch({ selection: next }) + } + var chosenEntries = candidates.filter(function (entry) { + return chosen.indexOf(keyOf(entry)) >= 0 && !importedYet(entry.skill) + }) + function importSelected() { + var targets = chosenEntries + if (targets.length === 0) return Promise.resolve() + patch({ busy: 'batch', notice: null, progress: { done: 0, total: targets.length } }) + var done = 0 + var failures = [] + var importedMap = Object.assign({}, s.imported) + var chain = Promise.resolve() + targets.forEach(function (entry) { + chain = chain.then(function () { + return postJson(API.skills + '/import', { sourceId: entry.source.id, folder: entry.skill.folder }).then( + function (res) { + if (res.body !== null && res.body !== undefined && res.body.ok === true) { + done += 1 + importedMap[entry.skill.folder] = true + } else failures.push(String(entry.skill.name) + '(' + bodyError(res, '失败') + ')') + patch({ progress: { done: done + failures.length, total: targets.length } }) + }, + function (error) { + failures.push(String(entry.skill.name) + '(' + messageOf(error) + ')') + patch({ progress: { done: done + failures.length, total: targets.length } }) + }, + ) + }) + }) + return chain.then(function () { + patch({ + busy: '', + progress: null, + selection: [], + imported: importedMap, + notice: { + tone: failures.length === 0 ? 'ok' : 'warn', + text: '已导入 ' + String(done) + ' / ' + String(targets.length) + ' 个技能到 DSH' + (failures.length === 0 ? '。' : ';失败:' + failures.join('、')), + }, + }) + if (onImported !== null) onImported() + load() + }) + } + if (!s.expanded) { + return h( + 'div', + { style: { display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: '8px', flexWrap: 'wrap' } }, + h(Btn, { + label: '从其他软件导入', + ariaLabel: '从其他软件导入', + onClick: openPanel, + }), + s.notice === null ? null : h(Banner, { tone: s.notice.tone }, s.notice.text), + ) + } + function describe(skill) { + var text = String(skill.description === undefined || skill.description === null ? '' : skill.description).trim() + if (/^[>|][+-]?$/.test(text)) return '' + return text + } + var blocks = others.map(function (source) { + var all = skillsOf(source) + var visible = matches.filter(function (entry) { return entry.source.id === source.id }) + var open = s.open[source.id] !== false + var selectable = visible.filter(function (entry) { return !importedYet(entry.skill) }) + return h('div', { key: source.id, style: styles.row }, + h('div', { style: styles.rowHead }, + h('span', { style: { display: 'inline-flex', alignItems: 'center', gap: '8px', minWidth: 0 } }, + h('span', { style: styles.sourceGlyph }, String(source.label).slice(0, 1)), + h('span', { style: styles.sourceTitle }, String(source.label)), + h( + 'span', + { style: styles.hint }, + source.exists + ? String(all.length) + ' 个技能' + (needle === '' ? '' : ' · 匹配 ' + String(visible.length)) + ' · 可导入 ' + String(selectable.length) + : '未安装', + ), + ), + source.exists + ? h('div', { style: styles.actions }, + h(Btn, { + label: '全选本来源(' + String(selectable.length) + ')', + ariaLabel: '全选本来源 ' + String(source.label), + variant: 'small', + disabled: selectable.length === 0 || s.busy !== '', + onClick: function () { chooseMany(visible, true) }, + }), + h(Btn, { label: open ? '收起' : '展开', variant: 'small', onClick: function () { toggleSource(source.id) } }), + ) + : null, + ), + h('span', { style: styles.mono }, String(source.root)), + !source.exists || !open + ? null + : visible.length === 0 + ? h('p', { style: styles.hint }, needle === '' ? '这个来源里还没有技能。' : '没有匹配「' + s.search + '」的技能。') + : h( + 'div', + { style: { display: 'flex', flexDirection: 'column', gap: '2px', maxHeight: '320px', overflow: 'auto' } }, + visible.map(function (entry) { + var skill = entry.skill + var has = importedYet(skill) + return h( + 'label', + { key: String(skill.folder), style: styles.checkboxRow }, + h('input', { + type: 'checkbox', + checked: has || isChosen(entry), + disabled: has || s.busy !== '', + 'aria-label': '选择技能 ' + String(skill.name), + onChange: function (event) { chooseMany([entry], event.target.checked === true) }, + }), + h( + 'span', + { style: { minWidth: 0, flex: '1 1 auto' } }, + h('span', { style: styles.rowTitle }, String(skill.name)), + describe(skill) === '' ? null : h('span', { style: Object.assign({}, styles.hint, styles.clamp1) }, describe(skill)), + ), + has ? h(Pill, { label: 'DSH', value: '已导入', tone: 'muted' }) : null, + ) + }), + ), + ) + }) + return h( + Section, + { + title: '从其他软件导入', + hint: '把其他软件技能目录里的技能复制一份到 DSH,原目录不动;同名技能不能重复导入。', + aside: h( + 'div', + { style: styles.actions }, + h(Btn, { label: '刷新', variant: 'small', onClick: load }), + h(Btn, { label: '收起', variant: 'small', onClick: closePanel }), + ), + }, + s.status === 'loading' ? h('p', { style: styles.hint }, '正在读取来源…') + : others.length === 0 ? h('p', { style: styles.hint }, '没有检测到其他软件的技能目录。') + : h( + 'div', + { style: styles.root }, + h( + 'div', + { style: styles.pills }, + h(Pill, { label: '来源', value: String(others.length), tone: 'muted' }), + h(Pill, { label: '可导入', value: String(candidates.length - alreadyCount), tone: 'muted' }), + s.search === '' ? null : h(Pill, { label: '匹配', value: String(matches.length), tone: matches.length === 0 ? 'muted' : 'ok' }), + h(Pill, { label: '已选', value: String(chosenEntries.length), tone: chosenEntries.length === 0 ? 'muted' : 'ok' }), + alreadyCount === 0 ? null : h(Pill, { label: '已在 DSH', value: String(alreadyCount), tone: 'muted' }), + ), + h( + 'div', + { style: { display: 'flex', gap: '8px', alignItems: 'center', flexWrap: 'wrap' } }, + h('input', { + type: 'search', + style: Object.assign({}, styles.input, { flex: '1 1 220px', minWidth: 0 }), + value: s.search, + placeholder: '搜索技能:名称 / 目录 / 描述', + 'aria-label': '搜索要导入的技能', + onChange: function (event) { patch({ search: event.target.value }) }, + }), + s.search === '' ? null : h(Btn, { + label: '清空搜索', + variant: 'small', + onClick: function () { patch({ search: '' }) }, + }), + ), + h( + 'div', + { style: Object.assign({}, styles.actions, { flexWrap: 'wrap' }) }, + h(Btn, { + label: '全选匹配可导入(' + String(importable.length) + ')', + ariaLabel: '全选匹配可导入', + variant: 'small', + disabled: importable.length === 0 || s.busy !== '', + onClick: function () { chooseMany(importable, true) }, + }), + h(Btn, { + label: '全选全部(' + String(candidates.length - alreadyCount) + ')', + ariaLabel: '全选全部', + variant: 'small', + disabled: candidates.length - alreadyCount === 0 || s.busy !== '', + onClick: function () { + chooseMany(candidates.filter(function (entry) { return !importedYet(entry.skill) }), true) + }, + }), + h(Btn, { + label: '清空已选', + ariaLabel: '清空已选', + variant: 'small', + disabled: chosenEntries.length === 0, + onClick: function () { patch({ selection: [] }) }, + }), + h(Btn, { + variant: 'primary', + label: s.busy === 'batch' ? '导入中…' : '导入所选(' + String(chosenEntries.length) + ')', + ariaLabel: '导入所选', + disabled: s.busy !== '' || chosenEntries.length === 0, + onClick: importSelected, + }), + ), + s.progress === null ? null : h('p', { style: styles.hint }, '正在导入:' + String(s.progress.done) + ' / ' + String(s.progress.total)), + h('div', { style: { display: 'flex', flexDirection: 'column', gap: '10px' } }, blocks), + ), + s.notice === null ? null : h(Banner, { tone: s.notice.tone }, s.notice.text), + ) +} + diff --git a/src-client/sections/MarketSection.tsx b/src-client/sections/MarketSection.tsx new file mode 100644 index 0000000..a522d5a --- /dev/null +++ b/src-client/sections/MarketSection.tsx @@ -0,0 +1,164 @@ +// dsh-brain client section component. +import * as React from 'react' +import { API, request, postJson, messageOf, bodyError } from '../api' +import { formatTime, formatIso, formatBytes, shortHash, readStored, store, OUTCOME_LABELS, OUTCOME_TONES, ORIGIN_LABELS, CHECK_TONES, CHECK_LABELS, MCP_STATE_LABELS } from '../format' +import { styles } from '../styles' +import { Pill, Banner, Field, Btn, IconBtn, Switch, Toggle, Section, CardBoundary } from '../components' + +const h = React.createElement + +export function MarketSection(props) { + var capabilities = props.capabilities || {} + var tuple = React.useState({ + providers: null, provider: '', query: '', entries: [], nextCursor: null, cursor: 0, + status: 'idle', error: null, detail: null, plan: null, selected: {}, busy: '', notice: null, + }) + var state = tuple[0] + var setState = tuple[1] + function patch(next) { setState(function (prev) { return Object.assign({}, prev, next) }) } + var available = capabilities.features !== undefined && capabilities.features.skillsMarketplace === true + + React.useEffect(function () { + if (available) loadProviders() + }, []) + + function loadProviders() { + request(API.market + '/providers', { method: 'GET' }).then(function (res) { + if (res.body !== null && res.body !== undefined && res.body.ok === true) { + var list = Array.isArray(res.body.providers) ? res.body.providers : [] + patch({ providers: list, provider: state.provider !== '' ? state.provider : (list.length > 0 ? list[0].id : '') }) + } else { + patch({ error: bodyError(res, '读取市场来源失败') }) + } + }, function (error) { patch({ error: messageOf(error) }) }) + } + function search(append) { + if (state.provider === '') return + var cursor = append === true ? state.cursor : 0 + patch({ busy: 'search', error: null, status: 'loading' }) + var url = API.market + '/search?provider=' + encodeURIComponent(state.provider) + '&q=' + encodeURIComponent(state.query) + '&cursor=' + String(cursor) + request(url, { method: 'GET' }).then(function (res) { + if (res.body !== null && res.body !== undefined && res.body.ok === true) { + var entries = append === true ? state.entries.concat(res.body.entries || []) : (res.body.entries || []) + patch({ entries: entries, nextCursor: res.body.nextCursor === undefined ? null : res.body.nextCursor, cursor: res.body.nextCursor === undefined ? cursor : res.body.nextCursor, status: 'ready', busy: '', detail: null, plan: null }) + } else { + patch({ status: 'error', busy: '', error: bodyError(res, '市场搜索失败') }) + } + }, function (error) { patch({ status: 'error', busy: '', error: messageOf(error) }) }) + } + function openDetail(entry) { + patch({ busy: 'detail', detail: null, plan: null, notice: null }) + postJson(API.market + '/detail', { provider: state.provider, id: entry.id }).then(function (res) { + if (res.body !== null && res.body !== undefined && res.body.ok === true) patch({ detail: res.body, busy: '' }) + else patch({ busy: '', error: bodyError(res, '读取条目详情失败') }) + }, function (error) { patch({ busy: '', error: messageOf(error) }) }) + } + function makePlan() { + if (state.detail === null) return + patch({ busy: 'plan', plan: null, notice: null }) + postJson(API.market + '/plan', { provider: state.provider, id: state.detail.id }).then(function (res) { + if (res.body !== null && res.body !== undefined && res.body.ok === true) { + var selected = {} + ;(res.body.candidates || []).forEach(function (candidate) { + if (candidate.conflict !== true && candidate.problem === undefined) selected[candidate.folder] = true + }) + patch({ plan: res.body, selected: selected, busy: '' }) + } else patch({ busy: '', error: bodyError(res, '无法生成安装计划') }) + }, function (error) { patch({ busy: '', error: messageOf(error) }) }) + } + function toggleFolder(folder) { + var next = Object.assign({}, state.selected) + next[folder] = next[folder] !== true + patch({ selected: next }) + } + function commitPlan() { + if (state.plan === null) return + var folders = Object.keys(state.selected).filter(function (folder) { return state.selected[folder] === true }) + if (folders.length === 0) { patch({ notice: { tone: 'warn', text: '请至少选中一个技能。' } }); return } + patch({ busy: 'commit' }) + postJson(API.market + '/commit', { planId: state.plan.planId, folders: folders }).then(function (res) { + if (res.body !== null && res.body !== undefined && res.body.ok === true) { + var skipped = Array.isArray(res.body.skipped) ? res.body.skipped.length : 0 + patch({ busy: '', plan: null, detail: null, notice: { tone: 'ok', text: '已安装:' + ((res.body.installed || []).join('、') || '无') + (skipped > 0 ? ';跳过 ' + skipped + ' 项' : '') } }) + if (typeof props.onInstalled === 'function') props.onInstalled() + } else patch({ busy: '', notice: { tone: 'bad', text: bodyError(res, '安装失败') } }) + }, function (error) { patch({ busy: '', notice: { tone: 'bad', text: messageOf(error) } }) }) + } + + if (!available) { + return h(Section, { title: '技能市场', hint: '本版没有配置经验证的市场来源。' }, + h('p', { style: styles.hint }, '能力缺口:capabilities.features.skillsMarketplace = false,因此不显示任何搜索框或示例条目。')) + } + var providerLabel = state.provider + if (state.providers !== null) { + state.providers.forEach(function (provider) { if (provider.id === state.provider) providerLabel = provider.label }) + } + var resultRows = state.entries.map(function (entry) { + return h('div', { key: entry.id, style: styles.row }, + h('div', { style: styles.rowHead }, + h('span', { style: styles.rowTitle }, entry.name), + h('div', { style: styles.pills }, + entry.version === undefined ? null : h(Pill, { label: '版本', value: String(entry.version), tone: 'muted' }), + h(Pill, { label: '技能', value: (entry.skills || []).length, tone: 'muted' }), + entry.author === undefined ? null : h(Pill, { label: '作者', value: String(entry.author), tone: 'muted' }), + ), + ), + h('p', { style: styles.hint }, entry.description === '' ? '(无描述)' : entry.description), + h('div', { style: styles.actions }, h(Btn, { label: '查看详情', variant: 'small', disabled: state.busy !== '', onClick: function () { openDetail(entry) } })), + ) + }) + var detailBox = null + if (state.detail !== null) { + var details = Array.isArray(state.detail.skillDetails) ? state.detail.skillDetails : [] + detailBox = h('div', { style: styles.nested }, + h('p', { style: styles.hint }, '来源:' + String(state.detail.sourceRepo) + ' · ' + (state.detail.description || '')), + h('div', { style: { display: 'flex', flexDirection: 'column', gap: '4px' } }, + details.map(function (skill) { + return h('div', { key: skill.folder, style: styles.rowHead }, + h('span', { style: styles.rowTitle }, skill.name === undefined ? skill.folder : skill.name), + h('span', { style: styles.hint }, skill.ok === false ? '(无法读取 SKILL.md)' : String(skill.description || '')), + ) + }), + ), + h('div', { style: styles.actions }, h(Btn, { variant: 'primary', label: state.busy === 'plan' ? '拉取中…' : '拉取安装计划', disabled: state.busy !== '', onClick: makePlan })), + ) + } + var planBox = null + if (state.plan !== null) { + var candidates = Array.isArray(state.plan.candidates) ? state.plan.candidates : [] + planBox = h('div', { style: styles.nested }, + h('p', { style: styles.hint }, '已固定提交:' + (state.plan.commit === undefined ? '克隆时记录实际提交' : String(state.plan.commit).slice(0, 12)) + ';只安装勾选项,冲突项默认不勾,不执行仓库脚本。'), + candidates.length === 0 ? h('p', { style: styles.hint }, '该条目没有可安装的技能。') : null, + h('div', { style: { display: 'flex', flexDirection: 'column', gap: '6px' } }, + candidates.map(function (candidate) { + return h('label', { key: candidate.folder, style: styles.checkboxRow }, + h('input', { type: 'checkbox', checked: state.selected[candidate.folder] === true, disabled: candidate.conflict === true || candidate.problem !== undefined, onChange: function () { toggleFolder(candidate.folder) } }), + h('span', null, candidate.folder + ' · ' + String(candidate.files) + ' 个文件 · ' + formatBytes(candidate.bytes) + (candidate.conflict === true ? ' · 已存在,跳过' : '') + (candidate.problem === undefined ? '' : ' · ' + candidate.problem)), + ) + }), + ), + h('div', { style: styles.actions }, h(Btn, { variant: 'primary', label: state.busy === 'commit' ? '安装中…' : '安装选中', disabled: state.busy !== '', onClick: commitPlan })), + ) + } + return h(Section, { title: '技能市场', hint: '来自经验证的公开市场清单;安装走暂存计划、只装勾选项并固定提交。' }, + h('div', { style: { display: 'flex', flexDirection: 'column', gap: '10px' } }, + h('div', { style: styles.pills }, + h(Pill, { label: '来源', value: String(providerLabel || '未配置'), tone: 'muted' }), + state.providers === null ? null : h(Pill, { label: '已接入', value: state.providers.length, tone: 'ok' }), + ), + h('div', { style: { display: 'flex', gap: '8px', alignItems: 'flex-end', flexWrap: 'wrap' } }, + h('div', { style: { flex: '1 1 220px', minWidth: 0 } }, h(Field, { label: '搜索市场', value: state.query, placeholder: '例如 xlsx / pdf / design', onChange: function (value) { patch({ query: value }) } })), + h(Btn, { variant: 'primary', label: state.busy === 'search' ? '搜索中…' : '搜索', disabled: state.busy !== '', onClick: function () { search(false) } }), + ), + state.error === null ? null : h(Banner, { tone: 'bad' }, state.error), + state.notice === null ? null : h(Banner, { tone: state.notice.tone }, state.notice.text), + state.status === 'ready' && state.entries.length === 0 ? h('p', { style: styles.hint }, '没有匹配的市场条目。') : null, + h('div', { style: { display: 'flex', flexDirection: 'column', gap: '8px' } }, resultRows), + state.nextCursor === null ? null : h('div', { style: styles.actions }, h(Btn, { label: '加载更多', variant: 'small', disabled: state.busy !== '', onClick: function () { search(true) } })), + detailBox, + planBox, + ), + ) +} + +// ---- P3: interactive sync conflict resolution ------------------------- diff --git a/src-client/sections/McpConfigSection.tsx b/src-client/sections/McpConfigSection.tsx new file mode 100644 index 0000000..35f4a6c --- /dev/null +++ b/src-client/sections/McpConfigSection.tsx @@ -0,0 +1,142 @@ +// dsh-brain client section component. +import * as React from 'react' +import { API, request, postJson, messageOf, bodyError } from '../api' +import { formatTime, formatIso, formatBytes, shortHash, readStored, store, OUTCOME_LABELS, OUTCOME_TONES, ORIGIN_LABELS, CHECK_TONES, CHECK_LABELS, MCP_STATE_LABELS } from '../format' +import { styles } from '../styles' +import { Pill, Banner, Field, Btn, IconBtn, Switch, Toggle, Section, CardBoundary } from '../components' + +const h = React.createElement +import { bridgeCall, bridgeMessage } from '../bridge' + +export function McpConfigSection(props) { + var items = Array.isArray(props.items) ? props.items : [] + var open = React.useState(false) + var tuple = React.useState({ key: '', json: '', busy: '', notice: null, scopePreview: null, targetScope: 'global', workspaceId: '', gov: null, policyPreview: null }) + var s = tuple[0] + var set = tuple[1] + function patch(next) { set(function (prev) { return Object.assign({}, prev, next) }) } + var selected = null + for (var i = 0; i < items.length; i += 1) if (items[i].key === s.key) selected = items[i] + React.useEffect(function () { + bridgeCall('governance', {}).then(function (res) { + if (res.body !== null && res.body.ok === true) patch({ gov: res.body.detail }) + }) + }, []) + function choose(key) { + patch({ key: key, json: '', scopePreview: null, policyPreview: null, notice: null }) + if (key === '') return + bridgeCall('editableConnectionConfig', { key: key }).then(function (res) { + if (res.body !== null && res.body.ok === true && res.body.detail !== undefined) patch({ json: String(res.body.detail.json === undefined ? '' : res.body.detail.json) }) + else patch({ json: '', notice: { tone: 'warn', text: bridgeMessage(res) } }) + }) + } + function save() { + if (s.key === '') return + patch({ busy: 'save' }) + bridgeCall('reconfigureConnection', { key: s.key, json: s.json }).then(function (res) { + var ok = res.body !== null && res.body.ok === true + patch({ busy: '', notice: ok ? { tone: 'ok', text: bridgeMessage(res) } : { tone: 'bad', text: '保存失败:' + bridgeMessage(res) } }) + if (ok && typeof props.onChanged === 'function') props.onChanged() + }) + } + function previewScope() { + if (s.key === '') return + patch({ busy: 'scope-preview' }) + var params: any = { key: s.key, targetScope: s.targetScope, mode: 'move' } + if (s.targetScope === 'project') params.workspaceId = s.workspaceId + bridgeCall('previewConnectionScope', params).then(function (res) { + var ok = res.body !== null && res.body.ok === true + patch({ busy: '', scopePreview: ok ? res.body.detail : null, notice: ok ? null : { tone: 'bad', text: bridgeMessage(res) } }) + if (ok && res.body.detail.canApply !== true) patch({ notice: { tone: 'warn', text: bridgeMessage(res) } }) + }) + } + function applyScope() { + if (s.key === '' || s.scopePreview === null) return + patch({ busy: 'scope-apply' }) + var params: any = { key: s.key, targetScope: s.targetScope, mode: 'move', expectedRevision: s.scopePreview.baseRevision } + if (s.targetScope === 'project') params.workspaceId = s.workspaceId + bridgeCall('applyConnectionScope', params).then(function (res) { + var ok = res.body !== null && res.body.ok === true + patch({ busy: '', notice: ok ? { tone: 'ok', text: bridgeMessage(res) } : { tone: 'bad', text: '作用域应用失败:' + bridgeMessage(res) }, scopePreview: null }) + if (ok && typeof props.onChanged === 'function') props.onChanged() + }) + } + function previewPolicy(effect) { + if (selected === null) return + patch({ busy: 'policy-preview' }) + bridgeCall('previewPolicy', { scope: 'connection', effect: effect, connectorId: selected.connectorId }).then(function (res) { + var ok = res.body !== null && res.body.ok === true + patch({ busy: '', policyPreview: ok ? res.body.detail : null, notice: ok ? { tone: 'ok', text: bridgeMessage(res) } : { tone: 'bad', text: '策略预览失败:' + bridgeMessage(res) } }) + }) + } + function applyPolicy() { + if (selected === null || s.policyPreview === null) return + patch({ busy: 'policy-apply' }) + bridgeCall('applyPolicy', { scope: 'connection', effect: s.policyPreview.mutation.effect, connectorId: selected.connectorId, expectedRevision: s.policyPreview.baseRevision }).then(function (res) { + var ok = res.body !== null && res.body.ok === true + patch({ busy: '', policyPreview: null, notice: ok ? { tone: 'ok', text: bridgeMessage(res) } : { tone: 'bad', text: '策略应用失败:' + bridgeMessage(res) } }) + if (ok) bridgeCall('governance', {}).then(function (g) { if (g.body !== null && g.body.ok === true) patch({ gov: g.body.detail }) }) + }) + } + var gov = s.gov + return h(Section, { + title: '连接配置、范围与治理', + hint: '配置编辑使用宿主的保留标记合并,空输入不会清空已有密钥;作用域与策略都带 revision 预览后再提交。', + aside: h(Btn, { + label: open[0] ? '收起' : '展开', + ariaLabel: '展开连接配置', + variant: 'small', + onClick: function () { open[1](!open[0]) }, + }), + }, + open[0] ? h('div', { style: { display: 'flex', flexDirection: 'column', gap: '12px' } }, + itemSelect(items, s.key, choose), + s.key === '' ? h('p', { style: styles.hint }, '选择一条连接后可编辑配置、调整范围或设置治理策略。') : h('div', { style: styles.nested }, + h('p', { style: styles.hint }, '可编辑配置(保留标记 表示保留原密钥):'), + h('textarea', { value: s.json, rows: 8, style: Object.assign({}, styles.input, { fontFamily: 'monospace', resize: 'vertical' }), onChange: function (e) { patch({ json: e.target.value }) } }), + h('div', { style: styles.actions }, h(Btn, { variant: 'primary', label: s.busy === 'save' ? '保存中…' : '保存配置', disabled: s.busy !== '' || s.json === '', onClick: save })), + h('p', { style: styles.hint }, '作用域(目标范围 + mode=move,凭据不复制):'), + h('div', { style: styles.grid }, + h('div', { style: styles.field }, + h('span', { style: styles.label }, '目标范围'), + h('select', { value: s.targetScope, style: styles.input, onChange: function (e) { patch({ targetScope: e.target.value }) } }, + h('option', { value: 'global' }, '全局'), + h('option', { value: 'project' }, '指定工作区'), + ), + ), + s.targetScope === 'project' ? h(Field, { label: '工作区 id', value: s.workspaceId, onChange: function (v) { patch({ workspaceId: v }) } }) : null, + ), + h('div', { style: styles.actions }, + h(Btn, { label: s.busy === 'scope-preview' ? '预览中…' : '预览作用域', disabled: s.busy !== '', onClick: previewScope }), + s.scopePreview === null ? null : h(Btn, { variant: 'primary', label: s.busy === 'scope-apply' ? '应用中…' : '应用作用域(rev ' + String(s.scopePreview.baseRevision) + ')', disabled: s.busy !== '' || s.scopePreview.canApply !== true, onClick: applyScope }), + ), + s.scopePreview === null ? null : h('p', { style: styles.hint }, '影响:' + String(s.scopePreview.impact === undefined ? '' : JSON.stringify(s.scopePreview.impact)) + ';可应用:' + (s.scopePreview.canApply === true ? '是' : '否') + ';凭据复制:否'), + h('p', { style: styles.hint }, '治理(当前 connection 级):'), + gov === null ? h('p', { style: styles.hint }, '治理文档未读取。') : h('div', { style: styles.pills }, + h(Pill, { label: 'revision', value: gov.revision, tone: 'muted' }), + h(Pill, { label: '规则', value: (gov.rules || []).length, tone: 'muted' }), + h(Pill, { label: '优先级', value: String(gov.precedenceLabel || '—'), tone: 'muted' }), + ), + h('div', { style: styles.actions }, + h(Btn, { label: '预览 allow', variant: 'small', disabled: s.busy !== '' || selected === null, onClick: function () { previewPolicy('allow') } }), + h(Btn, { label: '预览 deny', variant: 'small', disabled: s.busy !== '' || selected === null, onClick: function () { previewPolicy('deny') } }), + h(Btn, { label: '预览 inherit', variant: 'small', disabled: s.busy !== '' || selected === null, onClick: function () { previewPolicy('inherit') } }), + s.policyPreview === null ? null : h(Btn, { variant: 'primary', label: s.busy === 'policy-apply' ? '应用中…' : '应用策略(rev ' + String(s.policyPreview.baseRevision) + ')', disabled: s.busy !== '', onClick: applyPolicy }), + ), + ), + s.notice === null ? null : h(Banner, { tone: s.notice.tone }, s.notice.text), + ) : null, + ) +} + +export function itemSelect(items, value, onChange) { + return h('div', { style: styles.field }, + h('span', { style: styles.label }, '连接'), + h('select', { value: value, style: styles.input, onChange: function (e) { onChange(e.target.value) } }, + h('option', { value: '' }, '选择连接…'), + items.map(function (item) { return h('option', { key: item.key, value: item.key }, String(item.name || item.key) + '(' + String(item.transport || '') + ')') }), + ), + ) +} + +// ---- import skills from other tools ------------------------------- diff --git a/src-client/sections/McpCustomSection.tsx b/src-client/sections/McpCustomSection.tsx new file mode 100644 index 0000000..829a97c --- /dev/null +++ b/src-client/sections/McpCustomSection.tsx @@ -0,0 +1,99 @@ +// dsh-brain client section component. +import * as React from 'react' +import { API, request, postJson, messageOf, bodyError } from '../api' +import { formatTime, formatIso, formatBytes, shortHash, readStored, store, OUTCOME_LABELS, OUTCOME_TONES, ORIGIN_LABELS, CHECK_TONES, CHECK_LABELS, MCP_STATE_LABELS } from '../format' +import { styles } from '../styles' +import { Pill, Banner, Field, Btn, IconBtn, Switch, Toggle, Section, CardBoundary } from '../components' + +const h = React.createElement +import { bridgeCall, bridgeMessage } from '../bridge' + +export function McpCustomSection(props) { + var open = React.useState(false) + var tuple = React.useState({ transport: 'streamable-http', name: '', serverName: '', url: '', command: '', args: '', cwd: '', authMode: 'none', bearerToken: '', apiKeyHeader: 'X-Api-Key', apiKeyValue: '', importJson: '', busy: '', notice: null }) + var s = tuple[0] + var set = tuple[1] + function patch(next) { set(function (prev) { return Object.assign({}, prev, next) }) } + function submit() { + if (String(s.name).trim() === '') { patch({ notice: { tone: 'warn', text: '请填写连接名称。' } }); return } + var params: any = { name: String(s.name).trim(), serverName: String(s.serverName).trim() || undefined, transport: s.transport, authMode: s.authMode } + if (s.transport === 'stdio') { + if (String(s.command).trim() === '') { patch({ notice: { tone: 'warn', text: 'stdio 连接需要启动命令。' } }); return } + params.command = String(s.command).trim() + params.args = String(s.args).split(/\s+/).filter(function (x) { return x !== '' }) + if (String(s.cwd).trim() !== '') params.cwd = String(s.cwd).trim() + } else { + if (String(s.url).trim() === '') { patch({ notice: { tone: 'warn', text: 'HTTP 连接需要 URL。' } }); return } + params.url = String(s.url).trim() + if (s.authMode === 'bearer') params.bearerToken = String(s.bearerToken) + if (s.authMode === 'api-key') { params.apiKeyHeader = String(s.apiKeyHeader); params.apiKeyValue = String(s.apiKeyValue) } + } + patch({ busy: 'configure', notice: null }) + bridgeCall('configure', params).then(function (res) { + var ok = res.status === 200 && res.body !== null && res.body.ok === true + patch({ busy: '', notice: ok ? { tone: 'ok', text: bridgeMessage(res) } : { tone: 'bad', text: '新增失败:' + bridgeMessage(res) } }) + if (ok && typeof props.onChanged === 'function') props.onChanged() + }) + } + function importJson() { + if (String(s.importJson).trim() === '') { patch({ notice: { tone: 'warn', text: '请粘贴 mcpServers JSON。' } }); return } + patch({ busy: 'import', notice: null }) + bridgeCall('importJson', { json: s.importJson }).then(function (res) { + var ok = res.status === 200 && res.body !== null && res.body.ok === true + patch({ busy: '', notice: ok ? { tone: 'ok', text: bridgeMessage(res) } : { tone: 'bad', text: '导入失败:' + bridgeMessage(res) } }) + if (ok && typeof props.onChanged === 'function') props.onChanged() + }) + } + return h(Section, { + title: '新增 / 导入连接', + hint: 'HTTP 与 stdio 都由宿主校验并保存凭据;本卡不回显已有密钥。stdio 的启动命令由你显式填写,云端不会执行任何本机命令。', + aside: h(Btn, { + label: open[0] ? '收起' : '展开', + ariaLabel: '展开新增连接', + variant: 'small', + onClick: function () { open[1](!open[0]) }, + }), + }, + open[0] ? h('div', { style: { display: 'flex', flexDirection: 'column', gap: '10px' } }, + h('div', { style: styles.grid }, + h(Field, { label: '名称', value: s.name, onChange: function (v) { patch({ name: v }) } }), + h(Field, { label: 'Server 名(可选)', value: s.serverName, onChange: function (v) { patch({ serverName: v }) } }), + h('div', { style: styles.field }, + h('span', { style: styles.label }, '传输方式'), + h('select', { value: s.transport, style: styles.input, onChange: function (e) { patch({ transport: e.target.value }) } }, + h('option', { value: 'streamable-http' }, 'Streamable HTTP'), + h('option', { value: 'stdio' }, 'stdio(本机进程)'), + ), + ), + ), + s.transport === 'stdio' + ? h('div', { style: styles.grid }, + h(Field, { label: '启动命令', value: s.command, placeholder: 'npx / uvx / 本机可执行文件', onChange: function (v) { patch({ command: v }) } }), + h(Field, { label: '参数(空格分隔)', value: s.args, onChange: function (v) { patch({ args: v }) } }), + h(Field, { label: '工作目录(可选)', value: s.cwd, onChange: function (v) { patch({ cwd: v }) } }), + ) + : h('div', { style: { display: 'flex', flexDirection: 'column', gap: '10px' } }, + h(Field, { label: 'URL', value: s.url, placeholder: 'https://…', onChange: function (v) { patch({ url: v }) } }), + h('div', { style: styles.grid }, + h('div', { style: styles.field }, + h('span', { style: styles.label }, '鉴权'), + h('select', { value: s.authMode, style: styles.input, onChange: function (e) { patch({ authMode: e.target.value }) } }, + h('option', { value: 'none' }, '无'), + h('option', { value: 'bearer' }, 'Bearer Token'), + h('option', { value: 'api-key' }, 'API Key'), + ), + ), + s.authMode === 'bearer' ? h(Field, { label: 'Bearer Token', type: 'password', value: s.bearerToken, onChange: function (v) { patch({ bearerToken: v }) } }) : null, + s.authMode === 'api-key' ? h(Field, { label: 'API Key 头', value: s.apiKeyHeader, onChange: function (v) { patch({ apiKeyHeader: v }) } }) : null, + s.authMode === 'api-key' ? h(Field, { label: 'API Key 值', type: 'password', value: s.apiKeyValue, onChange: function (v) { patch({ apiKeyValue: v }) } }) : null, + ), + ), + h('div', { style: styles.actions }, h(Btn, { variant: 'primary', label: s.busy === 'configure' ? '保存中…' : '校验并保存连接', disabled: s.busy !== '', onClick: submit })), + h('p', { style: styles.hint }, '或粘贴 mcpServers JSON 批量导入:'), + h('textarea', { value: s.importJson, rows: 5, style: Object.assign({}, styles.input, { fontFamily: 'monospace', resize: 'vertical' }), placeholder: '{"mcpServers":{"name":{"url":"…"}}}', onChange: function (e) { patch({ importJson: e.target.value }) } }), + h('div', { style: styles.actions }, h(Btn, { label: s.busy === 'import' ? '导入中…' : '导入 JSON', disabled: s.busy !== '', onClick: importJson })), + s.notice === null ? null : h(Banner, { tone: s.notice.tone }, s.notice.text), + ) : null, + ) +} + diff --git a/src-client/sections/SyncResolveSection.tsx b/src-client/sections/SyncResolveSection.tsx new file mode 100644 index 0000000..04cb5eb --- /dev/null +++ b/src-client/sections/SyncResolveSection.tsx @@ -0,0 +1,118 @@ +// dsh-brain client section component. +import * as React from 'react' +import { API, request, postJson, messageOf, bodyError } from '../api' +import { formatTime, formatIso, formatBytes, shortHash, readStored, store, OUTCOME_LABELS, OUTCOME_TONES, ORIGIN_LABELS, CHECK_TONES, CHECK_LABELS, MCP_STATE_LABELS } from '../format' +import { styles } from '../styles' +import { Pill, Banner, Field, Btn, IconBtn, Switch, Toggle, Section, CardBoundary } from '../components' + +const h = React.createElement + +export function SyncResolveSection(props) { + var tuple = React.useState({ status: 'idle', data: null, busy: '', error: null, notice: null, pendingAll: null }) + var state = tuple[0] + var setState = tuple[1] + function patch(next) { setState(function (prev) { return Object.assign({}, prev, next) }) } + React.useEffect(function () { load() }, []) + function load() { + patch({ status: 'loading', error: null }) + request(API.sync + '/status', { method: 'GET' }).then(function (res) { + if (res.body !== null && res.body !== undefined && res.body.ok === true) patch({ status: 'ready', data: res.body }) + else patch({ status: 'error', error: bodyError(res, '读取同步状态失败') }) + }, function (error) { patch({ status: 'error', error: messageOf(error) }) }) + } + function resolve(name, action, conflictId) { + patch({ busy: name + ':' + action, notice: null }) + postJson(API.sync + '/resolve', { name: name, action: action, conflictId: conflictId }).then(function (res) { + if (res.body !== null && res.body !== undefined && res.body.ok === true) { + var stats = state.data === null ? null : state.data.stats + patch({ busy: '', data: { stats: stats, conflicts: res.body.conflicts || [] }, notice: { tone: 'ok', text: name + ':' + (res.body.note || '已处理') } }) + if (typeof props.onResolved === 'function') props.onResolved() + } else patch({ busy: '', notice: { tone: 'bad', text: bodyError(res, '处理冲突失败') } }) + }, function (error) { patch({ busy: '', notice: { tone: 'bad', text: messageOf(error) } }) }) + } + function runNow() { + patch({ busy: 'run' }) + postJson(API.sync + '/run', {}).then(function (res) { + if (res.body !== null && res.body !== undefined && res.body.ok === true) patch({ busy: '', data: { stats: res.body.stats, conflicts: res.body.conflicts || [] }, notice: { tone: 'ok', text: '已执行一次同步。' } }) + else patch({ busy: '', notice: { tone: 'bad', text: bodyError(res, '同步失败') } }) + }, function (error) { patch({ busy: '', notice: { tone: 'bad', text: messageOf(error) } }) }) + } + function resolveAll(action) { + if (conflicts.length === 0) { patch({ pendingAll: null }); return } + patch({ busy: 'all:' + action, notice: null }) + postJson(API.sync + '/resolve-all', { action: action, conflictIds: conflicts.map(function (item) { return item.conflictId }) }).then(function (res) { + if (res.body !== null && res.body !== undefined && res.body.ok === true) { + var resolved = Array.isArray(res.body.resolved) ? res.body.resolved : [] + var failed = Array.isArray(res.body.failed) ? res.body.failed : [] + var text = '批量处理完成:成功 ' + resolved.length + ' 项' + (failed.length === 0 ? '。' : ',失败 ' + failed.length + ' 项(' + failed.map(function (item) { return item.name + ':' + item.error }).join('、') + ')。') + patch({ busy: '', pendingAll: null, data: { stats: res.body.stats, conflicts: res.body.conflicts || [] }, notice: { tone: failed.length === 0 ? 'ok' : 'warn', text: text } }) + if (typeof props.onResolved === 'function') props.onResolved() + } else patch({ busy: '', notice: { tone: 'bad', text: bodyError(res, '批量处理冲突失败') } }) + }, function (error) { patch({ busy: '', notice: { tone: 'bad', text: messageOf(error) } }) }) + } + var data = state.data + var stats = data === null || data === undefined ? null : data.stats + var conflicts = data === null || data === undefined || !Array.isArray(data.conflicts) ? [] : data.conflicts + var statPills = stats === null ? h('p', { style: styles.hint }, '同步引擎未启用或尚未运行。') : h('div', { style: styles.pills }, + h(Pill, { label: '运行中', value: stats.running === true ? '是' : '否', tone: stats.running === true ? 'warn' : 'muted' }), + h(Pill, { label: '上次运行', value: formatIso(stats.lastRunAt), tone: 'muted' }), + h(Pill, { label: '上传', value: (stats.uploaded || []).length, tone: 'muted' }), + h(Pill, { label: '下载', value: (stats.downloaded || []).length, tone: 'muted' }), + h(Pill, { label: '冲突', value: conflicts.length, tone: conflicts.length > 0 ? 'warn' : 'muted' }), + ) + var conflictRows = conflicts.map(function (conflict) { + var local = conflict.local || {} + var cloud = conflict.cloud + return h('div', { key: conflict.name, style: styles.row }, + h('div', { style: styles.rowHead }, + h('span', { style: styles.rowTitle }, conflict.name), + h('div', { style: styles.pills }, + h(Pill, { label: '原因', value: String(conflict.reason), tone: 'warn' }), + h(Pill, { label: '本地', value: local.exists === true ? (local.files + ' 文件') : '缺失', tone: local.exists === true ? 'muted' : 'bad' }), + h(Pill, { label: '云端', value: cloud === null ? '无' : ('rev ' + String(cloud.revision === undefined ? '?' : cloud.revision)), tone: cloud === null ? 'muted' : 'ok' }), + ), + ), + h('p', { style: styles.hint }, conflict.note), + h('div', { style: styles.actions }, + h(Btn, { label: '保留本地', variant: 'small', disabled: state.busy !== '' || conflict.canKeepLocal !== true, onClick: function () { resolve(conflict.name, 'keep_local', conflict.conflictId) } }), + h(Btn, { label: '采用云端', variant: 'small', disabled: state.busy !== '' || conflict.canTakeCloud !== true, onClick: function () { resolve(conflict.name, 'take_cloud', conflict.conflictId) } }), + h(Btn, { label: '手动合并', variant: 'small', disabled: state.busy !== '', onClick: function () { resolve(conflict.name, 'manual', conflict.conflictId) } }), + ), + ) + }) + var batchRow = null + if (conflicts.length > 0) { + batchRow = h('div', { style: styles.actions }, + h(Btn, { label: '全部保留本地', variant: 'small', disabled: state.busy !== '' || !conflicts.some(function (item) { return item.canKeepLocal === true }), onClick: function () { patch({ pendingAll: 'keep_local', notice: null }) } }), + h(Btn, { label: '全部采用云端', variant: 'small', disabled: state.busy !== '' || !conflicts.some(function (item) { return item.canTakeCloud === true }), onClick: function () { patch({ pendingAll: 'take_cloud', notice: null }) } }), + ) + } + var confirmAllRow = null + if (state.pendingAll !== null && conflicts.length > 0) { + var pendingText = state.pendingAll === 'keep_local' ? '把全部本地版本上传为新版本' : '用云端版本覆盖全部本地(覆盖前自动备份)' + confirmAllRow = h('div', { style: styles.nested }, + h('p', { style: styles.hint }, '确认' + pendingText + '?共 ' + conflicts.length + ' 项。'), + h('div', { style: styles.actions }, + h(Btn, { variant: 'primary', label: state.busy === 'all:keep_local' || state.busy === 'all:take_cloud' ? '处理中…' : '确认批量', disabled: state.busy !== '', onClick: function () { resolveAll(state.pendingAll) } }), + h(Btn, { variant: 'small', label: '取消', disabled: state.busy !== '', onClick: function () { patch({ pendingAll: null }) } }), + ), + ) + } + return h(Section, { title: '云端同步与冲突', hint: '同步先比内容与更新时间:一致直接认领,不同则自动采用较新的一边(覆盖云端前先备份本地);只有时间相同/缺失或目录结构不支持时才逐项询问,也可用批量按钮一次处理。', aside: h(Btn, { label: state.busy === 'run' ? '同步中…' : '立即同步', variant: 'small', disabled: state.busy !== '', onClick: runNow }) }, + h('div', { style: { display: 'flex', flexDirection: 'column', gap: '10px' } }, + state.error === null ? null : h(Banner, { tone: 'bad' }, state.error), + state.notice === null ? null : h(Banner, { tone: state.notice.tone }, state.notice.text), + statPills, + batchRow, + confirmAllRow, + conflicts.length === 0 ? h('p', { style: styles.hint }, '当前没有待处理冲突。') : h('div', { style: { display: 'flex', flexDirection: 'column', gap: '8px' } }, conflictRows), + ), + ) +} + +// ---- P4: custom connection / edit / scope / policy --------------------- +// The MCP marketplace section was removed in 0.9.6: the host's `catalog` +// method answers 405 on current dsh-mcp-connector builds, so the card only +// rendered an error banner. Discover/install stays in the host's own +// plugin market instead of a broken embedded panel. + diff --git a/src-client/styles.ts b/src-client/styles.ts new file mode 100644 index 0000000..62638c6 --- /dev/null +++ b/src-client/styles.ts @@ -0,0 +1,368 @@ +// dsh-brain client style objects (inline styles, theme-token friendly). +export const styles = { + root: { + display: 'flex', + flexDirection: 'column', + gap: '18px', + padding: '4px 2px 20px', + fontSize: '13px', + lineHeight: 1.55, + minWidth: 0, + }, + tabs: { + display: 'flex', + gap: '4px', + padding: '3px', + borderRadius: '13px', + border: '1px solid rgba(127,127,127,0.22)', + background: 'rgba(127,127,127,0.06)', + flexWrap: 'wrap', + }, + tab: { + flex: '1 1 auto', + minWidth: '88px', + font: 'inherit', + fontSize: '12.5px', + padding: '7px 12px', + borderRadius: '10px', + border: '1px solid transparent', + background: 'transparent', + color: 'inherit', + cursor: 'pointer', + whiteSpace: 'nowrap', + }, + tabActive: { + border: '1px solid rgba(127,127,127,0.30)', + background: 'rgba(127,127,127,0.14)', + boxShadow: '0 1px 3px rgba(0,0,0,0.10)', + fontWeight: 600, + }, + section: { + display: 'flex', + flexDirection: 'column', + gap: '12px', + border: '1px solid rgba(127,127,127,0.22)', + borderRadius: '16px', + padding: '18px 20px', + minWidth: 0, + }, + sectionHead: { + display: 'flex', + alignItems: 'center', + justifyContent: 'space-between', + gap: '10px', + flexWrap: 'wrap', + }, + sectionTitle: { + margin: 0, + fontSize: '13.5px', + fontWeight: 650, + letterSpacing: '0.02em', + }, + hint: { + margin: 0, + fontSize: '11.5px', + opacity: 0.58, + }, + grid: { + display: 'flex', + flexWrap: 'wrap', + gap: '12px 18px', + }, + field: { + display: 'flex', + flexDirection: 'column', + gap: '4px', + flex: '1 1 auto', + minWidth: '220px', + }, + label: { + fontSize: '12px', + opacity: 0.75, + }, + input: { + font: 'inherit', + fontSize: '13px', + padding: '8px 11px', + borderRadius: '10px', + border: '1px solid rgba(127,127,127,0.35)', + background: 'rgba(127,127,127,0.05)', + color: 'inherit', + minWidth: 0, + width: '100%', + boxSizing: 'border-box', + }, + checkboxRow: { + display: 'flex', + alignItems: 'flex-start', + gap: '8px', + }, + actions: { + display: 'flex', + gap: '10px', + flexWrap: 'wrap', + alignItems: 'center', + }, + button: { + font: 'inherit', + fontSize: '13px', + padding: '7px 16px', + borderRadius: '10px', + border: '1px solid rgba(127,127,127,0.4)', + background: 'transparent', + color: 'inherit', + cursor: 'pointer', + }, + buttonPrimary: { + font: 'inherit', + fontSize: '13px', + padding: '7px 18px', + borderRadius: '10px', + border: '1px solid transparent', + background: '#3f3f46', + boxShadow: '0 2px 6px -2px rgba(0,0,0,0.35)', + color: '#ffffff', + cursor: 'pointer', + fontWeight: 600, + }, + buttonDanger: { + font: 'inherit', + fontSize: '13px', + padding: '7px 16px', + borderRadius: '10px', + border: '1px solid rgba(220,38,38,0.5)', + background: 'rgba(220,38,38,0.14)', + color: 'inherit', + cursor: 'pointer', + }, + buttonSmall: { + font: 'inherit', + fontSize: '12px', + padding: '3px 8px', + borderRadius: '8px', + border: '1px solid transparent', + background: 'transparent', + opacity: 0.85, + color: 'inherit', + cursor: 'pointer', + }, + pills: { + display: 'flex', + flexWrap: 'wrap', + gap: '8px', + }, + pill: { + display: 'inline-flex', + alignItems: 'center', + gap: '6px', + padding: '3px 10px', + borderRadius: '999px', + border: '1px solid rgba(127,127,127,0.35)', + fontSize: '12px', + maxWidth: '100%', + whiteSpace: 'nowrap', + flex: '0 0 auto', + }, + table: { + width: '100%', + borderCollapse: 'collapse', + fontSize: '12px', + tableLayout: 'fixed', + }, + th: { + textAlign: 'left', + fontWeight: 600, + padding: '4px 8px 4px 0', + borderBottom: '1px solid rgba(127,127,127,0.28)', + whiteSpace: 'nowrap', + }, + td: { + padding: '4px 8px 4px 0', + borderBottom: '1px solid rgba(127,127,127,0.14)', + verticalAlign: 'top', + overflowWrap: 'anywhere', + }, + mono: { + fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Consolas, monospace', + fontSize: '11px', + opacity: 0.72, + wordBreak: 'break-all', + }, + ok: { borderColor: 'rgba(22,163,74,0.55)', background: 'rgba(22,163,74,0.12)' }, + bad: { borderColor: 'rgba(220,38,38,0.55)', background: 'rgba(220,38,38,0.12)' }, + warn: { borderColor: 'rgba(217,119,6,0.55)', background: 'rgba(217,119,6,0.12)' }, + muted: {}, + iconBtn: { + width: '28px', + height: '28px', + display: 'inline-flex', + alignItems: 'center', + justifyContent: 'center', + fontSize: '13px', + lineHeight: 1, + padding: 0, + borderRadius: '8px', + border: '1px solid rgba(127,127,127,0.25)', + background: 'rgba(127,127,127,0.05)', + color: 'inherit', + cursor: 'pointer', + flex: '0 0 auto', + }, + iconBtnDanger: { + border: '1px solid rgba(220,38,38,0.40)', + background: 'rgba(220,38,38,0.08)', + }, + statsBar: { + display: 'flex', + alignItems: 'center', + flexWrap: 'wrap', + }, + stat: { + display: 'inline-flex', + alignItems: 'baseline', + gap: '6px', + paddingRight: '18px', + marginRight: '18px', + borderRight: '1px solid rgba(127,127,127,0.20)', + }, + statLast: { + display: 'inline-flex', + alignItems: 'baseline', + gap: '6px', + }, + statNum: { + fontSize: '17px', + fontWeight: 700, + }, + banner: { + padding: '10px 13px', + borderRadius: '11px', + border: '1px solid transparent', + fontSize: '12.5px', + }, + row: { + display: 'flex', + flexDirection: 'column', + gap: '6px', + border: '1px solid rgba(127,127,127,0.15)', + borderRadius: '11px', + padding: '10px 12px', + minWidth: 0, + }, + rowHead: { + display: 'flex', + alignItems: 'baseline', + justifyContent: 'space-between', + gap: '8px', + flexWrap: 'wrap', + }, + rowTitle: { + fontWeight: 600, + overflowWrap: 'anywhere', + }, + code: { + fontFamily: 'ui-monospace, SFMono-Regular, Menlo, Consolas, monospace', + fontSize: '11.5px', + whiteSpace: 'pre-wrap', + wordBreak: 'break-word', + maxHeight: '260px', + overflow: 'auto', + margin: 0, + padding: '10px', + borderRadius: '8px', + background: 'rgba(127,127,127,0.1)', + }, + chips: { + display: 'flex', + gap: '6px', + flexWrap: 'wrap', + }, + chip: { + font: 'inherit', + fontSize: '12px', + padding: '2px 10px', + borderRadius: '999px', + border: '1px solid rgba(127,127,127,0.35)', + background: 'transparent', + color: 'inherit', + cursor: 'pointer', + }, + chipActive: { + border: '1px solid rgba(99,102,241,0.6)', + background: 'rgba(99,102,241,0.16)', + }, + nested: { + display: 'flex', + flexDirection: 'column', + gap: '6px', + paddingLeft: '10px', + borderLeft: '2px solid rgba(127,127,127,0.18)', + minWidth: 0, + }, + sourceCard: { + display: 'flex', + flexDirection: 'column', + gap: '6px', + border: '1px solid rgba(127,127,127,0.17)', + borderRadius: '14px', + padding: '12px 14px', + minWidth: 0, + }, + groupRow: { + display: 'flex', + flexDirection: 'column', + gap: '6px', + border: '1px solid transparent', + borderRadius: '11px', + padding: '7px 9px', + background: 'transparent', + minWidth: 0, + }, + skillRow: { + display: 'flex', + alignItems: 'center', + justifyContent: 'space-between', + gap: '8px', + padding: '5px 8px', + borderRadius: '8px', + border: '1px solid transparent', + minWidth: 0, + flexWrap: 'nowrap', + }, + clamp1: { + display: '-webkit-box', + WebkitLineClamp: '1', + WebkitBoxOrient: 'vertical', + overflow: 'hidden', + }, + clamp2: { + display: '-webkit-box', + WebkitLineClamp: '2', + WebkitBoxOrient: 'vertical', + overflow: 'hidden', + }, + sourceGlyph: { + width: '20px', + height: '20px', + borderRadius: '6px', + display: 'inline-flex', + alignItems: 'center', + justifyContent: 'center', + fontSize: '11px', + fontWeight: 700, + border: '1px solid rgba(127,127,127,0.22)', + background: 'rgba(127,127,127,0.10)', + flex: '0 0 auto', + }, + folderGlyph: { + fontSize: '12px', + opacity: 0.85, + flex: '0 0 auto', + }, + sourceTitle: { + fontWeight: 600, + fontSize: '13px', + overflowWrap: 'anywhere', + }, +} + diff --git a/tsconfig.client.json b/tsconfig.client.json new file mode 100644 index 0000000..c35ab85 --- /dev/null +++ b/tsconfig.client.json @@ -0,0 +1,11 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "noEmit": true, + "jsx": "react-jsx", + "strict": false, + "noUncheckedIndexedAccess": false, + "types": ["node"] + }, + "include": ["src-client"] +} diff --git a/tsdown.config.ts b/tsdown.config.ts new file mode 100644 index 0000000..d9286bd --- /dev/null +++ b/tsdown.config.ts @@ -0,0 +1,25 @@ +import type { UserConfig } from 'tsdown' + +const PLUGIN_ID = 'dsh-brain' + +export default { + name: PLUGIN_ID + '/client', + entry: { index: 'src-client/index.ts' }, + outDir: 'client', + format: 'cjs', + platform: 'browser', + target: 'es2022', + fixedExtension: false, + outExtensions: () => ({ js: '.js' }), + dts: false, + sourcemap: false, + clean: false, + deps: { neverBundle: [/^react(?:\/.*)?$/, /^react-dom(?:\/.*)?$/] }, + define: { 'process.env.NODE_ENV': '"production"' }, + plugins: [], + outputOptions: { + banner: `window.__ModuleLoader__.load({ id: '${PLUGIN_ID}', factory: (require) => {`, + intro: 'var module = { exports: {} }; var exports = module.exports;', + footer: 'return module.exports; } });', + }, +} satisfies UserConfig diff --git a/verify-client-build.mjs b/verify-client-build.mjs new file mode 100644 index 0000000..e4b704a --- /dev/null +++ b/verify-client-build.mjs @@ -0,0 +1,116 @@ +// U0 client-build verification: the bundled client must keep the +// __ModuleLoader__ contract, load like the legacy hand-written file, and +// register the same settings seats with the same option shapes. +import { execFileSync } from 'node:child_process' +import { existsSync, readFileSync } from 'node:fs' +import { createRequire } from 'node:module' +import vm from 'node:vm' + +const require = createRequire(import.meta.url) +const React = require('react') + +const results = [] +function check(name, ok, detail = '') { + results.push({ name, ok }) + console.log((ok ? 'PASS ' : 'FAIL ') + name + (detail === '' ? '' : ' :: ' + detail)) +} + +const bundle = readFileSync(new URL('./client/index.js', import.meta.url), 'utf8') +check('client bundle uses the __ModuleLoader__ contract', bundle.includes('__ModuleLoader__.load')) +check('client bundle id stays dsh-brain', /id:\s*['"]dsh-brain['"]/.test(bundle)) +for (const seat of ['settings.section', 'plugins.bundle.config']) { + check('client bundle registers ' + seat, bundle.includes(seat)) +} +for (const label of ['连接与上报', '技能管理', 'MCP 管理']) { + check('client bundle keeps tab label ' + label, bundle.includes(label)) +} +check('client bundle is a single file with only react externals', (() => { + const requires = [...bundle.matchAll(/require\(\s*['"]([^'"]+)['"]\s*\)/g)].map((match) => match[1]) + return requires.every((name) => name === 'react' || name === 'react/jsx-runtime') +})()) + +function loadBundle(file) { + const code = readFileSync(file, 'utf8') + let definition = null + const sandbox = { + window: { + __ModuleLoader__: { + load(value) { definition = value }, + }, + localStorage: { getItem: () => null, setItem: () => {} }, + open: () => undefined, + }, + console, + setTimeout, + clearTimeout, + fetch: async () => { throw new Error('fetch not available in verification') }, + AbortController, + } + sandbox.window.window = sandbox.window + sandbox.globalThis = sandbox + vm.runInNewContext(code, sandbox, { filename: String(file) }) + if (definition === null) throw new Error('bundle did not register with __ModuleLoader__: ' + file) + return definition.factory((name) => { + if (name === 'react') return React + if (name === 'react/jsx-runtime') return require('react/jsx-runtime') + throw new Error('unexpected require: ' + name) + }) +} + +function registrationShape(file) { + const mod = loadBundle(file) + const records = [] + let component = null + const ctx = { + slots: { + inject(seat, callback) { + component = null + const value = callback() + records.push({ seat, value, component }) + }, + register(options, registeredComponent) { + component = registeredComponent + return options + }, + }, + } + mod.apply(ctx) + return { + name: mod.name, + inject: mod.inject, + records: records.map((entry) => ({ + seat: entry.seat, + name: entry.value.name, + id: entry.value.id, + key: entry.value.key, + order: entry.value.order, + label: typeof entry.value.label === 'function' ? entry.value.label() : undefined, + })), + components: records.map((entry) => entry.component), + } +} + +const current = registrationShape('client/index.js') +check('apply registers exactly the settings seats', current.records.length === 2) +check('settings.section keeps its id/order/label', JSON.stringify(current.records[0]) === JSON.stringify({ seat: 'settings.section', name: 'settings.section', id: 'dsh-brain', key: undefined, order: 100, label: '云端记忆' })) +check('plugins.bundle.config keeps its key', JSON.stringify(current.records[1]) === JSON.stringify({ seat: 'plugins.bundle.config', name: 'plugins.bundle.config', id: undefined, key: 'dsh-brain', order: undefined, label: undefined })) +check('module exports name dsh-brain', current.name === 'dsh-brain' && Array.isArray(current.inject) && current.inject.includes('slots')) + +// Each registered callback must produce a renderable React element without a DOM. +let rendered = 0 +for (const component of current.components) { + const element = typeof component === 'function' ? React.createElement(component) : null + if (element !== null && typeof element === 'object' && element.type !== undefined) rendered += 1 +} +check('registered components render without throwing', rendered === current.components.length) + +const legacyFile = new URL('./artifacts/analysis/client-legacy.js', import.meta.url) +if (existsSync(legacyFile)) { + const legacy = registrationShape(legacyFile) + check('migration preserves apply registration shape', JSON.stringify({ name: current.name, inject: current.inject, records: current.records }) === JSON.stringify({ name: legacy.name, inject: legacy.inject, records: legacy.records }), JSON.stringify({ current, legacy })) +} else { + console.log('SKIP legacy comparison (artifacts/analysis/client-legacy.js missing)') +} + +console.log('\n' + (results.every((result) => result.ok) ? 'ALL ' + results.length + ' CHECKS PASSED' : results.filter((result) => !result.ok).length + ' FAILED')) +process.exit(results.every((result) => result.ok) ? 0 : 1)