Skip to content

Commit

Permalink
fix: 🐛 unecessary agent conversation loader
Browse files Browse the repository at this point in the history
  • Loading branch information
gmpetrov committed Nov 4, 2023
1 parent b1188e0 commit 0e04d6b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 53 deletions.
85 changes: 42 additions & 43 deletions apps/dashboard/hooks/useChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,53 +91,30 @@ const useChat = ({ endpoint, channel, queryBody, ...otherProps }: Props) => {

const getConversationQuery = useSWRInfinite<
Prisma.PromiseReturnType<typeof getConversation>
>(
(pageIndex, previousPageData) => {
if (!state.conversationId) {
if (state.hasMoreMessages) {
setState({ hasMoreMessages: false });
}

return null;
}

if (previousPageData && previousPageData?.messages?.length === 0) {
setState({
hasMoreMessages: false,
});
return null;
>((pageIndex, previousPageData) => {
if (!state.conversationId) {
if (state.hasMoreMessages) {
setState({ hasMoreMessages: false });
}

const cursor = previousPageData?.messages?.[
previousPageData?.messages?.length - 1
]?.id as string;
return null;
}

return `${API_URL}/api/conversations/${state.conversationId}?cursor=${
cursor || ''
}`;
},
fetcher,
{
onSuccess: (data) => {
setState({
history: data
?.filter((each) => !!each)
?.map((each) => each?.messages)
?.flat()
?.reverse()
?.map((message) => ({
id: message?.id!,
eval: message?.eval,
from: message?.from!,
message: message?.text!,
createdAt: message?.createdAt!,
sources: message?.sources as Source[],
})),
conversationStatus: data[0]?.status ?? state.conversationStatus,
});
},
if (previousPageData && previousPageData?.messages?.length === 0) {
setState({
hasMoreMessages: false,
});
return null;
}
);

const cursor = previousPageData?.messages?.[
previousPageData?.messages?.length - 1
]?.id as string;

return `${API_URL}/api/conversations/${state.conversationId}?cursor=${
cursor || ''
}`;
}, fetcher);

const handleLoadMoreMessages = useCallback(() => {
if (getConversationQuery.isLoading || getConversationQuery.isValidating)
Expand Down Expand Up @@ -420,6 +397,28 @@ const useChat = ({ endpoint, channel, queryBody, ...otherProps }: Props) => {
[setState]
);

useEffect(() => {
if (getConversationQuery.data) {
setState({
history: getConversationQuery.data
?.filter((each) => !!each)
?.map((each) => each?.messages)
?.flat()
?.reverse()
?.map((message) => ({
id: message?.id!,
eval: message?.eval,
from: message?.from!,
message: message?.text!,
createdAt: message?.createdAt!,
sources: message?.sources as Source[],
})),
conversationStatus:
getConversationQuery.data[0]?.status ?? state.conversationStatus,
});
}
}, [getConversationQuery.data]);

// useSWRInfinite needs to be retriggered!
useEffect(() => {
if (state.conversationId) {
Expand Down
6 changes: 1 addition & 5 deletions apps/dashboard/pages/agents/[agentId]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,10 @@ export default function AgentPage() {
history,
handleChatSubmit,
isLoadingConversation,
isValidatingConversation,
hasMoreMessages,
handleLoadMoreMessages,
setConversationId,
conversationId,
conversationStatus,
handleEvalAnswer,
handleAbort,
} = useChat({
Expand Down Expand Up @@ -304,9 +302,7 @@ export default function AgentPage() {
messages={history}
onSubmit={handleChatSubmit}
agentIconUrl={query?.data?.iconUrl!}
isLoadingConversation={
isLoadingConversation || isValidatingConversation
}
isLoadingConversation={isLoadingConversation}
hasMoreMessages={hasMoreMessages}
handleLoadMoreMessages={handleLoadMoreMessages}
handleEvalAnswer={handleEvalAnswer}
Expand Down
5 changes: 1 addition & 4 deletions apps/dashboard/pages/chat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ export default function ChatPage() {
history,
handleChatSubmit,
isLoadingConversation,
isValidatingConversation,
hasMoreMessages,
handleLoadMoreMessages,
setConversationId,
Expand Down Expand Up @@ -345,9 +344,7 @@ export default function ChatPage() {
disableWatermark
messages={history}
onSubmit={handleChatSubmit}
isLoadingConversation={
isLoadingConversation || isValidatingConversation
}
isLoadingConversation={isLoadingConversation}
hasMoreMessages={hasMoreMessages}
handleLoadMoreMessages={handleLoadMoreMessages}
topSettings={Settings}
Expand Down
2 changes: 1 addition & 1 deletion packages/chat-bubble/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@databerry/chat-bubble",
"version": "1.0.94",
"version": "1.0.95",
"description": "Chaindesk Chat Bubble Widget",
"main": "chat-bubble.js",
"scripts": {},
Expand Down

0 comments on commit 0e04d6b

Please sign in to comment.