Skip to content

fix(chat): update provider handling and error messages - #314

Merged
steebchen merged 1 commit into
mainfrom
fix/chat-proider
Jun 8, 2025
Merged

steebchen merged 1 commit into
mainfrom
fix/chat-proider

Conversation

@steebchen

@steebchen steebchen commented Jun 8, 2025

Copy link
Copy Markdown
Member

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

  • Bug Fixes
    • Improved provider selection logic to better reflect the current project mode when choosing model providers.
    • Updated error messages to provide clearer guidance based on the selected project mode.

Refined provider handling to account for project modes. Improved error
messages to provide better guidance based on the current mode and
available providers.
@coderabbitai

coderabbitai Bot commented Jun 8, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

The 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

File Change Summary
apps/gateway/src/chat/chat.ts Updated provider selection logic to depend on project mode and revised error messages for unavailable providers.

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
Loading
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need 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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 5c90997 and 24ba0e2.

📒 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

Comment on lines +668 to +671
const availableProviders =
project.mode === "api-keys"
? providerKeys.map((key) => key.provider)
: providers.filter((p) => p.id !== "llmgateway").map((p) => p.id);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant