@google/gemini-cli-core 0.1.13 → 0.1.15

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 (234) hide show
  1. package/README.md +22 -1
  2. package/dist/google-gemini-cli-core-0.1.13.tgz +0 -0
  3. package/dist/src/code_assist/codeAssist.js +2 -2
  4. package/dist/src/code_assist/codeAssist.js.map +1 -1
  5. package/dist/src/code_assist/oauth2.js +9 -2
  6. package/dist/src/code_assist/oauth2.js.map +1 -1
  7. package/dist/src/code_assist/oauth2.test.js +99 -7
  8. package/dist/src/code_assist/oauth2.test.js.map +1 -1
  9. package/dist/src/code_assist/server.d.ts +4 -6
  10. package/dist/src/code_assist/server.js +4 -69
  11. package/dist/src/code_assist/server.js.map +1 -1
  12. package/dist/src/code_assist/setup.d.ts +6 -1
  13. package/dist/src/code_assist/setup.js +4 -1
  14. package/dist/src/code_assist/setup.js.map +1 -1
  15. package/dist/src/code_assist/setup.test.js +4 -1
  16. package/dist/src/code_assist/setup.test.js.map +1 -1
  17. package/dist/src/code_assist/types.d.ts +2 -2
  18. package/dist/src/config/config.d.ts +28 -7
  19. package/dist/src/config/config.js +52 -16
  20. package/dist/src/config/config.js.map +1 -1
  21. package/dist/src/config/config.test.js +1 -23
  22. package/dist/src/config/config.test.js.map +1 -1
  23. package/dist/src/config/flashFallback.test.js +1 -1
  24. package/dist/src/config/flashFallback.test.js.map +1 -1
  25. package/dist/src/core/client.d.ts +5 -2
  26. package/dist/src/core/client.js +39 -17
  27. package/dist/src/core/client.js.map +1 -1
  28. package/dist/src/core/client.test.js +51 -0
  29. package/dist/src/core/client.test.js.map +1 -1
  30. package/dist/src/core/contentGenerator.d.ts +1 -1
  31. package/dist/src/core/contentGenerator.js +1 -1
  32. package/dist/src/core/contentGenerator.js.map +1 -1
  33. package/dist/src/core/geminiChat.d.ts +4 -3
  34. package/dist/src/core/geminiChat.js +8 -11
  35. package/dist/src/core/geminiChat.js.map +1 -1
  36. package/dist/src/core/geminiRequest.js +2 -37
  37. package/dist/src/core/geminiRequest.js.map +1 -1
  38. package/dist/src/core/logger.js +6 -0
  39. package/dist/src/core/logger.js.map +1 -1
  40. package/dist/src/core/logger.test.js +1 -1
  41. package/dist/src/core/logger.test.js.map +1 -1
  42. package/dist/src/core/nonInteractiveToolExecutor.test.js +5 -5
  43. package/dist/src/core/prompts.js +42 -18
  44. package/dist/src/core/prompts.js.map +1 -1
  45. package/dist/src/core/prompts.test.js +121 -4
  46. package/dist/src/core/prompts.test.js.map +1 -1
  47. package/dist/src/core/turn.d.ts +7 -2
  48. package/dist/src/core/turn.js +9 -0
  49. package/dist/src/core/turn.js.map +1 -1
  50. package/dist/src/core/turn.test.js +129 -0
  51. package/dist/src/core/turn.test.js.map +1 -1
  52. package/dist/src/ide/ide-client.d.ts +28 -0
  53. package/dist/src/ide/ide-client.js +88 -0
  54. package/dist/src/ide/ide-client.js.map +1 -0
  55. package/dist/src/ide/ideContext.d.ts +174 -0
  56. package/dist/src/{services → ide}/ideContext.js +28 -25
  57. package/dist/src/ide/ideContext.js.map +1 -0
  58. package/dist/src/{services → ide}/ideContext.test.js +39 -39
  59. package/dist/src/ide/ideContext.test.js.map +1 -0
  60. package/dist/src/index.d.ts +8 -1
  61. package/dist/src/index.js +11 -1
  62. package/dist/src/index.js.map +1 -1
  63. package/dist/src/mcp/google-auth-provider.d.ts +23 -0
  64. package/dist/src/mcp/google-auth-provider.js +63 -0
  65. package/dist/src/mcp/google-auth-provider.js.map +1 -0
  66. package/dist/src/mcp/google-auth-provider.test.js +54 -0
  67. package/dist/src/mcp/google-auth-provider.test.js.map +1 -0
  68. package/dist/src/mcp/oauth-provider.d.ts +5 -1
  69. package/dist/src/mcp/oauth-provider.js +36 -11
  70. package/dist/src/mcp/oauth-provider.js.map +1 -1
  71. package/dist/src/mcp/oauth-provider.test.js +2 -2
  72. package/dist/src/mcp/oauth-provider.test.js.map +1 -1
  73. package/dist/src/mcp/oauth-token-storage.d.ts +3 -1
  74. package/dist/src/mcp/oauth-token-storage.js +3 -1
  75. package/dist/src/mcp/oauth-token-storage.js.map +1 -1
  76. package/dist/src/prompts/mcp-prompts.d.ts +8 -0
  77. package/dist/src/prompts/mcp-prompts.js +13 -0
  78. package/dist/src/prompts/mcp-prompts.js.map +1 -0
  79. package/dist/src/prompts/prompt-registry.d.ts +26 -0
  80. package/dist/src/prompts/prompt-registry.js +47 -0
  81. package/dist/src/prompts/prompt-registry.js.map +1 -0
  82. package/dist/src/services/fileDiscoveryService.test.js +101 -60
  83. package/dist/src/services/fileDiscoveryService.test.js.map +1 -1
  84. package/dist/src/services/gitService.test.js +67 -86
  85. package/dist/src/services/gitService.test.js.map +1 -1
  86. package/dist/src/services/loopDetectionService.d.ts +48 -5
  87. package/dist/src/services/loopDetectionService.js +124 -38
  88. package/dist/src/services/loopDetectionService.js.map +1 -1
  89. package/dist/src/services/loopDetectionService.test.js +39 -112
  90. package/dist/src/services/loopDetectionService.test.js.map +1 -1
  91. package/dist/src/services/shellExecutionService.d.ts +70 -0
  92. package/dist/src/services/shellExecutionService.js +152 -0
  93. package/dist/src/services/shellExecutionService.js.map +1 -0
  94. package/dist/src/services/shellExecutionService.test.d.ts +6 -0
  95. package/dist/src/services/shellExecutionService.test.js +258 -0
  96. package/dist/src/services/shellExecutionService.test.js.map +1 -0
  97. package/dist/src/telemetry/clearcut-logger/clearcut-logger.d.ts +2 -1
  98. package/dist/src/telemetry/clearcut-logger/clearcut-logger.js +17 -2
  99. package/dist/src/telemetry/clearcut-logger/clearcut-logger.js.map +1 -1
  100. package/dist/src/telemetry/constants.d.ts +1 -0
  101. package/dist/src/telemetry/constants.js +1 -0
  102. package/dist/src/telemetry/constants.js.map +1 -1
  103. package/dist/src/telemetry/file-exporters.d.ts +28 -0
  104. package/dist/src/telemetry/file-exporters.js +62 -0
  105. package/dist/src/telemetry/file-exporters.js.map +1 -0
  106. package/dist/src/telemetry/loggers.d.ts +2 -1
  107. package/dist/src/telemetry/loggers.js +17 -1
  108. package/dist/src/telemetry/loggers.js.map +1 -1
  109. package/dist/src/telemetry/sdk.js +17 -6
  110. package/dist/src/telemetry/sdk.js.map +1 -1
  111. package/dist/src/telemetry/types.d.ts +9 -2
  112. package/dist/src/telemetry/types.js +13 -1
  113. package/dist/src/telemetry/types.js.map +1 -1
  114. package/dist/src/tools/edit.js +10 -4
  115. package/dist/src/tools/edit.js.map +1 -1
  116. package/dist/src/tools/edit.test.js +12 -0
  117. package/dist/src/tools/edit.test.js.map +1 -1
  118. package/dist/src/tools/glob.test.js +7 -4
  119. package/dist/src/tools/glob.test.js.map +1 -1
  120. package/dist/src/tools/grep.test.js +5 -5
  121. package/dist/src/tools/grep.test.js.map +1 -1
  122. package/dist/src/tools/ls.d.ts +5 -2
  123. package/dist/src/tools/ls.js +39 -10
  124. package/dist/src/tools/ls.js.map +1 -1
  125. package/dist/src/tools/mcp-client.d.ts +31 -3
  126. package/dist/src/tools/mcp-client.js +478 -38
  127. package/dist/src/tools/mcp-client.js.map +1 -1
  128. package/dist/src/tools/mcp-client.test.js +99 -7
  129. package/dist/src/tools/mcp-client.test.js.map +1 -1
  130. package/dist/src/tools/mcp-tool.js +1 -1
  131. package/dist/src/tools/mcp-tool.js.map +1 -1
  132. package/dist/src/tools/mcp-tool.test.js +34 -0
  133. package/dist/src/tools/mcp-tool.test.js.map +1 -1
  134. package/dist/src/tools/modifiable-tool.test.js +51 -62
  135. package/dist/src/tools/modifiable-tool.test.js.map +1 -1
  136. package/dist/src/tools/read-file.test.js +98 -69
  137. package/dist/src/tools/read-file.test.js.map +1 -1
  138. package/dist/src/tools/read-many-files.d.ts +5 -3
  139. package/dist/src/tools/read-many-files.js +62 -22
  140. package/dist/src/tools/read-many-files.js.map +1 -1
  141. package/dist/src/tools/read-many-files.test.js +5 -2
  142. package/dist/src/tools/read-many-files.test.js.map +1 -1
  143. package/dist/src/tools/shell.d.ts +3 -23
  144. package/dist/src/tools/shell.js +165 -296
  145. package/dist/src/tools/shell.js.map +1 -1
  146. package/dist/src/tools/shell.test.js +254 -392
  147. package/dist/src/tools/shell.test.js.map +1 -1
  148. package/dist/src/tools/tool-registry.d.ts +13 -1
  149. package/dist/src/tools/tool-registry.js +46 -2
  150. package/dist/src/tools/tool-registry.js.map +1 -1
  151. package/dist/src/tools/tool-registry.test.js +5 -5
  152. package/dist/src/tools/tool-registry.test.js.map +1 -1
  153. package/dist/src/utils/bfsFileSearch.d.ts +2 -0
  154. package/dist/src/utils/bfsFileSearch.js +4 -1
  155. package/dist/src/utils/bfsFileSearch.js.map +1 -1
  156. package/dist/src/utils/bfsFileSearch.test.js +108 -105
  157. package/dist/src/utils/bfsFileSearch.test.js.map +1 -1
  158. package/dist/src/utils/editCorrector.js +4 -4
  159. package/dist/src/utils/editCorrector.js.map +1 -1
  160. package/dist/src/utils/editCorrector.test.js +1 -1
  161. package/dist/src/utils/editor.js +16 -10
  162. package/dist/src/utils/editor.js.map +1 -1
  163. package/dist/src/utils/editor.test.js +128 -28
  164. package/dist/src/utils/editor.test.js.map +1 -1
  165. package/dist/src/utils/errorReporting.d.ts +1 -1
  166. package/dist/src/utils/errorReporting.js +2 -2
  167. package/dist/src/utils/errorReporting.js.map +1 -1
  168. package/dist/src/utils/errorReporting.test.js +44 -38
  169. package/dist/src/utils/errorReporting.test.js.map +1 -1
  170. package/dist/src/utils/fileUtils.d.ts +4 -4
  171. package/dist/src/utils/fileUtils.js +31 -15
  172. package/dist/src/utils/fileUtils.js.map +1 -1
  173. package/dist/src/utils/fileUtils.test.js +37 -37
  174. package/dist/src/utils/fileUtils.test.js.map +1 -1
  175. package/dist/src/utils/formatters.d.ts +6 -0
  176. package/dist/src/utils/formatters.js +16 -0
  177. package/dist/src/utils/formatters.js.map +1 -0
  178. package/dist/src/utils/getFolderStructure.d.ts +3 -2
  179. package/dist/src/utils/getFolderStructure.js +27 -28
  180. package/dist/src/utils/getFolderStructure.js.map +1 -1
  181. package/dist/src/utils/getFolderStructure.test.js +169 -187
  182. package/dist/src/utils/getFolderStructure.test.js.map +1 -1
  183. package/dist/src/utils/gitIgnoreParser.js +4 -7
  184. package/dist/src/utils/gitIgnoreParser.js.map +1 -1
  185. package/dist/src/utils/gitIgnoreParser.test.js +70 -61
  186. package/dist/src/utils/gitIgnoreParser.test.js.map +1 -1
  187. package/dist/src/utils/memoryDiscovery.d.ts +2 -1
  188. package/dist/src/utils/memoryDiscovery.js +11 -5
  189. package/dist/src/utils/memoryDiscovery.js.map +1 -1
  190. package/dist/src/utils/memoryDiscovery.test.js +160 -371
  191. package/dist/src/utils/memoryDiscovery.test.js.map +1 -1
  192. package/dist/src/utils/partUtils.d.ts +14 -0
  193. package/dist/src/utils/partUtils.js +65 -0
  194. package/dist/src/utils/partUtils.js.map +1 -0
  195. package/dist/src/utils/partUtils.test.d.ts +6 -0
  196. package/dist/src/utils/partUtils.test.js +130 -0
  197. package/dist/src/utils/partUtils.test.js.map +1 -0
  198. package/dist/src/utils/paths.d.ts +11 -0
  199. package/dist/src/utils/paths.js +17 -1
  200. package/dist/src/utils/paths.js.map +1 -1
  201. package/dist/src/utils/quotaErrorDetection.js +0 -2
  202. package/dist/src/utils/quotaErrorDetection.js.map +1 -1
  203. package/dist/src/utils/retry.js +1 -1
  204. package/dist/src/utils/retry.js.map +1 -1
  205. package/dist/src/utils/schemaValidator.d.ts +1 -1
  206. package/dist/src/utils/schemaValidator.js +6 -3
  207. package/dist/src/utils/schemaValidator.js.map +1 -1
  208. package/dist/src/utils/shell-utils.d.ts +78 -0
  209. package/dist/src/utils/shell-utils.js +306 -0
  210. package/dist/src/utils/shell-utils.js.map +1 -0
  211. package/dist/src/utils/shell-utils.test.d.ts +6 -0
  212. package/dist/src/utils/shell-utils.test.js +200 -0
  213. package/dist/src/utils/shell-utils.test.js.map +1 -0
  214. package/dist/src/utils/summarizer.js +1 -30
  215. package/dist/src/utils/summarizer.js.map +1 -1
  216. package/dist/src/utils/systemEncoding.d.ts +40 -0
  217. package/dist/src/utils/systemEncoding.js +149 -0
  218. package/dist/src/utils/systemEncoding.js.map +1 -0
  219. package/dist/src/utils/systemEncoding.test.d.ts +6 -0
  220. package/dist/src/utils/systemEncoding.test.js +368 -0
  221. package/dist/src/utils/systemEncoding.test.js.map +1 -0
  222. package/dist/src/utils/textUtils.d.ts +13 -0
  223. package/dist/src/utils/textUtils.js +28 -0
  224. package/dist/src/utils/textUtils.js.map +1 -0
  225. package/dist/tsconfig.tsbuildinfo +1 -1
  226. package/package.json +2 -1
  227. package/dist/google-gemini-cli-core-0.1.12.tgz +0 -0
  228. package/dist/src/core/geminiRequest.test.js +0 -72
  229. package/dist/src/core/geminiRequest.test.js.map +0 -1
  230. package/dist/src/services/ideContext.d.ts +0 -126
  231. package/dist/src/services/ideContext.js.map +0 -1
  232. package/dist/src/services/ideContext.test.js.map +0 -1
  233. /package/dist/src/{services → ide}/ideContext.test.d.ts +0 -0
  234. /package/dist/src/{core/geminiRequest.test.d.ts → mcp/google-auth-provider.test.d.ts} +0 -0
