openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
319 lines (318 loc) • 12.7 kB
JavaScript
import { c as normalizeOptionalLowercaseString, l as normalizeOptionalString, o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs";
import { g as normalizeUniqueStringEntries, v as normalizeUniqueTrimmedStringList } from "./string-normalization-DLmfYZXI.mjs";
import { r as getPluginRegistryVersion, t as getActivePluginGatewayNodePolicyRegistry } from "./runtime-state-DVrhXeqI.mjs";
import { F as isPrivateNodeInvokeCommand, h as NODE_SYSTEM_RUN_COMMANDS, i as NODE_DEVICE_APPS_COMMAND, l as NODE_MCP_TOOLS_CALL_COMMAND, m as NODE_SYSTEM_NOTIFY_COMMAND, n as NODE_BROWSER_PROXY_COMMANDS, o as NODE_EXEC_APPROVALS_COMMANDS, s as NODE_FILE_COMMANDS, t as NODE_AGENT_CLI_CLAUDE_RUN_COMMAND, w as NODE_WORKER_PRIVATE_COMMANDS } from "./node-commands-BfMJIlXP.mjs";
import { n as NODE_DESKTOP_STREAM_COMMAND } from "./node-desktop-stream-BZM2AiRA.mjs";
//#region src/gateway/node-command-policy.ts
const MOBILE_NODE_COMMANDS = {
location: ["location.get"],
androidNotification: ["notifications.list", "notifications.actions"],
device: ["device.info", "device.status"]
};
const CAMERA_COMMANDS = ["camera.list"];
const MAC_CAMERA_COMMANDS = ["camera.ptz.status"];
const CAMERA_DANGEROUS_COMMANDS = [
"camera.snap",
"camera.clip",
"camera.ptz.control"
];
const SCREEN_COMMANDS = ["screen.snapshot"];
const SCREEN_DANGEROUS_COMMANDS = ["screen.record"];
const DESKTOP_SCREEN_COMMANDS = [...SCREEN_COMMANDS, NODE_DESKTOP_STREAM_COMMAND];
const COMPUTER_COMMANDS = ["computer.act"];
const MOBILE_UI_COMMANDS = ["mobile.ui.observe", "mobile.ui.act"];
const ANDROID_DEVICE_COMMANDS = [
...MOBILE_NODE_COMMANDS.device,
"device.permissions",
"device.health",
NODE_DEVICE_APPS_COMMAND
];
const CONTACTS_CALENDAR_COMMANDS = ["contacts.search", "calendar.events"];
const PERSONAL_DATA_COMMANDS = [
"reminders.list",
"photos.latest",
"motion.activity",
"motion.pedometer"
];
const TALK_PTT_COMMANDS = [
"talk.ptt.start",
"talk.ptt.stop",
"talk.ptt.cancel",
"talk.ptt.once"
];
const IOS_WATCH_RELAY_COMMANDS = ["watch.status", "watch.notify"];
const SYSTEM_COMMANDS = [
...NODE_SYSTEM_RUN_COMMANDS,
...NODE_EXEC_APPROVALS_COMMANDS,
...NODE_FILE_COMMANDS,
NODE_SYSTEM_NOTIFY_COMMAND,
...NODE_BROWSER_PROXY_COMMANDS,
NODE_MCP_TOOLS_CALL_COMMAND,
NODE_AGENT_CLI_CLAUDE_RUN_COMMAND
];
const DESKTOP_HOST_COMMANDS = /* @__PURE__ */ new Set([...SYSTEM_COMMANDS.filter((e) => e !== NODE_SYSTEM_NOTIFY_COMMAND), ...DESKTOP_SCREEN_COMMANDS]);
const DESKTOP_COMMANDS = [
...SYSTEM_COMMANDS,
...DESKTOP_SCREEN_COMMANDS,
...COMPUTER_COMMANDS
];
const DEFAULT_DANGEROUS_NODE_COMMANDS = [
...CAMERA_DANGEROUS_COMMANDS,
...SCREEN_DANGEROUS_COMMANDS,
"contacts.add",
"calendar.add",
"reminders.add",
"sms.send",
"sms.search",
"health.summary"
];
const PLATFORM_DEFAULTS = {
ios: [
...CAMERA_COMMANDS,
...MOBILE_NODE_COMMANDS.location,
...MOBILE_NODE_COMMANDS.device,
...CONTACTS_CALENDAR_COMMANDS,
...PERSONAL_DATA_COMMANDS,
NODE_SYSTEM_NOTIFY_COMMAND
],
watchos: [...MOBILE_NODE_COMMANDS.device, NODE_SYSTEM_NOTIFY_COMMAND],
android: [
...CAMERA_COMMANDS,
...MOBILE_NODE_COMMANDS.location,
...MOBILE_NODE_COMMANDS.androidNotification,
NODE_SYSTEM_NOTIFY_COMMAND,
...ANDROID_DEVICE_COMMANDS,
...CONTACTS_CALENDAR_COMMANDS,
"callLog.search",
...PERSONAL_DATA_COMMANDS,
...MOBILE_UI_COMMANDS
],
macos: [
...CAMERA_COMMANDS,
...MAC_CAMERA_COMMANDS,
...MOBILE_NODE_COMMANDS.location,
...MOBILE_NODE_COMMANDS.device,
NODE_DEVICE_APPS_COMMAND,
...CONTACTS_CALENDAR_COMMANDS,
...PERSONAL_DATA_COMMANDS,
...DESKTOP_COMMANDS
],
linux: [...DESKTOP_COMMANDS],
windows: [
...CAMERA_COMMANDS,
...MOBILE_NODE_COMMANDS.location,
...MOBILE_NODE_COMMANDS.device,
...DESKTOP_COMMANDS
],
unknown: [
...CAMERA_COMMANDS,
...MOBILE_NODE_COMMANDS.location,
NODE_SYSTEM_NOTIFY_COMMAND
]
};
const PLATFORM_RULES = [
{
id: "ios",
tokens: [
"iphone",
"ipad",
"ios"
],
nativeLabel: /^(?:ios|ipados) \d+(?:\.\d+){0,2}$/,
allowEmptyFamily: true
},
{
id: "watchos",
tokens: ["apple watch", "watchos"],
nativeLabel: /^watchos \d+(?:\.\d+){0,2}$/
},
{
id: "android",
tokens: ["android"],
nativeLabel: /^android \d+(?: \(sdk \d+\))?$/,
allowEmptyFamily: true
},
{
id: "macos",
tokens: ["mac"],
nativeLabel: /^macos \d+(?:\.\d+){0,2}$/
},
{
id: "windows",
tokens: ["windows"]
},
{
id: "linux",
tokens: ["linux"]
}
];
function normalizeDeviceMetadataForPolicy(e) {
const t = normalizeOptionalString(e);
if (!t) return "";
return normalizeLowercaseStringOrEmpty(t.normalize("NFKD").replace(/\p{M}/gu, ""));
}
function normalizePlatformId(e, t) {
const n = normalizeDeviceMetadataForPolicy(e);
const r = normalizeDeviceMetadataForPolicy(t);
if (n) {
const e = PLATFORM_RULES.find((e) => e.id === n);
if (e) return e.tokens.includes(r) || r === "" && e.allowEmptyFamily ? e.id : "unknown";
return PLATFORM_RULES.find((e) => e.nativeLabel?.test(n) && e.tokens.includes(r))?.id ?? "unknown";
}
return PLATFORM_RULES.find((e) => e.tokens.some((e) => r.includes(e)))?.id ?? "unknown";
}
const pluginPolicies = /* @__PURE__ */ new WeakMap();
function getPreparedPluginNodePolicy() {
const e = getActivePluginGatewayNodePolicyRegistry();
if (!e) return;
const t = getPluginRegistryVersion(e);
const n = pluginPolicies.get(e);
if (n && n.version === t) return n;
const r = new Set(normalizeUniqueStringEntries([...e.nodeHostCommands.flatMap(({ command: e }) => e.dangerous === true ? [e.command] : []), ...e.nodeInvokePolicies.flatMap(({ policy: e }) => e.dangerous === true ? e.commands : [])]));
const i = /* @__PURE__ */ new Map();
const a = ["unknown", ...PLATFORM_RULES.map(({ id: e }) => e)];
for (const t of a) {
if (t === "watchos") continue;
i.set(t, normalizeUniqueStringEntries([...e.nodeInvokePolicies.flatMap(({ policy: e }) => e.dangerous !== true && e.defaultPlatforms?.includes(t) ? e.commands : []), ...e.nodeHostCommands.flatMap(({ command: { dangerous: e, agentTool: n, command: r } }) => e !== true && n?.defaultPlatforms?.includes(t) ? [r] : [])]));
}
const o = {
version: t,
dangerous: r,
defaults: i,
foregroundRestricted: new Set(e.nodeInvokePolicies.flatMap(({ policy: e }) => e.foregroundRestrictedOnIos === true ? e.commands.map((e) => e.trim()) : []))
};
pluginPolicies.set(e, o);
return o;
}
function listDangerousPluginNodeCommands() {
return [...getPreparedPluginNodePolicy()?.dangerous ?? []];
}
function isForegroundRestrictedPluginNodeCommand(e) {
const t = e.trim();
return t !== "" && (getPreparedPluginNodePolicy()?.foregroundRestricted.has(t) ?? false);
}
function isLiveNodeSession(e) {
return typeof e?.nodeId === "string" && e.nodeId.trim() !== "" && typeof e.connId === "string" && e.connId.trim() !== "";
}
function hasTalkSurface(e) {
return (e?.caps ?? []).some((e) => normalizeOptionalLowercaseString(e) === "talk") || (e?.commands ?? []).some((e) => normalizeOptionalLowercaseString(e)?.startsWith("talk."));
}
function resolveNodeCommandAllowlistInternal(e, t, n = false) {
const r = normalizePlatformId(t?.platform, t?.deviceFamily);
const i = r === "macos" || r === "windows" || r === "linux";
const a = PLATFORM_DEFAULTS[r].filter((e) => n || !i || !DESKTOP_HOST_COMMANDS.has(e));
const o = r === "ios" && normalizeDeviceMetadataForPolicy(t?.deviceFamily) === "iphone" ? IOS_WATCH_RELAY_COMMANDS : [];
const s = hasTalkSurface(t) ? TALK_PTT_COMMANDS : [];
const c = getPreparedPluginNodePolicy();
const l = c?.defaults.get(r) ?? [];
const u = i ? (t?.approvedCommands ?? (isLiveNodeSession(t) ? t?.commands ?? [] : [])).filter((e) => DESKTOP_HOST_COMMANDS.has(e.trim())) : [];
const d = e.gateway?.nodes?.commands?.allow ?? [];
const f = new Set(e.gateway?.nodes?.commands?.deny ?? []);
const p = new Set(DEFAULT_DANGEROUS_NODE_COMMANDS);
const m = new Set([
...a,
...o,
...s,
...l,
...u,
...d
].map((e) => e.trim()).filter((e) => e && (!c?.dangerous.has(e) || a.includes(e)) && (n || !p.has(e))));
for (const e of d) {
const t = e.trim();
if (t) m.add(t);
}
if (e.wizard?.appRecommendations === false) m.delete(NODE_DEVICE_APPS_COMMAND);
for (const e of f) {
const t = e.trim();
if (t && (!n || !p.has(t))) m.delete(t);
}
for (const e of NODE_WORKER_PRIVATE_COMMANDS) m.delete(e);
return m;
}
function resolveNodeCommandAllowlist(e, t) {
return resolveNodeCommandAllowlistInternal(e, t);
}
function resolveNodePairingCommandAllowlist(e, t) {
return resolveNodeCommandAllowlistInternal(e, t, true);
}
function normalizeDeclaredNodeCommands(e) {
return normalizeUniqueTrimmedStringList(e.declaredCommands).filter((t) => !isPrivateNodeInvokeCommand(t) && e.allowlist.has(t));
}
const CAPABILITY_COMMAND_FAMILIES = /* @__PURE__ */ new Map([
["camera", /* @__PURE__ */ new Set([
...CAMERA_COMMANDS,
...MAC_CAMERA_COMMANDS,
...CAMERA_DANGEROUS_COMMANDS
])],
["computer", new Set(COMPUTER_COMMANDS)],
["location", new Set(MOBILE_NODE_COMMANDS.location)],
["screen", /* @__PURE__ */ new Set([...DESKTOP_SCREEN_COMMANDS, ...SCREEN_DANGEROUS_COMMANDS])]
]);
/** Drops capabilities whose commands policy withheld without admitting a sibling. */
function retainFulfilledNodeCapabilities(e) {
return e.caps.filter((t) => {
const n = CAPABILITY_COMMAND_FAMILIES.get(t);
return !n || !e.withheldCommands.some((e) => n.has(e)) || e.admittedCommands.some((e) => n.has(e));
});
}
function isNodeCommandAllowed(e) {
const t = e.command.trim();
if (!t) return {
ok: false,
reason: "command required"
};
if (isPrivateNodeInvokeCommand(t) || !e.allowlist.has(t)) return {
ok: false,
reason: "command not allowlisted"
};
if (Array.isArray(e.declaredCommands) && e.declaredCommands.length > 0) {
if (!e.declaredCommands.includes(t)) return {
ok: false,
reason: "command not declared by node"
};
} else return {
ok: false,
reason: "node did not declare commands"
};
return { ok: true };
}
/** Present the failed authority layer without suggesting that another layer can grant it. */
function formatRequiredNodeCommandUnavailable(e, t, n) {
const r = `paired-device command ${e}`;
if (t === "undeclared") {
const t = getActivePluginGatewayNodePolicyRegistry()?.nodeHostCommands.find((t) => t.command.command === e)?.pluginId;
if (t === "codex") return "This model uses the Codex harness, which is unavailable on this device. Install it on that node (openclaw plugins install @openclaw/codex), or enable an existing install (openclaw plugins enable codex). Then restart the node (openclaw node restart) and approve its updated command surface, or choose a model using the OpenClaw harness.";
return `${r} is not advertised by node ${n}; ${t ? `enable the ${t} plugin on that node (openclaw plugins enable ${t})` : "enable the plugin or node capability that provides this command on that node"}, then restart the node (openclaw node restart) and approve its updated command surface`;
}
if (t === "pending-approval") return `${r} is awaiting pairing approval for node ${n}; find its updated command surface request with openclaw nodes pending, then run openclaw nodes approve <requestId>`;
return `${r} is blocked by Gateway policy for node ${n}; allow it in gateway.nodes.commands.allow and remove any matching gateway.nodes.commands.deny entry`;
}
/**
* Resolves declaration, pairing, and runtime policy once at their Gateway owner.
* Clients receive one closed state instead of rebuilding authority from partial lists.
*/
function resolveRequiredNodeCommandAuthority(e) {
const t = new Set(e.declaredCommands);
const n = new Set(e.effectiveCommands);
const r = e.requiredCommands.find((t) => e.withheldCommands.includes(t));
const i = r || e.requiredCommands.find((t) => !n.has(t) || !isNodeCommandAllowed({
command: t,
declaredCommands: e.effectiveCommands,
allowlist: e.allowlist
}).ok);
if (i === void 0) {
const t = e.requiredCommands[0];
return t ? {
command: t,
state: "invocable"
} : void 0;
}
const a = r ? "unauthorized" : !t.has(i) ? "undeclared" : n.has(i) ? "unauthorized" : "pending-approval";
return {
command: i,
state: a,
message: formatRequiredNodeCommandUnavailable(i, a, e.nodeId)
};
}
//#endregion
export { isForegroundRestrictedPluginNodeCommand as a, normalizeDeclaredNodeCommands as c, resolveRequiredNodeCommandAuthority as d, retainFulfilledNodeCapabilities as f, TALK_PTT_COMMANDS as i, resolveNodeCommandAllowlist as l, IOS_WATCH_RELAY_COMMANDS as n, isNodeCommandAllowed as o, PLATFORM_DEFAULTS as r, listDangerousPluginNodeCommands as s, DEFAULT_DANGEROUS_NODE_COMMANDS as t, resolveNodePairingCommandAllowlist as u };