| 1 | import { ConfigResult, ConfigValidationError } from "@continuedev/config-yaml"; |
| 2 | import { createSlice, PayloadAction } from "@reduxjs/toolkit"; |
| 3 | import { BrowserSerializedContinueConfig } from "core"; |
| 4 | import { DEFAULT_MAX_TOKENS } from "core/llm/constants"; |
| 5 | |
| 6 | export type ConfigState = { |
| 7 | configError: ConfigValidationError[] | undefined; |
| 8 | config: BrowserSerializedContinueConfig; |
| 9 | loading: boolean; |
| 10 | }; |
| 11 | |
| 12 | export const EMPTY_CONFIG: BrowserSerializedContinueConfig = { |
| 13 | slashCommands: [], |
| 14 | contextProviders: [], |
| 15 | tools: [], |
| 16 | mcpServerStatuses: [], |
| 17 | usePlatform: true, |
| 18 | modelsByRole: { |
| 19 | chat: [], |
| 20 | apply: [], |
| 21 | edit: [], |
| 22 | summarize: [], |
| 23 | autocomplete: [], |
| 24 | rerank: [], |
| 25 | embed: [], |
| 26 | }, |
| 27 | selectedModelByRole: { |
| 28 | chat: null, |
| 29 | apply: null, |
| 30 | edit: null, |
| 31 | summarize: null, |
| 32 | autocomplete: null, |
| 33 | rerank: null, |
| 34 | embed: null, |
| 35 | }, |
| 36 | rules: [], |
| 37 | }; |
| 38 | |
| 39 | const initialState: ConfigState = { |
| 40 | configError: undefined, |
| 41 | config: EMPTY_CONFIG, |
| 42 | loading: false, |
| 43 | }; |
| 44 | |
| 45 | export const configSlice = createSlice({ |
| 46 | name: "config", |
| 47 | initialState, |
| 48 | reducers: { |
| 49 | setConfigResult: ( |
| 50 | state, |
| 51 | { |
| 52 | payload: result, |
| 53 | }: PayloadAction<ConfigResult<BrowserSerializedContinueConfig>>, |
| 54 | ) => { |
| 55 | const { config, errors } = result; |
| 56 | if (!errors || errors.length === 0) { |
| 57 | state.configError = undefined; |
| 58 | } else { |
| 59 | state.configError = errors; |
| 60 | } |
| 61 | |
| 62 | // If an error is found in config on save, |
| 63 | // We must invalidate the GUI config too, |
| 64 | // Since core won't be able to load config |
| 65 | // Don't invalidate the loaded config |
| 66 | if (!config) { |
| 67 | state.config = EMPTY_CONFIG; |
| 68 | } else { |
| 69 | state.config = config; |
| 70 | } |
| 71 | state.loading = false; |
| 72 | }, |
| 73 | updateConfig: ( |
| 74 | state, |
| 75 | { payload: config }: PayloadAction<BrowserSerializedContinueConfig>, |
| 76 | ) => { |
| 77 | state.config = config; |
| 78 | }, |
| 79 | setConfigLoading: (state, { payload: loading }: PayloadAction<boolean>) => { |
| 80 | state.loading = loading; |
| 81 | }, |
| 82 | }, |
| 83 | selectors: { |
| 84 | selectSelectedChatModelContextLength: (state): number => { |
| 85 | return ( |
| 86 | state.config.selectedModelByRole.chat?.contextLength || |
| 87 | DEFAULT_MAX_TOKENS |
| 88 | ); |
| 89 | }, |
| 90 | selectSelectedChatModel: (state) => { |
| 91 | return state.config.selectedModelByRole.chat; |
| 92 | }, |
| 93 | selectUIConfig: (state) => { |
| 94 | return state.config?.ui ?? null; |
| 95 | }, |
| 96 | }, |
| 97 | }); |
| 98 | |
| 99 | export const { updateConfig, setConfigResult, setConfigLoading } = |
| 100 | configSlice.actions; |
| 101 | |
| 102 | export const { |
| 103 | selectSelectedChatModelContextLength, |
| 104 | selectUIConfig, |
| 105 | selectSelectedChatModel, |
| 106 | } = configSlice.selectors; |
| 107 | |
| 108 | export default configSlice.reducer; |