From 79d4d87f2444d669b1be367afa59d53cfa7b2356 Mon Sep 17 00:00:00 2001 From: Justin Luoma Date: Sat, 25 May 2024 08:04:40 -0400 Subject: [PATCH] feat: add copilot setting to config refactor: move getConfig to actions, extracted Settings --- src/config.ts | 90 ++++++++++++------------- src/routes/config.ts | 2 + ui/components/ChatWindow.tsx | 11 ++- ui/components/EmptyChatMessageInput.tsx | 13 ++-- ui/components/MessageBox.tsx | 6 +- ui/components/MessageInput.tsx | 10 +-- ui/components/MessageInputActions.tsx | 48 +++++++------ ui/components/SettingsDialog.tsx | 46 ++++--------- ui/lib/actions.ts | 15 ++++- ui/types/Settings.ts | 12 ++++ 10 files changed, 140 insertions(+), 113 deletions(-) create mode 100644 ui/types/Settings.ts diff --git a/src/config.ts b/src/config.ts index 69dfe68..e08aa11 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,54 +1,55 @@ import fs from 'fs'; import path from 'path'; -import {parse, stringify} from "smol-toml"; +import { parse, stringify } from 'smol-toml'; const configFileName = 'config.toml'; interface Config { - GENERAL: { - PORT: number; - SIMILARITY_MEASURE: string; - }; - API_KEYS: { - OPENAI: string; - GROQ: string; - }; - API_ENDPOINTS: { - SEARXNG: string; - OLLAMA: string; - }; - MODELS: [ - { - "name": string; - "api_key": string; - "base_url": string; - "provider": string; - } - ]; - EMBEDDINGS: [ - { - "name": string; - "model": string; - "api_key": string; - "base_url": string; - "provider": string; - } - ]; + GENERAL: { + PORT: number; + SIMILARITY_MEASURE: string; + ENABLE_COPILOT: boolean; + }; + API_KEYS: { + OPENAI: string; + GROQ: string; + }; + API_ENDPOINTS: { + SEARXNG: string; + OLLAMA: string; + }; + MODELS: [ + { + name: string; + api_key: string; + base_url: string; + provider: string; + }, + ]; + EMBEDDINGS: [ + { + name: string; + model: string; + api_key: string; + base_url: string; + provider: string; + }, + ]; } type RecursivePartial = { - [P in keyof T]?: RecursivePartial; + [P in keyof T]?: RecursivePartial; }; const loadConfig = () => - parse( - fs.readFileSync(path.join(__dirname, `../${configFileName}`), 'utf-8'), - ) as any as Config; + parse( + fs.readFileSync(path.join(__dirname, `../${configFileName}`), 'utf-8'), + ) as any as Config; export const getPort = () => loadConfig().GENERAL.PORT; export const getSimilarityMeasure = () => - loadConfig().GENERAL.SIMILARITY_MEASURE; + loadConfig().GENERAL.SIMILARITY_MEASURE; export const getOpenaiApiKey = () => loadConfig().API_KEYS.OPENAI; @@ -62,18 +63,17 @@ export const getCustomModels = () => loadConfig().MODELS; export const getCustomEmbeddingModels = () => loadConfig().EMBEDDINGS; +export const getCopilotEnabled = () => loadConfig().GENERAL.ENABLE_COPILOT; + export const updateConfig = (config: RecursivePartial) => { - const currentConfig = loadConfig(); + const currentConfig = loadConfig(); - const updatedConfig = { - ...currentConfig, - ...config - }; + const updatedConfig = { + ...currentConfig, + ...config, + }; - const toml = stringify(updatedConfig); + const toml = stringify(updatedConfig); - fs.writeFileSync( - path.join(__dirname, `../${configFileName}`), - toml, - ); + fs.writeFileSync(path.join(__dirname, `../${configFileName}`), toml); }; diff --git a/src/routes/config.ts b/src/routes/config.ts index bf13b63..193fd87 100644 --- a/src/routes/config.ts +++ b/src/routes/config.ts @@ -4,6 +4,7 @@ import { getAvailableEmbeddingModelProviders, } from '../lib/providers'; import { + getCopilotEnabled, getGroqApiKey, getOllamaApiEndpoint, getOpenaiApiKey, @@ -38,6 +39,7 @@ router.get('/', async (_, res) => { config['openaiApiKey'] = getOpenaiApiKey(); config['ollamaApiUrl'] = getOllamaApiEndpoint(); config['groqApiKey'] = getGroqApiKey(); + config['copilotEnabled'] = getCopilotEnabled(); res.status(200).json(config); }); diff --git a/ui/components/ChatWindow.tsx b/ui/components/ChatWindow.tsx index 5f266b5..2bec802 100644 --- a/ui/components/ChatWindow.tsx +++ b/ui/components/ChatWindow.tsx @@ -7,7 +7,7 @@ import Chat from './Chat'; import EmptyChat from './EmptyChat'; import { toast } from 'sonner'; import { useSearchParams } from 'next/navigation'; -import { getSuggestions } from '@/lib/actions'; +import { getConfig, getSuggestions } from '@/lib/actions'; export type Message = { id: string; @@ -156,6 +156,15 @@ const ChatWindow = () => { messagesRef.current = messages; }, [messages]); + useEffect(() => { + const fetchConfig = async () => { + const config = await getConfig(); + localStorage.setItem('copilotEnabled', config.copilotEnabled.toString()); + }; + + fetchConfig(); + }); + const sendMessage = async (message: string) => { if (loading) return; setLoading(true); diff --git a/ui/components/EmptyChatMessageInput.tsx b/ui/components/EmptyChatMessageInput.tsx index 4932803..e268300 100644 --- a/ui/components/EmptyChatMessageInput.tsx +++ b/ui/components/EmptyChatMessageInput.tsx @@ -12,8 +12,9 @@ const EmptyChatMessageInput = ({ focusMode: string; setFocusMode: (mode: string) => void; }) => { - const [copilotEnabled, setCopilotEnabled] = useState(false); + const [copilotToggled, setCopilotToggled] = useState(false); const [message, setMessage] = useState(''); + const copilotEnabled = localStorage.getItem('copilotEnabled') === 'true'; return (
*/}
- + {copilotEnabled && ( + + )}