|
| 1 | +import Environment |
| 2 | +import Foundation |
| 3 | +import OpenAIService |
| 4 | +import Terminal |
| 5 | + |
| 6 | +public actor AITerminalChatPlugin: ChatPlugin { |
| 7 | + public static var command: String { "airun" } |
| 8 | + public nonisolated var name: String { "AI Terminal" } |
| 9 | + |
| 10 | + let chatGPTService: any ChatGPTServiceType |
| 11 | + var terminal: TerminalType = Terminal() |
| 12 | + var isCancelled = false |
| 13 | + weak var delegate: ChatPluginDelegate? |
| 14 | + var isStarted = false |
| 15 | + var command: String? |
| 16 | + |
| 17 | + public init(inside chatGPTService: any ChatGPTServiceType, delegate: ChatPluginDelegate) { |
| 18 | + self.chatGPTService = chatGPTService |
| 19 | + self.delegate = delegate |
| 20 | + } |
| 21 | + |
| 22 | + public func send(content: String) async { |
| 23 | + if !isStarted { |
| 24 | + isStarted = true |
| 25 | + delegate?.pluginDidStart(self) |
| 26 | + } |
| 27 | + |
| 28 | + do { |
| 29 | + if let command { |
| 30 | + await chatGPTService.mutateHistory { history in |
| 31 | + history.append(.init(role: .user, content: content)) |
| 32 | + } |
| 33 | + delegate?.pluginDidStartResponding(self) |
| 34 | + if try await checkConfirmation(content: content) { |
| 35 | + delegate?.pluginDidEndResponding(self) |
| 36 | + delegate?.pluginDidEnd(self) |
| 37 | + delegate?.shouldStartAnotherPlugin( |
| 38 | + TerminalChatPlugin.self, |
| 39 | + withContent: command |
| 40 | + ) |
| 41 | + } else { |
| 42 | + delegate?.pluginDidEndResponding(self) |
| 43 | + delegate?.pluginDidEnd(self) |
| 44 | + await chatGPTService.mutateHistory { history in |
| 45 | + history.append(.init(role: .assistant, content: "Cancelled")) |
| 46 | + } |
| 47 | + } |
| 48 | + } else { |
| 49 | + await chatGPTService.mutateHistory { history in |
| 50 | + history.append(.init(role: .user, content: "Run a command to \(content)")) |
| 51 | + } |
| 52 | + delegate?.pluginDidStartResponding(self) |
| 53 | + let result = try await generateCommand(task: content) |
| 54 | + command = result |
| 55 | + await chatGPTService.mutateHistory { history in |
| 56 | + history.append(.init(role: .assistant, content: """ |
| 57 | + Confirm to run? |
| 58 | + ``` |
| 59 | + \(result) |
| 60 | + ``` |
| 61 | + """)) |
| 62 | + } |
| 63 | + delegate?.pluginDidEndResponding(self) |
| 64 | + } |
| 65 | + } catch { |
| 66 | + await chatGPTService.mutateHistory { history in |
| 67 | + history.append(.init(role: .assistant, content: error.localizedDescription)) |
| 68 | + } |
| 69 | + delegate?.pluginDidEndResponding(self) |
| 70 | + delegate?.pluginDidEnd(self) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public func cancel() async { |
| 75 | + isCancelled = true |
| 76 | + } |
| 77 | + |
| 78 | + public func stopResponding() async {} |
| 79 | + |
| 80 | + func callAIFunction( |
| 81 | + function: String, |
| 82 | + args: [Any?], |
| 83 | + description: String |
| 84 | + ) async throws -> String { |
| 85 | + let args = args.map { arg -> String in |
| 86 | + if let arg = arg { |
| 87 | + return String(describing: arg) |
| 88 | + } else { |
| 89 | + return "None" |
| 90 | + } |
| 91 | + } |
| 92 | + let argsString = args.joined(separator: ", ") |
| 93 | + let service = ChatGPTService( |
| 94 | + systemPrompt: "You are now the following python function: ```# \(description)\n\(function)```\n\nOnly respond with your `return` value." |
| 95 | + ) |
| 96 | + return try await service.sendAndWait(content: argsString) |
| 97 | + } |
| 98 | + |
| 99 | + func generateCommand(task: String) async throws -> String { |
| 100 | + let f = "def generate_terminal_command(task: str) -> string:" |
| 101 | + let d = """ |
| 102 | + Available environment variables: |
| 103 | + - $PROJECT_ROOT: the root path of the project |
| 104 | + - $FILE_PATH: the currently editing file |
| 105 | +
|
| 106 | + Current directory path is the project root. |
| 107 | +
|
| 108 | + The return value should not be embedded in a markdown code block. |
| 109 | +
|
| 110 | + Generate a terminal command to solve the given task on macOS. If one command is not enough, you can use && to concatenate multiple commands. |
| 111 | + """ |
| 112 | + |
| 113 | + return try await callAIFunction(function: f, args: [task], description: d) |
| 114 | + .replacingOccurrences(of: "`", with: "") |
| 115 | + .replacingOccurrences(of: "\n", with: "") |
| 116 | + } |
| 117 | + |
| 118 | + func checkConfirmation(content: String) async throws -> Bool { |
| 119 | + let f = "def check_confirmation(content: str) -> bool:" |
| 120 | + let d = """ |
| 121 | + Check if the given content is a phrase or sentence that considered a confirmation to run a command. |
| 122 | +
|
| 123 | + For example: "Yes", "Confirm", "True", "Please run it". It can be in any language. |
| 124 | + """ |
| 125 | + |
| 126 | + let result = try await callAIFunction(function: f, args: [content], description: d) |
| 127 | + return result.lowercased().contains("true") |
| 128 | + } |
| 129 | +} |
0 commit comments