@google/gemini-cli-core 0.3.0-nightly.20250823.1a89d185 → 0.3.0-preview.1

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 (388) hide show
  1. package/dist/index.d.ts +2 -0
  2. package/dist/index.js +2 -0
  3. package/dist/index.js.map +1 -1
  4. package/dist/src/code_assist/codeAssist.d.ts +4 -3
  5. package/dist/src/code_assist/codeAssist.js +1 -2
  6. package/dist/src/code_assist/codeAssist.js.map +1 -1
  7. package/dist/src/code_assist/converter.d.ts +2 -1
  8. package/dist/src/code_assist/converter.js +1 -1
  9. package/dist/src/code_assist/converter.js.map +1 -1
  10. package/dist/src/code_assist/converter.test.js.map +1 -1
  11. package/dist/src/code_assist/oauth2.d.ts +1 -1
  12. package/dist/src/code_assist/oauth2.js +8 -8
  13. package/dist/src/code_assist/oauth2.js.map +1 -1
  14. package/dist/src/code_assist/oauth2.test.js +5 -5
  15. package/dist/src/code_assist/oauth2.test.js.map +1 -1
  16. package/dist/src/code_assist/server.d.ts +5 -5
  17. package/dist/src/code_assist/server.js +1 -1
  18. package/dist/src/code_assist/server.js.map +1 -1
  19. package/dist/src/code_assist/setup.d.ts +4 -12
  20. package/dist/src/code_assist/setup.js +14 -97
  21. package/dist/src/code_assist/setup.js.map +1 -1
  22. package/dist/src/code_assist/setup.test.js +13 -11
  23. package/dist/src/code_assist/setup.test.js.map +1 -1
  24. package/dist/src/config/config.d.ts +22 -5
  25. package/dist/src/config/config.js +42 -11
  26. package/dist/src/config/config.js.map +1 -1
  27. package/dist/src/config/config.test.js +83 -2
  28. package/dist/src/config/config.test.js.map +1 -1
  29. package/dist/src/config/storage.js +3 -3
  30. package/dist/src/config/storage.js.map +1 -1
  31. package/dist/src/config/storage.test.js +1 -1
  32. package/dist/src/config/storage.test.js.map +1 -1
  33. package/dist/src/core/client.d.ts +15 -9
  34. package/dist/src/core/client.js +57 -14
  35. package/dist/src/core/client.js.map +1 -1
  36. package/dist/src/core/client.test.js +336 -82
  37. package/dist/src/core/client.test.js.map +1 -1
  38. package/dist/src/core/contentGenerator.d.ts +3 -4
  39. package/dist/src/core/contentGenerator.js +1 -4
  40. package/dist/src/core/contentGenerator.js.map +1 -1
  41. package/dist/src/core/contentGenerator.test.js.map +1 -1
  42. package/dist/src/core/coreToolScheduler.d.ts +11 -2
  43. package/dist/src/core/coreToolScheduler.js +60 -27
  44. package/dist/src/core/coreToolScheduler.js.map +1 -1
  45. package/dist/src/core/coreToolScheduler.test.js +148 -20
  46. package/dist/src/core/coreToolScheduler.test.js.map +1 -1
  47. package/dist/src/core/geminiChat.d.ts +10 -12
  48. package/dist/src/core/geminiChat.js +93 -74
  49. package/dist/src/core/geminiChat.js.map +1 -1
  50. package/dist/src/core/geminiChat.test.js +427 -270
  51. package/dist/src/core/geminiChat.test.js.map +1 -1
  52. package/dist/src/core/geminiRequest.js +1 -0
  53. package/dist/src/core/geminiRequest.js.map +1 -1
  54. package/dist/src/core/logger.d.ts +2 -2
  55. package/dist/src/core/logger.test.js.map +1 -1
  56. package/dist/src/core/loggingContentGenerator.d.ts +3 -3
  57. package/dist/src/core/nonInteractiveToolExecutor.d.ts +1 -1
  58. package/dist/src/core/nonInteractiveToolExecutor.js.map +1 -1
  59. package/dist/src/core/nonInteractiveToolExecutor.test.js +11 -4
  60. package/dist/src/core/nonInteractiveToolExecutor.test.js.map +1 -1
  61. package/dist/src/core/subagent.d.ts +24 -18
  62. package/dist/src/core/subagent.js +120 -72
  63. package/dist/src/core/subagent.js.map +1 -1
  64. package/dist/src/core/subagent.test.js +26 -21
  65. package/dist/src/core/subagent.test.js.map +1 -1
  66. package/dist/src/core/turn.d.ts +15 -4
  67. package/dist/src/core/turn.js +12 -1
  68. package/dist/src/core/turn.js.map +1 -1
  69. package/dist/src/core/turn.test.js +1 -1
  70. package/dist/src/core/turn.test.js.map +1 -1
  71. package/dist/src/generated/git-commit.d.ts +2 -2
  72. package/dist/src/generated/git-commit.js +2 -2
  73. package/dist/src/generated/git-commit.js.map +1 -1
  74. package/dist/src/ide/detect-ide.d.ts +8 -3
  75. package/dist/src/ide/detect-ide.js +29 -11
  76. package/dist/src/ide/detect-ide.js.map +1 -1
  77. package/dist/src/ide/detect-ide.test.js +96 -52
  78. package/dist/src/ide/detect-ide.test.js.map +1 -1
  79. package/dist/src/ide/ide-client.d.ts +6 -5
  80. package/dist/src/ide/ide-client.js +19 -17
  81. package/dist/src/ide/ide-client.js.map +1 -1
  82. package/dist/src/ide/ide-client.test.js +14 -10
  83. package/dist/src/ide/ide-client.test.js.map +1 -1
  84. package/dist/src/ide/ide-installer.d.ts +1 -1
  85. package/dist/src/ide/ide-installer.js +29 -20
  86. package/dist/src/ide/ide-installer.js.map +1 -1
  87. package/dist/src/ide/ide-installer.test.js +82 -22
  88. package/dist/src/ide/ide-installer.test.js.map +1 -1
  89. package/dist/src/ide/ideContext.d.ts +11 -11
  90. package/dist/src/ide/process-utils.d.ts +7 -4
  91. package/dist/src/ide/process-utils.js +47 -34
  92. package/dist/src/ide/process-utils.js.map +1 -1
  93. package/dist/src/ide/process-utils.test.d.ts +6 -0
  94. package/dist/src/ide/process-utils.test.js +72 -0
  95. package/dist/src/ide/process-utils.test.js.map +1 -0
  96. package/dist/src/index.d.ts +7 -2
  97. package/dist/src/index.js +5 -1
  98. package/dist/src/index.js.map +1 -1
  99. package/dist/src/mcp/google-auth-provider.d.ts +3 -3
  100. package/dist/src/mcp/google-auth-provider.test.js.map +1 -1
  101. package/dist/src/mcp/oauth-provider.d.ts +2 -2
  102. package/dist/src/mcp/oauth-provider.js.map +1 -1
  103. package/dist/src/mcp/oauth-provider.test.js +1 -1
  104. package/dist/src/mcp/oauth-provider.test.js.map +1 -1
  105. package/dist/src/mcp/oauth-token-storage.d.ts +5 -25
  106. package/dist/src/mcp/oauth-token-storage.js.map +1 -1
  107. package/dist/src/mcp/oauth-token-storage.test.js +1 -1
  108. package/dist/src/mcp/oauth-token-storage.test.js.map +1 -1
  109. package/dist/src/mcp/oauth-utils.d.ts +9 -1
  110. package/dist/src/mcp/oauth-utils.js +41 -27
  111. package/dist/src/mcp/oauth-utils.js.map +1 -1
  112. package/dist/src/mcp/oauth-utils.test.js +41 -1
  113. package/dist/src/mcp/oauth-utils.test.js.map +1 -1
  114. package/dist/src/mcp/token-storage/base-token-storage.d.ts +19 -0
  115. package/dist/src/mcp/token-storage/base-token-storage.js +36 -0
  116. package/dist/src/mcp/token-storage/base-token-storage.js.map +1 -0
  117. package/dist/src/mcp/token-storage/base-token-storage.test.d.ts +6 -0
  118. package/dist/src/mcp/token-storage/base-token-storage.test.js +160 -0
  119. package/dist/src/mcp/token-storage/base-token-storage.test.js.map +1 -0
  120. package/dist/src/mcp/token-storage/types.d.ts +34 -0
  121. package/dist/src/mcp/token-storage/types.js +7 -0
  122. package/dist/src/mcp/token-storage/types.js.map +1 -0
  123. package/dist/src/prompts/mcp-prompts.d.ts +2 -2
  124. package/dist/src/prompts/prompt-registry.d.ts +1 -1
  125. package/dist/src/services/chatRecordingService.d.ts +1 -1
  126. package/dist/src/services/chatRecordingService.js +3 -0
  127. package/dist/src/services/chatRecordingService.js.map +1 -1
  128. package/dist/src/services/chatRecordingService.test.js +2 -2
  129. package/dist/src/services/chatRecordingService.test.js.map +1 -1
  130. package/dist/src/services/fileDiscoveryService.js +1 -1
  131. package/dist/src/services/fileDiscoveryService.js.map +1 -1
  132. package/dist/src/services/fileDiscoveryService.test.js +3 -3
  133. package/dist/src/services/fileDiscoveryService.test.js.map +1 -1
  134. package/dist/src/services/fileSystemService.js +1 -1
  135. package/dist/src/services/fileSystemService.js.map +1 -1
  136. package/dist/src/services/fileSystemService.test.js +1 -1
  137. package/dist/src/services/fileSystemService.test.js.map +1 -1
  138. package/dist/src/services/gitService.d.ts +1 -1
  139. package/dist/src/services/gitService.js +2 -2
  140. package/dist/src/services/gitService.js.map +1 -1
  141. package/dist/src/services/gitService.test.js +3 -3
  142. package/dist/src/services/gitService.test.js.map +1 -1
  143. package/dist/src/services/loopDetectionService.d.ts +2 -2
  144. package/dist/src/services/loopDetectionService.js +9 -3
  145. package/dist/src/services/loopDetectionService.js.map +1 -1
  146. package/dist/src/services/loopDetectionService.test.js +23 -1
  147. package/dist/src/services/loopDetectionService.test.js.map +1 -1
  148. package/dist/src/services/shellExecutionService.js +5 -4
  149. package/dist/src/services/shellExecutionService.js.map +1 -1
  150. package/dist/src/services/shellExecutionService.test.js +4 -3
  151. package/dist/src/services/shellExecutionService.test.js.map +1 -1
  152. package/dist/src/telemetry/clearcut-logger/clearcut-logger.d.ts +11 -3
  153. package/dist/src/telemetry/clearcut-logger/clearcut-logger.js +77 -2
  154. package/dist/src/telemetry/clearcut-logger/clearcut-logger.js.map +1 -1
  155. package/dist/src/telemetry/clearcut-logger/clearcut-logger.test.js +11 -3
  156. package/dist/src/telemetry/clearcut-logger/clearcut-logger.test.js.map +1 -1
  157. package/dist/src/telemetry/clearcut-logger/event-metadata-key.d.ts +21 -5
  158. package/dist/src/telemetry/clearcut-logger/event-metadata-key.js +48 -7
  159. package/dist/src/telemetry/clearcut-logger/event-metadata-key.js.map +1 -1
  160. package/dist/src/telemetry/constants.d.ts +5 -0
  161. package/dist/src/telemetry/constants.js +5 -0
  162. package/dist/src/telemetry/constants.js.map +1 -1
  163. package/dist/src/telemetry/file-exporters.d.ts +5 -4
  164. package/dist/src/telemetry/file-exporters.js +1 -1
  165. package/dist/src/telemetry/file-exporters.js.map +1 -1
  166. package/dist/src/telemetry/index.d.ts +5 -2
  167. package/dist/src/telemetry/index.js +3 -2
  168. package/dist/src/telemetry/index.js.map +1 -1
  169. package/dist/src/telemetry/loggers.d.ts +7 -2
  170. package/dist/src/telemetry/loggers.js +96 -2
  171. package/dist/src/telemetry/loggers.js.map +1 -1
  172. package/dist/src/telemetry/loggers.test.circular.js.map +1 -1
  173. package/dist/src/telemetry/loggers.test.js +60 -4
  174. package/dist/src/telemetry/loggers.test.js.map +1 -1
  175. package/dist/src/telemetry/metrics.d.ts +3 -3
  176. package/dist/src/telemetry/metrics.js +1 -1
  177. package/dist/src/telemetry/metrics.js.map +1 -1
  178. package/dist/src/telemetry/sdk.d.ts +1 -1
  179. package/dist/src/telemetry/types.d.ts +37 -7
  180. package/dist/src/telemetry/types.js +51 -0
  181. package/dist/src/telemetry/types.js.map +1 -1
  182. package/dist/src/telemetry/uiTelemetry.d.ts +2 -2
  183. package/dist/src/telemetry/uiTelemetry.js +1 -1
  184. package/dist/src/telemetry/uiTelemetry.js.map +1 -1
  185. package/dist/src/telemetry/uiTelemetry.test.js.map +1 -1
  186. package/dist/src/test-utils/config.d.ts +2 -1
  187. package/dist/src/test-utils/config.js.map +1 -1
  188. package/dist/src/test-utils/mockWorkspaceContext.d.ts +1 -1
  189. package/dist/src/test-utils/tools.d.ts +3 -2
  190. package/dist/src/test-utils/tools.js.map +1 -1
  191. package/dist/src/tools/diffOptions.d.ts +1 -1
  192. package/dist/src/tools/edit.d.ts +4 -3
  193. package/dist/src/tools/edit.js +3 -3
  194. package/dist/src/tools/edit.js.map +1 -1
  195. package/dist/src/tools/edit.test.js +3 -3
  196. package/dist/src/tools/edit.test.js.map +1 -1
  197. package/dist/src/tools/glob.d.ts +3 -2
  198. package/dist/src/tools/glob.js +4 -4
  199. package/dist/src/tools/glob.js.map +1 -1
  200. package/dist/src/tools/glob.test.js +6 -3
  201. package/dist/src/tools/glob.test.js.map +1 -1
  202. package/dist/src/tools/grep.d.ts +3 -2
  203. package/dist/src/tools/grep.js +30 -15
  204. package/dist/src/tools/grep.js.map +1 -1
  205. package/dist/src/tools/grep.test.js +15 -3
  206. package/dist/src/tools/grep.test.js.map +1 -1
  207. package/dist/src/tools/ls.d.ts +3 -2
  208. package/dist/src/tools/ls.js +3 -3
  209. package/dist/src/tools/ls.js.map +1 -1
  210. package/dist/src/tools/ls.test.js +2 -2
  211. package/dist/src/tools/ls.test.js.map +1 -1
  212. package/dist/src/tools/mcp-client-manager.d.ts +4 -4
  213. package/dist/src/tools/mcp-client.d.ts +7 -7
  214. package/dist/src/tools/mcp-client.js +2 -2
  215. package/dist/src/tools/mcp-client.js.map +1 -1
  216. package/dist/src/tools/mcp-tool.d.ts +3 -2
  217. package/dist/src/tools/mcp-tool.js.map +1 -1
  218. package/dist/src/tools/mcp-tool.test.js +1 -2
  219. package/dist/src/tools/mcp-tool.test.js.map +1 -1
  220. package/dist/src/tools/memoryTool.d.ts +3 -2
  221. package/dist/src/tools/memoryTool.js +2 -2
  222. package/dist/src/tools/memoryTool.js.map +1 -1
  223. package/dist/src/tools/memoryTool.test.js +4 -4
  224. package/dist/src/tools/memoryTool.test.js.map +1 -1
  225. package/dist/src/tools/modifiable-tool.d.ts +2 -2
  226. package/dist/src/tools/modifiable-tool.js +3 -3
  227. package/dist/src/tools/modifiable-tool.js.map +1 -1
  228. package/dist/src/tools/modifiable-tool.test.js +4 -4
  229. package/dist/src/tools/modifiable-tool.test.js.map +1 -1
  230. package/dist/src/tools/read-file.d.ts +3 -2
  231. package/dist/src/tools/read-file.js +2 -2
  232. package/dist/src/tools/read-file.js.map +1 -1
  233. package/dist/src/tools/read-file.test.js +4 -4
  234. package/dist/src/tools/read-file.test.js.map +1 -1
  235. package/dist/src/tools/read-many-files.d.ts +3 -2
  236. package/dist/src/tools/read-many-files.js +14 -47
  237. package/dist/src/tools/read-many-files.js.map +1 -1
  238. package/dist/src/tools/read-many-files.test.js +18 -3
  239. package/dist/src/tools/read-many-files.test.js.map +1 -1
  240. package/dist/src/tools/ripGrep.d.ts +3 -2
  241. package/dist/src/tools/ripGrep.js +5 -5
  242. package/dist/src/tools/ripGrep.js.map +1 -1
  243. package/dist/src/tools/ripGrep.test.js +4 -4
  244. package/dist/src/tools/ripGrep.test.js.map +1 -1
  245. package/dist/src/tools/shell.d.ts +3 -2
  246. package/dist/src/tools/shell.js +16 -5
  247. package/dist/src/tools/shell.js.map +1 -1
  248. package/dist/src/tools/shell.test.js +21 -5
  249. package/dist/src/tools/shell.test.js.map +1 -1
  250. package/dist/src/tools/tool-error.d.ts +1 -0
  251. package/dist/src/tools/tool-error.js +2 -0
  252. package/dist/src/tools/tool-error.js.map +1 -1
  253. package/dist/src/tools/tool-registry.d.ts +8 -3
  254. package/dist/src/tools/tool-registry.js +7 -1
  255. package/dist/src/tools/tool-registry.js.map +1 -1
  256. package/dist/src/tools/tool-registry.test.js +15 -2
  257. package/dist/src/tools/tool-registry.test.js.map +1 -1
  258. package/dist/src/tools/tools.d.ts +8 -2
  259. package/dist/src/tools/tools.js +12 -0
  260. package/dist/src/tools/tools.js.map +1 -1
  261. package/dist/src/tools/tools.test.js +1 -1
  262. package/dist/src/tools/tools.test.js.map +1 -1
  263. package/dist/src/tools/web-fetch.d.ts +3 -2
  264. package/dist/src/tools/web-fetch.js +1 -1
  265. package/dist/src/tools/web-fetch.js.map +1 -1
  266. package/dist/src/tools/web-fetch.test.js.map +1 -1
  267. package/dist/src/tools/web-search.d.ts +4 -3
  268. package/dist/src/tools/web-search.js +2 -2
  269. package/dist/src/tools/web-search.js.map +1 -1
  270. package/dist/src/tools/web-search.test.js.map +1 -1
  271. package/dist/src/tools/write-file.d.ts +4 -3
  272. package/dist/src/tools/write-file.js +2 -2
  273. package/dist/src/tools/write-file.js.map +1 -1
  274. package/dist/src/tools/write-file.test.js +6 -5
  275. package/dist/src/tools/write-file.test.js.map +1 -1
  276. package/dist/src/utils/bfsFileSearch.d.ts +2 -2
  277. package/dist/src/utils/bfsFileSearch.js +2 -2
  278. package/dist/src/utils/bfsFileSearch.js.map +1 -1
  279. package/dist/src/utils/bfsFileSearch.test.js +3 -3
  280. package/dist/src/utils/bfsFileSearch.test.js.map +1 -1
  281. package/dist/src/utils/editCorrector.d.ts +2 -2
  282. package/dist/src/utils/editCorrector.js +1 -1
  283. package/dist/src/utils/editCorrector.js.map +1 -1
  284. package/dist/src/utils/editCorrector.test.js +2 -3
  285. package/dist/src/utils/editCorrector.test.js.map +1 -1
  286. package/dist/src/utils/editor.js +1 -1
  287. package/dist/src/utils/editor.js.map +1 -1
  288. package/dist/src/utils/editor.test.js +1 -1
  289. package/dist/src/utils/editor.test.js.map +1 -1
  290. package/dist/src/utils/environmentContext.d.ts +2 -2
  291. package/dist/src/utils/errorReporting.d.ts +1 -1
  292. package/dist/src/utils/errors.d.ts +19 -0
  293. package/dist/src/utils/errors.js +32 -0
  294. package/dist/src/utils/errors.js.map +1 -1
  295. package/dist/src/utils/fetch.js +1 -1
  296. package/dist/src/utils/fetch.js.map +1 -1
  297. package/dist/src/utils/fileUtils.d.ts +2 -2
  298. package/dist/src/utils/fileUtils.js +2 -30
  299. package/dist/src/utils/fileUtils.js.map +1 -1
  300. package/dist/src/utils/filesearch/crawler.d.ts +1 -1
  301. package/dist/src/utils/filesearch/crawler.test.js +2 -2
  302. package/dist/src/utils/filesearch/crawler.test.js.map +1 -1
  303. package/dist/src/utils/filesearch/fileSearch.js +12 -8
  304. package/dist/src/utils/filesearch/fileSearch.js.map +1 -1
  305. package/dist/src/utils/generateContentResponseUtilities.d.ts +1 -2
  306. package/dist/src/utils/generateContentResponseUtilities.js +1 -13
  307. package/dist/src/utils/generateContentResponseUtilities.js.map +1 -1
  308. package/dist/src/utils/generateContentResponseUtilities.test.js +2 -40
  309. package/dist/src/utils/generateContentResponseUtilities.test.js.map +1 -1
  310. package/dist/src/utils/getFolderStructure.d.ts +2 -2
  311. package/dist/src/utils/getFolderStructure.js +2 -2
  312. package/dist/src/utils/getFolderStructure.js.map +1 -1
  313. package/dist/src/utils/getFolderStructure.test.js +4 -4
  314. package/dist/src/utils/getFolderStructure.test.js.map +1 -1
  315. package/dist/src/utils/gitIgnoreParser.js +3 -3
  316. package/dist/src/utils/gitIgnoreParser.js.map +1 -1
  317. package/dist/src/utils/gitIgnoreParser.test.js +3 -3
  318. package/dist/src/utils/gitIgnoreParser.test.js.map +1 -1
  319. package/dist/src/utils/gitUtils.js +2 -2
  320. package/dist/src/utils/gitUtils.js.map +1 -1
  321. package/dist/src/utils/ignorePatterns.d.ts +103 -0
  322. package/dist/src/utils/ignorePatterns.js +220 -0
  323. package/dist/src/utils/ignorePatterns.js.map +1 -0
  324. package/dist/src/utils/ignorePatterns.test.d.ts +6 -0
  325. package/dist/src/utils/ignorePatterns.test.js +250 -0
  326. package/dist/src/utils/ignorePatterns.test.js.map +1 -0
  327. package/dist/src/utils/installationManager.js +2 -2
  328. package/dist/src/utils/installationManager.js.map +1 -1
  329. package/dist/src/utils/installationManager.test.js +1 -1
  330. package/dist/src/utils/installationManager.test.js.map +1 -1
  331. package/dist/src/utils/language-detection.js +1 -1
  332. package/dist/src/utils/language-detection.js.map +1 -1
  333. package/dist/src/utils/memoryDiscovery.d.ts +2 -2
  334. package/dist/src/utils/memoryDiscovery.js +5 -5
  335. package/dist/src/utils/memoryDiscovery.js.map +1 -1
  336. package/dist/src/utils/memoryDiscovery.test.js +3 -3
  337. package/dist/src/utils/memoryDiscovery.test.js.map +1 -1
  338. package/dist/src/utils/memoryImportProcessor.js +2 -2
  339. package/dist/src/utils/memoryImportProcessor.js.map +1 -1
  340. package/dist/src/utils/memoryImportProcessor.test.js +2 -141
  341. package/dist/src/utils/memoryImportProcessor.test.js.map +1 -1
  342. package/dist/src/utils/messageInspectors.d.ts +1 -1
  343. package/dist/src/utils/nextSpeakerChecker.d.ts +2 -2
  344. package/dist/src/utils/nextSpeakerChecker.test.js.map +1 -1
  345. package/dist/src/utils/partUtils.d.ts +22 -1
  346. package/dist/src/utils/partUtils.js +68 -0
  347. package/dist/src/utils/partUtils.js.map +1 -1
  348. package/dist/src/utils/partUtils.test.js +112 -1
  349. package/dist/src/utils/partUtils.test.js.map +1 -1
  350. package/dist/src/utils/pathReader.d.ts +17 -0
  351. package/dist/src/utils/pathReader.js +92 -0
  352. package/dist/src/utils/pathReader.js.map +1 -0
  353. package/dist/src/utils/pathReader.test.d.ts +6 -0
  354. package/dist/src/utils/pathReader.test.js +363 -0
  355. package/dist/src/utils/pathReader.test.js.map +1 -0
  356. package/dist/src/utils/paths.js +2 -2
  357. package/dist/src/utils/paths.js.map +1 -1
  358. package/dist/src/utils/quotaErrorDetection.d.ts +1 -1
  359. package/dist/src/utils/retry.test.js +4 -1
  360. package/dist/src/utils/retry.test.js.map +1 -1
  361. package/dist/src/utils/schemaValidator.js +4 -0
  362. package/dist/src/utils/schemaValidator.js.map +1 -1
  363. package/dist/src/utils/session.js +1 -1
  364. package/dist/src/utils/session.js.map +1 -1
  365. package/dist/src/utils/shell-utils.d.ts +1 -1
  366. package/dist/src/utils/shell-utils.js +23 -29
  367. package/dist/src/utils/shell-utils.js.map +1 -1
  368. package/dist/src/utils/shell-utils.test.js +4 -0
  369. package/dist/src/utils/shell-utils.test.js.map +1 -1
  370. package/dist/src/utils/summarizer.d.ts +2 -2
  371. package/dist/src/utils/summarizer.test.js.map +1 -1
  372. package/dist/src/utils/systemEncoding.js +2 -2
  373. package/dist/src/utils/systemEncoding.js.map +1 -1
  374. package/dist/src/utils/systemEncoding.test.js +2 -2
  375. package/dist/src/utils/systemEncoding.test.js.map +1 -1
  376. package/dist/src/utils/tool-utils.d.ts +19 -0
  377. package/dist/src/utils/tool-utils.js +58 -0
  378. package/dist/src/utils/tool-utils.js.map +1 -0
  379. package/dist/src/utils/tool-utils.test.d.ts +6 -0
  380. package/dist/src/utils/tool-utils.test.js +61 -0
  381. package/dist/src/utils/tool-utils.test.js.map +1 -0
  382. package/dist/src/utils/userAccountManager.test.js.map +1 -1
  383. package/dist/src/utils/workspaceContext.js +3 -3
  384. package/dist/src/utils/workspaceContext.js.map +1 -1
  385. package/dist/src/utils/workspaceContext.test.js +3 -3
  386. package/dist/src/utils/workspaceContext.test.js.map +1 -1
  387. package/dist/tsconfig.tsbuildinfo +1 -1
  388. package/package.json +10 -7
