From 1f3d8bec75ef583a83aef7d08c2691f1e9b5a083 Mon Sep 17 00:00:00 2001 From: Haidong Xu Date: Fri, 20 Dec 2024 20:13:43 +0100 Subject: [PATCH] feat: use uuid to create id for chat --- src/components/Chatbot/Chatbot.tsx | 6 ++++-- src/domain/contexts/ChatbotContext.tsx | 5 +++-- src/domain/entities/chatbot/BackendCommunication.ts | 1 + src/domain/entities/chatbot/Chatbot.ts | 2 +- src/infrastructure/repositories/ChatbotRepositoryImpl.ts | 3 +++ src/operations/chatbot/Chatbot.ts | 4 +++- 6 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/components/Chatbot/Chatbot.tsx b/src/components/Chatbot/Chatbot.tsx index c4775c17..b887c542 100644 --- a/src/components/Chatbot/Chatbot.tsx +++ b/src/components/Chatbot/Chatbot.tsx @@ -99,20 +99,22 @@ export default function HungerMapChatbot() { if (chats[currentChatIndex].isReportStarter && chats[currentChatIndex].context) { aiResponse = ( await chatbot.sendMessage(text, { + chatId: chats[currentChatIndex].id, previous_messages: previousMessages, context: chats[currentChatIndex].context, }) ).response; chats[currentChatIndex].isReportStarter = false; } else { - aiResponse = (await chatbot.sendMessage(text, { previous_messages: previousMessages })).response; + aiResponse = ( + await chatbot.sendMessage(text, { chatId: chats[currentChatIndex].id, previous_messages: previousMessages }) + ).response; } } catch (err) { if (err instanceof APIError) { aiResponse = `Ups! Unfortunately, it seems like there was a problem connecting to the server...\n ${err.status}: ${err.message}`; } } - // TODO: get data sources from response later const updatedChatsWithAI = structuredClone(chats); updatedChatsWithAI[currentChatIndex].messages.push({ id: crypto.randomUUID(), diff --git a/src/domain/contexts/ChatbotContext.tsx b/src/domain/contexts/ChatbotContext.tsx index 082d327b..dd41c870 100644 --- a/src/domain/contexts/ChatbotContext.tsx +++ b/src/domain/contexts/ChatbotContext.tsx @@ -1,4 +1,5 @@ import React, { createContext, ReactNode, useContext, useEffect, useMemo, useState } from 'react'; +import { v4 as uuid } from 'uuid'; import { useSnackbar } from '@/domain/contexts/SnackbarContext'; import { IChat } from '@/domain/entities/chatbot/Chatbot'; @@ -53,7 +54,7 @@ export function ChatbotProvider({ children }: { children: ReactNode }) { if (!newChat) { chatToAdd = { - id: chats.length + 1, + id: uuid(), title: `Chat ${chats.length + 1}`, messages: [], isTyping: false, @@ -111,7 +112,7 @@ export function ChatbotProvider({ children }: { children: ReactNode }) { }; const newChat: IChat = { - id: chats.length + 1, + id: uuid(), title: `Report ${countryName}`, context: reportText, isReportStarter: true, diff --git a/src/domain/entities/chatbot/BackendCommunication.ts b/src/domain/entities/chatbot/BackendCommunication.ts index f7278966..708a0780 100644 --- a/src/domain/entities/chatbot/BackendCommunication.ts +++ b/src/domain/entities/chatbot/BackendCommunication.ts @@ -1,6 +1,7 @@ import { IMessage } from './Chatbot'; export interface QueryRequest { + chatId: string; context?: string; query: string; version?: number; diff --git a/src/domain/entities/chatbot/Chatbot.ts b/src/domain/entities/chatbot/Chatbot.ts index 11ae657a..8acdce4a 100644 --- a/src/domain/entities/chatbot/Chatbot.ts +++ b/src/domain/entities/chatbot/Chatbot.ts @@ -8,7 +8,7 @@ export interface IMessage { } export interface IChat { - id: number; + id: string; title: string; context?: string; isReportStarter?: boolean; diff --git a/src/infrastructure/repositories/ChatbotRepositoryImpl.ts b/src/infrastructure/repositories/ChatbotRepositoryImpl.ts index 7e6dfc58..18f19bc9 100644 --- a/src/infrastructure/repositories/ChatbotRepositoryImpl.ts +++ b/src/infrastructure/repositories/ChatbotRepositoryImpl.ts @@ -1,3 +1,5 @@ +import { v4 as uuid } from 'uuid'; + import { APIError, QueryRequest, QueryResponse } from '@/domain/entities/chatbot/BackendCommunication.ts'; import ChatbotRepository from '@/domain/repositories/ChatbotRepository'; @@ -14,6 +16,7 @@ export default class ChatbotRepositoryImpl implements ChatbotRepository { async sendMessage(message: string, options: Partial>): Promise { try { const payload: QueryRequest = { + chatId: options.chatId || uuid(), context: options.context || '', query: message, version: options.version || 1, diff --git a/src/operations/chatbot/Chatbot.ts b/src/operations/chatbot/Chatbot.ts index 4403e89f..356fc358 100644 --- a/src/operations/chatbot/Chatbot.ts +++ b/src/operations/chatbot/Chatbot.ts @@ -1,3 +1,5 @@ +import { v4 as uuid } from 'uuid'; + import { IChat } from '@/domain/entities/chatbot/Chatbot'; import { SNACKBAR_SHORT_DURATION } from '@/domain/entities/snackbar/Snackbar'; import { SenderRole } from '@/domain/enums/SenderRole'; @@ -45,7 +47,7 @@ export default class ChatbotOperations { // Return default chat if no recent chats are found return [ { - id: 1, + id: uuid(), title: 'Chat 1', messages: [], isTyping: false,