From 76da6aed0e54b0ab6bef32028a51f46a2cdce2e0 Mon Sep 17 00:00:00 2001 From: YasinOnm08 Date: Mon, 7 Oct 2024 16:41:31 +0200 Subject: [PATCH] start chatHistory --- app/backend/ChatHistory.ts | 29 +++++++++--------- app/backend/InputOutputHandler.tsx | 15 +++++++++- app/backend/database.ts | 44 +++++++++++++++++++++------- app/components/AI.tsx | 8 +++-- app/components/History.tsx | 16 ++++++++-- app/components/Login.tsx | 4 +-- app/components/settings/Settings.tsx | 6 ++-- app/page.tsx | 5 ++-- 8 files changed, 89 insertions(+), 38 deletions(-) diff --git a/app/backend/ChatHistory.ts b/app/backend/ChatHistory.ts index 918d2da..b7b28c1 100644 --- a/app/backend/ChatHistory.ts +++ b/app/backend/ChatHistory.ts @@ -1,23 +1,25 @@ -/* type ChatMessage = { + +type Message = { + role: string; + content:string +} + +type Chat = { name: string; - messages: any; + messages: Message[]; timestamp: number; }; -let chatHistory: ChatMessage[] = []; +export let chatHistory: Chat[] = []; -function addMessageToHistory(name: string, message: any): void { - const newMessage: ChatMessage = { - name: name, - messages: message, - timestamp: Date.now() - }; - chatHistory.push(newMessage); - console.log(`Added message from ${name}: ${message}`); - chatHistory.sort((a,b) => b.timestamp - a.timestamp) +export function addMessageToHistory(index: number, chat: Chat): void { + if (index >= 0 && index < chatHistory.length) { + chatHistory[index] = chat; + chatHistory.sort((a, b) => b.timestamp - a.timestamp) + } } -function removeMessageFromHistory(timestamp: number): void { +export function removeMessageFromHistory(timestamp: number): void { const index = chatHistory.findIndex((msg) => msg.timestamp === timestamp); if (index > -1) { chatHistory.splice(index, 1); @@ -26,4 +28,3 @@ function removeMessageFromHistory(timestamp: number): void { console.log(`Message not found with timestamp: ${timestamp}`); } } - */ \ No newline at end of file diff --git a/app/backend/InputOutputHandler.tsx b/app/backend/InputOutputHandler.tsx index 64fc4f1..dffc1bf 100644 --- a/app/backend/InputOutputHandler.tsx +++ b/app/backend/InputOutputHandler.tsx @@ -4,14 +4,26 @@ import ConversationFrontend from '../components/ConversationFrontend'; import InputFrontend from "../components/InputFrontend"; import { sendToVoiceRecognition } from "./voice_backend" import axios from "axios"; +import { changeHistory, checkCredentials, getHistory } from './database'; +import { addMessageToHistory, removeMessageFromHistory } from "./ChatHistory"; -const InputOutputBackend: React.FC = () => { +interface InputOutputHandlerProps { + selectedIndex: number; +} + +const InputOutputBackend: React.FC = ({selectedIndex}) => { // # variables type Message = { role: string content: string } + type Chat = { + name?: string + messages: Message[] + timestamp: string + } + // Define state variables for user preferences and messages const [preferredCurrency, setPreferredCurrency] = useState("USD"); const [preferredLanguage, setPreferredLanguage] = useState("english"); @@ -242,6 +254,7 @@ const InputOutputBackend: React.FC = () => { const handleStopClick = () => { endGetWorker() getNewToken() + setInputDisabled(false) } const handleResendClick = () => { diff --git a/app/backend/database.ts b/app/backend/database.ts index 7ef1870..c0cd51b 100644 --- a/app/backend/database.ts +++ b/app/backend/database.ts @@ -75,7 +75,7 @@ export const changePassword = async (usernameOrEmail: string, password: string, return await sendToDatabase(data); }; -export const getData = async (usernameOrEmail: string, password: string) => { +export const getSettings = async (usernameOrEmail: string, password: string) => { const data = { action: "get_settings", username: usernameOrEmail.includes('@') ? undefined : usernameOrEmail, @@ -85,6 +85,38 @@ export const getData = async (usernameOrEmail: string, password: string) => { return await sendToDatabaseAndGetString(data); }; +export const changeSettings = async (usernameOrEmail: string, password: string, newData: object) => { + const data = { + action: "change_settings", + username: usernameOrEmail.includes('@') ? undefined : usernameOrEmail, + email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined, + password, + data: newData, + }; + return await sendToDatabase(data); +}; + +export const getHistory = async (usernameOrEmail: string, password: string) => { + const data = { + action: "get_history", + username: usernameOrEmail.includes('@') ? undefined : usernameOrEmail, + email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined, + password, + }; + return await sendToDatabaseAndGetString(data); +}; + +export const changeHistory = async (usernameOrEmail: string, password: string, newData: object) => { + const data = { + action: "change_history", + username: usernameOrEmail.includes('@') ? undefined : usernameOrEmail, + email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined, + password, + data: newData, + }; + return await sendToDatabase(data); +}; + export const getEmail = async (usernameOrEmail: string, password: string): Promise => { const data = { action: "get_email", @@ -105,16 +137,6 @@ export const getName = async (usernameOrEmail: string, password: string): Promis return await sendToDatabaseAndGetString(data); }; -export const changeData = async (usernameOrEmail: string, password: string, newData: object) => { - const data = { - action: "change_settings", - username: usernameOrEmail.includes('@') ? undefined : usernameOrEmail, - email: usernameOrEmail.includes('@') ? usernameOrEmail : undefined, - password, - data: newData, - }; - return await sendToDatabase(data); -}; export const checkCredentials = async (usernameOrEmail: string, password: string) => { const data = { diff --git a/app/components/AI.tsx b/app/components/AI.tsx index 824f6d6..a4db7fa 100644 --- a/app/components/AI.tsx +++ b/app/components/AI.tsx @@ -2,10 +2,14 @@ import React from 'react'; import InputOutputBackend from '../backend/InputOutputHandler'; -const AI: React.FC = () => { +interface AIProps{ + selectedIndex:number +} + +const AI: React.FC = ({selectedIndex}) => { return (
- +
); }; diff --git a/app/components/History.tsx b/app/components/History.tsx index cd93950..82c1f65 100644 --- a/app/components/History.tsx +++ b/app/components/History.tsx @@ -1,6 +1,16 @@ -import React from 'react'; +import React, { useState } from 'react'; + +interface HistoryProps{ + selectedIndex: number; + setSelectedIndex: (index: number) => void; +} + +const History: React.FC = ({selectedIndex, setSelectedIndex}) => { + + const handleHistoryClick = (index: number) => { + setSelectedIndex(index) + } -const History: React.FC = () => { return (
@@ -8,7 +18,7 @@ const History: React.FC = () => { {/* Populate with history items */} {Array.from({ length: 20 }, (_, index) => (
  • - history{index + 1} + handleHistoryClick(index)}>history{index + 1}
  • ))} diff --git a/app/components/Login.tsx b/app/components/Login.tsx index 1660f39..a2c0f7f 100644 --- a/app/components/Login.tsx +++ b/app/components/Login.tsx @@ -2,7 +2,7 @@ import React, { useState, useEffect } from 'react'; import { createAccount, checkCredentials, - getData + getSettings } from '../backend/database'; import Settings from './settings/Settings'; // Import the Settings component @@ -62,7 +62,7 @@ const Login: React.FC = () => { const success = await checkCredentials(accountName, password); if (success) { setIsLoggedIn(true); // Successful login - const data = await getData(accountName, password) + const data = await getSettings(accountName, password) if (data) { if (typeof localStorage !== 'undefined') { localStorage.setItem("dataFromServer", data) diff --git a/app/components/settings/Settings.tsx b/app/components/settings/Settings.tsx index 874ebe1..d3989cb 100644 --- a/app/components/settings/Settings.tsx +++ b/app/components/settings/Settings.tsx @@ -11,7 +11,7 @@ import PrivacySettings from './PrivacySettings'; import FontSizeSetting from './FontSize'; import OpenSourceModeToggle from './OpenSourceToggle'; import { - changeData, + changeSettings, createAccount, deleteAccount, } from '../../backend/database'; @@ -351,7 +351,7 @@ const Settings: React.FC<{ closeSettings: () => void; accountName: string }> = ( } if (await createAccount(useName, useEmail, usePassword)) { - if (await changeData(useName, usePassword, settings)) { + if (await changeSettings(useName, usePassword, settings)) { localStorage.setItem("currentName", useName) localStorage.setItem("currentPassword", usePassword) localStorage.setItem("currentEmail", useEmail) @@ -729,7 +729,7 @@ const Settings: React.FC<{ closeSettings: () => void; accountName: string }> = (