Skip to content

Commit

Permalink
Re-render the conversation component when a new message is posted
Browse files Browse the repository at this point in the history
  • Loading branch information
lasryaric committed Sep 29, 2023
1 parent 114cb6f commit 5f02dd8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
15 changes: 11 additions & 4 deletions front/components/assistant/conversation/Conversation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import { WorkspaceType } from "@app/types/user";
export default function Conversation({
owner,
conversationId,
lastMessageSentTs,
}: {
owner: WorkspaceType;
conversationId: string;
lastMessageSentTs?: number;
}) {
const {
conversation,
Expand All @@ -37,8 +39,11 @@ export default function Conversation({
});

const lastConversationTs = (() => {
const allTs = conversation?.content.flat().map((m) => m.created);
if (!allTs) {
const allTs = conversation?.content.flat().map((m) => m.created) || [];
if (lastMessageSentTs) {
allTs.push(lastMessageSentTs);
}
if (allTs.length === 0) {
return null;
}
return Math.max(...allTs);
Expand All @@ -55,9 +60,11 @@ export default function Conversation({
if (!lastConversationTs) {
return null;
}
if (lastConversationTs + 60 * 1000 * 10 < Date.now()) {

if (lastConversationTs + 60 * 1000 < Date.now()) {
console.log(
"No need to poll for events if the last message is older than 10 minutes"
"No need to poll for events if the last message is older than 10 minutes",
lastConversationTs
);
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions front/hooks/useEventSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ export function useEventSource(
};

es.onerror = (event) => {
console.error('useEventSource.onerror()', event)
console.error("useEventSource.onerror()", event);
errorCount.current += 1;
if (errorCount.current >= 3) {
console.log('too many errors, not reconnecting..')
console.log("too many errors, not reconnecting..");
setIsError(new Error("Too many errors, closing connection."));
es.close();
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async function handler(
}
res.write("data: done\n\n");
// @ts-expect-error - We need it for streaming but it does not exists in the types.
res.flush();
res.flush();

res.end();
return;
Expand Down
11 changes: 10 additions & 1 deletion front/pages/w/[wId]/assistant/[cId]/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { GetServerSideProps, InferGetServerSidePropsType } from "next";
import { useRouter } from "next/router";
import { useState } from "react";

import Conversation from "@app/components/assistant/conversation/Conversation";
import { ConversationTitle } from "@app/components/assistant/conversation/ConversationTitle";
Expand Down Expand Up @@ -55,6 +56,9 @@ export default function AssistantConversation({
conversationId,
}: InferGetServerSidePropsType<typeof getServerSideProps>) {
const router = useRouter();
const [lastMessageSentTs, setLastMessageSentTs] = useState<
number | undefined
>(undefined);

const handleSubmit = async (input: string, mentions: MentionType[]) => {
// Create a new user message.
Expand All @@ -81,6 +85,7 @@ export default function AssistantConversation({
window.alert(`Error creating message: ${data.error.message}`);
return;
}
setLastMessageSentTs(new Date().getTime());
};

const handdleDeleteConversation = async () => {
Expand Down Expand Up @@ -121,7 +126,11 @@ export default function AssistantConversation({
<AssistantSidebarMenu owner={owner} triggerInputAnimation={null} />
}
>
<Conversation owner={owner} conversationId={conversationId} />
<Conversation
owner={owner}
conversationId={conversationId}
lastMessageSentTs={lastMessageSentTs}
/>
<FixedAssistantInputBar owner={owner} onSubmit={handleSubmit} />
</AppLayout>
);
Expand Down

0 comments on commit 5f02dd8

Please sign in to comment.