96 lines
3.8 KiB
TypeScript
96 lines
3.8 KiB
TypeScript
// 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: '' }
|
||
}
|
||
|