-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpatch-completed-message.ts
More file actions
50 lines (42 loc) · 1.3 KB
/
Copy pathpatch-completed-message.ts
File metadata and controls
50 lines (42 loc) · 1.3 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
import { Role } from '@open-webui-react-native/shared/data-access/common';
import { ChatResponse, Message, History } from '../models';
export function patchCompletedMessage(oldData: ChatResponse | undefined): ChatResponse | undefined {
if (
!oldData ||
oldData.chat.messages.length === 0 ||
oldData.chat.messages[oldData.chat.messages.length - 1].role !== Role.ASSISTANT
) {
return oldData;
}
const { chat } = oldData;
const { messages, history } = chat;
const lastMessage = messages[messages.length - 1];
if (lastMessage.role !== Role.ASSISTANT) return oldData;
const updatedLastMessage: Message = {
...lastMessage,
done: true,
socketStatusData: history.messages[lastMessage.id]?.socketStatusData,
};
const updatedMessages = [...messages];
updatedMessages[updatedMessages.length - 1] = updatedLastMessage;
const updatedHistoryMessages = history?.messages
? {
...history.messages,
[updatedLastMessage.id]: updatedLastMessage,
}
: undefined;
const updatedHistory = updatedHistoryMessages
? new History({
messages: updatedHistoryMessages,
currentId: history.currentId,
})
: history;
return {
...oldData,
chat: {
...chat,
messages: updatedMessages,
history: updatedHistory,
},
};
}