openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
244 lines (243 loc) • 6.37 kB
JavaScript
import { l as normalizeOptionalString, o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs";
import "./src-H3RMXyFL.mjs";
import { t as expectDefined } from "./expect-DDakjvvQ.mjs";
import { u as normalizeSortedUniqueStringEntries } from "./string-normalization-DLmfYZXI.mjs";
import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs";
import "./session-key-CqAyZeVk.mjs";
import { n as normalizeAccountId } from "./account-id-BqKT3GuY.mjs";
import { i as normalizeChatChannelId } from "./ids-BRSWrLzE.mjs";
import { t as listPluginContributionIds } from "./plugin-registry-contributions-iAw3gSDD.mjs";
import "./plugin-registry-BrQav-kR.mjs";
import { i as getBundledChannelSetupPlugin } from "./bundled-BnH1gY8j.mjs";
import "./registry-BB86xKJa.mjs";
import { n as getLoadedChannelPlugin } from "./registry-ByqVE5S3.mjs";
import "./plugins-BoMiRtiY.mjs";
import { i as listRouteBindings, t as isRouteBinding } from "./bindings-C2C-UuI7.mjs";
import { i as resolveChannelDefaultAccountId } from "./helpers-ELZNDAnv.mjs";
import { a as formatUnknownChannelMessage } from "./error-format-O7EzXfiI.mjs";
//#region src/commands/agents.bindings.ts
function bindingMatchKey(e) {
const t = normalizeOptionalString(e.accountId);
const n = bindingMatchIdentityKey(e);
return JSON.stringify([n, t === "*" ? "*" : normalizeAccountId(t)]);
}
function bindingMatchIdentityKey(e) {
const t = Array.isArray(e.roles) ? normalizeSortedUniqueStringEntries(e.roles) : [];
return JSON.stringify([
normalizeLowercaseStringOrEmpty(e.channel),
e.peer?.kind ?? "",
e.peer?.id ?? "",
e.guildId ?? "",
e.teamId ?? "",
t.join(",")
]);
}
function applyAgentBindings(e, t) {
const n = [...listRouteBindings(e)];
const r = (e.bindings ?? []).filter((e) => !isRouteBinding(e));
const i = /* @__PURE__ */ new Map();
for (const e of n) {
const t = bindingMatchKey(e.match);
if (!i.has(t)) i.set(t, normalizeAgentId(e.agentId));
}
const a = [];
const o = [];
const s = [];
const c = [];
for (const e of t) {
const t = normalizeAgentId(e.agentId);
const r = bindingMatchKey(e.match);
const l = i.get(r);
if (l) {
if (l === t) s.push(e);
else c.push({
binding: e,
existingAgentId: l
});
continue;
}
const u = normalizeOptionalString(e.match.accountId) ? n.findIndex((n) => !normalizeOptionalString(n.match.accountId) && normalizeAgentId(n.agentId) === t && bindingMatchIdentityKey(n.match) === bindingMatchIdentityKey(e.match)) : -1;
if (u >= 0) {
const r = n[u];
if (!r) continue;
const a = bindingMatchKey(r.match);
const s = {
...r,
agentId: t,
match: {
...r.match,
accountId: e.match.accountId?.trim()
}
};
n[u] = s;
i.delete(a);
i.set(bindingMatchKey(s.match), t);
o.push(s);
continue;
}
i.set(r, t);
a.push({
...e,
agentId: t
});
}
if (a.length === 0 && o.length === 0) return {
config: e,
added: a,
updated: o,
skipped: s,
conflicts: c
};
return {
config: {
...e,
bindings: [
...n,
...a,
...r
]
},
added: a,
updated: o,
skipped: s,
conflicts: c
};
}
function removeAgentBindings(e, t) {
const n = listRouteBindings(e);
const r = (e.bindings ?? []).filter((e) => !isRouteBinding(e));
const i = /* @__PURE__ */ new Set();
const a = [];
const o = [];
const s = [];
for (const e of t) {
const t = normalizeAgentId(e.agentId);
const r = bindingMatchKey(e.match);
let c = -1;
let l = null;
for (const [e, a] of n.entries()) {
if (i.has(e)) continue;
if (bindingMatchKey(a.match) !== r) continue;
const n = normalizeAgentId(a.agentId);
if (n === t) {
c = e;
break;
}
l = n;
}
if (c >= 0) {
const e = n[c];
if (e) {
i.add(c);
a.push(e);
}
continue;
}
if (l) {
s.push({
binding: e,
existingAgentId: l
});
continue;
}
o.push(e);
}
if (i.size === 0) return {
config: e,
removed: a,
missing: o,
conflicts: s
};
const c = [...n.filter((e, t) => !i.has(t)), ...r];
return {
config: {
...e,
bindings: c.length > 0 ? c : void 0
},
removed: a,
missing: o,
conflicts: s
};
}
function buildChannelBinding(e) {
let t = e.explicitAccountId?.trim();
if (!t) {
const n = getLoadedChannelPlugin(e.channel) ?? getBundledChannelSetupPlugin(e.channel);
const r = (n?.setupContract?.resolveBindingAccountId ?? n?.setup?.resolveBindingAccountId)?.({
cfg: e.config,
agentId: e.agentId
});
if (r?.trim()) t = r.trim();
else if (n && n.config.listAccountIds(e.config).length > 1) t = "*";
else if (n?.meta.forceAccountBinding) t = resolveChannelDefaultAccountId({
plugin: n,
cfg: e.config
});
}
const n = { channel: e.channel };
if (t) n.accountId = t;
return {
type: "route",
agentId: e.agentId,
match: n
};
}
function buildChannelBindings(e) {
const t = normalizeAgentId(e.agentId);
return Array.from(e.selection, (n) => buildChannelBinding({
channel: n,
config: e.config,
agentId: t,
explicitAccountId: e.accountIds?.[n]
}));
}
function parseBindingSpecs(e) {
const t = [];
const n = [];
const r = e.specs ?? [];
const i = normalizeAgentId(e.agentId);
let a;
for (const o of r) {
const r = o?.trim();
if (!r) continue;
const [s, c, ...l] = r.split(":");
if (l.length > 0) {
n.push(`Invalid binding "${r}". Account id cannot contain ":". Use <channel>:<account>, for example telegram:default.`);
continue;
}
let u = normalizeChatChannelId(s);
if (!u) {
const t = normalizeOptionalString(s)?.toLowerCase();
if (t) {
a ??= new Set(listPluginContributionIds({
contribution: "channels",
includeDisabled: true,
config: e.config,
env: process.env
}));
u = a.has(t) ? t : null;
}
}
if (!u) {
n.push(formatUnknownChannelMessage({ channel: expectDefined(s, "agents.bindings channel raw") }));
continue;
}
const d = c?.trim();
if (c !== void 0 && !d) {
n.push(`Invalid binding "${r}". Account id is empty. Use <channel>:<account>, for example telegram:default.`);
continue;
}
t.push(buildChannelBinding({
channel: u,
config: e.config,
agentId: i,
explicitAccountId: d
}));
}
return {
bindings: t,
errors: n
};
}
//#endregion
export { removeAgentBindings as i, buildChannelBindings as n, parseBindingSpecs as r, applyAgentBindings as t };