rename frontend subfolder
This commit is contained in:
parent
0a894237bb
commit
16f084bfcd
21 changed files with 12 additions and 2 deletions
153
frontend/src/lib/chatStore.svelte.js
Normal file
153
frontend/src/lib/chatStore.svelte.js
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
import { createChat, sendUserMessage, openStream, fetchModels } from "./chatApi.svelte.js";
|
||||
|
||||
const STORAGE_KEY = "chatHistory";
|
||||
|
||||
function loadHistory() {
|
||||
try {
|
||||
return JSON.parse(localStorage.getItem(STORAGE_KEY) || "[]");
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
function saveHistory(list) {
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(list));
|
||||
}
|
||||
|
||||
export const chatStore = (() => {
|
||||
let chatId = $state(null);
|
||||
let messages = $state([]);
|
||||
let loading = $state(false);
|
||||
let input = $state("");
|
||||
let model = $state("qwen/qwen3-235b-a22b-2507"); // default
|
||||
let models = $state([]);
|
||||
let loadingModels = $state(true);
|
||||
|
||||
// public helpers
|
||||
const history = $derived(loadHistory());
|
||||
|
||||
function pushHistory(id, title, msgs) {
|
||||
console.log(`push history: ${id} - ${title}`);
|
||||
const h = history.filter((c) => c.id !== id);
|
||||
h.unshift({ id, title, messages: msgs });
|
||||
saveHistory(h.slice(0, 50)); // keep last 50
|
||||
}
|
||||
|
||||
async function selectChat(id) {
|
||||
if (id === chatId) return;
|
||||
chatId = id;
|
||||
const stored = loadHistory().find((c) => c.id === id);
|
||||
messages = stored?.messages || [];
|
||||
loading = true;
|
||||
loading = false;
|
||||
window.history.replaceState({}, "", `/${id}`);
|
||||
}
|
||||
|
||||
async function createAndSelect() {
|
||||
const { id } = await createChat(model);
|
||||
console.log(id);
|
||||
selectChat(id);
|
||||
return id;
|
||||
}
|
||||
|
||||
async function send() {
|
||||
if (!input.trim()) return;
|
||||
if (!chatId) await createAndSelect();
|
||||
|
||||
const userMsg = { id: crypto.randomUUID(), role: "user", text: input };
|
||||
messages = [...messages, userMsg];
|
||||
|
||||
pushHistory(chatId, userMsg.text.slice(0, 30), messages);
|
||||
|
||||
loading = true;
|
||||
const { message_id } = await sendUserMessage(chatId, input, model);
|
||||
input = "";
|
||||
|
||||
let assistantMsg = { id: message_id, role: "assistant", text: "" };
|
||||
messages = [...messages, assistantMsg];
|
||||
|
||||
const es = openStream(chatId, message_id);
|
||||
es.onmessage = (e) => {
|
||||
assistantMsg = { ...assistantMsg, text: assistantMsg.text + e.data };
|
||||
messages = [...messages.slice(0, -1), assistantMsg];
|
||||
};
|
||||
es.onerror = () => {
|
||||
es.close();
|
||||
loading = false;
|
||||
};
|
||||
es.addEventListener("done", (e) => {
|
||||
console.log(e);
|
||||
es.close();
|
||||
loading = false;
|
||||
pushHistory(chatId, userMsg.text.slice(0, 30), messages);
|
||||
});
|
||||
}
|
||||
|
||||
async function loadModels() {
|
||||
loadingModels = true;
|
||||
models = await fetchModels();
|
||||
loadingModels = false;
|
||||
|
||||
// Set default model if available and not already set
|
||||
if (models.length > 0 && !model) {
|
||||
model = models[0].id || models[0];
|
||||
}
|
||||
}
|
||||
|
||||
function handleKey(e) {
|
||||
if (e.key === "Enter" && !e.shiftKey) {
|
||||
e.preventDefault();
|
||||
send();
|
||||
}
|
||||
}
|
||||
|
||||
// initial route handling
|
||||
const path = window.location.pathname.slice(1);
|
||||
const storedHistory = loadHistory();
|
||||
if (path && !storedHistory.find((c) => c.id === path)) {
|
||||
createAndSelect();
|
||||
} else if (path) {
|
||||
selectChat(path);
|
||||
}
|
||||
|
||||
// Load models on initialization
|
||||
loadModels();
|
||||
|
||||
return {
|
||||
get chatId() {
|
||||
return chatId;
|
||||
},
|
||||
get messages() {
|
||||
return messages;
|
||||
},
|
||||
get loading() {
|
||||
return loading;
|
||||
},
|
||||
get input() {
|
||||
return input;
|
||||
},
|
||||
set input(v) {
|
||||
input = v;
|
||||
},
|
||||
get model() {
|
||||
return model;
|
||||
},
|
||||
set model(v) {
|
||||
model = v;
|
||||
},
|
||||
get models() {
|
||||
return models;
|
||||
},
|
||||
get loadingModels() {
|
||||
return loadingModels;
|
||||
},
|
||||
get history() {
|
||||
return loadHistory();
|
||||
},
|
||||
selectChat,
|
||||
send,
|
||||
handleKey,
|
||||
createAndSelect,
|
||||
loadModels,
|
||||
};
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue