refactoring and clean up of chat cliet
This commit is contained in:
@@ -9,18 +9,27 @@ export function useSession() {
|
||||
const [loadingHistory, setLoadingHistory] = useState(false);
|
||||
const [error, setError] = useState(null);
|
||||
|
||||
// Load session list on mount
|
||||
useEffect(() => {
|
||||
loadSessions();
|
||||
}, []);
|
||||
|
||||
async function loadSessions() {
|
||||
// Called by useChat after a message completes — keeps session list fresh
|
||||
const refreshSessions = useCallback(async () => {
|
||||
try {
|
||||
const data = await fetchSessions();
|
||||
setSessions(data);
|
||||
} catch (err) {
|
||||
setError(err.message);
|
||||
} catch {
|
||||
// non-critical — sidebar just won't update
|
||||
}
|
||||
}, []);
|
||||
|
||||
// Load session list on mount
|
||||
useEffect(() => {
|
||||
refreshSessions();
|
||||
}, [refreshSessions]);
|
||||
|
||||
function episodesToMessages(episodes) {
|
||||
return [...episodes].reverse().flatMap(ep => [
|
||||
{ id: `${ep.id}-user`, role: 'user', text: ep.user_message },
|
||||
{ id: `${ep.id}-ai`, role: 'assistant', text: ep.ai_response },
|
||||
]);
|
||||
}
|
||||
|
||||
// Switch to an existing session and load its history
|
||||
@@ -32,10 +41,7 @@ export function useSession() {
|
||||
try {
|
||||
const data = await fetchSessionHistory(session.external_id);
|
||||
// History comes back newest-first — reverse for display
|
||||
const history = data.episodes.reverse().map(ep => ([
|
||||
{ id: `${ep.id}-user`, role: 'user', text: ep.user_message },
|
||||
{ id: `${ep.id}-ai`, role: 'assistant', text: ep.ai_response },
|
||||
])).flat();
|
||||
const history = episodesToMessages(data.episodes);
|
||||
|
||||
setMessages(history);
|
||||
} catch (err) {
|
||||
@@ -58,15 +64,7 @@ export function useSession() {
|
||||
setMessages([]);
|
||||
}, []);
|
||||
|
||||
// Called by useChat after a message completes — keeps session list fresh
|
||||
const refreshSessions = useCallback(async () => {
|
||||
try {
|
||||
const data = await fetchSessions();
|
||||
setSessions(data);
|
||||
} catch {
|
||||
// non-critical — sidebar just won't update
|
||||
}
|
||||
}, []);
|
||||
|
||||
|
||||
// Append a message to the current thread (used by useChat)
|
||||
const appendMessage = useCallback((message) => {
|
||||
|
||||
Reference in New Issue
Block a user