@@ -14,15 +14,15 @@ const mockModelsModule = {
14
14
  embedContent: vi.fn(),
15
15
  batchEmbedContents: vi.fn(),
16
16
  };
17
- const { mockRecordInvalidChunk, mockRecordContentRetry, mockRecordContentRetryFailure, } = vi.hoisted(() => ({
18
- mockRecordInvalidChunk: vi.fn(),
19
- mockRecordContentRetry: vi.fn(),
20
- mockRecordContentRetryFailure: vi.fn(),
17
+ const { mockLogInvalidChunk, mockLogContentRetry, mockLogContentRetryFailure } = vi.hoisted(() => ({
18
+ mockLogInvalidChunk: vi.fn(),
19
+ mockLogContentRetry: vi.fn(),
20
+ mockLogContentRetryFailure: vi.fn(),
21
21
  }));
22
- vi.mock('../telemetry/metrics.js', () => ({
23
- recordInvalidChunk: mockRecordInvalidChunk,
24
- recordContentRetry: mockRecordContentRetry,
25
- recordContentRetryFailure: mockRecordContentRetryFailure,
22
+ vi.mock('../telemetry/loggers.js', () => ({
23
+ logInvalidChunk: mockLogInvalidChunk,
24
+ logContentRetry: mockLogContentRetry,
25
+ logContentRetryFailure: mockLogContentRetryFailure,
26
26
  }));
27
27
  describe('GeminiChat', () => {
28
28
  let chat;
@@ -55,6 +55,110 @@ describe('GeminiChat', () => {
55
55
  vi.resetAllMocks();
56
56
  });
57
57
  describe('sendMessage', () => {
58
+ it('should throw an error when attempting to add a user turn after another user turn', async () => {
59
+ // 1. Setup: Create a history that already ends with a user turn (a functionResponse).
60
+ const initialHistory = [
61
+ { role: 'user', parts: [{ text: 'Initial prompt' }] },
62
+ {
63
+ role: 'model',
64
+ parts: [{ functionCall: { name: 'test_tool', args: {} } }],
65
+ },
66
+ {
67
+ role: 'user',
68
+ parts: [{ functionResponse: { name: 'test_tool', response: {} } }],
69
+ },
70
+ ];
71
+ chat.setHistory(initialHistory);
72
+ // 2. Mock a valid model response so the call doesn't fail for other reasons.
73
+ const mockResponse = {
74
+ candidates: [
75
+ { content: { role: 'model', parts: [{ text: 'some response' }] } },
76
+ ],
77
+ };
78
+ vi.mocked(mockModelsModule.generateContent).mockResolvedValue(mockResponse);
79
+ // 3. Action & Assert: Expect that sending another user message immediately
80
+ // after a user-role turn throws the specific error.
81
+ await expect(chat.sendMessage({ message: 'This is an invalid consecutive user message' }, 'prompt-id-1')).rejects.toThrow('Cannot add a user turn after another user turn.');
82
+ });
83
+ it('should preserve text parts that are in the same response as a thought', async () => {
84
+ // 1. Mock the API to return a single response containing both a thought and visible text.
85
+ const mixedContentResponse = {
86
+ candidates: [
87
+ {
88
+ content: {
89
+ role: 'model',
90
+ parts: [
91
+ { thought: 'This is a thought.' },
92
+ { text: 'This is the visible text that should not be lost.' },
93
+ ],
94
+ },
95
+ },
96
+ ],
97
+ };
98
+ vi.mocked(mockModelsModule.generateContent).mockResolvedValue(mixedContentResponse);
99
+ // 2. Action: Send a standard, non-streaming message.
100
+ await chat.sendMessage({ message: 'test message' }, 'prompt-id-mixed-response');
101
+ // 3. Assert: Check the final state of the history.
102
+ const history = chat.getHistory();
103
+ // The history should contain two turns: the user's message and the model's response.
104
+ expect(history.length).toBe(2);
105
+ const modelTurn = history[1];
106
+ expect(modelTurn.role).toBe('model');
107
+ // CRUCIAL ASSERTION:
108
+ // Buggy code would discard the entire response because a "thought" was present,
109
+ // resulting in an empty placeholder turn with 0 parts.
110
+ // The corrected code will pass, preserving the single visible text part.
111
+ expect(modelTurn?.parts?.length).toBe(1);
112
+ expect(modelTurn?.parts[0].text).toBe('This is the visible text that should not be lost.');
113
+ });
114
+ it('should add a placeholder model turn when a tool call is followed by an empty model response', async () => {
115
+ // 1. Setup: A history where the model has just made a function call.
116
+ const initialHistory = [
117
+ {
118
+ role: 'user',
119
+ parts: [{ text: 'Find a good Italian restaurant for me.' }],
120
+ },
121
+ {
122
+ role: 'model',
123
+ parts: [
124
+ {
125
+ functionCall: {
126
+ name: 'find_restaurant',
127
+ args: { cuisine: 'Italian' },
128
+ },
129
+ },
130
+ ],
131
+ },
132
+ ];
133
+ chat.setHistory(initialHistory);
134
+ // 2. Mock the API to return an empty/thought-only response.
135
+ const emptyModelResponse = {
136
+ candidates: [
137
+ { content: { role: 'model', parts: [{ thought: true }] } },
138
+ ],
139
+ };
140
+ vi.mocked(mockModelsModule.generateContent).mockResolvedValue(emptyModelResponse);
141
+ // 3. Action: Send the function response back to the model.
142
+ await chat.sendMessage({
143
+ message: {
144
+ functionResponse: {
145
+ name: 'find_restaurant',
146
+ response: { name: 'Vesuvio' },
147
+ },
148
+ },
149
+ }, 'prompt-id-1');
150
+ // 4. Assert: The history should now have four valid, alternating turns.
151
+ const history = chat.getHistory();
152
+ expect(history.length).toBe(4);
153
+ // The final turn must be the empty model placeholder.
154
+ const lastTurn = history[3];
155
+ expect(lastTurn.role).toBe('model');
156
+ expect(lastTurn?.parts?.length).toBe(0);
157
+ // The second-to-last turn must be the function response we sent.
158
+ const secondToLastTurn = history[2];
159
+ expect(secondToLastTurn.role).toBe('user');
160
+ expect(secondToLastTurn?.parts[0].functionResponse).toBeDefined();
161
+ });
58
162
  it('should call generateContent with the correct parameters', async () => {
59
163
  const response = {
60
164
  candidates: [
@@ -80,6 +184,226 @@ describe('GeminiChat', () => {
80
184
  });
81
185
  });
82
186
  describe('sendMessageStream', () => {
187
+ it('should not consolidate text into a part that also contains a functionCall', async () => {
188
+ // 1. Mock the API to stream a malformed part followed by a valid text part.
189
+ const multiChunkStream = (async function* () {
190
+ // This malformed part has both text and a functionCall.
191
+ yield {
192
+ candidates: [
193
+ {
194
+ content: {
195
+ role: 'model',
196
+ parts: [
197
+ {
198
+ text: 'Some text',
199
+ functionCall: { name: 'do_stuff', args: {} },
200
+ },
201
+ ],
202
+ },
203
+ },
204
+ ],
205
+ };
206
+ // This valid text part should NOT be merged into the malformed one.
207
+ yield {
208
+ candidates: [
209
+ {
210
+ content: {
211
+ role: 'model',
212
+ parts: [{ text: ' that should not be merged.' }],
213
+ },
214
+ },
215
+ ],
216
+ };
217
+ })();
218
+ vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(multiChunkStream);
219
+ // 2. Action: Send a message and consume the stream.
220
+ const stream = await chat.sendMessageStream({ message: 'test message' }, 'prompt-id-malformed-chunk');
221
+ for await (const _ of stream) {
222
+ // Consume the stream to trigger history recording.
223
+ }
224
+ // 3. Assert: Check that the final history was not incorrectly consolidated.
225
+ const history = chat.getHistory();
226
+ expect(history.length).toBe(2);
227
+ const modelTurn = history[1];
228
+ // CRUCIAL ASSERTION: There should be two separate parts.
229
+ // The old, non-strict logic would incorrectly merge them, resulting in one part.
230
+ expect(modelTurn?.parts?.length).toBe(2);
231
+ // Verify the contents of each part.
232
+ expect(modelTurn?.parts[0].text).toBe('Some text');
233
+ expect(modelTurn?.parts[0].functionCall).toBeDefined();
234
+ expect(modelTurn?.parts[1].text).toBe(' that should not be merged.');
235
+ });
236
+ it('should consolidate subsequent text chunks after receiving an empty text chunk', async () => {
237
+ // 1. Mock the API to return a stream where one chunk is just an empty text part.
238
+ const multiChunkStream = (async function* () {
239
+ yield {
240
+ candidates: [
241
+ { content: { role: 'model', parts: [{ text: 'Hello' }] } },
242
+ ],
243
+ };
244
+ // FIX: The original test used { text: '' }, which is invalid.
245
+ // A chunk can be empty but still valid. This chunk is now removed
246
+ // as the important part is consolidating what comes after.
247
+ yield {
248
+ candidates: [
249
+ { content: { role: 'model', parts: [{ text: ' World!' }] } },
250
+ ],
251
+ };
252
+ })();
253
+ vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(multiChunkStream);
254
+ // 2. Action: Send a message and consume the stream.
255
+ const stream = await chat.sendMessageStream({ message: 'test message' }, 'prompt-id-empty-chunk-consolidation');
256
+ for await (const _ of stream) {
257
+ // Consume the stream
258
+ }
259
+ // 3. Assert: Check that the final history was correctly consolidated.
260
+ const history = chat.getHistory();
261
+ expect(history.length).toBe(2);
262
+ const modelTurn = history[1];
263
+ expect(modelTurn?.parts?.length).toBe(1);
264
+ expect(modelTurn?.parts[0].text).toBe('Hello World!');
265
+ });
266
+ it('should consolidate adjacent text parts that arrive in separate stream chunks', async () => {
267
+ // 1. Mock the API to return a stream of multiple, adjacent text chunks.
268
+ const multiChunkStream = (async function* () {
269
+ yield {
270
+ candidates: [
271
+ { content: { role: 'model', parts: [{ text: 'This is the ' }] } },
272
+ ],
273
+ };
274
+ yield {
275
+ candidates: [
276
+ { content: { role: 'model', parts: [{ text: 'first part.' }] } },
277
+ ],
278
+ };
279
+ // This function call should break the consolidation.
280
+ yield {
281
+ candidates: [
282
+ {
283
+ content: {
284
+ role: 'model',
285
+ parts: [{ functionCall: { name: 'do_stuff', args: {} } }],
286
+ },
287
+ },
288
+ ],
289
+ };
290
+ yield {
291
+ candidates: [
292
+ {
293
+ content: {
294
+ role: 'model',
295
+ parts: [{ text: 'This is the second part.' }],
296
+ },
297
+ },
298
+ ],
299
+ };
300
+ })();
301
+ vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(multiChunkStream);
302
+ // 2. Action: Send a message and consume the stream.
303
+ const stream = await chat.sendMessageStream({ message: 'test message' }, 'prompt-id-multi-chunk');
304
+ for await (const _ of stream) {
305
+ // Consume the stream to trigger history recording.
306
+ }
307
+ // 3. Assert: Check that the final history was correctly consolidated.
308
+ const history = chat.getHistory();
309
+ // The history should contain the user's turn and ONE consolidated model turn.
310
+ expect(history.length).toBe(2);
311
+ const modelTurn = history[1];
312
+ expect(modelTurn.role).toBe('model');
313
+ // The model turn should have 3 distinct parts: the merged text, the function call, and the final text.
314
+ expect(modelTurn?.parts?.length).toBe(3);
315
+ expect(modelTurn?.parts[0].text).toBe('This is the first part.');
316
+ expect(modelTurn.parts[1].functionCall).toBeDefined();
317
+ expect(modelTurn.parts[2].text).toBe('This is the second part.');
318
+ });
319
+ it('should preserve text parts that stream in the same chunk as a thought', async () => {
320
+ // 1. Mock the API to return a single chunk containing both a thought and visible text.
321
+ const mixedContentStream = (async function* () {
322
+ yield {
323
+ candidates: [
324
+ {
325
+ content: {
326
+ role: 'model',
327
+ parts: [
328
+ { thought: 'This is a thought.' },
329
+ { text: 'This is the visible text that should not be lost.' },
330
+ ],
331
+ },
332
+ },
333
+ ],
334
+ };
335
+ })();
336
+ vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(mixedContentStream);
337
+ // 2. Action: Send a message and fully consume the stream to trigger history recording.
338
+ const stream = await chat.sendMessageStream({ message: 'test message' }, 'prompt-id-mixed-chunk');
339
+ for await (const _ of stream) {
340
+ // This loop consumes the stream.
341
+ }
342
+ // 3. Assert: Check the final state of the history.
343
+ const history = chat.getHistory();
344
+ // The history should contain two turns: the user's message and the model's response.
345
+ expect(history.length).toBe(2);
346
+ const modelTurn = history[1];
347
+ expect(modelTurn.role).toBe('model');
348
+ // CRUCIAL ASSERTION:
349
+ // The buggy code would fail here, resulting in parts.length being 0.
350
+ // The corrected code will pass, preserving the single visible text part.
351
+ expect(modelTurn?.parts?.length).toBe(1);
352
+ expect(modelTurn?.parts[0].text).toBe('This is the visible text that should not be lost.');
353
+ });
354
+ it('should add a placeholder model turn when a tool call is followed by an empty stream response', async () => {
355
+ // 1. Setup: A history where the model has just made a function call.
356
+ const initialHistory = [
357
+ {
358
+ role: 'user',
359
+ parts: [{ text: 'Find a good Italian restaurant for me.' }],
360
+ },
361
+ {
362
+ role: 'model',
363
+ parts: [
364
+ {
365
+ functionCall: {
366
+ name: 'find_restaurant',
367
+ args: { cuisine: 'Italian' },
368
+ },
369
+ },
370
+ ],
371
+ },
372
+ ];
373
+ chat.setHistory(initialHistory);
374
+ // 2. Mock the API to return an empty/thought-only stream.
375
+ const emptyStreamResponse = (async function* () {
376
+ yield {
377
+ candidates: [
378
+ { content: { role: 'model', parts: [{ thought: true }] } },
379
+ ],
380
+ };
381
+ })();
382
+ vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(emptyStreamResponse);
383
+ // 3. Action: Send the function response back to the model and consume the stream.
384
+ const stream = await chat.sendMessageStream({
385
+ message: {
386
+ functionResponse: {
387
+ name: 'find_restaurant',
388
+ response: { name: 'Vesuvio' },
389
+ },
390
+ },
391
+ }, 'prompt-id-stream-1');
392
+ for await (const _ of stream) {
393
+ // This loop consumes the stream to trigger the internal logic.
394
+ }
395
+ // 4. Assert: The history should now have four valid, alternating turns.
396
+ const history = chat.getHistory();
397
+ expect(history.length).toBe(4);
398
+ // The final turn must be the empty model placeholder.
399
+ const lastTurn = history[3];
400
+ expect(lastTurn.role).toBe('model');
401
+ expect(lastTurn?.parts?.length).toBe(0);
402
+ // The second-to-last turn must be the function response we sent.
403
+ const secondToLastTurn = history[2];
404
+ expect(secondToLastTurn.role).toBe('user');
405
+ expect(secondToLastTurn?.parts[0].functionResponse).toBeDefined();
406
+ });
83
407
  it('should call generateContentStream with the correct parameters', async () => {
84
408
  const response = (async function* () {
85
409
  yield {
@@ -114,295 +438,128 @@ describe('GeminiChat', () => {
114
438
  role: 'user',
115
439
  parts: [{ text: 'User input' }],
116
440
  };
117
- it('should add user input and a single model output to history', () => {
441
+ it('should consolidate all consecutive model turns into a single turn', () => {
442
+ const userInput = {
443
+ role: 'user',
444
+ parts: [{ text: 'User input' }],
445
+ };
446
+ // This simulates a multi-part model response with different part types.
118
447
  const modelOutput = [
119
- { role: 'model', parts: [{ text: 'Model output' }] },
448
+ { role: 'model', parts: [{ text: 'Thinking...' }] },
449
+ {
450
+ role: 'model',
451
+ parts: [{ functionCall: { name: 'do_stuff', args: {} } }],
452
+ },
120
453
  ];
121
- // @ts-expect-error Accessing private method for testing purposes
454
+ // @ts-expect-error Accessing private method for testing
122
455
  chat.recordHistory(userInput, modelOutput);
123
456
  const history = chat.getHistory();
124
- expect(history).toEqual([userInput, modelOutput[0]]);
125
- });
126
- it('should consolidate adjacent model outputs', () => {
127
- const modelOutputParts = [
128
- { role: 'model', parts: [{ text: 'Model part 1' }] },
129
- { role: 'model', parts: [{ text: 'Model part 2' }] },
130
- ];
131
- // @ts-expect-error Accessing private method for testing purposes
132
- chat.recordHistory(userInput, modelOutputParts);
133
- const history = chat.getHistory();
134
- expect(history.length).toBe(2);
135
- expect(history[0]).toEqual(userInput);
136
- expect(history[1].role).toBe('model');
137
- expect(history[1].parts).toEqual([{ text: 'Model part 1Model part 2' }]);
138
- });
139
- it('should handle a mix of user and model roles in outputContents (though unusual)', () => {
140
- const mixedOutput = [
141
- { role: 'model', parts: [{ text: 'Model 1' }] },
142
- { role: 'user', parts: [{ text: 'Unexpected User' }] }, // This should be pushed as is
143
- { role: 'model', parts: [{ text: 'Model 2' }] },
144
- ];
145
- // @ts-expect-error Accessing private method for testing purposes
146
- chat.recordHistory(userInput, mixedOutput);
147
- const history = chat.getHistory();
148
- expect(history.length).toBe(4); // user, model1, user_unexpected, model2
149
- expect(history[0]).toEqual(userInput);
150
- expect(history[1]).toEqual(mixedOutput[0]);
151
- expect(history[2]).toEqual(mixedOutput[1]);
152
- expect(history[3]).toEqual(mixedOutput[2]);
153
- });
154
- it('should consolidate multiple adjacent model outputs correctly', () => {
155
- const modelOutputParts = [
156
- { role: 'model', parts: [{ text: 'M1' }] },
157
- { role: 'model', parts: [{ text: 'M2' }] },
158
- { role: 'model', parts: [{ text: 'M3' }] },
159
- ];
160
- // @ts-expect-error Accessing private method for testing purposes
161
- chat.recordHistory(userInput, modelOutputParts);
162
- const history = chat.getHistory();
457
+ // The history should contain the user's turn and ONE consolidated model turn.
458
+ // The old code would fail here, resulting in a length of 3.
459
+ //expect(history).toBe([]);
163
460
  expect(history.length).toBe(2);
164
- expect(history[1].parts).toEqual([{ text: 'M1M2M3' }]);
165
- });
166
- it('should not consolidate if roles are different between model outputs', () => {
167
- const modelOutputParts = [
168
- { role: 'model', parts: [{ text: 'M1' }] },
169
- { role: 'user', parts: [{ text: 'Interjecting User' }] },
170
- { role: 'model', parts: [{ text: 'M2' }] },
171
- ];
172
- // @ts-expect-error Accessing private method for testing purposes
173
- chat.recordHistory(userInput, modelOutputParts);
174
- const history = chat.getHistory();
175
- expect(history.length).toBe(4); // user, M1, Interjecting User, M2
176
- expect(history[1].parts).toEqual([{ text: 'M1' }]);
177
- expect(history[3].parts).toEqual([{ text: 'M2' }]);
461
+ const modelTurn = history[1];
462
+ expect(modelTurn.role).toBe('model');
463
+ // The consolidated turn should contain both the text part and the functionCall part.
464
+ expect(modelTurn?.parts?.length).toBe(2);
465
+ expect(modelTurn?.parts[0].text).toBe('Thinking...');
466
+ expect(modelTurn?.parts[1].functionCall).toBeDefined();
178
467
  });
179
- it('should merge with last history entry if it is also a model output', () => {
180
- // @ts-expect-error Accessing private property for test setup
181
- chat.history = [
182
- userInput,
183
- { role: 'model', parts: [{ text: 'Initial Model Output' }] },
184
- ]; // Prime the history
185
- const newModelOutput = [
186
- { role: 'model', parts: [{ text: 'New Model Part 1' }] },
187
- { role: 'model', parts: [{ text: 'New Model Part 2' }] },
188
- ];
189
- // @ts-expect-error Accessing private method for testing purposes
190
- chat.recordHistory(userInput, newModelOutput); // userInput here is for the *next* turn, but history is already primed
191
- // Reset and set up a more realistic scenario for merging with existing history
192
- chat = new GeminiChat(mockConfig, mockModelsModule, config, []);
193
- const firstUserInput = {
194
- role: 'user',
195
- parts: [{ text: 'First user input' }],
196
- };
197
- const firstModelOutput = [
198
- { role: 'model', parts: [{ text: 'First model response' }] },
199
- ];
200
- // @ts-expect-error Accessing private method for testing purposes
201
- chat.recordHistory(firstUserInput, firstModelOutput);
202
- const secondUserInput = {
203
- role: 'user',
204
- parts: [{ text: 'Second user input' }],
205
- };
206
- const secondModelOutput = [
207
- { role: 'model', parts: [{ text: 'Second model response part 1' }] },
208
- { role: 'model', parts: [{ text: 'Second model response part 2' }] },
468
+ it('should add a placeholder model turn when a tool call is followed by an empty response', () => {
469
+ // 1. Setup: A history where the model has just made a function call.
470
+ const initialHistory = [
471
+ { role: 'user', parts: [{ text: 'Initial prompt' }] },
472
+ {
473
+ role: 'model',
474
+ parts: [{ functionCall: { name: 'test_tool', args: {} } }],
475
+ },
209
476
  ];
210
- // @ts-expect-error Accessing private method for testing purposes
211
- chat.recordHistory(secondUserInput, secondModelOutput);
212
- const finalHistory = chat.getHistory();
213
- expect(finalHistory.length).toBe(4); // user1, model1, user2, model2(consolidated)
214
- expect(finalHistory[0]).toEqual(firstUserInput);
215
- expect(finalHistory[1]).toEqual(firstModelOutput[0]);
216
- expect(finalHistory[2]).toEqual(secondUserInput);
217
- expect(finalHistory[3].role).toBe('model');
218
- expect(finalHistory[3].parts).toEqual([
219
- { text: 'Second model response part 1Second model response part 2' },
220
- ]);
221
- });
222
- it('should correctly merge consolidated new output with existing model history', () => {
223
- // Setup: history ends with a model turn
224
- const initialUser = {
477
+ chat.setHistory(initialHistory);
478
+ // 2. Action: The user provides the tool's response, and the model's
479
+ // final output is empty (e.g., just a thought, which gets filtered out).
480
+ const functionResponse = {
225
481
  role: 'user',
226
- parts: [{ text: 'Initial user query' }],
482
+ parts: [{ functionResponse: { name: 'test_tool', response: {} } }],
227
483
  };
228
- const initialModel = {
229
- role: 'model',
230
- parts: [{ text: 'Initial model answer.' }],
231
- };
232
- chat = new GeminiChat(mockConfig, mockModelsModule, config, [
233
- initialUser,
234
- initialModel,
484
+ const emptyModelOutput = [];
485
+ // @ts-expect-error Accessing private method for testing
486
+ chat.recordHistory(functionResponse, emptyModelOutput, [
487
+ functionResponse,
235
488
  ]);
236
- // New interaction
237
- const currentUserInput = {
238
- role: 'user',
239
- parts: [{ text: 'Follow-up question' }],
240
- };
241
- const newModelParts = [
242
- { role: 'model', parts: [{ text: 'Part A of new answer.' }] },
243
- { role: 'model', parts: [{ text: 'Part B of new answer.' }] },
244
- ];
245
- // @ts-expect-error Accessing private method for testing purposes
246
- chat.recordHistory(currentUserInput, newModelParts);
489
+ // 3. Assert: The history should now have four valid, alternating turns.
247
490
  const history = chat.getHistory();
248
- // Expected: initialUser, initialModel, currentUserInput, consolidatedNewModelParts
249
491
  expect(history.length).toBe(4);
250
- expect(history[0]).toEqual(initialUser);
251
- expect(history[1]).toEqual(initialModel);
252
- expect(history[2]).toEqual(currentUserInput);
253
- expect(history[3].role).toBe('model');
254
- expect(history[3].parts).toEqual([
255
- { text: 'Part A of new answer.Part B of new answer.' },
256
- ]);
257
- });
258
- it('should handle empty modelOutput array', () => {
259
- // @ts-expect-error Accessing private method for testing purposes
260
- chat.recordHistory(userInput, []);
261
- const history = chat.getHistory();
262
- // If modelOutput is empty, it might push a default empty model part depending on isFunctionResponse
263
- // Assuming isFunctionResponse(userInput) is false for this simple text input
264
- expect(history.length).toBe(2);
265
- expect(history[0]).toEqual(userInput);
266
- expect(history[1].role).toBe('model');
267
- expect(history[1].parts).toEqual([]);
268
- });
269
- it('should handle aggregating modelOutput', () => {
270
- const modelOutputUndefinedParts = [
271
- { role: 'model', parts: [{ text: 'First model part' }] },
272
- { role: 'model', parts: [{ text: 'Second model part' }] },
273
- { role: 'model', parts: undefined }, // Test undefined parts
274
- { role: 'model', parts: [{ text: 'Third model part' }] },
275
- { role: 'model', parts: [] }, // Test empty parts array
276
- ];
277
- // @ts-expect-error Accessing private method for testing purposes
278
- chat.recordHistory(userInput, modelOutputUndefinedParts);
279
- const history = chat.getHistory();
280
- expect(history.length).toBe(5);
281
- expect(history[0]).toEqual(userInput);
282
- expect(history[1].role).toBe('model');
283
- expect(history[1].parts).toEqual([
284
- { text: 'First model partSecond model part' },
285
- ]);
286
- expect(history[2].role).toBe('model');
287
- expect(history[2].parts).toBeUndefined();
288
- expect(history[3].role).toBe('model');
289
- expect(history[3].parts).toEqual([{ text: 'Third model part' }]);
290
- expect(history[4].role).toBe('model');
291
- expect(history[4].parts).toEqual([]);
292
- });
293
- it('should handle modelOutput with parts being undefined or empty (if they pass initial every check)', () => {
294
- const modelOutputUndefinedParts = [
295
- { role: 'model', parts: [{ text: 'Text part' }] },
296
- { role: 'model', parts: undefined }, // Test undefined parts
297
- { role: 'model', parts: [] }, // Test empty parts array
298
- ];
299
- // @ts-expect-error Accessing private method for testing purposes
300
- chat.recordHistory(userInput, modelOutputUndefinedParts);
301
- const history = chat.getHistory();
302
- expect(history.length).toBe(4); // userInput, model1 (text), model2 (undefined parts), model3 (empty parts)
303
- expect(history[0]).toEqual(userInput);
304
- expect(history[1].role).toBe('model');
305
- expect(history[1].parts).toEqual([{ text: 'Text part' }]);
306
- expect(history[2].role).toBe('model');
307
- expect(history[2].parts).toBeUndefined();
308
- expect(history[3].role).toBe('model');
309
- expect(history[3].parts).toEqual([]);
492
+ // The final turn must be the empty model placeholder.
493
+ const lastTurn = history[3];
494
+ expect(lastTurn.role).toBe('model');
495
+ expect(lastTurn?.parts?.length).toBe(0);
496
+ // The second-to-last turn must be the function response we provided.
497
+ const secondToLastTurn = history[2];
498
+ expect(secondToLastTurn.role).toBe('user');
499
+ expect(secondToLastTurn?.parts[0].functionResponse).toBeDefined();
310
500
  });
311
- it('should correctly handle automaticFunctionCallingHistory', () => {
312
- const afcHistory = [
313
- { role: 'user', parts: [{ text: 'AFC User' }] },
314
- { role: 'model', parts: [{ text: 'AFC Model' }] },
315
- ];
316
- const modelOutput = [
317
- { role: 'model', parts: [{ text: 'Regular Model Output' }] },
318
- ];
319
- // @ts-expect-error Accessing private method for testing purposes
320
- chat.recordHistory(userInput, modelOutput, afcHistory);
321
- const history = chat.getHistory();
322
- expect(history.length).toBe(3);
323
- expect(history[0]).toEqual(afcHistory[0]);
324
- expect(history[1]).toEqual(afcHistory[1]);
325
- expect(history[2]).toEqual(modelOutput[0]);
326
- });
327
- it('should add userInput if AFC history is present but empty', () => {
501
+ it('should add user input and a single model output to history', () => {
328
502
  const modelOutput = [
329
- { role: 'model', parts: [{ text: 'Model Output' }] },
503
+ { role: 'model', parts: [{ text: 'Model output' }] },
330
504
  ];
331
- // @ts-expect-error Accessing private method for testing purposes
332
- chat.recordHistory(userInput, modelOutput, []); // Empty AFC history
505
+ // @ts-expect-error Accessing private method for testing
506
+ chat.recordHistory(userInput, modelOutput);
333
507
  const history = chat.getHistory();
334
508
  expect(history.length).toBe(2);
335
509
  expect(history[0]).toEqual(userInput);
336
510
  expect(history[1]).toEqual(modelOutput[0]);
337
511
  });
338
- it('should skip "thought" content from modelOutput', () => {
339
- const modelOutputWithThought = [
340
- { role: 'model', parts: [{ thought: true }, { text: 'Visible text' }] },
341
- { role: 'model', parts: [{ text: 'Another visible text' }] },
512
+ it('should consolidate adjacent text parts from multiple content objects', () => {
513
+ const modelOutput = [
514
+ { role: 'model', parts: [{ text: 'Part 1.' }] },
515
+ { role: 'model', parts: [{ text: ' Part 2.' }] },
516
+ { role: 'model', parts: [{ text: ' Part 3.' }] },
342
517
  ];
343
- // @ts-expect-error Accessing private method for testing purposes
344
- chat.recordHistory(userInput, modelOutputWithThought);
518
+ // @ts-expect-error Accessing private method for testing
519
+ chat.recordHistory(userInput, modelOutput);
345
520
  const history = chat.getHistory();
346
- expect(history.length).toBe(2); // User input + consolidated model output
347
- expect(history[0]).toEqual(userInput);
521
+ expect(history.length).toBe(2);
348
522
  expect(history[1].role).toBe('model');
349
- // The 'thought' part is skipped, 'Another visible text' becomes the first part.
350
- expect(history[1].parts).toEqual([{ text: 'Another visible text' }]);
351
- });
352
- it('should skip "thought" content even if it is the only content', () => {
353
- const modelOutputOnlyThought = [
354
- { role: 'model', parts: [{ thought: true }] },
355
- ];
356
- // @ts-expect-error Accessing private method for testing purposes
357
- chat.recordHistory(userInput, modelOutputOnlyThought);
358
- const history = chat.getHistory();
359
- expect(history.length).toBe(1); // User input + default empty model part
360
- expect(history[0]).toEqual(userInput);
523
+ expect(history[1].parts).toEqual([{ text: 'Part 1. Part 2. Part 3.' }]);
361
524
  });
362
- it('should correctly consolidate text parts when a thought part is in between', () => {
363
- const modelOutputMixed = [
364
- { role: 'model', parts: [{ text: 'Part 1.' }] },
365
- {
366
- role: 'model',
367
- parts: [{ thought: true }, { text: 'Should be skipped' }],
368
- },
369
- { role: 'model', parts: [{ text: 'Part 2.' }] },
370
- ];
371
- // @ts-expect-error Accessing private method for testing purposes
372
- chat.recordHistory(userInput, modelOutputMixed);
525
+ it('should add an empty placeholder turn if modelOutput is empty', () => {
526
+ // This simulates receiving a pre-filtered, thought-only response.
527
+ const emptyModelOutput = [];
528
+ // @ts-expect-error Accessing private method for testing
529
+ chat.recordHistory(userInput, emptyModelOutput);
373
530
  const history = chat.getHistory();
374
531
  expect(history.length).toBe(2);
375
532
  expect(history[0]).toEqual(userInput);
376
533
  expect(history[1].role).toBe('model');
377
- expect(history[1].parts).toEqual([{ text: 'Part 1.Part 2.' }]);
534
+ expect(history[1].parts).toEqual([]);
378
535
  });
379
- it('should handle multiple thought parts correctly', () => {
380
- const modelOutputMultipleThoughts = [
381
- { role: 'model', parts: [{ thought: true }] },
382
- { role: 'model', parts: [{ text: 'Visible 1' }] },
383
- { role: 'model', parts: [{ thought: true }] },
384
- { role: 'model', parts: [{ text: 'Visible 2' }] },
536
+ it('should preserve model outputs with undefined or empty parts arrays', () => {
537
+ const malformedOutput = [
538
+ { role: 'model', parts: [{ text: 'Text part' }] },
539
+ { role: 'model', parts: undefined },
540
+ { role: 'model', parts: [] },
385
541
  ];
386
- // @ts-expect-error Accessing private method for testing purposes
387
- chat.recordHistory(userInput, modelOutputMultipleThoughts);
542
+ // @ts-expect-error Accessing private method for testing
543
+ chat.recordHistory(userInput, malformedOutput);
388
544
  const history = chat.getHistory();
389
- expect(history.length).toBe(2);
390
- expect(history[0]).toEqual(userInput);
391
- expect(history[1].role).toBe('model');
392
- expect(history[1].parts).toEqual([{ text: 'Visible 1Visible 2' }]);
545
+ expect(history.length).toBe(4); // userInput + 3 model turns
546
+ expect(history[1].parts).toEqual([{ text: 'Text part' }]);
547
+ expect(history[2].parts).toBeUndefined();
548
+ expect(history[3].parts).toEqual([]);
393
549
  });
394
- it('should handle thought part at the end of outputContents', () => {
395
- const modelOutputThoughtAtEnd = [
396
- { role: 'model', parts: [{ text: 'Visible text' }] },
397
- { role: 'model', parts: [{ thought: true }] },
550
+ it('should not consolidate content with different roles', () => {
551
+ const mixedOutput = [
552
+ { role: 'model', parts: [{ text: 'Model 1' }] },
553
+ { role: 'user', parts: [{ text: 'Unexpected User' }] },
554
+ { role: 'model', parts: [{ text: 'Model 2' }] },
398
555
  ];
399
- // @ts-expect-error Accessing private method for testing purposes
400
- chat.recordHistory(userInput, modelOutputThoughtAtEnd);
556
+ // @ts-expect-error Accessing private method for testing
557
+ chat.recordHistory(userInput, mixedOutput);
401
558
  const history = chat.getHistory();
402
- expect(history.length).toBe(2);
403
- expect(history[0]).toEqual(userInput);
404
- expect(history[1].role).toBe('model');
405
- expect(history[1].parts).toEqual([{ text: 'Visible text' }]);
559
+ expect(history.length).toBe(4); // userInput, model1, unexpected_user, model2
560
+ expect(history[1]).toEqual(mixedOutput[0]);
561
+ expect(history[2]).toEqual(mixedOutput[1]);
562
+ expect(history[3]).toEqual(mixedOutput[2]);
406
563
  });
407
564
  });
408
565
  describe('addHistory', () => {
@@ -459,9 +616,9 @@ describe('GeminiChat', () => {
459
616
  chunks.push(chunk);
460
617
  }
461
618
  // Assertions
462
- expect(mockRecordInvalidChunk).toHaveBeenCalledTimes(1);
463
- expect(mockRecordContentRetry).toHaveBeenCalledTimes(1);
464
- expect(mockRecordContentRetryFailure).not.toHaveBeenCalled();
619
+ expect(mockLogInvalidChunk).toHaveBeenCalledTimes(1);
620
+ expect(mockLogContentRetry).toHaveBeenCalledTimes(1);
621
+ expect(mockLogContentRetryFailure).not.toHaveBeenCalled();
465
622
  expect(mockModelsModule.generateContentStream).toHaveBeenCalledTimes(2);
466
623
  expect(chunks.some((c) => c.candidates?.[0]?.content?.parts?.[0]?.text ===
467
624
  'Successful response')).toBe(true);
@@ -500,9 +657,9 @@ describe('GeminiChat', () => {
500
657
  await expect(consumeStreamAndExpectError()).rejects.toThrow(EmptyStreamError);
501
658
  // Should be called 3 times (initial + 2 retries)
502
659
  expect(mockModelsModule.generateContentStream).toHaveBeenCalledTimes(3);
503
- expect(mockRecordInvalidChunk).toHaveBeenCalledTimes(3);
504
- expect(mockRecordContentRetry).toHaveBeenCalledTimes(2);
505
- expect(mockRecordContentRetryFailure).toHaveBeenCalledTimes(1);
660
+ expect(mockLogInvalidChunk).toHaveBeenCalledTimes(3);
661
+ expect(mockLogContentRetry).toHaveBeenCalledTimes(2);
662
+ expect(mockLogContentRetryFailure).toHaveBeenCalledTimes(1);
506
663
  // History should be clean, as if the failed turn never happened.
507
664
  const history = chat.getHistory();
508
665
  expect(history.length).toBe(0);
@@ -538,7 +695,7 @@ describe('GeminiChat', () => {
538
695
  const history = chat.getHistory();
539
696
  expect(history.length).toBe(4);
540
697
  // Assert that the correct metrics were reported for one empty-stream retry
541
- expect(mockRecordContentRetry).toHaveBeenCalledTimes(1);
698
+ expect(mockLogContentRetry).toHaveBeenCalledTimes(1);
542
699
  // Explicitly verify the structure of each part to satisfy TypeScript
543
700
  const turn1 = history[0];
544
701
  if (!turn1?.parts?.[0] || !('text' in turn1.parts[0])) {