213 lines
11 KiB
JavaScript
213 lines
11 KiB
JavaScript
// AgentHub 接入面板 — DeepSeek Harness Client 半(静态持久插件)
|
||
// 浏览器 bundle,经 ModuleLoader 加载(格式同 @nex/pet-dock、@xianxia/dashboard)。
|
||
// 注册:设置页的一个 section,展示本实例在 hub 上的身份 / 连接状态 / 最近被派的活。
|
||
// 数据:POST /api/agenthub/status(同源 fetch,带 dsh 自己的会话 cookie)→ Host 半的 hub.snapshot()。
|
||
// 这条路由由 Host 半用 ctx.connection.fetch.register 挂在 /api 共享通道上,
|
||
// 所以它天然在 Host/Origin 与浏览器鉴权围栏内,客户端不需要自己带 token。
|
||
|
||
window.__ModuleLoader__.load({
|
||
id: "@nex/dsh-agenthub",
|
||
factory: (require) => {
|
||
var module = { exports: {} };
|
||
var exports = module.exports;
|
||
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
||
var react = require("react");
|
||
var el = react.createElement;
|
||
|
||
var cssId = "@nex/dsh-agenthub/agenthub.css";
|
||
var css = [
|
||
".ahb { display: flex; flex-direction: column; gap: 10px; font-size: 13px; line-height: 1.55; }",
|
||
".ahb-row { display: flex; align-items: center; gap: 8px; }",
|
||
".ahb-dot { width: 8px; height: 8px; border-radius: 50%; flex: none; background: #94a3b8; }",
|
||
".ahb-dot.on { background: #2ecc71; }",
|
||
".ahb-dot.off { background: #e74c3c; }",
|
||
".ahb-kv { display: grid; grid-template-columns: 96px 1fr; gap: 4px 10px; }",
|
||
".ahb-kv dt { color: var(--dsw-alias-label-secondary, #888); }",
|
||
".ahb-kv dd { margin: 0; word-break: break-all; }",
|
||
".ahb-task { border: 1px solid var(--dsw-alias-border-l1, rgba(0,0,0,.08)); border-radius: 8px; padding: 6px 8px; }",
|
||
".ahb-task .ahb-dim { color: var(--dsw-alias-label-secondary, #888); font-size: 11px; }",
|
||
".ahb-btn { border: 1px solid var(--dsw-alias-border-l1, rgba(0,0,0,.12)); background: transparent; border-radius: 8px; padding: 4px 10px; cursor: pointer; font-size: 12px; }",
|
||
".ahb-form { display: grid; gap: 8px; }",
|
||
".ahb-form label { display: grid; gap: 3px; font-size: 11px; color: var(--dsw-alias-label-secondary, #888); }",
|
||
".ahb-form input { font-size: 12px; padding: 5px 8px; border-radius: 8px; }",
|
||
".ahb-btn[disabled] { opacity: .5; cursor: default; }",
|
||
].join("");
|
||
|
||
function claimStyles() {
|
||
if (typeof document === "undefined") return;
|
||
if (document.querySelector('style[data-plugin-css="' + cssId + '"]')) return;
|
||
var tag = document.createElement("style");
|
||
tag.dataset.plugin = "@nex/dsh-agenthub";
|
||
tag.dataset.pluginCss = cssId;
|
||
tag.textContent = css;
|
||
document.head.appendChild(tag);
|
||
}
|
||
|
||
function statusText(status) {
|
||
if (status === "running") return "执行中";
|
||
if (status === "completed") return "已完成";
|
||
if (status === "failed") return "失败";
|
||
if (status === "cancelled") return "已取消";
|
||
return String(status || "");
|
||
}
|
||
|
||
// 凭证不落盘:来源写清楚,面板上要能一眼看出现在用的是哪一份。
|
||
function credentialText(source) {
|
||
if (source === "config") return "插件 config 里的 agentToken(推荐:长期、重启不丢)";
|
||
if (source === "panel") return "面板这次填的凭证(不落盘,重启要重填)";
|
||
if (source === "legacy-file") return "老身份文件(建议迁进 config 后删除)";
|
||
return "未配置";
|
||
}
|
||
|
||
function Panel(ctx) {
|
||
var useState = react.useState;
|
||
var useEffect = react.useEffect;
|
||
var data = useState(null), snapshot = data[0], setSnapshot = data[1];
|
||
var form = useState({ server: "", port: "", agentToken: "", name: "" }), values = form[0], setForm = form[1];
|
||
var busyState = useState(false), busy = busyState[0], setBusy = busyState[1];
|
||
var errorState = useState(""), error = errorState[0], setError = errorState[1];
|
||
var noticeState = useState(""), notice = noticeState[0], setNotice = noticeState[1];
|
||
var touched = useState(false), edited = touched[0], setEdited = touched[1];
|
||
|
||
function call(action, extra) {
|
||
return fetch("/api/agenthub/status", {
|
||
method: "POST",
|
||
headers: { "content-type": "application/json" },
|
||
body: JSON.stringify({ action: action, ...(extra || {}) }),
|
||
})
|
||
.then(function (response) {
|
||
if (!response.ok) throw new Error("HTTP " + response.status);
|
||
return response.json();
|
||
})
|
||
.then(function (res) {
|
||
if (res && res.ok === false) throw new Error((res.error && res.error.message) || "RPC 失败");
|
||
var value = res && res.value !== undefined ? res.value : res;
|
||
setSnapshot(value);
|
||
return value;
|
||
});
|
||
}
|
||
|
||
// 打开面板先读一次状态;之后每 8 秒刷新(上线/下线后立即刷新)。
|
||
useEffect(function () {
|
||
var cancelled = false;
|
||
var load = function () {
|
||
call("status").catch(function (err) {
|
||
if (!cancelled) setError(String((err && err.message) || err));
|
||
});
|
||
};
|
||
load();
|
||
var timer = setInterval(load, 8000);
|
||
return function () { cancelled = true; clearInterval(timer); };
|
||
}, []);
|
||
|
||
// 状态里带回来的 server/name 用来预填表单(用户改过就别覆盖)。
|
||
useEffect(function () {
|
||
if (!snapshot || edited) return;
|
||
var parts = String(snapshot.server || "").match(/^(https?:\/\/[^/:]+)(?::(\d+))?/);
|
||
setForm({
|
||
server: parts ? parts[1] : "",
|
||
port: parts && parts[2] ? parts[2] : "8100",
|
||
agentToken: "",
|
||
name: String(snapshot.name || ""),
|
||
});
|
||
}, [snapshot && snapshot.server, edited]);
|
||
|
||
var online = Boolean(snapshot && (snapshot.connected || snapshot.joined));
|
||
var set = function (key) {
|
||
return function (event) {
|
||
setEdited(true);
|
||
var next = { ...values };
|
||
next[key] = event.target.value;
|
||
setForm(next);
|
||
};
|
||
};
|
||
var submit = function (action) {
|
||
setBusy(true);
|
||
setError("");
|
||
setNotice("");
|
||
return call(action, action === "connect" ? values : undefined)
|
||
.then(function () {
|
||
setNotice(action === "connect" ? "已尝试上线:hub 认这份凭证就能在控制台里看到这台 DSH(凭证不会写进磁盘,长期接入请写进 profile 的 config)" : "已下线(地址与名字留着,下次点上线即可)");
|
||
setEdited(false);
|
||
})
|
||
.catch(function (err) { setError(String((err && err.message) || err)); })
|
||
.then(function () { setBusy(false); });
|
||
};
|
||
|
||
var rows = [];
|
||
rows.push(el("div", { key: "dot", className: "ahb-row" },
|
||
el("span", { className: "ahb-dot " + (online ? "on" : "off") }),
|
||
el("strong", null, online ? "已上线" : snapshot ? "未上线" : "读取中…")));
|
||
|
||
if (!online) {
|
||
rows.push(el("div", { key: "form", className: "ahb-form" },
|
||
el("label", null, "Hub 服务器地址",
|
||
el("input", { className: "ahb-btn", style: { width: "100%" }, placeholder: "http://127.0.0.1", value: values.server, onChange: set("server") })),
|
||
el("label", null, "端口",
|
||
el("input", { className: "ahb-btn", style: { width: "100%" }, placeholder: "8100", value: values.port, onChange: set("port") })),
|
||
el("label", null, "这台 Agent 的凭证(ag_…)",
|
||
el("input", { className: "ahb-btn", style: { width: "100%" }, placeholder: "ag_…", value: values.agentToken, onChange: set("agentToken") })),
|
||
el("label", null, "这台 Agent 的名字(可留空用默认)",
|
||
el("input", { className: "ahb-btn", style: { width: "100%" }, placeholder: "dsh-1", value: values.name, onChange: set("name") }))));
|
||
rows.push(el("div", { key: "hint", className: "ahb-dim" },
|
||
"凭证在 hub 控制台「接入」页签发,是这台 Agent 的唯一身份(和桥接进程的 --token 同一份东西)。" +
|
||
"面板填的凭证不落盘;想重启还在,就写进 profile 的 cordis.patch.yml。"));
|
||
// 上一次上线失败的原因要摆在表单旁边,不然"点了没反应"只能靠猜。
|
||
if (snapshot && snapshot.last_error) {
|
||
rows.push(el("div", { key: "lastError", className: "ahb-task ahb-dim" },
|
||
"上次没接上:" + String(snapshot.last_error)));
|
||
}
|
||
rows.push(el("div", { key: "go", className: "ahb-row" },
|
||
el("button", { className: "ahb-btn", disabled: busy, onClick: function () { submit("connect"); } },
|
||
busy ? "上线中…" : "上线"),
|
||
el("button", { className: "ahb-btn", disabled: busy || !snapshot, onClick: function () { submit("leave"); } }, "下线")));
|
||
} else {
|
||
rows.push(el("dl", { key: "kv", className: "ahb-kv" },
|
||
el("dt", null, "Hub"), el("dd", null, String((snapshot && snapshot.server) || snapshot.hub || "-")),
|
||
el("dt", null, "Agent"), el("dd", null, String(snapshot.agent_id || "-")),
|
||
el("dt", null, "名字"), el("dd", null, String(snapshot.name || "-")),
|
||
el("dt", null, "在跑"), el("dd", null, String(snapshot.active_tasks || 0)),
|
||
el("dt", null, "凭证来源"), el("dd", null, credentialText(snapshot.credential)),
|
||
snapshot.last_error ? el("dt", null, "最近错误") : null,
|
||
snapshot.last_error ? el("dd", null, String(snapshot.last_error)) : null));
|
||
rows.push(el("div", { key: "leave", className: "ahb-row" },
|
||
el("button", { className: "ahb-btn", disabled: busy, onClick: function () { submit("leave"); } }, "下线"),
|
||
el("button", { className: "ahb-btn", disabled: busy, onClick: function () { submit("status"); } }, "刷新")));
|
||
}
|
||
|
||
if (notice) rows.push(el("div", { key: "notice", className: "ahb-dim" }, notice));
|
||
if (error) rows.push(el("div", { key: "err", className: "ahb-task ahb-dim" }, "操作失败:" + error));
|
||
|
||
if (snapshot && snapshot.agent_id) {
|
||
rows.push(el("div", { key: "tasks" },
|
||
el("div", { className: "ahb-dim" }, "最近被派的活"),
|
||
(snapshot.tasks || []).length === 0
|
||
? el("div", { className: "ahb-dim" }, "还没有任务")
|
||
: (snapshot.tasks || []).slice(0, 6).map(function (task, index) {
|
||
return el("div", { key: String(task.invocation_id || index), className: "ahb-task" },
|
||
el("div", null, String(task.prompt || "").slice(0, 120) || "(空 prompt)"),
|
||
el("div", { className: "ahb-dim" },
|
||
statusText(task.status) + " · " + String(task.session_key || "-") +
|
||
(task.task_id ? " · " + String(task.task_id).slice(0, 12) : "")));
|
||
})));
|
||
}
|
||
return el("div", { className: "ahb" }, rows);
|
||
}
|
||
|
||
function apply(ctx) {
|
||
claimStyles();
|
||
var slots = ctx.get ? ctx.get("slots") : ctx.slots;
|
||
if (!slots) return;
|
||
slots.inject("settings.section", function () {
|
||
return slots.register(
|
||
{ name: "settings.section", id: "agenthub", order: 60, label: "AgentHub" },
|
||
function () { return el(Panel, { ctx: ctx }); });
|
||
});
|
||
}
|
||
|
||
var inject = ["slots", "connection"];
|
||
module.exports = { apply: apply, inject: inject };
|
||
return module.exports;
|
||
},
|
||
});
|