Branchwell

Glimmer / app / native / Glimmer / AgentTutorial.swift

1#if os(macOS)
2import SwiftUI
3
4enum AgentGuideStep: Int, CaseIterable {
5 case access, connect, verify
6 var title: String {
7 switch self {
8 case .access: "Choose what to share"
9 case .connect: "Connect your agent"
10 case .verify: "Try your first question"
11 }
12 }
13}
14
15struct AgentTutorial: View {
16 @Environment(\.dismiss) private var dismiss
17 @State var step: AgentGuideStep = .access
18 var body: some View {
19 VStack(alignment: .leading, spacing: 16) {
20 HStack { Text("Agent setup").font(.headline); Spacer(); Button("Close") { dismiss() }.keyboardShortcut(.cancelAction).glimmerButton() }
21 StepDots(current: step.rawValue, total: AgentGuideStep.allCases.count)
22 Text(step.title).font(GlimmerFont.reading(.title, weight: .medium))
23 ScrollView {
24 VStack(alignment: .leading, spacing: 14) {
25 if step == .access {
26 Text("MCP connects an assistant on this Mac to Glimmer. Ask for today’s reflection or a practice to add to your daily dashboard.")
27 } else if step == .connect {
28 Text("Choose the app where you want to ask Glimmer a question.")
29 } else {
30 Text("Test the local connector here. Then open your assistant and ask:")
31 Text("“What’s my daily Glimmer, and what’s one practice I can try today?”")
32 .font(GlimmerFont.reading(.title3)).textSelection(.enabled)
33 }
34 AgentSetupContent(section: step).id(step)
35 }.frame(maxWidth: .infinity, alignment: .leading)
36 }
37 Divider()
38 HStack {
39 if step != .access {
40 Button("Back") { step = AgentGuideStep(rawValue: step.rawValue - 1) ?? .access }.glimmerButton()
41 }
42 Spacer()
43 Button(step == .verify ? "Done" : "Continue") {
44 if let next = AgentGuideStep(rawValue: step.rawValue + 1) { step = next } else { dismiss() }
45 }.keyboardShortcut(.defaultAction).glimmerButton(.prominent)
46 }
47 }.padding(24).frame(width: 520, height: 470).background { PageBackdrop() }
48 }
49}
50
51struct AgentSetupContent: View {
52 let section: AgentGuideStep
53 @Environment(JournalStore.self) private var store
54 @Environment(\.readingTheme) private var theme
55 @State private var agentError = false
56 @State private var agentClient = "Claude Code"
57 @State private var agentMessage: String?
58 @State private var testingAgent = false
59
60 var body: some View {
61 VStack(alignment: .leading, spacing: 12) {
62 switch section {
63 case .access:
64 agentToggle("Share read-only summaries", detail: "Daily coaching, the practice catalog, grouped ratings and plan counts",
65 isOn: store.agentLevel != .off) { $0 ? .summaries : .off }
66 agentToggle("Let agents log and check off", detail: "Save practices, check off tasks, log moments and record energy",
67 isOn: store.agentLevel == .changes) { $0 ? .changes : .summaries }
68 .disabled(store.agentLevel == .off)
69 Text(store.agentLevel == .changes
70 ? "Connected agents can read today’s plan with task names and add to your journal. They may send what they read to their model provider."
71 : "Connected agents can read today’s coaching, its saved/completed state, public exercises and summary counts. They may send the results to their model provider. You can turn access off here at any time.")
72 .font(.callout).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true)
73 if store.agentLevel == .summaries {
74 Label("Never shared: journal entries, custom source names, task titles or onboarding answers.", systemImage: "eye.slash")
75 .font(.callout).foregroundStyle(.secondary)
76 }
77 if store.agentLevel != .off {
78 Text("For a daily dashboard, ask your agent to fetch the Daily Glimmer on your schedule. Enable check-offs if you want it to save a practice and record completion when you report it.")
79 .font(.callout).foregroundStyle(.secondary).fixedSize(horizontal: false, vertical: true)
80 }
81 if agentError {
82 Label("Couldn’t change agent access. Please try again.", systemImage: "exclamationmark.triangle")
83 .font(.callout).foregroundStyle(GlimmerColor.ember)
84 }
85
86 case .connect:
87 GlassSegments(label: "Client", options: ["Claude Code", "Codex", "Gemini CLI", "Claude chat"].map { GlassSegment(value: $0, title: $0) },
88 selection: $agentClient)
89 agentClientSetup
90
91 case .verify:
92 if store.agentLevel == .off {
93 Label("Sharing is off. Enable read-only summaries in Choose what to share to test the connection.", systemImage: "hand.raised")
94 } else {
95 HStack(spacing: 10) {
96 Button("Test Connection", systemImage: "bolt.horizontal") { testAgentConnection() }
97 .glimmerButton(.standard).disabled(testingAgent)
98 if testingAgent { ProgressView().controlSize(.small) }
99 Spacer()
100 Link(destination: URL(string: "https://glimmertoday.ai/mcp")!) {
101 Label("Setup guide", systemImage: "book")
102 }
103 .font(.callout)
104 }
105 if let activity = store.agentActivity, let client = activity.client, let date = activity.clientDate {
106 LabeledContent("Last agent", value: client + " · " + date.formatted(date: .abbreviated, time: .shortened))
107 .font(.callout)
108 if let tool = activity.tool, let toolDate = activity.toolDate {
109 LabeledContent("Last request", value: tool + " · " + toolDate.formatted(date: .abbreviated, time: .shortened))
110 .font(.callout)
111 }
112 }
113
114 }
115 }
116 if let agentMessage {
117 Text(agentMessage).font(.callout).foregroundStyle(.secondary).textSelection(.enabled)
118 }
119 }
120 .onChange(of: agentClient) { agentMessage = nil }
121 }
122
123 private func agentToggle(_ title: String, detail: String, isOn: Bool,
124 level: @escaping (Bool) -> AgentAccess.Level) -> some View {
125 HStack(alignment: .center, spacing: 12) {
126 VStack(alignment: .leading, spacing: 2) {
127 Text(title).font(.body.weight(.semibold))
128 Text(detail).font(.caption).foregroundStyle(.secondary)
129 }
130 Spacer()
131 Toggle(title, isOn: Binding(get: { isOn }, set: { enabled in
132 do {
133 try store.setAgentLevel(level(enabled))
134 agentError = false
135 } catch { agentError = true }
136 agentMessage = nil
137 }))
138 .toggleStyle(.switch).labelsHidden().accessibilityLabel(title).accessibilityHint(detail)
139 }
140 }
141
142 @ViewBuilder private var agentClientSetup: some View {
143 switch agentClient {
144 case "Codex":
145 copyField("Command", AgentSetup.command)
146 copyField("Argument", AgentSetup.argument)
147 Text("In Codex Settings → MCP servers, add a STDIO server named glimmer. Paste both fields, save, then type /mcp in a local chat.")
148 .font(.callout).fixedSize(horizontal: false, vertical: true)
149 case "Gemini CLI":
150 Text("Choose the project folder you use with Gemini CLI. Glimmer adds its local connection to .gemini/settings.json and keeps your other settings.")
151 .fixedSize(horizontal: false, vertical: true)
152 Button("Save Gemini connection…", systemImage: "folder.badge.plus") {
153 runSetup { try AgentSetup.saveProjectConnection(gemini: true) }
154 }.glimmerButton()
155 Text("Restart Gemini in that trusted project and enter /mcp to see Glimmer’s tools.")
156 .font(.callout)
157 case "Claude chat":
158 Text("Save the extension, then in the Claude app open Settings → Extensions → Advanced settings → Install Extension… and choose Glimmer.mcpb. Start a new chat and check Connectors.")
159 .fixedSize(horizontal: false, vertical: true)
160 Button("Save Claude Extension…", systemImage: "puzzlepiece.extension") {
161 runSetup(AgentSetup.saveClaudeExtension)
162 }
163 .glimmerButton(.standard)
164 default:
165 Text("Save the connection in the folder you open in Claude Code. Then open that folder in Claude’s Code tab, start a new local session on This computer and approve Glimmer.")
166 .fixedSize(horizontal: false, vertical: true)
167 Button("Save Project Connection…", systemImage: "folder.badge.plus") {
168 runSetup { try AgentSetup.saveProjectConnection() }
169 }
170 .glimmerButton(.standard)
171 }
172 }
173
174 private func runSetup(_ action: () throws -> String?) {
175 do {
176 if let result = try action() { agentMessage = result }
177 } catch {
178 agentMessage = error.localizedDescription
179 }
180 }
181
182 private func testAgentConnection() {
183 testingAgent = true
184 agentMessage = nil
185 let mode = store.agentLevel == .changes ? "summaries, logging and check-offs" : "read-only summaries"
186 Task {
187 switch await AgentSetup.testConnection(executable: AgentSetup.command) {
188 case .success(let count): agentMessage = "Connection check passed. Glimmer offers \(count) tools, with \(mode) allowed."
189 case .failure: agentMessage = "Connection check failed. Quit and reopen Glimmer, then try again."
190 }
191 testingAgent = false
192 }
193 }
194
195 private func copyField(_ label: String, _ value: String) -> some View {
196 VStack(alignment: .leading, spacing: 4) {
197 Text(label).font(.caption.weight(.semibold)).foregroundStyle(.secondary)
198 HStack(spacing: 8) {
199 Text(value).font(.callout.monospaced()).textSelection(.enabled).lineLimit(1).truncationMode(.middle).help(value)
200 .padding(10).frame(maxWidth: .infinity, alignment: .leading)
201 .background(theme.inset, in: .rect(cornerRadius: 10))
202 Button("Copy \(label.lowercased())", systemImage: "doc.on.doc") {
203 AgentSetup.copy(value)
204 store.notice = "\(label) copied."
205 }
206 .labelStyle(.iconOnly).glimmerButton(.standard, circle: true)
207 }
208 }
209 }
210}
211#endif