UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

123 lines (122 loc) • 5.12 kB
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { b as uniqueStrings, m as normalizeTrimmedStringList } from "./string-normalization-DLmfYZXI.mjs"; import { r as isPathInside } from "./path-guards-TIjxtj6H.mjs"; import { t as PUBLIC_SURFACE_SOURCE_EXTENSIONS } from "./package-entrypoints-Do32y0v-.mjs"; import fs from "node:fs"; import path from "node:path"; //#region src/plugins/bundled-plugin-scan.ts /** Scans bundled plugin source/build roots and derives public/runtime artifacts from manifests. */ const RUNTIME_SIDECAR_ARTIFACTS = /* @__PURE__ */ new Set([ "helper-api.js", "light-runtime-api.js", "qa-runner-api.js", "runtime-api.js", "runtime-setter-api.js", "thread-bindings-runtime.js" ]); /** Converts a source entry path to its built JavaScript artifact path. */ function rewriteBundledPluginEntryToBuiltPath(e) { if (!e) return; return e.replace(/^\.\//u, "").replace(/\.[^.]+$/u, ".js"); } function isTopLevelPublicSurfaceSource(e) { if (!PUBLIC_SURFACE_SOURCE_EXTENSIONS.includes(path.extname(e))) return false; if (e.startsWith(".") || e.startsWith("test-") || e.includes(".test-")) return false; if (e.endsWith(".d.ts")) return false; if (/^config-api(\.[cm]?[jt]s)$/u.test(e)) return false; return !/(\.test|\.spec)(\.[cm]?[jt]s)$/u.test(e); } /** Derives a stable id hint for bundled plugins with one or more extension entrypoints. */ function deriveBundledPluginIdHint(e) { const t = path.basename(e.entryPath, path.extname(e.entryPath)); if (!e.hasMultipleExtensions) return e.manifestId; const n = normalizeOptionalString(e.packageName); if (!n) return `${e.manifestId}/${t}`; return `${n.includes("/") ? n.split("/").pop() ?? n : n}/${t}`; } /** Lists top-level public surface artifacts that should be copied with bundled plugin runtime. */ function collectBundledPluginPublicSurfaceArtifacts(e) { const t = new Set(normalizeTrimmedStringList([e.sourceEntry, e.setupEntry]).map((e) => path.basename(e))); const n = fs.readdirSync(e.pluginDir, { withFileTypes: true }).filter((e) => e.isFile()).map((e) => e.name).filter(isTopLevelPublicSurfaceSource).filter((e) => !t.has(e)).map((e) => rewriteBundledPluginEntryToBuiltPath(e)).filter((e) => typeof e === "string" && e.length > 0).toSorted((e, t) => e.localeCompare(t)); return n.length > 0 ? n : void 0; } /** Filters public artifacts down to runtime sidecars needed by bundled plugin execution. */ function collectBundledPluginRuntimeSidecarArtifacts(e) { if (!e) return; const t = e.filter((e) => RUNTIME_SIDECAR_ARTIFACTS.has(e)); return t.length > 0 ? t : void 0; } /** Chooses the source or built extension directory appropriate for the current package layout. */ function resolveBundledPluginScanDir(e) { const t = path.join(e.packageRoot, "extensions"); const n = path.join(e.packageRoot, "dist-runtime", "extensions"); const r = path.join(e.packageRoot, "dist", "extensions"); if (e.runningFromBuiltArtifact) return [ r, n, t ].find((e) => fs.existsSync(e)); if (fs.existsSync(t)) return t; return fs.existsSync(r) ? fs.existsSync(n) ? n : r : void 0; } function listBundledPluginEntryDirectories(e) { const t = e.scanDir ? [path.resolve(e.scanDir, e.pluginDirName ?? "")] : []; const n = path.resolve(e.rootDir, "extensions", e.pluginDirName ?? ""); const r = [path.resolve(e.rootDir, "dist", "extensions", e.pluginDirName ?? ""), path.resolve(e.rootDir, "dist-runtime", "extensions", e.pluginDirName ?? "")]; return { roots: uniqueStrings([ ...t, n, ...r ]), baseDirs: uniqueStrings([ ...t.flatMap((e) => [path.resolve(e, "dist"), e]), ...r, path.resolve(n, "dist"), n ]) }; } function listBundledPluginEntrySearchPaths(e, t) { const n = []; for (const r of [e.built, e.source]) { if (typeof r !== "string" || r.length === 0) continue; if (!path.isAbsolute(r)) { n.push(r); continue; } const e = path.normalize(r); for (const r of t) { if (!isPathInside(r, e)) continue; const t = path.relative(r, e); const i = rewriteBundledPluginEntryToBuiltPath(t); if (i) n.push(i); n.push(t); } } return uniqueStrings(n); } /** Resolves a generated runtime path for a bundled plugin entry. */ function resolveBundledPluginGeneratedPath(e, t, n, r) { if (!t) return null; const { roots: i, baseDirs: a } = listBundledPluginEntryDirectories({ rootDir: e, pluginDirName: n, scanDir: r }); const o = listBundledPluginEntrySearchPaths(t, i); for (const e of a) for (const t of o) { const n = resolveBundledPluginEntryCandidate(e, t); if (!n) continue; if (fs.existsSync(n)) return n; } return null; } function resolveBundledPluginEntryCandidate(e, t) { const n = t.replace(/^\.\//u, ""); const r = path.isAbsolute(n) ? path.normalize(n) : path.resolve(e, n); if (!isPathInside(e, r)) return null; return r; } //#endregion export { resolveBundledPluginScanDir as a, resolveBundledPluginGeneratedPath as i, collectBundledPluginRuntimeSidecarArtifacts as n, rewriteBundledPluginEntryToBuiltPath as o, deriveBundledPluginIdHint as r, collectBundledPluginPublicSurfaceArtifacts as t };