-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathuse-edit-message.ts
More file actions
104 lines (85 loc) · 2.94 KB
/
use-edit-message.ts
File metadata and controls
104 lines (85 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { useState } from 'react';
import { useForm } from 'react-hook-form';
import { FormValues } from '@open-webui-react-native/mobile/shared/utils/form';
import {
chatApi,
ChatResponse,
prepareCompleteChatPayload,
prepareUpdateMessageToSendPayload,
prepareEditAssistantMessagePayload,
prepareCopyEditedMessagePayload,
} from '@open-webui-react-native/shared/data-access/api';
import { Role } from '@open-webui-react-native/shared/data-access/common';
import { socketService } from '@open-webui-react-native/shared/data-access/websocket';
interface UseEditMessageProps {
chat?: ChatResponse;
modelId?: string;
}
export interface EditMessageSchema {
editMessageInputValue: string;
}
export const useEditMessage = ({ chat, modelId }: UseEditMessageProps): typeof result => {
const [editingMessageId, setEditingMessageId] = useState<string>();
const socketSessionId = socketService.socketSessionId;
const { control, handleSubmit, reset } = useForm<FormValues<EditMessageSchema>>({
defaultValues: {
editMessageInputValue: '',
},
});
const { mutateAsync: updateChat, isPending: isChatUpdating } = chatApi.useUpdate();
const { mutate: completeChat } = chatApi.useCompleteChat();
const startEditing = (messageId: string, content: string): void => {
setEditingMessageId(messageId);
reset({ editMessageInputValue: content });
};
const cancelEditing = (): void => {
setEditingMessageId(undefined);
reset({ editMessageInputValue: '' });
};
const saveMessage = async (message: string): Promise<void> => {
if (!chat?.chat || !editingMessageId) {
return;
}
const preparedChat = prepareCopyEditedMessagePayload(chat, editingMessageId, message);
await updateChat(preparedChat);
cancelEditing();
};
const sendEditedMessage = async (message: string): Promise<void> => {
if (!chat?.chat || !editingMessageId || !modelId) {
return;
}
const chatHistory = chat.chat.history;
const editedMessage = chatHistory.messages[editingMessageId];
let preparedChat: ChatResponse;
if (editedMessage.role === Role.ASSISTANT) {
preparedChat = prepareEditAssistantMessagePayload(chat, editingMessageId, message);
} else {
preparedChat = prepareUpdateMessageToSendPayload(chat, message, modelId, editedMessage.parentId);
}
await updateChat(preparedChat, {
onSuccess: (data) => {
if (editedMessage.role === Role.ASSISTANT) return;
const completePayload = prepareCompleteChatPayload({
chatId: data.id,
messageId: data.chat!.history.currentId,
messages: data.chat!.messages,
sessionId: socketSessionId,
model: modelId,
});
completeChat(completePayload);
},
});
cancelEditing();
};
const result = {
editingMessageId,
control,
handleSubmit,
startEditing,
cancelEditing,
saveMessage,
sendEditedMessage,
isChatUpdating,
};
return result;
};