forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatService.swift
More file actions
135 lines (122 loc) · 4.71 KB
/
ChatService.swift
File metadata and controls
135 lines (122 loc) · 4.71 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
import ChatPlugins
import Combine
import Foundation
import OpenAIService
public final class ChatService: ObservableObject {
public let chatGPTService: any ChatGPTServiceType
let plugins = registerPlugins(
TerminalChatPlugin.self,
AITerminalChatPlugin.self
)
var runningPlugin: ChatPlugin?
var cancellable = Set<AnyCancellable>()
public init<T: ChatGPTServiceType>(chatGPTService: T) {
self.chatGPTService = chatGPTService
chatGPTService.objectWillChange.sink { [weak self] _ in
self?.objectWillChange.send()
}.store(in: &cancellable)
}
public func send(content: String) async throws {
// look for the prefix of content, see if there is something like /command.
// If there is, then we need to find the plugin that can handle this command.
// If there is no such plugin, then we just send the message to the GPT service.
let regex = try NSRegularExpression(pattern: #"^\/([a-zA-Z0-9]+)"#)
let matches = regex.matches(in: content, range: NSRange(content.startIndex..., in: content))
if let match = matches.first {
let command = String(content[Range(match.range(at: 1), in: content)!])
if command == "exit" {
if let plugin = runningPlugin {
runningPlugin = nil
_ = await chatGPTService.mutateHistory { history in
history.append(.init(
role: .user,
content: "",
summary: "Exit plugin \(plugin.name)."
))
history.append(.init(
role: .system,
content: "",
summary: "Exited plugin \(plugin.name)."
))
}
} else {
_ = await chatGPTService.mutateHistory { history in
history.append(.init(
role: .system,
content: "",
summary: "No plugin running."
))
}
}
} else if let runningPlugin {
await runningPlugin.send(content: content)
} else if let pluginType = plugins[command] {
let plugin = pluginType.init(inside: chatGPTService, delegate: self)
if #available(macOS 13.0, *) {
await plugin.send(
content: String(
content.dropFirst(command.count + 1)
.trimmingPrefix(while: { $0 == " " })
)
)
} else {
await plugin.send(content: String(content.dropFirst(command.count + 1)))
}
} else {
_ = try await chatGPTService.send(content: content, summary: nil)
}
} else if let runningPlugin {
await runningPlugin.send(content: content)
} else {
_ = try await chatGPTService.send(content: content, summary: nil)
}
}
public func stopReceivingMessage() async {
if let runningPlugin {
await runningPlugin.stopResponding()
}
await chatGPTService.stopReceivingMessage()
}
public func clearHistory() async {
if let runningPlugin {
await runningPlugin.cancel()
}
await chatGPTService.clearHistory()
}
public func mutateSystemPrompt(_ newPrompt: String) async {
await chatGPTService.mutateSystemPrompt(newPrompt)
}
}
extension ChatService: ChatPluginDelegate {
public func pluginDidStartResponding(_: ChatPlugins.ChatPlugin) {
Task {
await chatGPTService.markReceivingMessage(true)
}
}
public func pluginDidEndResponding(_: ChatPlugins.ChatPlugin) {
Task {
await chatGPTService.markReceivingMessage(false)
}
}
public func pluginDidStart(_ plugin: ChatPlugin) {
runningPlugin = plugin
}
public func pluginDidEnd(_ plugin: ChatPlugin) {
if runningPlugin === plugin {
runningPlugin = nil
}
}
public func shouldStartAnotherPlugin(_ type: ChatPlugin.Type, withContent content: String) {
let plugin = type.init(inside: chatGPTService, delegate: self)
Task {
await plugin.send(content: content)
}
}
}
func registerPlugins(_ plugins: ChatPlugin.Type...) -> [String: ChatPlugin.Type] {
var all = [String: ChatPlugin.Type]()
for plugin in plugins {
all[plugin.command] = plugin
}
return all
}