forked from tddworks/ClaudeBar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserDefaultsProviderSettingsRepository.swift
More file actions
353 lines (281 loc) · 12.6 KB
/
UserDefaultsProviderSettingsRepository.swift
File metadata and controls
353 lines (281 loc) · 12.6 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
import Foundation
import Domain
/// UserDefaults-based implementation of ProviderSettingsRepository and its sub-protocols.
/// Persists provider settings like isEnabled state and provider-specific configuration.
public final class UserDefaultsProviderSettingsRepository: ZaiSettingsRepository, CopilotSettingsRepository, BedrockSettingsRepository, ClaudeSettingsRepository, CodexSettingsRepository, KimiSettingsRepository, MiniMaxSettingsRepository, HookSettingsRepository, @unchecked Sendable {
/// Shared singleton instance
public static let shared = UserDefaultsProviderSettingsRepository()
/// The UserDefaults instance to use
private let userDefaults: UserDefaults
/// Creates a new repository with the specified UserDefaults instance
/// - Parameter userDefaults: The UserDefaults to use (defaults to .standard)
public init(userDefaults: UserDefaults = .standard) {
self.userDefaults = userDefaults
}
// MARK: - ProviderSettingsRepository
public func isEnabled(forProvider id: String, defaultValue: Bool) -> Bool {
let key = Self.enabledKey(forProvider: id)
guard userDefaults.object(forKey: key) != nil else {
return defaultValue
}
return userDefaults.bool(forKey: key)
}
public func setEnabled(_ enabled: Bool, forProvider id: String) {
let key = Self.enabledKey(forProvider: id)
userDefaults.set(enabled, forKey: key)
}
// MARK: - ZaiSettingsRepository
public func zaiConfigPath() -> String {
userDefaults.string(forKey: Keys.zaiConfigPath) ?? ""
}
public func setZaiConfigPath(_ path: String) {
userDefaults.set(path, forKey: Keys.zaiConfigPath)
}
public func glmAuthEnvVar() -> String {
userDefaults.string(forKey: Keys.glmAuthEnvVar) ?? ""
}
public func setGlmAuthEnvVar(_ envVar: String) {
userDefaults.set(envVar, forKey: Keys.glmAuthEnvVar)
}
// MARK: - CopilotSettingsRepository (Probe Mode)
public func copilotProbeMode() -> CopilotProbeMode {
guard let rawValue = userDefaults.string(forKey: Keys.copilotProbeMode) else {
return .billing // Default to Billing API mode
}
return CopilotProbeMode(rawValue: rawValue) ?? .billing
}
public func setCopilotProbeMode(_ mode: CopilotProbeMode) {
userDefaults.set(mode.rawValue, forKey: Keys.copilotProbeMode)
}
// MARK: - CopilotSettingsRepository (Configuration)
public func copilotAuthEnvVar() -> String {
userDefaults.string(forKey: Keys.copilotAuthEnvVar) ?? ""
}
public func setCopilotAuthEnvVar(_ envVar: String) {
userDefaults.set(envVar, forKey: Keys.copilotAuthEnvVar)
}
public func copilotMonthlyLimit() -> Int? {
guard let intValue = userDefaults.object(forKey: Keys.copilotMonthlyLimit) as? Int else {
return nil
}
return intValue
}
public func setCopilotMonthlyLimit(_ limit: Int?) {
if let limit {
userDefaults.set(limit, forKey: Keys.copilotMonthlyLimit)
} else {
userDefaults.removeObject(forKey: Keys.copilotMonthlyLimit)
}
}
public func copilotManualUsageValue() -> Double? {
guard userDefaults.object(forKey: Keys.copilotManualUsageValue) != nil else {
return nil
}
return userDefaults.double(forKey: Keys.copilotManualUsageValue)
}
public func setCopilotManualUsageValue(_ value: Double?) {
if let value {
userDefaults.set(value, forKey: Keys.copilotManualUsageValue)
} else {
userDefaults.removeObject(forKey: Keys.copilotManualUsageValue)
}
}
public func copilotManualUsageIsPercent() -> Bool {
userDefaults.bool(forKey: Keys.copilotManualUsageIsPercent)
}
public func setCopilotManualUsageIsPercent(_ isPercent: Bool) {
userDefaults.set(isPercent, forKey: Keys.copilotManualUsageIsPercent)
}
public func copilotManualOverrideEnabled() -> Bool {
userDefaults.bool(forKey: Keys.copilotManualOverrideEnabled)
}
public func setCopilotManualOverrideEnabled(_ enabled: Bool) {
userDefaults.set(enabled, forKey: Keys.copilotManualOverrideEnabled)
}
public func copilotApiReturnedEmpty() -> Bool {
userDefaults.bool(forKey: Keys.copilotApiReturnedEmpty)
}
public func setCopilotApiReturnedEmpty(_ empty: Bool) {
userDefaults.set(empty, forKey: Keys.copilotApiReturnedEmpty)
}
// MARK: - CopilotSettingsRepository (Usage Period)
public func copilotLastUsagePeriodMonth() -> Int? {
guard userDefaults.object(forKey: Keys.copilotLastUsagePeriodMonth) != nil else {
return nil
}
return userDefaults.integer(forKey: Keys.copilotLastUsagePeriodMonth)
}
public func copilotLastUsagePeriodYear() -> Int? {
guard userDefaults.object(forKey: Keys.copilotLastUsagePeriodYear) != nil else {
return nil
}
return userDefaults.integer(forKey: Keys.copilotLastUsagePeriodYear)
}
public func setCopilotLastUsagePeriod(month: Int, year: Int) {
userDefaults.set(month, forKey: Keys.copilotLastUsagePeriodMonth)
userDefaults.set(year, forKey: Keys.copilotLastUsagePeriodYear)
}
// MARK: - CopilotSettingsRepository (Credentials)
public func saveGithubToken(_ token: String) {
userDefaults.set(token, forKey: Keys.githubToken)
}
public func getGithubToken() -> String? {
userDefaults.string(forKey: Keys.githubToken)
}
public func deleteGithubToken() {
userDefaults.removeObject(forKey: Keys.githubToken)
}
public func hasGithubToken() -> Bool {
userDefaults.object(forKey: Keys.githubToken) != nil
}
public func saveGithubUsername(_ username: String) {
userDefaults.set(username, forKey: Keys.githubUsername)
}
public func getGithubUsername() -> String? {
userDefaults.string(forKey: Keys.githubUsername)
}
public func deleteGithubUsername() {
userDefaults.removeObject(forKey: Keys.githubUsername)
}
// MARK: - ClaudeSettingsRepository
public func claudeProbeMode() -> ClaudeProbeMode {
guard let rawValue = userDefaults.string(forKey: Keys.claudeProbeMode) else {
return .cli // Default to CLI mode
}
return ClaudeProbeMode(rawValue: rawValue) ?? .cli
}
public func setClaudeProbeMode(_ mode: ClaudeProbeMode) {
userDefaults.set(mode.rawValue, forKey: Keys.claudeProbeMode)
}
// MARK: - CodexSettingsRepository
public func codexProbeMode() -> CodexProbeMode {
guard let rawValue = userDefaults.string(forKey: Keys.codexProbeMode) else {
return .rpc // Default to RPC mode
}
return CodexProbeMode(rawValue: rawValue) ?? .rpc
}
public func setCodexProbeMode(_ mode: CodexProbeMode) {
userDefaults.set(mode.rawValue, forKey: Keys.codexProbeMode)
}
// MARK: - KimiSettingsRepository
public func kimiProbeMode() -> KimiProbeMode {
guard let rawValue = userDefaults.string(forKey: Keys.kimiProbeMode) else {
return .cli // Default to CLI mode
}
return KimiProbeMode(rawValue: rawValue) ?? .cli
}
public func setKimiProbeMode(_ mode: KimiProbeMode) {
userDefaults.set(mode.rawValue, forKey: Keys.kimiProbeMode)
}
// MARK: - BedrockSettingsRepository
public func awsProfileName() -> String {
userDefaults.string(forKey: Keys.awsProfileName) ?? ""
}
public func setAWSProfileName(_ name: String) {
userDefaults.set(name, forKey: Keys.awsProfileName)
}
public func bedrockRegions() -> [String] {
userDefaults.stringArray(forKey: Keys.bedrockRegions) ?? ["us-east-1"]
}
public func setBedrockRegions(_ regions: [String]) {
userDefaults.set(regions, forKey: Keys.bedrockRegions)
}
public func bedrockDailyBudget() -> Decimal? {
guard let doubleValue = userDefaults.object(forKey: Keys.bedrockDailyBudget) as? Double else {
return nil
}
return Decimal(doubleValue)
}
public func setBedrockDailyBudget(_ amount: Decimal?) {
if let amount {
userDefaults.set(NSDecimalNumber(decimal: amount).doubleValue, forKey: Keys.bedrockDailyBudget)
} else {
userDefaults.removeObject(forKey: Keys.bedrockDailyBudget)
}
}
// MARK: - MiniMaxSettingsRepository
public func minimaxRegion() -> MiniMaxRegion {
// Legacy compatibility: key absent means user upgraded from pre-region version,
// which only supported china (minimaxi.com). (兼容旧版:无 key 则默认中国区)
guard let rawValue = userDefaults.string(forKey: Keys.minimaxRegion) else {
return .china
}
return MiniMaxRegion(rawValue: rawValue) ?? .china
}
public func setMinimaxRegion(_ region: MiniMaxRegion) {
userDefaults.set(region.rawValue, forKey: Keys.minimaxRegion)
}
public func minimaxAuthEnvVar() -> String {
userDefaults.string(forKey: Keys.minimaxiAuthEnvVar) ?? ""
}
public func setMinimaxAuthEnvVar(_ envVar: String) {
userDefaults.set(envVar, forKey: Keys.minimaxiAuthEnvVar)
}
public func saveMinimaxApiKey(_ key: String) {
userDefaults.set(key, forKey: Keys.minimaxiApiKey)
}
public func getMinimaxApiKey() -> String? {
userDefaults.string(forKey: Keys.minimaxiApiKey)
}
public func deleteMinimaxApiKey() {
userDefaults.removeObject(forKey: Keys.minimaxiApiKey)
}
public func hasMinimaxApiKey() -> Bool {
userDefaults.object(forKey: Keys.minimaxiApiKey) != nil
}
// MARK: - HookSettingsRepository
public func isHookEnabled() -> Bool {
guard userDefaults.object(forKey: Keys.hookEnabled) != nil else {
return false
}
return userDefaults.bool(forKey: Keys.hookEnabled)
}
public func setHookEnabled(_ enabled: Bool) {
userDefaults.set(enabled, forKey: Keys.hookEnabled)
}
public func hookPort() -> Int {
let port = userDefaults.integer(forKey: Keys.hookPort)
return port > 0 ? port : Int(HookConstants.defaultPort)
}
public func setHookPort(_ port: Int) {
userDefaults.set(port, forKey: Keys.hookPort)
}
// MARK: - Keys
private enum Keys {
// Hook settings
static let hookEnabled = "hookConfig.enabled"
static let hookPort = "hookConfig.port"
// Claude settings
static let claudeProbeMode = "providerConfig.claudeProbeMode"
// Codex settings
static let codexProbeMode = "providerConfig.codexProbeMode"
// Kimi settings
static let kimiProbeMode = "providerConfig.kimiProbeMode"
static let zaiConfigPath = "providerConfig.zaiConfigPath"
static let glmAuthEnvVar = "providerConfig.glmAuthEnvVar"
static let copilotProbeMode = "providerConfig.copilotProbeMode"
static let copilotAuthEnvVar = "providerConfig.copilotAuthEnvVar"
static let copilotMonthlyLimit = "providerConfig.copilotMonthlyLimit"
static let copilotManualUsageValue = "providerConfig.copilotManualUsageValue"
static let copilotManualUsageIsPercent = "providerConfig.copilotManualUsageIsPercent"
static let copilotManualOverrideEnabled = "providerConfig.copilotManualOverrideEnabled"
static let copilotApiReturnedEmpty = "providerConfig.copilotApiReturnedEmpty"
static let copilotLastUsagePeriodMonth = "providerConfig.copilotLastUsagePeriodMonth"
static let copilotLastUsagePeriodYear = "providerConfig.copilotLastUsagePeriodYear"
// Bedrock settings
static let awsProfileName = "providerConfig.awsProfileName"
static let bedrockRegions = "providerConfig.bedrockRegions"
static let bedrockDailyBudget = "providerConfig.bedrockDailyBudget"
// MiniMax settings (key strings kept for backward compatibility 保持向后兼容)
static let minimaxRegion = "providerConfig.minimaxRegion"
static let minimaxiAuthEnvVar = "providerConfig.minimaxiAuthEnvVar"
static let minimaxiApiKey = "com.claudebar.credentials.minimaxi-api-key"
// Credentials (kept compatible with old UserDefaultsCredentialRepository keys)
static let githubToken = "com.claudebar.credentials.github-copilot-token"
static let githubUsername = "com.claudebar.credentials.github-username"
}
/// Generates the UserDefaults key for a provider's enabled state
private static func enabledKey(forProvider id: String) -> String {
"provider.\(id).isEnabled"
}
}