diff --git a/src/components/Writing-Assistant/WritingAssistantFloatingMenu.tsx b/src/components/Writing-Assistant/WritingAssistantFloatingMenu.tsx index aaa3a1ad..6afcfbde 100644 --- a/src/components/Writing-Assistant/WritingAssistantFloatingMenu.tsx +++ b/src/components/Writing-Assistant/WritingAssistantFloatingMenu.tsx @@ -31,11 +31,15 @@ const WritingAssistant: React.FC = ({ const [loadingResponse, setLoadingResponse] = useState(false); const [customPrompt, setCustomPrompt] = useState(""); const [isOptionsVisible, setIsOptionsVisible] = useState(false); + const [prevPrompt, setPrevPrompt] = useState(""); const markdownContainerRef = useRef(null); const optionsContainerRef = useRef(null); const hasValidMessages = currentChatHistory?.displayableChatHistory.some( - (msg) => msg.role !== "system" && msg.role !== "user" + (msg) => msg.role === "assistant" ); + const lastAssistantMessage = currentChatHistory?.displayableChatHistory + .filter((msg) => msg.role === "assistant") + .pop(); useOutsideClick(markdownContainerRef, () => { setCurrentChatHistory(undefined); @@ -49,7 +53,7 @@ const WritingAssistant: React.FC = ({ !currentChatHistory || currentChatHistory.displayableChatHistory.length === 0 ) { - console.error("No chat history available for replacement."); + console.error("No chat history available for copying."); return; } const llmResponse = @@ -90,18 +94,16 @@ const WritingAssistant: React.FC = ({ ? llmResponse.visibleContent : formatOpenAIMessageContentIntoString(llmResponse.content); - // Ensure the editor is focused editor.view.focus(); - // Move the selection to the end of the current selection const { from, to } = editor.state.selection; const endOfSelection = Math.max(from, to); editor .chain() .focus() - .setTextSelection(endOfSelection) // Set the cursor position to the end of the current selection - .insertContent("\n" + insertionText) // Insert the new content after the current selection + .setTextSelection(endOfSelection) + .insertContent("\n" + insertionText) .run(); }; @@ -248,10 +250,8 @@ Write a markdown list (using dashes) of key takeaways from my notes. Write at le ` ,apply the prompt to the text in triple quotes """ ${selectedText} """` || "default prompt"; break; - default: - prompt = "default prompt"; } - + setPrevPrompt(prompt); await getLLMResponse(prompt, currentChatHistory); }; @@ -378,30 +378,29 @@ Write a markdown list (using dashes) of key takeaways from my notes. Write at le width: "385px", }} > - {currentChatHistory?.displayableChatHistory - .filter((msg) => msg.role !== "system" && msg.role !== "user") - .map((message, index) => ( - - {message.visibleContent - ? message.visibleContent - : formatOpenAIMessageContentIntoString(message.content)} - - ))} + {lastAssistantMessage && ( + + {lastAssistantMessage.visibleContent + ? lastAssistantMessage.visibleContent + : formatOpenAIMessageContentIntoString( + lastAssistantMessage.content + )} + + )}