Skip to content

Commit

Permalink
✨ Add a feature to delete all chats. Add a feature to set custom mode…
Browse files Browse the repository at this point in the history
…l name. Add a feature to set API URL. Automatically open lats opened chat. Fix bug when incorrect chat is opened on creating new chat
  • Loading branch information
Renset committed Nov 7, 2023
1 parent 8067ded commit cae233c
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 17 deletions.
8 changes: 4 additions & 4 deletions macai.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1.1.1;
CURRENT_PROJECT_VERSION = 1.1.2;
DEVELOPMENT_TEAM = ZRB8WDV435;
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
Expand All @@ -610,7 +610,7 @@
LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 12.0;
MARKETING_VERSION = 1.1.1;
MARKETING_VERSION = 1.1.2;
ONLY_ACTIVE_ARCH = YES;
PRODUCT_BUNDLE_IDENTIFIER = notfullin.com.macai;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -633,7 +633,7 @@
CODE_SIGN_IDENTITY = "Apple Development";
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1.1.1;
CURRENT_PROJECT_VERSION = 1.1.2;
DEVELOPMENT_TEAM = ZRB8WDV435;
ENABLE_HARDENED_RUNTIME = YES;
ENABLE_PREVIEWS = YES;
Expand All @@ -652,7 +652,7 @@
LD_RUNPATH_SEARCH_PATHS = "@executable_path/Frameworks";
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "@executable_path/../Frameworks";
MACOSX_DEPLOYMENT_TARGET = 12.0;
MARKETING_VERSION = 1.1.1;
MARKETING_VERSION = 1.1.2;
ONLY_ACTIVE_ARCH = NO;
PRODUCT_BUNDLE_IDENTIFIER = notfullin.com.macai;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down
1 change: 1 addition & 0 deletions macai/Store/ChatStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ class ChatStore: ObservableObject {
print("Migration from JSON successful")
}
catch {
UserDefaults.standard.set(true, forKey: migrationKey)
print("Error migrating chats: \(error)")
}

Expand Down
5 changes: 5 additions & 0 deletions macai/UI/Chat/ChatView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct ChatView: View {
@AppStorage("gptToken") var gptToken = ""
@AppStorage("gptModel") var gptModel = AppConstants.chatGptDefaultModel
@AppStorage("chatContext") var chatContext = AppConstants.chatGptContextSize
@AppStorage("lastOpenedChatId") var lastOpenedChatId = ""
@State var messageCount: Int = 0
@State private var messageField = ""
@State private var lastMessageError = false
Expand Down Expand Up @@ -155,6 +156,10 @@ struct ChatView: View {
}
.background(backgroundColor)
.navigationTitle("ChatGPT")
.onAppear(perform: {
self.lastOpenedChatId = chat.id.uuidString
print("lastOpenedChatId: \(lastOpenedChatId)")
})
}
}

