openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
132 lines (131 loc) • 5.51 kB
JavaScript
import { f as normalizeStringEntries } from "./string-normalization-DLmfYZXI.mjs";
import { o as writeRuntimeJson, r as defaultRuntime } from "./runtime-B-SLYP-m.mjs";
import { r as terminalText } from "./safe-text-C80HAQ2e.mjs";
import { t as formatCliCommand } from "./command-format-CqdwUgPk.mjs";
import { n as normalizeAgentId, r as normalizeAgentIdStrict } from "./agent-id-CQjtp2ft.mjs";
import "./session-key-CqAyZeVk.mjs";
import { i as listAgentIds } from "./agent-roster-t1ooUdXp.mjs";
import { l as resolveAgentOperationAgentId } from "./agent-scope-config-DmXgFGKP.mjs";
import { r as replaceConfigFile } from "./mutate-DhAOi0-2.mjs";
import "./config-CMA-J084.mjs";
import { i as listRouteBindings, t as isRouteBinding } from "./bindings-C2C-UuI7.mjs";
import { d as throwExpectedCliError } from "./failure-output-DVVDqwuu.mjs";
import { r as logConfigUpdated } from "./logging-D0RirHBi.mjs";
import { r as requireValidConfigForWrite, t as requireValidConfig } from "./config-validation-Cy6dkTWs.mjs";
import { n as describeBindingConflict, t as describeBinding } from "./agents.binding-format-DRkepwQw.mjs";
//#region src/commands/agents.commands.bind.ts
function resolveTargetAgentId(e) {
const t = e.agentInput === void 0 ? null : normalizeAgentIdStrict(e.agentInput);
if (t && !t.ok) throwExpectedCliError(`Agent "${e.agentInput}" not found. Run ${formatCliCommand("openclaw agents list")} to see configured agents.`);
const n = t?.value ?? resolveAgentOperationAgentId(e.cfg);
if (!listAgentIds(e.cfg).includes(n)) throwExpectedCliError(`Agent "${n}" not found. Run ${formatCliCommand("openclaw agents list")} to see configured agents.`);
return n;
}
async function agentsBindingsCommand(e) {
const t = await requireValidConfig(defaultRuntime, { skipPluginValidation: true });
if (!t) return;
const n = e.agent === void 0 ? void 0 : resolveTargetAgentId({
cfg: t,
agentInput: e.agent
});
const r = listRouteBindings(t).filter((e) => !n || normalizeAgentId(e.agentId) === n);
if (e.json) {
defaultRuntime.writeJson(r.map((e) => ({
agentId: normalizeAgentId(e.agentId),
match: e.match,
description: describeBinding(e)
})));
return;
}
if (r.length === 0) {
defaultRuntime.log(n ? terminalText`No routing bindings for agent "${n}".` : "No routing bindings.");
return;
}
defaultRuntime.log(["Routing bindings:", ...r.map((e) => terminalText`- ${normalizeAgentId(e.agentId)} <- ${describeBinding(e)}`)].join("\n"));
}
async function agentsUpdateBindingsCommand(e, t) {
const n = await requireValidConfigForWrite(defaultRuntime);
if (!n) return;
const r = n.snapshot.sourceConfig;
const i = resolveTargetAgentId({
cfg: r,
agentInput: t.agent
});
if (e === "unbind" && t.all && (t.bind?.length ?? 0) > 0) throwExpectedCliError("Use either --all or --bind, not both.");
const a = e === "unbind" && t.all;
const o = e === "bind";
let s;
if (a) {
const e = listRouteBindings(r);
const t = e.filter((e) => normalizeAgentId(e.agentId) === i);
const n = [...e.filter((e) => normalizeAgentId(e.agentId) !== i), ...(r.bindings ?? []).filter((e) => !isRouteBinding(e))];
s = {
config: {
...r,
bindings: n.length > 0 ? n : void 0
},
removed: t,
missing: [],
conflicts: []
};
} else {
const e = normalizeStringEntries(t.bind);
if (e.length === 0) throwExpectedCliError(o ? "Provide at least one --bind <channel[:accountId]>." : "Provide at least one --bind <channel[:accountId]> or use --all.");
const { parseBindingSpecs: n } = await import("./agents.bindings-9-kSlzCY.mjs");
const a = n({
agentId: i,
specs: e,
config: r
});
if (a.errors.length > 0) throwExpectedCliError(a.errors.join("\n"));
const c = a.bindings;
const { applyAgentBindings: l, removeAgentBindings: u } = await import("./agents.bindings-9-kSlzCY.mjs");
s = o ? l(r, c) : u(r, c);
}
const c = "added" in s ? s.added.length > 0 || s.updated.length > 0 : s.removed.length > 0;
if (c) {
await replaceConfigFile({
sourceConfig: s.config,
...n
});
if (!t.json) logConfigUpdated(defaultRuntime);
}
const l = {
agentId: i,
..."added" in s ? {
added: s.added.map(describeBinding),
updated: s.updated.map(describeBinding),
skipped: s.skipped.map(describeBinding)
} : {
removed: s.removed.map(describeBinding),
missing: s.missing.map(describeBinding)
},
conflicts: s.conflicts.map(describeBindingConflict)
};
if (t.json) {
writeRuntimeJson(defaultRuntime, l);
if (l.conflicts.length > 0) defaultRuntime.exit(1);
return;
}
if (a && "removed" in l) {
defaultRuntime.log(l.removed.length > 0 ? terminalText`Removed ${l.removed.length} binding(s) for "${i}".` : terminalText`No bindings to remove for agent "${i}".`);
return;
}
if (!c) defaultRuntime.log(o ? "No new bindings added." : "No bindings removed.");
const u = "added" in l ? [
["Added bindings:", l.added],
["Updated bindings:", l.updated],
["Already present:", l.skipped]
] : [["Removed bindings:", l.removed], ["Not found:", l.missing]];
for (const [e, t] of u) if (t.length > 0) {
defaultRuntime.log(e);
for (const e of t) defaultRuntime.log(terminalText`- ${e}`);
}
if (l.conflicts.length > 0) {
defaultRuntime.error(o ? "Skipped bindings already claimed by another agent:" : "Bindings are owned by another agent:");
for (const e of l.conflicts) defaultRuntime.error(terminalText`- ${e}`);
defaultRuntime.exit(1);
}
}
//#endregion
export { agentsBindingsCommand, agentsUpdateBindingsCommand };