openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
97 lines (96 loc) • 5.26 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { f as normalizeStringEntries } from "./string-normalization-DLmfYZXI.mjs";
import { t as formatCliCommand } from "./command-format-CqdwUgPk.mjs";
import { i as normalizeChatChannelId } from "./ids-BRSWrLzE.mjs";
import { t as INTERNAL_MESSAGE_CHANNEL } from "./message-channel-constants-CkbqRgGl.mjs";
import { i as resolveChannelPluginRegistration, t as getChannelPlugin } from "./registry-ByqVE5S3.mjs";
import { t as note } from "./note-CnLgHvug.mjs";
import { r as resolveReadOnlyChannelPluginsForConfig } from "./read-only-idsFCBWl.mjs";
//#region src/commands/doctor-command-owner.ts
/** Resolve installed setup contracts without activating channel runtimes during Doctor. */
function createCommandOwnerChannelResolver(e) {
let t;
return (n) => {
const r = resolveChannelPluginRegistration(n, { loadedOnly: true })?.plugin;
if (r) return r;
if (!t) try {
t = resolveReadOnlyChannelPluginsForConfig(e, {
includePersistedAuthState: false,
includeSetupFallbackPlugins: true
}).plugins;
} catch {
t = [];
}
const i = t.find((e) => e.id === n);
return i?.messaging?.inferTargetChatType ? i : getChannelPlugin(n) ?? i;
};
}
/** Preserve plugin-owned target kinds while normalizing legacy owner envelopes. */
function migrateLegacyCommandOwners(e, t) {
const n = e.commands?.ownerAllowFrom;
if (!Array.isArray(n)) return e;
let r = false;
const i = createCommandOwnerChannelResolver(e);
const a = n.map((e, n) => {
const a = typeof e === "string" ? /^([^:]+):user:([^:\s*]+)$/i.exec(e.trim()) : null;
const o = a && normalizeChatChannelId(a[1]);
if (!o || !a) return e;
const s = i(o);
if (!s || s.messaging?.directTargetStyle === "user-prefixed") return e;
r = true;
t.push(`Normalized commands.ownerAllowFrom[${n}] from ${o}:user:id to ${o}:id.`);
return `${o}:${a[2]}`;
});
return r ? {
...e,
commands: {
...e.commands,
ownerAllowFrom: a
}
} : e;
}
function resolveConfiguredCommandOwners(e) {
const t = e.commands?.ownerAllowFrom;
if (!Array.isArray(t)) return [];
return normalizeStringEntries(t.map((e) => String(e ?? ""))).filter((e) => e !== "*" && !e.endsWith(":*"));
}
function hasConfiguredCommandOwners(e) {
return resolveConfiguredCommandOwners(e).length > 0;
}
/** Formats a channel sender id into the commands.ownerAllowFrom entry shape. */
function formatCommandOwnerFromChannelSender(e) {
const t = normalizeOptionalString(e.id);
if (!t) return null;
const n = t.indexOf(":");
if (n > 0) {
if (t.slice(0, n).toLowerCase() === String(e.channel).toLowerCase()) return t;
}
return `${e.channel}:${t}`;
}
/** Gives admitted senders an operator-run command without granting owner authority. */
function formatCommandOwnerHint(e) {
if (e.channel === "webchat") return "Ask the operator to grant this Gateway client operator.admin access.";
const t = e.channel && e.id ? formatCommandOwnerFromChannelSender({
channel: e.channel,
id: e.id
}) : null;
if (!t) return "Ask the operator to set commands.ownerAllowFrom to your channel user id.";
if (!e.cfg) return `Ask the operator to add \`${t}\` to \`commands.ownerAllowFrom\`.`;
const n = JSON.stringify([.../* @__PURE__ */ new Set([...resolveConfiguredCommandOwners(e.cfg), t])]).replaceAll("'", process.platform === "win32" ? "''" : "'\\''");
return `Ask the operator to run \`${formatCliCommand("openclaw config set commands.ownerAllowFrom")} '${n}'\` in a terminal to make this sender a command owner.`;
}
function noteCommandOwnerHealth(e) {
if (hasConfiguredCommandOwners(e)) return;
note([
"No channel command owner is configured.",
"A command owner is your trusted human operator account, allowed to update OpenClaw with /update, restart the Gateway, change configuration, and approve commands. Chat allowlists do not grant this authority.",
`Local Control UI/WebChat clients with operator.admin already have owner authority; no channel is required. Run ${formatCliCommand("openclaw dashboard")} on the Gateway host and open its link in your browser (use ${formatCliCommand("openclaw dashboard --json")} on a headless host).`,
`If the Control UI shows limited access, open Inbox > System > Limited access > Request admin, then run its displayed ${formatCliCommand("openclaw devices approve <requestId>")} command on the Gateway host.`,
`For external channels, run ${formatCliCommand("openclaw channels add")} and complete a channel's setup to choose your operator account, including servers and groups without DM pairing.`,
"CLI channel pairing approval records the first command owner. Control UI channel-sender approval has a separate owner checkbox.",
`For an existing channel sender, set commands.ownerAllowFrom to your channel user id, for example ${formatCliCommand("openclaw config set commands.ownerAllowFrom '[\"telegram:123456789\"]'")}`,
"Restart the Gateway after changing commands.ownerAllowFrom if it is already running."
].join("\n"), "Command owner");
}
//#endregion
export { migrateLegacyCommandOwners as a, hasConfiguredCommandOwners as i, formatCommandOwnerFromChannelSender as n, noteCommandOwnerHealth as o, formatCommandOwnerHint as r, createCommandOwnerChannelResolver as t };