|
| 1 | +import ChatPlugins |
| 2 | +import Foundation |
| 3 | +import OpenAIService |
| 4 | + |
| 5 | +public final class ChatService: ObservableObject { |
| 6 | + let chatGPTService: ChatGPTServiceType |
| 7 | + let plugins = registerPlugins( |
| 8 | + TerminalChatPlugin.self |
| 9 | + ) |
| 10 | + var runningPlugin: ChatPlugin? |
| 11 | + |
| 12 | + public init(chatGPTService: ChatGPTServiceType) { |
| 13 | + self.chatGPTService = chatGPTService |
| 14 | + } |
| 15 | + |
| 16 | + deinit { |
| 17 | + print("deinit") |
| 18 | + } |
| 19 | + |
| 20 | + public func send(content: String) async throws { |
| 21 | + // look for the prefix of content, see if there is something like /command. |
| 22 | + // If there is, then we need to find the plugin that can handle this command. |
| 23 | + // If there is no such plugin, then we just send the message to the GPT service. |
| 24 | + let regex = try NSRegularExpression(pattern: #"^\/([a-zA-Z0-9]+)"#) |
| 25 | + let matches = regex.matches(in: content, range: NSRange(content.startIndex..., in: content)) |
| 26 | + if let match = matches.first { |
| 27 | + let command = String(content[Range(match.range(at: 1), in: content)!]) |
| 28 | + if let pluginType = plugins[command] { |
| 29 | + let plugin = pluginType.init(inside: chatGPTService, delegate: self) |
| 30 | + await plugin.send(content: String(content.dropFirst(command.count + 1))) |
| 31 | + } |
| 32 | + } else { |
| 33 | + _ = try await chatGPTService.send(content: content, summary: nil) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + public func stopReceivingMessage() async { |
| 38 | + if let runningPlugin { |
| 39 | + await runningPlugin.cancel() |
| 40 | + } |
| 41 | + await chatGPTService.stopReceivingMessage() |
| 42 | + } |
| 43 | + |
| 44 | + public func clearHistory() async { |
| 45 | + if let runningPlugin { |
| 46 | + await runningPlugin.cancel() |
| 47 | + } |
| 48 | + await chatGPTService.clearHistory() |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +extension ChatService: ChatPluginDelegate { |
| 53 | + public func pluginDidStart(_ plugin: ChatPlugin) { |
| 54 | + Task { |
| 55 | + await chatGPTService.markReceivingMessage(true) |
| 56 | + } |
| 57 | + runningPlugin = plugin |
| 58 | + } |
| 59 | + |
| 60 | + public func pluginDidEnd(_ plugin: ChatPlugin) { |
| 61 | + Task { |
| 62 | + await chatGPTService.markReceivingMessage(false) |
| 63 | + } |
| 64 | + runningPlugin = nil |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func registerPlugins(_ plugins: ChatPlugin.Type...) -> [String: ChatPlugin.Type] { |
| 69 | + var all = [String: ChatPlugin.Type]() |
| 70 | + for plugin in plugins { |
| 71 | + all[plugin.command] = plugin |
| 72 | + } |
| 73 | + return all |
| 74 | +} |
0 commit comments