Skip to content

Commit

Permalink
finished multiple stream. Now need to save the messages to db onStart…
Browse files Browse the repository at this point in the history
… and onCompletion.
  • Loading branch information
dokmy committed Jan 9, 2024
1 parent e8b28c5 commit cbe8349
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 50 deletions.
15 changes: 0 additions & 15 deletions src/app/(root)/(routes)/results/[searchId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,6 @@ const resultsPage = async ({ params: { searchId } }: Props) => {
},
});

const chatInitiated = async (searchResult: SearchResult) => {
const dbMessages = await prismadb.message.findMany({
where: {
searchResultId: searchResult.id,
},
});

if (dbMessages.length === 0) {
return false;
} else {
return true;
}
};

return (
<div className="flex overflow-x-auto h-screen">
{searchResults.map((result) => (
Expand All @@ -64,7 +50,6 @@ const resultsPage = async ({ params: { searchId } }: Props) => {
key={result.id}
data={result}
query={search_metadata[0].query}
chatInitiated={() => chatInitiated(result)}
/>
</div>
))}
Expand Down
82 changes: 47 additions & 35 deletions src/app/(root)/(routes)/results/components/chat-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,52 +22,64 @@ interface ChatComponentProps {
}

const ChatComponent: React.FC<ChatComponentProps> = ({ data, query }) => {
// const [dbMessages, setDbMessages] = useState([]);
// const [chatArgs, setChatArgs] = useState({});
const [dbMessages, setDbMessages] = useState([]);
const [chatArgs, setChatArgs] = useState({});

// useEffect(() => {
// const fetchDbMessages = async () => {
// try {
// const response = await axios.post(`/api/get-messages`, {
// searchResultId: data.id,
// });
// setDbMessages(response.data);
// } catch (error) {
// console.error("Error fetching messages:", error);
// }
// };
useEffect(() => {
const fetchDbMessages = async () => {
try {
const response = await axios.post(`/api/get-messages`, {
searchResultId: data.id,
});
setDbMessages(response.data);
} catch (error) {
console.error("Error fetching messages:", error);
}
};

// fetchDbMessages();
// }, [data.id]);
fetchDbMessages();
}, [data.id]);

// useEffect(() => {
// if (dbMessages.length === 0) {
// console.log("no messages. Adding initial input.");
// setChatArgs({
// initialInput:
// "Please first summarise this case for me and then explain why this case is relevant to my situation as follow: " +
// query,
// });
// console.log(chatArgs);
// } else {
// console.log("Have messages. Adding initial messages.");
// const simplifiedMessages = dbMessages.map(({ role, content }) => ({
// role,
// content,
// }));
// setChatArgs({ initialMessages: simplifiedMessages });
// console.log(chatArgs);
// }
// }, [dbMessages, query]);
useEffect(() => {
if (dbMessages.length === 0) {
console.log("no messages. Adding initial input.");
setChatArgs({
initialInput:
"Please first summarise this case for me and then explain why this case is relevant to my situation as follow: " +
query,
});
console.log(chatArgs);
} else {
console.log("Have messages. Adding initial messages.");
const simplifiedMessages = dbMessages.map(({ role, content }) => ({
role,
content,
}));
setChatArgs({ initialMessages: simplifiedMessages });
console.log(chatArgs);
}
}, [dbMessages, query]);

useEffect(() => {
const mockEvent = {
preventDefault: () => {},
// Add other minimal properties and methods if needed
} as unknown as React.FormEvent<HTMLFormElement>;
handleSubmit(mockEvent);
console.log("haha");
}, [query]);

console.log(chatArgs);

// console.log(chatArgs);
const { messages, input, handleInputChange, handleSubmit } = useChat({
body: { filter: data.caseNeutralCit },
initialInput:
"Please first summarise this case for me and then explain why this case is relevant to my siutation as follow: " +
query,
});

if (Object.keys(chatArgs).length == 0) return <div>Loading...</div>;

return (
<div>
<ResultCard data={data} />
Expand Down

0 comments on commit cbe8349

Please sign in to comment.