-
Notifications
You must be signed in to change notification settings - Fork 849
Expand file tree
/
Copy pathCodebuffProviderDescriptor.swift
More file actions
86 lines (78 loc) · 3.21 KB
/
CodebuffProviderDescriptor.swift
File metadata and controls
86 lines (78 loc) · 3.21 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
import CodexBarMacroSupport
import Foundation
@ProviderDescriptorRegistration
@ProviderDescriptorDefinition
public enum CodebuffProviderDescriptor {
static func makeDescriptor() -> ProviderDescriptor {
ProviderDescriptor(
id: .codebuff,
metadata: ProviderMetadata(
id: .codebuff,
displayName: "Codebuff",
sessionLabel: "Credits",
weeklyLabel: "Weekly",
opusLabel: nil,
supportsOpus: false,
supportsCredits: true,
creditsHint: "Credit balance from the Codebuff API",
toggleTitle: "Show Codebuff usage",
cliName: "codebuff",
defaultEnabled: false,
isPrimaryProvider: false,
usesAccountFallback: false,
browserCookieOrder: nil,
dashboardURL: "https://www.codebuff.com/usage",
statusPageURL: nil,
statusLinkURL: nil),
branding: ProviderBranding(
iconStyle: .codebuff,
iconResourceName: "ProviderIcon-codebuff",
color: ProviderColor(red: 68 / 255, green: 255 / 255, blue: 0 / 255)),
tokenCost: ProviderTokenCostConfig(
supportsTokenCost: false,
noDataMessage: { "Codebuff cost summary is not yet supported." }),
fetchPlan: ProviderFetchPlan(
sourceModes: [.auto, .api],
pipeline: ProviderFetchPipeline(resolveStrategies: { _ in [CodebuffAPIFetchStrategy()] })),
cli: ProviderCLIConfig(
name: "codebuff",
aliases: ["manicode"],
versionDetector: nil))
}
}
struct CodebuffAPIFetchStrategy: ProviderFetchStrategy {
let id: String = "codebuff.api"
let kind: ProviderFetchKind = .apiToken
func isAvailable(_ context: ProviderFetchContext) async -> Bool {
_ = context
// Keep the strategy available so missing-token surfaces as a user-friendly error
// instead of a generic "no strategy" outcome.
return true
}
func fetch(_ context: ProviderFetchContext) async throws -> ProviderFetchResult {
guard let apiKey = Self.resolveToken(environment: context.env) else {
throw CodebuffUsageError.missingCredentials
}
let usage = try await CodebuffUsageFetcher.fetchUsage(apiKey: apiKey, environment: context.env)
return self.makeResult(
usage: usage.toUsageSnapshot(),
sourceLabel: "api")
}
func shouldFallback(on _: Error, context _: ProviderFetchContext) -> Bool {
false
}
private static func resolveToken(environment: [String: String]) -> String? {
ProviderTokenResolver.codebuffToken(environment: environment)
}
}
/// Errors related to Codebuff settings.
public enum CodebuffSettingsError: LocalizedError, Sendable {
case missingToken
public var errorDescription: String? {
switch self {
case .missingToken:
"Codebuff API token not configured. Set CODEBUFF_API_KEY or run `codebuff login` to " +
"populate ~/.config/manicode/credentials.json."
}
}
}