@machina.ai/cell-cli-core 1.4.0-rc2 → 1.6.1-rc1

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 (421) hide show
  1. package/dist/index.d.ts +6 -4
  2. package/dist/index.js +6 -4
  3. package/dist/index.js.map +1 -1
  4. package/dist/package.json +6 -2
  5. package/dist/src/code_assist/codeAssist.js +1 -1
  6. package/dist/src/code_assist/codeAssist.js.map +1 -1
  7. package/dist/src/code_assist/converter.d.ts +1 -0
  8. package/dist/src/code_assist/converter.js +1 -0
  9. package/dist/src/code_assist/converter.js.map +1 -1
  10. package/dist/src/code_assist/converter.test.js +10 -0
  11. package/dist/src/code_assist/converter.test.js.map +1 -1
  12. package/dist/src/code_assist/oauth-credential-storage.d.ts +25 -0
  13. package/dist/src/code_assist/oauth-credential-storage.js +109 -0
  14. package/dist/src/code_assist/oauth-credential-storage.js.map +1 -0
  15. package/dist/src/code_assist/oauth-credential-storage.test.js +136 -0
  16. package/dist/src/code_assist/oauth-credential-storage.test.js.map +1 -0
  17. package/dist/src/code_assist/oauth2.js +28 -2
  18. package/dist/src/code_assist/oauth2.js.map +1 -1
  19. package/dist/src/code_assist/oauth2.test.js +674 -536
  20. package/dist/src/code_assist/oauth2.test.js.map +1 -1
  21. package/dist/src/code_assist/server.d.ts +1 -1
  22. package/dist/src/code_assist/server.js +24 -1
  23. package/dist/src/code_assist/server.js.map +1 -1
  24. package/dist/src/code_assist/server.test.js +25 -0
  25. package/dist/src/code_assist/server.test.js.map +1 -1
  26. package/dist/src/code_assist/types.d.ts +17 -2
  27. package/dist/src/config/config.d.ts +62 -5
  28. package/dist/src/config/config.js +154 -42
  29. package/dist/src/config/config.js.map +1 -1
  30. package/dist/src/config/config.test.js +235 -137
  31. package/dist/src/config/config.test.js.map +1 -1
  32. package/dist/src/config/models.d.ts +15 -0
  33. package/dist/src/config/models.js +27 -0
  34. package/dist/src/config/models.js.map +1 -1
  35. package/dist/src/config/models.test.d.ts +6 -0
  36. package/dist/src/config/models.test.js +55 -0
  37. package/dist/src/config/models.test.js.map +1 -0
  38. package/dist/src/config/storage.d.ts +2 -0
  39. package/dist/src/config/storage.js +6 -1
  40. package/dist/src/config/storage.js.map +1 -1
  41. package/dist/src/config/storage.test.js +4 -0
  42. package/dist/src/config/storage.test.js.map +1 -1
  43. package/dist/src/confirmation-bus/index.d.ts +7 -0
  44. package/dist/src/confirmation-bus/index.js +8 -0
  45. package/dist/src/confirmation-bus/index.js.map +1 -0
  46. package/dist/src/confirmation-bus/message-bus.d.ts +17 -0
  47. package/dist/src/confirmation-bus/message-bus.js +81 -0
  48. package/dist/src/confirmation-bus/message-bus.js.map +1 -0
  49. package/dist/src/confirmation-bus/message-bus.test.d.ts +6 -0
  50. package/dist/src/confirmation-bus/message-bus.test.js +164 -0
  51. package/dist/src/confirmation-bus/message-bus.test.js.map +1 -0
  52. package/dist/src/confirmation-bus/types.d.ts +38 -0
  53. package/dist/src/confirmation-bus/types.js +15 -0
  54. package/dist/src/confirmation-bus/types.js.map +1 -0
  55. package/dist/src/core/baseLlmClient.d.ts +46 -0
  56. package/dist/src/core/baseLlmClient.js +112 -0
  57. package/dist/src/core/baseLlmClient.js.map +1 -0
  58. package/dist/src/core/baseLlmClient.test.d.ts +6 -0
  59. package/dist/src/core/baseLlmClient.test.js +253 -0
  60. package/dist/src/core/baseLlmClient.test.js.map +1 -0
  61. package/dist/src/core/client.d.ts +8 -18
  62. package/dist/src/core/client.js +108 -227
  63. package/dist/src/core/client.js.map +1 -1
  64. package/dist/src/core/client.test.js +269 -491
  65. package/dist/src/core/client.test.js.map +1 -1
  66. package/dist/src/core/contentGenerator.d.ts +0 -1
  67. package/dist/src/core/contentGenerator.js +0 -4
  68. package/dist/src/core/contentGenerator.js.map +1 -1
  69. package/dist/src/core/contentGenerator.test.js +1 -3
  70. package/dist/src/core/contentGenerator.test.js.map +1 -1
  71. package/dist/src/core/coreToolScheduler.d.ts +8 -3
  72. package/dist/src/core/coreToolScheduler.js +118 -8
  73. package/dist/src/core/coreToolScheduler.js.map +1 -1
  74. package/dist/src/core/coreToolScheduler.test.js +314 -5
  75. package/dist/src/core/coreToolScheduler.test.js.map +1 -1
  76. package/dist/src/core/geminiChat.d.ts +15 -38
  77. package/dist/src/core/geminiChat.js +108 -257
  78. package/dist/src/core/geminiChat.js.map +1 -1
  79. package/dist/src/core/geminiChat.test.js +429 -491
  80. package/dist/src/core/geminiChat.test.js.map +1 -1
  81. package/dist/src/core/loggingContentGenerator.js +7 -10
  82. package/dist/src/core/loggingContentGenerator.js.map +1 -1
  83. package/dist/src/core/nonInteractiveToolExecutor.test.js +57 -1
  84. package/dist/src/core/nonInteractiveToolExecutor.test.js.map +1 -1
  85. package/dist/src/core/prompts.d.ts +5 -0
  86. package/dist/src/core/prompts.js +64 -43
  87. package/dist/src/core/prompts.js.map +1 -1
  88. package/dist/src/core/prompts.test.js +146 -17
  89. package/dist/src/core/prompts.test.js.map +1 -1
  90. package/dist/src/core/subagent.js +2 -4
  91. package/dist/src/core/subagent.js.map +1 -1
  92. package/dist/src/core/subagent.test.js +12 -13
  93. package/dist/src/core/subagent.test.js.map +1 -1
  94. package/dist/src/core/turn.d.ts +3 -1
  95. package/dist/src/core/turn.js +2 -2
  96. package/dist/src/core/turn.js.map +1 -1
  97. package/dist/src/core/turn.test.js +18 -18
  98. package/dist/src/core/turn.test.js.map +1 -1
  99. package/dist/src/fallback/handler.d.ts +7 -0
  100. package/dist/src/fallback/handler.js +51 -0
  101. package/dist/src/fallback/handler.js.map +1 -0
  102. package/dist/src/fallback/handler.test.d.ts +6 -0
  103. package/dist/src/fallback/handler.test.js +130 -0
  104. package/dist/src/fallback/handler.test.js.map +1 -0
  105. package/dist/src/fallback/types.d.ts +14 -0
  106. package/dist/src/fallback/types.js +7 -0
  107. package/dist/src/fallback/types.js.map +1 -0
  108. package/dist/src/generated/git-commit.d.ts +2 -2
  109. package/dist/src/generated/git-commit.js +2 -2
  110. package/dist/src/generated/git-commit.js.map +1 -1
  111. package/dist/src/ide/constants.d.ts +3 -0
  112. package/dist/src/ide/constants.js +3 -0
  113. package/dist/src/ide/constants.js.map +1 -1
  114. package/dist/src/ide/ide-client.d.ts +51 -13
  115. package/dist/src/ide/ide-client.js +241 -35
  116. package/dist/src/ide/ide-client.js.map +1 -1
  117. package/dist/src/ide/ide-client.test.js +236 -0
  118. package/dist/src/ide/ide-client.test.js.map +1 -1
  119. package/dist/src/ide/ide-installer.js +8 -2
  120. package/dist/src/ide/ide-installer.js.map +1 -1
  121. package/dist/src/ide/ide-installer.test.js +13 -2
  122. package/dist/src/ide/ide-installer.test.js.map +1 -1
  123. package/dist/src/ide/ideContext.d.ts +35 -377
  124. package/dist/src/ide/ideContext.js +60 -107
  125. package/dist/src/ide/ideContext.js.map +1 -1
  126. package/dist/src/ide/ideContext.test.js +152 -24
  127. package/dist/src/ide/ideContext.test.js.map +1 -1
  128. package/dist/src/ide/process-utils.js +8 -1
  129. package/dist/src/ide/process-utils.js.map +1 -1
  130. package/dist/src/ide/types.d.ts +486 -0
  131. package/dist/src/ide/types.js +138 -0
  132. package/dist/src/ide/types.js.map +1 -0
  133. package/dist/src/index.d.ts +6 -1
  134. package/dist/src/index.js +6 -1
  135. package/dist/src/index.js.map +1 -1
  136. package/dist/src/mcp/oauth-provider.d.ts +1 -0
  137. package/dist/src/mcp/oauth-provider.js +22 -17
  138. package/dist/src/mcp/oauth-provider.js.map +1 -1
  139. package/dist/src/mcp/oauth-provider.test.js +149 -13
  140. package/dist/src/mcp/oauth-provider.test.js.map +1 -1
  141. package/dist/src/mcp/oauth-token-storage.d.ts +10 -6
  142. package/dist/src/mcp/oauth-token-storage.js +48 -16
  143. package/dist/src/mcp/oauth-token-storage.js.map +1 -1
  144. package/dist/src/mcp/oauth-token-storage.test.js +254 -163
  145. package/dist/src/mcp/oauth-token-storage.test.js.map +1 -1
  146. package/dist/src/mcp/oauth-utils.js +1 -0
  147. package/dist/src/mcp/oauth-utils.js.map +1 -1
  148. package/dist/src/mcp/token-storage/index.d.ts +11 -0
  149. package/dist/src/mcp/token-storage/index.js +12 -0
  150. package/dist/src/mcp/token-storage/index.js.map +1 -0
  151. package/dist/src/output/json-formatter.d.ts +11 -0
  152. package/dist/src/output/json-formatter.js +30 -0
  153. package/dist/src/output/json-formatter.js.map +1 -0
  154. package/dist/src/output/json-formatter.test.d.ts +6 -0
  155. package/dist/src/output/json-formatter.test.js +266 -0
  156. package/dist/src/output/json-formatter.test.js.map +1 -0
  157. package/dist/src/output/types.d.ts +20 -0
  158. package/dist/src/output/types.js +11 -0
  159. package/dist/src/output/types.js.map +1 -0
  160. package/dist/src/policy/index.d.ts +7 -0
  161. package/dist/src/policy/index.js +8 -0
  162. package/dist/src/policy/index.js.map +1 -0
  163. package/dist/src/policy/policy-engine.d.ts +30 -0
  164. package/dist/src/policy/policy-engine.js +83 -0
  165. package/dist/src/policy/policy-engine.js.map +1 -0
  166. package/dist/src/policy/policy-engine.test.d.ts +6 -0
  167. package/dist/src/policy/policy-engine.test.js +470 -0
  168. package/dist/src/policy/policy-engine.test.js.map +1 -0
  169. package/dist/src/policy/stable-stringify.d.ts +58 -0
  170. package/dist/src/policy/stable-stringify.js +122 -0
  171. package/dist/src/policy/stable-stringify.js.map +1 -0
  172. package/dist/src/policy/types.d.ts +47 -0
  173. package/dist/src/policy/types.js +12 -0
  174. package/dist/src/policy/types.js.map +1 -0
  175. package/dist/src/routing/modelRouterService.d.ts +23 -0
  176. package/dist/src/routing/modelRouterService.js +70 -0
  177. package/dist/src/routing/modelRouterService.js.map +1 -0
  178. package/dist/src/routing/modelRouterService.test.d.ts +6 -0
  179. package/dist/src/routing/modelRouterService.test.js +98 -0
  180. package/dist/src/routing/modelRouterService.test.js.map +1 -0
  181. package/dist/src/routing/routingStrategy.d.ts +62 -0
  182. package/dist/src/routing/routingStrategy.js +7 -0
  183. package/dist/src/routing/routingStrategy.js.map +1 -0
  184. package/dist/src/routing/strategies/classifierStrategy.d.ts +12 -0
  185. package/dist/src/routing/strategies/classifierStrategy.js +173 -0
  186. package/dist/src/routing/strategies/classifierStrategy.js.map +1 -0
  187. package/dist/src/routing/strategies/classifierStrategy.test.d.ts +6 -0
  188. package/dist/src/routing/strategies/classifierStrategy.test.js +192 -0
  189. package/dist/src/routing/strategies/classifierStrategy.test.js.map +1 -0
  190. package/dist/src/routing/strategies/compositeStrategy.d.ts +26 -0
  191. package/dist/src/routing/strategies/compositeStrategy.js +67 -0
  192. package/dist/src/routing/strategies/compositeStrategy.js.map +1 -0
  193. package/dist/src/routing/strategies/compositeStrategy.test.d.ts +6 -0
  194. package/dist/src/routing/strategies/compositeStrategy.test.js +123 -0
  195. package/dist/src/routing/strategies/compositeStrategy.test.js.map +1 -0
  196. package/dist/src/routing/strategies/defaultStrategy.d.ts +12 -0
  197. package/dist/src/routing/strategies/defaultStrategy.js +20 -0
  198. package/dist/src/routing/strategies/defaultStrategy.js.map +1 -0
  199. package/dist/src/routing/strategies/defaultStrategy.test.d.ts +6 -0
  200. package/dist/src/routing/strategies/defaultStrategy.test.js +26 -0
  201. package/dist/src/routing/strategies/defaultStrategy.test.js.map +1 -0
  202. package/dist/src/routing/strategies/fallbackStrategy.d.ts +12 -0
  203. package/dist/src/routing/strategies/fallbackStrategy.js +25 -0
  204. package/dist/src/routing/strategies/fallbackStrategy.js.map +1 -0
  205. package/dist/src/routing/strategies/fallbackStrategy.test.d.ts +6 -0
  206. package/dist/src/routing/strategies/fallbackStrategy.test.js +55 -0
  207. package/dist/src/routing/strategies/fallbackStrategy.test.js.map +1 -0
  208. package/dist/src/routing/strategies/overrideStrategy.d.ts +15 -0
  209. package/dist/src/routing/strategies/overrideStrategy.js +28 -0
  210. package/dist/src/routing/strategies/overrideStrategy.js.map +1 -0
  211. package/dist/src/routing/strategies/overrideStrategy.test.d.ts +6 -0
  212. package/dist/src/routing/strategies/overrideStrategy.test.js +42 -0
  213. package/dist/src/routing/strategies/overrideStrategy.test.js.map +1 -0
  214. package/dist/src/services/chatRecordingService.d.ts +2 -1
  215. package/dist/src/services/chatRecordingService.js +3 -3
  216. package/dist/src/services/chatRecordingService.js.map +1 -1
  217. package/dist/src/services/chatRecordingService.test.js +8 -3
  218. package/dist/src/services/chatRecordingService.test.js.map +1 -1
  219. package/dist/src/services/fileDiscoveryService.d.ts +10 -0
  220. package/dist/src/services/fileDiscoveryService.js +31 -17
  221. package/dist/src/services/fileDiscoveryService.js.map +1 -1
  222. package/dist/src/services/gitService.js +9 -12
  223. package/dist/src/services/gitService.js.map +1 -1
  224. package/dist/src/services/gitService.test.js +10 -20
  225. package/dist/src/services/gitService.test.js.map +1 -1
  226. package/dist/src/services/loopDetectionService.d.ts +5 -0
  227. package/dist/src/services/loopDetectionService.js +36 -20
  228. package/dist/src/services/loopDetectionService.js.map +1 -1
  229. package/dist/src/services/loopDetectionService.test.js +41 -12
  230. package/dist/src/services/loopDetectionService.test.js.map +1 -1
  231. package/dist/src/services/shellExecutionService.d.ts +34 -2
  232. package/dist/src/services/shellExecutionService.js +192 -43
  233. package/dist/src/services/shellExecutionService.js.map +1 -1
  234. package/dist/src/services/shellExecutionService.test.js +184 -55
  235. package/dist/src/services/shellExecutionService.test.js.map +1 -1
  236. package/dist/src/telemetry/clearcut-logger/clearcut-logger.d.ts +14 -2
  237. package/dist/src/telemetry/clearcut-logger/clearcut-logger.js +107 -5
  238. package/dist/src/telemetry/clearcut-logger/clearcut-logger.js.map +1 -1
  239. package/dist/src/telemetry/clearcut-logger/clearcut-logger.test.js +82 -5
  240. package/dist/src/telemetry/clearcut-logger/clearcut-logger.test.js.map +1 -1
  241. package/dist/src/telemetry/clearcut-logger/event-metadata-key.d.ts +13 -2
  242. package/dist/src/telemetry/clearcut-logger/event-metadata-key.js +33 -2
  243. package/dist/src/telemetry/clearcut-logger/event-metadata-key.js.map +1 -1
  244. package/dist/src/telemetry/constants.d.ts +7 -0
  245. package/dist/src/telemetry/constants.js +7 -0
  246. package/dist/src/telemetry/constants.js.map +1 -1
  247. package/dist/src/telemetry/gcp-exporters.d.ts +34 -0
  248. package/dist/src/telemetry/gcp-exporters.js +117 -0
  249. package/dist/src/telemetry/gcp-exporters.js.map +1 -0
  250. package/dist/src/telemetry/gcp-exporters.test.d.ts +6 -0
  251. package/dist/src/telemetry/gcp-exporters.test.js +318 -0
  252. package/dist/src/telemetry/gcp-exporters.test.js.map +1 -0
  253. package/dist/src/telemetry/high-water-mark-tracker.d.ts +43 -0
  254. package/dist/src/telemetry/high-water-mark-tracker.js +88 -0
  255. package/dist/src/telemetry/high-water-mark-tracker.js.map +1 -0
  256. package/dist/src/telemetry/high-water-mark-tracker.test.d.ts +6 -0
  257. package/dist/src/telemetry/high-water-mark-tracker.test.js +152 -0
  258. package/dist/src/telemetry/high-water-mark-tracker.test.js.map +1 -0
  259. package/dist/src/telemetry/index.d.ts +5 -2
  260. package/dist/src/telemetry/index.js +5 -2
  261. package/dist/src/telemetry/index.js.map +1 -1
  262. package/dist/src/telemetry/loggers.d.ts +8 -1
  263. package/dist/src/telemetry/loggers.js +114 -7
  264. package/dist/src/telemetry/loggers.js.map +1 -1
  265. package/dist/src/telemetry/loggers.test.js +232 -39
  266. package/dist/src/telemetry/loggers.test.js.map +1 -1
  267. package/dist/src/telemetry/metrics.d.ts +3 -1
  268. package/dist/src/telemetry/metrics.js +32 -3
  269. package/dist/src/telemetry/metrics.js.map +1 -1
  270. package/dist/src/telemetry/metrics.test.js +42 -0
  271. package/dist/src/telemetry/metrics.test.js.map +1 -1
  272. package/dist/src/telemetry/rate-limiter.d.ts +48 -0
  273. package/dist/src/telemetry/rate-limiter.js +100 -0
  274. package/dist/src/telemetry/rate-limiter.js.map +1 -0
  275. package/dist/src/telemetry/rate-limiter.test.d.ts +6 -0
  276. package/dist/src/telemetry/rate-limiter.test.js +207 -0
  277. package/dist/src/telemetry/rate-limiter.test.js.map +1 -0
  278. package/dist/src/telemetry/sdk.js +19 -1
  279. package/dist/src/telemetry/sdk.js.map +1 -1
  280. package/dist/src/telemetry/sdk.test.js +95 -0
  281. package/dist/src/telemetry/sdk.test.js.map +1 -1
  282. package/dist/src/telemetry/types.d.ts +60 -3
  283. package/dist/src/telemetry/types.js +93 -3
  284. package/dist/src/telemetry/types.js.map +1 -1
  285. package/dist/src/tools/edit.js +12 -5
  286. package/dist/src/tools/edit.js.map +1 -1
  287. package/dist/src/tools/edit.test.js +120 -9
  288. package/dist/src/tools/edit.test.js.map +1 -1
  289. package/dist/src/tools/glob.d.ts +5 -1
  290. package/dist/src/tools/glob.js +24 -17
  291. package/dist/src/tools/glob.js.map +1 -1
  292. package/dist/src/tools/glob.test.js +51 -0
  293. package/dist/src/tools/glob.test.js.map +1 -1
  294. package/dist/src/tools/ls.js +19 -32
  295. package/dist/src/tools/ls.js.map +1 -1
  296. package/dist/src/tools/ls.test.js +140 -280
  297. package/dist/src/tools/ls.test.js.map +1 -1
  298. package/dist/src/tools/mcp-client-manager.js +5 -21
  299. package/dist/src/tools/mcp-client-manager.js.map +1 -1
  300. package/dist/src/tools/mcp-client.js +5 -5
  301. package/dist/src/tools/mcp-client.js.map +1 -1
  302. package/dist/src/tools/mcp-tool.js +30 -2
  303. package/dist/src/tools/mcp-tool.js.map +1 -1
  304. package/dist/src/tools/mcp-tool.test.js +117 -0
  305. package/dist/src/tools/mcp-tool.test.js.map +1 -1
  306. package/dist/src/tools/read-file.js +7 -2
  307. package/dist/src/tools/read-file.js.map +1 -1
  308. package/dist/src/tools/read-file.test.js +29 -0
  309. package/dist/src/tools/read-file.test.js.map +1 -1
  310. package/dist/src/tools/read-many-files.d.ts +1 -1
  311. package/dist/src/tools/read-many-files.js +17 -49
  312. package/dist/src/tools/read-many-files.js.map +1 -1
  313. package/dist/src/tools/ripGrep.d.ts +8 -0
  314. package/dist/src/tools/ripGrep.js +26 -1
  315. package/dist/src/tools/ripGrep.js.map +1 -1
  316. package/dist/src/tools/ripGrep.test.js +107 -5
  317. package/dist/src/tools/ripGrep.test.js.map +1 -1
  318. package/dist/src/tools/shell.d.ts +12 -2
  319. package/dist/src/tools/shell.js +20 -27
  320. package/dist/src/tools/shell.js.map +1 -1
  321. package/dist/src/tools/shell.test.js +33 -68
  322. package/dist/src/tools/shell.test.js.map +1 -1
  323. package/dist/src/tools/smart-edit.d.ts +0 -1
  324. package/dist/src/tools/smart-edit.js +12 -19
  325. package/dist/src/tools/smart-edit.js.map +1 -1
  326. package/dist/src/tools/smart-edit.test.js +68 -9
  327. package/dist/src/tools/smart-edit.test.js.map +1 -1
  328. package/dist/src/tools/tool-registry.js +1 -0
  329. package/dist/src/tools/tool-registry.js.map +1 -1
  330. package/dist/src/tools/tools.d.ts +8 -5
  331. package/dist/src/tools/tools.js +9 -2
  332. package/dist/src/tools/tools.js.map +1 -1
  333. package/dist/src/tools/write-file.js +4 -5
  334. package/dist/src/tools/write-file.js.map +1 -1
  335. package/dist/src/tools/write-file.test.js +94 -10
  336. package/dist/src/tools/write-file.test.js.map +1 -1
  337. package/dist/src/utils/bfsFileSearch.js +11 -5
  338. package/dist/src/utils/bfsFileSearch.js.map +1 -1
  339. package/dist/src/utils/editCorrector.d.ts +7 -6
  340. package/dist/src/utils/editCorrector.js +61 -18
  341. package/dist/src/utils/editCorrector.js.map +1 -1
  342. package/dist/src/utils/editCorrector.test.js +30 -79
  343. package/dist/src/utils/editCorrector.test.js.map +1 -1
  344. package/dist/src/utils/editor.js +31 -44
  345. package/dist/src/utils/editor.js.map +1 -1
  346. package/dist/src/utils/editor.test.js +61 -75
  347. package/dist/src/utils/editor.test.js.map +1 -1
  348. package/dist/src/utils/errorParsing.js +2 -2
  349. package/dist/src/utils/errorParsing.js.map +1 -1
  350. package/dist/src/utils/errorParsing.test.js +7 -7
  351. package/dist/src/utils/errorParsing.test.js.map +1 -1
  352. package/dist/src/utils/errors.d.ts +6 -0
  353. package/dist/src/utils/errors.js +10 -0
  354. package/dist/src/utils/errors.js.map +1 -1
  355. package/dist/src/utils/fileUtils.d.ts +1 -0
  356. package/dist/src/utils/fileUtils.js +10 -0
  357. package/dist/src/utils/fileUtils.js.map +1 -1
  358. package/dist/src/utils/fileUtils.test.js +34 -9
  359. package/dist/src/utils/fileUtils.test.js.map +1 -1
  360. package/dist/src/utils/filesearch/crawler.test.js +1 -1
  361. package/dist/src/utils/filesearch/crawler.test.js.map +1 -1
  362. package/dist/src/utils/filesearch/fileSearch.test.js +1 -1
  363. package/dist/src/utils/filesearch/fileSearch.test.js.map +1 -1
  364. package/dist/src/utils/filesearch/ignore.test.js +1 -1
  365. package/dist/src/utils/filesearch/ignore.test.js.map +1 -1
  366. package/dist/src/utils/flashFallback.test.d.ts +6 -0
  367. package/dist/src/utils/{flashFallback.integration.test.js → flashFallback.test.js} +31 -27
  368. package/dist/src/utils/flashFallback.test.js.map +1 -0
  369. package/dist/src/utils/geminiIgnoreParser.d.ts +18 -0
  370. package/dist/src/utils/geminiIgnoreParser.js +61 -0
  371. package/dist/src/utils/geminiIgnoreParser.js.map +1 -0
  372. package/dist/src/utils/geminiIgnoreParser.test.d.ts +6 -0
  373. package/dist/src/utils/geminiIgnoreParser.test.js +50 -0
  374. package/dist/src/utils/geminiIgnoreParser.test.js.map +1 -0
  375. package/dist/src/utils/gitIgnoreParser.d.ts +3 -8
  376. package/dist/src/utils/gitIgnoreParser.js +60 -60
  377. package/dist/src/utils/gitIgnoreParser.js.map +1 -1
  378. package/dist/src/utils/gitIgnoreParser.test.js +18 -53
  379. package/dist/src/utils/gitIgnoreParser.test.js.map +1 -1
  380. package/dist/src/utils/installationManager.test.js +1 -1
  381. package/dist/src/utils/installationManager.test.js.map +1 -1
  382. package/dist/src/utils/llm-edit-fixer.d.ts +4 -3
  383. package/dist/src/utils/llm-edit-fixer.js +19 -10
  384. package/dist/src/utils/llm-edit-fixer.js.map +1 -1
  385. package/dist/src/utils/llm-edit-fixer.test.d.ts +6 -0
  386. package/dist/src/utils/llm-edit-fixer.test.js +105 -0
  387. package/dist/src/utils/llm-edit-fixer.test.js.map +1 -0
  388. package/dist/src/utils/memoryDiscovery.test.js +12 -6
  389. package/dist/src/utils/memoryDiscovery.test.js.map +1 -1
  390. package/dist/src/utils/nextSpeakerChecker.d.ts +2 -2
  391. package/dist/src/utils/nextSpeakerChecker.js +8 -2
  392. package/dist/src/utils/nextSpeakerChecker.js.map +1 -1
  393. package/dist/src/utils/nextSpeakerChecker.test.js +52 -74
  394. package/dist/src/utils/nextSpeakerChecker.test.js.map +1 -1
  395. package/dist/src/utils/promptIdContext.d.ts +7 -0
  396. package/dist/src/utils/promptIdContext.js +8 -0
  397. package/dist/src/utils/promptIdContext.js.map +1 -0
  398. package/dist/src/utils/shell-utils.d.ts +5 -0
  399. package/dist/src/utils/shell-utils.js +23 -0
  400. package/dist/src/utils/shell-utils.js.map +1 -1
  401. package/dist/src/utils/terminalSerializer.d.ts +28 -0
  402. package/dist/src/utils/terminalSerializer.js +432 -0
  403. package/dist/src/utils/terminalSerializer.js.map +1 -0
  404. package/dist/src/utils/terminalSerializer.test.d.ts +6 -0
  405. package/dist/src/utils/terminalSerializer.test.js +176 -0
  406. package/dist/src/utils/terminalSerializer.test.js.map +1 -0
  407. package/dist/src/utils/textUtils.d.ts +5 -0
  408. package/dist/src/utils/textUtils.js +14 -0
  409. package/dist/src/utils/textUtils.js.map +1 -1
  410. package/dist/src/utils/textUtils.test.d.ts +6 -0
  411. package/dist/src/utils/textUtils.test.js +59 -0
  412. package/dist/src/utils/textUtils.test.js.map +1 -0
  413. package/dist/src/utils/userAccountManager.test.js +1 -1
  414. package/dist/src/utils/userAccountManager.test.js.map +1 -1
  415. package/dist/tsconfig.tsbuildinfo +1 -1
  416. package/package.json +7 -2
  417. package/dist/src/utils/flashFallback.integration.test.js.map +0 -1
  418. package/dist/src/utils/ide-trust.d.ts +0 -10
  419. package/dist/src/utils/ide-trust.js +0 -14
  420. package/dist/src/utils/ide-trust.js.map +0 -1
  421. /package/dist/src/{utils/flashFallback.integration.test.d.ts → code_assist/oauth-credential-storage.test.d.ts} +0 -0
