@google/gemini-cli-core 0.0.3-preview.4 → 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 +2 -2
- package/README.md +12 -2
- package/dist/index.d.ts +6 -2
- package/dist/index.js +6 -2
- package/dist/index.js.map +1 -1
- package/dist/src/code_assist/codeAssist.d.ts +2 -0
- package/dist/src/code_assist/codeAssist.js +12 -0
- package/dist/src/code_assist/codeAssist.js.map +1 -1
- package/dist/src/code_assist/converter.d.ts +3 -1
- package/dist/src/code_assist/converter.js +2 -1
- package/dist/src/code_assist/converter.js.map +1 -1
- package/dist/src/code_assist/converter.test.js +10 -0
- package/dist/src/code_assist/converter.test.js.map +1 -1
- package/dist/src/code_assist/oauth-credential-storage.d.ts +25 -0
- package/dist/src/code_assist/oauth-credential-storage.js +109 -0
- package/dist/src/code_assist/oauth-credential-storage.js.map +1 -0
- package/dist/src/code_assist/oauth-credential-storage.test.js +136 -0
- package/dist/src/code_assist/oauth-credential-storage.test.js.map +1 -0
- package/dist/src/code_assist/oauth2.js +92 -29
- package/dist/src/code_assist/oauth2.js.map +1 -1
- package/dist/src/code_assist/oauth2.test.js +729 -339
- package/dist/src/code_assist/oauth2.test.js.map +1 -1
- package/dist/src/code_assist/server.d.ts +1 -1
- package/dist/src/code_assist/server.js +24 -1
- package/dist/src/code_assist/server.js.map +1 -1
- package/dist/src/code_assist/server.test.js +25 -0
- package/dist/src/code_assist/server.test.js.map +1 -1
- package/dist/src/code_assist/types.d.ts +17 -2
- package/dist/src/config/config.d.ts +72 -12
- package/dist/src/config/config.js +196 -64
- package/dist/src/config/config.js.map +1 -1
- package/dist/src/config/config.test.js +305 -178
- package/dist/src/config/config.test.js.map +1 -1
- package/dist/src/config/models.d.ts +16 -0
- package/dist/src/config/models.js +29 -0
- package/dist/src/config/models.js.map +1 -1
- package/dist/src/config/models.test.d.ts +6 -0
- package/dist/src/config/models.test.js +55 -0
- package/dist/src/config/models.test.js.map +1 -0
- package/dist/src/config/storage.d.ts +2 -0
- package/dist/src/config/storage.js +6 -1
- package/dist/src/config/storage.js.map +1 -1
- package/dist/src/config/storage.test.js +4 -0
- package/dist/src/config/storage.test.js.map +1 -1
- package/dist/src/confirmation-bus/index.d.ts +7 -0
- package/dist/src/confirmation-bus/index.js +8 -0
- package/dist/src/confirmation-bus/index.js.map +1 -0
- package/dist/src/confirmation-bus/message-bus.d.ts +17 -0
- package/dist/src/confirmation-bus/message-bus.js +81 -0
- package/dist/src/confirmation-bus/message-bus.js.map +1 -0
- package/dist/src/confirmation-bus/message-bus.test.d.ts +6 -0
- package/dist/src/confirmation-bus/message-bus.test.js +164 -0
- package/dist/src/confirmation-bus/message-bus.test.js.map +1 -0
- package/dist/src/confirmation-bus/types.d.ts +38 -0
- package/dist/src/confirmation-bus/types.js +15 -0
- package/dist/src/confirmation-bus/types.js.map +1 -0
- package/dist/src/core/baseLlmClient.d.ts +46 -0
- package/dist/src/core/baseLlmClient.js +112 -0
- package/dist/src/core/baseLlmClient.js.map +1 -0
- package/dist/src/core/baseLlmClient.test.d.ts +6 -0
- package/dist/src/core/baseLlmClient.test.js +253 -0
- package/dist/src/core/baseLlmClient.test.js.map +1 -0
- package/dist/src/core/client.d.ts +16 -21
- package/dist/src/core/client.js +145 -232
- package/dist/src/core/client.js.map +1 -1
- package/dist/src/core/client.test.js +393 -492
- package/dist/src/core/client.test.js.map +1 -1
- package/dist/src/core/contentGenerator.d.ts +2 -3
- package/dist/src/core/contentGenerator.js +0 -4
- package/dist/src/core/contentGenerator.js.map +1 -1
- package/dist/src/core/contentGenerator.test.js +1 -3
- package/dist/src/core/contentGenerator.test.js.map +1 -1
- package/dist/src/core/coreToolScheduler.d.ts +8 -3
- package/dist/src/core/coreToolScheduler.js +106 -5
- package/dist/src/core/coreToolScheduler.js.map +1 -1
- package/dist/src/core/coreToolScheduler.test.js +233 -5
- package/dist/src/core/coreToolScheduler.test.js.map +1 -1
- package/dist/src/core/geminiChat.d.ts +38 -32
- package/dist/src/core/geminiChat.js +209 -219
- package/dist/src/core/geminiChat.js.map +1 -1
- package/dist/src/core/geminiChat.test.js +674 -386
- package/dist/src/core/geminiChat.test.js.map +1 -1
- package/dist/src/core/loggingContentGenerator.js +13 -16
- package/dist/src/core/loggingContentGenerator.js.map +1 -1
- package/dist/src/core/nonInteractiveToolExecutor.test.js +59 -1
- package/dist/src/core/nonInteractiveToolExecutor.test.js.map +1 -1
- package/dist/src/core/prompts.d.ts +5 -0
- package/dist/src/core/prompts.js +63 -42
- package/dist/src/core/prompts.js.map +1 -1
- package/dist/src/core/prompts.test.js +130 -1
- package/dist/src/core/prompts.test.js.map +1 -1
- package/dist/src/core/subagent.js +7 -10
- package/dist/src/core/subagent.js.map +1 -1
- package/dist/src/core/subagent.test.js +32 -22
- package/dist/src/core/subagent.test.js.map +1 -1
- package/dist/src/core/turn.d.ts +21 -5
- package/dist/src/core/turn.js +45 -11
- package/dist/src/core/turn.js.map +1 -1
- package/dist/src/core/turn.test.js +340 -100
- package/dist/src/core/turn.test.js.map +1 -1
- package/dist/src/fallback/handler.d.ts +7 -0
- package/dist/src/fallback/handler.js +51 -0
- package/dist/src/fallback/handler.js.map +1 -0
- package/dist/src/fallback/handler.test.d.ts +6 -0
- package/dist/src/fallback/handler.test.js +130 -0
- package/dist/src/fallback/handler.test.js.map +1 -0
- package/dist/src/fallback/types.d.ts +14 -0
- package/dist/src/fallback/types.js +7 -0
- package/dist/src/fallback/types.js.map +1 -0
- package/dist/src/generated/git-commit.d.ts +2 -2
- package/dist/src/generated/git-commit.js +2 -2
- package/dist/src/generated/git-commit.js.map +1 -1
- package/dist/src/ide/constants.d.ts +3 -0
- package/dist/src/ide/constants.js +3 -0
- package/dist/src/ide/constants.js.map +1 -1
- package/dist/src/ide/detect-ide.d.ts +42 -14
- package/dist/src/ide/detect-ide.js +22 -68
- package/dist/src/ide/detect-ide.js.map +1 -1
- package/dist/src/ide/detect-ide.test.js +11 -51
- package/dist/src/ide/detect-ide.test.js.map +1 -1
- package/dist/src/ide/ide-client.d.ts +60 -18
- package/dist/src/ide/ide-client.js +275 -53
- package/dist/src/ide/ide-client.js.map +1 -1
- package/dist/src/ide/ide-client.test.js +239 -6
- package/dist/src/ide/ide-client.test.js.map +1 -1
- package/dist/src/ide/ide-installer.d.ts +2 -2
- package/dist/src/ide/ide-installer.js +15 -11
- package/dist/src/ide/ide-installer.js.map +1 -1
- package/dist/src/ide/ide-installer.test.js +30 -12
- package/dist/src/ide/ide-installer.test.js.map +1 -1
- package/dist/src/ide/ideContext.d.ts +35 -365
- package/dist/src/ide/ideContext.js +60 -106
- package/dist/src/ide/ideContext.js.map +1 -1
- package/dist/src/ide/ideContext.test.js +152 -24
- package/dist/src/ide/ideContext.test.js.map +1 -1
- package/dist/src/ide/process-utils.d.ts +0 -1
- package/dist/src/ide/process-utils.js +43 -25
- package/dist/src/ide/process-utils.js.map +1 -1
- package/dist/src/ide/process-utils.test.js +90 -4
- package/dist/src/ide/process-utils.test.js.map +1 -1
- package/dist/src/ide/types.d.ts +486 -0
- package/dist/src/ide/types.js +138 -0
- package/dist/src/ide/types.js.map +1 -0
- package/dist/src/index.d.ts +10 -2
- package/dist/src/index.js +11 -2
- package/dist/src/index.js.map +1 -1
- package/dist/src/mcp/oauth-provider.d.ts +15 -12
- package/dist/src/mcp/oauth-provider.js +63 -56
- package/dist/src/mcp/oauth-provider.js.map +1 -1
- package/dist/src/mcp/oauth-provider.test.js +74 -35
- package/dist/src/mcp/oauth-provider.test.js.map +1 -1
- package/dist/src/mcp/oauth-token-storage.d.ts +14 -10
- package/dist/src/mcp/oauth-token-storage.js +52 -20
- package/dist/src/mcp/oauth-token-storage.js.map +1 -1
- package/dist/src/mcp/oauth-token-storage.test.js +255 -162
- package/dist/src/mcp/oauth-token-storage.test.js.map +1 -1
- package/dist/src/mcp/token-storage/base-token-storage.d.ts +1 -1
- package/dist/src/mcp/token-storage/base-token-storage.js +1 -1
- package/dist/src/mcp/token-storage/base-token-storage.js.map +1 -1
- package/dist/src/mcp/token-storage/base-token-storage.test.js +1 -1
- package/dist/src/mcp/token-storage/base-token-storage.test.js.map +1 -1
- package/dist/src/mcp/token-storage/file-token-storage.d.ts +24 -0
- package/dist/src/mcp/token-storage/file-token-storage.js +144 -0
- package/dist/src/mcp/token-storage/file-token-storage.js.map +1 -0
- package/dist/src/mcp/token-storage/file-token-storage.test.d.ts +6 -0
- package/dist/src/mcp/token-storage/file-token-storage.test.js +235 -0
- package/dist/src/mcp/token-storage/file-token-storage.test.js.map +1 -0
- package/dist/src/mcp/token-storage/hybrid-token-storage.d.ts +23 -0
- package/dist/src/mcp/token-storage/hybrid-token-storage.js +78 -0
- package/dist/src/mcp/token-storage/hybrid-token-storage.js.map +1 -0
- package/dist/src/mcp/token-storage/hybrid-token-storage.test.d.ts +6 -0
- package/dist/src/mcp/token-storage/hybrid-token-storage.test.js +193 -0
- package/dist/src/mcp/token-storage/hybrid-token-storage.test.js.map +1 -0
- package/dist/src/mcp/token-storage/index.d.ts +11 -0
- package/dist/src/mcp/token-storage/index.js +12 -0
- package/dist/src/mcp/token-storage/index.js.map +1 -0
- package/dist/src/mcp/token-storage/keychain-token-storage.d.ts +31 -0
- package/dist/src/mcp/token-storage/keychain-token-storage.js +190 -0
- package/dist/src/mcp/token-storage/keychain-token-storage.js.map +1 -0
- package/dist/src/mcp/token-storage/keychain-token-storage.test.d.ts +6 -0
- package/dist/src/mcp/token-storage/keychain-token-storage.test.js +254 -0
- package/dist/src/mcp/token-storage/keychain-token-storage.test.js.map +1 -0
- package/dist/src/mcp/token-storage/types.d.ts +4 -0
- package/dist/src/mcp/token-storage/types.js +5 -1
- package/dist/src/mcp/token-storage/types.js.map +1 -1
- package/dist/src/output/json-formatter.d.ts +11 -0
- package/dist/src/output/json-formatter.js +30 -0
- package/dist/src/output/json-formatter.js.map +1 -0
- package/dist/src/output/json-formatter.test.d.ts +6 -0
- package/dist/src/output/json-formatter.test.js +266 -0
- package/dist/src/output/json-formatter.test.js.map +1 -0
- package/dist/src/output/types.d.ts +20 -0
- package/dist/src/output/types.js +11 -0
- package/dist/src/output/types.js.map +1 -0
- package/dist/src/policy/index.d.ts +7 -0
- package/dist/src/policy/index.js +8 -0
- package/dist/src/policy/index.js.map +1 -0
- package/dist/src/policy/policy-engine.d.ts +30 -0
- package/dist/src/policy/policy-engine.js +92 -0
- package/dist/src/policy/policy-engine.js.map +1 -0
- package/dist/src/policy/policy-engine.test.d.ts +6 -0
- package/dist/src/policy/policy-engine.test.js +515 -0
- package/dist/src/policy/policy-engine.test.js.map +1 -0
- package/dist/src/policy/stable-stringify.d.ts +58 -0
- package/dist/src/policy/stable-stringify.js +122 -0
- package/dist/src/policy/stable-stringify.js.map +1 -0
- package/dist/src/policy/types.d.ts +47 -0
- package/dist/src/policy/types.js +12 -0
- package/dist/src/policy/types.js.map +1 -0
- package/dist/src/routing/modelRouterService.d.ts +23 -0
- package/dist/src/routing/modelRouterService.js +70 -0
- package/dist/src/routing/modelRouterService.js.map +1 -0
- package/dist/src/routing/modelRouterService.test.d.ts +6 -0
- package/dist/src/routing/modelRouterService.test.js +98 -0
- package/dist/src/routing/modelRouterService.test.js.map +1 -0
- package/dist/src/routing/routingStrategy.d.ts +62 -0
- package/dist/src/routing/routingStrategy.js +7 -0
- package/dist/src/routing/routingStrategy.js.map +1 -0
- package/dist/src/routing/strategies/classifierStrategy.d.ts +12 -0
- package/dist/src/routing/strategies/classifierStrategy.js +173 -0
- package/dist/src/routing/strategies/classifierStrategy.js.map +1 -0
- package/dist/src/routing/strategies/classifierStrategy.test.d.ts +6 -0
- package/dist/src/routing/strategies/classifierStrategy.test.js +192 -0
- package/dist/src/routing/strategies/classifierStrategy.test.js.map +1 -0
- package/dist/src/routing/strategies/compositeStrategy.d.ts +26 -0
- package/dist/src/routing/strategies/compositeStrategy.js +67 -0
- package/dist/src/routing/strategies/compositeStrategy.js.map +1 -0
- package/dist/src/routing/strategies/compositeStrategy.test.d.ts +6 -0
- package/dist/src/routing/strategies/compositeStrategy.test.js +123 -0
- package/dist/src/routing/strategies/compositeStrategy.test.js.map +1 -0
- package/dist/src/routing/strategies/defaultStrategy.d.ts +12 -0
- package/dist/src/routing/strategies/defaultStrategy.js +20 -0
- package/dist/src/routing/strategies/defaultStrategy.js.map +1 -0
- package/dist/src/routing/strategies/defaultStrategy.test.d.ts +6 -0
- package/dist/src/routing/strategies/defaultStrategy.test.js +26 -0
- package/dist/src/routing/strategies/defaultStrategy.test.js.map +1 -0
- package/dist/src/routing/strategies/fallbackStrategy.d.ts +12 -0
- package/dist/src/routing/strategies/fallbackStrategy.js +25 -0
- package/dist/src/routing/strategies/fallbackStrategy.js.map +1 -0
- package/dist/src/routing/strategies/fallbackStrategy.test.d.ts +6 -0
- package/dist/src/routing/strategies/fallbackStrategy.test.js +55 -0
- package/dist/src/routing/strategies/fallbackStrategy.test.js.map +1 -0
- package/dist/src/routing/strategies/overrideStrategy.d.ts +15 -0
- package/dist/src/routing/strategies/overrideStrategy.js +28 -0
- package/dist/src/routing/strategies/overrideStrategy.js.map +1 -0
- package/dist/src/routing/strategies/overrideStrategy.test.d.ts +6 -0
- package/dist/src/routing/strategies/overrideStrategy.test.js +42 -0
- package/dist/src/routing/strategies/overrideStrategy.test.js.map +1 -0
- package/dist/src/services/chatRecordingService.d.ts +7 -13
- package/dist/src/services/chatRecordingService.js +28 -19
- package/dist/src/services/chatRecordingService.js.map +1 -1
- package/dist/src/services/chatRecordingService.test.js +62 -20
- package/dist/src/services/chatRecordingService.test.js.map +1 -1
- package/dist/src/services/fileDiscoveryService.d.ts +10 -0
- package/dist/src/services/fileDiscoveryService.js +31 -17
- package/dist/src/services/fileDiscoveryService.js.map +1 -1
- package/dist/src/services/gitService.js +9 -12
- package/dist/src/services/gitService.js.map +1 -1
- package/dist/src/services/gitService.test.js +10 -20
- package/dist/src/services/gitService.test.js.map +1 -1
- package/dist/src/services/loopDetectionService.d.ts +5 -0
- package/dist/src/services/loopDetectionService.js +36 -20
- package/dist/src/services/loopDetectionService.js.map +1 -1
- package/dist/src/services/loopDetectionService.test.js +41 -12
- package/dist/src/services/loopDetectionService.test.js.map +1 -1
- package/dist/src/services/shellExecutionService.d.ts +34 -2
- package/dist/src/services/shellExecutionService.js +192 -43
- package/dist/src/services/shellExecutionService.js.map +1 -1
- package/dist/src/services/shellExecutionService.test.js +184 -55
- package/dist/src/services/shellExecutionService.test.js.map +1 -1
- package/dist/src/telemetry/activity-detector.d.ts +41 -0
- package/dist/src/telemetry/activity-detector.js +61 -0
- package/dist/src/telemetry/activity-detector.js.map +1 -0
- package/dist/src/telemetry/activity-detector.test.d.ts +6 -0
- package/dist/src/telemetry/activity-detector.test.js +136 -0
- package/dist/src/telemetry/activity-detector.test.js.map +1 -0
- package/dist/src/telemetry/activity-types.d.ts +19 -0
- package/dist/src/telemetry/activity-types.js +21 -0
- package/dist/src/telemetry/activity-types.js.map +1 -0
- package/dist/src/telemetry/clearcut-logger/clearcut-logger.d.ts +16 -2
- package/dist/src/telemetry/clearcut-logger/clearcut-logger.js +143 -24
- package/dist/src/telemetry/clearcut-logger/clearcut-logger.js.map +1 -1
- package/dist/src/telemetry/clearcut-logger/clearcut-logger.test.js +101 -1
- package/dist/src/telemetry/clearcut-logger/clearcut-logger.test.js.map +1 -1
- package/dist/src/telemetry/clearcut-logger/event-metadata-key.d.ts +19 -2
- package/dist/src/telemetry/clearcut-logger/event-metadata-key.js +48 -2
- package/dist/src/telemetry/clearcut-logger/event-metadata-key.js.map +1 -1
- package/dist/src/telemetry/constants.d.ts +8 -0
- package/dist/src/telemetry/constants.js +8 -0
- package/dist/src/telemetry/constants.js.map +1 -1
- package/dist/src/telemetry/gcp-exporters.d.ts +34 -0
- package/dist/src/telemetry/gcp-exporters.js +117 -0
- package/dist/src/telemetry/gcp-exporters.js.map +1 -0
- package/dist/src/telemetry/gcp-exporters.test.d.ts +6 -0
- package/dist/src/telemetry/gcp-exporters.test.js +318 -0
- package/dist/src/telemetry/gcp-exporters.test.js.map +1 -0
- package/dist/src/telemetry/high-water-mark-tracker.d.ts +43 -0
- package/dist/src/telemetry/high-water-mark-tracker.js +88 -0
- package/dist/src/telemetry/high-water-mark-tracker.js.map +1 -0
- package/dist/src/telemetry/high-water-mark-tracker.test.d.ts +6 -0
- package/dist/src/telemetry/high-water-mark-tracker.test.js +152 -0
- package/dist/src/telemetry/high-water-mark-tracker.test.js.map +1 -0
- package/dist/src/telemetry/index.d.ts +7 -2
- package/dist/src/telemetry/index.js +7 -2
- package/dist/src/telemetry/index.js.map +1 -1
- package/dist/src/telemetry/loggers.d.ts +8 -1
- package/dist/src/telemetry/loggers.js +140 -8
- package/dist/src/telemetry/loggers.js.map +1 -1
- package/dist/src/telemetry/loggers.test.js +268 -39
- package/dist/src/telemetry/loggers.test.js.map +1 -1
- package/dist/src/telemetry/metrics.d.ts +4 -3
- package/dist/src/telemetry/metrics.js +33 -10
- package/dist/src/telemetry/metrics.js.map +1 -1
- package/dist/src/telemetry/metrics.test.js +47 -25
- package/dist/src/telemetry/metrics.test.js.map +1 -1
- package/dist/src/telemetry/rate-limiter.d.ts +48 -0
- package/dist/src/telemetry/rate-limiter.js +100 -0
- package/dist/src/telemetry/rate-limiter.js.map +1 -0
- package/dist/src/telemetry/rate-limiter.test.d.ts +6 -0
- package/dist/src/telemetry/rate-limiter.test.js +207 -0
- package/dist/src/telemetry/rate-limiter.test.js.map +1 -0
- package/dist/src/telemetry/sdk.js +16 -1
- package/dist/src/telemetry/sdk.js.map +1 -1
- package/dist/src/telemetry/sdk.test.js +95 -0
- package/dist/src/telemetry/sdk.test.js.map +1 -1
- package/dist/src/telemetry/types.d.ts +70 -6
- package/dist/src/telemetry/types.js +112 -8
- package/dist/src/telemetry/types.js.map +1 -1
- package/dist/src/telemetry/uiTelemetry.d.ts +1 -1
- package/dist/src/telemetry/uiTelemetry.js +6 -7
- package/dist/src/telemetry/uiTelemetry.js.map +1 -1
- package/dist/src/telemetry/uiTelemetry.test.js +15 -15
- package/dist/src/telemetry/uiTelemetry.test.js.map +1 -1
- package/dist/src/test-utils/index.d.ts +6 -0
- package/dist/src/test-utils/index.js +7 -0
- package/dist/src/test-utils/index.js.map +1 -0
- package/dist/src/test-utils/mock-tool.d.ts +41 -0
- package/dist/src/test-utils/mock-tool.js +51 -0
- package/dist/src/test-utils/mock-tool.js.map +1 -0
- package/dist/src/tools/diffOptions.js +21 -13
- package/dist/src/tools/diffOptions.js.map +1 -1
- package/dist/src/tools/diffOptions.test.js +58 -22
- package/dist/src/tools/diffOptions.test.js.map +1 -1
- package/dist/src/tools/edit.d.ts +2 -2
- package/dist/src/tools/edit.js +35 -44
- package/dist/src/tools/edit.js.map +1 -1
- package/dist/src/tools/edit.test.js +124 -13
- package/dist/src/tools/edit.test.js.map +1 -1
- package/dist/src/tools/glob.d.ts +5 -1
- package/dist/src/tools/glob.js +24 -17
- package/dist/src/tools/glob.js.map +1 -1
- package/dist/src/tools/glob.test.js +51 -0
- package/dist/src/tools/glob.test.js.map +1 -1
- package/dist/src/tools/ls.js +19 -32
- package/dist/src/tools/ls.js.map +1 -1
- package/dist/src/tools/ls.test.js +140 -280
- package/dist/src/tools/ls.test.js.map +1 -1
- package/dist/src/tools/mcp-client-manager.d.ts +5 -3
- package/dist/src/tools/mcp-client-manager.js +13 -4
- package/dist/src/tools/mcp-client-manager.js.map +1 -1
- package/dist/src/tools/mcp-client-manager.test.js +20 -1
- package/dist/src/tools/mcp-client-manager.test.js.map +1 -1
- package/dist/src/tools/mcp-client.d.ts +5 -5
- package/dist/src/tools/mcp-client.js +40 -35
- package/dist/src/tools/mcp-client.js.map +1 -1
- package/dist/src/tools/mcp-client.test.js +3 -3
- package/dist/src/tools/mcp-client.test.js.map +1 -1
- package/dist/src/tools/mcp-tool.d.ts +3 -2
- package/dist/src/tools/mcp-tool.js +9 -9
- package/dist/src/tools/mcp-tool.js.map +1 -1
- package/dist/src/tools/mcp-tool.test.js +28 -7
- package/dist/src/tools/mcp-tool.test.js.map +1 -1
- package/dist/src/tools/memoryTool.js +5 -33
- package/dist/src/tools/memoryTool.js.map +1 -1
- package/dist/src/tools/read-file.js +8 -3
- package/dist/src/tools/read-file.js.map +1 -1
- package/dist/src/tools/read-file.test.js +29 -0
- package/dist/src/tools/read-file.test.js.map +1 -1
- package/dist/src/tools/read-many-files.d.ts +1 -1
- package/dist/src/tools/read-many-files.js +18 -50
- package/dist/src/tools/read-many-files.js.map +1 -1
- package/dist/src/tools/read-many-files.test.js +4 -4
- package/dist/src/tools/read-many-files.test.js.map +1 -1
- package/dist/src/tools/ripGrep.d.ts +8 -0
- package/dist/src/tools/ripGrep.js +26 -1
- package/dist/src/tools/ripGrep.js.map +1 -1
- package/dist/src/tools/ripGrep.test.js +107 -5
- package/dist/src/tools/ripGrep.test.js.map +1 -1
- package/dist/src/tools/shell.d.ts +12 -2
- package/dist/src/tools/shell.js +20 -24
- package/dist/src/tools/shell.js.map +1 -1
- package/dist/src/tools/shell.test.js +35 -70
- package/dist/src/tools/shell.test.js.map +1 -1
- package/dist/src/tools/smart-edit.d.ts +72 -0
- package/dist/src/tools/smart-edit.js +594 -0
- package/dist/src/tools/smart-edit.js.map +1 -0
- package/dist/src/tools/smart-edit.test.d.ts +6 -0
- package/dist/src/tools/smart-edit.test.js +419 -0
- package/dist/src/tools/smart-edit.test.js.map +1 -0
- package/dist/src/tools/tool-registry.d.ts +2 -1
- package/dist/src/tools/tool-registry.js +6 -5
- package/dist/src/tools/tool-registry.js.map +1 -1
- package/dist/src/tools/tools.d.ts +14 -7
- package/dist/src/tools/tools.js +9 -2
- package/dist/src/tools/tools.js.map +1 -1
- package/dist/src/tools/web-fetch.js +4 -3
- package/dist/src/tools/web-fetch.js.map +1 -1
- package/dist/src/tools/web-search.d.ts +1 -1
- package/dist/src/tools/web-search.js +3 -1
- package/dist/src/tools/web-search.js.map +1 -1
- package/dist/src/tools/write-file.js +14 -19
- package/dist/src/tools/write-file.js.map +1 -1
- package/dist/src/tools/write-file.test.js +99 -19
- package/dist/src/tools/write-file.test.js.map +1 -1
- package/dist/src/utils/bfsFileSearch.js +11 -5
- package/dist/src/utils/bfsFileSearch.js.map +1 -1
- package/dist/src/utils/editCorrector.d.ts +7 -6
- package/dist/src/utils/editCorrector.js +61 -18
- package/dist/src/utils/editCorrector.js.map +1 -1
- package/dist/src/utils/editCorrector.test.js +30 -79
- package/dist/src/utils/editCorrector.test.js.map +1 -1
- package/dist/src/utils/editor.js +31 -44
- package/dist/src/utils/editor.js.map +1 -1
- package/dist/src/utils/editor.test.js +61 -75
- package/dist/src/utils/editor.test.js.map +1 -1
- package/dist/src/utils/errorParsing.js +2 -2
- package/dist/src/utils/errorParsing.js.map +1 -1
- package/dist/src/utils/errorParsing.test.js +7 -7
- package/dist/src/utils/errorParsing.test.js.map +1 -1
- package/dist/src/utils/errors.d.ts +6 -0
- package/dist/src/utils/errors.js +10 -0
- package/dist/src/utils/errors.js.map +1 -1
- package/dist/src/utils/fileUtils.d.ts +20 -3
- package/dist/src/utils/fileUtils.js +154 -32
- package/dist/src/utils/fileUtils.js.map +1 -1
- package/dist/src/utils/fileUtils.test.js +347 -29
- package/dist/src/utils/fileUtils.test.js.map +1 -1
- package/dist/src/utils/flashFallback.test.d.ts +6 -0
- package/dist/src/utils/{flashFallback.integration.test.js → flashFallback.test.js} +31 -27
- package/dist/src/utils/flashFallback.test.js.map +1 -0
- package/dist/src/utils/geminiIgnoreParser.d.ts +18 -0
- package/dist/src/utils/geminiIgnoreParser.js +61 -0
- package/dist/src/utils/geminiIgnoreParser.js.map +1 -0
- package/dist/src/utils/geminiIgnoreParser.test.d.ts +6 -0
- package/dist/src/utils/geminiIgnoreParser.test.js +50 -0
- package/dist/src/utils/geminiIgnoreParser.test.js.map +1 -0
- package/dist/src/utils/gitIgnoreParser.d.ts +3 -7
- package/dist/src/utils/gitIgnoreParser.js +125 -34
- package/dist/src/utils/gitIgnoreParser.js.map +1 -1
- package/dist/src/utils/gitIgnoreParser.test.js +66 -35
- package/dist/src/utils/gitIgnoreParser.test.js.map +1 -1
- package/dist/src/utils/llm-edit-fixer.d.ts +26 -0
- package/dist/src/utils/llm-edit-fixer.js +121 -0
- package/dist/src/utils/llm-edit-fixer.js.map +1 -0
- package/dist/src/utils/llm-edit-fixer.test.d.ts +6 -0
- package/dist/src/utils/llm-edit-fixer.test.js +105 -0
- package/dist/src/utils/llm-edit-fixer.test.js.map +1 -0
- package/dist/src/utils/memoryDiscovery.d.ts +5 -4
- package/dist/src/utils/memoryDiscovery.js +10 -9
- package/dist/src/utils/memoryDiscovery.js.map +1 -1
- package/dist/src/utils/memoryDiscovery.test.js +50 -25
- package/dist/src/utils/memoryDiscovery.test.js.map +1 -1
- package/dist/src/utils/nextSpeakerChecker.d.ts +2 -2
- package/dist/src/utils/nextSpeakerChecker.js +8 -2
- package/dist/src/utils/nextSpeakerChecker.js.map +1 -1
- package/dist/src/utils/nextSpeakerChecker.test.js +75 -64
- package/dist/src/utils/nextSpeakerChecker.test.js.map +1 -1
- package/dist/src/utils/promptIdContext.d.ts +7 -0
- package/dist/src/utils/promptIdContext.js +8 -0
- package/dist/src/utils/promptIdContext.js.map +1 -0
- package/dist/src/utils/shell-utils.d.ts +5 -0
- package/dist/src/utils/shell-utils.js +23 -0
- package/dist/src/utils/shell-utils.js.map +1 -1
- package/dist/src/utils/terminalSerializer.d.ts +28 -0
- package/dist/src/utils/terminalSerializer.js +432 -0
- package/dist/src/utils/terminalSerializer.js.map +1 -0
- package/dist/src/utils/terminalSerializer.test.d.ts +6 -0
- package/dist/src/utils/terminalSerializer.test.js +176 -0
- package/dist/src/utils/terminalSerializer.test.js.map +1 -0
- package/dist/src/utils/textUtils.d.ts +5 -0
- package/dist/src/utils/textUtils.js +14 -0
- package/dist/src/utils/textUtils.js.map +1 -1
- package/dist/src/utils/textUtils.test.d.ts +6 -0
- package/dist/src/utils/textUtils.test.js +59 -0
- package/dist/src/utils/textUtils.test.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +9 -3
- package/dist/google-gemini-cli-core-0.3.0-preview.3.tgz +0 -0
- package/dist/src/utils/flashFallback.integration.test.js.map +0 -1
- /package/dist/src/{utils/flashFallback.integration.test.d.ts → code_assist/oauth-credential-storage.test.d.ts} +0 -0
|
@@ -4,16 +4,49 @@
|
|
|
4
4
|
* SPDX-License-Identifier: Apache-2.0
|
|
5
5
|
*/
|
|
6
6
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
7
|
-
import { GeminiChat, EmptyStreamError } from './geminiChat.js';
|
|
7
|
+
import { GeminiChat, EmptyStreamError, StreamEventType, } from './geminiChat.js';
|
|
8
8
|
import { setSimulate429 } from '../utils/testUtils.js';
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
9
|
+
import { DEFAULT_GEMINI_FLASH_MODEL } from '../config/models.js';
|
|
10
|
+
import { AuthType } from './contentGenerator.js';
|
|
11
|
+
import {} from '../utils/retry.js';
|
|
12
|
+
import { Kind } from '../tools/tools.js';
|
|
13
|
+
import { uiTelemetryService } from '../telemetry/uiTelemetry.js';
|
|
14
|
+
// Mock fs module to prevent actual file system operations during tests
|
|
15
|
+
const mockFileSystem = new Map();
|
|
16
|
+
vi.mock('node:fs', () => {
|
|
17
|
+
const fsModule = {
|
|
18
|
+
mkdirSync: vi.fn(),
|
|
19
|
+
writeFileSync: vi.fn((path, data) => {
|
|
20
|
+
mockFileSystem.set(path, data);
|
|
21
|
+
}),
|
|
22
|
+
readFileSync: vi.fn((path) => {
|
|
23
|
+
if (mockFileSystem.has(path)) {
|
|
24
|
+
return mockFileSystem.get(path);
|
|
25
|
+
}
|
|
26
|
+
throw Object.assign(new Error('ENOENT: no such file or directory'), {
|
|
27
|
+
code: 'ENOENT',
|
|
28
|
+
});
|
|
29
|
+
}),
|
|
30
|
+
existsSync: vi.fn((path) => mockFileSystem.has(path)),
|
|
31
|
+
};
|
|
32
|
+
return {
|
|
33
|
+
default: fsModule,
|
|
34
|
+
...fsModule,
|
|
35
|
+
};
|
|
36
|
+
});
|
|
37
|
+
const { mockHandleFallback } = vi.hoisted(() => ({
|
|
38
|
+
mockHandleFallback: vi.fn(),
|
|
39
|
+
}));
|
|
40
|
+
// Add mock for the retry utility
|
|
41
|
+
const { mockRetryWithBackoff } = vi.hoisted(() => ({
|
|
42
|
+
mockRetryWithBackoff: vi.fn(),
|
|
43
|
+
}));
|
|
44
|
+
vi.mock('../utils/retry.js', () => ({
|
|
45
|
+
retryWithBackoff: mockRetryWithBackoff,
|
|
46
|
+
}));
|
|
47
|
+
vi.mock('../fallback/handler.js', () => ({
|
|
48
|
+
handleFallback: mockHandleFallback,
|
|
49
|
+
}));
|
|
17
50
|
const { mockLogInvalidChunk, mockLogContentRetry, mockLogContentRetryFailure } = vi.hoisted(() => ({
|
|
18
51
|
mockLogInvalidChunk: vi.fn(),
|
|
19
52
|
mockLogContentRetry: vi.fn(),
|
|
@@ -24,214 +57,178 @@ vi.mock('../telemetry/loggers.js', () => ({
|
|
|
24
57
|
logContentRetry: mockLogContentRetry,
|
|
25
58
|
logContentRetryFailure: mockLogContentRetryFailure,
|
|
26
59
|
}));
|
|
60
|
+
vi.mock('../telemetry/uiTelemetry.js', () => ({
|
|
61
|
+
uiTelemetryService: {
|
|
62
|
+
setLastPromptTokenCount: vi.fn(),
|
|
63
|
+
},
|
|
64
|
+
}));
|
|
27
65
|
describe('GeminiChat', () => {
|
|
66
|
+
let mockContentGenerator;
|
|
28
67
|
let chat;
|
|
29
68
|
let mockConfig;
|
|
30
69
|
const config = {};
|
|
31
70
|
beforeEach(() => {
|
|
32
71
|
vi.clearAllMocks();
|
|
72
|
+
vi.mocked(uiTelemetryService.setLastPromptTokenCount).mockClear();
|
|
73
|
+
mockContentGenerator = {
|
|
74
|
+
generateContent: vi.fn(),
|
|
75
|
+
generateContentStream: vi.fn(),
|
|
76
|
+
countTokens: vi.fn(),
|
|
77
|
+
embedContent: vi.fn(),
|
|
78
|
+
batchEmbedContents: vi.fn(),
|
|
79
|
+
};
|
|
80
|
+
mockHandleFallback.mockClear();
|
|
81
|
+
// Default mock implementation for tests that don't care about retry logic
|
|
82
|
+
mockRetryWithBackoff.mockImplementation(async (apiCall) => apiCall());
|
|
33
83
|
mockConfig = {
|
|
34
84
|
getSessionId: () => 'test-session-id',
|
|
35
85
|
getTelemetryLogPromptsEnabled: () => true,
|
|
36
86
|
getUsageStatisticsEnabled: () => true,
|
|
37
87
|
getDebugMode: () => false,
|
|
38
|
-
getContentGeneratorConfig: ()
|
|
39
|
-
authType: 'oauth-personal',
|
|
88
|
+
getContentGeneratorConfig: vi.fn().mockReturnValue({
|
|
89
|
+
authType: 'oauth-personal', // Ensure this is set for fallback tests
|
|
40
90
|
model: 'test-model',
|
|
41
91
|
}),
|
|
42
92
|
getModel: vi.fn().mockReturnValue('gemini-pro'),
|
|
43
93
|
setModel: vi.fn(),
|
|
94
|
+
isInFallbackMode: vi.fn().mockReturnValue(false),
|
|
44
95
|
getQuotaErrorOccurred: vi.fn().mockReturnValue(false),
|
|
45
96
|
setQuotaErrorOccurred: vi.fn(),
|
|
46
97
|
flashFallbackHandler: undefined,
|
|
98
|
+
getProjectRoot: vi.fn().mockReturnValue('/test/project/root'),
|
|
99
|
+
storage: {
|
|
100
|
+
getProjectTempDir: vi.fn().mockReturnValue('/test/temp'),
|
|
101
|
+
},
|
|
102
|
+
getToolRegistry: vi.fn().mockReturnValue({
|
|
103
|
+
getTool: vi.fn(),
|
|
104
|
+
}),
|
|
105
|
+
getContentGenerator: vi.fn().mockReturnValue(mockContentGenerator),
|
|
47
106
|
};
|
|
48
107
|
// Disable 429 simulation for tests
|
|
49
108
|
setSimulate429(false);
|
|
50
109
|
// Reset history for each test by creating a new instance
|
|
51
|
-
chat = new GeminiChat(mockConfig,
|
|
110
|
+
chat = new GeminiChat(mockConfig, config, []);
|
|
52
111
|
});
|
|
53
112
|
afterEach(() => {
|
|
54
113
|
vi.restoreAllMocks();
|
|
55
114
|
vi.resetAllMocks();
|
|
56
115
|
});
|
|
57
|
-
describe('
|
|
58
|
-
it('should
|
|
59
|
-
// 1.
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
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
|
-
],
|
|
116
|
+
describe('sendMessageStream', () => {
|
|
117
|
+
it('should succeed if a tool call is followed by an empty part', async () => {
|
|
118
|
+
// 1. Mock a stream that contains a tool call, then an invalid (empty) part.
|
|
119
|
+
const streamWithToolCall = (async function* () {
|
|
120
|
+
yield {
|
|
121
|
+
candidates: [
|
|
122
|
+
{
|
|
123
|
+
content: {
|
|
124
|
+
role: 'model',
|
|
125
|
+
parts: [{ functionCall: { name: 'test_tool', args: {} } }],
|
|
126
|
+
},
|
|
94
127
|
},
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
128
|
+
],
|
|
129
|
+
};
|
|
130
|
+
// This second chunk is invalid according to isValidResponse
|
|
131
|
+
yield {
|
|
132
|
+
candidates: [
|
|
133
|
+
{
|
|
134
|
+
content: {
|
|
135
|
+
role: 'model',
|
|
136
|
+
parts: [{ text: '' }],
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
],
|
|
140
|
+
};
|
|
141
|
+
})();
|
|
142
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(streamWithToolCall);
|
|
143
|
+
// 2. Action & Assert: The stream processing should complete without throwing an error
|
|
144
|
+
// because the presence of a tool call makes the empty final chunk acceptable.
|
|
145
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-tool-call-empty-end');
|
|
146
|
+
await expect((async () => {
|
|
147
|
+
for await (const _ of stream) {
|
|
148
|
+
/* consume stream */
|
|
149
|
+
}
|
|
150
|
+
})()).resolves.not.toThrow();
|
|
151
|
+
// 3. Verify history was recorded correctly
|
|
102
152
|
const history = chat.getHistory();
|
|
103
|
-
|
|
104
|
-
expect(history.length).toBe(2);
|
|
153
|
+
expect(history.length).toBe(2); // user turn + model turn
|
|
105
154
|
const modelTurn = history[1];
|
|
106
|
-
expect(modelTurn
|
|
107
|
-
|
|
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.');
|
|
155
|
+
expect(modelTurn?.parts?.length).toBe(1); // The empty part is discarded
|
|
156
|
+
expect(modelTurn?.parts[0].functionCall).toBeDefined();
|
|
113
157
|
});
|
|
114
|
-
it('should
|
|
115
|
-
// 1.
|
|
116
|
-
const
|
|
117
|
-
{
|
|
118
|
-
|
|
119
|
-
parts: [{ text: 'Find a good Italian restaurant for me.' }],
|
|
120
|
-
},
|
|
121
|
-
{
|
|
122
|
-
role: 'model',
|
|
123
|
-
parts: [
|
|
158
|
+
it('should fail if the stream ends with an empty part and has no finishReason', async () => {
|
|
159
|
+
// 1. Mock a stream that ends with an invalid part and has no finish reason.
|
|
160
|
+
const streamWithNoFinish = (async function* () {
|
|
161
|
+
yield {
|
|
162
|
+
candidates: [
|
|
124
163
|
{
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
164
|
+
content: {
|
|
165
|
+
role: 'model',
|
|
166
|
+
parts: [{ text: 'Initial content...' }],
|
|
128
167
|
},
|
|
129
168
|
},
|
|
130
169
|
],
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
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',
|
|
170
|
+
};
|
|
171
|
+
// This second chunk is invalid and has no finishReason, so it should fail.
|
|
172
|
+
yield {
|
|
173
|
+
candidates: [
|
|
174
|
+
{
|
|
175
|
+
content: {
|
|
176
|
+
role: 'model',
|
|
177
|
+
parts: [{ text: '' }],
|
|
178
|
+
},
|
|
169
179
|
},
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
contents: [{ role: 'user', parts: [{ text: 'hello' }] }],
|
|
182
|
-
config: {},
|
|
183
|
-
}, 'prompt-id-1');
|
|
180
|
+
],
|
|
181
|
+
};
|
|
182
|
+
})();
|
|
183
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(streamWithNoFinish);
|
|
184
|
+
// 2. Action & Assert: The stream should fail because there's no finish reason.
|
|
185
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-no-finish-empty-end');
|
|
186
|
+
await expect((async () => {
|
|
187
|
+
for await (const _ of stream) {
|
|
188
|
+
/* consume stream */
|
|
189
|
+
}
|
|
190
|
+
})()).rejects.toThrow(EmptyStreamError);
|
|
184
191
|
});
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
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.
|
|
192
|
+
it('should succeed if the stream ends with an invalid part but has a finishReason and contained a valid part', async () => {
|
|
193
|
+
// 1. Mock a stream that sends a valid chunk, then an invalid one, but has a finish reason.
|
|
194
|
+
const streamWithInvalidEnd = (async function* () {
|
|
191
195
|
yield {
|
|
192
196
|
candidates: [
|
|
193
197
|
{
|
|
194
198
|
content: {
|
|
195
199
|
role: 'model',
|
|
196
|
-
parts: [
|
|
197
|
-
{
|
|
198
|
-
text: 'Some text',
|
|
199
|
-
functionCall: { name: 'do_stuff', args: {} },
|
|
200
|
-
},
|
|
201
|
-
],
|
|
200
|
+
parts: [{ text: 'Initial valid content...' }],
|
|
202
201
|
},
|
|
203
202
|
},
|
|
204
203
|
],
|
|
205
204
|
};
|
|
206
|
-
// This
|
|
205
|
+
// This second chunk is invalid, but the response has a finishReason.
|
|
207
206
|
yield {
|
|
208
207
|
candidates: [
|
|
209
208
|
{
|
|
210
209
|
content: {
|
|
211
210
|
role: 'model',
|
|
212
|
-
parts: [{ text: '
|
|
211
|
+
parts: [{ text: '' }], // Invalid part
|
|
213
212
|
},
|
|
213
|
+
finishReason: 'STOP',
|
|
214
214
|
},
|
|
215
215
|
],
|
|
216
216
|
};
|
|
217
217
|
})();
|
|
218
|
-
vi.mocked(
|
|
219
|
-
// 2. Action:
|
|
220
|
-
const stream = await chat.sendMessageStream({ message: 'test message' }, 'prompt-id-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
218
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(streamWithInvalidEnd);
|
|
219
|
+
// 2. Action & Assert: The stream should complete without throwing an error.
|
|
220
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-valid-then-invalid-end');
|
|
221
|
+
await expect((async () => {
|
|
222
|
+
for await (const _ of stream) {
|
|
223
|
+
/* consume stream */
|
|
224
|
+
}
|
|
225
|
+
})()).resolves.not.toThrow();
|
|
226
|
+
// 3. Verify history was recorded correctly with only the valid part.
|
|
225
227
|
const history = chat.getHistory();
|
|
226
|
-
expect(history.length).toBe(2);
|
|
228
|
+
expect(history.length).toBe(2); // user turn + model turn
|
|
227
229
|
const modelTurn = history[1];
|
|
228
|
-
|
|
229
|
-
|
|
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.');
|
|
230
|
+
expect(modelTurn?.parts?.length).toBe(1);
|
|
231
|
+
expect(modelTurn?.parts[0].text).toBe('Initial valid content...');
|
|
235
232
|
});
|
|
236
233
|
it('should consolidate subsequent text chunks after receiving an empty text chunk', async () => {
|
|
237
234
|
// 1. Mock the API to return a stream where one chunk is just an empty text part.
|
|
@@ -246,13 +243,16 @@ describe('GeminiChat', () => {
|
|
|
246
243
|
// as the important part is consolidating what comes after.
|
|
247
244
|
yield {
|
|
248
245
|
candidates: [
|
|
249
|
-
{
|
|
246
|
+
{
|
|
247
|
+
content: { role: 'model', parts: [{ text: ' World!' }] },
|
|
248
|
+
finishReason: 'STOP',
|
|
249
|
+
},
|
|
250
250
|
],
|
|
251
251
|
};
|
|
252
252
|
})();
|
|
253
|
-
vi.mocked(
|
|
253
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(multiChunkStream);
|
|
254
254
|
// 2. Action: Send a message and consume the stream.
|
|
255
|
-
const stream = await chat.sendMessageStream({ message: 'test message' }, 'prompt-id-empty-chunk-consolidation');
|
|
255
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-empty-chunk-consolidation');
|
|
256
256
|
for await (const _ of stream) {
|
|
257
257
|
// Consume the stream
|
|
258
258
|
}
|
|
@@ -298,9 +298,9 @@ describe('GeminiChat', () => {
|
|
|
298
298
|
],
|
|
299
299
|
};
|
|
300
300
|
})();
|
|
301
|
-
vi.mocked(
|
|
301
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(multiChunkStream);
|
|
302
302
|
// 2. Action: Send a message and consume the stream.
|
|
303
|
-
const stream = await chat.sendMessageStream({ message: 'test message' }, 'prompt-id-multi-chunk');
|
|
303
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-multi-chunk');
|
|
304
304
|
for await (const _ of stream) {
|
|
305
305
|
// Consume the stream to trigger history recording.
|
|
306
306
|
}
|
|
@@ -329,13 +329,14 @@ describe('GeminiChat', () => {
|
|
|
329
329
|
{ text: 'This is the visible text that should not be lost.' },
|
|
330
330
|
],
|
|
331
331
|
},
|
|
332
|
+
finishReason: 'STOP',
|
|
332
333
|
},
|
|
333
334
|
],
|
|
334
335
|
};
|
|
335
336
|
})();
|
|
336
|
-
vi.mocked(
|
|
337
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(mixedContentStream);
|
|
337
338
|
// 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
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-mixed-chunk');
|
|
339
340
|
for await (const _ of stream) {
|
|
340
341
|
// This loop consumes the stream.
|
|
341
342
|
}
|
|
@@ -375,13 +376,16 @@ describe('GeminiChat', () => {
|
|
|
375
376
|
const emptyStreamResponse = (async function* () {
|
|
376
377
|
yield {
|
|
377
378
|
candidates: [
|
|
378
|
-
{
|
|
379
|
+
{
|
|
380
|
+
content: { role: 'model', parts: [{ thought: true }] },
|
|
381
|
+
finishReason: 'STOP',
|
|
382
|
+
},
|
|
379
383
|
],
|
|
380
384
|
};
|
|
381
385
|
})();
|
|
382
|
-
vi.mocked(
|
|
386
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(emptyStreamResponse);
|
|
383
387
|
// 3. Action: Send the function response back to the model and consume the stream.
|
|
384
|
-
const stream = await chat.sendMessageStream({
|
|
388
|
+
const stream = await chat.sendMessageStream('test-model', {
|
|
385
389
|
message: {
|
|
386
390
|
functionResponse: {
|
|
387
391
|
name: 'find_restaurant',
|
|
@@ -419,147 +423,31 @@ describe('GeminiChat', () => {
|
|
|
419
423
|
},
|
|
420
424
|
],
|
|
421
425
|
text: () => 'response',
|
|
426
|
+
usageMetadata: {
|
|
427
|
+
promptTokenCount: 42,
|
|
428
|
+
candidatesTokenCount: 15,
|
|
429
|
+
totalTokenCount: 57,
|
|
430
|
+
},
|
|
422
431
|
};
|
|
423
432
|
})();
|
|
424
|
-
vi.mocked(
|
|
425
|
-
const stream = await chat.sendMessageStream({ message: 'hello' }, 'prompt-id-1');
|
|
433
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(response);
|
|
434
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'hello' }, 'prompt-id-1');
|
|
426
435
|
for await (const _ of stream) {
|
|
427
|
-
// consume stream
|
|
436
|
+
// consume stream
|
|
428
437
|
}
|
|
429
|
-
expect(
|
|
430
|
-
model: '
|
|
431
|
-
contents: [
|
|
438
|
+
expect(mockContentGenerator.generateContentStream).toHaveBeenCalledWith({
|
|
439
|
+
model: 'test-model',
|
|
440
|
+
contents: [
|
|
441
|
+
{
|
|
442
|
+
role: 'user',
|
|
443
|
+
parts: [{ text: 'hello' }],
|
|
444
|
+
},
|
|
445
|
+
],
|
|
432
446
|
config: {},
|
|
433
447
|
}, 'prompt-id-1');
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
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]);
|
|
448
|
+
// Verify that token counting is called when usageMetadata is present
|
|
449
|
+
expect(uiTelemetryService.setLastPromptTokenCount).toHaveBeenCalledWith(42);
|
|
450
|
+
expect(uiTelemetryService.setLastPromptTokenCount).toHaveBeenCalledTimes(1);
|
|
563
451
|
});
|
|
564
452
|
});
|
|
565
453
|
describe('addHistory', () => {
|
|
@@ -591,9 +479,42 @@ describe('GeminiChat', () => {
|
|
|
591
479
|
});
|
|
592
480
|
});
|
|
593
481
|
describe('sendMessageStream with retries', () => {
|
|
482
|
+
it('should yield a RETRY event when an invalid stream is encountered', async () => {
|
|
483
|
+
// ARRANGE: Mock the stream to fail once, then succeed.
|
|
484
|
+
vi.mocked(mockContentGenerator.generateContentStream)
|
|
485
|
+
.mockImplementationOnce(async () =>
|
|
486
|
+
// First attempt: An invalid stream with an empty text part.
|
|
487
|
+
(async function* () {
|
|
488
|
+
yield {
|
|
489
|
+
candidates: [{ content: { parts: [{ text: '' }] } }],
|
|
490
|
+
};
|
|
491
|
+
})())
|
|
492
|
+
.mockImplementationOnce(async () =>
|
|
493
|
+
// Second attempt (the retry): A minimal valid stream.
|
|
494
|
+
(async function* () {
|
|
495
|
+
yield {
|
|
496
|
+
candidates: [
|
|
497
|
+
{
|
|
498
|
+
content: { parts: [{ text: 'Success' }] },
|
|
499
|
+
finishReason: 'STOP',
|
|
500
|
+
},
|
|
501
|
+
],
|
|
502
|
+
};
|
|
503
|
+
})());
|
|
504
|
+
// ACT: Send a message and collect all events from the stream.
|
|
505
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-yield-retry');
|
|
506
|
+
const events = [];
|
|
507
|
+
for await (const event of stream) {
|
|
508
|
+
events.push(event);
|
|
509
|
+
}
|
|
510
|
+
// ASSERT: Check that a RETRY event was present in the stream's output.
|
|
511
|
+
const retryEvent = events.find((e) => e.type === StreamEventType.RETRY);
|
|
512
|
+
expect(retryEvent).toBeDefined();
|
|
513
|
+
expect(retryEvent?.type).toBe(StreamEventType.RETRY);
|
|
514
|
+
});
|
|
594
515
|
it('should retry on invalid content, succeed, and report metrics', async () => {
|
|
595
516
|
// Use mockImplementationOnce to provide a fresh, promise-wrapped generator for each attempt.
|
|
596
|
-
vi.mocked(
|
|
517
|
+
vi.mocked(mockContentGenerator.generateContentStream)
|
|
597
518
|
.mockImplementationOnce(async () =>
|
|
598
519
|
// First call returns an invalid stream
|
|
599
520
|
(async function* () {
|
|
@@ -606,11 +527,14 @@ describe('GeminiChat', () => {
|
|
|
606
527
|
(async function* () {
|
|
607
528
|
yield {
|
|
608
529
|
candidates: [
|
|
609
|
-
{
|
|
530
|
+
{
|
|
531
|
+
content: { parts: [{ text: 'Successful response' }] },
|
|
532
|
+
finishReason: 'STOP',
|
|
533
|
+
},
|
|
610
534
|
],
|
|
611
535
|
};
|
|
612
536
|
})());
|
|
613
|
-
const stream = await chat.sendMessageStream({ message: 'test' }, 'prompt-id-retry-success');
|
|
537
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-retry-success');
|
|
614
538
|
const chunks = [];
|
|
615
539
|
for await (const chunk of stream) {
|
|
616
540
|
chunks.push(chunk);
|
|
@@ -619,9 +543,13 @@ describe('GeminiChat', () => {
|
|
|
619
543
|
expect(mockLogInvalidChunk).toHaveBeenCalledTimes(1);
|
|
620
544
|
expect(mockLogContentRetry).toHaveBeenCalledTimes(1);
|
|
621
545
|
expect(mockLogContentRetryFailure).not.toHaveBeenCalled();
|
|
622
|
-
expect(
|
|
623
|
-
|
|
624
|
-
|
|
546
|
+
expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
|
|
547
|
+
// Check for a retry event
|
|
548
|
+
expect(chunks.some((c) => c.type === StreamEventType.RETRY)).toBe(true);
|
|
549
|
+
// Check for the successful content chunk
|
|
550
|
+
expect(chunks.some((c) => c.type === StreamEventType.CHUNK &&
|
|
551
|
+
c.value.candidates?.[0]?.content?.parts?.[0]?.text ===
|
|
552
|
+
'Successful response')).toBe(true);
|
|
625
553
|
// Check that history was recorded correctly once, with no duplicates.
|
|
626
554
|
const history = chat.getHistory();
|
|
627
555
|
expect(history.length).toBe(2);
|
|
@@ -633,9 +561,11 @@ describe('GeminiChat', () => {
|
|
|
633
561
|
role: 'model',
|
|
634
562
|
parts: [{ text: 'Successful response' }],
|
|
635
563
|
});
|
|
564
|
+
// Verify that token counting is not called when usageMetadata is missing
|
|
565
|
+
expect(uiTelemetryService.setLastPromptTokenCount).not.toHaveBeenCalled();
|
|
636
566
|
});
|
|
637
567
|
it('should fail after all retries on persistent invalid content and report metrics', async () => {
|
|
638
|
-
vi.mocked(
|
|
568
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockImplementation(async () => (async function* () {
|
|
639
569
|
yield {
|
|
640
570
|
candidates: [
|
|
641
571
|
{
|
|
@@ -647,16 +577,14 @@ describe('GeminiChat', () => {
|
|
|
647
577
|
],
|
|
648
578
|
};
|
|
649
579
|
})());
|
|
650
|
-
|
|
651
|
-
async
|
|
652
|
-
const stream = await chat.sendMessageStream({ message: 'test' }, 'prompt-id-retry-fail');
|
|
580
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-retry-fail');
|
|
581
|
+
await expect(async () => {
|
|
653
582
|
for await (const _ of stream) {
|
|
654
583
|
// Must loop to trigger the internal logic that throws.
|
|
655
584
|
}
|
|
656
|
-
}
|
|
657
|
-
await expect(consumeStreamAndExpectError()).rejects.toThrow(EmptyStreamError);
|
|
585
|
+
}).rejects.toThrow(EmptyStreamError);
|
|
658
586
|
// Should be called 3 times (initial + 2 retries)
|
|
659
|
-
expect(
|
|
587
|
+
expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(3);
|
|
660
588
|
expect(mockLogInvalidChunk).toHaveBeenCalledTimes(3);
|
|
661
589
|
expect(mockLogContentRetry).toHaveBeenCalledTimes(2);
|
|
662
590
|
expect(mockLogContentRetryFailure).toHaveBeenCalledTimes(1);
|
|
@@ -673,7 +601,7 @@ describe('GeminiChat', () => {
|
|
|
673
601
|
];
|
|
674
602
|
chat.setHistory(initialHistory);
|
|
675
603
|
// 2. Mock the API to fail once with an empty stream, then succeed.
|
|
676
|
-
vi.mocked(
|
|
604
|
+
vi.mocked(mockContentGenerator.generateContentStream)
|
|
677
605
|
.mockImplementationOnce(async () => (async function* () {
|
|
678
606
|
yield {
|
|
679
607
|
candidates: [{ content: { parts: [{ text: '' }] } }],
|
|
@@ -683,11 +611,16 @@ describe('GeminiChat', () => {
|
|
|
683
611
|
// Second attempt succeeds
|
|
684
612
|
(async function* () {
|
|
685
613
|
yield {
|
|
686
|
-
candidates: [
|
|
614
|
+
candidates: [
|
|
615
|
+
{
|
|
616
|
+
content: { parts: [{ text: 'Second answer' }] },
|
|
617
|
+
finishReason: 'STOP',
|
|
618
|
+
},
|
|
619
|
+
],
|
|
687
620
|
};
|
|
688
621
|
})());
|
|
689
622
|
// 3. Send a new message
|
|
690
|
-
const stream = await chat.sendMessageStream({ message: 'Second question' }, 'prompt-id-retry-existing');
|
|
623
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'Second question' }, 'prompt-id-retry-existing');
|
|
691
624
|
for await (const _ of stream) {
|
|
692
625
|
// consume stream
|
|
693
626
|
}
|
|
@@ -718,63 +651,9 @@ describe('GeminiChat', () => {
|
|
|
718
651
|
}
|
|
719
652
|
expect(turn4.parts[0].text).toBe('Second answer');
|
|
720
653
|
});
|
|
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
654
|
it('should retry if the model returns a completely empty stream (no chunks)', async () => {
|
|
776
655
|
// 1. Mock the API to return an empty stream first, then a valid one.
|
|
777
|
-
vi.mocked(
|
|
656
|
+
vi.mocked(mockContentGenerator.generateContentStream)
|
|
778
657
|
.mockImplementationOnce(
|
|
779
658
|
// First call resolves to an async generator that yields nothing.
|
|
780
659
|
async () => (async function* () { })())
|
|
@@ -787,20 +666,22 @@ describe('GeminiChat', () => {
|
|
|
787
666
|
content: {
|
|
788
667
|
parts: [{ text: 'Successful response after empty' }],
|
|
789
668
|
},
|
|
669
|
+
finishReason: 'STOP',
|
|
790
670
|
},
|
|
791
671
|
],
|
|
792
672
|
};
|
|
793
673
|
})());
|
|
794
674
|
// 2. Call the method and consume the stream.
|
|
795
|
-
const stream = await chat.sendMessageStream({ message: 'test empty stream' }, 'prompt-id-empty-stream');
|
|
675
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test empty stream' }, 'prompt-id-empty-stream');
|
|
796
676
|
const chunks = [];
|
|
797
677
|
for await (const chunk of stream) {
|
|
798
678
|
chunks.push(chunk);
|
|
799
679
|
}
|
|
800
680
|
// 3. Assert the results.
|
|
801
|
-
expect(
|
|
802
|
-
expect(chunks.some((c) => c.
|
|
803
|
-
|
|
681
|
+
expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
|
|
682
|
+
expect(chunks.some((c) => c.type === StreamEventType.CHUNK &&
|
|
683
|
+
c.value.candidates?.[0]?.content?.parts?.[0]?.text ===
|
|
684
|
+
'Successful response after empty')).toBe(true);
|
|
804
685
|
const history = chat.getHistory();
|
|
805
686
|
expect(history.length).toBe(2);
|
|
806
687
|
// Explicitly verify the structure of each part to satisfy TypeScript
|
|
@@ -830,25 +711,35 @@ describe('GeminiChat', () => {
|
|
|
830
711
|
};
|
|
831
712
|
await firstStreamContinuePromise; // Pause the stream
|
|
832
713
|
yield {
|
|
833
|
-
candidates: [
|
|
714
|
+
candidates: [
|
|
715
|
+
{
|
|
716
|
+
content: { parts: [{ text: ' part 2' }] },
|
|
717
|
+
finishReason: 'STOP',
|
|
718
|
+
},
|
|
719
|
+
],
|
|
834
720
|
};
|
|
835
721
|
})();
|
|
836
722
|
const secondStreamGenerator = (async function* () {
|
|
837
723
|
yield {
|
|
838
|
-
candidates: [
|
|
724
|
+
candidates: [
|
|
725
|
+
{
|
|
726
|
+
content: { parts: [{ text: 'second response' }] },
|
|
727
|
+
finishReason: 'STOP',
|
|
728
|
+
},
|
|
729
|
+
],
|
|
839
730
|
};
|
|
840
731
|
})();
|
|
841
|
-
vi.mocked(
|
|
732
|
+
vi.mocked(mockContentGenerator.generateContentStream)
|
|
842
733
|
.mockResolvedValueOnce(firstStreamGenerator)
|
|
843
734
|
.mockResolvedValueOnce(secondStreamGenerator);
|
|
844
735
|
// 3. Start the first stream and consume only the first chunk to pause it
|
|
845
|
-
const firstStream = await chat.sendMessageStream({ message: 'first' }, 'prompt-1');
|
|
736
|
+
const firstStream = await chat.sendMessageStream('test-model', { message: 'first' }, 'prompt-1');
|
|
846
737
|
const firstStreamIterator = firstStream[Symbol.asyncIterator]();
|
|
847
738
|
await firstStreamIterator.next();
|
|
848
739
|
// 4. While the first stream is paused, start the second call. It will block.
|
|
849
|
-
const secondStreamPromise = chat.sendMessageStream({ message: 'second' }, 'prompt-2');
|
|
740
|
+
const secondStreamPromise = chat.sendMessageStream('test-model', { message: 'second' }, 'prompt-2');
|
|
850
741
|
// 5. Assert that only one API call has been made so far.
|
|
851
|
-
expect(
|
|
742
|
+
expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(1);
|
|
852
743
|
// 6. Unblock and fully consume the first stream to completion.
|
|
853
744
|
continueFirstStream();
|
|
854
745
|
await firstStreamIterator.next(); // Consume the rest of the stream
|
|
@@ -859,7 +750,7 @@ describe('GeminiChat', () => {
|
|
|
859
750
|
const secondStreamIterator = secondStream[Symbol.asyncIterator]();
|
|
860
751
|
await secondStreamIterator.next();
|
|
861
752
|
// 9. The second API call should now have been made.
|
|
862
|
-
expect(
|
|
753
|
+
expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
|
|
863
754
|
// 10. FIX: Fully consume the second stream to ensure recordHistory is called.
|
|
864
755
|
await secondStreamIterator.next(); // This finishes the iterator.
|
|
865
756
|
// 11. Final check on history.
|
|
@@ -871,5 +762,402 @@ describe('GeminiChat', () => {
|
|
|
871
762
|
}
|
|
872
763
|
expect(turn4.parts[0].text).toBe('second response');
|
|
873
764
|
});
|
|
765
|
+
describe('stopBeforeSecondMutator', () => {
|
|
766
|
+
beforeEach(() => {
|
|
767
|
+
// Common setup for these tests: mock the tool registry.
|
|
768
|
+
const mockToolRegistry = {
|
|
769
|
+
getTool: vi.fn((toolName) => {
|
|
770
|
+
if (toolName === 'edit') {
|
|
771
|
+
return { kind: Kind.Edit };
|
|
772
|
+
}
|
|
773
|
+
return { kind: Kind.Other };
|
|
774
|
+
}),
|
|
775
|
+
};
|
|
776
|
+
vi.mocked(mockConfig.getToolRegistry).mockReturnValue(mockToolRegistry);
|
|
777
|
+
});
|
|
778
|
+
it('should stop streaming before a second mutator tool call', async () => {
|
|
779
|
+
const responses = [
|
|
780
|
+
{
|
|
781
|
+
candidates: [
|
|
782
|
+
{ content: { role: 'model', parts: [{ text: 'First part. ' }] } },
|
|
783
|
+
],
|
|
784
|
+
},
|
|
785
|
+
{
|
|
786
|
+
candidates: [
|
|
787
|
+
{
|
|
788
|
+
content: {
|
|
789
|
+
role: 'model',
|
|
790
|
+
parts: [{ functionCall: { name: 'edit', args: {} } }],
|
|
791
|
+
},
|
|
792
|
+
},
|
|
793
|
+
],
|
|
794
|
+
},
|
|
795
|
+
{
|
|
796
|
+
candidates: [
|
|
797
|
+
{
|
|
798
|
+
content: {
|
|
799
|
+
role: 'model',
|
|
800
|
+
parts: [{ functionCall: { name: 'fetch', args: {} } }],
|
|
801
|
+
},
|
|
802
|
+
},
|
|
803
|
+
],
|
|
804
|
+
},
|
|
805
|
+
// This chunk contains the second mutator and should be clipped.
|
|
806
|
+
{
|
|
807
|
+
candidates: [
|
|
808
|
+
{
|
|
809
|
+
content: {
|
|
810
|
+
role: 'model',
|
|
811
|
+
parts: [
|
|
812
|
+
{ functionCall: { name: 'edit', args: {} } },
|
|
813
|
+
{ text: 'some trailing text' },
|
|
814
|
+
],
|
|
815
|
+
},
|
|
816
|
+
},
|
|
817
|
+
],
|
|
818
|
+
},
|
|
819
|
+
// This chunk should never be reached.
|
|
820
|
+
{
|
|
821
|
+
candidates: [
|
|
822
|
+
{
|
|
823
|
+
content: {
|
|
824
|
+
role: 'model',
|
|
825
|
+
parts: [{ text: 'This should not appear.' }],
|
|
826
|
+
},
|
|
827
|
+
},
|
|
828
|
+
],
|
|
829
|
+
},
|
|
830
|
+
];
|
|
831
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue((async function* () {
|
|
832
|
+
for (const response of responses) {
|
|
833
|
+
yield response;
|
|
834
|
+
}
|
|
835
|
+
})());
|
|
836
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-mutator-test');
|
|
837
|
+
for await (const _ of stream) {
|
|
838
|
+
// Consume the stream to trigger history recording.
|
|
839
|
+
}
|
|
840
|
+
const history = chat.getHistory();
|
|
841
|
+
expect(history.length).toBe(2);
|
|
842
|
+
const modelTurn = history[1];
|
|
843
|
+
expect(modelTurn.role).toBe('model');
|
|
844
|
+
expect(modelTurn?.parts?.length).toBe(3);
|
|
845
|
+
expect(modelTurn?.parts[0].text).toBe('First part. ');
|
|
846
|
+
expect(modelTurn.parts[1].functionCall?.name).toBe('edit');
|
|
847
|
+
expect(modelTurn.parts[2].functionCall?.name).toBe('fetch');
|
|
848
|
+
});
|
|
849
|
+
it('should not stop streaming if only one mutator is present', async () => {
|
|
850
|
+
const responses = [
|
|
851
|
+
{
|
|
852
|
+
candidates: [
|
|
853
|
+
{ content: { role: 'model', parts: [{ text: 'Part 1. ' }] } },
|
|
854
|
+
],
|
|
855
|
+
},
|
|
856
|
+
{
|
|
857
|
+
candidates: [
|
|
858
|
+
{
|
|
859
|
+
content: {
|
|
860
|
+
role: 'model',
|
|
861
|
+
parts: [{ functionCall: { name: 'edit', args: {} } }],
|
|
862
|
+
},
|
|
863
|
+
},
|
|
864
|
+
],
|
|
865
|
+
},
|
|
866
|
+
{
|
|
867
|
+
candidates: [
|
|
868
|
+
{
|
|
869
|
+
content: {
|
|
870
|
+
role: 'model',
|
|
871
|
+
parts: [{ text: 'Part 2.' }],
|
|
872
|
+
},
|
|
873
|
+
finishReason: 'STOP',
|
|
874
|
+
},
|
|
875
|
+
],
|
|
876
|
+
},
|
|
877
|
+
];
|
|
878
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue((async function* () {
|
|
879
|
+
for (const response of responses) {
|
|
880
|
+
yield response;
|
|
881
|
+
}
|
|
882
|
+
})());
|
|
883
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-one-mutator');
|
|
884
|
+
for await (const _ of stream) {
|
|
885
|
+
/* consume */
|
|
886
|
+
}
|
|
887
|
+
const history = chat.getHistory();
|
|
888
|
+
const modelTurn = history[1];
|
|
889
|
+
expect(modelTurn?.parts?.length).toBe(3);
|
|
890
|
+
expect(modelTurn.parts[1].functionCall?.name).toBe('edit');
|
|
891
|
+
expect(modelTurn.parts[2].text).toBe('Part 2.');
|
|
892
|
+
});
|
|
893
|
+
it('should clip the chunk containing the second mutator, preserving prior parts', async () => {
|
|
894
|
+
const responses = [
|
|
895
|
+
{
|
|
896
|
+
candidates: [
|
|
897
|
+
{
|
|
898
|
+
content: {
|
|
899
|
+
role: 'model',
|
|
900
|
+
parts: [{ functionCall: { name: 'edit', args: {} } }],
|
|
901
|
+
},
|
|
902
|
+
},
|
|
903
|
+
],
|
|
904
|
+
},
|
|
905
|
+
// This chunk has a valid part before the second mutator.
|
|
906
|
+
// The valid part should be kept, the rest of the chunk discarded.
|
|
907
|
+
{
|
|
908
|
+
candidates: [
|
|
909
|
+
{
|
|
910
|
+
content: {
|
|
911
|
+
role: 'model',
|
|
912
|
+
parts: [
|
|
913
|
+
{ text: 'Keep this text. ' },
|
|
914
|
+
{ functionCall: { name: 'edit', args: {} } },
|
|
915
|
+
{ text: 'Discard this text.' },
|
|
916
|
+
],
|
|
917
|
+
},
|
|
918
|
+
finishReason: 'STOP',
|
|
919
|
+
},
|
|
920
|
+
],
|
|
921
|
+
},
|
|
922
|
+
];
|
|
923
|
+
const stream = (async function* () {
|
|
924
|
+
for (const response of responses) {
|
|
925
|
+
yield response;
|
|
926
|
+
}
|
|
927
|
+
})();
|
|
928
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(stream);
|
|
929
|
+
const resultStream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-clip-chunk');
|
|
930
|
+
for await (const _ of resultStream) {
|
|
931
|
+
/* consume */
|
|
932
|
+
}
|
|
933
|
+
const history = chat.getHistory();
|
|
934
|
+
const modelTurn = history[1];
|
|
935
|
+
expect(modelTurn?.parts?.length).toBe(2);
|
|
936
|
+
expect(modelTurn.parts[0].functionCall?.name).toBe('edit');
|
|
937
|
+
expect(modelTurn.parts[1].text).toBe('Keep this text. ');
|
|
938
|
+
});
|
|
939
|
+
it('should handle two mutators in the same chunk (parallel call scenario)', async () => {
|
|
940
|
+
const responses = [
|
|
941
|
+
{
|
|
942
|
+
candidates: [
|
|
943
|
+
{
|
|
944
|
+
content: {
|
|
945
|
+
role: 'model',
|
|
946
|
+
parts: [
|
|
947
|
+
{ text: 'Some text. ' },
|
|
948
|
+
{ functionCall: { name: 'edit', args: {} } },
|
|
949
|
+
{ functionCall: { name: 'edit', args: {} } },
|
|
950
|
+
],
|
|
951
|
+
},
|
|
952
|
+
finishReason: 'STOP',
|
|
953
|
+
},
|
|
954
|
+
],
|
|
955
|
+
},
|
|
956
|
+
];
|
|
957
|
+
const stream = (async function* () {
|
|
958
|
+
for (const response of responses) {
|
|
959
|
+
yield response;
|
|
960
|
+
}
|
|
961
|
+
})();
|
|
962
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(stream);
|
|
963
|
+
const resultStream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-parallel-mutators');
|
|
964
|
+
for await (const _ of resultStream) {
|
|
965
|
+
/* consume */
|
|
966
|
+
}
|
|
967
|
+
const history = chat.getHistory();
|
|
968
|
+
const modelTurn = history[1];
|
|
969
|
+
expect(modelTurn?.parts?.length).toBe(2);
|
|
970
|
+
expect(modelTurn.parts[0].text).toBe('Some text. ');
|
|
971
|
+
expect(modelTurn.parts[1].functionCall?.name).toBe('edit');
|
|
972
|
+
});
|
|
973
|
+
});
|
|
974
|
+
describe('Model Resolution', () => {
|
|
975
|
+
const mockResponse = {
|
|
976
|
+
candidates: [
|
|
977
|
+
{
|
|
978
|
+
content: { parts: [{ text: 'response' }], role: 'model' },
|
|
979
|
+
finishReason: 'STOP',
|
|
980
|
+
},
|
|
981
|
+
],
|
|
982
|
+
};
|
|
983
|
+
it('should use the FLASH model when in fallback mode (sendMessageStream)', async () => {
|
|
984
|
+
vi.mocked(mockConfig.getModel).mockReturnValue('gemini-pro');
|
|
985
|
+
vi.mocked(mockConfig.isInFallbackMode).mockReturnValue(true);
|
|
986
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockImplementation(async () => (async function* () {
|
|
987
|
+
yield mockResponse;
|
|
988
|
+
})());
|
|
989
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-res3');
|
|
990
|
+
for await (const _ of stream) {
|
|
991
|
+
// consume stream
|
|
992
|
+
}
|
|
993
|
+
expect(mockContentGenerator.generateContentStream).toHaveBeenCalledWith(expect.objectContaining({
|
|
994
|
+
model: DEFAULT_GEMINI_FLASH_MODEL,
|
|
995
|
+
}), 'prompt-id-res3');
|
|
996
|
+
});
|
|
997
|
+
});
|
|
998
|
+
describe('Fallback Integration (Retries)', () => {
|
|
999
|
+
const error429 = Object.assign(new Error('API Error 429: Quota exceeded'), {
|
|
1000
|
+
status: 429,
|
|
1001
|
+
});
|
|
1002
|
+
// Define the simulated behavior for retryWithBackoff for these tests.
|
|
1003
|
+
// This simulation tries the apiCall, if it fails, it calls the callback,
|
|
1004
|
+
// and then tries the apiCall again if the callback returns true.
|
|
1005
|
+
const simulateRetryBehavior = async (apiCall, options) => {
|
|
1006
|
+
try {
|
|
1007
|
+
return await apiCall();
|
|
1008
|
+
}
|
|
1009
|
+
catch (error) {
|
|
1010
|
+
if (options.onPersistent429) {
|
|
1011
|
+
// We simulate the "persistent" trigger here for simplicity.
|
|
1012
|
+
const shouldRetry = await options.onPersistent429(options.authType, error);
|
|
1013
|
+
if (shouldRetry) {
|
|
1014
|
+
return await apiCall();
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
throw error; // Stop if callback returns false/null or doesn't exist
|
|
1018
|
+
}
|
|
1019
|
+
};
|
|
1020
|
+
beforeEach(() => {
|
|
1021
|
+
mockRetryWithBackoff.mockImplementation(simulateRetryBehavior);
|
|
1022
|
+
});
|
|
1023
|
+
afterEach(() => {
|
|
1024
|
+
mockRetryWithBackoff.mockImplementation(async (apiCall) => apiCall());
|
|
1025
|
+
});
|
|
1026
|
+
it('should call handleFallback with the specific failed model and retry if handler returns true', async () => {
|
|
1027
|
+
const authType = AuthType.LOGIN_WITH_GOOGLE;
|
|
1028
|
+
vi.mocked(mockConfig.getContentGeneratorConfig).mockReturnValue({
|
|
1029
|
+
authType,
|
|
1030
|
+
});
|
|
1031
|
+
const isInFallbackModeSpy = vi.spyOn(mockConfig, 'isInFallbackMode');
|
|
1032
|
+
isInFallbackModeSpy.mockReturnValue(false);
|
|
1033
|
+
vi.mocked(mockContentGenerator.generateContentStream)
|
|
1034
|
+
.mockRejectedValueOnce(error429) // Attempt 1 fails
|
|
1035
|
+
.mockResolvedValueOnce(
|
|
1036
|
+
// Attempt 2 succeeds
|
|
1037
|
+
(async function* () {
|
|
1038
|
+
yield {
|
|
1039
|
+
candidates: [
|
|
1040
|
+
{
|
|
1041
|
+
content: { parts: [{ text: 'Success on retry' }] },
|
|
1042
|
+
finishReason: 'STOP',
|
|
1043
|
+
},
|
|
1044
|
+
],
|
|
1045
|
+
};
|
|
1046
|
+
})());
|
|
1047
|
+
mockHandleFallback.mockImplementation(async () => {
|
|
1048
|
+
isInFallbackModeSpy.mockReturnValue(true);
|
|
1049
|
+
return true; // Signal retry
|
|
1050
|
+
});
|
|
1051
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'trigger 429' }, 'prompt-id-fb1');
|
|
1052
|
+
// Consume stream to trigger logic
|
|
1053
|
+
for await (const _ of stream) {
|
|
1054
|
+
// no-op
|
|
1055
|
+
}
|
|
1056
|
+
expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
|
|
1057
|
+
expect(mockHandleFallback).toHaveBeenCalledTimes(1);
|
|
1058
|
+
expect(mockHandleFallback).toHaveBeenCalledWith(mockConfig, 'test-model', authType, error429);
|
|
1059
|
+
const history = chat.getHistory();
|
|
1060
|
+
const modelTurn = history[1];
|
|
1061
|
+
expect(modelTurn.parts[0].text).toBe('Success on retry');
|
|
1062
|
+
});
|
|
1063
|
+
it('should stop retrying if handleFallback returns false (e.g., auth intent)', async () => {
|
|
1064
|
+
vi.mocked(mockConfig.getModel).mockReturnValue('gemini-pro');
|
|
1065
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockRejectedValue(error429);
|
|
1066
|
+
mockHandleFallback.mockResolvedValue(false);
|
|
1067
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test stop' }, 'prompt-id-fb2');
|
|
1068
|
+
await expect((async () => {
|
|
1069
|
+
for await (const _ of stream) {
|
|
1070
|
+
/* consume stream */
|
|
1071
|
+
}
|
|
1072
|
+
})()).rejects.toThrow(error429);
|
|
1073
|
+
expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(1);
|
|
1074
|
+
expect(mockHandleFallback).toHaveBeenCalledTimes(1);
|
|
1075
|
+
});
|
|
1076
|
+
});
|
|
1077
|
+
it('should discard valid partial content from a failed attempt upon retry', async () => {
|
|
1078
|
+
// Mock the stream to fail on the first attempt after yielding some valid content.
|
|
1079
|
+
vi.mocked(mockContentGenerator.generateContentStream)
|
|
1080
|
+
.mockImplementationOnce(async () =>
|
|
1081
|
+
// First attempt: yields one valid chunk, then one invalid chunk
|
|
1082
|
+
(async function* () {
|
|
1083
|
+
yield {
|
|
1084
|
+
candidates: [
|
|
1085
|
+
{
|
|
1086
|
+
content: {
|
|
1087
|
+
parts: [{ text: 'This valid part should be discarded' }],
|
|
1088
|
+
},
|
|
1089
|
+
},
|
|
1090
|
+
],
|
|
1091
|
+
};
|
|
1092
|
+
yield {
|
|
1093
|
+
candidates: [{ content: { parts: [{ text: '' }] } }], // Invalid chunk triggers retry
|
|
1094
|
+
};
|
|
1095
|
+
})())
|
|
1096
|
+
.mockImplementationOnce(async () =>
|
|
1097
|
+
// Second attempt (the retry): succeeds
|
|
1098
|
+
(async function* () {
|
|
1099
|
+
yield {
|
|
1100
|
+
candidates: [
|
|
1101
|
+
{
|
|
1102
|
+
content: {
|
|
1103
|
+
parts: [{ text: 'Successful final response' }],
|
|
1104
|
+
},
|
|
1105
|
+
finishReason: 'STOP',
|
|
1106
|
+
},
|
|
1107
|
+
],
|
|
1108
|
+
};
|
|
1109
|
+
})());
|
|
1110
|
+
// Send a message and consume the stream
|
|
1111
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-discard-test');
|
|
1112
|
+
const events = [];
|
|
1113
|
+
for await (const event of stream) {
|
|
1114
|
+
events.push(event);
|
|
1115
|
+
}
|
|
1116
|
+
// Check that a retry happened
|
|
1117
|
+
expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
|
|
1118
|
+
expect(events.some((e) => e.type === StreamEventType.RETRY)).toBe(true);
|
|
1119
|
+
// Check the final recorded history
|
|
1120
|
+
const history = chat.getHistory();
|
|
1121
|
+
expect(history.length).toBe(2); // user turn + final model turn
|
|
1122
|
+
const modelTurn = history[1];
|
|
1123
|
+
// The model turn should only contain the text from the successful attempt
|
|
1124
|
+
expect(modelTurn.parts[0].text).toBe('Successful final response');
|
|
1125
|
+
// It should NOT contain any text from the failed attempt
|
|
1126
|
+
expect(modelTurn.parts[0].text).not.toContain('This valid part should be discarded');
|
|
1127
|
+
});
|
|
1128
|
+
describe('stripThoughtsFromHistory', () => {
|
|
1129
|
+
it('should strip thought signatures', () => {
|
|
1130
|
+
chat.setHistory([
|
|
1131
|
+
{
|
|
1132
|
+
role: 'user',
|
|
1133
|
+
parts: [{ text: 'hello' }],
|
|
1134
|
+
},
|
|
1135
|
+
{
|
|
1136
|
+
role: 'model',
|
|
1137
|
+
parts: [
|
|
1138
|
+
{ text: 'thinking...', thoughtSignature: 'thought-123' },
|
|
1139
|
+
{
|
|
1140
|
+
functionCall: { name: 'test', args: {} },
|
|
1141
|
+
thoughtSignature: 'thought-456',
|
|
1142
|
+
},
|
|
1143
|
+
],
|
|
1144
|
+
},
|
|
1145
|
+
]);
|
|
1146
|
+
chat.stripThoughtsFromHistory();
|
|
1147
|
+
expect(chat.getHistory()).toEqual([
|
|
1148
|
+
{
|
|
1149
|
+
role: 'user',
|
|
1150
|
+
parts: [{ text: 'hello' }],
|
|
1151
|
+
},
|
|
1152
|
+
{
|
|
1153
|
+
role: 'model',
|
|
1154
|
+
parts: [
|
|
1155
|
+
{ text: 'thinking...' },
|
|
1156
|
+
{ functionCall: { name: 'test', args: {} } },
|
|
1157
|
+
],
|
|
1158
|
+
},
|
|
1159
|
+
]);
|
|
1160
|
+
});
|
|
1161
|
+
});
|
|
874
1162
|
});
|
|
875
1163
|
//# sourceMappingURL=geminiChat.test.js.map
|