File size: 4,003 Bytes
7a1ad33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import {
  ALL_BUILTIN_TOOL_NAMES,
  TOOL_LEGACY_ALIASES,
} from '@google/gemini-cli-core';

export type ToolCategory =
  | 'file-system'
  | 'shell'
  | 'web'
  | 'planning'
  | 'user-interaction'
  | 'skills'
  | 'task-tracker'
  | 'agent'
  | 'mcp';

export interface ToolRegistryEntry {
  name: string;
  category: ToolCategory;
  aliases: readonly string[];
}

export interface ToolRegistry {
  tools: ReadonlyMap<string, ToolRegistryEntry>;
  totalTools: number;
  byCategory: ReadonlyMap<ToolCategory, readonly ToolRegistryEntry[]>;
  aliasLookup: ReadonlyMap<string, string>;
}

const TOOL_CATEGORIES: Record<
  (typeof ALL_BUILTIN_TOOL_NAMES)[number],
  ToolCategory
> = {
  glob: 'file-system',
  grep_search: 'file-system',
  list_directory: 'file-system',
  read_file: 'file-system',
  read_many_files: 'file-system',
  write_file: 'file-system',
  replace: 'file-system',
  run_shell_command: 'shell',
  google_web_search: 'web',
  web_fetch: 'web',
  enter_plan_mode: 'planning',
  exit_plan_mode: 'planning',
  write_todos: 'planning',
  ask_user: 'user-interaction',
  activate_skill: 'skills',
  get_internal_docs: 'skills',
  tracker_create_task: 'task-tracker',
  tracker_update_task: 'task-tracker',
  tracker_get_task: 'task-tracker',
  tracker_list_tasks: 'task-tracker',
  tracker_add_dependency: 'task-tracker',
  tracker_visualize: 'task-tracker',
  invoke_agent: 'agent',
  complete_task: 'agent',
  update_topic: 'agent',
  read_mcp_resource: 'mcp',
  list_mcp_resources: 'mcp',
};

let registryCache: ToolRegistry | undefined;

export function buildToolRegistry(): ToolRegistry {
  if (registryCache) {
    return registryCache;
  }

  const tools = new Map<string, ToolRegistryEntry>();
  const aliasLookup = new Map<string, string>();
  const categoryGroups = new Map<ToolCategory, ToolRegistryEntry[]>();

  for (const name of ALL_BUILTIN_TOOL_NAMES) {
    const category = TOOL_CATEGORIES[name];
    const aliases: string[] = [];

    for (const [legacyName, canonicalName] of Object.entries(
      TOOL_LEGACY_ALIASES,
    )) {
      if (canonicalName === name) {
        aliases.push(legacyName);
        aliasLookup.set(legacyName, name);
      }
    }

    aliasLookup.set(name, name);

    const entry: ToolRegistryEntry = {
      name,
      category,
      aliases: Object.freeze(aliases),
    };

    tools.set(name, entry);

    const group = categoryGroups.get(category);
    if (group) {
      group.push(entry);
    } else {
      categoryGroups.set(category, [entry]);
    }
  }

  const customTools: Array<[string, ToolCategory]> = [
    ['read_background_output', 'shell'],
    ['list_background_processes', 'shell'],
    ['mutation-agent', 'agent'],
    ['generalist', 'agent'],
    ['generalist-agent', 'agent'],
    ['coder-agent', 'agent'],
    ['task-tracker-agent', 'agent'],
  ];

  for (const [name, category] of customTools) {
    aliasLookup.set(name, name);
    const entry: ToolRegistryEntry = {
      name,
      category,
      aliases: Object.freeze([]),
    };
    tools.set(name, entry);
    const group = categoryGroups.get(category);
    if (group) {
      group.push(entry);
    } else {
      categoryGroups.set(category, [entry]);
    }
  }

  const frozenCategories = new Map<
    ToolCategory,
    readonly ToolRegistryEntry[]
  >();
  for (const [cat, entries] of categoryGroups) {
    frozenCategories.set(cat, Object.freeze(entries));
  }

  registryCache = {
    tools,
    totalTools: tools.size,
    byCategory: frozenCategories,
    aliasLookup,
  };
  return registryCache;
}

export function resolveToolName(
  registry: ToolRegistry,
  name: string,
): string | undefined {
  if (!name) {
    return undefined;
  }
  return registry.aliasLookup.get(name);
}

export function getToolsByCategory(
  registry: ToolRegistry,
  category: ToolCategory,
): readonly ToolRegistryEntry[] {
  return registry.byCategory.get(category) ?? [];
}