fix(chat): update provider handling and error messages - #314
Conversation
Refined provider handling to account for project modes. Improved error messages to provide better guidance based on the current mode and available providers.
WalkthroughThe logic for determining available providers when selecting a model provider has been updated. Now, the source of available providers depends on the project's mode: "api-keys" mode uses provider keys from the database, while other modes use static providers excluding "llmgateway." Error messages have also been adjusted to reflect these changes. Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant ChatModule
participant Database
participant StaticProviders
User->>ChatModule: Request model provider (no explicit provider)
ChatModule->>Database: Get project mode
alt Project mode is "api-keys"
ChatModule->>Database: Fetch active provider keys
ChatModule->>ChatModule: Filter providers from DB keys
else Project mode is not "api-keys"
ChatModule->>StaticProviders: Fetch static providers (exclude llmgateway)
end
ChatModule->>User: Return available providers or error message
✨ Finishing Touches
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
apps/gateway/src/chat/chat.ts (1)
680-684: Good improvement to error messages, with opportunity for further enhancement.The mode-specific error messages are a good improvement. The api-keys mode message provides clear, actionable guidance.
The non-api-keys mode message could be more helpful if combined with the environment token checking suggested above:
message: project.mode === "api-keys" ? `No provider key set for any of the providers that support model ${usedModel}. Please add the provider key in the settings or switch the project mode to credits or hybrid.` - : `No available provider could be found for model ${usedModel}`, + : project.mode === "credits" + ? `No provider with available environment token could be found for model ${usedModel}. Please ensure environment variables are set for supported providers.` + : `No available provider could be found for model ${usedModel}. Please add provider keys in settings or ensure environment variables are set.`,
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
apps/gateway/src/chat/chat.ts(2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
- GitHub Check: build / run
- GitHub Check: e2e / run
| const availableProviders = | ||
| project.mode === "api-keys" | ||
| ? providerKeys.map((key) => key.provider) | ||
| : providers.filter((p) => p.id !== "llmgateway").map((p) => p.id); |
There was a problem hiding this comment.
Inconsistent provider availability logic compared to auto model selection.
The provider selection logic here differs from the auto model logic (lines 573-619). For non-api-keys modes, this code uses all static providers without checking environment token availability, while the auto model logic properly validates environment tokens before including providers.
This inconsistency could lead to runtime errors in credits mode when providers without available environment tokens are selected.
Consider aligning this logic with the auto model approach:
- const availableProviders =
- project.mode === "api-keys"
- ? providerKeys.map((key) => key.provider)
- : providers.filter((p) => p.id !== "llmgateway").map((p) => p.id);
+ let availableProviders: string[] = [];
+
+ if (project.mode === "api-keys") {
+ availableProviders = providerKeys.map((key) => key.provider);
+ } else {
+ const databaseProviders = providerKeys.map((key) => key.provider);
+
+ // Check environment token availability
+ const envProviders: string[] = [];
+ const supportedProviders = providers
+ .filter((p) => p.id !== "llmgateway" && providerIds.includes(p.id))
+ .map((p) => p.id);
+
+ for (const provider of supportedProviders) {
+ try {
+ const envVarMap = {
+ openai: "OPENAI_API_KEY",
+ anthropic: "ANTHROPIC_API_KEY",
+ "google-vertex": "VERTEX_API_KEY",
+ "google-ai-studio": "GOOGLE_AI_STUDIO_API_KEY",
+ "inference.net": "INFERENCE_NET_API_KEY",
+ "kluster.ai": "KLUSTER_AI_API_KEY",
+ "together.ai": "TOGETHER_AI_API_KEY",
+ };
+ if (process.env[envVarMap[provider as keyof typeof envVarMap]]) {
+ envProviders.push(provider);
+ }
+ } catch {}
+ }
+
+ if (project.mode === "credits") {
+ availableProviders = envProviders;
+ } else {
+ availableProviders = [...new Set([...databaseProviders, ...envProviders])];
+ }
+ }Committable suggestion skipped: line range outside the PR's diff.
🤖 Prompt for AI Agents
In apps/gateway/src/chat/chat.ts around lines 668 to 671, the provider selection
logic for non-api-keys mode currently includes all static providers without
verifying if their environment tokens are available, unlike the auto model logic
between lines 573 and 619 which validates token availability. To fix this,
update the provider filtering to check for environment token presence before
including a provider, aligning it with the auto model selection approach to
prevent runtime errors in credits mode.
Refined provider handling to account for project modes. Improved error messages to provide better guidance based on the current mode and available providers.
Summary by CodeRabbit