Skip to content

Commit 60cfdf4

Browse files
committed
Adjust implementation of text field with picker
1 parent 40eba80 commit 60cfdf4

File tree

6 files changed

+115
-78
lines changed

6 files changed

+115
-78
lines changed

Core/Sources/HostApp/AccountSettings/APIKeyManagement/APIKeyPicker.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,25 @@ struct APIKeyPicker: View {
1414
if viewStore.state.availableAPIKeyNames.isEmpty {
1515
Text("No API key found, please add a new one →")
1616
}
17+
18+
if !viewStore.state.availableAPIKeyNames.contains(viewStore.state.apiKeyName),
19+
!viewStore.state.apiKeyName.isEmpty {
20+
Text("Key not found: \(viewStore.state.apiKeyName)")
21+
.tag(viewStore.state.apiKeyName)
22+
}
23+
1724
ForEach(viewStore.state.availableAPIKeyNames, id: \.self) { name in
1825
Text(name).tag(name)
1926
}
2027

2128
},
2229
label: { Text("API Key") }
2330
)
24-
}
2531

26-
Button(action: { store.send(.manageAPIKeysButtonClicked) }) {
27-
Text(Image(systemName: "key"))
28-
}
29-
.sheet(isPresented: viewStore.$isAPIKeyManagementPresented) {
32+
Button(action: { store.send(.manageAPIKeysButtonClicked) }) {
33+
Text(Image(systemName: "key"))
34+
}
35+
}.sheet(isPresented: viewStore.$isAPIKeyManagementPresented) {
3036
APIKeyManagementView(store: store.scope(
3137
state: \.apiKeyManagement,
3238
action: APIKeySelection.Action.apiKeyManagement

Core/Sources/HostApp/AccountSettings/ChatModelManagement/ChatModelEditView.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ struct ChatModelEditView: View {
203203
"",
204204
selection: viewStore.$modelName,
205205
content: {
206+
if ChatGPTModel(rawValue: viewStore.state.modelName) == nil {
207+
Text("Custom Model").tag(viewStore.state.modelName)
208+
}
206209
ForEach(ChatGPTModel.allCases, id: \.self) { model in
207210
Text(model.rawValue).tag(model.rawValue)
208211
}

Core/Sources/HostApp/AccountSettings/EmbeddingModelManagement/EmbeddingModelEditView.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,10 @@ struct EmbeddingModelEditView: View {
191191
"",
192192
selection: viewStore.$modelName,
193193
content: {
194-
ForEach(ChatGPTModel.allCases, id: \.self) { model in
194+
if OpenAIEmbeddingModel(rawValue: viewStore.state.modelName) == nil {
195+
Text("Custom Model").tag(viewStore.state.modelName)
196+
}
197+
ForEach(OpenAIEmbeddingModel.allCases, id: \.self) { model in
195198
Text(model.rawValue).tag(model.rawValue)
196199
}
197200
}

Core/Sources/HostApp/AccountSettings/SharedModelManagement/AIModelManagementVIew.swift

Lines changed: 84 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct AIModelManagementView<Management: AIModelManagement, Model: ManageableAIM
4747
store.send(.createModel)
4848
}
4949
}.padding(4)
50-
50+
5151
Divider()
5252

5353
ModelList(store: store)
@@ -92,6 +92,12 @@ struct AIModelManagementView<Management: AIModelManagement, Model: ManageableAIM
9292
.removeBackground()
9393
.listStyle(.plain)
9494
.listRowInsets(EdgeInsets())
95+
.overlay {
96+
if viewStore.state.models.isEmpty {
97+
Text("No model found, please add a new one.")
98+
.foregroundColor(.secondary)
99+
}
100+
}
95101
}
96102
}
97103
}
@@ -159,72 +165,82 @@ struct AIModelManagementView<Management: AIModelManagement, Model: ManageableAIM
159165

160166
// MARK: - Previews
161167

162-
class AIModelManagement_Previews: PreviewProvider {
163-
static var previews: some View {
164-
AIModelManagementView<ChatModelManagement, _>(
165-
store: .init(
166-
initialState: .init(
167-
models: IdentifiedArray<String, ChatModel>(uniqueElements: [
168-
ChatModel(
169-
id: "1",
170-
name: "Test Model",
171-
format: .openAI,
172-
info: .init(
173-
apiKeyName: "key",
174-
baseURL: "google.com",
175-
maxTokens: 3000,
176-
supportsFunctionCalling: true,
177-
modelName: "gpt-3.5-turbo"
178-
)
179-
),
180-
ChatModel(
181-
id: "2",
182-
name: "Test Model 2",
183-
format: .azureOpenAI,
184-
info: .init(
185-
apiKeyName: "key",
186-
baseURL: "apple.com",
187-
maxTokens: 3000,
188-
supportsFunctionCalling: false,
189-
modelName: "gpt-3.5-turbo"
190-
)
191-
),
192-
ChatModel(
193-
id: "3",
194-
name: "Test Model 3",
195-
format: .openAICompatible,
196-
info: .init(
197-
apiKeyName: "key",
198-
baseURL: "apple.com",
199-
maxTokens: 3000,
200-
supportsFunctionCalling: false,
201-
modelName: "gpt-3.5-turbo"
202-
)
203-
),
204-
]),
205-
editingModel: .init(
206-
model: ChatModel(
207-
id: "3",
208-
name: "Test Model 3",
209-
format: .openAICompatible,
210-
info: .init(
211-
apiKeyName: "key",
212-
baseURL: "apple.com",
213-
maxTokens: 3000,
214-
supportsFunctionCalling: false,
215-
modelName: "gpt-3.5-turbo"
216-
)
217-
)
218-
)
219-
),
220-
reducer: ChatModelManagement()
221-
)
222-
)
223-
}
224-
}
225-
226-
227-
class AIModelManagement_Cell_Previews: PreviewProvider {
168+
class AIModelManagement_Previews: PreviewProvider {
169+
static var previews: some View {
170+
AIModelManagementView<ChatModelManagement, _>(
171+
store: .init(
172+
initialState: .init(
173+
models: IdentifiedArray<String, ChatModel>(uniqueElements: [
174+
ChatModel(
175+
id: "1",
176+
name: "Test Model",
177+
format: .openAI,
178+
info: .init(
179+
apiKeyName: "key",
180+
baseURL: "google.com",
181+
maxTokens: 3000,
182+
supportsFunctionCalling: true,
183+
modelName: "gpt-3.5-turbo"
184+
)
185+
),
186+
ChatModel(
187+
id: "2",
188+
name: "Test Model 2",
189+
format: .azureOpenAI,
190+
info: .init(
191+
apiKeyName: "key",
192+
baseURL: "apple.com",
193+
maxTokens: 3000,
194+
supportsFunctionCalling: false,
195+
modelName: "gpt-3.5-turbo"
196+
)
197+
),
198+
ChatModel(
199+
id: "3",
200+
name: "Test Model 3",
201+
format: .openAICompatible,
202+
info: .init(
203+
apiKeyName: "key",
204+
baseURL: "apple.com",
205+
maxTokens: 3000,
206+
supportsFunctionCalling: false,
207+
modelName: "gpt-3.5-turbo"
208+
)
209+
),
210+
]),
211+
editingModel: .init(
212+
model: ChatModel(
213+
id: "3",
214+
name: "Test Model 3",
215+
format: .openAICompatible,
216+
info: .init(
217+
apiKeyName: "key",
218+
baseURL: "apple.com",
219+
maxTokens: 3000,
220+
supportsFunctionCalling: false,
221+
modelName: "gpt-3.5-turbo"
222+
)
223+
)
224+
)
225+
),
226+
reducer: ChatModelManagement()
227+
)
228+
)
229+
}
230+
}
231+
232+
class AIModelManagement_Empty_Previews: PreviewProvider {
233+
static var previews: some View {
234+
AIModelManagementView<ChatModelManagement, _>(
235+
store: .init(
236+
initialState: .init(models: []),
237+
reducer: ChatModelManagement()
238+
)
239+
)
240+
}
241+
}
242+
243+
class AIModelManagement_Cell_Previews: PreviewProvider {
228244
static var previews: some View {
229245
AIModelManagementView<ChatModelManagement, ChatModel>.Cell(model: ChatModel(
230246
id: "1",
@@ -239,6 +255,5 @@ struct AIModelManagementView<Management: AIModelManagement, Model: ManageableAIM
239255
)
240256
), isSelected: false)
241257
}
242-
}
243-
258+
}
244259

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import SwiftUI
21
import ComposableArchitecture
2+
import SwiftUI
33

44
struct BaseURLPicker: View {
55
let prompt: Text?
66
let store: StoreOf<BaseURLSelection>
7-
7+
88
var body: some View {
99
WithViewStore(store) { viewStore in
1010
TextField("Base URL", text: viewStore.$baseURL, prompt: prompt)
@@ -13,13 +13,23 @@ struct BaseURLPicker: View {
1313
"",
1414
selection: viewStore.$baseURL,
1515
content: {
16+
if !viewStore.state.availableBaseURLs
17+
.contains(viewStore.state.baseURL)
18+
{
19+
Text("Custom Value").tag(viewStore.state.baseURL)
20+
}
21+
1622
ForEach(viewStore.state.availableBaseURLs, id: \.self) { baseURL in
1723
Text(baseURL).tag(baseURL)
1824
}
1925
}
2026
)
2127
.frame(width: 20)
2228
}
29+
.onAppear {
30+
viewStore.send(.appear)
31+
}
2332
}
2433
}
2534
}
35+

Core/Sources/HostApp/AccountSettings/SharedModelManagement/BaseURLSelection.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct BaseURLSelection: ReducerProtocol {
3131
case .refreshAvailableBaseURLNames:
3232
let chatModels = userDefaults.value(for: \.chatModels)
3333
let embeddingModels = userDefaults.value(for: \.embeddingModels)
34-
var allBaseURLs = Set(
34+
let allBaseURLs = Set(
3535
chatModels.map(\.info.baseURL) + embeddingModels.map(\.info.baseURL)
3636
)
3737
state.availableBaseURLs = Array(allBaseURLs).sorted()

0 commit comments

Comments
 (0)