Skip to content

Commit 9b22c29

Browse files
committed
Add references to chat message
1 parent a1c1043 commit 9b22c29

2 files changed

Lines changed: 41 additions & 8 deletions

File tree

Core/Sources/ChatGPTChatTab/Chat.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ public struct DisplayedChatMessage: Equatable {
1515
public var id: String
1616
public var role: Role
1717
public var text: String
18+
public var references: [ChatMessage.Reference] = []
1819

19-
public init(id: String, role: Role, text: String) {
20+
public init(id: String, role: Role, text: String, references: [ChatMessage.Reference]) {
2021
self.id = id
2122
self.role = role
2223
self.text = text
24+
self.references = references
2325
}
2426
}
2527

@@ -247,7 +249,8 @@ struct Chat: ReducerProtocol {
247249
case .function: return .function
248250
}
249251
}(),
250-
text: message.summary ?? message.content ?? ""
252+
text: message.summary ?? message.content ?? "",
253+
references: message.references
251254
)
252255
}
253256

Tool/Sources/OpenAIService/Models.swift

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import CodableWrappers
12
import Foundation
23

34
struct Cancellable {
@@ -24,9 +25,32 @@ public struct ChatMessage: Equatable, Codable {
2425
}
2526
}
2627

28+
public struct Reference: Codable, Equatable {
29+
public var title: String
30+
public var subTitle: String
31+
public var uri: String
32+
public var startLine: Int?
33+
public var endLine: Int?
34+
public var metadata: [String: String]
35+
36+
public init(
37+
title: String,
38+
subTitle: String,
39+
uri: String,
40+
startLine: Int?,
41+
endLine: Int?,
42+
metadata: [String: String]
43+
) {
44+
self.title = title
45+
self.subTitle = subTitle
46+
self.uri = uri
47+
self.metadata = metadata
48+
}
49+
}
50+
2751
/// The role of a message.
2852
public var role: Role
29-
53+
3054
/// The content of the message, either the chat message, or a result of a function call.
3155
public var content: String? {
3256
didSet { tokensCount = nil }
@@ -41,16 +65,20 @@ public struct ChatMessage: Equatable, Codable {
4165
public var name: String? {
4266
didSet { tokensCount = nil }
4367
}
44-
68+
4569
/// The summary of a message that is used for display.
4670
public var summary: String?
47-
71+
4872
/// The id of the message.
4973
public var id: String
50-
74+
5175
/// The number of tokens of this message.
5276
var tokensCount: Int?
53-
77+
78+
/// The references of this message.
79+
@FallbackDecoding<EmptyArray<Reference>>
80+
public var references: [Reference]
81+
5482
/// Is the message considered empty.
5583
var isEmpty: Bool {
5684
if let content, !content.isEmpty { return false }
@@ -66,7 +94,8 @@ public struct ChatMessage: Equatable, Codable {
6694
name: String? = nil,
6795
functionCall: FunctionCall? = nil,
6896
summary: String? = nil,
69-
tokenCount: Int? = nil
97+
tokenCount: Int? = nil,
98+
references: [Reference] = []
7099
) {
71100
self.role = role
72101
self.content = content
@@ -75,6 +104,7 @@ public struct ChatMessage: Equatable, Codable {
75104
self.summary = summary
76105
self.id = id
77106
tokensCount = tokenCount
107+
self.references = references
78108
}
79109
}
80110

0 commit comments

Comments
 (0)