UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

331 lines (330 loc) • 12.8 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { s as isVitestRuntimeEnv } from "./env-HehwidXj.mjs"; import { a as extractErrorCode } from "./error-coercion-hDrHOSGy.mjs"; import { r as isPathInside } from "./path-guards-TIjxtj6H.mjs"; import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs"; import { c as getPluginCacheRoot, l as getPluginCacheSource } from "./plugin-cache-B_eRuqTM.mjs"; import { t as createSubsystemLogger } from "./subsystem-BHJiy01V.mjs"; import { r as resolveOpenClawPackageRootSync } from "./openclaw-root-C-ayo09z.mjs"; import { a as pluginCacheRealpathSync, r as pluginCacheExistsSync } from "./plugin-cache-files-e1aGp2KM.mjs"; import { s as resolveBundledPluginsDir } from "./bundled-dir-BKvdAqkP.mjs"; import { p as resolvePluginMetadataSnapshot } from "./plugin-metadata-snapshot-By9ySblt.mjs"; import { t as loadChannelPluginModule } from "./module-loader-DHnaSokp.mjs"; import { i as resolveBundledPluginGeneratedPath } from "./bundled-plugin-scan-VUxPP72r.mjs"; import { n as unwrapDefaultModuleExport } from "./module-export-DTZ89pa-.mjs"; import { fileURLToPath } from "node:url"; import path from "node:path"; //#region src/plugins/bundled-channel-runtime.ts /** Loads bundled channel plugin runtime entries and setup metadata. */ function toBundledChannelPluginMetadata(e) { if (e.origin !== "bundled" || !e.source) return null; return { dirName: path.basename(e.rootDir), source: { source: e.source, built: e.source }, ...e.setupSource ? { setupSource: { source: e.setupSource, built: e.setupSource } } : {}, manifest: { id: e.id, channels: e.channels }, ...e.packageManifest ? { packageManifest: e.packageManifest } : {}, rootDir: e.rootDir }; } /** Lists bundled channel plugin metadata from default or caller-provided scan roots. */ function listBundledChannelPluginMetadata(e) { const t = e?.rootDir; const n = e?.scanDir ? path.resolve(e.scanDir) : t ? [ "extensions", "dist-runtime/extensions", "dist/extensions" ].map((e) => path.join(t, e)).find((e) => pluginCacheExistsSync(e)) : void 0; if (n ? !pluginCacheExistsSync(n) : t) return []; return resolvePluginMetadataSnapshot({ env: n ? { ...process.env, OPENCLAW_BUNDLED_PLUGINS_DIR: n, ...isVitestRuntimeEnv() ? { OPENCLAW_TEST_TRUST_BUNDLED_PLUGINS_DIR: "1" } : {} } : void 0 }).plugins.flatMap((e) => toBundledChannelPluginMetadata(e) ?? []); } //#endregion //#region src/channels/plugins/bundled-root.ts /** * Bundled channel package-root resolver. * * Computes cache scopes for generated channel metadata across source and packaged layouts. */ const OPENCLAW_PACKAGE_ROOT = resolveOpenClawPackageRootSync({ argv1: process.argv[1], cwd: process.cwd(), moduleUrl: import.meta.url.startsWith("file:") ? import.meta.url : void 0 }) ?? (import.meta.url.startsWith("file:") ? path.resolve(fileURLToPath(new URL("../../..", import.meta.url))) : process.cwd()); function derivePackageRootFromExtensionsDir(e) { const t = path.dirname(e); const n = path.basename(t); if (n === "dist" || n === "dist-runtime") return path.dirname(t); return t; } /** * Resolves the package/cache scope used for bundled channel plugin metadata. */ function resolveBundledChannelRootScope(e = process.env) { const t = resolveBundledPluginsDir(e); if (!t) return { packageRoot: OPENCLAW_PACKAGE_ROOT }; const n = path.resolve(t); return { packageRoot: path.basename(n) === "extensions" ? derivePackageRootFromExtensionsDir(n) : n, pluginsDir: n }; } //#endregion //#region src/channels/plugins/meta-normalization.ts function normalizeChannelMeta(e) { const t = e.meta; const n = e.existing; const r = normalizeOptionalString(t?.label) ?? normalizeOptionalString(n?.label); const i = normalizeOptionalString(t?.selectionLabel) ?? normalizeOptionalString(n?.selectionLabel); const a = normalizeOptionalString(t?.docsPath) ?? normalizeOptionalString(n?.docsPath) ?? `/channels/${e.id}`; const o = normalizeOptionalString(t?.blurb) ?? normalizeOptionalString(n?.blurb) ?? ""; return { ...n, ...t, id: e.id, label: r ?? i ?? e.id, selectionLabel: i ?? r ?? e.id, docsPath: a, blurb: o }; } //#endregion //#region src/channels/plugins/bundled.ts const log = createSubsystemLogger("channels"); function resolveCanonicalPathOrAbsolute(e) { return pluginCacheRealpathSync(e, true) ?? path.resolve(e); } function resolveBundledChannelModuleEntry(e, t) { const n = unwrapDefaultModuleExport(e); if (!n || typeof n !== "object") return null; const r = n; const i = t === "setupEntry"; if (r.kind !== (i ? "bundled-channel-setup-entry" : "bundled-channel-entry")) return null; const a = i ? [] : [ "id", "name", "description" ]; const o = i ? ["loadSetupPlugin"] : ["register", "loadChannelPlugin"]; if (a.some((e) => typeof r[e] !== "string") || o.some((e) => typeof r[e] !== "function")) return null; return r; } function resolveBundledChannelBoundaryRoot(e) { const t = [ e.packageRoot, e.pluginsDir ?? "", e.metadata.dirName, e.modulePath ].join("\0"); const n = getPluginCacheRoot(e.packageRoot).artifacts; const r = n.get(`bundled-channel-boundary:${t}`); if (r) return r.boundaryRoot; const i = resolveCanonicalPathOrAbsolute(e.modulePath); const a = path.resolve(e.packageRoot, "extensions", e.metadata.dirName); const o = [ ...e.pluginsDir ? [path.resolve(e.pluginsDir, e.metadata.dirName)] : [], ...["dist", "dist-runtime"].map((t) => path.resolve(e.packageRoot, t, "extensions", e.metadata.dirName)), a ].map(resolveCanonicalPathOrAbsolute).find((e) => isPathInside(e, i)) ?? resolveCanonicalPathOrAbsolute(a); n.set(`bundled-channel-boundary:${t}`, { modulePath: e.modulePath, boundaryRoot: o }); return o; } function resolveGeneratedBundledChannelModulePath(e) { if (!e.entry) return null; const t = resolveBundledPluginGeneratedPath(e.rootScope.packageRoot, e.entry, e.metadata.dirName, e.rootScope.pluginsDir); if (t) return t; const n = pluginCacheRealpathSync(e.rootScope.packageRoot, true); const r = pluginCacheRealpathSync(e.metadata.rootDir, true); if (!n || !r || !isPathInside(n, r)) return null; const i = e.entry.source; const a = path.isAbsolute(i) ? path.normalize(i) : path.resolve(r, i); const o = pluginCacheRealpathSync(a, true); return o && isPathInside(r, o) ? o : null; } function findMissingModuleCodeInChain(e) { const t = /* @__PURE__ */ new Set(); let n = e; while (n && !t.has(n)) { t.add(n); const e = extractErrorCode(n); if (e === "ERR_MODULE_NOT_FOUND" || e === "MODULE_NOT_FOUND") return e; if (typeof n !== "object") return; n = n.cause; } } function describeBundledChannelLoadError(e, t) { const n = formatErrorMessage(e); if (findMissingModuleCodeInChain(e) !== void 0) return `${n} (run \`openclaw doctor --fix\` to install missing bundled runtime dependencies for channel ${t})`; return n; } function loadGeneratedBundledChannelEntry(e, t, n) { const r = e === "setupEntry"; const i = r ? n.setupSource : n.source; if (r && !i) return; try { const a = resolveGeneratedBundledChannelModulePath({ rootScope: t, metadata: n, entry: i }); if (!a) throw new Error(`missing generated module for bundled channel ${n.manifest.id}`); const o = t.pluginsDir; const s = resolveBundledChannelBoundaryRoot({ packageRoot: t.packageRoot, ...o ? { pluginsDir: o } : {}, metadata: n, modulePath: a }); const c = resolveBundledChannelModuleEntry(loadChannelPluginModule({ modulePath: a, rootDir: s }), e); if (!c) { const e = r ? "setup entry" : "entry"; const t = r ? "bundled-channel-setup-entry" : "bundled-channel-entry"; log.warn(`[channels] bundled channel ${e} ${n.manifest.id} missing ${t} contract; skipping`); } return c ?? void 0; } catch (e) { const t = describeBundledChannelLoadError(e, n.manifest.id); const i = r ? " setup entry" : ""; log.warn(`[channels] failed to load bundled channel${i} ${n.manifest.id}: ${t}`); return; } } function listBundledChannelMetadata(e = resolveBundledChannelRootScope()) { const t = e.pluginsDir; return listBundledChannelPluginMetadata({ rootDir: e.packageRoot, ...t ? { scanDir: t } : {} }).filter((e) => (e.manifest.channels?.length ?? 0) > 0); } function listBundledChannelPluginIdsForRoot(e) { return listBundledChannelMetadata(e).map((e) => e.manifest.id).toSorted((e, t) => e.localeCompare(t)); } function resolveBundledChannelMetadata(e, t) { return listBundledChannelMetadata(t).findLast((t) => t.manifest.id === e || t.manifest.channels?.includes(e)); } function rememberBundledChannelArtifact(e, t, n, r, i = resolveBundledChannelMetadata(n, e)) { if (i) getPluginCacheSource(path.resolve(i.rootDir, i.source.source)).variants.set(`bundled-channel:${t}:${n}`, { exports: { value: r } }); } function getBundledChannelArtifactForRoot(e, t, n) { const r = resolveBundledChannelMetadata(t, n); if (!r) return; const i = getPluginCacheSource(path.resolve(r.rootDir, r.source.source)).variants.get(`bundled-channel:${e}:${t}`)?.exports; if (i) return i.value; try { const i = bundledChannelArtifactLoaders[e]({ id: t, rootScope: n, metadata: r }); rememberBundledChannelArtifact(n, e, t, i, r); return i; } catch (i) { if (e === "entry" || e === "setupEntry") throw i; const a = { entry: "", setupEntry: " setup entry", plugin: "", setupPlugin: " setup", secrets: " secrets", setupSecrets: " setup secrets", accountInspector: " account inspector" }; const o = describeBundledChannelLoadError(i, t); log.warn(`[channels] failed to load bundled channel${a[e]} ${t}: ${o}`); rememberBundledChannelArtifact(n, e, t, void 0, r); return; } } const bundledChannelArtifactLoaders = { entry({ id: e, rootScope: t, metadata: n }) { const r = loadGeneratedBundledChannelEntry("entry", t, n); if (r && r.id !== e) rememberBundledChannelArtifact(t, "entry", r.id, r); return r; }, setupEntry({ id: e, rootScope: t, metadata: n }) { const r = loadGeneratedBundledChannelEntry("setupEntry", t, n); const i = /* @__PURE__ */ new Set([ n.manifest.id, ...n.manifest.channels ?? [], e ]); for (const e of i) rememberBundledChannelArtifact(t, "setupEntry", e, r); return r; }, plugin({ id: e, rootScope: t, metadata: n }) { const r = getBundledChannelArtifactForRoot("entry", e, t); if (!r) return; const i = r.loadChannelPlugin(); return i ? { ...i, meta: normalizeChannelMeta({ id: i.id, meta: i.meta, existing: n.packageManifest?.channel }) } : void 0; }, setupPlugin({ id: e, rootScope: t }) { return getBundledChannelArtifactForRoot("setupEntry", e, t)?.loadSetupPlugin(); }, secrets({ id: e, rootScope: t }) { const n = getBundledChannelArtifactForRoot("entry", e, t); return n ? n.loadChannelSecrets?.() ?? getBundledChannelArtifactForRoot("plugin", e, t)?.secrets : void 0; }, setupSecrets({ id: e, rootScope: t }) { const n = getBundledChannelArtifactForRoot("setupEntry", e, t); return n ? n.loadSetupSecrets?.() ?? getBundledChannelArtifactForRoot("setupPlugin", e, t)?.secrets : void 0; }, accountInspector({ id: e, rootScope: t }) { return getBundledChannelArtifactForRoot("entry", e, t)?.loadChannelAccountInspector?.(); } }; function listBundledChannelPlugins() { const e = resolveBundledChannelRootScope(); return listBundledChannelPluginIdsForRoot(e).flatMap((t) => { const n = getBundledChannelArtifactForRoot("plugin", t, e); return n ? [n] : []; }); } function listBundledChannelSetupPlugins() { const e = resolveBundledChannelRootScope(); return listBundledChannelPluginIdsForRoot(e).flatMap((t) => { const n = getBundledChannelArtifactForRoot("setupPlugin", t, e); return n ? [n] : []; }); } function getBundledChannelAccountInspector(e) { return getBundledChannelArtifactForRoot("accountInspector", e, resolveBundledChannelRootScope()); } function getBundledChannelPlugin(e) { return getBundledChannelArtifactForRoot("plugin", e, resolveBundledChannelRootScope()); } function getBundledChannelSecrets(e) { return getBundledChannelArtifactForRoot("secrets", e, resolveBundledChannelRootScope()); } function getBundledChannelSetupPlugin(e, t = process.env) { return getBundledChannelArtifactForRoot("setupPlugin", e, resolveBundledChannelRootScope(t)); } function getBundledChannelSetupSecrets(e, t = process.env) { return getBundledChannelArtifactForRoot("setupSecrets", e, resolveBundledChannelRootScope(t)); } //#endregion export { getBundledChannelSetupSecrets as a, normalizeChannelMeta as c, getBundledChannelSetupPlugin as i, getBundledChannelPlugin as n, listBundledChannelPlugins as o, getBundledChannelSecrets as r, listBundledChannelSetupPlugins as s, getBundledChannelAccountInspector as t };