@machina.ai/cell-cli-core 1.0.13-rc3
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.
- package/dist/.last_build +0 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/src/__mocks__/fs/promises.d.ts +11 -0
- package/dist/src/__mocks__/fs/promises.js +17 -0
- package/dist/src/__mocks__/fs/promises.js.map +1 -0
- package/dist/src/code_assist/codeAssist.d.ts +9 -0
- package/dist/src/code_assist/codeAssist.js +19 -0
- package/dist/src/code_assist/codeAssist.js.map +1 -0
- package/dist/src/code_assist/converter.d.ts +68 -0
- package/dist/src/code_assist/converter.js +125 -0
- package/dist/src/code_assist/converter.js.map +1 -0
- package/dist/src/code_assist/converter.test.d.ts +6 -0
- package/dist/src/code_assist/converter.test.js +229 -0
- package/dist/src/code_assist/converter.test.js.map +1 -0
- package/dist/src/code_assist/oauth2.d.ts +20 -0
- package/dist/src/code_assist/oauth2.js +329 -0
- package/dist/src/code_assist/oauth2.js.map +1 -0
- package/dist/src/code_assist/oauth2.test.d.ts +6 -0
- package/dist/src/code_assist/oauth2.test.js +244 -0
- package/dist/src/code_assist/oauth2.test.js.map +1 -0
- package/dist/src/code_assist/server.d.ts +37 -0
- package/dist/src/code_assist/server.js +125 -0
- package/dist/src/code_assist/server.js.map +1 -0
- package/dist/src/code_assist/server.test.d.ts +6 -0
- package/dist/src/code_assist/server.test.js +131 -0
- package/dist/src/code_assist/server.test.js.map +1 -0
- package/dist/src/code_assist/setup.d.ts +20 -0
- package/dist/src/code_assist/setup.js +70 -0
- package/dist/src/code_assist/setup.js.map +1 -0
- package/dist/src/code_assist/setup.test.d.ts +6 -0
- package/dist/src/code_assist/setup.test.js +65 -0
- package/dist/src/code_assist/setup.test.js.map +1 -0
- package/dist/src/code_assist/types.d.ts +148 -0
- package/dist/src/code_assist/types.js +46 -0
- package/dist/src/code_assist/types.js.map +1 -0
- package/dist/src/config/config.d.ts +239 -0
- package/dist/src/config/config.js +484 -0
- package/dist/src/config/config.js.map +1 -0
- package/dist/src/config/config.test.d.ts +6 -0
- package/dist/src/config/config.test.js +304 -0
- package/dist/src/config/config.test.js.map +1 -0
- package/dist/src/config/flashFallback.test.d.ts +6 -0
- package/dist/src/config/flashFallback.test.js +115 -0
- package/dist/src/config/flashFallback.test.js.map +1 -0
- package/dist/src/config/models.d.ts +8 -0
- package/dist/src/config/models.js +9 -0
- package/dist/src/config/models.js.map +1 -0
- package/dist/src/core/client.d.ts +60 -0
- package/dist/src/core/client.js +502 -0
- package/dist/src/core/client.js.map +1 -0
- package/dist/src/core/client.test.d.ts +6 -0
- package/dist/src/core/client.test.js +853 -0
- package/dist/src/core/client.test.js.map +1 -0
- package/dist/src/core/contentGenerator.d.ts +36 -0
- package/dist/src/core/contentGenerator.js +110 -0
- package/dist/src/core/contentGenerator.js.map +1 -0
- package/dist/src/core/contentGenerator.test.d.ts +6 -0
- package/dist/src/core/contentGenerator.test.js +225 -0
- package/dist/src/core/contentGenerator.test.js.map +1 -0
- package/dist/src/core/coreToolScheduler.d.ts +104 -0
- package/dist/src/core/coreToolScheduler.js +410 -0
- package/dist/src/core/coreToolScheduler.js.map +1 -0
- package/dist/src/core/coreToolScheduler.test.d.ts +6 -0
- package/dist/src/core/coreToolScheduler.test.js +402 -0
- package/dist/src/core/coreToolScheduler.test.js.map +1 -0
- package/dist/src/core/geminiChat.d.ts +116 -0
- package/dist/src/core/geminiChat.js +511 -0
- package/dist/src/core/geminiChat.js.map +1 -0
- package/dist/src/core/geminiChat.test.d.ts +6 -0
- package/dist/src/core/geminiChat.test.js +424 -0
- package/dist/src/core/geminiChat.test.js.map +1 -0
- package/dist/src/core/geminiRequest.d.ts +13 -0
- package/dist/src/core/geminiRequest.js +45 -0
- package/dist/src/core/geminiRequest.js.map +1 -0
- package/dist/src/core/geminiRequest.test.d.ts +6 -0
- package/dist/src/core/geminiRequest.test.js +72 -0
- package/dist/src/core/geminiRequest.test.js.map +1 -0
- package/dist/src/core/logger.d.ts +35 -0
- package/dist/src/core/logger.js +235 -0
- package/dist/src/core/logger.js.map +1 -0
- package/dist/src/core/logger.test.d.ts +6 -0
- package/dist/src/core/logger.test.js +387 -0
- package/dist/src/core/logger.test.js.map +1 -0
- package/dist/src/core/modelCheck.d.ts +14 -0
- package/dist/src/core/modelCheck.js +62 -0
- package/dist/src/core/modelCheck.js.map +1 -0
- package/dist/src/core/nonInteractiveToolExecutor.d.ts +12 -0
- package/dist/src/core/nonInteractiveToolExecutor.js +97 -0
- package/dist/src/core/nonInteractiveToolExecutor.js.map +1 -0
- package/dist/src/core/nonInteractiveToolExecutor.test.d.ts +6 -0
- package/dist/src/core/nonInteractiveToolExecutor.test.js +189 -0
- package/dist/src/core/nonInteractiveToolExecutor.test.js.map +1 -0
- package/dist/src/core/prompts.d.ts +12 -0
- package/dist/src/core/prompts.js +335 -0
- package/dist/src/core/prompts.js.map +1 -0
- package/dist/src/core/prompts.test.d.ts +6 -0
- package/dist/src/core/prompts.test.js +97 -0
- package/dist/src/core/prompts.test.js.map +1 -0
- package/dist/src/core/tokenLimits.d.ts +10 -0
- package/dist/src/core/tokenLimits.js +27 -0
- package/dist/src/core/tokenLimits.js.map +1 -0
- package/dist/src/core/turn.d.ts +111 -0
- package/dist/src/core/turn.js +139 -0
- package/dist/src/core/turn.js.map +1 -0
- package/dist/src/core/turn.test.d.ts +6 -0
- package/dist/src/core/turn.test.js +365 -0
- package/dist/src/core/turn.test.js.map +1 -0
- package/dist/src/index.d.ts +57 -0
- package/dist/src/index.js +64 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/index.test.d.ts +6 -0
- package/dist/src/index.test.js +12 -0
- package/dist/src/index.test.js.map +1 -0
- package/dist/src/mcp/oauth-provider.d.ts +142 -0
- package/dist/src/mcp/oauth-provider.js +446 -0
- package/dist/src/mcp/oauth-provider.js.map +1 -0
- package/dist/src/mcp/oauth-provider.test.d.ts +6 -0
- package/dist/src/mcp/oauth-provider.test.js +520 -0
- package/dist/src/mcp/oauth-provider.test.js.map +1 -0
- package/dist/src/mcp/oauth-token-storage.d.ts +81 -0
- package/dist/src/mcp/oauth-token-storage.js +149 -0
- package/dist/src/mcp/oauth-token-storage.js.map +1 -0
- package/dist/src/mcp/oauth-token-storage.test.d.ts +6 -0
- package/dist/src/mcp/oauth-token-storage.test.js +205 -0
- package/dist/src/mcp/oauth-token-storage.test.js.map +1 -0
- package/dist/src/mcp/oauth-utils.d.ts +109 -0
- package/dist/src/mcp/oauth-utils.js +183 -0
- package/dist/src/mcp/oauth-utils.js.map +1 -0
- package/dist/src/mcp/oauth-utils.test.d.ts +6 -0
- package/dist/src/mcp/oauth-utils.test.js +144 -0
- package/dist/src/mcp/oauth-utils.test.js.map +1 -0
- package/dist/src/services/fileDiscoveryService.d.ts +35 -0
- package/dist/src/services/fileDiscoveryService.js +91 -0
- package/dist/src/services/fileDiscoveryService.js.map +1 -0
- package/dist/src/services/fileDiscoveryService.test.d.ts +6 -0
- package/dist/src/services/fileDiscoveryService.test.js +102 -0
- package/dist/src/services/fileDiscoveryService.test.js.map +1 -0
- package/dist/src/services/gitService.d.ts +21 -0
- package/dist/src/services/gitService.js +101 -0
- package/dist/src/services/gitService.js.map +1 -0
- package/dist/src/services/gitService.test.d.ts +6 -0
- package/dist/src/services/gitService.test.js +228 -0
- package/dist/src/services/gitService.test.js.map +1 -0
- package/dist/src/services/ideContext.d.ts +178 -0
- package/dist/src/services/ideContext.js +105 -0
- package/dist/src/services/ideContext.js.map +1 -0
- package/dist/src/services/ideContext.test.d.ts +6 -0
- package/dist/src/services/ideContext.test.js +111 -0
- package/dist/src/services/ideContext.test.js.map +1 -0
- package/dist/src/services/loopDetectionService.d.ts +51 -0
- package/dist/src/services/loopDetectionService.js +232 -0
- package/dist/src/services/loopDetectionService.js.map +1 -0
- package/dist/src/services/loopDetectionService.test.d.ts +6 -0
- package/dist/src/services/loopDetectionService.test.js +339 -0
- package/dist/src/services/loopDetectionService.test.js.map +1 -0
- package/dist/src/telemetry/clearcut-logger/clearcut-logger.d.ts +37 -0
- package/dist/src/telemetry/clearcut-logger/clearcut-logger.js +456 -0
- package/dist/src/telemetry/clearcut-logger/clearcut-logger.js.map +1 -0
- package/dist/src/telemetry/clearcut-logger/event-metadata-key.d.ts +49 -0
- package/dist/src/telemetry/clearcut-logger/event-metadata-key.js +127 -0
- package/dist/src/telemetry/clearcut-logger/event-metadata-key.js.map +1 -0
- package/dist/src/telemetry/constants.d.ts +20 -0
- package/dist/src/telemetry/constants.js +21 -0
- package/dist/src/telemetry/constants.js.map +1 -0
- package/dist/src/telemetry/index.d.ts +18 -0
- package/dist/src/telemetry/index.js +20 -0
- package/dist/src/telemetry/index.js.map +1 -0
- package/dist/src/telemetry/integration.test.circular.d.ts +6 -0
- package/dist/src/telemetry/integration.test.circular.js +53 -0
- package/dist/src/telemetry/integration.test.circular.js.map +1 -0
- package/dist/src/telemetry/loggers.d.ts +15 -0
- package/dist/src/telemetry/loggers.js +220 -0
- package/dist/src/telemetry/loggers.js.map +1 -0
- package/dist/src/telemetry/loggers.test.circular.d.ts +6 -0
- package/dist/src/telemetry/loggers.test.circular.js +100 -0
- package/dist/src/telemetry/loggers.test.circular.js.map +1 -0
- package/dist/src/telemetry/loggers.test.d.ts +6 -0
- package/dist/src/telemetry/loggers.test.js +558 -0
- package/dist/src/telemetry/loggers.test.js.map +1 -0
- package/dist/src/telemetry/metrics.d.ts +19 -0
- package/dist/src/telemetry/metrics.js +144 -0
- package/dist/src/telemetry/metrics.js.map +1 -0
- package/dist/src/telemetry/metrics.test.d.ts +6 -0
- package/dist/src/telemetry/metrics.test.js +162 -0
- package/dist/src/telemetry/metrics.test.js.map +1 -0
- package/dist/src/telemetry/sdk.d.ts +9 -0
- package/dist/src/telemetry/sdk.js +116 -0
- package/dist/src/telemetry/sdk.js.map +1 -0
- package/dist/src/telemetry/telemetry.test.d.ts +6 -0
- package/dist/src/telemetry/telemetry.test.js +50 -0
- package/dist/src/telemetry/telemetry.test.js.map +1 -0
- package/dist/src/telemetry/types.d.ts +116 -0
- package/dist/src/telemetry/types.js +218 -0
- package/dist/src/telemetry/types.js.map +1 -0
- package/dist/src/telemetry/uiTelemetry.d.ts +68 -0
- package/dist/src/telemetry/uiTelemetry.js +138 -0
- package/dist/src/telemetry/uiTelemetry.js.map +1 -0
- package/dist/src/telemetry/uiTelemetry.test.d.ts +6 -0
- package/dist/src/telemetry/uiTelemetry.test.js +504 -0
- package/dist/src/telemetry/uiTelemetry.test.js.map +1 -0
- package/dist/src/tools/diffOptions.d.ts +7 -0
- package/dist/src/tools/diffOptions.js +10 -0
- package/dist/src/tools/diffOptions.js.map +1 -0
- package/dist/src/tools/edit.d.ts +79 -0
- package/dist/src/tools/edit.js +362 -0
- package/dist/src/tools/edit.js.map +1 -0
- package/dist/src/tools/edit.test.d.ts +6 -0
- package/dist/src/tools/edit.test.js +512 -0
- package/dist/src/tools/edit.test.js.map +1 -0
- package/dist/src/tools/glob.d.ts +58 -0
- package/dist/src/tools/glob.js +193 -0
- package/dist/src/tools/glob.js.map +1 -0
- package/dist/src/tools/glob.test.d.ts +6 -0
- package/dist/src/tools/glob.test.js +294 -0
- package/dist/src/tools/glob.test.js.map +1 -0
- package/dist/src/tools/grep.d.ts +78 -0
- package/dist/src/tools/grep.js +422 -0
- package/dist/src/tools/grep.js.map +1 -0
- package/dist/src/tools/grep.test.d.ts +6 -0
- package/dist/src/tools/grep.test.js +188 -0
- package/dist/src/tools/grep.test.js.map +1 -0
- package/dist/src/tools/ls.d.ts +86 -0
- package/dist/src/tools/ls.js +223 -0
- package/dist/src/tools/ls.js.map +1 -0
- package/dist/src/tools/mcp-client.d.ts +110 -0
- package/dist/src/tools/mcp-client.js +319 -0
- package/dist/src/tools/mcp-client.js.map +1 -0
- package/dist/src/tools/mcp-client.test.d.ts +6 -0
- package/dist/src/tools/mcp-client.test.js +156 -0
- package/dist/src/tools/mcp-client.test.js.map +1 -0
- package/dist/src/tools/mcp-tool.d.ts +29 -0
- package/dist/src/tools/mcp-tool.js +143 -0
- package/dist/src/tools/mcp-tool.js.map +1 -0
- package/dist/src/tools/mcp-tool.test.d.ts +6 -0
- package/dist/src/tools/mcp-tool.test.js +189 -0
- package/dist/src/tools/mcp-tool.test.js.map +1 -0
- package/dist/src/tools/memoryTool.d.ts +28 -0
- package/dist/src/tools/memoryTool.js +172 -0
- package/dist/src/tools/memoryTool.js.map +1 -0
- package/dist/src/tools/memoryTool.test.d.ts +6 -0
- package/dist/src/tools/memoryTool.test.js +181 -0
- package/dist/src/tools/memoryTool.test.js.map +1 -0
- package/dist/src/tools/modifiable-tool.d.ts +29 -0
- package/dist/src/tools/modifiable-tool.js +85 -0
- package/dist/src/tools/modifiable-tool.js.map +1 -0
- package/dist/src/tools/modifiable-tool.test.d.ts +6 -0
- package/dist/src/tools/modifiable-tool.test.js +204 -0
- package/dist/src/tools/modifiable-tool.test.js.map +1 -0
- package/dist/src/tools/read-file.d.ts +36 -0
- package/dist/src/tools/read-file.js +102 -0
- package/dist/src/tools/read-file.js.map +1 -0
- package/dist/src/tools/read-file.test.d.ts +6 -0
- package/dist/src/tools/read-file.test.js +181 -0
- package/dist/src/tools/read-file.test.js.map +1 -0
- package/dist/src/tools/read-many-files.d.ts +62 -0
- package/dist/src/tools/read-many-files.js +362 -0
- package/dist/src/tools/read-many-files.js.map +1 -0
- package/dist/src/tools/read-many-files.test.d.ts +6 -0
- package/dist/src/tools/read-many-files.test.js +315 -0
- package/dist/src/tools/read-many-files.test.js.map +1 -0
- package/dist/src/tools/shell.d.ts +43 -0
- package/dist/src/tools/shell.js +435 -0
- package/dist/src/tools/shell.js.map +1 -0
- package/dist/src/tools/shell.test.d.ts +6 -0
- package/dist/src/tools/shell.test.js +406 -0
- package/dist/src/tools/shell.test.js.map +1 -0
- package/dist/src/tools/tool-registry.d.ts +66 -0
- package/dist/src/tools/tool-registry.js +329 -0
- package/dist/src/tools/tool-registry.js.map +1 -0
- package/dist/src/tools/tool-registry.test.d.ts +6 -0
- package/dist/src/tools/tool-registry.test.js +424 -0
- package/dist/src/tools/tool-registry.test.js.map +1 -0
- package/dist/src/tools/tools.d.ts +219 -0
- package/dist/src/tools/tools.js +111 -0
- package/dist/src/tools/tools.js.map +1 -0
- package/dist/src/tools/web-fetch.d.ts +29 -0
- package/dist/src/tools/web-fetch.js +246 -0
- package/dist/src/tools/web-fetch.js.map +1 -0
- package/dist/src/tools/web-fetch.test.d.ts +6 -0
- package/dist/src/tools/web-fetch.test.js +71 -0
- package/dist/src/tools/web-fetch.test.js.map +1 -0
- package/dist/src/tools/web-search.d.ts +49 -0
- package/dist/src/tools/web-search.js +120 -0
- package/dist/src/tools/web-search.js.map +1 -0
- package/dist/src/tools/write-file.d.ts +42 -0
- package/dist/src/tools/write-file.js +253 -0
- package/dist/src/tools/write-file.js.map +1 -0
- package/dist/src/tools/write-file.test.d.ts +6 -0
- package/dist/src/tools/write-file.test.js +413 -0
- package/dist/src/tools/write-file.test.js.map +1 -0
- package/dist/src/utils/LruCache.d.ts +13 -0
- package/dist/src/utils/LruCache.js +38 -0
- package/dist/src/utils/LruCache.js.map +1 -0
- package/dist/src/utils/bfsFileSearch.d.ts +24 -0
- package/dist/src/utils/bfsFileSearch.js +65 -0
- package/dist/src/utils/bfsFileSearch.js.map +1 -0
- package/dist/src/utils/bfsFileSearch.test.d.ts +6 -0
- package/dist/src/utils/bfsFileSearch.test.js +162 -0
- package/dist/src/utils/bfsFileSearch.test.js.map +1 -0
- package/dist/src/utils/browser.d.ts +13 -0
- package/dist/src/utils/browser.js +49 -0
- package/dist/src/utils/browser.js.map +1 -0
- package/dist/src/utils/editCorrector.d.ts +53 -0
- package/dist/src/utils/editCorrector.js +546 -0
- package/dist/src/utils/editCorrector.js.map +1 -0
- package/dist/src/utils/editCorrector.test.d.ts +6 -0
- package/dist/src/utils/editCorrector.test.js +564 -0
- package/dist/src/utils/editCorrector.test.js.map +1 -0
- package/dist/src/utils/editor.d.ts +28 -0
- package/dist/src/utils/editor.js +169 -0
- package/dist/src/utils/editor.js.map +1 -0
- package/dist/src/utils/editor.test.d.ts +6 -0
- package/dist/src/utils/editor.test.js +393 -0
- package/dist/src/utils/editor.test.js.map +1 -0
- package/dist/src/utils/errorReporting.d.ts +14 -0
- package/dist/src/utils/errorReporting.js +88 -0
- package/dist/src/utils/errorReporting.js.map +1 -0
- package/dist/src/utils/errorReporting.test.d.ts +6 -0
- package/dist/src/utils/errorReporting.test.js +124 -0
- package/dist/src/utils/errorReporting.test.js.map +1 -0
- package/dist/src/utils/errors.d.ts +14 -0
- package/dist/src/utils/errors.js +54 -0
- package/dist/src/utils/errors.js.map +1 -0
- package/dist/src/utils/fetch.d.ts +11 -0
- package/dist/src/utils/fetch.js +51 -0
- package/dist/src/utils/fetch.js.map +1 -0
- package/dist/src/utils/fileUtils.d.ts +49 -0
- package/dist/src/utils/fileUtils.js +300 -0
- package/dist/src/utils/fileUtils.js.map +1 -0
- package/dist/src/utils/fileUtils.test.d.ts +6 -0
- package/dist/src/utils/fileUtils.test.js +321 -0
- package/dist/src/utils/fileUtils.test.js.map +1 -0
- package/dist/src/utils/flashFallback.integration.test.d.ts +6 -0
- package/dist/src/utils/flashFallback.integration.test.js +112 -0
- package/dist/src/utils/flashFallback.integration.test.js.map +1 -0
- package/dist/src/utils/generateContentResponseUtilities.d.ts +14 -0
- package/dist/src/utils/generateContentResponseUtilities.js +92 -0
- package/dist/src/utils/generateContentResponseUtilities.js.map +1 -0
- package/dist/src/utils/generateContentResponseUtilities.test.d.ts +6 -0
- package/dist/src/utils/generateContentResponseUtilities.test.js +273 -0
- package/dist/src/utils/generateContentResponseUtilities.test.js.map +1 -0
- package/dist/src/utils/getFolderStructure.d.ts +31 -0
- package/dist/src/utils/getFolderStructure.js +246 -0
- package/dist/src/utils/getFolderStructure.js.map +1 -0
- package/dist/src/utils/getFolderStructure.test.d.ts +6 -0
- package/dist/src/utils/getFolderStructure.test.js +284 -0
- package/dist/src/utils/getFolderStructure.test.js.map +1 -0
- package/dist/src/utils/gitIgnoreParser.d.ts +20 -0
- package/dist/src/utils/gitIgnoreParser.js +67 -0
- package/dist/src/utils/gitIgnoreParser.js.map +1 -0
- package/dist/src/utils/gitIgnoreParser.test.d.ts +6 -0
- package/dist/src/utils/gitIgnoreParser.test.js +157 -0
- package/dist/src/utils/gitIgnoreParser.test.js.map +1 -0
- package/dist/src/utils/gitUtils.d.ts +17 -0
- package/dist/src/utils/gitUtils.js +61 -0
- package/dist/src/utils/gitUtils.js.map +1 -0
- package/dist/src/utils/memoryDiscovery.d.ts +15 -0
- package/dist/src/utils/memoryDiscovery.js +226 -0
- package/dist/src/utils/memoryDiscovery.js.map +1 -0
- package/dist/src/utils/memoryDiscovery.test.d.ts +6 -0
- package/dist/src/utils/memoryDiscovery.test.js +432 -0
- package/dist/src/utils/memoryDiscovery.test.js.map +1 -0
- package/dist/src/utils/memoryImportProcessor.d.ts +35 -0
- package/dist/src/utils/memoryImportProcessor.js +141 -0
- package/dist/src/utils/memoryImportProcessor.js.map +1 -0
- package/dist/src/utils/memoryImportProcessor.test.d.ts +6 -0
- package/dist/src/utils/memoryImportProcessor.test.js +170 -0
- package/dist/src/utils/memoryImportProcessor.test.js.map +1 -0
- package/dist/src/utils/messageInspectors.d.ts +8 -0
- package/dist/src/utils/messageInspectors.js +16 -0
- package/dist/src/utils/messageInspectors.js.map +1 -0
- package/dist/src/utils/nextSpeakerChecker.d.ts +12 -0
- package/dist/src/utils/nextSpeakerChecker.js +112 -0
- package/dist/src/utils/nextSpeakerChecker.js.map +1 -0
- package/dist/src/utils/nextSpeakerChecker.test.d.ts +6 -0
- package/dist/src/utils/nextSpeakerChecker.test.js +168 -0
- package/dist/src/utils/nextSpeakerChecker.test.js.map +1 -0
- package/dist/src/utils/paths.d.ts +59 -0
- package/dist/src/utils/paths.js +153 -0
- package/dist/src/utils/paths.js.map +1 -0
- package/dist/src/utils/quotaErrorDetection.d.ts +22 -0
- package/dist/src/utils/quotaErrorDetection.js +65 -0
- package/dist/src/utils/quotaErrorDetection.js.map +1 -0
- package/dist/src/utils/retry.d.ts +27 -0
- package/dist/src/utils/retry.js +276 -0
- package/dist/src/utils/retry.js.map +1 -0
- package/dist/src/utils/retry.test.d.ts +6 -0
- package/dist/src/utils/retry.test.js +322 -0
- package/dist/src/utils/retry.test.js.map +1 -0
- package/dist/src/utils/safeJsonStringify.d.ts +13 -0
- package/dist/src/utils/safeJsonStringify.js +25 -0
- package/dist/src/utils/safeJsonStringify.js.map +1 -0
- package/dist/src/utils/safeJsonStringify.test.d.ts +6 -0
- package/dist/src/utils/safeJsonStringify.test.js +61 -0
- package/dist/src/utils/safeJsonStringify.test.js.map +1 -0
- package/dist/src/utils/schemaValidator.d.ts +22 -0
- package/dist/src/utils/schemaValidator.js +65 -0
- package/dist/src/utils/schemaValidator.js.map +1 -0
- package/dist/src/utils/session.d.ts +6 -0
- package/dist/src/utils/session.js +8 -0
- package/dist/src/utils/session.js.map +1 -0
- package/dist/src/utils/stripJsonCodeBlock.d.ts +13 -0
- package/dist/src/utils/stripJsonCodeBlock.js +21 -0
- package/dist/src/utils/stripJsonCodeBlock.js.map +1 -0
- package/dist/src/utils/summarizer.d.ts +25 -0
- package/dist/src/utils/summarizer.js +80 -0
- package/dist/src/utils/summarizer.js.map +1 -0
- package/dist/src/utils/summarizer.test.d.ts +6 -0
- package/dist/src/utils/summarizer.test.js +131 -0
- package/dist/src/utils/summarizer.test.js.map +1 -0
- package/dist/src/utils/systemEncoding.d.ts +40 -0
- package/dist/src/utils/systemEncoding.js +149 -0
- package/dist/src/utils/systemEncoding.js.map +1 -0
- package/dist/src/utils/systemEncoding.test.d.ts +6 -0
- package/dist/src/utils/systemEncoding.test.js +368 -0
- package/dist/src/utils/systemEncoding.test.js.map +1 -0
- package/dist/src/utils/testUtils.d.ts +29 -0
- package/dist/src/utils/testUtils.js +70 -0
- package/dist/src/utils/testUtils.js.map +1 -0
- package/dist/src/utils/user_account.d.ts +9 -0
- package/dist/src/utils/user_account.js +99 -0
- package/dist/src/utils/user_account.js.map +1 -0
- package/dist/src/utils/user_account.test.d.ts +6 -0
- package/dist/src/utils/user_account.test.js +153 -0
- package/dist/src/utils/user_account.test.js.map +1 -0
- package/dist/src/utils/user_id.d.ts +11 -0
- package/dist/src/utils/user_id.js +49 -0
- package/dist/src/utils/user_id.js.map +1 -0
- package/dist/src/utils/user_id.test.d.ts +6 -0
- package/dist/src/utils/user_id.test.js +21 -0
- package/dist/src/utils/user_id.test.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +62 -0
|
@@ -0,0 +1,853 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* Copyright 2025 Google LLC
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
7
|
+
import { GoogleGenAI, } from '@google/genai';
|
|
8
|
+
import { findIndexAfterFraction, GeminiClient } from './client.js';
|
|
9
|
+
import { AuthType } from './contentGenerator.js';
|
|
10
|
+
import { Config } from '../config/config.js';
|
|
11
|
+
import { GeminiEventType, Turn } from './turn.js';
|
|
12
|
+
import { getCoreSystemPrompt } from './prompts.js';
|
|
13
|
+
import { DEFAULT_GEMINI_FLASH_MODEL } from '../config/models.js';
|
|
14
|
+
import { FileDiscoveryService } from '../services/fileDiscoveryService.js';
|
|
15
|
+
import { setSimulate429 } from '../utils/testUtils.js';
|
|
16
|
+
import { tokenLimit } from './tokenLimits.js';
|
|
17
|
+
import { ideContext } from '../services/ideContext.js';
|
|
18
|
+
// --- Mocks ---
|
|
19
|
+
const mockChatCreateFn = vi.fn();
|
|
20
|
+
const mockGenerateContentFn = vi.fn();
|
|
21
|
+
const mockEmbedContentFn = vi.fn();
|
|
22
|
+
const mockTurnRunFn = vi.fn();
|
|
23
|
+
vi.mock('@google/genai');
|
|
24
|
+
vi.mock('./turn', () => {
|
|
25
|
+
// Define a mock class that has the same shape as the real Turn
|
|
26
|
+
class MockTurn {
|
|
27
|
+
pendingToolCalls = [];
|
|
28
|
+
// The run method is a property that holds our mock function
|
|
29
|
+
run = mockTurnRunFn;
|
|
30
|
+
constructor() {
|
|
31
|
+
// The constructor can be empty or do some mock setup
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
// Export the mock class as 'Turn'
|
|
35
|
+
return {
|
|
36
|
+
Turn: MockTurn,
|
|
37
|
+
GeminiEventType: {
|
|
38
|
+
MaxSessionTurns: 'MaxSessionTurns',
|
|
39
|
+
ChatCompressed: 'ChatCompressed',
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
});
|
|
43
|
+
vi.mock('../config/config.js');
|
|
44
|
+
vi.mock('./prompts');
|
|
45
|
+
vi.mock('../utils/getFolderStructure', () => ({
|
|
46
|
+
getFolderStructure: vi.fn().mockResolvedValue('Mock Folder Structure'),
|
|
47
|
+
}));
|
|
48
|
+
vi.mock('../utils/errorReporting', () => ({ reportError: vi.fn() }));
|
|
49
|
+
vi.mock('../utils/nextSpeakerChecker', () => ({
|
|
50
|
+
checkNextSpeaker: vi.fn().mockResolvedValue(null),
|
|
51
|
+
}));
|
|
52
|
+
vi.mock('../utils/generateContentResponseUtilities', () => ({
|
|
53
|
+
getResponseText: (result) => result.candidates?.[0]?.content?.parts?.map((part) => part.text).join('') ||
|
|
54
|
+
undefined,
|
|
55
|
+
}));
|
|
56
|
+
vi.mock('../telemetry/index.js', () => ({
|
|
57
|
+
logApiRequest: vi.fn(),
|
|
58
|
+
logApiResponse: vi.fn(),
|
|
59
|
+
logApiError: vi.fn(),
|
|
60
|
+
}));
|
|
61
|
+
vi.mock('../services/ideContext.js');
|
|
62
|
+
describe('findIndexAfterFraction', () => {
|
|
63
|
+
const history = [
|
|
64
|
+
{ role: 'user', parts: [{ text: 'This is the first message.' }] }, // JSON length: 66
|
|
65
|
+
{ role: 'model', parts: [{ text: 'This is the second message.' }] }, // JSON length: 68
|
|
66
|
+
{ role: 'user', parts: [{ text: 'This is the third message.' }] }, // JSON length: 66
|
|
67
|
+
{ role: 'model', parts: [{ text: 'This is the fourth message.' }] }, // JSON length: 68
|
|
68
|
+
{ role: 'user', parts: [{ text: 'This is the fifth message.' }] }, // JSON length: 65
|
|
69
|
+
];
|
|
70
|
+
// Total length: 333
|
|
71
|
+
it('should throw an error for non-positive numbers', () => {
|
|
72
|
+
expect(() => findIndexAfterFraction(history, 0)).toThrow('Fraction must be between 0 and 1');
|
|
73
|
+
});
|
|
74
|
+
it('should throw an error for a fraction greater than or equal to 1', () => {
|
|
75
|
+
expect(() => findIndexAfterFraction(history, 1)).toThrow('Fraction must be between 0 and 1');
|
|
76
|
+
});
|
|
77
|
+
it('should handle a fraction in the middle', () => {
|
|
78
|
+
// 333 * 0.5 = 166.5
|
|
79
|
+
// 0: 66
|
|
80
|
+
// 1: 66 + 68 = 134
|
|
81
|
+
// 2: 134 + 66 = 200
|
|
82
|
+
// 200 >= 166.5, so index is 2
|
|
83
|
+
expect(findIndexAfterFraction(history, 0.5)).toBe(2);
|
|
84
|
+
});
|
|
85
|
+
it('should handle a fraction that results in the last index', () => {
|
|
86
|
+
// 333 * 0.9 = 299.7
|
|
87
|
+
// ...
|
|
88
|
+
// 3: 200 + 68 = 268
|
|
89
|
+
// 4: 268 + 65 = 333
|
|
90
|
+
// 333 >= 299.7, so index is 4
|
|
91
|
+
expect(findIndexAfterFraction(history, 0.9)).toBe(4);
|
|
92
|
+
});
|
|
93
|
+
it('should handle an empty history', () => {
|
|
94
|
+
expect(findIndexAfterFraction([], 0.5)).toBe(0);
|
|
95
|
+
});
|
|
96
|
+
it('should handle a history with only one item', () => {
|
|
97
|
+
expect(findIndexAfterFraction(history.slice(0, 1), 0.5)).toBe(0);
|
|
98
|
+
});
|
|
99
|
+
it('should handle history with weird parts', () => {
|
|
100
|
+
const historyWithEmptyParts = [
|
|
101
|
+
{ role: 'user', parts: [{ text: 'Message 1' }] },
|
|
102
|
+
{ role: 'model', parts: [{ fileData: { fileUri: 'derp' } }] },
|
|
103
|
+
{ role: 'user', parts: [{ text: 'Message 2' }] },
|
|
104
|
+
];
|
|
105
|
+
expect(findIndexAfterFraction(historyWithEmptyParts, 0.5)).toBe(1);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
describe('Gemini Client (client.ts)', () => {
|
|
109
|
+
let client;
|
|
110
|
+
beforeEach(async () => {
|
|
111
|
+
vi.resetAllMocks();
|
|
112
|
+
// Disable 429 simulation for tests
|
|
113
|
+
setSimulate429(false);
|
|
114
|
+
// Set up the mock for GoogleGenAI constructor and its methods
|
|
115
|
+
const MockedGoogleGenAI = vi.mocked(GoogleGenAI);
|
|
116
|
+
MockedGoogleGenAI.mockImplementation(() => {
|
|
117
|
+
const mock = {
|
|
118
|
+
chats: { create: mockChatCreateFn },
|
|
119
|
+
models: {
|
|
120
|
+
generateContent: mockGenerateContentFn,
|
|
121
|
+
embedContent: mockEmbedContentFn,
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
return mock;
|
|
125
|
+
});
|
|
126
|
+
mockChatCreateFn.mockResolvedValue({});
|
|
127
|
+
mockGenerateContentFn.mockResolvedValue({
|
|
128
|
+
candidates: [
|
|
129
|
+
{
|
|
130
|
+
content: {
|
|
131
|
+
parts: [{ text: '{"key": "value"}' }],
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
],
|
|
135
|
+
});
|
|
136
|
+
// Because the GeminiClient constructor kicks off an async process (startChat)
|
|
137
|
+
// that depends on a fully-formed Config object, we need to mock the
|
|
138
|
+
// entire implementation of Config for these tests.
|
|
139
|
+
const mockToolRegistry = {
|
|
140
|
+
getFunctionDeclarations: vi.fn().mockReturnValue([]),
|
|
141
|
+
getTool: vi.fn().mockReturnValue(null),
|
|
142
|
+
};
|
|
143
|
+
const fileService = new FileDiscoveryService('/test/dir');
|
|
144
|
+
const contentGeneratorConfig = {
|
|
145
|
+
model: 'test-model',
|
|
146
|
+
apiKey: 'test-key',
|
|
147
|
+
vertexai: false,
|
|
148
|
+
authType: AuthType.USE_GEMINI,
|
|
149
|
+
};
|
|
150
|
+
const mockConfigObject = {
|
|
151
|
+
getContentGeneratorConfig: vi
|
|
152
|
+
.fn()
|
|
153
|
+
.mockReturnValue(contentGeneratorConfig),
|
|
154
|
+
getToolRegistry: vi.fn().mockResolvedValue(mockToolRegistry),
|
|
155
|
+
getModel: vi.fn().mockReturnValue('test-model'),
|
|
156
|
+
getEmbeddingModel: vi.fn().mockReturnValue('test-embedding-model'),
|
|
157
|
+
getApiKey: vi.fn().mockReturnValue('test-key'),
|
|
158
|
+
getVertexAI: vi.fn().mockReturnValue(false),
|
|
159
|
+
getUserAgent: vi.fn().mockReturnValue('test-agent'),
|
|
160
|
+
getUserMemory: vi.fn().mockReturnValue(''),
|
|
161
|
+
getFullContext: vi.fn().mockReturnValue(false),
|
|
162
|
+
getSessionId: vi.fn().mockReturnValue('test-session-id'),
|
|
163
|
+
getProxy: vi.fn().mockReturnValue(undefined),
|
|
164
|
+
getWorkingDir: vi.fn().mockReturnValue('/test/dir'),
|
|
165
|
+
getFileService: vi.fn().mockReturnValue(fileService),
|
|
166
|
+
getMaxSessionTurns: vi.fn().mockReturnValue(0),
|
|
167
|
+
getQuotaErrorOccurred: vi.fn().mockReturnValue(false),
|
|
168
|
+
setQuotaErrorOccurred: vi.fn(),
|
|
169
|
+
getNoBrowser: vi.fn().mockReturnValue(false),
|
|
170
|
+
getIdeMode: vi.fn().mockReturnValue(false),
|
|
171
|
+
getGeminiClient: vi.fn(),
|
|
172
|
+
};
|
|
173
|
+
const MockedConfig = vi.mocked(Config, true);
|
|
174
|
+
MockedConfig.mockImplementation(() => mockConfigObject);
|
|
175
|
+
// We can instantiate the client here since Config is mocked
|
|
176
|
+
// and the constructor will use the mocked GoogleGenAI
|
|
177
|
+
client = new GeminiClient(new Config({}));
|
|
178
|
+
mockConfigObject.getGeminiClient.mockReturnValue(client);
|
|
179
|
+
await client.initialize(contentGeneratorConfig);
|
|
180
|
+
});
|
|
181
|
+
afterEach(() => {
|
|
182
|
+
vi.restoreAllMocks();
|
|
183
|
+
});
|
|
184
|
+
// NOTE: The following tests for startChat were removed due to persistent issues with
|
|
185
|
+
// the @google/genai mock. Specifically, the mockChatCreateFn (representing instance.chats.create)
|
|
186
|
+
// was not being detected as called by the GeminiClient instance.
|
|
187
|
+
// This likely points to a subtle issue in how the GoogleGenerativeAI class constructor
|
|
188
|
+
// and its instance methods are mocked and then used by the class under test.
|
|
189
|
+
// For future debugging, ensure that the `this.client` in `GeminiClient` (which is an
|
|
190
|
+
// instance of the mocked GoogleGenerativeAI) correctly has its `chats.create` method
|
|
191
|
+
// pointing to `mockChatCreateFn`.
|
|
192
|
+
// it('startChat should call getCoreSystemPrompt with userMemory and pass to chats.create', async () => { ... });
|
|
193
|
+
// it('startChat should call getCoreSystemPrompt with empty string if userMemory is empty', async () => { ... });
|
|
194
|
+
// NOTE: The following tests for generateJson were removed due to persistent issues with
|
|
195
|
+
// the @google/genai mock, similar to the startChat tests. The mockGenerateContentFn
|
|
196
|
+
// (representing instance.models.generateContent) was not being detected as called, or the mock
|
|
197
|
+
// was not preventing an actual API call (leading to API key errors).
|
|
198
|
+
// For future debugging, ensure `this.client.models.generateContent` in `GeminiClient` correctly
|
|
199
|
+
// uses the `mockGenerateContentFn`.
|
|
200
|
+
// it('generateJson should call getCoreSystemPrompt with userMemory and pass to generateContent', async () => { ... });
|
|
201
|
+
// it('generateJson should call getCoreSystemPrompt with empty string if userMemory is empty', async () => { ... });
|
|
202
|
+
describe('generateEmbedding', () => {
|
|
203
|
+
const texts = ['hello world', 'goodbye world'];
|
|
204
|
+
const testEmbeddingModel = 'test-embedding-model';
|
|
205
|
+
it('should call embedContent with correct parameters and return embeddings', async () => {
|
|
206
|
+
const mockEmbeddings = [
|
|
207
|
+
[0.1, 0.2, 0.3],
|
|
208
|
+
[0.4, 0.5, 0.6],
|
|
209
|
+
];
|
|
210
|
+
const mockResponse = {
|
|
211
|
+
embeddings: [
|
|
212
|
+
{ values: mockEmbeddings[0] },
|
|
213
|
+
{ values: mockEmbeddings[1] },
|
|
214
|
+
],
|
|
215
|
+
};
|
|
216
|
+
mockEmbedContentFn.mockResolvedValue(mockResponse);
|
|
217
|
+
const result = await client.generateEmbedding(texts);
|
|
218
|
+
expect(mockEmbedContentFn).toHaveBeenCalledTimes(1);
|
|
219
|
+
expect(mockEmbedContentFn).toHaveBeenCalledWith({
|
|
220
|
+
model: testEmbeddingModel,
|
|
221
|
+
contents: texts,
|
|
222
|
+
});
|
|
223
|
+
expect(result).toEqual(mockEmbeddings);
|
|
224
|
+
});
|
|
225
|
+
it('should return an empty array if an empty array is passed', async () => {
|
|
226
|
+
const result = await client.generateEmbedding([]);
|
|
227
|
+
expect(result).toEqual([]);
|
|
228
|
+
expect(mockEmbedContentFn).not.toHaveBeenCalled();
|
|
229
|
+
});
|
|
230
|
+
it('should throw an error if API response has no embeddings array', async () => {
|
|
231
|
+
mockEmbedContentFn.mockResolvedValue({}); // No `embeddings` key
|
|
232
|
+
await expect(client.generateEmbedding(texts)).rejects.toThrow('No embeddings found in API response.');
|
|
233
|
+
});
|
|
234
|
+
it('should throw an error if API response has an empty embeddings array', async () => {
|
|
235
|
+
const mockResponse = {
|
|
236
|
+
embeddings: [],
|
|
237
|
+
};
|
|
238
|
+
mockEmbedContentFn.mockResolvedValue(mockResponse);
|
|
239
|
+
await expect(client.generateEmbedding(texts)).rejects.toThrow('No embeddings found in API response.');
|
|
240
|
+
});
|
|
241
|
+
it('should throw an error if API returns a mismatched number of embeddings', async () => {
|
|
242
|
+
const mockResponse = {
|
|
243
|
+
embeddings: [{ values: [1, 2, 3] }], // Only one for two texts
|
|
244
|
+
};
|
|
245
|
+
mockEmbedContentFn.mockResolvedValue(mockResponse);
|
|
246
|
+
await expect(client.generateEmbedding(texts)).rejects.toThrow('API returned a mismatched number of embeddings. Expected 2, got 1.');
|
|
247
|
+
});
|
|
248
|
+
it('should throw an error if any embedding has nullish values', async () => {
|
|
249
|
+
const mockResponse = {
|
|
250
|
+
embeddings: [{ values: [1, 2, 3] }, { values: undefined }], // Second one is bad
|
|
251
|
+
};
|
|
252
|
+
mockEmbedContentFn.mockResolvedValue(mockResponse);
|
|
253
|
+
await expect(client.generateEmbedding(texts)).rejects.toThrow('API returned an empty embedding for input text at index 1: "goodbye world"');
|
|
254
|
+
});
|
|
255
|
+
it('should throw an error if any embedding has an empty values array', async () => {
|
|
256
|
+
const mockResponse = {
|
|
257
|
+
embeddings: [{ values: [] }, { values: [1, 2, 3] }], // First one is bad
|
|
258
|
+
};
|
|
259
|
+
mockEmbedContentFn.mockResolvedValue(mockResponse);
|
|
260
|
+
await expect(client.generateEmbedding(texts)).rejects.toThrow('API returned an empty embedding for input text at index 0: "hello world"');
|
|
261
|
+
});
|
|
262
|
+
it('should propagate errors from the API call', async () => {
|
|
263
|
+
const apiError = new Error('API Failure');
|
|
264
|
+
mockEmbedContentFn.mockRejectedValue(apiError);
|
|
265
|
+
await expect(client.generateEmbedding(texts)).rejects.toThrow('API Failure');
|
|
266
|
+
});
|
|
267
|
+
});
|
|
268
|
+
describe('generateContent', () => {
|
|
269
|
+
it('should call generateContent with the correct parameters', async () => {
|
|
270
|
+
const contents = [{ role: 'user', parts: [{ text: 'hello' }] }];
|
|
271
|
+
const generationConfig = { temperature: 0.5 };
|
|
272
|
+
const abortSignal = new AbortController().signal;
|
|
273
|
+
// Mock countTokens
|
|
274
|
+
const mockGenerator = {
|
|
275
|
+
countTokens: vi.fn().mockResolvedValue({ totalTokens: 1 }),
|
|
276
|
+
generateContent: mockGenerateContentFn,
|
|
277
|
+
};
|
|
278
|
+
client['contentGenerator'] = mockGenerator;
|
|
279
|
+
await client.generateContent(contents, generationConfig, abortSignal);
|
|
280
|
+
expect(mockGenerateContentFn).toHaveBeenCalledWith({
|
|
281
|
+
model: 'test-model',
|
|
282
|
+
config: {
|
|
283
|
+
abortSignal,
|
|
284
|
+
systemInstruction: getCoreSystemPrompt(''),
|
|
285
|
+
temperature: 0.5,
|
|
286
|
+
topP: 1,
|
|
287
|
+
},
|
|
288
|
+
contents,
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
describe('generateJson', () => {
|
|
293
|
+
it('should call generateContent with the correct parameters', async () => {
|
|
294
|
+
const contents = [{ role: 'user', parts: [{ text: 'hello' }] }];
|
|
295
|
+
const schema = { type: 'string' };
|
|
296
|
+
const abortSignal = new AbortController().signal;
|
|
297
|
+
// Mock countTokens
|
|
298
|
+
const mockGenerator = {
|
|
299
|
+
countTokens: vi.fn().mockResolvedValue({ totalTokens: 1 }),
|
|
300
|
+
generateContent: mockGenerateContentFn,
|
|
301
|
+
};
|
|
302
|
+
client['contentGenerator'] = mockGenerator;
|
|
303
|
+
await client.generateJson(contents, schema, abortSignal);
|
|
304
|
+
expect(mockGenerateContentFn).toHaveBeenCalledWith({
|
|
305
|
+
model: 'test-model', // Should use current model from config
|
|
306
|
+
config: {
|
|
307
|
+
abortSignal,
|
|
308
|
+
systemInstruction: getCoreSystemPrompt(''),
|
|
309
|
+
temperature: 0,
|
|
310
|
+
topP: 1,
|
|
311
|
+
responseSchema: schema,
|
|
312
|
+
responseMimeType: 'application/json',
|
|
313
|
+
},
|
|
314
|
+
contents,
|
|
315
|
+
});
|
|
316
|
+
});
|
|
317
|
+
it('parses JSON wrapped in a markdown code block', async () => {
|
|
318
|
+
const contents = [{ role: 'user', parts: [{ text: 'hello' }] }];
|
|
319
|
+
const schema = {
|
|
320
|
+
type: 'object',
|
|
321
|
+
properties: { foo: { type: 'string' } },
|
|
322
|
+
};
|
|
323
|
+
const abortSignal = new AbortController().signal;
|
|
324
|
+
const mockGenerator = {
|
|
325
|
+
countTokens: vi.fn().mockResolvedValue({ totalTokens: 1 }),
|
|
326
|
+
generateContent: vi.fn().mockResolvedValue({
|
|
327
|
+
candidates: [
|
|
328
|
+
{
|
|
329
|
+
content: {
|
|
330
|
+
parts: [{ text: '```json\n{\n "foo": "bar"\n}\n```' }],
|
|
331
|
+
},
|
|
332
|
+
},
|
|
333
|
+
],
|
|
334
|
+
}),
|
|
335
|
+
};
|
|
336
|
+
client['contentGenerator'] = mockGenerator;
|
|
337
|
+
const result = await client.generateJson(contents, schema, abortSignal);
|
|
338
|
+
expect(result).toEqual({ foo: 'bar' });
|
|
339
|
+
});
|
|
340
|
+
});
|
|
341
|
+
describe('addHistory', () => {
|
|
342
|
+
it('should call chat.addHistory with the provided content', async () => {
|
|
343
|
+
const mockChat = {
|
|
344
|
+
addHistory: vi.fn(),
|
|
345
|
+
};
|
|
346
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
347
|
+
client['chat'] = mockChat;
|
|
348
|
+
const newContent = {
|
|
349
|
+
role: 'user',
|
|
350
|
+
parts: [{ text: 'New history item' }],
|
|
351
|
+
};
|
|
352
|
+
await client.addHistory(newContent);
|
|
353
|
+
expect(mockChat.addHistory).toHaveBeenCalledWith(newContent);
|
|
354
|
+
});
|
|
355
|
+
});
|
|
356
|
+
describe('resetChat', () => {
|
|
357
|
+
it('should create a new chat session, clearing the old history', async () => {
|
|
358
|
+
// 1. Get the initial chat instance and add some history.
|
|
359
|
+
const initialChat = client.getChat();
|
|
360
|
+
const initialHistory = await client.getHistory();
|
|
361
|
+
await client.addHistory({
|
|
362
|
+
role: 'user',
|
|
363
|
+
parts: [{ text: 'some old message' }],
|
|
364
|
+
});
|
|
365
|
+
const historyWithOldMessage = await client.getHistory();
|
|
366
|
+
expect(historyWithOldMessage.length).toBeGreaterThan(initialHistory.length);
|
|
367
|
+
// 2. Call resetChat.
|
|
368
|
+
await client.resetChat();
|
|
369
|
+
// 3. Get the new chat instance and its history.
|
|
370
|
+
const newChat = client.getChat();
|
|
371
|
+
const newHistory = await client.getHistory();
|
|
372
|
+
// 4. Assert that the chat instance is new and the history is reset.
|
|
373
|
+
expect(newChat).not.toBe(initialChat);
|
|
374
|
+
expect(newHistory.length).toBe(initialHistory.length);
|
|
375
|
+
expect(JSON.stringify(newHistory)).not.toContain('some old message');
|
|
376
|
+
});
|
|
377
|
+
});
|
|
378
|
+
describe('tryCompressChat', () => {
|
|
379
|
+
const mockCountTokens = vi.fn();
|
|
380
|
+
const mockSendMessage = vi.fn();
|
|
381
|
+
const mockGetHistory = vi.fn();
|
|
382
|
+
beforeEach(() => {
|
|
383
|
+
vi.mock('./tokenLimits', () => ({
|
|
384
|
+
tokenLimit: vi.fn(),
|
|
385
|
+
}));
|
|
386
|
+
client['contentGenerator'] = {
|
|
387
|
+
countTokens: mockCountTokens,
|
|
388
|
+
};
|
|
389
|
+
client['chat'] = {
|
|
390
|
+
getHistory: mockGetHistory,
|
|
391
|
+
addHistory: vi.fn(),
|
|
392
|
+
setHistory: vi.fn(),
|
|
393
|
+
sendMessage: mockSendMessage,
|
|
394
|
+
};
|
|
395
|
+
});
|
|
396
|
+
it('should not trigger summarization if token count is below threshold', async () => {
|
|
397
|
+
const MOCKED_TOKEN_LIMIT = 1000;
|
|
398
|
+
vi.mocked(tokenLimit).mockReturnValue(MOCKED_TOKEN_LIMIT);
|
|
399
|
+
mockGetHistory.mockReturnValue([
|
|
400
|
+
{ role: 'user', parts: [{ text: '...history...' }] },
|
|
401
|
+
]);
|
|
402
|
+
mockCountTokens.mockResolvedValue({
|
|
403
|
+
totalTokens: MOCKED_TOKEN_LIMIT * 0.699, // TOKEN_THRESHOLD_FOR_SUMMARIZATION = 0.7
|
|
404
|
+
});
|
|
405
|
+
const initialChat = client.getChat();
|
|
406
|
+
const result = await client.tryCompressChat('prompt-id-2');
|
|
407
|
+
const newChat = client.getChat();
|
|
408
|
+
expect(tokenLimit).toHaveBeenCalled();
|
|
409
|
+
expect(result).toBeNull();
|
|
410
|
+
expect(newChat).toBe(initialChat);
|
|
411
|
+
});
|
|
412
|
+
it('should trigger summarization if token count is at threshold', async () => {
|
|
413
|
+
const MOCKED_TOKEN_LIMIT = 1000;
|
|
414
|
+
vi.mocked(tokenLimit).mockReturnValue(MOCKED_TOKEN_LIMIT);
|
|
415
|
+
mockGetHistory.mockReturnValue([
|
|
416
|
+
{ role: 'user', parts: [{ text: '...history...' }] },
|
|
417
|
+
]);
|
|
418
|
+
const originalTokenCount = 1000 * 0.7;
|
|
419
|
+
const newTokenCount = 100;
|
|
420
|
+
mockCountTokens
|
|
421
|
+
.mockResolvedValueOnce({ totalTokens: originalTokenCount }) // First call for the check
|
|
422
|
+
.mockResolvedValueOnce({ totalTokens: newTokenCount }); // Second call for the new history
|
|
423
|
+
// Mock the summary response from the chat
|
|
424
|
+
mockSendMessage.mockResolvedValue({
|
|
425
|
+
role: 'model',
|
|
426
|
+
parts: [{ text: 'This is a summary.' }],
|
|
427
|
+
});
|
|
428
|
+
const initialChat = client.getChat();
|
|
429
|
+
const result = await client.tryCompressChat('prompt-id-3');
|
|
430
|
+
const newChat = client.getChat();
|
|
431
|
+
expect(tokenLimit).toHaveBeenCalled();
|
|
432
|
+
expect(mockSendMessage).toHaveBeenCalled();
|
|
433
|
+
// Assert that summarization happened and returned the correct stats
|
|
434
|
+
expect(result).toEqual({
|
|
435
|
+
originalTokenCount,
|
|
436
|
+
newTokenCount,
|
|
437
|
+
});
|
|
438
|
+
// Assert that the chat was reset
|
|
439
|
+
expect(newChat).not.toBe(initialChat);
|
|
440
|
+
});
|
|
441
|
+
it('should not compress across a function call response', async () => {
|
|
442
|
+
const MOCKED_TOKEN_LIMIT = 1000;
|
|
443
|
+
vi.mocked(tokenLimit).mockReturnValue(MOCKED_TOKEN_LIMIT);
|
|
444
|
+
mockGetHistory.mockReturnValue([
|
|
445
|
+
{ role: 'user', parts: [{ text: '...history 1...' }] },
|
|
446
|
+
{ role: 'model', parts: [{ text: '...history 2...' }] },
|
|
447
|
+
{ role: 'user', parts: [{ text: '...history 3...' }] },
|
|
448
|
+
{ role: 'model', parts: [{ text: '...history 4...' }] },
|
|
449
|
+
{ role: 'user', parts: [{ text: '...history 5...' }] },
|
|
450
|
+
{ role: 'model', parts: [{ text: '...history 6...' }] },
|
|
451
|
+
{ role: 'user', parts: [{ text: '...history 7...' }] },
|
|
452
|
+
{ role: 'model', parts: [{ text: '...history 8...' }] },
|
|
453
|
+
// Normally we would break here, but we have a function response.
|
|
454
|
+
{
|
|
455
|
+
role: 'user',
|
|
456
|
+
parts: [{ functionResponse: { name: '...history 8...' } }],
|
|
457
|
+
},
|
|
458
|
+
{ role: 'model', parts: [{ text: '...history 10...' }] },
|
|
459
|
+
// Instead we will break here.
|
|
460
|
+
{ role: 'user', parts: [{ text: '...history 10...' }] },
|
|
461
|
+
]);
|
|
462
|
+
const originalTokenCount = 1000 * 0.7;
|
|
463
|
+
const newTokenCount = 100;
|
|
464
|
+
mockCountTokens
|
|
465
|
+
.mockResolvedValueOnce({ totalTokens: originalTokenCount }) // First call for the check
|
|
466
|
+
.mockResolvedValueOnce({ totalTokens: newTokenCount }); // Second call for the new history
|
|
467
|
+
// Mock the summary response from the chat
|
|
468
|
+
mockSendMessage.mockResolvedValue({
|
|
469
|
+
role: 'model',
|
|
470
|
+
parts: [{ text: 'This is a summary.' }],
|
|
471
|
+
});
|
|
472
|
+
const initialChat = client.getChat();
|
|
473
|
+
const result = await client.tryCompressChat('prompt-id-3');
|
|
474
|
+
const newChat = client.getChat();
|
|
475
|
+
expect(tokenLimit).toHaveBeenCalled();
|
|
476
|
+
expect(mockSendMessage).toHaveBeenCalled();
|
|
477
|
+
// Assert that summarization happened and returned the correct stats
|
|
478
|
+
expect(result).toEqual({
|
|
479
|
+
originalTokenCount,
|
|
480
|
+
newTokenCount,
|
|
481
|
+
});
|
|
482
|
+
// Assert that the chat was reset
|
|
483
|
+
expect(newChat).not.toBe(initialChat);
|
|
484
|
+
// 1. standard start context message
|
|
485
|
+
// 2. standard canned user start message
|
|
486
|
+
// 3. compressed summary message
|
|
487
|
+
// 4. standard canned user summary message
|
|
488
|
+
// 5. The last user message (not the last 3 because that would start with a function response)
|
|
489
|
+
expect(newChat.getHistory().length).toEqual(5);
|
|
490
|
+
});
|
|
491
|
+
it('should always trigger summarization when force is true, regardless of token count', async () => {
|
|
492
|
+
mockGetHistory.mockReturnValue([
|
|
493
|
+
{ role: 'user', parts: [{ text: '...history...' }] },
|
|
494
|
+
]);
|
|
495
|
+
const originalTokenCount = 10; // Well below threshold
|
|
496
|
+
const newTokenCount = 5;
|
|
497
|
+
mockCountTokens
|
|
498
|
+
.mockResolvedValueOnce({ totalTokens: originalTokenCount })
|
|
499
|
+
.mockResolvedValueOnce({ totalTokens: newTokenCount });
|
|
500
|
+
// Mock the summary response from the chat
|
|
501
|
+
mockSendMessage.mockResolvedValue({
|
|
502
|
+
role: 'model',
|
|
503
|
+
parts: [{ text: 'This is a summary.' }],
|
|
504
|
+
});
|
|
505
|
+
const initialChat = client.getChat();
|
|
506
|
+
const result = await client.tryCompressChat('prompt-id-1', true); // force = true
|
|
507
|
+
const newChat = client.getChat();
|
|
508
|
+
expect(mockSendMessage).toHaveBeenCalled();
|
|
509
|
+
expect(result).toEqual({
|
|
510
|
+
originalTokenCount,
|
|
511
|
+
newTokenCount,
|
|
512
|
+
});
|
|
513
|
+
// Assert that the chat was reset
|
|
514
|
+
expect(newChat).not.toBe(initialChat);
|
|
515
|
+
});
|
|
516
|
+
});
|
|
517
|
+
describe('sendMessageStream', () => {
|
|
518
|
+
it('should include IDE context when ideMode is enabled', async () => {
|
|
519
|
+
// Arrange
|
|
520
|
+
vi.mocked(ideContext.getOpenFilesContext).mockReturnValue({
|
|
521
|
+
activeFile: '/path/to/active/file.ts',
|
|
522
|
+
selectedText: 'hello',
|
|
523
|
+
cursor: { line: 5, character: 10 },
|
|
524
|
+
recentOpenFiles: [
|
|
525
|
+
{ filePath: '/path/to/recent/file1.ts', timestamp: Date.now() },
|
|
526
|
+
{ filePath: '/path/to/recent/file2.ts', timestamp: Date.now() },
|
|
527
|
+
],
|
|
528
|
+
});
|
|
529
|
+
vi.spyOn(client['config'], 'getIdeMode').mockReturnValue(true);
|
|
530
|
+
const mockStream = (async function* () {
|
|
531
|
+
yield { type: 'content', value: 'Hello' };
|
|
532
|
+
})();
|
|
533
|
+
mockTurnRunFn.mockReturnValue(mockStream);
|
|
534
|
+
const mockChat = {
|
|
535
|
+
addHistory: vi.fn(),
|
|
536
|
+
getHistory: vi.fn().mockReturnValue([]),
|
|
537
|
+
};
|
|
538
|
+
client['chat'] = mockChat;
|
|
539
|
+
const mockGenerator = {
|
|
540
|
+
countTokens: vi.fn().mockResolvedValue({ totalTokens: 0 }),
|
|
541
|
+
generateContent: mockGenerateContentFn,
|
|
542
|
+
};
|
|
543
|
+
client['contentGenerator'] = mockGenerator;
|
|
544
|
+
const initialRequest = [{ text: 'Hi' }];
|
|
545
|
+
// Act
|
|
546
|
+
const stream = client.sendMessageStream(initialRequest, new AbortController().signal, 'prompt-id-ide');
|
|
547
|
+
for await (const _ of stream) {
|
|
548
|
+
// consume stream
|
|
549
|
+
}
|
|
550
|
+
// Assert
|
|
551
|
+
expect(ideContext.getOpenFilesContext).toHaveBeenCalled();
|
|
552
|
+
const expectedContext = `
|
|
553
|
+
This is the file that the user was most recently looking at:
|
|
554
|
+
- Path: /path/to/active/file.ts
|
|
555
|
+
This is the cursor position in the file:
|
|
556
|
+
- Cursor Position: Line 5, Character 10
|
|
557
|
+
This is the selected text in the active file:
|
|
558
|
+
- hello
|
|
559
|
+
Here are files the user has recently opened, with the most recent at the top:
|
|
560
|
+
- /path/to/recent/file1.ts
|
|
561
|
+
- /path/to/recent/file2.ts
|
|
562
|
+
`.trim();
|
|
563
|
+
const expectedRequest = [{ text: expectedContext }, ...initialRequest];
|
|
564
|
+
expect(mockTurnRunFn).toHaveBeenCalledWith(expectedRequest, expect.any(Object));
|
|
565
|
+
});
|
|
566
|
+
it('should return the turn instance after the stream is complete', async () => {
|
|
567
|
+
// Arrange
|
|
568
|
+
const mockStream = (async function* () {
|
|
569
|
+
yield { type: 'content', value: 'Hello' };
|
|
570
|
+
})();
|
|
571
|
+
mockTurnRunFn.mockReturnValue(mockStream);
|
|
572
|
+
const mockChat = {
|
|
573
|
+
addHistory: vi.fn(),
|
|
574
|
+
getHistory: vi.fn().mockReturnValue([]),
|
|
575
|
+
};
|
|
576
|
+
client['chat'] = mockChat;
|
|
577
|
+
const mockGenerator = {
|
|
578
|
+
countTokens: vi.fn().mockResolvedValue({ totalTokens: 0 }),
|
|
579
|
+
generateContent: mockGenerateContentFn,
|
|
580
|
+
};
|
|
581
|
+
client['contentGenerator'] = mockGenerator;
|
|
582
|
+
// Act
|
|
583
|
+
const stream = client.sendMessageStream([{ text: 'Hi' }], new AbortController().signal, 'prompt-id-1');
|
|
584
|
+
// Consume the stream manually to get the final return value.
|
|
585
|
+
let finalResult;
|
|
586
|
+
while (true) {
|
|
587
|
+
const result = await stream.next();
|
|
588
|
+
if (result.done) {
|
|
589
|
+
finalResult = result.value;
|
|
590
|
+
break;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
// Assert
|
|
594
|
+
expect(finalResult).toBeInstanceOf(Turn);
|
|
595
|
+
});
|
|
596
|
+
it('should stop infinite loop after MAX_TURNS when nextSpeaker always returns model', async () => {
|
|
597
|
+
// Get the mocked checkNextSpeaker function and configure it to trigger infinite loop
|
|
598
|
+
const { checkNextSpeaker } = await import('../utils/nextSpeakerChecker.js');
|
|
599
|
+
const mockCheckNextSpeaker = vi.mocked(checkNextSpeaker);
|
|
600
|
+
mockCheckNextSpeaker.mockResolvedValue({
|
|
601
|
+
next_speaker: 'model',
|
|
602
|
+
reasoning: 'Test case - always continue',
|
|
603
|
+
});
|
|
604
|
+
// Mock Turn to have no pending tool calls (which would allow nextSpeaker check)
|
|
605
|
+
const mockStream = (async function* () {
|
|
606
|
+
yield { type: 'content', value: 'Continue...' };
|
|
607
|
+
})();
|
|
608
|
+
mockTurnRunFn.mockReturnValue(mockStream);
|
|
609
|
+
const mockChat = {
|
|
610
|
+
addHistory: vi.fn(),
|
|
611
|
+
getHistory: vi.fn().mockReturnValue([]),
|
|
612
|
+
};
|
|
613
|
+
client['chat'] = mockChat;
|
|
614
|
+
const mockGenerator = {
|
|
615
|
+
countTokens: vi.fn().mockResolvedValue({ totalTokens: 0 }),
|
|
616
|
+
generateContent: mockGenerateContentFn,
|
|
617
|
+
};
|
|
618
|
+
client['contentGenerator'] = mockGenerator;
|
|
619
|
+
// Use a signal that never gets aborted
|
|
620
|
+
const abortController = new AbortController();
|
|
621
|
+
const signal = abortController.signal;
|
|
622
|
+
// Act - Start the stream that should loop
|
|
623
|
+
const stream = client.sendMessageStream([{ text: 'Start conversation' }], signal, 'prompt-id-2');
|
|
624
|
+
// Count how many stream events we get
|
|
625
|
+
let eventCount = 0;
|
|
626
|
+
let finalResult;
|
|
627
|
+
// Consume the stream and count iterations
|
|
628
|
+
while (true) {
|
|
629
|
+
const result = await stream.next();
|
|
630
|
+
if (result.done) {
|
|
631
|
+
finalResult = result.value;
|
|
632
|
+
break;
|
|
633
|
+
}
|
|
634
|
+
eventCount++;
|
|
635
|
+
// Safety check to prevent actual infinite loop in test
|
|
636
|
+
if (eventCount > 200) {
|
|
637
|
+
abortController.abort();
|
|
638
|
+
throw new Error('Test exceeded expected event limit - possible actual infinite loop');
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
// Assert
|
|
642
|
+
expect(finalResult).toBeInstanceOf(Turn);
|
|
643
|
+
// Debug: Check how many times checkNextSpeaker was called
|
|
644
|
+
const callCount = mockCheckNextSpeaker.mock.calls.length;
|
|
645
|
+
// If infinite loop protection is working, checkNextSpeaker should be called many times
|
|
646
|
+
// but stop at MAX_TURNS (100). Since each recursive call should trigger checkNextSpeaker,
|
|
647
|
+
// we expect it to be called multiple times before hitting the limit
|
|
648
|
+
expect(mockCheckNextSpeaker).toHaveBeenCalled();
|
|
649
|
+
// The test should demonstrate that the infinite loop protection works:
|
|
650
|
+
// - If checkNextSpeaker is called many times (close to MAX_TURNS), it shows the loop was happening
|
|
651
|
+
// - If it's only called once, the recursive behavior might not be triggered
|
|
652
|
+
if (callCount === 0) {
|
|
653
|
+
throw new Error('checkNextSpeaker was never called - the recursive condition was not met');
|
|
654
|
+
}
|
|
655
|
+
else if (callCount === 1) {
|
|
656
|
+
// This might be expected behavior if the turn has pending tool calls or other conditions prevent recursion
|
|
657
|
+
console.log('checkNextSpeaker called only once - no infinite loop occurred');
|
|
658
|
+
}
|
|
659
|
+
else {
|
|
660
|
+
console.log(`checkNextSpeaker called ${callCount} times - infinite loop protection worked`);
|
|
661
|
+
// If called multiple times, we expect it to be stopped before MAX_TURNS
|
|
662
|
+
expect(callCount).toBeLessThanOrEqual(100); // Should not exceed MAX_TURNS
|
|
663
|
+
}
|
|
664
|
+
// The stream should produce events and eventually terminate
|
|
665
|
+
expect(eventCount).toBeGreaterThanOrEqual(1);
|
|
666
|
+
expect(eventCount).toBeLessThan(200); // Should not exceed our safety limit
|
|
667
|
+
});
|
|
668
|
+
it('should yield MaxSessionTurns and stop when session turn limit is reached', async () => {
|
|
669
|
+
// Arrange
|
|
670
|
+
const MAX_SESSION_TURNS = 5;
|
|
671
|
+
vi.spyOn(client['config'], 'getMaxSessionTurns').mockReturnValue(MAX_SESSION_TURNS);
|
|
672
|
+
const mockStream = (async function* () {
|
|
673
|
+
yield { type: 'content', value: 'Hello' };
|
|
674
|
+
})();
|
|
675
|
+
mockTurnRunFn.mockReturnValue(mockStream);
|
|
676
|
+
const mockChat = {
|
|
677
|
+
addHistory: vi.fn(),
|
|
678
|
+
getHistory: vi.fn().mockReturnValue([]),
|
|
679
|
+
};
|
|
680
|
+
client['chat'] = mockChat;
|
|
681
|
+
const mockGenerator = {
|
|
682
|
+
countTokens: vi.fn().mockResolvedValue({ totalTokens: 0 }),
|
|
683
|
+
generateContent: mockGenerateContentFn,
|
|
684
|
+
};
|
|
685
|
+
client['contentGenerator'] = mockGenerator;
|
|
686
|
+
// Act & Assert
|
|
687
|
+
// Run up to the limit
|
|
688
|
+
for (let i = 0; i < MAX_SESSION_TURNS; i++) {
|
|
689
|
+
const stream = client.sendMessageStream([{ text: 'Hi' }], new AbortController().signal, 'prompt-id-4');
|
|
690
|
+
// consume stream
|
|
691
|
+
for await (const _event of stream) {
|
|
692
|
+
// do nothing
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
// This call should exceed the limit
|
|
696
|
+
const stream = client.sendMessageStream([{ text: 'Hi' }], new AbortController().signal, 'prompt-id-5');
|
|
697
|
+
const events = [];
|
|
698
|
+
for await (const event of stream) {
|
|
699
|
+
events.push(event);
|
|
700
|
+
}
|
|
701
|
+
expect(events).toEqual([{ type: GeminiEventType.MaxSessionTurns }]);
|
|
702
|
+
expect(mockTurnRunFn).toHaveBeenCalledTimes(MAX_SESSION_TURNS);
|
|
703
|
+
});
|
|
704
|
+
it('should respect MAX_TURNS limit even when turns parameter is set to a large value', async () => {
|
|
705
|
+
// This test verifies that the infinite loop protection works even when
|
|
706
|
+
// someone tries to bypass it by calling with a very large turns value
|
|
707
|
+
// Get the mocked checkNextSpeaker function and configure it to trigger infinite loop
|
|
708
|
+
const { checkNextSpeaker } = await import('../utils/nextSpeakerChecker.js');
|
|
709
|
+
const mockCheckNextSpeaker = vi.mocked(checkNextSpeaker);
|
|
710
|
+
mockCheckNextSpeaker.mockResolvedValue({
|
|
711
|
+
next_speaker: 'model',
|
|
712
|
+
reasoning: 'Test case - always continue',
|
|
713
|
+
});
|
|
714
|
+
// Mock Turn to have no pending tool calls (which would allow nextSpeaker check)
|
|
715
|
+
const mockStream = (async function* () {
|
|
716
|
+
yield { type: 'content', value: 'Continue...' };
|
|
717
|
+
})();
|
|
718
|
+
mockTurnRunFn.mockReturnValue(mockStream);
|
|
719
|
+
const mockChat = {
|
|
720
|
+
addHistory: vi.fn(),
|
|
721
|
+
getHistory: vi.fn().mockReturnValue([]),
|
|
722
|
+
};
|
|
723
|
+
client['chat'] = mockChat;
|
|
724
|
+
const mockGenerator = {
|
|
725
|
+
countTokens: vi.fn().mockResolvedValue({ totalTokens: 0 }),
|
|
726
|
+
generateContent: mockGenerateContentFn,
|
|
727
|
+
};
|
|
728
|
+
client['contentGenerator'] = mockGenerator;
|
|
729
|
+
// Use a signal that never gets aborted
|
|
730
|
+
const abortController = new AbortController();
|
|
731
|
+
const signal = abortController.signal;
|
|
732
|
+
// Act - Start the stream with an extremely high turns value
|
|
733
|
+
// This simulates a case where the turns protection is bypassed
|
|
734
|
+
const stream = client.sendMessageStream([{ text: 'Start conversation' }], signal, 'prompt-id-3', Number.MAX_SAFE_INTEGER);
|
|
735
|
+
// Count how many stream events we get
|
|
736
|
+
let eventCount = 0;
|
|
737
|
+
const maxTestIterations = 1000; // Higher limit to show the loop continues
|
|
738
|
+
// Consume the stream and count iterations
|
|
739
|
+
try {
|
|
740
|
+
while (true) {
|
|
741
|
+
const result = await stream.next();
|
|
742
|
+
if (result.done) {
|
|
743
|
+
break;
|
|
744
|
+
}
|
|
745
|
+
eventCount++;
|
|
746
|
+
// This test should hit this limit, demonstrating the infinite loop
|
|
747
|
+
if (eventCount > maxTestIterations) {
|
|
748
|
+
abortController.abort();
|
|
749
|
+
// This is the expected behavior - we hit the infinite loop
|
|
750
|
+
break;
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
catch (error) {
|
|
755
|
+
// If the test framework times out, that also demonstrates the infinite loop
|
|
756
|
+
console.error('Test timed out or errored:', error);
|
|
757
|
+
}
|
|
758
|
+
// Assert that the fix works - the loop should stop at MAX_TURNS
|
|
759
|
+
const callCount = mockCheckNextSpeaker.mock.calls.length;
|
|
760
|
+
// With the fix: even when turns is set to a very high value,
|
|
761
|
+
// the loop should stop at MAX_TURNS (100)
|
|
762
|
+
expect(callCount).toBeLessThanOrEqual(100); // Should not exceed MAX_TURNS
|
|
763
|
+
expect(eventCount).toBeLessThanOrEqual(200); // Should have reasonable number of events
|
|
764
|
+
console.log(`Infinite loop protection working: checkNextSpeaker called ${callCount} times, ` +
|
|
765
|
+
`${eventCount} events generated (properly bounded by MAX_TURNS)`);
|
|
766
|
+
});
|
|
767
|
+
});
|
|
768
|
+
describe('generateContent', () => {
|
|
769
|
+
it('should use current model from config for content generation', async () => {
|
|
770
|
+
const initialModel = client['config'].getModel();
|
|
771
|
+
const contents = [{ role: 'user', parts: [{ text: 'test' }] }];
|
|
772
|
+
const currentModel = initialModel + '-changed';
|
|
773
|
+
vi.spyOn(client['config'], 'getModel').mockReturnValueOnce(currentModel);
|
|
774
|
+
const mockGenerator = {
|
|
775
|
+
countTokens: vi.fn().mockResolvedValue({ totalTokens: 1 }),
|
|
776
|
+
generateContent: mockGenerateContentFn,
|
|
777
|
+
};
|
|
778
|
+
client['contentGenerator'] = mockGenerator;
|
|
779
|
+
await client.generateContent(contents, {}, new AbortController().signal);
|
|
780
|
+
expect(mockGenerateContentFn).not.toHaveBeenCalledWith({
|
|
781
|
+
model: initialModel,
|
|
782
|
+
config: expect.any(Object),
|
|
783
|
+
contents,
|
|
784
|
+
});
|
|
785
|
+
expect(mockGenerateContentFn).toHaveBeenCalledWith({
|
|
786
|
+
model: currentModel,
|
|
787
|
+
config: expect.any(Object),
|
|
788
|
+
contents,
|
|
789
|
+
});
|
|
790
|
+
});
|
|
791
|
+
});
|
|
792
|
+
describe('tryCompressChat', () => {
|
|
793
|
+
it('should use current model from config for token counting after sendMessage', async () => {
|
|
794
|
+
const initialModel = client['config'].getModel();
|
|
795
|
+
const mockCountTokens = vi
|
|
796
|
+
.fn()
|
|
797
|
+
.mockResolvedValueOnce({ totalTokens: 100000 })
|
|
798
|
+
.mockResolvedValueOnce({ totalTokens: 5000 });
|
|
799
|
+
const mockSendMessage = vi.fn().mockResolvedValue({ text: 'Summary' });
|
|
800
|
+
const mockChatHistory = [
|
|
801
|
+
{ role: 'user', parts: [{ text: 'Long conversation' }] },
|
|
802
|
+
{ role: 'model', parts: [{ text: 'Long response' }] },
|
|
803
|
+
];
|
|
804
|
+
const mockChat = {
|
|
805
|
+
getHistory: vi.fn().mockReturnValue(mockChatHistory),
|
|
806
|
+
setHistory: vi.fn(),
|
|
807
|
+
sendMessage: mockSendMessage,
|
|
808
|
+
};
|
|
809
|
+
const mockGenerator = {
|
|
810
|
+
countTokens: mockCountTokens,
|
|
811
|
+
};
|
|
812
|
+
// mock the model has been changed between calls of `countTokens`
|
|
813
|
+
const firstCurrentModel = initialModel + '-changed-1';
|
|
814
|
+
const secondCurrentModel = initialModel + '-changed-2';
|
|
815
|
+
vi.spyOn(client['config'], 'getModel')
|
|
816
|
+
.mockReturnValueOnce(firstCurrentModel)
|
|
817
|
+
.mockReturnValueOnce(secondCurrentModel);
|
|
818
|
+
client['chat'] = mockChat;
|
|
819
|
+
client['contentGenerator'] = mockGenerator;
|
|
820
|
+
client['startChat'] = vi.fn().mockResolvedValue(mockChat);
|
|
821
|
+
const result = await client.tryCompressChat('prompt-id-4', true);
|
|
822
|
+
expect(mockCountTokens).toHaveBeenCalledTimes(2);
|
|
823
|
+
expect(mockCountTokens).toHaveBeenNthCalledWith(1, {
|
|
824
|
+
model: firstCurrentModel,
|
|
825
|
+
contents: mockChatHistory,
|
|
826
|
+
});
|
|
827
|
+
expect(mockCountTokens).toHaveBeenNthCalledWith(2, {
|
|
828
|
+
model: secondCurrentModel,
|
|
829
|
+
contents: expect.any(Array),
|
|
830
|
+
});
|
|
831
|
+
expect(result).toEqual({
|
|
832
|
+
originalTokenCount: 100000,
|
|
833
|
+
newTokenCount: 5000,
|
|
834
|
+
});
|
|
835
|
+
});
|
|
836
|
+
});
|
|
837
|
+
describe('handleFlashFallback', () => {
|
|
838
|
+
it('should use current model from config when checking for fallback', async () => {
|
|
839
|
+
const initialModel = client['config'].getModel();
|
|
840
|
+
const fallbackModel = DEFAULT_GEMINI_FLASH_MODEL;
|
|
841
|
+
// mock config been changed
|
|
842
|
+
const currentModel = initialModel + '-changed';
|
|
843
|
+
vi.spyOn(client['config'], 'getModel').mockReturnValueOnce(currentModel);
|
|
844
|
+
const mockFallbackHandler = vi.fn().mockResolvedValue(true);
|
|
845
|
+
client['config'].flashFallbackHandler = mockFallbackHandler;
|
|
846
|
+
client['config'].setModel = vi.fn();
|
|
847
|
+
const result = await client['handleFlashFallback'](AuthType.LOGIN_WITH_GOOGLE);
|
|
848
|
+
expect(result).toBe(fallbackModel);
|
|
849
|
+
expect(mockFallbackHandler).toHaveBeenCalledWith(currentModel, fallbackModel, undefined);
|
|
850
|
+
});
|
|
851
|
+
});
|
|
852
|
+
});
|
|
853
|
+
//# sourceMappingURL=client.test.js.map
|