| 1 | import { ModelRole } from "@continuedev/config-yaml"; |
| 2 | |
| 3 | import { ContinueConfig, ILLM } from ".."; |
| 4 | import { LLMConfigurationStatuses } from "../llm/constants"; |
| 5 | import { |
| 6 | GlobalContext, |
| 7 | GlobalContextModelSelections, |
| 8 | } from "../util/GlobalContext"; |
| 9 | |
| 10 | export function rectifySelectedModelsFromGlobalContext( |
| 11 | continueConfig: ContinueConfig, |
| 12 | profileId: string, |
| 13 | ): ContinueConfig { |
| 14 | const configCopy = { ...continueConfig }; |
| 15 | |
| 16 | const globalContext = new GlobalContext(); |
| 17 | const currentSelectedModels = globalContext.get("selectedModelsByProfileId"); |
| 18 | const currentForProfile: GlobalContextModelSelections = |
| 19 | currentSelectedModels?.[profileId] ?? {}; |
| 20 | |
| 21 | let fellBack = false; |
| 22 | |
| 23 | // summarize not implemented yet |
| 24 | const roles: ModelRole[] = [ |
| 25 | "autocomplete", |
| 26 | "apply", |
| 27 | "edit", |
| 28 | "embed", |
| 29 | "rerank", |
| 30 | "chat", |
| 31 | ]; |
| 32 | |
| 33 | for (const role of roles) { |
| 34 | let newModel: ILLM | null = null; |
| 35 | const currentSelection = currentForProfile[role] ?? null; |
| 36 | |
| 37 | if (currentSelection) { |
| 38 | const match = continueConfig.modelsByRole[role].find( |
| 39 | (m) => m.title === currentSelection, |
| 40 | ); |
| 41 | if (match) { |
| 42 | newModel = match; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | if (!newModel && continueConfig.modelsByRole[role].length > 0) { |
| 47 | newModel = continueConfig.modelsByRole[role][0]; |
| 48 | } |
| 49 | |
| 50 | if (!(currentSelection === (newModel?.title ?? null))) { |
| 51 | fellBack = true; |
| 52 | } |
| 53 | |
| 54 | // Currently only check for configuration status for apply |
| 55 | if ( |
| 56 | role === "apply" && |
| 57 | newModel?.getConfigurationStatus() !== LLMConfigurationStatuses.VALID |
| 58 | ) { |
| 59 | continue; |
| 60 | } |
| 61 | |
| 62 | configCopy.selectedModelByRole[role] = newModel; |
| 63 | } |
| 64 | |
| 65 | // In the case shared config wasn't respected, |
| 66 | // Rewrite the shared config |
| 67 | if (fellBack) { |
| 68 | globalContext.update("selectedModelsByProfileId", { |
| 69 | ...currentSelectedModels, |
| 70 | [profileId]: Object.fromEntries( |
| 71 | Object.entries(configCopy.selectedModelByRole).map(([key, value]) => [ |
| 72 | key, |
| 73 | value?.title ?? null, |
| 74 | ]), |
| 75 | ), |
| 76 | }); |
| 77 | } |
| 78 | |
| 79 | return configCopy; |
| 80 | } |