exyte / Chat

A SwiftUI Chat UI framework with fully customizable message cells and a built-in media picker
MIT License
873 stars 133 forks source link

Message update, UI interface does not refresh issue. #78

Closed hx23840 closed 2 months ago

hx23840 commented 2 months ago

I am interfacing with OpenAI's SSE format API, and I tried to update the content of corresponding messages. It can be confirmed that the message has been updated, but I did not see the update in the UI. Is there a problem with the way I call it?thx My code

    private func updateBotMessage(_ text: String, messageId: String?) {
        DispatchQueue.main.async {
            if let currentId = self.currentBotMessageId,
               let index = self.messages.firstIndex(where: { $0.id == currentId }) {
                var updatedMessage = self.messages[index]
                updatedMessage.text += text
                self.messages[index] = updatedMessage
            } else {
                let newId = messageId ?? UUID().uuidString
                let newMessage = Message(
                    id: newId,
                    user: self.botUser,
                    status: .sent,
                    createdAt: Date(),
                    text: text
                )
                self.messages.append(newMessage)
                self.currentBotMessageId = newId
            }

            self.objectWillChange.send()
            print("Current bot message: \(self.messages.last?.text ?? "")")
        }
    }

Log

EventSource connection opened
Received event: message
Current bot message: Hello
Received event: message
Received event: message
Current bot message: Hello!
Current bot message: Hello! How
Received event: message
Received event: message
Current bot message: Hello! How can
Current bot message: Hello! How can I
Received event: message
Current bot message: Hello! How can I assist
Received event: message
Current bot message: Hello! How can I assist you
Received event: message
Current bot message: Hello! How can I assist you today
Received event: message
Current bot message: Hello! How can I assist you today?
Received event: message
Received event: message
Current bot message: Hello! How can I assist you today?
f3dm76 commented 2 months ago

Hey @hx23840, I can not tell for sure, it's a small code sample, but please make sure your messages array is a @published property, and also your message's isEqual method takes text into account. If you need further assistance, please be sure to attach minimal compilable reproducible example. Have a nice day!

hx23840 commented 2 months ago

Thanks for the reply, I will go check the property information.

ywalterh commented 2 months ago

Hi @f3dm76 , I can confirm the same behavior. And I'm using ExyteChat.Message's === method only takes message.id and message.status and not message.text Can we use custom message type or override this some how?

f3dm76 commented 2 months ago

Hey @ywalterh, I added all the fields to isEqual, could you please try 2.0.2?