Open History to see the last commit
1import SwiftUI
2
3struct ProviderSetupContext {
4 let model: FeatureRootModel
5 let environmentID: String
6}
7
8private struct ProviderSetupContextKey: EnvironmentKey {
9 static let defaultValue: ProviderSetupContext? = nil
10}
11
12extension EnvironmentValues {
13 var providerSetupContext: ProviderSetupContext? {
14 get { self[ProviderSetupContextKey.self] }
15 set { self[ProviderSetupContextKey.self] = newValue }
16 }
17}
18
19struct ProvidersSettingsView: View {
20 @Bindable var model: FeatureRootModel
21 var environmentID: String?
22
23 var body: some View {
24 List {
25 ForEach(model.snapshot.environments.filter { environmentID == nil || $0.id == environmentID }) { environment in
26 Section(environment.name) {
27 let providers = model.snapshot.providersByEnvironment?[environment.id] ?? []
28 if providers.isEmpty { Text("Connect this environment to load providers.") }
29 ForEach(providers) { provider in
30 NavigationLink {
31 ProviderSetupView(model: model, environmentID: environment.id, instanceID: provider.id)
32 } label: {
33 HStack(spacing: 12) {
34 ProviderIcon(driver: provider.driver, providerID: provider.id, fallbackName: provider.name, size: 24)
35 VStack(alignment: .leading) {
36 Text(provider.name)
37 Text(provider.isAvailable ? "Ready" : provider.statusMessage ?? "Setup needed")
38 .font(.caption).foregroundStyle(T3Colors.textSecondary)
39 }
40 }
41 }
42 }
43 }
44 }
45 }
46 .scrollContentBackground(.hidden)
47 .background(T3Colors.background)
48 .navigationTitle("Providers")
49 .navigationBarTitleDisplayMode(.inline)
50 }
51}
52
53private struct ProviderSetupView: View {
54 @SwiftUI.Environment(\.openURL) private var openURL
55 @Bindable var model: FeatureRootModel
56 let environmentID: String
57 let instanceID: String
58 @State private var auth: ProviderAuthState?
59 @State private var install: ProviderInstallState?
60 @State private var callbackURL = ""
61 @State private var busy = false
62 @State private var errorMessage: String?
63 @State private var confirmSignOut = false
64 @State private var confirmRemove = false
65
66 private var provider: FeatureProvider? {
67 model.snapshot.providersByEnvironment?[environmentID]?.first { $0.id == instanceID }
68 }
69
70 var body: some View {
71 Form {
72 if let provider {
73 Section {
74 Text(provider.statusMessage ?? (provider.isAvailable ? "Ready" : "Setup needed"))
75 if provider.driver == "antigravity" {
76 Toggle("Enabled", isOn: Binding(
77 get: { provider.isEnabled == true },
78 set: { enabled in
79 Task {
80 busy = true
81 defer { busy = false }
82 do {
83 try await model.client.setProviderEnabled(environmentID: environmentID, instanceID: instanceID, enabled: enabled)
84 _ = await model.refreshProviders(environmentID: environmentID)
85 } catch { errorMessage = "Could not change provider settings." }
86 }
87 }
88 ))
89 }
90 }
91 if provider.setup?.canInstall == true {
92 Section("Runtime") {
93 if let install, install.isActive {
94 Text(install.phase.capitalized)
95 if let total = install.totalBytes, total > 0 {
96 ProgressView(value: Double(install.downloadedBytes), total: Double(total))
97 }
98 if let operationID = install.operationId {
99 Button("Cancel installation") { run(.cancelInstall(operationID: operationID)) }
100 }
101 } else {
102 Button(provider.isInstalled == true ? "Reinstall runtime" : "Install runtime") { run(.install) }
103 if install?.canRemove == true {
104 Button("Remove runtime", role: .destructive) { confirmRemove = true }
105 }
106 }
107 if let message = install?.message { Text(message).font(.footnote) }
108 }
109 }
110 if provider.setup?.canAuthenticate == true {
111 Section("Account") {
112 if provider.authStatus == "authenticated"
113 || (provider.isEnabled == false && provider.authStatus == "unknown") {
114 if provider.authStatus == "authenticated" { Text("Signed in") }
115 Button("Sign out", role: .destructive) { confirmSignOut = true }
116 } else if let auth, auth.isActive {
117 if let rawURL = auth.authorizationUrl, let url = URL(string: rawURL), url.scheme == "https" {
118 Button("Open sign-in page") { openURL(url) }
119 }
120 if let flowID = auth.flowId {
121 TextField("Paste the return URL", text: $callbackURL, axis: .vertical)
122 .textInputAutocapitalization(.never).autocorrectionDisabled()
123 .privacySensitive()
124 Button("Finish sign-in") {
125 let url = callbackURL.trimmingCharacters(in: .whitespacesAndNewlines)
126 callbackURL = ""
127 run(.completeSignIn(flowID: flowID, callbackURL: url))
128 }.disabled(callbackURL.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
129 Button("Cancel sign-in") { callbackURL = ""; run(.cancelSignIn(flowID: flowID)) }
130 }
131 } else {
132 Button("Sign in") { run(.signIn) }
133 .disabled(provider.isEnabled == false || provider.isInstalled == false)
134 }
135 if auth?.phase != "succeeded", let message = auth?.message { Text(message).font(.footnote) }
136 }
137 }
138 if provider.setup == nil {
139 Section { Text("Configure this provider on its computer.") }
140 }
141 Section { Button("Refresh models") { Task { _ = await model.refreshProviders(environmentID: environmentID) } } }
142 Section { Text("Runtime and credentials stay on this environment.").font(.footnote) }
143 }
144 if let errorMessage { Section { Text(errorMessage).foregroundStyle(T3Colors.textSecondary) } }
145 }
146 .disabled(busy)
147 .scrollContentBackground(.hidden)
148 .background(T3Colors.background)
149 .navigationTitle(provider?.name ?? "Provider")
150 .navigationBarTitleDisplayMode(.inline)
151 .tint(T3Colors.accent)
152 .task(id: instanceID) {
153 do {
154 for try await event in model.client.providerSetupEvents(environmentID: environmentID, instanceID: instanceID) {
155 receive(event)
156 }
157 } catch is CancellationError {} catch { errorMessage = "Could not load provider setup. Check this connection and its permissions." }
158 }
159 .onDisappear { callbackURL = "" }
160 .confirmationDialog("Sign out on this environment?", isPresented: $confirmSignOut) {
161 Button("Sign out", role: .destructive) { run(.signOut) }
162 }
163 .confirmationDialog("Remove the runtime from this environment?", isPresented: $confirmRemove) {
164 Button("Remove runtime", role: .destructive) { run(.remove) }
165 }
166 }
167
168 private func receive(_ event: ProviderSetupEvent) {
169 switch event {
170 case let .auth(state): auth = state
171 case let .install(state): install = state
172 }
173 }
174
175 private func run(_ action: ProviderSetupAction) {
176 Task {
177 busy = true
178 errorMessage = nil
179 defer { busy = false }
180 do {
181 receive(try await model.client.providerSetup(environmentID: environmentID, instanceID: instanceID, action: action))
182 } catch {
183 // Provider errors can contain callback URLs. Do not display or persist them.
184 errorMessage = "Provider setup failed. Check the connection and try again."
185 }
186 }
187 }
188}