forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatGPTChatTab.swift
More file actions
146 lines (125 loc) · 4.18 KB
/
ChatGPTChatTab.swift
File metadata and controls
146 lines (125 loc) · 4.18 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
import ChatService
import ChatTab
import Combine
import Foundation
import Preferences
import SwiftUI
/// A chat tab that provides a context aware chat bot, powered by ChatGPT.
public class ChatGPTChatTab: ChatTab {
public static var name: String { "Chat" }
public let service: ChatService
public let provider: ChatProvider
private var cancellable = Set<AnyCancellable>()
struct Builder: ChatTabBuilder {
var title: String
var customCommand: CustomCommand?
func build() -> any ChatTab {
let tab = ChatGPTChatTab()
Task {
if let customCommand {
try await tab.service.handleCustomCommand(customCommand)
}
}
return tab
}
}
public func buildView() -> any View {
ChatPanel(chat: provider)
}
public func buildMenu() -> any View {
ChatContextMenu(chat: provider)
}
public static func chatBuilders(externalDependency: Void) -> [ChatTabBuilder] {
let customCommands = UserDefaults.shared.value(for: \.customCommands).compactMap {
command in
if case .customChat = command.feature {
return Builder(title: command.name, customCommand: command)
}
return nil
}
return [Builder(title: "ChatGPT", customCommand: nil)] + customCommands
}
public init(service: ChatService = .init()) {
self.service = service
provider = .init(service: service)
super.init(id: "Chat-" + provider.id.uuidString, title: "Chat")
provider.$history.sink { [weak self] _ in
if let title = self?.provider.title {
self?.title = title
}
}.store(in: &cancellable)
}
}
extension ChatProvider {
convenience init(service: ChatService) {
self.init(pluginIdentifiers: service.allPluginCommands)
let cancellable = service.objectWillChange.sink { [weak self] in
guard let self else { return }
Task { @MainActor in
self.history = (await service.memory.history).map { message in
.init(
id: message.id,
role: {
switch message.role {
case .system: return .ignored
case .user: return .user
case .assistant:
if let text = message.summary ?? message.content, !text.isEmpty {
return .assistant
}
return .ignored
case .function: return .function
}
}(),
text: message.summary ?? message.content ?? ""
)
}
self.isReceivingMessage = service.isReceivingMessage
self.systemPrompt = service.systemPrompt
self.extraSystemPrompt = service.extraSystemPrompt
}
}
service.objectWillChange.send()
onMessageSend = { [cancellable] message in
_ = cancellable
Task {
try await service.send(content: message)
}
}
onStop = {
Task {
await service.stopReceivingMessage()
}
}
onClear = {
Task {
await service.clearHistory()
}
}
onDeleteMessage = { id in
Task {
await service.deleteMessage(id: id)
}
}
onResendMessage = { id in
Task {
try await service.resendMessage(id: id)
}
}
onResetPrompt = {
Task {
await service.resetPrompt()
}
}
onRunCustomCommand = { command in
Task {
try await service.handleCustomCommand(command)
}
}
onSetAsExtraPrompt = { id in
Task {
await service.setMessageAsExtraPrompt(id: id)
}
}
}
}