-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move ChatSession behind pro version (Behind firebase remove config va…
…lue) (#206) * move ChatSession behind pro version * refactor example phases * fix issue with sending first message * add events and error capture
- Loading branch information
1 parent
a5dcb8b
commit 94eaa6f
Showing
7 changed files
with
206 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { useState } from 'react'; | ||
import { createChatSession, followUpChatSession, getChatSession } from '../network/chat_session'; | ||
import { sendClientError } from '../libs/Helpers'; | ||
|
||
const useChatSession = (initialMessages) => { | ||
const [messages, setMessages] = useState(initialMessages || []); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [isCreatingSession, setIsCreatingSession] = useState(false); | ||
const [error, setError] = useState(); | ||
|
||
const fetchNewMessages = async (chatSession, latestMessage) => { | ||
const interval = setInterval(async () => { | ||
try { | ||
const messageResponse = await getChatSession(chatSession.data.id, latestMessage.id); | ||
if (messageResponse.data.length > 0) { | ||
clearInterval(interval); | ||
setMessages((prevMessages) => [...prevMessages, ...messageResponse.data]); | ||
setIsLoading(false); | ||
} | ||
} catch (error) { | ||
console.error('Error fetching chat session updates', error); | ||
setError(error); | ||
sendClientError('fetch_new_message', error.message); | ||
clearInterval(interval); | ||
} | ||
}, 2000); | ||
}; | ||
|
||
const createSession = async (message) => { | ||
setIsLoading(true); | ||
setIsCreatingSession(true); | ||
setError(null); | ||
try { | ||
const chatSession = await createChatSession(message); | ||
const latestMessage = chatSession.data.messages[chatSession.data.messages.length - 1]; | ||
setMessages([...messages, latestMessage]); | ||
await fetchNewMessages(chatSession, latestMessage); | ||
return chatSession; | ||
} catch (error) { | ||
console.error('Error creating chat session', error); | ||
setError(error); | ||
sendClientError('create_session', error.message); | ||
throw error; | ||
} finally { | ||
setIsCreatingSession(false); | ||
} | ||
}; | ||
|
||
const followUpSession = async (sessionId, message) => { | ||
setIsLoading(true); | ||
setError(null); | ||
|
||
try { | ||
const chatSession = await followUpChatSession(sessionId, message); | ||
const latestMessage = chatSession.data.messages[chatSession.data.messages.length - 1]; | ||
setMessages((prevMessages) => [...prevMessages, latestMessage]); | ||
await fetchNewMessages(chatSession, latestMessage); | ||
return chatSession; | ||
} catch (error) { | ||
console.error('Error following up chat session', error); | ||
setError(error); | ||
sendClientError('followUpSession', error.message); | ||
throw error; | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
}; | ||
|
||
return { messages, isLoading, isCreatingSession, error, createSession, followUpSession }; | ||
}; | ||
|
||
export default useChatSession; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.