From 7e307ca39da0c1e307934969ab32e778e2fdc29b Mon Sep 17 00:00:00 2001 From: ericcccliu Date: Sat, 30 Mar 2024 23:04:13 -0500 Subject: [PATCH] add edit message functionality --- api/__pycache__/index.cpython-311.pyc | Bin 1948 -> 1950 bytes api/index.py | 2 ++ .../__pycache__/conversation.cpython-311.pyc | Bin 2101 -> 2103 bytes api/models/__pycache__/user.cpython-311.pyc | Bin 571 -> 571 bytes api/models/conversation.py | 2 ++ api/models/user.py | 1 + .../conversation_routes.cpython-311.pyc | Bin 6701 -> 6703 bytes api/routes/conversation_routes.py | 2 ++ .../conversation_utils.cpython-311.pyc | Bin 5928 -> 5930 bytes api/utils/conversation_utils.py | 2 ++ app/components/AutoResizeTextArea.tsx | 2 +- app/components/ConversationMessages.tsx | 16 +++++++++ app/components/Sidebar.tsx | 2 ++ app/page.tsx | 17 +++++++++ app/utils.ts | 33 ++++++++++++++++++ 15 files changed, 78 insertions(+), 1 deletion(-) diff --git a/api/__pycache__/index.cpython-311.pyc b/api/__pycache__/index.cpython-311.pyc index 3f0a288a189baccf1387d845f997127481d205eb..4b67a919d40485cd1f84ad596ce3a61a5873ed27 100644 GIT binary patch delta 55 zcmbQkKaZbxIWI340}zzlT0EKi8_W%F@ diff --git a/api/index.py b/api/index.py index 161418c..c8fa0ab 100644 --- a/api/index.py +++ b/api/index.py @@ -1,3 +1,5 @@ +#api/index.py + from fastapi import FastAPI from fastapi.middleware.cors import CORSMiddleware from starlette.middleware.sessions import SessionMiddleware diff --git a/api/models/__pycache__/conversation.cpython-311.pyc b/api/models/__pycache__/conversation.cpython-311.pyc index 0f722600a168223d6c5bfb1b26424ca1eb9416b1..94ed9dc18c97f3202160e6fdce0e286ff79ece83 100644 GIT binary patch delta 74 zcmdlguw8(6IWI340}zPbpk@qhnBj;pprdy1Bo1ZZ?GBOHp&S0@)WR#k`oOKeT d&}J((aYnwoK;<8p85tQrurV;oO^#*10RYvZ6A1tS delta 72 zcmdlkuvLI}IWI340}xzu{gzs}k@qhnBl~1-rdy0Wo1ZZ?GBOHo&S0@)WR#e^oOKeT bz-B8paYmloK;<8p85tRGFvw1hXTJde=~ENc diff --git a/api/models/__pycache__/user.cpython-311.pyc b/api/models/__pycache__/user.cpython-311.pyc index 1eee7dbe6a6addbdf17dac27d05016bb6d7e7868..d66d344168aafdbcfe7798bd732627f5a54d1eab 100644 GIT binary patch delta 34 ocmdnZvYUl>IWI340}$xkIWI340}wDx{F0ipkvEBvk$G|fV*}%j$sZZL0G+@J00000 diff --git a/api/models/conversation.py b/api/models/conversation.py index 5068716..365c05d 100644 --- a/api/models/conversation.py +++ b/api/models/conversation.py @@ -1,3 +1,5 @@ +# api/models/conversation.py + from datetime import datetime from typing import List from pydantic import BaseModel, Field diff --git a/api/models/user.py b/api/models/user.py index 8ea692f..47e8dcd 100644 --- a/api/models/user.py +++ b/api/models/user.py @@ -1,3 +1,4 @@ +#api/models/user.py from pydantic import BaseModel class User(BaseModel): diff --git a/api/routes/__pycache__/conversation_routes.cpython-311.pyc b/api/routes/__pycache__/conversation_routes.cpython-311.pyc index 94c29058a8933539603648c462ddc39d33b53286..248b36104d4173ef86c15f5522a2bb760de23aa5 100644 GIT binary patch delta 111 zcmZ2$vfhMmIWI340}!~~ltXoQ;i5vZXE9$Q#eX$hkR}MU07E5GYvO%`iEiQ)BZcwrxy|lAAL* z*D Promise; handleDeleteMessage: (index: number) => Promise; + handleEditMessage: (index: number) => Promise; userName?: string; } @@ -32,6 +35,7 @@ const ConversationMessages: React.FC = ({ messages, handleHideMessage, handleDeleteMessage, + handleEditMessage, userName = "User", }) => { const messagesEndRef = useRef(null); @@ -157,6 +161,18 @@ const ConversationMessages: React.FC = ({ > delete + {message.role === "user" && ( + + )} )) )} diff --git a/app/components/Sidebar.tsx b/app/components/Sidebar.tsx index 3ab0f77..96029a7 100644 --- a/app/components/Sidebar.tsx +++ b/app/components/Sidebar.tsx @@ -1,3 +1,5 @@ +//app/components/Sidebar.tsx + import { useState } from "react"; import { Box, diff --git a/app/page.tsx b/app/page.tsx index 14bc3ef..35147cc 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,4 +1,6 @@ "use client"; + +// app/page.tsx import { useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import { useSearchParams } from "next/navigation"; @@ -26,6 +28,7 @@ import { fetchConversationById, handleHideMessage, handleDeleteMessage, + handleEditMessage, } from "./utils"; import Link from "next/link"; import { jwtDecode, JwtPayload } from "jwt-decode"; @@ -82,6 +85,19 @@ export default function Home() { } }; + const handleEditMessageWrapper = async (index: number) => { + if (conversationId) { + await handleEditMessage( + index, + messages, + conversationId, + setMessages, + setTextValue, + cookies + ); + } + }; + useEffect(() => { const token = searchParams.get("token"); @@ -199,6 +215,7 @@ export default function Home() { messages={messages} handleHideMessage={handleHideMessageWrapper} handleDeleteMessage={handleDeleteMessageWrapper} + handleEditMessage={handleEditMessageWrapper} userName={clientUserName} /> diff --git a/app/utils.ts b/app/utils.ts index a9991e7..2f9a3bb 100644 --- a/app/utils.ts +++ b/app/utils.ts @@ -1,3 +1,5 @@ +//app/utils.ts + import { Message } from "./components/ConversationMessages"; import { LLMProviders } from "./llm_providers"; @@ -267,4 +269,35 @@ export const handleDeleteMessage = async ( } catch (error) { console.error("Error deleting message:", error); } +}; + + +export const handleEditMessage = async ( + index: number, + messages: Message[], + conversationId: string, + setMessages: React.Dispatch>, + setTextValue: React.Dispatch>, + cookies: { [key: string]: string } +) => { + try { + const updatedMessages = messages.slice(0, index); + await fetch( + `${process.env.BACKEND_URL}/conversations/${conversationId}/messages`, + { + method: "PUT", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${cookies.token}`, + }, + body: JSON.stringify(updatedMessages), + mode: "cors", + credentials: "include", + } + ); + setMessages(updatedMessages); + setTextValue(messages[index].content); + } catch (error) { + console.error("Error editing message:", error); + } }; \ No newline at end of file