UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

154 lines (153 loc) • 5.18 kB
import "./session-key-CqAyZeVk.mjs"; import { t as DEFAULT_ACCOUNT_ID } from "./account-id-BqKT3GuY.mjs"; import { i as resolveChannelAccountKey, n as resolveAccountKey } from "./account-lookup-FhQZVFfo.mjs"; //#region src/channels/plugins/config-helpers.ts /** Replace one section; undefined removes it and prunes an empty channels object. */ function writeChannelSection(e, t, n) { if (n !== void 0) return { ...e, channels: { ...e.channels, [t]: n } }; const r = { ...e.channels }; delete r[t]; const i = { ...e }; if (Object.keys(r).length > 0) i.channels = r; else delete i.channels; return i; } function setTopLevelChannelEnabledInConfigSection(e) { return writeChannelSection(e.cfg, e.sectionKey, { ...e.cfg.channels?.[e.sectionKey], enabled: e.enabled }); } function clearTopLevelChannelConfigFields(e) { const t = e.cfg.channels?.[e.sectionKey]; if (!t) return e.cfg; const n = { ...t }; for (const t of e.clearBaseFields) delete n[t]; return writeChannelSection(e.cfg, e.sectionKey, n); } function isConfiguredSecretValue(e) { if (typeof e === "string") return e.trim().length > 0; return Boolean(e); } /** * Updates an account enabled flag in a channel config section. */ function setAccountEnabledInConfigSection(e) { const t = e.accountId || "default"; const n = e.cfg.channels?.[e.sectionKey]; const r = resolveChannelAccountKey(n?.accounts, t, e.sectionKey, (e) => e, e.accountKeyPolicy, { allowMissing: true }); const i = Boolean(n?.accounts); if (e.allowTopLevel && r === "default" && !i) return setTopLevelChannelEnabledInConfigSection(e); const a = n?.accounts ?? {}; const o = a[r] ?? {}; return writeChannelSection(e.cfg, e.sectionKey, { ...n, accounts: { ...a, [r]: { ...o, enabled: e.enabled } } }); } /** * Deletes one account from a channel config section, pruning empty channel/accounts objects. */ function deleteAccountFromConfigSection(e) { const t = e.accountId || "default"; const n = e.cfg.channels?.[e.sectionKey]; const resolveStoredKey = (n) => resolveChannelAccountKey(n, t, e.sectionKey, (e) => e, e.accountKeyPolicy); const r = resolveStoredKey(n?.accounts); if (!n) return e.cfg; const i = n.accounts && typeof n.accounts === "object" ? { ...n.accounts } : {}; if (t === "default" && Object.keys(i).length === 0) return writeChannelSection(e.cfg, e.sectionKey, void 0); if (r !== void 0) { delete i[r]; const e = resolveStoredKey(i); if (e !== void 0) throw new Error(`Cannot delete account "${t}": stored keys "${r}" and "${e}" resolve to the same account. Resolve the collision before deleting.`); } const a = { ...n }; if (t === "default") { for (const t of e.clearBaseFields ?? []) if (t in a) a[t] = void 0; } return writeChannelSection(e.cfg, e.sectionKey, { ...a, accounts: Object.keys(i).length ? i : void 0 }); } /** * Clears selected fields from one account entry and reports whether configured data was removed. */ function clearAccountEntryFields(e) { const t = e.accountId || "default"; const n = e.channelId ? resolveChannelAccountKey(e.accounts, t, e.channelId, (e) => e, e.accountKeyPolicy) : resolveAccountKey(e.accounts, t, (e) => e, e.accountKeyPolicy); const r = e.accounts && typeof e.accounts === "object" ? { ...e.accounts } : void 0; if (!r || n === void 0) return { nextAccounts: r, changed: false, cleared: false }; const i = r[n]; if (!i || typeof i !== "object") return { nextAccounts: r, changed: false, cleared: false }; const a = { ...i }; if (!e.fields.some((e) => e in a)) return { nextAccounts: r, changed: false, cleared: false }; const o = e.isValueSet ?? isConfiguredSecretValue; let s = Boolean(e.markClearedOnFieldPresence); for (const t of e.fields) { if (!(t in a)) continue; if (o(a[t])) s = true; delete a[t]; } if (Object.keys(a).length === 0) delete r[n]; else r[n] = a; return { nextAccounts: Object.keys(r).length > 0 ? r : void 0, changed: true, cleared: s }; } /** Clear plugin-selected account fields and prune only the config branches changed by cleanup. */ function clearAccountFieldsFromConfigSection(e) { const t = { ...e.cfg.channels?.[e.sectionKey] }; const n = e.accountId === "default" && e.fields.some((e) => Boolean(t[e])); if (n) for (const n of e.fields) delete t[n]; const r = clearAccountEntryFields({ accounts: t.accounts, channelId: e.sectionKey, accountKeyPolicy: e.accountKeyPolicy, accountId: e.accountId, fields: e.fields, markClearedOnFieldPresence: e.markClearedOnFieldPresence }); if (!n && !r.changed) return { nextConfig: e.cfg, changed: false, cleared: false }; if (r.changed) { if (r.nextAccounts) t.accounts = r.nextAccounts; else delete t.accounts; } return { nextConfig: writeChannelSection(e.cfg, e.sectionKey, Object.keys(t).length > 0 ? t : void 0), changed: true, cleared: n || r.cleared }; } //#endregion export { setAccountEnabledInConfigSection as a, deleteAccountFromConfigSection as i, clearAccountFieldsFromConfigSection as n, setTopLevelChannelEnabledInConfigSection as o, clearTopLevelChannelConfigFields as r, writeChannelSection as s, clearAccountEntryFields as t };