logancyang_obsidian-copilot/src/utils/modelUtils.ts
Zero Liu adf8c68a73
chore(deps): remove unused deps and dead code to shrink bundle (#2460)
Drop unused runtime deps (huggingface, koa, langchain, react-markdown,
react-syntax-highlighter, next-i18next, p-queue, trie-search, sse,
codemirror-companion-extension, tabler/icons-react, etc.) and demote
build-only packages to devDependencies. Delete dead source files
(quick-ask modes, ListPromptModal, NewChatConfirmModal,
PatternMatchingModal, ContainerContext, command-ui chat-message,
ui/tabs) and unused exports flagged by knip. Add `lint:dead` script
backed by knip and a knip.json config.

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-14 22:25:59 -07:00

57 lines
2.1 KiB
TypeScript

import {
ChatModelProviders,
ChatModels,
ProviderSettingsKeyMap,
SettingKeyProviders,
} from "@/constants";
import { getSettings } from "@/settings/model";
import { CustomModel } from "@/aiParams";
/**
* Get API key for a provider, with model-specific key taking precedence over global settings.
*
* @param provider - The provider to get the API key for
* @param model - Optional model instance; if provided and has apiKey, it will be used instead of global key
* @returns The API key (model-specific if available, otherwise global provider key, or empty string)
*
* @example
* // Get global API key for OpenAI
* const globalKey = getApiKeyForProvider(ChatModelProviders.OPENAI);
*
* // Get model-specific key (falls back to global if model.apiKey is empty)
* const modelKey = getApiKeyForProvider(ChatModelProviders.OPENAI, customModel);
*/
export function getApiKeyForProvider(provider: SettingKeyProviders, model?: CustomModel): string {
const settings = getSettings();
return model?.apiKey || (settings[ProviderSettingsKeyMap[provider]] as string | undefined) || "";
}
/**
* Get the list of models that are always required and cannot be disabled.
* These models provide essential functionality for the plugin.
* Uses a getter function to avoid circular dependency issues.
*/
function getRequiredModels(): ReadonlyArray<{ name: string; provider: string }> {
return [
{ name: ChatModels.COPILOT_PLUS_FLASH, provider: ChatModelProviders.COPILOT_PLUS },
{ name: ChatModels.OPENROUTER_GEMINI_2_5_FLASH, provider: ChatModelProviders.OPENROUTERAI },
];
}
/**
* Checks if a model is required and should always be enabled.
* Required models cannot be disabled by users as they provide core plugin functionality.
*
* @param model - The model to check
* @returns true if the model is required and must remain enabled, false otherwise
*
* @example
* if (isRequiredChatModel(model)) {
* // This model cannot be disabled
* }
*/
export function isRequiredChatModel(model: CustomModel): boolean {
return getRequiredModels().some(
(required) => required.name === model.name && required.provider === model.provider
);
}