import { createHash } from 'node:crypto'; import type { AgentRuntimeContext } from '../runtime/agentRuntimeContext.js'; import type { Tool } from '../types.js'; import { filterToolsForRuntime } from '../runtime/agentRuntimeContext.js'; export interface EffectiveToolSet { readonly tools: Tool[]; readonly names: ReadonlySet; readonly signature: string; readonly requiredNames: ReadonlySet; } const toolIds = new WeakMap(); let nextToolId = 1; const effectiveSetCache = new Map(); function identity(tool: Tool): number { let id = toolIds.get(tool); if (id === undefined) { id = nextToolId++; toolIds.set(tool, id); } return id; } /** * Resolve the host-visible tool catalog once for a run. The returned set is * both what the model sees and the execution boundary; callers must not * reconstruct capabilities from ALL_TOOLS later in the call chain. */ export function createEffectiveToolSet(input: { availableTools: readonly Tool[]; runtime: AgentRuntimeContext; allowedNames?: readonly string[]; requiredNames?: readonly string[]; }): EffectiveToolSet { const declared = new Set(); for (const tool of input.availableTools) { if (declared.has(tool.name)) throw new Error(`重复的工具名:${tool.name}`); declared.add(tool.name); } const resolvedAvailability = new Map( input.availableTools.map( (tool) => [ tool, tool.resolveAvailability?.(input.runtime) ?? { available: true, signature: 'always', }, ] as const, ), ); const availability = input.availableTools.map((tool) => { const resolved = resolvedAvailability.get(tool)!; return [tool.name, resolved.available, resolved.signature]; }); const cacheKey = JSON.stringify([ input.runtime.cwd, input.runtime.surface, input.runtime.profile, input.runtime.conversationMode ?? 'private', input.runtime.memoryAccess, input.runtime.browser ? [ 'browser', input.runtime.browser.supervised === true ? 'supervised' : 'headless', input.runtime.browser.windowMode ?? 'default', ].join(':') : 'no-browser', input.runtime.computer ? 'computer' : 'no-computer', input.runtime.computerIntent?.needed === true ? `computer-intent:${input.runtime.computerIntent.source}` : 'no-computer-intent', input.availableTools.map(identity), availability, input.allowedNames ?? null, input.requiredNames ?? [], ]); const cached = effectiveSetCache.get(cacheKey); if (cached) return cached; const allowed = input.allowedNames === undefined ? undefined : new Set(input.allowedNames); const requested = allowed === undefined ? input.availableTools : input.availableTools.filter((tool) => allowed.has(tool.name)); const tools = filterToolsForRuntime( requested, input.runtime, resolvedAvailability, ); const names = new Set(tools.map((tool) => tool.name)); const requiredNames = new Set( (input.requiredNames ?? []).filter((name) => names.has(name)), ); const schemaIdentity = tools.map((tool) => ({ name: tool.name, description: typeof tool.description === 'function' ? tool.description() : tool.description, parameters: tool.parameters, })); const signature = createHash('sha256') .update(JSON.stringify(schemaIdentity)) .digest('hex'); const result = Object.freeze({ tools: Object.freeze([...tools]) as unknown as Tool[], names, signature, requiredNames, }); effectiveSetCache.set(cacheKey, result); if (effectiveSetCache.size > 128) { effectiveSetCache.delete(effectiveSetCache.keys().next().value!); } return result; }