Merge branch 'main' of github.com:MustangYM/OSXChatGpt

# Conflicts:
#	OSXChatGPT/OSXChatGPT/DataProvider/ChatGPTManager.swift
This commit is contained in:
陈连辰
2023-03-23 23:32:37 +08:00
4 changed files with 19 additions and 6 deletions

View File

@@ -3,7 +3,7 @@
archiveVersion = 1; archiveVersion = 1;
classes = { classes = {
}; };
objectVersion = 56; objectVersion = 50;
objects = { objects = {
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */

View File

@@ -7,14 +7,14 @@
import Foundation import Foundation
private let OSXChatGPTKEY = "OSXChatGPT_apiKey_key"
class ChatGPTManager { class ChatGPTManager {
static let shared = ChatGPTManager() static let shared = ChatGPTManager()
private let saveApi_key: String = "OSXChatGPT_apiKey_key"
private var messagesDict: [String:[Message]] = [:] private var messagesDict: [String:[Message]] = [:]
private let httpClient: HTTPClient = HTTPClient() private let httpClient: HTTPClient = HTTPClient()
private lazy var apiKey : String = { private lazy var apiKey : String = {
let key = UserDefaults.standard.value(forKey: saveApi_key) as? String let key = UserDefaults.standard.value(forKey: OSXChatGPTKEY) as? String
return key ?? "" return key ?? ""
}() }()
private var askContextCount: Int = 3 private var askContextCount: Int = 3
@@ -32,11 +32,12 @@ extension ChatGPTManager {
return return
} }
self.apiKey = apiKey self.apiKey = apiKey
UserDefaults.standard.set(apiKey, forKey: saveApi_key) UserDefaults.standard.set(apiKey, forKey: OSXChatGPTKEY)
} }
func getApiKey() -> String { func getApiKey() -> String {
let key = String(self.apiKey) let key = String(self.apiKey)
return key return key
} }
func getMaskApiKey() -> String { func getMaskApiKey() -> String {
var key = String(self.apiKey) var key = String(self.apiKey)

View File

@@ -4,7 +4,9 @@
// //
// Created by MustangYM on 2023/3/11. // Created by MustangYM on 2023/3/11.
// //
/*
https://platform.openai.com/docs/api-reference/introduction
*/
import AppKit import AppKit
import SwiftUI import SwiftUI

View File

@@ -27,6 +27,8 @@ struct ChatRoomView: View {
@State private var scrollID = UUID() @State private var scrollID = UUID()
@State private var isOnAppear: Bool = false @State private var isOnAppear: Bool = false
@State var apiKey: String = ChatGPTManager.shared.getMaskApiKey()
@State var openArgumentSeet: Bool = false
init(conversation: Conversation?) { init(conversation: Conversation?) {
self.conversation = conversation self.conversation = conversation
@@ -120,6 +122,11 @@ struct ChatRoomView: View {
.onAppear { .onAppear {
print("View appeared!") print("View appeared!")
if apiKey.count < 10 {
}
KeyboardMonitor.shared.startMonitorPasteboard() KeyboardMonitor.shared.startMonitorPasteboard()
KeyboardMonitor.shared.startMonitorShiftKey() KeyboardMonitor.shared.startMonitorShiftKey()
viewModel.currentConversation = conversation viewModel.currentConversation = conversation
@@ -137,6 +144,9 @@ struct ChatRoomView: View {
self.isOnAppear = false self.isOnAppear = false
} }
.sheet(isPresented: $openArgumentSeet) {
EnterAPIView(apiKey: $apiKey)
}
} }
@@ -179,7 +189,7 @@ struct ChatRoomView: View {
private func sendMessage(scrollView: ScrollViewProxy?, text: String) { private func sendMessage(scrollView: ScrollViewProxy?, text: String) {
guard !newMessageText.isEmpty else { return } guard !newMessageText.isEmpty else { return }
let msg = String(text) let msg = String(newMessageText.dropLast())
let replaceStr = msg.replacingOccurrences(of: " ", with: "") let replaceStr = msg.replacingOccurrences(of: " ", with: "")
if replaceStr.count == 0 { if replaceStr.count == 0 {
newMessageText = "" newMessageText = ""