Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

FeatureComposerTraitsControl.swift

219 lines7.9 KB

1/// The composer traits menu is derived from the selected model's option
2/// descriptors. It deliberately knows nothing about which providers expose
3/// which sections: select descriptors use their advertised choices, boolean
4/// descriptors use On/Off, and descriptors without a usable control disappear.
5struct FeatureComposerTraitsControl: Equatable {
6 struct Choice: Identifiable, Equatable {
7 let id: String
8 let label: String
9 let detail: String?
10 let isDefault: Bool
11 let value: FeatureModelOptionValue
12 }
13​
14 struct Section: Identifiable, Equatable {
15 let id: String
16 let label: String
17 let choices: [Choice]
18 let currentChoiceID: String?
19 }
20​
21 let sections: [Section]
22 let triggerLabel: String
23 let showsFastModeIcon: Bool
24 private let resolvedSelection: FeatureSelection
25​
26 static func resolve(
27 explicit: FeatureSelection?,
28 inherited: FeatureSelection?,
29 providers: [FeatureProvider],
30 materializesDefaultSelection: Bool
31 ) -> FeatureComposerTraitsControl? {
32 let providers = ProviderModelCatalogNormalizer.normalized(providers)
33 let selection = if materializesDefaultSelection {
34 ProviderModelSelectionResolver.materialized(explicit, in: providers)
35 } else {
36 ThreadComposerModelSelectionPolicy.resolvedSelection(
37 explicit: explicit,
38 inherited: inherited,
39 providers: providers
40 )
41 }
42 guard let selection,
43 let provider = providers.first(where: { $0.id == selection.providerID }),
44 let model = provider.models.first(where: { $0.id == selection.modelID }) else {
45 return nil
46 }
47​
48 let sections = model.options.compactMap {
49 section(for: $0, selections: selection.options)
50 }
51 guard !sections.isEmpty else { return nil }
52​
53 let trigger = triggerDisplay(
54 sections: sections,
55 descriptors: model.options,
56 providerDriver: provider.driver
57 )
58 return FeatureComposerTraitsControl(
59 sections: sections,
60 triggerLabel: trigger.label,
61 showsFastModeIcon: trigger.showsFastModeIcon,
62 resolvedSelection: selection
63 )
64 }
65​
66 /// The shared model-selection policy adds declared defaults. A traits choice
67 /// changes only its own option, preserving saved values and unset options.
68 func selection(choosing choiceID: String, in descriptorID: String) -> FeatureSelection {
69 guard let section = sections.first(where: { $0.id == descriptorID }),
70 let choice = section.choices.first(where: { $0.id == choiceID }) else {
71 return resolvedSelection
72 }
73​
74 var next = resolvedSelection
75 next.options = DailyUXModelOptions.updating(
76 next.options,
77 id: descriptorID,
78 value: choice.value
79 )
80 return next
81 }
82​
83 private static func section(
84 for descriptor: FeatureModelOptionDescriptor,
85 selections: [FeatureModelOptionSelection]
86 ) -> Section? {
87 switch descriptor.kind {
88 case .select:
89 let supportedChoices = descriptor.choices.filter {
90 !(descriptor.promptInjectedValues ?? []).contains($0.id)
91 }
92 guard !supportedChoices.isEmpty else { return nil }
93 return Section(
94 id: descriptor.id,
95 label: descriptor.label,
96 choices: supportedChoices.map {
97 Choice(
98 id: $0.id,
99 label: $0.label,
100 detail: $0.detail,
101 isDefault: $0.isDefault,
102 value: .string($0.id)
103 )
104 },
105 currentChoiceID: currentSelectChoiceID(
106 for: descriptor,
107 selections: selections
108 )
109 )
110 case .boolean:
111 let current = currentBooleanValue(for: descriptor, selections: selections)
112 return Section(
113 id: descriptor.id,
114 label: descriptor.label,
115 choices: [
116 Choice(
117 id: "on",
118 label: "On",
119 detail: nil,
120 isDefault: false,
121 value: .boolean(true)
122 ),
123 Choice(
124 id: "off",
125 label: "Off",
126 detail: nil,
127 isDefault: false,
128 value: .boolean(false)
129 ),
130 ],
131 currentChoiceID: current.map { $0 ? "on" : "off" }
132 )
133 }
134 }
135​
136 private static func currentSelectChoiceID(
137 for descriptor: FeatureModelOptionDescriptor,
138 selections: [FeatureModelOptionSelection]
139 ) -> String? {
140 if case .string(let selected)? = DailyUXModelOptions.value(
141 for: descriptor,
142 in: selections
143 ) {
144 return selected
145 }
146 return nil
147 }
148​
149 private static func currentBooleanValue(
150 for descriptor: FeatureModelOptionDescriptor,
151 selections: [FeatureModelOptionSelection]
152 ) -> Bool? {
153 if case .boolean(let selected)? = DailyUXModelOptions.value(
154 for: descriptor,
155 in: selections
156 ) {
157 return selected
158 }
159 return nil
160 }
161​
162 /// Mirrors Electron's compact TraitsPicker display. Fast mode is a bolt when
163 /// another trait supplies readable text; when it is the only trait its state
164 /// remains text so the trigger never becomes an unexplained icon.
165 private static func triggerDisplay(
166 sections: [Section],
167 descriptors: [FeatureModelOptionDescriptor],
168 providerDriver: String
169 ) -> (label: String, showsFastModeIcon: Bool) {
170 var fastModeFallbackLabel: String?
171 var fastModeEnabled = false
172 var labels: [String] = []
173​
174 for descriptor in descriptors {
175 guard let section = sections.first(where: { $0.id == descriptor.id }) else {
176 continue
177 }
178 let current = section.choices.first(where: {
179 $0.id == section.currentChoiceID
180 })
181​
182 if descriptor.id == "fastMode", descriptor.kind == .boolean {
183 guard let current else { continue }
184 fastModeEnabled = current.value == .boolean(true)
185 fastModeFallbackLabel = fastModeEnabled ? "Fast" : "Normal"
186 continue
187 }
188​
189 if providerDriver == "codex",
190 descriptor.id == "serviceTier",
191 descriptor.kind == .select,
192 let fastChoice = section.choices.first(where: { $0.label == "Fast" }),
193 section.currentChoiceID == "default"
194 || section.currentChoiceID == fastChoice.id {
195 fastModeEnabled = section.currentChoiceID == fastChoice.id
196 fastModeFallbackLabel = current?.label
197 continue
198 }
199​
200 switch descriptor.kind {
201 case .select:
202 let label = current?.label ?? descriptor.choices.first(where: {
203 $0.id == section.currentChoiceID
204 })?.label ?? section.currentChoiceID
205 if let label {
206 labels.append(label)
207 }
208 case .boolean:
209 guard case .boolean(let value)? = current?.value else { continue }
210 labels.append("\(descriptor.label) \(value ? "On" : "Off")")
211 }
212 }
213​
214 if labels.isEmpty, let fastModeFallbackLabel {
215 return (fastModeFallbackLabel, false)
216 }
217 return (labels.isEmpty ? "Provider default" : labels.joined(separator: " · "), fastModeEnabled)
218 }