Elffuss BraidHub

kikocis/continue public

3ecf862e2425 · 2.6 KiBRawPermalink
1import { ConfigResult, ConfigValidationError } from "@continuedev/config-yaml";
2import { createSlice, PayloadAction } from "@reduxjs/toolkit";
3import { BrowserSerializedContinueConfig } from "core";
4import { DEFAULT_MAX_TOKENS } from "core/llm/constants";
5
6export type ConfigState = {
7 configError: ConfigValidationError[] | undefined;
8 config: BrowserSerializedContinueConfig;
9 loading: boolean;
10};
11
12export 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
39const initialState: ConfigState = {
40 configError: undefined,
41 config: EMPTY_CONFIG,
42 loading: false,
43};
44
45export 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 },
72 updateConfig: (
73 state,
74 { payload: config }: PayloadAction<BrowserSerializedContinueConfig>,
75 ) => {
76 state.config = config;
77 },
78 setConfigLoading: (state, { payload: loading }: PayloadAction<boolean>) => {
79 state.loading = loading;
80 },
81 },
82 selectors: {
83 selectSelectedChatModelContextLength: (state): number => {
84 return (
85 state.config.selectedModelByRole.chat?.contextLength ||
86 DEFAULT_MAX_TOKENS
87 );
88 },
89 selectSelectedChatModel: (state) => {
90 return state.config.selectedModelByRole.chat;
91 },
92 selectUIConfig: (state) => {
93 return state.config?.ui ?? null;
94 },
95 },
96});
97
98export const { updateConfig, setConfigResult, setConfigLoading } =
99 configSlice.actions;
100
101export const {
102 selectSelectedChatModelContextLength,
103 selectUIConfig,
104 selectSelectedChatModel,
105} = configSlice.selectors;
106
107export default configSlice.reducer;