UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

137 lines (136 loc) 6.21 kB
import { o as getPluginCacheRoot } from "./plugin-cache-DGWspMEc.js"; import { c as isRecord } from "./record-coerce-DItp3I4t.js"; import { a as pluginCacheRealpathSync, c as readPluginCacheFile, n as parsePluginCacheJson, r as pluginCacheExistsSync } from "./plugin-cache-files-DLPF_Tw2.js"; import { o as getPackageManifestMetadata } from "./manifest-ByRdkf9X.js"; import { a as listBuiltRuntimeEntryCandidates, i as isTypeScriptPackageEntry } from "./package-entry-resolution-DZYcOePc.js"; import path from "node:path"; //#region src/plugins/plugin-runtime-artifact-selection.ts /** Selects built plugin artifacts without importing active runtime state. */ /** Built hosts default only checkout plugins to compiled execution, not installed packages. */ function resolvePluginRuntimeArtifactPreference(preferBuiltPluginArtifacts) { if (preferBuiltPluginArtifacts !== void 0) return preferBuiltPluginArtifacts ? "all" : "source"; return /\.[cm]?js$/.test(new URL(import.meta.url).pathname) ? "bundled" : "source"; } function prefersBuiltPluginArtifacts(preference, origin) { return preference === "all" || preference === "bundled" && origin === "bundled"; } function resolveBundledArtifactRelativePath(rootDir, relativeSource) { const file = readPluginCacheFile({ rootDir, relativePath: "package.json", rejectHardlinks: false }); const parsed = file.ok ? parsePluginCacheJson(file) : void 0; const metadata = parsed?.ok && isRecord(parsed.value) ? getPackageManifestMetadata(parsed.value) : void 0; const entries = [...metadata?.runtimeExtensions?.length ? metadata.runtimeExtensions : metadata?.extensions ?? [], metadata?.runtimeSetupEntry ?? metadata?.setupEntry].filter((entry) => typeof entry === "string"); const sourceStem = relativeSource.replace(/\.[^.]+$/u, ""); const declared = entries.find((entry) => path.normalize(entry).replace(/\.[^.]+$/u, "") === sourceStem); if (declared) return /\.[cm]?js$/.test(declared) ? declared : null; const extensions = new Set(entries.map((entry) => path.extname(entry))); const extension = extensions.size === 1 ? [...extensions][0] : void 0; if (!extension || ![ ".js", ".mjs", ".cjs" ].includes(extension) || entries.some((entry) => path.normalize(entry).startsWith(`dist${path.sep}`))) return null; return relativeSource.replace(/\.[^.]+$/u, extension); } function resolvePackageLocalDistRuntimeArtifact(params) { const relativeSource = path.relative(params.rootDir, params.source); if (!isTypeScriptPackageEntry(relativeSource) || relativeSource === "" || relativeSource.startsWith("..") || path.isAbsolute(relativeSource)) return null; for (const artifactRelativePath of listBuiltRuntimeEntryCandidates(relativeSource)) { if (params.origin === "bundled" && !artifactRelativePath.startsWith("./dist/")) continue; const artifactSource = path.resolve(params.rootDir, artifactRelativePath); if (pluginCacheExistsSync(artifactSource)) return pluginCacheRealpathSync(artifactSource) ?? path.resolve(artifactSource); } return null; } function resolvePreferredBundledRootArtifactFromCanonicalPaths(params) { const { rootDir, source } = params; const sourceExternal = params.packageManifest?.build?.bundledDist === false; const extensionsDir = path.dirname(rootDir); if (path.basename(extensionsDir) !== "extensions") return { source, rootDir }; const packageRoot = path.dirname(extensionsDir); if (path.basename(packageRoot) === "dist" || path.basename(packageRoot) === "dist-runtime") return { source, rootDir }; const relativeSource = path.relative(rootDir, source); if (relativeSource === "" || relativeSource.startsWith("..") || path.isAbsolute(relativeSource)) return { source, rootDir }; for (const artifactRootName of sourceExternal ? ["dist"] : ["dist-runtime", "dist"]) { const artifactRoot = path.join(packageRoot, artifactRootName, "extensions", path.basename(rootDir)); const artifactRelativePath = resolveBundledArtifactRelativePath(artifactRoot, relativeSource); if (!artifactRelativePath) continue; const artifactSource = path.join(artifactRoot, artifactRelativePath); if (pluginCacheExistsSync(artifactSource)) return { source: pluginCacheRealpathSync(artifactSource) ?? path.resolve(artifactSource), rootDir: pluginCacheRealpathSync(artifactRoot) ?? path.resolve(artifactRoot) }; } return { source, rootDir }; } /** Selects the lifecycle-owned root build for one bundled source artifact. */ function resolvePreferredBundledRootArtifact(params) { const artifacts = getPluginCacheRoot(params.rootDir).runtimeArtifacts; const key = JSON.stringify([ "bundled-root", params.source, params.packageManifest?.build?.bundledDist ]); const cached = artifacts.get(key); if (cached) return cached; const resolved = resolvePreferredBundledRootArtifactFromCanonicalPaths({ source: pluginCacheRealpathSync(params.source) ?? path.resolve(params.source), rootDir: pluginCacheRealpathSync(params.rootDir) ?? path.resolve(params.rootDir), packageManifest: params.packageManifest }); artifacts.set(key, resolved); return resolved; } /** Applies source, package-local, and root-build preference without runtime memo state. */ function resolvePreferredBuiltRuntimeArtifact(params) { const { rootDir, source } = params; if (!params.preferBuiltPluginArtifacts || params.sourcePreferred) return { source, rootDir }; if (params.origin !== "bundled") { const artifactSource = resolvePackageLocalDistRuntimeArtifact({ ...params, source, rootDir }); return artifactSource ? { source: artifactSource, rootDir } : { source, rootDir }; } const packageLocalArtifactSource = params.packageManifest?.build?.bundledDist === false ? null : resolvePackageLocalDistRuntimeArtifact({ ...params, source, rootDir }); if (packageLocalArtifactSource) return { source: packageLocalArtifactSource, rootDir }; return resolvePreferredBundledRootArtifactFromCanonicalPaths({ source, rootDir, packageManifest: params.packageManifest }); } //#endregion export { resolvePreferredBundledRootArtifact as i, resolvePluginRuntimeArtifactPreference as n, resolvePreferredBuiltRuntimeArtifact as r, prefersBuiltPluginArtifacts as t };