-
Notifications
You must be signed in to change notification settings - Fork 840
Expand file tree
/
Copy pathCodexBarConfigValidation.swift
More file actions
200 lines (182 loc) · 7.78 KB
/
CodexBarConfigValidation.swift
File metadata and controls
200 lines (182 loc) · 7.78 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
import Foundation
public enum CodexBarConfigIssueSeverity: String, Codable, Sendable {
case warning
case error
}
public struct CodexBarConfigIssue: Codable, Sendable, Equatable {
public let severity: CodexBarConfigIssueSeverity
public let provider: UsageProvider?
public let field: String?
public let code: String
public let message: String
public init(
severity: CodexBarConfigIssueSeverity,
provider: UsageProvider?,
field: String?,
code: String,
message: String)
{
self.severity = severity
self.provider = provider
self.field = field
self.code = code
self.message = message
}
}
public enum CodexBarConfigValidator {
public static func validate(_ config: CodexBarConfig) -> [CodexBarConfigIssue] {
var issues: [CodexBarConfigIssue] = []
if config.version != CodexBarConfig.currentVersion {
issues.append(CodexBarConfigIssue(
severity: .error,
provider: nil,
field: "version",
code: "version_mismatch",
message: "Unsupported config version \(config.version)."))
}
for entry in config.providers {
self.validateProvider(entry, issues: &issues)
}
return issues
}
private static func validateProvider(_ entry: ProviderConfig, issues: inout [CodexBarConfigIssue]) {
let provider = entry.id
let descriptor = ProviderDescriptorRegistry.descriptor(for: provider)
let supportedSources = descriptor.fetchPlan.sourceModes
let supportsWeb = supportedSources.contains(.auto) || supportedSources.contains(.web)
let supportsAPI = supportedSources.contains(.api)
if let source = entry.source, !supportedSources.contains(source) {
issues.append(CodexBarConfigIssue(
severity: .error,
provider: provider,
field: "source",
code: "unsupported_source",
message: "Source \(source.rawValue) is not supported for \(provider.rawValue)."))
}
if let apiKey = entry.apiKey, !apiKey.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty, !supportsAPI {
issues.append(CodexBarConfigIssue(
severity: .warning,
provider: provider,
field: "apiKey",
code: "api_key_unused",
message: "apiKey is set but \(provider.rawValue) does not support api source."))
}
if let source = entry.source, source == .api, !supportsAPI {
issues.append(CodexBarConfigIssue(
severity: .error,
provider: provider,
field: "source",
code: "api_source_unsupported",
message: "Source api is not supported for \(provider.rawValue)."))
}
if let source = entry.source, source == .api,
entry.apiKey?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ?? true
{
issues.append(CodexBarConfigIssue(
severity: .warning,
provider: provider,
field: "apiKey",
code: "api_key_missing",
message: "Source api is selected but apiKey is missing for \(provider.rawValue)."))
}
if entry.cookieSource != nil, !supportsWeb {
issues.append(CodexBarConfigIssue(
severity: .warning,
provider: provider,
field: "cookieSource",
code: "cookie_source_unused",
message: "cookieSource is set but \(provider.rawValue) does not use web cookies."))
}
if let cookieHeader = entry.cookieHeader,
!cookieHeader.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty,
!supportsWeb
{
issues.append(CodexBarConfigIssue(
severity: .warning,
provider: provider,
field: "cookieHeader",
code: "cookie_header_unused",
message: "cookieHeader is set but \(provider.rawValue) does not use web cookies."))
}
if let cookieSource = entry.cookieSource,
cookieSource == .manual,
entry.cookieHeader?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ?? true
{
issues.append(CodexBarConfigIssue(
severity: .warning,
provider: provider,
field: "cookieHeader",
code: "cookie_header_missing",
message: "cookieSource manual is set but cookieHeader is missing for \(provider.rawValue)."))
}
if let region = entry.region, !region.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
switch provider {
case .minimax:
if MiniMaxAPIRegion(rawValue: region) == nil {
issues.append(CodexBarConfigIssue(
severity: .error,
provider: provider,
field: "region",
code: "invalid_region",
message: "Region \(region) is not a valid MiniMax region."))
}
case .zai:
if ZaiAPIRegion(rawValue: region) == nil {
issues.append(CodexBarConfigIssue(
severity: .error,
provider: provider,
field: "region",
code: "invalid_region",
message: "Region \(region) is not a valid z.ai region."))
}
case .alibaba:
if AlibabaCodingPlanAPIRegion(rawValue: region) == nil {
issues.append(CodexBarConfigIssue(
severity: .error,
provider: provider,
field: "region",
code: "invalid_region",
message: "Region \(region) is not a valid Alibaba Coding Plan region."))
}
case .moonshot:
if MoonshotRegion(rawValue: region) == nil {
issues.append(CodexBarConfigIssue(
severity: .error,
provider: provider,
field: "region",
code: "invalid_region",
message: "Region \(region) is not a valid Moonshot region."))
}
default:
issues.append(CodexBarConfigIssue(
severity: .warning,
provider: provider,
field: "region",
code: "region_unused",
message: "region is set but \(provider.rawValue) does not use regions."))
}
}
if let workspaceID = entry.workspaceID,
!workspaceID.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty,
provider != .opencode,
provider != .opencodego
{
issues.append(CodexBarConfigIssue(
severity: .warning,
provider: provider,
field: "workspaceID",
code: "workspace_unused",
message: "workspaceID is set but only opencode and opencodego support workspaceID."))
}
if let tokenAccounts = entry.tokenAccounts, !tokenAccounts.accounts.isEmpty,
TokenAccountSupportCatalog.support(for: provider) == nil
{
issues.append(CodexBarConfigIssue(
severity: .warning,
provider: provider,
field: "tokenAccounts",
code: "token_accounts_unused",
message: "tokenAccounts are set but \(provider.rawValue) does not support token accounts."))
}
}
}