Files
openclaw/src/plugins/tools.ts

140 lines
4.3 KiB
TypeScript
Raw Normal View History

2026-02-18 01:29:02 +00:00
import { normalizeToolName } from "../agents/tool-policy.js";
import type { AnyAgentTool } from "../agents/tools/common.js";
import { createSubsystemLogger } from "../logging/subsystem.js";
import { applyTestPluginDefaults, normalizePluginsConfig } from "./config-state.js";
2026-01-30 03:15:10 +01:00
import { loadOpenClawPlugins } from "./loader.js";
import { createPluginLoaderLogger } from "./logger.js";
import type { OpenClawPluginToolContext } from "./types.js";
2026-01-11 12:11:12 +00:00
const log = createSubsystemLogger("plugins");
2026-01-18 04:07:19 +00:00
type PluginToolMeta = {
pluginId: string;
optional: boolean;
};
const pluginToolMeta = new WeakMap<AnyAgentTool, PluginToolMeta>();
export function getPluginToolMeta(tool: AnyAgentTool): PluginToolMeta | undefined {
return pluginToolMeta.get(tool);
}
function normalizeAllowlist(list?: string[]) {
return new Set((list ?? []).map(normalizeToolName).filter(Boolean));
}
function isOptionalToolAllowed(params: {
toolName: string;
pluginId: string;
allowlist: Set<string>;
}): boolean {
if (params.allowlist.size === 0) {
return false;
}
2026-01-18 04:07:19 +00:00
const toolName = normalizeToolName(params.toolName);
if (params.allowlist.has(toolName)) {
return true;
}
2026-01-18 04:07:19 +00:00
const pluginKey = normalizeToolName(params.pluginId);
if (params.allowlist.has(pluginKey)) {
return true;
}
2026-01-18 04:07:19 +00:00
return params.allowlist.has("group:plugins");
}
2026-01-11 12:11:12 +00:00
export function resolvePluginTools(params: {
2026-01-30 03:15:10 +01:00
context: OpenClawPluginToolContext;
2026-01-11 12:11:12 +00:00
existingToolNames?: Set<string>;
2026-01-18 04:07:19 +00:00
toolAllowlist?: string[];
suppressNameConflicts?: boolean;
2026-01-11 12:11:12 +00:00
}): AnyAgentTool[] {
// Fast path: when plugins are effectively disabled, avoid discovery/jiti entirely.
// This matters a lot for unit tests and for tool construction hot paths.
const effectiveConfig = applyTestPluginDefaults(params.context.config ?? {}, process.env);
const normalized = normalizePluginsConfig(effectiveConfig.plugins);
if (!normalized.enabled) {
return [];
}
2026-01-30 03:15:10 +01:00
const registry = loadOpenClawPlugins({
config: effectiveConfig,
2026-01-11 12:11:12 +00:00
workspaceDir: params.context.workspaceDir,
logger: createPluginLoaderLogger(log),
2026-01-11 12:11:12 +00:00
});
const tools: AnyAgentTool[] = [];
const existing = params.existingToolNames ?? new Set<string>();
2026-01-18 04:24:16 +00:00
const existingNormalized = new Set(Array.from(existing, (tool) => normalizeToolName(tool)));
2026-01-18 04:07:19 +00:00
const allowlist = normalizeAllowlist(params.toolAllowlist);
const blockedPlugins = new Set<string>();
2026-01-11 12:11:12 +00:00
for (const entry of registry.tools) {
if (blockedPlugins.has(entry.pluginId)) {
continue;
}
2026-01-18 04:07:19 +00:00
const pluginIdKey = normalizeToolName(entry.pluginId);
if (existingNormalized.has(pluginIdKey)) {
const message = `plugin id conflicts with core tool name (${entry.pluginId})`;
if (!params.suppressNameConflicts) {
log.error(message);
registry.diagnostics.push({
level: "error",
pluginId: entry.pluginId,
source: entry.source,
message,
});
}
2026-01-18 04:07:19 +00:00
blockedPlugins.add(entry.pluginId);
continue;
}
2026-01-11 12:11:12 +00:00
let resolved: AnyAgentTool | AnyAgentTool[] | null | undefined = null;
try {
resolved = entry.factory(params.context);
} catch (err) {
log.error(`plugin tool failed (${entry.pluginId}): ${String(err)}`);
continue;
}
if (!resolved) {
continue;
}
2026-01-18 04:07:19 +00:00
const listRaw = Array.isArray(resolved) ? resolved : [resolved];
const list = entry.optional
? listRaw.filter((tool) =>
isOptionalToolAllowed({
toolName: tool.name,
pluginId: entry.pluginId,
allowlist,
}),
)
: listRaw;
if (list.length === 0) {
continue;
}
2026-01-18 04:07:19 +00:00
const nameSet = new Set<string>();
2026-01-11 12:11:12 +00:00
for (const tool of list) {
2026-01-18 04:07:19 +00:00
if (nameSet.has(tool.name) || existing.has(tool.name)) {
const message = `plugin tool name conflict (${entry.pluginId}): ${tool.name}`;
if (!params.suppressNameConflicts) {
log.error(message);
registry.diagnostics.push({
level: "error",
pluginId: entry.pluginId,
source: entry.source,
message,
});
}
2026-01-11 12:11:12 +00:00
continue;
}
2026-01-18 04:07:19 +00:00
nameSet.add(tool.name);
2026-01-11 12:11:12 +00:00
existing.add(tool.name);
2026-01-18 04:07:19 +00:00
pluginToolMeta.set(tool, {
pluginId: entry.pluginId,
optional: entry.optional,
});
2026-01-11 12:11:12 +00:00
tools.push(tool);
}
}
return tools;
}