mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-03 03:03:24 -04:00
feat: add optional plugin tools
This commit is contained in:
@@ -1,13 +1,43 @@
|
||||
import type { AnyAgentTool } from "../agents/tools/common.js";
|
||||
import { normalizeToolName } from "../agents/tool-policy.js";
|
||||
import { createSubsystemLogger } from "../logging.js";
|
||||
import { loadClawdbotPlugins } from "./loader.js";
|
||||
import type { ClawdbotPluginToolContext } from "./types.js";
|
||||
|
||||
const log = createSubsystemLogger("plugins");
|
||||
|
||||
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;
|
||||
const toolName = normalizeToolName(params.toolName);
|
||||
if (params.allowlist.has(toolName)) return true;
|
||||
const pluginKey = normalizeToolName(params.pluginId);
|
||||
if (params.allowlist.has(pluginKey)) return true;
|
||||
return params.allowlist.has("group:plugins");
|
||||
}
|
||||
|
||||
export function resolvePluginTools(params: {
|
||||
context: ClawdbotPluginToolContext;
|
||||
existingToolNames?: Set<string>;
|
||||
toolAllowlist?: string[];
|
||||
}): AnyAgentTool[] {
|
||||
const registry = loadClawdbotPlugins({
|
||||
config: params.context.config,
|
||||
@@ -22,8 +52,27 @@ export function resolvePluginTools(params: {
|
||||
|
||||
const tools: AnyAgentTool[] = [];
|
||||
const existing = params.existingToolNames ?? new Set<string>();
|
||||
const existingNormalized = new Set(
|
||||
Array.from(existing, (tool) => normalizeToolName(tool)),
|
||||
);
|
||||
const allowlist = normalizeAllowlist(params.toolAllowlist);
|
||||
const blockedPlugins = new Set<string>();
|
||||
|
||||
for (const entry of registry.tools) {
|
||||
if (blockedPlugins.has(entry.pluginId)) continue;
|
||||
const pluginIdKey = normalizeToolName(entry.pluginId);
|
||||
if (existingNormalized.has(pluginIdKey)) {
|
||||
const message = `plugin id conflicts with core tool name (${entry.pluginId})`;
|
||||
log.error(message);
|
||||
registry.diagnostics.push({
|
||||
level: "error",
|
||||
pluginId: entry.pluginId,
|
||||
source: entry.source,
|
||||
message,
|
||||
});
|
||||
blockedPlugins.add(entry.pluginId);
|
||||
continue;
|
||||
}
|
||||
let resolved: AnyAgentTool | AnyAgentTool[] | null | undefined = null;
|
||||
try {
|
||||
resolved = entry.factory(params.context);
|
||||
@@ -32,13 +81,36 @@ export function resolvePluginTools(params: {
|
||||
continue;
|
||||
}
|
||||
if (!resolved) continue;
|
||||
const list = Array.isArray(resolved) ? resolved : [resolved];
|
||||
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;
|
||||
const nameSet = new Set<string>();
|
||||
for (const tool of list) {
|
||||
if (existing.has(tool.name)) {
|
||||
log.warn(`plugin tool name conflict (${entry.pluginId}): ${tool.name}`);
|
||||
if (nameSet.has(tool.name) || existing.has(tool.name)) {
|
||||
const message = `plugin tool name conflict (${entry.pluginId}): ${tool.name}`;
|
||||
log.error(message);
|
||||
registry.diagnostics.push({
|
||||
level: "error",
|
||||
pluginId: entry.pluginId,
|
||||
source: entry.source,
|
||||
message,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
nameSet.add(tool.name);
|
||||
existing.add(tool.name);
|
||||
pluginToolMeta.set(tool, {
|
||||
pluginId: entry.pluginId,
|
||||
optional: entry.optional,
|
||||
});
|
||||
tools.push(tool);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user