// 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, ) }