Skip to content

Commit

Permalink
add edit message functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ericcccsliu committed Mar 31, 2024
1 parent 9ff22d9 commit 7e307ca
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 1 deletion.
Binary file modified api/__pycache__/index.cpython-311.pyc
Binary file not shown.
2 changes: 2 additions & 0 deletions api/index.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#api/index.py

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from starlette.middleware.sessions import SessionMiddleware
Expand Down
Binary file modified api/models/__pycache__/conversation.cpython-311.pyc
Binary file not shown.
Binary file modified api/models/__pycache__/user.cpython-311.pyc
Binary file not shown.
2 changes: 2 additions & 0 deletions api/models/conversation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# api/models/conversation.py

from datetime import datetime
from typing import List
from pydantic import BaseModel, Field
Expand Down
1 change: 1 addition & 0 deletions api/models/user.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#api/models/user.py
from pydantic import BaseModel

class User(BaseModel):
Expand Down
Binary file modified api/routes/__pycache__/conversation_routes.cpython-311.pyc
Binary file not shown.
2 changes: 2 additions & 0 deletions api/routes/conversation_routes.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#api/routes/conversation_routes.py

from typing import List
from fastapi import APIRouter, Depends, HTTPException
from fastapi.responses import StreamingResponse
Expand Down
Binary file modified api/utils/__pycache__/conversation_utils.cpython-311.pyc
Binary file not shown.
2 changes: 2 additions & 0 deletions api/utils/conversation_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# api/utils/conversation_utils.py

from datetime import datetime, timezone
from typing import List

Expand Down
2 changes: 1 addition & 1 deletion app/components/AutoResizeTextArea.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// components/AutoResizeTextarea.tsx
// app/components/AutoResizeTextarea.tsx
import React, { useEffect, useRef } from "react";
import { Textarea, TextareaProps } from "@chakra-ui/react";

Expand Down
16 changes: 16 additions & 0 deletions app/components/ConversationMessages.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//app/components/ConversationMessages.tsx

import React, { useEffect, useRef } from "react";
import { Box, Text, VStack, Button, Center, Heading } from "@chakra-ui/react";
import ReactMarkdown from "react-markdown";
Expand All @@ -19,6 +21,7 @@ interface ConversationMessagesProps {
messages: Message[];
handleHideMessage: (index: number) => Promise<void>;
handleDeleteMessage: (index: number) => Promise<void>;
handleEditMessage: (index: number) => Promise<void>;
userName?: string;
}

Expand All @@ -32,6 +35,7 @@ const ConversationMessages: React.FC<ConversationMessagesProps> = ({
messages,
handleHideMessage,
handleDeleteMessage,
handleEditMessage,
userName = "User",
}) => {
const messagesEndRef = useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -157,6 +161,18 @@ const ConversationMessages: React.FC<ConversationMessagesProps> = ({
>
delete
</Button>
{message.role === "user" && (
<Button
size="xs"
variant="ghost"
onClick={() => handleEditMessage(index)}
mt={4}
px={1}
color="gray.600"
>
edit
</Button>
)}
</Box>
))
)}
Expand Down
2 changes: 2 additions & 0 deletions app/components/Sidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//app/components/Sidebar.tsx

import { useState } from "react";
import {
Box,
Expand Down
17 changes: 17 additions & 0 deletions app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"use client";

// app/page.tsx
import { useEffect, useState } from "react";
import { useRouter } from "next/navigation";
import { useSearchParams } from "next/navigation";
Expand Down Expand Up @@ -26,6 +28,7 @@ import {
fetchConversationById,
handleHideMessage,
handleDeleteMessage,
handleEditMessage,
} from "./utils";
import Link from "next/link";
import { jwtDecode, JwtPayload } from "jwt-decode";
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -199,6 +215,7 @@ export default function Home() {
messages={messages}
handleHideMessage={handleHideMessageWrapper}
handleDeleteMessage={handleDeleteMessageWrapper}
handleEditMessage={handleEditMessageWrapper}
userName={clientUserName}
/>
</Box>
Expand Down
33 changes: 33 additions & 0 deletions app/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//app/utils.ts

import { Message } from "./components/ConversationMessages";
import { LLMProviders } from "./llm_providers";

Expand Down Expand Up @@ -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<React.SetStateAction<Message[]>>,
setTextValue: React.Dispatch<React.SetStateAction<string>>,
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);
}
};

0 comments on commit 7e307ca

Please sign in to comment.