@google/gemini-cli-core 0.21.0-nightly.20251218.739c02bd6 → 0.21.0-nightly.20251220.41a1a3eed

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (212) hide show
  1. package/dist/docs/CONTRIBUTING.md +546 -0
  2. package/dist/docs/architecture.md +80 -0
  3. package/dist/docs/assets/connected_devtools.png +0 -0
  4. package/dist/docs/assets/gemini-screenshot.png +0 -0
  5. package/dist/docs/assets/release_patch.png +0 -0
  6. package/dist/docs/assets/theme-ansi-light.png +0 -0
  7. package/dist/docs/assets/theme-ansi.png +0 -0
  8. package/dist/docs/assets/theme-atom-one.png +0 -0
  9. package/dist/docs/assets/theme-ayu-light.png +0 -0
  10. package/dist/docs/assets/theme-ayu.png +0 -0
  11. package/dist/docs/assets/theme-custom.png +0 -0
  12. package/dist/docs/assets/theme-default-light.png +0 -0
  13. package/dist/docs/assets/theme-default.png +0 -0
  14. package/dist/docs/assets/theme-dracula.png +0 -0
  15. package/dist/docs/assets/theme-github-light.png +0 -0
  16. package/dist/docs/assets/theme-github.png +0 -0
  17. package/dist/docs/assets/theme-google-light.png +0 -0
  18. package/dist/docs/assets/theme-xcode-light.png +0 -0
  19. package/dist/docs/changelogs/index.md +592 -0
  20. package/dist/docs/changelogs/latest.md +225 -0
  21. package/dist/docs/changelogs/preview.md +129 -0
  22. package/dist/docs/changelogs/releases.md +896 -0
  23. package/dist/docs/cli/authentication.md +3 -0
  24. package/dist/docs/cli/checkpointing.md +94 -0
  25. package/dist/docs/cli/commands.md +354 -0
  26. package/dist/docs/cli/configuration.md +780 -0
  27. package/dist/docs/cli/custom-commands.md +315 -0
  28. package/dist/docs/cli/enterprise.md +565 -0
  29. package/dist/docs/cli/gemini-ignore.md +71 -0
  30. package/dist/docs/cli/gemini-md.md +108 -0
  31. package/dist/docs/cli/generation-settings.md +210 -0
  32. package/dist/docs/cli/headless.md +388 -0
  33. package/dist/docs/cli/index.md +63 -0
  34. package/dist/docs/cli/keyboard-shortcuts.md +143 -0
  35. package/dist/docs/cli/model-routing.md +37 -0
  36. package/dist/docs/cli/model.md +62 -0
  37. package/dist/docs/cli/sandbox.md +171 -0
  38. package/dist/docs/cli/session-management.md +158 -0
  39. package/dist/docs/cli/settings.md +112 -0
  40. package/dist/docs/cli/system-prompt.md +93 -0
  41. package/dist/docs/cli/telemetry.md +791 -0
  42. package/dist/docs/cli/themes.md +237 -0
  43. package/dist/docs/cli/token-caching.md +20 -0
  44. package/dist/docs/cli/trusted-folders.md +95 -0
  45. package/dist/docs/cli/tutorials.md +83 -0
  46. package/dist/docs/cli/uninstall.md +47 -0
  47. package/dist/docs/core/index.md +101 -0
  48. package/dist/docs/core/memport.md +244 -0
  49. package/dist/docs/core/policy-engine.md +267 -0
  50. package/dist/docs/core/tools-api.md +131 -0
  51. package/dist/docs/examples/proxy-script.md +83 -0
  52. package/dist/docs/extensions/extension-releasing.md +183 -0
  53. package/dist/docs/extensions/getting-started-extensions.md +245 -0
  54. package/dist/docs/extensions/index.md +293 -0
  55. package/dist/docs/faq.md +154 -0
  56. package/dist/docs/get-started/authentication.md +321 -0
  57. package/dist/docs/get-started/configuration-v1.md +888 -0
  58. package/dist/docs/get-started/configuration.md +1444 -0
  59. package/dist/docs/get-started/deployment.md +143 -0
  60. package/dist/docs/get-started/examples.md +219 -0
  61. package/dist/docs/get-started/gemini-3.md +116 -0
  62. package/dist/docs/get-started/index.md +71 -0
  63. package/dist/docs/get-started/installation.md +141 -0
  64. package/dist/docs/hooks/best-practices.md +806 -0
  65. package/dist/docs/hooks/index.md +665 -0
  66. package/dist/docs/hooks/reference.md +168 -0
  67. package/dist/docs/hooks/writing-hooks.md +1026 -0
  68. package/dist/docs/ide-integration/ide-companion-spec.md +267 -0
  69. package/dist/docs/ide-integration/index.md +202 -0
  70. package/dist/docs/index.md +147 -0
  71. package/dist/docs/integration-tests.md +211 -0
  72. package/dist/docs/issue-and-pr-automation.md +134 -0
  73. package/dist/docs/local-development.md +128 -0
  74. package/dist/docs/mermaid/context.mmd +103 -0
  75. package/dist/docs/mermaid/render-path.mmd +64 -0
  76. package/dist/docs/npm.md +62 -0
  77. package/dist/docs/quota-and-pricing.md +158 -0
  78. package/dist/docs/release-confidence.md +164 -0
  79. package/dist/docs/releases.md +540 -0
  80. package/dist/docs/sidebar.json +297 -0
  81. package/dist/docs/tools/file-system.md +217 -0
  82. package/dist/docs/tools/index.md +95 -0
  83. package/dist/docs/tools/mcp-server.md +1044 -0
  84. package/dist/docs/tools/memory.md +54 -0
  85. package/dist/docs/tools/shell.md +260 -0
  86. package/dist/docs/tools/todos.md +57 -0
  87. package/dist/docs/tools/web-fetch.md +59 -0
  88. package/dist/docs/tools/web-search.md +42 -0
  89. package/dist/docs/tos-privacy.md +96 -0
  90. package/dist/docs/troubleshooting.md +158 -0
  91. package/dist/google-gemini-cli-core-0.21.0-nightly.20251219.70696e364.tgz +0 -0
  92. package/dist/src/agents/delegate-to-agent-tool.test.js +1 -0
  93. package/dist/src/agents/delegate-to-agent-tool.test.js.map +1 -1
  94. package/dist/src/agents/introspection-agent.d.ts +23 -0
  95. package/dist/src/agents/introspection-agent.js +72 -0
  96. package/dist/src/agents/introspection-agent.js.map +1 -0
  97. package/dist/src/agents/introspection-agent.test.d.ts +6 -0
  98. package/dist/src/agents/introspection-agent.test.js +47 -0
  99. package/dist/src/agents/introspection-agent.test.js.map +1 -0
  100. package/dist/src/agents/local-executor.d.ts +0 -6
  101. package/dist/src/agents/local-executor.js +73 -47
  102. package/dist/src/agents/local-executor.js.map +1 -1
  103. package/dist/src/agents/local-executor.test.d.ts +1 -7
  104. package/dist/src/agents/local-executor.test.js +27 -9
  105. package/dist/src/agents/local-executor.test.js.map +1 -1
  106. package/dist/src/agents/registry.d.ts +1 -0
  107. package/dist/src/agents/registry.js +51 -7
  108. package/dist/src/agents/registry.js.map +1 -1
  109. package/dist/src/agents/registry.test.js +112 -1
  110. package/dist/src/agents/registry.test.js.map +1 -1
  111. package/dist/src/agents/toml-loader.d.ts +65 -0
  112. package/dist/src/agents/toml-loader.js +176 -0
  113. package/dist/src/agents/toml-loader.js.map +1 -0
  114. package/dist/src/agents/toml-loader.test.d.ts +6 -0
  115. package/dist/src/agents/toml-loader.test.js +190 -0
  116. package/dist/src/agents/toml-loader.test.js.map +1 -0
  117. package/dist/src/availability/modelAvailabilityService.d.ts +2 -1
  118. package/dist/src/config/config.d.ts +9 -0
  119. package/dist/src/config/config.js +29 -0
  120. package/dist/src/config/config.js.map +1 -1
  121. package/dist/src/config/config.test.js +59 -1
  122. package/dist/src/config/config.test.js.map +1 -1
  123. package/dist/src/config/storage.d.ts +2 -0
  124. package/dist/src/config/storage.js +6 -0
  125. package/dist/src/config/storage.js.map +1 -1
  126. package/dist/src/config/storage.test.js +8 -0
  127. package/dist/src/config/storage.test.js.map +1 -1
  128. package/dist/src/core/client.js +8 -4
  129. package/dist/src/core/client.js.map +1 -1
  130. package/dist/src/core/client.test.js +20 -0
  131. package/dist/src/core/client.test.js.map +1 -1
  132. package/dist/src/core/clientHookTriggers.js +2 -2
  133. package/dist/src/core/clientHookTriggers.js.map +1 -1
  134. package/dist/src/core/coreToolHookTriggers.js +3 -3
  135. package/dist/src/core/coreToolHookTriggers.js.map +1 -1
  136. package/dist/src/core/geminiChatHookTriggers.js +3 -3
  137. package/dist/src/core/geminiChatHookTriggers.js.map +1 -1
  138. package/dist/src/core/sessionHookTriggers.js +3 -3
  139. package/dist/src/core/sessionHookTriggers.js.map +1 -1
  140. package/dist/src/generated/git-commit.d.ts +2 -2
  141. package/dist/src/generated/git-commit.js +2 -2
  142. package/dist/src/hooks/hookEventHandler.js +10 -4
  143. package/dist/src/hooks/hookEventHandler.js.map +1 -1
  144. package/dist/src/hooks/hookEventHandler.test.js +40 -0
  145. package/dist/src/hooks/hookEventHandler.test.js.map +1 -1
  146. package/dist/src/hooks/hookPlanner.js +3 -1
  147. package/dist/src/hooks/hookPlanner.js.map +1 -1
  148. package/dist/src/hooks/hookPlanner.test.js +61 -0
  149. package/dist/src/hooks/hookPlanner.test.js.map +1 -1
  150. package/dist/src/hooks/hookRegistry.d.ts +1 -1
  151. package/dist/src/hooks/hookRegistry.js +2 -2
  152. package/dist/src/hooks/hookRegistry.js.map +1 -1
  153. package/dist/src/hooks/hookRegistry.test.js +73 -0
  154. package/dist/src/hooks/hookRegistry.test.js.map +1 -1
  155. package/dist/src/hooks/hookRunner.js +14 -10
  156. package/dist/src/hooks/hookRunner.js.map +1 -1
  157. package/dist/src/hooks/hookRunner.test.js +81 -33
  158. package/dist/src/hooks/hookRunner.test.js.map +1 -1
  159. package/dist/src/hooks/types.d.ts +2 -0
  160. package/dist/src/hooks/types.js.map +1 -1
  161. package/dist/src/mcp/oauth-provider.js +6 -2
  162. package/dist/src/mcp/oauth-provider.js.map +1 -1
  163. package/dist/src/mcp/oauth-provider.test.js +4 -1
  164. package/dist/src/mcp/oauth-provider.test.js.map +1 -1
  165. package/dist/src/mcp/oauth-utils.d.ts +8 -1
  166. package/dist/src/mcp/oauth-utils.js +30 -1
  167. package/dist/src/mcp/oauth-utils.js.map +1 -1
  168. package/dist/src/mcp/oauth-utils.test.js +42 -0
  169. package/dist/src/mcp/oauth-utils.test.js.map +1 -1
  170. package/dist/src/services/contextManager.d.ts +5 -11
  171. package/dist/src/services/contextManager.js +20 -17
  172. package/dist/src/services/contextManager.js.map +1 -1
  173. package/dist/src/services/contextManager.test.js +40 -41
  174. package/dist/src/services/contextManager.test.js.map +1 -1
  175. package/dist/src/services/shellExecutionService.js +18 -2
  176. package/dist/src/services/shellExecutionService.js.map +1 -1
  177. package/dist/src/tools/get-internal-docs.d.ts +27 -0
  178. package/dist/src/tools/get-internal-docs.js +129 -0
  179. package/dist/src/tools/get-internal-docs.js.map +1 -0
  180. package/dist/src/tools/get-internal-docs.test.d.ts +6 -0
  181. package/dist/src/tools/get-internal-docs.test.js +56 -0
  182. package/dist/src/tools/get-internal-docs.test.js.map +1 -0
  183. package/dist/src/tools/tool-names.d.ts +14 -0
  184. package/dist/src/tools/tool-names.js +55 -0
  185. package/dist/src/tools/tool-names.js.map +1 -1
  186. package/dist/src/tools/tool-names.test.d.ts +6 -0
  187. package/dist/src/tools/tool-names.test.js +43 -0
  188. package/dist/src/tools/tool-names.test.js.map +1 -0
  189. package/dist/src/tools/tool-registry.d.ts +0 -1
  190. package/dist/src/tools/tool-registry.js +1 -1
  191. package/dist/src/tools/tool-registry.js.map +1 -1
  192. package/dist/src/tools/tool-registry.test.js +2 -1
  193. package/dist/src/tools/tool-registry.test.js.map +1 -1
  194. package/dist/src/utils/environmentContext.js +3 -0
  195. package/dist/src/utils/environmentContext.js.map +1 -1
  196. package/dist/src/utils/environmentContext.test.js +2 -0
  197. package/dist/src/utils/environmentContext.test.js.map +1 -1
  198. package/dist/src/utils/events.d.ts +3 -2
  199. package/dist/src/utils/events.js.map +1 -1
  200. package/dist/src/utils/googleQuotaErrors.js +20 -0
  201. package/dist/src/utils/googleQuotaErrors.js.map +1 -1
  202. package/dist/src/utils/googleQuotaErrors.test.js +53 -2
  203. package/dist/src/utils/googleQuotaErrors.test.js.map +1 -1
  204. package/dist/src/utils/memoryDiscovery.js +1 -1
  205. package/dist/src/utils/memoryDiscovery.js.map +1 -1
  206. package/dist/src/utils/memoryDiscovery.test.js +3 -1
  207. package/dist/src/utils/memoryDiscovery.test.js.map +1 -1
  208. package/dist/src/utils/shell-utils.js +25 -4
  209. package/dist/src/utils/shell-utils.js.map +1 -1
  210. package/dist/tsconfig.tsbuildinfo +1 -1
  211. package/package.json +1 -6
  212. package/dist/google-gemini-cli-core-0.21.0-nightly.20251216.bb0c0d8ee.tgz +0 -0
