Files
openclaw/src/plugins/runtime/load-context.ts
2026-04-06 15:26:32 +01:00

84 lines
2.7 KiB
TypeScript

import { resolveAgentWorkspaceDir, resolveDefaultAgentId } from "../../agents/agent-scope.js";
import type { OpenClawConfig } from "../../config/config.js";
import { loadConfig } from "../../config/config.js";
import { applyPluginAutoEnable } from "../../config/plugin-auto-enable.js";
import { createSubsystemLogger } from "../../logging.js";
import type { PluginLoadOptions } from "../loader.js";
import type { PluginLogger } from "../types.js";
const log = createSubsystemLogger("plugins");
export type PluginRuntimeLoadContext = {
rawConfig: OpenClawConfig;
config: OpenClawConfig;
activationSourceConfig: OpenClawConfig;
autoEnabledReasons: Readonly<Record<string, string[]>>;
workspaceDir: string | undefined;
env: NodeJS.ProcessEnv;
logger: PluginLogger;
};
export type PluginRuntimeResolvedLoadValues = Pick<
PluginLoadOptions,
"config" | "activationSourceConfig" | "autoEnabledReasons" | "workspaceDir" | "env" | "logger"
>;
export type PluginRuntimeLoadContextOptions = {
config?: OpenClawConfig;
activationSourceConfig?: OpenClawConfig;
env?: NodeJS.ProcessEnv;
workspaceDir?: string;
logger?: PluginLogger;
};
export function createPluginRuntimeLoaderLogger(): PluginLogger {
return {
info: (message) => log.info(message),
warn: (message) => log.warn(message),
error: (message) => log.error(message),
debug: (message) => log.debug(message),
};
}
export function resolvePluginRuntimeLoadContext(
options?: PluginRuntimeLoadContextOptions,
): PluginRuntimeLoadContext {
const env = options?.env ?? process.env;
const rawConfig = options?.config ?? loadConfig();
const autoEnabled = applyPluginAutoEnable({ config: rawConfig, env });
const config = autoEnabled.config;
const workspaceDir =
options?.workspaceDir ?? resolveAgentWorkspaceDir(config, resolveDefaultAgentId(config));
return {
rawConfig,
config,
activationSourceConfig: options?.activationSourceConfig ?? rawConfig,
autoEnabledReasons: autoEnabled.autoEnabledReasons,
workspaceDir,
env,
logger: options?.logger ?? createPluginRuntimeLoaderLogger(),
};
}
export function buildPluginRuntimeLoadOptions(
context: PluginRuntimeLoadContext,
overrides?: Partial<PluginLoadOptions>,
): PluginLoadOptions {
return buildPluginRuntimeLoadOptionsFromValues(context, overrides);
}
export function buildPluginRuntimeLoadOptionsFromValues(
values: PluginRuntimeResolvedLoadValues,
overrides?: Partial<PluginLoadOptions>,
): PluginLoadOptions {
return {
config: values.config,
activationSourceConfig: values.activationSourceConfig,
autoEnabledReasons: values.autoEnabledReasons,
workspaceDir: values.workspaceDir,
env: values.env,
logger: values.logger,
...overrides,
};
}