Branchwell

Glimmer / app · glimmertoday.ai / native / Glimmer / AgentSkillSection.swift

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