@office-ai/aioncli-core 0.2.3 → 0.18.4

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 (1051) hide show
  1. package/dist/index.d.ts +16 -3
  2. package/dist/index.js +15 -3
  3. package/dist/index.js.map +1 -1
  4. package/dist/src/agents/codebase-investigator.d.ts +46 -0
  5. package/dist/src/agents/codebase-investigator.js +132 -0
  6. package/dist/src/agents/codebase-investigator.js.map +1 -0
  7. package/dist/src/agents/codebase-investigator.test.js +35 -0
  8. package/dist/src/agents/codebase-investigator.test.js.map +1 -0
  9. package/dist/src/agents/executor.d.ts +114 -0
  10. package/dist/src/agents/executor.js +779 -0
  11. package/dist/src/agents/executor.js.map +1 -0
  12. package/dist/src/agents/executor.test.js +1362 -0
  13. package/dist/src/agents/executor.test.js.map +1 -0
  14. package/dist/src/agents/invocation.d.ts +46 -0
  15. package/dist/src/agents/invocation.js +102 -0
  16. package/dist/src/agents/invocation.js.map +1 -0
  17. package/dist/src/agents/invocation.test.js +215 -0
  18. package/dist/src/agents/invocation.test.js.map +1 -0
  19. package/dist/src/agents/registry.d.ts +40 -0
  20. package/dist/src/agents/registry.js +105 -0
  21. package/dist/src/agents/registry.js.map +1 -0
  22. package/dist/src/agents/registry.test.d.ts +6 -0
  23. package/dist/src/agents/registry.test.js +160 -0
  24. package/dist/src/agents/registry.test.js.map +1 -0
  25. package/dist/src/agents/schema-utils.d.ts +39 -0
  26. package/dist/src/agents/schema-utils.js +57 -0
  27. package/dist/src/agents/schema-utils.js.map +1 -0
  28. package/dist/src/agents/schema-utils.test.d.ts +6 -0
  29. package/dist/src/agents/schema-utils.test.js +144 -0
  30. package/dist/src/agents/schema-utils.test.js.map +1 -0
  31. package/dist/src/agents/subagent-tool-wrapper.d.ts +38 -0
  32. package/dist/src/agents/subagent-tool-wrapper.js +48 -0
  33. package/dist/src/agents/subagent-tool-wrapper.js.map +1 -0
  34. package/dist/src/agents/subagent-tool-wrapper.test.d.ts +6 -0
  35. package/dist/src/agents/subagent-tool-wrapper.test.js +110 -0
  36. package/dist/src/agents/subagent-tool-wrapper.test.js.map +1 -0
  37. package/dist/src/agents/types.d.ts +146 -0
  38. package/dist/src/agents/types.js +19 -0
  39. package/dist/src/agents/types.js.map +1 -0
  40. package/dist/src/agents/utils.d.ts +15 -0
  41. package/dist/src/agents/utils.js +29 -0
  42. package/dist/src/agents/utils.js.map +1 -0
  43. package/dist/src/agents/utils.test.d.ts +6 -0
  44. package/dist/src/agents/utils.test.js +87 -0
  45. package/dist/src/agents/utils.test.js.map +1 -0
  46. package/dist/src/code_assist/codeAssist.d.ts +6 -3
  47. package/dist/src/code_assist/codeAssist.js +13 -1
  48. package/dist/src/code_assist/codeAssist.js.map +1 -1
  49. package/dist/src/code_assist/codeAssist.test.d.ts +6 -0
  50. package/dist/src/code_assist/codeAssist.test.js +99 -0
  51. package/dist/src/code_assist/codeAssist.test.js.map +1 -0
  52. package/dist/src/code_assist/converter.d.ts +5 -1
  53. package/dist/src/code_assist/converter.js +39 -5
  54. package/dist/src/code_assist/converter.js.map +1 -1
  55. package/dist/src/code_assist/converter.test.js +112 -0
  56. package/dist/src/code_assist/converter.test.js.map +1 -1
  57. package/dist/src/code_assist/experiments/client_metadata.d.ts +12 -0
  58. package/dist/src/code_assist/experiments/client_metadata.js +50 -0
  59. package/dist/src/code_assist/experiments/client_metadata.js.map +1 -0
  60. package/dist/src/code_assist/experiments/client_metadata.test.d.ts +6 -0
  61. package/dist/src/code_assist/experiments/client_metadata.test.js +99 -0
  62. package/dist/src/code_assist/experiments/client_metadata.test.js.map +1 -0
  63. package/dist/src/code_assist/experiments/experiments.d.ts +17 -0
  64. package/dist/src/code_assist/experiments/experiments.js +36 -0
  65. package/dist/src/code_assist/experiments/experiments.js.map +1 -0
  66. package/dist/src/code_assist/experiments/experiments.test.d.ts +6 -0
  67. package/dist/src/code_assist/experiments/experiments.test.js +92 -0
  68. package/dist/src/code_assist/experiments/experiments.test.js.map +1 -0
  69. package/dist/src/code_assist/experiments/flagNames.d.ts +13 -0
  70. package/dist/src/code_assist/experiments/flagNames.js +13 -0
  71. package/dist/src/code_assist/experiments/flagNames.js.map +1 -0
  72. package/dist/src/code_assist/experiments/types.d.ts +35 -0
  73. package/dist/src/code_assist/experiments/types.js +7 -0
  74. package/dist/src/code_assist/experiments/types.js.map +1 -0
  75. package/dist/src/code_assist/oauth-credential-storage.d.ts +25 -0
  76. package/dist/src/code_assist/oauth-credential-storage.js +110 -0
  77. package/dist/src/code_assist/oauth-credential-storage.js.map +1 -0
  78. package/dist/src/code_assist/oauth-credential-storage.test.d.ts +6 -0
  79. package/dist/src/code_assist/oauth-credential-storage.test.js +198 -0
  80. package/dist/src/code_assist/oauth-credential-storage.test.js.map +1 -0
  81. package/dist/src/code_assist/oauth2.d.ts +3 -3
  82. package/dist/src/code_assist/oauth2.js +252 -125
  83. package/dist/src/code_assist/oauth2.js.map +1 -1
  84. package/dist/src/code_assist/oauth2.test.js +788 -350
  85. package/dist/src/code_assist/oauth2.test.js.map +1 -1
  86. package/dist/src/code_assist/server.d.ts +8 -6
  87. package/dist/src/code_assist/server.js +41 -10
  88. package/dist/src/code_assist/server.js.map +1 -1
  89. package/dist/src/code_assist/server.test.js +151 -28
  90. package/dist/src/code_assist/server.test.js.map +1 -1
  91. package/dist/src/code_assist/setup.d.ts +2 -2
  92. package/dist/src/code_assist/setup.js +5 -3
  93. package/dist/src/code_assist/setup.js.map +1 -1
  94. package/dist/src/code_assist/setup.test.js.map +1 -1
  95. package/dist/src/code_assist/types.d.ts +18 -3
  96. package/dist/src/code_assist/types.js.map +1 -1
  97. package/dist/src/commands/extensions.d.ts +7 -0
  98. package/dist/src/commands/extensions.js +9 -0
  99. package/dist/src/commands/extensions.js.map +1 -0
  100. package/dist/src/commands/extensions.test.d.ts +6 -0
  101. package/dist/src/commands/extensions.test.js +19 -0
  102. package/dist/src/commands/extensions.test.js.map +1 -0
  103. package/dist/src/config/config.d.ts +282 -60
  104. package/dist/src/config/config.js +677 -129
  105. package/dist/src/config/config.js.map +1 -1
  106. package/dist/src/config/config.test.js +1020 -146
  107. package/dist/src/config/config.test.js.map +1 -1
  108. package/dist/src/config/constants.d.ts +11 -0
  109. package/dist/src/config/constants.js +16 -0
  110. package/dist/src/config/constants.js.map +1 -0
  111. package/dist/src/config/defaultModelConfigs.d.ts +7 -0
  112. package/dist/src/config/defaultModelConfigs.js +185 -0
  113. package/dist/src/config/defaultModelConfigs.js.map +1 -0
  114. package/dist/src/config/models.d.ts +37 -0
  115. package/dist/src/config/models.js +72 -0
  116. package/dist/src/config/models.js.map +1 -1
  117. package/dist/src/config/models.test.d.ts +6 -0
  118. package/dist/src/config/models.test.js +116 -0
  119. package/dist/src/config/models.test.js.map +1 -0
  120. package/dist/src/config/storage.d.ts +36 -0
  121. package/dist/src/config/storage.js +115 -0
  122. package/dist/src/config/storage.js.map +1 -0
  123. package/dist/src/config/storage.test.d.ts +6 -0
  124. package/dist/src/config/storage.test.js +48 -0
  125. package/dist/src/config/storage.test.js.map +1 -0
  126. package/dist/src/confirmation-bus/index.d.ts +7 -0
  127. package/dist/src/confirmation-bus/index.js +8 -0
  128. package/dist/src/confirmation-bus/index.js.map +1 -0
  129. package/dist/src/confirmation-bus/message-bus.d.ts +18 -0
  130. package/dist/src/confirmation-bus/message-bus.js +87 -0
  131. package/dist/src/confirmation-bus/message-bus.js.map +1 -0
  132. package/dist/src/confirmation-bus/message-bus.test.d.ts +6 -0
  133. package/dist/src/confirmation-bus/message-bus.test.js +170 -0
  134. package/dist/src/confirmation-bus/message-bus.test.js.map +1 -0
  135. package/dist/src/confirmation-bus/types.d.ts +49 -0
  136. package/dist/src/confirmation-bus/types.js +16 -0
  137. package/dist/src/confirmation-bus/types.js.map +1 -0
  138. package/dist/src/core/apiKeyCredentialStorage.d.ts +17 -0
  139. package/dist/src/core/apiKeyCredentialStorage.js +64 -0
  140. package/dist/src/core/apiKeyCredentialStorage.js.map +1 -0
  141. package/dist/src/core/apiKeyCredentialStorage.test.d.ts +6 -0
  142. package/dist/src/core/apiKeyCredentialStorage.test.js +71 -0
  143. package/dist/src/core/apiKeyCredentialStorage.test.js.map +1 -0
  144. package/dist/src/core/baseLlmClient.d.ts +50 -0
  145. package/dist/src/core/baseLlmClient.js +185 -0
  146. package/dist/src/core/baseLlmClient.js.map +1 -0
  147. package/dist/src/core/baseLlmClient.test.d.ts +6 -0
  148. package/dist/src/core/baseLlmClient.test.js +311 -0
  149. package/dist/src/core/baseLlmClient.test.js.map +1 -0
  150. package/dist/src/core/client.d.ts +30 -42
  151. package/dist/src/core/client.js +178 -477
  152. package/dist/src/core/client.js.map +1 -1
  153. package/dist/src/core/client.test.js +739 -617
  154. package/dist/src/core/client.test.js.map +1 -1
  155. package/dist/src/core/contentGenerator.d.ts +7 -6
  156. package/dist/src/core/contentGenerator.js +59 -45
  157. package/dist/src/core/contentGenerator.js.map +1 -1
  158. package/dist/src/core/contentGenerator.test.js +50 -4
  159. package/dist/src/core/contentGenerator.test.js.map +1 -1
  160. package/dist/src/core/coreToolScheduler.d.ts +28 -11
  161. package/dist/src/core/coreToolScheduler.js +493 -161
  162. package/dist/src/core/coreToolScheduler.js.map +1 -1
  163. package/dist/src/core/coreToolScheduler.test.js +995 -163
  164. package/dist/src/core/coreToolScheduler.test.js.map +1 -1
  165. package/dist/src/core/fakeContentGenerator.d.ts +33 -0
  166. package/dist/src/core/fakeContentGenerator.js +58 -0
  167. package/dist/src/core/fakeContentGenerator.js.map +1 -0
  168. package/dist/src/core/fakeContentGenerator.test.d.ts +6 -0
  169. package/dist/src/core/fakeContentGenerator.test.js +127 -0
  170. package/dist/src/core/fakeContentGenerator.test.js.map +1 -0
  171. package/dist/src/core/geminiChat.d.ts +61 -55
  172. package/dist/src/core/geminiChat.js +388 -366
  173. package/dist/src/core/geminiChat.js.map +1 -1
  174. package/dist/src/core/geminiChat.test.js +1600 -355
  175. package/dist/src/core/geminiChat.test.js.map +1 -1
  176. package/dist/src/core/geminiRequest.js +1 -0
  177. package/dist/src/core/geminiRequest.js.map +1 -1
  178. package/dist/src/core/logger.d.ts +11 -4
  179. package/dist/src/core/logger.js +39 -30
  180. package/dist/src/core/logger.js.map +1 -1
  181. package/dist/src/core/logger.test.js +62 -45
  182. package/dist/src/core/logger.test.js.map +1 -1
  183. package/dist/src/core/loggingContentGenerator.d.ts +4 -3
  184. package/dist/src/core/loggingContentGenerator.js +116 -37
  185. package/dist/src/core/loggingContentGenerator.js.map +1 -1
  186. package/dist/src/core/loggingContentGenerator.test.d.ts +6 -0
  187. package/dist/src/core/loggingContentGenerator.test.js +180 -0
  188. package/dist/src/core/loggingContentGenerator.test.js.map +1 -0
  189. package/dist/src/core/nonInteractiveToolExecutor.d.ts +4 -5
  190. package/dist/src/core/nonInteractiveToolExecutor.js +17 -120
  191. package/dist/src/core/nonInteractiveToolExecutor.js.map +1 -1
  192. package/dist/src/core/nonInteractiveToolExecutor.test.js +168 -84
  193. package/dist/src/core/nonInteractiveToolExecutor.test.js.map +1 -1
  194. package/dist/src/core/openaiContentGenerator.d.ts +4 -3
  195. package/dist/src/core/openaiContentGenerator.js +49 -19
  196. package/dist/src/core/openaiContentGenerator.js.map +1 -1
  197. package/dist/src/core/openaiContentGenerator.test.js +1 -0
  198. package/dist/src/core/openaiContentGenerator.test.js.map +1 -1
  199. package/dist/src/core/prompts.d.ts +7 -1
  200. package/dist/src/core/prompts.js +198 -195
  201. package/dist/src/core/prompts.js.map +1 -1
  202. package/dist/src/core/prompts.test.js +172 -104
  203. package/dist/src/core/prompts.test.js.map +1 -1
  204. package/dist/src/core/recordingContentGenerator.d.ts +18 -0
  205. package/dist/src/core/recordingContentGenerator.js +77 -0
  206. package/dist/src/core/recordingContentGenerator.js.map +1 -0
  207. package/dist/src/core/recordingContentGenerator.test.d.ts +6 -0
  208. package/dist/src/core/recordingContentGenerator.test.js +101 -0
  209. package/dist/src/core/recordingContentGenerator.test.js.map +1 -0
  210. package/dist/src/core/subagent.d.ts +24 -18
  211. package/dist/src/core/subagent.js +125 -90
  212. package/dist/src/core/subagent.js.map +1 -1
  213. package/dist/src/core/subagent.test.js +59 -44
  214. package/dist/src/core/subagent.test.js.map +1 -1
  215. package/dist/src/core/tokenLimits.test.d.ts +6 -0
  216. package/dist/src/core/tokenLimits.test.js +26 -0
  217. package/dist/src/core/tokenLimits.test.js.map +1 -0
  218. package/dist/src/core/turn.d.ts +57 -13
  219. package/dist/src/core/turn.js +79 -35
  220. package/dist/src/core/turn.js.map +1 -1
  221. package/dist/src/core/turn.test.js +373 -120
  222. package/dist/src/core/turn.test.js.map +1 -1
  223. package/dist/src/fallback/handler.d.ts +7 -0
  224. package/dist/src/fallback/handler.js +181 -0
  225. package/dist/src/fallback/handler.js.map +1 -0
  226. package/dist/src/fallback/handler.test.d.ts +6 -0
  227. package/dist/src/fallback/handler.test.js +245 -0
  228. package/dist/src/fallback/handler.test.js.map +1 -0
  229. package/dist/src/fallback/types.d.ts +14 -0
  230. package/dist/src/fallback/types.js +7 -0
  231. package/dist/src/fallback/types.js.map +1 -0
  232. package/dist/src/generated/git-commit.d.ts +2 -2
  233. package/dist/src/generated/git-commit.js +2 -2
  234. package/dist/src/generated/git-commit.js.map +1 -1
  235. package/dist/src/hooks/hookAggregator.d.ts +68 -0
  236. package/dist/src/hooks/hookAggregator.js +262 -0
  237. package/dist/src/hooks/hookAggregator.js.map +1 -0
  238. package/dist/src/hooks/hookAggregator.test.d.ts +6 -0
  239. package/dist/src/hooks/hookAggregator.test.js +387 -0
  240. package/dist/src/hooks/hookAggregator.test.js.map +1 -0
  241. package/dist/src/hooks/hookPlanner.d.ts +46 -0
  242. package/dist/src/hooks/hookPlanner.js +108 -0
  243. package/dist/src/hooks/hookPlanner.js.map +1 -0
  244. package/dist/src/hooks/hookPlanner.test.d.ts +6 -0
  245. package/dist/src/hooks/hookPlanner.test.js +255 -0
  246. package/dist/src/hooks/hookPlanner.test.js.map +1 -0
  247. package/dist/src/hooks/hookRegistry.d.ts +87 -0
  248. package/dist/src/hooks/hookRegistry.js +198 -0
  249. package/dist/src/hooks/hookRegistry.js.map +1 -0
  250. package/dist/src/hooks/hookRegistry.test.d.ts +6 -0
  251. package/dist/src/hooks/hookRegistry.test.js +341 -0
  252. package/dist/src/hooks/hookRegistry.test.js.map +1 -0
  253. package/dist/src/hooks/hookRunner.d.ts +42 -0
  254. package/dist/src/hooks/hookRunner.js +272 -0
  255. package/dist/src/hooks/hookRunner.js.map +1 -0
  256. package/dist/src/hooks/hookRunner.test.d.ts +6 -0
  257. package/dist/src/hooks/hookRunner.test.js +468 -0
  258. package/dist/src/hooks/hookRunner.test.js.map +1 -0
  259. package/dist/src/hooks/hookTranslator.d.ts +113 -0
  260. package/dist/src/hooks/hookTranslator.js +232 -0
  261. package/dist/src/hooks/hookTranslator.js.map +1 -0
  262. package/dist/src/hooks/hookTranslator.test.d.ts +6 -0
  263. package/dist/src/hooks/hookTranslator.test.js +192 -0
  264. package/dist/src/hooks/hookTranslator.test.js.map +1 -0
  265. package/dist/src/hooks/types.d.ts +384 -0
  266. package/dist/src/hooks/types.js +284 -0
  267. package/dist/src/hooks/types.js.map +1 -0
  268. package/dist/src/hooks/types.test.d.ts +6 -0
  269. package/dist/src/hooks/types.test.js +313 -0
  270. package/dist/src/hooks/types.test.js.map +1 -0
  271. package/dist/src/ide/constants.d.ts +3 -0
  272. package/dist/src/ide/constants.js +3 -0
  273. package/dist/src/ide/constants.js.map +1 -1
  274. package/dist/src/ide/detect-ide.d.ts +52 -12
  275. package/dist/src/ide/detect-ide.js +51 -65
  276. package/dist/src/ide/detect-ide.js.map +1 -1
  277. package/dist/src/ide/detect-ide.test.js +95 -52
  278. package/dist/src/ide/detect-ide.test.js.map +1 -1
  279. package/dist/src/ide/ide-client.d.ts +72 -24
  280. package/dist/src/ide/ide-client.js +380 -84
  281. package/dist/src/ide/ide-client.js.map +1 -1
  282. package/dist/src/ide/ide-client.test.js +539 -35
  283. package/dist/src/ide/ide-client.test.js.map +1 -1
  284. package/dist/src/ide/ide-installer.d.ts +2 -2
  285. package/dist/src/ide/ide-installer.js +93 -35
  286. package/dist/src/ide/ide-installer.js.map +1 -1
  287. package/dist/src/ide/ide-installer.test.js +157 -26
  288. package/dist/src/ide/ide-installer.test.js.map +1 -1
  289. package/dist/src/ide/ideContext.d.ts +35 -365
  290. package/dist/src/ide/ideContext.js +60 -106
  291. package/dist/src/ide/ideContext.js.map +1 -1
  292. package/dist/src/ide/ideContext.test.js +152 -24
  293. package/dist/src/ide/ideContext.test.js.map +1 -1
  294. package/dist/src/ide/process-utils.d.ts +7 -5
  295. package/dist/src/ide/process-utils.js +108 -67
  296. package/dist/src/ide/process-utils.js.map +1 -1
  297. package/dist/src/ide/process-utils.test.d.ts +6 -0
  298. package/dist/src/ide/process-utils.test.js +151 -0
  299. package/dist/src/ide/process-utils.test.js.map +1 -0
  300. package/dist/src/ide/types.d.ts +486 -0
  301. package/dist/src/ide/types.js +138 -0
  302. package/dist/src/ide/types.js.map +1 -0
  303. package/dist/src/index.d.ts +41 -2
  304. package/dist/src/index.js +44 -2
  305. package/dist/src/index.js.map +1 -1
  306. package/dist/src/mcp/google-auth-provider.d.ts +5 -3
  307. package/dist/src/mcp/google-auth-provider.js +21 -3
  308. package/dist/src/mcp/google-auth-provider.js.map +1 -1
  309. package/dist/src/mcp/google-auth-provider.test.js +42 -9
  310. package/dist/src/mcp/google-auth-provider.test.js.map +1 -1
  311. package/dist/src/mcp/oauth-provider.d.ts +25 -18
  312. package/dist/src/mcp/oauth-provider.js +194 -97
  313. package/dist/src/mcp/oauth-provider.js.map +1 -1
  314. package/dist/src/mcp/oauth-provider.test.js +581 -39
  315. package/dist/src/mcp/oauth-provider.test.js.map +1 -1
  316. package/dist/src/mcp/oauth-token-storage.d.ts +14 -32
  317. package/dist/src/mcp/oauth-token-storage.js +58 -28
  318. package/dist/src/mcp/oauth-token-storage.js.map +1 -1
  319. package/dist/src/mcp/oauth-token-storage.test.js +262 -162
  320. package/dist/src/mcp/oauth-token-storage.test.js.map +1 -1
  321. package/dist/src/mcp/oauth-utils.d.ts +16 -1
  322. package/dist/src/mcp/oauth-utils.js +68 -33
  323. package/dist/src/mcp/oauth-utils.js.map +1 -1
  324. package/dist/src/mcp/oauth-utils.test.js +86 -3
  325. package/dist/src/mcp/oauth-utils.test.js.map +1 -1
  326. package/dist/src/mcp/sa-impersonation-provider.d.ts +27 -0
  327. package/dist/src/mcp/sa-impersonation-provider.js +113 -0
  328. package/dist/src/mcp/sa-impersonation-provider.js.map +1 -0
  329. package/dist/src/mcp/sa-impersonation-provider.test.d.ts +6 -0
  330. package/dist/src/mcp/sa-impersonation-provider.test.js +117 -0
  331. package/dist/src/mcp/sa-impersonation-provider.test.js.map +1 -0
  332. package/dist/src/mcp/token-storage/base-token-storage.d.ts +19 -0
  333. package/dist/src/mcp/token-storage/base-token-storage.js +36 -0
  334. package/dist/src/mcp/token-storage/base-token-storage.js.map +1 -0
  335. package/dist/src/mcp/token-storage/base-token-storage.test.d.ts +6 -0
  336. package/dist/src/mcp/token-storage/base-token-storage.test.js +151 -0
  337. package/dist/src/mcp/token-storage/base-token-storage.test.js.map +1 -0
  338. package/dist/src/mcp/token-storage/file-token-storage.d.ts +24 -0
  339. package/dist/src/mcp/token-storage/file-token-storage.js +145 -0
  340. package/dist/src/mcp/token-storage/file-token-storage.js.map +1 -0
  341. package/dist/src/mcp/token-storage/file-token-storage.test.d.ts +6 -0
  342. package/dist/src/mcp/token-storage/file-token-storage.test.js +238 -0
  343. package/dist/src/mcp/token-storage/file-token-storage.test.js.map +1 -0
  344. package/dist/src/mcp/token-storage/hybrid-token-storage.d.ts +23 -0
  345. package/dist/src/mcp/token-storage/hybrid-token-storage.js +78 -0
  346. package/dist/src/mcp/token-storage/hybrid-token-storage.js.map +1 -0
  347. package/dist/src/mcp/token-storage/hybrid-token-storage.test.d.ts +6 -0
  348. package/dist/src/mcp/token-storage/hybrid-token-storage.test.js +193 -0
  349. package/dist/src/mcp/token-storage/hybrid-token-storage.test.js.map +1 -0
  350. package/dist/src/mcp/token-storage/index.d.ts +11 -0
  351. package/dist/src/mcp/token-storage/index.js +12 -0
  352. package/dist/src/mcp/token-storage/index.js.map +1 -0
  353. package/dist/src/mcp/token-storage/keychain-token-storage.d.ts +35 -0
  354. package/dist/src/mcp/token-storage/keychain-token-storage.js +246 -0
  355. package/dist/src/mcp/token-storage/keychain-token-storage.js.map +1 -0
  356. package/dist/src/mcp/token-storage/keychain-token-storage.test.d.ts +6 -0
  357. package/dist/src/mcp/token-storage/keychain-token-storage.test.js +305 -0
  358. package/dist/src/mcp/token-storage/keychain-token-storage.test.js.map +1 -0
  359. package/dist/src/mcp/token-storage/types.d.ts +44 -0
  360. package/dist/src/mcp/token-storage/types.js +11 -0
  361. package/dist/src/mcp/token-storage/types.js.map +1 -0
  362. package/dist/src/output/json-formatter.d.ts +11 -0
  363. package/dist/src/output/json-formatter.js +30 -0
  364. package/dist/src/output/json-formatter.js.map +1 -0
  365. package/dist/src/output/json-formatter.test.d.ts +6 -0
  366. package/dist/src/output/json-formatter.test.js +266 -0
  367. package/dist/src/output/json-formatter.test.js.map +1 -0
  368. package/dist/src/output/stream-json-formatter.d.ts +32 -0
  369. package/dist/src/output/stream-json-formatter.js +52 -0
  370. package/dist/src/output/stream-json-formatter.js.map +1 -0
  371. package/dist/src/output/stream-json-formatter.test.d.ts +6 -0
  372. package/dist/src/output/stream-json-formatter.test.js +479 -0
  373. package/dist/src/output/stream-json-formatter.test.js.map +1 -0
  374. package/dist/src/output/types.d.ts +82 -0
  375. package/dist/src/output/types.js +22 -0
  376. package/dist/src/output/types.js.map +1 -0
  377. package/dist/src/policy/config.d.ts +31 -0
  378. package/dist/src/policy/config.js +199 -0
  379. package/dist/src/policy/config.js.map +1 -0
  380. package/dist/src/policy/config.test.d.ts +6 -0
  381. package/dist/src/policy/config.test.js +538 -0
  382. package/dist/src/policy/config.test.js.map +1 -0
  383. package/dist/src/policy/index.d.ts +9 -0
  384. package/dist/src/policy/index.js +10 -0
  385. package/dist/src/policy/index.js.map +1 -0
  386. package/dist/src/policy/policies/discovered.toml +8 -0
  387. package/dist/src/policy/policies/read-only.toml +56 -0
  388. package/dist/src/policy/policies/write.toml +73 -0
  389. package/dist/src/policy/policies/yolo.toml +31 -0
  390. package/dist/src/policy/policy-engine.d.ts +39 -0
  391. package/dist/src/policy/policy-engine.js +158 -0
  392. package/dist/src/policy/policy-engine.js.map +1 -0
  393. package/dist/src/policy/policy-engine.test.d.ts +6 -0
  394. package/dist/src/policy/policy-engine.test.js +899 -0
  395. package/dist/src/policy/policy-engine.test.js.map +1 -0
  396. package/dist/src/policy/stable-stringify.d.ts +58 -0
  397. package/dist/src/policy/stable-stringify.js +122 -0
  398. package/dist/src/policy/stable-stringify.js.map +1 -0
  399. package/dist/src/policy/toml-loader.d.ts +47 -0
  400. package/dist/src/policy/toml-loader.js +411 -0
  401. package/dist/src/policy/toml-loader.js.map +1 -0
  402. package/dist/src/policy/toml-loader.test.d.ts +6 -0
  403. package/dist/src/policy/toml-loader.test.js +376 -0
  404. package/dist/src/policy/toml-loader.test.js.map +1 -0
  405. package/dist/src/policy/types.d.ts +130 -0
  406. package/dist/src/policy/types.js +22 -0
  407. package/dist/src/policy/types.js.map +1 -0
  408. package/dist/src/prompts/mcp-prompts.d.ts +2 -2
  409. package/dist/src/prompts/mcp-prompts.test.d.ts +6 -0
  410. package/dist/src/prompts/mcp-prompts.test.js +39 -0
  411. package/dist/src/prompts/mcp-prompts.test.js.map +1 -0
  412. package/dist/src/prompts/prompt-registry.d.ts +1 -1
  413. package/dist/src/prompts/prompt-registry.js +2 -1
  414. package/dist/src/prompts/prompt-registry.js.map +1 -1
  415. package/dist/src/prompts/prompt-registry.test.d.ts +6 -0
  416. package/dist/src/prompts/prompt-registry.test.js +96 -0
  417. package/dist/src/prompts/prompt-registry.test.js.map +1 -0
  418. package/dist/src/routing/modelRouterService.d.ts +23 -0
  419. package/dist/src/routing/modelRouterService.js +85 -0
  420. package/dist/src/routing/modelRouterService.js.map +1 -0
  421. package/dist/src/routing/modelRouterService.test.d.ts +6 -0
  422. package/dist/src/routing/modelRouterService.test.js +160 -0
  423. package/dist/src/routing/modelRouterService.test.js.map +1 -0
  424. package/dist/src/routing/routingStrategy.d.ts +62 -0
  425. package/dist/src/routing/routingStrategy.js +7 -0
  426. package/dist/src/routing/routingStrategy.js.map +1 -0
  427. package/dist/src/routing/strategies/classifierStrategy.d.ts +12 -0
  428. package/dist/src/routing/strategies/classifierStrategy.js +166 -0
  429. package/dist/src/routing/strategies/classifierStrategy.js.map +1 -0
  430. package/dist/src/routing/strategies/classifierStrategy.test.d.ts +6 -0
  431. package/dist/src/routing/strategies/classifierStrategy.test.js +196 -0
  432. package/dist/src/routing/strategies/classifierStrategy.test.js.map +1 -0
  433. package/dist/src/routing/strategies/compositeStrategy.d.ts +26 -0
  434. package/dist/src/routing/strategies/compositeStrategy.js +68 -0
  435. package/dist/src/routing/strategies/compositeStrategy.js.map +1 -0
  436. package/dist/src/routing/strategies/compositeStrategy.test.d.ts +6 -0
  437. package/dist/src/routing/strategies/compositeStrategy.test.js +123 -0
  438. package/dist/src/routing/strategies/compositeStrategy.test.js.map +1 -0
  439. package/dist/src/routing/strategies/defaultStrategy.d.ts +12 -0
  440. package/dist/src/routing/strategies/defaultStrategy.js +20 -0
  441. package/dist/src/routing/strategies/defaultStrategy.js.map +1 -0
  442. package/dist/src/routing/strategies/defaultStrategy.test.d.ts +6 -0
  443. package/dist/src/routing/strategies/defaultStrategy.test.js +26 -0
  444. package/dist/src/routing/strategies/defaultStrategy.test.js.map +1 -0
  445. package/dist/src/routing/strategies/fallbackStrategy.d.ts +12 -0
  446. package/dist/src/routing/strategies/fallbackStrategy.js +25 -0
  447. package/dist/src/routing/strategies/fallbackStrategy.js.map +1 -0
  448. package/dist/src/routing/strategies/fallbackStrategy.test.d.ts +6 -0
  449. package/dist/src/routing/strategies/fallbackStrategy.test.js +59 -0
  450. package/dist/src/routing/strategies/fallbackStrategy.test.js.map +1 -0
  451. package/dist/src/routing/strategies/overrideStrategy.d.ts +15 -0
  452. package/dist/src/routing/strategies/overrideStrategy.js +28 -0
  453. package/dist/src/routing/strategies/overrideStrategy.js.map +1 -0
  454. package/dist/src/routing/strategies/overrideStrategy.test.d.ts +6 -0
  455. package/dist/src/routing/strategies/overrideStrategy.test.js +45 -0
  456. package/dist/src/routing/strategies/overrideStrategy.test.js.map +1 -0
  457. package/dist/src/safety/built-in.d.ts +21 -0
  458. package/dist/src/safety/built-in.js +106 -0
  459. package/dist/src/safety/built-in.js.map +1 -0
  460. package/dist/src/safety/built-in.test.d.ts +6 -0
  461. package/dist/src/safety/built-in.test.js +199 -0
  462. package/dist/src/safety/built-in.test.js.map +1 -0
  463. package/dist/src/safety/checker-runner.d.ts +48 -0
  464. package/dist/src/safety/checker-runner.js +208 -0
  465. package/dist/src/safety/checker-runner.js.map +1 -0
  466. package/dist/src/safety/checker-runner.test.d.ts +6 -0
  467. package/dist/src/safety/checker-runner.test.js +238 -0
  468. package/dist/src/safety/checker-runner.test.js.map +1 -0
  469. package/dist/src/safety/context-builder.d.ts +23 -0
  470. package/dist/src/safety/context-builder.js +47 -0
  471. package/dist/src/safety/context-builder.js.map +1 -0
  472. package/dist/src/safety/context-builder.test.d.ts +6 -0
  473. package/dist/src/safety/context-builder.test.js +49 -0
  474. package/dist/src/safety/context-builder.test.js.map +1 -0
  475. package/dist/src/safety/protocol.d.ts +88 -0
  476. package/dist/src/safety/protocol.js +15 -0
  477. package/dist/src/safety/protocol.js.map +1 -0
  478. package/dist/src/safety/registry.d.ts +26 -0
  479. package/dist/src/safety/registry.js +65 -0
  480. package/dist/src/safety/registry.js.map +1 -0
  481. package/dist/src/safety/registry.test.d.ts +6 -0
  482. package/dist/src/safety/registry.test.js +31 -0
  483. package/dist/src/safety/registry.test.js.map +1 -0
  484. package/dist/src/services/chatCompressionService.d.ts +32 -0
  485. package/dist/src/services/chatCompressionService.js +162 -0
  486. package/dist/src/services/chatCompressionService.js.map +1 -0
  487. package/dist/src/services/chatCompressionService.test.d.ts +6 -0
  488. package/dist/src/services/chatCompressionService.test.js +210 -0
  489. package/dist/src/services/chatCompressionService.test.js.map +1 -0
  490. package/dist/src/services/chatRecordingService.d.ts +10 -15
  491. package/dist/src/services/chatRecordingService.js +43 -29
  492. package/dist/src/services/chatRecordingService.js.map +1 -1
  493. package/dist/src/services/chatRecordingService.test.js +69 -25
  494. package/dist/src/services/chatRecordingService.test.js.map +1 -1
  495. package/dist/src/services/fileDiscoveryService.d.ts +8 -10
  496. package/dist/src/services/fileDiscoveryService.js +31 -53
  497. package/dist/src/services/fileDiscoveryService.js.map +1 -1
  498. package/dist/src/services/fileDiscoveryService.test.js +94 -14
  499. package/dist/src/services/fileDiscoveryService.test.js.map +1 -1
  500. package/dist/src/services/fileSystemService.d.ts +9 -0
  501. package/dist/src/services/fileSystemService.js +12 -1
  502. package/dist/src/services/fileSystemService.js.map +1 -1
  503. package/dist/src/services/fileSystemService.test.js +1 -1
  504. package/dist/src/services/fileSystemService.test.js.map +1 -1
  505. package/dist/src/services/gitService.d.ts +3 -1
  506. package/dist/src/services/gitService.js +30 -24
  507. package/dist/src/services/gitService.js.map +1 -1
  508. package/dist/src/services/gitService.test.js +30 -37
  509. package/dist/src/services/gitService.test.js.map +1 -1
  510. package/dist/src/services/loopDetectionService.d.ts +12 -3
  511. package/dist/src/services/loopDetectionService.js +148 -55
  512. package/dist/src/services/loopDetectionService.js.map +1 -1
  513. package/dist/src/services/loopDetectionService.test.js +280 -21
  514. package/dist/src/services/loopDetectionService.test.js.map +1 -1
  515. package/dist/src/services/modelConfig.golden.test.d.ts +6 -0
  516. package/dist/src/services/modelConfig.golden.test.js +42 -0
  517. package/dist/src/services/modelConfig.golden.test.js.map +1 -0
  518. package/dist/src/services/modelConfig.integration.test.d.ts +6 -0
  519. package/dist/src/services/modelConfig.integration.test.js +247 -0
  520. package/dist/src/services/modelConfig.integration.test.js.map +1 -0
  521. package/dist/src/services/modelConfigService.d.ts +48 -0
  522. package/dist/src/services/modelConfigService.js +151 -0
  523. package/dist/src/services/modelConfigService.js.map +1 -0
  524. package/dist/src/services/modelConfigService.test.d.ts +6 -0
  525. package/dist/src/services/modelConfigService.test.js +531 -0
  526. package/dist/src/services/modelConfigService.test.js.map +1 -0
  527. package/dist/src/services/shellExecutionService.d.ts +37 -2
  528. package/dist/src/services/shellExecutionService.js +361 -67
  529. package/dist/src/services/shellExecutionService.js.map +1 -1
  530. package/dist/src/services/shellExecutionService.test.js +333 -71
  531. package/dist/src/services/shellExecutionService.test.js.map +1 -1
  532. package/dist/src/services/test-data/resolved-aliases.golden.json +202 -0
  533. package/dist/src/telemetry/activity-detector.d.ts +41 -0
  534. package/dist/src/telemetry/activity-detector.js +61 -0
  535. package/dist/src/telemetry/activity-detector.js.map +1 -0
  536. package/dist/src/telemetry/activity-detector.test.d.ts +6 -0
  537. package/dist/src/telemetry/activity-detector.test.js +136 -0
  538. package/dist/src/telemetry/activity-detector.test.js.map +1 -0
  539. package/dist/src/telemetry/activity-monitor.d.ts +116 -0
  540. package/dist/src/telemetry/activity-monitor.js +209 -0
  541. package/dist/src/telemetry/activity-monitor.js.map +1 -0
  542. package/dist/src/telemetry/activity-monitor.test.d.ts +6 -0
  543. package/dist/src/telemetry/activity-monitor.test.js +251 -0
  544. package/dist/src/telemetry/activity-monitor.test.js.map +1 -0
  545. package/dist/src/telemetry/activity-types.d.ts +19 -0
  546. package/dist/src/telemetry/activity-types.js +21 -0
  547. package/dist/src/telemetry/activity-types.js.map +1 -0
  548. package/dist/src/telemetry/clearcut-logger/clearcut-logger.d.ts +53 -5
  549. package/dist/src/telemetry/clearcut-logger/clearcut-logger.js +581 -56
  550. package/dist/src/telemetry/clearcut-logger/clearcut-logger.js.map +1 -1
  551. package/dist/src/telemetry/clearcut-logger/clearcut-logger.test.d.ts +2 -0
  552. package/dist/src/telemetry/clearcut-logger/clearcut-logger.test.js +467 -31
  553. package/dist/src/telemetry/clearcut-logger/clearcut-logger.test.js.map +1 -1
  554. package/dist/src/telemetry/clearcut-logger/event-metadata-key.d.ts +75 -4
  555. package/dist/src/telemetry/clearcut-logger/event-metadata-key.js +182 -6
  556. package/dist/src/telemetry/clearcut-logger/event-metadata-key.js.map +1 -1
  557. package/dist/src/telemetry/config.d.ts +31 -0
  558. package/dist/src/telemetry/config.js +76 -0
  559. package/dist/src/telemetry/config.js.map +1 -0
  560. package/dist/src/telemetry/config.test.d.ts +6 -0
  561. package/dist/src/telemetry/config.test.js +124 -0
  562. package/dist/src/telemetry/config.test.js.map +1 -0
  563. package/dist/src/telemetry/constants.d.ts +0 -18
  564. package/dist/src/telemetry/constants.js +0 -18
  565. package/dist/src/telemetry/constants.js.map +1 -1
  566. package/dist/src/telemetry/file-exporters.d.ts +5 -4
  567. package/dist/src/telemetry/file-exporters.js +1 -1
  568. package/dist/src/telemetry/file-exporters.js.map +1 -1
  569. package/dist/src/telemetry/gcp-exporters.d.ts +34 -0
  570. package/dist/src/telemetry/gcp-exporters.js +116 -0
  571. package/dist/src/telemetry/gcp-exporters.js.map +1 -0
  572. package/dist/src/telemetry/gcp-exporters.test.d.ts +6 -0
  573. package/dist/src/telemetry/gcp-exporters.test.js +318 -0
  574. package/dist/src/telemetry/gcp-exporters.test.js.map +1 -0
  575. package/dist/src/telemetry/high-water-mark-tracker.d.ts +43 -0
  576. package/dist/src/telemetry/high-water-mark-tracker.js +88 -0
  577. package/dist/src/telemetry/high-water-mark-tracker.js.map +1 -0
  578. package/dist/src/telemetry/high-water-mark-tracker.test.d.ts +6 -0
  579. package/dist/src/telemetry/high-water-mark-tracker.test.js +152 -0
  580. package/dist/src/telemetry/high-water-mark-tracker.test.js.map +1 -0
  581. package/dist/src/telemetry/index.d.ts +16 -2
  582. package/dist/src/telemetry/index.js +25 -2
  583. package/dist/src/telemetry/index.js.map +1 -1
  584. package/dist/src/telemetry/loggers.d.ts +25 -3
  585. package/dist/src/telemetry/loggers.js +333 -154
  586. package/dist/src/telemetry/loggers.js.map +1 -1
  587. package/dist/src/telemetry/loggers.test.circular.js +3 -4
  588. package/dist/src/telemetry/loggers.test.circular.js.map +1 -1
  589. package/dist/src/telemetry/loggers.test.js +868 -63
  590. package/dist/src/telemetry/loggers.test.js.map +1 -1
  591. package/dist/src/telemetry/memory-monitor.d.ts +149 -0
  592. package/dist/src/telemetry/memory-monitor.js +335 -0
  593. package/dist/src/telemetry/memory-monitor.js.map +1 -0
  594. package/dist/src/telemetry/memory-monitor.test.d.ts +6 -0
  595. package/dist/src/telemetry/memory-monitor.test.js +472 -0
  596. package/dist/src/telemetry/memory-monitor.test.js.map +1 -0
  597. package/dist/src/telemetry/metrics.d.ts +498 -11
  598. package/dist/src/telemetry/metrics.js +729 -84
  599. package/dist/src/telemetry/metrics.js.map +1 -1
  600. package/dist/src/telemetry/metrics.test.js +964 -101
  601. package/dist/src/telemetry/metrics.test.js.map +1 -1
  602. package/dist/src/telemetry/rate-limiter.d.ts +48 -0
  603. package/dist/src/telemetry/rate-limiter.js +100 -0
  604. package/dist/src/telemetry/rate-limiter.js.map +1 -0
  605. package/dist/src/telemetry/rate-limiter.test.d.ts +6 -0
  606. package/dist/src/telemetry/rate-limiter.test.js +207 -0
  607. package/dist/src/telemetry/rate-limiter.test.js.map +1 -0
  608. package/dist/src/telemetry/sdk.d.ts +1 -1
  609. package/dist/src/telemetry/sdk.js +23 -4
  610. package/dist/src/telemetry/sdk.js.map +1 -1
  611. package/dist/src/telemetry/sdk.test.js +108 -0
  612. package/dist/src/telemetry/sdk.test.js.map +1 -1
  613. package/dist/src/telemetry/semantic.d.ts +82 -0
  614. package/dist/src/telemetry/semantic.js +269 -0
  615. package/dist/src/telemetry/semantic.js.map +1 -0
  616. package/dist/src/telemetry/semantic.test.d.ts +6 -0
  617. package/dist/src/telemetry/semantic.test.js +387 -0
  618. package/dist/src/telemetry/semantic.test.js.map +1 -0
  619. package/dist/src/telemetry/telemetry-utils.d.ts +6 -0
  620. package/dist/src/telemetry/telemetry-utils.js +14 -0
  621. package/dist/src/telemetry/telemetry-utils.js.map +1 -0
  622. package/dist/src/telemetry/telemetry-utils.test.d.ts +6 -0
  623. package/dist/src/telemetry/telemetry-utils.test.js +41 -0
  624. package/dist/src/telemetry/telemetry-utils.test.js.map +1 -0
  625. package/dist/src/telemetry/telemetryAttributes.d.ts +8 -0
  626. package/dist/src/telemetry/telemetryAttributes.js +19 -0
  627. package/dist/src/telemetry/telemetryAttributes.js.map +1 -0
  628. package/dist/src/telemetry/trace.d.ts +46 -0
  629. package/dist/src/telemetry/trace.js +121 -0
  630. package/dist/src/telemetry/trace.js.map +1 -0
  631. package/dist/src/telemetry/types.d.ts +351 -25
  632. package/dist/src/telemetry/types.js +1071 -63
  633. package/dist/src/telemetry/types.js.map +1 -1
  634. package/dist/src/telemetry/uiTelemetry.d.ts +4 -4
  635. package/dist/src/telemetry/uiTelemetry.js +14 -15
  636. package/dist/src/telemetry/uiTelemetry.js.map +1 -1
  637. package/dist/src/telemetry/uiTelemetry.test.js +122 -96
  638. package/dist/src/telemetry/uiTelemetry.test.js.map +1 -1
  639. package/dist/src/test-utils/config.d.ts +3 -2
  640. package/dist/src/test-utils/config.js +1 -1
  641. package/dist/src/test-utils/config.js.map +1 -1
  642. package/dist/src/test-utils/index.d.ts +6 -0
  643. package/dist/src/test-utils/index.js +7 -0
  644. package/dist/src/test-utils/index.js.map +1 -0
  645. package/dist/src/test-utils/mock-tool.d.ts +66 -0
  646. package/dist/src/test-utils/{tools.js → mock-tool.js} +45 -29
  647. package/dist/src/test-utils/mock-tool.js.map +1 -0
  648. package/dist/src/test-utils/mockWorkspaceContext.d.ts +1 -1
  649. package/dist/src/tools/base-tool-invocation.test.d.ts +6 -0
  650. package/dist/src/tools/base-tool-invocation.test.js +85 -0
  651. package/dist/src/tools/base-tool-invocation.test.js.map +1 -0
  652. package/dist/src/tools/diffOptions.d.ts +1 -1
  653. package/dist/src/tools/diffOptions.js +21 -13
  654. package/dist/src/tools/diffOptions.js.map +1 -1
  655. package/dist/src/tools/diffOptions.test.js +58 -22
  656. package/dist/src/tools/diffOptions.test.js.map +1 -1
  657. package/dist/src/tools/edit.d.ts +10 -8
  658. package/dist/src/tools/edit.js +100 -79
  659. package/dist/src/tools/edit.js.map +1 -1
  660. package/dist/src/tools/edit.test.js +429 -163
  661. package/dist/src/tools/edit.test.js.map +1 -1
  662. package/dist/src/tools/glob.d.ts +11 -5
  663. package/dist/src/tools/glob.js +58 -42
  664. package/dist/src/tools/glob.js.map +1 -1
  665. package/dist/src/tools/glob.test.js +249 -166
  666. package/dist/src/tools/glob.test.js.map +1 -1
  667. package/dist/src/tools/grep.d.ts +7 -5
  668. package/dist/src/tools/grep.js +66 -40
  669. package/dist/src/tools/grep.js.map +1 -1
  670. package/dist/src/tools/grep.test.js +41 -15
  671. package/dist/src/tools/grep.test.js.map +1 -1
  672. package/dist/src/tools/ls.d.ts +7 -5
  673. package/dist/src/tools/ls.js +54 -68
  674. package/dist/src/tools/ls.js.map +1 -1
  675. package/dist/src/tools/ls.test.js +150 -293
  676. package/dist/src/tools/ls.test.js.map +1 -1
  677. package/dist/src/tools/mcp-client-manager.d.ts +52 -12
  678. package/dist/src/tools/mcp-client-manager.js +196 -27
  679. package/dist/src/tools/mcp-client-manager.js.map +1 -1
  680. package/dist/src/tools/mcp-client-manager.test.js +139 -13
  681. package/dist/src/tools/mcp-client-manager.test.js.map +1 -1
  682. package/dist/src/tools/mcp-client.d.ts +28 -24
  683. package/dist/src/tools/mcp-client.js +338 -351
  684. package/dist/src/tools/mcp-client.js.map +1 -1
  685. package/dist/src/tools/mcp-client.test.js +380 -193
  686. package/dist/src/tools/mcp-client.test.js.map +1 -1
  687. package/dist/src/tools/mcp-tool.d.ts +11 -5
  688. package/dist/src/tools/mcp-tool.js +66 -17
  689. package/dist/src/tools/mcp-tool.js.map +1 -1
  690. package/dist/src/tools/mcp-tool.test.js +278 -211
  691. package/dist/src/tools/mcp-tool.test.js.map +1 -1
  692. package/dist/src/tools/memoryTool.d.ts +10 -7
  693. package/dist/src/tools/memoryTool.js +28 -49
  694. package/dist/src/tools/memoryTool.js.map +1 -1
  695. package/dist/src/tools/memoryTool.test.js +26 -13
  696. package/dist/src/tools/memoryTool.test.js.map +1 -1
  697. package/dist/src/tools/message-bus-integration.test.d.ts +6 -0
  698. package/dist/src/tools/message-bus-integration.test.js +196 -0
  699. package/dist/src/tools/message-bus-integration.test.js.map +1 -0
  700. package/dist/src/tools/modifiable-tool.d.ts +7 -3
  701. package/dist/src/tools/modifiable-tool.js +41 -19
  702. package/dist/src/tools/modifiable-tool.js.map +1 -1
  703. package/dist/src/tools/modifiable-tool.test.js +70 -35
  704. package/dist/src/tools/modifiable-tool.test.js.map +1 -1
  705. package/dist/src/tools/read-file.d.ts +9 -7
  706. package/dist/src/tools/read-file.js +39 -60
  707. package/dist/src/tools/read-file.js.map +1 -1
  708. package/dist/src/tools/read-file.test.js +103 -36
  709. package/dist/src/tools/read-file.test.js.map +1 -1
  710. package/dist/src/tools/read-many-files.d.ts +9 -14
  711. package/dist/src/tools/read-many-files.js +71 -155
  712. package/dist/src/tools/read-many-files.js.map +1 -1
  713. package/dist/src/tools/read-many-files.test.js +98 -44
  714. package/dist/src/tools/read-many-files.test.js.map +1 -1
  715. package/dist/src/tools/ripGrep.d.ts +73 -0
  716. package/dist/src/tools/ripGrep.js +395 -0
  717. package/dist/src/tools/ripGrep.js.map +1 -0
  718. package/dist/src/tools/ripGrep.test.d.ts +6 -0
  719. package/dist/src/tools/ripGrep.test.js +1305 -0
  720. package/dist/src/tools/ripGrep.test.js.map +1 -0
  721. package/dist/src/tools/shell.d.ts +19 -6
  722. package/dist/src/tools/shell.js +92 -56
  723. package/dist/src/tools/shell.js.map +1 -1
  724. package/dist/src/tools/shell.test.js +176 -103
  725. package/dist/src/tools/shell.test.js.map +1 -1
  726. package/dist/src/tools/smart-edit.d.ts +78 -0
  727. package/dist/src/tools/smart-edit.js +717 -0
  728. package/dist/src/tools/smart-edit.js.map +1 -0
  729. package/dist/src/tools/smart-edit.test.d.ts +6 -0
  730. package/dist/src/tools/smart-edit.test.js +592 -0
  731. package/dist/src/tools/smart-edit.test.js.map +1 -0
  732. package/dist/src/tools/tool-error.d.ts +39 -1
  733. package/dist/src/tools/tool-error.js +54 -0
  734. package/dist/src/tools/tool-error.js.map +1 -1
  735. package/dist/src/tools/tool-names.d.ts +17 -0
  736. package/dist/src/tools/tool-names.js +21 -0
  737. package/dist/src/tools/tool-names.js.map +1 -0
  738. package/dist/src/tools/tool-registry.d.ts +38 -20
  739. package/dist/src/tools/tool-registry.js +134 -77
  740. package/dist/src/tools/tool-registry.js.map +1 -1
  741. package/dist/src/tools/tool-registry.test.js +218 -58
  742. package/dist/src/tools/tool-registry.test.js.map +1 -1
  743. package/dist/src/tools/tools.d.ts +49 -17
  744. package/dist/src/tools/tools.js +150 -8
  745. package/dist/src/tools/tools.js.map +1 -1
  746. package/dist/src/tools/tools.test.js +1 -2
  747. package/dist/src/tools/tools.test.js.map +1 -1
  748. package/dist/src/tools/web-fetch.d.ts +14 -5
  749. package/dist/src/tools/web-fetch.js +90 -44
  750. package/dist/src/tools/web-fetch.js.map +1 -1
  751. package/dist/src/tools/web-fetch.test.js +388 -20
  752. package/dist/src/tools/web-fetch.test.js.map +1 -1
  753. package/dist/src/tools/web-search.d.ts +8 -6
  754. package/dist/src/tools/web-search.js +40 -15
  755. package/dist/src/tools/web-search.js.map +1 -1
  756. package/dist/src/tools/web-search.test.js +75 -1
  757. package/dist/src/tools/web-search.test.js.map +1 -1
  758. package/dist/src/tools/write-file.d.ts +7 -5
  759. package/dist/src/tools/write-file.js +54 -54
  760. package/dist/src/tools/write-file.js.map +1 -1
  761. package/dist/src/tools/write-file.test.js +237 -146
  762. package/dist/src/tools/write-file.test.js.map +1 -1
  763. package/dist/src/tools/write-todos.d.ts +50 -0
  764. package/dist/src/tools/write-todos.js +193 -0
  765. package/dist/src/tools/write-todos.js.map +1 -0
  766. package/dist/src/tools/write-todos.test.d.ts +6 -0
  767. package/dist/src/tools/write-todos.test.js +89 -0
  768. package/dist/src/tools/write-todos.test.js.map +1 -0
  769. package/dist/src/utils/bfsFileSearch.d.ts +2 -2
  770. package/dist/src/utils/bfsFileSearch.js +16 -9
  771. package/dist/src/utils/bfsFileSearch.js.map +1 -1
  772. package/dist/src/utils/bfsFileSearch.test.js +3 -3
  773. package/dist/src/utils/bfsFileSearch.test.js.map +1 -1
  774. package/dist/src/utils/channel.d.ts +19 -0
  775. package/dist/src/utils/channel.js +49 -0
  776. package/dist/src/utils/channel.js.map +1 -0
  777. package/dist/src/utils/channel.test.d.ts +6 -0
  778. package/dist/src/utils/channel.test.js +170 -0
  779. package/dist/src/utils/channel.test.js.map +1 -0
  780. package/dist/src/utils/debugLogger.d.ts +25 -0
  781. package/dist/src/utils/debugLogger.js +33 -0
  782. package/dist/src/utils/debugLogger.js.map +1 -0
  783. package/dist/src/utils/debugLogger.test.d.ts +6 -0
  784. package/dist/src/utils/debugLogger.test.js +69 -0
  785. package/dist/src/utils/debugLogger.test.js.map +1 -0
  786. package/dist/src/utils/delay.d.ts +16 -0
  787. package/dist/src/utils/delay.js +43 -0
  788. package/dist/src/utils/delay.js.map +1 -0
  789. package/dist/src/utils/delay.test.d.ts +6 -0
  790. package/dist/src/utils/delay.test.js +88 -0
  791. package/dist/src/utils/delay.test.js.map +1 -0
  792. package/dist/src/utils/editCorrector.d.ts +9 -8
  793. package/dist/src/utils/editCorrector.js +62 -34
  794. package/dist/src/utils/editCorrector.js.map +1 -1
  795. package/dist/src/utils/editCorrector.test.js +52 -87
  796. package/dist/src/utils/editCorrector.test.js.map +1 -1
  797. package/dist/src/utils/editor.d.ts +4 -2
  798. package/dist/src/utils/editor.js +56 -55
  799. package/dist/src/utils/editor.js.map +1 -1
  800. package/dist/src/utils/editor.test.js +47 -88
  801. package/dist/src/utils/editor.test.js.map +1 -1
  802. package/dist/src/utils/environmentContext.d.ts +3 -2
  803. package/dist/src/utils/environmentContext.js +20 -33
  804. package/dist/src/utils/environmentContext.js.map +1 -1
  805. package/dist/src/utils/environmentContext.test.js +6 -34
  806. package/dist/src/utils/environmentContext.test.js.map +1 -1
  807. package/dist/src/utils/errorParsing.d.ts +1 -1
  808. package/dist/src/utils/errorParsing.js +5 -33
  809. package/dist/src/utils/errorParsing.js.map +1 -1
  810. package/dist/src/utils/errorParsing.test.js +0 -88
  811. package/dist/src/utils/errorParsing.test.js.map +1 -1
  812. package/dist/src/utils/errorReporting.d.ts +1 -1
  813. package/dist/src/utils/errors.d.ts +28 -0
  814. package/dist/src/utils/errors.js +48 -0
  815. package/dist/src/utils/errors.js.map +1 -1
  816. package/dist/src/utils/events.d.ts +121 -0
  817. package/dist/src/utils/events.js +84 -0
  818. package/dist/src/utils/events.js.map +1 -0
  819. package/dist/src/utils/events.test.d.ts +6 -0
  820. package/dist/src/utils/events.test.js +212 -0
  821. package/dist/src/utils/events.test.js.map +1 -0
  822. package/dist/src/utils/extensionLoader.d.ts +86 -0
  823. package/dist/src/utils/extensionLoader.js +208 -0
  824. package/dist/src/utils/extensionLoader.js.map +1 -0
  825. package/dist/src/utils/extensionLoader.test.d.ts +6 -0
  826. package/dist/src/utils/extensionLoader.test.js +154 -0
  827. package/dist/src/utils/extensionLoader.test.js.map +1 -0
  828. package/dist/src/utils/fetch.d.ts +1 -0
  829. package/dist/src/utils/fetch.js +5 -1
  830. package/dist/src/utils/fetch.js.map +1 -1
  831. package/dist/src/utils/fileUtils.d.ts +28 -12
  832. package/dist/src/utils/fileUtils.js +204 -81
  833. package/dist/src/utils/fileUtils.js.map +1 -1
  834. package/dist/src/utils/fileUtils.test.js +426 -82
  835. package/dist/src/utils/fileUtils.test.js.map +1 -1
  836. package/dist/src/utils/filesearch/crawler.d.ts +1 -1
  837. package/dist/src/utils/filesearch/crawler.test.js +2 -2
  838. package/dist/src/utils/filesearch/crawler.test.js.map +1 -1
  839. package/dist/src/utils/filesearch/fileSearch.d.ts +1 -0
  840. package/dist/src/utils/filesearch/fileSearch.js +14 -9
  841. package/dist/src/utils/filesearch/fileSearch.js.map +1 -1
  842. package/dist/src/utils/filesearch/fileSearch.test.js +90 -0
  843. package/dist/src/utils/filesearch/fileSearch.test.js.map +1 -1
  844. package/dist/src/utils/flashFallback.test.d.ts +6 -0
  845. package/dist/src/utils/flashFallback.test.js +103 -0
  846. package/dist/src/utils/flashFallback.test.js.map +1 -0
  847. package/dist/src/utils/formatters.d.ts +1 -0
  848. package/dist/src/utils/formatters.js +2 -1
  849. package/dist/src/utils/formatters.js.map +1 -1
  850. package/dist/src/utils/formatters.test.d.ts +6 -0
  851. package/dist/src/utils/formatters.test.js +26 -0
  852. package/dist/src/utils/formatters.test.js.map +1 -0
  853. package/dist/src/utils/geminiIgnoreParser.d.ts +18 -0
  854. package/dist/src/utils/geminiIgnoreParser.js +61 -0
  855. package/dist/src/utils/geminiIgnoreParser.js.map +1 -0
  856. package/dist/src/utils/geminiIgnoreParser.test.d.ts +6 -0
  857. package/dist/src/utils/geminiIgnoreParser.test.js +50 -0
  858. package/dist/src/utils/geminiIgnoreParser.test.js.map +1 -0
  859. package/dist/src/utils/generateContentResponseUtilities.d.ts +1 -2
  860. package/dist/src/utils/generateContentResponseUtilities.js +1 -13
  861. package/dist/src/utils/generateContentResponseUtilities.js.map +1 -1
  862. package/dist/src/utils/generateContentResponseUtilities.test.js +2 -40
  863. package/dist/src/utils/generateContentResponseUtilities.test.js.map +1 -1
  864. package/dist/src/utils/getFolderStructure.d.ts +2 -2
  865. package/dist/src/utils/getFolderStructure.js +12 -20
  866. package/dist/src/utils/getFolderStructure.js.map +1 -1
  867. package/dist/src/utils/getFolderStructure.test.js +11 -10
  868. package/dist/src/utils/getFolderStructure.test.js.map +1 -1
  869. package/dist/src/utils/gitIgnoreParser.d.ts +7 -8
  870. package/dist/src/utils/gitIgnoreParser.js +145 -36
  871. package/dist/src/utils/gitIgnoreParser.js.map +1 -1
  872. package/dist/src/utils/gitIgnoreParser.test.js +127 -38
  873. package/dist/src/utils/gitIgnoreParser.test.js.map +1 -1
  874. package/dist/src/utils/gitUtils.js +2 -2
  875. package/dist/src/utils/gitUtils.js.map +1 -1
  876. package/dist/src/utils/googleErrors.d.ts +104 -0
  877. package/dist/src/utils/googleErrors.js +152 -0
  878. package/dist/src/utils/googleErrors.js.map +1 -0
  879. package/dist/src/utils/googleErrors.test.d.ts +6 -0
  880. package/dist/src/utils/googleErrors.test.js +301 -0
  881. package/dist/src/utils/googleErrors.test.js.map +1 -0
  882. package/dist/src/utils/googleQuotaErrors.d.ts +37 -0
  883. package/dist/src/utils/googleQuotaErrors.js +157 -0
  884. package/dist/src/utils/googleQuotaErrors.js.map +1 -0
  885. package/dist/src/utils/googleQuotaErrors.test.d.ts +6 -0
  886. package/dist/src/utils/googleQuotaErrors.test.js +311 -0
  887. package/dist/src/utils/googleQuotaErrors.test.js.map +1 -0
  888. package/dist/src/utils/httpErrors.d.ts +18 -0
  889. package/dist/src/utils/httpErrors.js +36 -0
  890. package/dist/src/utils/httpErrors.js.map +1 -0
  891. package/dist/src/utils/ignorePatterns.d.ts +103 -0
  892. package/dist/src/utils/ignorePatterns.js +220 -0
  893. package/dist/src/utils/ignorePatterns.js.map +1 -0
  894. package/dist/src/utils/ignorePatterns.test.d.ts +6 -0
  895. package/dist/src/utils/ignorePatterns.test.js +246 -0
  896. package/dist/src/utils/ignorePatterns.test.js.map +1 -0
  897. package/dist/src/utils/installationManager.d.ts +16 -0
  898. package/dist/src/utils/installationManager.js +51 -0
  899. package/dist/src/utils/installationManager.js.map +1 -0
  900. package/dist/src/utils/installationManager.test.d.ts +6 -0
  901. package/dist/src/utils/installationManager.test.js +85 -0
  902. package/dist/src/utils/installationManager.test.js.map +1 -0
  903. package/dist/src/utils/language-detection.d.ts +6 -0
  904. package/dist/src/utils/language-detection.js +101 -0
  905. package/dist/src/utils/language-detection.js.map +1 -0
  906. package/dist/src/utils/llm-edit-fixer.d.ts +26 -0
  907. package/dist/src/utils/llm-edit-fixer.js +155 -0
  908. package/dist/src/utils/llm-edit-fixer.js.map +1 -0
  909. package/dist/src/utils/llm-edit-fixer.test.d.ts +6 -0
  910. package/dist/src/utils/llm-edit-fixer.test.js +223 -0
  911. package/dist/src/utils/llm-edit-fixer.test.js.map +1 -0
  912. package/dist/src/utils/memoryDiscovery.d.ts +26 -6
  913. package/dist/src/utils/memoryDiscovery.js +239 -40
  914. package/dist/src/utils/memoryDiscovery.js.map +1 -1
  915. package/dist/src/utils/memoryDiscovery.test.js +365 -44
  916. package/dist/src/utils/memoryDiscovery.test.js.map +1 -1
  917. package/dist/src/utils/memoryImportProcessor.js +19 -25
  918. package/dist/src/utils/memoryImportProcessor.js.map +1 -1
  919. package/dist/src/utils/memoryImportProcessor.test.js +24 -155
  920. package/dist/src/utils/memoryImportProcessor.test.js.map +1 -1
  921. package/dist/src/utils/messageInspectors.d.ts +1 -1
  922. package/dist/src/utils/nextSpeakerChecker.d.ts +3 -3
  923. package/dist/src/utils/nextSpeakerChecker.js +10 -4
  924. package/dist/src/utils/nextSpeakerChecker.js.map +1 -1
  925. package/dist/src/utils/nextSpeakerChecker.test.js +85 -66
  926. package/dist/src/utils/nextSpeakerChecker.test.js.map +1 -1
  927. package/dist/src/utils/package.d.ts +12 -0
  928. package/dist/src/utils/package.js +15 -0
  929. package/dist/src/utils/package.js.map +1 -0
  930. package/dist/src/utils/partUtils.d.ts +22 -1
  931. package/dist/src/utils/partUtils.js +68 -0
  932. package/dist/src/utils/partUtils.js.map +1 -1
  933. package/dist/src/utils/partUtils.test.js +112 -1
  934. package/dist/src/utils/partUtils.test.js.map +1 -1
  935. package/dist/src/utils/pathCorrector.d.ts +25 -0
  936. package/dist/src/utils/pathCorrector.js +33 -0
  937. package/dist/src/utils/pathCorrector.js.map +1 -0
  938. package/dist/src/utils/pathCorrector.test.d.ts +6 -0
  939. package/dist/src/utils/pathCorrector.test.js +83 -0
  940. package/dist/src/utils/pathCorrector.test.js.map +1 -0
  941. package/dist/src/utils/pathReader.d.ts +17 -0
  942. package/dist/src/utils/pathReader.js +92 -0
  943. package/dist/src/utils/pathReader.js.map +1 -0
  944. package/dist/src/utils/pathReader.test.d.ts +6 -0
  945. package/dist/src/utils/pathReader.test.js +406 -0
  946. package/dist/src/utils/pathReader.test.js.map +1 -0
  947. package/dist/src/utils/paths.d.ts +1 -18
  948. package/dist/src/utils/paths.js +133 -57
  949. package/dist/src/utils/paths.js.map +1 -1
  950. package/dist/src/utils/paths.test.js +200 -68
  951. package/dist/src/utils/paths.test.js.map +1 -1
  952. package/dist/src/utils/promptIdContext.d.ts +7 -0
  953. package/dist/src/utils/promptIdContext.js +8 -0
  954. package/dist/src/utils/promptIdContext.js.map +1 -0
  955. package/dist/src/utils/quotaErrorDetection.d.ts +1 -3
  956. package/dist/src/utils/quotaErrorDetection.js +0 -46
  957. package/dist/src/utils/quotaErrorDetection.js.map +1 -1
  958. package/dist/src/utils/retry.d.ts +5 -10
  959. package/dist/src/utils/retry.js +114 -197
  960. package/dist/src/utils/retry.js.map +1 -1
  961. package/dist/src/utils/retry.test.js +196 -130
  962. package/dist/src/utils/retry.test.js.map +1 -1
  963. package/dist/src/utils/safeJsonStringify.d.ts +4 -4
  964. package/dist/src/utils/safeJsonStringify.js +31 -7
  965. package/dist/src/utils/safeJsonStringify.js.map +1 -1
  966. package/dist/src/utils/schemaValidator.js +15 -1
  967. package/dist/src/utils/schemaValidator.js.map +1 -1
  968. package/dist/src/utils/schemaValidator.test.d.ts +6 -0
  969. package/dist/src/utils/schemaValidator.test.js +113 -0
  970. package/dist/src/utils/schemaValidator.test.js.map +1 -0
  971. package/dist/src/utils/session.js +1 -1
  972. package/dist/src/utils/session.js.map +1 -1
  973. package/dist/src/utils/shell-utils.d.ts +21 -3
  974. package/dist/src/utils/shell-utils.js +427 -159
  975. package/dist/src/utils/shell-utils.js.map +1 -1
  976. package/dist/src/utils/shell-utils.test.js +250 -59
  977. package/dist/src/utils/shell-utils.test.js.map +1 -1
  978. package/dist/src/utils/stdio.d.ts +32 -0
  979. package/dist/src/utils/stdio.js +85 -0
  980. package/dist/src/utils/stdio.js.map +1 -0
  981. package/dist/src/utils/stdio.test.d.ts +6 -0
  982. package/dist/src/utils/stdio.test.js +47 -0
  983. package/dist/src/utils/stdio.test.js.map +1 -0
  984. package/dist/src/utils/summarizer.d.ts +6 -4
  985. package/dist/src/utils/summarizer.js +8 -9
  986. package/dist/src/utils/summarizer.js.map +1 -1
  987. package/dist/src/utils/summarizer.test.js +32 -12
  988. package/dist/src/utils/summarizer.test.js.map +1 -1
  989. package/dist/src/utils/systemEncoding.js +7 -6
  990. package/dist/src/utils/systemEncoding.js.map +1 -1
  991. package/dist/src/utils/systemEncoding.test.js +4 -3
  992. package/dist/src/utils/systemEncoding.test.js.map +1 -1
  993. package/dist/src/utils/terminal.d.ts +14 -0
  994. package/dist/src/utils/terminal.js +38 -0
  995. package/dist/src/utils/terminal.js.map +1 -0
  996. package/dist/src/utils/terminalSerializer.d.ts +25 -0
  997. package/dist/src/utils/terminalSerializer.js +432 -0
  998. package/dist/src/utils/terminalSerializer.js.map +1 -0
  999. package/dist/src/utils/terminalSerializer.test.d.ts +6 -0
  1000. package/dist/src/utils/terminalSerializer.test.js +176 -0
  1001. package/dist/src/utils/terminalSerializer.test.js.map +1 -0
  1002. package/dist/src/utils/textUtils.d.ts +5 -0
  1003. package/dist/src/utils/textUtils.js +14 -0
  1004. package/dist/src/utils/textUtils.js.map +1 -1
  1005. package/dist/src/utils/textUtils.test.d.ts +6 -0
  1006. package/dist/src/utils/textUtils.test.js +59 -0
  1007. package/dist/src/utils/textUtils.test.js.map +1 -0
  1008. package/dist/src/utils/thoughtUtils.d.ts +21 -0
  1009. package/dist/src/utils/thoughtUtils.js +39 -0
  1010. package/dist/src/utils/thoughtUtils.js.map +1 -0
  1011. package/dist/src/utils/thoughtUtils.test.d.ts +6 -0
  1012. package/dist/src/utils/thoughtUtils.test.js +78 -0
  1013. package/dist/src/utils/thoughtUtils.test.js.map +1 -0
  1014. package/dist/src/utils/tool-utils.d.ts +19 -0
  1015. package/dist/src/utils/tool-utils.js +67 -0
  1016. package/dist/src/utils/tool-utils.js.map +1 -0
  1017. package/dist/src/utils/tool-utils.test.d.ts +6 -0
  1018. package/dist/src/utils/tool-utils.test.js +69 -0
  1019. package/dist/src/utils/tool-utils.test.js.map +1 -0
  1020. package/dist/src/utils/userAccountManager.d.ts +20 -0
  1021. package/dist/src/utils/userAccountManager.js +115 -0
  1022. package/dist/src/utils/userAccountManager.js.map +1 -0
  1023. package/dist/src/utils/userAccountManager.test.d.ts +6 -0
  1024. package/dist/src/utils/{user_account.test.js → userAccountManager.test.js} +41 -36
  1025. package/dist/src/utils/userAccountManager.test.js.map +1 -0
  1026. package/dist/src/utils/workspaceContext.d.ts +4 -3
  1027. package/dist/src/utils/workspaceContext.js +23 -17
  1028. package/dist/src/utils/workspaceContext.js.map +1 -1
  1029. package/dist/src/utils/workspaceContext.test.js +45 -19
  1030. package/dist/src/utils/workspaceContext.test.js.map +1 -1
  1031. package/dist/tsconfig.tsbuildinfo +1 -1
  1032. package/package.json +24 -10
  1033. package/dist/src/core/modelCheck.d.ts +0 -14
  1034. package/dist/src/core/modelCheck.js +0 -62
  1035. package/dist/src/core/modelCheck.js.map +0 -1
  1036. package/dist/src/test-utils/tools.d.ts +0 -44
  1037. package/dist/src/test-utils/tools.js.map +0 -1
  1038. package/dist/src/utils/flashFallback.integration.test.js +0 -118
  1039. package/dist/src/utils/flashFallback.integration.test.js.map +0 -1
  1040. package/dist/src/utils/user_account.d.ts +0 -9
  1041. package/dist/src/utils/user_account.js +0 -109
  1042. package/dist/src/utils/user_account.js.map +0 -1
  1043. package/dist/src/utils/user_account.test.js.map +0 -1
  1044. package/dist/src/utils/user_id.d.ts +0 -11
  1045. package/dist/src/utils/user_id.js +0 -49
  1046. package/dist/src/utils/user_id.js.map +0 -1
  1047. package/dist/src/utils/user_id.test.js +0 -21
  1048. package/dist/src/utils/user_id.test.js.map +0 -1
  1049. /package/dist/src/{utils/flashFallback.integration.test.d.ts → agents/codebase-investigator.test.d.ts} +0 -0
  1050. /package/dist/src/{utils/user_account.test.d.ts → agents/executor.test.d.ts} +0 -0
  1051. /package/dist/src/{utils/user_id.test.d.ts → agents/invocation.test.d.ts} +0 -0
