forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatMessage+Storage.swift
More file actions
123 lines (111 loc) · 5.12 KB
/
ChatMessage+Storage.swift
File metadata and controls
123 lines (111 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import Foundation
import ChatAPIService
import Persist
import Logger
import ConversationServiceProvider
extension ChatMessage {
struct TurnItemData: Codable {
var content: String
var rating: ConversationRating
var references: [ConversationReference]
var followUp: ConversationFollowUp?
var suggestedTitle: String?
var errorMessages: [String] = []
var steps: [ConversationProgressStep]
var editAgentRounds: [AgentRound]
var panelMessages: [CopilotShowMessageParams]
var fileEdits: [FileEdit]
// Custom decoder to provide default value for steps
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
content = try container.decode(String.self, forKey: .content)
rating = try container.decode(ConversationRating.self, forKey: .rating)
references = try container.decode([ConversationReference].self, forKey: .references)
followUp = try container.decodeIfPresent(ConversationFollowUp.self, forKey: .followUp)
suggestedTitle = try container.decodeIfPresent(String.self, forKey: .suggestedTitle)
errorMessages = try container.decodeIfPresent([String].self, forKey: .errorMessages) ?? []
steps = try container.decodeIfPresent([ConversationProgressStep].self, forKey: .steps) ?? []
editAgentRounds = try container.decodeIfPresent([AgentRound].self, forKey: .editAgentRounds) ?? []
panelMessages = try container.decodeIfPresent([CopilotShowMessageParams].self, forKey: .panelMessages) ?? []
fileEdits = try container.decodeIfPresent([FileEdit].self, forKey: .fileEdits) ?? []
}
// Default memberwise init for encoding
init(
content: String,
rating: ConversationRating,
references: [ConversationReference],
followUp: ConversationFollowUp?,
suggestedTitle: String?,
errorMessages: [String] = [],
steps: [ConversationProgressStep]?,
editAgentRounds: [AgentRound]? = nil,
panelMessages: [CopilotShowMessageParams]? = nil,
fileEdits: [FileEdit]? = nil
) {
self.content = content
self.rating = rating
self.references = references
self.followUp = followUp
self.suggestedTitle = suggestedTitle
self.errorMessages = errorMessages
self.steps = steps ?? []
self.editAgentRounds = editAgentRounds ?? []
self.panelMessages = panelMessages ?? []
self.fileEdits = fileEdits ?? []
}
}
func toTurnItem() -> TurnItem {
let turnItemData = TurnItemData(
content: self.content,
rating: self.rating,
references: self.references,
followUp: self.followUp,
suggestedTitle: self.suggestedTitle,
errorMessages: self.errorMessages,
steps: self.steps,
editAgentRounds: self.editAgentRounds,
panelMessages: self.panelMessages,
fileEdits: self.fileEdits
)
// TODO: handle exception
let encoder = JSONEncoder()
let encodeData = (try? encoder.encode(turnItemData)) ?? Data()
let data = String(data: encodeData, encoding: .utf8) ?? "{}"
return TurnItem(id: self.id, conversationID: self.chatTabID, CLSTurnID: self.clsTurnID, role: role.rawValue, data: data, createdAt: self.createdAt, updatedAt: self.updatedAt)
}
static func from(_ turnItem: TurnItem) -> ChatMessage? {
var chatMessage: ChatMessage? = nil
do {
if let jsonData = turnItem.data.data(using: .utf8) {
let decoder = JSONDecoder()
let turnItemData = try decoder.decode(TurnItemData.self, from: jsonData)
chatMessage = .init(
id: turnItem.id,
chatTabID: turnItem.conversationID,
clsTurnID: turnItem.CLSTurnID,
role: ChatMessage.Role(rawValue: turnItem.role)!,
content: turnItemData.content,
references: turnItemData.references,
followUp: turnItemData.followUp,
suggestedTitle: turnItemData.suggestedTitle,
errorMessages: turnItemData.errorMessages,
rating: turnItemData.rating,
steps: turnItemData.steps,
editAgentRounds: turnItemData.editAgentRounds,
panelMessages: turnItemData.panelMessages,
fileEdits: turnItemData.fileEdits,
createdAt: turnItem.createdAt,
updatedAt: turnItem.updatedAt
)
}
} catch {
Logger.client.error("Failed to restore chat message: \(error)")
}
return chatMessage
}
}
extension Array where Element == ChatMessage {
func toTurnItems() -> [TurnItem] {
return self.map { $0.toTurnItem() }
}
}