openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
182 lines (181 loc) • 7.78 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { m as normalizeTrimmedStringList } from "./string-normalization-DLmfYZXI.mjs";
import { t as createSubsystemLogger } from "./subsystem-BHJiy01V.mjs";
import { r as pluginCacheExistsSync } from "./plugin-cache-files-e1aGp2KM.mjs";
import { t as PUBLIC_SURFACE_SOURCE_EXTENSIONS } from "./package-entrypoints-Do32y0v-.mjs";
import { r as loadPluginManifest } from "./manifest-Y5vPZPOf.mjs";
import { n as getPackageManifestMetadata } from "./package-manifest-CC8p56hY.mjs";
import { d as tryReadJsonSync } from "./json-files-Cb1h8pYs.mjs";
import { t as getCachedPluginModuleLoader } from "./plugin-module-loader-cache-DgNX_BTY.mjs";
import { s as resolveLoaderPackageRoot } from "./sdk-alias-ByYgWl8I.mjs";
import { a as resolveBundledPluginScanDir, n as collectBundledPluginRuntimeSidecarArtifacts, o as rewriteBundledPluginEntryToBuiltPath, r as deriveBundledPluginIdHint, t as collectBundledPluginPublicSurfaceArtifacts } from "./bundled-plugin-scan-VUxPP72r.mjs";
import { i as buildChannelConfigSchema, o as buildJsonChannelConfigSchema } from "./config-schema-BxWrycwn.mjs";
import fs from "node:fs";
import { fileURLToPath } from "node:url";
import path from "node:path";
//#region src/plugins/bundled-channel-config-metadata.ts
/** Loads bundled channel config schema metadata from source or public surface modules. */
const log = createSubsystemLogger("plugins");
const CONFIG_SCHEMA_CANDIDATES = [...[
".ts",
".js",
".mts",
".mjs",
".cts",
".cjs"
].map((e) => path.join("src", `config-schema${e}`)), ...PUBLIC_SURFACE_SOURCE_EXTENSIONS.map((e) => `channel-config-api${e}`)];
function isBuiltChannelConfigSchema(e) {
if (!e || typeof e !== "object") return false;
const t = e;
return Boolean(t.schema && typeof t.schema === "object");
}
function isJsonSchemaConfigSurface(e) {
if (!e || typeof e !== "object") return false;
const t = e;
if (typeof t.safeParse === "function" || typeof t.toJSONSchema === "function") return false;
return typeof t.type === "string" || Array.isArray(t.anyOf) || Array.isArray(t.oneOf) || Array.isArray(t.allOf) || Array.isArray(t.enum) || Object.hasOwn(t, "const");
}
function resolveConfigSchemaExport(e) {
for (const [t, n] of Object.entries(e)) if (t.endsWith("ChannelConfigSchema") && isBuiltChannelConfigSchema(n)) return n;
for (const [t, n] of Object.entries(e)) {
if (!t.endsWith("ConfigSchema") || t.endsWith("AccountConfigSchema")) continue;
if (isBuiltChannelConfigSchema(n)) return n;
if (isJsonSchemaConfigSurface(n)) return buildJsonChannelConfigSchema(n);
if (n && typeof n === "object") return buildChannelConfigSchema(n);
}
return Object.values(e).find(isBuiltChannelConfigSchema) ?? null;
}
function resolveChannelConfigSchemaModulePath(e) {
return CONFIG_SCHEMA_CANDIDATES.map((t) => path.join(e, t)).find(pluginCacheExistsSync);
}
function loadChannelConfigSurfaceModuleSync(e) {
try {
return resolveConfigSchemaExport(getCachedPluginModuleLoader({
modulePath: e,
importerUrl: import.meta.url,
preferBuiltDist: true,
loaderFilename: import.meta.url
})(e));
} catch (t) {
log.warn(`Unable to load bundled channel config metadata from ${e}; using manifest metadata`, { error: t });
return null;
}
}
function resolvePackageChannelMeta(e, t) {
const n = e?.channel;
return n?.id?.trim() === t ? n : void 0;
}
function collectBundledChannelConfigsCore(e) {
const t = normalizeTrimmedStringList(e.manifest.channels);
const n = e.manifest.channelConfigs && Object.keys(e.manifest.channelConfigs).length > 0 ? { ...e.manifest.channelConfigs } : {};
if (t.length === 0) return Object.keys(n).length > 0 ? n : void 0;
const r = resolveChannelConfigSchemaModulePath(e.pluginDir);
const i = r ? loadChannelConfigSurfaceModuleSync(r) : null;
for (const r of t) {
const t = n[r];
const a = resolvePackageChannelMeta(e.packageManifest, r);
const o = normalizeTrimmedStringList(a?.preferOver);
const s = {
...i?.uiHints,
...t?.uiHints
};
if (!i?.schema && !t?.schema) continue;
const c = i?.runtime ?? t?.runtime;
const l = normalizeOptionalString(t?.label) ?? normalizeOptionalString(a?.label);
const u = normalizeOptionalString(t?.description) ?? normalizeOptionalString(a?.blurb);
const d = t?.commands ?? a?.commands;
n[r] = {
schema: i?.schema ?? t?.schema ?? {},
...Object.keys(s).length > 0 ? { uiHints: s } : {},
...c ? { runtime: c } : {},
...l ? { label: l } : {},
...u ? { description: u } : {},
...t?.preferOver?.length ? { preferOver: t.preferOver } : o.length > 0 ? { preferOver: o } : {},
...d ? { commands: d } : {}
};
}
return Object.keys(n).length > 0 ? n : void 0;
}
//#endregion
//#region src/plugins/bundled-plugin-metadata.ts
const OPENCLAW_PACKAGE_ROOT = resolveLoaderPackageRoot({
modulePath: fileURLToPath(import.meta.url),
moduleUrl: import.meta.url
}) ?? fileURLToPath(new URL("../..", import.meta.url));
const CURRENT_MODULE_PATH = fileURLToPath(import.meta.url);
const RUNNING_FROM_BUILT_ARTIFACT = CURRENT_MODULE_PATH.includes(`${path.sep}dist${path.sep}`) || CURRENT_MODULE_PATH.includes(`${path.sep}dist-runtime${path.sep}`);
/** Lists bundled plugin metadata from source or built package layouts. */
function listBundledPluginMetadata(e) {
const t = path.resolve(e?.rootDir ?? OPENCLAW_PACKAGE_ROOT);
const n = e?.scanDir ? path.resolve(e.scanDir) : resolveBundledPluginScanDir({
packageRoot: t,
runningFromBuiltArtifact: RUNNING_FROM_BUILT_ARTIFACT
});
const r = e?.includeChannelConfigs ?? !RUNNING_FROM_BUILT_ARTIFACT;
const i = e?.includeSyntheticChannelConfigs ?? r;
if (!n || !fs.existsSync(n)) return [];
const a = [];
for (const e of fs.readdirSync(n, { withFileTypes: true }).filter((e) => e.isDirectory()).map((e) => e.name).toSorted((e, t) => e.localeCompare(t))) {
const t = path.join(n, e);
const o = loadPluginManifest(t, false);
if (!o.ok) continue;
const s = tryReadJsonSync(path.join(t, "package.json")) ?? void 0;
const c = getPackageManifestMetadata(s);
const l = normalizeTrimmedStringList(c?.extensions);
const u = l[0];
if (u === void 0) continue;
const d = rewriteBundledPluginEntryToBuiltPath(u);
if (!d) continue;
const f = normalizeOptionalString(c?.setupEntry);
const p = rewriteBundledPluginEntryToBuiltPath(f);
const m = f && p ? {
source: f,
built: p
} : void 0;
const h = collectBundledPluginPublicSurfaceArtifacts({
pluginDir: t,
sourceEntry: u,
...f ? { setupEntry: f } : {}
});
const g = collectBundledPluginRuntimeSidecarArtifacts(h);
const _ = r && i ? collectBundledChannelConfigsCore({
pluginDir: t,
manifest: o.manifest,
packageManifest: c
}) : o.manifest.channelConfigs;
const v = normalizeOptionalString(s?.name);
const y = normalizeOptionalString(s?.version);
const b = normalizeOptionalString(s?.description);
a.push({
dirName: e,
idHint: deriveBundledPluginIdHint({
entryPath: u,
manifestId: o.manifest.id,
packageName: v,
hasMultipleExtensions: l.length > 1
}),
source: {
source: u,
built: d
},
...m ? { setupSource: m } : {},
...h ? { publicSurfaceArtifacts: h } : {},
...g ? { runtimeSidecarArtifacts: g } : {},
...v ? { packageName: v } : {},
...y ? { packageVersion: y } : {},
...b ? { packageDescription: b } : {},
...c ? { packageManifest: c } : {},
manifest: {
...o.manifest,
..._ ? { channelConfigs: _ } : {}
}
});
}
return a;
}
/** Finds bundled plugin metadata by manifest id. */
function findBundledPluginMetadataById(e, t) {
return listBundledPluginMetadata(t).find((t) => t.manifest.id === e);
}
//#endregion
export { listBundledPluginMetadata as n, findBundledPluginMetadataById as t };