| 1 | import { ConfigValidationError } from "@continuedev/config-yaml"; |
| 2 | |
| 3 | import { ModelDescription, SerializedContinueConfig } from "../"; |
| 4 | import { Telemetry } from "../util/posthog"; |
| 5 | |
| 6 | /** |
| 7 | * Validates a SerializedContinueConfig object to ensure all properties are correctly formed. |
| 8 | * @param config The configuration object to validate. |
| 9 | * @returns An array of error messages if there are any. Otherwise, the config is valid. |
| 10 | */ |
| 11 | export function validateConfig(config: SerializedContinueConfig) { |
| 12 | const errors: ConfigValidationError[] = []; |
| 13 | |
| 14 | // Validate chat models |
| 15 | if (!Array.isArray(config.models)) { |
| 16 | errors.push({ |
| 17 | fatal: true, |
| 18 | message: "The 'models' field should be an array.", |
| 19 | }); |
| 20 | } else { |
| 21 | config.models.forEach((model, index) => { |
| 22 | if (typeof model.title !== "string" || model.title.trim() === "") { |
| 23 | errors.push({ |
| 24 | fatal: true, |
| 25 | message: `Model at index ${index} has an invalid or missing 'title'.`, |
| 26 | }); |
| 27 | } |
| 28 | if (typeof model.provider !== "string") { |
| 29 | errors.push({ |
| 30 | fatal: true, |
| 31 | message: `Model at index ${index} has an invalid 'provider'.`, |
| 32 | }); |
| 33 | } |
| 34 | |
| 35 | if (model.contextLength && model.completionOptions?.maxTokens) { |
| 36 | const difference = |
| 37 | model.contextLength - model.completionOptions.maxTokens; |
| 38 | |
| 39 | if (difference < 1000) { |
| 40 | errors.push({ |
| 41 | fatal: false, |
| 42 | message: `Model "${model.title}" has a contextLength of ${model.contextLength} and a maxTokens of ${model.completionOptions.maxTokens}. This leaves only ${difference} tokens for input context and will likely result in your inputs being truncated.`, |
| 43 | }); |
| 44 | } |
| 45 | } |
| 46 | }); |
| 47 | } |
| 48 | |
| 49 | // Validate tab autocomplete model(s) |
| 50 | if (config.tabAutocompleteModel) { |
| 51 | function validateTabAutocompleteModel(modelDescription: ModelDescription) { |
| 52 | const modelName = modelDescription.model.toLowerCase(); |
| 53 | const nonAutocompleteModels = [ |
| 54 | // "gpt", |
| 55 | // "claude", |
| 56 | "mistral", |
| 57 | "instruct", |
| 58 | ]; |
| 59 | |
| 60 | if ( |
| 61 | nonAutocompleteModels.some((m) => modelName.includes(m)) && |
| 62 | !modelName.includes("deepseek") && |
| 63 | !modelName.includes("codestral") && |
| 64 | !modelName.toLowerCase().includes("coder") |
| 65 | ) { |
| 66 | errors.push({ |
| 67 | fatal: false, |
| 68 | message: `${modelDescription.model} is not trained for tab-autocomplete, and will result in low-quality suggestions. See the docs to learn more about why: https://docs.continue.dev/features/tab-autocomplete#i-want-better-completions-should-i-use-gpt-4`, |
| 69 | }); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | if (Array.isArray(config.tabAutocompleteModel)) { |
| 74 | config.tabAutocompleteModel.forEach(validateTabAutocompleteModel); |
| 75 | } else { |
| 76 | validateTabAutocompleteModel(config.tabAutocompleteModel); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // Validate slashCommands |
| 81 | if (config.slashCommands) { |
| 82 | if (!Array.isArray(config.slashCommands)) { |
| 83 | errors.push({ |
| 84 | fatal: true, |
| 85 | message: "The 'slashCommands' field should be an array if defined.", |
| 86 | }); |
| 87 | } else { |
| 88 | config.slashCommands.forEach((command, index) => { |
| 89 | if (typeof command.name !== "string" || command.name.trim() === "") { |
| 90 | errors.push({ |
| 91 | fatal: true, |
| 92 | message: `Slash command at index ${index} has an invalid or missing 'name'.`, |
| 93 | }); |
| 94 | } |
| 95 | if (typeof command.description !== "string") { |
| 96 | errors.push({ |
| 97 | fatal: true, |
| 98 | message: `Slash command at index ${index} has an invalid or missing 'description'.`, |
| 99 | }); |
| 100 | } |
| 101 | }); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Validate contextProviders |
| 106 | if (config.contextProviders) { |
| 107 | if (!Array.isArray(config.contextProviders)) { |
| 108 | errors.push({ |
| 109 | fatal: true, |
| 110 | message: "The 'contextProviders' field should be an array if defined.", |
| 111 | }); |
| 112 | } else { |
| 113 | config.contextProviders.forEach((provider, index) => { |
| 114 | if (typeof provider.name !== "string" || provider.name.trim() === "") { |
| 115 | errors.push({ |
| 116 | fatal: true, |
| 117 | message: `Context provider at index ${index} has an invalid or missing 'name'.`, |
| 118 | }); |
| 119 | } |
| 120 | }); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // Validate embeddingsProvider |
| 125 | if ( |
| 126 | config.embeddingsProvider && |
| 127 | typeof config.embeddingsProvider !== "object" |
| 128 | ) { |
| 129 | errors.push({ |
| 130 | fatal: true, |
| 131 | message: "The 'embeddingsProvider' field should be an object if defined.", |
| 132 | }); |
| 133 | } |
| 134 | |
| 135 | // Validate reranker |
| 136 | if (config.reranker && typeof config.reranker !== "object") { |
| 137 | errors.push({ |
| 138 | fatal: true, |
| 139 | message: "The 'reranker' field should be an object if defined.", |
| 140 | }); |
| 141 | } |
| 142 | |
| 143 | // Validate other boolean flags |
| 144 | const booleanFlags: Array< |
| 145 | keyof Pick< |
| 146 | SerializedContinueConfig, |
| 147 | "allowAnonymousTelemetry" | "disableIndexing" | "disableSessionTitles" |
| 148 | > |
| 149 | > = ["allowAnonymousTelemetry", "disableIndexing", "disableSessionTitles"]; |
| 150 | |
| 151 | booleanFlags.forEach((flag) => { |
| 152 | if (config[flag] !== undefined && typeof config[flag] !== "boolean") { |
| 153 | errors.push({ |
| 154 | fatal: true, |
| 155 | message: `The '${flag}' field should be a boolean if defined.`, |
| 156 | }); |
| 157 | } |
| 158 | }); |
| 159 | |
| 160 | if (errors.length > 0) { |
| 161 | void Telemetry.capture( |
| 162 | "configValidationError", |
| 163 | { |
| 164 | errors, |
| 165 | }, |
| 166 | true, |
| 167 | ); |
| 168 | |
| 169 | return errors; |
| 170 | } |
| 171 | |
| 172 | return undefined; |
| 173 | } |