Skip to content

Commit

Permalink
model improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ctate committed May 24, 2024
1 parent 7aca213 commit ecf60af
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
10 changes: 8 additions & 2 deletions platforms/apple/Crystal/APIs/OpenAiApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,14 @@ class OpenAiApi: ObservableObject {
return
}

guard let loadedData = load(key: "\(bundleIdentifier).OpenAIApiKey") else { return }
guard let apiKey = String(data: loadedData, encoding: .utf8) else { return }
guard let loadedData = load(key: "\(bundleIdentifier).OpenAIApiKey") else {
alertError("No API key found")
return
}
guard let apiKey = String(data: loadedData, encoding: .utf8) else {
alertError("Failed to load API key")
return
}

var request = URLRequest(url: url)
request.httpMethod = "POST"
Expand Down
22 changes: 18 additions & 4 deletions platforms/apple/Crystal/Chat/ChatToolbarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ struct ChatToolbarView: ToolbarContent {

var sidebarAction: (() -> Void)? = {}
var historyAction: (() -> Void)? = {}
var settingsAction: (() -> Void)? = {}
var settingsAction: ((_ name: String) -> Void)? = { _ in }
var newAction: (() -> Void)? = {}

@State private var functionsDisabled = UserDefaults.standard.bool(forKey: "functionsDisabled")
Expand All @@ -26,7 +26,7 @@ struct ChatToolbarView: ToolbarContent {
}
.keyboardShortcut("/", modifiers: .command)
}
ToolbarItem(placement: .principal) {
ToolbarItem(placement: .navigation) {
Button(action: {
showPopover = true
}) {
Expand Down Expand Up @@ -58,6 +58,19 @@ struct ChatToolbarView: ToolbarContent {

Text(provider.name)
.bold()

if !UserDefaults.standard.bool(forKey: "\(provider.id):isEnabled") {
Text("(Disabled)")
.bold()
Spacer()
Button(action: {
if settingsAction != nil {
settingsAction!("models")
}
}) {
Text("Settings")
}
}
}
.padding(.horizontal, 20)
.padding(.vertical, 5)
Expand All @@ -71,15 +84,16 @@ struct ChatToolbarView: ToolbarContent {
}) {
HStack {
Text(model.name)
.foregroundColor(!UserDefaults.standard.bool(forKey: "\(provider.id):isEnabled") ? .gray : .black)
Spacer()
if selectedModelId == model.id {
Image(systemName: "checkmark")
}
}
}
.disabled(!UserDefaults.standard.bool(forKey: "\(provider.id):isEnabled"))
.padding(.horizontal, 20)
.padding(.vertical, 5)
.foregroundColor(.black)
.buttonStyle(PlainButtonStyle())
}
}
Expand All @@ -105,7 +119,7 @@ struct ChatToolbarView: ToolbarContent {
#if os(iOS)
Button(action: {
if settingsAction != nil {
settingsAction!()
settingsAction!("general")
}
}) {
Image(systemName: "gear")
Expand Down
8 changes: 6 additions & 2 deletions platforms/apple/Crystal/Chat/ChatView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct ChatContentView: View {
@State private var isSheetPresented = false
@State private var keyboardHeight: CGFloat = 0
@State var presentSheet = ""
@State private var defaultSettingsTab = "general"
@State private var showPopover = false
@State private var showRecorder = false
@State private var textHeight: CGFloat = 30
Expand Down Expand Up @@ -171,7 +172,9 @@ struct ChatContentView: View {
ChatHistoryView()
#endif
case "settings":
SettingsView()
NavigationView {
SettingsView(selectedTab: defaultSettingsTab)
}
default:
Text("No sheet")
}
Expand All @@ -188,7 +191,8 @@ struct ChatContentView: View {
}
}, historyAction: {
presentSheet = "history"
}, settingsAction: {
}, settingsAction: { tab in
defaultSettingsTab = tab
presentSheet = "settings"
}, newAction: {
viewModel.currentView = AnyView(TextCard(text: "How can I help?"))
Expand Down
2 changes: 1 addition & 1 deletion platforms/apple/Crystal/CrystalApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ struct CrystalApp: App {

#if os(macOS)
Settings {
SettingsView()
SettingsView(selectedTab: "general")
}
#endif
}
Expand Down
10 changes: 8 additions & 2 deletions platforms/apple/Crystal/Sheets/SettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -417,32 +417,38 @@ struct DataSettingsView: View {
}

struct SettingsView: View {
@State var selectedTab: String

#if os(macOS)
let screenWidth = NSScreen.main?.visibleFrame.width ?? 800
let screenHeight = NSScreen.main?.visibleFrame.height ?? 600
#endif

var body: some View {
TabView {
TabView(selection: $selectedTab) {
GeneralSettingsView()
.tabItem {
Label("General", systemImage: "gear")
}
.tag("general")

ModelsSettingsView()
.tabItem {
Label("Models", systemImage: "diamond")
}
.tag("models")

IntegrationsSettingsView()
.tabItem {
Label("Integrations", systemImage: "glowplug")
}
.tag("integrations")

DataSettingsView()
.tabItem {
Label("Data", systemImage: "externaldrive")
}
.tag("data")
}
#if os(macOS)
.frame(width: screenWidth/2, height: screenHeight/2)
Expand All @@ -453,5 +459,5 @@ struct SettingsView: View {

// Preview for the SwiftUI canvas
#Preview {
SettingsView()
SettingsView(selectedTab: "general")
}

0 comments on commit ecf60af

Please sign in to comment.