|
| 1 | +import ChatPlugins |
| 2 | +import Combine |
| 3 | +import Foundation |
| 4 | +import OpenAIService |
| 5 | + |
| 6 | +final class ChatPluginController { |
| 7 | + let chatGPTService: any ChatGPTServiceType |
| 8 | + let plugins: [String: ChatPlugin.Type] |
| 9 | + var runningPlugin: ChatPlugin? |
| 10 | + |
| 11 | + init(chatGPTService: any ChatGPTServiceType, plugins: ChatPlugin.Type...) { |
| 12 | + self.chatGPTService = chatGPTService |
| 13 | + var all = [String: ChatPlugin.Type]() |
| 14 | + for plugin in plugins { |
| 15 | + all[plugin.command] = plugin |
| 16 | + } |
| 17 | + self.plugins = all |
| 18 | + } |
| 19 | + |
| 20 | + /// Handle the message in a plugin if required. Return false if no plugin handles the message. |
| 21 | + func handleContent(_ content: String) async throws -> Bool { |
| 22 | + // look for the prefix of content, see if there is something like /command. |
| 23 | + // If there is, then we need to find the plugin that can handle this command. |
| 24 | + // If there is no such plugin, then we just send the message to the GPT service. |
| 25 | + let regex = try NSRegularExpression(pattern: #"^\/([a-zA-Z0-9]+)"#) |
| 26 | + let matches = regex.matches(in: content, range: NSRange(content.startIndex..., in: content)) |
| 27 | + if let match = matches.first { |
| 28 | + let command = String(content[Range(match.range(at: 1), in: content)!]) |
| 29 | + // handle exit plugin |
| 30 | + if command == "exit" { |
| 31 | + if let plugin = runningPlugin { |
| 32 | + runningPlugin = nil |
| 33 | + _ = await chatGPTService.mutateHistory { history in |
| 34 | + history.append(.init( |
| 35 | + role: .user, |
| 36 | + content: "", |
| 37 | + summary: "Exit plugin \(plugin.name)." |
| 38 | + )) |
| 39 | + history.append(.init( |
| 40 | + role: .system, |
| 41 | + content: "", |
| 42 | + summary: "Exited plugin \(plugin.name)." |
| 43 | + )) |
| 44 | + } |
| 45 | + } else { |
| 46 | + _ = await chatGPTService.mutateHistory { history in |
| 47 | + history.append(.init( |
| 48 | + role: .system, |
| 49 | + content: "", |
| 50 | + summary: "No plugin running." |
| 51 | + )) |
| 52 | + } |
| 53 | + } |
| 54 | + return true |
| 55 | + } |
| 56 | + |
| 57 | + // pass message to running plugin |
| 58 | + if let runningPlugin { |
| 59 | + await runningPlugin.send(content: content, originalMessage: content) |
| 60 | + return true |
| 61 | + } |
| 62 | + |
| 63 | + // pass message to new plugin |
| 64 | + if let pluginType = plugins[command] { |
| 65 | + let plugin = pluginType.init(inside: chatGPTService, delegate: self) |
| 66 | + if #available(macOS 13.0, *) { |
| 67 | + await plugin.send( |
| 68 | + content: String( |
| 69 | + content.dropFirst(command.count + 1) |
| 70 | + .trimmingPrefix(while: { $0 == " " }) |
| 71 | + ), |
| 72 | + originalMessage: content |
| 73 | + ) |
| 74 | + } else { |
| 75 | + await plugin.send( |
| 76 | + content: String(content.dropFirst(command.count + 1)), |
| 77 | + originalMessage: content |
| 78 | + ) |
| 79 | + } |
| 80 | + return true |
| 81 | + } |
| 82 | + |
| 83 | + return false |
| 84 | + } else if let runningPlugin { |
| 85 | + // pass message to running plugin |
| 86 | + await runningPlugin.send(content: content, originalMessage: content) |
| 87 | + return true |
| 88 | + } else { |
| 89 | + return false |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + func stopResponding() async { |
| 94 | + await runningPlugin?.stopResponding() |
| 95 | + } |
| 96 | + |
| 97 | + func cancel() async { |
| 98 | + await runningPlugin?.cancel() |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// MARK: - ChatPluginDelegate |
| 103 | + |
| 104 | +extension ChatPluginController: ChatPluginDelegate { |
| 105 | + public func pluginDidStartResponding(_: ChatPlugins.ChatPlugin) { |
| 106 | + Task { |
| 107 | + await chatGPTService.markReceivingMessage(true) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public func pluginDidEndResponding(_: ChatPlugins.ChatPlugin) { |
| 112 | + Task { |
| 113 | + await chatGPTService.markReceivingMessage(false) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + public func pluginDidStart(_ plugin: ChatPlugin) { |
| 118 | + runningPlugin = plugin |
| 119 | + } |
| 120 | + |
| 121 | + public func pluginDidEnd(_ plugin: ChatPlugin) { |
| 122 | + if runningPlugin === plugin { |
| 123 | + runningPlugin = nil |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + public func shouldStartAnotherPlugin(_ type: ChatPlugin.Type, withContent content: String) { |
| 128 | + let plugin = type.init(inside: chatGPTService, delegate: self) |
| 129 | + Task { |
| 130 | + await plugin.send(content: content, originalMessage: content) |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
0 commit comments