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

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 (332) hide show
  1. package/LICENSE +2 -2
  2. package/README.md +12 -2
  3. package/dist/index.d.ts +3 -3
  4. package/dist/index.js +3 -3
  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 +32 -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 +5 -4
  49. package/dist/src/core/client.js +80 -140
  50. package/dist/src/core/client.js.map +1 -1
  51. package/dist/src/core/client.test.js +247 -186
  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 +3 -30
  64. package/dist/src/core/geminiChat.js +32 -228
  65. package/dist/src/core/geminiChat.js.map +1 -1
  66. package/dist/src/core/geminiChat.test.js +58 -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 +177 -43
  197. package/dist/src/services/shellExecutionService.js.map +1 -1
  198. package/dist/src/services/shellExecutionService.test.js +153 -56
  199. package/dist/src/services/shellExecutionService.test.js.map +1 -1
  200. package/dist/src/telemetry/clearcut-logger/clearcut-logger.d.ts +10 -2
  201. package/dist/src/telemetry/clearcut-logger/clearcut-logger.js +85 -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 +12 -2
  206. package/dist/src/telemetry/clearcut-logger/event-metadata-key.js +31 -2
  207. package/dist/src/telemetry/clearcut-logger/event-metadata-key.js.map +1 -1
  208. package/dist/src/telemetry/constants.d.ts +3 -0
  209. package/dist/src/telemetry/constants.js +3 -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 +4 -1
  221. package/dist/src/telemetry/loggers.js +42 -7
  222. package/dist/src/telemetry/loggers.js.map +1 -1
  223. package/dist/src/telemetry/loggers.test.js +84 -36
  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 +16 -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 +47 -3
  235. package/dist/src/telemetry/types.js +67 -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/tools.d.ts +7 -5
  269. package/dist/src/tools/tools.js +2 -2
  270. package/dist/src/tools/tools.js.map +1 -1
  271. package/dist/src/tools/write-file.js +4 -5
  272. package/dist/src/tools/write-file.js.map +1 -1
  273. package/dist/src/tools/write-file.test.js +94 -10
  274. package/dist/src/tools/write-file.test.js.map +1 -1
  275. package/dist/src/utils/bfsFileSearch.js +11 -5
  276. package/dist/src/utils/bfsFileSearch.js.map +1 -1
  277. package/dist/src/utils/editCorrector.d.ts +7 -6
  278. package/dist/src/utils/editCorrector.js +61 -18
  279. package/dist/src/utils/editCorrector.js.map +1 -1
  280. package/dist/src/utils/editCorrector.test.js +30 -79
  281. package/dist/src/utils/editCorrector.test.js.map +1 -1
  282. package/dist/src/utils/editor.js +31 -44
  283. package/dist/src/utils/editor.js.map +1 -1
  284. package/dist/src/utils/editor.test.js +61 -75
  285. package/dist/src/utils/editor.test.js.map +1 -1
  286. package/dist/src/utils/errorParsing.js +2 -2
  287. package/dist/src/utils/errorParsing.js.map +1 -1
  288. package/dist/src/utils/errorParsing.test.js +7 -7
  289. package/dist/src/utils/errorParsing.test.js.map +1 -1
  290. package/dist/src/utils/errors.d.ts +6 -0
  291. package/dist/src/utils/errors.js +10 -0
  292. package/dist/src/utils/errors.js.map +1 -1
  293. package/dist/src/utils/fileUtils.test.js +17 -8
  294. package/dist/src/utils/fileUtils.test.js.map +1 -1
  295. package/dist/src/utils/geminiIgnoreParser.d.ts +18 -0
  296. package/dist/src/utils/geminiIgnoreParser.js +61 -0
  297. package/dist/src/utils/geminiIgnoreParser.js.map +1 -0
  298. package/dist/src/utils/geminiIgnoreParser.test.d.ts +6 -0
  299. package/dist/src/utils/geminiIgnoreParser.test.js +50 -0
  300. package/dist/src/utils/geminiIgnoreParser.test.js.map +1 -0
  301. package/dist/src/utils/gitIgnoreParser.d.ts +3 -9
  302. package/dist/src/utils/gitIgnoreParser.js +60 -69
  303. package/dist/src/utils/gitIgnoreParser.js.map +1 -1
  304. package/dist/src/utils/gitIgnoreParser.test.js +18 -53
  305. package/dist/src/utils/gitIgnoreParser.test.js.map +1 -1
  306. package/dist/src/utils/memoryDiscovery.test.js +12 -6
  307. package/dist/src/utils/memoryDiscovery.test.js.map +1 -1
  308. package/dist/src/utils/nextSpeakerChecker.d.ts +2 -2
  309. package/dist/src/utils/nextSpeakerChecker.js +8 -2
  310. package/dist/src/utils/nextSpeakerChecker.js.map +1 -1
  311. package/dist/src/utils/nextSpeakerChecker.test.js +40 -33
  312. package/dist/src/utils/nextSpeakerChecker.test.js.map +1 -1
  313. package/dist/src/utils/shell-utils.d.ts +5 -0
  314. package/dist/src/utils/shell-utils.js +23 -0
  315. package/dist/src/utils/shell-utils.js.map +1 -1
  316. package/dist/src/utils/terminalSerializer.d.ts +28 -0
  317. package/dist/src/utils/terminalSerializer.js +432 -0
  318. package/dist/src/utils/terminalSerializer.js.map +1 -0
  319. package/dist/src/utils/terminalSerializer.test.d.ts +6 -0
  320. package/dist/src/utils/terminalSerializer.test.js +176 -0
  321. package/dist/src/utils/terminalSerializer.test.js.map +1 -0
  322. package/dist/src/utils/textUtils.d.ts +5 -0
  323. package/dist/src/utils/textUtils.js +14 -0
  324. package/dist/src/utils/textUtils.js.map +1 -1
  325. package/dist/src/utils/textUtils.test.d.ts +6 -0
  326. package/dist/src/utils/textUtils.test.js +59 -0
  327. package/dist/src/utils/textUtils.test.js.map +1 -0
  328. package/dist/tsconfig.tsbuildinfo +1 -1
  329. package/package.json +5 -1
  330. package/dist/src/utils/ide-trust.d.ts +0 -10
  331. package/dist/src/utils/ide-trust.js +0 -14
  332. package/dist/src/utils/ide-trust.js.map +0 -1
