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

ProjectsSettingsView.swift

251 lines10.9 KB

1import SwiftUI
2​
3struct ResponseStreamingPicker: View {
4 let title: String
5 @Binding var selection: ResponseStreamingMode
6 @State private var showingTokenWarning = false
7​
8 var body: some View {
9 Picker(title, selection: Binding(
10 get: { selection },
11 set: { mode in
12 if mode == .token { showingTokenWarning = true }
13 else { selection = mode }
14 }
15 )) {
16 ForEach(ResponseStreamingMode.allCases, id: \.self) { mode in
17 Text(mode.label).tag(mode)
18 }
19 }
20 .confirmationDialog("Use token streaming?", isPresented: $showingTokenWarning, titleVisibility: .visible) {
21 Button("Use paragraphs") { selection = .paragraph }
22 Button("Use tokens") { selection = .token }
23 Button("Cancel", role: .cancel) { }
24 } message: {
25 Text("Token streaming updates more often and can use more battery. Paragraph streaming is recommended.")
26 }
27 }
29​
30struct ProjectPreferencesSheet: View {
31 @SwiftUI.Environment(\.dismiss) private var dismiss
32 @Bindable var model: FeatureRootModel
33 let projectID: String
34​
35 var body: some View {
36 NavigationStack {
37 ProjectPreferencesView(model: model, projectID: projectID)
38 .toolbar {
39 ToolbarItem(placement: .confirmationAction) {
40 Button("Done") { dismiss() }
41 }
42 }
43 }
44 .onAppear { model.setConnectionManagementPresented(true) }
45 .onDisappear { model.setConnectionManagementPresented(false) }
46 .presentationBackground(T3Colors.background)
47 }
49​
50struct ProjectsSettingsView: View {
51 @Bindable var model: FeatureRootModel
52​
53 var body: some View {
54 List {
55 ForEach(model.snapshot.environments) { environment in
56 let projects = model.snapshot.projects.filter { $0.environmentID == environment.id }
57 .sorted { $0.name.localizedStandardCompare($1.name) == .orderedAscending }
58 if !projects.isEmpty {
59 Section(environment.name) {
60 ForEach(projects) { project in
61 NavigationLink(project.name) {
62 ProjectPreferencesView(model: model, projectID: project.id)
63 }
64 }
65 }
66 .listRowBackground(T3Colors.background)
67 }
68 }
69 if model.snapshot.projects.isEmpty { Text("No projects") }
70 }
71 .scrollContentBackground(.hidden)
72 .background(T3Colors.background)
73 .navigationTitle("Projects")
74 .navigationBarTitleDisplayMode(.inline)
75 .t3NavigationChrome()
76 }
78​
79struct ProjectPreferencesView: View {
80 @Bindable var model: FeatureRootModel
81 let projectID: String
82 @State private var preferences: FeatureProjectPreferences?
83 @State private var busy = false
84 @State private var errorMessage: String?
85​
86 private var project: FeatureProject? { model.snapshot.projects.first { $0.id == projectID } }
87 private var wireID: String { project?.wireID ?? projectID }
88 private var providers: [FeatureProvider] {
89 DailyUXCreationContext.providers(for: project, in: model.snapshot)
90 }
91 private var settings: ServerSettingsSnapshot? { preferences?.environment }
92 private var effective: ServerSettingsSnapshot? { preferences?.effective }
93 private var supportsRestartContinuation: Bool {
94 guard let project else { return false }
95 return model.snapshot.preferencesByEnvironment?[project.environmentID]?.continueThreadsAfterServerUpdate != nil
96 }
97​
98 var body: some View {
99 Form {
100 if project?.supportsProjectSettingsOverrides != true {
101 Text("Update this environment to change project settings.")
102 } else if let effective {
103 projectSetting(.defaultModelSelection) {
104 Menu {
105 Button("No default model") { save(.defaultModelSelection, value: .null) }
106 ForEach(providers.filter(\.isAvailable)) { provider in
107 Section(provider.name) {
108 ForEach(provider.models) { model in
109 Button(model.name) {
110 setDefaultModel(providerID: provider.id, modelID: model.id)
111 }
112 }
113 }
114 }
115 } label: {
116 LabeledContent("Default model", value: modelLabel(effective.defaultModelSelection))
117 }
118 .accessibilityIdentifier("project-default-model")
119 }
120 projectSetting(.defaultThreadEnvMode) {
121 Picker("New threads", selection: Binding(
122 get: { effective.defaultThreadEnvMode },
123 set: { save(.defaultThreadEnvMode, value: .string($0.rawValue)) }
124 )) {
125 Text("Local workspace").tag(ServerThreadEnvironmentMode.local)
126 Text("New worktree").tag(ServerThreadEnvironmentMode.worktree)
127 }
128 .accessibilityIdentifier("project-default-workspace")
129 }
130 projectSetting(.newWorktreesStartFromOrigin) {
131 Toggle("Start worktrees from origin", isOn: booleanBinding(
132 .newWorktreesStartFromOrigin, value: effective.newWorktreesStartFromOrigin
133 ))
134 }
135 projectSetting(.defaultAutoPull) {
136 Toggle("Pull before new threads", isOn: booleanBinding(.defaultAutoPull, value: effective.defaultAutoPull))
137 }
138 if settings?.responseStreamingMode != nil {
139 projectSetting(.responseStreamingMode) {
140 ResponseStreamingPicker(title: "Response streaming", selection: Binding(
141 get: { effective.responseStreamingMode ?? .paragraph },
142 set: { save(.responseStreamingMode, value: .string($0.rawValue)) }
143 ))
144 .accessibilityIdentifier("project-response-streaming")
145 }
146 }
147 projectSetting(.sidebarAutoSettleOnMerge) {
148 Toggle("Settle after merge", isOn: booleanBinding(
149 .sidebarAutoSettleOnMerge, value: effective.sidebarAutoSettleOnMerge
150 ))
151 }
152 projectSetting(.sidebarAutoSettleAfterDays) {
153 Picker("Settle after inactivity", selection: Binding(
154 get: { effective.sidebarAutoSettleAfterDays ?? 0 },
155 set: { save(.sidebarAutoSettleAfterDays, value: $0 == 0 ? .null : .number($0)) }
156 )) {
157 Text("Never").tag(0.0)
158 ForEach(settlementDays(effective.sidebarAutoSettleAfterDays), id: \.self) { days in
159 Text(days == 1 ? "1 day" : "\(days.formatted()) days").tag(days)
160 }
161 }
162 }
163 if supportsRestartContinuation {
164 projectSetting(.continueThreadsAfterServerUpdate) {
165 Toggle("Continue threads after restarts", isOn: booleanBinding(
166 .continueThreadsAfterServerUpdate, value: effective.continueThreadsAfterServerUpdate
167 ))
168 }
169 }
170 } else if errorMessage == nil {
171 Text("Loading preferences...")
172 }
173 if let errorMessage {
174 Section {
175 Text(errorMessage)
176 .foregroundStyle(T3Colors.danger)
177 Button("Try again") { Task { await load() } }
178 }
179 }
180 }
181 .disabled(busy)
182 .scrollContentBackground(.hidden)
183 .background(T3Colors.background)
184 .navigationTitle(project?.name ?? "Project")
185 .navigationBarTitleDisplayMode(.inline)
186 .t3NavigationChrome()
187 .task(id: project?.supportsProjectSettingsOverrides) { await load() }
188 }
189​
190 private func projectSetting<Content: View>(
191 _ key: ServerProjectSettingKey,
192 @ViewBuilder content: () -> Content
193 ) -> some View {
194 let overridden = settings?.projectSettingsOverrides[wireID]?[key.rawValue] != nil
195 return Section {
196 content()
197 if overridden {
198 Button("Use environment setting") { save(key, value: nil) }
199 .accessibilityIdentifier("project-reset-\(key.rawValue)")
200 }
201 } footer: {
202 Text(overridden ? "Project setting" : "Uses environment setting")
203 if key == .responseStreamingMode, effective?.responseStreamingMode == .token {
204 Text("Token streaming updates more often and can use more battery.")
205 }
206 }
207 .listRowBackground(T3Colors.background)
208 }
209​
210 private func booleanBinding(_ key: ServerProjectSettingKey, value: Bool) -> Binding<Bool> {
211 Binding(get: { value }, set: { save(key, value: .bool($0)) })
212 }
213​
214 private func settlementDays(_ current: Double?) -> [Double] {
215 Array(Set([1, 3, 7, 14, 30, 90] + (current.map { [$0] } ?? []))).sorted()
216 }
217​
218 private func modelLabel(_ selection: ModelSelection?) -> String {
219 guard let selection else { return "None" }
220 return providers.first { $0.id == selection.instanceId }?
221 .models.first { $0.id == selection.model }?.name ?? selection.model
222 }
223​
224 private func setDefaultModel(providerID: String, modelID: String) {
225 do {
226 let selection = ModelSelection(instanceId: providerID, model: modelID)
227 save(.defaultModelSelection, value: try JSONValue.encode(selection))
228 } catch { errorMessage = "Could not save this model." }
229 }
230​
231 private func load() async {
232 guard let project, project.supportsProjectSettingsOverrides == true else { return }
233 do {
234 preferences = try await model.client.projectPreferences(projectID: projectID)
235 errorMessage = nil
236 } catch { errorMessage = "Could not load project settings. Check this connection." }
237 }
238​
239 private func save(_ key: ServerProjectSettingKey, value: JSONValue?) {
240 busy = true
241 Task {
242 defer { busy = false }
243 do {
244 try await model.client.updateProjectPreferences(
245 projectID: projectID, change: .init(key: key, value: value)
246 )
247 await load()
248 } catch { errorMessage = "Could not save project settings. Check this connection and try again." }
249 }
250 }