-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAIBackendStatusMenu.swift
More file actions
307 lines (275 loc) · 9.89 KB
/
AIBackendStatusMenu.swift
File metadata and controls
307 lines (275 loc) · 9.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import SwiftUI
//
// AIBackendStatusMenu.swift
// Universal AI Backend Status Menu
//
// Reusable component for all Jordan Koch AI projects
// Shows backend status, model selection, and quick settings access
//
// Author: Jordan Koch
// Date: 2026-01-26
// Version: 1.0.0
//
// Usage:
// Add to any SwiftUI view:
// AIBackendStatusMenu()
//
// Or with custom theme:
// AIBackendStatusMenu(accentColor: .blue, compact: true)
//
struct AIBackendStatusMenu: View {
@ObservedObject var manager = AIBackendManager.shared
@State private var showSettings = false
@State private var isRefreshing = false
// Theme customization
var accentColor: Color = .blue
var compact: Bool = false
var showModelPicker: Bool = true
var body: some View {
HStack(spacing: compact ? 8 : 12) {
// Status Indicator
backendStatusIndicator
// Backend Selector
if !compact {
backendSelector
}
// Model Selector (for Ollama)
if showModelPicker && manager.activeBackend == .ollama && !manager.ollamaModels.isEmpty {
modelSelector
}
// Action Buttons
actionButtons
}
.padding(.horizontal, compact ? 8 : 12)
.padding(.vertical, compact ? 4 : 8)
.background(
RoundedRectangle(cornerRadius: 8)
.fill(Color.secondary.opacity(0.1))
)
.sheet(isPresented: $showSettings) {
AIBackendSelectionView()
}
}
// MARK: - Status Indicator
private var backendStatusIndicator: some View {
HStack(spacing: 6) {
// Main status dot
Circle()
.fill(statusColor)
.frame(width: 10, height: 10)
.overlay(
Circle()
.stroke(statusColor.opacity(0.3), lineWidth: 2)
.scaleEffect(isRefreshing ? 1.5 : 1.0)
.opacity(isRefreshing ? 0 : 1)
.animation(.easeOut(duration: 1).repeatForever(autoreverses: false), value: isRefreshing)
)
if !compact {
VStack(alignment: .leading, spacing: 2) {
Text(statusText)
.font(.system(size: 11, weight: .semibold))
.foregroundColor(statusColor)
if let backend = manager.activeBackend {
Text(backend.rawValue)
.font(.system(size: 9))
.foregroundColor(.secondary)
}
}
}
}
}
private var statusColor: Color {
if manager.activeBackend != nil {
return .green
} else if hasAnyConfigured {
return .gray
} else {
return .red
}
}
private var statusText: String {
if manager.activeBackend != nil {
return "Connected"
} else if hasAnyConfigured {
return "Not Configured"
} else {
return "Offline"
}
}
private var hasAnyConfigured: Bool {
manager.isOllamaAvailable || manager.isMLXAvailable ||
manager.isTinyLLMAvailable || manager.isTinyChatAvailable ||
manager.isOpenWebUIAvailable || manager.isOpenAIAvailable ||
manager.isGoogleCloudAvailable || manager.isAzureAvailable ||
manager.isAWSAvailable || manager.isIBMWatsonAvailable
}
// MARK: - Backend Selector
private var backendSelector: some View {
Menu {
ForEach(AIBackendManager.AIBackend.allCases, id: \.self) { backend in
Button(action: {
manager.activeBackend = backend
manager.saveConfiguration()
Task {
await manager.refreshAllBackends()
}
}) {
HStack {
Image(systemName: backendIcon(backend))
Text(backend.rawValue)
Spacer()
if isBackendAvailable(backend) {
Image(systemName: "checkmark.circle.fill")
.foregroundColor(.green)
} else if isBackendConfigured(backend) {
Image(systemName: "circle.fill")
.foregroundColor(.gray)
} else {
Image(systemName: "xmark.circle.fill")
.foregroundColor(.red)
}
}
}
}
Divider()
Button(action: { showSettings = true }) {
Label("Settings", systemImage: "gear")
}
} label: {
HStack(spacing: 4) {
Image(systemName: "cpu")
Text("Backend")
.font(.system(size: 11))
Image(systemName: "chevron.down")
.font(.system(size: 8))
}
.foregroundColor(accentColor)
}
.menuStyle(.borderlessButton)
.frame(height: 24)
}
// MARK: - Model Selector
private var modelSelector: some View {
Menu {
ForEach(manager.ollamaModels, id: \.self) { model in
Button(action: {
manager.selectedOllamaModel = model
manager.saveConfiguration()
}) {
HStack {
Text(model)
Spacer()
if manager.selectedOllamaModel == model {
Image(systemName: "checkmark")
}
}
}
}
} label: {
HStack(spacing: 4) {
Image(systemName: "brain")
Text(truncateModelName(manager.selectedOllamaModel))
.font(.system(size: 11))
.lineLimit(1)
Image(systemName: "chevron.down")
.font(.system(size: 8))
}
.foregroundColor(accentColor)
}
.menuStyle(.borderlessButton)
.frame(height: 24)
}
// MARK: - Action Buttons
private var actionButtons: some View {
HStack(spacing: 4) {
// Refresh Button
Button(action: {
isRefreshing = true
Task {
await manager.refreshAllBackends()
try? await Task.sleep(nanoseconds: 500_000_000)
isRefreshing = false
}
}) {
Image(systemName: "arrow.clockwise")
.font(.system(size: 11))
.foregroundColor(accentColor)
.rotationEffect(.degrees(isRefreshing ? 360 : 0))
.animation(.linear(duration: 1).repeatForever(autoreverses: false), value: isRefreshing)
}
.buttonStyle(.plain)
.help("Refresh backend status")
// Settings Button
Button(action: { showSettings = true }) {
Image(systemName: "gear")
.font(.system(size: 11))
.foregroundColor(accentColor)
}
.buttonStyle(.plain)
.help("Configure AI backends")
}
}
// MARK: - Helper Functions
private func backendIcon(_ backend: AIBackendManager.AIBackend) -> String {
switch backend {
case .ollama: return "network"
case .mlx: return "cpu"
case .tinyLLM: return "cube"
case .tinyChat: return "bubble.left.and.bubble.right.fill"
case .openWebUI: return "globe"
case .openAI: return "brain"
case .googleCloud: return "cloud"
case .azureCognitive: return "cloud.fill"
case .awsAI: return "server.rack"
case .ibmWatson: return "atom"
}
}
private func isBackendAvailable(_ backend: AIBackendManager.AIBackend) -> Bool {
switch backend {
case .ollama: return manager.isOllamaAvailable
case .mlx: return manager.isMLXAvailable
case .tinyLLM: return manager.isTinyLLMAvailable
case .tinyChat: return manager.isTinyChatAvailable
case .openWebUI: return manager.isOpenWebUIAvailable
case .openAI: return manager.isOpenAIAvailable
case .googleCloud: return manager.isGoogleCloudAvailable
case .azureCognitive: return manager.isAzureAvailable
case .awsAI: return manager.isAWSAvailable
case .ibmWatson: return manager.isIBMWatsonAvailable
}
}
private func isBackendConfigured(_ backend: AIBackendManager.AIBackend) -> Bool {
switch backend {
case .ollama, .mlx, .tinyLLM, .tinyChat, .openWebUI:
return true // Local backends don't need configuration
case .openAI: return !manager.openAIAPIKey.isEmpty
case .googleCloud: return !manager.googleCloudAPIKey.isEmpty
case .azureCognitive: return !manager.azureAPIKey.isEmpty
case .awsAI: return !manager.awsAccessKey.isEmpty
case .ibmWatson: return !manager.ibmWatsonAPIKey.isEmpty
}
}
private func truncateModelName(_ name: String) -> String {
let parts = name.split(separator: ":")
return String(parts.first ?? name)
}
}
// MARK: - Compact Variant
struct AIBackendStatusMenuCompact: View {
var body: some View {
AIBackendStatusMenu(compact: true, showModelPicker: false)
}
}
// MARK: - Preview
#if DEBUG
struct AIBackendStatusMenu_Previews: PreviewProvider {
static var previews: some View {
VStack(spacing: 20) {
AIBackendStatusMenu()
AIBackendStatusMenu(accentColor: .green)
AIBackendStatusMenu(compact: true)
}
.padding()
}
}
#endif