|
| 1 | +import CodableWrappers |
| 2 | +import Foundation |
| 3 | + |
| 4 | +public struct ChatMessage: Equatable, Codable { |
| 5 | + public typealias ID = String |
| 6 | + |
| 7 | + public enum Role: String, Codable, Equatable { |
| 8 | + case system |
| 9 | + case user |
| 10 | + case assistant |
| 11 | + } |
| 12 | + |
| 13 | + public struct FunctionCall: Codable, Equatable { |
| 14 | + public var name: String |
| 15 | + public var arguments: String |
| 16 | + public init(name: String, arguments: String) { |
| 17 | + self.name = name |
| 18 | + self.arguments = arguments |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + public struct ToolCall: Codable, Equatable, Identifiable { |
| 23 | + public var id: String |
| 24 | + public var type: String |
| 25 | + public var function: FunctionCall |
| 26 | + public var response: ToolCallResponse |
| 27 | + public init( |
| 28 | + id: String, |
| 29 | + type: String, |
| 30 | + function: FunctionCall, |
| 31 | + response: ToolCallResponse? = nil |
| 32 | + ) { |
| 33 | + self.id = id |
| 34 | + self.type = type |
| 35 | + self.function = function |
| 36 | + self.response = response ?? .init(content: "", summary: nil) |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public struct ToolCallResponse: Codable, Equatable { |
| 41 | + public var content: String |
| 42 | + public var summary: String? |
| 43 | + public init(content: String, summary: String?) { |
| 44 | + self.content = content |
| 45 | + self.summary = summary |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public struct Reference: Codable, Equatable { |
| 50 | + public enum Kind: String, Codable { |
| 51 | + case `class` |
| 52 | + case `struct` |
| 53 | + case `enum` |
| 54 | + case `actor` |
| 55 | + case `protocol` |
| 56 | + case `extension` |
| 57 | + case `case` |
| 58 | + case property |
| 59 | + case `typealias` |
| 60 | + case function |
| 61 | + case method |
| 62 | + case text |
| 63 | + case webpage |
| 64 | + case other |
| 65 | + } |
| 66 | + |
| 67 | + public var title: String |
| 68 | + public var subTitle: String |
| 69 | + public var uri: String |
| 70 | + public var content: String |
| 71 | + public var startLine: Int? |
| 72 | + public var endLine: Int? |
| 73 | + @FallbackDecoding<ReferenceKindFallback> |
| 74 | + public var kind: Kind |
| 75 | + |
| 76 | + public init( |
| 77 | + title: String, |
| 78 | + subTitle: String, |
| 79 | + content: String, |
| 80 | + uri: String, |
| 81 | + startLine: Int?, |
| 82 | + endLine: Int?, |
| 83 | + kind: Kind |
| 84 | + ) { |
| 85 | + self.title = title |
| 86 | + self.subTitle = subTitle |
| 87 | + self.content = content |
| 88 | + self.uri = uri |
| 89 | + self.startLine = startLine |
| 90 | + self.endLine = endLine |
| 91 | + self.kind = kind |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /// The role of a message. |
| 96 | + @FallbackDecoding<ChatMessageRoleFallback> |
| 97 | + public var role: Role |
| 98 | + |
| 99 | + /// The content of the message, either the chat message, or a result of a function call. |
| 100 | + public var content: String? { |
| 101 | + didSet { tokensCount = nil } |
| 102 | + } |
| 103 | + |
| 104 | + /// A function call from the bot. |
| 105 | + public var toolCalls: [ToolCall]? { |
| 106 | + didSet { tokensCount = nil } |
| 107 | + } |
| 108 | + |
| 109 | + /// The function name of a reply to a function call. |
| 110 | + public var name: String? { |
| 111 | + didSet { tokensCount = nil } |
| 112 | + } |
| 113 | + |
| 114 | + /// The summary of a message that is used for display. |
| 115 | + public var summary: String? |
| 116 | + |
| 117 | + /// The id of the message. |
| 118 | + public var id: ID |
| 119 | + |
| 120 | + /// The number of tokens of this message. |
| 121 | + public var tokensCount: Int? |
| 122 | + |
| 123 | + /// The references of this message. |
| 124 | + @FallbackDecoding<EmptyArray<Reference>> |
| 125 | + public var references: [Reference] |
| 126 | + |
| 127 | + /// Is the message considered empty. |
| 128 | + public var isEmpty: Bool { |
| 129 | + if let content, !content.isEmpty { return false } |
| 130 | + if let toolCalls, !toolCalls.isEmpty { return false } |
| 131 | + if let name, !name.isEmpty { return false } |
| 132 | + return true |
| 133 | + } |
| 134 | + |
| 135 | + public init( |
| 136 | + id: String = UUID().uuidString, |
| 137 | + role: Role, |
| 138 | + content: String?, |
| 139 | + name: String? = nil, |
| 140 | + toolCalls: [ToolCall]? = nil, |
| 141 | + summary: String? = nil, |
| 142 | + tokenCount: Int? = nil, |
| 143 | + references: [Reference] = [] |
| 144 | + ) { |
| 145 | + self.role = role |
| 146 | + self.content = content |
| 147 | + self.name = name |
| 148 | + self.toolCalls = toolCalls |
| 149 | + self.summary = summary |
| 150 | + self.id = id |
| 151 | + tokensCount = tokenCount |
| 152 | + self.references = references |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +public struct ReferenceKindFallback: FallbackValueProvider { |
| 157 | + public static var defaultValue: ChatMessage.Reference.Kind { .other } |
| 158 | +} |
| 159 | + |
| 160 | +public struct ChatMessageRoleFallback: FallbackValueProvider { |
| 161 | + public static var defaultValue: ChatMessage.Role { .user } |
| 162 | +} |
| 163 | + |
0 commit comments