UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

279 lines (278 loc) • 10.5 kB
import { o as normalizeLowercaseStringOrEmpty } from "./string-coerce-CCIiB4H7.mjs"; import { o as openRootFileSync } from "./boundary-file-read-BfaFLDqt.mjs"; import { a as pluginInstanceInvocation } from "./plugin-instance-invocation-Bq_toGDw.mjs"; import { c as getPluginCacheRoot, l as getPluginCacheSource } from "./plugin-cache-B_eRuqTM.mjs"; import { c as tryNativeRequireJavaScriptModule } from "./native-module-require-CqM0GFP3.mjs"; import { c as toSafeImportPath, t as getCachedPluginModuleLoader } from "./plugin-module-loader-cache-DgNX_BTY.mjs"; import { s as resolveLoaderPackageRoot, t as buildPluginLoaderAliasMap } from "./sdk-alias-ByYgWl8I.mjs"; import { n as formatPluginLoadProfileLine, r as shouldProfilePluginLoader, t as createProfiler } from "./plugin-load-profile-DzyLBTel.mjs"; import { l as emptyChannelConfigSchema } from "./config-schema-BxWrycwn.mjs"; import { t as createCachedLazyValueGetter } from "./lazy-value-BV7AkcP3.mjs"; import fs from "node:fs"; import { fileURLToPath } from "node:url"; import path from "node:path"; //#region src/plugin-sdk/channel-entry-contract.ts const disableBundledEntrySourceFallbackEnv = "OPENCLAW_DISABLE_BUNDLED_ENTRY_SOURCE_FALLBACK"; function isBundledEntrySourceFallbackDisabled(e) { return e !== void 0 && !/^(?:0|false)$/iu.test(e.trim()); } function resolveSpecifierCandidates(e) { const t = normalizeLowercaseStringOrEmpty(path.extname(e)); if (t === ".js") return [e, e.slice(0, -3) + ".ts"]; if (t === ".mjs") return [e, e.slice(0, -4) + ".mts"]; if (t === ".cjs") return [e, e.slice(0, -4) + ".cts"]; return [e]; } function resolveEntryBoundaryRoot(e) { return path.dirname(fileURLToPath(e)); } function resolveBundledEntryBoundaryInfo(e) { const t = `${process.argv[1] ?? ""}\0${e}`; const n = getPluginCacheRoot(resolveEntryBoundaryRoot(e)).entryBoundaries; const r = n.get(t); if (r) return r; const i = fileURLToPath(e); const a = path.dirname(i); const o = { importerPath: i, importerDir: a, boundaryRoot: path.dirname(i), packageRoot: resolveLoaderPackageRoot({ modulePath: i, moduleUrl: e, cwd: a, argv1: process.argv[1] }) ?? null }; n.set(t, o); return o; } function addBundledEntryCandidates(e, t, n) { for (const r of resolveSpecifierCandidates(t)) { if (e.some((e) => e.path === r && e.boundaryRoot === n)) continue; e.push({ path: r, boundaryRoot: n }); } } function resolveBundledEntryModuleCandidates(e, t) { const { importerPath: n, importerDir: r, boundaryRoot: i, packageRoot: a } = resolveBundledEntryBoundaryInfo(e); const o = path.resolve(r, t); const s = [{ path: o, boundaryRoot: i }]; addBundledEntryCandidates(s, o, i); const c = t.replace(/^\.\/src\//u, "./"); if (c !== t) addBundledEntryCandidates(s, path.resolve(r, c), i); if (!a) return s; const l = path.join(a, "dist", "extensions") + path.sep; if (!n.startsWith(l)) return s; if (isBundledEntrySourceFallbackDisabled(process.env[disableBundledEntrySourceFallbackEnv])) return s; const u = path.basename(r); const d = path.join(a, "extensions", u); if (d === i) return s; addBundledEntryCandidates(s, path.resolve(d, t), d); if (c !== t) addBundledEntryCandidates(s, path.resolve(d, c), d); return s; } function formatBundledEntryUnknownError(e) { if (typeof e === "string") return e; if (e === void 0) return "boundary validation failed"; try { return JSON.stringify(e); } catch { return "non-serializable error"; } } function formatBundledEntryModuleOpenFailure(e) { const t = fileURLToPath(e.importMetaUrl); const n = e.failure.error instanceof Error ? e.failure.error.message : formatBundledEntryUnknownError(e.failure.error); return [ `bundled plugin entry "${e.specifier}" failed to open`, `from "${t}"`, `(resolved "${e.resolvedPath}", plugin root "${e.boundaryRoot}",`, `reason "${e.failure.reason}"): ${n}` ].join(" "); } function createBundledEntryModulePathCacheKey(e, t) { return `${isBundledEntrySourceFallbackDisabled(process.env[disableBundledEntrySourceFallbackEnv]) ? "1" : "0"}\0${e}\0${t}`; } function resolveBundledEntryModulePath(e, t) { const n = createBundledEntryModulePathCacheKey(e, t); const r = getPluginCacheRoot(resolveEntryBoundaryRoot(e)).entryPaths; const i = r.get(n); if (i) { if ("error" in i) throw i.error; return i.path; } const a = resolveBundledEntryModuleCandidates(e, t); let o = null; let s = 0; let c = a[0]; do { const e = openRootFileSync({ absolutePath: c.path, rootPath: c.boundaryRoot, boundaryLabel: "plugin root", rejectHardlinks: false, skipLexicalRootCheck: true }); if (e.ok) { fs.closeSync(e.fd); r.set(n, { path: e.path }); return e.path; } o ??= { candidate: c, failure: e }; c = a[++s]; } while (c); const l = o; const u = new Error(formatBundledEntryModuleOpenFailure({ importMetaUrl: e, specifier: t, resolvedPath: l.candidate.path, boundaryRoot: l.candidate.boundaryRoot, failure: l.failure })); r.set(n, { error: u }); throw u; } function canTryNodeRequireBuiltModule(e) { return (e.includes(`${path.sep}dist${path.sep}`) || e.includes(`${path.sep}dist-runtime${path.sep}`)) && [ ".js", ".mjs", ".cjs" ].includes(normalizeLowercaseStringOrEmpty(path.extname(e))); } function loadBundledEntryModuleSync(e, t, n = {}) { const r = resolveBundledEntryModulePath(e, t); const i = pluginInstanceInvocation.getStore()?.instance; const a = i?.hasModuleSource(r); if (a === false) throw new Error(`Bundled companion is outside the plugin's captured module graph: ${r}`); if (i && a) return i.loadModule(r); const o = getPluginCacheSource(r); const s = o.variants.get("bundled-entry")?.exports; if (s) return s.value; let c; const l = shouldProfilePluginLoader(); const u = l ? performance.now() : 0; let d = 0; const f = canTryNodeRequireBuiltModule(r) ? tryNativeRequireJavaScriptModule(r, { aliasMap: buildPluginLoaderAliasMap(r, process.argv[1], import.meta.url), fallbackOnMissingDependency: true }) : void 0; if (f?.ok) c = f.moduleExport; else { const e = getCachedPluginModuleLoader({ modulePath: r, importerUrl: import.meta.url, loaderFilename: import.meta.url, ...n.createLoaderForTest ? { createLoader: n.createLoaderForTest } : {}, tryNative: false }); d = l ? performance.now() : 0; c = e(toSafeImportPath(r)); } if (l) { const e = performance.now(); console.error(formatPluginLoadProfileLine({ phase: "bundled-entry-module-load", pluginId: "(bundled-entry)", source: r, elapsedMs: e - u, extras: [["sourceLoaderCreateMs", d ? d - u : 0], ["sourceLoaderCallMs", d ? e - d : 0]] })); } o.variants.set("bundled-entry", { exports: { value: c } }); return c; } /** Loads one export from a bundled channel sidecar module through the guarded entry boundary. */ function loadBundledEntryExportSync(e, t, n) { const r = loadBundledEntryModuleSync(e, t.specifier, n); const i = r && typeof r === "object" && "default" in r ? r.default : r; if (!t.exportName) return i; const a = i ?? r; if (!a || !(t.exportName in a)) throw new Error(`missing export "${t.exportName}" from bundled entry module ${t.specifier}`); return a[t.exportName]; } function createOptionalBundledEntryLoader(e, t, n) { return t ? (r) => n(e, t, r) : void 0; } function createBundledEntryRuntimeSetter(e, t) { return t ? (n) => { loadBundledEntryExportSync(e, t)(n); } : void 0; } /** Defines the full bundled channel entry contract used by core plugin registration. */ function defineBundledChannelEntry({ id: e, name: t, description: n, importMetaUrl: r, plugin: i, outbound: a, secrets: o, configSchema: s, runtime: c, accountInspect: l, features: u, registerCliMetadata: d, registerFull: f, registerCapabilities: p }) { const m = createCachedLazyValueGetter(s ?? emptyChannelConfigSchema); const loadChannelPlugin = (e) => loadBundledEntryExportSync(r, i, e); const h = createOptionalBundledEntryLoader(r, a, loadBundledEntryExportSync); const g = createOptionalBundledEntryLoader(r, o, loadBundledEntryExportSync); const _ = createOptionalBundledEntryLoader(r, l, loadBundledEntryExportSync); const v = createBundledEntryRuntimeSetter(r, c); return { kind: "bundled-channel-entry", id: e, name: t, description: n, get configSchema() { return m(); }, ...u || l ? { features: { ...u, ...l ? { accountInspect: true } : {} } } : {}, register(t) { if (t.registrationMode === "cli-metadata") { d?.(t); return; } const n = createProfiler({ pluginId: e, source: r }); if (t.registrationMode === "tool-discovery") { n("bundled-register:registerFull", () => f?.(t)); n("bundled-register:registerCapabilities", () => p?.(t)); return; } const i = n("bundled-register:loadChannelPlugin", loadChannelPlugin); n("bundled-register:registerChannel", () => t.registerChannel({ plugin: i })); n("bundled-register:setChannelRuntime", () => v?.(t.runtime)); const a = t.registrationMode; if (a !== "discovery" && a !== "full") return; n("bundled-register:registerCliMetadata", () => d?.(t)); if (a === "full") n("bundled-register:registerFull", () => f?.(t)); n("bundled-register:registerCapabilities", () => p?.(t)); }, loadChannelPlugin, ...h ? { loadChannelOutbound: h } : {}, ...g ? { loadChannelSecrets: g } : {}, ..._ ? { loadChannelAccountInspector: _ } : {}, ...v ? { setChannelRuntime: v } : {} }; } /** Defines the setup-only bundled channel entry contract for onboarding and migration surfaces. */ function defineBundledChannelSetupEntry({ importMetaUrl: e, plugin: t, secrets: n, runtime: r, legacyStateMigrations: i, legacySessionSurface: a, registerSetupRuntime: o, features: s }) { const c = createBundledEntryRuntimeSetter(e, r); const l = createOptionalBundledEntryLoader(e, n, loadBundledEntryExportSync); const u = createOptionalBundledEntryLoader(e, i, loadBundledEntryExportSync); const d = createOptionalBundledEntryLoader(e, a, loadBundledEntryExportSync); return { kind: "bundled-channel-setup-entry", loadSetupPlugin: (n) => loadBundledEntryExportSync(e, t, n), ...l ? { loadSetupSecrets: l } : {}, ...u ? { loadLegacyStateMigrationDetector: u } : {}, ...d ? { loadLegacySessionSurface: d } : {}, ...c ? { setChannelRuntime: c } : {}, ...o ? { registerSetupRuntime: o } : {}, ...s ? { features: s } : {} }; } //#endregion export { defineBundledChannelSetupEntry as n, loadBundledEntryExportSync as r, defineBundledChannelEntry as t };