obsidian-copilot icon indicating copy to clipboard operation
obsidian-copilot copied to clipboard

Refactor model API key handling and improve model filtering.

Open Emt-lin opened this issue 1 month ago • 2 comments

Summary

  • Extract API key handling logic into reusable utility functions
  • Add model filtering in ModelSelector to show only models with valid API keys or required models
  • Prevent disabling required models (Copilot Plus Flash, OpenRouter Gemini)

Key Changes

  • New utility file: src/utils/modelUtils.ts with getApiKeyForProvider() and isRequiredChatModel() helpers
  • ModelSelector: Now filters models by API key availability
  • Consistent API key retrieval: Unified across ApiKeyDialog, ModelAddDialog, and ModelEditDialog
  • Required models protection: Cannot be disabled in ModelTable

Files Changed

  • src/utils/modelUtils.ts (new file, 54 lines)
  • src/components/ui/ModelSelector.tsx
  • src/settings/v2/components/ApiKeyDialog.tsx
  • src/settings/v2/components/ModelAddDialog.tsx
  • src/settings/v2/components/ModelEditDialog.tsx
  • src/settings/v2/components/ModelTable.tsx
  • src/utils.ts
CleanShot 2025-11-05 at 20 19 37

[!NOTE] Centralizes API key logic in utils/modelUtils, filters selectable models by API key or required status, and disables toggling of required models, updating dialogs and utils accordingly.

  • Utilities:
    • Add src/utils/modelUtils.ts with getApiKeyForProvider() and isRequiredChatModel().
  • UI:
    • src/components/ui/ModelSelector.tsx: Show only models with valid API keys or required models; use filtered list for selection/restore.
    • src/settings/v2/components/ModelTable.tsx: Disable enable toggle for required chat models.
  • Settings dialogs:
    • src/settings/v2/components/ApiKeyDialog.tsx, ModelAddDialog.tsx, ModelEditDialog.tsx: Use getApiKeyForProvider() for consistent key retrieval.
  • Core utils:
    • src/utils.ts: Simplify getNeedSetKeyProvider(); update checkModelApiKey() to use centralized key lookup and improved notices.

Written by Cursor Bugbot for commit 1a29fee054780454e68834814dfb0280c36cdb28. This will update automatically on new commits. Configure here.

Emt-lin avatar Nov 05 '25 12:11 Emt-lin

bugbot run

logancyang avatar Nov 05 '25 19:11 logancyang

@Emt-lin I've removed the popups complaining about no API key during onboarding #2015. As byproduct, I removed the logic that forbids the user to switch to a default model without API key. Now every "no API key" error should show as AI response errorblock. As for the model picker in chat input, we should simply hide those models with no API key.

Does this PR get affected by this design above? And could you please resolve conflict and rebase? thanks!

logancyang avatar Nov 11 '25 22:11 logancyang