|
| 1 | +import ChatBasic |
| 2 | +import Foundation |
| 3 | +import Terminal |
| 4 | + |
| 5 | +public final class ShortcutChatPlugin: ChatPlugin { |
| 6 | + public static var id: String { "com.intii.shortcut" } |
| 7 | + public static var command: String { "shortcut" } |
| 8 | + public static var name: String { "Shortcut" } |
| 9 | + public static var description: String { """ |
| 10 | + Run a shortcut and use message content as input. You need to provide the shortcut name as an argument, for example, `/shortcut(Shortcut Name)`. |
| 11 | + """ } |
| 12 | + |
| 13 | + let terminal: TerminalType |
| 14 | + |
| 15 | + init(terminal: TerminalType) { |
| 16 | + self.terminal = terminal |
| 17 | + } |
| 18 | + |
| 19 | + public init() { |
| 20 | + terminal = Terminal() |
| 21 | + } |
| 22 | + |
| 23 | + public func send(_ request: Request) async -> AsyncThrowingStream<Response, any Error> { |
| 24 | + return .init { continuation in |
| 25 | + let task = Task { |
| 26 | + let id = "\(Self.command)-\(UUID().uuidString)" |
| 27 | + |
| 28 | + guard let shortcutName = request.arguments.first, !shortcutName.isEmpty else { |
| 29 | + continuation.yield(.content(.text( |
| 30 | + "Please provide the shortcut name in format: `/\(Self.command)(shortcut name)`" |
| 31 | + ))) |
| 32 | + return |
| 33 | + } |
| 34 | + |
| 35 | + var input = String(request.text).trimmingCharacters(in: .whitespacesAndNewlines) |
| 36 | + if input.isEmpty { |
| 37 | + // if no input detected, use the previous message as input |
| 38 | + input = request.history.last?.content ?? "" |
| 39 | + } |
| 40 | + |
| 41 | + do { |
| 42 | + continuation.yield(.startAction( |
| 43 | + id: "run", |
| 44 | + task: "Run shortcut `\(shortcutName)`" |
| 45 | + )) |
| 46 | + |
| 47 | + let env = ProcessInfo.processInfo.environment |
| 48 | + let shell = env["SHELL"] ?? "/bin/bash" |
| 49 | + let temporaryURL = FileManager.default.temporaryDirectory |
| 50 | + let temporaryInputFileURL = temporaryURL |
| 51 | + .appendingPathComponent("\(id)-input.txt") |
| 52 | + let temporaryOutputFileURL = temporaryURL |
| 53 | + .appendingPathComponent("\(id)-output") |
| 54 | + |
| 55 | + try input.write(to: temporaryInputFileURL, atomically: true, encoding: .utf8) |
| 56 | + |
| 57 | + let command = """ |
| 58 | + shortcuts run "\(shortcutName)" \ |
| 59 | + -i "\(temporaryInputFileURL.path)" \ |
| 60 | + -o "\(temporaryOutputFileURL.path)" |
| 61 | + """ |
| 62 | + |
| 63 | + continuation.yield(.startAction( |
| 64 | + id: "run", |
| 65 | + task: "Run shortcut \(shortcutName)" |
| 66 | + )) |
| 67 | + |
| 68 | + do { |
| 69 | + let result = try await terminal.runCommand( |
| 70 | + shell, |
| 71 | + arguments: ["-i", "-l", "-c", command], |
| 72 | + currentDirectoryURL: nil, |
| 73 | + environment: [:] |
| 74 | + ) |
| 75 | + continuation.yield(.finishAction(id: "run", result: .success(result))) |
| 76 | + } catch { |
| 77 | + continuation.yield(.finishAction( |
| 78 | + id: "run", |
| 79 | + result: .failure(error.localizedDescription) |
| 80 | + )) |
| 81 | + throw error |
| 82 | + } |
| 83 | + |
| 84 | + await Task.yield() |
| 85 | + try Task.checkCancellation() |
| 86 | + |
| 87 | + if FileManager.default.fileExists(atPath: temporaryOutputFileURL.path) { |
| 88 | + let data = try Data(contentsOf: temporaryOutputFileURL) |
| 89 | + if let text = String(data: data, encoding: .utf8) { |
| 90 | + var response = text |
| 91 | + if response.isEmpty { |
| 92 | + response = "Finished" |
| 93 | + } |
| 94 | + continuation.yield(.content(.text(response))) |
| 95 | + } else { |
| 96 | + let content = """ |
| 97 | + [View File](\(temporaryOutputFileURL)) |
| 98 | + """ |
| 99 | + continuation.yield(.content(.text(content))) |
| 100 | + } |
| 101 | + } else { |
| 102 | + continuation.yield(.content(.text("Finished"))) |
| 103 | + } |
| 104 | + |
| 105 | + } catch { |
| 106 | + continuation.yield(.content(.text(error.localizedDescription))) |
| 107 | + } |
| 108 | + |
| 109 | + continuation.finish() |
| 110 | + } |
| 111 | + |
| 112 | + continuation.onTermination = { _ in |
| 113 | + task.cancel() |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | + |
0 commit comments