openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
218 lines (217 loc) • 5.67 kB
JavaScript
import { l as normalizeOptionalString } from "../../string-coerce-CCIiB4H7.mjs";
import { l as isRecord } from "../../record-coerce-CVPuDqcc.mjs";
import { o as getPluginRuntimeGatewayRequestScope } from "../../gateway-request-scope-BvvDhs5D.mjs";
import { a as readJsonBodyWithLimit, f as sendHttpRequestRejection } from "../../http-body-B9w7rzPz.mjs";
import { n as sendJsonNoStore } from "../../http-json-ddGJz5CA.mjs";
import "../../string-coerce-runtime-BwAZZDf5.mjs";
import { t as WEBHOOK_BODY_READ_DEFAULTS } from "../../webhook-request-guards-Br7AftEb.mjs";
import { t as definePluginEntry } from "../../plugin-entry-CiJdaHtx.mjs";
import "../../plugin-runtime-z_iV2vts.mjs";
import { t as dispatchGatewayMethod } from "../../gateway-method-runtime-Bu-SKY1m.mjs";
import { randomUUID } from "node:crypto";
//#region extensions/admin-http-rpc/src/handler.ts
const ADMIN_HTTP_RPC_ALLOWED_METHODS = new Set(Object.values({
gateway: [
"health",
"status",
"logs.tail",
"usage.status",
"usage.cost",
"gateway.restart.request",
"gateway.suspend.prepare",
"gateway.suspend.status",
"gateway.suspend.resume"
],
discovery: ["commands.list"],
config: [
"config.get",
"config.schema",
"config.schema.lookup",
"config.set",
"config.patch",
"config.apply"
],
channels: [
"channels.status",
"channels.start",
"channels.stop",
"channels.logout"
],
web: ["web.login.start", "web.login.wait"],
models: ["models.list", "models.authStatus"],
agents: [
"agents.list",
"agents.create",
"agents.update",
"agents.delete"
],
approvals: [
"exec.approvals.get",
"exec.approvals.set",
"exec.approvals.node.get",
"exec.approvals.node.set"
],
cron: [
"cron.status",
"cron.list",
"cron.get",
"cron.runs",
"cron.add",
"cron.update",
"cron.remove",
"cron.run"
],
devices: [
"device.pair.list",
"device.pair.approve",
"device.pair.reject",
"device.pair.remove"
],
nodes: [
"node.list",
"node.describe",
"node.pair.list",
"node.pair.approve",
"node.pair.reject",
"node.pair.remove",
"node.rename"
],
diagnostics: ["doctor.memory.status", "update.status"]
}).flat());
function rpcHttpStatus(e) {
if (e.ok) return 200;
switch (e.error.code) {
case "INVALID_REQUEST": return 400;
case "APPROVAL_NOT_FOUND": return 404;
case "UNAVAILABLE": return 503;
case "AGENT_TIMEOUT": return 504;
case "NOT_LINKED":
case "NOT_PAIRED": return 409;
default: return 500;
}
}
function sendError(e, t, n) {
sendJsonNoStore(e, t, {
ok: false,
error: n
});
}
/** Handle one gateway-authenticated Admin HTTP RPC request. */
async function handleAdminHttpRpcRequest(e, t) {
if ((e.method ?? "GET").toUpperCase() !== "POST") {
t.setHeader("Allow", "POST");
sendError(t, 405, {
type: "method_not_allowed",
message: "Method Not Allowed"
});
return true;
}
const n = await readJsonBodyWithLimit(e, {
...WEBHOOK_BODY_READ_DEFAULTS.postAuthResponseFirst,
emptyObjectOnEmpty: false
});
if (!n.ok) {
const r = n.code === "PAYLOAD_TOO_LARGE" ? 413 : n.code === "REQUEST_BODY_TIMEOUT" ? 408 : 400;
const i = n.code === "INVALID_JSON" ? n.error === "empty payload" ? "request body must be JSON" : "request body must be valid JSON" : n.error;
if (n.code !== "INVALID_JSON" && n.code !== "CONNECTION_CLOSED") {
if (!t.headersSent) t.setHeader("Cache-Control", "no-store");
await sendHttpRequestRejection(e, t, r, JSON.stringify({
ok: false,
error: {
type: "invalid_request",
message: i
}
}), "application/json; charset=utf-8");
} else sendError(t, r, {
type: "invalid_request",
message: i
});
return true;
}
if (!isRecord(n.value)) {
sendError(t, 400, {
type: "invalid_request",
message: "request body must be an object"
});
return true;
}
const r = normalizeOptionalString(n.value.method);
if (!r) {
sendError(t, 400, {
type: "invalid_request",
message: "method must be a non-empty string"
});
return true;
}
const i = normalizeOptionalString(n.value.id) ?? randomUUID();
const a = Object.hasOwn(n.value, "params") ? n.value.params : void 0;
if (!ADMIN_HTTP_RPC_ALLOWED_METHODS.has(r)) {
sendJsonNoStore(t, 400, {
id: i,
ok: false,
error: {
code: "INVALID_REQUEST",
message: `admin HTTP RPC method is not supported: ${r}`
}
});
return true;
}
if (r === "commands.list") {
sendJsonNoStore(t, 200, {
id: i,
ok: true,
payload: { methods: Array.from(ADMIN_HTTP_RPC_ALLOWED_METHODS) }
});
return true;
}
await getPluginRuntimeGatewayRequestScope()?.revalidate?.();
const o = {
code: "UNAVAILABLE",
message: "gateway method failed before returning a response"
};
let s;
try {
const e = await dispatchGatewayMethod(r, a);
s = {
id: i,
...e.ok ? {
ok: true,
payload: e.payload
} : {
ok: false,
error: e.error ?? o
},
...e.meta ? { meta: e.meta } : {}
};
} catch {
s = {
id: i,
ok: false,
error: o
};
}
sendJsonNoStore(t, rpcHttpStatus(s), s);
return true;
}
//#endregion
//#region extensions/admin-http-rpc/index.ts
/**
* Admin HTTP RPC plugin entry. It exposes a trusted gateway-authenticated HTTP
* endpoint for the explicit admin method allowlist.
*/
var admin_http_rpc_default = definePluginEntry({
id: "admin-http-rpc",
name: "Admin HTTP RPC",
description: "Expose selected Gateway admin RPC methods over HTTP",
register(e) {
e.registerHttpRoute({
path: "/api/v1/admin/rpc",
auth: "gateway",
match: "exact",
gatewayRuntimeScopeSurface: "trusted-operator",
handler: handleAdminHttpRpcRequest
});
}
});
//#endregion
export { admin_http_rpc_default as default };