|
| 1 | +import Environment |
| 2 | +import Foundation |
| 3 | +import OpenAIService |
| 4 | +import Terminal |
| 5 | + |
| 6 | +public actor TerminalChatPlugin: ChatPlugin { |
| 7 | + public static var command: String { "run" } |
| 8 | + public nonisolated var name: String { "Terminal" } |
| 9 | + |
| 10 | + let chatGPTService: ChatGPTServiceType |
| 11 | + var terminal: TerminalType = Terminal() |
| 12 | + var isCancelled = false |
| 13 | + weak var delegate: ChatPluginDelegate? |
| 14 | + |
| 15 | + public init(inside chatGPTService: ChatGPTServiceType, delegate: ChatPluginDelegate) { |
| 16 | + self.chatGPTService = chatGPTService |
| 17 | + self.delegate = delegate |
| 18 | + } |
| 19 | + |
| 20 | + public func send(content: String) async { |
| 21 | + delegate?.pluginDidStart(self) |
| 22 | + |
| 23 | + let id = "\(Self.command)-\(UUID().uuidString)" |
| 24 | + var message = ChatMessage(id: id, role: .assistant, content: "") |
| 25 | + var outputContent = "" { |
| 26 | + didSet { |
| 27 | + message.content = """ |
| 28 | + ``` |
| 29 | + \(outputContent) |
| 30 | + ``` |
| 31 | + """ |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + do { |
| 36 | + let fileURL = try await Environment.fetchCurrentFileURL() |
| 37 | + let projectURL = try await Environment.fetchCurrentProjectRootURL(fileURL) |
| 38 | + |
| 39 | + await chatGPTService.mutateHistory { history in |
| 40 | + history.append(.init(role: .user, content: "Run command: \(content)")) |
| 41 | + } |
| 42 | + |
| 43 | + if isCancelled { throw CancellationError() } |
| 44 | + |
| 45 | + let output = terminal.streamCommand( |
| 46 | + "/bin/bash", |
| 47 | + arguments: ["-c", content], |
| 48 | + currentDirectoryPath: projectURL?.path ?? fileURL.path, |
| 49 | + environment: [ |
| 50 | + "PROJECT_ROOT": projectURL?.path ?? fileURL.path, |
| 51 | + "FILE_PATH": fileURL.path, |
| 52 | + ] |
| 53 | + ) |
| 54 | + |
| 55 | + for try await content in output { |
| 56 | + if isCancelled { throw CancellationError() } |
| 57 | + await chatGPTService.mutateHistory { history in |
| 58 | + if history.last?.id == id { |
| 59 | + history.removeLast() |
| 60 | + } |
| 61 | + outputContent += content |
| 62 | + history.append(message) |
| 63 | + } |
| 64 | + } |
| 65 | + outputContent += "\n[finished]" |
| 66 | + await chatGPTService.mutateHistory { history in |
| 67 | + if history.last?.id == id { |
| 68 | + history.removeLast() |
| 69 | + } |
| 70 | + history.append(message) |
| 71 | + } |
| 72 | + } catch let error as Terminal.TerminationError { |
| 73 | + outputContent += "\n[error: \(error.status)]" |
| 74 | + await chatGPTService.mutateHistory { history in |
| 75 | + if history.last?.id == id { |
| 76 | + history.removeLast() |
| 77 | + } |
| 78 | + history.append(message) |
| 79 | + } |
| 80 | + } catch { |
| 81 | + outputContent += "\n[error: \(error.localizedDescription)]" |
| 82 | + await chatGPTService.mutateHistory { history in |
| 83 | + if history.last?.id == id { |
| 84 | + history.removeLast() |
| 85 | + } |
| 86 | + history.append(message) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + delegate?.pluginDidEnd(self) |
| 91 | + } |
| 92 | + |
| 93 | + public func cancel() async { |
| 94 | + isCancelled = true |
| 95 | + await terminal.terminate() |
| 96 | + } |
| 97 | +} |
0 commit comments