@blackbox_ai/blackbox-cli-core 0.0.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.
- package/LICENSE +203 -0
- package/README.md +420 -0
- package/dist/.last_build +0 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +14 -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/blackbox/blackboxContentGenerator.d.ts +70 -0
- package/dist/src/blackbox/blackboxContentGenerator.js +180 -0
- package/dist/src/blackbox/blackboxContentGenerator.js.map +1 -0
- package/dist/src/blackbox/blackboxContentGenerator.test.d.ts +6 -0
- package/dist/src/blackbox/blackboxContentGenerator.test.js +1178 -0
- package/dist/src/blackbox/blackboxContentGenerator.test.js.map +1 -0
- package/dist/src/blackbox/blackboxOAuth2.d.ts +191 -0
- package/dist/src/blackbox/blackboxOAuth2.js +566 -0
- package/dist/src/blackbox/blackboxOAuth2.js.map +1 -0
- package/dist/src/blackbox/blackboxOAuth2.test.d.ts +6 -0
- package/dist/src/blackbox/blackboxOAuth2.test.js +1718 -0
- package/dist/src/blackbox/blackboxOAuth2.test.js.map +1 -0
- package/dist/src/blackbox/sharedTokenManager.d.ts +196 -0
- package/dist/src/blackbox/sharedTokenManager.js +647 -0
- package/dist/src/blackbox/sharedTokenManager.js.map +1 -0
- package/dist/src/blackbox/sharedTokenManager.test.d.ts +7 -0
- package/dist/src/blackbox/sharedTokenManager.test.js +662 -0
- package/dist/src/blackbox/sharedTokenManager.test.js.map +1 -0
- package/dist/src/code_assist/codeAssist.d.ts +10 -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 +72 -0
- package/dist/src/code_assist/converter.js +159 -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 +362 -0
- package/dist/src/code_assist/converter.test.js.map +1 -0
- package/dist/src/code_assist/oauth2.d.ts +22 -0
- package/dist/src/code_assist/oauth2.js +368 -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 +436 -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 +134 -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 +101 -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 +171 -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/blackboxModels.d.ts +28 -0
- package/dist/src/config/blackboxModels.js +148 -0
- package/dist/src/config/blackboxModels.js.map +1 -0
- package/dist/src/config/config.d.ts +381 -0
- package/dist/src/config/config.js +753 -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 +674 -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 +87 -0
- package/dist/src/config/flashFallback.test.js.map +1 -0
- package/dist/src/config/models.d.ts +12 -0
- package/dist/src/config/models.js +13 -0
- package/dist/src/config/models.js.map +1 -0
- package/dist/src/config/storage.d.ts +32 -0
- package/dist/src/config/storage.js +90 -0
- package/dist/src/config/storage.js.map +1 -0
- package/dist/src/config/storage.test.d.ts +6 -0
- package/dist/src/config/storage.test.js +43 -0
- package/dist/src/config/storage.test.js.map +1 -0
- package/dist/src/core/__tests__/openaiTimeoutHandling.test.d.ts +6 -0
- package/dist/src/core/__tests__/openaiTimeoutHandling.test.js +292 -0
- package/dist/src/core/__tests__/openaiTimeoutHandling.test.js.map +1 -0
- package/dist/src/core/__tests__/orphanedToolCallsTest.d.ts +64 -0
- package/dist/src/core/__tests__/orphanedToolCallsTest.js +122 -0
- package/dist/src/core/__tests__/orphanedToolCallsTest.js.map +1 -0
- package/dist/src/core/client.d.ts +75 -0
- package/dist/src/core/client.js +826 -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 +2059 -0
- package/dist/src/core/client.test.js.map +1 -0
- package/dist/src/core/contentGenerator.d.ts +51 -0
- package/dist/src/core/contentGenerator.js +149 -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 +133 -0
- package/dist/src/core/contentGenerator.test.js.map +1 -0
- package/dist/src/core/coreToolScheduler.d.ts +126 -0
- package/dist/src/core/coreToolScheduler.js +630 -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 +1228 -0
- package/dist/src/core/coreToolScheduler.test.js.map +1 -0
- package/dist/src/core/geminiChat.d.ts +139 -0
- package/dist/src/core/geminiChat.js +636 -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 +1178 -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 +11 -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 +73 -0
- package/dist/src/core/geminiRequest.test.js.map +1 -0
- package/dist/src/core/logger.d.ts +68 -0
- package/dist/src/core/logger.js +370 -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 +591 -0
- package/dist/src/core/logger.test.js.map +1 -0
- package/dist/src/core/loggingContentGenerator.d.ts +25 -0
- package/dist/src/core/loggingContentGenerator.js +97 -0
- package/dist/src/core/loggingContentGenerator.js.map +1 -0
- package/dist/src/core/nonInteractiveToolExecutor.d.ts +10 -0
- package/dist/src/core/nonInteractiveToolExecutor.js +24 -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 +236 -0
- package/dist/src/core/nonInteractiveToolExecutor.test.js.map +1 -0
- package/dist/src/core/openaiContentGenerator/constants.d.ts +2 -0
- package/dist/src/core/openaiContentGenerator/constants.js +3 -0
- package/dist/src/core/openaiContentGenerator/constants.js.map +1 -0
- package/dist/src/core/openaiContentGenerator/converter.d.ts +107 -0
- package/dist/src/core/openaiContentGenerator/converter.js +802 -0
- package/dist/src/core/openaiContentGenerator/converter.js.map +1 -0
- package/dist/src/core/openaiContentGenerator/converter.test.d.ts +6 -0
- package/dist/src/core/openaiContentGenerator/converter.test.js +47 -0
- package/dist/src/core/openaiContentGenerator/converter.test.js.map +1 -0
- package/dist/src/core/openaiContentGenerator/errorHandler.d.ts +20 -0
- package/dist/src/core/openaiContentGenerator/errorHandler.js +82 -0
- package/dist/src/core/openaiContentGenerator/errorHandler.js.map +1 -0
- package/dist/src/core/openaiContentGenerator/errorHandler.test.d.ts +6 -0
- package/dist/src/core/openaiContentGenerator/errorHandler.test.js +287 -0
- package/dist/src/core/openaiContentGenerator/errorHandler.test.js.map +1 -0
- package/dist/src/core/openaiContentGenerator/index.d.ts +22 -0
- package/dist/src/core/openaiContentGenerator/index.js +41 -0
- package/dist/src/core/openaiContentGenerator/index.js.map +1 -0
- package/dist/src/core/openaiContentGenerator/openaiContentGenerator.d.ts +21 -0
- package/dist/src/core/openaiContentGenerator/openaiContentGenerator.js +105 -0
- package/dist/src/core/openaiContentGenerator/openaiContentGenerator.js.map +1 -0
- package/dist/src/core/openaiContentGenerator/openaiContentGenerator.test.d.ts +6 -0
- package/dist/src/core/openaiContentGenerator/openaiContentGenerator.test.js +230 -0
- package/dist/src/core/openaiContentGenerator/openaiContentGenerator.test.js.map +1 -0
- package/dist/src/core/openaiContentGenerator/pipeline.d.ts +66 -0
- package/dist/src/core/openaiContentGenerator/pipeline.js +256 -0
- package/dist/src/core/openaiContentGenerator/pipeline.js.map +1 -0
- package/dist/src/core/openaiContentGenerator/pipeline.test.d.ts +6 -0
- package/dist/src/core/openaiContentGenerator/pipeline.test.js +977 -0
- package/dist/src/core/openaiContentGenerator/pipeline.test.js.map +1 -0
- package/dist/src/core/openaiContentGenerator/provider/README.md +61 -0
- package/dist/src/core/openaiContentGenerator/provider/dashscope.d.ts +63 -0
- package/dist/src/core/openaiContentGenerator/provider/dashscope.js +241 -0
- package/dist/src/core/openaiContentGenerator/provider/dashscope.js.map +1 -0
- package/dist/src/core/openaiContentGenerator/provider/dashscope.test.d.ts +6 -0
- package/dist/src/core/openaiContentGenerator/provider/dashscope.test.js +707 -0
- package/dist/src/core/openaiContentGenerator/provider/dashscope.test.js.map +1 -0
- package/dist/src/core/openaiContentGenerator/provider/deepseek.d.ts +14 -0
- package/dist/src/core/openaiContentGenerator/provider/deepseek.js +52 -0
- package/dist/src/core/openaiContentGenerator/provider/deepseek.js.map +1 -0
- package/dist/src/core/openaiContentGenerator/provider/deepseek.test.d.ts +6 -0
- package/dist/src/core/openaiContentGenerator/provider/deepseek.test.js +103 -0
- package/dist/src/core/openaiContentGenerator/provider/deepseek.test.js.map +1 -0
- package/dist/src/core/openaiContentGenerator/provider/default.d.ts +15 -0
- package/dist/src/core/openaiContentGenerator/provider/default.js +38 -0
- package/dist/src/core/openaiContentGenerator/provider/default.js.map +1 -0
- package/dist/src/core/openaiContentGenerator/provider/default.test.d.ts +6 -0
- package/dist/src/core/openaiContentGenerator/provider/default.test.js +176 -0
- package/dist/src/core/openaiContentGenerator/provider/default.test.js.map +1 -0
- package/dist/src/core/openaiContentGenerator/provider/index.d.ts +5 -0
- package/dist/src/core/openaiContentGenerator/provider/index.js +5 -0
- package/dist/src/core/openaiContentGenerator/provider/index.js.map +1 -0
- package/dist/src/core/openaiContentGenerator/provider/openrouter.d.ts +8 -0
- package/dist/src/core/openaiContentGenerator/provider/openrouter.js +21 -0
- package/dist/src/core/openaiContentGenerator/provider/openrouter.js.map +1 -0
- package/dist/src/core/openaiContentGenerator/provider/openrouter.test.d.ts +6 -0
- package/dist/src/core/openaiContentGenerator/provider/openrouter.test.js +152 -0
- package/dist/src/core/openaiContentGenerator/provider/openrouter.test.js.map +1 -0
- package/dist/src/core/openaiContentGenerator/provider/types.d.ts +23 -0
- package/dist/src/core/openaiContentGenerator/provider/types.js +2 -0
- package/dist/src/core/openaiContentGenerator/provider/types.js.map +1 -0
- package/dist/src/core/openaiContentGenerator/streamingToolCallParser.d.ts +142 -0
- package/dist/src/core/openaiContentGenerator/streamingToolCallParser.js +363 -0
- package/dist/src/core/openaiContentGenerator/streamingToolCallParser.js.map +1 -0
- package/dist/src/core/openaiContentGenerator/streamingToolCallParser.test.d.ts +6 -0
- package/dist/src/core/openaiContentGenerator/streamingToolCallParser.test.js +537 -0
- package/dist/src/core/openaiContentGenerator/streamingToolCallParser.test.js.map +1 -0
- package/dist/src/core/openaiContentGenerator/telemetryService.d.ts +35 -0
- package/dist/src/core/openaiContentGenerator/telemetryService.js +146 -0
- package/dist/src/core/openaiContentGenerator/telemetryService.js.map +1 -0
- package/dist/src/core/openaiContentGenerator/telemetryService.test.d.ts +6 -0
- package/dist/src/core/openaiContentGenerator/telemetryService.test.js +978 -0
- package/dist/src/core/openaiContentGenerator/telemetryService.test.js.map +1 -0
- package/dist/src/core/prompts.d.ts +76 -0
- package/dist/src/core/prompts.js +860 -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 +436 -0
- package/dist/src/core/prompts.test.js.map +1 -0
- package/dist/src/core/tokenLimits.d.ts +25 -0
- package/dist/src/core/tokenLimits.js +175 -0
- package/dist/src/core/tokenLimits.js.map +1 -0
- package/dist/src/core/tokenLimits.test.d.ts +1 -0
- package/dist/src/core/tokenLimits.test.js +261 -0
- package/dist/src/core/tokenLimits.test.js.map +1 -0
- package/dist/src/core/turn.d.ts +140 -0
- package/dist/src/core/turn.js +168 -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 +450 -0
- package/dist/src/core/turn.test.js.map +1 -0
- package/dist/src/generated/git-commit.d.ts +7 -0
- package/dist/src/generated/git-commit.js +10 -0
- package/dist/src/generated/git-commit.js.map +1 -0
- package/dist/src/ide/constants.d.ts +6 -0
- package/dist/src/ide/constants.js +7 -0
- package/dist/src/ide/constants.js.map +1 -0
- package/dist/src/ide/detect-ide.d.ts +25 -0
- package/dist/src/ide/detect-ide.js +104 -0
- package/dist/src/ide/detect-ide.js.map +1 -0
- package/dist/src/ide/detect-ide.test.d.ts +6 -0
- package/dist/src/ide/detect-ide.test.js +120 -0
- package/dist/src/ide/detect-ide.test.js.map +1 -0
- package/dist/src/ide/ide-client.d.ts +67 -0
- package/dist/src/ide/ide-client.js +424 -0
- package/dist/src/ide/ide-client.js.map +1 -0
- package/dist/src/ide/ide-client.test.d.ts +6 -0
- package/dist/src/ide/ide-client.test.js +165 -0
- package/dist/src/ide/ide-client.test.js.map +1 -0
- package/dist/src/ide/ide-installer.d.ts +14 -0
- package/dist/src/ide/ide-installer.js +107 -0
- package/dist/src/ide/ide-installer.js.map +1 -0
- package/dist/src/ide/ide-installer.test.d.ts +6 -0
- package/dist/src/ide/ide-installer.test.js +113 -0
- package/dist/src/ide/ide-installer.test.js.map +1 -0
- package/dist/src/ide/ideContext.d.ts +374 -0
- package/dist/src/ide/ideContext.js +147 -0
- package/dist/src/ide/ideContext.js.map +1 -0
- package/dist/src/ide/ideContext.test.d.ts +6 -0
- package/dist/src/ide/ideContext.test.js +265 -0
- package/dist/src/ide/ideContext.test.js.map +1 -0
- package/dist/src/ide/process-utils.d.ts +21 -0
- package/dist/src/ide/process-utils.js +164 -0
- package/dist/src/ide/process-utils.js.map +1 -0
- package/dist/src/ide/process-utils.test.d.ts +6 -0
- package/dist/src/ide/process-utils.test.js +158 -0
- package/dist/src/ide/process-utils.test.js.map +1 -0
- package/dist/src/index.d.ts +86 -0
- package/dist/src/index.js +97 -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/google-auth-provider.d.ts +23 -0
- package/dist/src/mcp/google-auth-provider.js +72 -0
- package/dist/src/mcp/google-auth-provider.js.map +1 -0
- package/dist/src/mcp/google-auth-provider.test.d.ts +6 -0
- package/dist/src/mcp/google-auth-provider.test.js +89 -0
- package/dist/src/mcp/google-auth-provider.test.js.map +1 -0
- package/dist/src/mcp/oauth-provider.d.ts +146 -0
- package/dist/src/mcp/oauth-provider.js +601 -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 +672 -0
- package/dist/src/mcp/oauth-provider.test.js.map +1 -0
- package/dist/src/mcp/oauth-token-storage.d.ts +61 -0
- package/dist/src/mcp/oauth-token-storage.js +148 -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 +208 -0
- package/dist/src/mcp/oauth-token-storage.test.js.map +1 -0
- package/dist/src/mcp/oauth-utils.d.ts +119 -0
- package/dist/src/mcp/oauth-utils.js +235 -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 +199 -0
- package/dist/src/mcp/oauth-utils.test.js.map +1 -0
- package/dist/src/mcp/token-storage/base-token-storage.d.ts +19 -0
- package/dist/src/mcp/token-storage/base-token-storage.js +36 -0
- package/dist/src/mcp/token-storage/base-token-storage.js.map +1 -0
- package/dist/src/mcp/token-storage/base-token-storage.test.d.ts +6 -0
- package/dist/src/mcp/token-storage/base-token-storage.test.js +160 -0
- package/dist/src/mcp/token-storage/base-token-storage.test.js.map +1 -0
- package/dist/src/mcp/token-storage/types.d.ts +34 -0
- package/dist/src/mcp/token-storage/types.js +7 -0
- package/dist/src/mcp/token-storage/types.js.map +1 -0
- package/dist/src/mocks/msw.d.ts +6 -0
- package/dist/src/mocks/msw.js +8 -0
- package/dist/src/mocks/msw.js.map +1 -0
- package/dist/src/prompts/mcp-prompts.d.ts +8 -0
- package/dist/src/prompts/mcp-prompts.js +13 -0
- package/dist/src/prompts/mcp-prompts.js.map +1 -0
- package/dist/src/prompts/prompt-registry.d.ts +34 -0
- package/dist/src/prompts/prompt-registry.js +63 -0
- package/dist/src/prompts/prompt-registry.js.map +1 -0
- package/dist/src/services/chatRecordingService.d.ts +150 -0
- package/dist/src/services/chatRecordingService.js +321 -0
- package/dist/src/services/chatRecordingService.js.map +1 -0
- package/dist/src/services/chatRecordingService.test.d.ts +6 -0
- package/dist/src/services/chatRecordingService.test.js +290 -0
- package/dist/src/services/chatRecordingService.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 +143 -0
- package/dist/src/services/fileDiscoveryService.test.js.map +1 -0
- package/dist/src/services/fileSystemService.d.ts +31 -0
- package/dist/src/services/fileSystemService.js +18 -0
- package/dist/src/services/fileSystemService.js.map +1 -0
- package/dist/src/services/fileSystemService.test.d.ts +6 -0
- package/dist/src/services/fileSystemService.test.js +41 -0
- package/dist/src/services/fileSystemService.test.js.map +1 -0
- package/dist/src/services/gitService.d.ts +23 -0
- package/dist/src/services/gitService.js +110 -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 +212 -0
- package/dist/src/services/gitService.test.js.map +1 -0
- package/dist/src/services/loopDetectionService.d.ts +98 -0
- package/dist/src/services/loopDetectionService.js +363 -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 +560 -0
- package/dist/src/services/loopDetectionService.test.js.map +1 -0
- package/dist/src/services/shellExecutionService.d.ts +68 -0
- package/dist/src/services/shellExecutionService.js +332 -0
- package/dist/src/services/shellExecutionService.js.map +1 -0
- package/dist/src/services/shellExecutionService.test.d.ts +6 -0
- package/dist/src/services/shellExecutionService.test.js +517 -0
- package/dist/src/services/shellExecutionService.test.js.map +1 -0
- package/dist/src/subagents/builtin-agents.d.ts +35 -0
- package/dist/src/subagents/builtin-agents.js +85 -0
- package/dist/src/subagents/builtin-agents.js.map +1 -0
- package/dist/src/subagents/builtin-agents.test.d.ts +6 -0
- package/dist/src/subagents/builtin-agents.test.js +78 -0
- package/dist/src/subagents/builtin-agents.test.js.map +1 -0
- package/dist/src/subagents/index.d.ts +29 -0
- package/dist/src/subagents/index.js +15 -0
- package/dist/src/subagents/index.js.map +1 -0
- package/dist/src/subagents/subagent-events.d.ts +93 -0
- package/dist/src/subagents/subagent-events.js +31 -0
- package/dist/src/subagents/subagent-events.js.map +1 -0
- package/dist/src/subagents/subagent-hooks.d.ts +29 -0
- package/dist/src/subagents/subagent-hooks.js +7 -0
- package/dist/src/subagents/subagent-hooks.js.map +1 -0
- package/dist/src/subagents/subagent-manager.d.ts +170 -0
- package/dist/src/subagents/subagent-manager.js +593 -0
- package/dist/src/subagents/subagent-manager.js.map +1 -0
- package/dist/src/subagents/subagent-manager.test.d.ts +6 -0
- package/dist/src/subagents/subagent-manager.test.js +822 -0
- package/dist/src/subagents/subagent-manager.test.js.map +1 -0
- package/dist/src/subagents/subagent-statistics.d.ts +46 -0
- package/dist/src/subagents/subagent-statistics.js +193 -0
- package/dist/src/subagents/subagent-statistics.js.map +1 -0
- package/dist/src/subagents/subagent-statistics.test.d.ts +6 -0
- package/dist/src/subagents/subagent-statistics.test.js +231 -0
- package/dist/src/subagents/subagent-statistics.test.js.map +1 -0
- package/dist/src/subagents/subagent.d.ts +161 -0
- package/dist/src/subagents/subagent.js +667 -0
- package/dist/src/subagents/subagent.js.map +1 -0
- package/dist/src/subagents/subagent.test.d.ts +6 -0
- package/dist/src/subagents/subagent.test.js +487 -0
- package/dist/src/subagents/subagent.test.js.map +1 -0
- package/dist/src/subagents/types.d.ts +218 -0
- package/dist/src/subagents/types.js +58 -0
- package/dist/src/subagents/types.js.map +1 -0
- package/dist/src/subagents/types.test.d.ts +6 -0
- package/dist/src/subagents/types.test.js +31 -0
- package/dist/src/subagents/types.test.js.map +1 -0
- package/dist/src/subagents/validation.d.ts +63 -0
- package/dist/src/subagents/validation.js +293 -0
- package/dist/src/subagents/validation.js.map +1 -0
- package/dist/src/subagents/validation.test.d.ts +6 -0
- package/dist/src/subagents/validation.test.js +330 -0
- package/dist/src/subagents/validation.test.js.map +1 -0
- package/dist/src/telemetry/blackbox-logger/blackbox-logger.d.ts +80 -0
- package/dist/src/telemetry/blackbox-logger/blackbox-logger.js +583 -0
- package/dist/src/telemetry/blackbox-logger/blackbox-logger.js.map +1 -0
- package/dist/src/telemetry/blackbox-logger/blackbox-logger.test.d.ts +6 -0
- package/dist/src/telemetry/blackbox-logger/blackbox-logger.test.js +301 -0
- package/dist/src/telemetry/blackbox-logger/blackbox-logger.test.js.map +1 -0
- package/dist/src/telemetry/blackbox-logger/event-types.d.ts +73 -0
- package/dist/src/telemetry/blackbox-logger/event-types.js +2 -0
- package/dist/src/telemetry/blackbox-logger/event-types.js.map +1 -0
- package/dist/src/telemetry/clearcut-logger/clearcut-logger.d.ts +121 -0
- package/dist/src/telemetry/clearcut-logger/clearcut-logger.js +773 -0
- package/dist/src/telemetry/clearcut-logger/clearcut-logger.js.map +1 -0
- package/dist/src/telemetry/clearcut-logger/clearcut-logger.test.d.ts +17 -0
- package/dist/src/telemetry/clearcut-logger/clearcut-logger.test.js +420 -0
- package/dist/src/telemetry/clearcut-logger/clearcut-logger.test.js.map +1 -0
- package/dist/src/telemetry/clearcut-logger/event-metadata-key.d.ts +90 -0
- package/dist/src/telemetry/clearcut-logger/event-metadata-key.js +229 -0
- package/dist/src/telemetry/clearcut-logger/event-metadata-key.js.map +1 -0
- package/dist/src/telemetry/constants.d.ts +34 -0
- package/dist/src/telemetry/constants.js +35 -0
- package/dist/src/telemetry/constants.js.map +1 -0
- package/dist/src/telemetry/file-exporters.d.ts +29 -0
- package/dist/src/telemetry/file-exporters.js +62 -0
- package/dist/src/telemetry/file-exporters.js.map +1 -0
- package/dist/src/telemetry/index.d.ts +20 -0
- package/dist/src/telemetry/index.js +21 -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 +95 -0
- package/dist/src/telemetry/integration.test.circular.js.map +1 -0
- package/dist/src/telemetry/loggers.d.ts +26 -0
- package/dist/src/telemetry/loggers.js +406 -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 +107 -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 +638 -0
- package/dist/src/telemetry/loggers.test.js.map +1 -0
- package/dist/src/telemetry/metrics.d.ts +40 -0
- package/dist/src/telemetry/metrics.js +229 -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 +242 -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 +161 -0
- package/dist/src/telemetry/sdk.js.map +1 -0
- package/dist/src/telemetry/sdk.test.d.ts +6 -0
- package/dist/src/telemetry/sdk.test.js +82 -0
- package/dist/src/telemetry/sdk.test.js.map +1 -0
- package/dist/src/telemetry/telemetry-utils.d.ts +6 -0
- package/dist/src/telemetry/telemetry-utils.js +14 -0
- package/dist/src/telemetry/telemetry-utils.js.map +1 -0
- package/dist/src/telemetry/telemetry-utils.test.d.ts +6 -0
- package/dist/src/telemetry/telemetry-utils.test.js +40 -0
- package/dist/src/telemetry/telemetry-utils.test.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/tool-call-decision.d.ts +13 -0
- package/dist/src/telemetry/tool-call-decision.js +29 -0
- package/dist/src/telemetry/tool-call-decision.js.map +1 -0
- package/dist/src/telemetry/types.d.ts +236 -0
- package/dist/src/telemetry/types.js +405 -0
- package/dist/src/telemetry/types.js.map +1 -0
- package/dist/src/telemetry/uiTelemetry.d.ts +75 -0
- package/dist/src/telemetry/uiTelemetry.js +153 -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 +558 -0
- package/dist/src/telemetry/uiTelemetry.test.js.map +1 -0
- package/dist/src/test-utils/config.d.ts +17 -0
- package/dist/src/test-utils/config.js +32 -0
- package/dist/src/test-utils/config.js.map +1 -0
- package/dist/src/test-utils/mockWorkspaceContext.d.ts +13 -0
- package/dist/src/test-utils/mockWorkspaceContext.js +24 -0
- package/dist/src/test-utils/mockWorkspaceContext.js.map +1 -0
- package/dist/src/test-utils/tools.d.ts +45 -0
- package/dist/src/test-utils/tools.js +105 -0
- package/dist/src/test-utils/tools.js.map +1 -0
- package/dist/src/tools/diffOptions.d.ts +9 -0
- package/dist/src/tools/diffOptions.js +38 -0
- package/dist/src/tools/diffOptions.js.map +1 -0
- package/dist/src/tools/diffOptions.test.d.ts +6 -0
- package/dist/src/tools/diffOptions.test.js +119 -0
- package/dist/src/tools/diffOptions.test.js.map +1 -0
- package/dist/src/tools/edit.d.ts +56 -0
- package/dist/src/tools/edit.js +434 -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 +569 -0
- package/dist/src/tools/edit.test.js.map +1 -0
- package/dist/src/tools/exitPlanMode.d.ts +28 -0
- package/dist/src/tools/exitPlanMode.js +137 -0
- package/dist/src/tools/exitPlanMode.js.map +1 -0
- package/dist/src/tools/exitPlanMode.test.d.ts +6 -0
- package/dist/src/tools/exitPlanMode.test.js +219 -0
- package/dist/src/tools/exitPlanMode.test.js.map +1 -0
- package/dist/src/tools/glob.d.ts +52 -0
- package/dist/src/tools/glob.js +237 -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 +375 -0
- package/dist/src/tools/glob.test.js.map +1 -0
- package/dist/src/tools/grep.d.ts +51 -0
- package/dist/src/tools/grep.js +566 -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 +356 -0
- package/dist/src/tools/grep.test.js.map +1 -0
- package/dist/src/tools/ls.d.ts +68 -0
- package/dist/src/tools/ls.js +227 -0
- package/dist/src/tools/ls.js.map +1 -0
- package/dist/src/tools/ls.test.d.ts +6 -0
- package/dist/src/tools/ls.test.js +389 -0
- package/dist/src/tools/ls.test.js.map +1 -0
- package/dist/src/tools/mcp-client-manager.d.ts +38 -0
- package/dist/src/tools/mcp-client-manager.js +74 -0
- package/dist/src/tools/mcp-client-manager.js.map +1 -0
- package/dist/src/tools/mcp-client-manager.test.d.ts +6 -0
- package/dist/src/tools/mcp-client-manager.test.js +39 -0
- package/dist/src/tools/mcp-client-manager.test.js.map +1 -0
- package/dist/src/tools/mcp-client.d.ts +199 -0
- package/dist/src/tools/mcp-client.js +995 -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 +454 -0
- package/dist/src/tools/mcp-client.test.js.map +1 -0
- package/dist/src/tools/mcp-tool.d.ts +23 -0
- package/dist/src/tools/mcp-tool.js +240 -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 +576 -0
- package/dist/src/tools/mcp-tool.test.js.map +1 -0
- package/dist/src/tools/memoryTool.d.ts +41 -0
- package/dist/src/tools/memoryTool.js +435 -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 +420 -0
- package/dist/src/tools/memoryTool.test.js.map +1 -0
- package/dist/src/tools/modifiable-tool.d.ts +32 -0
- package/dist/src/tools/modifiable-tool.js +88 -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 +193 -0
- package/dist/src/tools/modifiable-tool.test.js.map +1 -0
- package/dist/src/tools/read-file.d.ts +35 -0
- package/dist/src/tools/read-file.js +128 -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 +311 -0
- package/dist/src/tools/read-file.test.js.map +1 -0
- package/dist/src/tools/read-many-files.d.ts +60 -0
- package/dist/src/tools/read-many-files.js +416 -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 +565 -0
- package/dist/src/tools/read-many-files.test.js.map +1 -0
- package/dist/src/tools/ripGrep.d.ts +47 -0
- package/dist/src/tools/ripGrep.js +375 -0
- package/dist/src/tools/ripGrep.js.map +1 -0
- package/dist/src/tools/ripGrep.test.d.ts +6 -0
- package/dist/src/tools/ripGrep.test.js +874 -0
- package/dist/src/tools/ripGrep.test.js.map +1 -0
- package/dist/src/tools/shell.d.ts +23 -0
- package/dist/src/tools/shell.js +387 -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 +632 -0
- package/dist/src/tools/shell.test.js.map +1 -0
- package/dist/src/tools/task.d.ts +59 -0
- package/dist/src/tools/task.js +412 -0
- package/dist/src/tools/task.js.map +1 -0
- package/dist/src/tools/task.test.d.ts +6 -0
- package/dist/src/tools/task.test.js +369 -0
- package/dist/src/tools/task.test.js.map +1 -0
- package/dist/src/tools/todoWrite.d.ts +42 -0
- package/dist/src/tools/todoWrite.js +385 -0
- package/dist/src/tools/todoWrite.js.map +1 -0
- package/dist/src/tools/todoWrite.test.d.ts +6 -0
- package/dist/src/tools/todoWrite.test.js +222 -0
- package/dist/src/tools/todoWrite.test.js.map +1 -0
- package/dist/src/tools/tool-error.d.ts +43 -0
- package/dist/src/tools/tool-error.js +58 -0
- package/dist/src/tools/tool-error.js.map +1 -0
- package/dist/src/tools/tool-names.d.ts +23 -0
- package/dist/src/tools/tool-names.js +24 -0
- package/dist/src/tools/tool-names.js.map +1 -0
- package/dist/src/tools/tool-registry.d.ts +86 -0
- package/dist/src/tools/tool-registry.js +369 -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 +332 -0
- package/dist/src/tools/tool-registry.test.js.map +1 -0
- package/dist/src/tools/tools.d.ts +316 -0
- package/dist/src/tools/tools.js +251 -0
- package/dist/src/tools/tools.js.map +1 -0
- package/dist/src/tools/tools.test.d.ts +6 -0
- package/dist/src/tools/tools.test.js +205 -0
- package/dist/src/tools/tools.test.js.map +1 -0
- package/dist/src/tools/web-fetch.d.ts +31 -0
- package/dist/src/tools/web-fetch.js +161 -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 +133 -0
- package/dist/src/tools/web-fetch.test.js.map +1 -0
- package/dist/src/tools/web-search.d.ts +40 -0
- package/dist/src/tools/web-search.js +134 -0
- package/dist/src/tools/web-search.js.map +1 -0
- package/dist/src/tools/web-search.test.d.ts +6 -0
- package/dist/src/tools/web-search.test.js +125 -0
- package/dist/src/tools/web-search.test.js.map +1 -0
- package/dist/src/tools/write-file.d.ts +52 -0
- package/dist/src/tools/write-file.js +298 -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 +436 -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 +89 -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 +163 -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 +50 -0
- package/dist/src/utils/browser.js.map +1 -0
- package/dist/src/utils/editor.d.ts +28 -0
- package/dist/src/utils/editor.js +186 -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 +445 -0
- package/dist/src/utils/editor.test.js.map +1 -0
- package/dist/src/utils/environmentContext.d.ts +21 -0
- package/dist/src/utils/environmentContext.js +90 -0
- package/dist/src/utils/environmentContext.js.map +1 -0
- package/dist/src/utils/environmentContext.test.d.ts +6 -0
- package/dist/src/utils/environmentContext.test.js +148 -0
- package/dist/src/utils/environmentContext.test.js.map +1 -0
- package/dist/src/utils/errorParsing.d.ts +8 -0
- package/dist/src/utils/errorParsing.js +93 -0
- package/dist/src/utils/errorParsing.js.map +1 -0
- package/dist/src/utils/errorParsing.test.d.ts +6 -0
- package/dist/src/utils/errorParsing.test.js +172 -0
- package/dist/src/utils/errorParsing.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 +130 -0
- package/dist/src/utils/errorReporting.test.js.map +1 -0
- package/dist/src/utils/errors.d.ts +33 -0
- package/dist/src/utils/errors.js +86 -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 +53 -0
- package/dist/src/utils/fileUtils.js +283 -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 +364 -0
- package/dist/src/utils/fileUtils.test.js.map +1 -0
- package/dist/src/utils/filesearch/crawlCache.d.ts +25 -0
- package/dist/src/utils/filesearch/crawlCache.js +57 -0
- package/dist/src/utils/filesearch/crawlCache.js.map +1 -0
- package/dist/src/utils/filesearch/crawlCache.test.d.ts +6 -0
- package/dist/src/utils/filesearch/crawlCache.test.js +103 -0
- package/dist/src/utils/filesearch/crawlCache.test.js.map +1 -0
- package/dist/src/utils/filesearch/crawler.d.ts +15 -0
- package/dist/src/utils/filesearch/crawler.js +50 -0
- package/dist/src/utils/filesearch/crawler.js.map +1 -0
- package/dist/src/utils/filesearch/crawler.test.d.ts +6 -0
- package/dist/src/utils/filesearch/crawler.test.js +468 -0
- package/dist/src/utils/filesearch/crawler.test.js.map +1 -0
- package/dist/src/utils/filesearch/fileSearch.d.ts +38 -0
- package/dist/src/utils/filesearch/fileSearch.js +191 -0
- package/dist/src/utils/filesearch/fileSearch.js.map +1 -0
- package/dist/src/utils/filesearch/fileSearch.test.d.ts +6 -0
- package/dist/src/utils/filesearch/fileSearch.test.js +642 -0
- package/dist/src/utils/filesearch/fileSearch.test.js.map +1 -0
- package/dist/src/utils/filesearch/ignore.d.ts +42 -0
- package/dist/src/utils/filesearch/ignore.js +106 -0
- package/dist/src/utils/filesearch/ignore.js.map +1 -0
- package/dist/src/utils/filesearch/ignore.test.d.ts +6 -0
- package/dist/src/utils/filesearch/ignore.test.js +144 -0
- package/dist/src/utils/filesearch/ignore.test.js.map +1 -0
- package/dist/src/utils/filesearch/result-cache.d.ts +33 -0
- package/dist/src/utils/filesearch/result-cache.js +59 -0
- package/dist/src/utils/filesearch/result-cache.js.map +1 -0
- package/dist/src/utils/filesearch/result-cache.test.d.ts +6 -0
- package/dist/src/utils/filesearch/result-cache.test.js +46 -0
- package/dist/src/utils/filesearch/result-cache.test.js.map +1 -0
- package/dist/src/utils/flashFallback.integration.test.d.ts +6 -0
- package/dist/src/utils/flashFallback.integration.test.js +119 -0
- package/dist/src/utils/flashFallback.integration.test.js.map +1 -0
- package/dist/src/utils/formatters.d.ts +6 -0
- package/dist/src/utils/formatters.js +16 -0
- package/dist/src/utils/formatters.js.map +1 -0
- package/dist/src/utils/generateContentResponseUtilities.d.ts +13 -0
- package/dist/src/utils/generateContentResponseUtilities.js +80 -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 +235 -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 +282 -0
- package/dist/src/utils/getFolderStructure.test.js.map +1 -0
- package/dist/src/utils/getPty.d.ts +19 -0
- package/dist/src/utils/getPty.js +23 -0
- package/dist/src/utils/getPty.js.map +1 -0
- package/dist/src/utils/gitIgnoreParser.d.ts +20 -0
- package/dist/src/utils/gitIgnoreParser.js +61 -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 +154 -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/ignorePatterns.d.ts +103 -0
- package/dist/src/utils/ignorePatterns.js +220 -0
- package/dist/src/utils/ignorePatterns.js.map +1 -0
- package/dist/src/utils/ignorePatterns.test.d.ts +6 -0
- package/dist/src/utils/ignorePatterns.test.js +250 -0
- package/dist/src/utils/ignorePatterns.test.js.map +1 -0
- package/dist/src/utils/installationManager.d.ts +16 -0
- package/dist/src/utils/installationManager.js +50 -0
- package/dist/src/utils/installationManager.js.map +1 -0
- package/dist/src/utils/installationManager.test.d.ts +6 -0
- package/dist/src/utils/installationManager.test.js +83 -0
- package/dist/src/utils/installationManager.test.js.map +1 -0
- package/dist/src/utils/language-detection.d.ts +6 -0
- package/dist/src/utils/language-detection.js +101 -0
- package/dist/src/utils/language-detection.js.map +1 -0
- package/dist/src/utils/memoryDiscovery.d.ts +15 -0
- package/dist/src/utils/memoryDiscovery.js +271 -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 +219 -0
- package/dist/src/utils/memoryDiscovery.test.js.map +1 -0
- package/dist/src/utils/memoryImportProcessor.d.ts +42 -0
- package/dist/src/utils/memoryImportProcessor.js +296 -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 +573 -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 +91 -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/openaiLogger.d.ts +42 -0
- package/dist/src/utils/openaiLogger.js +123 -0
- package/dist/src/utils/openaiLogger.js.map +1 -0
- package/dist/src/utils/partUtils.d.ts +35 -0
- package/dist/src/utils/partUtils.js +133 -0
- package/dist/src/utils/partUtils.js.map +1 -0
- package/dist/src/utils/partUtils.test.d.ts +6 -0
- package/dist/src/utils/partUtils.test.js +241 -0
- package/dist/src/utils/partUtils.test.js.map +1 -0
- package/dist/src/utils/pathReader.d.ts +17 -0
- package/dist/src/utils/pathReader.js +92 -0
- package/dist/src/utils/pathReader.js.map +1 -0
- package/dist/src/utils/pathReader.test.d.ts +6 -0
- package/dist/src/utils/pathReader.test.js +363 -0
- package/dist/src/utils/pathReader.test.js.map +1 -0
- package/dist/src/utils/paths.d.ts +58 -0
- package/dist/src/utils/paths.js +159 -0
- package/dist/src/utils/paths.js.map +1 -0
- package/dist/src/utils/paths.test.d.ts +6 -0
- package/dist/src/utils/paths.test.js +225 -0
- package/dist/src/utils/paths.test.js.map +1 -0
- package/dist/src/utils/projectSummary.d.ts +22 -0
- package/dist/src/utils/projectSummary.js +86 -0
- package/dist/src/utils/projectSummary.js.map +1 -0
- package/dist/src/utils/quotaErrorDetection.d.ts +20 -0
- package/dist/src/utils/quotaErrorDetection.js +114 -0
- package/dist/src/utils/quotaErrorDetection.js.map +1 -0
- package/dist/src/utils/quotaErrorDetection.test.d.ts +6 -0
- package/dist/src/utils/quotaErrorDetection.test.js +153 -0
- package/dist/src/utils/quotaErrorDetection.test.js.map +1 -0
- package/dist/src/utils/request-tokenizer/imageTokenizer.d.ts +112 -0
- package/dist/src/utils/request-tokenizer/imageTokenizer.js +401 -0
- package/dist/src/utils/request-tokenizer/imageTokenizer.js.map +1 -0
- package/dist/src/utils/request-tokenizer/imageTokenizer.test.d.ts +6 -0
- package/dist/src/utils/request-tokenizer/imageTokenizer.test.js +114 -0
- package/dist/src/utils/request-tokenizer/imageTokenizer.test.js.map +1 -0
- package/dist/src/utils/request-tokenizer/index.d.ts +18 -0
- package/dist/src/utils/request-tokenizer/index.js +30 -0
- package/dist/src/utils/request-tokenizer/index.js.map +1 -0
- package/dist/src/utils/request-tokenizer/requestTokenizer.d.ts +56 -0
- package/dist/src/utils/request-tokenizer/requestTokenizer.js +263 -0
- package/dist/src/utils/request-tokenizer/requestTokenizer.js.map +1 -0
- package/dist/src/utils/request-tokenizer/requestTokenizer.test.d.ts +6 -0
- package/dist/src/utils/request-tokenizer/requestTokenizer.test.js +245 -0
- package/dist/src/utils/request-tokenizer/requestTokenizer.test.js.map +1 -0
- package/dist/src/utils/request-tokenizer/supportedImageFormats.d.ts +30 -0
- package/dist/src/utils/request-tokenizer/supportedImageFormats.js +41 -0
- package/dist/src/utils/request-tokenizer/supportedImageFormats.js.map +1 -0
- package/dist/src/utils/request-tokenizer/textTokenizer.d.ts +29 -0
- package/dist/src/utils/request-tokenizer/textTokenizer.js +88 -0
- package/dist/src/utils/request-tokenizer/textTokenizer.js.map +1 -0
- package/dist/src/utils/request-tokenizer/textTokenizer.test.d.ts +6 -0
- package/dist/src/utils/request-tokenizer/textTokenizer.test.js +253 -0
- package/dist/src/utils/request-tokenizer/textTokenizer.test.js.map +1 -0
- package/dist/src/utils/request-tokenizer/types.d.ts +55 -0
- package/dist/src/utils/request-tokenizer/types.js +7 -0
- package/dist/src/utils/request-tokenizer/types.js.map +1 -0
- package/dist/src/utils/retry.d.ts +30 -0
- package/dist/src/utils/retry.js +288 -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 +452 -0
- package/dist/src/utils/retry.test.js.map +1 -0
- package/dist/src/utils/safeJsonParse.d.ts +15 -0
- package/dist/src/utils/safeJsonParse.js +41 -0
- package/dist/src/utils/safeJsonParse.js.map +1 -0
- package/dist/src/utils/safeJsonParse.test.d.ts +6 -0
- package/dist/src/utils/safeJsonParse.test.js +112 -0
- package/dist/src/utils/safeJsonParse.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 +15 -0
- package/dist/src/utils/schemaValidator.js +57 -0
- package/dist/src/utils/schemaValidator.js.map +1 -0
- package/dist/src/utils/secure-browser-launcher.d.ts +23 -0
- package/dist/src/utils/secure-browser-launcher.js +165 -0
- package/dist/src/utils/secure-browser-launcher.js.map +1 -0
- package/dist/src/utils/secure-browser-launcher.test.d.ts +6 -0
- package/dist/src/utils/secure-browser-launcher.test.js +149 -0
- package/dist/src/utils/secure-browser-launcher.test.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/shell-utils.d.ts +121 -0
- package/dist/src/utils/shell-utils.js +381 -0
- package/dist/src/utils/shell-utils.js.map +1 -0
- package/dist/src/utils/shell-utils.test.d.ts +6 -0
- package/dist/src/utils/shell-utils.test.js +346 -0
- package/dist/src/utils/shell-utils.test.js.map +1 -0
- package/dist/src/utils/shellReadOnlyChecker.d.ts +6 -0
- package/dist/src/utils/shellReadOnlyChecker.js +247 -0
- package/dist/src/utils/shellReadOnlyChecker.js.map +1 -0
- package/dist/src/utils/shellReadOnlyChecker.test.d.ts +6 -0
- package/dist/src/utils/shellReadOnlyChecker.test.js +47 -0
- package/dist/src/utils/shellReadOnlyChecker.test.js.map +1 -0
- package/dist/src/utils/subagentGenerator.d.ts +20 -0
- package/dist/src/utils/subagentGenerator.js +116 -0
- package/dist/src/utils/subagentGenerator.js.map +1 -0
- package/dist/src/utils/subagentGenerator.test.d.ts +6 -0
- package/dist/src/utils/subagentGenerator.test.js +158 -0
- package/dist/src/utils/subagentGenerator.test.js.map +1 -0
- package/dist/src/utils/summarizer.d.ts +25 -0
- package/dist/src/utils/summarizer.js +51 -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/textUtils.d.ts +13 -0
- package/dist/src/utils/textUtils.js +28 -0
- package/dist/src/utils/textUtils.js.map +1 -0
- package/dist/src/utils/tool-utils.d.ts +19 -0
- package/dist/src/utils/tool-utils.js +58 -0
- package/dist/src/utils/tool-utils.js.map +1 -0
- package/dist/src/utils/tool-utils.test.d.ts +6 -0
- package/dist/src/utils/tool-utils.test.js +61 -0
- package/dist/src/utils/tool-utils.test.js.map +1 -0
- package/dist/src/utils/userAccountManager.d.ts +20 -0
- package/dist/src/utils/userAccountManager.js +114 -0
- package/dist/src/utils/userAccountManager.js.map +1 -0
- package/dist/src/utils/userAccountManager.test.d.ts +6 -0
- package/dist/src/utils/userAccountManager.test.js +223 -0
- package/dist/src/utils/userAccountManager.test.js.map +1 -0
- package/dist/src/utils/workspaceContext.d.ts +66 -0
- package/dist/src/utils/workspaceContext.js +171 -0
- package/dist/src/utils/workspaceContext.js.map +1 -0
- package/dist/src/utils/workspaceContext.test.d.ts +6 -0
- package/dist/src/utils/workspaceContext.test.js +318 -0
- package/dist/src/utils/workspaceContext.test.js.map +1 -0
- package/dist/src/utils/yaml-parser.d.ts +29 -0
- package/dist/src/utils/yaml-parser.js +172 -0
- package/dist/src/utils/yaml-parser.js.map +1 -0
- package/dist/src/utils/yaml-parser.test.d.ts +6 -0
- package/dist/src/utils/yaml-parser.test.js +170 -0
- package/dist/src/utils/yaml-parser.test.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +89 -0
|
@@ -0,0 +1,1178 @@
|
|
|
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 { GeminiChat, EmptyStreamError, StreamEventType, } from './geminiChat.js';
|
|
8
|
+
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
|
+
};
|
|
17
|
+
const { mockLogInvalidChunk, mockLogContentRetry, mockLogContentRetryFailure } = vi.hoisted(() => ({
|
|
18
|
+
mockLogInvalidChunk: vi.fn(),
|
|
19
|
+
mockLogContentRetry: vi.fn(),
|
|
20
|
+
mockLogContentRetryFailure: vi.fn(),
|
|
21
|
+
}));
|
|
22
|
+
vi.mock('../telemetry/loggers.js', () => ({
|
|
23
|
+
logInvalidChunk: mockLogInvalidChunk,
|
|
24
|
+
logContentRetry: mockLogContentRetry,
|
|
25
|
+
logContentRetryFailure: mockLogContentRetryFailure,
|
|
26
|
+
}));
|
|
27
|
+
describe('GeminiChat', () => {
|
|
28
|
+
let chat;
|
|
29
|
+
let mockConfig;
|
|
30
|
+
const config = {};
|
|
31
|
+
beforeEach(() => {
|
|
32
|
+
vi.clearAllMocks();
|
|
33
|
+
mockConfig = {
|
|
34
|
+
getSessionId: () => 'test-session-id',
|
|
35
|
+
getTelemetryLogPromptsEnabled: () => true,
|
|
36
|
+
getUsageStatisticsEnabled: () => true,
|
|
37
|
+
getDebugMode: () => false,
|
|
38
|
+
getContentGeneratorConfig: () => ({
|
|
39
|
+
authType: 'oauth-personal',
|
|
40
|
+
model: 'test-model',
|
|
41
|
+
}),
|
|
42
|
+
getModel: vi.fn().mockReturnValue('gemini-pro'),
|
|
43
|
+
setModel: vi.fn(),
|
|
44
|
+
getQuotaErrorOccurred: vi.fn().mockReturnValue(false),
|
|
45
|
+
setQuotaErrorOccurred: vi.fn(),
|
|
46
|
+
flashFallbackHandler: undefined,
|
|
47
|
+
};
|
|
48
|
+
// Disable 429 simulation for tests
|
|
49
|
+
setSimulate429(false);
|
|
50
|
+
// Reset history for each test by creating a new instance
|
|
51
|
+
chat = new GeminiChat(mockConfig, mockModelsModule, config, []);
|
|
52
|
+
});
|
|
53
|
+
afterEach(() => {
|
|
54
|
+
vi.restoreAllMocks();
|
|
55
|
+
vi.resetAllMocks();
|
|
56
|
+
});
|
|
57
|
+
describe('sendMessage', () => {
|
|
58
|
+
it('should retain the initial user message when an automatic function call occurs', async () => {
|
|
59
|
+
// 1. Define the user's initial text message. This is the turn that gets dropped by the buggy logic.
|
|
60
|
+
const userInitialMessage = {
|
|
61
|
+
role: 'user',
|
|
62
|
+
parts: [{ text: 'How is the weather in Boston?' }],
|
|
63
|
+
};
|
|
64
|
+
// 2. Mock the full API response, including the automaticFunctionCallingHistory.
|
|
65
|
+
// This history represents the full turn: user asks, model calls tool, tool responds, model answers.
|
|
66
|
+
const mockAfcResponse = {
|
|
67
|
+
candidates: [
|
|
68
|
+
{
|
|
69
|
+
content: {
|
|
70
|
+
role: 'model',
|
|
71
|
+
parts: [
|
|
72
|
+
{ text: 'The weather in Boston is 72 degrees and sunny.' },
|
|
73
|
+
],
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
],
|
|
77
|
+
automaticFunctionCallingHistory: [
|
|
78
|
+
userInitialMessage, // The user's turn
|
|
79
|
+
{
|
|
80
|
+
// The model's first response: a tool call
|
|
81
|
+
role: 'model',
|
|
82
|
+
parts: [
|
|
83
|
+
{
|
|
84
|
+
functionCall: {
|
|
85
|
+
name: 'get_weather',
|
|
86
|
+
args: { location: 'Boston' },
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
],
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
// The tool's response, which has a 'user' role
|
|
93
|
+
role: 'user',
|
|
94
|
+
parts: [
|
|
95
|
+
{
|
|
96
|
+
functionResponse: {
|
|
97
|
+
name: 'get_weather',
|
|
98
|
+
response: { temperature: 72, condition: 'sunny' },
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
],
|
|
102
|
+
},
|
|
103
|
+
],
|
|
104
|
+
};
|
|
105
|
+
vi.mocked(mockModelsModule.generateContent).mockResolvedValue(mockAfcResponse);
|
|
106
|
+
// 3. Action: Send the initial message.
|
|
107
|
+
await chat.sendMessage({ message: 'How is the weather in Boston?' }, 'prompt-id-afc-bug');
|
|
108
|
+
// 4. Assert: Check the final state of the history.
|
|
109
|
+
const history = chat.getHistory();
|
|
110
|
+
// With the bug, history.length will be 3, because the first user message is dropped.
|
|
111
|
+
// The correct behavior is for the history to contain all 4 turns.
|
|
112
|
+
expect(history.length).toBe(4);
|
|
113
|
+
// Crucially, assert that the very first turn in the history matches the user's initial message.
|
|
114
|
+
// This is the assertion that will fail.
|
|
115
|
+
const firstTurn = history[0];
|
|
116
|
+
expect(firstTurn.role).toBe('user');
|
|
117
|
+
expect(firstTurn?.parts[0].text).toBe('How is the weather in Boston?');
|
|
118
|
+
// Verify the rest of the history is also correct.
|
|
119
|
+
const secondTurn = history[1];
|
|
120
|
+
expect(secondTurn.role).toBe('model');
|
|
121
|
+
expect(secondTurn?.parts[0].functionCall).toBeDefined();
|
|
122
|
+
const thirdTurn = history[2];
|
|
123
|
+
expect(thirdTurn.role).toBe('user');
|
|
124
|
+
expect(thirdTurn?.parts[0].functionResponse).toBeDefined();
|
|
125
|
+
const fourthTurn = history[3];
|
|
126
|
+
expect(fourthTurn.role).toBe('model');
|
|
127
|
+
expect(fourthTurn?.parts[0].text).toContain('72 degrees and sunny');
|
|
128
|
+
});
|
|
129
|
+
it('should throw an error when attempting to add a user turn after another user turn', async () => {
|
|
130
|
+
// 1. Setup: Create a history that already ends with a user turn (a functionResponse).
|
|
131
|
+
const initialHistory = [
|
|
132
|
+
{ role: 'user', parts: [{ text: 'Initial prompt' }] },
|
|
133
|
+
{
|
|
134
|
+
role: 'model',
|
|
135
|
+
parts: [{ functionCall: { name: 'test_tool', args: {} } }],
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
role: 'user',
|
|
139
|
+
parts: [{ functionResponse: { name: 'test_tool', response: {} } }],
|
|
140
|
+
},
|
|
141
|
+
];
|
|
142
|
+
chat.setHistory(initialHistory);
|
|
143
|
+
// 2. Mock a valid model response so the call doesn't fail for other reasons.
|
|
144
|
+
const mockResponse = {
|
|
145
|
+
candidates: [
|
|
146
|
+
{ content: { role: 'model', parts: [{ text: 'some response' }] } },
|
|
147
|
+
],
|
|
148
|
+
};
|
|
149
|
+
vi.mocked(mockModelsModule.generateContent).mockResolvedValue(mockResponse);
|
|
150
|
+
// 3. Action & Assert: Expect that sending another user message immediately
|
|
151
|
+
// after a user-role turn throws the specific error.
|
|
152
|
+
await expect(chat.sendMessage({ message: 'This is an invalid consecutive user message' }, 'prompt-id-1')).rejects.toThrow('Cannot add a user turn after another user turn.');
|
|
153
|
+
});
|
|
154
|
+
it('should preserve text parts that are in the same response as a thought', async () => {
|
|
155
|
+
// 1. Mock the API to return a single response containing both a thought and visible text.
|
|
156
|
+
const mixedContentResponse = {
|
|
157
|
+
candidates: [
|
|
158
|
+
{
|
|
159
|
+
content: {
|
|
160
|
+
role: 'model',
|
|
161
|
+
parts: [
|
|
162
|
+
{ thought: 'This is a thought.' },
|
|
163
|
+
{ text: 'This is the visible text that should not be lost.' },
|
|
164
|
+
],
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
],
|
|
168
|
+
};
|
|
169
|
+
vi.mocked(mockModelsModule.generateContent).mockResolvedValue(mixedContentResponse);
|
|
170
|
+
// 2. Action: Send a standard, non-streaming message.
|
|
171
|
+
await chat.sendMessage({ message: 'test message' }, 'prompt-id-mixed-response');
|
|
172
|
+
// 3. Assert: Check the final state of the history.
|
|
173
|
+
const history = chat.getHistory();
|
|
174
|
+
// The history should contain two turns: the user's message and the model's response.
|
|
175
|
+
expect(history.length).toBe(2);
|
|
176
|
+
const modelTurn = history[1];
|
|
177
|
+
expect(modelTurn.role).toBe('model');
|
|
178
|
+
// CRUCIAL ASSERTION:
|
|
179
|
+
// Buggy code would discard the entire response because a "thought" was present,
|
|
180
|
+
// resulting in an empty placeholder turn with 0 parts.
|
|
181
|
+
// The corrected code will pass, preserving the single visible text part.
|
|
182
|
+
expect(modelTurn?.parts?.length).toBe(1);
|
|
183
|
+
expect(modelTurn?.parts[0].text).toBe('This is the visible text that should not be lost.');
|
|
184
|
+
});
|
|
185
|
+
it('should add a placeholder model turn when a tool call is followed by an empty model response', async () => {
|
|
186
|
+
// 1. Setup: A history where the model has just made a function call.
|
|
187
|
+
const initialHistory = [
|
|
188
|
+
{
|
|
189
|
+
role: 'user',
|
|
190
|
+
parts: [{ text: 'Find a good Italian restaurant for me.' }],
|
|
191
|
+
},
|
|
192
|
+
{
|
|
193
|
+
role: 'model',
|
|
194
|
+
parts: [
|
|
195
|
+
{
|
|
196
|
+
functionCall: {
|
|
197
|
+
name: 'find_restaurant',
|
|
198
|
+
args: { cuisine: 'Italian' },
|
|
199
|
+
},
|
|
200
|
+
},
|
|
201
|
+
],
|
|
202
|
+
},
|
|
203
|
+
];
|
|
204
|
+
chat.setHistory(initialHistory);
|
|
205
|
+
// 2. Mock the API to return an empty/thought-only response.
|
|
206
|
+
const emptyModelResponse = {
|
|
207
|
+
candidates: [
|
|
208
|
+
{ content: { role: 'model', parts: [{ thought: true }] } },
|
|
209
|
+
],
|
|
210
|
+
};
|
|
211
|
+
vi.mocked(mockModelsModule.generateContent).mockResolvedValue(emptyModelResponse);
|
|
212
|
+
// 3. Action: Send the function response back to the model.
|
|
213
|
+
await chat.sendMessage({
|
|
214
|
+
message: {
|
|
215
|
+
functionResponse: {
|
|
216
|
+
name: 'find_restaurant',
|
|
217
|
+
response: { name: 'Vesuvio' },
|
|
218
|
+
},
|
|
219
|
+
},
|
|
220
|
+
}, 'prompt-id-1');
|
|
221
|
+
// 4. Assert: The history should now have four valid, alternating turns.
|
|
222
|
+
const history = chat.getHistory();
|
|
223
|
+
expect(history.length).toBe(4);
|
|
224
|
+
// The final turn must be the empty model placeholder.
|
|
225
|
+
const lastTurn = history[3];
|
|
226
|
+
expect(lastTurn.role).toBe('model');
|
|
227
|
+
expect(lastTurn?.parts?.length).toBe(0);
|
|
228
|
+
// The second-to-last turn must be the function response we sent.
|
|
229
|
+
const secondToLastTurn = history[2];
|
|
230
|
+
expect(secondToLastTurn.role).toBe('user');
|
|
231
|
+
expect(secondToLastTurn?.parts[0].functionResponse).toBeDefined();
|
|
232
|
+
});
|
|
233
|
+
it('should call generateContent with the correct parameters', async () => {
|
|
234
|
+
const response = {
|
|
235
|
+
candidates: [
|
|
236
|
+
{
|
|
237
|
+
content: {
|
|
238
|
+
parts: [{ text: 'response' }],
|
|
239
|
+
role: 'model',
|
|
240
|
+
},
|
|
241
|
+
finishReason: 'STOP',
|
|
242
|
+
index: 0,
|
|
243
|
+
safetyRatings: [],
|
|
244
|
+
},
|
|
245
|
+
],
|
|
246
|
+
text: () => 'response',
|
|
247
|
+
};
|
|
248
|
+
vi.mocked(mockModelsModule.generateContent).mockResolvedValue(response);
|
|
249
|
+
await chat.sendMessage({ message: 'hello' }, 'prompt-id-1');
|
|
250
|
+
expect(mockModelsModule.generateContent).toHaveBeenCalledWith({
|
|
251
|
+
model: 'gemini-pro',
|
|
252
|
+
contents: [{ role: 'user', parts: [{ text: 'hello' }] }],
|
|
253
|
+
config: {},
|
|
254
|
+
}, 'prompt-id-1');
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
describe('sendMessageStream', () => {
|
|
258
|
+
it('should succeed if a tool call is followed by an empty part', async () => {
|
|
259
|
+
// 1. Mock a stream that contains a tool call, then an invalid (empty) part.
|
|
260
|
+
const streamWithToolCall = (async function* () {
|
|
261
|
+
yield {
|
|
262
|
+
candidates: [
|
|
263
|
+
{
|
|
264
|
+
content: {
|
|
265
|
+
role: 'model',
|
|
266
|
+
parts: [{ functionCall: { name: 'test_tool', args: {} } }],
|
|
267
|
+
},
|
|
268
|
+
},
|
|
269
|
+
],
|
|
270
|
+
};
|
|
271
|
+
// This second chunk is invalid according to isValidResponse
|
|
272
|
+
yield {
|
|
273
|
+
candidates: [
|
|
274
|
+
{
|
|
275
|
+
content: {
|
|
276
|
+
role: 'model',
|
|
277
|
+
parts: [{ text: '' }],
|
|
278
|
+
},
|
|
279
|
+
},
|
|
280
|
+
],
|
|
281
|
+
};
|
|
282
|
+
})();
|
|
283
|
+
vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(streamWithToolCall);
|
|
284
|
+
// 2. Action & Assert: The stream processing should complete without throwing an error
|
|
285
|
+
// because the presence of a tool call makes the empty final chunk acceptable.
|
|
286
|
+
const stream = await chat.sendMessageStream({ message: 'test message' }, 'prompt-id-tool-call-empty-end');
|
|
287
|
+
await expect((async () => {
|
|
288
|
+
for await (const _ of stream) {
|
|
289
|
+
/* consume stream */
|
|
290
|
+
}
|
|
291
|
+
})()).resolves.not.toThrow();
|
|
292
|
+
// 3. Verify history was recorded correctly
|
|
293
|
+
const history = chat.getHistory();
|
|
294
|
+
expect(history.length).toBe(2); // user turn + model turn
|
|
295
|
+
const modelTurn = history[1];
|
|
296
|
+
expect(modelTurn?.parts?.length).toBe(1); // The empty part is discarded
|
|
297
|
+
expect(modelTurn?.parts[0].functionCall).toBeDefined();
|
|
298
|
+
});
|
|
299
|
+
it('should fail if the stream ends with an empty part and has no finishReason', async () => {
|
|
300
|
+
// 1. Mock a stream that ends with an invalid part and has no finish reason.
|
|
301
|
+
const streamWithNoFinish = (async function* () {
|
|
302
|
+
yield {
|
|
303
|
+
candidates: [
|
|
304
|
+
{
|
|
305
|
+
content: {
|
|
306
|
+
role: 'model',
|
|
307
|
+
parts: [{ text: 'Initial content...' }],
|
|
308
|
+
},
|
|
309
|
+
},
|
|
310
|
+
],
|
|
311
|
+
};
|
|
312
|
+
// This second chunk is invalid and has no finishReason, so it should fail.
|
|
313
|
+
yield {
|
|
314
|
+
candidates: [
|
|
315
|
+
{
|
|
316
|
+
content: {
|
|
317
|
+
role: 'model',
|
|
318
|
+
parts: [{ text: '' }],
|
|
319
|
+
},
|
|
320
|
+
},
|
|
321
|
+
],
|
|
322
|
+
};
|
|
323
|
+
})();
|
|
324
|
+
vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(streamWithNoFinish);
|
|
325
|
+
// 2. Action & Assert: The stream should fail because there's no finish reason.
|
|
326
|
+
const stream = await chat.sendMessageStream({ message: 'test message' }, 'prompt-id-no-finish-empty-end');
|
|
327
|
+
await expect((async () => {
|
|
328
|
+
for await (const _ of stream) {
|
|
329
|
+
/* consume stream */
|
|
330
|
+
}
|
|
331
|
+
})()).rejects.toThrow(EmptyStreamError);
|
|
332
|
+
});
|
|
333
|
+
it('should succeed if the stream ends with an invalid part but has a finishReason and contained a valid part', async () => {
|
|
334
|
+
// 1. Mock a stream that sends a valid chunk, then an invalid one, but has a finish reason.
|
|
335
|
+
const streamWithInvalidEnd = (async function* () {
|
|
336
|
+
yield {
|
|
337
|
+
candidates: [
|
|
338
|
+
{
|
|
339
|
+
content: {
|
|
340
|
+
role: 'model',
|
|
341
|
+
parts: [{ text: 'Initial valid content...' }],
|
|
342
|
+
},
|
|
343
|
+
},
|
|
344
|
+
],
|
|
345
|
+
};
|
|
346
|
+
// This second chunk is invalid, but the response has a finishReason.
|
|
347
|
+
yield {
|
|
348
|
+
candidates: [
|
|
349
|
+
{
|
|
350
|
+
content: {
|
|
351
|
+
role: 'model',
|
|
352
|
+
parts: [{ text: '' }], // Invalid part
|
|
353
|
+
},
|
|
354
|
+
finishReason: 'STOP',
|
|
355
|
+
},
|
|
356
|
+
],
|
|
357
|
+
};
|
|
358
|
+
})();
|
|
359
|
+
vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(streamWithInvalidEnd);
|
|
360
|
+
// 2. Action & Assert: The stream should complete without throwing an error.
|
|
361
|
+
const stream = await chat.sendMessageStream({ message: 'test message' }, 'prompt-id-valid-then-invalid-end');
|
|
362
|
+
await expect((async () => {
|
|
363
|
+
for await (const _ of stream) {
|
|
364
|
+
/* consume stream */
|
|
365
|
+
}
|
|
366
|
+
})()).resolves.not.toThrow();
|
|
367
|
+
// 3. Verify history was recorded correctly with only the valid part.
|
|
368
|
+
const history = chat.getHistory();
|
|
369
|
+
expect(history.length).toBe(2); // user turn + model turn
|
|
370
|
+
const modelTurn = history[1];
|
|
371
|
+
expect(modelTurn?.parts?.length).toBe(1);
|
|
372
|
+
expect(modelTurn?.parts[0].text).toBe('Initial valid content...');
|
|
373
|
+
});
|
|
374
|
+
it('should not consolidate text into a part that also contains a functionCall', async () => {
|
|
375
|
+
// 1. Mock the API to stream a malformed part followed by a valid text part.
|
|
376
|
+
const multiChunkStream = (async function* () {
|
|
377
|
+
// This malformed part has both text and a functionCall.
|
|
378
|
+
yield {
|
|
379
|
+
candidates: [
|
|
380
|
+
{
|
|
381
|
+
content: {
|
|
382
|
+
role: 'model',
|
|
383
|
+
parts: [
|
|
384
|
+
{
|
|
385
|
+
text: 'Some text',
|
|
386
|
+
functionCall: { name: 'do_stuff', args: {} },
|
|
387
|
+
},
|
|
388
|
+
],
|
|
389
|
+
},
|
|
390
|
+
},
|
|
391
|
+
],
|
|
392
|
+
};
|
|
393
|
+
// This valid text part should NOT be merged into the malformed one.
|
|
394
|
+
yield {
|
|
395
|
+
candidates: [
|
|
396
|
+
{
|
|
397
|
+
content: {
|
|
398
|
+
role: 'model',
|
|
399
|
+
parts: [{ text: ' that should not be merged.' }],
|
|
400
|
+
},
|
|
401
|
+
},
|
|
402
|
+
],
|
|
403
|
+
};
|
|
404
|
+
})();
|
|
405
|
+
vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(multiChunkStream);
|
|
406
|
+
// 2. Action: Send a message and consume the stream.
|
|
407
|
+
const stream = await chat.sendMessageStream({ message: 'test message' }, 'prompt-id-malformed-chunk');
|
|
408
|
+
for await (const _ of stream) {
|
|
409
|
+
// Consume the stream to trigger history recording.
|
|
410
|
+
}
|
|
411
|
+
// 3. Assert: Check that the final history was not incorrectly consolidated.
|
|
412
|
+
const history = chat.getHistory();
|
|
413
|
+
expect(history.length).toBe(2);
|
|
414
|
+
const modelTurn = history[1];
|
|
415
|
+
// CRUCIAL ASSERTION: There should be two separate parts.
|
|
416
|
+
// The old, non-strict logic would incorrectly merge them, resulting in one part.
|
|
417
|
+
expect(modelTurn?.parts?.length).toBe(2);
|
|
418
|
+
// Verify the contents of each part.
|
|
419
|
+
expect(modelTurn?.parts[0].text).toBe('Some text');
|
|
420
|
+
expect(modelTurn?.parts[0].functionCall).toBeDefined();
|
|
421
|
+
expect(modelTurn?.parts[1].text).toBe(' that should not be merged.');
|
|
422
|
+
});
|
|
423
|
+
it('should consolidate subsequent text chunks after receiving an empty text chunk', async () => {
|
|
424
|
+
// 1. Mock the API to return a stream where one chunk is just an empty text part.
|
|
425
|
+
const multiChunkStream = (async function* () {
|
|
426
|
+
yield {
|
|
427
|
+
candidates: [
|
|
428
|
+
{ content: { role: 'model', parts: [{ text: 'Hello' }] } },
|
|
429
|
+
],
|
|
430
|
+
};
|
|
431
|
+
// FIX: The original test used { text: '' }, which is invalid.
|
|
432
|
+
// A chunk can be empty but still valid. This chunk is now removed
|
|
433
|
+
// as the important part is consolidating what comes after.
|
|
434
|
+
yield {
|
|
435
|
+
candidates: [
|
|
436
|
+
{
|
|
437
|
+
content: { role: 'model', parts: [{ text: ' World!' }] },
|
|
438
|
+
finishReason: 'STOP',
|
|
439
|
+
},
|
|
440
|
+
],
|
|
441
|
+
};
|
|
442
|
+
})();
|
|
443
|
+
vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(multiChunkStream);
|
|
444
|
+
// 2. Action: Send a message and consume the stream.
|
|
445
|
+
const stream = await chat.sendMessageStream({ message: 'test message' }, 'prompt-id-empty-chunk-consolidation');
|
|
446
|
+
for await (const _ of stream) {
|
|
447
|
+
// Consume the stream
|
|
448
|
+
}
|
|
449
|
+
// 3. Assert: Check that the final history was correctly consolidated.
|
|
450
|
+
const history = chat.getHistory();
|
|
451
|
+
expect(history.length).toBe(2);
|
|
452
|
+
const modelTurn = history[1];
|
|
453
|
+
expect(modelTurn?.parts?.length).toBe(1);
|
|
454
|
+
expect(modelTurn?.parts[0].text).toBe('Hello World!');
|
|
455
|
+
});
|
|
456
|
+
it('should consolidate adjacent text parts that arrive in separate stream chunks', async () => {
|
|
457
|
+
// 1. Mock the API to return a stream of multiple, adjacent text chunks.
|
|
458
|
+
const multiChunkStream = (async function* () {
|
|
459
|
+
yield {
|
|
460
|
+
candidates: [
|
|
461
|
+
{ content: { role: 'model', parts: [{ text: 'This is the ' }] } },
|
|
462
|
+
],
|
|
463
|
+
};
|
|
464
|
+
yield {
|
|
465
|
+
candidates: [
|
|
466
|
+
{ content: { role: 'model', parts: [{ text: 'first part.' }] } },
|
|
467
|
+
],
|
|
468
|
+
};
|
|
469
|
+
// This function call should break the consolidation.
|
|
470
|
+
yield {
|
|
471
|
+
candidates: [
|
|
472
|
+
{
|
|
473
|
+
content: {
|
|
474
|
+
role: 'model',
|
|
475
|
+
parts: [{ functionCall: { name: 'do_stuff', args: {} } }],
|
|
476
|
+
},
|
|
477
|
+
},
|
|
478
|
+
],
|
|
479
|
+
};
|
|
480
|
+
yield {
|
|
481
|
+
candidates: [
|
|
482
|
+
{
|
|
483
|
+
content: {
|
|
484
|
+
role: 'model',
|
|
485
|
+
parts: [{ text: 'This is the second part.' }],
|
|
486
|
+
},
|
|
487
|
+
},
|
|
488
|
+
],
|
|
489
|
+
};
|
|
490
|
+
})();
|
|
491
|
+
vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(multiChunkStream);
|
|
492
|
+
// 2. Action: Send a message and consume the stream.
|
|
493
|
+
const stream = await chat.sendMessageStream({ message: 'test message' }, 'prompt-id-multi-chunk');
|
|
494
|
+
for await (const _ of stream) {
|
|
495
|
+
// Consume the stream to trigger history recording.
|
|
496
|
+
}
|
|
497
|
+
// 3. Assert: Check that the final history was correctly consolidated.
|
|
498
|
+
const history = chat.getHistory();
|
|
499
|
+
// The history should contain the user's turn and ONE consolidated model turn.
|
|
500
|
+
expect(history.length).toBe(2);
|
|
501
|
+
const modelTurn = history[1];
|
|
502
|
+
expect(modelTurn.role).toBe('model');
|
|
503
|
+
// The model turn should have 3 distinct parts: the merged text, the function call, and the final text.
|
|
504
|
+
expect(modelTurn?.parts?.length).toBe(3);
|
|
505
|
+
expect(modelTurn?.parts[0].text).toBe('This is the first part.');
|
|
506
|
+
expect(modelTurn.parts[1].functionCall).toBeDefined();
|
|
507
|
+
expect(modelTurn.parts[2].text).toBe('This is the second part.');
|
|
508
|
+
});
|
|
509
|
+
it('should preserve text parts that stream in the same chunk as a thought', async () => {
|
|
510
|
+
// 1. Mock the API to return a single chunk containing both a thought and visible text.
|
|
511
|
+
const mixedContentStream = (async function* () {
|
|
512
|
+
yield {
|
|
513
|
+
candidates: [
|
|
514
|
+
{
|
|
515
|
+
content: {
|
|
516
|
+
role: 'model',
|
|
517
|
+
parts: [
|
|
518
|
+
{ thought: 'This is a thought.' },
|
|
519
|
+
{ text: 'This is the visible text that should not be lost.' },
|
|
520
|
+
],
|
|
521
|
+
},
|
|
522
|
+
finishReason: 'STOP',
|
|
523
|
+
},
|
|
524
|
+
],
|
|
525
|
+
};
|
|
526
|
+
})();
|
|
527
|
+
vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(mixedContentStream);
|
|
528
|
+
// 2. Action: Send a message and fully consume the stream to trigger history recording.
|
|
529
|
+
const stream = await chat.sendMessageStream({ message: 'test message' }, 'prompt-id-mixed-chunk');
|
|
530
|
+
for await (const _ of stream) {
|
|
531
|
+
// This loop consumes the stream.
|
|
532
|
+
}
|
|
533
|
+
// 3. Assert: Check the final state of the history.
|
|
534
|
+
const history = chat.getHistory();
|
|
535
|
+
// The history should contain two turns: the user's message and the model's response.
|
|
536
|
+
expect(history.length).toBe(2);
|
|
537
|
+
const modelTurn = history[1];
|
|
538
|
+
expect(modelTurn.role).toBe('model');
|
|
539
|
+
// CRUCIAL ASSERTION:
|
|
540
|
+
// The buggy code would fail here, resulting in parts.length being 0.
|
|
541
|
+
// The corrected code will pass, preserving the single visible text part.
|
|
542
|
+
expect(modelTurn?.parts?.length).toBe(1);
|
|
543
|
+
expect(modelTurn?.parts[0].text).toBe('This is the visible text that should not be lost.');
|
|
544
|
+
});
|
|
545
|
+
it('should add a placeholder model turn when a tool call is followed by an empty stream response', async () => {
|
|
546
|
+
// 1. Setup: A history where the model has just made a function call.
|
|
547
|
+
const initialHistory = [
|
|
548
|
+
{
|
|
549
|
+
role: 'user',
|
|
550
|
+
parts: [{ text: 'Find a good Italian restaurant for me.' }],
|
|
551
|
+
},
|
|
552
|
+
{
|
|
553
|
+
role: 'model',
|
|
554
|
+
parts: [
|
|
555
|
+
{
|
|
556
|
+
functionCall: {
|
|
557
|
+
name: 'find_restaurant',
|
|
558
|
+
args: { cuisine: 'Italian' },
|
|
559
|
+
},
|
|
560
|
+
},
|
|
561
|
+
],
|
|
562
|
+
},
|
|
563
|
+
];
|
|
564
|
+
chat.setHistory(initialHistory);
|
|
565
|
+
// 2. Mock the API to return an empty/thought-only stream.
|
|
566
|
+
const emptyStreamResponse = (async function* () {
|
|
567
|
+
yield {
|
|
568
|
+
candidates: [
|
|
569
|
+
{
|
|
570
|
+
content: { role: 'model', parts: [{ thought: true }] },
|
|
571
|
+
finishReason: 'STOP',
|
|
572
|
+
},
|
|
573
|
+
],
|
|
574
|
+
};
|
|
575
|
+
})();
|
|
576
|
+
vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(emptyStreamResponse);
|
|
577
|
+
// 3. Action: Send the function response back to the model and consume the stream.
|
|
578
|
+
const stream = await chat.sendMessageStream({
|
|
579
|
+
message: {
|
|
580
|
+
functionResponse: {
|
|
581
|
+
name: 'find_restaurant',
|
|
582
|
+
response: { name: 'Vesuvio' },
|
|
583
|
+
},
|
|
584
|
+
},
|
|
585
|
+
}, 'prompt-id-stream-1');
|
|
586
|
+
for await (const _ of stream) {
|
|
587
|
+
// This loop consumes the stream to trigger the internal logic.
|
|
588
|
+
}
|
|
589
|
+
// 4. Assert: The history should now have four valid, alternating turns.
|
|
590
|
+
const history = chat.getHistory();
|
|
591
|
+
expect(history.length).toBe(4);
|
|
592
|
+
// The final turn must be the empty model placeholder.
|
|
593
|
+
const lastTurn = history[3];
|
|
594
|
+
expect(lastTurn.role).toBe('model');
|
|
595
|
+
expect(lastTurn?.parts?.length).toBe(0);
|
|
596
|
+
// The second-to-last turn must be the function response we sent.
|
|
597
|
+
const secondToLastTurn = history[2];
|
|
598
|
+
expect(secondToLastTurn.role).toBe('user');
|
|
599
|
+
expect(secondToLastTurn?.parts[0].functionResponse).toBeDefined();
|
|
600
|
+
});
|
|
601
|
+
it('should call generateContentStream with the correct parameters', async () => {
|
|
602
|
+
const response = (async function* () {
|
|
603
|
+
yield {
|
|
604
|
+
candidates: [
|
|
605
|
+
{
|
|
606
|
+
content: {
|
|
607
|
+
parts: [{ text: 'response' }],
|
|
608
|
+
role: 'model',
|
|
609
|
+
},
|
|
610
|
+
finishReason: 'STOP',
|
|
611
|
+
index: 0,
|
|
612
|
+
safetyRatings: [],
|
|
613
|
+
},
|
|
614
|
+
],
|
|
615
|
+
text: () => 'response',
|
|
616
|
+
};
|
|
617
|
+
})();
|
|
618
|
+
vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(response);
|
|
619
|
+
const stream = await chat.sendMessageStream({ message: 'hello' }, 'prompt-id-1');
|
|
620
|
+
for await (const _ of stream) {
|
|
621
|
+
// consume stream to trigger internal logic
|
|
622
|
+
}
|
|
623
|
+
expect(mockModelsModule.generateContentStream).toHaveBeenCalledWith({
|
|
624
|
+
model: 'gemini-pro',
|
|
625
|
+
contents: [{ role: 'user', parts: [{ text: 'hello' }] }],
|
|
626
|
+
config: {},
|
|
627
|
+
}, 'prompt-id-1');
|
|
628
|
+
});
|
|
629
|
+
});
|
|
630
|
+
describe('recordHistory', () => {
|
|
631
|
+
const userInput = {
|
|
632
|
+
role: 'user',
|
|
633
|
+
parts: [{ text: 'User input' }],
|
|
634
|
+
};
|
|
635
|
+
it('should consolidate all consecutive model turns into a single turn', () => {
|
|
636
|
+
const userInput = {
|
|
637
|
+
role: 'user',
|
|
638
|
+
parts: [{ text: 'User input' }],
|
|
639
|
+
};
|
|
640
|
+
// This simulates a multi-part model response with different part types.
|
|
641
|
+
const modelOutput = [
|
|
642
|
+
{ role: 'model', parts: [{ text: 'Thinking...' }] },
|
|
643
|
+
{
|
|
644
|
+
role: 'model',
|
|
645
|
+
parts: [{ functionCall: { name: 'do_stuff', args: {} } }],
|
|
646
|
+
},
|
|
647
|
+
];
|
|
648
|
+
// @ts-expect-error Accessing private method for testing
|
|
649
|
+
chat.recordHistory(userInput, modelOutput);
|
|
650
|
+
const history = chat.getHistory();
|
|
651
|
+
// The history should contain the user's turn and ONE consolidated model turn.
|
|
652
|
+
// The old code would fail here, resulting in a length of 3.
|
|
653
|
+
//expect(history).toBe([]);
|
|
654
|
+
expect(history.length).toBe(2);
|
|
655
|
+
const modelTurn = history[1];
|
|
656
|
+
expect(modelTurn.role).toBe('model');
|
|
657
|
+
// The consolidated turn should contain both the text part and the functionCall part.
|
|
658
|
+
expect(modelTurn?.parts?.length).toBe(2);
|
|
659
|
+
expect(modelTurn?.parts[0].text).toBe('Thinking...');
|
|
660
|
+
expect(modelTurn?.parts[1].functionCall).toBeDefined();
|
|
661
|
+
});
|
|
662
|
+
it('should add a placeholder model turn when a tool call is followed by an empty response', () => {
|
|
663
|
+
// 1. Setup: A history where the model has just made a function call.
|
|
664
|
+
const initialHistory = [
|
|
665
|
+
{ role: 'user', parts: [{ text: 'Initial prompt' }] },
|
|
666
|
+
{
|
|
667
|
+
role: 'model',
|
|
668
|
+
parts: [{ functionCall: { name: 'test_tool', args: {} } }],
|
|
669
|
+
},
|
|
670
|
+
];
|
|
671
|
+
chat.setHistory(initialHistory);
|
|
672
|
+
// 2. Action: The user provides the tool's response, and the model's
|
|
673
|
+
// final output is empty (e.g., just a thought, which gets filtered out).
|
|
674
|
+
const functionResponse = {
|
|
675
|
+
role: 'user',
|
|
676
|
+
parts: [{ functionResponse: { name: 'test_tool', response: {} } }],
|
|
677
|
+
};
|
|
678
|
+
const emptyModelOutput = [];
|
|
679
|
+
// @ts-expect-error Accessing private method for testing
|
|
680
|
+
chat.recordHistory(functionResponse, emptyModelOutput, [
|
|
681
|
+
functionResponse,
|
|
682
|
+
]);
|
|
683
|
+
// 3. Assert: The history should now have four valid, alternating turns.
|
|
684
|
+
const history = chat.getHistory();
|
|
685
|
+
expect(history.length).toBe(4);
|
|
686
|
+
// The final turn must be the empty model placeholder.
|
|
687
|
+
const lastTurn = history[3];
|
|
688
|
+
expect(lastTurn.role).toBe('model');
|
|
689
|
+
expect(lastTurn?.parts?.length).toBe(0);
|
|
690
|
+
// The second-to-last turn must be the function response we provided.
|
|
691
|
+
const secondToLastTurn = history[2];
|
|
692
|
+
expect(secondToLastTurn.role).toBe('user');
|
|
693
|
+
expect(secondToLastTurn?.parts[0].functionResponse).toBeDefined();
|
|
694
|
+
});
|
|
695
|
+
it('should add user input and a single model output to history', () => {
|
|
696
|
+
const modelOutput = [
|
|
697
|
+
{ role: 'model', parts: [{ text: 'Model output' }] },
|
|
698
|
+
];
|
|
699
|
+
// @ts-expect-error Accessing private method for testing
|
|
700
|
+
chat.recordHistory(userInput, modelOutput);
|
|
701
|
+
const history = chat.getHistory();
|
|
702
|
+
expect(history.length).toBe(2);
|
|
703
|
+
expect(history[0]).toEqual(userInput);
|
|
704
|
+
expect(history[1]).toEqual(modelOutput[0]);
|
|
705
|
+
});
|
|
706
|
+
it('should consolidate adjacent text parts from multiple content objects', () => {
|
|
707
|
+
const modelOutput = [
|
|
708
|
+
{ role: 'model', parts: [{ text: 'Part 1.' }] },
|
|
709
|
+
{ role: 'model', parts: [{ text: ' Part 2.' }] },
|
|
710
|
+
{ role: 'model', parts: [{ text: ' Part 3.' }] },
|
|
711
|
+
];
|
|
712
|
+
// @ts-expect-error Accessing private method for testing
|
|
713
|
+
chat.recordHistory(userInput, modelOutput);
|
|
714
|
+
const history = chat.getHistory();
|
|
715
|
+
expect(history.length).toBe(2);
|
|
716
|
+
expect(history[1].role).toBe('model');
|
|
717
|
+
expect(history[1].parts).toEqual([{ text: 'Part 1. Part 2. Part 3.' }]);
|
|
718
|
+
});
|
|
719
|
+
it('should add an empty placeholder turn if modelOutput is empty', () => {
|
|
720
|
+
// This simulates receiving a pre-filtered, thought-only response.
|
|
721
|
+
const emptyModelOutput = [];
|
|
722
|
+
// @ts-expect-error Accessing private method for testing
|
|
723
|
+
chat.recordHistory(userInput, emptyModelOutput);
|
|
724
|
+
const history = chat.getHistory();
|
|
725
|
+
expect(history.length).toBe(2);
|
|
726
|
+
expect(history[0]).toEqual(userInput);
|
|
727
|
+
expect(history[1].role).toBe('model');
|
|
728
|
+
expect(history[1].parts).toEqual([]);
|
|
729
|
+
});
|
|
730
|
+
it('should preserve model outputs with undefined or empty parts arrays', () => {
|
|
731
|
+
const malformedOutput = [
|
|
732
|
+
{ role: 'model', parts: [{ text: 'Text part' }] },
|
|
733
|
+
{ role: 'model', parts: undefined },
|
|
734
|
+
{ role: 'model', parts: [] },
|
|
735
|
+
];
|
|
736
|
+
// @ts-expect-error Accessing private method for testing
|
|
737
|
+
chat.recordHistory(userInput, malformedOutput);
|
|
738
|
+
const history = chat.getHistory();
|
|
739
|
+
expect(history.length).toBe(4); // userInput + 3 model turns
|
|
740
|
+
expect(history[1].parts).toEqual([{ text: 'Text part' }]);
|
|
741
|
+
expect(history[2].parts).toBeUndefined();
|
|
742
|
+
expect(history[3].parts).toEqual([]);
|
|
743
|
+
});
|
|
744
|
+
it('should not consolidate content with different roles', () => {
|
|
745
|
+
const mixedOutput = [
|
|
746
|
+
{ role: 'model', parts: [{ text: 'Model 1' }] },
|
|
747
|
+
{ role: 'user', parts: [{ text: 'Unexpected User' }] },
|
|
748
|
+
{ role: 'model', parts: [{ text: 'Model 2' }] },
|
|
749
|
+
];
|
|
750
|
+
// @ts-expect-error Accessing private method for testing
|
|
751
|
+
chat.recordHistory(userInput, mixedOutput);
|
|
752
|
+
const history = chat.getHistory();
|
|
753
|
+
expect(history.length).toBe(4); // userInput, model1, unexpected_user, model2
|
|
754
|
+
expect(history[1]).toEqual(mixedOutput[0]);
|
|
755
|
+
expect(history[2]).toEqual(mixedOutput[1]);
|
|
756
|
+
expect(history[3]).toEqual(mixedOutput[2]);
|
|
757
|
+
});
|
|
758
|
+
});
|
|
759
|
+
describe('addHistory', () => {
|
|
760
|
+
it('should add a new content item to the history', () => {
|
|
761
|
+
const newContent = {
|
|
762
|
+
role: 'user',
|
|
763
|
+
parts: [{ text: 'A new message' }],
|
|
764
|
+
};
|
|
765
|
+
chat.addHistory(newContent);
|
|
766
|
+
const history = chat.getHistory();
|
|
767
|
+
expect(history.length).toBe(1);
|
|
768
|
+
expect(history[0]).toEqual(newContent);
|
|
769
|
+
});
|
|
770
|
+
it('should add multiple items correctly', () => {
|
|
771
|
+
const content1 = {
|
|
772
|
+
role: 'user',
|
|
773
|
+
parts: [{ text: 'Message 1' }],
|
|
774
|
+
};
|
|
775
|
+
const content2 = {
|
|
776
|
+
role: 'model',
|
|
777
|
+
parts: [{ text: 'Message 2' }],
|
|
778
|
+
};
|
|
779
|
+
chat.addHistory(content1);
|
|
780
|
+
chat.addHistory(content2);
|
|
781
|
+
const history = chat.getHistory();
|
|
782
|
+
expect(history.length).toBe(2);
|
|
783
|
+
expect(history[0]).toEqual(content1);
|
|
784
|
+
expect(history[1]).toEqual(content2);
|
|
785
|
+
});
|
|
786
|
+
});
|
|
787
|
+
describe('sendMessageStream with retries', () => {
|
|
788
|
+
it('should yield a RETRY event when an invalid stream is encountered', async () => {
|
|
789
|
+
// ARRANGE: Mock the stream to fail once, then succeed.
|
|
790
|
+
vi.mocked(mockModelsModule.generateContentStream)
|
|
791
|
+
.mockImplementationOnce(async () =>
|
|
792
|
+
// First attempt: An invalid stream with an empty text part.
|
|
793
|
+
(async function* () {
|
|
794
|
+
yield {
|
|
795
|
+
candidates: [{ content: { parts: [{ text: '' }] } }],
|
|
796
|
+
};
|
|
797
|
+
})())
|
|
798
|
+
.mockImplementationOnce(async () =>
|
|
799
|
+
// Second attempt (the retry): A minimal valid stream.
|
|
800
|
+
(async function* () {
|
|
801
|
+
yield {
|
|
802
|
+
candidates: [
|
|
803
|
+
{
|
|
804
|
+
content: { parts: [{ text: 'Success' }] },
|
|
805
|
+
finishReason: 'STOP',
|
|
806
|
+
},
|
|
807
|
+
],
|
|
808
|
+
};
|
|
809
|
+
})());
|
|
810
|
+
// ACT: Send a message and collect all events from the stream.
|
|
811
|
+
const stream = await chat.sendMessageStream({ message: 'test' }, 'prompt-id-yield-retry');
|
|
812
|
+
const events = [];
|
|
813
|
+
for await (const event of stream) {
|
|
814
|
+
events.push(event);
|
|
815
|
+
}
|
|
816
|
+
// ASSERT: Check that a RETRY event was present in the stream's output.
|
|
817
|
+
const retryEvent = events.find((e) => e.type === StreamEventType.RETRY);
|
|
818
|
+
expect(retryEvent).toBeDefined();
|
|
819
|
+
expect(retryEvent?.type).toBe(StreamEventType.RETRY);
|
|
820
|
+
});
|
|
821
|
+
it('should retry on invalid content, succeed, and report metrics', async () => {
|
|
822
|
+
// Use mockImplementationOnce to provide a fresh, promise-wrapped generator for each attempt.
|
|
823
|
+
vi.mocked(mockModelsModule.generateContentStream)
|
|
824
|
+
.mockImplementationOnce(async () =>
|
|
825
|
+
// First call returns an invalid stream
|
|
826
|
+
(async function* () {
|
|
827
|
+
yield {
|
|
828
|
+
candidates: [{ content: { parts: [{ text: '' }] } }], // Invalid empty text part
|
|
829
|
+
};
|
|
830
|
+
})())
|
|
831
|
+
.mockImplementationOnce(async () =>
|
|
832
|
+
// Second call returns a valid stream
|
|
833
|
+
(async function* () {
|
|
834
|
+
yield {
|
|
835
|
+
candidates: [
|
|
836
|
+
{
|
|
837
|
+
content: { parts: [{ text: 'Successful response' }] },
|
|
838
|
+
finishReason: 'STOP',
|
|
839
|
+
},
|
|
840
|
+
],
|
|
841
|
+
};
|
|
842
|
+
})());
|
|
843
|
+
const stream = await chat.sendMessageStream({ message: 'test' }, 'prompt-id-retry-success');
|
|
844
|
+
const chunks = [];
|
|
845
|
+
for await (const chunk of stream) {
|
|
846
|
+
chunks.push(chunk);
|
|
847
|
+
}
|
|
848
|
+
// Assertions
|
|
849
|
+
expect(mockLogInvalidChunk).toHaveBeenCalledTimes(1);
|
|
850
|
+
expect(mockLogContentRetry).toHaveBeenCalledTimes(1);
|
|
851
|
+
expect(mockLogContentRetryFailure).not.toHaveBeenCalled();
|
|
852
|
+
expect(mockModelsModule.generateContentStream).toHaveBeenCalledTimes(2);
|
|
853
|
+
// Check for a retry event
|
|
854
|
+
expect(chunks.some((c) => c.type === StreamEventType.RETRY)).toBe(true);
|
|
855
|
+
// Check for the successful content chunk
|
|
856
|
+
expect(chunks.some((c) => c.type === StreamEventType.CHUNK &&
|
|
857
|
+
c.value.candidates?.[0]?.content?.parts?.[0]?.text ===
|
|
858
|
+
'Successful response')).toBe(true);
|
|
859
|
+
// Check that history was recorded correctly once, with no duplicates.
|
|
860
|
+
const history = chat.getHistory();
|
|
861
|
+
expect(history.length).toBe(2);
|
|
862
|
+
expect(history[0]).toEqual({
|
|
863
|
+
role: 'user',
|
|
864
|
+
parts: [{ text: 'test' }],
|
|
865
|
+
});
|
|
866
|
+
expect(history[1]).toEqual({
|
|
867
|
+
role: 'model',
|
|
868
|
+
parts: [{ text: 'Successful response' }],
|
|
869
|
+
});
|
|
870
|
+
});
|
|
871
|
+
it('should fail after all retries on persistent invalid content and report metrics', async () => {
|
|
872
|
+
vi.mocked(mockModelsModule.generateContentStream).mockImplementation(async () => (async function* () {
|
|
873
|
+
yield {
|
|
874
|
+
candidates: [
|
|
875
|
+
{
|
|
876
|
+
content: {
|
|
877
|
+
parts: [{ text: '' }],
|
|
878
|
+
role: 'model',
|
|
879
|
+
},
|
|
880
|
+
},
|
|
881
|
+
],
|
|
882
|
+
};
|
|
883
|
+
})());
|
|
884
|
+
// This helper function consumes the stream and allows us to test for rejection.
|
|
885
|
+
async function consumeStreamAndExpectError() {
|
|
886
|
+
const stream = await chat.sendMessageStream({ message: 'test' }, 'prompt-id-retry-fail');
|
|
887
|
+
for await (const _ of stream) {
|
|
888
|
+
// Must loop to trigger the internal logic that throws.
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
await expect(consumeStreamAndExpectError()).rejects.toThrow(EmptyStreamError);
|
|
892
|
+
// Should be called 3 times (initial + 2 retries)
|
|
893
|
+
expect(mockModelsModule.generateContentStream).toHaveBeenCalledTimes(3);
|
|
894
|
+
expect(mockLogInvalidChunk).toHaveBeenCalledTimes(3);
|
|
895
|
+
expect(mockLogContentRetry).toHaveBeenCalledTimes(2);
|
|
896
|
+
expect(mockLogContentRetryFailure).toHaveBeenCalledTimes(1);
|
|
897
|
+
// History should be clean, as if the failed turn never happened.
|
|
898
|
+
const history = chat.getHistory();
|
|
899
|
+
expect(history.length).toBe(0);
|
|
900
|
+
});
|
|
901
|
+
});
|
|
902
|
+
it('should correctly retry and append to an existing history mid-conversation', async () => {
|
|
903
|
+
// 1. Setup
|
|
904
|
+
const initialHistory = [
|
|
905
|
+
{ role: 'user', parts: [{ text: 'First question' }] },
|
|
906
|
+
{ role: 'model', parts: [{ text: 'First answer' }] },
|
|
907
|
+
];
|
|
908
|
+
chat.setHistory(initialHistory);
|
|
909
|
+
// 2. Mock the API to fail once with an empty stream, then succeed.
|
|
910
|
+
vi.mocked(mockModelsModule.generateContentStream)
|
|
911
|
+
.mockImplementationOnce(async () => (async function* () {
|
|
912
|
+
yield {
|
|
913
|
+
candidates: [{ content: { parts: [{ text: '' }] } }],
|
|
914
|
+
};
|
|
915
|
+
})())
|
|
916
|
+
.mockImplementationOnce(async () =>
|
|
917
|
+
// Second attempt succeeds
|
|
918
|
+
(async function* () {
|
|
919
|
+
yield {
|
|
920
|
+
candidates: [
|
|
921
|
+
{
|
|
922
|
+
content: { parts: [{ text: 'Second answer' }] },
|
|
923
|
+
finishReason: 'STOP',
|
|
924
|
+
},
|
|
925
|
+
],
|
|
926
|
+
};
|
|
927
|
+
})());
|
|
928
|
+
// 3. Send a new message
|
|
929
|
+
const stream = await chat.sendMessageStream({ message: 'Second question' }, 'prompt-id-retry-existing');
|
|
930
|
+
for await (const _ of stream) {
|
|
931
|
+
// consume stream
|
|
932
|
+
}
|
|
933
|
+
// 4. Assert the final history and metrics
|
|
934
|
+
const history = chat.getHistory();
|
|
935
|
+
expect(history.length).toBe(4);
|
|
936
|
+
// Assert that the correct metrics were reported for one empty-stream retry
|
|
937
|
+
expect(mockLogContentRetry).toHaveBeenCalledTimes(1);
|
|
938
|
+
// Explicitly verify the structure of each part to satisfy TypeScript
|
|
939
|
+
const turn1 = history[0];
|
|
940
|
+
if (!turn1?.parts?.[0] || !('text' in turn1.parts[0])) {
|
|
941
|
+
throw new Error('Test setup error: First turn is not a valid text part.');
|
|
942
|
+
}
|
|
943
|
+
expect(turn1.parts[0].text).toBe('First question');
|
|
944
|
+
const turn2 = history[1];
|
|
945
|
+
if (!turn2?.parts?.[0] || !('text' in turn2.parts[0])) {
|
|
946
|
+
throw new Error('Test setup error: Second turn is not a valid text part.');
|
|
947
|
+
}
|
|
948
|
+
expect(turn2.parts[0].text).toBe('First answer');
|
|
949
|
+
const turn3 = history[2];
|
|
950
|
+
if (!turn3?.parts?.[0] || !('text' in turn3.parts[0])) {
|
|
951
|
+
throw new Error('Test setup error: Third turn is not a valid text part.');
|
|
952
|
+
}
|
|
953
|
+
expect(turn3.parts[0].text).toBe('Second question');
|
|
954
|
+
const turn4 = history[3];
|
|
955
|
+
if (!turn4?.parts?.[0] || !('text' in turn4.parts[0])) {
|
|
956
|
+
throw new Error('Test setup error: Fourth turn is not a valid text part.');
|
|
957
|
+
}
|
|
958
|
+
expect(turn4.parts[0].text).toBe('Second answer');
|
|
959
|
+
});
|
|
960
|
+
describe('concurrency control', () => {
|
|
961
|
+
it('should queue a subsequent sendMessage call until the first one completes', async () => {
|
|
962
|
+
// 1. Create promises to manually control when the API calls resolve
|
|
963
|
+
let firstCallResolver;
|
|
964
|
+
const firstCallPromise = new Promise((resolve) => {
|
|
965
|
+
firstCallResolver = resolve;
|
|
966
|
+
});
|
|
967
|
+
let secondCallResolver;
|
|
968
|
+
const secondCallPromise = new Promise((resolve) => {
|
|
969
|
+
secondCallResolver = resolve;
|
|
970
|
+
});
|
|
971
|
+
// A standard response body for the mock
|
|
972
|
+
const mockResponse = {
|
|
973
|
+
candidates: [
|
|
974
|
+
{
|
|
975
|
+
content: { parts: [{ text: 'response' }], role: 'model' },
|
|
976
|
+
},
|
|
977
|
+
],
|
|
978
|
+
};
|
|
979
|
+
// 2. Mock the API to return our controllable promises in order
|
|
980
|
+
vi.mocked(mockModelsModule.generateContent)
|
|
981
|
+
.mockReturnValueOnce(firstCallPromise)
|
|
982
|
+
.mockReturnValueOnce(secondCallPromise);
|
|
983
|
+
// 3. Start the first message call. Do not await it yet.
|
|
984
|
+
const firstMessagePromise = chat.sendMessage({ message: 'first' }, 'prompt-1');
|
|
985
|
+
// Give the event loop a chance to run the async call up to the `await`
|
|
986
|
+
await new Promise(process.nextTick);
|
|
987
|
+
// 4. While the first call is "in-flight", start the second message call.
|
|
988
|
+
const secondMessagePromise = chat.sendMessage({ message: 'second' }, 'prompt-2');
|
|
989
|
+
// 5. CRUCIAL CHECK: At this point, only the first API call should have been made.
|
|
990
|
+
// The second call should be waiting on `sendPromise`.
|
|
991
|
+
expect(mockModelsModule.generateContent).toHaveBeenCalledTimes(1);
|
|
992
|
+
expect(mockModelsModule.generateContent).toHaveBeenCalledWith(expect.objectContaining({
|
|
993
|
+
contents: expect.arrayContaining([
|
|
994
|
+
expect.objectContaining({ parts: [{ text: 'first' }] }),
|
|
995
|
+
]),
|
|
996
|
+
}), 'prompt-1');
|
|
997
|
+
// 6. Unblock the first API call and wait for the first message to fully complete.
|
|
998
|
+
firstCallResolver(mockResponse);
|
|
999
|
+
await firstMessagePromise;
|
|
1000
|
+
// Give the event loop a chance to unblock and run the second call.
|
|
1001
|
+
await new Promise(process.nextTick);
|
|
1002
|
+
// 7. CRUCIAL CHECK: Now, the second API call should have been made.
|
|
1003
|
+
expect(mockModelsModule.generateContent).toHaveBeenCalledTimes(2);
|
|
1004
|
+
expect(mockModelsModule.generateContent).toHaveBeenCalledWith(expect.objectContaining({
|
|
1005
|
+
contents: expect.arrayContaining([
|
|
1006
|
+
expect.objectContaining({ parts: [{ text: 'second' }] }),
|
|
1007
|
+
]),
|
|
1008
|
+
}), 'prompt-2');
|
|
1009
|
+
// 8. Clean up by resolving the second call.
|
|
1010
|
+
secondCallResolver(mockResponse);
|
|
1011
|
+
await secondMessagePromise;
|
|
1012
|
+
});
|
|
1013
|
+
});
|
|
1014
|
+
it('should retry if the model returns a completely empty stream (no chunks)', async () => {
|
|
1015
|
+
// 1. Mock the API to return an empty stream first, then a valid one.
|
|
1016
|
+
vi.mocked(mockModelsModule.generateContentStream)
|
|
1017
|
+
.mockImplementationOnce(
|
|
1018
|
+
// First call resolves to an async generator that yields nothing.
|
|
1019
|
+
async () => (async function* () { })())
|
|
1020
|
+
.mockImplementationOnce(
|
|
1021
|
+
// Second call returns a valid stream.
|
|
1022
|
+
async () => (async function* () {
|
|
1023
|
+
yield {
|
|
1024
|
+
candidates: [
|
|
1025
|
+
{
|
|
1026
|
+
content: {
|
|
1027
|
+
parts: [{ text: 'Successful response after empty' }],
|
|
1028
|
+
},
|
|
1029
|
+
finishReason: 'STOP',
|
|
1030
|
+
},
|
|
1031
|
+
],
|
|
1032
|
+
};
|
|
1033
|
+
})());
|
|
1034
|
+
// 2. Call the method and consume the stream.
|
|
1035
|
+
const stream = await chat.sendMessageStream({ message: 'test empty stream' }, 'prompt-id-empty-stream');
|
|
1036
|
+
const chunks = [];
|
|
1037
|
+
for await (const chunk of stream) {
|
|
1038
|
+
chunks.push(chunk);
|
|
1039
|
+
}
|
|
1040
|
+
// 3. Assert the results.
|
|
1041
|
+
expect(mockModelsModule.generateContentStream).toHaveBeenCalledTimes(2);
|
|
1042
|
+
expect(chunks.some((c) => c.type === StreamEventType.CHUNK &&
|
|
1043
|
+
c.value.candidates?.[0]?.content?.parts?.[0]?.text ===
|
|
1044
|
+
'Successful response after empty')).toBe(true);
|
|
1045
|
+
const history = chat.getHistory();
|
|
1046
|
+
expect(history.length).toBe(2);
|
|
1047
|
+
// Explicitly verify the structure of each part to satisfy TypeScript
|
|
1048
|
+
const turn1 = history[0];
|
|
1049
|
+
if (!turn1?.parts?.[0] || !('text' in turn1.parts[0])) {
|
|
1050
|
+
throw new Error('Test setup error: First turn is not a valid text part.');
|
|
1051
|
+
}
|
|
1052
|
+
expect(turn1.parts[0].text).toBe('test empty stream');
|
|
1053
|
+
const turn2 = history[1];
|
|
1054
|
+
if (!turn2?.parts?.[0] || !('text' in turn2.parts[0])) {
|
|
1055
|
+
throw new Error('Test setup error: Second turn is not a valid text part.');
|
|
1056
|
+
}
|
|
1057
|
+
expect(turn2.parts[0].text).toBe('Successful response after empty');
|
|
1058
|
+
});
|
|
1059
|
+
it('should queue a subsequent sendMessageStream call until the first stream is fully consumed', async () => {
|
|
1060
|
+
// 1. Create a promise to manually control the stream's lifecycle
|
|
1061
|
+
let continueFirstStream;
|
|
1062
|
+
const firstStreamContinuePromise = new Promise((resolve) => {
|
|
1063
|
+
continueFirstStream = resolve;
|
|
1064
|
+
});
|
|
1065
|
+
// 2. Mock the API to return controllable async generators
|
|
1066
|
+
const firstStreamGenerator = (async function* () {
|
|
1067
|
+
yield {
|
|
1068
|
+
candidates: [
|
|
1069
|
+
{ content: { parts: [{ text: 'first response part 1' }] } },
|
|
1070
|
+
],
|
|
1071
|
+
};
|
|
1072
|
+
await firstStreamContinuePromise; // Pause the stream
|
|
1073
|
+
yield {
|
|
1074
|
+
candidates: [
|
|
1075
|
+
{
|
|
1076
|
+
content: { parts: [{ text: ' part 2' }] },
|
|
1077
|
+
finishReason: 'STOP',
|
|
1078
|
+
},
|
|
1079
|
+
],
|
|
1080
|
+
};
|
|
1081
|
+
})();
|
|
1082
|
+
const secondStreamGenerator = (async function* () {
|
|
1083
|
+
yield {
|
|
1084
|
+
candidates: [
|
|
1085
|
+
{
|
|
1086
|
+
content: { parts: [{ text: 'second response' }] },
|
|
1087
|
+
finishReason: 'STOP',
|
|
1088
|
+
},
|
|
1089
|
+
],
|
|
1090
|
+
};
|
|
1091
|
+
})();
|
|
1092
|
+
vi.mocked(mockModelsModule.generateContentStream)
|
|
1093
|
+
.mockResolvedValueOnce(firstStreamGenerator)
|
|
1094
|
+
.mockResolvedValueOnce(secondStreamGenerator);
|
|
1095
|
+
// 3. Start the first stream and consume only the first chunk to pause it
|
|
1096
|
+
const firstStream = await chat.sendMessageStream({ message: 'first' }, 'prompt-1');
|
|
1097
|
+
const firstStreamIterator = firstStream[Symbol.asyncIterator]();
|
|
1098
|
+
await firstStreamIterator.next();
|
|
1099
|
+
// 4. While the first stream is paused, start the second call. It will block.
|
|
1100
|
+
const secondStreamPromise = chat.sendMessageStream({ message: 'second' }, 'prompt-2');
|
|
1101
|
+
// 5. Assert that only one API call has been made so far.
|
|
1102
|
+
expect(mockModelsModule.generateContentStream).toHaveBeenCalledTimes(1);
|
|
1103
|
+
// 6. Unblock and fully consume the first stream to completion.
|
|
1104
|
+
continueFirstStream();
|
|
1105
|
+
await firstStreamIterator.next(); // Consume the rest of the stream
|
|
1106
|
+
await firstStreamIterator.next(); // Finish the iterator
|
|
1107
|
+
// 7. Now that the first stream is done, await the second promise to get its generator.
|
|
1108
|
+
const secondStream = await secondStreamPromise;
|
|
1109
|
+
// 8. Start consuming the second stream, which triggers its internal API call.
|
|
1110
|
+
const secondStreamIterator = secondStream[Symbol.asyncIterator]();
|
|
1111
|
+
await secondStreamIterator.next();
|
|
1112
|
+
// 9. The second API call should now have been made.
|
|
1113
|
+
expect(mockModelsModule.generateContentStream).toHaveBeenCalledTimes(2);
|
|
1114
|
+
// 10. FIX: Fully consume the second stream to ensure recordHistory is called.
|
|
1115
|
+
await secondStreamIterator.next(); // This finishes the iterator.
|
|
1116
|
+
// 11. Final check on history.
|
|
1117
|
+
const history = chat.getHistory();
|
|
1118
|
+
expect(history.length).toBe(4);
|
|
1119
|
+
const turn4 = history[3];
|
|
1120
|
+
if (!turn4?.parts?.[0] || !('text' in turn4.parts[0])) {
|
|
1121
|
+
throw new Error('Test setup error: Fourth turn is not a valid text part.');
|
|
1122
|
+
}
|
|
1123
|
+
expect(turn4.parts[0].text).toBe('second response');
|
|
1124
|
+
});
|
|
1125
|
+
it('should discard valid partial content from a failed attempt upon retry', async () => {
|
|
1126
|
+
// ARRANGE: Mock the stream to fail on the first attempt after yielding some valid content.
|
|
1127
|
+
vi.mocked(mockModelsModule.generateContentStream)
|
|
1128
|
+
.mockImplementationOnce(async () =>
|
|
1129
|
+
// First attempt: yields one valid chunk, then one invalid chunk
|
|
1130
|
+
(async function* () {
|
|
1131
|
+
yield {
|
|
1132
|
+
candidates: [
|
|
1133
|
+
{
|
|
1134
|
+
content: {
|
|
1135
|
+
parts: [{ text: 'This valid part should be discarded' }],
|
|
1136
|
+
},
|
|
1137
|
+
},
|
|
1138
|
+
],
|
|
1139
|
+
};
|
|
1140
|
+
yield {
|
|
1141
|
+
candidates: [{ content: { parts: [{ text: '' }] } }], // Invalid chunk triggers retry
|
|
1142
|
+
};
|
|
1143
|
+
})())
|
|
1144
|
+
.mockImplementationOnce(async () =>
|
|
1145
|
+
// Second attempt (the retry): succeeds
|
|
1146
|
+
(async function* () {
|
|
1147
|
+
yield {
|
|
1148
|
+
candidates: [
|
|
1149
|
+
{
|
|
1150
|
+
content: {
|
|
1151
|
+
parts: [{ text: 'Successful final response' }],
|
|
1152
|
+
},
|
|
1153
|
+
finishReason: 'STOP',
|
|
1154
|
+
},
|
|
1155
|
+
],
|
|
1156
|
+
};
|
|
1157
|
+
})());
|
|
1158
|
+
// ACT: Send a message and consume the stream
|
|
1159
|
+
const stream = await chat.sendMessageStream({ message: 'test' }, 'prompt-id-discard-test');
|
|
1160
|
+
const events = [];
|
|
1161
|
+
for await (const event of stream) {
|
|
1162
|
+
events.push(event);
|
|
1163
|
+
}
|
|
1164
|
+
// ASSERT
|
|
1165
|
+
// Check that a retry happened
|
|
1166
|
+
expect(mockModelsModule.generateContentStream).toHaveBeenCalledTimes(2);
|
|
1167
|
+
expect(events.some((e) => e.type === StreamEventType.RETRY)).toBe(true);
|
|
1168
|
+
// Check the final recorded history
|
|
1169
|
+
const history = chat.getHistory();
|
|
1170
|
+
expect(history.length).toBe(2); // user turn + final model turn
|
|
1171
|
+
const modelTurn = history[1];
|
|
1172
|
+
// The model turn should only contain the text from the successful attempt
|
|
1173
|
+
expect(modelTurn.parts[0].text).toBe('Successful final response');
|
|
1174
|
+
// It should NOT contain any text from the failed attempt
|
|
1175
|
+
expect(modelTurn.parts[0].text).not.toContain('This valid part should be discarded');
|
|
1176
|
+
});
|
|
1177
|
+
});
|
|
1178
|
+
//# sourceMappingURL=geminiChat.test.js.map
|