Open History to see the last commit
1import SwiftUI
2
3enum ProviderInstanceDisplay {
4 static func name(instanceID: String, driver: String, displayName: String?) -> String {
5 let brand = UsageLimitsPresentation.providerLabel(driver: driver)
6 let trimmed = displayName?.trimmingCharacters(in: .whitespacesAndNewlines)
7 if let trimmed, !trimmed.isEmpty, trimmed != brand { return trimmed }
8 if instanceID != driver {
9 let label = instanceID
10 .replacingOccurrences(of: "([a-z])([A-Z])", with: "$1 $2", options: .regularExpression)
11 .replacingOccurrences(of: "[_-]+", with: " ", options: .regularExpression)
12 .trimmingCharacters(in: .whitespacesAndNewlines)
13 .split(whereSeparator: \.isWhitespace)
14 .map { $0.prefix(1).uppercased() + $0.dropFirst() }
15 .joined(separator: " ")
16 if !label.isEmpty { return label }
17 }
18 if let trimmed, !trimmed.isEmpty { return trimmed }
19 return brand
20 }
21
22 static func initials(_ name: String) -> String {
23 let words = name.replacingOccurrences(of: "[_-]+", with: " ", options: .regularExpression)
24 .split(whereSeparator: \.isWhitespace)
25 if words.count == 1 { return String(words[0].prefix(2)).uppercased() }
26 return words.prefix(2).compactMap(\.first).map(String.init).joined().uppercased()
27 }
28
29 static func accentColor(_ value: String?) -> String? {
30 guard let value = value?.trimmingCharacters(in: .whitespacesAndNewlines),
31 value.range(of: "^#[0-9a-fA-F]{6}$", options: .regularExpression) != nil else { return nil }
32 return value
33 }
34}
35
36struct ProviderAccountBadge: Equatable {
37 let initials: String
38 let accentColor: String?
39}
40
41enum ProviderBrand: String {
42 case openAI = "ProviderOpenAI"
43 case claude = "ProviderClaude"
44 case cursor = "ProviderCursor"
45 case grok = "ProviderGrok"
46 case openCode = "ProviderOpenCode"
47 case antigravity = "ProviderAntigravity"
48
49 static func resolve(
50 driver: String,
51 providerID: String,
52 providerName: String = ""
53 ) -> ProviderBrand? {
54 for value in [driver, providerID, providerName] {
55 let normalized = value
56 .lowercased()
57 .filter(\.isLetter)
58 switch normalized {
59 case "codex", "codexcli", "openai", "openaicodex":
60 return .openAI
61 case "anthropic", "anthropicclaude", "claudeagent", "claude", "claudecode":
62 return .claude
63 case "cursor", "cursoragent":
64 return .cursor
65 case "grok", "xai", "xaigrok":
66 return .grok
67 case "opencode":
68 return .openCode
69 case "antigravity", "googleantigravity":
70 return .antigravity
71 default:
72 continue
73 }
74 }
75 return nil
76 }
77
78 var usesTemplateRendering: Bool {
79 switch self {
80 case .openAI, .cursor, .grok: true
81 case .claude, .openCode, .antigravity: false
82 }
83 }
84}
85
86/// Displays the same provider artwork used by the web and marketing surfaces.
87/// Unknown provider instances retain a compact initial so custom adapters remain legible.
88struct ProviderIcon: View {
89 let driver: String
90 let providerID: String
91 let fallbackName: String
92 let size: CGFloat
93 var accountBadge: ProviderAccountBadge? = nil
94
95 var body: some View {
96 Group {
97 if let brand = ProviderBrand.resolve(
98 driver: driver,
99 providerID: providerID,
100 providerName: fallbackName
101 ) {
102 Image(brand.rawValue)
103 .resizable()
104 .renderingMode(brand.usesTemplateRendering ? .template : .original)
105 .foregroundStyle(T3Colors.textSecondary)
106 .scaledToFit()
107 } else {
108 Text(fallbackInitial)
109 .font(.system(size: max(9, size * 0.44), weight: .bold))
110 .foregroundStyle(T3Colors.textPrimary)
111 .frame(maxWidth: .infinity, maxHeight: .infinity)
112 .background(
113 T3Colors.surfaceRaised,
114 in: RoundedRectangle(cornerRadius: max(4, size * 0.22))
115 )
116 }
117 }
118 .frame(width: size, height: size)
119 .overlay(alignment: .bottomTrailing) {
120 if let accountBadge {
121 Text(accountBadge.initials)
122 .font(.system(size: max(8, size * 0.45), weight: .bold))
123 .foregroundStyle(.white)
124 .padding(.horizontal, 2)
125 .frame(minWidth: 12, minHeight: 12)
126 .background(badgeColor(accountBadge.accentColor), in: RoundedRectangle(cornerRadius: 3))
127 .overlay {
128 RoundedRectangle(cornerRadius: 3).stroke(T3Colors.background, lineWidth: 1)
129 }
130 .offset(x: 5, y: 5)
131 }
132 }
133 .accessibilityHidden(true)
134 }
135
136 private var fallbackInitial: String {
137 fallbackName.trimmingCharacters(in: .whitespacesAndNewlines)
138 .first
139 .map { String($0).uppercased() } ?? "?"
140 }
141
142 private func badgeColor(_ value: String?) -> Color {
143 guard let value = ProviderInstanceDisplay.accentColor(value),
144 let rgb = UInt32(value.dropFirst(), radix: 16) else { return Color(white: 0.3) }
145 return Color(red: Double(rgb >> 16) / 255, green: Double((rgb >> 8) & 255) / 255, blue: Double(rgb & 255) / 255)
146 }
147}