Expand Down
17 changes: 15 additions & 2 deletions macai/UI/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ struct ContentView: View {
@AppStorage("gptToken") var gptToken = ""
@AppStorage("gptModel") var gptModel = "gpt-3.5-turbo"
@AppStorage("systemMessage") var systemMessage = AppConstants.chatGptSystemMessage
@AppStorage("lastOpenedChatId") var lastOpenedChatId = ""

@State private var windowRef: NSWindow?

Expand Down Expand Up @@ -92,7 +93,16 @@ struct ContentView: View {
)

}
.onAppear(perform: updateOldChatsOnceIssue7)
.onAppear(perform: {
updateOldChatsOnceIssue7()

if let lastOpenedChatId = UUID(uuidString: lastOpenedChatId) {
if let lastOpenedChat = chats.first(where: { $0.id == lastOpenedChatId }) {
selectedChat = lastOpenedChat
}
}

})
.navigationTitle("Chats")
.toolbar {
// Button to hide and display Navigation List
Expand Down Expand Up @@ -151,7 +161,10 @@ struct ContentView: View {

do {
try viewContext.save()
selectedChat = newChat
// Fix bug when new chat is not selected after creation by using DispatchQueue
DispatchQueue.main.async {
selectedChat = newChat
}
}
catch {
print("Error saving new chat: \(error.localizedDescription)")
Expand Down
80 changes: 71 additions & 9 deletions macai/UI/Preferences/PreferencesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ struct APIRequestData: Codable {

}

func testAPIToken(model: String, token: String, completion: @escaping (Result<Bool, Error>) -> Void) {
let url = URL(string: "https://api.openai.com/v1/chat/completions")!
func testAPIToken(apiUrl: String, model: String, token: String, completion: @escaping (Result<Bool, Error>) -> Void) {
let url = URL(string: apiUrl)!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
Expand Down Expand Up @@ -68,9 +68,12 @@ struct PreferencesView: View {
@AppStorage("gptModel") var gptModel: String = AppConstants.chatGptDefaultModel
@AppStorage("systemMessage") var systemMessage = AppConstants.chatGptSystemMessage
@AppStorage("chatContext") var chatContext: Double = AppConstants.chatGptContextSize
@AppStorage("isCustomGptModel") var isCustomGptModel: Bool = false
@AppStorage("apiUrl") var apiUrl: String = AppConstants.apiUrlChatCompletions
@StateObject private var store = ChatStore(persistenceController: PersistenceController.shared)
@State private var lampColor: Color = .gray
@State private var previousGptModel: String = ""
@State private var selectedGptModel: String = ""

var body: some View {
ScrollView {
Expand All @@ -86,6 +89,15 @@ struct PreferencesView: View {
Spacer()
}

HStack {
Text("ChatGPT API URL:")
.frame(width: 160, alignment: .leading)

TextField("Paste your URL here", text: $apiUrl)
.textFieldStyle(RoundedBorderTextFieldStyle())

}

HStack {
Text("ChatGPT API Token:")
.frame(width: 160, alignment: .leading)
Expand Down Expand Up @@ -129,7 +141,7 @@ struct PreferencesView: View {
.padding(.top, 16)

}
.padding(.bottom)
.padding(16)

Spacer()

Expand All @@ -145,7 +157,7 @@ struct PreferencesView: View {
Text("ChatGPT Model:")
.frame(width: 160, alignment: .leading)

Picker("", selection: $gptModel) {
Picker("", selection: $selectedGptModel) {
Text("gpt-3.5-turbo").tag("gpt-3.5-turbo")
Text("gpt-3.5-turbo-0301").tag("gpt-3.5-turbo-0301")
Text("gpt-4").tag("gpt-4")
Expand All @@ -154,14 +166,29 @@ struct PreferencesView: View {
Text("gpt-4-32k-0314").tag("gpt-4-32k-0314")
Text("gpt-4-1106-preview").tag("gpt-4-1106-preview")
Text("gpt-4-vision-preview").tag("gpt-4-vision-preview")
}.onReceive([self.gptModel].publisher.first()) { newValue in
Text("Enter custom model").tag("custom")

}.onReceive([self.selectedGptModel].publisher.first()) { newValue in
if newValue == "custom" {
self.isCustomGptModel = true
} else {
self.isCustomGptModel = false
}

if self.gptModel != self.previousGptModel {
self.lampColor = .gray
self.previousGptModel = self.gptModel
}
}
}

if (self.isCustomGptModel) {
VStack {
TextField("Enter custom model name", text: $gptModel)
.textFieldStyle(RoundedBorderTextFieldStyle())
}
}

HStack {
Spacer()
Link(
Expand Down Expand Up @@ -203,7 +230,7 @@ struct PreferencesView: View {
Spacer()
Button(action: {
lampColor = .yellow
testAPIToken(model: gptModel, token: gptToken) { result in
testAPIToken(apiUrl: apiUrl, model: gptModel, token: gptToken) { result in
DispatchQueue.main.async {
switch result {
case .success(_):
Expand Down Expand Up @@ -307,19 +334,54 @@ struct PreferencesView: View {
}
}
}
}
Spacer()
VStack {
HStack {
Text("Danger Zone")
.font(.headline)
Spacer()
}

HStack {
Button(action: {
let alert = NSAlert()
alert.messageText = "Are you sure you want to delete all chats?"
alert.informativeText = "This action cannot be undone. It's recommended to make an export of your chats before deleting them."
alert.alertStyle = .warning
alert.addButton(withTitle: "Delete")
alert.addButton(withTitle: "Cancel")
alert.beginSheetModal(for: NSApp.mainWindow!) { (response) in
if response == .alertFirstButtonReturn {
store.deleteAllChats()
}
}
}, label: {
Text("Delete all chats").foregroundColor(.red)
})
Spacer()
}


}

VStack {
HStack {
Text("More options for ChatGPT API settings are coming soon, stay tuned!")
.font(.system(size: 8))
}
.padding(.top, 20)
}
}

.padding(16)
}
.padding(16)
.frame(width: 420, height: 580)
.frame(width: 420, height: 650)
.onAppear(perform: {
if (self.isCustomGptModel) {
self.selectedGptModel = "custom"
} else {
self.selectedGptModel = self.gptModel
}
store.saveInCoreData()
})
}
Expand Down
3 changes: 1 addition & 2 deletions macai/UI/WelcomeScreen/WelcomeScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ struct WelcomeScreen: View {
else {
//
VStack {
WelcomeIcon()

VStack {
if chatsCount == 0 {
WelcomeIcon()
Text("No chats were created yet. Create new one?")
Button("Create new chat") {
newChat()
Expand Down

0 comments on commit cae233c

Please sign in to comment.