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