diff --git a/web/hooks/useSendChatMessage.ts b/web/hooks/useSendChatMessage.ts index 9cf61969de..7a1e1dea14 100644 --- a/web/hooks/useSendChatMessage.ts +++ b/web/hooks/useSendChatMessage.ts @@ -48,6 +48,36 @@ export default function useSendChatMessage() { const { startModel } = useActiveModel() const [queuedMessage, setQueuedMessage] = useState(false) + const resendChatMessage = async () => { + if (!activeThread) { + console.error('No active thread') + return + } + + updateThreadWaiting(activeThread.id, true) + + const messageRequest: MessageRequest = { + id: ulid(), + messages: currentMessages.slice(0, -1).map((e) => { + const msg: ChatCompletionMessage = { + role: e.role, + content: e.content[0].text.value, + } + return msg + }), + threadId: activeThread.id, + } + + const modelId = selectedModel?.id ?? activeThread.assistants[0].model.id + + if (activeModel?.id !== modelId) { + setQueuedMessage(true) + await startModel(modelId) + setQueuedMessage(false) + } + events.emit(EventName.OnMessageSent, messageRequest) + } + const sendChatMessage = async () => { if (!currentPrompt || currentPrompt.trim().length === 0) { return @@ -162,6 +192,7 @@ export default function useSendChatMessage() { return { sendChatMessage, + resendChatMessage, queuedMessage, } } diff --git a/web/screens/Chat/MessageToolbar/index.tsx b/web/screens/Chat/MessageToolbar/index.tsx index 5380c7e292..a0929336c7 100644 --- a/web/screens/Chat/MessageToolbar/index.tsx +++ b/web/screens/Chat/MessageToolbar/index.tsx @@ -1,7 +1,5 @@ import { - ChatCompletionMessage, EventName, - MessageRequest, MessageStatus, ExtensionType, ThreadMessage, @@ -21,17 +19,15 @@ import { getCurrentChatMessagesAtom, } from '@/helpers/atoms/ChatMessage.atom' import { activeThreadAtom } from '@/helpers/atoms/Conversation.atom' +import useSendChatMessage from '@/hooks/useSendChatMessage' const MessageToolbar = ({ message }: { message: ThreadMessage }) => { const deleteMessage = useSetAtom(deleteMessageAtom) const thread = useAtomValue(activeThreadAtom) const messages = useAtomValue(getCurrentChatMessagesAtom) - // const threadStateAtom = useMemo( - // () => atom((get) => get(threadStatesAtom)[thread?.id ?? '']), - // [thread?.id] - // ) - // const threadState = useAtomValue(threadStateAtom) - const stopInference = async () => { + const { resendChatMessage } = useSendChatMessage() + + const onStopInferenceClick = async () => { await extensionManager .get(ExtensionType.Inference) ?.stopInference() @@ -43,13 +39,25 @@ const MessageToolbar = ({ message }: { message: ThreadMessage }) => { }, 300) } + const onDeleteClick = async () => { + deleteMessage(message.id ?? '') + if (thread) { + await extensionManager + .get(ExtensionType.Conversational) + ?.writeMessages( + thread.id, + messages.filter((msg) => msg.id !== message.id) + ) + } + } + return (
{message.status === MessageStatus.Pending && (
stopInference()} + onClick={onStopInferenceClick} >
@@ -58,20 +66,7 @@ const MessageToolbar = ({ message }: { message: ThreadMessage }) => { message.id === messages[messages.length - 1]?.id && (
{ - const messageRequest: MessageRequest = { - id: message.id ?? '', - messages: messages.slice(0, -1).map((e) => { - const msg: ChatCompletionMessage = { - role: e.role, - content: e.content[0].text.value, - } - return msg - }), - threadId: message.thread_id ?? '', - } - events.emit(EventName.OnMessageSent, messageRequest) - }} + onClick={resendChatMessage} >
@@ -89,16 +84,7 @@ const MessageToolbar = ({ message }: { message: ThreadMessage }) => {
{ - deleteMessage(message.id ?? '') - if (thread) - await extensionManager - .get(ExtensionType.Conversational) - ?.writeMessages( - thread.id, - messages.filter((msg) => msg.id !== message.id) - ) - }} + onClick={onDeleteClick} >