@google/gemini-cli-core 0.0.3-preview.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 +202 -0
- package/README.md +310 -0
- package/dist/.last_build +0 -0
- package/dist/google-gemini-cli-core-0.3.0-preview.3.tgz +0 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/src/__mocks__/fs/promises.d.ts +11 -0
- package/dist/src/__mocks__/fs/promises.js +17 -0
- package/dist/src/__mocks__/fs/promises.js.map +1 -0
- package/dist/src/code_assist/codeAssist.d.ts +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 +353 -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 +427 -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/config.d.ts +318 -0
- package/dist/src/config/config.js +633 -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 +585 -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 +9 -0
- package/dist/src/config/models.js +10 -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/client.d.ts +65 -0
- package/dist/src/core/client.js +689 -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 +1857 -0
- package/dist/src/core/client.test.js.map +1 -0
- package/dist/src/core/contentGenerator.d.ts +33 -0
- package/dist/src/core/contentGenerator.js +80 -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 +124 -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 +605 -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 +923 -0
- package/dist/src/core/coreToolScheduler.test.js.map +1 -0
- package/dist/src/core/geminiChat.d.ts +122 -0
- package/dist/src/core/geminiChat.js +547 -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 +875 -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/logger.d.ts +60 -0
- package/dist/src/core/logger.js +360 -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 +534 -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/prompts.d.ts +12 -0
- package/dist/src/core/prompts.js +359 -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 +214 -0
- package/dist/src/core/prompts.test.js.map +1 -0
- package/dist/src/core/subagent.d.ts +236 -0
- package/dist/src/core/subagent.js +485 -0
- package/dist/src/core/subagent.js.map +1 -0
- package/dist/src/core/subagent.test.d.ts +6 -0
- package/dist/src/core/subagent.test.js +520 -0
- package/dist/src/core/subagent.test.js.map +1 -0
- package/dist/src/core/tokenLimits.d.ts +10 -0
- package/dist/src/core/tokenLimits.js +28 -0
- package/dist/src/core/tokenLimits.js.map +1 -0
- package/dist/src/core/turn.d.ts +125 -0
- package/dist/src/core/turn.js +154 -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 +388 -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 +109 -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 +418 -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 +155 -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 +22 -0
- package/dist/src/ide/process-utils.js +153 -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 +72 -0
- package/dist/src/ide/process-utils.test.js.map +1 -0
- package/dist/src/index.d.ts +81 -0
- package/dist/src/index.js +90 -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 +206 -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 +558 -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/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 +407 -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 +32 -0
- package/dist/src/telemetry/constants.js +33 -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 +21 -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 +54 -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 +404 -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 +658 -0
- package/dist/src/telemetry/loggers.test.js.map +1 -0
- package/dist/src/telemetry/metrics.d.ts +36 -0
- package/dist/src/telemetry/metrics.js +208 -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 +163 -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 +220 -0
- package/dist/src/telemetry/types.js +383 -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 +423 -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 +713 -0
- package/dist/src/tools/edit.test.js.map +1 -0
- package/dist/src/tools/glob.d.ts +52 -0
- package/dist/src/tools/glob.js +236 -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 +47 -0
- package/dist/src/tools/grep.js +517 -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 +295 -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 +40 -0
- package/dist/src/tools/memoryTool.js +296 -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 +298 -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 +127 -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 +414 -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 +368 -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 +22 -0
- package/dist/src/tools/shell.js +320 -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 +336 -0
- package/dist/src/tools/shell.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-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 +274 -0
- package/dist/src/tools/tools.js +250 -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 +27 -0
- package/dist/src/tools/web-fetch.js +243 -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 +114 -0
- package/dist/src/tools/web-fetch.test.js.map +1 -0
- package/dist/src/tools/web-search.d.ts +49 -0
- package/dist/src/tools/web-search.js +137 -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 +207 -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 +314 -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 +531 -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/editCorrector.d.ts +53 -0
- package/dist/src/utils/editCorrector.js +545 -0
- package/dist/src/utils/editCorrector.js.map +1 -0
- package/dist/src/utils/editCorrector.test.d.ts +6 -0
- package/dist/src/utils/editCorrector.test.js +564 -0
- package/dist/src/utils/editCorrector.test.js.map +1 -0
- package/dist/src/utils/editor.d.ts +28 -0
- package/dist/src/utils/editor.js +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 +140 -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 +52 -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 +118 -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 +253 -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/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/quotaErrorDetection.d.ts +18 -0
- package/dist/src/utils/quotaErrorDetection.js +65 -0
- package/dist/src/utils/quotaErrorDetection.js.map +1 -0
- package/dist/src/utils/retry.d.ts +30 -0
- package/dist/src/utils/retry.js +276 -0
- package/dist/src/utils/retry.js.map +1 -0
- package/dist/src/utils/retry.test.d.ts +6 -0
- package/dist/src/utils/retry.test.js +325 -0
- package/dist/src/utils/retry.test.js.map +1 -0
- package/dist/src/utils/safeJsonStringify.d.ts +13 -0
- package/dist/src/utils/safeJsonStringify.js +25 -0
- package/dist/src/utils/safeJsonStringify.js.map +1 -0
- package/dist/src/utils/safeJsonStringify.test.d.ts +6 -0
- package/dist/src/utils/safeJsonStringify.test.js +61 -0
- package/dist/src/utils/safeJsonStringify.test.js.map +1 -0
- package/dist/src/utils/schemaValidator.d.ts +15 -0
- package/dist/src/utils/schemaValidator.js +38 -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 +117 -0
- package/dist/src/utils/shell-utils.js +370 -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 +332 -0
- package/dist/src/utils/shell-utils.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/tsconfig.tsbuildinfo +1 -0
- package/package.json +85 -0
|
@@ -0,0 +1,875 @@
|
|
|
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 } 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 throw an error when attempting to add a user turn after another user turn', async () => {
|
|
59
|
+
// 1. Setup: Create a history that already ends with a user turn (a functionResponse).
|
|
60
|
+
const initialHistory = [
|
|
61
|
+
{ role: 'user', parts: [{ text: 'Initial prompt' }] },
|
|
62
|
+
{
|
|
63
|
+
role: 'model',
|
|
64
|
+
parts: [{ functionCall: { name: 'test_tool', args: {} } }],
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
role: 'user',
|
|
68
|
+
parts: [{ functionResponse: { name: 'test_tool', response: {} } }],
|
|
69
|
+
},
|
|
70
|
+
];
|
|
71
|
+
chat.setHistory(initialHistory);
|
|
72
|
+
// 2. Mock a valid model response so the call doesn't fail for other reasons.
|
|
73
|
+
const mockResponse = {
|
|
74
|
+
candidates: [
|
|
75
|
+
{ content: { role: 'model', parts: [{ text: 'some response' }] } },
|
|
76
|
+
],
|
|
77
|
+
};
|
|
78
|
+
vi.mocked(mockModelsModule.generateContent).mockResolvedValue(mockResponse);
|
|
79
|
+
// 3. Action & Assert: Expect that sending another user message immediately
|
|
80
|
+
// after a user-role turn throws the specific error.
|
|
81
|
+
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.');
|
|
82
|
+
});
|
|
83
|
+
it('should preserve text parts that are in the same response as a thought', async () => {
|
|
84
|
+
// 1. Mock the API to return a single response containing both a thought and visible text.
|
|
85
|
+
const mixedContentResponse = {
|
|
86
|
+
candidates: [
|
|
87
|
+
{
|
|
88
|
+
content: {
|
|
89
|
+
role: 'model',
|
|
90
|
+
parts: [
|
|
91
|
+
{ thought: 'This is a thought.' },
|
|
92
|
+
{ text: 'This is the visible text that should not be lost.' },
|
|
93
|
+
],
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
],
|
|
97
|
+
};
|
|
98
|
+
vi.mocked(mockModelsModule.generateContent).mockResolvedValue(mixedContentResponse);
|
|
99
|
+
// 2. Action: Send a standard, non-streaming message.
|
|
100
|
+
await chat.sendMessage({ message: 'test message' }, 'prompt-id-mixed-response');
|
|
101
|
+
// 3. Assert: Check the final state of the history.
|
|
102
|
+
const history = chat.getHistory();
|
|
103
|
+
// The history should contain two turns: the user's message and the model's response.
|
|
104
|
+
expect(history.length).toBe(2);
|
|
105
|
+
const modelTurn = history[1];
|
|
106
|
+
expect(modelTurn.role).toBe('model');
|
|
107
|
+
// CRUCIAL ASSERTION:
|
|
108
|
+
// Buggy code would discard the entire response because a "thought" was present,
|
|
109
|
+
// resulting in an empty placeholder turn with 0 parts.
|
|
110
|
+
// The corrected code will pass, preserving the single visible text part.
|
|
111
|
+
expect(modelTurn?.parts?.length).toBe(1);
|
|
112
|
+
expect(modelTurn?.parts[0].text).toBe('This is the visible text that should not be lost.');
|
|
113
|
+
});
|
|
114
|
+
it('should add a placeholder model turn when a tool call is followed by an empty model response', async () => {
|
|
115
|
+
// 1. Setup: A history where the model has just made a function call.
|
|
116
|
+
const initialHistory = [
|
|
117
|
+
{
|
|
118
|
+
role: 'user',
|
|
119
|
+
parts: [{ text: 'Find a good Italian restaurant for me.' }],
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
role: 'model',
|
|
123
|
+
parts: [
|
|
124
|
+
{
|
|
125
|
+
functionCall: {
|
|
126
|
+
name: 'find_restaurant',
|
|
127
|
+
args: { cuisine: 'Italian' },
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
],
|
|
131
|
+
},
|
|
132
|
+
];
|
|
133
|
+
chat.setHistory(initialHistory);
|
|
134
|
+
// 2. Mock the API to return an empty/thought-only response.
|
|
135
|
+
const emptyModelResponse = {
|
|
136
|
+
candidates: [
|
|
137
|
+
{ content: { role: 'model', parts: [{ thought: true }] } },
|
|
138
|
+
],
|
|
139
|
+
};
|
|
140
|
+
vi.mocked(mockModelsModule.generateContent).mockResolvedValue(emptyModelResponse);
|
|
141
|
+
// 3. Action: Send the function response back to the model.
|
|
142
|
+
await chat.sendMessage({
|
|
143
|
+
message: {
|
|
144
|
+
functionResponse: {
|
|
145
|
+
name: 'find_restaurant',
|
|
146
|
+
response: { name: 'Vesuvio' },
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
}, 'prompt-id-1');
|
|
150
|
+
// 4. Assert: The history should now have four valid, alternating turns.
|
|
151
|
+
const history = chat.getHistory();
|
|
152
|
+
expect(history.length).toBe(4);
|
|
153
|
+
// The final turn must be the empty model placeholder.
|
|
154
|
+
const lastTurn = history[3];
|
|
155
|
+
expect(lastTurn.role).toBe('model');
|
|
156
|
+
expect(lastTurn?.parts?.length).toBe(0);
|
|
157
|
+
// The second-to-last turn must be the function response we sent.
|
|
158
|
+
const secondToLastTurn = history[2];
|
|
159
|
+
expect(secondToLastTurn.role).toBe('user');
|
|
160
|
+
expect(secondToLastTurn?.parts[0].functionResponse).toBeDefined();
|
|
161
|
+
});
|
|
162
|
+
it('should call generateContent with the correct parameters', async () => {
|
|
163
|
+
const response = {
|
|
164
|
+
candidates: [
|
|
165
|
+
{
|
|
166
|
+
content: {
|
|
167
|
+
parts: [{ text: 'response' }],
|
|
168
|
+
role: 'model',
|
|
169
|
+
},
|
|
170
|
+
finishReason: 'STOP',
|
|
171
|
+
index: 0,
|
|
172
|
+
safetyRatings: [],
|
|
173
|
+
},
|
|
174
|
+
],
|
|
175
|
+
text: () => 'response',
|
|
176
|
+
};
|
|
177
|
+
vi.mocked(mockModelsModule.generateContent).mockResolvedValue(response);
|
|
178
|
+
await chat.sendMessage({ message: 'hello' }, 'prompt-id-1');
|
|
179
|
+
expect(mockModelsModule.generateContent).toHaveBeenCalledWith({
|
|
180
|
+
model: 'gemini-pro',
|
|
181
|
+
contents: [{ role: 'user', parts: [{ text: 'hello' }] }],
|
|
182
|
+
config: {},
|
|
183
|
+
}, 'prompt-id-1');
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
describe('sendMessageStream', () => {
|
|
187
|
+
it('should not consolidate text into a part that also contains a functionCall', async () => {
|
|
188
|
+
// 1. Mock the API to stream a malformed part followed by a valid text part.
|
|
189
|
+
const multiChunkStream = (async function* () {
|
|
190
|
+
// This malformed part has both text and a functionCall.
|
|
191
|
+
yield {
|
|
192
|
+
candidates: [
|
|
193
|
+
{
|
|
194
|
+
content: {
|
|
195
|
+
role: 'model',
|
|
196
|
+
parts: [
|
|
197
|
+
{
|
|
198
|
+
text: 'Some text',
|
|
199
|
+
functionCall: { name: 'do_stuff', args: {} },
|
|
200
|
+
},
|
|
201
|
+
],
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
],
|
|
205
|
+
};
|
|
206
|
+
// This valid text part should NOT be merged into the malformed one.
|
|
207
|
+
yield {
|
|
208
|
+
candidates: [
|
|
209
|
+
{
|
|
210
|
+
content: {
|
|
211
|
+
role: 'model',
|
|
212
|
+
parts: [{ text: ' that should not be merged.' }],
|
|
213
|
+
},
|
|
214
|
+
},
|
|
215
|
+
],
|
|
216
|
+
};
|
|
217
|
+
})();
|
|
218
|
+
vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(multiChunkStream);
|
|
219
|
+
// 2. Action: Send a message and consume the stream.
|
|
220
|
+
const stream = await chat.sendMessageStream({ message: 'test message' }, 'prompt-id-malformed-chunk');
|
|
221
|
+
for await (const _ of stream) {
|
|
222
|
+
// Consume the stream to trigger history recording.
|
|
223
|
+
}
|
|
224
|
+
// 3. Assert: Check that the final history was not incorrectly consolidated.
|
|
225
|
+
const history = chat.getHistory();
|
|
226
|
+
expect(history.length).toBe(2);
|
|
227
|
+
const modelTurn = history[1];
|
|
228
|
+
// CRUCIAL ASSERTION: There should be two separate parts.
|
|
229
|
+
// The old, non-strict logic would incorrectly merge them, resulting in one part.
|
|
230
|
+
expect(modelTurn?.parts?.length).toBe(2);
|
|
231
|
+
// Verify the contents of each part.
|
|
232
|
+
expect(modelTurn?.parts[0].text).toBe('Some text');
|
|
233
|
+
expect(modelTurn?.parts[0].functionCall).toBeDefined();
|
|
234
|
+
expect(modelTurn?.parts[1].text).toBe(' that should not be merged.');
|
|
235
|
+
});
|
|
236
|
+
it('should consolidate subsequent text chunks after receiving an empty text chunk', async () => {
|
|
237
|
+
// 1. Mock the API to return a stream where one chunk is just an empty text part.
|
|
238
|
+
const multiChunkStream = (async function* () {
|
|
239
|
+
yield {
|
|
240
|
+
candidates: [
|
|
241
|
+
{ content: { role: 'model', parts: [{ text: 'Hello' }] } },
|
|
242
|
+
],
|
|
243
|
+
};
|
|
244
|
+
// FIX: The original test used { text: '' }, which is invalid.
|
|
245
|
+
// A chunk can be empty but still valid. This chunk is now removed
|
|
246
|
+
// as the important part is consolidating what comes after.
|
|
247
|
+
yield {
|
|
248
|
+
candidates: [
|
|
249
|
+
{ content: { role: 'model', parts: [{ text: ' World!' }] } },
|
|
250
|
+
],
|
|
251
|
+
};
|
|
252
|
+
})();
|
|
253
|
+
vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(multiChunkStream);
|
|
254
|
+
// 2. Action: Send a message and consume the stream.
|
|
255
|
+
const stream = await chat.sendMessageStream({ message: 'test message' }, 'prompt-id-empty-chunk-consolidation');
|
|
256
|
+
for await (const _ of stream) {
|
|
257
|
+
// Consume the stream
|
|
258
|
+
}
|
|
259
|
+
// 3. Assert: Check that the final history was correctly consolidated.
|
|
260
|
+
const history = chat.getHistory();
|
|
261
|
+
expect(history.length).toBe(2);
|
|
262
|
+
const modelTurn = history[1];
|
|
263
|
+
expect(modelTurn?.parts?.length).toBe(1);
|
|
264
|
+
expect(modelTurn?.parts[0].text).toBe('Hello World!');
|
|
265
|
+
});
|
|
266
|
+
it('should consolidate adjacent text parts that arrive in separate stream chunks', async () => {
|
|
267
|
+
// 1. Mock the API to return a stream of multiple, adjacent text chunks.
|
|
268
|
+
const multiChunkStream = (async function* () {
|
|
269
|
+
yield {
|
|
270
|
+
candidates: [
|
|
271
|
+
{ content: { role: 'model', parts: [{ text: 'This is the ' }] } },
|
|
272
|
+
],
|
|
273
|
+
};
|
|
274
|
+
yield {
|
|
275
|
+
candidates: [
|
|
276
|
+
{ content: { role: 'model', parts: [{ text: 'first part.' }] } },
|
|
277
|
+
],
|
|
278
|
+
};
|
|
279
|
+
// This function call should break the consolidation.
|
|
280
|
+
yield {
|
|
281
|
+
candidates: [
|
|
282
|
+
{
|
|
283
|
+
content: {
|
|
284
|
+
role: 'model',
|
|
285
|
+
parts: [{ functionCall: { name: 'do_stuff', args: {} } }],
|
|
286
|
+
},
|
|
287
|
+
},
|
|
288
|
+
],
|
|
289
|
+
};
|
|
290
|
+
yield {
|
|
291
|
+
candidates: [
|
|
292
|
+
{
|
|
293
|
+
content: {
|
|
294
|
+
role: 'model',
|
|
295
|
+
parts: [{ text: 'This is the second part.' }],
|
|
296
|
+
},
|
|
297
|
+
},
|
|
298
|
+
],
|
|
299
|
+
};
|
|
300
|
+
})();
|
|
301
|
+
vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(multiChunkStream);
|
|
302
|
+
// 2. Action: Send a message and consume the stream.
|
|
303
|
+
const stream = await chat.sendMessageStream({ message: 'test message' }, 'prompt-id-multi-chunk');
|
|
304
|
+
for await (const _ of stream) {
|
|
305
|
+
// Consume the stream to trigger history recording.
|
|
306
|
+
}
|
|
307
|
+
// 3. Assert: Check that the final history was correctly consolidated.
|
|
308
|
+
const history = chat.getHistory();
|
|
309
|
+
// The history should contain the user's turn and ONE consolidated model turn.
|
|
310
|
+
expect(history.length).toBe(2);
|
|
311
|
+
const modelTurn = history[1];
|
|
312
|
+
expect(modelTurn.role).toBe('model');
|
|
313
|
+
// The model turn should have 3 distinct parts: the merged text, the function call, and the final text.
|
|
314
|
+
expect(modelTurn?.parts?.length).toBe(3);
|
|
315
|
+
expect(modelTurn?.parts[0].text).toBe('This is the first part.');
|
|
316
|
+
expect(modelTurn.parts[1].functionCall).toBeDefined();
|
|
317
|
+
expect(modelTurn.parts[2].text).toBe('This is the second part.');
|
|
318
|
+
});
|
|
319
|
+
it('should preserve text parts that stream in the same chunk as a thought', async () => {
|
|
320
|
+
// 1. Mock the API to return a single chunk containing both a thought and visible text.
|
|
321
|
+
const mixedContentStream = (async function* () {
|
|
322
|
+
yield {
|
|
323
|
+
candidates: [
|
|
324
|
+
{
|
|
325
|
+
content: {
|
|
326
|
+
role: 'model',
|
|
327
|
+
parts: [
|
|
328
|
+
{ thought: 'This is a thought.' },
|
|
329
|
+
{ text: 'This is the visible text that should not be lost.' },
|
|
330
|
+
],
|
|
331
|
+
},
|
|
332
|
+
},
|
|
333
|
+
],
|
|
334
|
+
};
|
|
335
|
+
})();
|
|
336
|
+
vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(mixedContentStream);
|
|
337
|
+
// 2. Action: Send a message and fully consume the stream to trigger history recording.
|
|
338
|
+
const stream = await chat.sendMessageStream({ message: 'test message' }, 'prompt-id-mixed-chunk');
|
|
339
|
+
for await (const _ of stream) {
|
|
340
|
+
// This loop consumes the stream.
|
|
341
|
+
}
|
|
342
|
+
// 3. Assert: Check the final state of the history.
|
|
343
|
+
const history = chat.getHistory();
|
|
344
|
+
// The history should contain two turns: the user's message and the model's response.
|
|
345
|
+
expect(history.length).toBe(2);
|
|
346
|
+
const modelTurn = history[1];
|
|
347
|
+
expect(modelTurn.role).toBe('model');
|
|
348
|
+
// CRUCIAL ASSERTION:
|
|
349
|
+
// The buggy code would fail here, resulting in parts.length being 0.
|
|
350
|
+
// The corrected code will pass, preserving the single visible text part.
|
|
351
|
+
expect(modelTurn?.parts?.length).toBe(1);
|
|
352
|
+
expect(modelTurn?.parts[0].text).toBe('This is the visible text that should not be lost.');
|
|
353
|
+
});
|
|
354
|
+
it('should add a placeholder model turn when a tool call is followed by an empty stream response', async () => {
|
|
355
|
+
// 1. Setup: A history where the model has just made a function call.
|
|
356
|
+
const initialHistory = [
|
|
357
|
+
{
|
|
358
|
+
role: 'user',
|
|
359
|
+
parts: [{ text: 'Find a good Italian restaurant for me.' }],
|
|
360
|
+
},
|
|
361
|
+
{
|
|
362
|
+
role: 'model',
|
|
363
|
+
parts: [
|
|
364
|
+
{
|
|
365
|
+
functionCall: {
|
|
366
|
+
name: 'find_restaurant',
|
|
367
|
+
args: { cuisine: 'Italian' },
|
|
368
|
+
},
|
|
369
|
+
},
|
|
370
|
+
],
|
|
371
|
+
},
|
|
372
|
+
];
|
|
373
|
+
chat.setHistory(initialHistory);
|
|
374
|
+
// 2. Mock the API to return an empty/thought-only stream.
|
|
375
|
+
const emptyStreamResponse = (async function* () {
|
|
376
|
+
yield {
|
|
377
|
+
candidates: [
|
|
378
|
+
{ content: { role: 'model', parts: [{ thought: true }] } },
|
|
379
|
+
],
|
|
380
|
+
};
|
|
381
|
+
})();
|
|
382
|
+
vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(emptyStreamResponse);
|
|
383
|
+
// 3. Action: Send the function response back to the model and consume the stream.
|
|
384
|
+
const stream = await chat.sendMessageStream({
|
|
385
|
+
message: {
|
|
386
|
+
functionResponse: {
|
|
387
|
+
name: 'find_restaurant',
|
|
388
|
+
response: { name: 'Vesuvio' },
|
|
389
|
+
},
|
|
390
|
+
},
|
|
391
|
+
}, 'prompt-id-stream-1');
|
|
392
|
+
for await (const _ of stream) {
|
|
393
|
+
// This loop consumes the stream to trigger the internal logic.
|
|
394
|
+
}
|
|
395
|
+
// 4. Assert: The history should now have four valid, alternating turns.
|
|
396
|
+
const history = chat.getHistory();
|
|
397
|
+
expect(history.length).toBe(4);
|
|
398
|
+
// The final turn must be the empty model placeholder.
|
|
399
|
+
const lastTurn = history[3];
|
|
400
|
+
expect(lastTurn.role).toBe('model');
|
|
401
|
+
expect(lastTurn?.parts?.length).toBe(0);
|
|
402
|
+
// The second-to-last turn must be the function response we sent.
|
|
403
|
+
const secondToLastTurn = history[2];
|
|
404
|
+
expect(secondToLastTurn.role).toBe('user');
|
|
405
|
+
expect(secondToLastTurn?.parts[0].functionResponse).toBeDefined();
|
|
406
|
+
});
|
|
407
|
+
it('should call generateContentStream with the correct parameters', async () => {
|
|
408
|
+
const response = (async function* () {
|
|
409
|
+
yield {
|
|
410
|
+
candidates: [
|
|
411
|
+
{
|
|
412
|
+
content: {
|
|
413
|
+
parts: [{ text: 'response' }],
|
|
414
|
+
role: 'model',
|
|
415
|
+
},
|
|
416
|
+
finishReason: 'STOP',
|
|
417
|
+
index: 0,
|
|
418
|
+
safetyRatings: [],
|
|
419
|
+
},
|
|
420
|
+
],
|
|
421
|
+
text: () => 'response',
|
|
422
|
+
};
|
|
423
|
+
})();
|
|
424
|
+
vi.mocked(mockModelsModule.generateContentStream).mockResolvedValue(response);
|
|
425
|
+
const stream = await chat.sendMessageStream({ message: 'hello' }, 'prompt-id-1');
|
|
426
|
+
for await (const _ of stream) {
|
|
427
|
+
// consume stream to trigger internal logic
|
|
428
|
+
}
|
|
429
|
+
expect(mockModelsModule.generateContentStream).toHaveBeenCalledWith({
|
|
430
|
+
model: 'gemini-pro',
|
|
431
|
+
contents: [{ role: 'user', parts: [{ text: 'hello' }] }],
|
|
432
|
+
config: {},
|
|
433
|
+
}, 'prompt-id-1');
|
|
434
|
+
});
|
|
435
|
+
});
|
|
436
|
+
describe('recordHistory', () => {
|
|
437
|
+
const userInput = {
|
|
438
|
+
role: 'user',
|
|
439
|
+
parts: [{ text: 'User input' }],
|
|
440
|
+
};
|
|
441
|
+
it('should consolidate all consecutive model turns into a single turn', () => {
|
|
442
|
+
const userInput = {
|
|
443
|
+
role: 'user',
|
|
444
|
+
parts: [{ text: 'User input' }],
|
|
445
|
+
};
|
|
446
|
+
// This simulates a multi-part model response with different part types.
|
|
447
|
+
const modelOutput = [
|
|
448
|
+
{ role: 'model', parts: [{ text: 'Thinking...' }] },
|
|
449
|
+
{
|
|
450
|
+
role: 'model',
|
|
451
|
+
parts: [{ functionCall: { name: 'do_stuff', args: {} } }],
|
|
452
|
+
},
|
|
453
|
+
];
|
|
454
|
+
// @ts-expect-error Accessing private method for testing
|
|
455
|
+
chat.recordHistory(userInput, modelOutput);
|
|
456
|
+
const history = chat.getHistory();
|
|
457
|
+
// The history should contain the user's turn and ONE consolidated model turn.
|
|
458
|
+
// The old code would fail here, resulting in a length of 3.
|
|
459
|
+
//expect(history).toBe([]);
|
|
460
|
+
expect(history.length).toBe(2);
|
|
461
|
+
const modelTurn = history[1];
|
|
462
|
+
expect(modelTurn.role).toBe('model');
|
|
463
|
+
// The consolidated turn should contain both the text part and the functionCall part.
|
|
464
|
+
expect(modelTurn?.parts?.length).toBe(2);
|
|
465
|
+
expect(modelTurn?.parts[0].text).toBe('Thinking...');
|
|
466
|
+
expect(modelTurn?.parts[1].functionCall).toBeDefined();
|
|
467
|
+
});
|
|
468
|
+
it('should add a placeholder model turn when a tool call is followed by an empty response', () => {
|
|
469
|
+
// 1. Setup: A history where the model has just made a function call.
|
|
470
|
+
const initialHistory = [
|
|
471
|
+
{ role: 'user', parts: [{ text: 'Initial prompt' }] },
|
|
472
|
+
{
|
|
473
|
+
role: 'model',
|
|
474
|
+
parts: [{ functionCall: { name: 'test_tool', args: {} } }],
|
|
475
|
+
},
|
|
476
|
+
];
|
|
477
|
+
chat.setHistory(initialHistory);
|
|
478
|
+
// 2. Action: The user provides the tool's response, and the model's
|
|
479
|
+
// final output is empty (e.g., just a thought, which gets filtered out).
|
|
480
|
+
const functionResponse = {
|
|
481
|
+
role: 'user',
|
|
482
|
+
parts: [{ functionResponse: { name: 'test_tool', response: {} } }],
|
|
483
|
+
};
|
|
484
|
+
const emptyModelOutput = [];
|
|
485
|
+
// @ts-expect-error Accessing private method for testing
|
|
486
|
+
chat.recordHistory(functionResponse, emptyModelOutput, [
|
|
487
|
+
functionResponse,
|
|
488
|
+
]);
|
|
489
|
+
// 3. Assert: The history should now have four valid, alternating turns.
|
|
490
|
+
const history = chat.getHistory();
|
|
491
|
+
expect(history.length).toBe(4);
|
|
492
|
+
// The final turn must be the empty model placeholder.
|
|
493
|
+
const lastTurn = history[3];
|
|
494
|
+
expect(lastTurn.role).toBe('model');
|
|
495
|
+
expect(lastTurn?.parts?.length).toBe(0);
|
|
496
|
+
// The second-to-last turn must be the function response we provided.
|
|
497
|
+
const secondToLastTurn = history[2];
|
|
498
|
+
expect(secondToLastTurn.role).toBe('user');
|
|
499
|
+
expect(secondToLastTurn?.parts[0].functionResponse).toBeDefined();
|
|
500
|
+
});
|
|
501
|
+
it('should add user input and a single model output to history', () => {
|
|
502
|
+
const modelOutput = [
|
|
503
|
+
{ role: 'model', parts: [{ text: 'Model output' }] },
|
|
504
|
+
];
|
|
505
|
+
// @ts-expect-error Accessing private method for testing
|
|
506
|
+
chat.recordHistory(userInput, modelOutput);
|
|
507
|
+
const history = chat.getHistory();
|
|
508
|
+
expect(history.length).toBe(2);
|
|
509
|
+
expect(history[0]).toEqual(userInput);
|
|
510
|
+
expect(history[1]).toEqual(modelOutput[0]);
|
|
511
|
+
});
|
|
512
|
+
it('should consolidate adjacent text parts from multiple content objects', () => {
|
|
513
|
+
const modelOutput = [
|
|
514
|
+
{ role: 'model', parts: [{ text: 'Part 1.' }] },
|
|
515
|
+
{ role: 'model', parts: [{ text: ' Part 2.' }] },
|
|
516
|
+
{ role: 'model', parts: [{ text: ' Part 3.' }] },
|
|
517
|
+
];
|
|
518
|
+
// @ts-expect-error Accessing private method for testing
|
|
519
|
+
chat.recordHistory(userInput, modelOutput);
|
|
520
|
+
const history = chat.getHistory();
|
|
521
|
+
expect(history.length).toBe(2);
|
|
522
|
+
expect(history[1].role).toBe('model');
|
|
523
|
+
expect(history[1].parts).toEqual([{ text: 'Part 1. Part 2. Part 3.' }]);
|
|
524
|
+
});
|
|
525
|
+
it('should add an empty placeholder turn if modelOutput is empty', () => {
|
|
526
|
+
// This simulates receiving a pre-filtered, thought-only response.
|
|
527
|
+
const emptyModelOutput = [];
|
|
528
|
+
// @ts-expect-error Accessing private method for testing
|
|
529
|
+
chat.recordHistory(userInput, emptyModelOutput);
|
|
530
|
+
const history = chat.getHistory();
|
|
531
|
+
expect(history.length).toBe(2);
|
|
532
|
+
expect(history[0]).toEqual(userInput);
|
|
533
|
+
expect(history[1].role).toBe('model');
|
|
534
|
+
expect(history[1].parts).toEqual([]);
|
|
535
|
+
});
|
|
536
|
+
it('should preserve model outputs with undefined or empty parts arrays', () => {
|
|
537
|
+
const malformedOutput = [
|
|
538
|
+
{ role: 'model', parts: [{ text: 'Text part' }] },
|
|
539
|
+
{ role: 'model', parts: undefined },
|
|
540
|
+
{ role: 'model', parts: [] },
|
|
541
|
+
];
|
|
542
|
+
// @ts-expect-error Accessing private method for testing
|
|
543
|
+
chat.recordHistory(userInput, malformedOutput);
|
|
544
|
+
const history = chat.getHistory();
|
|
545
|
+
expect(history.length).toBe(4); // userInput + 3 model turns
|
|
546
|
+
expect(history[1].parts).toEqual([{ text: 'Text part' }]);
|
|
547
|
+
expect(history[2].parts).toBeUndefined();
|
|
548
|
+
expect(history[3].parts).toEqual([]);
|
|
549
|
+
});
|
|
550
|
+
it('should not consolidate content with different roles', () => {
|
|
551
|
+
const mixedOutput = [
|
|
552
|
+
{ role: 'model', parts: [{ text: 'Model 1' }] },
|
|
553
|
+
{ role: 'user', parts: [{ text: 'Unexpected User' }] },
|
|
554
|
+
{ role: 'model', parts: [{ text: 'Model 2' }] },
|
|
555
|
+
];
|
|
556
|
+
// @ts-expect-error Accessing private method for testing
|
|
557
|
+
chat.recordHistory(userInput, mixedOutput);
|
|
558
|
+
const history = chat.getHistory();
|
|
559
|
+
expect(history.length).toBe(4); // userInput, model1, unexpected_user, model2
|
|
560
|
+
expect(history[1]).toEqual(mixedOutput[0]);
|
|
561
|
+
expect(history[2]).toEqual(mixedOutput[1]);
|
|
562
|
+
expect(history[3]).toEqual(mixedOutput[2]);
|
|
563
|
+
});
|
|
564
|
+
});
|
|
565
|
+
describe('addHistory', () => {
|
|
566
|
+
it('should add a new content item to the history', () => {
|
|
567
|
+
const newContent = {
|
|
568
|
+
role: 'user',
|
|
569
|
+
parts: [{ text: 'A new message' }],
|
|
570
|
+
};
|
|
571
|
+
chat.addHistory(newContent);
|
|
572
|
+
const history = chat.getHistory();
|
|
573
|
+
expect(history.length).toBe(1);
|
|
574
|
+
expect(history[0]).toEqual(newContent);
|
|
575
|
+
});
|
|
576
|
+
it('should add multiple items correctly', () => {
|
|
577
|
+
const content1 = {
|
|
578
|
+
role: 'user',
|
|
579
|
+
parts: [{ text: 'Message 1' }],
|
|
580
|
+
};
|
|
581
|
+
const content2 = {
|
|
582
|
+
role: 'model',
|
|
583
|
+
parts: [{ text: 'Message 2' }],
|
|
584
|
+
};
|
|
585
|
+
chat.addHistory(content1);
|
|
586
|
+
chat.addHistory(content2);
|
|
587
|
+
const history = chat.getHistory();
|
|
588
|
+
expect(history.length).toBe(2);
|
|
589
|
+
expect(history[0]).toEqual(content1);
|
|
590
|
+
expect(history[1]).toEqual(content2);
|
|
591
|
+
});
|
|
592
|
+
});
|
|
593
|
+
describe('sendMessageStream with retries', () => {
|
|
594
|
+
it('should retry on invalid content, succeed, and report metrics', async () => {
|
|
595
|
+
// Use mockImplementationOnce to provide a fresh, promise-wrapped generator for each attempt.
|
|
596
|
+
vi.mocked(mockModelsModule.generateContentStream)
|
|
597
|
+
.mockImplementationOnce(async () =>
|
|
598
|
+
// First call returns an invalid stream
|
|
599
|
+
(async function* () {
|
|
600
|
+
yield {
|
|
601
|
+
candidates: [{ content: { parts: [{ text: '' }] } }], // Invalid empty text part
|
|
602
|
+
};
|
|
603
|
+
})())
|
|
604
|
+
.mockImplementationOnce(async () =>
|
|
605
|
+
// Second call returns a valid stream
|
|
606
|
+
(async function* () {
|
|
607
|
+
yield {
|
|
608
|
+
candidates: [
|
|
609
|
+
{ content: { parts: [{ text: 'Successful response' }] } },
|
|
610
|
+
],
|
|
611
|
+
};
|
|
612
|
+
})());
|
|
613
|
+
const stream = await chat.sendMessageStream({ message: 'test' }, 'prompt-id-retry-success');
|
|
614
|
+
const chunks = [];
|
|
615
|
+
for await (const chunk of stream) {
|
|
616
|
+
chunks.push(chunk);
|
|
617
|
+
}
|
|
618
|
+
// Assertions
|
|
619
|
+
expect(mockLogInvalidChunk).toHaveBeenCalledTimes(1);
|
|
620
|
+
expect(mockLogContentRetry).toHaveBeenCalledTimes(1);
|
|
621
|
+
expect(mockLogContentRetryFailure).not.toHaveBeenCalled();
|
|
622
|
+
expect(mockModelsModule.generateContentStream).toHaveBeenCalledTimes(2);
|
|
623
|
+
expect(chunks.some((c) => c.candidates?.[0]?.content?.parts?.[0]?.text ===
|
|
624
|
+
'Successful response')).toBe(true);
|
|
625
|
+
// Check that history was recorded correctly once, with no duplicates.
|
|
626
|
+
const history = chat.getHistory();
|
|
627
|
+
expect(history.length).toBe(2);
|
|
628
|
+
expect(history[0]).toEqual({
|
|
629
|
+
role: 'user',
|
|
630
|
+
parts: [{ text: 'test' }],
|
|
631
|
+
});
|
|
632
|
+
expect(history[1]).toEqual({
|
|
633
|
+
role: 'model',
|
|
634
|
+
parts: [{ text: 'Successful response' }],
|
|
635
|
+
});
|
|
636
|
+
});
|
|
637
|
+
it('should fail after all retries on persistent invalid content and report metrics', async () => {
|
|
638
|
+
vi.mocked(mockModelsModule.generateContentStream).mockImplementation(async () => (async function* () {
|
|
639
|
+
yield {
|
|
640
|
+
candidates: [
|
|
641
|
+
{
|
|
642
|
+
content: {
|
|
643
|
+
parts: [{ text: '' }],
|
|
644
|
+
role: 'model',
|
|
645
|
+
},
|
|
646
|
+
},
|
|
647
|
+
],
|
|
648
|
+
};
|
|
649
|
+
})());
|
|
650
|
+
// This helper function consumes the stream and allows us to test for rejection.
|
|
651
|
+
async function consumeStreamAndExpectError() {
|
|
652
|
+
const stream = await chat.sendMessageStream({ message: 'test' }, 'prompt-id-retry-fail');
|
|
653
|
+
for await (const _ of stream) {
|
|
654
|
+
// Must loop to trigger the internal logic that throws.
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
await expect(consumeStreamAndExpectError()).rejects.toThrow(EmptyStreamError);
|
|
658
|
+
// Should be called 3 times (initial + 2 retries)
|
|
659
|
+
expect(mockModelsModule.generateContentStream).toHaveBeenCalledTimes(3);
|
|
660
|
+
expect(mockLogInvalidChunk).toHaveBeenCalledTimes(3);
|
|
661
|
+
expect(mockLogContentRetry).toHaveBeenCalledTimes(2);
|
|
662
|
+
expect(mockLogContentRetryFailure).toHaveBeenCalledTimes(1);
|
|
663
|
+
// History should be clean, as if the failed turn never happened.
|
|
664
|
+
const history = chat.getHistory();
|
|
665
|
+
expect(history.length).toBe(0);
|
|
666
|
+
});
|
|
667
|
+
});
|
|
668
|
+
it('should correctly retry and append to an existing history mid-conversation', async () => {
|
|
669
|
+
// 1. Setup
|
|
670
|
+
const initialHistory = [
|
|
671
|
+
{ role: 'user', parts: [{ text: 'First question' }] },
|
|
672
|
+
{ role: 'model', parts: [{ text: 'First answer' }] },
|
|
673
|
+
];
|
|
674
|
+
chat.setHistory(initialHistory);
|
|
675
|
+
// 2. Mock the API to fail once with an empty stream, then succeed.
|
|
676
|
+
vi.mocked(mockModelsModule.generateContentStream)
|
|
677
|
+
.mockImplementationOnce(async () => (async function* () {
|
|
678
|
+
yield {
|
|
679
|
+
candidates: [{ content: { parts: [{ text: '' }] } }],
|
|
680
|
+
};
|
|
681
|
+
})())
|
|
682
|
+
.mockImplementationOnce(async () =>
|
|
683
|
+
// Second attempt succeeds
|
|
684
|
+
(async function* () {
|
|
685
|
+
yield {
|
|
686
|
+
candidates: [{ content: { parts: [{ text: 'Second answer' }] } }],
|
|
687
|
+
};
|
|
688
|
+
})());
|
|
689
|
+
// 3. Send a new message
|
|
690
|
+
const stream = await chat.sendMessageStream({ message: 'Second question' }, 'prompt-id-retry-existing');
|
|
691
|
+
for await (const _ of stream) {
|
|
692
|
+
// consume stream
|
|
693
|
+
}
|
|
694
|
+
// 4. Assert the final history and metrics
|
|
695
|
+
const history = chat.getHistory();
|
|
696
|
+
expect(history.length).toBe(4);
|
|
697
|
+
// Assert that the correct metrics were reported for one empty-stream retry
|
|
698
|
+
expect(mockLogContentRetry).toHaveBeenCalledTimes(1);
|
|
699
|
+
// Explicitly verify the structure of each part to satisfy TypeScript
|
|
700
|
+
const turn1 = history[0];
|
|
701
|
+
if (!turn1?.parts?.[0] || !('text' in turn1.parts[0])) {
|
|
702
|
+
throw new Error('Test setup error: First turn is not a valid text part.');
|
|
703
|
+
}
|
|
704
|
+
expect(turn1.parts[0].text).toBe('First question');
|
|
705
|
+
const turn2 = history[1];
|
|
706
|
+
if (!turn2?.parts?.[0] || !('text' in turn2.parts[0])) {
|
|
707
|
+
throw new Error('Test setup error: Second turn is not a valid text part.');
|
|
708
|
+
}
|
|
709
|
+
expect(turn2.parts[0].text).toBe('First answer');
|
|
710
|
+
const turn3 = history[2];
|
|
711
|
+
if (!turn3?.parts?.[0] || !('text' in turn3.parts[0])) {
|
|
712
|
+
throw new Error('Test setup error: Third turn is not a valid text part.');
|
|
713
|
+
}
|
|
714
|
+
expect(turn3.parts[0].text).toBe('Second question');
|
|
715
|
+
const turn4 = history[3];
|
|
716
|
+
if (!turn4?.parts?.[0] || !('text' in turn4.parts[0])) {
|
|
717
|
+
throw new Error('Test setup error: Fourth turn is not a valid text part.');
|
|
718
|
+
}
|
|
719
|
+
expect(turn4.parts[0].text).toBe('Second answer');
|
|
720
|
+
});
|
|
721
|
+
describe('concurrency control', () => {
|
|
722
|
+
it('should queue a subsequent sendMessage call until the first one completes', async () => {
|
|
723
|
+
// 1. Create promises to manually control when the API calls resolve
|
|
724
|
+
let firstCallResolver;
|
|
725
|
+
const firstCallPromise = new Promise((resolve) => {
|
|
726
|
+
firstCallResolver = resolve;
|
|
727
|
+
});
|
|
728
|
+
let secondCallResolver;
|
|
729
|
+
const secondCallPromise = new Promise((resolve) => {
|
|
730
|
+
secondCallResolver = resolve;
|
|
731
|
+
});
|
|
732
|
+
// A standard response body for the mock
|
|
733
|
+
const mockResponse = {
|
|
734
|
+
candidates: [
|
|
735
|
+
{
|
|
736
|
+
content: { parts: [{ text: 'response' }], role: 'model' },
|
|
737
|
+
},
|
|
738
|
+
],
|
|
739
|
+
};
|
|
740
|
+
// 2. Mock the API to return our controllable promises in order
|
|
741
|
+
vi.mocked(mockModelsModule.generateContent)
|
|
742
|
+
.mockReturnValueOnce(firstCallPromise)
|
|
743
|
+
.mockReturnValueOnce(secondCallPromise);
|
|
744
|
+
// 3. Start the first message call. Do not await it yet.
|
|
745
|
+
const firstMessagePromise = chat.sendMessage({ message: 'first' }, 'prompt-1');
|
|
746
|
+
// Give the event loop a chance to run the async call up to the `await`
|
|
747
|
+
await new Promise(process.nextTick);
|
|
748
|
+
// 4. While the first call is "in-flight", start the second message call.
|
|
749
|
+
const secondMessagePromise = chat.sendMessage({ message: 'second' }, 'prompt-2');
|
|
750
|
+
// 5. CRUCIAL CHECK: At this point, only the first API call should have been made.
|
|
751
|
+
// The second call should be waiting on `sendPromise`.
|
|
752
|
+
expect(mockModelsModule.generateContent).toHaveBeenCalledTimes(1);
|
|
753
|
+
expect(mockModelsModule.generateContent).toHaveBeenCalledWith(expect.objectContaining({
|
|
754
|
+
contents: expect.arrayContaining([
|
|
755
|
+
expect.objectContaining({ parts: [{ text: 'first' }] }),
|
|
756
|
+
]),
|
|
757
|
+
}), 'prompt-1');
|
|
758
|
+
// 6. Unblock the first API call and wait for the first message to fully complete.
|
|
759
|
+
firstCallResolver(mockResponse);
|
|
760
|
+
await firstMessagePromise;
|
|
761
|
+
// Give the event loop a chance to unblock and run the second call.
|
|
762
|
+
await new Promise(process.nextTick);
|
|
763
|
+
// 7. CRUCIAL CHECK: Now, the second API call should have been made.
|
|
764
|
+
expect(mockModelsModule.generateContent).toHaveBeenCalledTimes(2);
|
|
765
|
+
expect(mockModelsModule.generateContent).toHaveBeenCalledWith(expect.objectContaining({
|
|
766
|
+
contents: expect.arrayContaining([
|
|
767
|
+
expect.objectContaining({ parts: [{ text: 'second' }] }),
|
|
768
|
+
]),
|
|
769
|
+
}), 'prompt-2');
|
|
770
|
+
// 8. Clean up by resolving the second call.
|
|
771
|
+
secondCallResolver(mockResponse);
|
|
772
|
+
await secondMessagePromise;
|
|
773
|
+
});
|
|
774
|
+
});
|
|
775
|
+
it('should retry if the model returns a completely empty stream (no chunks)', async () => {
|
|
776
|
+
// 1. Mock the API to return an empty stream first, then a valid one.
|
|
777
|
+
vi.mocked(mockModelsModule.generateContentStream)
|
|
778
|
+
.mockImplementationOnce(
|
|
779
|
+
// First call resolves to an async generator that yields nothing.
|
|
780
|
+
async () => (async function* () { })())
|
|
781
|
+
.mockImplementationOnce(
|
|
782
|
+
// Second call returns a valid stream.
|
|
783
|
+
async () => (async function* () {
|
|
784
|
+
yield {
|
|
785
|
+
candidates: [
|
|
786
|
+
{
|
|
787
|
+
content: {
|
|
788
|
+
parts: [{ text: 'Successful response after empty' }],
|
|
789
|
+
},
|
|
790
|
+
},
|
|
791
|
+
],
|
|
792
|
+
};
|
|
793
|
+
})());
|
|
794
|
+
// 2. Call the method and consume the stream.
|
|
795
|
+
const stream = await chat.sendMessageStream({ message: 'test empty stream' }, 'prompt-id-empty-stream');
|
|
796
|
+
const chunks = [];
|
|
797
|
+
for await (const chunk of stream) {
|
|
798
|
+
chunks.push(chunk);
|
|
799
|
+
}
|
|
800
|
+
// 3. Assert the results.
|
|
801
|
+
expect(mockModelsModule.generateContentStream).toHaveBeenCalledTimes(2);
|
|
802
|
+
expect(chunks.some((c) => c.candidates?.[0]?.content?.parts?.[0]?.text ===
|
|
803
|
+
'Successful response after empty')).toBe(true);
|
|
804
|
+
const history = chat.getHistory();
|
|
805
|
+
expect(history.length).toBe(2);
|
|
806
|
+
// Explicitly verify the structure of each part to satisfy TypeScript
|
|
807
|
+
const turn1 = history[0];
|
|
808
|
+
if (!turn1?.parts?.[0] || !('text' in turn1.parts[0])) {
|
|
809
|
+
throw new Error('Test setup error: First turn is not a valid text part.');
|
|
810
|
+
}
|
|
811
|
+
expect(turn1.parts[0].text).toBe('test empty stream');
|
|
812
|
+
const turn2 = history[1];
|
|
813
|
+
if (!turn2?.parts?.[0] || !('text' in turn2.parts[0])) {
|
|
814
|
+
throw new Error('Test setup error: Second turn is not a valid text part.');
|
|
815
|
+
}
|
|
816
|
+
expect(turn2.parts[0].text).toBe('Successful response after empty');
|
|
817
|
+
});
|
|
818
|
+
it('should queue a subsequent sendMessageStream call until the first stream is fully consumed', async () => {
|
|
819
|
+
// 1. Create a promise to manually control the stream's lifecycle
|
|
820
|
+
let continueFirstStream;
|
|
821
|
+
const firstStreamContinuePromise = new Promise((resolve) => {
|
|
822
|
+
continueFirstStream = resolve;
|
|
823
|
+
});
|
|
824
|
+
// 2. Mock the API to return controllable async generators
|
|
825
|
+
const firstStreamGenerator = (async function* () {
|
|
826
|
+
yield {
|
|
827
|
+
candidates: [
|
|
828
|
+
{ content: { parts: [{ text: 'first response part 1' }] } },
|
|
829
|
+
],
|
|
830
|
+
};
|
|
831
|
+
await firstStreamContinuePromise; // Pause the stream
|
|
832
|
+
yield {
|
|
833
|
+
candidates: [{ content: { parts: [{ text: ' part 2' }] } }],
|
|
834
|
+
};
|
|
835
|
+
})();
|
|
836
|
+
const secondStreamGenerator = (async function* () {
|
|
837
|
+
yield {
|
|
838
|
+
candidates: [{ content: { parts: [{ text: 'second response' }] } }],
|
|
839
|
+
};
|
|
840
|
+
})();
|
|
841
|
+
vi.mocked(mockModelsModule.generateContentStream)
|
|
842
|
+
.mockResolvedValueOnce(firstStreamGenerator)
|
|
843
|
+
.mockResolvedValueOnce(secondStreamGenerator);
|
|
844
|
+
// 3. Start the first stream and consume only the first chunk to pause it
|
|
845
|
+
const firstStream = await chat.sendMessageStream({ message: 'first' }, 'prompt-1');
|
|
846
|
+
const firstStreamIterator = firstStream[Symbol.asyncIterator]();
|
|
847
|
+
await firstStreamIterator.next();
|
|
848
|
+
// 4. While the first stream is paused, start the second call. It will block.
|
|
849
|
+
const secondStreamPromise = chat.sendMessageStream({ message: 'second' }, 'prompt-2');
|
|
850
|
+
// 5. Assert that only one API call has been made so far.
|
|
851
|
+
expect(mockModelsModule.generateContentStream).toHaveBeenCalledTimes(1);
|
|
852
|
+
// 6. Unblock and fully consume the first stream to completion.
|
|
853
|
+
continueFirstStream();
|
|
854
|
+
await firstStreamIterator.next(); // Consume the rest of the stream
|
|
855
|
+
await firstStreamIterator.next(); // Finish the iterator
|
|
856
|
+
// 7. Now that the first stream is done, await the second promise to get its generator.
|
|
857
|
+
const secondStream = await secondStreamPromise;
|
|
858
|
+
// 8. Start consuming the second stream, which triggers its internal API call.
|
|
859
|
+
const secondStreamIterator = secondStream[Symbol.asyncIterator]();
|
|
860
|
+
await secondStreamIterator.next();
|
|
861
|
+
// 9. The second API call should now have been made.
|
|
862
|
+
expect(mockModelsModule.generateContentStream).toHaveBeenCalledTimes(2);
|
|
863
|
+
// 10. FIX: Fully consume the second stream to ensure recordHistory is called.
|
|
864
|
+
await secondStreamIterator.next(); // This finishes the iterator.
|
|
865
|
+
// 11. Final check on history.
|
|
866
|
+
const history = chat.getHistory();
|
|
867
|
+
expect(history.length).toBe(4);
|
|
868
|
+
const turn4 = history[3];
|
|
869
|
+
if (!turn4?.parts?.[0] || !('text' in turn4.parts[0])) {
|
|
870
|
+
throw new Error('Test setup error: Fourth turn is not a valid text part.');
|
|
871
|
+
}
|
|
872
|
+
expect(turn4.parts[0].text).toBe('second response');
|
|
873
|
+
});
|
|
874
|
+
});
|
|
875
|
+
//# sourceMappingURL=geminiChat.test.js.map
|