@@ -0,0 +1,306 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2025 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ /**
7
+ * Splits a shell command into a list of individual commands, respecting quotes.
8
+ * This is used to separate chained commands (e.g., using &&, ||, ;).
9
+ * @param command The shell command string to parse
10
+ * @returns An array of individual command strings
11
+ */
12
+ export function splitCommands(command) {
13
+ const commands = [];
14
+ let currentCommand = '';
15
+ let inSingleQuotes = false;
16
+ let inDoubleQuotes = false;
17
+ let i = 0;
18
+ while (i < command.length) {
19
+ const char = command[i];
20
+ const nextChar = command[i + 1];
21
+ if (char === '\\' && i < command.length - 1) {
22
+ currentCommand += char + command[i + 1];
23
+ i += 2;
24
+ continue;
25
+ }
26
+ if (char === "'" && !inDoubleQuotes) {
27
+ inSingleQuotes = !inSingleQuotes;
28
+ }
29
+ else if (char === '"' && !inSingleQuotes) {
30
+ inDoubleQuotes = !inDoubleQuotes;
31
+ }
32
+ if (!inSingleQuotes && !inDoubleQuotes) {
33
+ if ((char === '&' && nextChar === '&') ||
34
+ (char === '|' && nextChar === '|')) {
35
+ commands.push(currentCommand.trim());
36
+ currentCommand = '';
37
+ i++; // Skip the next character
38
+ }
39
+ else if (char === ';' || char === '&' || char === '|') {
40
+ commands.push(currentCommand.trim());
41
+ currentCommand = '';
42
+ }
43
+ else {
44
+ currentCommand += char;
45
+ }
46
+ }
47
+ else {
48
+ currentCommand += char;
49
+ }
50
+ i++;
51
+ }
52
+ if (currentCommand.trim()) {
53
+ commands.push(currentCommand.trim());
54
+ }
55
+ return commands.filter(Boolean); // Filter out any empty strings
56
+ }
57
+ /**
58
+ * Extracts the root command from a given shell command string.
59
+ * This is used to identify the base command for permission checks.
60
+ * @param command The shell command string to parse
61
+ * @returns The root command name, or undefined if it cannot be determined
62
+ * @example getCommandRoot("ls -la /tmp") returns "ls"
63
+ * @example getCommandRoot("git status && npm test") returns "git"
64
+ */
65
+ export function getCommandRoot(command) {
66
+ const trimmedCommand = command.trim();
67
+ if (!trimmedCommand) {
68
+ return undefined;
69
+ }
70
+ // This regex is designed to find the first "word" of a command,
71
+ // while respecting quotes. It looks for a sequence of non-whitespace
72
+ // characters that are not inside quotes.
73
+ const match = trimmedCommand.match(/^"([^"]+)"|^'([^']+)'|^(\S+)/);
74
+ if (match) {
75
+ // The first element in the match array is the full match.
76
+ // The subsequent elements are the capture groups.
77
+ // We prefer a captured group because it will be unquoted.
78
+ const commandRoot = match[1] || match[2] || match[3];
79
+ if (commandRoot) {
80
+ // If the command is a path, return the last component.
81
+ return commandRoot.split(/[\\/]/).pop();
82
+ }
83
+ }
84
+ return undefined;
85
+ }
86
+ export function getCommandRoots(command) {
87
+ if (!command) {
88
+ return [];
89
+ }
90
+ return splitCommands(command)
91
+ .map((c) => getCommandRoot(c))
92
+ .filter((c) => !!c);
93
+ }
94
+ export function stripShellWrapper(command) {
95
+ const pattern = /^\s*(?:sh|bash|zsh|cmd.exe)\s+(?:\/c|-c)\s+/;
96
+ const match = command.match(pattern);
97
+ if (match) {
98
+ let newCommand = command.substring(match[0].length).trim();
99
+ if ((newCommand.startsWith('"') && newCommand.endsWith('"')) ||
100
+ (newCommand.startsWith("'") && newCommand.endsWith("'"))) {
101
+ newCommand = newCommand.substring(1, newCommand.length - 1);
102
+ }
103
+ return newCommand;
104
+ }
105
+ return command.trim();
106
+ }
107
+ /**
108
+ * Detects command substitution patterns in a shell command, following bash quoting rules:
109
+ * - Single quotes ('): Everything literal, no substitution possible
110
+ * - Double quotes ("): Command substitution with $() and backticks unless escaped with \
111
+ * - No quotes: Command substitution with $(), <(), and backticks
112
+ * @param command The shell command string to check
113
+ * @returns true if command substitution would be executed by bash
114
+ */
115
+ export function detectCommandSubstitution(command) {
116
+ let inSingleQuotes = false;
117
+ let inDoubleQuotes = false;
118
+ let inBackticks = false;
119
+ let i = 0;
120
+ while (i < command.length) {
121
+ const char = command[i];
122
+ const nextChar = command[i + 1];
123
+ // Handle escaping - only works outside single quotes
124
+ if (char === '\\' && !inSingleQuotes) {
125
+ i += 2; // Skip the escaped character
126
+ continue;
127
+ }
128
+ // Handle quote state changes
129
+ if (char === "'" && !inDoubleQuotes && !inBackticks) {
130
+ inSingleQuotes = !inSingleQuotes;
131
+ }
132
+ else if (char === '"' && !inSingleQuotes && !inBackticks) {
133
+ inDoubleQuotes = !inDoubleQuotes;
134
+ }
135
+ else if (char === '`' && !inSingleQuotes) {
136
+ // Backticks work outside single quotes (including in double quotes)
137
+ inBackticks = !inBackticks;
138
+ }
139
+ // Check for command substitution patterns that would be executed
140
+ if (!inSingleQuotes) {
141
+ // $(...) command substitution - works in double quotes and unquoted
142
+ if (char === '$' && nextChar === '(') {
143
+ return true;
144
+ }
145
+ // <(...) process substitution - works unquoted only (not in double quotes)
146
+ if (char === '<' && nextChar === '(' && !inDoubleQuotes && !inBackticks) {
147
+ return true;
148
+ }
149
+ // Backtick command substitution - check for opening backtick
150
+ // (We track the state above, so this catches the start of backtick substitution)
151
+ if (char === '`' && !inBackticks) {
152
+ return true;
153
+ }
154
+ }
155
+ i++;
156
+ }
157
+ return false;
158
+ }
159
+ /**
160
+ * Checks a shell command against security policies and allowlists.
161
+ *
162
+ * This function operates in one of two modes depending on the presence of
163
+ * the `sessionAllowlist` parameter:
164
+ *
165
+ * 1. **"Default Deny" Mode (sessionAllowlist is provided):** This is the
166
+ * strictest mode, used for user-defined scripts like custom commands.
167
+ * A command is only permitted if it is found on the global `coreTools`
168
+ * allowlist OR the provided `sessionAllowlist`. It must not be on the
169
+ * global `excludeTools` blocklist.
170
+ *
171
+ * 2. **"Default Allow" Mode (sessionAllowlist is NOT provided):** This mode
172
+ * is used for direct tool invocations (e.g., by the model). If a strict
173
+ * global `coreTools` allowlist exists, commands must be on it. Otherwise,
174
+ * any command is permitted as long as it is not on the `excludeTools`
175
+ * blocklist.
176
+ *
177
+ * @param command The shell command string to validate.
178
+ * @param config The application configuration.
179
+ * @param sessionAllowlist A session-level list of approved commands. Its
180
+ * presence activates "Default Deny" mode.
181
+ * @returns An object detailing which commands are not allowed.
182
+ */
183
+ export function checkCommandPermissions(command, config, sessionAllowlist) {
184
+ // Disallow command substitution for security.
185
+ if (detectCommandSubstitution(command)) {
186
+ return {
187
+ allAllowed: false,
188
+ disallowedCommands: [command],
189
+ blockReason: 'Command substitution using $(), <(), or >() is not allowed for security reasons',
190
+ isHardDenial: true,
191
+ };
192
+ }
193
+ const SHELL_TOOL_NAMES = ['run_shell_command', 'ShellTool'];
194
+ const normalize = (cmd) => cmd.trim().replace(/\s+/g, ' ');
195
+ const isPrefixedBy = (cmd, prefix) => {
196
+ if (!cmd.startsWith(prefix)) {
197
+ return false;
198
+ }
199
+ return cmd.length === prefix.length || cmd[prefix.length] === ' ';
200
+ };
201
+ const extractCommands = (tools) => tools.flatMap((tool) => {
202
+ for (const toolName of SHELL_TOOL_NAMES) {
203
+ if (tool.startsWith(`${toolName}(`) && tool.endsWith(')')) {
204
+ return [normalize(tool.slice(toolName.length + 1, -1))];
205
+ }
206
+ }
207
+ return [];
208
+ });
209
+ const coreTools = config.getCoreTools() || [];
210
+ const excludeTools = config.getExcludeTools() || [];
211
+ const commandsToValidate = splitCommands(command).map(normalize);
212
+ // 1. Blocklist Check (Highest Priority)
213
+ if (SHELL_TOOL_NAMES.some((name) => excludeTools.includes(name))) {
214
+ return {
215
+ allAllowed: false,
216
+ disallowedCommands: commandsToValidate,
217
+ blockReason: 'Shell tool is globally disabled in configuration',
218
+ isHardDenial: true,
219
+ };
220
+ }
221
+ const blockedCommands = extractCommands(excludeTools);
222
+ for (const cmd of commandsToValidate) {
223
+ if (blockedCommands.some((blocked) => isPrefixedBy(cmd, blocked))) {
224
+ return {
225
+ allAllowed: false,
226
+ disallowedCommands: [cmd],
227
+ blockReason: `Command '${cmd}' is blocked by configuration`,
228
+ isHardDenial: true,
229
+ };
230
+ }
231
+ }
232
+ const globallyAllowedCommands = extractCommands(coreTools);
233
+ const isWildcardAllowed = SHELL_TOOL_NAMES.some((name) => coreTools.includes(name));
234
+ // If there's a global wildcard, all commands are allowed at this point
235
+ // because they have already passed the blocklist check.
236
+ if (isWildcardAllowed) {
237
+ return { allAllowed: true, disallowedCommands: [] };
238
+ }
239
+ if (sessionAllowlist) {
240
+ // "DEFAULT DENY" MODE: A session allowlist is provided.
241
+ // All commands must be in either the session or global allowlist.
242
+ const disallowedCommands = [];
243
+ for (const cmd of commandsToValidate) {
244
+ const isSessionAllowed = [...sessionAllowlist].some((allowed) => isPrefixedBy(cmd, normalize(allowed)));
245
+ if (isSessionAllowed)
246
+ continue;
247
+ const isGloballyAllowed = globallyAllowedCommands.some((allowed) => isPrefixedBy(cmd, allowed));
248
+ if (isGloballyAllowed)
249
+ continue;
250
+ disallowedCommands.push(cmd);
251
+ }
252
+ if (disallowedCommands.length > 0) {
253
+ return {
254
+ allAllowed: false,
255
+ disallowedCommands,
256
+ blockReason: `Command(s) not on the global or session allowlist.`,
257
+ isHardDenial: false, // This is a soft denial; confirmation is possible.
258
+ };
259
+ }
260
+ }
261
+ else {
262
+ // "DEFAULT ALLOW" MODE: No session allowlist.
263
+ const hasSpecificAllowedCommands = globallyAllowedCommands.length > 0;
264
+ if (hasSpecificAllowedCommands) {
265
+ const disallowedCommands = [];
266
+ for (const cmd of commandsToValidate) {
267
+ const isGloballyAllowed = globallyAllowedCommands.some((allowed) => isPrefixedBy(cmd, allowed));
268
+ if (!isGloballyAllowed) {
269
+ disallowedCommands.push(cmd);
270
+ }
271
+ }
272
+ if (disallowedCommands.length > 0) {
273
+ return {
274
+ allAllowed: false,
275
+ disallowedCommands,
276
+ blockReason: `Command(s) not in the allowed commands list.`,
277
+ isHardDenial: false, // This is a soft denial.
278
+ };
279
+ }
280
+ }
281
+ // If no specific global allowlist exists, and it passed the blocklist,
282
+ // the command is allowed by default.
283
+ }
284
+ // If all checks for the current mode pass, the command is allowed.
285
+ return { allAllowed: true, disallowedCommands: [] };
286
+ }
287
+ /**
288
+ * Determines whether a given shell command is allowed to execute based on
289
+ * the tool's configuration including allowlists and blocklists.
290
+ *
291
+ * This function operates in "default allow" mode. It is a wrapper around
292
+ * `checkCommandPermissions`.
293
+ *
294
+ * @param command The shell command string to validate.
295
+ * @param config The application configuration.
296
+ * @returns An object with 'allowed' boolean and optional 'reason' string if not allowed.
297
+ */
298
+ export function isCommandAllowed(command, config) {
299
+ // By not providing a sessionAllowlist, we invoke "default allow" behavior.
300
+ const { allAllowed, blockReason } = checkCommandPermissions(command, config);
301
+ if (allAllowed) {
302
+ return { allowed: true };
303
+ }
304
+ return { allowed: false, reason: blockReason };
305
+ }
306
+ //# sourceMappingURL=shell-utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shell-utils.js","sourceRoot":"","sources":["../../../src/utils/shell-utils.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH;;;;;GAKG;AACH,MAAM,UAAU,aAAa,CAAC,OAAe;IAC3C,MAAM,QAAQ,GAAa,EAAE,CAAC;IAC9B,IAAI,cAAc,GAAG,EAAE,CAAC;IACxB,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A8C;oBAC3D,YAAY,EAAE,KAAK,EAAE,yBAAyB;iBAC/C,CAAC;YACJ,CAAC;QACH,CAAC;QACD,uEAAuE;QACvE,qCAAqC;IACvC,CAAC;IAED,mEAAmE;IACnE,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,kBAAkB,EAAE,EAAE,EAAE,CAAC;AACtD,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,gBAAgB,CAC9B,OAAe,EACf,MAAc;IAEd,2EAA2E;IAC3E,MAAM,EAAE,UAAU,EAAE,WAAW,EAAE,GAAG,uBAAuB,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IAC7E,IAAI,UAAU,EAAE,CAAC;QACf,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;IAC3B,CAAC;IACD,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,CAAC;AACjD,CAAC"}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2025 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ export {};
@@ -0,0 +1,200 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2025 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ import { expect, describe, it, beforeEach } from 'vitest';
7
+ import { checkCommandPermissions, getCommandRoots, isCommandAllowed, stripShellWrapper, } from './shell-utils.js';
8
+ let config;
9
+ beforeEach(() => {
10
+ config = {
11
+ getCoreTools: () => [],
12
+ getExcludeTools: () => [],
13
+ };
14
+ });
15
+ describe('isCommandAllowed', () => {
16
+ it('should allow a command if no restrictions are provided', () => {
17
+ const result = isCommandAllowed('ls -l', config);
18
+ expect(result.allowed).toBe(true);
19
+ });
20
+ it('should allow a command if it is in the global allowlist', () => {
21
+ config.getCoreTools = () => ['ShellTool(ls)'];
22
+ const result = isCommandAllowed('ls -l', config);
23
+ expect(result.allowed).toBe(true);
24
+ });
25
+ it('should block a command if it is not in a strict global allowlist', () => {
26
+ config.getCoreTools = () => ['ShellTool(ls -l)'];
27
+ const result = isCommandAllowed('rm -rf /', config);
28
+ expect(result.allowed).toBe(false);
29
+ expect(result.reason).toBe(`Command(s) not in the allowed commands list.`);
30
+ });
31
+ it('should block a command if it is in the blocked list', () => {
32
+ config.getExcludeTools = () => ['ShellTool(rm -rf /)'];
33
+ const result = isCommandAllowed('rm -rf /', config);
34
+ expect(result.allowed).toBe(false);
35
+ expect(result.reason).toBe(`Command 'rm -rf /' is blocked by configuration`);
36
+ });
37
+ it('should prioritize the blocklist over the allowlist', () => {
38
+ config.getCoreTools = () => ['ShellTool(rm -rf /)'];
39
+ config.getExcludeTools = () => ['ShellTool(rm -rf /)'];
40
+ const result = isCommandAllowed('rm -rf /', config);
41
+ expect(result.allowed).toBe(false);
42
+ expect(result.reason).toBe(`Command 'rm -rf /' is blocked by configuration`);
43
+ });
44
+ it('should allow any command when a wildcard is in coreTools', () => {
45
+ config.getCoreTools = () => ['ShellTool'];
46
+ const result = isCommandAllowed('any random command', config);
47
+ expect(result.allowed).toBe(true);
48
+ });
49
+ it('should block any command when a wildcard is in excludeTools', () => {
50
+ config.getExcludeTools = () => ['run_shell_command'];
51
+ const result = isCommandAllowed('any random command', config);
52
+ expect(result.allowed).toBe(false);
53
+ expect(result.reason).toBe('Shell tool is globally disabled in configuration');
54
+ });
55
+ it('should block a command on the blocklist even with a wildcard allow', () => {
56
+ config.getCoreTools = () => ['ShellTool'];
57
+ config.getExcludeTools = () => ['ShellTool(rm -rf /)'];
58
+ const result = isCommandAllowed('rm -rf /', config);
59
+ expect(result.allowed).toBe(false);
60
+ expect(result.reason).toBe(`Command 'rm -rf /' is blocked by configuration`);
61
+ });
62
+ it('should allow a chained command if all parts are on the global allowlist', () => {
63
+ config.getCoreTools = () => [
64
+ 'run_shell_command(echo)',
65
+ 'run_shell_command(ls)',
66
+ ];
67
+ const result = isCommandAllowed('echo "hello" && ls -l', config);
68
+ expect(result.allowed).toBe(true);
69
+ });
70
+ it('should block a chained command if any part is blocked', () => {
71
+ config.getExcludeTools = () => ['run_shell_command(rm)'];
72
+ const result = isCommandAllowed('echo "hello" && rm -rf /', config);
73
+ expect(result.allowed).toBe(false);
74
+ expect(result.reason).toBe(`Command 'rm -rf /' is blocked by configuration`);
75
+ });
76
+ describe('command substitution', () => {
77
+ it('should block command substitution using `$(...)`', () => {
78
+ const result = isCommandAllowed('echo $(rm -rf /)', config);
79
+ expect(result.allowed).toBe(false);
80
+ expect(result.reason).toContain('Command substitution');
81
+ });
82
+ it('should block command substitution using `<(...)`', () => {
83
+ const result = isCommandAllowed('diff <(ls) <(ls -a)', config);
84
+ expect(result.allowed).toBe(false);
85
+ expect(result.reason).toContain('Command substitution');
86
+ });
87
+ it('should block command substitution using backticks', () => {
88
+ const result = isCommandAllowed('echo `rm -rf /`', config);
89
+ expect(result.allowed).toBe(false);
90
+ expect(result.reason).toContain('Command substitution');
91
+ });
92
+ it('should allow substitution-like patterns inside single quotes', () => {
93
+ config.getCoreTools = () => ['ShellTool(echo)'];
94
+ const result = isCommandAllowed("echo '$(pwd)'", config);
95
+ expect(result.allowed).toBe(true);
96
+ });
97
+ });
98
+ });
99
+ describe('checkCommandPermissions', () => {
100
+ describe('in "Default Allow" mode (no sessionAllowlist)', () => {
101
+ it('should return a detailed success object for an allowed command', () => {
102
+ const result = checkCommandPermissions('ls -l', config);
103
+ expect(result).toEqual({
104
+ allAllowed: true,
105
+ disallowedCommands: [],
106
+ });
107
+ });
108
+ it('should return a detailed failure object for a blocked command', () => {
109
+ config.getExcludeTools = () => ['ShellTool(rm)'];
110
+ const result = checkCommandPermissions('rm -rf /', config);
111
+ expect(result).toEqual({
112
+ allAllowed: false,
113
+ disallowedCommands: ['rm -rf /'],
114
+ blockReason: `Command 'rm -rf /' is blocked by configuration`,
115
+ isHardDenial: true,
116
+ });
117
+ });
118
+ it('should return a detailed failure object for a command not on a strict allowlist', () => {
119
+ config.getCoreTools = () => ['ShellTool(ls)'];
120
+ const result = checkCommandPermissions('git status && ls', config);
121
+ expect(result).toEqual({
122
+ allAllowed: false,
123
+ disallowedCommands: ['git status'],
124
+ blockReason: `Command(s) not in the allowed commands list.`,
125
+ isHardDenial: false,
126
+ });
127
+ });
128
+ });
129
+ describe('in "Default Deny" mode (with sessionAllowlist)', () => {
130
+ it('should allow a command on the sessionAllowlist', () => {
131
+ const result = checkCommandPermissions('ls -l', config, new Set(['ls -l']));
132
+ expect(result.allAllowed).toBe(true);
133
+ });
134
+ it('should block a command not on the sessionAllowlist or global allowlist', () => {
135
+ const result = checkCommandPermissions('rm -rf /', config, new Set(['ls -l']));
136
+ expect(result.allAllowed).toBe(false);
137
+ expect(result.blockReason).toContain('not on the global or session allowlist');
138
+ expect(result.disallowedCommands).toEqual(['rm -rf /']);
139
+ });
140
+ it('should allow a command on the global allowlist even if not on the session allowlist', () => {
141
+ config.getCoreTools = () => ['ShellTool(git status)'];
142
+ const result = checkCommandPermissions('git status', config, new Set(['ls -l']));
143
+ expect(result.allAllowed).toBe(true);
144
+ });
145
+ it('should allow a chained command if parts are on different allowlists', () => {
146
+ config.getCoreTools = () => ['ShellTool(git status)'];
147
+ const result = checkCommandPermissions('git status && git commit', config, new Set(['git commit']));
148
+ expect(result.allAllowed).toBe(true);
149
+ });
150
+ it('should block a command on the sessionAllowlist if it is also globally blocked', () => {
151
+ config.getExcludeTools = () => ['run_shell_command(rm)'];
152
+ const result = checkCommandPermissions('rm -rf /', config, new Set(['rm -rf /']));
153
+ expect(result.allAllowed).toBe(false);
154
+ expect(result.blockReason).toContain('is blocked by configuration');
155
+ });
156
+ it('should block a chained command if one part is not on any allowlist', () => {
157
+ config.getCoreTools = () => ['run_shell_command(echo)'];
158
+ const result = checkCommandPermissions('echo "hello" && rm -rf /', config, new Set(['echo']));
159
+ expect(result.allAllowed).toBe(false);
160
+ expect(result.disallowedCommands).toEqual(['rm -rf /']);
161
+ });
162
+ });
163
+ });
164
+ describe('getCommandRoots', () => {
165
+ it('should return a single command', () => {
166
+ expect(getCommandRoots('ls -l')).toEqual(['ls']);
167
+ });
168
+ it('should handle paths and return the binary name', () => {
169
+ expect(getCommandRoots('/usr/local/bin/node script.js')).toEqual(['node']);
170
+ });
171
+ it('should return an empty array for an empty string', () => {
172
+ expect(getCommandRoots('')).toEqual([]);
173
+ });
174
+ it('should handle a mix of operators', () => {
175
+ const result = getCommandRoots('a;b|c&&d||e&f');
176
+ expect(result).toEqual(['a', 'b', 'c', 'd', 'e', 'f']);
177
+ });
178
+ it('should correctly parse a chained command with quotes', () => {
179
+ const result = getCommandRoots('echo "hello" && git commit -m "feat"');
180
+ expect(result).toEqual(['echo', 'git']);
181
+ });
182
+ });
183
+ describe('stripShellWrapper', () => {
184
+ it('should strip sh -c with quotes', () => {
185
+ expect(stripShellWrapper('sh -c "ls -l"')).toEqual('ls -l');
186
+ });
187
+ it('should strip bash -c with extra whitespace', () => {
188
+ expect(stripShellWrapper(' bash -c "ls -l" ')).toEqual('ls -l');
189
+ });
190
+ it('should strip zsh -c without quotes', () => {
191
+ expect(stripShellWrapper('zsh -c ls -l')).toEqual('ls -l');
192
+ });
193
+ it('should strip cmd.exe /c', () => {
194
+ expect(stripShellWrapper('cmd.exe /c "dir"')).toEqual('dir');
195
+ });
196
+ it('should not strip anything if no wrapper is present', () => {
197
+ expect(stripShellWrapper('ls -l')).toEqual('ls -l');
198
+ });
199
+ });
200
+ //# sourceMappingURL=shell-utils.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shell-utils.test.js","sourceRoot":"","sources":["../../../src/utils/shell-utils.test.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAC1D,OAAO,EACL,uBAAuB,EACvB,eAAe,EACf,gBAAgB,EAChB,iBAAiB,GAClB,MAAM,kBAAkB,CAAC;AAG1B,IAAI,MAAc,CAAC;AAEnB,UAAU,CAAC,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C,oCAAoC,EAAE,GAAG,EAAE;QAC5C,MAAM,CAAC,iBAAiB,CAAC,cAAc,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAC7D,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,yBAAyB,EAAE,GAAG,EAAE;QACjC,MAAM,CAAC,iBAAiB,CAAC,kBAAkB,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IAC/D,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,oDAAoD,EAAE,GAAG,EAAE;QAC5D,MAAM,CAAC,iBAAiB,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IACtD,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
@@ -4,6 +4,7 @@
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
6
  import { DEFAULT_GEMINI_FLASH_MODEL } from '../config/models.js';
