dsh-brain/src-client/sections/McpConfigSection.tsx

143 lines
9.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

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

// 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 }, '可编辑配置(保留标记 <KEEP_EXISTING> 表示保留原密钥):'),
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 -------------------------------