Initial commit
This commit is contained in:
91
src/js/state.js
Normal file
91
src/js/state.js
Normal file
@@ -0,0 +1,91 @@
|
||||
// ==========================================
|
||||
// State Management
|
||||
// ==========================================
|
||||
|
||||
const State = {
|
||||
settings: {
|
||||
apiEndpoint: '',
|
||||
apiKey: '',
|
||||
model: 'gpt-3.5-turbo',
|
||||
stream: true,
|
||||
saveHistory: true,
|
||||
showThinking: true,
|
||||
systemPrompt: '',
|
||||
maxTokens: 4096,
|
||||
temperature: 0.7,
|
||||
topP: 1,
|
||||
presencePenalty: 0
|
||||
},
|
||||
models: [],
|
||||
chats: [],
|
||||
currentChatId: null,
|
||||
isGenerating: false,
|
||||
abortController: null
|
||||
};
|
||||
|
||||
function loadSettings() {
|
||||
const saved = Storage.get('chatSwitchboard_settings');
|
||||
if (saved) {
|
||||
State.settings = { ...State.settings, ...saved };
|
||||
}
|
||||
}
|
||||
|
||||
function saveSettings() {
|
||||
Storage.set('chatSwitchboard_settings', State.settings);
|
||||
}
|
||||
|
||||
function loadModels() {
|
||||
State.models = Storage.get('chatSwitchboard_models', []);
|
||||
}
|
||||
|
||||
function saveModels() {
|
||||
Storage.set('chatSwitchboard_models', State.models);
|
||||
}
|
||||
|
||||
function loadChats() {
|
||||
State.chats = Storage.get('chatSwitchboard_chats', []);
|
||||
}
|
||||
|
||||
function saveChats() {
|
||||
if (State.settings.saveHistory) {
|
||||
Storage.set('chatSwitchboard_chats', State.chats);
|
||||
}
|
||||
}
|
||||
|
||||
function createChat(title, messages) {
|
||||
const chat = {
|
||||
id: Date.now().toString(),
|
||||
title,
|
||||
messages,
|
||||
model: State.settings.model,
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString()
|
||||
};
|
||||
State.chats.unshift(chat);
|
||||
if (State.chats.length > 100) {
|
||||
State.chats = State.chats.slice(0, 100);
|
||||
}
|
||||
saveChats();
|
||||
return chat;
|
||||
}
|
||||
|
||||
function updateChat(chatId, updates) {
|
||||
const chat = State.chats.find(c => c.id === chatId);
|
||||
if (chat) {
|
||||
Object.assign(chat, updates, { updatedAt: new Date().toISOString() });
|
||||
saveChats();
|
||||
}
|
||||
}
|
||||
|
||||
function deleteChat(chatId) {
|
||||
State.chats = State.chats.filter(c => c.id !== chatId);
|
||||
saveChats();
|
||||
}
|
||||
|
||||
function getChat(chatId) {
|
||||
return State.chats.find(c => c.id === chatId);
|
||||
}
|
||||
|
||||
function getCurrentChat() {
|
||||
return State.currentChatId ? getChat(State.currentChatId) : null;
|
||||
}
|
||||
Reference in New Issue
Block a user