2026-01-18 15:56:24 -05:00
|
|
|
import { resolveAgentWorkspaceDir, resolveDefaultAgentId } from "../agents/agent-scope.js";
|
|
|
|
|
import { loadConfig } from "../config/config.js";
|
|
|
|
|
import { createSubsystemLogger } from "../logging.js";
|
2026-03-16 13:30:18 +00:00
|
|
|
import {
|
|
|
|
|
resolveChannelPluginIds,
|
|
|
|
|
resolveConfiguredChannelPluginIds,
|
|
|
|
|
} from "../plugins/channel-plugin-ids.js";
|
2026-01-30 03:15:10 +01:00
|
|
|
import { loadOpenClawPlugins } from "../plugins/loader.js";
|
2026-02-07 07:57:50 +00:00
|
|
|
import { getActivePluginRegistry } from "../plugins/runtime.js";
|
2026-02-18 01:34:35 +00:00
|
|
|
import type { PluginLogger } from "../plugins/types.js";
|
2026-01-18 15:56:24 -05:00
|
|
|
|
|
|
|
|
const log = createSubsystemLogger("plugins");
|
2026-03-15 20:05:19 -07:00
|
|
|
let pluginRegistryLoaded: "none" | "configured-channels" | "channels" | "all" = "none";
|
2026-01-18 15:56:24 -05:00
|
|
|
|
2026-03-15 20:05:19 -07:00
|
|
|
export type PluginRegistryScope = "configured-channels" | "channels" | "all";
|
|
|
|
|
|
|
|
|
|
function scopeRank(scope: typeof pluginRegistryLoaded): number {
|
|
|
|
|
switch (scope) {
|
|
|
|
|
case "none":
|
|
|
|
|
return 0;
|
|
|
|
|
case "configured-channels":
|
|
|
|
|
return 1;
|
|
|
|
|
case "channels":
|
|
|
|
|
return 2;
|
|
|
|
|
case "all":
|
|
|
|
|
return 3;
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-15 17:37:36 -07:00
|
|
|
|
|
|
|
|
export function ensurePluginRegistryLoaded(options?: { scope?: PluginRegistryScope }): void {
|
|
|
|
|
const scope = options?.scope ?? "all";
|
2026-03-15 20:05:19 -07:00
|
|
|
if (scopeRank(pluginRegistryLoaded) >= scopeRank(scope)) {
|
2026-01-31 16:19:20 +09:00
|
|
|
return;
|
|
|
|
|
}
|
2026-02-07 07:57:50 +00:00
|
|
|
const active = getActivePluginRegistry();
|
|
|
|
|
// Tests (and callers) can pre-seed a registry (e.g. `test/setup.ts`); avoid
|
|
|
|
|
// doing an expensive load when we already have plugins/channels/tools.
|
|
|
|
|
if (
|
2026-03-15 20:05:19 -07:00
|
|
|
pluginRegistryLoaded === "none" &&
|
2026-02-07 07:57:50 +00:00
|
|
|
active &&
|
|
|
|
|
(active.plugins.length > 0 || active.channels.length > 0 || active.tools.length > 0)
|
|
|
|
|
) {
|
2026-03-15 17:37:36 -07:00
|
|
|
pluginRegistryLoaded = "all";
|
2026-02-07 07:57:50 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2026-01-18 15:56:24 -05:00
|
|
|
const config = loadConfig();
|
|
|
|
|
const workspaceDir = resolveAgentWorkspaceDir(config, resolveDefaultAgentId(config));
|
|
|
|
|
const logger: PluginLogger = {
|
|
|
|
|
info: (msg) => log.info(msg),
|
|
|
|
|
warn: (msg) => log.warn(msg),
|
|
|
|
|
error: (msg) => log.error(msg),
|
|
|
|
|
debug: (msg) => log.debug(msg),
|
|
|
|
|
};
|
2026-01-30 03:15:10 +01:00
|
|
|
loadOpenClawPlugins({
|
2026-01-18 15:56:24 -05:00
|
|
|
config,
|
|
|
|
|
workspaceDir,
|
|
|
|
|
logger,
|
2026-03-19 16:04:19 -07:00
|
|
|
throwOnLoadError: true,
|
2026-03-15 20:05:19 -07:00
|
|
|
...(scope === "configured-channels"
|
2026-03-15 17:37:36 -07:00
|
|
|
? {
|
2026-03-15 20:05:19 -07:00
|
|
|
onlyPluginIds: resolveConfiguredChannelPluginIds({
|
2026-03-15 17:37:36 -07:00
|
|
|
config,
|
|
|
|
|
workspaceDir,
|
|
|
|
|
env: process.env,
|
|
|
|
|
}),
|
|
|
|
|
}
|
2026-03-15 20:05:19 -07:00
|
|
|
: scope === "channels"
|
|
|
|
|
? {
|
|
|
|
|
onlyPluginIds: resolveChannelPluginIds({
|
|
|
|
|
config,
|
|
|
|
|
workspaceDir,
|
|
|
|
|
env: process.env,
|
|
|
|
|
}),
|
|
|
|
|
}
|
|
|
|
|
: {}),
|
2026-01-18 15:56:24 -05:00
|
|
|
});
|
2026-03-15 17:37:36 -07:00
|
|
|
pluginRegistryLoaded = scope;
|
2026-01-18 15:56:24 -05:00
|
|
|
}
|