Branchwell

Bucksley / app · bucksley.ai / Apps / Mac / AgentSkillSection.swift

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