@@ -4,420 +4,1665 @@
4
4
  * SPDX-License-Identifier: Apache-2.0
5
5
  */
6
6
  import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
7
- import { GeminiChat } from './geminiChat.js';
7
+ import { ApiError, ThinkingLevel } from '@google/genai';
8
+ import { GeminiChat, InvalidStreamError, StreamEventType, SYNTHETIC_THOUGHT_SIGNATURE, } from './geminiChat.js';
8
9
  import { setSimulate429 } from '../utils/testUtils.js';
9
- // Mocks
10
- const mockModelsModule = {
11
- generateContent: vi.fn(),
12
- generateContentStream: vi.fn(),
13
- countTokens: vi.fn(),
14
- embedContent: vi.fn(),
15
- batchEmbedContents: vi.fn(),
16
- };
10
+ import { DEFAULT_GEMINI_FLASH_MODEL, DEFAULT_GEMINI_MODEL, DEFAULT_THINKING_MODE, PREVIEW_GEMINI_MODEL, } from '../config/models.js';
11
+ import { AuthType } from './contentGenerator.js';
12
+ import { TerminalQuotaError } from '../utils/googleQuotaErrors.js';
13
+ import { retryWithBackoff } from '../utils/retry.js';
14
+ import { uiTelemetryService } from '../telemetry/uiTelemetry.js';
15
+ // Mock fs module to prevent actual file system operations during tests
16
+ const mockFileSystem = new Map();
17
+ vi.mock('node:fs', () => {
18
+ const fsModule = {
19
+ mkdirSync: vi.fn(),
20
+ writeFileSync: vi.fn((path, data) => {
21
+ mockFileSystem.set(path, data);
22
+ }),
23
+ readFileSync: vi.fn((path) => {
24
+ if (mockFileSystem.has(path)) {
25
+ return mockFileSystem.get(path);
26
+ }
27
+ throw Object.assign(new Error('ENOENT: no such file or directory'), {
28
+ code: 'ENOENT',
29
+ });
30
+ }),
31
+ existsSync: vi.fn((path) => mockFileSystem.has(path)),
32
+ };
33
+ return {
34
+ default: fsModule,
35
+ ...fsModule,
36
+ };
37
+ });
38
+ const { mockHandleFallback } = vi.hoisted(() => ({
39
+ mockHandleFallback: vi.fn(),
40
+ }));
41
+ // Add mock for the retry utility
42
+ const { mockRetryWithBackoff } = vi.hoisted(() => ({
43
+ mockRetryWithBackoff: vi.fn(),
44
+ }));
45
+ vi.mock('../utils/retry.js', () => ({
46
+ retryWithBackoff: mockRetryWithBackoff,
47
+ }));
48
+ vi.mock('../fallback/handler.js', () => ({
49
+ handleFallback: mockHandleFallback,
50
+ }));
51
+ const { mockLogContentRetry, mockLogContentRetryFailure } = vi.hoisted(() => ({
52
+ mockLogContentRetry: vi.fn(),
53
+ mockLogContentRetryFailure: vi.fn(),
54
+ }));
55
+ vi.mock('../telemetry/loggers.js', () => ({
56
+ logContentRetry: mockLogContentRetry,
57
+ logContentRetryFailure: mockLogContentRetryFailure,
58
+ }));
59
+ vi.mock('../telemetry/uiTelemetry.js', () => ({
60
+ uiTelemetryService: {
61
+ setLastPromptTokenCount: vi.fn(),
62
+ },
63
+ }));
17
64
  describe('GeminiChat', () => {
65
+ let mockContentGenerator;
18
66
  let chat;
19
67
  let mockConfig;
20
- const config = {};
21
68
  beforeEach(() => {
22
69
  vi.clearAllMocks();
70
+ vi.mocked(uiTelemetryService.setLastPromptTokenCount).mockClear();
71
+ mockContentGenerator = {
72
+ generateContent: vi.fn(),
73
+ generateContentStream: vi.fn(),
74
+ countTokens: vi.fn(),
75
+ embedContent: vi.fn(),
76
+ batchEmbedContents: vi.fn(),
77
+ };
78
+ mockHandleFallback.mockClear();
79
+ // Default mock implementation for tests that don't care about retry logic
80
+ mockRetryWithBackoff.mockImplementation(async (apiCall) => apiCall());
23
81
  mockConfig = {
24
82
  getSessionId: () => 'test-session-id',
25
83
  getTelemetryLogPromptsEnabled: () => true,
26
84
  getUsageStatisticsEnabled: () => true,
27
85
  getDebugMode: () => false,
28
- getContentGeneratorConfig: () => ({
29
- authType: 'oauth-personal',
86
+ getPreviewFeatures: () => false,
87
+ getContentGeneratorConfig: vi.fn().mockReturnValue({
88
+ authType: 'oauth-personal', // Ensure this is set for fallback tests
30
89
  model: 'test-model',
31
90
  }),
32
91
  getModel: vi.fn().mockReturnValue('gemini-pro'),
33
92
  setModel: vi.fn(),
93
+ isInFallbackMode: vi.fn().mockReturnValue(false),
34
94
  getQuotaErrorOccurred: vi.fn().mockReturnValue(false),
35
95
  setQuotaErrorOccurred: vi.fn(),
36
96
  flashFallbackHandler: undefined,
97
+ getProjectRoot: vi.fn().mockReturnValue('/test/project/root'),
98
+ storage: {
99
+ getProjectTempDir: vi.fn().mockReturnValue('/test/temp'),
100
+ },
101
+ getToolRegistry: vi.fn().mockReturnValue({
102
+ getTool: vi.fn(),
103
+ }),
104
+ getContentGenerator: vi.fn().mockReturnValue(mockContentGenerator),
105
+ getRetryFetchErrors: vi.fn().mockReturnValue(false),
106
+ modelConfigService: {
107
+ getResolvedConfig: vi.fn().mockImplementation((modelConfigKey) => {
108
+ const thinkingConfig = modelConfigKey.model.startsWith('gemini-3')
109
+ ? {
110
+ thinkingLevel: ThinkingLevel.HIGH,
111
+ }
112
+ : {
113
+ thinkingBudget: DEFAULT_THINKING_MODE,
114
+ };
115
+ return {
116
+ model: modelConfigKey.model,
117
+ generateContentConfig: {
118
+ temperature: 0,
119
+ thinkingConfig,
120
+ },
121
+ };
122
+ }),
123
+ },
124
+ isPreviewModelBypassMode: vi.fn().mockReturnValue(false),
125
+ setPreviewModelBypassMode: vi.fn(),
126
+ isPreviewModelFallbackMode: vi.fn().mockReturnValue(false),
127
+ setPreviewModelFallbackMode: vi.fn(),
128
+ isInteractive: vi.fn().mockReturnValue(false),
37
129
  };
38
130
  // Disable 429 simulation for tests
39
131
  setSimulate429(false);
40
132
  // Reset history for each test by creating a new instance
41
- chat = new GeminiChat(mockConfig, mockModelsModule, config, []);
133
+ chat = new GeminiChat(mockConfig);
42
134
  });
43
135
  afterEach(() => {
44
136
  vi.restoreAllMocks();
45
137
  vi.resetAllMocks();
46
138
  });
47
- describe('sendMessage', () => {
48
- it('should call generateContent with the correct parameters', async () => {
49
- const response = {
50
- candidates: [
51
- {
52
- content: {
53
- parts: [{ text: 'response' }],
54
- role: 'model',
55
- },
56
- finishReason: 'STOP',
57
- index: 0,
58
- safetyRatings: [],
59
- },
60
- ],
61
- text: () => 'response',
62
- };
63
- vi.mocked(mockModelsModule.generateContent).mockResolvedValue(response);
64
- await chat.sendMessage({ message: 'hello' }, 'prompt-id-1');
65
- expect(mockModelsModule.generateContent).toHaveBeenCalledWith({
66
- model: 'gemini-pro',
67
- contents: [{ role: 'user', parts: [{ text: 'hello' }] }],
68
- config: {},
69
- }, 'prompt-id-1');
139
+ describe('constructor', () => {
140
+ it('should initialize lastPromptTokenCount based on history size', () => {
141
+ const history = [
142
+ { role: 'user', parts: [{ text: 'Hello' }] },
143
+ { role: 'model', parts: [{ text: 'Hi there' }] },
144
+ ];
145
+ const chatWithHistory = new GeminiChat(mockConfig, '', [], history);
146
+ const estimatedTokens = Math.ceil(JSON.stringify(history).length / 4);
147
+ expect(chatWithHistory.getLastPromptTokenCount()).toBe(estimatedTokens);
148
+ });
149
+ it('should initialize lastPromptTokenCount for empty history', () => {
150
+ const chatEmpty = new GeminiChat(mockConfig);
151
+ expect(chatEmpty.getLastPromptTokenCount()).toBe(Math.ceil(JSON.stringify([]).length / 4));
70
152
  });
71
153
  });
72
154
  describe('sendMessageStream', () => {
73
- it('should call generateContentStream with the correct parameters', async () => {
74
- const response = (async function* () {
155
+ it('should succeed if a tool call is followed by an empty part', async () => {
156
+ // 1. Mock a stream that contains a tool call, then an invalid (empty) part.
157
+ const streamWithToolCall = (async function* () {
75
158
  yield {
76
159
  candidates: [
77
160
  {
78
161
  content: {
79
- parts: [{ text: 'response' }],
80
162
  role: 'model',
163
+ parts: [{ functionCall: { name: 'test_tool', args: {} } }],
164
+ },
165
+ },
166
+ ],
167
+ };
168
+ // This second chunk is invalid according to isValidResponse
169
+ yield {
170
+ candidates: [
171
+ {
172
+ content: {
173
+ role: 'model',
174
+ parts: [{ text: '' }],
81
175
  },
82
- finishReason: 'STOP',
83
- index: 0,
84
- safetyRatings: [],
85
176
  },
86
177
  ],
87
- text: () => 'response',
88
178
  };
89
179
  })();
90
- vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(response);
91
- await chat.sendMessageStream({ message: 'hello' }, 'prompt-id-1');
92
- expect(mockModelsModule.generateContentStream).toHaveBeenCalledWith({
93
- model: 'gemini-pro',
94
- contents: [{ role: 'user', parts: [{ text: 'hello' }] }],
95
- config: {},
96
- }, 'prompt-id-1');
97
- });
98
- });
99
- describe('recordHistory', () => {
100
- const userInput = {
101
- role: 'user',
102
- parts: [{ text: 'User input' }],
103
- };
104
- it('should add user input and a single model output to history', () => {
105
- const modelOutput = [
106
- { role: 'model', parts: [{ text: 'Model output' }] },
107
- ];
108
- // @ts-expect-error Accessing private method for testing purposes
109
- chat.recordHistory(userInput, modelOutput);
180
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(streamWithToolCall);
181
+ // 2. Action & Assert: The stream processing should complete without throwing an error
182
+ // because the presence of a tool call makes the empty final chunk acceptable.
183
+ const stream = await chat.sendMessageStream({ model: 'test-model' }, 'test message', 'prompt-id-tool-call-empty-end', new AbortController().signal);
184
+ await expect((async () => {
185
+ for await (const _ of stream) {
186
+ /* consume stream */
187
+ }
188
+ })()).resolves.not.toThrow();
189
+ // 3. Verify history was recorded correctly
110
190
  const history = chat.getHistory();
111
- expect(history).toEqual([userInput, modelOutput[0]]);
191
+ expect(history.length).toBe(2); // user turn + model turn
192
+ const modelTurn = history[1];
193
+ expect(modelTurn?.parts?.length).toBe(1); // The empty part is discarded
194
+ expect(modelTurn?.parts[0].functionCall).toBeDefined();
112
195
  });
113
- it('should consolidate adjacent model outputs', () => {
114
- const modelOutputParts = [
115
- { role: 'model', parts: [{ text: 'Model part 1' }] },
116
- { role: 'model', parts: [{ text: 'Model part 2' }] },
117
- ];
118
- // @ts-expect-error Accessing private method for testing purposes
119
- chat.recordHistory(userInput, modelOutputParts);
120
- const history = chat.getHistory();
121
- expect(history.length).toBe(2);
122
- expect(history[0]).toEqual(userInput);
123
- expect(history[1].role).toBe('model');
124
- expect(history[1].parts).toEqual([{ text: 'Model part 1Model part 2' }]);
125
- });
126
- it('should handle a mix of user and model roles in outputContents (though unusual)', () => {
127
- const mixedOutput = [
128
- { role: 'model', parts: [{ text: 'Model 1' }] },
129
- { role: 'user', parts: [{ text: 'Unexpected User' }] }, // This should be pushed as is
130
- { role: 'model', parts: [{ text: 'Model 2' }] },
131
- ];
132
- // @ts-expect-error Accessing private method for testing purposes
133
- chat.recordHistory(userInput, mixedOutput);
134
- const history = chat.getHistory();
135
- expect(history.length).toBe(4); // user, model1, user_unexpected, model2
136
- expect(history[0]).toEqual(userInput);
137
- expect(history[1]).toEqual(mixedOutput[0]);
138
- expect(history[2]).toEqual(mixedOutput[1]);
139
- expect(history[3]).toEqual(mixedOutput[2]);
140
- });
141
- it('should consolidate multiple adjacent model outputs correctly', () => {
142
- const modelOutputParts = [
143
- { role: 'model', parts: [{ text: 'M1' }] },
144
- { role: 'model', parts: [{ text: 'M2' }] },
145
- { role: 'model', parts: [{ text: 'M3' }] },
146
- ];
147
- // @ts-expect-error Accessing private method for testing purposes
148
- chat.recordHistory(userInput, modelOutputParts);
149
- const history = chat.getHistory();
150
- expect(history.length).toBe(2);
151
- expect(history[1].parts).toEqual([{ text: 'M1M2M3' }]);
196
+ it('should fail if the stream ends with an empty part and has no finishReason', async () => {
197
+ // 1. Mock a stream that ends with an invalid part and has no finish reason.
198
+ const streamWithNoFinish = (async function* () {
199
+ yield {
200
+ candidates: [
201
+ {
202
+ content: {
203
+ role: 'model',
204
+ parts: [{ text: 'Initial content...' }],
205
+ },
206
+ },
207
+ ],
208
+ };
209
+ // This second chunk is invalid and has no finishReason, so it should fail.
210
+ yield {
211
+ candidates: [
212
+ {
213
+ content: {
214
+ role: 'model',
215
+ parts: [{ text: '' }],
216
+ },
217
+ },
218
+ ],
219
+ };
220
+ })();
221
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(streamWithNoFinish);
222
+ // 2. Action & Assert: The stream should fail because there's no finish reason.
223
+ const stream = await chat.sendMessageStream({ model: 'gemini-2.0-flash' }, 'test message', 'prompt-id-no-finish-empty-end', new AbortController().signal);
224
+ await expect((async () => {
225
+ for await (const _ of stream) {
226
+ /* consume stream */
227
+ }
228
+ })()).rejects.toThrow(InvalidStreamError);
152
229
  });
153
- it('should not consolidate if roles are different between model outputs', () => {
154
- const modelOutputParts = [
155
- { role: 'model', parts: [{ text: 'M1' }] },
156
- { role: 'user', parts: [{ text: 'Interjecting User' }] },
157
- { role: 'model', parts: [{ text: 'M2' }] },
158
- ];
159
- // @ts-expect-error Accessing private method for testing purposes
160
- chat.recordHistory(userInput, modelOutputParts);
230
+ it('should succeed if the stream ends with an invalid part but has a finishReason and contained a valid part', async () => {
231
+ // 1. Mock a stream that sends a valid chunk, then an invalid one, but has a finish reason.
232
+ const streamWithInvalidEnd = (async function* () {
233
+ yield {
234
+ candidates: [
235
+ {
236
+ content: {
237
+ role: 'model',
238
+ parts: [{ text: 'Initial valid content...' }],
239
+ },
240
+ },
241
+ ],
242
+ };
243
+ // This second chunk is invalid, but the response has a finishReason.
244
+ yield {
245
+ candidates: [
246
+ {
247
+ content: {
248
+ role: 'model',
249
+ parts: [{ text: '' }], // Invalid part
250
+ },
251
+ finishReason: 'STOP',
252
+ },
253
+ ],
254
+ };
255
+ })();
256
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(streamWithInvalidEnd);
257
+ // 2. Action & Assert: The stream should complete without throwing an error.
258
+ const stream = await chat.sendMessageStream({ model: 'test-model' }, 'test message', 'prompt-id-valid-then-invalid-end', new AbortController().signal);
259
+ await expect((async () => {
260
+ for await (const _ of stream) {
261
+ /* consume stream */
262
+ }
263
+ })()).resolves.not.toThrow();
264
+ // 3. Verify history was recorded correctly with only the valid part.
161
265
  const history = chat.getHistory();
162
- expect(history.length).toBe(4); // user, M1, Interjecting User, M2
163
- expect(history[1].parts).toEqual([{ text: 'M1' }]);
164
- expect(history[3].parts).toEqual([{ text: 'M2' }]);
165
- });
166
- it('should merge with last history entry if it is also a model output', () => {
167
- // @ts-expect-error Accessing private property for test setup
168
- chat.history = [
169
- userInput,
170
- { role: 'model', parts: [{ text: 'Initial Model Output' }] },
171
- ]; // Prime the history
172
- const newModelOutput = [
173
- { role: 'model', parts: [{ text: 'New Model Part 1' }] },
174
- { role: 'model', parts: [{ text: 'New Model Part 2' }] },
175
- ];
176
- // @ts-expect-error Accessing private method for testing purposes
177
- chat.recordHistory(userInput, newModelOutput); // userInput here is for the *next* turn, but history is already primed
178
- // Reset and set up a more realistic scenario for merging with existing history
179
- chat = new GeminiChat(mockConfig, mockModelsModule, config, []);
180
- const firstUserInput = {
181
- role: 'user',
182
- parts: [{ text: 'First user input' }],
183
- };
184
- const firstModelOutput = [
185
- { role: 'model', parts: [{ text: 'First model response' }] },
186
- ];
187
- // @ts-expect-error Accessing private method for testing purposes
188
- chat.recordHistory(firstUserInput, firstModelOutput);
189
- const secondUserInput = {
190
- role: 'user',
191
- parts: [{ text: 'Second user input' }],
192
- };
193
- const secondModelOutput = [
194
- { role: 'model', parts: [{ text: 'Second model response part 1' }] },
195
- { role: 'model', parts: [{ text: 'Second model response part 2' }] },
196
- ];
197
- // @ts-expect-error Accessing private method for testing purposes
198
- chat.recordHistory(secondUserInput, secondModelOutput);
199
- const finalHistory = chat.getHistory();
200
- expect(finalHistory.length).toBe(4); // user1, model1, user2, model2(consolidated)
201
- expect(finalHistory[0]).toEqual(firstUserInput);
202
- expect(finalHistory[1]).toEqual(firstModelOutput[0]);
203
- expect(finalHistory[2]).toEqual(secondUserInput);
204
- expect(finalHistory[3].role).toBe('model');
205
- expect(finalHistory[3].parts).toEqual([
206
- { text: 'Second model response part 1Second model response part 2' },
207
- ]);
266
+ expect(history.length).toBe(2); // user turn + model turn
267
+ const modelTurn = history[1];
268
+ expect(modelTurn?.parts?.length).toBe(1);
269
+ expect(modelTurn?.parts[0].text).toBe('Initial valid content...');
208
270
  });
209
- it('should correctly merge consolidated new output with existing model history', () => {
210
- // Setup: history ends with a model turn
211
- const initialUser = {
212
- role: 'user',
213
- parts: [{ text: 'Initial user query' }],
214
- };
215
- const initialModel = {
216
- role: 'model',
217
- parts: [{ text: 'Initial model answer.' }],
218
- };
219
- chat = new GeminiChat(mockConfig, mockModelsModule, config, [
220
- initialUser,
221
- initialModel,
222
- ]);
223
- // New interaction
224
- const currentUserInput = {
225
- role: 'user',
226
- parts: [{ text: 'Follow-up question' }],
227
- };
228
- const newModelParts = [
229
- { role: 'model', parts: [{ text: 'Part A of new answer.' }] },
230
- { role: 'model', parts: [{ text: 'Part B of new answer.' }] },
231
- ];
232
- // @ts-expect-error Accessing private method for testing purposes
233
- chat.recordHistory(currentUserInput, newModelParts);
271
+ it('should consolidate subsequent text chunks after receiving an empty text chunk', async () => {
272
+ // 1. Mock the API to return a stream where one chunk is just an empty text part.
273
+ const multiChunkStream = (async function* () {
274
+ yield {
275
+ candidates: [
276
+ { content: { role: 'model', parts: [{ text: 'Hello' }] } },
277
+ ],
278
+ };
279
+ // FIX: The original test used { text: '' }, which is invalid.
280
+ // A chunk can be empty but still valid. This chunk is now removed
281
+ // as the important part is consolidating what comes after.
282
+ yield {
283
+ candidates: [
284
+ {
285
+ content: { role: 'model', parts: [{ text: ' World!' }] },
286
+ finishReason: 'STOP',
287
+ },
288
+ ],
289
+ };
290
+ })();
291
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(multiChunkStream);
292
+ // 2. Action: Send a message and consume the stream.
293
+ const stream = await chat.sendMessageStream({ model: 'test-model' }, 'test message', 'prompt-id-empty-chunk-consolidation', new AbortController().signal);
294
+ for await (const _ of stream) {
295
+ // Consume the stream
296
+ }
297
+ // 3. Assert: Check that the final history was correctly consolidated.
234
298
  const history = chat.getHistory();
235
- // Expected: initialUser, initialModel, currentUserInput, consolidatedNewModelParts
236
- expect(history.length).toBe(4);
237
- expect(history[0]).toEqual(initialUser);
238
- expect(history[1]).toEqual(initialModel);
239
- expect(history[2]).toEqual(currentUserInput);
240
- expect(history[3].role).toBe('model');
241
- expect(history[3].parts).toEqual([
242
- { text: 'Part A of new answer.Part B of new answer.' },
243
- ]);
299
+ expect(history.length).toBe(2);
300
+ const modelTurn = history[1];
301
+ expect(modelTurn?.parts?.length).toBe(1);
302
+ expect(modelTurn?.parts[0].text).toBe('Hello World!');
244
303
  });
245
- it('should handle empty modelOutput array', () => {
246
- // @ts-expect-error Accessing private method for testing purposes
247
- chat.recordHistory(userInput, []);
304
+ it('should consolidate adjacent text parts that arrive in separate stream chunks', async () => {
305
+ // 1. Mock the API to return a stream of multiple, adjacent text chunks.
306
+ const multiChunkStream = (async function* () {
307
+ yield {
308
+ candidates: [
309
+ { content: { role: 'model', parts: [{ text: 'This is the ' }] } },
310
+ ],
311
+ };
312
+ yield {
313
+ candidates: [
314
+ { content: { role: 'model', parts: [{ text: 'first part.' }] } },
315
+ ],
316
+ };
317
+ // This function call should break the consolidation.
318
+ yield {
319
+ candidates: [
320
+ {
321
+ content: {
322
+ role: 'model',
323
+ parts: [{ functionCall: { name: 'do_stuff', args: {} } }],
324
+ },
325
+ },
326
+ ],
327
+ };
328
+ yield {
329
+ candidates: [
330
+ {
331
+ content: {
332
+ role: 'model',
333
+ parts: [{ text: 'This is the second part.' }],
334
+ },
335
+ },
336
+ ],
337
+ };
338
+ })();
339
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(multiChunkStream);
340
+ // 2. Action: Send a message and consume the stream.
341
+ const stream = await chat.sendMessageStream({ model: 'test-model' }, 'test message', 'prompt-id-multi-chunk', new AbortController().signal);
342
+ for await (const _ of stream) {
343
+ // Consume the stream to trigger history recording.
344
+ }
345
+ // 3. Assert: Check that the final history was correctly consolidated.
248
346
  const history = chat.getHistory();
249
- // If modelOutput is empty, it might push a default empty model part depending on isFunctionResponse
250
- // Assuming isFunctionResponse(userInput) is false for this simple text input
347
+ // The history should contain the user's turn and ONE consolidated model turn.
251
348
  expect(history.length).toBe(2);
252
- expect(history[0]).toEqual(userInput);
253
- expect(history[1].role).toBe('model');
254
- expect(history[1].parts).toEqual([]);
255
- });
256
- it('should handle aggregating modelOutput', () => {
257
- const modelOutputUndefinedParts = [
258
- { role: 'model', parts: [{ text: 'First model part' }] },
259
- { role: 'model', parts: [{ text: 'Second model part' }] },
260
- { role: 'model', parts: undefined }, // Test undefined parts
261
- { role: 'model', parts: [{ text: 'Third model part' }] },
262
- { role: 'model', parts: [] }, // Test empty parts array
263
- ];
264
- // @ts-expect-error Accessing private method for testing purposes
265
- chat.recordHistory(userInput, modelOutputUndefinedParts);
266
- const history = chat.getHistory();
267
- expect(history.length).toBe(5);
268
- expect(history[0]).toEqual(userInput);
269
- expect(history[1].role).toBe('model');
270
- expect(history[1].parts).toEqual([
271
- { text: 'First model partSecond model part' },
272
- ]);
273
- expect(history[2].role).toBe('model');
274
- expect(history[2].parts).toBeUndefined();
275
- expect(history[3].role).toBe('model');
276
- expect(history[3].parts).toEqual([{ text: 'Third model part' }]);
277
- expect(history[4].role).toBe('model');
278
- expect(history[4].parts).toEqual([]);
279
- });
280
- it('should handle modelOutput with parts being undefined or empty (if they pass initial every check)', () => {
281
- const modelOutputUndefinedParts = [
282
- { role: 'model', parts: [{ text: 'Text part' }] },
283
- { role: 'model', parts: undefined }, // Test undefined parts
284
- { role: 'model', parts: [] }, // Test empty parts array
285
- ];
286
- // @ts-expect-error Accessing private method for testing purposes
287
- chat.recordHistory(userInput, modelOutputUndefinedParts);
288
- const history = chat.getHistory();
289
- expect(history.length).toBe(4); // userInput, model1 (text), model2 (undefined parts), model3 (empty parts)
290
- expect(history[0]).toEqual(userInput);
291
- expect(history[1].role).toBe('model');
292
- expect(history[1].parts).toEqual([{ text: 'Text part' }]);
293
- expect(history[2].role).toBe('model');
294
- expect(history[2].parts).toBeUndefined();
295
- expect(history[3].role).toBe('model');
296
- expect(history[3].parts).toEqual([]);
297
- });
298
- it('should correctly handle automaticFunctionCallingHistory', () => {
299
- const afcHistory = [
300
- { role: 'user', parts: [{ text: 'AFC User' }] },
301
- { role: 'model', parts: [{ text: 'AFC Model' }] },
302
- ];
303
- const modelOutput = [
304
- { role: 'model', parts: [{ text: 'Regular Model Output' }] },
305
- ];
306
- // @ts-expect-error Accessing private method for testing purposes
307
- chat.recordHistory(userInput, modelOutput, afcHistory);
308
- const history = chat.getHistory();
309
- expect(history.length).toBe(3);
310
- expect(history[0]).toEqual(afcHistory[0]);
311
- expect(history[1]).toEqual(afcHistory[1]);
312
- expect(history[2]).toEqual(modelOutput[0]);
313
- });
314
- it('should add userInput if AFC history is present but empty', () => {
315
- const modelOutput = [
316
- { role: 'model', parts: [{ text: 'Model Output' }] },
317
- ];
318
- // @ts-expect-error Accessing private method for testing purposes
319
- chat.recordHistory(userInput, modelOutput, []); // Empty AFC history
349
+ const modelTurn = history[1];
350
+ expect(modelTurn.role).toBe('model');
351
+ // The model turn should have 3 distinct parts: the merged text, the function call, and the final text.
352
+ expect(modelTurn?.parts?.length).toBe(3);
353
+ expect(modelTurn?.parts[0].text).toBe('This is the first part.');
354
+ expect(modelTurn.parts[1].functionCall).toBeDefined();
355
+ expect(modelTurn.parts[2].text).toBe('This is the second part.');
356
+ });
357
+ it('should preserve text parts that stream in the same chunk as a thought', async () => {
358
+ // 1. Mock the API to return a single chunk containing both a thought and visible text.
359
+ const mixedContentStream = (async function* () {
360
+ yield {
361
+ candidates: [
362
+ {
363
+ content: {
364
+ role: 'model',
365
+ parts: [
366
+ { thought: 'This is a thought.' },
367
+ { text: 'This is the visible text that should not be lost.' },
368
+ ],
369
+ },
370
+ finishReason: 'STOP',
371
+ },
372
+ ],
373
+ };
374
+ })();
375
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(mixedContentStream);
376
+ // 2. Action: Send a message and fully consume the stream to trigger history recording.
377
+ const stream = await chat.sendMessageStream({ model: 'test-model' }, 'test message', 'prompt-id-mixed-chunk', new AbortController().signal);
378
+ for await (const _ of stream) {
379
+ // This loop consumes the stream.
380
+ }
381
+ // 3. Assert: Check the final state of the history.
320
382
  const history = chat.getHistory();
383
+ // The history should contain two turns: the user's message and the model's response.
321
384
  expect(history.length).toBe(2);
322
- expect(history[0]).toEqual(userInput);
323
- expect(history[1]).toEqual(modelOutput[0]);
385
+ const modelTurn = history[1];
386
+ expect(modelTurn.role).toBe('model');
387
+ // CRUCIAL ASSERTION:
388
+ // The buggy code would fail here, resulting in parts.length being 0.
389
+ // The corrected code will pass, preserving the single visible text part.
390
+ expect(modelTurn?.parts?.length).toBe(1);
391
+ expect(modelTurn?.parts[0].text).toBe('This is the visible text that should not be lost.');
324
392
  });
325
- it('should skip "thought" content from modelOutput', () => {
326
- const modelOutputWithThought = [
327
- { role: 'model', parts: [{ thought: true }, { text: 'Visible text' }] },
328
- { role: 'model', parts: [{ text: 'Another visible text' }] },
329
- ];
330
- // @ts-expect-error Accessing private method for testing purposes
331
- chat.recordHistory(userInput, modelOutputWithThought);
332
- const history = chat.getHistory();
333
- expect(history.length).toBe(2); // User input + consolidated model output
334
- expect(history[0]).toEqual(userInput);
335
- expect(history[1].role).toBe('model');
336
- // The 'thought' part is skipped, 'Another visible text' becomes the first part.
337
- expect(history[1].parts).toEqual([{ text: 'Another visible text' }]);
338
- });
339
- it('should skip "thought" content even if it is the only content', () => {
340
- const modelOutputOnlyThought = [
341
- { role: 'model', parts: [{ thought: true }] },
342
- ];
343
- // @ts-expect-error Accessing private method for testing purposes
344
- chat.recordHistory(userInput, modelOutputOnlyThought);
345
- const history = chat.getHistory();
346
- expect(history.length).toBe(1); // User input + default empty model part
347
- expect(history[0]).toEqual(userInput);
393
+ it('should use maxAttempts=1 for retryWithBackoff when in Preview Model Fallback Mode', async () => {
394
+ vi.mocked(mockConfig.isPreviewModelFallbackMode).mockReturnValue(true);
395
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue((async function* () {
396
+ yield {
397
+ candidates: [
398
+ {
399
+ content: { parts: [{ text: 'Success' }] },
400
+ finishReason: 'STOP',
401
+ },
402
+ ],
403
+ };
404
+ })());
405
+ const stream = await chat.sendMessageStream({ model: PREVIEW_GEMINI_MODEL }, 'test', 'prompt-id-fast-retry', new AbortController().signal);
406
+ for await (const _ of stream) {
407
+ // consume stream
408
+ }
409
+ expect(mockRetryWithBackoff).toHaveBeenCalledWith(expect.any(Function), expect.objectContaining({
410
+ maxAttempts: 1,
411
+ }));
412
+ });
413
+ it('should NOT use maxAttempts=1 for other models even in Preview Model Fallback Mode', async () => {
414
+ vi.mocked(mockConfig.isPreviewModelFallbackMode).mockReturnValue(true);
415
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue((async function* () {
416
+ yield {
417
+ candidates: [
418
+ {
419
+ content: { parts: [{ text: 'Success' }] },
420
+ finishReason: 'STOP',
421
+ },
422
+ ],
423
+ };
424
+ })());
425
+ const stream = await chat.sendMessageStream({ model: DEFAULT_GEMINI_FLASH_MODEL }, 'test', 'prompt-id-normal-retry', new AbortController().signal);
426
+ for await (const _ of stream) {
427
+ // consume stream
428
+ }
429
+ expect(mockRetryWithBackoff).toHaveBeenCalledWith(expect.any(Function), expect.objectContaining({
430
+ maxAttempts: undefined, // Should use default
431
+ }));
432
+ });
433
+ it('should pass DEFAULT_GEMINI_MODEL to handleFallback when Preview Model is bypassed (downgraded)', async () => {
434
+ // ARRANGE
435
+ vi.mocked(mockConfig.isPreviewModelBypassMode).mockReturnValue(true);
436
+ // Mock retryWithBackoff to simulate catching the error and calling onPersistent429
437
+ vi.mocked(retryWithBackoff).mockImplementation(async (apiCall, options) => {
438
+ const onPersistent429 = options?.onPersistent429;
439
+ try {
440
+ await apiCall();
441
+ }
442
+ catch (error) {
443
+ if (onPersistent429) {
444
+ await onPersistent429(AuthType.LOGIN_WITH_GOOGLE, error);
445
+ }
446
+ throw error;
447
+ }
448
+ });
449
+ // We need the API call to fail so retryWithBackoff calls the callback.
450
+ vi.mocked(mockContentGenerator.generateContentStream).mockRejectedValue(new TerminalQuotaError('Simulated Quota Error', {
451
+ code: 429,
452
+ message: 'Simulated Quota Error',
453
+ details: [],
454
+ }));
455
+ // ACT
456
+ const consumeStream = async () => {
457
+ const stream = await chat.sendMessageStream({ model: PREVIEW_GEMINI_MODEL }, 'test', 'prompt-id-bypass', new AbortController().signal);
458
+ // Consume the stream to trigger execution
459
+ for await (const _ of stream) {
460
+ // do nothing
461
+ }
462
+ };
463
+ await expect(consumeStream()).rejects.toThrow('Simulated Quota Error');
464
+ expect(retryWithBackoff).toHaveBeenCalled();
465
+ // ASSERT
466
+ // handleFallback is called via onPersistent429Callback
467
+ // We verify it was called with DEFAULT_GEMINI_MODEL
468
+ expect(mockHandleFallback).toHaveBeenCalledWith(expect.anything(), DEFAULT_GEMINI_MODEL, // This is the key assertion
469
+ expect.anything(), expect.anything());
348
470
  });
349
- it('should correctly consolidate text parts when a thought part is in between', () => {
350
- const modelOutputMixed = [
351
- { role: 'model', parts: [{ text: 'Part 1.' }] },
471
+ it('should throw an error when a tool call is followed by an empty stream response', async () => {
472
+ // 1. Setup: A history where the model has just made a function call.
473
+ const initialHistory = [
474
+ {
475
+ role: 'user',
476
+ parts: [{ text: 'Find a good Italian restaurant for me.' }],
477
+ },
352
478
  {
353
479
  role: 'model',
354
- parts: [{ thought: true }, { text: 'Should be skipped' }],
480
+ parts: [
481
+ {
482
+ functionCall: {
483
+ name: 'find_restaurant',
484
+ args: { cuisine: 'Italian' },
485
+ },
486
+ },
487
+ ],
355
488
  },
356
- { role: 'model', parts: [{ text: 'Part 2.' }] },
357
- ];
358
- // @ts-expect-error Accessing private method for testing purposes
359
- chat.recordHistory(userInput, modelOutputMixed);
360
- const history = chat.getHistory();
361
- expect(history.length).toBe(2);
362
- expect(history[0]).toEqual(userInput);
363
- expect(history[1].role).toBe('model');
364
- expect(history[1].parts).toEqual([{ text: 'Part 1.Part 2.' }]);
365
- });
366
- it('should handle multiple thought parts correctly', () => {
367
- const modelOutputMultipleThoughts = [
368
- { role: 'model', parts: [{ thought: true }] },
369
- { role: 'model', parts: [{ text: 'Visible 1' }] },
370
- { role: 'model', parts: [{ thought: true }] },
371
- { role: 'model', parts: [{ text: 'Visible 2' }] },
372
489
  ];
373
- // @ts-expect-error Accessing private method for testing purposes
374
- chat.recordHistory(userInput, modelOutputMultipleThoughts);
375
- const history = chat.getHistory();
376
- expect(history.length).toBe(2);
377
- expect(history[0]).toEqual(userInput);
378
- expect(history[1].role).toBe('model');
379
- expect(history[1].parts).toEqual([{ text: 'Visible 1Visible 2' }]);
380
- });
381
- it('should handle thought part at the end of outputContents', () => {
382
- const modelOutputThoughtAtEnd = [
383
- { role: 'model', parts: [{ text: 'Visible text' }] },
384
- { role: 'model', parts: [{ thought: true }] },
385
- ];
386
- // @ts-expect-error Accessing private method for testing purposes
387
- chat.recordHistory(userInput, modelOutputThoughtAtEnd);
388
- const history = chat.getHistory();
389
- expect(history.length).toBe(2);
390
- expect(history[0]).toEqual(userInput);
391
- expect(history[1].role).toBe('model');
392
- expect(history[1].parts).toEqual([{ text: 'Visible text' }]);
490
+ chat.setHistory(initialHistory);
491
+ // 2. Mock the API to return an empty/thought-only stream.
492
+ const emptyStreamResponse = (async function* () {
493
+ yield {
494
+ candidates: [
495
+ {
496
+ content: { role: 'model', parts: [{ thought: true }] },
497
+ finishReason: 'STOP',
498
+ },
499
+ ],
500
+ };
501
+ })();
502
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(emptyStreamResponse);
503
+ // 3. Action: Send the function response back to the model and consume the stream.
504
+ const stream = await chat.sendMessageStream({ model: 'gemini-2.0-flash' }, {
505
+ functionResponse: {
506
+ name: 'find_restaurant',
507
+ response: { name: 'Vesuvio' },
508
+ },
509
+ }, 'prompt-id-stream-1', new AbortController().signal);
510
+ // 4. Assert: The stream processing should throw an InvalidStreamError.
511
+ await expect((async () => {
512
+ for await (const _ of stream) {
513
+ // This loop consumes the stream to trigger the internal logic.
514
+ }
515
+ })()).rejects.toThrow(InvalidStreamError);
393
516
  });
394
- });
395
- describe('addHistory', () => {
396
- it('should add a new content item to the history', () => {
397
- const newContent = {
398
- role: 'user',
399
- parts: [{ text: 'A new message' }],
400
- };
401
- chat.addHistory(newContent);
402
- const history = chat.getHistory();
403
- expect(history.length).toBe(1);
404
- expect(history[0]).toEqual(newContent);
517
+ it('should succeed when there is a tool call without finish reason', async () => {
518
+ // Setup: Stream with tool call but no finish reason
519
+ const streamWithToolCall = (async function* () {
520
+ yield {
521
+ candidates: [
522
+ {
523
+ content: {
524
+ role: 'model',
525
+ parts: [
526
+ {
527
+ functionCall: {
528
+ name: 'test_function',
529
+ args: { param: 'value' },
530
+ },
531
+ },
532
+ ],
533
+ },
534
+ // No finishReason
535
+ },
536
+ ],
537
+ };
538
+ })();
539
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(streamWithToolCall);
540
+ const stream = await chat.sendMessageStream({ model: 'test-model' }, 'test message', 'prompt-id-1', new AbortController().signal);
541
+ // Should not throw an error
542
+ await expect((async () => {
543
+ for await (const _ of stream) {
544
+ // consume stream
545
+ }
546
+ })()).resolves.not.toThrow();
405
547
  });
406
- it('should add multiple items correctly', () => {
407
- const content1 = {
408
- role: 'user',
409
- parts: [{ text: 'Message 1' }],
410
- };
411
- const content2 = {
412
- role: 'model',
413
- parts: [{ text: 'Message 2' }],
414
- };
415
- chat.addHistory(content1);
416
- chat.addHistory(content2);
417
- const history = chat.getHistory();
418
- expect(history.length).toBe(2);
419
- expect(history[0]).toEqual(content1);
420
- expect(history[1]).toEqual(content2);
548
+ it('should throw InvalidStreamError when no tool call and no finish reason', async () => {
549
+ // Setup: Stream with text but no finish reason and no tool call
550
+ const streamWithoutFinishReason = (async function* () {
551
+ yield {
552
+ candidates: [
553
+ {
554
+ content: {
555
+ role: 'model',
556
+ parts: [{ text: 'some response' }],
557
+ },
558
+ // No finishReason
559
+ },
560
+ ],
561
+ };
562
+ })();
563
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(streamWithoutFinishReason);
564
+ const stream = await chat.sendMessageStream({ model: 'gemini-2.0-flash' }, 'test message', 'prompt-id-1', new AbortController().signal);
565
+ await expect((async () => {
566
+ for await (const _ of stream) {
567
+ // consume stream
568
+ }
569
+ })()).rejects.toThrow(InvalidStreamError);
570
+ });
571
+ it('should throw InvalidStreamError when no tool call and empty response text', async () => {
572
+ // Setup: Stream with finish reason but empty response (only thoughts)
573
+ const streamWithEmptyResponse = (async function* () {
574
+ yield {
575
+ candidates: [
576
+ {
577
+ content: {
578
+ role: 'model',
579
+ parts: [{ thought: 'thinking...' }],
580
+ },
581
+ finishReason: 'STOP',
582
+ },
583
+ ],
584
+ };
585
+ })();
586
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(streamWithEmptyResponse);
587
+ const stream = await chat.sendMessageStream({ model: 'gemini-2.0-flash' }, 'test message', 'prompt-id-1', new AbortController().signal);
588
+ await expect((async () => {
589
+ for await (const _ of stream) {
590
+ // consume stream
591
+ }
592
+ })()).rejects.toThrow(InvalidStreamError);
593
+ });
594
+ it('should succeed when there is finish reason and response text', async () => {
595
+ // Setup: Stream with both finish reason and text content
596
+ const validStream = (async function* () {
597
+ yield {
598
+ candidates: [
599
+ {
600
+ content: {
601
+ role: 'model',
602
+ parts: [{ text: 'valid response' }],
603
+ },
604
+ finishReason: 'STOP',
605
+ },
606
+ ],
607
+ };
608
+ })();
609
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(validStream);
610
+ const stream = await chat.sendMessageStream({ model: 'test-model' }, 'test message', 'prompt-id-1', new AbortController().signal);
611
+ // Should not throw an error
612
+ await expect((async () => {
613
+ for await (const _ of stream) {
614
+ // consume stream
615
+ }
616
+ })()).resolves.not.toThrow();
617
+ });
618
+ it('should throw InvalidStreamError when finishReason is MALFORMED_FUNCTION_CALL', async () => {
619
+ // Setup: Stream with MALFORMED_FUNCTION_CALL finish reason and empty response
620
+ const streamWithMalformedFunctionCall = (async function* () {
621
+ yield {
622
+ candidates: [
623
+ {
624
+ content: {
625
+ role: 'model',
626
+ parts: [], // Empty parts
627
+ },
628
+ finishReason: 'MALFORMED_FUNCTION_CALL',
629
+ },
630
+ ],
631
+ };
632
+ })();
633
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(streamWithMalformedFunctionCall);
634
+ const stream = await chat.sendMessageStream({ model: 'gemini-2.5-pro' }, 'test', 'prompt-id-malformed', new AbortController().signal);
635
+ // Should throw an error
636
+ await expect((async () => {
637
+ for await (const _ of stream) {
638
+ // consume stream
639
+ }
640
+ })()).rejects.toThrow(InvalidStreamError);
641
+ });
642
+ it('should retry when finishReason is MALFORMED_FUNCTION_CALL', async () => {
643
+ // 1. Mock the API to fail once with MALFORMED_FUNCTION_CALL, then succeed.
644
+ vi.mocked(mockContentGenerator.generateContentStream)
645
+ .mockImplementationOnce(async () => (async function* () {
646
+ yield {
647
+ candidates: [
648
+ {
649
+ content: { parts: [], role: 'model' },
650
+ finishReason: 'MALFORMED_FUNCTION_CALL',
651
+ },
652
+ ],
653
+ };
654
+ })())
655
+ .mockImplementationOnce(async () =>
656
+ // Second attempt succeeds
657
+ (async function* () {
658
+ yield {
659
+ candidates: [
660
+ {
661
+ content: { parts: [{ text: 'Success after retry' }] },
662
+ finishReason: 'STOP',
663
+ },
664
+ ],
665
+ };
666
+ })());
667
+ // 2. Send a message
668
+ const stream = await chat.sendMessageStream({ model: 'gemini-2.5-pro' }, 'test retry', 'prompt-id-retry-malformed', new AbortController().signal);
669
+ const events = [];
670
+ for await (const event of stream) {
671
+ events.push(event);
672
+ }
673
+ // 3. Assertions
674
+ // Should be called twice (initial + retry)
675
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
676
+ // Check for a retry event
677
+ expect(events.some((e) => e.type === StreamEventType.RETRY)).toBe(true);
678
+ // Check for the successful content chunk
679
+ expect(events.some((e) => e.type === StreamEventType.CHUNK &&
680
+ e.value.candidates?.[0]?.content?.parts?.[0]?.text ===
681
+ 'Success after retry')).toBe(true);
682
+ });
683
+ it('should call generateContentStream with the correct parameters', async () => {
684
+ const response = (async function* () {
685
+ yield {
686
+ candidates: [
687
+ {
688
+ content: {
689
+ parts: [{ text: 'response' }],
690
+ role: 'model',
691
+ },
692
+ finishReason: 'STOP',
693
+ index: 0,
694
+ safetyRatings: [],
695
+ },
696
+ ],
697
+ text: () => 'response',
698
+ usageMetadata: {
699
+ promptTokenCount: 42,
700
+ candidatesTokenCount: 15,
701
+ totalTokenCount: 57,
702
+ },
703
+ };
704
+ })();
705
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(response);
706
+ const stream = await chat.sendMessageStream({ model: 'test-model' }, 'hello', 'prompt-id-1', new AbortController().signal);
707
+ for await (const _ of stream) {
708
+ // consume stream
709
+ }
710
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledWith({
711
+ model: 'test-model',
712
+ contents: [
713
+ {
714
+ role: 'user',
715
+ parts: [{ text: 'hello' }],
716
+ },
717
+ ],
718
+ config: {
719
+ systemInstruction: '',
720
+ tools: [],
721
+ temperature: 0,
722
+ thinkingConfig: {
723
+ thinkingBudget: DEFAULT_THINKING_MODE,
724
+ },
725
+ abortSignal: expect.any(AbortSignal),
726
+ },
727
+ }, 'prompt-id-1');
728
+ });
729
+ it('should use thinkingLevel and remove thinkingBudget for gemini-3 models', async () => {
730
+ const response = (async function* () {
731
+ yield {
732
+ candidates: [
733
+ {
734
+ content: { parts: [{ text: 'response' }], role: 'model' },
735
+ finishReason: 'STOP',
736
+ },
737
+ ],
738
+ };
739
+ })();
740
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(response);
741
+ const stream = await chat.sendMessageStream({ model: 'gemini-3-test-only-model-string-for-testing' }, 'hello', 'prompt-id-thinking-level', new AbortController().signal);
742
+ for await (const _ of stream) {
743
+ // consume stream
744
+ }
745
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledWith(expect.objectContaining({
746
+ model: 'gemini-3-test-only-model-string-for-testing',
747
+ config: expect.objectContaining({
748
+ thinkingConfig: {
749
+ thinkingBudget: undefined,
750
+ thinkingLevel: ThinkingLevel.HIGH,
751
+ },
752
+ }),
753
+ }), 'prompt-id-thinking-level');
754
+ });
755
+ it('should use thinkingBudget and remove thinkingLevel for non-gemini-3 models', async () => {
756
+ const response = (async function* () {
757
+ yield {
758
+ candidates: [
759
+ {
760
+ content: { parts: [{ text: 'response' }], role: 'model' },
761
+ finishReason: 'STOP',
762
+ },
763
+ ],
764
+ };
765
+ })();
766
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(response);
767
+ const stream = await chat.sendMessageStream({ model: 'gemini-2.0-flash' }, 'hello', 'prompt-id-thinking-budget', new AbortController().signal);
768
+ for await (const _ of stream) {
769
+ // consume stream
770
+ }
771
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledWith(expect.objectContaining({
772
+ model: 'gemini-2.0-flash',
773
+ config: expect.objectContaining({
774
+ thinkingConfig: {
775
+ thinkingBudget: DEFAULT_THINKING_MODE,
776
+ thinkingLevel: undefined,
777
+ },
778
+ }),
779
+ }), 'prompt-id-thinking-budget');
780
+ });
781
+ });
782
+ describe('addHistory', () => {
783
+ it('should add a new content item to the history', () => {
784
+ const newContent = {
785
+ role: 'user',
786
+ parts: [{ text: 'A new message' }],
787
+ };
788
+ chat.addHistory(newContent);
789
+ const history = chat.getHistory();
790
+ expect(history.length).toBe(1);
791
+ expect(history[0]).toEqual(newContent);
792
+ });
793
+ it('should add multiple items correctly', () => {
794
+ const content1 = {
795
+ role: 'user',
796
+ parts: [{ text: 'Message 1' }],
797
+ };
798
+ const content2 = {
799
+ role: 'model',
800
+ parts: [{ text: 'Message 2' }],
801
+ };
802
+ chat.addHistory(content1);
803
+ chat.addHistory(content2);
804
+ const history = chat.getHistory();
805
+ expect(history.length).toBe(2);
806
+ expect(history[0]).toEqual(content1);
807
+ expect(history[1]).toEqual(content2);
808
+ });
809
+ });
810
+ describe('sendMessageStream with retries', () => {
811
+ it('should not retry on invalid content if model does not start with gemini-2', async () => {
812
+ // Mock the stream to fail.
813
+ vi.mocked(mockContentGenerator.generateContentStream).mockImplementation(async () => (async function* () {
814
+ yield {
815
+ candidates: [{ content: { parts: [{ text: '' }] } }],
816
+ };
817
+ })());
818
+ const stream = await chat.sendMessageStream({ model: 'gemini-1.5-pro' }, 'test', 'prompt-id-no-retry', new AbortController().signal);
819
+ await expect((async () => {
820
+ for await (const _ of stream) {
821
+ // Must loop to trigger the internal logic that throws.
822
+ }
823
+ })()).rejects.toThrow(InvalidStreamError);
824
+ // Should be called only 1 time (no retry)
825
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(1);
826
+ expect(mockLogContentRetry).not.toHaveBeenCalled();
827
+ });
828
+ it('should yield a RETRY event when an invalid stream is encountered', async () => {
829
+ // ARRANGE: Mock the stream to fail once, then succeed.
830
+ vi.mocked(mockContentGenerator.generateContentStream)
831
+ .mockImplementationOnce(async () =>
832
+ // First attempt: An invalid stream with an empty text part.
833
+ (async function* () {
834
+ yield {
835
+ candidates: [{ content: { parts: [{ text: '' }] } }],
836
+ };
837
+ })())
838
+ .mockImplementationOnce(async () =>
839
+ // Second attempt (the retry): A minimal valid stream.
840
+ (async function* () {
841
+ yield {
842
+ candidates: [
843
+ {
844
+ content: { parts: [{ text: 'Success' }] },
845
+ finishReason: 'STOP',
846
+ },
847
+ ],
848
+ };
849
+ })());
850
+ // ACT: Send a message and collect all events from the stream.
851
+ const stream = await chat.sendMessageStream({ model: 'gemini-2.0-flash' }, 'test message', 'prompt-id-yield-retry', new AbortController().signal);
852
+ const events = [];
853
+ for await (const event of stream) {
854
+ events.push(event);
855
+ }
856
+ // ASSERT: Check that a RETRY event was present in the stream's output.
857
+ const retryEvent = events.find((e) => e.type === StreamEventType.RETRY);
858
+ expect(retryEvent).toBeDefined();
859
+ expect(retryEvent?.type).toBe(StreamEventType.RETRY);
860
+ });
861
+ it('should retry on invalid content, succeed, and report metrics', async () => {
862
+ // Use mockImplementationOnce to provide a fresh, promise-wrapped generator for each attempt.
863
+ vi.mocked(mockContentGenerator.generateContentStream)
864
+ .mockImplementationOnce(async () =>
865
+ // First call returns an invalid stream
866
+ (async function* () {
867
+ yield {
868
+ candidates: [{ content: { parts: [{ text: '' }] } }], // Invalid empty text part
869
+ };
870
+ })())
871
+ .mockImplementationOnce(async () =>
872
+ // Second call returns a valid stream
873
+ (async function* () {
874
+ yield {
875
+ candidates: [
876
+ {
877
+ content: { parts: [{ text: 'Successful response' }] },
878
+ finishReason: 'STOP',
879
+ },
880
+ ],
881
+ };
882
+ })());
883
+ const stream = await chat.sendMessageStream({ model: 'gemini-2.0-flash' }, 'test', 'prompt-id-retry-success', new AbortController().signal);
884
+ const chunks = [];
885
+ for await (const chunk of stream) {
886
+ chunks.push(chunk);
887
+ }
888
+ // Assertions
889
+ expect(mockLogContentRetry).toHaveBeenCalledTimes(1);
890
+ expect(mockLogContentRetryFailure).not.toHaveBeenCalled();
891
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
892
+ // Check for a retry event
893
+ expect(chunks.some((c) => c.type === StreamEventType.RETRY)).toBe(true);
894
+ // Check for the successful content chunk
895
+ expect(chunks.some((c) => c.type === StreamEventType.CHUNK &&
896
+ c.value.candidates?.[0]?.content?.parts?.[0]?.text ===
897
+ 'Successful response')).toBe(true);
898
+ // Check that history was recorded correctly once, with no duplicates.
899
+ const history = chat.getHistory();
900
+ expect(history.length).toBe(2);
901
+ expect(history[0]).toEqual({
902
+ role: 'user',
903
+ parts: [{ text: 'test' }],
904
+ });
905
+ expect(history[1]).toEqual({
906
+ role: 'model',
907
+ parts: [{ text: 'Successful response' }],
908
+ });
909
+ // Verify that token counting is not called when usageMetadata is missing
910
+ expect(uiTelemetryService.setLastPromptTokenCount).not.toHaveBeenCalled();
911
+ });
912
+ it('should set temperature to 1 on retry', async () => {
913
+ // Use mockImplementationOnce to provide a fresh, promise-wrapped generator for each attempt.
914
+ vi.mocked(mockContentGenerator.generateContentStream)
915
+ .mockImplementationOnce(async () =>
916
+ // First call returns an invalid stream
917
+ (async function* () {
918
+ yield {
919
+ candidates: [{ content: { parts: [{ text: '' }] } }], // Invalid empty text part
920
+ };
921
+ })())
922
+ .mockImplementationOnce(async () =>
923
+ // Second call returns a valid stream
924
+ (async function* () {
925
+ yield {
926
+ candidates: [
927
+ {
928
+ content: { parts: [{ text: 'Successful response' }] },
929
+ finishReason: 'STOP',
930
+ },
931
+ ],
932
+ };
933
+ })());
934
+ const stream = await chat.sendMessageStream({ model: 'gemini-2.0-flash' }, 'test message', 'prompt-id-retry-temperature', new AbortController().signal);
935
+ for await (const _ of stream) {
936
+ // consume stream
937
+ }
938
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
939
+ // First call should have original temperature
940
+ expect(mockContentGenerator.generateContentStream).toHaveBeenNthCalledWith(1, expect.objectContaining({
941
+ config: expect.objectContaining({
942
+ temperature: 0,
943
+ }),
944
+ }), 'prompt-id-retry-temperature');
945
+ // Second call (retry) should have temperature 1
946
+ expect(mockContentGenerator.generateContentStream).toHaveBeenNthCalledWith(2, expect.objectContaining({
947
+ config: expect.objectContaining({
948
+ temperature: 1,
949
+ }),
950
+ }), 'prompt-id-retry-temperature');
951
+ });
952
+ it('should fail after all retries on persistent invalid content and report metrics', async () => {
953
+ vi.mocked(mockContentGenerator.generateContentStream).mockImplementation(async () => (async function* () {
954
+ yield {
955
+ candidates: [
956
+ {
957
+ content: {
958
+ parts: [{ text: '' }],
959
+ role: 'model',
960
+ },
961
+ },
962
+ ],
963
+ };
964
+ })());
965
+ const stream = await chat.sendMessageStream({ model: 'gemini-2.0-flash' }, 'test', 'prompt-id-retry-fail', new AbortController().signal);
966
+ await expect(async () => {
967
+ for await (const _ of stream) {
968
+ // Must loop to trigger the internal logic that throws.
969
+ }
970
+ }).rejects.toThrow(InvalidStreamError);
971
+ // Should be called 2 times (initial + 1 retry)
972
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
973
+ expect(mockLogContentRetry).toHaveBeenCalledTimes(1);
974
+ expect(mockLogContentRetryFailure).toHaveBeenCalledTimes(1);
975
+ // History should still contain the user message.
976
+ const history = chat.getHistory();
977
+ expect(history.length).toBe(1);
978
+ expect(history[0]).toEqual({
979
+ role: 'user',
980
+ parts: [{ text: 'test' }],
981
+ });
982
+ });
983
+ describe('API error retry behavior', () => {
984
+ beforeEach(() => {
985
+ // Use a more direct mock for retry testing
986
+ mockRetryWithBackoff.mockImplementation(async (apiCall) => {
987
+ try {
988
+ return await apiCall();
989
+ }
990
+ catch (error) {
991
+ // Simulate the logic of defaultShouldRetry for ApiError
992
+ let shouldRetry = false;
993
+ if (error instanceof ApiError && error.message) {
994
+ if (error.status === 429 ||
995
+ (error.status >= 500 && error.status < 600)) {
996
+ shouldRetry = true;
997
+ }
998
+ // Explicitly don't retry on these
999
+ if (error.status === 400) {
1000
+ shouldRetry = false;
1001
+ }
1002
+ }
1003
+ if (shouldRetry) {
1004
+ // Try again
1005
+ return await apiCall();
1006
+ }
1007
+ throw error;
1008
+ }
1009
+ });
1010
+ });
1011
+ it('should not retry on 400 Bad Request errors', async () => {
1012
+ const error400 = new ApiError({ message: 'Bad Request', status: 400 });
1013
+ vi.mocked(mockContentGenerator.generateContentStream).mockRejectedValue(error400);
1014
+ const stream = await chat.sendMessageStream({ model: 'gemini-2.0-flash' }, 'test message', 'prompt-id-400', new AbortController().signal);
1015
+ await expect((async () => {
1016
+ for await (const _ of stream) {
1017
+ /* consume stream */
1018
+ }
1019
+ })()).rejects.toThrow(error400);
1020
+ // Should only be called once (no retry)
1021
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(1);
1022
+ });
1023
+ it('should retry on 429 Rate Limit errors', async () => {
1024
+ const error429 = new ApiError({ message: 'Rate Limited', status: 429 });
1025
+ vi.mocked(mockContentGenerator.generateContentStream)
1026
+ .mockRejectedValueOnce(error429)
1027
+ .mockResolvedValueOnce((async function* () {
1028
+ yield {
1029
+ candidates: [
1030
+ {
1031
+ content: { parts: [{ text: 'Success after retry' }] },
1032
+ finishReason: 'STOP',
1033
+ },
1034
+ ],
1035
+ };
1036
+ })());
1037
+ const stream = await chat.sendMessageStream({ model: 'test-model' }, 'test message', 'prompt-id-429-retry', new AbortController().signal);
1038
+ const events = [];
1039
+ for await (const event of stream) {
1040
+ events.push(event);
1041
+ }
1042
+ // Should be called twice (initial + retry)
1043
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
1044
+ // Should have successful content
1045
+ expect(events.some((e) => e.type === StreamEventType.CHUNK &&
1046
+ e.value.candidates?.[0]?.content?.parts?.[0]?.text ===
1047
+ 'Success after retry')).toBe(true);
1048
+ });
1049
+ it('should retry on 5xx server errors', async () => {
1050
+ const error500 = new ApiError({
1051
+ message: 'Internal Server Error 500',
1052
+ status: 500,
1053
+ });
1054
+ vi.mocked(mockContentGenerator.generateContentStream)
1055
+ .mockRejectedValueOnce(error500)
1056
+ .mockResolvedValueOnce((async function* () {
1057
+ yield {
1058
+ candidates: [
1059
+ {
1060
+ content: { parts: [{ text: 'Recovered from 500' }] },
1061
+ finishReason: 'STOP',
1062
+ },
1063
+ ],
1064
+ };
1065
+ })());
1066
+ const stream = await chat.sendMessageStream({ model: 'test-model' }, 'test message', 'prompt-id-500-retry', new AbortController().signal);
1067
+ const events = [];
1068
+ for await (const event of stream) {
1069
+ events.push(event);
1070
+ }
1071
+ // Should be called twice (initial + retry)
1072
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
1073
+ });
1074
+ it('should retry on specific fetch errors when configured', async () => {
1075
+ vi.mocked(mockConfig.getRetryFetchErrors).mockReturnValue(true);
1076
+ const fetchError = new Error('exception TypeError: fetch failed sending request');
1077
+ vi.mocked(mockContentGenerator.generateContentStream)
1078
+ .mockRejectedValueOnce(fetchError)
1079
+ .mockResolvedValueOnce((async function* () {
1080
+ yield {
1081
+ candidates: [
1082
+ {
1083
+ content: { parts: [{ text: 'Success after fetch error' }] },
1084
+ finishReason: 'STOP',
1085
+ },
1086
+ ],
1087
+ };
1088
+ })());
1089
+ mockRetryWithBackoff.mockImplementation(async (apiCall, options) => {
1090
+ try {
1091
+ return await apiCall();
1092
+ }
1093
+ catch (error) {
1094
+ if (options?.retryFetchErrors &&
1095
+ error instanceof Error &&
1096
+ error.message.includes('exception TypeError: fetch failed sending request')) {
1097
+ return await apiCall();
1098
+ }
1099
+ throw error;
1100
+ }
1101
+ });
1102
+ const stream = await chat.sendMessageStream({ model: 'test-model' }, 'test message', 'prompt-id-fetch-error-retry', new AbortController().signal);
1103
+ const events = [];
1104
+ for await (const event of stream) {
1105
+ events.push(event);
1106
+ }
1107
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
1108
+ expect(events.some((e) => e.type === StreamEventType.CHUNK &&
1109
+ e.value.candidates?.[0]?.content?.parts?.[0]?.text ===
1110
+ 'Success after fetch error')).toBe(true);
1111
+ });
1112
+ afterEach(() => {
1113
+ // Reset to default behavior
1114
+ mockRetryWithBackoff.mockImplementation(async (apiCall) => apiCall());
1115
+ });
1116
+ });
1117
+ });
1118
+ it('should correctly retry and append to an existing history mid-conversation', async () => {
1119
+ // 1. Setup
1120
+ const initialHistory = [
1121
+ { role: 'user', parts: [{ text: 'First question' }] },
1122
+ { role: 'model', parts: [{ text: 'First answer' }] },
1123
+ ];
1124
+ chat.setHistory(initialHistory);
1125
+ // 2. Mock the API to fail once with an empty stream, then succeed.
1126
+ vi.mocked(mockContentGenerator.generateContentStream)
1127
+ .mockImplementationOnce(async () => (async function* () {
1128
+ yield {
1129
+ candidates: [{ content: { parts: [{ text: '' }] } }],
1130
+ };
1131
+ })())
1132
+ .mockImplementationOnce(async () =>
1133
+ // Second attempt succeeds
1134
+ (async function* () {
1135
+ yield {
1136
+ candidates: [
1137
+ {
1138
+ content: { parts: [{ text: 'Second answer' }] },
1139
+ finishReason: 'STOP',
1140
+ },
1141
+ ],
1142
+ };
1143
+ })());
1144
+ // 3. Send a new message
1145
+ const stream = await chat.sendMessageStream({ model: 'gemini-2.0-flash' }, 'Second question', 'prompt-id-retry-existing', new AbortController().signal);
1146
+ for await (const _ of stream) {
1147
+ // consume stream
1148
+ }
1149
+ // 4. Assert the final history and metrics
1150
+ const history = chat.getHistory();
1151
+ expect(history.length).toBe(4);
1152
+ // Assert that the correct metrics were reported for one empty-stream retry
1153
+ expect(mockLogContentRetry).toHaveBeenCalledTimes(1);
1154
+ // Explicitly verify the structure of each part to satisfy TypeScript
1155
+ const turn1 = history[0];
1156
+ if (!turn1?.parts?.[0] || !('text' in turn1.parts[0])) {
1157
+ throw new Error('Test setup error: First turn is not a valid text part.');
1158
+ }
1159
+ expect(turn1.parts[0].text).toBe('First question');
1160
+ const turn2 = history[1];
1161
+ if (!turn2?.parts?.[0] || !('text' in turn2.parts[0])) {
1162
+ throw new Error('Test setup error: Second turn is not a valid text part.');
1163
+ }
1164
+ expect(turn2.parts[0].text).toBe('First answer');
1165
+ const turn3 = history[2];
1166
+ if (!turn3?.parts?.[0] || !('text' in turn3.parts[0])) {
1167
+ throw new Error('Test setup error: Third turn is not a valid text part.');
1168
+ }
1169
+ expect(turn3.parts[0].text).toBe('Second question');
1170
+ const turn4 = history[3];
1171
+ if (!turn4?.parts?.[0] || !('text' in turn4.parts[0])) {
1172
+ throw new Error('Test setup error: Fourth turn is not a valid text part.');
1173
+ }
1174
+ expect(turn4.parts[0].text).toBe('Second answer');
1175
+ });
1176
+ it('should retry if the model returns a completely empty stream (no chunks)', async () => {
1177
+ // 1. Mock the API to return an empty stream first, then a valid one.
1178
+ vi.mocked(mockContentGenerator.generateContentStream)
1179
+ .mockImplementationOnce(
1180
+ // First call resolves to an async generator that yields nothing.
1181
+ async () => (async function* () { })())
1182
+ .mockImplementationOnce(
1183
+ // Second call returns a valid stream.
1184
+ async () => (async function* () {
1185
+ yield {
1186
+ candidates: [
1187
+ {
1188
+ content: {
1189
+ parts: [{ text: 'Successful response after empty' }],
1190
+ },
1191
+ finishReason: 'STOP',
1192
+ },
1193
+ ],
1194
+ };
1195
+ })());
1196
+ // 2. Call the method and consume the stream.
1197
+ const stream = await chat.sendMessageStream({ model: 'gemini-2.0-flash' }, 'test empty stream', 'prompt-id-empty-stream', new AbortController().signal);
1198
+ const chunks = [];
1199
+ for await (const chunk of stream) {
1200
+ chunks.push(chunk);
1201
+ }
1202
+ // 3. Assert the results.
1203
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
1204
+ expect(chunks.some((c) => c.type === StreamEventType.CHUNK &&
1205
+ c.value.candidates?.[0]?.content?.parts?.[0]?.text ===
1206
+ 'Successful response after empty')).toBe(true);
1207
+ const history = chat.getHistory();
1208
+ expect(history.length).toBe(2);
1209
+ // Explicitly verify the structure of each part to satisfy TypeScript
1210
+ const turn1 = history[0];
1211
+ if (!turn1?.parts?.[0] || !('text' in turn1.parts[0])) {
1212
+ throw new Error('Test setup error: First turn is not a valid text part.');
1213
+ }
1214
+ expect(turn1.parts[0].text).toBe('test empty stream');
1215
+ const turn2 = history[1];
1216
+ if (!turn2?.parts?.[0] || !('text' in turn2.parts[0])) {
1217
+ throw new Error('Test setup error: Second turn is not a valid text part.');
1218
+ }
1219
+ expect(turn2.parts[0].text).toBe('Successful response after empty');
1220
+ });
1221
+ it('should queue a subsequent sendMessageStream call until the first stream is fully consumed', async () => {
1222
+ // 1. Create a promise to manually control the stream's lifecycle
1223
+ let continueFirstStream;
1224
+ const firstStreamContinuePromise = new Promise((resolve) => {
1225
+ continueFirstStream = resolve;
1226
+ });
1227
+ // 2. Mock the API to return controllable async generators
1228
+ const firstStreamGenerator = (async function* () {
1229
+ yield {
1230
+ candidates: [
1231
+ { content: { parts: [{ text: 'first response part 1' }] } },
1232
+ ],
1233
+ };
1234
+ await firstStreamContinuePromise; // Pause the stream
1235
+ yield {
1236
+ candidates: [
1237
+ {
1238
+ content: { parts: [{ text: ' part 2' }] },
1239
+ finishReason: 'STOP',
1240
+ },
1241
+ ],
1242
+ };
1243
+ })();
1244
+ const secondStreamGenerator = (async function* () {
1245
+ yield {
1246
+ candidates: [
1247
+ {
1248
+ content: { parts: [{ text: 'second response' }] },
1249
+ finishReason: 'STOP',
1250
+ },
1251
+ ],
1252
+ };
1253
+ })();
1254
+ vi.mocked(mockContentGenerator.generateContentStream)
1255
+ .mockResolvedValueOnce(firstStreamGenerator)
1256
+ .mockResolvedValueOnce(secondStreamGenerator);
1257
+ // 3. Start the first stream and consume only the first chunk to pause it
1258
+ const firstStream = await chat.sendMessageStream({ model: 'test-model' }, 'first', 'prompt-1', new AbortController().signal);
1259
+ const firstStreamIterator = firstStream[Symbol.asyncIterator]();
1260
+ await firstStreamIterator.next();
1261
+ // 4. While the first stream is paused, start the second call. It will block.
1262
+ const secondStreamPromise = chat.sendMessageStream({ model: 'test-model' }, 'second', 'prompt-2', new AbortController().signal);
1263
+ // 5. Assert that only one API call has been made so far.
1264
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(1);
1265
+ // 6. Unblock and fully consume the first stream to completion.
1266
+ continueFirstStream();
1267
+ await firstStreamIterator.next(); // Consume the rest of the stream
1268
+ await firstStreamIterator.next(); // Finish the iterator
1269
+ // 7. Now that the first stream is done, await the second promise to get its generator.
1270
+ const secondStream = await secondStreamPromise;
1271
+ // 8. Start consuming the second stream, which triggers its internal API call.
1272
+ const secondStreamIterator = secondStream[Symbol.asyncIterator]();
1273
+ await secondStreamIterator.next();
1274
+ // 9. The second API call should now have been made.
1275
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
1276
+ // 10. FIX: Fully consume the second stream to ensure recordHistory is called.
1277
+ await secondStreamIterator.next(); // This finishes the iterator.
1278
+ // 11. Final check on history.
1279
+ const history = chat.getHistory();
1280
+ expect(history.length).toBe(4);
1281
+ const turn4 = history[3];
1282
+ if (!turn4?.parts?.[0] || !('text' in turn4.parts[0])) {
1283
+ throw new Error('Test setup error: Fourth turn is not a valid text part.');
1284
+ }
1285
+ expect(turn4.parts[0].text).toBe('second response');
1286
+ });
1287
+ describe('Model Resolution', () => {
1288
+ const mockResponse = {
1289
+ candidates: [
1290
+ {
1291
+ content: { parts: [{ text: 'response' }], role: 'model' },
1292
+ finishReason: 'STOP',
1293
+ },
1294
+ ],
1295
+ };
1296
+ it('should use the FLASH model when in fallback mode (sendMessageStream)', async () => {
1297
+ vi.mocked(mockConfig.getModel).mockReturnValue('gemini-pro');
1298
+ vi.mocked(mockConfig.isInFallbackMode).mockReturnValue(true);
1299
+ vi.mocked(mockContentGenerator.generateContentStream).mockImplementation(async () => (async function* () {
1300
+ yield mockResponse;
1301
+ })());
1302
+ const stream = await chat.sendMessageStream({ model: 'test-model' }, 'test message', 'prompt-id-res3', new AbortController().signal);
1303
+ for await (const _ of stream) {
1304
+ // consume stream
1305
+ }
1306
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledWith(expect.objectContaining({
1307
+ model: DEFAULT_GEMINI_FLASH_MODEL,
1308
+ }), 'prompt-id-res3');
1309
+ });
1310
+ });
1311
+ describe('Fallback Integration (Retries)', () => {
1312
+ const error429 = new ApiError({
1313
+ message: 'API Error 429: Quota exceeded',
1314
+ status: 429,
1315
+ });
1316
+ // Define the simulated behavior for retryWithBackoff for these tests.
1317
+ // This simulation tries the apiCall, if it fails, it calls the callback,
1318
+ // and then tries the apiCall again if the callback returns true.
1319
+ const simulateRetryBehavior = async (apiCall, options) => {
1320
+ try {
1321
+ return await apiCall();
1322
+ }
1323
+ catch (error) {
1324
+ if (options.onPersistent429) {
1325
+ // We simulate the "persistent" trigger here for simplicity.
1326
+ const shouldRetry = await options.onPersistent429(options.authType, error);
1327
+ if (shouldRetry) {
1328
+ return await apiCall();
1329
+ }
1330
+ }
1331
+ throw error; // Stop if callback returns false/null or doesn't exist
1332
+ }
1333
+ };
1334
+ beforeEach(() => {
1335
+ mockRetryWithBackoff.mockImplementation(simulateRetryBehavior);
1336
+ });
1337
+ afterEach(() => {
1338
+ mockRetryWithBackoff.mockImplementation(async (apiCall) => apiCall());
1339
+ });
1340
+ it('should call handleFallback with the specific failed model and retry if handler returns true', async () => {
1341
+ const authType = AuthType.LOGIN_WITH_GOOGLE;
1342
+ vi.mocked(mockConfig.getContentGeneratorConfig).mockReturnValue({
1343
+ authType,
1344
+ });
1345
+ const isInFallbackModeSpy = vi.spyOn(mockConfig, 'isInFallbackMode');
1346
+ isInFallbackModeSpy.mockReturnValue(false);
1347
+ vi.mocked(mockContentGenerator.generateContentStream)
1348
+ .mockRejectedValueOnce(error429) // Attempt 1 fails
1349
+ .mockResolvedValueOnce(
1350
+ // Attempt 2 succeeds
1351
+ (async function* () {
1352
+ yield {
1353
+ candidates: [
1354
+ {
1355
+ content: { parts: [{ text: 'Success on retry' }] },
1356
+ finishReason: 'STOP',
1357
+ },
1358
+ ],
1359
+ };
1360
+ })());
1361
+ mockHandleFallback.mockImplementation(async () => {
1362
+ isInFallbackModeSpy.mockReturnValue(true);
1363
+ return true; // Signal retry
1364
+ });
1365
+ const stream = await chat.sendMessageStream({ model: 'test-model' }, 'trigger 429', 'prompt-id-fb1', new AbortController().signal);
1366
+ // Consume stream to trigger logic
1367
+ for await (const _ of stream) {
1368
+ // no-op
1369
+ }
1370
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
1371
+ expect(mockHandleFallback).toHaveBeenCalledTimes(1);
1372
+ expect(mockHandleFallback).toHaveBeenCalledWith(mockConfig, 'test-model', authType, error429);
1373
+ const history = chat.getHistory();
1374
+ const modelTurn = history[1];
1375
+ expect(modelTurn.parts[0].text).toBe('Success on retry');
1376
+ });
1377
+ it('should switch to DEFAULT_GEMINI_FLASH_MODEL and use thinkingBudget when falling back from a gemini-3 model', async () => {
1378
+ // ARRANGE
1379
+ const authType = AuthType.LOGIN_WITH_GOOGLE;
1380
+ vi.mocked(mockConfig.getContentGeneratorConfig).mockReturnValue({
1381
+ authType,
1382
+ });
1383
+ // Initial state: Not in fallback mode
1384
+ const isInFallbackModeSpy = vi.spyOn(mockConfig, 'isInFallbackMode');
1385
+ isInFallbackModeSpy.mockReturnValue(false);
1386
+ // Mock API calls:
1387
+ // 1. Fails with 429 (simulating gemini-3 failure)
1388
+ // 2. Succeeds (simulating fallback success)
1389
+ vi.mocked(mockContentGenerator.generateContentStream)
1390
+ .mockRejectedValueOnce(error429)
1391
+ .mockResolvedValueOnce((async function* () {
1392
+ yield {
1393
+ candidates: [
1394
+ {
1395
+ content: { parts: [{ text: 'Fallback success' }] },
1396
+ finishReason: 'STOP',
1397
+ },
1398
+ ],
1399
+ };
1400
+ })());
1401
+ // Mock handleFallback to enable fallback mode and signal retry
1402
+ mockHandleFallback.mockImplementation(async () => {
1403
+ isInFallbackModeSpy.mockReturnValue(true); // Next call will see fallback mode = true
1404
+ return true;
1405
+ });
1406
+ // ACT
1407
+ const stream = await chat.sendMessageStream({ model: 'gemini-3-test-model' }, // Start with a gemini-3 model
1408
+ 'test fallback thinking', 'prompt-id-fb3', new AbortController().signal);
1409
+ for await (const _ of stream) {
1410
+ // consume stream
1411
+ }
1412
+ // ASSERT
1413
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
1414
+ // First call: gemini-3 model, thinkingLevel set
1415
+ expect(mockContentGenerator.generateContentStream).toHaveBeenNthCalledWith(1, expect.objectContaining({
1416
+ model: 'gemini-3-test-model',
1417
+ config: expect.objectContaining({
1418
+ thinkingConfig: {
1419
+ thinkingBudget: undefined,
1420
+ thinkingLevel: ThinkingLevel.HIGH,
1421
+ },
1422
+ }),
1423
+ }), 'prompt-id-fb3');
1424
+ // Second call: DEFAULT_GEMINI_FLASH_MODEL (due to fallback), thinkingBudget set (due to fix)
1425
+ expect(mockContentGenerator.generateContentStream).toHaveBeenNthCalledWith(2, expect.objectContaining({
1426
+ model: DEFAULT_GEMINI_FLASH_MODEL,
1427
+ config: expect.objectContaining({
1428
+ thinkingConfig: {
1429
+ thinkingBudget: DEFAULT_THINKING_MODE,
1430
+ thinkingLevel: undefined,
1431
+ },
1432
+ }),
1433
+ }), 'prompt-id-fb3');
1434
+ });
1435
+ it('should stop retrying if handleFallback returns false (e.g., auth intent)', async () => {
1436
+ vi.mocked(mockConfig.getModel).mockReturnValue('gemini-pro');
1437
+ vi.mocked(mockContentGenerator.generateContentStream).mockRejectedValue(error429);
1438
+ mockHandleFallback.mockResolvedValue(false);
1439
+ const stream = await chat.sendMessageStream({ model: 'gemini-2.0-flash' }, 'test stop', 'prompt-id-fb2', new AbortController().signal);
1440
+ await expect((async () => {
1441
+ for await (const _ of stream) {
1442
+ /* consume stream */
1443
+ }
1444
+ })()).rejects.toThrow(error429);
1445
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(1);
1446
+ expect(mockHandleFallback).toHaveBeenCalledTimes(1);
1447
+ });
1448
+ });
1449
+ it('should discard valid partial content from a failed attempt upon retry', async () => {
1450
+ // Mock the stream to fail on the first attempt after yielding some valid content.
1451
+ vi.mocked(mockContentGenerator.generateContentStream)
1452
+ .mockImplementationOnce(async () =>
1453
+ // First attempt: yields one valid chunk, then one invalid chunk
1454
+ (async function* () {
1455
+ yield {
1456
+ candidates: [
1457
+ {
1458
+ content: {
1459
+ parts: [{ text: 'This valid part should be discarded' }],
1460
+ },
1461
+ },
1462
+ ],
1463
+ };
1464
+ yield {
1465
+ candidates: [{ content: { parts: [{ text: '' }] } }], // Invalid chunk triggers retry
1466
+ };
1467
+ })())
1468
+ .mockImplementationOnce(async () =>
1469
+ // Second attempt (the retry): succeeds
1470
+ (async function* () {
1471
+ yield {
1472
+ candidates: [
1473
+ {
1474
+ content: {
1475
+ parts: [{ text: 'Successful final response' }],
1476
+ },
1477
+ finishReason: 'STOP',
1478
+ },
1479
+ ],
1480
+ };
1481
+ })());
1482
+ // Send a message and consume the stream
1483
+ const stream = await chat.sendMessageStream({ model: 'gemini-2.0-flash' }, 'test message', 'prompt-id-discard-test', new AbortController().signal);
1484
+ const events = [];
1485
+ for await (const event of stream) {
1486
+ events.push(event);
1487
+ }
1488
+ // Check that a retry happened
1489
+ expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
1490
+ expect(events.some((e) => e.type === StreamEventType.RETRY)).toBe(true);
1491
+ // Check the final recorded history
1492
+ const history = chat.getHistory();
1493
+ expect(history.length).toBe(2); // user turn + final model turn
1494
+ const modelTurn = history[1];
1495
+ // The model turn should only contain the text from the successful attempt
1496
+ expect(modelTurn.parts[0].text).toBe('Successful final response');
1497
+ // It should NOT contain any text from the failed attempt
1498
+ expect(modelTurn.parts[0].text).not.toContain('This valid part should be discarded');
1499
+ });
1500
+ describe('stripThoughtsFromHistory', () => {
1501
+ it('should strip thought signatures', () => {
1502
+ chat.setHistory([
1503
+ {
1504
+ role: 'user',
1505
+ parts: [{ text: 'hello' }],
1506
+ },
1507
+ {
1508
+ role: 'model',
1509
+ parts: [
1510
+ { text: 'thinking...', thoughtSignature: 'thought-123' },
1511
+ {
1512
+ functionCall: { name: 'test', args: {} },
1513
+ thoughtSignature: 'thought-456',
1514
+ },
1515
+ ],
1516
+ },
1517
+ ]);
1518
+ chat.stripThoughtsFromHistory();
1519
+ expect(chat.getHistory()).toEqual([
1520
+ {
1521
+ role: 'user',
1522
+ parts: [{ text: 'hello' }],
1523
+ },
1524
+ {
1525
+ role: 'model',
1526
+ parts: [
1527
+ { text: 'thinking...' },
1528
+ { functionCall: { name: 'test', args: {} } },
1529
+ ],
1530
+ },
1531
+ ]);
1532
+ });
1533
+ });
1534
+ describe('Preview Model Fallback Logic', () => {
1535
+ it('should reset previewModelBypassMode to false at the start of sendMessageStream', async () => {
1536
+ const stream = (async function* () {
1537
+ yield {
1538
+ candidates: [
1539
+ {
1540
+ content: { role: 'model', parts: [{ text: 'Success' }] },
1541
+ finishReason: 'STOP',
1542
+ },
1543
+ ],
1544
+ };
1545
+ })();
1546
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(stream);
1547
+ await chat.sendMessageStream({ model: 'test-model' }, 'test', 'prompt-id-preview-model-reset', new AbortController().signal);
1548
+ expect(mockConfig.setPreviewModelBypassMode).toHaveBeenCalledWith(false);
1549
+ });
1550
+ it('should reset previewModelFallbackMode to false upon successful Preview Model usage', async () => {
1551
+ const stream = (async function* () {
1552
+ yield {
1553
+ candidates: [
1554
+ {
1555
+ content: { role: 'model', parts: [{ text: 'Success' }] },
1556
+ finishReason: 'STOP',
1557
+ },
1558
+ ],
1559
+ };
1560
+ })();
1561
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(stream);
1562
+ const resultStream = await chat.sendMessageStream({ model: PREVIEW_GEMINI_MODEL }, 'test', 'prompt-id-preview-model-healing', new AbortController().signal);
1563
+ for await (const _ of resultStream) {
1564
+ // consume stream
1565
+ }
1566
+ expect(mockConfig.setPreviewModelFallbackMode).toHaveBeenCalledWith(false);
1567
+ });
1568
+ it('should NOT reset previewModelFallbackMode if Preview Model was bypassed (downgraded)', async () => {
1569
+ const stream = (async function* () {
1570
+ yield {
1571
+ candidates: [
1572
+ {
1573
+ content: { role: 'model', parts: [{ text: 'Success' }] },
1574
+ finishReason: 'STOP',
1575
+ },
1576
+ ],
1577
+ };
1578
+ })();
1579
+ vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(stream);
1580
+ // Simulate bypass mode being active (downgrade happened)
1581
+ vi.mocked(mockConfig.isPreviewModelBypassMode).mockReturnValue(true);
1582
+ const resultStream = await chat.sendMessageStream({ model: PREVIEW_GEMINI_MODEL }, 'test', 'prompt-id-bypass-no-healing', new AbortController().signal);
1583
+ for await (const _ of resultStream) {
1584
+ // consume stream
1585
+ }
1586
+ expect(mockConfig.setPreviewModelFallbackMode).not.toHaveBeenCalled();
1587
+ });
1588
+ });
1589
+ describe('ensureActiveLoopHasThoughtSignatures', () => {
1590
+ it('should add thoughtSignature to the first functionCall in each model turn of the active loop', () => {
1591
+ const chat = new GeminiChat(mockConfig, '', [], []);
1592
+ const history = [
1593
+ { role: 'user', parts: [{ text: 'Old message' }] },
1594
+ {
1595
+ role: 'model',
1596
+ parts: [{ functionCall: { name: 'old_tool', args: {} } }],
1597
+ },
1598
+ { role: 'user', parts: [{ text: 'Find a restaurant' }] }, // active loop starts here
1599
+ {
1600
+ role: 'model',
1601
+ parts: [
1602
+ { functionCall: { name: 'find_restaurant', args: {} } }, // This one gets a signature
1603
+ { functionCall: { name: 'find_restaurant_2', args: {} } }, // This one does NOT
1604
+ ],
1605
+ },
1606
+ {
1607
+ role: 'user',
1608
+ parts: [
1609
+ { functionResponse: { name: 'find_restaurant', response: {} } },
1610
+ ],
1611
+ },
1612
+ {
1613
+ role: 'model',
1614
+ parts: [
1615
+ {
1616
+ functionCall: { name: 'tool_with_sig', args: {} },
1617
+ thoughtSignature: 'existing-sig',
1618
+ },
1619
+ { functionCall: { name: 'another_tool', args: {} } }, // This one does NOT get a signature
1620
+ ],
1621
+ },
1622
+ ];
1623
+ const newContents = chat.ensureActiveLoopHasThoughtSignatures(history);
1624
+ // Outside active loop - unchanged
1625
+ expect(newContents[1]?.parts?.[0]).not.toHaveProperty('thoughtSignature');
1626
+ // Inside active loop, first model turn
1627
+ // First function call gets a signature
1628
+ expect(newContents[3]?.parts?.[0]?.thoughtSignature).toBe(SYNTHETIC_THOUGHT_SIGNATURE);
1629
+ // Second function call does NOT
1630
+ expect(newContents[3]?.parts?.[1]).not.toHaveProperty('thoughtSignature');
1631
+ // User functionResponse part - unchanged (this is not a model turn)
1632
+ expect(newContents[4]?.parts?.[0]).not.toHaveProperty('thoughtSignature');
1633
+ // Inside active loop, second model turn
1634
+ // First function call already has a signature, so nothing changes
1635
+ expect(newContents[5]?.parts?.[0]?.thoughtSignature).toBe('existing-sig');
1636
+ // Second function call does NOT get a signature
1637
+ expect(newContents[5]?.parts?.[1]).not.toHaveProperty('thoughtSignature');
1638
+ });
1639
+ it('should not modify contents if there is no user text message', () => {
1640
+ const chat = new GeminiChat(mockConfig, '', [], []);
1641
+ const history = [
1642
+ {
1643
+ role: 'user',
1644
+ parts: [{ functionResponse: { name: 'tool1', response: {} } }],
1645
+ },
1646
+ {
1647
+ role: 'model',
1648
+ parts: [{ functionCall: { name: 'tool2', args: {} } }],
1649
+ },
1650
+ ];
1651
+ const newContents = chat.ensureActiveLoopHasThoughtSignatures(history);
1652
+ expect(newContents).toEqual(history);
1653
+ expect(newContents[1]?.parts?.[0]).not.toHaveProperty('thoughtSignature');
1654
+ });
1655
+ it('should handle an empty history', () => {
1656
+ const chat = new GeminiChat(mockConfig, '', []);
1657
+ const history = [];
1658
+ const newContents = chat.ensureActiveLoopHasThoughtSignatures(history);
1659
+ expect(newContents).toEqual([]);
1660
+ });
1661
+ it('should handle history with only a user message', () => {
1662
+ const chat = new GeminiChat(mockConfig, '', []);
1663
+ const history = [{ role: 'user', parts: [{ text: 'Hello' }] }];
1664
+ const newContents = chat.ensureActiveLoopHasThoughtSignatures(history);
1665
+ expect(newContents).toEqual(history);
421
1666
  });
422
1667
  });
423
1668
  });