mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-11 12:00:43 +00:00
69 lines
2.1 KiB
TypeScript
69 lines
2.1 KiB
TypeScript
import { applyOwnerOnlyToolPolicy } from "../agents/tool-policy.js";
|
|
import type { OpenClawConfig } from "../config/types.openclaw.js";
|
|
import {
|
|
buildMcpToolSchema,
|
|
type McpLoopbackTool,
|
|
type McpToolSchemaEntry,
|
|
} from "./mcp-http.schema.js";
|
|
import { resolveGatewayScopedTools } from "./tool-resolution.js";
|
|
|
|
const TOOL_CACHE_TTL_MS = 30_000;
|
|
const NATIVE_TOOL_EXCLUDE = new Set(["read", "write", "edit", "apply_patch", "exec", "process"]);
|
|
|
|
type CachedScopedTools = {
|
|
agentId: string | undefined;
|
|
tools: McpLoopbackTool[];
|
|
toolSchema: McpToolSchemaEntry[];
|
|
configRef: OpenClawConfig;
|
|
time: number;
|
|
};
|
|
|
|
export class McpLoopbackToolCache {
|
|
#entries = new Map<string, CachedScopedTools>();
|
|
|
|
resolve(params: {
|
|
cfg: OpenClawConfig;
|
|
sessionKey: string;
|
|
messageProvider: string | undefined;
|
|
accountId: string | undefined;
|
|
senderIsOwner: boolean | undefined;
|
|
}): CachedScopedTools {
|
|
const cacheKey = [
|
|
params.sessionKey,
|
|
params.messageProvider ?? "",
|
|
params.accountId ?? "",
|
|
params.senderIsOwner === true ? "owner" : "non-owner",
|
|
].join("\u0000");
|
|
const now = Date.now();
|
|
const cached = this.#entries.get(cacheKey);
|
|
if (cached && cached.configRef === params.cfg && now - cached.time < TOOL_CACHE_TTL_MS) {
|
|
return cached;
|
|
}
|
|
|
|
const next = resolveGatewayScopedTools({
|
|
cfg: params.cfg,
|
|
sessionKey: params.sessionKey,
|
|
messageProvider: params.messageProvider,
|
|
accountId: params.accountId,
|
|
senderIsOwner: params.senderIsOwner,
|
|
surface: "loopback",
|
|
excludeToolNames: NATIVE_TOOL_EXCLUDE,
|
|
});
|
|
const tools = applyOwnerOnlyToolPolicy(next.tools, params.senderIsOwner === true);
|
|
const nextEntry: CachedScopedTools = {
|
|
agentId: next.agentId,
|
|
tools,
|
|
toolSchema: buildMcpToolSchema(tools),
|
|
configRef: params.cfg,
|
|
time: now,
|
|
};
|
|
this.#entries.set(cacheKey, nextEntry);
|
|
for (const [key, entry] of this.#entries) {
|
|
if (now - entry.time >= TOOL_CACHE_TTL_MS) {
|
|
this.#entries.delete(key);
|
|
}
|
|
}
|
|
return nextEntry;
|
|
}
|
|
}
|