@office-ai/aioncli-core 0.2.3 → 0.8.1
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 +10 -3
- package/dist/index.js +10 -3
- package/dist/index.js.map +1 -1
- package/dist/src/agents/codebase-investigator.d.ts +11 -0
- package/dist/src/agents/codebase-investigator.js +73 -0
- package/dist/src/agents/codebase-investigator.js.map +1 -0
- package/dist/src/agents/executor.d.ts +88 -0
- package/dist/src/agents/executor.js +417 -0
- package/dist/src/agents/executor.js.map +1 -0
- package/dist/src/agents/executor.test.js +419 -0
- package/dist/src/agents/executor.test.js.map +1 -0
- package/dist/src/agents/invocation.d.ts +43 -0
- package/dist/src/agents/invocation.js +100 -0
- package/dist/src/agents/invocation.js.map +1 -0
- package/dist/src/agents/invocation.test.js +206 -0
- package/dist/src/agents/invocation.test.js.map +1 -0
- package/dist/src/agents/registry.d.ts +35 -0
- package/dist/src/agents/registry.js +58 -0
- package/dist/src/agents/registry.js.map +1 -0
- package/dist/src/agents/registry.test.js +146 -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 +36 -0
- package/dist/src/agents/subagent-tool-wrapper.js +47 -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 +105 -0
- package/dist/src/agents/subagent-tool-wrapper.test.js.map +1 -0
- package/dist/src/agents/types.d.ts +116 -0
- package/dist/src/agents/types.js +17 -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 +12 -0
- package/dist/src/code_assist/codeAssist.js.map +1 -1
- package/dist/src/code_assist/converter.d.ts +4 -1
- package/dist/src/code_assist/converter.js +38 -5
- package/dist/src/code_assist/converter.js.map +1 -1
- package/dist/src/code_assist/converter.test.js +93 -0
- package/dist/src/code_assist/converter.test.js.map +1 -1
- package/dist/src/code_assist/oauth-credential-storage.d.ts +25 -0
- package/dist/src/code_assist/oauth-credential-storage.js +109 -0
- package/dist/src/code_assist/oauth-credential-storage.js.map +1 -0
- package/dist/src/code_assist/oauth-credential-storage.test.d.ts +6 -0
- package/dist/src/code_assist/oauth-credential-storage.test.js +136 -0
- package/dist/src/code_assist/oauth-credential-storage.test.js.map +1 -0
- package/dist/src/code_assist/oauth2.d.ts +1 -1
- package/dist/src/code_assist/oauth2.js +107 -48
- package/dist/src/code_assist/oauth2.js.map +1 -1
- package/dist/src/code_assist/oauth2.test.js +735 -343
- package/dist/src/code_assist/oauth2.test.js.map +1 -1
- package/dist/src/code_assist/server.d.ts +4 -4
- package/dist/src/code_assist/server.js +25 -2
- package/dist/src/code_assist/server.js.map +1 -1
- package/dist/src/code_assist/server.test.js +25 -0
- package/dist/src/code_assist/server.test.js.map +1 -1
- package/dist/src/code_assist/setup.d.ts +1 -1
- package/dist/src/code_assist/setup.js +1 -1
- 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 +17 -2
- package/dist/src/config/config.d.ts +121 -25
- package/dist/src/config/config.js +298 -89
- package/dist/src/config/config.js.map +1 -1
- package/dist/src/config/config.test.js +370 -131
- 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/models.d.ts +16 -0
- package/dist/src/config/models.js +29 -0
- package/dist/src/config/models.js.map +1 -1
- package/dist/src/config/models.test.d.ts +6 -0
- package/dist/src/config/models.test.js +55 -0
- package/dist/src/config/models.test.js.map +1 -0
- package/dist/src/config/storage.d.ts +34 -0
- package/dist/src/config/storage.js +95 -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 +47 -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 +17 -0
- package/dist/src/confirmation-bus/message-bus.js +81 -0
- package/dist/src/confirmation-bus/message-bus.js.map +1 -0
- package/dist/src/confirmation-bus/message-bus.test.d.ts +6 -0
- package/dist/src/confirmation-bus/message-bus.test.js +164 -0
- package/dist/src/confirmation-bus/message-bus.test.js.map +1 -0
- package/dist/src/confirmation-bus/types.d.ts +38 -0
- package/dist/src/confirmation-bus/types.js +15 -0
- package/dist/src/confirmation-bus/types.js.map +1 -0
- package/dist/src/core/baseLlmClient.d.ts +54 -0
- package/dist/src/core/baseLlmClient.js +190 -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 +316 -0
- package/dist/src/core/baseLlmClient.test.js.map +1 -0
- package/dist/src/core/client.d.ts +28 -33
- package/dist/src/core/client.js +187 -384
- package/dist/src/core/client.js.map +1 -1
- package/dist/src/core/client.test.js +745 -500
- package/dist/src/core/client.test.js.map +1 -1
- package/dist/src/core/contentGenerator.d.ts +4 -4
- package/dist/src/core/contentGenerator.js +6 -7
- package/dist/src/core/contentGenerator.js.map +1 -1
- package/dist/src/core/contentGenerator.test.js +1 -3
- package/dist/src/core/contentGenerator.test.js.map +1 -1
- package/dist/src/core/coreToolScheduler.d.ts +20 -7
- package/dist/src/core/coreToolScheduler.js +216 -53
- package/dist/src/core/coreToolScheduler.js.map +1 -1
- package/dist/src/core/coreToolScheduler.test.js +564 -88
- package/dist/src/core/coreToolScheduler.test.js.map +1 -1
- package/dist/src/core/geminiChat.d.ts +54 -53
- package/dist/src/core/geminiChat.js +300 -356
- package/dist/src/core/geminiChat.js.map +1 -1
- package/dist/src/core/geminiChat.test.js +1255 -321
- 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 +4 -2
- package/dist/src/core/logger.js +4 -3
- package/dist/src/core/logger.js.map +1 -1
- package/dist/src/core/logger.test.js +17 -16
- package/dist/src/core/logger.test.js.map +1 -1
- package/dist/src/core/loggingContentGenerator.d.ts +3 -3
- package/dist/src/core/loggingContentGenerator.js +15 -16
- package/dist/src/core/loggingContentGenerator.js.map +1 -1
- package/dist/src/core/nonInteractiveToolExecutor.d.ts +3 -5
- package/dist/src/core/nonInteractiveToolExecutor.js +14 -122
- package/dist/src/core/nonInteractiveToolExecutor.js.map +1 -1
- package/dist/src/core/nonInteractiveToolExecutor.test.js +158 -78
- 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 +21 -14
- 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 +5 -0
- package/dist/src/core/prompts.js +66 -44
- package/dist/src/core/prompts.js.map +1 -1
- package/dist/src/core/prompts.test.js +130 -1
- package/dist/src/core/prompts.test.js.map +1 -1
- package/dist/src/core/subagent.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/turn.d.ts +37 -13
- package/dist/src/core/turn.js +63 -28
- package/dist/src/core/turn.js.map +1 -1
- package/dist/src/core/turn.test.js +359 -100
- package/dist/src/core/turn.test.js.map +1 -1
- package/dist/src/fallback/handler.d.ts +7 -0
- package/dist/src/fallback/handler.js +129 -0
- package/dist/src/fallback/handler.js.map +1 -0
- package/dist/src/fallback/handler.test.d.ts +6 -0
- package/dist/src/fallback/handler.test.js +130 -0
- package/dist/src/fallback/handler.test.js.map +1 -0
- package/dist/src/fallback/types.d.ts +14 -0
- package/dist/src/fallback/types.js +7 -0
- package/dist/src/fallback/types.js.map +1 -0
- package/dist/src/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 +48 -12
- package/dist/src/ide/detect-ide.js +47 -66
- package/dist/src/ide/detect-ide.js.map +1 -1
- package/dist/src/ide/detect-ide.test.js +79 -52
- package/dist/src/ide/detect-ide.test.js.map +1 -1
- package/dist/src/ide/ide-client.d.ts +69 -23
- package/dist/src/ide/ide-client.js +372 -78
- package/dist/src/ide/ide-client.js.map +1 -1
- package/dist/src/ide/ide-client.test.js +375 -30
- 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 +37 -24
- package/dist/src/ide/ide-installer.js.map +1 -1
- package/dist/src/ide/ide-installer.test.js +104 -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 +81 -50
- 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 +158 -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 +20 -2
- package/dist/src/index.js +20 -2
- package/dist/src/index.js.map +1 -1
- package/dist/src/mcp/google-auth-provider.d.ts +3 -3
- package/dist/src/mcp/google-auth-provider.test.js.map +1 -1
- package/dist/src/mcp/oauth-provider.d.ts +17 -13
- package/dist/src/mcp/oauth-provider.js +81 -69
- package/dist/src/mcp/oauth-provider.js.map +1 -1
- package/dist/src/mcp/oauth-provider.test.js +212 -37
- 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 +54 -25
- package/dist/src/mcp/oauth-token-storage.js.map +1 -1
- package/dist/src/mcp/oauth-token-storage.test.js +256 -162
- package/dist/src/mcp/oauth-token-storage.test.js.map +1 -1
- package/dist/src/mcp/oauth-utils.d.ts +9 -1
- package/dist/src/mcp/oauth-utils.js +42 -27
- package/dist/src/mcp/oauth-utils.js.map +1 -1
- package/dist/src/mcp/oauth-utils.test.js +41 -1
- package/dist/src/mcp/oauth-utils.test.js.map +1 -1
- package/dist/src/mcp/sa-impersonation-provider.d.ts +33 -0
- package/dist/src/mcp/sa-impersonation-provider.js +130 -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 +160 -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 +144 -0
- package/dist/src/mcp/token-storage/file-token-storage.js.map +1 -0
- package/dist/src/mcp/token-storage/file-token-storage.test.d.ts +6 -0
- package/dist/src/mcp/token-storage/file-token-storage.test.js +235 -0
- package/dist/src/mcp/token-storage/file-token-storage.test.js.map +1 -0
- package/dist/src/mcp/token-storage/hybrid-token-storage.d.ts +23 -0
- package/dist/src/mcp/token-storage/hybrid-token-storage.js +78 -0
- package/dist/src/mcp/token-storage/hybrid-token-storage.js.map +1 -0
- package/dist/src/mcp/token-storage/hybrid-token-storage.test.d.ts +6 -0
- package/dist/src/mcp/token-storage/hybrid-token-storage.test.js +193 -0
- package/dist/src/mcp/token-storage/hybrid-token-storage.test.js.map +1 -0
- package/dist/src/mcp/token-storage/index.d.ts +11 -0
- package/dist/src/mcp/token-storage/index.js +12 -0
- package/dist/src/mcp/token-storage/index.js.map +1 -0
- package/dist/src/mcp/token-storage/keychain-token-storage.d.ts +31 -0
- package/dist/src/mcp/token-storage/keychain-token-storage.js +190 -0
- package/dist/src/mcp/token-storage/keychain-token-storage.js.map +1 -0
- package/dist/src/mcp/token-storage/keychain-token-storage.test.d.ts +6 -0
- package/dist/src/mcp/token-storage/keychain-token-storage.test.js +254 -0
- package/dist/src/mcp/token-storage/keychain-token-storage.test.js.map +1 -0
- package/dist/src/mcp/token-storage/types.d.ts +38 -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/types.d.ts +20 -0
- package/dist/src/output/types.js +11 -0
- package/dist/src/output/types.js.map +1 -0
- package/dist/src/policy/index.d.ts +7 -0
- package/dist/src/policy/index.js +8 -0
- package/dist/src/policy/index.js.map +1 -0
- package/dist/src/policy/policy-engine.d.ts +30 -0
- package/dist/src/policy/policy-engine.js +92 -0
- package/dist/src/policy/policy-engine.js.map +1 -0
- package/dist/src/policy/policy-engine.test.d.ts +6 -0
- package/dist/src/policy/policy-engine.test.js +515 -0
- package/dist/src/policy/policy-engine.test.js.map +1 -0
- package/dist/src/policy/stable-stringify.d.ts +58 -0
- package/dist/src/policy/stable-stringify.js +122 -0
- package/dist/src/policy/stable-stringify.js.map +1 -0
- package/dist/src/policy/types.d.ts +47 -0
- package/dist/src/policy/types.js +12 -0
- package/dist/src/policy/types.js.map +1 -0
- package/dist/src/prompts/mcp-prompts.d.ts +2 -2
- package/dist/src/prompts/prompt-registry.d.ts +1 -1
- package/dist/src/routing/modelRouterService.d.ts +23 -0
- package/dist/src/routing/modelRouterService.js +70 -0
- package/dist/src/routing/modelRouterService.js.map +1 -0
- package/dist/src/routing/modelRouterService.test.d.ts +6 -0
- package/dist/src/routing/modelRouterService.test.js +98 -0
- package/dist/src/routing/modelRouterService.test.js.map +1 -0
- package/dist/src/routing/routingStrategy.d.ts +62 -0
- package/dist/src/routing/routingStrategy.js +7 -0
- package/dist/src/routing/routingStrategy.js.map +1 -0
- package/dist/src/routing/strategies/classifierStrategy.d.ts +12 -0
- package/dist/src/routing/strategies/classifierStrategy.js +173 -0
- package/dist/src/routing/strategies/classifierStrategy.js.map +1 -0
- package/dist/src/routing/strategies/classifierStrategy.test.d.ts +6 -0
- package/dist/src/routing/strategies/classifierStrategy.test.js +192 -0
- package/dist/src/routing/strategies/classifierStrategy.test.js.map +1 -0
- package/dist/src/routing/strategies/compositeStrategy.d.ts +26 -0
- package/dist/src/routing/strategies/compositeStrategy.js +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 +55 -0
- package/dist/src/routing/strategies/fallbackStrategy.test.js.map +1 -0
- package/dist/src/routing/strategies/overrideStrategy.d.ts +15 -0
- package/dist/src/routing/strategies/overrideStrategy.js +28 -0
- package/dist/src/routing/strategies/overrideStrategy.js.map +1 -0
- package/dist/src/routing/strategies/overrideStrategy.test.d.ts +6 -0
- package/dist/src/routing/strategies/overrideStrategy.test.js +42 -0
- package/dist/src/routing/strategies/overrideStrategy.test.js.map +1 -0
- package/dist/src/services/chatRecordingService.d.ts +8 -14
- package/dist/src/services/chatRecordingService.js +33 -21
- 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 +10 -0
- package/dist/src/services/fileDiscoveryService.js +32 -18
- package/dist/src/services/fileDiscoveryService.js.map +1 -1
- package/dist/src/services/fileDiscoveryService.test.js +3 -3
- 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 +8 -2
- package/dist/src/services/loopDetectionService.js +64 -24
- package/dist/src/services/loopDetectionService.js.map +1 -1
- package/dist/src/services/loopDetectionService.test.js +64 -13
- package/dist/src/services/loopDetectionService.test.js.map +1 -1
- package/dist/src/services/shellExecutionService.d.ts +36 -2
- package/dist/src/services/shellExecutionService.js +238 -47
- package/dist/src/services/shellExecutionService.js.map +1 -1
- package/dist/src/services/shellExecutionService.test.js +197 -58
- package/dist/src/services/shellExecutionService.test.js.map +1 -1
- package/dist/src/telemetry/activity-detector.d.ts +41 -0
- package/dist/src/telemetry/activity-detector.js +61 -0
- package/dist/src/telemetry/activity-detector.js.map +1 -0
- package/dist/src/telemetry/activity-detector.test.d.ts +6 -0
- package/dist/src/telemetry/activity-detector.test.js +136 -0
- package/dist/src/telemetry/activity-detector.test.js.map +1 -0
- package/dist/src/telemetry/activity-types.d.ts +19 -0
- package/dist/src/telemetry/activity-types.js +21 -0
- package/dist/src/telemetry/activity-types.js.map +1 -0
- package/dist/src/telemetry/clearcut-logger/clearcut-logger.d.ts +34 -4
- package/dist/src/telemetry/clearcut-logger/clearcut-logger.js +322 -15
- package/dist/src/telemetry/clearcut-logger/clearcut-logger.js.map +1 -1
- package/dist/src/telemetry/clearcut-logger/clearcut-logger.test.d.ts +1 -0
- package/dist/src/telemetry/clearcut-logger/clearcut-logger.test.js +321 -11
- package/dist/src/telemetry/clearcut-logger/clearcut-logger.test.js.map +1 -1
- package/dist/src/telemetry/clearcut-logger/event-metadata-key.d.ts +51 -2
- package/dist/src/telemetry/clearcut-logger/event-metadata-key.js +124 -2
- 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 +17 -7
- package/dist/src/telemetry/constants.js +18 -7
- 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 +117 -0
- package/dist/src/telemetry/gcp-exporters.js.map +1 -0
- package/dist/src/telemetry/gcp-exporters.test.d.ts +6 -0
- package/dist/src/telemetry/gcp-exporters.test.js +318 -0
- package/dist/src/telemetry/gcp-exporters.test.js.map +1 -0
- package/dist/src/telemetry/high-water-mark-tracker.d.ts +43 -0
- package/dist/src/telemetry/high-water-mark-tracker.js +88 -0
- package/dist/src/telemetry/high-water-mark-tracker.js.map +1 -0
- package/dist/src/telemetry/high-water-mark-tracker.test.d.ts +6 -0
- package/dist/src/telemetry/high-water-mark-tracker.test.js +152 -0
- package/dist/src/telemetry/high-water-mark-tracker.test.js.map +1 -0
- package/dist/src/telemetry/index.d.ts +12 -2
- package/dist/src/telemetry/index.js +16 -2
- package/dist/src/telemetry/index.js.map +1 -1
- package/dist/src/telemetry/loggers.d.ts +17 -2
- package/dist/src/telemetry/loggers.js +316 -14
- package/dist/src/telemetry/loggers.js.map +1 -1
- package/dist/src/telemetry/loggers.test.circular.js +3 -3
- package/dist/src/telemetry/loggers.test.circular.js.map +1 -1
- package/dist/src/telemetry/loggers.test.js +452 -48
- package/dist/src/telemetry/loggers.test.js.map +1 -1
- package/dist/src/telemetry/metrics.d.ts +323 -12
- package/dist/src/telemetry/metrics.js +464 -83
- package/dist/src/telemetry/metrics.js.map +1 -1
- package/dist/src/telemetry/metrics.test.js +583 -38
- 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 +20 -2
- 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/telemetry-utils.d.ts +6 -0
- package/dist/src/telemetry/telemetry-utils.js +14 -0
- package/dist/src/telemetry/telemetry-utils.js.map +1 -0
- package/dist/src/telemetry/telemetry-utils.test.d.ts +6 -0
- package/dist/src/telemetry/telemetry-utils.test.js +40 -0
- package/dist/src/telemetry/telemetry-utils.test.js.map +1 -0
- package/dist/src/telemetry/types.d.ts +136 -8
- package/dist/src/telemetry/types.js +233 -11
- package/dist/src/telemetry/types.js.map +1 -1
- package/dist/src/telemetry/uiTelemetry.d.ts +3 -3
- package/dist/src/telemetry/uiTelemetry.js +7 -8
- package/dist/src/telemetry/uiTelemetry.js.map +1 -1
- package/dist/src/telemetry/uiTelemetry.test.js +33 -29
- package/dist/src/telemetry/uiTelemetry.test.js.map +1 -1
- package/dist/src/test-utils/config.d.ts +2 -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/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 +6 -5
- package/dist/src/tools/edit.js +58 -40
- package/dist/src/tools/edit.js.map +1 -1
- package/dist/src/tools/edit.test.js +192 -16
- package/dist/src/tools/edit.test.js.map +1 -1
- package/dist/src/tools/glob.d.ts +7 -2
- package/dist/src/tools/glob.js +42 -23
- package/dist/src/tools/glob.js.map +1 -1
- package/dist/src/tools/glob.test.js +80 -4
- package/dist/src/tools/glob.test.js.map +1 -1
- package/dist/src/tools/grep.d.ts +3 -2
- package/dist/src/tools/grep.js +35 -15
- package/dist/src/tools/grep.js.map +1 -1
- package/dist/src/tools/grep.test.js +26 -3
- package/dist/src/tools/grep.test.js.map +1 -1
- package/dist/src/tools/ls.d.ts +3 -2
- package/dist/src/tools/ls.js +31 -39
- package/dist/src/tools/ls.js.map +1 -1
- package/dist/src/tools/ls.test.js +145 -280
- package/dist/src/tools/ls.test.js.map +1 -1
- package/dist/src/tools/mcp-client-manager.d.ts +8 -6
- package/dist/src/tools/mcp-client-manager.js +13 -4
- package/dist/src/tools/mcp-client-manager.js.map +1 -1
- package/dist/src/tools/mcp-client-manager.test.js +20 -1
- package/dist/src/tools/mcp-client-manager.test.js.map +1 -1
- package/dist/src/tools/mcp-client.d.ts +18 -21
- package/dist/src/tools/mcp-client.js +87 -120
- package/dist/src/tools/mcp-client.js.map +1 -1
- package/dist/src/tools/mcp-client.test.js +32 -152
- package/dist/src/tools/mcp-client.test.js.map +1 -1
- package/dist/src/tools/mcp-tool.d.ts +6 -4
- package/dist/src/tools/mcp-tool.js +51 -13
- package/dist/src/tools/mcp-tool.js.map +1 -1
- package/dist/src/tools/mcp-tool.test.js +166 -12
- package/dist/src/tools/mcp-tool.test.js.map +1 -1
- package/dist/src/tools/memoryTool.d.ts +3 -2
- package/dist/src/tools/memoryTool.js +14 -37
- package/dist/src/tools/memoryTool.js.map +1 -1
- package/dist/src/tools/memoryTool.test.js +16 -4
- 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 +183 -0
- package/dist/src/tools/message-bus-integration.test.js.map +1 -0
- package/dist/src/tools/modifiable-tool.d.ts +2 -2
- package/dist/src/tools/modifiable-tool.js +3 -3
- package/dist/src/tools/modifiable-tool.js.map +1 -1
- package/dist/src/tools/modifiable-tool.test.js +4 -4
- package/dist/src/tools/modifiable-tool.test.js.map +1 -1
- package/dist/src/tools/read-file.d.ts +3 -2
- package/dist/src/tools/read-file.js +23 -38
- package/dist/src/tools/read-file.js.map +1 -1
- package/dist/src/tools/read-file.test.js +38 -6
- package/dist/src/tools/read-file.test.js.map +1 -1
- package/dist/src/tools/read-many-files.d.ts +3 -2
- package/dist/src/tools/read-many-files.js +52 -107
- package/dist/src/tools/read-many-files.js.map +1 -1
- package/dist/src/tools/read-many-files.test.js +64 -11
- package/dist/src/tools/read-many-files.test.js.map +1 -1
- package/dist/src/tools/ripGrep.d.ts +55 -0
- package/dist/src/tools/ripGrep.js +393 -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 +976 -0
- package/dist/src/tools/ripGrep.test.js.map +1 -0
- package/dist/src/tools/shell.d.ts +13 -2
- package/dist/src/tools/shell.js +42 -32
- package/dist/src/tools/shell.js.map +1 -1
- package/dist/src/tools/shell.test.js +57 -75
- package/dist/src/tools/shell.test.js.map +1 -1
- package/dist/src/tools/smart-edit.d.ts +91 -0
- package/dist/src/tools/smart-edit.js +702 -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 +542 -0
- package/dist/src/tools/smart-edit.test.js.map +1 -0
- package/dist/src/tools/tool-error.d.ts +18 -1
- package/dist/src/tools/tool-error.js +27 -0
- package/dist/src/tools/tool-error.js.map +1 -1
- package/dist/src/tools/tool-registry.d.ts +10 -4
- package/dist/src/tools/tool-registry.js +20 -7
- package/dist/src/tools/tool-registry.js.map +1 -1
- package/dist/src/tools/tool-registry.test.js +93 -10
- package/dist/src/tools/tool-registry.test.js.map +1 -1
- package/dist/src/tools/tools.d.ts +33 -16
- package/dist/src/tools/tools.js +115 -5
- 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 +3 -2
- package/dist/src/tools/web-fetch.js +14 -10
- package/dist/src/tools/web-fetch.js.map +1 -1
- package/dist/src/tools/web-fetch.test.js +55 -16
- package/dist/src/tools/web-fetch.test.js.map +1 -1
- package/dist/src/tools/web-search.d.ts +4 -3
- package/dist/src/tools/web-search.js +31 -8
- package/dist/src/tools/web-search.js.map +1 -1
- package/dist/src/tools/web-search.test.js +69 -1
- package/dist/src/tools/web-search.test.js.map +1 -1
- package/dist/src/tools/write-file.d.ts +4 -3
- package/dist/src/tools/write-file.js +17 -18
- package/dist/src/tools/write-file.js.map +1 -1
- package/dist/src/tools/write-file.test.js +108 -24
- package/dist/src/tools/write-file.test.js.map +1 -1
- package/dist/src/tools/write-todos.d.ts +25 -0
- package/dist/src/tools/write-todos.js +150 -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 +13 -7
- 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/editCorrector.d.ts +9 -8
- package/dist/src/utils/editCorrector.js +62 -19
- package/dist/src/utils/editCorrector.js.map +1 -1
- package/dist/src/utils/editCorrector.test.js +33 -82
- package/dist/src/utils/editCorrector.test.js.map +1 -1
- package/dist/src/utils/editor.js +32 -45
- package/dist/src/utils/editor.js.map +1 -1
- package/dist/src/utils/editor.test.js +62 -76
- package/dist/src/utils/editor.test.js.map +1 -1
- package/dist/src/utils/environmentContext.d.ts +2 -2
- package/dist/src/utils/errorParsing.js +2 -2
- package/dist/src/utils/errorParsing.js.map +1 -1
- package/dist/src/utils/errorParsing.test.js +7 -7
- package/dist/src/utils/errorParsing.test.js.map +1 -1
- package/dist/src/utils/errorReporting.d.ts +1 -1
- package/dist/src/utils/errors.d.ts +25 -0
- package/dist/src/utils/errors.js +42 -0
- package/dist/src/utils/errors.js.map +1 -1
- package/dist/src/utils/fetch.js +1 -1
- package/dist/src/utils/fetch.js.map +1 -1
- package/dist/src/utils/fileUtils.d.ts +24 -12
- package/dist/src/utils/fileUtils.js +170 -79
- package/dist/src/utils/fileUtils.js.map +1 -1
- package/dist/src/utils/fileUtils.test.js +347 -29
- package/dist/src/utils/fileUtils.test.js.map +1 -1
- package/dist/src/utils/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.integration.test.js → flashFallback.test.js} +33 -29
- package/dist/src/utils/flashFallback.test.js.map +1 -0
- package/dist/src/utils/geminiIgnoreParser.d.ts +18 -0
- package/dist/src/utils/geminiIgnoreParser.js +61 -0
- package/dist/src/utils/geminiIgnoreParser.js.map +1 -0
- package/dist/src/utils/geminiIgnoreParser.test.d.ts +6 -0
- package/dist/src/utils/geminiIgnoreParser.test.js +50 -0
- package/dist/src/utils/geminiIgnoreParser.test.js.map +1 -0
- package/dist/src/utils/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 +3 -3
- package/dist/src/utils/getFolderStructure.js.map +1 -1
- package/dist/src/utils/getFolderStructure.test.js +4 -4
- package/dist/src/utils/getFolderStructure.test.js.map +1 -1
- package/dist/src/utils/gitIgnoreParser.d.ts +3 -7
- package/dist/src/utils/gitIgnoreParser.js +126 -35
- package/dist/src/utils/gitIgnoreParser.js.map +1 -1
- package/dist/src/utils/gitIgnoreParser.test.js +69 -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/ignorePatterns.d.ts +103 -0
- package/dist/src/utils/ignorePatterns.js +220 -0
- package/dist/src/utils/ignorePatterns.js.map +1 -0
- package/dist/src/utils/ignorePatterns.test.d.ts +6 -0
- package/dist/src/utils/ignorePatterns.test.js +250 -0
- package/dist/src/utils/ignorePatterns.test.js.map +1 -0
- package/dist/src/utils/installationManager.d.ts +16 -0
- package/dist/src/utils/installationManager.js +50 -0
- package/dist/src/utils/installationManager.js.map +1 -0
- package/dist/src/utils/installationManager.test.d.ts +6 -0
- package/dist/src/utils/installationManager.test.js +83 -0
- package/dist/src/utils/installationManager.test.js.map +1 -0
- package/dist/src/utils/language-detection.d.ts +6 -0
- package/dist/src/utils/language-detection.js +101 -0
- package/dist/src/utils/language-detection.js.map +1 -0
- package/dist/src/utils/llm-edit-fixer.d.ts +26 -0
- package/dist/src/utils/llm-edit-fixer.js +131 -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 +186 -0
- package/dist/src/utils/llm-edit-fixer.test.js.map +1 -0
- package/dist/src/utils/memoryDiscovery.d.ts +7 -6
- package/dist/src/utils/memoryDiscovery.js +68 -33
- package/dist/src/utils/memoryDiscovery.js.map +1 -1
- package/dist/src/utils/memoryDiscovery.test.js +88 -26
- package/dist/src/utils/memoryDiscovery.test.js.map +1 -1
- package/dist/src/utils/memoryImportProcessor.js +15 -22
- package/dist/src/utils/memoryImportProcessor.js.map +1 -1
- package/dist/src/utils/memoryImportProcessor.test.js +16 -141
- 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 +8 -2
- package/dist/src/utils/nextSpeakerChecker.js.map +1 -1
- package/dist/src/utils/nextSpeakerChecker.test.js +75 -64
- package/dist/src/utils/nextSpeakerChecker.test.js.map +1 -1
- package/dist/src/utils/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/pathReader.d.ts +17 -0
- package/dist/src/utils/pathReader.js +92 -0
- package/dist/src/utils/pathReader.js.map +1 -0
- package/dist/src/utils/pathReader.test.d.ts +6 -0
- package/dist/src/utils/pathReader.test.js +363 -0
- package/dist/src/utils/pathReader.test.js.map +1 -0
- package/dist/src/utils/paths.d.ts +0 -17
- package/dist/src/utils/paths.js +2 -28
- package/dist/src/utils/paths.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 -1
- package/dist/src/utils/retry.d.ts +3 -1
- package/dist/src/utils/retry.js +20 -5
- package/dist/src/utils/retry.js.map +1 -1
- package/dist/src/utils/retry.test.js +35 -3
- package/dist/src/utils/retry.test.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 +6 -1
- package/dist/src/utils/shell-utils.js +51 -30
- package/dist/src/utils/shell-utils.js.map +1 -1
- package/dist/src/utils/shell-utils.test.js +9 -0
- package/dist/src/utils/shell-utils.test.js.map +1 -1
- package/dist/src/utils/summarizer.d.ts +2 -2
- package/dist/src/utils/summarizer.test.js.map +1 -1
- package/dist/src/utils/systemEncoding.js +2 -2
- package/dist/src/utils/systemEncoding.js.map +1 -1
- package/dist/src/utils/systemEncoding.test.js +2 -2
- package/dist/src/utils/systemEncoding.test.js.map +1 -1
- 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 +58 -0
- package/dist/src/utils/tool-utils.js.map +1 -0
- package/dist/src/utils/tool-utils.test.d.ts +6 -0
- package/dist/src/utils/tool-utils.test.js +61 -0
- package/dist/src/utils/tool-utils.test.js.map +1 -0
- package/dist/src/utils/userAccountManager.d.ts +20 -0
- package/dist/src/utils/userAccountManager.js +114 -0
- package/dist/src/utils/userAccountManager.js.map +1 -0
- package/dist/src/utils/userAccountManager.test.d.ts +6 -0
- package/dist/src/utils/{user_account.test.js → userAccountManager.test.js} +33 -30
- package/dist/src/utils/userAccountManager.test.js.map +1 -0
- package/dist/src/utils/workspaceContext.js +13 -7
- package/dist/src/utils/workspaceContext.js.map +1 -1
- package/dist/src/utils/workspaceContext.test.js +41 -16
- package/dist/src/utils/workspaceContext.test.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +18 -9
- 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.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/executor.test.d.ts} +0 -0
- /package/dist/src/{utils/user_account.test.d.ts → agents/invocation.test.d.ts} +0 -0
- /package/dist/src/{utils/user_id.test.d.ts → agents/registry.test.d.ts} +0 -0
|
@@ -4,72 +4,502 @@
|
|
|
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 } from '@google/genai';
|
|
8
|
+
import { GeminiChat, InvalidStreamError, StreamEventType, } 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 } from '../config/models.js';
|
|
11
|
+
import { AuthType } from './contentGenerator.js';
|
|
12
|
+
import {} from '../utils/retry.js';
|
|
13
|
+
import { Kind } from '../tools/tools.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
68
|
const config = {};
|
|
21
69
|
beforeEach(() => {
|
|
22
70
|
vi.clearAllMocks();
|
|
71
|
+
vi.mocked(uiTelemetryService.setLastPromptTokenCount).mockClear();
|
|
72
|
+
mockContentGenerator = {
|
|
73
|
+
generateContent: vi.fn(),
|
|
74
|
+
generateContentStream: vi.fn(),
|
|
75
|
+
countTokens: vi.fn(),
|
|
76
|
+
embedContent: vi.fn(),
|
|
77
|
+
batchEmbedContents: vi.fn(),
|
|
78
|
+
};
|
|
79
|
+
mockHandleFallback.mockClear();
|
|
80
|
+
// Default mock implementation for tests that don't care about retry logic
|
|
81
|
+
mockRetryWithBackoff.mockImplementation(async (apiCall) => apiCall());
|
|
23
82
|
mockConfig = {
|
|
24
83
|
getSessionId: () => 'test-session-id',
|
|
25
84
|
getTelemetryLogPromptsEnabled: () => true,
|
|
26
85
|
getUsageStatisticsEnabled: () => true,
|
|
27
86
|
getDebugMode: () => false,
|
|
28
|
-
getContentGeneratorConfig: ()
|
|
29
|
-
authType: 'oauth-personal',
|
|
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),
|
|
37
105
|
};
|
|
38
106
|
// Disable 429 simulation for tests
|
|
39
107
|
setSimulate429(false);
|
|
40
108
|
// Reset history for each test by creating a new instance
|
|
41
|
-
chat = new GeminiChat(mockConfig,
|
|
109
|
+
chat = new GeminiChat(mockConfig, config, []);
|
|
42
110
|
});
|
|
43
111
|
afterEach(() => {
|
|
44
112
|
vi.restoreAllMocks();
|
|
45
113
|
vi.resetAllMocks();
|
|
46
114
|
});
|
|
47
|
-
describe('
|
|
48
|
-
it('should call
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
115
|
+
describe('sendMessageStream', () => {
|
|
116
|
+
it('should succeed if a tool call is followed by an empty part', async () => {
|
|
117
|
+
// 1. Mock a stream that contains a tool call, then an invalid (empty) part.
|
|
118
|
+
const streamWithToolCall = (async function* () {
|
|
119
|
+
yield {
|
|
120
|
+
candidates: [
|
|
121
|
+
{
|
|
122
|
+
content: {
|
|
123
|
+
role: 'model',
|
|
124
|
+
parts: [{ functionCall: { name: 'test_tool', args: {} } }],
|
|
125
|
+
},
|
|
55
126
|
},
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
127
|
+
],
|
|
128
|
+
};
|
|
129
|
+
// This second chunk is invalid according to isValidResponse
|
|
130
|
+
yield {
|
|
131
|
+
candidates: [
|
|
132
|
+
{
|
|
133
|
+
content: {
|
|
134
|
+
role: 'model',
|
|
135
|
+
parts: [{ text: '' }],
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
],
|
|
139
|
+
};
|
|
140
|
+
})();
|
|
141
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(streamWithToolCall);
|
|
142
|
+
// 2. Action & Assert: The stream processing should complete without throwing an error
|
|
143
|
+
// because the presence of a tool call makes the empty final chunk acceptable.
|
|
144
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-tool-call-empty-end');
|
|
145
|
+
await expect((async () => {
|
|
146
|
+
for await (const _ of stream) {
|
|
147
|
+
/* consume stream */
|
|
148
|
+
}
|
|
149
|
+
})()).resolves.not.toThrow();
|
|
150
|
+
// 3. Verify history was recorded correctly
|
|
151
|
+
const history = chat.getHistory();
|
|
152
|
+
expect(history.length).toBe(2); // user turn + model turn
|
|
153
|
+
const modelTurn = history[1];
|
|
154
|
+
expect(modelTurn?.parts?.length).toBe(1); // The empty part is discarded
|
|
155
|
+
expect(modelTurn?.parts[0].functionCall).toBeDefined();
|
|
156
|
+
});
|
|
157
|
+
it('should fail if the stream ends with an empty part and has no finishReason', async () => {
|
|
158
|
+
// 1. Mock a stream that ends with an invalid part and has no finish reason.
|
|
159
|
+
const streamWithNoFinish = (async function* () {
|
|
160
|
+
yield {
|
|
161
|
+
candidates: [
|
|
162
|
+
{
|
|
163
|
+
content: {
|
|
164
|
+
role: 'model',
|
|
165
|
+
parts: [{ text: 'Initial content...' }],
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
],
|
|
169
|
+
};
|
|
170
|
+
// This second chunk is invalid and has no finishReason, so it should fail.
|
|
171
|
+
yield {
|
|
172
|
+
candidates: [
|
|
173
|
+
{
|
|
174
|
+
content: {
|
|
175
|
+
role: 'model',
|
|
176
|
+
parts: [{ text: '' }],
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
],
|
|
180
|
+
};
|
|
181
|
+
})();
|
|
182
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(streamWithNoFinish);
|
|
183
|
+
// 2. Action & Assert: The stream should fail because there's no finish reason.
|
|
184
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-no-finish-empty-end');
|
|
185
|
+
await expect((async () => {
|
|
186
|
+
for await (const _ of stream) {
|
|
187
|
+
/* consume stream */
|
|
188
|
+
}
|
|
189
|
+
})()).rejects.toThrow(InvalidStreamError);
|
|
190
|
+
});
|
|
191
|
+
it('should succeed if the stream ends with an invalid part but has a finishReason and contained a valid part', async () => {
|
|
192
|
+
// 1. Mock a stream that sends a valid chunk, then an invalid one, but has a finish reason.
|
|
193
|
+
const streamWithInvalidEnd = (async function* () {
|
|
194
|
+
yield {
|
|
195
|
+
candidates: [
|
|
196
|
+
{
|
|
197
|
+
content: {
|
|
198
|
+
role: 'model',
|
|
199
|
+
parts: [{ text: 'Initial valid content...' }],
|
|
200
|
+
},
|
|
201
|
+
},
|
|
202
|
+
],
|
|
203
|
+
};
|
|
204
|
+
// This second chunk is invalid, but the response has a finishReason.
|
|
205
|
+
yield {
|
|
206
|
+
candidates: [
|
|
207
|
+
{
|
|
208
|
+
content: {
|
|
209
|
+
role: 'model',
|
|
210
|
+
parts: [{ text: '' }], // Invalid part
|
|
211
|
+
},
|
|
212
|
+
finishReason: 'STOP',
|
|
213
|
+
},
|
|
214
|
+
],
|
|
215
|
+
};
|
|
216
|
+
})();
|
|
217
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(streamWithInvalidEnd);
|
|
218
|
+
// 2. Action & Assert: The stream should complete without throwing an error.
|
|
219
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-valid-then-invalid-end');
|
|
220
|
+
await expect((async () => {
|
|
221
|
+
for await (const _ of stream) {
|
|
222
|
+
/* consume stream */
|
|
223
|
+
}
|
|
224
|
+
})()).resolves.not.toThrow();
|
|
225
|
+
// 3. Verify history was recorded correctly with only the valid part.
|
|
226
|
+
const history = chat.getHistory();
|
|
227
|
+
expect(history.length).toBe(2); // user turn + model turn
|
|
228
|
+
const modelTurn = history[1];
|
|
229
|
+
expect(modelTurn?.parts?.length).toBe(1);
|
|
230
|
+
expect(modelTurn?.parts[0].text).toBe('Initial valid content...');
|
|
231
|
+
});
|
|
232
|
+
it('should consolidate subsequent text chunks after receiving an empty text chunk', async () => {
|
|
233
|
+
// 1. Mock the API to return a stream where one chunk is just an empty text part.
|
|
234
|
+
const multiChunkStream = (async function* () {
|
|
235
|
+
yield {
|
|
236
|
+
candidates: [
|
|
237
|
+
{ content: { role: 'model', parts: [{ text: 'Hello' }] } },
|
|
238
|
+
],
|
|
239
|
+
};
|
|
240
|
+
// FIX: The original test used { text: '' }, which is invalid.
|
|
241
|
+
// A chunk can be empty but still valid. This chunk is now removed
|
|
242
|
+
// as the important part is consolidating what comes after.
|
|
243
|
+
yield {
|
|
244
|
+
candidates: [
|
|
245
|
+
{
|
|
246
|
+
content: { role: 'model', parts: [{ text: ' World!' }] },
|
|
247
|
+
finishReason: 'STOP',
|
|
248
|
+
},
|
|
249
|
+
],
|
|
250
|
+
};
|
|
251
|
+
})();
|
|
252
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(multiChunkStream);
|
|
253
|
+
// 2. Action: Send a message and consume the stream.
|
|
254
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-empty-chunk-consolidation');
|
|
255
|
+
for await (const _ of stream) {
|
|
256
|
+
// Consume the stream
|
|
257
|
+
}
|
|
258
|
+
// 3. Assert: Check that the final history was correctly consolidated.
|
|
259
|
+
const history = chat.getHistory();
|
|
260
|
+
expect(history.length).toBe(2);
|
|
261
|
+
const modelTurn = history[1];
|
|
262
|
+
expect(modelTurn?.parts?.length).toBe(1);
|
|
263
|
+
expect(modelTurn?.parts[0].text).toBe('Hello World!');
|
|
264
|
+
});
|
|
265
|
+
it('should consolidate adjacent text parts that arrive in separate stream chunks', async () => {
|
|
266
|
+
// 1. Mock the API to return a stream of multiple, adjacent text chunks.
|
|
267
|
+
const multiChunkStream = (async function* () {
|
|
268
|
+
yield {
|
|
269
|
+
candidates: [
|
|
270
|
+
{ content: { role: 'model', parts: [{ text: 'This is the ' }] } },
|
|
271
|
+
],
|
|
272
|
+
};
|
|
273
|
+
yield {
|
|
274
|
+
candidates: [
|
|
275
|
+
{ content: { role: 'model', parts: [{ text: 'first part.' }] } },
|
|
276
|
+
],
|
|
277
|
+
};
|
|
278
|
+
// This function call should break the consolidation.
|
|
279
|
+
yield {
|
|
280
|
+
candidates: [
|
|
281
|
+
{
|
|
282
|
+
content: {
|
|
283
|
+
role: 'model',
|
|
284
|
+
parts: [{ functionCall: { name: 'do_stuff', args: {} } }],
|
|
285
|
+
},
|
|
286
|
+
},
|
|
287
|
+
],
|
|
288
|
+
};
|
|
289
|
+
yield {
|
|
290
|
+
candidates: [
|
|
291
|
+
{
|
|
292
|
+
content: {
|
|
293
|
+
role: 'model',
|
|
294
|
+
parts: [{ text: 'This is the second part.' }],
|
|
295
|
+
},
|
|
296
|
+
},
|
|
297
|
+
],
|
|
298
|
+
};
|
|
299
|
+
})();
|
|
300
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(multiChunkStream);
|
|
301
|
+
// 2. Action: Send a message and consume the stream.
|
|
302
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-multi-chunk');
|
|
303
|
+
for await (const _ of stream) {
|
|
304
|
+
// Consume the stream to trigger history recording.
|
|
305
|
+
}
|
|
306
|
+
// 3. Assert: Check that the final history was correctly consolidated.
|
|
307
|
+
const history = chat.getHistory();
|
|
308
|
+
// The history should contain the user's turn and ONE consolidated model turn.
|
|
309
|
+
expect(history.length).toBe(2);
|
|
310
|
+
const modelTurn = history[1];
|
|
311
|
+
expect(modelTurn.role).toBe('model');
|
|
312
|
+
// The model turn should have 3 distinct parts: the merged text, the function call, and the final text.
|
|
313
|
+
expect(modelTurn?.parts?.length).toBe(3);
|
|
314
|
+
expect(modelTurn?.parts[0].text).toBe('This is the first part.');
|
|
315
|
+
expect(modelTurn.parts[1].functionCall).toBeDefined();
|
|
316
|
+
expect(modelTurn.parts[2].text).toBe('This is the second part.');
|
|
317
|
+
});
|
|
318
|
+
it('should preserve text parts that stream in the same chunk as a thought', async () => {
|
|
319
|
+
// 1. Mock the API to return a single chunk containing both a thought and visible text.
|
|
320
|
+
const mixedContentStream = (async function* () {
|
|
321
|
+
yield {
|
|
322
|
+
candidates: [
|
|
323
|
+
{
|
|
324
|
+
content: {
|
|
325
|
+
role: 'model',
|
|
326
|
+
parts: [
|
|
327
|
+
{ thought: 'This is a thought.' },
|
|
328
|
+
{ text: 'This is the visible text that should not be lost.' },
|
|
329
|
+
],
|
|
330
|
+
},
|
|
331
|
+
finishReason: 'STOP',
|
|
332
|
+
},
|
|
333
|
+
],
|
|
334
|
+
};
|
|
335
|
+
})();
|
|
336
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(mixedContentStream);
|
|
337
|
+
// 2. Action: Send a message and fully consume the stream to trigger history recording.
|
|
338
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-mixed-chunk');
|
|
339
|
+
for await (const _ of stream) {
|
|
340
|
+
// This loop consumes the stream.
|
|
341
|
+
}
|
|
342
|
+
// 3. Assert: Check the final state of the history.
|
|
343
|
+
const history = chat.getHistory();
|
|
344
|
+
// The history should contain two turns: the user's message and the model's response.
|
|
345
|
+
expect(history.length).toBe(2);
|
|
346
|
+
const modelTurn = history[1];
|
|
347
|
+
expect(modelTurn.role).toBe('model');
|
|
348
|
+
// CRUCIAL ASSERTION:
|
|
349
|
+
// The buggy code would fail here, resulting in parts.length being 0.
|
|
350
|
+
// The corrected code will pass, preserving the single visible text part.
|
|
351
|
+
expect(modelTurn?.parts?.length).toBe(1);
|
|
352
|
+
expect(modelTurn?.parts[0].text).toBe('This is the visible text that should not be lost.');
|
|
353
|
+
});
|
|
354
|
+
it('should throw an error when a tool call is followed by an empty stream response', async () => {
|
|
355
|
+
// 1. Setup: A history where the model has just made a function call.
|
|
356
|
+
const initialHistory = [
|
|
357
|
+
{
|
|
358
|
+
role: 'user',
|
|
359
|
+
parts: [{ text: 'Find a good Italian restaurant for me.' }],
|
|
360
|
+
},
|
|
361
|
+
{
|
|
362
|
+
role: 'model',
|
|
363
|
+
parts: [
|
|
364
|
+
{
|
|
365
|
+
functionCall: {
|
|
366
|
+
name: 'find_restaurant',
|
|
367
|
+
args: { cuisine: 'Italian' },
|
|
368
|
+
},
|
|
369
|
+
},
|
|
370
|
+
],
|
|
371
|
+
},
|
|
372
|
+
];
|
|
373
|
+
chat.setHistory(initialHistory);
|
|
374
|
+
// 2. Mock the API to return an empty/thought-only stream.
|
|
375
|
+
const emptyStreamResponse = (async function* () {
|
|
376
|
+
yield {
|
|
377
|
+
candidates: [
|
|
378
|
+
{
|
|
379
|
+
content: { role: 'model', parts: [{ thought: true }] },
|
|
380
|
+
finishReason: 'STOP',
|
|
381
|
+
},
|
|
382
|
+
],
|
|
383
|
+
};
|
|
384
|
+
})();
|
|
385
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(emptyStreamResponse);
|
|
386
|
+
// 3. Action: Send the function response back to the model and consume the stream.
|
|
387
|
+
const stream = await chat.sendMessageStream('test-model', {
|
|
388
|
+
message: {
|
|
389
|
+
functionResponse: {
|
|
390
|
+
name: 'find_restaurant',
|
|
391
|
+
response: { name: 'Vesuvio' },
|
|
59
392
|
},
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
393
|
+
},
|
|
394
|
+
}, 'prompt-id-stream-1');
|
|
395
|
+
// 4. Assert: The stream processing should throw an InvalidStreamError.
|
|
396
|
+
await expect((async () => {
|
|
397
|
+
for await (const _ of stream) {
|
|
398
|
+
// This loop consumes the stream to trigger the internal logic.
|
|
399
|
+
}
|
|
400
|
+
})()).rejects.toThrow(InvalidStreamError);
|
|
401
|
+
});
|
|
402
|
+
it('should succeed when there is a tool call without finish reason', async () => {
|
|
403
|
+
// Setup: Stream with tool call but no finish reason
|
|
404
|
+
const streamWithToolCall = (async function* () {
|
|
405
|
+
yield {
|
|
406
|
+
candidates: [
|
|
407
|
+
{
|
|
408
|
+
content: {
|
|
409
|
+
role: 'model',
|
|
410
|
+
parts: [
|
|
411
|
+
{
|
|
412
|
+
functionCall: {
|
|
413
|
+
name: 'test_function',
|
|
414
|
+
args: { param: 'value' },
|
|
415
|
+
},
|
|
416
|
+
},
|
|
417
|
+
],
|
|
418
|
+
},
|
|
419
|
+
// No finishReason
|
|
420
|
+
},
|
|
421
|
+
],
|
|
422
|
+
};
|
|
423
|
+
})();
|
|
424
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(streamWithToolCall);
|
|
425
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-1');
|
|
426
|
+
// Should not throw an error
|
|
427
|
+
await expect((async () => {
|
|
428
|
+
for await (const _ of stream) {
|
|
429
|
+
// consume stream
|
|
430
|
+
}
|
|
431
|
+
})()).resolves.not.toThrow();
|
|
432
|
+
});
|
|
433
|
+
it('should throw InvalidStreamError when no tool call and no finish reason', async () => {
|
|
434
|
+
// Setup: Stream with text but no finish reason and no tool call
|
|
435
|
+
const streamWithoutFinishReason = (async function* () {
|
|
436
|
+
yield {
|
|
437
|
+
candidates: [
|
|
438
|
+
{
|
|
439
|
+
content: {
|
|
440
|
+
role: 'model',
|
|
441
|
+
parts: [{ text: 'some response' }],
|
|
442
|
+
},
|
|
443
|
+
// No finishReason
|
|
444
|
+
},
|
|
445
|
+
],
|
|
446
|
+
};
|
|
447
|
+
})();
|
|
448
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(streamWithoutFinishReason);
|
|
449
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-1');
|
|
450
|
+
await expect((async () => {
|
|
451
|
+
for await (const _ of stream) {
|
|
452
|
+
// consume stream
|
|
453
|
+
}
|
|
454
|
+
})()).rejects.toThrow(InvalidStreamError);
|
|
455
|
+
});
|
|
456
|
+
it('should throw InvalidStreamError when no tool call and empty response text', async () => {
|
|
457
|
+
// Setup: Stream with finish reason but empty response (only thoughts)
|
|
458
|
+
const streamWithEmptyResponse = (async function* () {
|
|
459
|
+
yield {
|
|
460
|
+
candidates: [
|
|
461
|
+
{
|
|
462
|
+
content: {
|
|
463
|
+
role: 'model',
|
|
464
|
+
parts: [{ thought: 'thinking...' }],
|
|
465
|
+
},
|
|
466
|
+
finishReason: 'STOP',
|
|
467
|
+
},
|
|
468
|
+
],
|
|
469
|
+
};
|
|
470
|
+
})();
|
|
471
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(streamWithEmptyResponse);
|
|
472
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-1');
|
|
473
|
+
await expect((async () => {
|
|
474
|
+
for await (const _ of stream) {
|
|
475
|
+
// consume stream
|
|
476
|
+
}
|
|
477
|
+
})()).rejects.toThrow(InvalidStreamError);
|
|
478
|
+
});
|
|
479
|
+
it('should succeed when there is finish reason and response text', async () => {
|
|
480
|
+
// Setup: Stream with both finish reason and text content
|
|
481
|
+
const validStream = (async function* () {
|
|
482
|
+
yield {
|
|
483
|
+
candidates: [
|
|
484
|
+
{
|
|
485
|
+
content: {
|
|
486
|
+
role: 'model',
|
|
487
|
+
parts: [{ text: 'valid response' }],
|
|
488
|
+
},
|
|
489
|
+
finishReason: 'STOP',
|
|
490
|
+
},
|
|
491
|
+
],
|
|
492
|
+
};
|
|
493
|
+
})();
|
|
494
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(validStream);
|
|
495
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-1');
|
|
496
|
+
// Should not throw an error
|
|
497
|
+
await expect((async () => {
|
|
498
|
+
for await (const _ of stream) {
|
|
499
|
+
// consume stream
|
|
500
|
+
}
|
|
501
|
+
})()).resolves.not.toThrow();
|
|
70
502
|
});
|
|
71
|
-
});
|
|
72
|
-
describe('sendMessageStream', () => {
|
|
73
503
|
it('should call generateContentStream with the correct parameters', async () => {
|
|
74
504
|
const response = (async function* () {
|
|
75
505
|
yield {
|
|
@@ -85,339 +515,843 @@ describe('GeminiChat', () => {
|
|
|
85
515
|
},
|
|
86
516
|
],
|
|
87
517
|
text: () => 'response',
|
|
518
|
+
usageMetadata: {
|
|
519
|
+
promptTokenCount: 42,
|
|
520
|
+
candidatesTokenCount: 15,
|
|
521
|
+
totalTokenCount: 57,
|
|
522
|
+
},
|
|
88
523
|
};
|
|
89
524
|
})();
|
|
90
|
-
vi.mocked(
|
|
91
|
-
await chat.sendMessageStream({ message: 'hello' }, 'prompt-id-1');
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
525
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(response);
|
|
526
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'hello' }, 'prompt-id-1');
|
|
527
|
+
for await (const _ of stream) {
|
|
528
|
+
// consume stream
|
|
529
|
+
}
|
|
530
|
+
expect(mockContentGenerator.generateContentStream).toHaveBeenCalledWith({
|
|
531
|
+
model: 'test-model',
|
|
532
|
+
contents: [
|
|
533
|
+
{
|
|
534
|
+
role: 'user',
|
|
535
|
+
parts: [{ text: 'hello' }],
|
|
536
|
+
},
|
|
537
|
+
],
|
|
95
538
|
config: {},
|
|
96
539
|
}, 'prompt-id-1');
|
|
540
|
+
// Verify that token counting is called when usageMetadata is present
|
|
541
|
+
expect(uiTelemetryService.setLastPromptTokenCount).toHaveBeenCalledWith(42);
|
|
542
|
+
expect(uiTelemetryService.setLastPromptTokenCount).toHaveBeenCalledTimes(1);
|
|
97
543
|
});
|
|
98
544
|
});
|
|
99
|
-
describe('
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
{ role: 'model', parts: [{ text: 'Model output' }] },
|
|
107
|
-
];
|
|
108
|
-
// @ts-expect-error Accessing private method for testing purposes
|
|
109
|
-
chat.recordHistory(userInput, modelOutput);
|
|
545
|
+
describe('addHistory', () => {
|
|
546
|
+
it('should add a new content item to the history', () => {
|
|
547
|
+
const newContent = {
|
|
548
|
+
role: 'user',
|
|
549
|
+
parts: [{ text: 'A new message' }],
|
|
550
|
+
};
|
|
551
|
+
chat.addHistory(newContent);
|
|
110
552
|
const history = chat.getHistory();
|
|
111
|
-
expect(history).
|
|
553
|
+
expect(history.length).toBe(1);
|
|
554
|
+
expect(history[0]).toEqual(newContent);
|
|
112
555
|
});
|
|
113
|
-
it('should
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
556
|
+
it('should add multiple items correctly', () => {
|
|
557
|
+
const content1 = {
|
|
558
|
+
role: 'user',
|
|
559
|
+
parts: [{ text: 'Message 1' }],
|
|
560
|
+
};
|
|
561
|
+
const content2 = {
|
|
562
|
+
role: 'model',
|
|
563
|
+
parts: [{ text: 'Message 2' }],
|
|
564
|
+
};
|
|
565
|
+
chat.addHistory(content1);
|
|
566
|
+
chat.addHistory(content2);
|
|
120
567
|
const history = chat.getHistory();
|
|
121
568
|
expect(history.length).toBe(2);
|
|
122
|
-
expect(history[0]).toEqual(
|
|
123
|
-
expect(history[1]
|
|
124
|
-
expect(history[1].parts).toEqual([{ text: 'Model part 1Model part 2' }]);
|
|
569
|
+
expect(history[0]).toEqual(content1);
|
|
570
|
+
expect(history[1]).toEqual(content2);
|
|
125
571
|
});
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
//
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
572
|
+
});
|
|
573
|
+
describe('sendMessageStream with retries', () => {
|
|
574
|
+
it('should yield a RETRY event when an invalid stream is encountered', async () => {
|
|
575
|
+
// ARRANGE: Mock the stream to fail once, then succeed.
|
|
576
|
+
vi.mocked(mockContentGenerator.generateContentStream)
|
|
577
|
+
.mockImplementationOnce(async () =>
|
|
578
|
+
// First attempt: An invalid stream with an empty text part.
|
|
579
|
+
(async function* () {
|
|
580
|
+
yield {
|
|
581
|
+
candidates: [{ content: { parts: [{ text: '' }] } }],
|
|
582
|
+
};
|
|
583
|
+
})())
|
|
584
|
+
.mockImplementationOnce(async () =>
|
|
585
|
+
// Second attempt (the retry): A minimal valid stream.
|
|
586
|
+
(async function* () {
|
|
587
|
+
yield {
|
|
588
|
+
candidates: [
|
|
589
|
+
{
|
|
590
|
+
content: { parts: [{ text: 'Success' }] },
|
|
591
|
+
finishReason: 'STOP',
|
|
592
|
+
},
|
|
593
|
+
],
|
|
594
|
+
};
|
|
595
|
+
})());
|
|
596
|
+
// ACT: Send a message and collect all events from the stream.
|
|
597
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-yield-retry');
|
|
598
|
+
const events = [];
|
|
599
|
+
for await (const event of stream) {
|
|
600
|
+
events.push(event);
|
|
601
|
+
}
|
|
602
|
+
// ASSERT: Check that a RETRY event was present in the stream's output.
|
|
603
|
+
const retryEvent = events.find((e) => e.type === StreamEventType.RETRY);
|
|
604
|
+
expect(retryEvent).toBeDefined();
|
|
605
|
+
expect(retryEvent?.type).toBe(StreamEventType.RETRY);
|
|
140
606
|
});
|
|
141
|
-
it('should
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
607
|
+
it('should retry on invalid content, succeed, and report metrics', async () => {
|
|
608
|
+
// Use mockImplementationOnce to provide a fresh, promise-wrapped generator for each attempt.
|
|
609
|
+
vi.mocked(mockContentGenerator.generateContentStream)
|
|
610
|
+
.mockImplementationOnce(async () =>
|
|
611
|
+
// First call returns an invalid stream
|
|
612
|
+
(async function* () {
|
|
613
|
+
yield {
|
|
614
|
+
candidates: [{ content: { parts: [{ text: '' }] } }], // Invalid empty text part
|
|
615
|
+
};
|
|
616
|
+
})())
|
|
617
|
+
.mockImplementationOnce(async () =>
|
|
618
|
+
// Second call returns a valid stream
|
|
619
|
+
(async function* () {
|
|
620
|
+
yield {
|
|
621
|
+
candidates: [
|
|
622
|
+
{
|
|
623
|
+
content: { parts: [{ text: 'Successful response' }] },
|
|
624
|
+
finishReason: 'STOP',
|
|
625
|
+
},
|
|
626
|
+
],
|
|
627
|
+
};
|
|
628
|
+
})());
|
|
629
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-retry-success');
|
|
630
|
+
const chunks = [];
|
|
631
|
+
for await (const chunk of stream) {
|
|
632
|
+
chunks.push(chunk);
|
|
633
|
+
}
|
|
634
|
+
// Assertions
|
|
635
|
+
expect(mockLogContentRetry).toHaveBeenCalledTimes(1);
|
|
636
|
+
expect(mockLogContentRetryFailure).not.toHaveBeenCalled();
|
|
637
|
+
expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
|
|
638
|
+
// Check for a retry event
|
|
639
|
+
expect(chunks.some((c) => c.type === StreamEventType.RETRY)).toBe(true);
|
|
640
|
+
// Check for the successful content chunk
|
|
641
|
+
expect(chunks.some((c) => c.type === StreamEventType.CHUNK &&
|
|
642
|
+
c.value.candidates?.[0]?.content?.parts?.[0]?.text ===
|
|
643
|
+
'Successful response')).toBe(true);
|
|
644
|
+
// Check that history was recorded correctly once, with no duplicates.
|
|
149
645
|
const history = chat.getHistory();
|
|
150
646
|
expect(history.length).toBe(2);
|
|
151
|
-
expect(history[
|
|
647
|
+
expect(history[0]).toEqual({
|
|
648
|
+
role: 'user',
|
|
649
|
+
parts: [{ text: 'test' }],
|
|
650
|
+
});
|
|
651
|
+
expect(history[1]).toEqual({
|
|
652
|
+
role: 'model',
|
|
653
|
+
parts: [{ text: 'Successful response' }],
|
|
654
|
+
});
|
|
655
|
+
// Verify that token counting is not called when usageMetadata is missing
|
|
656
|
+
expect(uiTelemetryService.setLastPromptTokenCount).not.toHaveBeenCalled();
|
|
152
657
|
});
|
|
153
|
-
it('should
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
658
|
+
it('should fail after all retries on persistent invalid content and report metrics', async () => {
|
|
659
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockImplementation(async () => (async function* () {
|
|
660
|
+
yield {
|
|
661
|
+
candidates: [
|
|
662
|
+
{
|
|
663
|
+
content: {
|
|
664
|
+
parts: [{ text: '' }],
|
|
665
|
+
role: 'model',
|
|
666
|
+
},
|
|
667
|
+
},
|
|
668
|
+
],
|
|
669
|
+
};
|
|
670
|
+
})());
|
|
671
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-retry-fail');
|
|
672
|
+
await expect(async () => {
|
|
673
|
+
for await (const _ of stream) {
|
|
674
|
+
// Must loop to trigger the internal logic that throws.
|
|
675
|
+
}
|
|
676
|
+
}).rejects.toThrow(InvalidStreamError);
|
|
677
|
+
// Should be called 2 times (initial + 1 retry)
|
|
678
|
+
expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
|
|
679
|
+
expect(mockLogContentRetry).toHaveBeenCalledTimes(1);
|
|
680
|
+
expect(mockLogContentRetryFailure).toHaveBeenCalledTimes(1);
|
|
681
|
+
// History should still contain the user message.
|
|
161
682
|
const history = chat.getHistory();
|
|
162
|
-
expect(history.length).toBe(
|
|
163
|
-
expect(history[
|
|
164
|
-
expect(history[3].parts).toEqual([{ text: 'M2' }]);
|
|
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 = {
|
|
683
|
+
expect(history.length).toBe(1);
|
|
684
|
+
expect(history[0]).toEqual({
|
|
181
685
|
role: 'user',
|
|
182
|
-
parts: [{ text: '
|
|
686
|
+
parts: [{ text: 'test' }],
|
|
687
|
+
});
|
|
688
|
+
});
|
|
689
|
+
describe('API error retry behavior', () => {
|
|
690
|
+
beforeEach(() => {
|
|
691
|
+
// Use a more direct mock for retry testing
|
|
692
|
+
mockRetryWithBackoff.mockImplementation(async (apiCall, options) => {
|
|
693
|
+
try {
|
|
694
|
+
return await apiCall();
|
|
695
|
+
}
|
|
696
|
+
catch (error) {
|
|
697
|
+
if (options?.shouldRetryOnError &&
|
|
698
|
+
options.shouldRetryOnError(error)) {
|
|
699
|
+
// Try again
|
|
700
|
+
return await apiCall();
|
|
701
|
+
}
|
|
702
|
+
throw error;
|
|
703
|
+
}
|
|
704
|
+
});
|
|
705
|
+
});
|
|
706
|
+
it('should not retry on 400 Bad Request errors', async () => {
|
|
707
|
+
const error400 = new ApiError({ message: 'Bad Request', status: 400 });
|
|
708
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockRejectedValue(error400);
|
|
709
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-400');
|
|
710
|
+
await expect((async () => {
|
|
711
|
+
for await (const _ of stream) {
|
|
712
|
+
/* consume stream */
|
|
713
|
+
}
|
|
714
|
+
})()).rejects.toThrow(error400);
|
|
715
|
+
// Should only be called once (no retry)
|
|
716
|
+
expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(1);
|
|
717
|
+
});
|
|
718
|
+
it('should retry on 429 Rate Limit errors', async () => {
|
|
719
|
+
const error429 = new ApiError({ message: 'Rate Limited', status: 429 });
|
|
720
|
+
vi.mocked(mockContentGenerator.generateContentStream)
|
|
721
|
+
.mockRejectedValueOnce(error429)
|
|
722
|
+
.mockResolvedValueOnce((async function* () {
|
|
723
|
+
yield {
|
|
724
|
+
candidates: [
|
|
725
|
+
{
|
|
726
|
+
content: { parts: [{ text: 'Success after retry' }] },
|
|
727
|
+
finishReason: 'STOP',
|
|
728
|
+
},
|
|
729
|
+
],
|
|
730
|
+
};
|
|
731
|
+
})());
|
|
732
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-429-retry');
|
|
733
|
+
const events = [];
|
|
734
|
+
for await (const event of stream) {
|
|
735
|
+
events.push(event);
|
|
736
|
+
}
|
|
737
|
+
// Should be called twice (initial + retry)
|
|
738
|
+
expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
|
|
739
|
+
// Should have successful content
|
|
740
|
+
expect(events.some((e) => e.type === StreamEventType.CHUNK &&
|
|
741
|
+
e.value.candidates?.[0]?.content?.parts?.[0]?.text ===
|
|
742
|
+
'Success after retry')).toBe(true);
|
|
743
|
+
});
|
|
744
|
+
it('should not retry on schema depth errors', async () => {
|
|
745
|
+
const schemaError = new ApiError({
|
|
746
|
+
message: 'Request failed: maximum schema depth exceeded',
|
|
747
|
+
status: 500,
|
|
748
|
+
});
|
|
749
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockRejectedValue(schemaError);
|
|
750
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-schema');
|
|
751
|
+
await expect((async () => {
|
|
752
|
+
for await (const _ of stream) {
|
|
753
|
+
/* consume stream */
|
|
754
|
+
}
|
|
755
|
+
})()).rejects.toThrow(schemaError);
|
|
756
|
+
// Should only be called once (no retry)
|
|
757
|
+
expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(1);
|
|
758
|
+
});
|
|
759
|
+
it('should retry on 5xx server errors', async () => {
|
|
760
|
+
const error500 = new ApiError({
|
|
761
|
+
message: 'Internal Server Error 500',
|
|
762
|
+
status: 500,
|
|
763
|
+
});
|
|
764
|
+
vi.mocked(mockContentGenerator.generateContentStream)
|
|
765
|
+
.mockRejectedValueOnce(error500)
|
|
766
|
+
.mockResolvedValueOnce((async function* () {
|
|
767
|
+
yield {
|
|
768
|
+
candidates: [
|
|
769
|
+
{
|
|
770
|
+
content: { parts: [{ text: 'Recovered from 500' }] },
|
|
771
|
+
finishReason: 'STOP',
|
|
772
|
+
},
|
|
773
|
+
],
|
|
774
|
+
};
|
|
775
|
+
})());
|
|
776
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-500-retry');
|
|
777
|
+
const events = [];
|
|
778
|
+
for await (const event of stream) {
|
|
779
|
+
events.push(event);
|
|
780
|
+
}
|
|
781
|
+
// Should be called twice (initial + retry)
|
|
782
|
+
expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
|
|
783
|
+
});
|
|
784
|
+
afterEach(() => {
|
|
785
|
+
// Reset to default behavior
|
|
786
|
+
mockRetryWithBackoff.mockImplementation(async (apiCall) => apiCall());
|
|
787
|
+
});
|
|
788
|
+
});
|
|
789
|
+
});
|
|
790
|
+
it('should correctly retry and append to an existing history mid-conversation', async () => {
|
|
791
|
+
// 1. Setup
|
|
792
|
+
const initialHistory = [
|
|
793
|
+
{ role: 'user', parts: [{ text: 'First question' }] },
|
|
794
|
+
{ role: 'model', parts: [{ text: 'First answer' }] },
|
|
795
|
+
];
|
|
796
|
+
chat.setHistory(initialHistory);
|
|
797
|
+
// 2. Mock the API to fail once with an empty stream, then succeed.
|
|
798
|
+
vi.mocked(mockContentGenerator.generateContentStream)
|
|
799
|
+
.mockImplementationOnce(async () => (async function* () {
|
|
800
|
+
yield {
|
|
801
|
+
candidates: [{ content: { parts: [{ text: '' }] } }],
|
|
183
802
|
};
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
803
|
+
})())
|
|
804
|
+
.mockImplementationOnce(async () =>
|
|
805
|
+
// Second attempt succeeds
|
|
806
|
+
(async function* () {
|
|
807
|
+
yield {
|
|
808
|
+
candidates: [
|
|
809
|
+
{
|
|
810
|
+
content: { parts: [{ text: 'Second answer' }] },
|
|
811
|
+
finishReason: 'STOP',
|
|
812
|
+
},
|
|
813
|
+
],
|
|
192
814
|
};
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
//
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
815
|
+
})());
|
|
816
|
+
// 3. Send a new message
|
|
817
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'Second question' }, 'prompt-id-retry-existing');
|
|
818
|
+
for await (const _ of stream) {
|
|
819
|
+
// consume stream
|
|
820
|
+
}
|
|
821
|
+
// 4. Assert the final history and metrics
|
|
822
|
+
const history = chat.getHistory();
|
|
823
|
+
expect(history.length).toBe(4);
|
|
824
|
+
// Assert that the correct metrics were reported for one empty-stream retry
|
|
825
|
+
expect(mockLogContentRetry).toHaveBeenCalledTimes(1);
|
|
826
|
+
// Explicitly verify the structure of each part to satisfy TypeScript
|
|
827
|
+
const turn1 = history[0];
|
|
828
|
+
if (!turn1?.parts?.[0] || !('text' in turn1.parts[0])) {
|
|
829
|
+
throw new Error('Test setup error: First turn is not a valid text part.');
|
|
830
|
+
}
|
|
831
|
+
expect(turn1.parts[0].text).toBe('First question');
|
|
832
|
+
const turn2 = history[1];
|
|
833
|
+
if (!turn2?.parts?.[0] || !('text' in turn2.parts[0])) {
|
|
834
|
+
throw new Error('Test setup error: Second turn is not a valid text part.');
|
|
835
|
+
}
|
|
836
|
+
expect(turn2.parts[0].text).toBe('First answer');
|
|
837
|
+
const turn3 = history[2];
|
|
838
|
+
if (!turn3?.parts?.[0] || !('text' in turn3.parts[0])) {
|
|
839
|
+
throw new Error('Test setup error: Third turn is not a valid text part.');
|
|
840
|
+
}
|
|
841
|
+
expect(turn3.parts[0].text).toBe('Second question');
|
|
842
|
+
const turn4 = history[3];
|
|
843
|
+
if (!turn4?.parts?.[0] || !('text' in turn4.parts[0])) {
|
|
844
|
+
throw new Error('Test setup error: Fourth turn is not a valid text part.');
|
|
845
|
+
}
|
|
846
|
+
expect(turn4.parts[0].text).toBe('Second answer');
|
|
847
|
+
});
|
|
848
|
+
it('should retry if the model returns a completely empty stream (no chunks)', async () => {
|
|
849
|
+
// 1. Mock the API to return an empty stream first, then a valid one.
|
|
850
|
+
vi.mocked(mockContentGenerator.generateContentStream)
|
|
851
|
+
.mockImplementationOnce(
|
|
852
|
+
// First call resolves to an async generator that yields nothing.
|
|
853
|
+
async () => (async function* () { })())
|
|
854
|
+
.mockImplementationOnce(
|
|
855
|
+
// Second call returns a valid stream.
|
|
856
|
+
async () => (async function* () {
|
|
857
|
+
yield {
|
|
858
|
+
candidates: [
|
|
859
|
+
{
|
|
860
|
+
content: {
|
|
861
|
+
parts: [{ text: 'Successful response after empty' }],
|
|
862
|
+
},
|
|
863
|
+
finishReason: 'STOP',
|
|
864
|
+
},
|
|
865
|
+
],
|
|
866
|
+
};
|
|
867
|
+
})());
|
|
868
|
+
// 2. Call the method and consume the stream.
|
|
869
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test empty stream' }, 'prompt-id-empty-stream');
|
|
870
|
+
const chunks = [];
|
|
871
|
+
for await (const chunk of stream) {
|
|
872
|
+
chunks.push(chunk);
|
|
873
|
+
}
|
|
874
|
+
// 3. Assert the results.
|
|
875
|
+
expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
|
|
876
|
+
expect(chunks.some((c) => c.type === StreamEventType.CHUNK &&
|
|
877
|
+
c.value.candidates?.[0]?.content?.parts?.[0]?.text ===
|
|
878
|
+
'Successful response after empty')).toBe(true);
|
|
879
|
+
const history = chat.getHistory();
|
|
880
|
+
expect(history.length).toBe(2);
|
|
881
|
+
// Explicitly verify the structure of each part to satisfy TypeScript
|
|
882
|
+
const turn1 = history[0];
|
|
883
|
+
if (!turn1?.parts?.[0] || !('text' in turn1.parts[0])) {
|
|
884
|
+
throw new Error('Test setup error: First turn is not a valid text part.');
|
|
885
|
+
}
|
|
886
|
+
expect(turn1.parts[0].text).toBe('test empty stream');
|
|
887
|
+
const turn2 = history[1];
|
|
888
|
+
if (!turn2?.parts?.[0] || !('text' in turn2.parts[0])) {
|
|
889
|
+
throw new Error('Test setup error: Second turn is not a valid text part.');
|
|
890
|
+
}
|
|
891
|
+
expect(turn2.parts[0].text).toBe('Successful response after empty');
|
|
892
|
+
});
|
|
893
|
+
it('should queue a subsequent sendMessageStream call until the first stream is fully consumed', async () => {
|
|
894
|
+
// 1. Create a promise to manually control the stream's lifecycle
|
|
895
|
+
let continueFirstStream;
|
|
896
|
+
const firstStreamContinuePromise = new Promise((resolve) => {
|
|
897
|
+
continueFirstStream = resolve;
|
|
208
898
|
});
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
899
|
+
// 2. Mock the API to return controllable async generators
|
|
900
|
+
const firstStreamGenerator = (async function* () {
|
|
901
|
+
yield {
|
|
902
|
+
candidates: [
|
|
903
|
+
{ content: { parts: [{ text: 'first response part 1' }] } },
|
|
904
|
+
],
|
|
214
905
|
};
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
906
|
+
await firstStreamContinuePromise; // Pause the stream
|
|
907
|
+
yield {
|
|
908
|
+
candidates: [
|
|
909
|
+
{
|
|
910
|
+
content: { parts: [{ text: ' part 2' }] },
|
|
911
|
+
finishReason: 'STOP',
|
|
912
|
+
},
|
|
913
|
+
],
|
|
218
914
|
};
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
915
|
+
})();
|
|
916
|
+
const secondStreamGenerator = (async function* () {
|
|
917
|
+
yield {
|
|
918
|
+
candidates: [
|
|
919
|
+
{
|
|
920
|
+
content: { parts: [{ text: 'second response' }] },
|
|
921
|
+
finishReason: 'STOP',
|
|
922
|
+
},
|
|
923
|
+
],
|
|
227
924
|
};
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
925
|
+
})();
|
|
926
|
+
vi.mocked(mockContentGenerator.generateContentStream)
|
|
927
|
+
.mockResolvedValueOnce(firstStreamGenerator)
|
|
928
|
+
.mockResolvedValueOnce(secondStreamGenerator);
|
|
929
|
+
// 3. Start the first stream and consume only the first chunk to pause it
|
|
930
|
+
const firstStream = await chat.sendMessageStream('test-model', { message: 'first' }, 'prompt-1');
|
|
931
|
+
const firstStreamIterator = firstStream[Symbol.asyncIterator]();
|
|
932
|
+
await firstStreamIterator.next();
|
|
933
|
+
// 4. While the first stream is paused, start the second call. It will block.
|
|
934
|
+
const secondStreamPromise = chat.sendMessageStream('test-model', { message: 'second' }, 'prompt-2');
|
|
935
|
+
// 5. Assert that only one API call has been made so far.
|
|
936
|
+
expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(1);
|
|
937
|
+
// 6. Unblock and fully consume the first stream to completion.
|
|
938
|
+
continueFirstStream();
|
|
939
|
+
await firstStreamIterator.next(); // Consume the rest of the stream
|
|
940
|
+
await firstStreamIterator.next(); // Finish the iterator
|
|
941
|
+
// 7. Now that the first stream is done, await the second promise to get its generator.
|
|
942
|
+
const secondStream = await secondStreamPromise;
|
|
943
|
+
// 8. Start consuming the second stream, which triggers its internal API call.
|
|
944
|
+
const secondStreamIterator = secondStream[Symbol.asyncIterator]();
|
|
945
|
+
await secondStreamIterator.next();
|
|
946
|
+
// 9. The second API call should now have been made.
|
|
947
|
+
expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
|
|
948
|
+
// 10. FIX: Fully consume the second stream to ensure recordHistory is called.
|
|
949
|
+
await secondStreamIterator.next(); // This finishes the iterator.
|
|
950
|
+
// 11. Final check on history.
|
|
951
|
+
const history = chat.getHistory();
|
|
952
|
+
expect(history.length).toBe(4);
|
|
953
|
+
const turn4 = history[3];
|
|
954
|
+
if (!turn4?.parts?.[0] || !('text' in turn4.parts[0])) {
|
|
955
|
+
throw new Error('Test setup error: Fourth turn is not a valid text part.');
|
|
956
|
+
}
|
|
957
|
+
expect(turn4.parts[0].text).toBe('second response');
|
|
958
|
+
});
|
|
959
|
+
describe('stopBeforeSecondMutator', () => {
|
|
960
|
+
beforeEach(() => {
|
|
961
|
+
// Common setup for these tests: mock the tool registry.
|
|
962
|
+
const mockToolRegistry = {
|
|
963
|
+
getTool: vi.fn((toolName) => {
|
|
964
|
+
if (toolName === 'edit') {
|
|
965
|
+
return { kind: Kind.Edit };
|
|
966
|
+
}
|
|
967
|
+
return { kind: Kind.Other };
|
|
968
|
+
}),
|
|
969
|
+
};
|
|
970
|
+
vi.mocked(mockConfig.getToolRegistry).mockReturnValue(mockToolRegistry);
|
|
244
971
|
});
|
|
245
|
-
it('should
|
|
246
|
-
|
|
247
|
-
|
|
972
|
+
it('should stop streaming before a second mutator tool call', async () => {
|
|
973
|
+
const responses = [
|
|
974
|
+
{
|
|
975
|
+
candidates: [
|
|
976
|
+
{ content: { role: 'model', parts: [{ text: 'First part. ' }] } },
|
|
977
|
+
],
|
|
978
|
+
},
|
|
979
|
+
{
|
|
980
|
+
candidates: [
|
|
981
|
+
{
|
|
982
|
+
content: {
|
|
983
|
+
role: 'model',
|
|
984
|
+
parts: [{ functionCall: { name: 'edit', args: {} } }],
|
|
985
|
+
},
|
|
986
|
+
},
|
|
987
|
+
],
|
|
988
|
+
},
|
|
989
|
+
{
|
|
990
|
+
candidates: [
|
|
991
|
+
{
|
|
992
|
+
content: {
|
|
993
|
+
role: 'model',
|
|
994
|
+
parts: [{ functionCall: { name: 'fetch', args: {} } }],
|
|
995
|
+
},
|
|
996
|
+
},
|
|
997
|
+
],
|
|
998
|
+
},
|
|
999
|
+
// This chunk contains the second mutator and should be clipped.
|
|
1000
|
+
{
|
|
1001
|
+
candidates: [
|
|
1002
|
+
{
|
|
1003
|
+
content: {
|
|
1004
|
+
role: 'model',
|
|
1005
|
+
parts: [
|
|
1006
|
+
{ functionCall: { name: 'edit', args: {} } },
|
|
1007
|
+
{ text: 'some trailing text' },
|
|
1008
|
+
],
|
|
1009
|
+
},
|
|
1010
|
+
},
|
|
1011
|
+
],
|
|
1012
|
+
},
|
|
1013
|
+
// This chunk should never be reached.
|
|
1014
|
+
{
|
|
1015
|
+
candidates: [
|
|
1016
|
+
{
|
|
1017
|
+
content: {
|
|
1018
|
+
role: 'model',
|
|
1019
|
+
parts: [{ text: 'This should not appear.' }],
|
|
1020
|
+
},
|
|
1021
|
+
},
|
|
1022
|
+
],
|
|
1023
|
+
},
|
|
1024
|
+
];
|
|
1025
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue((async function* () {
|
|
1026
|
+
for (const response of responses) {
|
|
1027
|
+
yield response;
|
|
1028
|
+
}
|
|
1029
|
+
})());
|
|
1030
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-mutator-test');
|
|
1031
|
+
for await (const _ of stream) {
|
|
1032
|
+
// Consume the stream to trigger history recording.
|
|
1033
|
+
}
|
|
248
1034
|
const history = chat.getHistory();
|
|
249
|
-
// If modelOutput is empty, it might push a default empty model part depending on isFunctionResponse
|
|
250
|
-
// Assuming isFunctionResponse(userInput) is false for this simple text input
|
|
251
1035
|
expect(history.length).toBe(2);
|
|
252
|
-
|
|
253
|
-
expect(
|
|
254
|
-
expect(
|
|
1036
|
+
const modelTurn = history[1];
|
|
1037
|
+
expect(modelTurn.role).toBe('model');
|
|
1038
|
+
expect(modelTurn?.parts?.length).toBe(3);
|
|
1039
|
+
expect(modelTurn?.parts[0].text).toBe('First part. ');
|
|
1040
|
+
expect(modelTurn.parts[1].functionCall?.name).toBe('edit');
|
|
1041
|
+
expect(modelTurn.parts[2].functionCall?.name).toBe('fetch');
|
|
255
1042
|
});
|
|
256
|
-
it('should
|
|
257
|
-
const
|
|
258
|
-
{
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
1043
|
+
it('should not stop streaming if only one mutator is present', async () => {
|
|
1044
|
+
const responses = [
|
|
1045
|
+
{
|
|
1046
|
+
candidates: [
|
|
1047
|
+
{ content: { role: 'model', parts: [{ text: 'Part 1. ' }] } },
|
|
1048
|
+
],
|
|
1049
|
+
},
|
|
1050
|
+
{
|
|
1051
|
+
candidates: [
|
|
1052
|
+
{
|
|
1053
|
+
content: {
|
|
1054
|
+
role: 'model',
|
|
1055
|
+
parts: [{ functionCall: { name: 'edit', args: {} } }],
|
|
1056
|
+
},
|
|
1057
|
+
},
|
|
1058
|
+
],
|
|
1059
|
+
},
|
|
1060
|
+
{
|
|
1061
|
+
candidates: [
|
|
1062
|
+
{
|
|
1063
|
+
content: {
|
|
1064
|
+
role: 'model',
|
|
1065
|
+
parts: [{ text: 'Part 2.' }],
|
|
1066
|
+
},
|
|
1067
|
+
finishReason: 'STOP',
|
|
1068
|
+
},
|
|
1069
|
+
],
|
|
1070
|
+
},
|
|
263
1071
|
];
|
|
264
|
-
|
|
265
|
-
|
|
1072
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue((async function* () {
|
|
1073
|
+
for (const response of responses) {
|
|
1074
|
+
yield response;
|
|
1075
|
+
}
|
|
1076
|
+
})());
|
|
1077
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test message' }, 'prompt-id-one-mutator');
|
|
1078
|
+
for await (const _ of stream) {
|
|
1079
|
+
/* consume */
|
|
1080
|
+
}
|
|
266
1081
|
const history = chat.getHistory();
|
|
267
|
-
|
|
268
|
-
expect(
|
|
269
|
-
expect(
|
|
270
|
-
expect(
|
|
271
|
-
{ text: 'First model partSecond model part' },
|
|
272
|
-
]);
|
|
273
|
-
expect(history[2].role).toBe('model');
|
|
274
|
-
expect(history[2].parts).toBeUndefined();
|
|
275
|
-
expect(history[3].role).toBe('model');
|
|
276
|
-
expect(history[3].parts).toEqual([{ text: 'Third model part' }]);
|
|
277
|
-
expect(history[4].role).toBe('model');
|
|
278
|
-
expect(history[4].parts).toEqual([]);
|
|
1082
|
+
const modelTurn = history[1];
|
|
1083
|
+
expect(modelTurn?.parts?.length).toBe(3);
|
|
1084
|
+
expect(modelTurn.parts[1].functionCall?.name).toBe('edit');
|
|
1085
|
+
expect(modelTurn.parts[2].text).toBe('Part 2.');
|
|
279
1086
|
});
|
|
280
|
-
it('should
|
|
281
|
-
const
|
|
282
|
-
{
|
|
283
|
-
|
|
284
|
-
|
|
1087
|
+
it('should clip the chunk containing the second mutator, preserving prior parts', async () => {
|
|
1088
|
+
const responses = [
|
|
1089
|
+
{
|
|
1090
|
+
candidates: [
|
|
1091
|
+
{
|
|
1092
|
+
content: {
|
|
1093
|
+
role: 'model',
|
|
1094
|
+
parts: [{ functionCall: { name: 'edit', args: {} } }],
|
|
1095
|
+
},
|
|
1096
|
+
},
|
|
1097
|
+
],
|
|
1098
|
+
},
|
|
1099
|
+
// This chunk has a valid part before the second mutator.
|
|
1100
|
+
// The valid part should be kept, the rest of the chunk discarded.
|
|
1101
|
+
{
|
|
1102
|
+
candidates: [
|
|
1103
|
+
{
|
|
1104
|
+
content: {
|
|
1105
|
+
role: 'model',
|
|
1106
|
+
parts: [
|
|
1107
|
+
{ text: 'Keep this text. ' },
|
|
1108
|
+
{ functionCall: { name: 'edit', args: {} } },
|
|
1109
|
+
{ text: 'Discard this text.' },
|
|
1110
|
+
],
|
|
1111
|
+
},
|
|
1112
|
+
finishReason: 'STOP',
|
|
1113
|
+
},
|
|
1114
|
+
],
|
|
1115
|
+
},
|
|
285
1116
|
];
|
|
286
|
-
|
|
287
|
-
|
|
1117
|
+
const stream = (async function* () {
|
|
1118
|
+
for (const response of responses) {
|
|
1119
|
+
yield response;
|
|
1120
|
+
}
|
|
1121
|
+
})();
|
|
1122
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(stream);
|
|
1123
|
+
const resultStream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-clip-chunk');
|
|
1124
|
+
for await (const _ of resultStream) {
|
|
1125
|
+
/* consume */
|
|
1126
|
+
}
|
|
288
1127
|
const history = chat.getHistory();
|
|
289
|
-
|
|
290
|
-
expect(
|
|
291
|
-
expect(
|
|
292
|
-
expect(
|
|
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([]);
|
|
1128
|
+
const modelTurn = history[1];
|
|
1129
|
+
expect(modelTurn?.parts?.length).toBe(2);
|
|
1130
|
+
expect(modelTurn.parts[0].functionCall?.name).toBe('edit');
|
|
1131
|
+
expect(modelTurn.parts[1].text).toBe('Keep this text. ');
|
|
297
1132
|
});
|
|
298
|
-
it('should
|
|
299
|
-
const
|
|
300
|
-
{
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
1133
|
+
it('should handle two mutators in the same chunk (parallel call scenario)', async () => {
|
|
1134
|
+
const responses = [
|
|
1135
|
+
{
|
|
1136
|
+
candidates: [
|
|
1137
|
+
{
|
|
1138
|
+
content: {
|
|
1139
|
+
role: 'model',
|
|
1140
|
+
parts: [
|
|
1141
|
+
{ text: 'Some text. ' },
|
|
1142
|
+
{ functionCall: { name: 'edit', args: {} } },
|
|
1143
|
+
{ functionCall: { name: 'edit', args: {} } },
|
|
1144
|
+
],
|
|
1145
|
+
},
|
|
1146
|
+
finishReason: 'STOP',
|
|
1147
|
+
},
|
|
1148
|
+
],
|
|
1149
|
+
},
|
|
305
1150
|
];
|
|
306
|
-
|
|
307
|
-
|
|
1151
|
+
const stream = (async function* () {
|
|
1152
|
+
for (const response of responses) {
|
|
1153
|
+
yield response;
|
|
1154
|
+
}
|
|
1155
|
+
})();
|
|
1156
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockResolvedValue(stream);
|
|
1157
|
+
const resultStream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-parallel-mutators');
|
|
1158
|
+
for await (const _ of resultStream) {
|
|
1159
|
+
/* consume */
|
|
1160
|
+
}
|
|
308
1161
|
const history = chat.getHistory();
|
|
309
|
-
|
|
310
|
-
expect(
|
|
311
|
-
expect(
|
|
312
|
-
expect(
|
|
1162
|
+
const modelTurn = history[1];
|
|
1163
|
+
expect(modelTurn?.parts?.length).toBe(2);
|
|
1164
|
+
expect(modelTurn.parts[0].text).toBe('Some text. ');
|
|
1165
|
+
expect(modelTurn.parts[1].functionCall?.name).toBe('edit');
|
|
313
1166
|
});
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
1167
|
+
});
|
|
1168
|
+
describe('Model Resolution', () => {
|
|
1169
|
+
const mockResponse = {
|
|
1170
|
+
candidates: [
|
|
1171
|
+
{
|
|
1172
|
+
content: { parts: [{ text: 'response' }], role: 'model' },
|
|
1173
|
+
finishReason: 'STOP',
|
|
1174
|
+
},
|
|
1175
|
+
],
|
|
1176
|
+
};
|
|
1177
|
+
it('should use the FLASH model when in fallback mode (sendMessageStream)', async () => {
|
|
1178
|
+
vi.mocked(mockConfig.getModel).mockReturnValue('gemini-pro');
|
|
1179
|
+
vi.mocked(mockConfig.isInFallbackMode).mockReturnValue(true);
|
|
1180
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockImplementation(async () => (async function* () {
|
|
1181
|
+
yield mockResponse;
|
|
1182
|
+
})());
|
|
1183
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-res3');
|
|
1184
|
+
for await (const _ of stream) {
|
|
1185
|
+
// consume stream
|
|
1186
|
+
}
|
|
1187
|
+
expect(mockContentGenerator.generateContentStream).toHaveBeenCalledWith(expect.objectContaining({
|
|
1188
|
+
model: DEFAULT_GEMINI_FLASH_MODEL,
|
|
1189
|
+
}), 'prompt-id-res3');
|
|
324
1190
|
});
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
// @ts-expect-error Accessing private method for testing purposes
|
|
331
|
-
chat.recordHistory(userInput, modelOutputWithThought);
|
|
332
|
-
const history = chat.getHistory();
|
|
333
|
-
expect(history.length).toBe(2); // User input + consolidated model output
|
|
334
|
-
expect(history[0]).toEqual(userInput);
|
|
335
|
-
expect(history[1].role).toBe('model');
|
|
336
|
-
// The 'thought' part is skipped, 'Another visible text' becomes the first part.
|
|
337
|
-
expect(history[1].parts).toEqual([{ text: 'Another visible text' }]);
|
|
1191
|
+
});
|
|
1192
|
+
describe('Fallback Integration (Retries)', () => {
|
|
1193
|
+
const error429 = new ApiError({
|
|
1194
|
+
message: 'API Error 429: Quota exceeded',
|
|
1195
|
+
status: 429,
|
|
338
1196
|
});
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
1197
|
+
// Define the simulated behavior for retryWithBackoff for these tests.
|
|
1198
|
+
// This simulation tries the apiCall, if it fails, it calls the callback,
|
|
1199
|
+
// and then tries the apiCall again if the callback returns true.
|
|
1200
|
+
const simulateRetryBehavior = async (apiCall, options) => {
|
|
1201
|
+
try {
|
|
1202
|
+
return await apiCall();
|
|
1203
|
+
}
|
|
1204
|
+
catch (error) {
|
|
1205
|
+
if (options.onPersistent429) {
|
|
1206
|
+
// We simulate the "persistent" trigger here for simplicity.
|
|
1207
|
+
const shouldRetry = await options.onPersistent429(options.authType, error);
|
|
1208
|
+
if (shouldRetry) {
|
|
1209
|
+
return await apiCall();
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
throw error; // Stop if callback returns false/null or doesn't exist
|
|
1213
|
+
}
|
|
1214
|
+
};
|
|
1215
|
+
beforeEach(() => {
|
|
1216
|
+
mockRetryWithBackoff.mockImplementation(simulateRetryBehavior);
|
|
348
1217
|
});
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
{ role: 'model', parts: [{ text: 'Part 1.' }] },
|
|
352
|
-
{
|
|
353
|
-
role: 'model',
|
|
354
|
-
parts: [{ thought: true }, { text: 'Should be skipped' }],
|
|
355
|
-
},
|
|
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.' }]);
|
|
1218
|
+
afterEach(() => {
|
|
1219
|
+
mockRetryWithBackoff.mockImplementation(async (apiCall) => apiCall());
|
|
365
1220
|
});
|
|
366
|
-
it('should
|
|
367
|
-
const
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
1221
|
+
it('should call handleFallback with the specific failed model and retry if handler returns true', async () => {
|
|
1222
|
+
const authType = AuthType.LOGIN_WITH_GOOGLE;
|
|
1223
|
+
vi.mocked(mockConfig.getContentGeneratorConfig).mockReturnValue({
|
|
1224
|
+
authType,
|
|
1225
|
+
});
|
|
1226
|
+
const isInFallbackModeSpy = vi.spyOn(mockConfig, 'isInFallbackMode');
|
|
1227
|
+
isInFallbackModeSpy.mockReturnValue(false);
|
|
1228
|
+
vi.mocked(mockContentGenerator.generateContentStream)
|
|
1229
|
+
.mockRejectedValueOnce(error429) // Attempt 1 fails
|
|
1230
|
+
.mockResolvedValueOnce(
|
|
1231
|
+
// Attempt 2 succeeds
|
|
1232
|
+
(async function* () {
|
|
1233
|
+
yield {
|
|
1234
|
+
candidates: [
|
|
1235
|
+
{
|
|
1236
|
+
content: { parts: [{ text: 'Success on retry' }] },
|
|
1237
|
+
finishReason: 'STOP',
|
|
1238
|
+
},
|
|
1239
|
+
],
|
|
1240
|
+
};
|
|
1241
|
+
})());
|
|
1242
|
+
mockHandleFallback.mockImplementation(async () => {
|
|
1243
|
+
isInFallbackModeSpy.mockReturnValue(true);
|
|
1244
|
+
return true; // Signal retry
|
|
1245
|
+
});
|
|
1246
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'trigger 429' }, 'prompt-id-fb1');
|
|
1247
|
+
// Consume stream to trigger logic
|
|
1248
|
+
for await (const _ of stream) {
|
|
1249
|
+
// no-op
|
|
1250
|
+
}
|
|
1251
|
+
expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
|
|
1252
|
+
expect(mockHandleFallback).toHaveBeenCalledTimes(1);
|
|
1253
|
+
expect(mockHandleFallback).toHaveBeenCalledWith(mockConfig, 'test-model', authType, error429);
|
|
375
1254
|
const history = chat.getHistory();
|
|
376
|
-
|
|
377
|
-
expect(
|
|
378
|
-
expect(history[1].role).toBe('model');
|
|
379
|
-
expect(history[1].parts).toEqual([{ text: 'Visible 1Visible 2' }]);
|
|
1255
|
+
const modelTurn = history[1];
|
|
1256
|
+
expect(modelTurn.parts[0].text).toBe('Success on retry');
|
|
380
1257
|
});
|
|
381
|
-
it('should
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
expect(
|
|
392
|
-
expect(
|
|
1258
|
+
it('should stop retrying if handleFallback returns false (e.g., auth intent)', async () => {
|
|
1259
|
+
vi.mocked(mockConfig.getModel).mockReturnValue('gemini-pro');
|
|
1260
|
+
vi.mocked(mockContentGenerator.generateContentStream).mockRejectedValue(error429);
|
|
1261
|
+
mockHandleFallback.mockResolvedValue(false);
|
|
1262
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test stop' }, 'prompt-id-fb2');
|
|
1263
|
+
await expect((async () => {
|
|
1264
|
+
for await (const _ of stream) {
|
|
1265
|
+
/* consume stream */
|
|
1266
|
+
}
|
|
1267
|
+
})()).rejects.toThrow(error429);
|
|
1268
|
+
expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(1);
|
|
1269
|
+
expect(mockHandleFallback).toHaveBeenCalledTimes(1);
|
|
393
1270
|
});
|
|
394
1271
|
});
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
1272
|
+
it('should discard valid partial content from a failed attempt upon retry', async () => {
|
|
1273
|
+
// Mock the stream to fail on the first attempt after yielding some valid content.
|
|
1274
|
+
vi.mocked(mockContentGenerator.generateContentStream)
|
|
1275
|
+
.mockImplementationOnce(async () =>
|
|
1276
|
+
// First attempt: yields one valid chunk, then one invalid chunk
|
|
1277
|
+
(async function* () {
|
|
1278
|
+
yield {
|
|
1279
|
+
candidates: [
|
|
1280
|
+
{
|
|
1281
|
+
content: {
|
|
1282
|
+
parts: [{ text: 'This valid part should be discarded' }],
|
|
1283
|
+
},
|
|
1284
|
+
},
|
|
1285
|
+
],
|
|
400
1286
|
};
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
expect(history.length).toBe(1);
|
|
404
|
-
expect(history[0]).toEqual(newContent);
|
|
405
|
-
});
|
|
406
|
-
it('should add multiple items correctly', () => {
|
|
407
|
-
const content1 = {
|
|
408
|
-
role: 'user',
|
|
409
|
-
parts: [{ text: 'Message 1' }],
|
|
1287
|
+
yield {
|
|
1288
|
+
candidates: [{ content: { parts: [{ text: '' }] } }], // Invalid chunk triggers retry
|
|
410
1289
|
};
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
1290
|
+
})())
|
|
1291
|
+
.mockImplementationOnce(async () =>
|
|
1292
|
+
// Second attempt (the retry): succeeds
|
|
1293
|
+
(async function* () {
|
|
1294
|
+
yield {
|
|
1295
|
+
candidates: [
|
|
1296
|
+
{
|
|
1297
|
+
content: {
|
|
1298
|
+
parts: [{ text: 'Successful final response' }],
|
|
1299
|
+
},
|
|
1300
|
+
finishReason: 'STOP',
|
|
1301
|
+
},
|
|
1302
|
+
],
|
|
414
1303
|
};
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
1304
|
+
})());
|
|
1305
|
+
// Send a message and consume the stream
|
|
1306
|
+
const stream = await chat.sendMessageStream('test-model', { message: 'test' }, 'prompt-id-discard-test');
|
|
1307
|
+
const events = [];
|
|
1308
|
+
for await (const event of stream) {
|
|
1309
|
+
events.push(event);
|
|
1310
|
+
}
|
|
1311
|
+
// Check that a retry happened
|
|
1312
|
+
expect(mockContentGenerator.generateContentStream).toHaveBeenCalledTimes(2);
|
|
1313
|
+
expect(events.some((e) => e.type === StreamEventType.RETRY)).toBe(true);
|
|
1314
|
+
// Check the final recorded history
|
|
1315
|
+
const history = chat.getHistory();
|
|
1316
|
+
expect(history.length).toBe(2); // user turn + final model turn
|
|
1317
|
+
const modelTurn = history[1];
|
|
1318
|
+
// The model turn should only contain the text from the successful attempt
|
|
1319
|
+
expect(modelTurn.parts[0].text).toBe('Successful final response');
|
|
1320
|
+
// It should NOT contain any text from the failed attempt
|
|
1321
|
+
expect(modelTurn.parts[0].text).not.toContain('This valid part should be discarded');
|
|
1322
|
+
});
|
|
1323
|
+
describe('stripThoughtsFromHistory', () => {
|
|
1324
|
+
it('should strip thought signatures', () => {
|
|
1325
|
+
chat.setHistory([
|
|
1326
|
+
{
|
|
1327
|
+
role: 'user',
|
|
1328
|
+
parts: [{ text: 'hello' }],
|
|
1329
|
+
},
|
|
1330
|
+
{
|
|
1331
|
+
role: 'model',
|
|
1332
|
+
parts: [
|
|
1333
|
+
{ text: 'thinking...', thoughtSignature: 'thought-123' },
|
|
1334
|
+
{
|
|
1335
|
+
functionCall: { name: 'test', args: {} },
|
|
1336
|
+
thoughtSignature: 'thought-456',
|
|
1337
|
+
},
|
|
1338
|
+
],
|
|
1339
|
+
},
|
|
1340
|
+
]);
|
|
1341
|
+
chat.stripThoughtsFromHistory();
|
|
1342
|
+
expect(chat.getHistory()).toEqual([
|
|
1343
|
+
{
|
|
1344
|
+
role: 'user',
|
|
1345
|
+
parts: [{ text: 'hello' }],
|
|
1346
|
+
},
|
|
1347
|
+
{
|
|
1348
|
+
role: 'model',
|
|
1349
|
+
parts: [
|
|
1350
|
+
{ text: 'thinking...' },
|
|
1351
|
+
{ functionCall: { name: 'test', args: {} } },
|
|
1352
|
+
],
|
|
1353
|
+
},
|
|
1354
|
+
]);
|
|
421
1355
|
});
|
|
422
1356
|
});
|
|
423
1357
|
});
|