adamrushy / OpenAISwift

This is a wrapper library around the ChatGPT and OpenAI HTTP API
MIT License
1.6k stars 242 forks source link

Add id property to ChatMessage to conform to Identifiable protocol #94

Closed ysak-y closed 11 months ago

ysak-y commented 1 year ago

What

Add id property that contains UUID to ChatMessage protocol.

Why

I think ChatMessage struct will be used in list components to show conversation with assistant and user. One way to implement it in SwiftUI is to use the List (or other iterable) component. But this doesn't work because ChatMessage doesn't conform to identifiable.

struct ContentView: View {
    @State private var messages: [ChatMessage] = [
        ChatMessage(role: .system, content: "You are a helpful assistant."),
        ChatMessage(role: .user, content: "Who won the world series in 2020?"),
    ]

    var body: some View {
        VStack {
            List(messages) { m in   <-------- Initializer 'init(_:rowContent:)' requires that 'ChatMessage' conform to 'Identifiable'
                Text(m.content)
            }

        }
        .padding()
    }
}

Using id: \.self for List is usual to define identification manually if object doesn't conform to Identifiable, but it needs to support Hashable protocol and ChatMessage doesn't.

So my idea is to add id property with uuid and make ChatMessage conform to Identifiable protocol.

How

Add id property that is initialized by UUID() method (I think it is usual way). And add Identifiable protocol to it.

Mmanion commented 11 months ago

I'm getting the ChatMessage failing because of ID as well. We're you able to sendChats after adding the id?

Screenshot 2023-07-30 at 4 32 21 PM