mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-16 03:31:10 +00:00
refactor: share plugin runtime load context
This commit is contained in:
83
src/plugins/runtime/load-context.ts
Normal file
83
src/plugins/runtime/load-context.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
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,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user