-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprepare-copy-edited-message-payload.ts
More file actions
64 lines (54 loc) · 1.54 KB
/
prepare-copy-edited-message-payload.ts
File metadata and controls
64 lines (54 loc) · 1.54 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
import { Role } from '@open-webui-react-native/shared/data-access/common';
import { ChatResponse, Message } from '../models';
export function prepareCopyEditedMessagePayload(
oldData: ChatResponse,
messageId: string,
newContent: string,
): ChatResponse {
const history = oldData.chat.history;
const messagesMap = { ...history.messages };
const target = messagesMap[messageId];
if (!target) return oldData;
const isAIMessage = target.role === Role.ASSISTANT;
const updatedMessage: Message = {
...target,
content: newContent,
};
messagesMap[messageId] = updatedMessage;
if (!isAIMessage) {
const patchedMessagesList = oldData.chat.messages.map((msg) => (msg.id === messageId ? updatedMessage : msg));
const lastAssistantMessage = oldData.chat.history.lastAssistantMessage;
return {
...oldData,
chat: {
...oldData.chat,
history: {
...history,
messages: messagesMap,
lastAssistantMessage,
},
messages: patchedMessagesList,
},
};
}
const chain: Array<Message> = [];
let pointer: Message | undefined = updatedMessage;
while (pointer) {
chain.unshift(pointer);
pointer = pointer.parentId ? messagesMap[pointer.parentId] : undefined;
}
const newCurrentId = updatedMessage.id;
return {
...oldData,
chat: {
...oldData.chat,
history: {
...history,
messages: messagesMap,
currentId: newCurrentId,
lastAssistantMessage: updatedMessage,
},
messages: chain,
},
};
}