openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
160 lines (159 loc) • 5.3 kB
JavaScript
import "./session-key-BnWWjqNc.js";
import "./account-id-CETVCrTz.js";
//#region src/channels/plugins/config-helpers.ts
function isConfiguredSecretValue(value) {
if (typeof value === "string") return value.trim().length > 0;
return Boolean(value);
}
/**
* Updates an account enabled flag in a channel config section.
*/
function setAccountEnabledInConfigSection(params) {
const accountKey = params.accountId || "default";
const base = params.cfg.channels?.[params.sectionKey];
const hasAccounts = Boolean(base?.accounts);
if (params.allowTopLevel && accountKey === "default" && !hasAccounts) return {
...params.cfg,
channels: {
...params.cfg.channels,
[params.sectionKey]: {
...base,
enabled: params.enabled
}
}
};
const baseAccounts = base?.accounts ?? {};
const existing = baseAccounts[accountKey] ?? {};
return {
...params.cfg,
channels: {
...params.cfg.channels,
[params.sectionKey]: {
...base,
accounts: {
...baseAccounts,
[accountKey]: {
...existing,
enabled: params.enabled
}
}
}
}
};
}
/**
* Deletes one account from a channel config section, pruning empty channel/accounts objects.
*/
function deleteAccountFromConfigSection(params) {
const accountKey = params.accountId || "default";
const base = params.cfg.channels?.[params.sectionKey];
if (!base) return params.cfg;
const baseAccounts = base.accounts && typeof base.accounts === "object" ? { ...base.accounts } : void 0;
if (accountKey !== "default") {
const accounts = baseAccounts ? { ...baseAccounts } : {};
delete accounts[accountKey];
return {
...params.cfg,
channels: {
...params.cfg.channels,
[params.sectionKey]: {
...base,
accounts: Object.keys(accounts).length ? accounts : void 0
}
}
};
}
if (baseAccounts && Object.keys(baseAccounts).length > 0) {
delete baseAccounts[accountKey];
const baseRecord = { ...base };
for (const field of params.clearBaseFields ?? []) if (field in baseRecord) baseRecord[field] = void 0;
return {
...params.cfg,
channels: {
...params.cfg.channels,
[params.sectionKey]: {
...baseRecord,
accounts: Object.keys(baseAccounts).length ? baseAccounts : void 0
}
}
};
}
const nextChannels = { ...params.cfg.channels };
delete nextChannels[params.sectionKey];
const nextCfg = { ...params.cfg };
if (Object.keys(nextChannels).length > 0) nextCfg.channels = nextChannels;
else delete nextCfg.channels;
return nextCfg;
}
/**
* Clears selected fields from one account entry and reports whether configured data was removed.
*/
function clearAccountEntryFields(params) {
const accountKey = params.accountId || "default";
const baseAccounts = params.accounts && typeof params.accounts === "object" ? { ...params.accounts } : void 0;
if (!baseAccounts || !(accountKey in baseAccounts)) return {
nextAccounts: baseAccounts,
changed: false,
cleared: false
};
const entry = baseAccounts[accountKey];
if (!entry || typeof entry !== "object") return {
nextAccounts: baseAccounts,
changed: false,
cleared: false
};
const nextEntry = { ...entry };
if (!params.fields.some((field) => field in nextEntry)) return {
nextAccounts: baseAccounts,
changed: false,
cleared: false
};
const isValueSet = params.isValueSet ?? isConfiguredSecretValue;
let cleared = Boolean(params.markClearedOnFieldPresence);
for (const field of params.fields) {
if (!(field in nextEntry)) continue;
if (isValueSet(nextEntry[field])) cleared = true;
delete nextEntry[field];
}
if (Object.keys(nextEntry).length === 0) delete baseAccounts[accountKey];
else baseAccounts[accountKey] = nextEntry;
return {
nextAccounts: Object.keys(baseAccounts).length > 0 ? baseAccounts : void 0,
changed: true,
cleared
};
}
/** Clear plugin-selected account fields and prune only the config branches changed by cleanup. */
function clearAccountFieldsFromConfigSection(params) {
const nextSection = { ...params.cfg.channels?.[params.sectionKey] };
const clearedRoot = params.accountId === "default" && params.fields.some((field) => Boolean(nextSection[field]));
if (clearedRoot) for (const field of params.fields) delete nextSection[field];
const accountCleanup = clearAccountEntryFields({
accounts: nextSection.accounts,
accountId: params.accountId,
fields: params.fields,
markClearedOnFieldPresence: params.markClearedOnFieldPresence
});
if (!clearedRoot && !accountCleanup.changed) return {
nextConfig: params.cfg,
changed: false,
cleared: false
};
if (accountCleanup.changed) {
if (accountCleanup.nextAccounts) nextSection.accounts = accountCleanup.nextAccounts;
else delete nextSection.accounts;
}
const nextChannels = { ...params.cfg.channels };
if (Object.keys(nextSection).length > 0) nextChannels[params.sectionKey] = nextSection;
else delete nextChannels[params.sectionKey];
const nextConfig = { ...params.cfg };
if (Object.keys(nextChannels).length > 0) nextConfig.channels = nextChannels;
else delete nextConfig.channels;
return {
nextConfig,
changed: true,
cleared: clearedRoot || accountCleanup.cleared
};
}
//#endregion
export { setAccountEnabledInConfigSection as i, clearAccountFieldsFromConfigSection as n, deleteAccountFromConfigSection as r, clearAccountEntryFields as t };