mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-25 00:53:04 +00:00
132 lines
4.2 KiB
TypeScript
132 lines
4.2 KiB
TypeScript
import { resolveAgentWorkspaceDir, resolveDefaultAgentId } from "../../agents/agent-scope.js";
|
|
import { getRuntimeConfig } from "../../config/config.js";
|
|
import { applyPluginAutoEnable } from "../../config/plugin-auto-enable.js";
|
|
import type { OpenClawConfig } from "../../config/types.openclaw.js";
|
|
import { createSubsystemLogger } from "../../logging.js";
|
|
import { resolvePluginActivationSourceConfig } from "../activation-source-config.js";
|
|
import {
|
|
getCurrentPluginMetadataSnapshot,
|
|
setCurrentPluginMetadataSnapshot,
|
|
} from "../current-plugin-metadata-snapshot.js";
|
|
import type { PluginLoadOptions } from "../loader.js";
|
|
import type { PluginManifestRegistry } from "../manifest-registry.js";
|
|
import { loadPluginMetadataSnapshot } from "../plugin-metadata-snapshot.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;
|
|
manifestRegistry?: PluginManifestRegistry;
|
|
};
|
|
|
|
export type PluginRuntimeResolvedLoadValues = Pick<
|
|
PluginLoadOptions,
|
|
| "config"
|
|
| "activationSourceConfig"
|
|
| "autoEnabledReasons"
|
|
| "workspaceDir"
|
|
| "env"
|
|
| "logger"
|
|
| "manifestRegistry"
|
|
>;
|
|
|
|
export type PluginRuntimeLoadContextOptions = {
|
|
config?: OpenClawConfig;
|
|
activationSourceConfig?: OpenClawConfig;
|
|
env?: NodeJS.ProcessEnv;
|
|
workspaceDir?: string;
|
|
logger?: PluginLogger;
|
|
manifestRegistry?: PluginManifestRegistry;
|
|
};
|
|
|
|
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 ?? getRuntimeConfig();
|
|
const rawWorkspaceDir =
|
|
options?.workspaceDir ?? resolveAgentWorkspaceDir(rawConfig, resolveDefaultAgentId(rawConfig));
|
|
const metadataSnapshot = options?.manifestRegistry
|
|
? undefined
|
|
: (getCurrentPluginMetadataSnapshot({
|
|
config: rawConfig,
|
|
env,
|
|
workspaceDir: rawWorkspaceDir,
|
|
}) ??
|
|
loadPluginMetadataSnapshot({
|
|
config: rawConfig,
|
|
env,
|
|
workspaceDir: rawWorkspaceDir,
|
|
}));
|
|
const manifestRegistry = options?.manifestRegistry ?? metadataSnapshot?.manifestRegistry;
|
|
const activationSourceConfig = resolvePluginActivationSourceConfig({
|
|
config: rawConfig,
|
|
activationSourceConfig: options?.activationSourceConfig,
|
|
});
|
|
const autoEnabled = applyPluginAutoEnable({
|
|
config: rawConfig,
|
|
env,
|
|
manifestRegistry,
|
|
});
|
|
const config = autoEnabled.config;
|
|
const workspaceDir =
|
|
options?.workspaceDir ?? resolveAgentWorkspaceDir(config, resolveDefaultAgentId(config));
|
|
if (metadataSnapshot) {
|
|
setCurrentPluginMetadataSnapshot(metadataSnapshot, {
|
|
config: rawConfig,
|
|
compatibleConfigs: [config, activationSourceConfig],
|
|
env,
|
|
workspaceDir,
|
|
});
|
|
}
|
|
return {
|
|
rawConfig,
|
|
config,
|
|
activationSourceConfig,
|
|
autoEnabledReasons: autoEnabled.autoEnabledReasons,
|
|
workspaceDir,
|
|
env,
|
|
logger: options?.logger ?? createPluginRuntimeLoaderLogger(),
|
|
...(manifestRegistry ? { manifestRegistry } : {}),
|
|
};
|
|
}
|
|
|
|
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,
|
|
...(values.manifestRegistry ? { manifestRegistry: values.manifestRegistry } : {}),
|
|
...overrides,
|
|
};
|
|
}
|