@@ -0,0 +1,244 @@
1
+ # Memory Import Processor
2
+
3
+ The Memory Import Processor is a feature that allows you to modularize your
4
+ GEMINI.md files by importing content from other files using the `@file.md`
5
+ syntax.
6
+
7
+ ## Overview
8
+
9
+ This feature enables you to break down large GEMINI.md files into smaller, more
10
+ manageable components that can be reused across different contexts. The import
11
+ processor supports both relative and absolute paths, with built-in safety
12
+ features to prevent circular imports and ensure file access security.
13
+
14
+ ## Syntax
15
+
16
+ Use the `@` symbol followed by the path to the file you want to import:
17
+
18
+ ```markdown
19
+ # Main GEMINI.md file
20
+
21
+ This is the main content.
22
+
23
+ @./components/instructions.md
24
+
25
+ More content here.
26
+
27
+ @./shared/configuration.md
28
+ ```
29
+
30
+ ## Supported path formats
31
+
32
+ ### Relative paths
33
+
34
+ - `@./file.md` - Import from the same directory
35
+ - `@../file.md` - Import from parent directory
36
+ - `@./components/file.md` - Import from subdirectory
37
+
38
+ ### Absolute paths
39
+
40
+ - `@/absolute/path/to/file.md` - Import using absolute path
41
+
42
+ ## Examples
43
+
44
+ ### Basic import
45
+
46
+ ```markdown
47
+ # My GEMINI.md
48
+
49
+ Welcome to my project!
50
+
51
+ @./get-started.md
52
+
53
+ ## Features
54
+
55
+ @./features/overview.md
56
+ ```
57
+
58
+ ### Nested imports
59
+
60
+ The imported files can themselves contain imports, creating a nested structure:
61
+
62
+ ```markdown
63
+ # main.md
64
+
65
+ @./header.md @./content.md @./footer.md
66
+ ```
67
+
68
+ ```markdown
69
+ # header.md
70
+
71
+ # Project Header
72
+
73
+ @./shared/title.md
74
+ ```
75
+
76
+ ## Safety features
77
+
78
+ ### Circular import detection
79
+
80
+ The processor automatically detects and prevents circular imports:
81
+
82
+ ```markdown
83
+ # file-a.md
84
+
85
+ @./file-b.md
86
+
87
+ # file-b.md
88
+
89
+ @./file-a.md <!-- This will be detected and prevented -->
90
+ ```
91
+
92
+ ### File access security
93
+
94
+ The `validateImportPath` function ensures that imports are only allowed from
95
+ specified directories, preventing access to sensitive files outside the allowed
96
+ scope.
97
+
98
+ ### Maximum import depth
99
+
100
+ To prevent infinite recursion, there's a configurable maximum import depth
101
+ (default: 5 levels).
102
+
103
+ ## Error handling
104
+
105
+ ### Missing files
106
+
107
+ If a referenced file doesn't exist, the import will fail gracefully with an
108
+ error comment in the output.
109
+
110
+ ### File access errors
111
+
112
+ Permission issues or other file system errors are handled gracefully with
113
+ appropriate error messages.
114
+
115
+ ## Code region detection
116
+
117
+ The import processor uses the `marked` library to detect code blocks and inline
118
+ code spans, ensuring that `@` imports inside these regions are properly ignored.
119
+ This provides robust handling of nested code blocks and complex Markdown
120
+ structures.
121
+
122
+ ## Import tree structure
123
+
124
+ The processor returns an import tree that shows the hierarchy of imported files,
125
+ similar to Claude's `/memory` feature. This helps users debug problems with
126
+ their GEMINI.md files by showing which files were read and their import
127
+ relationships.
128
+
129
+ Example tree structure:
130
+
131
+ ```
132
+ Memory Files
133
+ L project: GEMINI.md
134
+ L a.md
135
+ L b.md
136
+ L c.md
137
+ L d.md
138
+ L e.md
139
+ L f.md
140
+ L included.md
141
+ ```
142
+
143
+ The tree preserves the order that files were imported and shows the complete
144
+ import chain for debugging purposes.
145
+
146
+ ## Comparison to Claude Code's `/memory` (`claude.md`) approach
147
+
148
+ Claude Code's `/memory` feature (as seen in `claude.md`) produces a flat, linear
149
+ document by concatenating all included files, always marking file boundaries
150
+ with clear comments and path names. It does not explicitly present the import
151
+ hierarchy, but the LLM receives all file contents and paths, which is sufficient
152
+ for reconstructing the hierarchy if needed.
153
+
154
+ > [!NOTE] The import tree is mainly for clarity during development and has
155
+ > limited relevance to LLM consumption.
156
+
157
+ ## API reference
158
+
159
+ ### `processImports(content, basePath, debugMode?, importState?)`
160
+
161
+ Processes import statements in GEMINI.md content.
162
+
163
+ **Parameters:**
164
+
165
+ - `content` (string): The content to process for imports
166
+ - `basePath` (string): The directory path where the current file is located
167
+ - `debugMode` (boolean, optional): Whether to enable debug logging (default:
168
+ false)
169
+ - `importState` (ImportState, optional): State tracking for circular import
170
+ prevention
171
+
172
+ **Returns:** Promise&lt;ProcessImportsResult&gt; - Object containing processed
173
+ content and import tree
174
+
175
+ ### `ProcessImportsResult`
176
+
177
+ ```typescript
178
+ interface ProcessImportsResult {
179
+ content: string; // The processed content with imports resolved
180
+ importTree: MemoryFile; // Tree structure showing the import hierarchy
181
+ }
182
+ ```
183
+
184
+ ### `MemoryFile`
185
+
186
+ ```typescript
187
+ interface MemoryFile {
188
+ path: string; // The file path
189
+ imports?: MemoryFile[]; // Direct imports, in the order they were imported
190
+ }
191
+ ```
192
+
193
+ ### `validateImportPath(importPath, basePath, allowedDirectories)`
194
+
195
+ Validates import paths to ensure they are safe and within allowed directories.
196
+
197
+ **Parameters:**
198
+
199
+ - `importPath` (string): The import path to validate
200
+ - `basePath` (string): The base directory for resolving relative paths
201
+ - `allowedDirectories` (string[]): Array of allowed directory paths
202
+
203
+ **Returns:** boolean - Whether the import path is valid
204
+
205
+ ### `findProjectRoot(startDir)`
206
+
207
+ Finds the project root by searching for a `.git` directory upwards from the
208
+ given start directory. Implemented as an **async** function using non-blocking
209
+ file system APIs to avoid blocking the Node.js event loop.
210
+
211
+ **Parameters:**
212
+
213
+ - `startDir` (string): The directory to start searching from
214
+
215
+ **Returns:** Promise&lt;string&gt; - The project root directory (or the start
216
+ directory if no `.git` is found)
217
+
218
+ ## Best Practices
219
+
220
+ 1. **Use descriptive file names** for imported components
221
+ 2. **Keep imports shallow** - avoid deeply nested import chains
222
+ 3. **Document your structure** - maintain a clear hierarchy of imported files
223
+ 4. **Test your imports** - ensure all referenced files exist and are accessible
224
+ 5. **Use relative paths** when possible for better portability
225
+
226
+ ## Troubleshooting
227
+
228
+ ### Common issues
229
+
230
+ 1. **Import not working**: Check that the file exists and the path is correct
231
+ 2. **Circular import warnings**: Review your import structure for circular
232
+ references
233
+ 3. **Permission errors**: Ensure the files are readable and within allowed
234
+ directories
235
+ 4. **Path resolution issues**: Use absolute paths if relative paths aren't
236
+ resolving correctly
237
+
238
+ ### Debug mode
239
+
240
+ Enable debug mode to see detailed logging of the import process:
241
+
242
+ ```typescript
243
+ const result = await processImports(content, basePath, true);
244
+ ```
@@ -0,0 +1,267 @@
1
+ # Policy engine
2
+
3
+ The Gemini CLI includes a powerful policy engine that provides fine-grained
4
+ control over tool execution. It allows users and administrators to define rules
5
+ that determine whether a tool call should be allowed, denied, or require user
6
+ confirmation.
7
+
8
+ ## Quick start
9
+
10
+ To create your first policy:
11
+
12
+ 1. **Create the policy directory** if it doesn't exist:
13
+ ```bash
14
+ mkdir -p ~/.gemini/policies
15
+ ```
16
+ 2. **Create a new policy file** (e.g., `~/.gemini/policies/my-rules.toml`). You
17
+ can use any filename ending in `.toml`; all such files in this directory
18
+ will be loaded and combined:
19
+ ```toml
20
+ [[rule]]
21
+ toolName = "run_shell_command"
22
+ commandPrefix = "git status"
23
+ decision = "allow"
24
+ priority = 100
25
+ ```
26
+ 3. **Run a command** that triggers the policy (e.g., ask Gemini CLI to
27
+ `git status`). The tool will now execute automatically without prompting for
28
+ confirmation.
29
+
30
+ ## Core concepts
31
+
32
+ The policy engine operates on a set of rules. Each rule is a combination of
33
+ conditions and a resulting decision. When a large language model wants to
34
+ execute a tool, the policy engine evaluates all rules to find the
35
+ highest-priority rule that matches the tool call.
36
+
37
+ A rule consists of the following main components:
38
+
39
+ - **Conditions**: Criteria that a tool call must meet for the rule to apply.
40
+ This can include the tool's name, the arguments provided to it, or the current
41
+ approval mode.
42
+ - **Decision**: The action to take if the rule matches (`allow`, `deny`, or
43
+ `ask_user`).
44
+ - **Priority**: A number that determines the rule's precedence. Higher numbers
45
+ win.
46
+
47
+ For example, this rule will ask for user confirmation before executing any `git`
48
+ command.
49
+
50
+ ```toml
51
+ [[rule]]
52
+ toolName = "run_shell_command"
53
+ commandPrefix = "git "
54
+ decision = "ask_user"
55
+ priority = 100
56
+ ```
57
+
58
+ ### Conditions
59
+
60
+ Conditions are the criteria that a tool call must meet for a rule to apply. The
61
+ primary conditions are the tool's name and its arguments.
62
+
63
+ #### Tool Name
64
+
65
+ The `toolName` in the rule must match the name of the tool being called.
66
+
67
+ - **Wildcards**: For Model-hosting-protocol (MCP) servers, you can use a
68
+ wildcard. A `toolName` of `my-server__*` will match any tool from the
69
+ `my-server` MCP.
70
+
71
+ #### Arguments pattern
72
+
73
+ If `argsPattern` is specified, the tool's arguments are converted to a stable
74
+ JSON string, which is then tested against the provided regular expression. If
75
+ the arguments don't match the pattern, the rule does not apply.
76
+
77
+ ### Decisions
78
+
79
+ There are three possible decisions a rule can enforce:
80
+
81
+ - `allow`: The tool call is executed automatically without user interaction.
82
+ - `deny`: The tool call is blocked and is not executed.
83
+ - `ask_user`: The user is prompted to approve or deny the tool call. (In
84
+ non-interactive mode, this is treated as `deny`.)
85
+
86
+ ### Priority system and tiers
87
+
88
+ The policy engine uses a sophisticated priority system to resolve conflicts when
89
+ multiple rules match a single tool call. The core principle is simple: **the
90
+ rule with the highest priority wins**.
91
+
92
+ To provide a clear hierarchy, policies are organized into three tiers. Each tier
93
+ has a designated number that forms the base of the final priority calculation.
94
+
95
+ | Tier | Base | Description |
96
+ | :------ | :--- | :------------------------------------------------------------------------- |
97
+ | Default | 1 | Built-in policies that ship with the Gemini CLI. |
98
+ | User | 2 | Custom policies defined by the user. |
99
+ | Admin | 3 | Policies managed by an administrator (e.g., in an enterprise environment). |
100
+
101
+ Within a TOML policy file, you assign a priority value from **0 to 999**. The
102
+ engine transforms this into a final priority using the following formula:
103
+
104
+ `final_priority = tier_base + (toml_priority / 1000)`
105
+
106
+ This system guarantees that:
107
+
108
+ - Admin policies always override User and Default policies.
109
+ - User policies always override Default policies.
110
+ - You can still order rules within a single tier with fine-grained control.
111
+
112
+ For example:
113
+
114
+ - A `priority: 50` rule in a Default policy file becomes `1.050`.
115
+ - A `priority: 100` rule in a User policy file becomes `2.100`.
116
+ - A `priority: 20` rule in an Admin policy file becomes `3.020`.
117
+
118
+ ### Approval modes
119
+
120
+ Approval modes allow the policy engine to apply different sets of rules based on
121
+ the CLI's operational mode. A rule can be associated with one or more modes
122
+ (e.g., `yolo`, `autoEdit`). The rule will only be active if the CLI is running
123
+ in one of its specified modes. If a rule has no modes specified, it is always
124
+ active.
125
+
126
+ ## Rule matching
127
+
128
+ When a tool call is made, the engine checks it against all active rules,
129
+ starting from the highest priority. The first rule that matches determines the
130
+ outcome.
131
+
132
+ A rule matches a tool call if all of its conditions are met:
133
+
134
+ 1. **Tool name**: The `toolName` in the rule must match the name of the tool
135
+ being called.
136
+ - **Wildcards**: For Model-hosting-protocol (MCP) servers, you can use a
137
+ wildcard. A `toolName` of `my-server__*` will match any tool from the
138
+ `my-server` MCP.
139
+ 2. **Arguments pattern**: If `argsPattern` is specified, the tool's arguments
140
+ are converted to a stable JSON string, which is then tested against the
141
+ provided regular expression. If the arguments don't match the pattern, the
142
+ rule does not apply.
143
+
144
+ ## Configuration
145
+
146
+ Policies are defined in `.toml` files. The CLI loads these files from Default,
147
+ User, and (if configured) Admin directories.
148
+
149
+ ### TOML rule schema
150
+
151
+ Here is a breakdown of the fields available in a TOML policy rule:
152
+
153
+ ```toml
154
+ [[rule]]
155
+ # A unique name for the tool, or an array of names.
156
+ toolName = "run_shell_command"
157
+
158
+ # (Optional) The name of an MCP server. Can be combined with toolName
159
+ # to form a composite name like "mcpName__toolName".
160
+ mcpName = "my-custom-server"
161
+
162
+ # (Optional) A regex to match against the tool's arguments.
163
+ argsPattern = '"command":"(git|npm)'
164
+
165
+ # (Optional) A string or array of strings that a shell command must start with.
166
+ # This is syntactic sugar for `toolName = "run_shell_command"` and an `argsPattern`.
167
+ commandPrefix = "git "
168
+
169
+ # (Optional) A regex to match against the entire shell command.
170
+ # This is also syntactic sugar for `toolName = "run_shell_command"`.
171
+ # Note: This pattern is tested against the JSON representation of the arguments (e.g., `{"command":"<your_command>"}`), so anchors like `^` or `$` will apply to the full JSON string, not just the command text.
172
+ # You cannot use commandPrefix and commandRegex in the same rule.
173
+ commandRegex = "^git (commit|push)"
174
+
175
+ # The decision to take. Must be "allow", "deny", or "ask_user".
176
+ decision = "ask_user"
177
+
178
+ # The priority of the rule, from 0 to 999.
179
+ priority = 10
180
+
181
+ # (Optional) An array of approval modes where this rule is active.
182
+ modes = ["autoEdit"]
183
+ ```
184
+
185
+ ### Using arrays (lists)
186
+
187
+ To apply the same rule to multiple tools or command prefixes, you can provide an
188
+ array of strings for the `toolName` and `commandPrefix` fields.
189
+
190
+ **Example:**
191
+
192
+ This single rule will apply to both the `write_file` and `replace` tools.
193
+
194
+ ```toml
195
+ [[rule]]
196
+ toolName = ["write_file", "replace"]
197
+ decision = "ask_user"
198
+ priority = 10
199
+ ```
200
+
201
+ ### Special syntax for `run_shell_command`
202
+
203
+ To simplify writing policies for `run_shell_command`, you can use
204
+ `commandPrefix` or `commandRegex` instead of the more complex `argsPattern`.
205
+
206
+ - `commandPrefix`: Matches if the `command` argument starts with the given
207
+ string.
208
+ - `commandRegex`: Matches if the `command` argument matches the given regular
209
+ expression.
210
+
211
+ **Example:**
212
+
213
+ This rule will ask for user confirmation before executing any `git` command.
214
+
215
+ ```toml
216
+ [[rule]]
217
+ toolName = "run_shell_command"
218
+ commandPrefix = "git "
219
+ decision = "ask_user"
220
+ priority = 100
221
+ ```
222
+
223
+ ### Special syntax for MCP tools
224
+
225
+ You can create rules that target tools from Model-hosting-protocol (MCP) servers
226
+ using the `mcpName` field or a wildcard pattern.
227
+
228
+ **1. Using `mcpName`**
229
+
230
+ To target a specific tool from a specific server, combine `mcpName` and
231
+ `toolName`.
232
+
233
+ ```toml
234
+ # Allows the `search` tool on the `my-jira-server` MCP
235
+ [[rule]]
236
+ mcpName = "my-jira-server"
237
+ toolName = "search"
238
+ decision = "allow"
239
+ priority = 200
240
+ ```
241
+
242
+ **2. Using a wildcard**
243
+
244
+ To create a rule that applies to _all_ tools on a specific MCP server, specify
245
+ only the `mcpName`.
246
+
247
+ ```toml
248
+ # Denies all tools from the `untrusted-server` MCP
249
+ [[rule]]
250
+ mcpName = "untrusted-server"
251
+ decision = "deny"
252
+ priority = 500
253
+ ```
254
+
255
+ ## Default policies
256
+
257
+ The Gemini CLI ships with a set of default policies to provide a safe
258
+ out-of-the-box experience.
259
+
260
+ - **Read-only tools** (like `read_file`, `glob`) are generally **allowed**.
261
+ - **Agent delegation** (like `delegate_to_agent`) is **allowed** (sub-agent
262
+ actions are checked individually).
263
+ - **Write tools** (like `write_file`, `run_shell_command`) default to
264
+ **`ask_user`**.
265
+ - In **`yolo`** mode, a high-priority rule allows all tools.
266
+ - In **`autoEdit`** mode, rules allow certain write operations to happen without
267
+ prompting.
@@ -0,0 +1,131 @@
1
+ # Gemini CLI core: Tools API
2
+
3
+ The Gemini CLI core (`packages/core`) features a robust system for defining,
4
+ registering, and executing tools. These tools extend the capabilities of the
5
+ Gemini model, allowing it to interact with the local environment, fetch web
6
+ content, and perform various actions beyond simple text generation.
7
+
8
+ ## Core concepts
9
+
10
+ - **Tool (`tools.ts`):** An interface and base class (`BaseTool`) that defines
11
+ the contract for all tools. Each tool must have:
12
+ - `name`: A unique internal name (used in API calls to Gemini).
13
+ - `displayName`: A user-friendly name.
14
+ - `description`: A clear explanation of what the tool does, which is provided
15
+ to the Gemini model.
16
+ - `parameterSchema`: A JSON schema defining the parameters that the tool
17
+ accepts. This is crucial for the Gemini model to understand how to call the
18
+ tool correctly.
19
+ - `validateToolParams()`: A method to validate incoming parameters.
20
+ - `getDescription()`: A method to provide a human-readable description of what
21
+ the tool will do with specific parameters before execution.
22
+ - `shouldConfirmExecute()`: A method to determine if user confirmation is
23
+ required before execution (e.g., for potentially destructive operations).
24
+ - `execute()`: The core method that performs the tool's action and returns a
25
+ `ToolResult`.
26
+
27
+ - **`ToolResult` (`tools.ts`):** An interface defining the structure of a tool's
28
+ execution outcome:
29
+ - `llmContent`: The factual content to be included in the history sent back to
30
+ the LLM for context. This can be a simple string or a `PartListUnion` (an
31
+ array of `Part` objects and strings) for rich content.
32
+ - `returnDisplay`: A user-friendly string (often Markdown) or a special object
33
+ (like `FileDiff`) for display in the CLI.
34
+
35
+ - **Returning rich content:** Tools are not limited to returning simple text.
36
+ The `llmContent` can be a `PartListUnion`, which is an array that can contain
37
+ a mix of `Part` objects (for images, audio, etc.) and `string`s. This allows a
38
+ single tool execution to return multiple pieces of rich content.
39
+
40
+ - **Tool registry (`tool-registry.ts`):** A class (`ToolRegistry`) responsible
41
+ for:
42
+ - **Registering tools:** Holding a collection of all available built-in tools
43
+ (e.g., `ReadFileTool`, `ShellTool`).
44
+ - **Discovering tools:** It can also discover tools dynamically:
45
+ - **Command-based discovery:** If `tools.discoveryCommand` is configured in
46
+ settings, this command is executed. It's expected to output JSON
47
+ describing custom tools, which are then registered as `DiscoveredTool`
48
+ instances.
49
+ - **MCP-based discovery:** If `mcp.serverCommand` is configured, the
50
+ registry can connect to a Model Context Protocol (MCP) server to list and
51
+ register tools (`DiscoveredMCPTool`).
52
+ - **Providing schemas:** Exposing the `FunctionDeclaration` schemas of all
53
+ registered tools to the Gemini model, so it knows what tools are available
54
+ and how to use them.
55
+ - **Retrieving tools:** Allowing the core to get a specific tool by name for
56
+ execution.
57
+
58
+ ## Built-in tools
59
+
60
+ The core comes with a suite of pre-defined tools, typically found in
61
+ `packages/core/src/tools/`. These include:
62
+
63
+ - **File system tools:**
64
+ - `LSTool` (`ls.ts`): Lists directory contents.
65
+ - `ReadFileTool` (`read-file.ts`): Reads the content of a single file.
66
+ - `WriteFileTool` (`write-file.ts`): Writes content to a file.
67
+ - `GrepTool` (`grep.ts`): Searches for patterns in files.
68
+ - `GlobTool` (`glob.ts`): Finds files matching glob patterns.
69
+ - `EditTool` (`edit.ts`): Performs in-place modifications to files (often
70
+ requiring confirmation).
71
+ - `ReadManyFilesTool` (`read-many-files.ts`): Reads and concatenates content
72
+ from multiple files or glob patterns (used by the `@` command in CLI).
73
+ - **Execution tools:**
74
+ - `ShellTool` (`shell.ts`): Executes arbitrary shell commands (requires
75
+ careful sandboxing and user confirmation).
76
+ - **Web tools:**
77
+ - `WebFetchTool` (`web-fetch.ts`): Fetches content from a URL.
78
+ - `WebSearchTool` (`web-search.ts`): Performs a web search.
79
+ - **Memory tools:**
80
+ - `MemoryTool` (`memoryTool.ts`): Interacts with the AI's memory.
81
+
82
+ Each of these tools extends `BaseTool` and implements the required methods for
83
+ its specific functionality.
84
+
85
+ ## Tool execution flow
86
+
87
+ 1. **Model request:** The Gemini model, based on the user's prompt and the
88
+ provided tool schemas, decides to use a tool and returns a `FunctionCall`
89
+ part in its response, specifying the tool name and arguments.
90
+ 2. **Core receives request:** The core parses this `FunctionCall`.
91
+ 3. **Tool retrieval:** It looks up the requested tool in the `ToolRegistry`.
92
+ 4. **Parameter validation:** The tool's `validateToolParams()` method is
93
+ called.
94
+ 5. **Confirmation (if needed):**
95
+ - The tool's `shouldConfirmExecute()` method is called.
96
+ - If it returns details for confirmation, the core communicates this back to
97
+ the CLI, which prompts the user.
98
+ - The user's decision (e.g., proceed, cancel) is sent back to the core.
99
+ 6. **Execution:** If validated and confirmed (or if no confirmation is needed),
100
+ the core calls the tool's `execute()` method with the provided arguments and
101
+ an `AbortSignal` (for potential cancellation).
102
+ 7. **Result processing:** The `ToolResult` from `execute()` is received by the
103
+ core.
104
+ 8. **Response to model:** The `llmContent` from the `ToolResult` is packaged as
105
+ a `FunctionResponse` and sent back to the Gemini model so it can continue
106
+ generating a user-facing response.
107
+ 9. **Display to user:** The `returnDisplay` from the `ToolResult` is sent to
108
+ the CLI to show the user what the tool did.
109
+
110
+ ## Extending with custom tools
111
+
112
+ While direct programmatic registration of new tools by users isn't explicitly
113
+ detailed as a primary workflow in the provided files for typical end-users, the
114
+ architecture supports extension through:
115
+
116
+ - **Command-based discovery:** Advanced users or project administrators can
117
+ define a `tools.discoveryCommand` in `settings.json`. This command, when run
118
+ by the Gemini CLI core, should output a JSON array of `FunctionDeclaration`
119
+ objects. The core will then make these available as `DiscoveredTool`
120
+ instances. The corresponding `tools.callCommand` would then be responsible for
121
+ actually executing these custom tools.
122
+ - **MCP server(s):** For more complex scenarios, one or more MCP servers can be
123
+ set up and configured via the `mcpServers` setting in `settings.json`. The
124
+ Gemini CLI core can then discover and use tools exposed by these servers. As
125
+ mentioned, if you have multiple MCP servers, the tool names will be prefixed
126
+ with the server name from your configuration (e.g.,
127
+ `serverAlias__actualToolName`).
128
+
129
+ This tool system provides a flexible and powerful way to augment the Gemini
130
+ model's capabilities, making the Gemini CLI a versatile assistant for a wide
131
+ range of tasks.