forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatPluginController.swift
More file actions
135 lines (119 loc) · 4.75 KB
/
ChatPluginController.swift
File metadata and controls
135 lines (119 loc) · 4.75 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
import ChatPlugin
import Combine
import Foundation
import OpenAIService
final class ChatPluginController {
let chatGPTService: any ChatGPTServiceType
let plugins: [String: ChatPlugin.Type]
var runningPlugin: ChatPlugin?
weak var chatService: ChatService?
init(chatGPTService: any ChatGPTServiceType, plugins: [ChatPlugin.Type]) {
self.chatGPTService = chatGPTService
var all = [String: ChatPlugin.Type]()
for plugin in plugins {
all[plugin.command.lowercased()] = plugin
}
self.plugins = all
}
convenience init(chatGPTService: any ChatGPTServiceType, plugins: ChatPlugin.Type...) {
self.init(chatGPTService: chatGPTService, plugins: plugins)
}
/// Handle the message in a plugin if required. Return false if no plugin handles the message.
func handleContent(_ content: String) async throws -> Bool {
// 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)!]).lowercased()
// handle exit plugin
if command == "exit" {
if let plugin = runningPlugin {
runningPlugin = nil
_ = await chatGPTService.memory.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.memory.mutateHistory { history in
history.append(.init(
role: .system,
content: "",
summary: "No plugin running."
))
}
}
return true
}
// pass message to running plugin
if let runningPlugin {
await runningPlugin.send(content: content, originalMessage: content)
return true
}
// pass message to new plugin
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 == " " })
),
originalMessage: content
)
} else {
await plugin.send(
content: String(content.dropFirst(command.count + 1)),
originalMessage: content
)
}
return true
}
return false
} else if let runningPlugin {
// pass message to running plugin
await runningPlugin.send(content: content, originalMessage: content)
return true
} else {
return false
}
}
func stopResponding() async {
await runningPlugin?.stopResponding()
}
func cancel() async {
await runningPlugin?.cancel()
}
}
// MARK: - ChatPluginDelegate
extension ChatPluginController: ChatPluginDelegate {
public func pluginDidStartResponding(_: ChatPlugin) {
chatService?.isReceivingMessage = true
}
public func pluginDidEndResponding(_: ChatPlugin) {
chatService?.isReceivingMessage = 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, originalMessage: content)
}
}
}