Update consistente, da completare history redis
This commit is contained in:
@@ -1,15 +1,17 @@
|
||||
//App.jsx
|
||||
import React, { useState, useEffect } from "react";
|
||||
import "./App.css";
|
||||
import { themes } from "./themes";
|
||||
import ChatLayout from "./ChatLayout";
|
||||
import { useChatStream } from "./useChatStream";
|
||||
import { getSessionId } from "./useSessionId";
|
||||
import { getSessionId, getUserId, resetSessionId } from "./useSessionId";
|
||||
|
||||
export default function App() {
|
||||
const { messages, loading, sendMessage, stopGenerating, setMessages } = useChatStream();
|
||||
const [themeName, setThemeName] = useState("light");
|
||||
const theme = themes[themeName];
|
||||
const sessionId = getSessionId();
|
||||
const userId = getUserId();
|
||||
|
||||
useEffect(() => {
|
||||
const saved = localStorage.getItem("preferredTheme");
|
||||
@@ -25,17 +27,18 @@ export default function App() {
|
||||
};
|
||||
|
||||
const reloadHistory = async () => {
|
||||
const res = await fetch(`/api/history?user_id=${sessionId}`);
|
||||
const res = await fetch(`/api/history?user_id=${userId}&session_id=${sessionId}`);
|
||||
const history = await res.json();
|
||||
setMessages(history); // from useChatStream
|
||||
};
|
||||
|
||||
const freshStart = async () => {
|
||||
await fetch(`/api/history?user_id=${sessionId}`, { method: "DELETE" });
|
||||
await fetch(`/api/v1/history?user_id=${userId}&session_id=${sessionId}`, { method: "DELETE" });
|
||||
setMessages([]);
|
||||
resetSessionId();
|
||||
// or start a brand new sessionId:
|
||||
localStorage.removeItem("sessionId");
|
||||
window.location.reload();
|
||||
//localStorage.removeItem("sessionId");
|
||||
//window.location.reload();
|
||||
};
|
||||
|
||||
return (
|
||||
|
||||
@@ -15,7 +15,7 @@ export default function ChatLayout({
|
||||
onFreshStart
|
||||
}) {
|
||||
return (
|
||||
<div className={`d-flex flex-column vh-100 ${theme.bodyBg}`}>
|
||||
<div className={`mx-3 d-flex flex-column vh-100 ${theme.bodyBg}`}>
|
||||
<ChatHeader
|
||||
theme={theme}
|
||||
onToggleTheme={onToggleTheme}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
// useChatStream.js
|
||||
import { useState, useCallback, useRef } from "react";
|
||||
import { getSessionId } from './useSessionId';
|
||||
import { getSessionId, getUserId } from './useSessionId';
|
||||
|
||||
export function useChatStream() {
|
||||
const [messages, setMessages] = useState([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const abortRef = useRef(null);
|
||||
const sessionId = getSessionId();
|
||||
const userId = getUserId();
|
||||
|
||||
const sendMessage = useCallback(
|
||||
async (input) => {
|
||||
@@ -32,7 +33,7 @@ export function useChatStream() {
|
||||
"Content-Type": "application/json",
|
||||
Accept: "text/event-stream",
|
||||
},
|
||||
body: JSON.stringify({ user_id: sessionId, message: input }),
|
||||
body: JSON.stringify({ user_id: userId, session_id: sessionId, message: input }),
|
||||
signal: controller.signal,
|
||||
});
|
||||
|
||||
@@ -111,7 +112,7 @@ export function useChatStream() {
|
||||
}
|
||||
}, []);
|
||||
|
||||
return { messages, loading, sendMessage, stopGenerating };
|
||||
return { messages, loading, sendMessage, stopGenerating, setMessages };
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +1,26 @@
|
||||
// useSessionId.js
|
||||
export function getSessionId() {
|
||||
let sessionId = localStorage.getItem('sessionId');
|
||||
if (!sessionId) {
|
||||
sessionId = crypto.randomUUID();
|
||||
localStorage.setItem('sessionId', sessionId);
|
||||
// useIds.js
|
||||
export function getUserId() {
|
||||
let id = localStorage.getItem("userId");
|
||||
if (!id) {
|
||||
id = crypto.randomUUID();
|
||||
localStorage.setItem("userId", id);
|
||||
}
|
||||
return sessionId;
|
||||
return id;
|
||||
}
|
||||
|
||||
export function getSessionId() {
|
||||
let id = localStorage.getItem("sessionId");
|
||||
if (!id) {
|
||||
id = crypto.randomUUID();
|
||||
localStorage.setItem("sessionId", id);
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
export function resetSessionId() {
|
||||
const newId = crypto.randomUUID();
|
||||
localStorage.setItem("sessionId", newId);
|
||||
return newId;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user