feat: add database support, conversation sidebar, and improve chat streaming

- Update .gitignore to exclude database files, extensions, and .cursor directory
- Enhance chat streaming API with safe enqueue and close functions to prevent errors
- Integrate ConversationSidebar component into ClientHome for better UI
- Add dependencies like better-sqlite3 and esbuild packages for database functionality

This commit introduces database persistence for conversations, improves error handling in streaming responses, and adds a sidebar for managing chats, enhancing overall app robustness and user experience.
This commit is contained in:
Evandro Fonseca Junior committed 2025-10-28 14:14:07 -03:00
1 parent a3f8226fda
commit 0cc7a81319
34 files changed
+2370 -198

No files matched your search

+28
View File
@@ -0,0 +1,28 @@
"use client";
import { useCallback, useEffect, useState } from "react";
export type CopyMode = "markdown" | "text";
const KEY = "ollahub-copy-mode";
export function useCopyPrefs() {
const [copyMode, setCopyModeState] = useState<CopyMode>("markdown");
useEffect(() => {
if (typeof window === "undefined") return;
const saved = localStorage.getItem(KEY) as CopyMode | null;
if (saved === "markdown" || saved === "text") {
setCopyModeState(saved);
}
}, []);
const setCopyMode = useCallback((mode: CopyMode) => {
setCopyModeState(mode);
if (typeof window !== "undefined") {
localStorage.setItem(KEY, mode);
}
}, []);
return { copyMode, setCopyMode };
}
+27 -30
View File
@@ -5,20 +5,15 @@ import { useCallback, useEffect, useState } from "react";
export type DevicePref = "auto" | "cpu" | "gpu";
type Prefs = {
selectedModel: string | null; // NOVO
systemPrompt: string;
device: DevicePref;
gpuIndex: number; // main_gpu
numGpu: number; // num_gpu
gpuIndex: number;
numGpu: number;
};
const KEYS = {
systemPrompt: "ollahub-system-prompt",
device: "ollahub-device-pref",
gpuIndex: "ollahub-gpu-index",
numGpu: "ollahub-num-gpu",
} as const;
const defaultPrefs: Prefs = {
selectedModel: null, // NOVO
systemPrompt: "",
device: "auto",
gpuIndex: 0,
@@ -29,32 +24,34 @@ export function useUserPrefs() {
const [prefs, setPrefs] = useState<Prefs>(defaultPrefs);
const [ready, setReady] = useState(false);
// Carregar do Redis
useEffect(() => {
if (typeof window === "undefined") return;
const p: Prefs = {
systemPrompt:
localStorage.getItem(KEYS.systemPrompt) || defaultPrefs.systemPrompt,
device:
(localStorage.getItem(KEYS.device) as DevicePref) ||
defaultPrefs.device,
gpuIndex: Number(
localStorage.getItem(KEYS.gpuIndex) || defaultPrefs.gpuIndex
),
numGpu: Number(localStorage.getItem(KEYS.numGpu) || defaultPrefs.numGpu),
};
setPrefs(p);
setReady(true);
fetch("/api/preferences")
.then((res) => res.json())
.then((data) => {
setPrefs(data);
setReady(true);
})
.catch((err) => {
console.error("Erro ao carregar preferências:", err);
setReady(true);
});
}, []);
// Salvar no Redis
const update = useCallback((next: Partial<Prefs>) => {
setPrefs((prev) => {
const merged: Prefs = { ...prev, ...next };
if (typeof window !== "undefined") {
localStorage.setItem(KEYS.systemPrompt, merged.systemPrompt);
localStorage.setItem(KEYS.device, merged.device);
localStorage.setItem(KEYS.gpuIndex, String(merged.gpuIndex));
localStorage.setItem(KEYS.numGpu, String(merged.numGpu));
}
// Salvar no Redis
fetch("/api/preferences", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(merged),
}).catch((err) =>
console.error("Erro ao salvar preferências:", err)
);
return merged;
});
}, []);
@@ -74,4 +71,4 @@ export function buildOllamaOptions(
num_gpu: Math.max(1, numGpu),
main_gpu: Math.max(0, gpuIndex),
} as const;
}
}