forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatService.swift
More file actions
364 lines (315 loc) · 13.5 KB
/
ChatService.swift
File metadata and controls
364 lines (315 loc) · 13.5 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import ChatAPIService
import Combine
import Foundation
import GitHubCopilotService
import Preferences
import ConversationServiceProvider
import BuiltinExtension
import JSONRPC
import Status
public protocol ChatServiceType {
var memory: ContextAwareAutoManagedChatMemory { get set }
func send(_ id: String, content: String, skillSet: [ConversationSkill], references: [FileReference]) async throws
func stopReceivingMessage() async
func upvote(_ id: String, _ rating: ConversationRating) async
func downvote(_ id: String, _ rating: ConversationRating) async
func copyCode(_ id: String) async
}
public final class ChatService: ChatServiceType, ObservableObject {
public var memory: ContextAwareAutoManagedChatMemory
@Published public internal(set) var chatHistory: [ChatMessage] = []
@Published public internal(set) var isReceivingMessage = false
public var chatTemplates: [ChatTemplate]? = nil
public static var shared: ChatService = ChatService.service()
private let conversationProvider: ConversationServiceProvider?
private let conversationProgressHandler: ConversationProgressHandler
private let conversationContextHandler: ConversationContextHandler = ConversationContextHandlerImpl.shared
private var cancellables = Set<AnyCancellable>()
private var activeRequestId: String?
private var conversationId: String?
private var skillSet: [ConversationSkill] = []
init(provider: any ConversationServiceProvider,
memory: ContextAwareAutoManagedChatMemory = ContextAwareAutoManagedChatMemory(),
conversationProgressHandler: ConversationProgressHandler = ConversationProgressHandlerImpl.shared) {
self.memory = memory
self.conversationProvider = provider
self.conversationProgressHandler = conversationProgressHandler
memory.chatService = self
subscribeToNotifications()
subscribeToConversationContextRequest()
}
private func subscribeToNotifications() {
memory.observeHistoryChange { [weak self] in
Task { [weak self] in
guard let memory = self?.memory else { return }
self?.chatHistory = await memory.history
}
}
conversationProgressHandler.onBegin.sink { [weak self] (token, progress) in
self?.handleProgressBegin(token: token, progress: progress)
}.store(in: &cancellables)
conversationProgressHandler.onProgress.sink { [weak self] (token, progress) in
self?.handleProgressReport(token: token, progress: progress)
}.store(in: &cancellables)
conversationProgressHandler.onEnd.sink { [weak self] (token, progress) in
self?.handleProgressEnd(token: token, progress: progress)
}.store(in: &cancellables)
}
private func subscribeToConversationContextRequest() {
self.conversationContextHandler.onConversationContext.sink(receiveValue: { [weak self] (request, completion) in
guard let skills = self?.skillSet, !skills.isEmpty, request.params!.conversationId == self?.conversationId else { return }
skills.forEach { skill in
if (skill.applies(params: request.params!)) {
skill.resolveSkill(request: request, completion: completion)
}
}
}).store(in: &cancellables)
}
public static func service() -> ChatService {
let provider = BuiltinExtensionConversationServiceProvider(
extension: GitHubCopilotExtension.self
)
return ChatService(provider: provider)
}
public func send(_ id: String, content: String, skillSet: Array<ConversationSkill>, references: Array<FileReference>) async throws {
guard activeRequestId == nil else { return }
let workDoneToken = UUID().uuidString
activeRequestId = workDoneToken
await memory.appendMessage(ChatMessage(id: id, role: .user, content: content, references: []))
let skillCapabilities: [String] = [ CurrentEditorSkill.ID, ProblemsInActiveDocumentSkill.ID ]
let supportedSkills: [String] = skillSet.map { $0.id }
let ignoredSkills: [String] = skillCapabilities.filter {
!supportedSkills.contains($0)
}
let request = ConversationRequest(workDoneToken: workDoneToken,
content: content,
workspaceFolder: "",
skills: skillCapabilities,
ignoredSkills: ignoredSkills,
references: references)
self.skillSet = skillSet
try await send(request)
}
public func sendAndWait(_ id: String, content: String) async throws -> String {
try await send(id, content: content, skillSet: [], references: [])
if let reply = await memory.history.last(where: { $0.role == .assistant })?.content {
return reply
}
return ""
}
public func stopReceivingMessage() async {
if let activeRequestId = activeRequestId {
do {
try await conversationProvider?.stopReceivingMessage(activeRequestId)
} catch {
print("Failed to cancel ongoing request with WDT: \(activeRequestId)")
}
}
resetOngoingRequest()
}
public func clearHistory() async {
await memory.clearHistory()
if let activeRequestId = activeRequestId {
do {
try await conversationProvider?.stopReceivingMessage(activeRequestId)
} catch {
print("Failed to cancel ongoing request with WDT: \(activeRequestId)")
}
}
resetOngoingRequest()
}
public func deleteMessage(id: String) async {
await memory.removeMessage(id)
}
public func resendMessage(id: String) async throws {
if let message = (await memory.history).first(where: { $0.id == id })
{
do {
try await send(id, content: message.content, skillSet: [], references: [])
} catch {
print("Failed to resend message")
}
}
}
public func setMessageAsExtraPrompt(id: String) async {
if let message = (await memory.history).first(where: { $0.id == id })
{
await mutateHistory { history in
history.append(.init(
role: .assistant,
content: message.content
))
}
}
}
public func mutateHistory(_ mutator: @escaping (inout [ChatMessage]) -> Void) async {
await memory.mutateHistory(mutator)
}
public func handleCustomCommand(_ command: CustomCommand) async throws {
struct CustomCommandInfo {
var specifiedSystemPrompt: String?
var extraSystemPrompt: String?
var sendingMessageImmediately: String?
var name: String?
}
let info: CustomCommandInfo? = {
switch command.feature {
case let .chatWithSelection(extraSystemPrompt, prompt, useExtraSystemPrompt):
let updatePrompt = useExtraSystemPrompt ?? true
return .init(
extraSystemPrompt: updatePrompt ? extraSystemPrompt : nil,
sendingMessageImmediately: prompt,
name: command.name
)
case let .customChat(systemPrompt, prompt):
return .init(
specifiedSystemPrompt: systemPrompt,
extraSystemPrompt: "",
sendingMessageImmediately: prompt,
name: command.name
)
case .promptToCode: return nil
case .singleRoundDialog: return nil
}
}()
guard let info else { return }
let templateProcessor = CustomCommandTemplateProcessor()
if info.specifiedSystemPrompt != nil || info.extraSystemPrompt != nil {
await mutateHistory { history in
history.append(.init(
role: .assistant,
content: ""
))
}
}
if let sendingMessageImmediately = info.sendingMessageImmediately,
!sendingMessageImmediately.isEmpty
{
try await send(UUID().uuidString, content: templateProcessor.process(sendingMessageImmediately), skillSet: [], references: [])
}
}
public func upvote(_ id: String, _ rating: ConversationRating) async {
try? await conversationProvider?.rateConversation(turnId: id, rating: rating)
}
public func downvote(_ id: String, _ rating: ConversationRating) async {
try? await conversationProvider?.rateConversation(turnId: id, rating: rating)
}
public func copyCode(_ id: String) async {
// TODO: pass copy code info to Copilot server
}
public func loadChatTemplates() async -> [ChatTemplate]? {
guard self.chatTemplates == nil else { return self.chatTemplates }
do {
if let templates = (try await conversationProvider?.templates()) {
self.chatTemplates = templates
return templates
}
} catch {
// handle error if desired
}
return nil
}
public func handleSingleRoundDialogCommand(
systemPrompt: String?,
overwriteSystemPrompt: Bool,
prompt: String
) async throws -> String {
let templateProcessor = CustomCommandTemplateProcessor()
return try await sendAndWait(UUID().uuidString, content: templateProcessor.process(prompt))
}
private func handleProgressBegin(token: String, progress: ConversationProgressBegin) {
guard let workDoneToken = activeRequestId, workDoneToken == token else { return }
conversationId = progress.conversationId
Task {
if var lastUserMessage = await memory.history.last(where: { $0.role == .user }) {
lastUserMessage.turnId = progress.turnId
}
}
}
private func handleProgressReport(token: String, progress: ConversationProgressReport) {
guard let workDownToken = activeRequestId, workDownToken == token else {
return
}
let id = progress.turnId
var content = ""
var references: [ConversationReference] = []
if let reply = progress.reply {
content = reply
}
if let progressReferences = progress.references, !progressReferences.isEmpty {
progressReferences.forEach { item in
let reference = ConversationReference(
uri: item.uri,
status: .included,
kind: .other
)
references.append(reference)
}
}
if content.isEmpty && references.isEmpty {
return
}
// create immutable copies
let messageContent = content
let messageReferences = references
Task {
let message = ChatMessage(id: id, role: .assistant, content: messageContent, references: messageReferences)
await memory.appendMessage(message)
}
}
private func handleProgressEnd(token: String, progress: ConversationProgressEnd) {
guard let workDoneToken = activeRequestId, workDoneToken == token else { return }
let followUp = progress.followUp
if let CLSError = progress.error {
// CLS Error Code 402: reached monthly chat messages limit
if CLSError.code == 402 {
Task {
await Status.shared
.updateCLSStatus(.error, message: CLSError.message)
let errorMessage = ChatMessage(
id: progress.turnId,
role: .system,
content: CLSError.message
)
await memory.removeMessage(progress.turnId)
await memory.appendMessage(errorMessage)
}
} else {
Task {
let errorMessage = ChatMessage(
id: progress.turnId,
role: .assistant,
content: "",
errorMessage: CLSError.message
)
await memory.appendMessage(errorMessage)
}
}
resetOngoingRequest()
return
}
Task {
let message = ChatMessage(id: progress.turnId, role: .assistant, content: "", followUp: followUp, suggestedTitle: progress.suggestedTitle)
await memory.appendMessage(message)
}
resetOngoingRequest()
}
private func resetOngoingRequest() {
activeRequestId = nil
isReceivingMessage = false
}
private func send(_ request: ConversationRequest) async throws {
guard !isReceivingMessage else { throw CancellationError() }
isReceivingMessage = true
do {
if let conversationId = conversationId {
try await conversationProvider?.createTurn(with: conversationId, request: request)
} else {
try await conversationProvider?.createConversation(request)
}
} catch {
resetOngoingRequest()
throw error
}
}
}