@@ -6,6 +6,10 @@
6
6
  import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
7
7
  import { GeminiChat, EmptyStreamError, StreamEventType, } from './geminiChat.js';
8
8
  import { setSimulate429 } from '../utils/testUtils.js';
9
+ import { DEFAULT_GEMINI_FLASH_MODEL } from '../config/models.js';
10
+ import { AuthType } from './contentGenerator.js';
11
+ import {} from '../utils/retry.js';
12
+ import { Kind } from '../tools/tools.js';
9
13
  // Mock fs module to prevent actual file system operations during tests
10
14
  const mockFileSystem = new Map();
11
15
  vi.mock('node:fs', () => {
@@ -29,14 +33,19 @@ vi.mock('node:fs', () => {
29
33
  ...fsModule,
30
34
  };
31
35
  });
32
- // Mocks
33
- const mockModelsModule = {
34
- generateContent: vi.fn(),
35
- generateContentStream: vi.fn(),
36
- countTokens: vi.fn(),
37
- embedContent: vi.fn(),
38
- batchEmbedContents: vi.fn(),
39
- };
36
+ const { mockHandleFallback } = vi.hoisted(() => ({
37
+ mockHandleFallback: vi.fn(),
38
+ }));
39
+ // Add mock for the retry utility
40
+ const { mockRetryWithBackoff } = vi.hoisted(() => ({
41
+ mockRetryWithBackoff: vi.fn(),
42
+ }));
43
+ vi.mock('../utils/retry.js', () => ({
44
+ retryWithBackoff: mockRetryWithBackoff,
45
+ }));
46
+ vi.mock('../fallback/handler.js', () => ({
47
+ handleFallback: mockHandleFallback,
48
+ }));
40
49
  const { mockLogInvalidChunk, mockLogContentRetry, mockLogContentRetryFailure } = vi.hoisted(() => ({
41
50
  mockLogInvalidChunk: vi.fn(),
42
51
  mockLogContentRetry: vi.fn(),
@@ -48,22 +57,34 @@ vi.mock('../telemetry/loggers.js', () => ({
48
57
  logContentRetryFailure: mockLogContentRetryFailure,
49
58
  }));
50
59
  describe('GeminiChat', () => {
60
+ let mockContentGenerator;
51
61
  let chat;
52
62
  let mockConfig;
53
63
  const config = {};
54
64
  beforeEach(() => {
55
65
  vi.clearAllMocks();
66
+ mockContentGenerator = {
67
+ generateContent: vi.fn(),
68
+ generateContentStream: vi.fn(),
69
+ countTokens: vi.fn(),
70
+ embedContent: vi.fn(),
71
+ batchEmbedContents: vi.fn(),
72
+ };
73
+ mockHandleFallback.mockClear();
74
+ // Default mock implementation for tests that don't care about retry logic
75
+ mockRetryWithBackoff.mockImplementation(async (apiCall) => apiCall());
56
76
  mockConfig = {
57
77
  getSessionId: () => 'test-session-id',
58
78
  getTelemetryLogPromptsEnabled: () => true,
59
79
  getUsageStatisticsEnabled: () => true,
60
80
  getDebugMode: () => false,
61
- getContentGeneratorConfig: () => ({
62
- authType: 'oauth-personal',
81
+ getContentGeneratorConfig: vi.fn().mockReturnValue({
82
+ authType: 'oauth-personal', // Ensure this is set for fallback tests
63
83
  model: 'test-model',
64
84
  }),
65
85
  getModel: vi.fn().mockReturnValue('gemini-pro'),
66
86
  setModel: vi.fn(),
87
+ isInFallbackMode: vi.fn().mockReturnValue(false),
67
88
  getQuotaErrorOccurred: vi.fn().mockReturnValue(false),
68
89
  setQuotaErrorOccurred: vi.fn(),
69
90
  flashFallbackHandler: undefined,
@@ -74,216 +95,17 @@ describe('GeminiChat', () => {
74
95
  getToolRegistry: vi.fn().mockReturnValue({
75
96
  getTool: vi.fn(),
76
97
  }),
98
+ getContentGenerator: vi.fn().mockReturnValue(mockContentGenerator),
77
99
  };
78
100
  // Disable 429 simulation for tests
79
101
  setSimulate429(false);
80
102
  // Reset history for each test by creating a new instance
81
- chat = new GeminiChat(mockConfig, mockModelsModule, config, []);
103
+ chat = new GeminiChat(mockConfig, config, []);
82
104
  });
83
105
  afterEach(() => {
84
106
  vi.restoreAllMocks();
85
107
  vi.resetAllMocks();
86
108
  });
87
- describe('sendMessage', () => {
88
- it('should retain the initial user message when an automatic function call occurs', async () => {
89
- // 1. Define the user's initial text message. This is the turn that gets dropped by the buggy logic.
90
- const userInitialMessage = {
91
- role: 'user',
92
- parts: [{ text: 'How is the weather in Boston?' }],
93
- };
94
- // 2. Mock the full API response, including the automaticFunctionCallingHistory.
95
- // This history represents the full turn: user asks, model calls tool, tool responds, model answers.
96
- const mockAfcResponse = {
97
- candidates: [
98
- {
99
- content: {
100
- role: 'model',
101
- parts: [
102
- { text: 'The weather in Boston is 72 degrees and sunny.' },
103
- ],
104
- },
105
- },
106
- ],
107
- automaticFunctionCallingHistory: [
108
- userInitialMessage, // The user's turn
109
- {
110
- // The model's first response: a tool call
111
- role: 'model',
112
- parts: [
113
- {
114
- functionCall: {
115
- name: 'get_weather',
116
- args: { location: 'Boston' },
117
- },
118
- },
119
- ],
120
- },
121
- {
122
- // The tool's response, which has a 'user' role
123
- role: 'user',
124
- parts: [
125
- {
126
- functionResponse: {
127
- name: 'get_weather',
128
- response: { temperature: 72, condition: 'sunny' },
129
- },
130
- },
131
- ],
132
- },
133
- ],
134
- };
135
- vi.mocked(mockModelsModule.generateContent).mockResolvedValue(mockAfcResponse);
136
- // 3. Action: Send the initial message.
137
- await chat.sendMessage({ message: 'How is the weather in Boston?' }, 'prompt-id-afc-bug');
138
- // 4. Assert: Check the final state of the history.
139
- const history = chat.getHistory();
140
- // With the bug, history.length will be 3, because the first user message is dropped.
141
- // The correct behavior is for the history to contain all 4 turns.
142
- expect(history.length).toBe(4);
143
- // Crucially, assert that the very first turn in the history matches the user's initial message.
144
- // This is the assertion that will fail.
145
- const firstTurn = history[0];
146
- expect(firstTurn.role).toBe('user');
147
- expect(firstTurn?.parts[0].text).toBe('How is the weather in Boston?');
148
- // Verify the rest of the history is also correct.
149
- const secondTurn = history[1];
150
- expect(secondTurn.role).toBe('model');
151
- expect(secondTurn?.parts[0].functionCall).toBeDefined();
152
- const thirdTurn = history[2];
153
- expect(thirdTurn.role).toBe('user');
154
- expect(thirdTurn?.parts[0].functionResponse).toBeDefined();
155
- const fourthTurn = history[3];
156
- expect(fourthTurn.role).toBe('model');
157
- expect(fourthTurn?.parts[0].text).toContain('72 degrees and sunny');
158
- });
159
- it('should throw an error when attempting to add a user turn after another user turn', async () => {
160
- // 1. Setup: Create a history that already ends with a user turn (a functionResponse).
161
- const initialHistory = [
162
- { role: 'user', parts: [{ text: 'Initial prompt' }] },
163
- {
164
- role: 'model',
165
- parts: [{ functionCall: { name: 'test_tool', args: {} } }],
166
- },
167
- {
168
- role: 'user',
169
- parts: [{ functionResponse: { name: 'test_tool', response: {} } }],
170
- },
171
- ];
172
- chat.setHistory(initialHistory);
173
- // 2. Mock a valid model response so the call doesn't fail for other reasons.
174
- const mockResponse = {
175
- candidates: [
176
- { content: { role: 'model', parts: [{ text: 'some response' }] } },
177
- ],
178
- };
179
- vi.mocked(mockModelsModule.generateContent).mockResolvedValue(mockResponse);
180
- // 3. Action & Assert: Expect that sending another user message immediately
181
- // after a user-role turn throws the specific error.
182
- 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.');
183
- });
184
- it('should preserve text parts that are in the same response as a thought', async () => {
185
- // 1. Mock the API to return a single response containing both a thought and visible text.
186
- const mixedContentResponse = {
187
- candidates: [
188
- {
189
- content: {
190
- role: 'model',
191
- parts: [
192
- { thought: 'This is a thought.' },
193
- { text: 'This is the visible text that should not be lost.' },
194
- ],
195
- },
196
- },
197
- ],
198
- };
199
- vi.mocked(mockModelsModule.generateContent).mockResolvedValue(mixedContentResponse);
200
- // 2. Action: Send a standard, non-streaming message.
201
- await chat.sendMessage({ message: 'test message' }, 'prompt-id-mixed-response');
202
- // 3. Assert: Check the final state of the history.
203
- const history = chat.getHistory();
204
- // The history should contain two turns: the user's message and the model's response.
205
- expect(history.length).toBe(2);
206
- const modelTurn = history[1];
207
- expect(modelTurn.role).toBe('model');
208
- // CRUCIAL ASSERTION:
209
- // Buggy code would discard the entire response because a "thought" was present,
210
- // resulting in an empty placeholder turn with 0 parts.
211
- // The corrected code will pass, preserving the single visible text part.
212
- expect(modelTurn?.parts?.length).toBe(1);
213
- expect(modelTurn?.parts[0].text).toBe('This is the visible text that should not be lost.');
214
- });
215
- it('should add a placeholder model turn when a tool call is followed by an empty model response', async () => {
216
- // 1. Setup: A history where the model has just made a function call.
217
- const initialHistory = [
218
- {
219
- role: 'user',
220
- parts: [{ text: 'Find a good Italian restaurant for me.' }],
221
- },
222
- {
223
- role: 'model',
224
- parts: [
225
- {
226
- functionCall: {
227
- name: 'find_restaurant',
228
- args: { cuisine: 'Italian' },
229
- },
230
- },
231
- ],
232
- },
233
- ];
234
- chat.setHistory(initialHistory);
235
- // 2. Mock the API to return an empty/thought-only response.
236
- const emptyModelResponse = {
237
- candidates: [
238
- { content: { role: 'model', parts: [{ thought: true }] } },
239
- ],
240
- };
241
- vi.mocked(mockModelsModule.generateContent).mockResolvedValue(emptyModelResponse);
242
- // 3. Action: Send the function response back to the model.
243
- await chat.sendMessage({
244
- message: {
245
- functionResponse: {
246
- name: 'find_restaurant',
247
- response: { name: 'Vesuvio' },
248
- },
249
- },
250
- }, 'prompt-id-1');
251
- // 4. Assert: The history should now have four valid, alternating turns.
252
- const history = chat.getHistory();
253
- expect(history.length).toBe(4);
254
- // The final turn must be the empty model placeholder.
255
- const lastTurn = history[3];
256
- expect(lastTurn.role).toBe('model');
257
- expect(lastTurn?.parts?.length).toBe(0);
258
- // The second-to-last turn must be the function response we sent.
259
- const secondToLastTurn = history[2];
260
- expect(secondToLastTurn.role).toBe('user');
261
- expect(secondToLastTurn?.parts[0].functionResponse).toBeDefined();
262
- });
263
- it('should call generateContent with the correct parameters', async () => {
264
- const response = {
265
- candidates: [
266
- {
267
- content: {
268
- parts: [{ text: 'response' }],
269
- role: 'model',
270
- },
271
- finishReason: 'STOP',
272
- index: 0,
273
- safetyRatings: [],
274
- },
275
- ],
276
- text: () => 'response',
277
- };
278
- vi.mocked(mockModelsModule.generateContent).mockResolvedValue(response);
279
- await chat.sendMessage({ message: 'hello' }, 'prompt-id-1');
280
- expect(mockModelsModule.generateContent).toHaveBeenCalledWith({
281
- model: 'gemini-pro',
282
- contents: [{ role: 'user', parts: [{ text: 'hello' }] }],
283
- config: {},
284
- }, 'prompt-id-1');
285
- });
286
- });
287
109
  describe('sendMessageStream', () => {
288
110
  it('should succeed if a tool call is followed by an empty part', async () => {
289
111
  // 1. Mock a stream that contains a tool call, then an invalid (empty) part.
@@ -310,10 +132,10 @@ describe('GeminiChat', () => {
310
132
  ],
311
133
  };
312
134
  })();
313
- vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(streamWithToolCall);
135
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(streamWithToolCall);
314
136
  // 2. Action & Assert: The stream processing should complete without throwing an error
315
137
  // because the presence of a tool call makes the empty final chunk acceptable.
316
- 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');
317
139
  await expect((async () => {
318
140
  for await (const _ of stream) {
319
141
  /* consume stream */
@@ -351,9 +173,9 @@ describe('GeminiChat', () => {
351
173
  ],
352
174
  };
353
175
  })();
354
- vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(streamWithNoFinish);
176
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(streamWithNoFinish);
355
177
  // 2. Action & Assert: The stream should fail because there's no finish reason.
356
- 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');
357
179
  await expect((async () => {
358
180
  for await (const _ of stream) {
359
181
  /* consume stream */
@@ -386,9 +208,9 @@ describe('GeminiChat', () => {
386
208
  ],
387
209
  };
388
210
  })();
389
- vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(streamWithInvalidEnd);
211
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(streamWithInvalidEnd);
390
212
  // 2. Action & Assert: The stream should complete without throwing an error.
391
- 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');
392
214
  await expect((async () => {
393
215
  for await (const _ of stream) {
394
216
  /* consume stream */
@@ -401,55 +223,6 @@ describe('GeminiChat', () => {
401
223
  expect(modelTurn?.parts?.length).toBe(1);
402
224
  expect(modelTurn?.parts[0].text).toBe('Initial valid content...');
403
225
  });
404
- it('should not consolidate text into a part that also contains a functionCall', async () => {
405
- // 1. Mock the API to stream a malformed part followed by a valid text part.
406
- const multiChunkStream = (async function* () {
407
- // This malformed part has both text and a functionCall.
408
- yield {
409
- candidates: [
410
- {
411
- content: {
412
- role: 'model',
413
- parts: [
414
- {
415
- text: 'Some text',
416
- functionCall: { name: 'do_stuff', args: {} },
417
- },
418
- ],
419
- },
420
- },
421
- ],
422
- };
423
- // This valid text part should NOT be merged into the malformed one.
424
- yield {
425
- candidates: [
426
- {
427
- content: {
428
- role: 'model',
429
- parts: [{ text: ' that should not be merged.' }],
430
- },
431
- },
432
- ],
433
- };
434
- })();
435
- vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(multiChunkStream);
436
- // 2. Action: Send a message and consume the stream.
437
- const stream = await chat.sendMessageStream({ message: 'test message' }, 'prompt-id-malformed-chunk');
438
- for await (const _ of stream) {
439
- // Consume the stream to trigger history recording.
440
- }
441
- // 3. Assert: Check that the final history was not incorrectly consolidated.
442
- const history = chat.getHistory();
443
- expect(history.length).toBe(2);
444
- const modelTurn = history[1];
445
- // CRUCIAL ASSERTION: There should be two separate parts.
446
- // The old, non-strict logic would incorrectly merge them, resulting in one part.
447
- expect(modelTurn?.parts?.length).toBe(2);
448
- // Verify the contents of each part.
449
- expect(modelTurn?.parts[0].text).toBe('Some text');
450
- expect(modelTurn?.parts[0].functionCall).toBeDefined();
451
- expect(modelTurn?.parts[1].text).toBe(' that should not be merged.');
452
- });
453
226
  it('should consolidate subsequent text chunks after receiving an empty text chunk', async () => {
454
227
  // 1. Mock the API to return a stream where one chunk is just an empty text part.
455
228
  const multiChunkStream = (async function* () {
@@ -470,9 +243,9 @@ describe('GeminiChat', () => {
470
243
  ],
471
244
  };
472
245
  })();
473
- vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(multiChunkStream);
246
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(multiChunkStream);
474
247
  // 2. Action: Send a message and consume the stream.
475
- 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');
476
249
  for await (const _ of stream) {
477
250
  // Consume the stream
478
251
  }
@@ -518,9 +291,9 @@ describe('GeminiChat', () => {
518
291
  ],
519
292
  };
520
293
  })();
521
- vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(multiChunkStream);
294
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(multiChunkStream);
522
295
  // 2. Action: Send a message and consume the stream.
523
- 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');
524
297
  for await (const _ of stream) {
525
298
  // Consume the stream to trigger history recording.
526
299
  }
@@ -554,9 +327,9 @@ describe('GeminiChat', () => {
554
327
  ],
555
328
  };
556
329
  })();
557
- vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(mixedContentStream);
330
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(mixedContentStream);
558
331
  // 2. Action: Send a message and fully consume the stream to trigger history recording.
559
- 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');
560
333
  for await (const _ of stream) {
561
334
  // This loop consumes the stream.
562
335
  }
@@ -603,9 +376,9 @@ describe('GeminiChat', () => {
603
376
  ],
604
377
  };
605
378
  })();
606
- vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(emptyStreamResponse);
379
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(emptyStreamResponse);
607
380
  // 3. Action: Send the function response back to the model and consume the stream.
608
- const stream = await chat.sendMessageStream({
381
+ const stream = await chat.sendMessageStream('test-model', {
609
382
  message: {
610
383
  functionResponse: {
611
384
  name: 'find_restaurant',
@@ -645,147 +418,23 @@ describe('GeminiChat', () => {
645
418
  text: () => 'response',
646
419
  };
647
420
  })();
648
- vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(response);
649
- const stream = await chat.sendMessageStream({ message: 'hello' }, 'prompt-id-1');
421
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(response);
422
+ const stream = await chat.sendMessageStream('test-model', { message: 'hello' }, 'prompt-id-1');
650
423
  for await (const _ of stream) {
651
- // consume stream to trigger internal logic
424
+ // consume stream
652
425
  }
653
- expect(mockModelsModule.generateContentStream).toHaveBeenCalledWith({
654
- model: 'gemini-pro',
655
- contents: [{ role: 'user', parts: [{ text: 'hello' }] }],
426
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledWith({
427
+ model: 'test-model',
428
+ contents: [
429
+ {
430
+ role: 'user',
431
+ parts: [{ text: 'hello' }],
432
+ },
433
+ ],
656
434
  config: {},
657
435
  }, 'prompt-id-1');
658
436
  });
659
437
  });
660
- describe('recordHistory', () => {
661
- const userInput = {
662
- role: 'user',
663
- parts: [{ text: 'User input' }],
664
- };
665
- it('should consolidate all consecutive model turns into a single turn', () => {
666
- const userInput = {
667
- role: 'user',
668
- parts: [{ text: 'User input' }],
669
- };
670
- // This simulates a multi-part model response with different part types.
671
- const modelOutput = [
672
- { role: 'model', parts: [{ text: 'Thinking...' }] },
673
- {
674
- role: 'model',
675
- parts: [{ functionCall: { name: 'do_stuff', args: {} } }],
676
- },
677
- ];
678
- // @ts-expect-error Accessing private method for testing
679
- chat.recordHistory(userInput, modelOutput);
680
- const history = chat.getHistory();
681
- // The history should contain the user's turn and ONE consolidated model turn.
682
- // The old code would fail here, resulting in a length of 3.
683
- //expect(history).toBe([]);
684
- expect(history.length).toBe(2);
685
- const modelTurn = history[1];
686
- expect(modelTurn.role).toBe('model');
687
- // The consolidated turn should contain both the text part and the functionCall part.
688
- expect(modelTurn?.parts?.length).toBe(2);
689
- expect(modelTurn?.parts[0].text).toBe('Thinking...');
690
- expect(modelTurn?.parts[1].functionCall).toBeDefined();
691
- });
692
- it('should add a placeholder model turn when a tool call is followed by an empty response', () => {
693
- // 1. Setup: A history where the model has just made a function call.
694
- const initialHistory = [
695
- { role: 'user', parts: [{ text: 'Initial prompt' }] },
696
- {
697
- role: 'model',
698
- parts: [{ functionCall: { name: 'test_tool', args: {} } }],
699
- },
700
- ];
701
- chat.setHistory(initialHistory);
702
- // 2. Action: The user provides the tool's response, and the model's
703
- // final output is empty (e.g., just a thought, which gets filtered out).
704
- const functionResponse = {
705
- role: 'user',
706
- parts: [{ functionResponse: { name: 'test_tool', response: {} } }],
707
- };
708
- const emptyModelOutput = [];
709
- // @ts-expect-error Accessing private method for testing
710
- chat.recordHistory(functionResponse, emptyModelOutput, [
711
- functionResponse,
712
- ]);
713
- // 3. Assert: The history should now have four valid, alternating turns.
714
- const history = chat.getHistory();
715
- expect(history.length).toBe(4);
716
- // The final turn must be the empty model placeholder.
717
- const lastTurn = history[3];
718
- expect(lastTurn.role).toBe('model');
719
- expect(lastTurn?.parts?.length).toBe(0);
720
- // The second-to-last turn must be the function response we provided.
721
- const secondToLastTurn = history[2];
722
- expect(secondToLastTurn.role).toBe('user');
723
- expect(secondToLastTurn?.parts[0].functionResponse).toBeDefined();
724
- });
725
- it('should add user input and a single model output to history', () => {
726
- const modelOutput = [
727
- { role: 'model', parts: [{ text: 'Model output' }] },
728
- ];
729
- // @ts-expect-error Accessing private method for testing
730
- chat.recordHistory(userInput, modelOutput);
731
- const history = chat.getHistory();
732
- expect(history.length).toBe(2);
733
- expect(history[0]).toEqual(userInput);
734
- expect(history[1]).toEqual(modelOutput[0]);
735
- });
736
- it('should consolidate adjacent text parts from multiple content objects', () => {
737
- const modelOutput = [
738
- { role: 'model', parts: [{ text: 'Part 1.' }] },
739
- { role: 'model', parts: [{ text: ' Part 2.' }] },
740
- { role: 'model', parts: [{ text: ' Part 3.' }] },
741
- ];
742
- // @ts-expect-error Accessing private method for testing
743
- chat.recordHistory(userInput, modelOutput);
744
- const history = chat.getHistory();
745
- expect(history.length).toBe(2);
746
- expect(history[1].role).toBe('model');
747
- expect(history[1].parts).toEqual([{ text: 'Part 1. Part 2. Part 3.' }]);
748
- });
749
- it('should add an empty placeholder turn if modelOutput is empty', () => {
750
- // This simulates receiving a pre-filtered, thought-only response.
751
- const emptyModelOutput = [];
752
- // @ts-expect-error Accessing private method for testing
753
- chat.recordHistory(userInput, emptyModelOutput);
754
- const history = chat.getHistory();
755
- expect(history.length).toBe(2);
756
- expect(history[0]).toEqual(userInput);
757
- expect(history[1].role).toBe('model');
758
- expect(history[1].parts).toEqual([]);
759
- });
760
- it('should preserve model outputs with undefined or empty parts arrays', () => {
761
- const malformedOutput = [
762
- { role: 'model', parts: [{ text: 'Text part' }] },
763
- { role: 'model', parts: undefined },
764
- { role: 'model', parts: [] },
765
- ];
766
- // @ts-expect-error Accessing private method for testing
767
- chat.recordHistory(userInput, malformedOutput);
768
- const history = chat.getHistory();
769
- expect(history.length).toBe(4); // userInput + 3 model turns
770
- expect(history[1].parts).toEqual([{ text: 'Text part' }]);
771
- expect(history[2].parts).toBeUndefined();
772
- expect(history[3].parts).toEqual([]);
773
- });
774
- it('should not consolidate content with different roles', () => {
775
- const mixedOutput = [
776
- { role: 'model', parts: [{ text: 'Model 1' }] },
777
- { role: 'user', parts: [{ text: 'Unexpected User' }] },
778
- { role: 'model', parts: [{ text: 'Model 2' }] },
779
- ];
780
- // @ts-expect-error Accessing private method for testing
781
- chat.recordHistory(userInput, mixedOutput);
782
- const history = chat.getHistory();
783
- expect(history.length).toBe(4); // userInput, model1, unexpected_user, model2
784
- expect(history[1]).toEqual(mixedOutput[0]);
785
- expect(history[2]).toEqual(mixedOutput[1]);
786
- expect(history[3]).toEqual(mixedOutput[2]);
787
- });
788
- });
789
438
  describe('addHistory', () => {
790
439
  it('should add a new content item to the history', () => {
791
440
  const newContent = {
@@ -817,7 +466,7 @@ describe('GeminiChat', () => {
817
466
  describe('sendMessageStream with retries', () => {
818
467
  it('should yield a RETRY event when an invalid stream is encountered', async () => {
819
468
  // ARRANGE: Mock the stream to fail once, then succeed.
820
- vi.mocked(mockModelsModule.generateContentStream)
469
+ vi.mocked(mockContentGenerator.generateContentStream)
821
470
  .mockImplementationOnce(async () =>
822
471
  // First attempt: An invalid stream with an empty text part.
823
472
  (async function* () {
@@ -838,7 +487,7 @@ describe('GeminiChat', () => {
838
487
  };
839
488
  })());
840
489
  // ACT: Send a message and collect all events from the stream.
841
- 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');
842
491
  const events = [];
843
492
  for await (const event of stream) {
844
493
  events.push(event);
@@ -850,7 +499,7 @@ describe('GeminiChat', () => {
850
499
  });
851
500
  it('should retry on invalid content, succeed, and report metrics', async () => {
852
501
  // Use mockImplementationOnce to provide a fresh, promise-wrapped generator for each attempt.
853
- vi.mocked(mockModelsModule.generateContentStream)
502
+ vi.mocked(mockContentGenerator.generateContentStream)
854
503
  .mockImplementationOnce(async () =>
855
504
  // First call returns an invalid stream
856
505
  (async function* () {
@@ -870,7 +519,7 @@ describe('GeminiChat', () => {
870
519
  ],
871
520
  };
872
521
  })());
873
- 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');
874
523
  const chunks = [];
875
524
  for await (const chunk of stream) {
876
525
  chunks.push(chunk);
@@ -879,7 +528,7 @@ describe('GeminiChat', () => {
879
528
  expect(mockLogInvalidChunk).toHaveBeenCalledTimes(1);
880
529
  expect(mockLogContentRetry).toHaveBeenCalledTimes(1);
881
530
  expect(mockLogContentRetryFailure).not.toHaveBeenCalled();
882
- expect(mockModelsModule.generateContentStream).toHaveBeenCalledTimes(2);
531
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
883
532
  // Check for a retry event
884
533
  expect(chunks.some((c) => c.type === StreamEventType.RETRY)).toBe(true);
885
534
  // Check for the successful content chunk
@@ -899,7 +548,7 @@ describe('GeminiChat', () => {
899
548
  });
900
549
  });
901
550
  it('should fail after all retries on persistent invalid content and report metrics', async () => {
902
- vi.mocked(mockModelsModule.generateContentStream).mockImplementation(async () => (async function* () {
551
+ vi.mocked(mockContentGenerator.generateContentStream).mockImplementation(async () => (async function* () {
903
552
  yield {
904
553
  candidates: [
905
554
  {
@@ -911,16 +560,14 @@ describe('GeminiChat', () => {
911
560
  ],
912
561
  };
913
562
  })());
914
- // This helper function consumes the stream and allows us to test for rejection.
915
- async function consumeStreamAndExpectError() {
916
- 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 () => {
917
565
  for await (const _ of stream) {
918
566
  // Must loop to trigger the internal logic that throws.
919
567
  }
920
- }
921
- await expect(consumeStreamAndExpectError()).rejects.toThrow(EmptyStreamError);
568
+ }).rejects.toThrow(EmptyStreamError);
922
569
  // Should be called 3 times (initial + 2 retries)
923
- expect(mockModelsModule.generateContentStream).toHaveBeenCalledTimes(3);
570
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(3);
924
571
  expect(mockLogInvalidChunk).toHaveBeenCalledTimes(3);
925
572
  expect(mockLogContentRetry).toHaveBeenCalledTimes(2);
926
573
  expect(mockLogContentRetryFailure).toHaveBeenCalledTimes(1);
@@ -937,7 +584,7 @@ describe('GeminiChat', () => {
937
584
  ];
938
585
  chat.setHistory(initialHistory);
939
586
  // 2. Mock the API to fail once with an empty stream, then succeed.
940
- vi.mocked(mockModelsModule.generateContentStream)
587
+ vi.mocked(mockContentGenerator.generateContentStream)
941
588
  .mockImplementationOnce(async () => (async function* () {
942
589
  yield {
943
590
  candidates: [{ content: { parts: [{ text: '' }] } }],
@@ -956,7 +603,7 @@ describe('GeminiChat', () => {
956
603
  };
957
604
  })());
958
605
  // 3. Send a new message
959
- 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');
960
607
  for await (const _ of stream) {
961
608
  // consume stream
962
609
  }
@@ -987,63 +634,9 @@ describe('GeminiChat', () => {
987
634
  }
988
635
  expect(turn4.parts[0].text).toBe('Second answer');
989
636
  });
990
- describe('concurrency control', () => {
991
- it('should queue a subsequent sendMessage call until the first one completes', async () => {
992
- // 1. Create promises to manually control when the API calls resolve
993
- let firstCallResolver;
994
- const firstCallPromise = new Promise((resolve) => {
995
- firstCallResolver = resolve;
996
- });
997
- let secondCallResolver;
998
- const secondCallPromise = new Promise((resolve) => {
999
- secondCallResolver = resolve;
1000
- });
1001
- // A standard response body for the mock
1002
- const mockResponse = {
1003
- candidates: [
1004
- {
1005
- content: { parts: [{ text: 'response' }], role: 'model' },
1006
- },
1007
- ],
1008
- };
1009
- // 2. Mock the API to return our controllable promises in order
1010
- vi.mocked(mockModelsModule.generateContent)
1011
- .mockReturnValueOnce(firstCallPromise)
1012
- .mockReturnValueOnce(secondCallPromise);
1013
- // 3. Start the first message call. Do not await it yet.
1014
- const firstMessagePromise = chat.sendMessage({ message: 'first' }, 'prompt-1');
1015
- // Give the event loop a chance to run the async call up to the `await`
1016
- await new Promise(process.nextTick);
1017
- // 4. While the first call is "in-flight", start the second message call.
1018
- const secondMessagePromise = chat.sendMessage({ message: 'second' }, 'prompt-2');
1019
- // 5. CRUCIAL CHECK: At this point, only the first API call should have been made.
1020
- // The second call should be waiting on `sendPromise`.
1021
- expect(mockModelsModule.generateContent).toHaveBeenCalledTimes(1);
1022
- expect(mockModelsModule.generateContent).toHaveBeenCalledWith(expect.objectContaining({
1023
- contents: expect.arrayContaining([
1024
- expect.objectContaining({ parts: [{ text: 'first' }] }),
1025
- ]),
1026
- }), 'prompt-1');
1027
- // 6. Unblock the first API call and wait for the first message to fully complete.
1028
- firstCallResolver(mockResponse);
1029
- await firstMessagePromise;
1030
- // Give the event loop a chance to unblock and run the second call.
1031
- await new Promise(process.nextTick);
1032
- // 7. CRUCIAL CHECK: Now, the second API call should have been made.
1033
- expect(mockModelsModule.generateContent).toHaveBeenCalledTimes(2);
1034
- expect(mockModelsModule.generateContent).toHaveBeenCalledWith(expect.objectContaining({
1035
- contents: expect.arrayContaining([
1036
- expect.objectContaining({ parts: [{ text: 'second' }] }),
1037
- ]),
1038
- }), 'prompt-2');
1039
- // 8. Clean up by resolving the second call.
1040
- secondCallResolver(mockResponse);
1041
- await secondMessagePromise;
1042
- });
1043
- });
1044
637
  it('should retry if the model returns a completely empty stream (no chunks)', async () => {
1045
638
  // 1. Mock the API to return an empty stream first, then a valid one.
1046
- vi.mocked(mockModelsModule.generateContentStream)
639
+ vi.mocked(mockContentGenerator.generateContentStream)
1047
640
  .mockImplementationOnce(
1048
641
  // First call resolves to an async generator that yields nothing.
1049
642
  async () => (async function* () { })())
@@ -1062,13 +655,13 @@ describe('GeminiChat', () => {
1062
655
  };
1063
656
  })());
1064
657
  // 2. Call the method and consume the stream.
1065
- 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');
1066
659
  const chunks = [];
1067
660
  for await (const chunk of stream) {
1068
661
  chunks.push(chunk);
1069
662
  }
1070
663
  // 3. Assert the results.
1071
- expect(mockModelsModule.generateContentStream).toHaveBeenCalledTimes(2);
664
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
1072
665
  expect(chunks.some((c) => c.type === StreamEventType.CHUNK &&
1073
666
  c.value.candidates?.[0]?.content?.parts?.[0]?.text ===
1074
667
  'Successful response after empty')).toBe(true);
@@ -1119,17 +712,17 @@ describe('GeminiChat', () => {
1119
712
  ],
1120
713
  };
1121
714
  })();
1122
- vi.mocked(mockModelsModule.generateContentStream)
715
+ vi.mocked(mockContentGenerator.generateContentStream)
1123
716
  .mockResolvedValueOnce(firstStreamGenerator)
1124
717
  .mockResolvedValueOnce(secondStreamGenerator);
1125
718
  // 3. Start the first stream and consume only the first chunk to pause it
1126
- const firstStream = await chat.sendMessageStream({ message: 'first' }, 'prompt-1');
719
+ const firstStream = await chat.sendMessageStream('test-model', { message: 'first' }, 'prompt-1');
1127
720
  const firstStreamIterator = firstStream[Symbol.asyncIterator]();
1128
721
  await firstStreamIterator.next();
1129
722
  // 4. While the first stream is paused, start the second call. It will block.
1130
- const secondStreamPromise = chat.sendMessageStream({ message: 'second' }, 'prompt-2');
723
+ const secondStreamPromise = chat.sendMessageStream('test-model', { message: 'second' }, 'prompt-2');
1131
724
  // 5. Assert that only one API call has been made so far.
1132
- expect(mockModelsModule.generateContentStream).toHaveBeenCalledTimes(1);
725
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(1);
1133
726
  // 6. Unblock and fully consume the first stream to completion.
1134
727
  continueFirstStream();
1135
728
  await firstStreamIterator.next(); // Consume the rest of the stream
@@ -1140,7 +733,7 @@ describe('GeminiChat', () => {
1140
733
  const secondStreamIterator = secondStream[Symbol.asyncIterator]();
1141
734
  await secondStreamIterator.next();
1142
735
  // 9. The second API call should now have been made.
1143
- expect(mockModelsModule.generateContentStream).toHaveBeenCalledTimes(2);
736
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
1144
737
  // 10. FIX: Fully consume the second stream to ensure recordHistory is called.
1145
738
  await secondStreamIterator.next(); // This finishes the iterator.
1146
739
  // 11. Final check on history.
@@ -1152,9 +745,321 @@ describe('GeminiChat', () => {
1152
745
  }
1153
746
  expect(turn4.parts[0].text).toBe('second response');
1154
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
+ });
957
+ describe('Model Resolution', () => {
958
+ const mockResponse = {
959
+ candidates: [
960
+ {
961
+ content: { parts: [{ text: 'response' }], role: 'model' },
962
+ finishReason: 'STOP',
963
+ },
964
+ ],
965
+ };
966
+ it('should use the FLASH model when in fallback mode (sendMessageStream)', async () => {
967
+ vi.mocked(mockConfig.getModel).mockReturnValue('gemini-pro');
968
+ vi.mocked(mockConfig.isInFallbackMode).mockReturnValue(true);
969
+ vi.mocked(mockContentGenerator.generateContentStream).mockImplementation(async () => (async function* () {
970
+ yield mockResponse;
971
+ })());
972
+ const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-res3');
973
+ for await (const _ of stream) {
974
+ // consume stream
975
+ }
976
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledWith(expect.objectContaining({
977
+ model: DEFAULT_GEMINI_FLASH_MODEL,
978
+ }), 'prompt-id-res3');
979
+ });
980
+ });
981
+ describe('Fallback Integration (Retries)', () => {
982
+ const error429 = Object.assign(new Error('API Error 429: Quota exceeded'), {
983
+ status: 429,
984
+ });
985
+ // Define the simulated behavior for retryWithBackoff for these tests.
986
+ // This simulation tries the apiCall, if it fails, it calls the callback,
987
+ // and then tries the apiCall again if the callback returns true.
988
+ const simulateRetryBehavior = async (apiCall, options) => {
989
+ try {
990
+ return await apiCall();
991
+ }
992
+ catch (error) {
993
+ if (options.onPersistent429) {
994
+ // We simulate the "persistent" trigger here for simplicity.
995
+ const shouldRetry = await options.onPersistent429(options.authType, error);
996
+ if (shouldRetry) {
997
+ return await apiCall();
998
+ }
999
+ }
1000
+ throw error; // Stop if callback returns false/null or doesn't exist
1001
+ }
1002
+ };
1003
+ beforeEach(() => {
1004
+ mockRetryWithBackoff.mockImplementation(simulateRetryBehavior);
1005
+ });
1006
+ afterEach(() => {
1007
+ mockRetryWithBackoff.mockImplementation(async (apiCall) => apiCall());
1008
+ });
1009
+ it('should call handleFallback with the specific failed model and retry if handler returns true', async () => {
1010
+ const authType = AuthType.LOGIN_WITH_GOOGLE;
1011
+ vi.mocked(mockConfig.getContentGeneratorConfig).mockReturnValue({
1012
+ authType,
1013
+ });
1014
+ const isInFallbackModeSpy = vi.spyOn(mockConfig, 'isInFallbackMode');
1015
+ isInFallbackModeSpy.mockReturnValue(false);
1016
+ vi.mocked(mockContentGenerator.generateContentStream)
1017
+ .mockRejectedValueOnce(error429) // Attempt 1 fails
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
+ })());
1030
+ mockHandleFallback.mockImplementation(async () => {
1031
+ isInFallbackModeSpy.mockReturnValue(true);
1032
+ return true; // Signal retry
1033
+ });
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);
1040
+ expect(mockHandleFallback).toHaveBeenCalledTimes(1);
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');
1045
+ });
1046
+ it('should stop retrying if handleFallback returns false (e.g., auth intent)', async () => {
1047
+ vi.mocked(mockConfig.getModel).mockReturnValue('gemini-pro');
1048
+ vi.mocked(mockContentGenerator.generateContentStream).mockRejectedValue(error429);
1049
+ mockHandleFallback.mockResolvedValue(false);
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);
1057
+ expect(mockHandleFallback).toHaveBeenCalledTimes(1);
1058
+ });
1059
+ });
1155
1060
  it('should discard valid partial content from a failed attempt upon retry', async () => {
1156
- // ARRANGE: Mock the stream to fail on the first attempt after yielding some valid content.
1157
- vi.mocked(mockModelsModule.generateContentStream)
1061
+ // Mock the stream to fail on the first attempt after yielding some valid content.
1062
+ vi.mocked(mockContentGenerator.generateContentStream)
1158
1063
  .mockImplementationOnce(async () =>
1159
1064
  // First attempt: yields one valid chunk, then one invalid chunk
1160
1065
  (async function* () {
@@ -1185,15 +1090,14 @@ describe('GeminiChat', () => {
1185
1090
  ],
1186
1091
  };
1187
1092
  })());
1188
- // ACT: Send a message and consume the stream
1189
- const stream = await chat.sendMessageStream({ message: 'test' }, 'prompt-id-discard-test');
1093
+ // Send a message and consume the stream
1094
+ const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-discard-test');
1190
1095
  const events = [];
1191
1096
  for await (const event of stream) {
1192
1097
  events.push(event);
1193
1098
  }
1194
- // ASSERT
1195
1099
  // Check that a retry happened
1196
- expect(mockModelsModule.generateContentStream).toHaveBeenCalledTimes(2);
1100
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
1197
1101
  expect(events.some((e) => e.type === StreamEventType.RETRY)).toBe(true);
1198
1102
  // Check the final recorded history
1199
1103
  const history = chat.getHistory();
@@ -1204,5 +1108,39 @@ describe('GeminiChat', () => {
1204
1108
  // It should NOT contain any text from the failed attempt
1205
1109
  expect(modelTurn.parts[0].text).not.toContain('This valid part should be discarded');
1206
1110
  });
1111
+ describe('stripThoughtsFromHistory', () => {
1112
+ it('should strip thought signatures', () => {
1113
+ chat.setHistory([
1114
+ {
1115
+ role: 'user',
1116
+ parts: [{ text: 'hello' }],
1117
+ },
1118
+ {
1119
+ role: 'model',
1120
+ parts: [
1121
+ { text: 'thinking...', thoughtSignature: 'thought-123' },
1122
+ {
1123
+ functionCall: { name: 'test', args: {} },
1124
+ thoughtSignature: 'thought-456',
1125
+ },
1126
+ ],
1127
+ },
1128
+ ]);
1129
+ chat.stripThoughtsFromHistory();
1130
+ expect(chat.getHistory()).toEqual([
1131
+ {
1132
+ role: 'user',
1133
+ parts: [{ text: 'hello' }],
1134
+ },
1135
+ {
1136
+ role: 'model',
1137
+ parts: [
1138
+ { text: 'thinking...' },
1139
+ { functionCall: { name: 'test', args: {} } },
1140
+ ],
1141
+ },
1142
+ ]);
1143
+ });
1144
+ });
1207
1145
  });
1208
1146
  //# sourceMappingURL=geminiChat.test.js.map