@google/gemini-cli-core 0.6.0-nightly.20250910.a31830a3 → 0.6.0-preview.10

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 (334) hide show
  1. package/LICENSE +2 -2
  2. package/README.md +12 -2
  3. package/dist/index.d.ts +4 -4
  4. package/dist/index.js +4 -4
  5. package/dist/index.js.map +1 -1
  6. package/dist/src/code_assist/converter.d.ts +1 -0
  7. package/dist/src/code_assist/converter.js +1 -0
  8. package/dist/src/code_assist/converter.js.map +1 -1
  9. package/dist/src/code_assist/converter.test.js +10 -0
  10. package/dist/src/code_assist/converter.test.js.map +1 -1
  11. package/dist/src/code_assist/oauth-credential-storage.d.ts +5 -7
  12. package/dist/src/code_assist/oauth-credential-storage.js +5 -8
  13. package/dist/src/code_assist/oauth-credential-storage.js.map +1 -1
  14. package/dist/src/code_assist/oauth-credential-storage.test.js +35 -33
  15. package/dist/src/code_assist/oauth-credential-storage.test.js.map +1 -1
  16. package/dist/src/code_assist/oauth2.js +28 -2
  17. package/dist/src/code_assist/oauth2.js.map +1 -1
  18. package/dist/src/code_assist/oauth2.test.js +674 -536
  19. package/dist/src/code_assist/oauth2.test.js.map +1 -1
  20. package/dist/src/config/config.d.ts +37 -1
  21. package/dist/src/config/config.js +74 -17
  22. package/dist/src/config/config.js.map +1 -1
  23. package/dist/src/config/config.test.js +104 -16
  24. package/dist/src/config/config.test.js.map +1 -1
  25. package/dist/src/config/models.d.ts +15 -0
  26. package/dist/src/config/models.js +27 -0
  27. package/dist/src/config/models.js.map +1 -1
  28. package/dist/src/config/models.test.d.ts +6 -0
  29. package/dist/src/config/models.test.js +55 -0
  30. package/dist/src/config/models.test.js.map +1 -0
  31. package/dist/src/confirmation-bus/index.d.ts +7 -0
  32. package/dist/src/confirmation-bus/index.js +8 -0
  33. package/dist/src/confirmation-bus/index.js.map +1 -0
  34. package/dist/src/confirmation-bus/message-bus.d.ts +17 -0
  35. package/dist/src/confirmation-bus/message-bus.js +81 -0
  36. package/dist/src/confirmation-bus/message-bus.js.map +1 -0
  37. package/dist/src/confirmation-bus/message-bus.test.d.ts +6 -0
  38. package/dist/src/confirmation-bus/message-bus.test.js +164 -0
  39. package/dist/src/confirmation-bus/message-bus.test.js.map +1 -0
  40. package/dist/src/confirmation-bus/types.d.ts +38 -0
  41. package/dist/src/confirmation-bus/types.js +15 -0
  42. package/dist/src/confirmation-bus/types.js.map +1 -0
  43. package/dist/src/core/baseLlmClient.d.ts +1 -0
  44. package/dist/src/core/baseLlmClient.js +24 -0
  45. package/dist/src/core/baseLlmClient.js.map +1 -1
  46. package/dist/src/core/baseLlmClient.test.js +63 -0
  47. package/dist/src/core/baseLlmClient.test.js.map +1 -1
  48. package/dist/src/core/client.d.ts +4 -3
  49. package/dist/src/core/client.js +76 -141
  50. package/dist/src/core/client.js.map +1 -1
  51. package/dist/src/core/client.test.js +218 -220
  52. package/dist/src/core/client.test.js.map +1 -1
  53. package/dist/src/core/contentGenerator.d.ts +0 -1
  54. package/dist/src/core/contentGenerator.js +0 -4
  55. package/dist/src/core/contentGenerator.js.map +1 -1
  56. package/dist/src/core/contentGenerator.test.js +0 -3
  57. package/dist/src/core/contentGenerator.test.js.map +1 -1
  58. package/dist/src/core/coreToolScheduler.d.ts +4 -3
  59. package/dist/src/core/coreToolScheduler.js +42 -5
  60. package/dist/src/core/coreToolScheduler.js.map +1 -1
  61. package/dist/src/core/coreToolScheduler.test.js +43 -0
  62. package/dist/src/core/coreToolScheduler.test.js.map +1 -1
  63. package/dist/src/core/geminiChat.d.ts +13 -30
  64. package/dist/src/core/geminiChat.js +88 -230
  65. package/dist/src/core/geminiChat.js.map +1 -1
  66. package/dist/src/core/geminiChat.test.js +268 -489
  67. package/dist/src/core/geminiChat.test.js.map +1 -1
  68. package/dist/src/core/loggingContentGenerator.js +5 -5
  69. package/dist/src/core/loggingContentGenerator.js.map +1 -1
  70. package/dist/src/core/nonInteractiveToolExecutor.test.js +49 -0
  71. package/dist/src/core/nonInteractiveToolExecutor.test.js.map +1 -1
  72. package/dist/src/core/subagent.js +1 -1
  73. package/dist/src/core/subagent.js.map +1 -1
  74. package/dist/src/core/subagent.test.js +9 -8
  75. package/dist/src/core/subagent.test.js.map +1 -1
  76. package/dist/src/core/turn.d.ts +2 -1
  77. package/dist/src/core/turn.js +2 -2
  78. package/dist/src/core/turn.js.map +1 -1
  79. package/dist/src/core/turn.test.js +18 -18
  80. package/dist/src/core/turn.test.js.map +1 -1
  81. package/dist/src/generated/git-commit.d.ts +2 -2
  82. package/dist/src/generated/git-commit.js +2 -2
  83. package/dist/src/generated/git-commit.js.map +1 -1
  84. package/dist/src/ide/constants.d.ts +1 -0
  85. package/dist/src/ide/constants.js +1 -0
  86. package/dist/src/ide/constants.js.map +1 -1
  87. package/dist/src/ide/ide-client.d.ts +51 -13
  88. package/dist/src/ide/ide-client.js +184 -37
  89. package/dist/src/ide/ide-client.js.map +1 -1
  90. package/dist/src/ide/ide-client.test.js +93 -3
  91. package/dist/src/ide/ide-client.test.js.map +1 -1
  92. package/dist/src/ide/ide-installer.js +8 -2
  93. package/dist/src/ide/ide-installer.js.map +1 -1
  94. package/dist/src/ide/ide-installer.test.js +13 -2
  95. package/dist/src/ide/ide-installer.test.js.map +1 -1
  96. package/dist/src/ide/ideContext.d.ts +34 -113
  97. package/dist/src/ide/ideContext.js +20 -78
  98. package/dist/src/ide/ideContext.js.map +1 -1
  99. package/dist/src/ide/ideContext.test.js +37 -39
  100. package/dist/src/ide/ideContext.test.js.map +1 -1
  101. package/dist/src/ide/types.d.ts +141 -0
  102. package/dist/src/ide/types.js +73 -0
  103. package/dist/src/ide/types.js.map +1 -1
  104. package/dist/src/index.d.ts +3 -1
  105. package/dist/src/index.js +3 -1
  106. package/dist/src/index.js.map +1 -1
  107. package/dist/src/mcp/oauth-token-storage.d.ts +2 -0
  108. package/dist/src/mcp/oauth-token-storage.js +25 -0
  109. package/dist/src/mcp/oauth-token-storage.js.map +1 -1
  110. package/dist/src/mcp/oauth-token-storage.test.js +251 -160
  111. package/dist/src/mcp/oauth-token-storage.test.js.map +1 -1
  112. package/dist/src/mcp/token-storage/index.d.ts +11 -0
  113. package/dist/src/mcp/token-storage/index.js +12 -0
  114. package/dist/src/mcp/token-storage/index.js.map +1 -0
  115. package/dist/src/output/json-formatter.d.ts +11 -0
  116. package/dist/src/output/json-formatter.js +30 -0
  117. package/dist/src/output/json-formatter.js.map +1 -0
  118. package/dist/src/output/json-formatter.test.d.ts +6 -0
  119. package/dist/src/output/json-formatter.test.js +266 -0
  120. package/dist/src/output/json-formatter.test.js.map +1 -0
  121. package/dist/src/output/types.d.ts +20 -0
  122. package/dist/src/output/types.js +11 -0
  123. package/dist/src/output/types.js.map +1 -0
  124. package/dist/src/policy/index.d.ts +7 -0
  125. package/dist/src/policy/index.js +8 -0
  126. package/dist/src/policy/index.js.map +1 -0
  127. package/dist/src/policy/policy-engine.d.ts +30 -0
  128. package/dist/src/policy/policy-engine.js +83 -0
  129. package/dist/src/policy/policy-engine.js.map +1 -0
  130. package/dist/src/policy/policy-engine.test.d.ts +6 -0
  131. package/dist/src/policy/policy-engine.test.js +470 -0
  132. package/dist/src/policy/policy-engine.test.js.map +1 -0
  133. package/dist/src/policy/stable-stringify.d.ts +58 -0
  134. package/dist/src/policy/stable-stringify.js +122 -0
  135. package/dist/src/policy/stable-stringify.js.map +1 -0
  136. package/dist/src/policy/types.d.ts +47 -0
  137. package/dist/src/policy/types.js +12 -0
  138. package/dist/src/policy/types.js.map +1 -0
  139. package/dist/src/routing/modelRouterService.d.ts +23 -0
  140. package/dist/src/routing/modelRouterService.js +70 -0
  141. package/dist/src/routing/modelRouterService.js.map +1 -0
  142. package/dist/src/routing/modelRouterService.test.d.ts +6 -0
  143. package/dist/src/routing/modelRouterService.test.js +98 -0
  144. package/dist/src/routing/modelRouterService.test.js.map +1 -0
  145. package/dist/src/routing/routingStrategy.d.ts +62 -0
  146. package/dist/src/routing/routingStrategy.js +7 -0
  147. package/dist/src/routing/routingStrategy.js.map +1 -0
  148. package/dist/src/routing/strategies/classifierStrategy.d.ts +12 -0
  149. package/dist/src/routing/strategies/classifierStrategy.js +173 -0
  150. package/dist/src/routing/strategies/classifierStrategy.js.map +1 -0
  151. package/dist/src/routing/strategies/classifierStrategy.test.d.ts +6 -0
  152. package/dist/src/routing/strategies/classifierStrategy.test.js +192 -0
  153. package/dist/src/routing/strategies/classifierStrategy.test.js.map +1 -0
  154. package/dist/src/routing/strategies/compositeStrategy.d.ts +26 -0
  155. package/dist/src/routing/strategies/compositeStrategy.js +67 -0
  156. package/dist/src/routing/strategies/compositeStrategy.js.map +1 -0
  157. package/dist/src/routing/strategies/compositeStrategy.test.d.ts +6 -0
  158. package/dist/src/routing/strategies/compositeStrategy.test.js +123 -0
  159. package/dist/src/routing/strategies/compositeStrategy.test.js.map +1 -0
  160. package/dist/src/routing/strategies/defaultStrategy.d.ts +12 -0
  161. package/dist/src/routing/strategies/defaultStrategy.js +20 -0
  162. package/dist/src/routing/strategies/defaultStrategy.js.map +1 -0
  163. package/dist/src/routing/strategies/defaultStrategy.test.d.ts +6 -0
  164. package/dist/src/routing/strategies/defaultStrategy.test.js +26 -0
  165. package/dist/src/routing/strategies/defaultStrategy.test.js.map +1 -0
  166. package/dist/src/routing/strategies/fallbackStrategy.d.ts +12 -0
  167. package/dist/src/routing/strategies/fallbackStrategy.js +25 -0
  168. package/dist/src/routing/strategies/fallbackStrategy.js.map +1 -0
  169. package/dist/src/routing/strategies/fallbackStrategy.test.d.ts +6 -0
  170. package/dist/src/routing/strategies/fallbackStrategy.test.js +55 -0
  171. package/dist/src/routing/strategies/fallbackStrategy.test.js.map +1 -0
  172. package/dist/src/routing/strategies/overrideStrategy.d.ts +15 -0
  173. package/dist/src/routing/strategies/overrideStrategy.js +28 -0
  174. package/dist/src/routing/strategies/overrideStrategy.js.map +1 -0
  175. package/dist/src/routing/strategies/overrideStrategy.test.d.ts +6 -0
  176. package/dist/src/routing/strategies/overrideStrategy.test.js +42 -0
  177. package/dist/src/routing/strategies/overrideStrategy.test.js.map +1 -0
  178. package/dist/src/services/chatRecordingService.d.ts +2 -1
  179. package/dist/src/services/chatRecordingService.js +3 -3
  180. package/dist/src/services/chatRecordingService.js.map +1 -1
  181. package/dist/src/services/chatRecordingService.test.js +8 -3
  182. package/dist/src/services/chatRecordingService.test.js.map +1 -1
  183. package/dist/src/services/fileDiscoveryService.d.ts +10 -0
  184. package/dist/src/services/fileDiscoveryService.js +31 -17
  185. package/dist/src/services/fileDiscoveryService.js.map +1 -1
  186. package/dist/src/services/gitService.js +9 -12
  187. package/dist/src/services/gitService.js.map +1 -1
  188. package/dist/src/services/gitService.test.js +10 -20
  189. package/dist/src/services/gitService.test.js.map +1 -1
  190. package/dist/src/services/loopDetectionService.d.ts +5 -0
  191. package/dist/src/services/loopDetectionService.js +36 -20
  192. package/dist/src/services/loopDetectionService.js.map +1 -1
  193. package/dist/src/services/loopDetectionService.test.js +41 -12
  194. package/dist/src/services/loopDetectionService.test.js.map +1 -1
  195. package/dist/src/services/shellExecutionService.d.ts +34 -2
  196. package/dist/src/services/shellExecutionService.js +192 -43
  197. package/dist/src/services/shellExecutionService.js.map +1 -1
  198. package/dist/src/services/shellExecutionService.test.js +184 -55
  199. package/dist/src/services/shellExecutionService.test.js.map +1 -1
  200. package/dist/src/telemetry/clearcut-logger/clearcut-logger.d.ts +12 -2
  201. package/dist/src/telemetry/clearcut-logger/clearcut-logger.js +100 -5
  202. package/dist/src/telemetry/clearcut-logger/clearcut-logger.js.map +1 -1
  203. package/dist/src/telemetry/clearcut-logger/clearcut-logger.test.js +63 -5
  204. package/dist/src/telemetry/clearcut-logger/clearcut-logger.test.js.map +1 -1
  205. package/dist/src/telemetry/clearcut-logger/event-metadata-key.d.ts +13 -2
  206. package/dist/src/telemetry/clearcut-logger/event-metadata-key.js +33 -2
  207. package/dist/src/telemetry/clearcut-logger/event-metadata-key.js.map +1 -1
  208. package/dist/src/telemetry/constants.d.ts +6 -0
  209. package/dist/src/telemetry/constants.js +6 -0
  210. package/dist/src/telemetry/constants.js.map +1 -1
  211. package/dist/src/telemetry/gcp-exporters.d.ts +34 -0
  212. package/dist/src/telemetry/gcp-exporters.js +117 -0
  213. package/dist/src/telemetry/gcp-exporters.js.map +1 -0
  214. package/dist/src/telemetry/gcp-exporters.test.d.ts +6 -0
  215. package/dist/src/telemetry/gcp-exporters.test.js +318 -0
  216. package/dist/src/telemetry/gcp-exporters.test.js.map +1 -0
  217. package/dist/src/telemetry/index.d.ts +3 -2
  218. package/dist/src/telemetry/index.js +3 -2
  219. package/dist/src/telemetry/index.js.map +1 -1
  220. package/dist/src/telemetry/loggers.d.ts +7 -1
  221. package/dist/src/telemetry/loggers.js +97 -7
  222. package/dist/src/telemetry/loggers.js.map +1 -1
  223. package/dist/src/telemetry/loggers.test.js +171 -37
  224. package/dist/src/telemetry/loggers.test.js.map +1 -1
  225. package/dist/src/telemetry/metrics.d.ts +3 -1
  226. package/dist/src/telemetry/metrics.js +32 -3
  227. package/dist/src/telemetry/metrics.js.map +1 -1
  228. package/dist/src/telemetry/metrics.test.js +42 -0
  229. package/dist/src/telemetry/metrics.test.js.map +1 -1
  230. package/dist/src/telemetry/sdk.js +19 -1
  231. package/dist/src/telemetry/sdk.js.map +1 -1
  232. package/dist/src/telemetry/sdk.test.js +95 -0
  233. package/dist/src/telemetry/sdk.test.js.map +1 -1
  234. package/dist/src/telemetry/types.d.ts +54 -3
  235. package/dist/src/telemetry/types.js +79 -3
  236. package/dist/src/telemetry/types.js.map +1 -1
  237. package/dist/src/tools/edit.js +6 -5
  238. package/dist/src/tools/edit.js.map +1 -1
  239. package/dist/src/tools/edit.test.js +79 -9
  240. package/dist/src/tools/edit.test.js.map +1 -1
  241. package/dist/src/tools/glob.d.ts +5 -1
  242. package/dist/src/tools/glob.js +24 -17
  243. package/dist/src/tools/glob.js.map +1 -1
  244. package/dist/src/tools/glob.test.js +51 -0
  245. package/dist/src/tools/glob.test.js.map +1 -1
  246. package/dist/src/tools/ls.js +19 -32
  247. package/dist/src/tools/ls.js.map +1 -1
  248. package/dist/src/tools/ls.test.js +140 -280
  249. package/dist/src/tools/ls.test.js.map +1 -1
  250. package/dist/src/tools/read-many-files.d.ts +1 -1
  251. package/dist/src/tools/read-many-files.js +17 -49
  252. package/dist/src/tools/read-many-files.js.map +1 -1
  253. package/dist/src/tools/ripGrep.d.ts +4 -0
  254. package/dist/src/tools/ripGrep.js +11 -1
  255. package/dist/src/tools/ripGrep.js.map +1 -1
  256. package/dist/src/tools/ripGrep.test.js +51 -1
  257. package/dist/src/tools/ripGrep.test.js.map +1 -1
  258. package/dist/src/tools/shell.d.ts +12 -2
  259. package/dist/src/tools/shell.js +20 -27
  260. package/dist/src/tools/shell.js.map +1 -1
  261. package/dist/src/tools/shell.test.js +33 -68
  262. package/dist/src/tools/shell.test.js.map +1 -1
  263. package/dist/src/tools/smart-edit.d.ts +0 -1
  264. package/dist/src/tools/smart-edit.js +5 -18
  265. package/dist/src/tools/smart-edit.js.map +1 -1
  266. package/dist/src/tools/smart-edit.test.js +18 -9
  267. package/dist/src/tools/smart-edit.test.js.map +1 -1
  268. package/dist/src/tools/tool-registry.js +1 -0
  269. package/dist/src/tools/tool-registry.js.map +1 -1
  270. package/dist/src/tools/tools.d.ts +8 -5
  271. package/dist/src/tools/tools.js +9 -2
  272. package/dist/src/tools/tools.js.map +1 -1
  273. package/dist/src/tools/write-file.js +4 -5
  274. package/dist/src/tools/write-file.js.map +1 -1
  275. package/dist/src/tools/write-file.test.js +94 -10
  276. package/dist/src/tools/write-file.test.js.map +1 -1
  277. package/dist/src/utils/bfsFileSearch.js +11 -5
  278. package/dist/src/utils/bfsFileSearch.js.map +1 -1
  279. package/dist/src/utils/editCorrector.d.ts +7 -6
  280. package/dist/src/utils/editCorrector.js +61 -18
  281. package/dist/src/utils/editCorrector.js.map +1 -1
  282. package/dist/src/utils/editCorrector.test.js +30 -79
  283. package/dist/src/utils/editCorrector.test.js.map +1 -1
  284. package/dist/src/utils/editor.js +31 -44
  285. package/dist/src/utils/editor.js.map +1 -1
  286. package/dist/src/utils/editor.test.js +61 -75
  287. package/dist/src/utils/editor.test.js.map +1 -1
  288. package/dist/src/utils/errorParsing.js +2 -2
  289. package/dist/src/utils/errorParsing.js.map +1 -1
  290. package/dist/src/utils/errorParsing.test.js +7 -7
  291. package/dist/src/utils/errorParsing.test.js.map +1 -1
  292. package/dist/src/utils/errors.d.ts +6 -0
  293. package/dist/src/utils/errors.js +10 -0
  294. package/dist/src/utils/errors.js.map +1 -1
  295. package/dist/src/utils/fileUtils.test.js +17 -8
  296. package/dist/src/utils/fileUtils.test.js.map +1 -1
  297. package/dist/src/utils/geminiIgnoreParser.d.ts +18 -0
  298. package/dist/src/utils/geminiIgnoreParser.js +61 -0
  299. package/dist/src/utils/geminiIgnoreParser.js.map +1 -0
  300. package/dist/src/utils/geminiIgnoreParser.test.d.ts +6 -0
  301. package/dist/src/utils/geminiIgnoreParser.test.js +50 -0
  302. package/dist/src/utils/geminiIgnoreParser.test.js.map +1 -0
  303. package/dist/src/utils/gitIgnoreParser.d.ts +3 -9
  304. package/dist/src/utils/gitIgnoreParser.js +60 -69
  305. package/dist/src/utils/gitIgnoreParser.js.map +1 -1
  306. package/dist/src/utils/gitIgnoreParser.test.js +18 -53
  307. package/dist/src/utils/gitIgnoreParser.test.js.map +1 -1
  308. package/dist/src/utils/memoryDiscovery.test.js +12 -6
  309. package/dist/src/utils/memoryDiscovery.test.js.map +1 -1
  310. package/dist/src/utils/nextSpeakerChecker.d.ts +2 -2
  311. package/dist/src/utils/nextSpeakerChecker.js +8 -2
  312. package/dist/src/utils/nextSpeakerChecker.js.map +1 -1
  313. package/dist/src/utils/nextSpeakerChecker.test.js +40 -33
  314. package/dist/src/utils/nextSpeakerChecker.test.js.map +1 -1
  315. package/dist/src/utils/shell-utils.d.ts +5 -0
  316. package/dist/src/utils/shell-utils.js +23 -0
  317. package/dist/src/utils/shell-utils.js.map +1 -1
  318. package/dist/src/utils/terminalSerializer.d.ts +28 -0
  319. package/dist/src/utils/terminalSerializer.js +432 -0
  320. package/dist/src/utils/terminalSerializer.js.map +1 -0
  321. package/dist/src/utils/terminalSerializer.test.d.ts +6 -0
  322. package/dist/src/utils/terminalSerializer.test.js +176 -0
  323. package/dist/src/utils/terminalSerializer.test.js.map +1 -0
  324. package/dist/src/utils/textUtils.d.ts +5 -0
  325. package/dist/src/utils/textUtils.js +14 -0
  326. package/dist/src/utils/textUtils.js.map +1 -1
  327. package/dist/src/utils/textUtils.test.d.ts +6 -0
  328. package/dist/src/utils/textUtils.test.js +59 -0
  329. package/dist/src/utils/textUtils.test.js.map +1 -0
  330. package/dist/tsconfig.tsbuildinfo +1 -1
  331. package/package.json +5 -1
  332. package/dist/src/utils/ide-trust.d.ts +0 -10
  333. package/dist/src/utils/ide-trust.js +0 -14
  334. package/dist/src/utils/ide-trust.js.map +0 -1
@@ -9,6 +9,7 @@ import { setSimulate429 } from '../utils/testUtils.js';
9
9
  import { DEFAULT_GEMINI_FLASH_MODEL } from '../config/models.js';
10
10
  import { AuthType } from './contentGenerator.js';
11
11
  import {} from '../utils/retry.js';
12
+ import { Kind } from '../tools/tools.js';
12
13
  // Mock fs module to prevent actual file system operations during tests
13
14
  const mockFileSystem = new Map();
14
15
  vi.mock('node:fs', () => {
@@ -105,206 +106,6 @@ describe('GeminiChat', () => {
105
106
  vi.restoreAllMocks();
106
107
  vi.resetAllMocks();
107
108
  });
108
- describe('sendMessage', () => {
109
- it('should retain the initial user message when an automatic function call occurs', async () => {
110
- // 1. Define the user's initial text message. This is the turn that gets dropped by the buggy logic.
111
- const userInitialMessage = {
112
- role: 'user',
113
- parts: [{ text: 'How is the weather in Boston?' }],
114
- };
115
- // 2. Mock the full API response, including the automaticFunctionCallingHistory.
116
- // This history represents the full turn: user asks, model calls tool, tool responds, model answers.
117
- const mockAfcResponse = {
118
- candidates: [
119
- {
120
- content: {
121
- role: 'model',
122
- parts: [
123
- { text: 'The weather in Boston is 72 degrees and sunny.' },
124
- ],
125
- },
126
- },
127
- ],
128
- automaticFunctionCallingHistory: [
129
- userInitialMessage, // The user's turn
130
- {
131
- // The model's first response: a tool call
132
- role: 'model',
133
- parts: [
134
- {
135
- functionCall: {
136
- name: 'get_weather',
137
- args: { location: 'Boston' },
138
- },
139
- },
140
- ],
141
- },
142
- {
143
- // The tool's response, which has a 'user' role
144
- role: 'user',
145
- parts: [
146
- {
147
- functionResponse: {
148
- name: 'get_weather',
149
- response: { temperature: 72, condition: 'sunny' },
150
- },
151
- },
152
- ],
153
- },
154
- ],
155
- };
156
- vi.mocked(mockContentGenerator.generateContent).mockResolvedValue(mockAfcResponse);
157
- // 3. Action: Send the initial message.
158
- await chat.sendMessage({ message: 'How is the weather in Boston?' }, 'prompt-id-afc-bug');
159
- // 4. Assert: Check the final state of the history.
160
- const history = chat.getHistory();
161
- // With the bug, history.length will be 3, because the first user message is dropped.
162
- // The correct behavior is for the history to contain all 4 turns.
163
- expect(history.length).toBe(4);
164
- // Crucially, assert that the very first turn in the history matches the user's initial message.
165
- // This is the assertion that will fail.
166
- const firstTurn = history[0];
167
- expect(firstTurn.role).toBe('user');
168
- expect(firstTurn?.parts[0].text).toBe('How is the weather in Boston?');
169
- // Verify the rest of the history is also correct.
170
- const secondTurn = history[1];
171
- expect(secondTurn.role).toBe('model');
172
- expect(secondTurn?.parts[0].functionCall).toBeDefined();
173
- const thirdTurn = history[2];
174
- expect(thirdTurn.role).toBe('user');
175
- expect(thirdTurn?.parts[0].functionResponse).toBeDefined();
176
- const fourthTurn = history[3];
177
- expect(fourthTurn.role).toBe('model');
178
- expect(fourthTurn?.parts[0].text).toContain('72 degrees and sunny');
179
- });
180
- it('should throw an error when attempting to add a user turn after another user turn', async () => {
181
- // 1. Setup: Create a history that already ends with a user turn (a functionResponse).
182
- const initialHistory = [
183
- { role: 'user', parts: [{ text: 'Initial prompt' }] },
184
- {
185
- role: 'model',
186
- parts: [{ functionCall: { name: 'test_tool', args: {} } }],
187
- },
188
- {
189
- role: 'user',
190
- parts: [{ functionResponse: { name: 'test_tool', response: {} } }],
191
- },
192
- ];
193
- chat.setHistory(initialHistory);
194
- // 2. Mock a valid model response so the call doesn't fail for other reasons.
195
- const mockResponse = {
196
- candidates: [
197
- { content: { role: 'model', parts: [{ text: 'some response' }] } },
198
- ],
199
- };
200
- vi.mocked(mockContentGenerator.generateContent).mockResolvedValue(mockResponse);
201
- // 3. Action & Assert: Expect that sending another user message immediately
202
- // after a user-role turn throws the specific error.
203
- 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.');
204
- });
205
- it('should preserve text parts that are in the same response as a thought', async () => {
206
- // 1. Mock the API to return a single response containing both a thought and visible text.
207
- const mixedContentResponse = {
208
- candidates: [
209
- {
210
- content: {
211
- role: 'model',
212
- parts: [
213
- { thought: 'This is a thought.' },
214
- { text: 'This is the visible text that should not be lost.' },
215
- ],
216
- },
217
- },
218
- ],
219
- };
220
- vi.mocked(mockContentGenerator.generateContent).mockResolvedValue(mixedContentResponse);
221
- // 2. Action: Send a standard, non-streaming message.
222
- await chat.sendMessage({ message: 'test message' }, 'prompt-id-mixed-response');
223
- // 3. Assert: Check the final state of the history.
224
- const history = chat.getHistory();
225
- // The history should contain two turns: the user's message and the model's response.
226
- expect(history.length).toBe(2);
227
- const modelTurn = history[1];
228
- expect(modelTurn.role).toBe('model');
229
- // CRUCIAL ASSERTION:
230
- // Buggy code would discard the entire response because a "thought" was present,
231
- // resulting in an empty placeholder turn with 0 parts.
232
- // The corrected code will pass, preserving the single visible text part.
233
- expect(modelTurn?.parts?.length).toBe(1);
234
- expect(modelTurn?.parts[0].text).toBe('This is the visible text that should not be lost.');
235
- });
236
- it('should add a placeholder model turn when a tool call is followed by an empty model response', async () => {
237
- // 1. Setup: A history where the model has just made a function call.
238
- const initialHistory = [
239
- {
240
- role: 'user',
241
- parts: [{ text: 'Find a good Italian restaurant for me.' }],
242
- },
243
- {
244
- role: 'model',
245
- parts: [
246
- {
247
- functionCall: {
248
- name: 'find_restaurant',
249
- args: { cuisine: 'Italian' },
250
- },
251
- },
252
- ],
253
- },
254
- ];
255
- chat.setHistory(initialHistory);
256
- // 2. Mock the API to return an empty/thought-only response.
257
- const emptyModelResponse = {
258
- candidates: [
259
- { content: { role: 'model', parts: [{ thought: true }] } },
260
- ],
261
- };
262
- vi.mocked(mockContentGenerator.generateContent).mockResolvedValue(emptyModelResponse);
263
- // 3. Action: Send the function response back to the model.
264
- await chat.sendMessage({
265
- message: {
266
- functionResponse: {
267
- name: 'find_restaurant',
268
- response: { name: 'Vesuvio' },
269
- },
270
- },
271
- }, 'prompt-id-1');
272
- // 4. Assert: The history should now have four valid, alternating turns.
273
- const history = chat.getHistory();
274
- expect(history.length).toBe(4);
275
- // The final turn must be the empty model placeholder.
276
- const lastTurn = history[3];
277
- expect(lastTurn.role).toBe('model');
278
- expect(lastTurn?.parts?.length).toBe(0);
279
- // The second-to-last turn must be the function response we sent.
280
- const secondToLastTurn = history[2];
281
- expect(secondToLastTurn.role).toBe('user');
282
- expect(secondToLastTurn?.parts[0].functionResponse).toBeDefined();
283
- });
284
- it('should call generateContent with the correct parameters', async () => {
285
- const response = {
286
- candidates: [
287
- {
288
- content: {
289
- parts: [{ text: 'response' }],
290
- role: 'model',
291
- },
292
- finishReason: 'STOP',
293
- index: 0,
294
- safetyRatings: [],
295
- },
296
- ],
297
- text: () => 'response',
298
- };
299
- vi.mocked(mockContentGenerator.generateContent).mockResolvedValue(response);
300
- await chat.sendMessage({ message: 'hello' }, 'prompt-id-1');
301
- expect(mockContentGenerator.generateContent).toHaveBeenCalledWith({
302
- model: 'gemini-pro',
303
- contents: [{ role: 'user', parts: [{ text: 'hello' }] }],
304
- config: {},
305
- }, 'prompt-id-1');
306
- });
307
- });
308
109
  describe('sendMessageStream', () => {
309
110
  it('should succeed if a tool call is followed by an empty part', async () => {
310
111
  // 1. Mock a stream that contains a tool call, then an invalid (empty) part.
@@ -334,7 +135,7 @@ describe('GeminiChat', () => {
334
135
  vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(streamWithToolCall);
335
136
  // 2. Action & Assert: The stream processing should complete without throwing an error
336
137
  // because the presence of a tool call makes the empty final chunk acceptable.
337
- const stream = await chat.sendMessageStream({ message: 'test message' }, 'prompt-id-tool-call-empty-end');
138
+ const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-tool-call-empty-end');
338
139
  await expect((async () => {
339
140
  for await (const _ of stream) {
340
141
  /* consume stream */
@@ -374,7 +175,7 @@ describe('GeminiChat', () => {
374
175
  })();
375
176
  vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(streamWithNoFinish);
376
177
  // 2. Action & Assert: The stream should fail because there's no finish reason.
377
- const stream = await chat.sendMessageStream({ message: 'test message' }, 'prompt-id-no-finish-empty-end');
178
+ const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-no-finish-empty-end');
378
179
  await expect((async () => {
379
180
  for await (const _ of stream) {
380
181
  /* consume stream */
@@ -409,7 +210,7 @@ describe('GeminiChat', () => {
409
210
  })();
410
211
  vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(streamWithInvalidEnd);
411
212
  // 2. Action & Assert: The stream should complete without throwing an error.
412
- const stream = await chat.sendMessageStream({ message: 'test message' }, 'prompt-id-valid-then-invalid-end');
213
+ const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-valid-then-invalid-end');
413
214
  await expect((async () => {
414
215
  for await (const _ of stream) {
415
216
  /* consume stream */
@@ -422,55 +223,6 @@ describe('GeminiChat', () => {
422
223
  expect(modelTurn?.parts?.length).toBe(1);
423
224
  expect(modelTurn?.parts[0].text).toBe('Initial valid content...');
424
225
  });
425
- it('should not consolidate text into a part that also contains a functionCall', async () => {
426
- // 1. Mock the API to stream a malformed part followed by a valid text part.
427
- const multiChunkStream = (async function* () {
428
- // This malformed part has both text and a functionCall.
429
- yield {
430
- candidates: [
431
- {
432
- content: {
433
- role: 'model',
434
- parts: [
435
- {
436
- text: 'Some text',
437
- functionCall: { name: 'do_stuff', args: {} },
438
- },
439
- ],
440
- },
441
- },
442
- ],
443
- };
444
- // This valid text part should NOT be merged into the malformed one.
445
- yield {
446
- candidates: [
447
- {
448
- content: {
449
- role: 'model',
450
- parts: [{ text: ' that should not be merged.' }],
451
- },
452
- },
453
- ],
454
- };
455
- })();
456
- vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(multiChunkStream);
457
- // 2. Action: Send a message and consume the stream.
458
- const stream = await chat.sendMessageStream({ message: 'test message' }, 'prompt-id-malformed-chunk');
459
- for await (const _ of stream) {
460
- // Consume the stream to trigger history recording.
461
- }
462
- // 3. Assert: Check that the final history was not incorrectly consolidated.
463
- const history = chat.getHistory();
464
- expect(history.length).toBe(2);
465
- const modelTurn = history[1];
466
- // CRUCIAL ASSERTION: There should be two separate parts.
467
- // The old, non-strict logic would incorrectly merge them, resulting in one part.
468
- expect(modelTurn?.parts?.length).toBe(2);
469
- // Verify the contents of each part.
470
- expect(modelTurn?.parts[0].text).toBe('Some text');
471
- expect(modelTurn?.parts[0].functionCall).toBeDefined();
472
- expect(modelTurn?.parts[1].text).toBe(' that should not be merged.');
473
- });
474
226
  it('should consolidate subsequent text chunks after receiving an empty text chunk', async () => {
475
227
  // 1. Mock the API to return a stream where one chunk is just an empty text part.
476
228
  const multiChunkStream = (async function* () {
@@ -493,7 +245,7 @@ describe('GeminiChat', () => {
493
245
  })();
494
246
  vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(multiChunkStream);
495
247
  // 2. Action: Send a message and consume the stream.
496
- const stream = await chat.sendMessageStream({ message: 'test message' }, 'prompt-id-empty-chunk-consolidation');
248
+ const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-empty-chunk-consolidation');
497
249
  for await (const _ of stream) {
498
250
  // Consume the stream
499
251
  }
@@ -541,7 +293,7 @@ describe('GeminiChat', () => {
541
293
  })();
542
294
  vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(multiChunkStream);
543
295
  // 2. Action: Send a message and consume the stream.
544
- const stream = await chat.sendMessageStream({ message: 'test message' }, 'prompt-id-multi-chunk');
296
+ const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-multi-chunk');
545
297
  for await (const _ of stream) {
546
298
  // Consume the stream to trigger history recording.
547
299
  }
@@ -577,7 +329,7 @@ describe('GeminiChat', () => {
577
329
  })();
578
330
  vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(mixedContentStream);
579
331
  // 2. Action: Send a message and fully consume the stream to trigger history recording.
580
- const stream = await chat.sendMessageStream({ message: 'test message' }, 'prompt-id-mixed-chunk');
332
+ const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-mixed-chunk');
581
333
  for await (const _ of stream) {
582
334
  // This loop consumes the stream.
583
335
  }
@@ -626,7 +378,7 @@ describe('GeminiChat', () => {
626
378
  })();
627
379
  vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(emptyStreamResponse);
628
380
  // 3. Action: Send the function response back to the model and consume the stream.
629
- const stream = await chat.sendMessageStream({
381
+ const stream = await chat.sendMessageStream('test-model', {
630
382
  message: {
631
383
  functionResponse: {
632
384
  name: 'find_restaurant',
@@ -667,146 +419,22 @@ describe('GeminiChat', () => {
667
419
  };
668
420
  })();
669
421
  vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(response);
670
- const stream = await chat.sendMessageStream({ message: 'hello' }, 'prompt-id-1');
422
+ const stream = await chat.sendMessageStream('test-model', { message: 'hello' }, 'prompt-id-1');
671
423
  for await (const _ of stream) {
672
- // consume stream to trigger internal logic
424
+ // consume stream
673
425
  }
674
426
  expect(mockContentGenerator.generateContentStream).toHaveBeenCalledWith({
675
- model: 'gemini-pro',
676
- contents: [{ role: 'user', parts: [{ text: 'hello' }] }],
427
+ model: 'test-model',
428
+ contents: [
429
+ {
430
+ role: 'user',
431
+ parts: [{ text: 'hello' }],
432
+ },
433
+ ],
677
434
  config: {},
678
435
  }, 'prompt-id-1');
679
436
  });
680
437
  });
681
- describe('recordHistory', () => {
682
- const userInput = {
683
- role: 'user',
684
- parts: [{ text: 'User input' }],
685
- };
686
- it('should consolidate all consecutive model turns into a single turn', () => {
687
- const userInput = {
688
- role: 'user',
689
- parts: [{ text: 'User input' }],
690
- };
691
- // This simulates a multi-part model response with different part types.
692
- const modelOutput = [
693
- { role: 'model', parts: [{ text: 'Thinking...' }] },
694
- {
695
- role: 'model',
696
- parts: [{ functionCall: { name: 'do_stuff', args: {} } }],
697
- },
698
- ];
699
- // @ts-expect-error Accessing private method for testing
700
- chat.recordHistory(userInput, modelOutput);
701
- const history = chat.getHistory();
702
- // The history should contain the user's turn and ONE consolidated model turn.
703
- // The old code would fail here, resulting in a length of 3.
704
- //expect(history).toBe([]);
705
- expect(history.length).toBe(2);
706
- const modelTurn = history[1];
707
- expect(modelTurn.role).toBe('model');
708
- // The consolidated turn should contain both the text part and the functionCall part.
709
- expect(modelTurn?.parts?.length).toBe(2);
710
- expect(modelTurn?.parts[0].text).toBe('Thinking...');
711
- expect(modelTurn?.parts[1].functionCall).toBeDefined();
712
- });
713
- it('should add a placeholder model turn when a tool call is followed by an empty response', () => {
714
- // 1. Setup: A history where the model has just made a function call.
715
- const initialHistory = [
716
- { role: 'user', parts: [{ text: 'Initial prompt' }] },
717
- {
718
- role: 'model',
719
- parts: [{ functionCall: { name: 'test_tool', args: {} } }],
720
- },
721
- ];
722
- chat.setHistory(initialHistory);
723
- // 2. Action: The user provides the tool's response, and the model's
724
- // final output is empty (e.g., just a thought, which gets filtered out).
725
- const functionResponse = {
726
- role: 'user',
727
- parts: [{ functionResponse: { name: 'test_tool', response: {} } }],
728
- };
729
- const emptyModelOutput = [];
730
- // @ts-expect-error Accessing private method for testing
731
- chat.recordHistory(functionResponse, emptyModelOutput, [
732
- functionResponse,
733
- ]);
734
- // 3. Assert: The history should now have four valid, alternating turns.
735
- const history = chat.getHistory();
736
- expect(history.length).toBe(4);
737
- // The final turn must be the empty model placeholder.
738
- const lastTurn = history[3];
739
- expect(lastTurn.role).toBe('model');
740
- expect(lastTurn?.parts?.length).toBe(0);
741
- // The second-to-last turn must be the function response we provided.
742
- const secondToLastTurn = history[2];
743
- expect(secondToLastTurn.role).toBe('user');
744
- expect(secondToLastTurn?.parts[0].functionResponse).toBeDefined();
745
- });
746
- it('should add user input and a single model output to history', () => {
747
- const modelOutput = [
748
- { role: 'model', parts: [{ text: 'Model output' }] },
749
- ];
750
- // @ts-expect-error Accessing private method for testing
751
- chat.recordHistory(userInput, modelOutput);
752
- const history = chat.getHistory();
753
- expect(history.length).toBe(2);
754
- expect(history[0]).toEqual(userInput);
755
- expect(history[1]).toEqual(modelOutput[0]);
756
- });
757
- it('should consolidate adjacent text parts from multiple content objects', () => {
758
- const modelOutput = [
759
- { role: 'model', parts: [{ text: 'Part 1.' }] },
760
- { role: 'model', parts: [{ text: ' Part 2.' }] },
761
- { role: 'model', parts: [{ text: ' Part 3.' }] },
762
- ];
763
- // @ts-expect-error Accessing private method for testing
764
- chat.recordHistory(userInput, modelOutput);
765
- const history = chat.getHistory();
766
- expect(history.length).toBe(2);
767
- expect(history[1].role).toBe('model');
768
- expect(history[1].parts).toEqual([{ text: 'Part 1. Part 2. Part 3.' }]);
769
- });
770
- it('should add an empty placeholder turn if modelOutput is empty', () => {
771
- // This simulates receiving a pre-filtered, thought-only response.
772
- const emptyModelOutput = [];
773
- // @ts-expect-error Accessing private method for testing
774
- chat.recordHistory(userInput, emptyModelOutput);
775
- const history = chat.getHistory();
776
- expect(history.length).toBe(2);
777
- expect(history[0]).toEqual(userInput);
778
- expect(history[1].role).toBe('model');
779
- expect(history[1].parts).toEqual([]);
780
- });
781
- it('should preserve model outputs with undefined or empty parts arrays', () => {
782
- const malformedOutput = [
783
- { role: 'model', parts: [{ text: 'Text part' }] },
784
- { role: 'model', parts: undefined },
785
- { role: 'model', parts: [] },
786
- ];
787
- // @ts-expect-error Accessing private method for testing
788
- chat.recordHistory(userInput, malformedOutput);
789
- const history = chat.getHistory();
790
- expect(history.length).toBe(4); // userInput + 3 model turns
791
- expect(history[1].parts).toEqual([{ text: 'Text part' }]);
792
- expect(history[2].parts).toBeUndefined();
793
- expect(history[3].parts).toEqual([]);
794
- });
795
- it('should not consolidate content with different roles', () => {
796
- const mixedOutput = [
797
- { role: 'model', parts: [{ text: 'Model 1' }] },
798
- { role: 'user', parts: [{ text: 'Unexpected User' }] },
799
- { role: 'model', parts: [{ text: 'Model 2' }] },
800
- ];
801
- // @ts-expect-error Accessing private method for testing
802
- chat.recordHistory(userInput, mixedOutput);
803
- const history = chat.getHistory();
804
- expect(history.length).toBe(4); // userInput, model1, unexpected_user, model2
805
- expect(history[1]).toEqual(mixedOutput[0]);
806
- expect(history[2]).toEqual(mixedOutput[1]);
807
- expect(history[3]).toEqual(mixedOutput[2]);
808
- });
809
- });
810
438
  describe('addHistory', () => {
811
439
  it('should add a new content item to the history', () => {
812
440
  const newContent = {
@@ -859,7 +487,7 @@ describe('GeminiChat', () => {
859
487
  };
860
488
  })());
861
489
  // ACT: Send a message and collect all events from the stream.
862
- const stream = await chat.sendMessageStream({ message: 'test' }, 'prompt-id-yield-retry');
490
+ const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-yield-retry');
863
491
  const events = [];
864
492
  for await (const event of stream) {
865
493
  events.push(event);
@@ -891,7 +519,7 @@ describe('GeminiChat', () => {
891
519
  ],
892
520
  };
893
521
  })());
894
- const stream = await chat.sendMessageStream({ message: 'test' }, 'prompt-id-retry-success');
522
+ const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-retry-success');
895
523
  const chunks = [];
896
524
  for await (const chunk of stream) {
897
525
  chunks.push(chunk);
@@ -932,14 +560,12 @@ describe('GeminiChat', () => {
932
560
  ],
933
561
  };
934
562
  })());
935
- // This helper function consumes the stream and allows us to test for rejection.
936
- async function consumeStreamAndExpectError() {
937
- const stream = await chat.sendMessageStream({ message: 'test' }, 'prompt-id-retry-fail');
563
+ const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-retry-fail');
564
+ await expect(async () => {
938
565
  for await (const _ of stream) {
939
566
  // Must loop to trigger the internal logic that throws.
940
567
  }
941
- }
942
- await expect(consumeStreamAndExpectError()).rejects.toThrow(EmptyStreamError);
568
+ }).rejects.toThrow(EmptyStreamError);
943
569
  // Should be called 3 times (initial + 2 retries)
944
570
  expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(3);
945
571
  expect(mockLogInvalidChunk).toHaveBeenCalledTimes(3);
@@ -977,7 +603,7 @@ describe('GeminiChat', () => {
977
603
  };
978
604
  })());
979
605
  // 3. Send a new message
980
- const stream = await chat.sendMessageStream({ message: 'Second question' }, 'prompt-id-retry-existing');
606
+ const stream = await chat.sendMessageStream('test-model', { message: 'Second question' }, 'prompt-id-retry-existing');
981
607
  for await (const _ of stream) {
982
608
  // consume stream
983
609
  }
@@ -1008,60 +634,6 @@ describe('GeminiChat', () => {
1008
634
  }
1009
635
  expect(turn4.parts[0].text).toBe('Second answer');
1010
636
  });
1011
- describe('concurrency control', () => {
1012
- it('should queue a subsequent sendMessage call until the first one completes', async () => {
1013
- // 1. Create promises to manually control when the API calls resolve
1014
- let firstCallResolver;
1015
- const firstCallPromise = new Promise((resolve) => {
1016
- firstCallResolver = resolve;
1017
- });
1018
- let secondCallResolver;
1019
- const secondCallPromise = new Promise((resolve) => {
1020
- secondCallResolver = resolve;
1021
- });
1022
- // A standard response body for the mock
1023
- const mockResponse = {
1024
- candidates: [
1025
- {
1026
- content: { parts: [{ text: 'response' }], role: 'model' },
1027
- },
1028
- ],
1029
- };
1030
- // 2. Mock the API to return our controllable promises in order
1031
- vi.mocked(mockContentGenerator.generateContent)
1032
- .mockReturnValueOnce(firstCallPromise)
1033
- .mockReturnValueOnce(secondCallPromise);
1034
- // 3. Start the first message call. Do not await it yet.
1035
- const firstMessagePromise = chat.sendMessage({ message: 'first' }, 'prompt-1');
1036
- // Give the event loop a chance to run the async call up to the `await`
1037
- await new Promise(process.nextTick);
1038
- // 4. While the first call is "in-flight", start the second message call.
1039
- const secondMessagePromise = chat.sendMessage({ message: 'second' }, 'prompt-2');
1040
- // 5. CRUCIAL CHECK: At this point, only the first API call should have been made.
1041
- // The second call should be waiting on `sendPromise`.
1042
- expect(mockContentGenerator.generateContent).toHaveBeenCalledTimes(1);
1043
- expect(mockContentGenerator.generateContent).toHaveBeenCalledWith(expect.objectContaining({
1044
- contents: expect.arrayContaining([
1045
- expect.objectContaining({ parts: [{ text: 'first' }] }),
1046
- ]),
1047
- }), 'prompt-1');
1048
- // 6. Unblock the first API call and wait for the first message to fully complete.
1049
- firstCallResolver(mockResponse);
1050
- await firstMessagePromise;
1051
- // Give the event loop a chance to unblock and run the second call.
1052
- await new Promise(process.nextTick);
1053
- // 7. CRUCIAL CHECK: Now, the second API call should have been made.
1054
- expect(mockContentGenerator.generateContent).toHaveBeenCalledTimes(2);
1055
- expect(mockContentGenerator.generateContent).toHaveBeenCalledWith(expect.objectContaining({
1056
- contents: expect.arrayContaining([
1057
- expect.objectContaining({ parts: [{ text: 'second' }] }),
1058
- ]),
1059
- }), 'prompt-2');
1060
- // 8. Clean up by resolving the second call.
1061
- secondCallResolver(mockResponse);
1062
- await secondMessagePromise;
1063
- });
1064
- });
1065
637
  it('should retry if the model returns a completely empty stream (no chunks)', async () => {
1066
638
  // 1. Mock the API to return an empty stream first, then a valid one.
1067
639
  vi.mocked(mockContentGenerator.generateContentStream)
@@ -1083,7 +655,7 @@ describe('GeminiChat', () => {
1083
655
  };
1084
656
  })());
1085
657
  // 2. Call the method and consume the stream.
1086
- const stream = await chat.sendMessageStream({ message: 'test empty stream' }, 'prompt-id-empty-stream');
658
+ const stream = await chat.sendMessageStream('test-model', { message: 'test empty stream' }, 'prompt-id-empty-stream');
1087
659
  const chunks = [];
1088
660
  for await (const chunk of stream) {
1089
661
  chunks.push(chunk);
@@ -1144,11 +716,11 @@ describe('GeminiChat', () => {
1144
716
  .mockResolvedValueOnce(firstStreamGenerator)
1145
717
  .mockResolvedValueOnce(secondStreamGenerator);
1146
718
  // 3. Start the first stream and consume only the first chunk to pause it
1147
- const firstStream = await chat.sendMessageStream({ message: 'first' }, 'prompt-1');
719
+ const firstStream = await chat.sendMessageStream('test-model', { message: 'first' }, 'prompt-1');
1148
720
  const firstStreamIterator = firstStream[Symbol.asyncIterator]();
1149
721
  await firstStreamIterator.next();
1150
722
  // 4. While the first stream is paused, start the second call. It will block.
1151
- const secondStreamPromise = chat.sendMessageStream({ message: 'second' }, 'prompt-2');
723
+ const secondStreamPromise = chat.sendMessageStream('test-model', { message: 'second' }, 'prompt-2');
1152
724
  // 5. Assert that only one API call has been made so far.
1153
725
  expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(1);
1154
726
  // 6. Unblock and fully consume the first stream to completion.
@@ -1173,6 +745,215 @@ describe('GeminiChat', () => {
1173
745
  }
1174
746
  expect(turn4.parts[0].text).toBe('second response');
1175
747
  });
748
+ describe('stopBeforeSecondMutator', () => {
749
+ beforeEach(() => {
750
+ // Common setup for these tests: mock the tool registry.
751
+ const mockToolRegistry = {
752
+ getTool: vi.fn((toolName) => {
753
+ if (toolName === 'edit') {
754
+ return { kind: Kind.Edit };
755
+ }
756
+ return { kind: Kind.Other };
757
+ }),
758
+ };
759
+ vi.mocked(mockConfig.getToolRegistry).mockReturnValue(mockToolRegistry);
760
+ });
761
+ it('should stop streaming before a second mutator tool call', async () => {
762
+ const responses = [
763
+ {
764
+ candidates: [
765
+ { content: { role: 'model', parts: [{ text: 'First part. ' }] } },
766
+ ],
767
+ },
768
+ {
769
+ candidates: [
770
+ {
771
+ content: {
772
+ role: 'model',
773
+ parts: [{ functionCall: { name: 'edit', args: {} } }],
774
+ },
775
+ },
776
+ ],
777
+ },
778
+ {
779
+ candidates: [
780
+ {
781
+ content: {
782
+ role: 'model',
783
+ parts: [{ functionCall: { name: 'fetch', args: {} } }],
784
+ },
785
+ },
786
+ ],
787
+ },
788
+ // This chunk contains the second mutator and should be clipped.
789
+ {
790
+ candidates: [
791
+ {
792
+ content: {
793
+ role: 'model',
794
+ parts: [
795
+ { functionCall: { name: 'edit', args: {} } },
796
+ { text: 'some trailing text' },
797
+ ],
798
+ },
799
+ },
800
+ ],
801
+ },
802
+ // This chunk should never be reached.
803
+ {
804
+ candidates: [
805
+ {
806
+ content: {
807
+ role: 'model',
808
+ parts: [{ text: 'This should not appear.' }],
809
+ },
810
+ },
811
+ ],
812
+ },
813
+ ];
814
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue((async function* () {
815
+ for (const response of responses) {
816
+ yield response;
817
+ }
818
+ })());
819
+ const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-mutator-test');
820
+ for await (const _ of stream) {
821
+ // Consume the stream to trigger history recording.
822
+ }
823
+ const history = chat.getHistory();
824
+ expect(history.length).toBe(2);
825
+ const modelTurn = history[1];
826
+ expect(modelTurn.role).toBe('model');
827
+ expect(modelTurn?.parts?.length).toBe(3);
828
+ expect(modelTurn?.parts[0].text).toBe('First part. ');
829
+ expect(modelTurn.parts[1].functionCall?.name).toBe('edit');
830
+ expect(modelTurn.parts[2].functionCall?.name).toBe('fetch');
831
+ });
832
+ it('should not stop streaming if only one mutator is present', async () => {
833
+ const responses = [
834
+ {
835
+ candidates: [
836
+ { content: { role: 'model', parts: [{ text: 'Part 1. ' }] } },
837
+ ],
838
+ },
839
+ {
840
+ candidates: [
841
+ {
842
+ content: {
843
+ role: 'model',
844
+ parts: [{ functionCall: { name: 'edit', args: {} } }],
845
+ },
846
+ },
847
+ ],
848
+ },
849
+ {
850
+ candidates: [
851
+ {
852
+ content: {
853
+ role: 'model',
854
+ parts: [{ text: 'Part 2.' }],
855
+ },
856
+ finishReason: 'STOP',
857
+ },
858
+ ],
859
+ },
860
+ ];
861
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue((async function* () {
862
+ for (const response of responses) {
863
+ yield response;
864
+ }
865
+ })());
866
+ const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-one-mutator');
867
+ for await (const _ of stream) {
868
+ /* consume */
869
+ }
870
+ const history = chat.getHistory();
871
+ const modelTurn = history[1];
872
+ expect(modelTurn?.parts?.length).toBe(3);
873
+ expect(modelTurn.parts[1].functionCall?.name).toBe('edit');
874
+ expect(modelTurn.parts[2].text).toBe('Part 2.');
875
+ });
876
+ it('should clip the chunk containing the second mutator, preserving prior parts', async () => {
877
+ const responses = [
878
+ {
879
+ candidates: [
880
+ {
881
+ content: {
882
+ role: 'model',
883
+ parts: [{ functionCall: { name: 'edit', args: {} } }],
884
+ },
885
+ },
886
+ ],
887
+ },
888
+ // This chunk has a valid part before the second mutator.
889
+ // The valid part should be kept, the rest of the chunk discarded.
890
+ {
891
+ candidates: [
892
+ {
893
+ content: {
894
+ role: 'model',
895
+ parts: [
896
+ { text: 'Keep this text. ' },
897
+ { functionCall: { name: 'edit', args: {} } },
898
+ { text: 'Discard this text.' },
899
+ ],
900
+ },
901
+ finishReason: 'STOP',
902
+ },
903
+ ],
904
+ },
905
+ ];
906
+ const stream = (async function* () {
907
+ for (const response of responses) {
908
+ yield response;
909
+ }
910
+ })();
911
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(stream);
912
+ const resultStream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-clip-chunk');
913
+ for await (const _ of resultStream) {
914
+ /* consume */
915
+ }
916
+ const history = chat.getHistory();
917
+ const modelTurn = history[1];
918
+ expect(modelTurn?.parts?.length).toBe(2);
919
+ expect(modelTurn.parts[0].functionCall?.name).toBe('edit');
920
+ expect(modelTurn.parts[1].text).toBe('Keep this text. ');
921
+ });
922
+ it('should handle two mutators in the same chunk (parallel call scenario)', async () => {
923
+ const responses = [
924
+ {
925
+ candidates: [
926
+ {
927
+ content: {
928
+ role: 'model',
929
+ parts: [
930
+ { text: 'Some text. ' },
931
+ { functionCall: { name: 'edit', args: {} } },
932
+ { functionCall: { name: 'edit', args: {} } },
933
+ ],
934
+ },
935
+ finishReason: 'STOP',
936
+ },
937
+ ],
938
+ },
939
+ ];
940
+ const stream = (async function* () {
941
+ for (const response of responses) {
942
+ yield response;
943
+ }
944
+ })();
945
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(stream);
946
+ const resultStream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-parallel-mutators');
947
+ for await (const _ of resultStream) {
948
+ /* consume */
949
+ }
950
+ const history = chat.getHistory();
951
+ const modelTurn = history[1];
952
+ expect(modelTurn?.parts?.length).toBe(2);
953
+ expect(modelTurn.parts[0].text).toBe('Some text. ');
954
+ expect(modelTurn.parts[1].functionCall?.name).toBe('edit');
955
+ });
956
+ });
1176
957
  describe('Model Resolution', () => {
1177
958
  const mockResponse = {
1178
959
  candidates: [
@@ -1182,31 +963,13 @@ describe('GeminiChat', () => {
1182
963
  },
1183
964
  ],
1184
965
  };
1185
- it('should use the configured model when not in fallback mode (sendMessage)', async () => {
1186
- vi.mocked(mockConfig.getModel).mockReturnValue('gemini-2.5-pro');
1187
- vi.mocked(mockConfig.isInFallbackMode).mockReturnValue(false);
1188
- vi.mocked(mockContentGenerator.generateContent).mockResolvedValue(mockResponse);
1189
- await chat.sendMessage({ message: 'test' }, 'prompt-id-res1');
1190
- expect(mockContentGenerator.generateContent).toHaveBeenCalledWith(expect.objectContaining({
1191
- model: 'gemini-2.5-pro',
1192
- }), 'prompt-id-res1');
1193
- });
1194
- it('should use the FLASH model when in fallback mode (sendMessage)', async () => {
1195
- vi.mocked(mockConfig.getModel).mockReturnValue('gemini-2.5-pro');
1196
- vi.mocked(mockConfig.isInFallbackMode).mockReturnValue(true);
1197
- vi.mocked(mockContentGenerator.generateContent).mockResolvedValue(mockResponse);
1198
- await chat.sendMessage({ message: 'test' }, 'prompt-id-res2');
1199
- expect(mockContentGenerator.generateContent).toHaveBeenCalledWith(expect.objectContaining({
1200
- model: DEFAULT_GEMINI_FLASH_MODEL,
1201
- }), 'prompt-id-res2');
1202
- });
1203
966
  it('should use the FLASH model when in fallback mode (sendMessageStream)', async () => {
1204
967
  vi.mocked(mockConfig.getModel).mockReturnValue('gemini-pro');
1205
968
  vi.mocked(mockConfig.isInFallbackMode).mockReturnValue(true);
1206
969
  vi.mocked(mockContentGenerator.generateContentStream).mockImplementation(async () => (async function* () {
1207
970
  yield mockResponse;
1208
971
  })());
1209
- const stream = await chat.sendMessageStream({ message: 'test' }, 'prompt-id-res3');
972
+ const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-res3');
1210
973
  for await (const _ of stream) {
1211
974
  // consume stream
1212
975
  }
@@ -1244,37 +1007,53 @@ describe('GeminiChat', () => {
1244
1007
  mockRetryWithBackoff.mockImplementation(async (apiCall) => apiCall());
1245
1008
  });
1246
1009
  it('should call handleFallback with the specific failed model and retry if handler returns true', async () => {
1247
- const FAILED_MODEL = 'gemini-2.5-pro';
1248
- vi.mocked(mockConfig.getModel).mockReturnValue(FAILED_MODEL);
1249
1010
  const authType = AuthType.LOGIN_WITH_GOOGLE;
1250
1011
  vi.mocked(mockConfig.getContentGeneratorConfig).mockReturnValue({
1251
1012
  authType,
1252
- model: FAILED_MODEL,
1253
1013
  });
1254
1014
  const isInFallbackModeSpy = vi.spyOn(mockConfig, 'isInFallbackMode');
1255
1015
  isInFallbackModeSpy.mockReturnValue(false);
1256
- vi.mocked(mockContentGenerator.generateContent)
1016
+ vi.mocked(mockContentGenerator.generateContentStream)
1257
1017
  .mockRejectedValueOnce(error429) // Attempt 1 fails
1258
- .mockResolvedValueOnce({
1259
- candidates: [{ content: { parts: [{ text: 'Success on retry' }] } }],
1260
- }); // Attempt 2 succeeds
1018
+ .mockResolvedValueOnce(
1019
+ // Attempt 2 succeeds
1020
+ (async function* () {
1021
+ yield {
1022
+ candidates: [
1023
+ {
1024
+ content: { parts: [{ text: 'Success on retry' }] },
1025
+ finishReason: 'STOP',
1026
+ },
1027
+ ],
1028
+ };
1029
+ })());
1261
1030
  mockHandleFallback.mockImplementation(async () => {
1262
1031
  isInFallbackModeSpy.mockReturnValue(true);
1263
1032
  return true; // Signal retry
1264
1033
  });
1265
- const result = await chat.sendMessage({ message: 'trigger 429' }, 'prompt-id-fb1');
1266
- expect(mockRetryWithBackoff).toHaveBeenCalledTimes(1);
1267
- expect(mockContentGenerator.generateContent).toHaveBeenCalledTimes(2);
1034
+ const stream = await chat.sendMessageStream('test-model', { message: 'trigger 429' }, 'prompt-id-fb1');
1035
+ // Consume stream to trigger logic
1036
+ for await (const _ of stream) {
1037
+ // no-op
1038
+ }
1039
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
1268
1040
  expect(mockHandleFallback).toHaveBeenCalledTimes(1);
1269
- expect(mockHandleFallback).toHaveBeenCalledWith(mockConfig, FAILED_MODEL, authType, error429);
1270
- expect(result.candidates?.[0]?.content?.parts?.[0]?.text).toBe('Success on retry');
1041
+ expect(mockHandleFallback).toHaveBeenCalledWith(mockConfig, 'test-model', authType, error429);
1042
+ const history = chat.getHistory();
1043
+ const modelTurn = history[1];
1044
+ expect(modelTurn.parts[0].text).toBe('Success on retry');
1271
1045
  });
1272
1046
  it('should stop retrying if handleFallback returns false (e.g., auth intent)', async () => {
1273
1047
  vi.mocked(mockConfig.getModel).mockReturnValue('gemini-pro');
1274
- vi.mocked(mockContentGenerator.generateContent).mockRejectedValue(error429);
1048
+ vi.mocked(mockContentGenerator.generateContentStream).mockRejectedValue(error429);
1275
1049
  mockHandleFallback.mockResolvedValue(false);
1276
- await expect(chat.sendMessage({ message: 'test stop' }, 'prompt-id-fb2')).rejects.toThrow(error429);
1277
- expect(mockContentGenerator.generateContent).toHaveBeenCalledTimes(1);
1050
+ const stream = await chat.sendMessageStream('test-model', { message: 'test stop' }, 'prompt-id-fb2');
1051
+ await expect((async () => {
1052
+ for await (const _ of stream) {
1053
+ /* consume stream */
1054
+ }
1055
+ })()).rejects.toThrow(error429);
1056
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(1);
1278
1057
  expect(mockHandleFallback).toHaveBeenCalledTimes(1);
1279
1058
  });
1280
1059
  });
@@ -1312,7 +1091,7 @@ describe('GeminiChat', () => {
1312
1091
  };
1313
1092
  })());
1314
1093
  // Send a message and consume the stream
1315
- const stream = await chat.sendMessageStream({ message: 'test' }, 'prompt-id-discard-test');
1094
+ const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-discard-test');
1316
1095
  const events = [];
1317
1096
  for await (const event of stream) {
1318
1097
  events.push(event);