openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
39 lines (38 loc) • 2.05 kB
JavaScript
import { r as normalizeProviderId } from "./provider-id-D1VImdKk.mjs";
//#region src/plugins/provider-owner-index.ts
/** Captures declared receivers independently from plugins triggered by a provider. */
function buildDeclaredProviderOwnerIndex(manifests) {
const winners = /* @__PURE__ */ new Map();
for (const plugin of manifests) if (!winners.has(plugin.id)) winners.set(plugin.id, plugin);
const owners = /* @__PURE__ */ new Map();
for (const phase of ["runtime", "setup"]) {
const runtimeRefs = new Set(owners.keys());
for (const plugin of winners.values()) {
const refs = phase === "runtime" ? plugin.providers : plugin.setup?.providers?.map((entry) => entry.id) ?? [];
for (const ref of refs) {
const normalized = normalizeProviderId(ref);
if (phase === "setup" && runtimeRefs.has(normalized)) continue;
const ids = owners.get(normalized) ?? /* @__PURE__ */ new Set();
ids.add(plugin.id);
owners.set(normalized, ids);
}
}
}
return owners;
}
function matchesDeclaredProviderOwner(owners, provider, pluginId) {
return owners?.get(normalizeProviderId(provider))?.has(pluginId) ?? true;
}
/** Runtime aliases require a provider declared by the same plugin. */
function pluginOwnsProviderRef(plugin, normalizedProvider) {
if (plugin.providers.length === 0) return false;
const providers = new Set(plugin.providers.map(normalizeProviderId));
if (providers.has(normalizedProvider)) return true;
const ownsAlias = (rawAlias, target) => {
const targetProvider = normalizeProviderId(target);
return normalizeProviderId(rawAlias) === normalizedProvider && Boolean(targetProvider) && providers.has(targetProvider);
};
return Object.entries(plugin.providerAuthAliases ?? {}).some(([alias, target]) => typeof target === "string" && ownsAlias(alias, target)) || Object.entries(plugin.modelCatalog?.aliases ?? {}).some(([alias, target]) => ownsAlias(alias, target.provider));
}
//#endregion
export { matchesDeclaredProviderOwner as n, pluginOwnsProviderRef as r, buildDeclaredProviderOwnerIndex as t };