Files
cline-multi-mirror/webview-ui/src/utils/validate.ts
T
Daniel TrugmanandDennis Bartlett d1a097cbd4 Requesty dynamic model selection (#1836)
* Extract reuseable ModelDescriptionMarkdown from OpenRouter model picker

* Requesty: Add model picker component

* Refactor readOpenRouterModels to allow any dynamic list filename

* Extract parsePrice to allow reuse by other providers

* Simplify model display name switch case

* Requesty: Add dynamic model list fetching from API

* Requesty: Add default model selection

* Requesty: Specify max_tokens when sending request

* Add changeset

---------

Co-authored-by: Dennis Bartlett <bartlett.dc.1@gmail.com>
2025-02-28 13:29:34 -08:00

122 lines
4.3 KiB
TypeScript

import { ApiConfiguration, openRouterDefaultModelId, requestyDefaultModelId } from "../../../src/shared/api"
import { ModelInfo } from "../../../src/shared/api"
export function validateApiConfiguration(apiConfiguration?: ApiConfiguration): string | undefined {
if (apiConfiguration) {
switch (apiConfiguration.apiProvider) {
case "anthropic":
if (!apiConfiguration.apiKey) {
return "You must provide a valid API key or choose a different provider."
}
break
case "bedrock":
if (!apiConfiguration.awsRegion) {
return "You must choose a region to use with AWS Bedrock."
}
break
case "openrouter":
if (!apiConfiguration.openRouterApiKey) {
return "You must provide a valid API key or choose a different provider."
}
break
case "vertex":
if (!apiConfiguration.vertexProjectId || !apiConfiguration.vertexRegion) {
return "You must provide a valid Google Cloud Project ID and Region."
}
break
case "gemini":
if (!apiConfiguration.geminiApiKey) {
return "You must provide a valid API key or choose a different provider."
}
break
case "openai-native":
if (!apiConfiguration.openAiNativeApiKey) {
return "You must provide a valid API key or choose a different provider."
}
break
case "deepseek":
if (!apiConfiguration.deepSeekApiKey) {
return "You must provide a valid API key or choose a different provider."
}
break
case "xai":
if (!apiConfiguration.xaiApiKey) {
return "You must provide a valid API key or choose a different provider."
}
break
case "qwen":
if (!apiConfiguration.qwenApiKey) {
return "You must provide a valid API key or choose a different provider."
}
break
case "mistral":
if (!apiConfiguration.mistralApiKey) {
return "You must provide a valid API key or choose a different provider."
}
break
case "openai":
if (!apiConfiguration.openAiBaseUrl || !apiConfiguration.openAiApiKey || !apiConfiguration.openAiModelId) {
return "You must provide a valid base URL, API key, and model ID."
}
break
case "requesty":
if (!apiConfiguration.requestyApiKey || !apiConfiguration.requestyModelId) {
return "You must provide a valid API key or choose a different provider."
}
break
case "together":
if (!apiConfiguration.togetherApiKey || !apiConfiguration.togetherModelId) {
return "You must provide a valid API key or choose a different provider."
}
break
case "ollama":
if (!apiConfiguration.ollamaModelId) {
return "You must provide a valid model ID."
}
break
case "lmstudio":
if (!apiConfiguration.lmStudioModelId) {
return "You must provide a valid model ID."
}
break
case "vscode-lm":
if (!apiConfiguration.vsCodeLmModelSelector) {
return "You must provide a valid model selector."
}
break
}
}
return undefined
}
export function validateModelId(
apiConfiguration?: ApiConfiguration,
openRouterModels?: Record<string, ModelInfo>,
requestyModels?: Record<string, ModelInfo>,
): string | undefined {
if (apiConfiguration) {
switch (apiConfiguration.apiProvider) {
case "openrouter":
const openRouterModelId = apiConfiguration.openRouterModelId || openRouterDefaultModelId // in case the user hasn't changed the model id, it will be undefined by default
if (!openRouterModelId) {
return "You must provide a model ID."
}
if (openRouterModels && !Object.keys(openRouterModels).includes(openRouterModelId)) {
// even if the model list endpoint failed, extensionstatecontext will always have the default model info
return "The model ID you provided is not available. Please choose a different model."
}
break
case "requesty":
const requestyModelId = apiConfiguration.requestyModelId || requestyDefaultModelId // in case the user hasn't changed the model id, it will be undefined by default
if (!requestyModelId) {
return "You must provide a model ID."
}
if (requestyModels && !Object.keys(requestyModels).includes(requestyModelId)) {
// even if the model list endpoint failed, extensionstatecontext will always have the default model info
return "The model ID you provided is not available. Please choose a different model."
}
break
}
}
return undefined
}