added persistent storage for the settings

This commit is contained in:
overcuriousity 2024-07-18 21:54:02 +02:00
parent 9bfe56f7ce
commit 5b882f4e13
5 changed files with 89 additions and 10 deletions

View File

@ -17,3 +17,14 @@ export const chats = sqliteTable('chats', {
createdAt: text('createdAt').notNull(),
focusMode: text('focusMode').notNull(),
});
export const settings = sqliteTable('settings', {
id: integer('id').primaryKey(),
chatModelProvider: text('chatModelProvider'),
chatModel: text('chatModel'),
embeddingModelProvider: text('embeddingModelProvider'),
embeddingModel: text('embeddingModel'),
openAIApiKey: text('openAIApiKey'),
openAIBaseURL: text('openAIBaseURL'),
// TODO: add user auth
});

View File

@ -5,6 +5,7 @@ import configRouter from './config';
import modelsRouter from './models';
import suggestionsRouter from './suggestions';
import chatsRouter from './chats';
import settingsRouter from './settings';
const router = express.Router();
@ -14,5 +15,6 @@ router.use('/config', configRouter);
router.use('/models', modelsRouter);
router.use('/suggestions', suggestionsRouter);
router.use('/chats', chatsRouter);
router.use('/settings', settingsRouter);
export default router;

38
src/routes/settings.ts Normal file
View File

@ -0,0 +1,38 @@
import express from 'express';
import db from '../db';
import { settings } from '../db/schema';
const router = express.Router();
router.post('/', async (req, res) => {
try {
const { chatModelProvider, chatModel, embeddingModelProvider, embeddingModel, openAIApiKey, openAIBaseURL } = req.body;
// TODO: Add user authentication
await db.insert(settings).values({
chatModelProvider,
chatModel,
embeddingModelProvider,
embeddingModel,
openAIApiKey,
openAIBaseURL,
}).execute();
res.status(200).json({ message: 'Settings saved successfully' });
} catch (err) {
res.status(500).json({ message: 'An error occurred while saving settings' });
}
});
router.get('/', async (req, res) => {
try {
// TODO: Add user authentication
const userSettings = await db.query.settings.findFirst();
res.status(200).json(userSettings);
} catch (err) {
res.status(500).json({ message: 'An error occurred while fetching settings' });
}
});
export default router;

View File

@ -21,6 +21,25 @@ export type Message = {
sources?: Document[];
};
const fetchSettings = async () => {
try {
const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/settings`);
const settings = await res.json();
if (settings) {
localStorage.setItem('chatModelProvider', settings.chatModelProvider);
localStorage.setItem('chatModel', settings.chatModel);
localStorage.setItem('embeddingModelProvider', settings.embeddingModelProvider);
localStorage.setItem('embeddingModel', settings.embeddingModel);
localStorage.setItem('openAIApiKey', settings.openAIApiKey);
localStorage.setItem('openAIBaseURL', settings.openAIBaseURL);
}
} catch (err) {
console.error('Failed to fetch settings:', err);
}
};
const useSocket = (
url: string,
setIsWSReady: (ready: boolean) => void,
@ -31,6 +50,8 @@ const useSocket = (
useEffect(() => {
if (!ws) {
const connectWs = async () => {
await fetchSettings();
let chatModel = localStorage.getItem('chatModel');
let chatModelProvider = localStorage.getItem('chatModelProvider');
let embeddingModel = localStorage.getItem('embeddingModel');
@ -306,6 +327,9 @@ const ChatWindow = ({ id }: { id?: string }) => {
const messagesRef = useRef<Message[]>([]);
useEffect(() => {
fetchSettings();
}, []);
useEffect(() => {
messagesRef.current = messages;
}, [messages]);

View File

@ -148,29 +148,33 @@ const SettingsDialog = ({
setIsUpdating(true);
try {
await fetch(`${process.env.NEXT_PUBLIC_API_URL}/config`, {
await fetch(`${process.env.NEXT_PUBLIC_API_URL}/settings`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(config),
body: JSON.stringify({
chatModelProvider: selectedChatModelProvider,
chatModel: selectedChatModel,
embeddingModelProvider: selectedEmbeddingModelProvider,
embeddingModel: selectedEmbeddingModel,
openAIApiKey: customOpenAIApiKey,
openAIBaseURL: customOpenAIBaseURL,
}),
});
// Still keep localStorage for quick access on the client-side
localStorage.setItem('chatModelProvider', selectedChatModelProvider!);
localStorage.setItem('chatModel', selectedChatModel!);
localStorage.setItem(
'embeddingModelProvider',
selectedEmbeddingModelProvider!,
);
localStorage.setItem('embeddingModelProvider', selectedEmbeddingModelProvider!);
localStorage.setItem('embeddingModel', selectedEmbeddingModel!);
localStorage.setItem('openAIApiKey', customOpenAIApiKey!);
localStorage.setItem('openAIBaseURL', customOpenAIBaseURL!);
} catch (err) {
console.log(err);
console.error(err);
} finally {
setIsUpdating(false);
setIsOpen(false);
window.location.reload();
}
};