/** * DSH client 半(H2):侧边栏「调度网关」面板 + 编程客户端。 * 侧边栏直接内嵌 standalone 中心网关自带面板(同一套核心/同一 API)。 */ const GW = (typeof process !== "undefined" && process.env?.GATEWAY_URL) || "http://127.0.0.1:4180"; export class GatewayClient { constructor(baseUrl = GW, nodeToken) { this.base = baseUrl.replace(/\/$/, ""); this.nodeToken = nodeToken; } async _p(path, opts = {}) { const headers = { ...(opts.headers || {}) }; if (opts.body) headers["content-type"] = "application/json"; if (this.nodeToken) headers["x-node-token"] = this.nodeToken; const r = await fetch(this.base + path, { ...opts, headers }); return r.json(); } status() { return this._p("/api/status"); } tasks() { return this._p("/api/tasks"); } nodes() { return this._p("/api/nodes"); } createTask(task) { return this._p("/api/tasks", { method: "POST", body: JSON.stringify(task) }); } pipeline(goal, opts = {}) { return this._p("/api/pipeline", { method: "POST", body: JSON.stringify({ goal, opts }) }); } approve(taskId) { return this._p(`/api/tasks/${taskId}/approve`, { method: "POST" }); } requeueDead(taskId) { return this._p(`/api/deadletter/${taskId}/retry`, { method: "POST" }); } events(onEvent) { const es = new EventSource(this.base + "/api/events"); es.onmessage = (e) => onEvent(JSON.parse(e.data)); return () => es.close(); } } /** DSH 侧边栏注册(宿主支持时)。 */ export function registerSidebar(dsh) { if (!dsh?.ui?.registerPanel) return false; dsh.ui.registerPanel({ id: "scheduler-gateway-live", title: "调度网关", render: () => ({ type: "iframe", src: `${GW}/panel`, }), }); return true; } export default GatewayClient;