openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
351 lines (350 loc) • 14.9 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { m as asSafeIntegerInRange } from "./number-coercion-D6u-4YD8.mjs";
import { a as getPluginCache, d as getProcessPluginCache, f as getScopedPluginCache, u as getPluginMetadataSnapshotCache, w as withPluginCache } from "./plugin-cache-B_eRuqTM.mjs";
import { a as getProcessGatewayPluginMetadataSnapshot } from "./current-plugin-metadata-state-BmYo9Q7A.mjs";
import { t as MANIFEST_KEY } from "./legacy-names-Cq8ruaO5.mjs";
import { M as parseClawHubPluginSpec, d as loadConfiguredHostedOfficialExternalPluginCatalogEntries, l as listOfficialExternalPluginCatalogEntries, m as resolveOfficialExternalPluginInstallSources, p as resolveOfficialExternalPluginInstall } from "./official-external-plugin-catalog-DtkWz1Xo.mjs";
import { s as parseRegistryNpmSpec } from "./npm-registry-spec-HKkAvl6N.mjs";
import { g as resolveOfficialExternalPluginId, o as getOfficialExternalPluginCatalogManifest } from "./official-external-plugin-catalog-source-4o5oPhVN.mjs";
import { a as resolveTrustedOfficialClawHubPackageName, o as resolveTrustedSourceLinkedOfficialClawHubInstall, s as resolveTrustedSourceLinkedOfficialNpmInstall } from "./official-external-install-records-DHqBi8wk.mjs";
import { t as createInstalledPluginOwnershipResolver } from "./installed-plugin-package-ownership-BAh5IZ9-.mjs";
import { v as resolveClawHubBaseUrl } from "./clawhub-client-BLg-dsn2.mjs";
import { a as fetchClawHubPluginVersionCategories, r as fetchClawHubPluginDetail } from "./clawhub-plugin-catalog-BC-4D5Jm.mjs";
//#region src/plugins/management-catalog.ts
function resolveInstalledPluginClawHubIconSource(e) {
const t = e.installRecord;
const n = t?.source === "clawhub" && normalizeOptionalString(t.clawhubPackage);
const r = t?.source === "clawhub" && normalizeOptionalString(t.clawhubUrl);
if (n && r) return {
kind: "clawhub",
baseUrl: resolveClawHubBaseUrl(r),
packageName: n
};
return e.clawhubPackage ? {
kind: "clawhub",
baseUrl: "https://clawhub.ai",
packageName: e.clawhubPackage
} : void 0;
}
function resolvePluginIconSource(e) {
const t = e.metadata.normalizePluginId(e.pluginId);
const n = e.metadata.byPluginId.get(t);
const r = normalizeOptionalString(n?.iconPath);
if (r && n) return {
kind: "file",
path: r,
rootPath: n.rootDir
};
}
async function resolvePluginIconSources(e) {
const { metadata: t, env: n } = e;
const r = t.normalizePluginId(e.pluginId);
const i = resolvePluginIconSource({
metadata: t,
pluginId: r
});
const a = i ? [i] : [];
const o = t.index.plugins.find((e) => t.normalizePluginId(e.pluginId) === r);
if (!o) return a;
const s = createInstalledPluginOwnershipResolver(t.index, n).resolvePackage(o.pluginId);
const c = s.ok ? s.value.installOwner : void 0;
const l = c ? t.index.installRecords[c] : void 0;
const { clawhubPackage: u } = resolveInstalledHostedOfficialEntry({
record: o,
installOwner: c,
installRecord: l,
officialEntries: prepareCatalogEntries((await loadOfficialCatalog()).entries),
bundledOfficialEntries: prepareCatalogEntries(listOfficialExternalPluginCatalogEntries())
});
const d = resolveInstalledPluginClawHubIconSource({
installRecord: l,
clawhubPackage: u
});
if (d) a.push(d);
return a;
}
function resolvePluginActivityIconSource(e) {
const t = e.metadata.normalizePluginId(e.pluginId);
const n = e.metadata.byPluginId.get(t);
if (!n) return;
const r = n.toolActivityIconPaths;
const i = (e.toolName && r && Object.hasOwn(r, e.toolName) ? r[e.toolName] : void 0) ?? n.activityIconPath;
return i ? {
kind: "file",
path: i,
rootPath: n.rootDir
} : void 0;
}
function getManagedPluginCache(e) {
if (e) return getPluginMetadataSnapshotCache(e);
const t = getScopedPluginCache();
if (t?.kind === "operation") return t;
const n = getProcessPluginCache().desiredMetadata;
if (n && n.boot === getProcessGatewayPluginMetadataSnapshot()) return n.cache;
return getPluginCache();
}
function withManagedPluginCache(e) {
return (t) => withPluginCache(getManagedPluginCache(t.metadata), () => e(t));
}
/** Clear process-stable catalog snapshots after an explicit owner reload. */
function clearManagedPluginCatalogCache() {
const e = getManagedPluginCache();
e.officialCatalog = void 0;
e.pluginVersionCategories = void 0;
e.pluginPublisherHandles = void 0;
}
function mergeCatalogMetadata(e, t, n) {
const r = getOfficialExternalPluginCatalogManifest(e);
const i = getOfficialExternalPluginCatalogManifest(t);
const a = i?.catalog;
const o = i?.plugin;
const s = normalizeOptionalString(t.name);
const c = normalizeOptionalString(t.description);
const l = normalizeOptionalString(t.kind);
const u = normalizeOptionalString(t.source);
const d = typeof e.featured === "boolean" ? e.featured : false;
const f = a || r?.catalog || n.hostedFeaturedAuthoritative && d ? {
...r?.catalog,
...a,
...n.hostedFeaturedAuthoritative ? { featured: d } : {}
} : void 0;
if (!f && !o) return e;
return {
...e,
...!normalizeOptionalString(e.name) && s ? { name: s } : {},
...!normalizeOptionalString(e.description) && c ? { description: c } : {},
...!normalizeOptionalString(e.kind) && l ? { kind: l } : {},
...!normalizeOptionalString(e.source) && u ? { source: u } : {},
[MANIFEST_KEY]: {
...r,
...o ? { plugin: {
...r?.plugin,
...o
} } : {},
...f ? { catalog: f } : {}
}
};
}
function prepareCatalogEntry(e) {
const t = resolveOfficialExternalPluginInstall(e);
const n = resolveOfficialExternalPluginInstallSources(e, { resolvedInstall: t });
const r = n.find((e) => e.source === "clawhub")?.spec;
const i = n.find((e) => e.source === "npm")?.spec;
return {
entry: e,
install: t,
selectedSource: n[0],
clawhub: r ? parseClawHubPluginSpec(r) : void 0,
npmPackage: i ? parseRegistryNpmSpec(i)?.name : void 0
};
}
function prepareCatalogEntries(e) {
let t;
return () => t ??= e.map(prepareCatalogEntry);
}
/**
* Overlay local runtime identity and ordering after an exact package/source match.
* Hosted curation wins; bundled Featured state survives only in fallback mode.
*/
function overlayBundledOfficialPluginCatalogMetadata(e, t) {
const n = e.length > 0 ? listOfficialExternalPluginCatalogEntries().map(prepareCatalogEntry) : [];
return e.map((e) => {
const { clawhub: r, npmPackage: i } = prepareCatalogEntry(e);
const a = n.filter((e) => r && e.clawhub?.name === r.name || i && e.npmPackage === i);
const o = a.length === 1 ? a[0]?.entry : void 0;
if (o) return mergeCatalogMetadata(e, o, t);
if (!t.hostedFeaturedAuthoritative) return e;
const s = getOfficialExternalPluginCatalogManifest(e);
if (e.featured !== true && !s?.catalog) return e;
return {
...e,
[MANIFEST_KEY]: {
...s,
catalog: {
...s?.catalog,
featured: e.featured === true
}
}
};
});
}
async function loadOfficialCatalog(e = false) {
const t = getManagedPluginCache();
let n = t.officialCatalog;
if (!n) {
const e = { value: {
entries: overlayBundledOfficialPluginCatalogMetadata(listOfficialExternalPluginCatalogEntries(), { hostedFeaturedAuthoritative: false }),
error: "catalog refresh pending; refresh the plugin list to see completed enrichment"
} };
e.pending = loadConfiguredHostedOfficialExternalPluginCatalogEntries().then((t) => {
const n = t.source === "hosted" || t.source === "hosted-snapshot";
e.value = {
entries: overlayBundledOfficialPluginCatalogMetadata(t.entries, { hostedFeaturedAuthoritative: n }),
hostedFeaturedAuthoritative: n,
..."error" in t ? { error: t.error } : {}
};
e.pending = void 0;
return e.value;
});
n = e;
t.officialCatalog = n;
e.pending.catch(() => {
if (t.officialCatalog === n) t.officialCatalog = void 0;
});
}
return e ? n.value : n.pending ?? n.value;
}
function pluginVersionKey(e, t) {
return JSON.stringify([e, t]);
}
/** Publisher identity is registry-owned, never inferred from package names or manifests. */
async function loadInstalledPluginPublisherHandle(e, t = false) {
const n = getManagedPluginCache().pluginPublisherHandles ??= /* @__PURE__ */ new Map();
const r = JSON.stringify([e.baseUrl, e.packageName]);
let i = n.get(r);
if (!i) {
i = { pending: fetchClawHubPluginDetail({
baseUrl: e.baseUrl,
packageName: e.packageName,
skipAuth: true
}).then((e) => ({ value: normalizeOptionalString(e.owner?.handle ?? e.ownerHandle)?.toLowerCase() }), () => ({ value: void 0 })).then((e) => {
n.set(r, e);
return e;
}) };
n.set(r, i);
}
return "value" in i ? i.value : t ? void 0 : (await i.pending).value;
}
/** Undefined is pending, null is unavailable until this catalog generation is refreshed. */
async function loadInstalledPluginCategories(e) {
const t = getManagedPluginCache().pluginVersionCategories ??= /* @__PURE__ */ new Map();
const n = e.packages.toSorted((e, t) => e.name.localeCompare(t.name) || e.version.localeCompare(t.version));
const r = JSON.stringify([e.baseUrl, n]);
let i = t.get(r);
if (!i) {
i = { pending: fetchClawHubPluginVersionCategories({
baseUrl: e.baseUrl,
skipAuth: true,
packages: n
}).then((e) => new Map(e.map((e) => [pluginVersionKey(e.name, e.version), e.categories])), () => null).then((e) => {
const n = { value: e };
t.set(r, n);
return n;
}) };
t.set(r, i);
}
return "value" in i ? i.value : e.cachedOnly ? void 0 : (await i.pending).value;
}
function normalizeKinds(e) {
const t = (typeof e === "string" ? [e] : e ?? []).map((e) => e.trim()).filter(Boolean);
return t.length > 0 ? [...new Set(t)] : void 0;
}
function normalizeFeaturedAt(e) {
return asSafeIntegerInRange(e, { min: 0 });
}
/** Keep authored purpose separate from capability-derived discovery memberships. */
function projectPluginCatalogCategoryFacts(e, t) {
const n = deriveLegacyPluginCategory(e);
const r = e?.categories;
const i = t && Boolean(e?.contracts?.imageGenerationProviders?.length || e?.contracts?.videoGenerationProviders?.length || e?.contracts?.musicGenerationProviders?.length);
return {
...r?.length ? { categories: [...r] } : {},
...n ? { category: n } : {},
...i ? { capabilityCategories: ["media"] } : {}
};
}
/** Preserve the shipped coarse category projection for older catalog clients. */
function deriveLegacyPluginCategory(e) {
if (!e) return;
if (e.channels.length > 0 || Object.keys(e.channelConfigs ?? {}).length > 0) return "channel";
const t = Object.keys(e.imageGenerationProviderMetadata ?? {}).length > 0 || Object.keys(e.videoGenerationProviderMetadata ?? {}).length > 0 || Object.keys(e.musicGenerationProviderMetadata ?? {}).length > 0 || Object.keys(e.mediaUnderstandingProviderMetadata ?? {}).length > 0;
if (e.providers.length > 0 || e.providerEndpoints?.length || e.modelCatalog || t) return "provider";
const n = normalizeKinds(e.kind);
if (n?.includes("memory")) return "memory";
if (n?.includes("context-engine")) return "context-engine";
if (e.contracts?.tools?.length || Object.keys(e.toolMetadata ?? {}).length > 0 || e.skills.length > 0) return "tool";
}
function firstPluginError(e, t) {
return e.find((e) => e.level === "error" && e.pluginId === t)?.message;
}
function compareCatalogEntries(e, t) {
const n = Number(Boolean(t.featured)) - Number(Boolean(e.featured));
if (n !== 0) return n;
if (e.featured && t.featured && e.featuredAt !== t.featuredAt) {
if (e.featuredAt === void 0) return 1;
if (t.featuredAt === void 0) return -1;
return t.featuredAt - e.featuredAt;
}
const r = (e.order ?? Number.MAX_SAFE_INTEGER) - (t.order ?? Number.MAX_SAFE_INTEGER);
return r !== 0 ? r : e.name.localeCompare(t.name);
}
function resolveInstalledOfficialCatalogEntry(e) {
if (!e.packageName) return;
const t = e.entries().filter(({ clawhub: t, npmPackage: n }) => (e.source === "clawhub" ? t?.name : n) === e.packageName);
return t.length === 1 ? t[0] : void 0;
}
function resolveInstalledPluginPresentation(e) {
const { record: t, manifest: n, officialEntry: r, hostedListingAuthoritative: i } = e;
const a = (n?.name !== t.packageName ? n?.name : void 0) ?? n?.channelCatalogMeta?.label ?? t.pluginId;
const o = n?.description ?? n?.channelCatalogMeta?.blurb ?? n?.packageDescription;
const s = (i ? normalizeOptionalString(r?.title) : void 0) ?? a;
const c = (i ? normalizeOptionalString(r?.description) : void 0) ?? o;
const l = t.packageVersion ?? n?.version;
return {
name: s,
...c ? { description: c } : {},
...l ? { version: l } : {}
};
}
function resolveInstalledHostedOfficialEntry(e) {
const t = e.installOwner ?? e.record.pluginId;
const n = e.installRecord ? resolveTrustedSourceLinkedOfficialClawHubInstall({
pluginId: t,
record: e.installRecord
})?.clawhubSpec : void 0;
const r = e.installRecord ? resolveTrustedSourceLinkedOfficialNpmInstall({
pluginId: t,
record: e.installRecord
})?.npmSpec : void 0;
const i = n ? parseClawHubPluginSpec(n)?.name : void 0;
const a = e.installRecord ? resolveTrustedOfficialClawHubPackageName(e.installRecord) : void 0;
const o = r ? parseRegistryNpmSpec(r)?.name : void 0;
const s = e.record.origin === "bundled" ? resolveInstalledOfficialCatalogEntry({
entries: e.bundledOfficialEntries,
packageName: e.record.packageName,
source: "npm"
}) : void 0;
const c = i ? {
source: "clawhub",
packageName: i
} : o ? {
source: "npm",
packageName: o
} : a && (!e.record.packageName || e.record.packageName === a) ? {
source: "clawhub",
packageName: a
} : s && e.record.packageName ? {
source: "npm",
packageName: e.record.packageName
} : void 0;
const l = Boolean(c && (!e.record.packageName || e.record.packageName === c.packageName));
const u = s ?? resolveInstalledOfficialCatalogEntry({
entries: e.bundledOfficialEntries,
packageName: l ? c?.packageName : void 0,
source: c?.source ?? "clawhub"
});
const d = c?.source === "npm" ? u?.clawhub?.name : c?.packageName;
return {
entry: resolveInstalledOfficialCatalogEntry({
entries: e.officialEntries,
packageName: l ? d : void 0,
source: "clawhub"
})?.entry,
clawhubPackage: l ? d : void 0
};
}
function resolveOfficialEntryById(e, t) {
return e.find((e) => resolveOfficialExternalPluginId(e) === t);
}
//#endregion
export { resolvePluginActivityIconSource as _, loadInstalledPluginPublisherHandle as a, withManagedPluginCache as b, normalizeKinds as c, prepareCatalogEntry as d, projectPluginCatalogCategoryFacts as f, resolveOfficialEntryById as g, resolveInstalledPluginPresentation as h, loadInstalledPluginCategories as i, pluginVersionKey as l, resolveInstalledPluginClawHubIconSource as m, compareCatalogEntries as n, loadOfficialCatalog as o, resolveInstalledHostedOfficialEntry as p, firstPluginError as r, normalizeFeaturedAt as s, clearManagedPluginCatalogCache as t, prepareCatalogEntries as u, resolvePluginIconSource as v, resolvePluginIconSources as y };