forked from masorange/ClaudeUsageTracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsView.swift
More file actions
409 lines (359 loc) · 18.5 KB
/
SettingsView.swift
File metadata and controls
409 lines (359 loc) · 18.5 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//
// SettingsView.swift
// Claude Usage Tracker
//
// Copyright © 2025 Sergio Bañuls. All rights reserved.
// Licensed under Personal Use License (Non-Commercial)
//
import SwiftUI
struct SettingsView: View {
@EnvironmentObject var pricingManager: PricingManager
@EnvironmentObject var localizationManager: LocalizationManager
@EnvironmentObject var liteLLMManager: LiteLLMManager
@EnvironmentObject var preferencesManager: PreferencesManager
@EnvironmentObject var lookerStudioManager: LookerStudioManager
@Environment(\.dismiss) var dismiss
@State private var apiKey: String = ""
@State private var isConnectingLooker: Bool = false
@State private var standardInput: String
@State private var standardOutput: String
@State private var standardCacheCreation: String
@State private var standardCacheRead: String
@State private var longInput: String
@State private var longOutput: String
@State private var longCacheCreation: String
@State private var longCacheRead: String
init() {
let pricing = PricingManager()
_standardInput = State(initialValue: String(format: "%.2f", pricing.standardContext.inputTokens))
_standardOutput = State(initialValue: String(format: "%.2f", pricing.standardContext.outputTokens))
_standardCacheCreation = State(initialValue: String(format: "%.2f", pricing.standardContext.cacheCreation))
_standardCacheRead = State(initialValue: String(format: "%.2f", pricing.standardContext.cacheRead))
_longInput = State(initialValue: String(format: "%.2f", pricing.longContext.inputTokens))
_longOutput = State(initialValue: String(format: "%.2f", pricing.longContext.outputTokens))
_longCacheCreation = State(initialValue: String(format: "%.2f", pricing.longContext.cacheCreation))
_longCacheRead = State(initialValue: String(format: "%.2f", pricing.longContext.cacheRead))
}
var body: some View {
VStack(spacing: 0) {
// Header
HStack {
Text(isEnglish ? "Pricing Settings" : "Configuración de Precios")
.font(.headline)
Spacer()
Button(action: { dismiss() }) {
Image(systemName: "xmark.circle.fill")
.foregroundColor(.secondary)
}
.buttonStyle(.plain)
}
.padding()
Divider()
ScrollView {
VStack(alignment: .leading, spacing: 20) {
// API Configuration Section
VStack(alignment: .leading, spacing: 12) {
Text(isEnglish ? "🔑 API Configuration" : "🔑 Configuración de API")
.font(.subheadline)
.bold()
Text(isEnglish ? "Enter your LiteLLM API key to get exact usage data from the server. Leave empty to use local file calculation." : "Introduce tu API key de LiteLLM para obtener datos exactos del servidor. Déjalo vacío para usar el cálculo local.")
.font(.caption)
.foregroundColor(.secondary)
.fixedSize(horizontal: false, vertical: true)
VStack(alignment: .leading, spacing: 6) {
Text(isEnglish ? "LiteLLM API Key" : "API Key de LiteLLM")
.font(.caption)
.foregroundColor(.secondary)
SecureField(isEnglish ? "sk-..." : "sk-...", text: $apiKey)
.textFieldStyle(.roundedBorder)
.font(.system(.body, design: .monospaced))
}
if !apiKey.isEmpty && !apiKey.hasPrefix("sk-") {
HStack(spacing: 4) {
Image(systemName: "exclamationmark.triangle.fill")
.foregroundColor(.orange)
.font(.caption)
Text(isEnglish ? "API keys typically start with 'sk-'" : "Las API keys suelen empezar con 'sk-'")
.font(.caption)
.foregroundColor(.orange)
}
}
}
.padding()
.background(Color.blue.opacity(0.1))
.cornerRadius(8)
// Account Filter Section
VStack(alignment: .leading, spacing: 12) {
Text(isEnglish ? "🏢 Account Filter" : "🏢 Filtro de Cuenta")
.font(.subheadline)
.bold()
Text(isEnglish ? "Filter costs by account type based on message ID prefix (msg_vrtx_* for Vertex/work, msg_01* for personal)." : "Filtra los costes por tipo de cuenta basándose en el prefijo del ID del mensaje (msg_vrtx_* para Vertex/trabajo, msg_01* para personal).")
.font(.caption)
.foregroundColor(.secondary)
.fixedSize(horizontal: false, vertical: true)
Picker(isEnglish ? "Account" : "Cuenta", selection: $preferencesManager.accountFilter) {
Text(isEnglish ? "All accounts" : "Todas las cuentas").tag(AccountFilter.all)
Text(isEnglish ? "Work only (Vertex)" : "Solo trabajo (Vertex)").tag(AccountFilter.workOnly)
Text(isEnglish ? "Personal only" : "Solo personal").tag(AccountFilter.personalOnly)
}
.pickerStyle(.segmented)
}
.padding()
.background(Color.purple.opacity(0.1))
.cornerRadius(8)
// Looker Studio Configuration Section
VStack(alignment: .leading, spacing: 12) {
Text(isEnglish ? "📊 Looker Studio Connection" : "📊 Conexion Looker Studio")
.font(.subheadline)
.bold()
if lookerStudioManager.isConfigured {
// Connected state
HStack(spacing: 6) {
Image(systemName: "checkmark.circle.fill")
.foregroundColor(.green)
Text(isEnglish ? "Connected" : "Conectado")
.font(.caption)
.foregroundColor(.green)
Spacer()
}
if lookerStudioManager.totalSpend > 0 {
HStack(spacing: 12) {
VStack(alignment: .leading, spacing: 2) {
Text(isEnglish ? "Total Spend" : "Gasto Total")
.font(.caption2)
.foregroundColor(.secondary)
Text("$\(String(format: "%.2f", lookerStudioManager.totalSpend))")
.font(.caption)
.bold()
}
VStack(alignment: .leading, spacing: 2) {
Text(isEnglish ? "Monthly Spend" : "Gasto Mensual")
.font(.caption2)
.foregroundColor(.secondary)
Text("$\(String(format: "%.2f", lookerStudioManager.monthlySpend))")
.font(.caption)
.bold()
}
Spacer()
}
}
HStack(spacing: 8) {
Button(action: openLookerLogin) {
HStack(spacing: 4) {
if isConnectingLooker {
ProgressView()
.scaleEffect(0.5)
.frame(width: 12, height: 12)
} else {
Image(systemName: "arrow.triangle.2.circlepath")
}
Text(isEnglish ? "Refresh" : "Actualizar")
}
.font(.caption)
}
.buttonStyle(.bordered)
.disabled(isConnectingLooker)
Button(action: clearLookerCookies) {
HStack(spacing: 4) {
Image(systemName: "xmark.circle")
Text(isEnglish ? "Disconnect" : "Desconectar")
}
.font(.caption)
.foregroundColor(.red)
}
.buttonStyle(.plain)
Spacer()
}
} else {
// Not connected state
Text(isEnglish ? "Connect to Looker Studio to get real spend data from the company dashboard." : "Conecta con Looker Studio para obtener datos reales de gasto del dashboard de la empresa.")
.font(.caption)
.foregroundColor(.secondary)
.fixedSize(horizontal: false, vertical: true)
Button(action: openLookerLogin) {
HStack(spacing: 6) {
if isConnectingLooker {
ProgressView()
.scaleEffect(0.6)
.frame(width: 14, height: 14)
} else {
Image(systemName: "link.badge.plus")
}
Text(isEnglish ? "Connect with Google" : "Conectar con Google")
}
.font(.callout)
.frame(maxWidth: .infinity)
}
.buttonStyle(.borderedProminent)
.disabled(isConnectingLooker)
}
}
.padding()
.background(Color.green.opacity(0.1))
.cornerRadius(8)
Divider()
// Model info
VStack(alignment: .leading, spacing: 4) {
Text(isEnglish ? "Model: Claude Sonnet 4.5" : "Modelo: Claude Sonnet 4.5")
.font(.subheadline)
.bold()
Text(isEnglish ? "Prices per million tokens (for local calculation fallback)" : "Precios por millón de tokens (para cálculo local de respaldo)")
.font(.caption)
.foregroundColor(.secondary)
}
.padding(.bottom, 8)
// Standard Context
VStack(alignment: .leading, spacing: 12) {
Text(isEnglish ? "📄 Standard Context (≤ 200K tokens)" : "📄 Contexto Estándar (≤ 200K tokens)")
.font(.subheadline)
.bold()
PriceField(
label: isEnglish ? "Input tokens" : "Tokens de entrada",
value: $standardInput
)
PriceField(
label: isEnglish ? "Output tokens" : "Tokens de salida",
value: $standardOutput
)
PriceField(
label: isEnglish ? "Cache creation" : "Creación de caché",
value: $standardCacheCreation
)
PriceField(
label: isEnglish ? "Cache read" : "Lectura de caché",
value: $standardCacheRead
)
}
.padding()
.background(Color.secondary.opacity(0.1))
.cornerRadius(8)
// Long Context
VStack(alignment: .leading, spacing: 12) {
Text(isEnglish ? "📚 Long Context (> 200K tokens)" : "📚 Contexto Largo (> 200K tokens)")
.font(.subheadline)
.bold()
PriceField(
label: isEnglish ? "Input tokens" : "Tokens de entrada",
value: $longInput
)
PriceField(
label: isEnglish ? "Output tokens" : "Tokens de salida",
value: $longOutput
)
PriceField(
label: isEnglish ? "Cache creation" : "Creación de caché",
value: $longCacheCreation
)
PriceField(
label: isEnglish ? "Cache read" : "Lectura de caché",
value: $longCacheRead
)
}
.padding()
.background(Color.secondary.opacity(0.1))
.cornerRadius(8)
}
.padding()
}
Divider()
// Footer buttons
HStack(spacing: 12) {
Button(action: resetToDefaults) {
Text(isEnglish ? "Reset to Defaults" : "Restaurar Valores")
.frame(maxWidth: .infinity)
}
.buttonStyle(.bordered)
Button(action: saveSettings) {
Text(isEnglish ? "Save" : "Guardar")
.frame(maxWidth: .infinity)
}
.buttonStyle(.borderedProminent)
}
.padding()
}
.frame(width: 450, height: 600)
.onAppear {
loadCurrentValues()
apiKey = liteLLMManager.apiKey
}
}
private var isEnglish: Bool {
localizationManager.currentLanguage == .english
}
private func loadCurrentValues() {
standardInput = String(format: "%.2f", pricingManager.standardContext.inputTokens)
standardOutput = String(format: "%.2f", pricingManager.standardContext.outputTokens)
standardCacheCreation = String(format: "%.2f", pricingManager.standardContext.cacheCreation)
standardCacheRead = String(format: "%.2f", pricingManager.standardContext.cacheRead)
longInput = String(format: "%.2f", pricingManager.longContext.inputTokens)
longOutput = String(format: "%.2f", pricingManager.longContext.outputTokens)
longCacheCreation = String(format: "%.2f", pricingManager.longContext.cacheCreation)
longCacheRead = String(format: "%.2f", pricingManager.longContext.cacheRead)
}
private func openLookerLogin() {
isConnectingLooker = true
let bridge = LookerWebBridge()
SettingsView.activeBridge = bridge
let lookerManager = lookerStudioManager
bridge.show(
onDataFetched: { data in
DispatchQueue.main.async { [self] in
lookerManager.updateWithData(data)
lookerManager.markConnected()
self.isConnectingLooker = false
SettingsView.activeBridge = nil
}
},
onDismiss: { [self] in
DispatchQueue.main.async {
self.isConnectingLooker = false
SettingsView.activeBridge = nil
}
}
)
}
private static var activeBridge: LookerWebBridge?
private func clearLookerCookies() {
lookerStudioManager.clearConnection()
}
private func saveSettings() {
// Save API key
liteLLMManager.apiKey = apiKey
// Save pricing
pricingManager.standardContext.inputTokens = Double(standardInput) ?? 3.00
pricingManager.standardContext.outputTokens = Double(standardOutput) ?? 15.00
pricingManager.standardContext.cacheCreation = Double(standardCacheCreation) ?? 3.75
pricingManager.standardContext.cacheRead = Double(standardCacheRead) ?? 0.30
pricingManager.longContext.inputTokens = Double(longInput) ?? 6.00
pricingManager.longContext.outputTokens = Double(longOutput) ?? 22.50
pricingManager.longContext.cacheCreation = Double(longCacheCreation) ?? 7.50
pricingManager.longContext.cacheRead = Double(longCacheRead) ?? 0.60
pricingManager.save()
dismiss()
}
private func resetToDefaults() {
pricingManager.reset()
loadCurrentValues()
}
}
struct PriceField: View {
let label: String
@Binding var value: String
var body: some View {
HStack {
Text(label)
.font(.caption)
.foregroundColor(.secondary)
.frame(width: 120, alignment: .leading)
Text("$")
.foregroundColor(.secondary)
TextField("0.00", text: $value)
.textFieldStyle(.roundedBorder)
.frame(width: 80)
.multilineTextAlignment(.trailing)
Text("/ 1M")
.font(.caption)
.foregroundColor(.secondary)
}
}
}