dsh-brain/src-client/bridge.ts

38 lines
1.6 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.

// 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.