UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

161 lines (160 loc) • 6.34 kB
import { u as normalizePluginsConfig } from "./config-state-B1JlHgba.mjs"; import { _ as withBundledPluginEnablementCompat, h as isBundledProviderCompatContract } from "./installed-plugin-index-DYqse_gE.mjs"; import { r as getPluginInstance } from "./plugin-instance-scope-oezVdCr2.mjs"; import { t as MissingPublicSurfaceError } from "./facade-loader-08DBXZOS.mjs"; import { n as loadBundledPluginPublicArtifactModuleSync, r as loadPluginPublicArtifactModuleSync } from "./public-surface-loader-DENdCm5a.mjs"; import { r as isManifestPluginOwnerAllowedByControlPlanePolicy } from "./manifest-contract-eligibility-DoGGge-2.mjs"; import { n as getLoadedRuntimePluginRegistry } from "./active-runtime-registry-Y-wDXpG6.mjs"; import { t as acquirePluginRegistryForInspection } from "./loader-runtime-load-Ba16vW6K.mjs"; import { n as listBundledPluginMetadata } from "./bundled-plugin-metadata-CbO_KdWv.mjs"; import "./loader--j41eir2.mjs"; import { t as runWithLocalStateOwner } from "./local-state-owner-DTl7OHpC.mjs"; import { t as resolveManifestContractRuntimePluginResolution } from "./manifest-contract-runtime-qfMBqzwD.mjs"; //#region src/plugins/migration-provider-runtime.ts const MIGRATION_PROVIDER_ARTIFACT = "migration-provider-api.js"; function resolveMigrationProviderPublicArtifacts(e) { const t = []; for (const n of e.plugins) { const r = n.contracts?.migrationProviders ?? []; if (r.length === 0 || !r.includes(e.providerId)) continue; let i; try { i = n.origin === "bundled" ? loadBundledPluginPublicArtifactModuleSync({ dirName: n.dirName, artifactBasename: MIGRATION_PROVIDER_ARTIFACT }) : loadPluginPublicArtifactModuleSync({ pluginRoot: n.rootDir, artifactBasename: MIGRATION_PROVIDER_ARTIFACT, origin: n.origin }); } catch (e) { if (e instanceof MissingPublicSurfaceError) continue; throw e; } if (typeof i.buildMigrationProvider !== "function") throw new Error(`Plugin "${n.id}" has an invalid ${MIGRATION_PROVIDER_ARTIFACT}: buildMigrationProvider is required.`); const a = i.buildMigrationProvider(); if (a.id !== e.providerId || typeof a.plan !== "function" || typeof a.apply !== "function") throw new Error(`Plugin "${n.id}" returned an invalid migration provider from ${MIGRATION_PROVIDER_ARTIFACT}.`); const o = t.find((e) => e.provider.id === a.id); if (o) throw new Error(`Multiple plugins declare migration provider "${a.id}": ${o.pluginId}, ${n.id}.`); t.push({ pluginId: n.id, provider: a }); } return t; } function resolveMigrationProviderPluginResolution(e) { const t = resolveManifestContractRuntimePluginResolution({ cfg: e.cfg, contract: "migrationProviders", ...e.providerId ? { value: e.providerId } : {} }); const n = new Set(t.pluginIds); const r = new Set(t.bundledCompatPluginIds); const i = t.plugins.flatMap((e) => e.origin === "global" && n.has(e.id) ? [{ id: e.id, origin: "global", rootDir: e.rootDir, contracts: e.contracts }] : []); const a = normalizePluginsConfig(e.cfg?.plugins); for (const t of listBundledPluginMetadata({ includeChannelConfigs: false })) { const o = t.manifest.contracts?.migrationProviders ?? []; if (o.length === 0 || e.providerId && !o.includes(e.providerId) || i.some((e) => e.id === t.manifest.id) || !isManifestPluginOwnerAllowedByControlPlanePolicy({ plugin: { id: t.manifest.id, origin: "bundled", channels: t.manifest.channels }, config: e.cfg, normalizedConfig: a, allowBundledProviderCompat: isBundledProviderCompatContract("migrationProviders") })) continue; n.add(t.manifest.id); r.add(t.manifest.id); i.push({ id: t.manifest.id, origin: "bundled", dirName: t.dirName, contracts: { migrationProviders: o } }); } return { pluginIds: [...n].toSorted((e, t) => e.localeCompare(t)), bundledCompatPluginIds: [...r].toSorted((e, t) => e.localeCompare(t)), publicPlugins: i }; } function mergeMigrationProviders(...e) { const t = /* @__PURE__ */ new Map(); for (const n of e) for (const { pluginId: e, provider: r } of n?.migrationProviders ?? []) if (!t.has(r.id)) { const i = n?.plugins?.find((t) => t.id === e); const a = i && getPluginInstance(i); t.set(r.id, a?.wrap(r) ?? r); } return [...t.values()].toSorted((e, t) => e.id.localeCompare(t.id)); } /** Keep provider callbacks and opaque plans inside the operation that owns their registration. */ async function withPluginMigrationProviders(e, t) { const n = e.onForeignOwner; return await runWithLocalStateOwner({ method: "migrate", params: {}, target: e.providerId ?? "migration providers", onForeignOwner: n ? ({ requestOwner: e }) => n(e) : "refuse", runLocal: async (n) => { const { assertCurrent: r } = n; r(); const i = e.cfg ?? n.config; const consume = async (e) => { r(); return await t(e); }; const a = getLoadedRuntimePluginRegistry(); const o = a?.migrationProviders ?? []; if (e.providerId && o.some(({ provider: t }) => t.id === e.providerId)) return await consume(mergeMigrationProviders(a)); const s = resolveMigrationProviderPluginResolution({ ...e, cfg: i }); if (e.providerId) { const t = resolveMigrationProviderPublicArtifacts({ plugins: s.publicPlugins, providerId: e.providerId }); if (t.length > 0) return await consume(mergeMigrationProviders(a, { migrationProviders: t })); } if (s.pluginIds.length === 0 || getLoadedRuntimePluginRegistry({ requiredPluginIds: s.pluginIds })) return await consume(mergeMigrationProviders(a)); const c = withBundledPluginEnablementCompat({ config: i, pluginIds: s.bundledCompatPluginIds }); const l = await acquirePluginRegistryForInspection({ ...c === void 0 ? {} : { config: c }, onlyPluginIds: s.pluginIds }); let u; try { u = await consume(mergeMigrationProviders(a, l.registry)); } catch (e) { const t = [e]; try { await l.release(); } catch (e) { t.push(e); } if (t.length > 1) throw new AggregateError(t, "Migration failed and its plugin resources could not be disposed", { cause: e }); throw e; } try { await l.release(); } catch (t) { if (!e.onCleanupError) throw t; await e.onCleanupError(t); } return u; } }); } //#endregion export { withPluginMigrationProviders as t };