|
| 1 | +# ConversationKit |
| 2 | + |
| 3 | +ConversationKit is a Swift package that provides an elegant and easy-to-use chat interface for iOS applications built with SwiftUI. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- 💬 Ready-to-use chat interface |
| 8 | +- 👤 Support for multiple participants in conversations |
| 9 | +- ⚡️ Real-time message streaming support |
| 10 | +- 🎨 SwiftUI-native implementation |
| 11 | +- 🔄 Async/await support for message handling |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +Add ConversationKit to your project using Swift Package Manager: |
| 16 | + |
| 17 | +```swift |
| 18 | +dependencies: [ |
| 19 | + .package(url: "PATH_TO_CONVERSATIONKIT_REPO", from: "1.0.0") |
| 20 | +] |
| 21 | +``` |
| 22 | + |
| 23 | +## Quick Start |
| 24 | + |
| 25 | +1. Import ConversationKit in your SwiftUI view: |
| 26 | + |
| 27 | +```swift |
| 28 | +import SwiftUI |
| 29 | +import ConversationKit |
| 30 | +``` |
| 31 | + |
| 32 | +2. Create a view with a conversation: |
| 33 | + |
| 34 | +```swift |
| 35 | +struct ChatView: View { |
| 36 | + @State private var messages: [Message] = [] |
| 37 | + |
| 38 | + var body: some View { |
| 39 | + ConversationView(messages: $messages) |
| 40 | + .onSendMessage { userMessage in |
| 41 | + // Handle the sent message |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +## Core Components |
| 48 | + |
| 49 | +### Message |
| 50 | + |
| 51 | +The basic unit of conversation that includes: |
| 52 | +- Content: The text of the message |
| 53 | +- Participant: The sender of the message (user or other) |
| 54 | + |
| 55 | +### ConversationView |
| 56 | + |
| 57 | +A SwiftUI view that handles the display and interaction of messages. Features include: |
| 58 | +- Message list display |
| 59 | +- Built-in send message functionality |
| 60 | +- Support for streaming responses |
| 61 | + |
| 62 | +## Example Usage |
| 63 | + |
| 64 | +Here's a complete example showing how to implement a basic chat interface: |
| 65 | + |
| 66 | +```swift |
| 67 | +import SwiftUI |
| 68 | +import ConversationKit |
| 69 | + |
| 70 | +struct ContentView: View { |
| 71 | + @State var messages: [Message] = [] |
| 72 | + |
| 73 | + var body: some View { |
| 74 | + NavigationStack { |
| 75 | + ConversationView(messages: $messages) |
| 76 | + .onSendMessage { userMessage in |
| 77 | + Task { |
| 78 | + // Handle the message |
| 79 | + await generateResponse(for: userMessage) |
| 80 | + } |
| 81 | + } |
| 82 | + .navigationTitle("Chat") |
| 83 | + .navigationBarTitleDisplayMode(.inline) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + func generateResponse(for message: Message) async { |
| 88 | + // Implement your response generation logic here |
| 89 | + } |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +## Message Streaming |
| 94 | + |
| 95 | +ConversationKit supports streaming responses, allowing for a more dynamic chat experience. You can update message content incrementally: |
| 96 | + |
| 97 | +```swift |
| 98 | +func streamResponse() async { |
| 99 | + var message = Message(content: "", participant: .other) |
| 100 | + messages.append(message) |
| 101 | + |
| 102 | + // Stream content updates |
| 103 | + for chunk in responseChunks { |
| 104 | + message.content += chunk |
| 105 | + messages[messages.count - 1] = message |
| 106 | + try? await Task.sleep(nanoseconds: 100_000_000) // Adjust timing as needed |
| 107 | + } |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +## License |
| 112 | + |
| 113 | +ConversationKit is licensed under the Apache License, Version 2.0. See the LICENSE file for more details. |
| 114 | + |
| 115 | +## Requirements |
| 116 | + |
| 117 | +- iOS 15.0+ |
| 118 | +- Swift 5.5+ |
0 commit comments