@@ -105,206 +105,6 @@ describe('GeminiChat', () => {
105
105
  vi.restoreAllMocks();
106
106
  vi.resetAllMocks();
107
107
  });
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
108
  describe('sendMessageStream', () => {
309
109
  it('should succeed if a tool call is followed by an empty part', async () => {
310
110
  // 1. Mock a stream that contains a tool call, then an invalid (empty) part.
@@ -334,7 +134,7 @@ describe('GeminiChat', () => {
334
134
  vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(streamWithToolCall);
335
135
  // 2. Action & Assert: The stream processing should complete without throwing an error
336
136
  // 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');
137
+ const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-tool-call-empty-end');
338
138
  await expect((async () => {
339
139
  for await (const _ of stream) {
340
140
  /* consume stream */
@@ -374,7 +174,7 @@ describe('GeminiChat', () => {
374
174
  })();
375
175
  vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(streamWithNoFinish);
376
176
  // 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');
177
+ const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-no-finish-empty-end');
378
178
  await expect((async () => {
379
179
  for await (const _ of stream) {
380
180
  /* consume stream */
@@ -409,7 +209,7 @@ describe('GeminiChat', () => {
409
209
  })();
410
210
  vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(streamWithInvalidEnd);
411
211
  // 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');
212
+ const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-valid-then-invalid-end');
413
213
  await expect((async () => {
414
214
  for await (const _ of stream) {
415
215
  /* consume stream */
@@ -422,55 +222,6 @@ describe('GeminiChat', () => {
422
222
  expect(modelTurn?.parts?.length).toBe(1);
423
223
  expect(modelTurn?.parts[0].text).toBe('Initial valid content...');
424
224
  });
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
225
  it('should consolidate subsequent text chunks after receiving an empty text chunk', async () => {
475
226
  // 1. Mock the API to return a stream where one chunk is just an empty text part.
476
227
  const multiChunkStream = (async function* () {
@@ -493,7 +244,7 @@ describe('GeminiChat', () => {
493
244
  })();
494
245
  vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(multiChunkStream);
495
246
  // 2. Action: Send a message and consume the stream.
496
- const stream = await chat.sendMessageStream({ message: 'test message' }, 'prompt-id-empty-chunk-consolidation');
247
+ const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-empty-chunk-consolidation');
497
248
  for await (const _ of stream) {
498
249
  // Consume the stream
499
250
  }
@@ -541,7 +292,7 @@ describe('GeminiChat', () => {
541
292
  })();
542
293
  vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(multiChunkStream);
543
294
  // 2. Action: Send a message and consume the stream.
544
- const stream = await chat.sendMessageStream({ message: 'test message' }, 'prompt-id-multi-chunk');
295
+ const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-multi-chunk');
545
296
  for await (const _ of stream) {
546
297
  // Consume the stream to trigger history recording.
547
298
  }
@@ -577,7 +328,7 @@ describe('GeminiChat', () => {
577
328
  })();
578
329
  vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(mixedContentStream);
579
330
  // 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');
331
+ const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-mixed-chunk');
581
332
  for await (const _ of stream) {
582
333
  // This loop consumes the stream.
583
334
  }
@@ -626,7 +377,7 @@ describe('GeminiChat', () => {
626
377
  })();
627
378
  vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(emptyStreamResponse);
628
379
  // 3. Action: Send the function response back to the model and consume the stream.
629
- const stream = await chat.sendMessageStream({
380
+ const stream = await chat.sendMessageStream('test-model', {
630
381
  message: {
631
382
  functionResponse: {
632
383
  name: 'find_restaurant',
@@ -667,146 +418,22 @@ describe('GeminiChat', () => {
667
418
  };
668
419
  })();
669
420
  vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(response);
670
- const stream = await chat.sendMessageStream({ message: 'hello' }, 'prompt-id-1');
421
+ const stream = await chat.sendMessageStream('test-model', { message: 'hello' }, 'prompt-id-1');
671
422
  for await (const _ of stream) {
672
- // consume stream to trigger internal logic
423
+ // consume stream
673
424
  }
674
425
  expect(mockContentGenerator.generateContentStream).toHaveBeenCalledWith({
675
- model: 'gemini-pro',
676
- contents: [{ role: 'user', parts: [{ text: 'hello' }] }],
426
+ model: 'test-model',
427
+ contents: [
428
+ {
429
+ role: 'user',
430
+ parts: [{ text: 'hello' }],
431
+ },
432
+ ],
677
433
  config: {},
678
434
  }, 'prompt-id-1');
679
435
  });
680
436
  });
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
437
  describe('addHistory', () => {
811
438
  it('should add a new content item to the history', () => {
812
439
  const newContent = {
@@ -859,7 +486,7 @@ describe('GeminiChat', () => {
859
486
  };
860
487
  })());
861
488
  // ACT: Send a message and collect all events from the stream.
862
- const stream = await chat.sendMessageStream({ message: 'test' }, 'prompt-id-yield-retry');
489
+ const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-yield-retry');
863
490
  const events = [];
864
491
  for await (const event of stream) {
865
492
  events.push(event);
@@ -891,7 +518,7 @@ describe('GeminiChat', () => {
891
518
  ],
892
519
  };
893
520
  })());
894
- const stream = await chat.sendMessageStream({ message: 'test' }, 'prompt-id-retry-success');
521
+ const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-retry-success');
895
522
  const chunks = [];
896
523
  for await (const chunk of stream) {
897
524
  chunks.push(chunk);
@@ -932,14 +559,12 @@ describe('GeminiChat', () => {
932
559
  ],
933
560
  };
934
561
  })());
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');
562
+ const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-retry-fail');
563
+ await expect(async () => {
938
564
  for await (const _ of stream) {
939
565
  // Must loop to trigger the internal logic that throws.
940
566
  }
941
- }
942
- await expect(consumeStreamAndExpectError()).rejects.toThrow(EmptyStreamError);
567
+ }).rejects.toThrow(EmptyStreamError);
943
568
  // Should be called 3 times (initial + 2 retries)
944
569
  expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(3);
945
570
  expect(mockLogInvalidChunk).toHaveBeenCalledTimes(3);
@@ -977,7 +602,7 @@ describe('GeminiChat', () => {
977
602
  };
978
603
  })());
979
604
  // 3. Send a new message
980
- const stream = await chat.sendMessageStream({ message: 'Second question' }, 'prompt-id-retry-existing');
605
+ const stream = await chat.sendMessageStream('test-model', { message: 'Second question' }, 'prompt-id-retry-existing');
981
606
  for await (const _ of stream) {
982
607
  // consume stream
983
608
  }
@@ -1008,60 +633,6 @@ describe('GeminiChat', () => {
1008
633
  }
1009
634
  expect(turn4.parts[0].text).toBe('Second answer');
1010
635
  });
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
636
  it('should retry if the model returns a completely empty stream (no chunks)', async () => {
1066
637
  // 1. Mock the API to return an empty stream first, then a valid one.
1067
638
  vi.mocked(mockContentGenerator.generateContentStream)
@@ -1083,7 +654,7 @@ describe('GeminiChat', () => {
1083
654
  };
1084
655
  })());
1085
656
  // 2. Call the method and consume the stream.
1086
- const stream = await chat.sendMessageStream({ message: 'test empty stream' }, 'prompt-id-empty-stream');
657
+ const stream = await chat.sendMessageStream('test-model', { message: 'test empty stream' }, 'prompt-id-empty-stream');
1087
658
  const chunks = [];
1088
659
  for await (const chunk of stream) {
1089
660
  chunks.push(chunk);
@@ -1144,11 +715,11 @@ describe('GeminiChat', () => {
1144
715
  .mockResolvedValueOnce(firstStreamGenerator)
1145
716
  .mockResolvedValueOnce(secondStreamGenerator);
1146
717
  // 3. Start the first stream and consume only the first chunk to pause it
1147
- const firstStream = await chat.sendMessageStream({ message: 'first' }, 'prompt-1');
718
+ const firstStream = await chat.sendMessageStream('test-model', { message: 'first' }, 'prompt-1');
1148
719
  const firstStreamIterator = firstStream[Symbol.asyncIterator]();
1149
720
  await firstStreamIterator.next();
1150
721
  // 4. While the first stream is paused, start the second call. It will block.
1151
- const secondStreamPromise = chat.sendMessageStream({ message: 'second' }, 'prompt-2');
722
+ const secondStreamPromise = chat.sendMessageStream('test-model', { message: 'second' }, 'prompt-2');
1152
723
  // 5. Assert that only one API call has been made so far.
1153
724
  expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(1);
1154
725
  // 6. Unblock and fully consume the first stream to completion.
@@ -1182,31 +753,13 @@ describe('GeminiChat', () => {
1182
753
  },
1183
754
  ],
1184
755
  };
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
756
  it('should use the FLASH model when in fallback mode (sendMessageStream)', async () => {
1204
757
  vi.mocked(mockConfig.getModel).mockReturnValue('gemini-pro');
1205
758
  vi.mocked(mockConfig.isInFallbackMode).mockReturnValue(true);
1206
759
  vi.mocked(mockContentGenerator.generateContentStream).mockImplementation(async () => (async function* () {
1207
760
  yield mockResponse;
1208
761
  })());
1209
- const stream = await chat.sendMessageStream({ message: 'test' }, 'prompt-id-res3');
762
+ const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-res3');
1210
763
  for await (const _ of stream) {
1211
764
  // consume stream
1212
765
  }
@@ -1244,37 +797,53 @@ describe('GeminiChat', () => {
1244
797
  mockRetryWithBackoff.mockImplementation(async (apiCall) => apiCall());
1245
798
  });
1246
799
  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
800
  const authType = AuthType.LOGIN_WITH_GOOGLE;
1250
801
  vi.mocked(mockConfig.getContentGeneratorConfig).mockReturnValue({
1251
802
  authType,
1252
- model: FAILED_MODEL,
1253
803
  });
1254
804
  const isInFallbackModeSpy = vi.spyOn(mockConfig, 'isInFallbackMode');
1255
805
  isInFallbackModeSpy.mockReturnValue(false);
1256
- vi.mocked(mockContentGenerator.generateContent)
806
+ vi.mocked(mockContentGenerator.generateContentStream)
1257
807
  .mockRejectedValueOnce(error429) // Attempt 1 fails
1258
- .mockResolvedValueOnce({
1259
- candidates: [{ content: { parts: [{ text: 'Success on retry' }] } }],
1260
- }); // Attempt 2 succeeds
808
+ .mockResolvedValueOnce(
809
+ // Attempt 2 succeeds
810
+ (async function* () {
811
+ yield {
812
+ candidates: [
813
+ {
814
+ content: { parts: [{ text: 'Success on retry' }] },
815
+ finishReason: 'STOP',
816
+ },
817
+ ],
818
+ };
819
+ })());
1261
820
  mockHandleFallback.mockImplementation(async () => {
1262
821
  isInFallbackModeSpy.mockReturnValue(true);
1263
822
  return true; // Signal retry
1264
823
  });
1265
- const result = await chat.sendMessage({ message: 'trigger 429' }, 'prompt-id-fb1');
1266
- expect(mockRetryWithBackoff).toHaveBeenCalledTimes(1);
1267
- expect(mockContentGenerator.generateContent).toHaveBeenCalledTimes(2);
824
+ const stream = await chat.sendMessageStream('test-model', { message: 'trigger 429' }, 'prompt-id-fb1');
825
+ // Consume stream to trigger logic
826
+ for await (const _ of stream) {
827
+ // no-op
828
+ }
829
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
1268
830
  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');
831
+ expect(mockHandleFallback).toHaveBeenCalledWith(mockConfig, 'test-model', authType, error429);
832
+ const history = chat.getHistory();
833
+ const modelTurn = history[1];
834
+ expect(modelTurn.parts[0].text).toBe('Success on retry');
1271
835
  });
1272
836
  it('should stop retrying if handleFallback returns false (e.g., auth intent)', async () => {
1273
837
  vi.mocked(mockConfig.getModel).mockReturnValue('gemini-pro');
1274
- vi.mocked(mockContentGenerator.generateContent).mockRejectedValue(error429);
838
+ vi.mocked(mockContentGenerator.generateContentStream).mockRejectedValue(error429);
1275
839
  mockHandleFallback.mockResolvedValue(false);
1276
- await expect(chat.sendMessage({ message: 'test stop' }, 'prompt-id-fb2')).rejects.toThrow(error429);
1277
- expect(mockContentGenerator.generateContent).toHaveBeenCalledTimes(1);
840
+ const stream = await chat.sendMessageStream('test-model', { message: 'test stop' }, 'prompt-id-fb2');
841
+ await expect((async () => {
842
+ for await (const _ of stream) {
843
+ /* consume stream */
844
+ }
845
+ })()).rejects.toThrow(error429);
846
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(1);
1278
847
  expect(mockHandleFallback).toHaveBeenCalledTimes(1);
1279
848
  });
1280
849
  });
@@ -1312,7 +881,7 @@ describe('GeminiChat', () => {
1312
881
  };
1313
882
  })());
1314
883
  // Send a message and consume the stream
1315
- const stream = await chat.sendMessageStream({ message: 'test' }, 'prompt-id-discard-test');
884
+ const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-discard-test');
1316
885
  const events = [];
1317
886
  for await (const event of stream) {
1318
887
  events.push(event);