Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use uuid to create id for chat #153

Merged
merged 2 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/components/Chatbot/Chatbot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
5 changes: 3 additions & 2 deletions src/domain/contexts/ChatbotContext.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -111,7 +112,7 @@ export function ChatbotProvider({ children }: { children: ReactNode }) {
};

const newChat: IChat = {
id: chats.length + 1,
id: uuid(),
title: `Report ${reportName}`,
context: reportText,
isReportStarter: true,
Expand Down
1 change: 1 addition & 0 deletions src/domain/entities/chatbot/BackendCommunication.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IMessage } from './Chatbot';

export interface QueryRequest {
chatId: string;
context?: string;
query: string;
version?: number;
Expand Down
2 changes: 1 addition & 1 deletion src/domain/entities/chatbot/Chatbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface IMessage {
}

export interface IChat {
id: number;
id: string;
title: string;
context?: string;
isReportStarter?: boolean;
Expand Down
3 changes: 3 additions & 0 deletions src/infrastructure/repositories/ChatbotRepositoryImpl.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -14,6 +16,7 @@ export default class ChatbotRepositoryImpl implements ChatbotRepository {
async sendMessage(message: string, options: Partial<Omit<QueryRequest, 'query'>>): Promise<QueryResponse> {
try {
const payload: QueryRequest = {
chatId: options.chatId || uuid(),
context: options.context || '',
query: message,
version: options.version || 1,
Expand Down
4 changes: 3 additions & 1 deletion src/operations/chatbot/Chatbot.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
Expand Down
Loading