Add a unique_id field for identifiying conversations (#914)

* Add a unique_id field to the conversation object

- This helps us keep track of the unique identity of the conversation without expose the internal id
- Create three staged migrations in order to first add the field, then add unique values to pre-fill, and then set the unique constraint. Without this, it tries to initialize all the existing conversations with the same ID.

* Parse and utilize the unique_id field in the query parameters of the front-end view

- Handle the unique_id field when creating a new conversation from the home page
- Parse the id field with a lightweight parameter called v in the chat page
- Share page should not be affected, as it uses the public slug

* Fix suggested card category
This commit is contained in:
sabaimran
2024-09-16 12:19:16 -07:00
committed by GitHub
parent e6bc7a2ba2
commit ece2ec2d90
11 changed files with 169 additions and 20 deletions

View File

@@ -32,7 +32,8 @@ interface ChatBodyDataProps {
function ChatBodyData(props: ChatBodyDataProps) {
const searchParams = useSearchParams();
const conversationId = searchParams.get("conversationId");
const conversationUniqueId = searchParams.get("v");
const [conversationId, setConversationId] = useState<string | null>("");
const [message, setMessage] = useState("");
const [image, setImage] = useState<string | null>(null);
const [processingMessage, setProcessingMessage] = useState(false);
@@ -60,6 +61,11 @@ function ChatBodyData(props: ChatBodyDataProps) {
setProcessingMessage(true);
setQueryToProcess(storedMessage);
}
const conversationId = localStorage.getItem("conversationId");
if (conversationId) {
setConversationId(conversationId);
}
}, [setQueryToProcess]);
useEffect(() => {
@@ -69,6 +75,30 @@ function ChatBodyData(props: ChatBodyDataProps) {
}
}, [message, setQueryToProcess]);
useEffect(() => {
if (!conversationUniqueId) {
return;
}
fetch(
`/api/chat/metadata?conversation_unique_id=${encodeURIComponent(conversationUniqueId)}`,
)
.then((response) => {
if (!response.ok) {
throw new Error(response.statusText);
}
return response.json();
})
.then((data) => {
setConversationId(data.conversationId);
})
.catch((err) => {
console.error(err);
setConversationId(null);
return;
});
});
useEffect(() => {
if (conversationId) {
onConversationIdChange?.(conversationId);
@@ -87,11 +117,15 @@ function ChatBodyData(props: ChatBodyDataProps) {
}
}, [props.streamedMessages]);
if (!conversationId) {
if (!conversationUniqueId || conversationId === null) {
window.location.href = "/";
return;
}
if (!conversationId) {
return <Loading />;
}
return (
<>
<div className={false ? styles.chatBody : styles.chatBodyFull}>