Branchwell

Jotley / app / macOS / Views / AgentSkillSection.swift

1import AppKit
2import SwiftUI
3
4/// Settings block that installs Jotley's agent skill beside the MCP connection steps.
5/// Shared layout; the sandboxed app asks once for each agent folder.
6struct AgentSkillSection: View {
7 private let installer = AgentSkillInstaller(app: "Jotley", skillID: "jotley-mcp")
8 @State private var statuses: [AgentSkillInstaller.Status] = []
9 @State private var busy: AgentSkillInstaller.Agent?
10 @State private var failures: [AgentSkillInstaller.Agent: String] = [:]
11 @State private var installedThisSession = false
12 @State private var replacing: AgentSkillInstaller.Status?
13
14 var body: some View {
15 VStack(alignment: .leading, spacing: 12) {
16 VStack(alignment: .leading, spacing: 4) {
17 Text("Agent skill").font(.headline)
18 Text("Teaches your agent Jotley’s tools and when to use them.")
19 .font(.callout).foregroundStyle(.secondary)
20 }
21 if installer == nil {
22 Text("This build doesn’t include the skill.").font(.callout).foregroundStyle(.secondary)
23 }
24 VStack(alignment: .leading, spacing: 0) {
25 ForEach(statuses, id: \.agent) { status in
26 row(status)
27 if status.agent != statuses.last?.agent { Divider() }
28 }
29 }
30 HStack(spacing: 14) {
31 if installedThisSession {
32 Text("New agent sessions will load it.").font(.callout).foregroundStyle(.secondary)
33 }
34 Button("Show in Finder", action: showInFinder).font(.callout)
35 }
36 }
37 .frame(maxWidth: .infinity, alignment: .leading)
38 .onAppear(perform: refresh)
39 .onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in refresh() }
40 .alert("Replace the jotley-mcp skill?", isPresented: Binding(get: { replacing != nil }, set: { if !$0 { replacing = nil } })) {
41 Button("Cancel", role: .cancel) { replacing = nil }
42 Button("Replace") {
43 if let status = replacing { install(status.agent, replacing: true) }
44 replacing = nil
45 }
46 } message: { Text("The existing folder moves to the Trash.") }
47 }
48
49 private func row(_ status: AgentSkillInstaller.Status) -> some View {
50 HStack(spacing: 12) {
51 VStack(alignment: .leading, spacing: 3) {
52 Text(status.agent.title)
53 Text(caption(status))
54 .font(.caption.monospaced())
55 .foregroundStyle(.secondary)
56 .lineLimit(1)
57 .truncationMode(.middle)
58 .help(status.location.path)
59 if let failure = failures[status.agent] {
60 Text(failure).font(.caption).foregroundStyle(Color.red).lineLimit(1).help(failure)
61 }
62 }
63 Spacer(minLength: 12)
64 trailing(status)
65 }
66 .padding(.vertical, 9)
67 .opacity(dimmed(status) ? 0.6 : 1)
68 }
69
70 @ViewBuilder private func trailing(_ status: AgentSkillInstaller.Status) -> some View {
71 if busy == status.agent {
72 ProgressView().controlSize(.small).accessibilityLabel("Installing")
73 } else if let installer, !installer.hasAccess(status.agent) {
74 Button("Install…") { install(status.agent) }
75 .accessibilityLabel("Install the Jotley skill for \(status.agent.title)")
76 .disabled(busy != nil)
77 } else {
78 switch status.state {
79 case .installed:
80 Label("Installed", systemImage: "checkmark").foregroundStyle(.secondary)
81 case .plugin:
82 Label("Installed by Claude plugin", systemImage: "checkmark").foregroundStyle(.secondary)
83 case .updateAvailable:
84 Button("Update") { install(status.agent) }.disabled(busy != nil)
85 case .differentVersion:
86 HStack(spacing: 8) {
87 Text("Different skill found").foregroundStyle(.secondary)
88 Button("Replace…") { replacing = status }.disabled(busy != nil)
89 }
90 case .notInstalled:
91 Button("Install") { install(status.agent) }
92 .accessibilityLabel("Install the Jotley skill for \(status.agent.title)")
93 .disabled(busy != nil || installer == nil)
94 }
95 }
96 }
97
98 private func dimmed(_ status: AgentSkillInstaller.Status) -> Bool {
99 guard let installer, installer.hasAccess(status.agent) else { return false }
100 return !status.agentFound && status.state == .notInstalled
101 }
102
103 private func caption(_ status: AgentSkillInstaller.Status) -> String {
104 if case .plugin(let name) = status.state { return "Claude plugin \(name)" }
105 let path = status.location.path.replacingOccurrences(of: AgentSkillInstaller.userHome.path, with: "~")
106 if dimmed(status) { return "Not found on this Mac · \(path)" }
107 return status.shared ? "\(path) · shared with \(status.agent == .codex ? "Gemini CLI" : "Codex")" : path
108 }
109
110 private func refresh() {
111 guard let installer else { statuses = []; return }
112 statuses = installer.withAccess { installer.statuses() }
113 }
114
115 private func install(_ agent: AgentSkillInstaller.Agent, replacing: Bool = false) {
116 guard let installer, busy == nil else { return }
117 failures[agent] = nil
118 if !installer.hasAccess(agent) {
119 guard grantAccess(agent, installer: installer) else { return }
120 refresh()
121 // A skill already there after the grant is left alone until the person chooses again.
122 if statuses.first(where: { $0.agent == agent })?.state != .notInstalled { return }
123 }
124 busy = agent
125 Task {
126 defer { busy = nil; refresh() }
127 do {
128 // Off the main thread: moving an old copy to the Trash can take a while.
129 try await Task.detached { try installer.withAccess { try installer.install(agent, replacing: replacing) } }.value
130 installedThisSession = true
131 } catch {
132 failures[agent] = error.localizedDescription
133 }
134 }
135 }
136
137 /// The App Sandbox needs one explicit grant of the agent's folder, chosen in an open panel.
138 private func grantAccess(_ agent: AgentSkillInstaller.Agent, installer: AgentSkillInstaller) -> Bool {
139 let folder = installer.accessFolder(agent)
140 let exists = FileManager.default.fileExists(atPath: folder.path)
141 let panel = NSOpenPanel()
142 panel.title = "Allow Jotley to add its skill"
143 panel.message = exists
144 ? "Choose the \(folder.lastPathComponent) folder so Jotley can add its skill for \(agent.title)."
145 : "Choose your home folder so Jotley can create \(folder.lastPathComponent) and add its skill for \(agent.title)."
146 panel.prompt = "Allow"
147 panel.canChooseFiles = false
148 panel.canChooseDirectories = true
149 panel.showsHiddenFiles = true
150 panel.directoryURL = exists ? folder : AgentSkillInstaller.userHome
151 guard panel.runModal() == .OK, let chosen = panel.url else { return false }
152 guard installer.rememberGrant(chosen, for: agent) else {
153 failures[agent] = "Choose \(folder.path) or your home folder."
154 return false
155 }
156 return true
157 }
158
159 private func showInFinder() {
160 let installed = statuses.first { status in
161 if case .installed = status.state { return true }
162 return status.state == .updateAvailable || status.state == .differentVersion
163 }?.location.appending(path: "SKILL.md")
164 guard let url = installed ?? AgentSkillInstaller.bundledSkill() else { return }
165 NSWorkspace.shared.activateFileViewerSelecting([url])
166 }
167}