| 1 | import { ModelRole } from "@continuedev/config-yaml"; |
| 2 | import { createAsyncThunk } from "@reduxjs/toolkit"; |
| 3 | import { ProfileDescription } from "core/config/ProfileLifecycleManager"; |
| 4 | import { updateConfig } from "../slices/configSlice"; |
| 5 | import { ThunkApiType } from "../store"; |
| 6 | |
| 7 | export const updateSelectedModelByRole = createAsyncThunk< |
| 8 | void, |
| 9 | { |
| 10 | role: ModelRole; |
| 11 | modelTitle: string; |
| 12 | selectedProfile: ProfileDescription | null; |
| 13 | }, |
| 14 | ThunkApiType |
| 15 | >( |
| 16 | "config/updateSelectedModel", |
| 17 | async ( |
| 18 | { role, modelTitle, selectedProfile }, |
| 19 | { dispatch, extra, getState }, |
| 20 | ) => { |
| 21 | if (!selectedProfile) { |
| 22 | return; |
| 23 | } |
| 24 | |
| 25 | const state = getState(); |
| 26 | |
| 27 | const { |
| 28 | config: { config }, |
| 29 | } = state; |
| 30 | |
| 31 | const model = state.config.config.modelsByRole[role]?.find( |
| 32 | (m) => m.title === modelTitle, |
| 33 | ); |
| 34 | |
| 35 | if (!model) { |
| 36 | console.error( |
| 37 | `Model with title "${modelTitle}" not found for role "${role}"`, |
| 38 | ); |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | dispatch( |
| 43 | updateConfig({ |
| 44 | ...config, |
| 45 | selectedModelByRole: { |
| 46 | ...config.selectedModelByRole, |
| 47 | [role]: model, |
| 48 | }, |
| 49 | }), |
| 50 | ); |
| 51 | |
| 52 | extra.ideMessenger.post("config/updateSelectedModel", { |
| 53 | role, |
| 54 | profileId: selectedProfile.id, |
| 55 | title: modelTitle, |
| 56 | }); |
| 57 | }, |
| 58 | ); |