import type { Provider, Tool } from '../types.js'; import { ONLINE_TOOL_CATEGORY_BY_NAME, type OnlineToolCategory, onlineToolCategory as sharedOnlineToolCategory, } from './onlineToolContract.js'; /** * Categories shared with minimal-server. They are a server-enforced upper * bound when a remote model is selected. Unknown/dynamic tools fail closed: * a plugin cannot silently gain remote-model visibility just by registering a * new tool name. */ export type { OnlineToolCategory } from './onlineToolContract.js'; export interface ToolSecurityMetadata { name: string; category: OnlineToolCategory; /** Reserved for the privacy UI and future per-tool redaction policy. */ mayTransmitLocalData: boolean; } const TOOL_SECURITY_METADATA: Readonly> = Object.fromEntries( Object.entries(ONLINE_TOOL_CATEGORY_BY_NAME).map(([name, category]) => [ name, { name, category, mayTransmitLocalData: category === 'workspace' || category === 'computer', }, ]), ) as Readonly>; export function onlineToolCategory( name: string, ): OnlineToolCategory | undefined { return sharedOnlineToolCategory(name); } export function toolSecurityMetadata( name: string, ): ToolSecurityMetadata | undefined { return TOOL_SECURITY_METADATA[name]; } /** Applies the remote model's maximum category boundary before schemas leave * the device. Local providers retain their original complete tool set. */ export function filterToolsForProvider( tools: readonly Tool[], provider: Provider, ): Tool[] { if (provider.source !== 'gateway') return [...tools]; return filterToolsForCategories( tools, // The server maximum is the non-negotiable upper bound. The compatibility // field is only a fallback for old descriptors that lack the explicit // max/default split; it must never override the maximum with defaults. provider.gatewayMaxToolCategories ?? provider.gatewayToolCategories, ); } /** Applies the per-Session switch inside the server/model upper bound. */ export function filterToolsForCategories( tools: readonly Tool[], categories?: readonly string[], ): Tool[] { if (!categories) return [...tools]; const allowed = new Set(categories as OnlineToolCategory[]); return tools.filter((tool) => { const category = onlineToolCategory(tool.name); return category !== undefined && allowed.has(category); }); }