openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
106 lines (105 loc) • 5.14 kB
JavaScript
import { c as normalizeOptionalLowercaseString, t as hasNonEmptyString } from "./string-coerce-CCIiB4H7.mjs";
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { b as uniqueStrings } from "./string-normalization-DLmfYZXI.mjs";
import { r as resolveStateDir } from "./state-dir-DiG0gXzD.mjs";
import "./utils-C8YECn2Q.mjs";
import "./paths-DAcAg5aV.mjs";
import { c as listOfficialExternalChannelEnvVars } from "./official-external-plugin-catalog-DtkWz1Xo.mjs";
import { t as hasMeaningfulChannelConfig } from "./channel-config-activation-BzEa8FnN.mjs";
import { n as isChannelConfigMetadataKey } from "./config-metadata-BVUrt758.mjs";
import { t as listChannelCatalogEntries } from "./channel-catalog-registry-DPZYoC27.mjs";
import { a as listBundledChannelIdsForPackageState, n as hasBundledChannelPackageState, r as hasBundledChannelPackageStateAsync } from "./package-state-probes-BxreN8Qk.mjs";
import fs from "node:fs";
import os from "node:os";
//#region src/channels/plugins/bundled-ids.ts
function listBundledChannelIds(e = process.env, t) {
return listChannelCatalogEntries({
origin: "bundled",
env: e,
discovery: t
}).map((e) => e.channel.id).filter((e) => Boolean(e)).toSorted((e, t) => e.localeCompare(t));
}
//#endregion
//#region src/channels/config-presence.ts
/** Lists channels explicitly disabled in config so activation logic can suppress auto-detection. */
function listExplicitlyDisabledChannelIdsForConfig(e) {
const t = isRecord(e.channels) ? e.channels : null;
if (!t) return [];
return Object.entries(t).filter(([, e]) => isRecord(e) && e.enabled === false).map(([e]) => e.trim()).filter((e) => e && !isChannelConfigMetadataKey(e)).map((e) => normalizeOptionalLowercaseString(e)).filter((e) => Boolean(e));
}
function listChannelEnvPrefixes(e) {
return e.map((e) => [`${e.replace(/[^a-z0-9]+/gi, "_").toUpperCase()}_`, e]);
}
/** Lists channel ids detected from config, env vars, or persisted auth state. */
function listPotentialConfiguredChannelIds(e, t = process.env, n = {}) {
return uniqueStrings(listPotentialConfiguredChannelPresenceSignals(e, t, n).map((e) => e.channelId));
}
/** Lists deduplicated channel presence signals with their detection source. */
function listPotentialConfiguredChannelPresenceSignals(e, t = process.env, n = {}) {
const r = /* @__PURE__ */ new Map();
const addSignal = (e, t) => {
const n = e.trim();
if (!n || isChannelConfigMetadataKey(n)) return;
const i = `${t}:${n}`;
if (!r.has(i)) r.set(i, {
channelId: n,
source: t
});
};
const i = isRecord(e.channels) ? e.channels : null;
if (i) for (const [e, t] of Object.entries(i)) {
if (isChannelConfigMetadataKey(e)) continue;
if (hasMeaningfulChannelConfig(t, e)) addSignal(e, "config");
}
if (n.ambientEnvTriggers !== "suppress") {
const e = n.channelIds ? new Set(n.channelIds.map((e) => normalizeOptionalLowercaseString(e)).filter((e) => Boolean(e))) : void 0;
const r = listChannelEnvPrefixes(n.channelIds ?? listBundledChannelIds(t, n.discovery));
const i = listOfficialExternalChannelEnvVars().filter(({ channelId: t }) => !e || e.has(t));
for (const [e, n] of Object.entries(t)) {
if (!hasNonEmptyString(n)) continue;
for (const [t, n] of r) if (e.startsWith(t)) addSignal(n, "env");
for (const { channelId: t, envVars: n } of i) if (n.includes(e)) addSignal(t, "env");
}
}
if (n.includePersistedAuthState !== false && fs.existsSync(resolveStateDir(t, os.homedir))) for (const r of listBundledChannelIdsForPackageState("persistedAuthState", n.discovery)) {
if (n.persistedAuthChannelIds && !n.persistedAuthChannelIds.has(normalizeOptionalLowercaseString(r) ?? "")) continue;
if (hasBundledChannelPackageState({
metadataKey: "persistedAuthState",
channelId: r,
cfg: e,
env: t,
discovery: n.discovery
})) addSignal(r, "persisted-auth");
}
return [...r.values()];
}
/** Reads persisted credential presence off-thread before publishing channel signals. */
async function listPotentialConfiguredChannelPresenceSignalsAsync(e, t = process.env, n = {}) {
const r = listPotentialConfiguredChannelPresenceSignals(e, t, {
...n,
includePersistedAuthState: false
});
const i = /* @__PURE__ */ new Set();
if (n.includePersistedAuthState === false || n.persistedAuthChannelIds?.size === 0 || !fs.existsSync(resolveStateDir(t, os.homedir))) return r;
for (const a of listBundledChannelIdsForPackageState("persistedAuthState", n.discovery)) {
const o = a.trim();
if (!o || isChannelConfigMetadataKey(o) || i.has(o)) continue;
if (n.persistedAuthChannelIds && !n.persistedAuthChannelIds.has(normalizeOptionalLowercaseString(a) ?? "")) continue;
if (await hasBundledChannelPackageStateAsync({
metadataKey: "persistedAuthState",
channelId: a,
cfg: e,
env: t,
discovery: n.discovery
})) {
i.add(o);
r.push({
channelId: o,
source: "persisted-auth"
});
}
}
return r;
}
//#endregion
export { listPotentialConfiguredChannelPresenceSignalsAsync as i, listPotentialConfiguredChannelIds as n, listPotentialConfiguredChannelPresenceSignals as r, listExplicitlyDisabledChannelIdsForConfig as t };