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