Elffuss BraidHub

kikocis/continue public

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