/** * @license * Copyright 2025 Google LLC * SPDX-License-Identifier: Apache-2.0 */ /** * Tool name constants to avoid circular dependencies. * These constants are used across multiple files and should be kept in sync * with the actual tool class names. * * Filesystem-path-bearing tools (whose inputs name actual project files) * also need to be added to `FS_PATH_TOOL_NAMES` in * `core/coreToolScheduler.ts` so conditional rules and path-conditional * skill activation see the touched paths. Forgetting that registration * silently skips the activation pipeline for that tool — there is no * compile-time guard. (TODO: replace the manual allowlist with a * per-declaration `pathFields?: string[]` annotation on the tool class.) */ export const ToolNames = { EXEC: 'exec', EDIT: 'edit', WRITE_FILE: 'write_file', READ_FILE: 'read_file', ZOOM_IMAGE: 'zoom_image', GREP: 'grep_search', GLOB: 'glob', SHELL: 'run_shell_command', TODO_WRITE: 'todo_write', MEMORY: 'save_memory', MANAGE_MEMORY: 'manage_memory', SEARCH_MEMORY: 'search_memory', AGENT: 'agent', SKILL: 'skill', EXIT_PLAN_MODE: 'exit_plan_mode', ENTER_PLAN_MODE: 'enter_plan_mode', WEB_FETCH: 'web_fetch', WEB_SEARCH: 'web_search', IMAGE_GEN: 'image_gen', LS: 'list_directory', LSP: 'lsp', ASK_USER_QUESTION: 'ask_user_question', CRON_CREATE: 'cron_create', CRON_LIST: 'cron_list', CRON_DELETE: 'cron_delete', AUTOFIX_PR: 'autofix_pr', LOOP_WAKEUP: 'loop_wakeup', CREATE_SUB_SESSION: 'create_sub_session', LIST_AGENTS: 'list_agents', TASK_STOP: 'task_stop', TASK_CREATE: 'task_create', TASK_UPDATE: 'task_update', TASK_LIST: 'task_list', TEAM_CREATE: 'team_create', TEAM_DELETE: 'team_delete', TEAM_PLAN_APPROVAL: 'team_plan_approval', REQUEST_SHUTDOWN: 'request_shutdown', SEND_MESSAGE: 'send_message', STRUCTURED_OUTPUT: 'structured_output', MONITOR: 'monitor', NOTEBOOK_EDIT: 'notebook_edit', TOOL_CALL: 'tool_call', TOOL_SEARCH: 'tool_search', READ_MCP_RESOURCE: 'read_mcp_resource', ADVISOR: 'advisor', ENTER_WORKTREE: 'enter_worktree', EXIT_WORKTREE: 'exit_worktree', WORKFLOW: 'workflow', ARTIFACT: 'artifact', RECORD_ARTIFACT: 'record_artifact', RECORD_SOURCE: 'record_source', REPORT_FINDINGS: 'report_findings', GET_GOAL: 'get_goal', UPDATE_GOAL: 'update_goal', // Omni media-policy tools (fixed-policy-only by default; modelAccess // config can open them to the model). OMNI_DOWNSAMPLE_IMAGE: 'omni_downsample_image', OMNI_DOWNSCALE_VIDEO: 'omni_downscale_video', OMNI_DOWNSAMPLE_AUDIO: 'omni_downsample_audio', OMNI_EXTRACT_KEYFRAMES: 'omni_extract_keyframes', OMNI_EXTRACT_AUDIO: 'omni_extract_audio', OMNI_CLIP_VIDEO: 'omni_clip_video', OMNI_CONVERT_IMAGE: 'omni_convert_image', OMNI_TRANSCRIBE_AUDIO: 'omni_transcribe_audio', OMNI_CLIP_IMAGE: 'omni_clip_image', OMNI_CLIP_AUDIO: 'omni_clip_audio', OMNI_CAPTION_IMAGE: 'omni_caption_image', OMNI_CAPTION_AUDIO: 'omni_caption_audio', OMNI_OCR_IMAGE: 'omni_ocr_image', OMNI_UNDERSTAND_VIDEO_SEGMENTS: 'omni_understand_video_segments', // Omni memory recall (registered only when omni is enabled AND // `omni.memory.recall.mode === 'active'` — D10 mutual exclusion). OMNI_RECALL_MEDIA_MEMORY: 'omni_recall_media_memory', PROPOSE_GOAL: 'propose_goal', DISPLAY_IMAGE: 'display_image', } as const; /** * The only tools an agent-host session declares and runs. The Host is * read-only and auto-rejects permission prompts, so anything outside this * set would be advertised to the model yet fail every invocation. */ export const AGENT_HOST_TOOL_NAMES: readonly string[] = [ ToolNames.READ_FILE, ToolNames.GREP, ToolNames.LS, ]; /** * Tool display name constants to avoid circular dependencies. * These constants are used across multiple files and should be kept in sync * with the actual tool display names. */ export const ToolDisplayNames = { EXEC: 'Exec', EDIT: 'Edit', WRITE_FILE: 'WriteFile', READ_FILE: 'ReadFile', ZOOM_IMAGE: 'ZoomImage', GREP: 'Grep', GLOB: 'Glob', SHELL: 'Shell', TODO_WRITE: 'TodoList', MEMORY: 'SaveMemory', MANAGE_MEMORY: 'ManageMemory', SEARCH_MEMORY: 'SearchMemory', AGENT: 'Agent', SKILL: 'Skill', EXIT_PLAN_MODE: 'ExitPlanMode', ENTER_PLAN_MODE: 'EnterPlanMode', WEB_FETCH: 'WebFetch', WEB_SEARCH: 'WebSearch', IMAGE_GEN: 'ImageGen', LS: 'ListFiles', LSP: 'Lsp', ASK_USER_QUESTION: 'AskUserQuestion', CRON_CREATE: 'CronCreate', CRON_LIST: 'CronList', CRON_DELETE: 'CronDelete', AUTOFIX_PR: 'AutofixPr', LOOP_WAKEUP: 'LoopWakeup', CREATE_SUB_SESSION: 'CreateSubSession', LIST_AGENTS: 'ListAgents', TASK_STOP: 'TaskStop', TASK_CREATE: 'TaskCreate', TASK_UPDATE: 'TaskUpdate', TASK_LIST: 'TaskList', TEAM_CREATE: 'TeamCreate', TEAM_DELETE: 'TeamDelete', TEAM_PLAN_APPROVAL: 'TeamPlanApproval', REQUEST_SHUTDOWN: 'RequestShutdown', SEND_MESSAGE: 'SendMessage', STRUCTURED_OUTPUT: 'StructuredOutput', MONITOR: 'Monitor', NOTEBOOK_EDIT: 'NotebookEdit', TOOL_CALL: 'ToolCall', TOOL_SEARCH: 'ToolSearch', READ_MCP_RESOURCE: 'ReadMcpResource', ADVISOR: 'Advisor', ENTER_WORKTREE: 'EnterWorktree', EXIT_WORKTREE: 'ExitWorktree', WORKFLOW: 'Workflow', ARTIFACT: 'Artifact', RECORD_ARTIFACT: 'RecordArtifact', RECORD_SOURCE: 'RecordSource', REPORT_FINDINGS: 'ReportFindings', GET_GOAL: 'Goal', UPDATE_GOAL: 'UpdateGoal', OMNI_DOWNSAMPLE_IMAGE: 'DownsampleImage', OMNI_DOWNSCALE_VIDEO: 'DownscaleVideo', OMNI_DOWNSAMPLE_AUDIO: 'DownsampleAudio', OMNI_EXTRACT_KEYFRAMES: 'ExtractKeyframes', OMNI_EXTRACT_AUDIO: 'ExtractAudio', OMNI_CLIP_VIDEO: 'ClipVideo', OMNI_CONVERT_IMAGE: 'ConvertImage', OMNI_TRANSCRIBE_AUDIO: 'TranscribeAudio', OMNI_CLIP_IMAGE: 'ClipImage', OMNI_CLIP_AUDIO: 'ClipAudio', OMNI_CAPTION_IMAGE: 'CaptionImage', OMNI_CAPTION_AUDIO: 'CaptionAudio', OMNI_OCR_IMAGE: 'OcrImage', OMNI_UNDERSTAND_VIDEO_SEGMENTS: 'UnderstandVideoSegments', OMNI_RECALL_MEDIA_MEMORY: 'RecallMediaMemory', PROPOSE_GOAL: 'ProposeGoal', DISPLAY_IMAGE: 'DisplayImage', } as const; // Migration from old tool names to new tool names // These legacy tool names were used in earlier versions and need to be supported // for backward compatibility with existing user configurations export const ToolNamesMigration = { search_file_content: ToolNames.GREP, // Legacy name from grep tool replace: ToolNames.EDIT, // Legacy name from edit tool task: ToolNames.AGENT, // Legacy name from agent tool (renamed from task) } as const; /** * Resolve a tool name through the legacy-alias migration map (e.g. * `search_file_content` → `grep_search`) to its canonical form. The single * alias-resolution site: every caller that classifies or keys tool calls by * name — the scheduler, loop detection, plan redaction, memory refresh, the * headless partitioner in nonInteractiveCli, the daemon/ACP session — must * use this so an aliased call is treated identically everywhere. */ export function canonicalToolName(toolName: string): string { if (!Object.prototype.hasOwnProperty.call(ToolNamesMigration, toolName)) { return toolName; } return (ToolNamesMigration as Record)[toolName] ?? toolName; } /** * Resolve a model-supplied tool name against the registered names the way * both halves of the deferred-tool bridge must agree on: an exact match wins, * otherwise a single case-insensitive match. Returns the registered name, * the candidate list when several registered names differ from the request * only by case, or `undefined` when nothing matches. * * Returning the candidates instead of picking one keeps the answer * independent of registration order, which `ensureTool` changes when it * moves a lazily-built tool from the factory map into the tool map (#11321). */ export function resolveRegisteredToolName( requested: string, registered: readonly string[], ): string | string[] | undefined { if (registered.includes(requested)) return requested; const lower = requested.toLowerCase(); const candidates = [ ...new Set(registered.filter((name) => name.toLowerCase() === lower)), ].sort(); if (candidates.length === 1) return candidates[0]; return candidates.length > 1 ? candidates : undefined; } // Migration from old tool display names to new tool display names // These legacy display names were used before the tool naming standardization export const ToolDisplayNamesMigration = { SearchFiles: ToolDisplayNames.GREP, // Old display name for Grep FindFiles: ToolDisplayNames.GLOB, // Old display name for Glob ReadFolder: ToolDisplayNames.LS, // Old display name for ListFiles Task: ToolDisplayNames.AGENT, // Old display name for Agent (renamed from Task) TodoWrite: ToolDisplayNames.TODO_WRITE, // Old display name for TodoList (renamed from TodoWrite) } as const; /** * Every spelling of a built-in tool, mapped to the name it is registered * under: the tool name itself, its display name, and the legacy aliases of * either. Built at module end so every table above is initialised. */ const BUILTIN_TOOL_NAMES: ReadonlyMap = (() => { const lookup = new Map(); const displayNames = ToolDisplayNames as Record; for (const name of Object.values(ToolNames)) { lookup.set(name, name); } for (const [key, name] of Object.entries(ToolNames)) { const display = displayNames[key]; if (display !== undefined && !lookup.has(display)) { lookup.set(display, name); } } for (const [legacy, name] of Object.entries(ToolNamesMigration)) { if (!lookup.has(legacy)) lookup.set(legacy, name); } for (const [legacyDisplay, display] of Object.entries( ToolDisplayNamesMigration, )) { const name = lookup.get(display); if (name !== undefined && !lookup.has(legacyDisplay)) { lookup.set(legacyDisplay, name); } } return lookup; })(); /** * The tool name a built-in tool is registered under, given its tool name, its * display name, or a legacy alias of either; `undefined` for anything that is * not a built-in tool (an MCP tool, a discovered tool, a typo). Static, so the * answer does not depend on whether the tool is registered in this session. */ export function resolveBuiltinToolName(name: string): string | undefined { return BUILTIN_TOOL_NAMES.get(name); }