openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
120 lines (119 loc) • 4.91 kB
JavaScript
import { a as getPluginCache } from "./plugin-cache-B_eRuqTM.mjs";
import { i as formatInvalidConfigDetails, r as createInvalidConfigError } from "./io.invalid-config-DK-BYZ4Y.mjs";
import { d as loadPinnedRuntimeConfigAsync } from "./runtime-snapshot-WbwbY5b7.mjs";
import { s as readConfigFileSnapshot } from "./io.runtime-DsJ2dLCQ.mjs";
import "./config-CMA-J084.mjs";
import { a as pluginRuntimeLoaderLogger } from "./load-context-CwTU1iXB.mjs";
import { a as registerLazyCommandGroup, n as getCommandGroupNames, o as removeCommandGroupNames, t as findCommandGroupEntry } from "./register-command-groups-r1Sqeebh.mjs";
import { n as setCommandJsonMode } from "./json-mode-DF5JGxtm.mjs";
import { r as loadPluginCliRegistrationEntriesWithDefaults, t as createPluginCliLoadSession } from "./cli-registry-loader-BHWGJ72E.mjs";
//#region src/plugins/cli.ts
function canRegisterPluginCliLazily(e) {
if (e.placeholders.length === 0) return false;
const t = new Set(e.placeholders.map((e) => e.name));
return getCommandGroupNames(e).every((e) => t.has(e));
}
function findCommandByPath(e, t) {
let n = e;
for (const e of t) {
const t = n.commands.find((t) => t.name() === e || t.aliases().includes(e));
if (!t) return null;
n = t;
}
return n;
}
function applyMachineOutputMode(e, t) {
if (!t.machineOutput) return;
const n = e.commands.find((e) => e.name() === t.name);
if (!n) return;
setCommandJsonMode(n, "output", ({ argv: e, stdoutIsTTY: n }) => t.machineOutput?.({
argv: e,
stdoutIsTTY: n
}) === true);
}
async function registerPluginCliCommandsFromValidatedConfig(e, t, n, r) {
const i = r?.session ?? createPluginCliLoadSession(getPluginCache());
try {
const a = await i.readConfig(() => readConfigFileSnapshot({
observe: false,
skipPluginValidation: r?.skipPluginValidation
}));
if (!a.valid) throw createInvalidConfigError(a.path, formatInvalidConfigDetails(a.issues));
const o = await loadPinnedRuntimeConfigAsync(async () => ({ config: a.runtimeConfig }));
const s = r?.mode ?? "eager";
const c = r?.primary ?? void 0;
const l = (await loadPluginCliRegistrationEntriesWithDefaults({
cfg: o,
env: t,
loaderOptions: n,
primaryCommand: c,
session: i
}, s === "metadata" ? "metadata" : "runtime")).map((e) => {
if (s === "eager" || s === "lazy" && c && (e.parentPath[0] === c || e.names.includes(c) || e.placeholders.some((e) => e.name === c))) return e;
return Object.assign({}, e, { register: async (r) => {
const a = createPluginCliLoadSession(getPluginCache(), { resources: i.resources });
try {
const i = (await loadPluginCliRegistrationEntriesWithDefaults({
cfg: o,
env: t,
loaderOptions: n,
primaryCommand: s === "metadata" ? c : void 0,
session: a
})).find((t) => t.pluginId === e.pluginId && t.parentPath.join("\0") === e.parentPath.join("\0") && t.names.join("\0") === e.names.join("\0"));
if (!i) throw new Error(`Plugin CLI registration is no longer available (${e.pluginId}).`);
await i.register(r);
} finally {
a.close();
}
} });
});
const u = s === "metadata" ? "lazy" : s;
const d = s === "metadata" ? void 0 : c;
const f = new Set(e.commands.flatMap((e) => [e.name(), ...e.aliases()]));
for (const t of l) {
const n = t.parentPath ?? [];
const r = findCommandByPath(e, n);
if (!r) {
pluginRuntimeLoaderLogger.debug?.(`plugin CLI register skipped (${t.pluginId}): parent command missing (${n.join(" ")})`);
continue;
}
const i = n.length === 0 ? f : new Set(r.commands.flatMap((e) => [e.name(), ...e.aliases()]));
const registerEntry = async () => {
await t.register(r);
for (const e of t.placeholders) applyMachineOutputMode(r, e);
for (const e of getCommandGroupNames(t)) i.add(e);
};
if (d && (n[0] === d || findCommandGroupEntry([t], d))) {
removeCommandGroupNames(r, t);
await registerEntry();
continue;
}
const a = getCommandGroupNames(t).filter((e) => i.has(e));
if (a.length > 0) {
pluginRuntimeLoaderLogger.debug?.(`plugin CLI register skipped (${t.pluginId}): command already registered (${a.join(", ")})`);
continue;
}
try {
if (u === "lazy" && canRegisterPluginCliLazily(t)) {
for (const e of t.placeholders) {
registerLazyCommandGroup(r, {
...t,
register: registerEntry
}, e);
applyMachineOutputMode(r, e);
}
continue;
}
if (u === "lazy" && t.placeholders.length > 0) pluginRuntimeLoaderLogger.debug?.(`plugin CLI lazy register fallback to eager (${t.pluginId}): descriptors do not cover all command roots`);
await registerEntry();
} catch (e) {
pluginRuntimeLoaderLogger.warn(`plugin CLI register failed (${t.pluginId}): ${String(e)}`);
}
}
return o;
} finally {
if (!r?.session) i.close();
}
}
//#endregion
export { registerPluginCliCommandsFromValidatedConfig };