Skip to content

Commit 17ae22b

Browse files
committed
Update ChatMessage.Reference
1 parent ba7a741 commit 17ae22b

4 files changed

Lines changed: 87 additions & 36 deletions

File tree

Core/Sources/ChatGPTChatTab/Chat.swift

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -343,15 +343,7 @@ struct Chat {
343343
}
344344
}(),
345345
text: message.summary ?? message.content ?? "",
346-
references: message.references.map {
347-
.init(
348-
title: $0.title,
349-
subtitle: $0.subTitle,
350-
uri: $0.uri,
351-
startLine: $0.startLine,
352-
kind: $0.kind
353-
)
354-
}
346+
references: message.references.map(convertReference)
355347
))
356348

357349
for call in message.toolCalls ?? [] {
@@ -513,3 +505,48 @@ private actor TimedDebounceFunction {
513505
}
514506
}
515507

508+
private func convertReference(
509+
_ reference: ChatMessage.Reference
510+
) -> DisplayedChatMessage.Reference {
511+
.init(
512+
title: reference.title,
513+
subtitle: {
514+
switch reference.kind {
515+
case let .symbol(_, uri, _, _):
516+
return uri
517+
case let .webpage(uri):
518+
return uri
519+
case let .textFile(uri):
520+
return uri
521+
case let .other(kind):
522+
return kind
523+
case .text:
524+
return reference.content
525+
}
526+
}(),
527+
uri: {
528+
switch reference.kind {
529+
case let .symbol(_, uri, _, _):
530+
return uri
531+
case let .webpage(uri):
532+
return uri
533+
case let .textFile(uri):
534+
return uri
535+
case .other:
536+
return ""
537+
case .text:
538+
return ""
539+
}
540+
}(),
541+
startLine: {
542+
switch reference.kind {
543+
case let .symbol(_, _, startLine, _):
544+
return startLine
545+
default:
546+
return nil
547+
}
548+
}(),
549+
kind: reference.kind
550+
)
551+
}
552+

Core/Sources/ChatGPTChatTab/ChatPanel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ struct ChatPanel_Preview: PreviewProvider {
504504
subtitle: "Hi Hi Hi Hi",
505505
uri: "https://google.com",
506506
startLine: nil,
507-
kind: .symbol(.class)
507+
kind: .symbol(.class, uri: "https://google.com", startLine: nil, endLine: nil)
508508
),
509509
]
510510
),

Core/Sources/ChatGPTChatTab/Views/BotMessage.swift

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ struct ReferenceIcon: View {
139139
RoundedRectangle(cornerRadius: 4)
140140
.fill({
141141
switch kind {
142-
case .symbol(let symbol):
142+
case .symbol(let symbol, _, _, _):
143143
switch symbol {
144144
case .class:
145145
Color.purple
@@ -168,6 +168,8 @@ struct ReferenceIcon: View {
168168
Color.gray
169169
case .webpage:
170170
Color.blue
171+
case .textFile:
172+
Color.gray
171173
case .other:
172174
Color.gray
173175
}
@@ -176,7 +178,7 @@ struct ReferenceIcon: View {
176178
.overlay(alignment: .center) {
177179
Group {
178180
switch kind {
179-
case .symbol(let symbol):
181+
case .symbol(let symbol, _, _, _):
180182
switch symbol {
181183
case .class:
182184
Text("C")
@@ -207,6 +209,8 @@ struct ReferenceIcon: View {
207209
Text("Wb")
208210
case .other:
209211
Text("Ot")
212+
case .textFile:
213+
Text("Tx")
210214
}
211215
}
212216
.font(.system(size: 12).monospaced())
@@ -231,7 +235,7 @@ struct ReferenceIcon: View {
231235
subtitle: "/Core/Sources/ChatGPTChatTab/Views/BotMessage.swift:100",
232236
uri: "https://google.com",
233237
startLine: nil,
234-
kind: .symbol(.class)
238+
kind: .symbol(.class, uri: "https://google.com", startLine: nil, endLine: nil)
235239
), count: 20),
236240
chat: .init(initialState: .init(), reducer: { Chat(service: .init()) })
237241
)
@@ -246,43 +250,42 @@ struct ReferenceIcon: View {
246250
subtitle: "/Core/Sources/ChatGPTChatTab/Views/BotMessage.swift:100",
247251
uri: "https://google.com",
248252
startLine: nil,
249-
kind: .symbol(.class)
253+
kind: .symbol(.class, uri: "https://google.com", startLine: nil, endLine: nil)
250254
),
251255
.init(
252256
title: "BotMessage.swift:100-102",
253257
subtitle: "/Core/Sources/ChatGPTChatTab/Views",
254258
uri: "https://google.com",
255259
startLine: nil,
256-
kind: .symbol(.struct)
260+
kind: .symbol(.struct, uri: "https://google.com", startLine: nil, endLine: nil)
257261
),
258262
.init(
259263
title: "ReferenceList",
260264
subtitle: "/Core/Sources/ChatGPTChatTab/Views/BotMessage.swift:100",
261265
uri: "https://google.com",
262266
startLine: nil,
263-
kind: .symbol(.function)
267+
kind: .symbol(.function, uri: "https://google.com", startLine: nil, endLine: nil)
264268
),
265269
.init(
266270
title: "ReferenceList",
267271
subtitle: "/Core/Sources/ChatGPTChatTab/Views/BotMessage.swift:100",
268272
uri: "https://google.com",
269273
startLine: nil,
270-
kind: .symbol(.case)
274+
kind: .symbol(.case, uri: "https://google.com", startLine: nil, endLine: nil)
271275
),
272276
.init(
273277
title: "ReferenceList",
274278
subtitle: "/Core/Sources/ChatGPTChatTab/Views/BotMessage.swift:100",
275279
uri: "https://google.com",
276280
startLine: nil,
277-
kind: .symbol(.extension)
281+
kind: .symbol(.extension, uri: "https://google.com", startLine: nil, endLine: nil)
278282
),
279283
.init(
280284
title: "ReferenceList",
281285
subtitle: "/Core/Sources/ChatGPTChatTab/Views/BotMessage.swift:100",
282286
uri: "https://google.com",
283287
startLine: nil,
284-
kind: .webpage
288+
kind: .webpage(uri: "https://google.com")
285289
),
286290
], chat: .init(initialState: .init(), reducer: { Chat(service: .init()) }))
287291
}
288-

Tool/Sources/ChatBasic/ChatMessage.swift

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
import CodableWrappers
22
import Foundation
33

4+
/// A chat message that can be sent or received.
45
public struct ChatMessage: Equatable, Codable {
56
public typealias ID = String
67

8+
/// The role of a message.
79
public enum Role: String, Codable, Equatable {
810
case system
911
case user
1012
case assistant
1113
}
1214

15+
/// A function call that can be made by the bot.
1316
public struct FunctionCall: Codable, Equatable {
17+
/// The name of the function.
1418
public var name: String
19+
/// Arguments in the format of a JSON string.
1520
public var arguments: String
1621
public init(name: String, arguments: String) {
1722
self.name = name
1823
self.arguments = arguments
1924
}
2025
}
2126

27+
/// A tool call that can be made by the bot.
2228
public struct ToolCall: Codable, Equatable, Identifiable {
2329
public var id: String
30+
/// The type of tool call.
2431
public var type: String
32+
/// The actual function call.
2533
public var function: FunctionCall
34+
/// The response of the function call.
2635
public var response: ToolCallResponse
2736
public init(
2837
id: String,
@@ -37,16 +46,21 @@ public struct ChatMessage: Equatable, Codable {
3746
}
3847
}
3948

49+
/// The response of a tool call
4050
public struct ToolCallResponse: Codable, Equatable {
51+
/// The content of the response.
4152
public var content: String
53+
/// The summary of the response to display in UI.
4254
public var summary: String?
4355
public init(content: String, summary: String?) {
4456
self.content = content
4557
self.summary = summary
4658
}
4759
}
4860

61+
/// A reference to include in a chat message.
4962
public struct Reference: Codable, Equatable {
63+
/// The kind of reference.
5064
public enum Kind: Codable, Equatable {
5165
public enum Symbol: String, Codable {
5266
case `class`
@@ -61,36 +75,33 @@ public struct ChatMessage: Equatable, Codable {
6175
case function
6276
case method
6377
}
64-
case symbol(Symbol)
78+
/// Code symbol.
79+
case symbol(Symbol, uri: String, startLine: Int?, endLine: Int?)
80+
/// Some text.
6581
case text
66-
case webpage
67-
case other(String)
82+
/// A webpage.
83+
case webpage(uri: String)
84+
/// A text file.
85+
case textFile(uri: String)
86+
/// Other kind of reference.
87+
case other(kind: String)
6888
}
6989

90+
/// The title of the reference.
7091
public var title: String
71-
public var subTitle: String
72-
public var uri: String
92+
/// The content of the reference.
7393
public var content: String
74-
public var startLine: Int?
75-
public var endLine: Int?
94+
/// The kind of the reference.
7695
@FallbackDecoding<ReferenceKindFallback>
7796
public var kind: Kind
7897

7998
public init(
8099
title: String,
81-
subTitle: String,
82100
content: String,
83-
uri: String,
84-
startLine: Int? = nil,
85-
endLine: Int? = nil,
86101
kind: Kind
87102
) {
88103
self.title = title
89-
self.subTitle = subTitle
90104
self.content = content
91-
self.uri = uri
92-
self.startLine = startLine
93-
self.endLine = endLine
94105
self.kind = kind
95106
}
96107
}
@@ -167,7 +178,7 @@ public struct ChatMessage: Equatable, Codable {
167178
}
168179

169180
public struct ReferenceKindFallback: FallbackValueProvider {
170-
public static var defaultValue: ChatMessage.Reference.Kind { .other("Unknown") }
181+
public static var defaultValue: ChatMessage.Reference.Kind { .other(kind: "Unknown") }
171182
}
172183

173184
public struct ChatMessageRoleFallback: FallbackValueProvider {

0 commit comments

Comments
 (0)