7
+ import { getResponseText, partToString } from './partUtils.js';
7
8
  /**
8
9
  * The default summarizer for tool results.
9
10
  *
@@ -13,36 +14,6 @@ import { DEFAULT_GEMINI_FLASH_MODEL } from '../config/models.js';
13
14
  * @returns The summary of the result.
14
15
  */
15
16
  export const defaultSummarizer = (result, _geminiClient, _abortSignal) => Promise.resolve(JSON.stringify(result.llmContent));
16
- // TODO: Move both these functions to utils
17
- function partToString(part) {
18
- if (!part) {
19
- return '';
20
- }
21
- if (typeof part === 'string') {
22
- return part;
23
- }
24
- if (Array.isArray(part)) {
25
- return part.map(partToString).join('');
26
- }
27
- if ('text' in part) {
28
- return part.text ?? '';
29
- }
30
- return '';
31
- }
32
- function getResponseText(response) {
33
- if (response.candidates && response.candidates.length > 0) {
34
- const candidate = response.candidates[0];
35
- if (candidate.content &&
36
- candidate.content.parts &&
37
- candidate.content.parts.length > 0) {
38
- return candidate.content.parts
39
- .filter((part) => part.text)
40
- .map((part) => part.text)
41
- .join('');
42
- }
43
- }
44
- return null;
45
- }
46
17
  const SUMMARIZE_TOOL_OUTPUT_PROMPT = `Summarize the following tool output to be a maximum of {maxOutputTokens} tokens. The summary should be concise and capture the main points of the tool output.
47
18
 
48
19
  The summarization should be done based on the content that is provided. Here are the basic rules to follow:
@@ -1 +1 @@
1
- {"version":3,"file":"summarizer.js","sourceRoot":"","sources":["../../../src/utils/summarizer.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AASH,OAAO,EAAE,0BAA0B,EAAE,MAAM,qBAAqB,CAAC;AAejE;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAe,CAC3C,MAAkB,EAClB,aAA2B,EAC3B,YAAyB,EACzB,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC;AAExD,2CAA2C;AAC3C,SAAS,YAAY,CAAC,IAAmB;IACvC,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,OAAO,EAAE,CAAC;IACZ,CAAC;IACD,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC7B,OAAO,IAAI,CAAC;IACd,CAAC;IACD,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;QACxB,OAAO,IAAI,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACzC,CAAC;IACD,IAAI,MAAM,IAAI,IAAI,EAAE,CAAC;QACnB,OAAO,IAAI,CAAC,IAAI,IAAI,EAAE,CAAC;IACzB,CAAC;IACD,OAAO,EAAE,CAAC;AACZ,CAAC;AAED,SAAS,eAAe,CAAC,QAAiC;IACxD,IAAI,QAAQ,CAAC,UAAU,IAAI,QAAQ,CAAC,UAAU,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC1D,MAAM,SAAS,GAAG,QAAQ,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;QACzC,IACE,SAAS,CAAC,OAAO;YACjB,SAAS,CAAC,OAAO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B,MAAM,CAAC,eAAe,CAAC,CACxB,CAAC,OAAO,CAAC,mBAAmB,EAAE,eAAe,CAAC,CAAC;IAEhD,MAAM,QAAQ,GAAc,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,CAAC,EAAE,CAAC,CAAC;IAC1E,MAAM,0BAA0B,GAA0B;QACxD,eAAe;KAChB,CAAC;IACF,IAAI,CAAC;QACH,MAAM,cAAc,GAAG,CAAC,MAAM,YAAY,CAAC,eAAe,CACxD,QAAQ,EACR,0BAA0B,EAC1B,WAAW,EACX,0BAA0B,CAC3B,CAAuC,CAAC;QACzC,OAAO,eAAe,CAAC,cAAc,CAAC,IAAI,eAAe,CAAC;IAC5D,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,KAAK,CAAC,kCAAkC,EAAE,KAAK,CAAC,CAAC;QACzD,OAAO,eAAe,CAAC;IACzB,CAAC;AACH,CAAC"}
@@ -0,0 +1,40 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2025 Google LLC
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+ /**
7
+ * Reset the encoding cache - useful for testing
8
+ */
9
+ export declare function resetEncodingCache(): void;
10
+ /**
11
+ * Returns the system encoding, caching the result to avoid repeated system calls.
12
+ * If system encoding detection fails, falls back to detecting from the provided buffer.
13
+ * Note: Only the system encoding is cached - buffer-based detection runs for each buffer
14
+ * since different buffers may have different encodings.
15
+ * @param buffer A buffer to use for detecting encoding if system detection fails.
16
+ */
17
+ export declare function getCachedEncodingForBuffer(buffer: Buffer): string;
18
+ /**
19
+ * Detects the system encoding based on the platform.
20
+ * For Windows, it uses the 'chcp' command to get the current code page.
21
+ * For Unix-like systems, it checks environment variables like LC_ALL, LC_CTYPE, and LANG.
22
+ * If those are not set, it tries to run 'locale charmap' to get the encoding.
23
+ * If detection fails, it returns null.
24
+ * @returns The system encoding as a string, or null if detection fails.
25
+ */
26
+ export declare function getSystemEncoding(): string | null;
27
+ /**
28
+ * Converts a Windows code page number to a corresponding encoding name.
29
+ * @param cp The Windows code page number (e.g., 437, 850, etc.)
30
+ * @returns The corresponding encoding name as a string, or null if no mapping exists.
31
+ */
32
+ export declare function windowsCodePageToEncoding(cp: number): string | null;
33
+ /**
34
+ * Attempts to detect encoding from a buffer using chardet.
35
+ * This is useful when system encoding detection fails.
36
+ * Returns the detected encoding in lowercase, or null if detection fails.
37
+ * @param buffer The buffer to analyze for encoding.
38
+ * @return The detected encoding as a lowercase string, or null if detection fails.
39
+ */
40
+ export declare function detectEncodingFromBuffer(buffer: Buffer): string | null;