Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jul 11, 2023

Conversation

ysak-y
Copy link
Contributor

@ysak-y ysak-y commented Jun 21, 2023

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.

@adamrushy adamrushy merged commit 8d1f308 into adamrushy:main Jul 11, 2023
1 check passed
@ysak-y ysak-y deleted the add_uuid_to_chat_message branch July 11, 2023 21:13
@Mmanion
Copy link

Mmanion commented Jul 30, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants