Files
OllaHub/hooks/useUserPrefs.ts
Evandro Fonseca Junior 0cc7a81319 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.
2025-10-28 14:14:07 -03:00

74 lines
1.6 KiB
TypeScript

"use client";
import { useCallback, useEffect, useState } from "react";
export type DevicePref = "auto" | "cpu" | "gpu";
type Prefs = {
selectedModel: string | null; // NOVO
systemPrompt: string;
device: DevicePref;
gpuIndex: number;
numGpu: number;
};
const defaultPrefs: Prefs = {
selectedModel: null, // NOVO
systemPrompt: "",
device: "auto",
gpuIndex: 0,
numGpu: 1,
};
export function useUserPrefs() {
const [prefs, setPrefs] = useState<Prefs>(defaultPrefs);
const [ready, setReady] = useState(false);
// Carregar do Redis
useEffect(() => {
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 };
// 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;
});
}, []);
return { prefs, ready, update };
}
export function buildOllamaOptions(
device: DevicePref,
numGpu: number,
gpuIndex: number
) {
if (device === "auto") return undefined;
if (device === "cpu") return { num_gpu: 0 } as const;
// gpu
return {
num_gpu: Math.max(1, numGpu),
main_gpu: Math.max(0, gpuIndex),
} as const;
}