Skip to content

Commit

Permalink
add more function
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderLineChan committed Mar 27, 2023
1 parent 1f12ed7 commit 7b2efae
Show file tree
Hide file tree
Showing 13 changed files with 249 additions and 20 deletions.
28 changes: 20 additions & 8 deletions OSXChatGPT/OSXChatGPT.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
CB1DCAC629B4F09F00B1D4E1 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = CB1DCAC529B4F09F00B1D4E1 /* Assets.xcassets */; };
CB1DCAC929B4F09F00B1D4E1 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = CB1DCAC829B4F09F00B1D4E1 /* Preview Assets.xcassets */; };
CB27655C29D1C12C00897E0E /* MarkdownView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB27655B29D1C12C00897E0E /* MarkdownView.swift */; };
CB27656629D1DA9800897E0E /* AIPromptView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB27656529D1DA9800897E0E /* AIPromptView.swift */; };
CB27656929D1E65100897E0E /* Conversation+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB27656729D1E65100897E0E /* Conversation+CoreDataClass.swift */; };
CB27656A29D1E65100897E0E /* Conversation+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB27656829D1E65100897E0E /* Conversation+CoreDataProperties.swift */; };
CB27656D29D1E6A100897E0E /* Prompt+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB27656B29D1E6A100897E0E /* Prompt+CoreDataClass.swift */; };
CB27656E29D1E6A100897E0E /* Prompt+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB27656C29D1E6A100897E0E /* Prompt+CoreDataProperties.swift */; };
CB28A52229C07BE500F0286A /* KeyboardMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB28A52129C07BE500F0286A /* KeyboardMonitor.swift */; };
CB28A52829C1569900F0286A /* ThinkingAnimationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB28A52729C1569900F0286A /* ThinkingAnimationView.swift */; };
CB2F971F29CE12B6004EBD96 /* MarkdownUI in Frameworks */ = {isa = PBXBuildFile; productRef = CB2F971E29CE12B6004EBD96 /* MarkdownUI */; };
Expand All @@ -38,8 +43,6 @@
CB2F972829CEFB65004EBD96 /* ChatRoomToolBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB2F972729CEFB65004EBD96 /* ChatRoomToolBar.swift */; };
CBC4B12329B8D28D00650296 /* Message+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBC4B11D29B8D28D00650296 /* Message+CoreDataClass.swift */; };
CBC4B12429B8D28D00650296 /* Message+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBC4B11E29B8D28D00650296 /* Message+CoreDataProperties.swift */; };
CBC4B12529B8D28D00650296 /* Conversation+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBC4B11F29B8D28D00650296 /* Conversation+CoreDataClass.swift */; };
CBC4B12629B8D28D00650296 /* Conversation+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = CBC4B12029B8D28D00650296 /* Conversation+CoreDataProperties.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -66,15 +69,18 @@
CB1DCACA29B4F09F00B1D4E1 /* OSXChatGPT.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = OSXChatGPT.entitlements; sourceTree = "<group>"; };
CB228EA129CD4949006B3559 /* libz.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libz.tbd; path = usr/lib/libz.tbd; sourceTree = SDKROOT; };
CB27655B29D1C12C00897E0E /* MarkdownView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MarkdownView.swift; sourceTree = "<group>"; };
CB27656529D1DA9800897E0E /* AIPromptView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AIPromptView.swift; sourceTree = "<group>"; };
CB27656729D1E65100897E0E /* Conversation+CoreDataClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Conversation+CoreDataClass.swift"; sourceTree = "<group>"; };
CB27656829D1E65100897E0E /* Conversation+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Conversation+CoreDataProperties.swift"; sourceTree = "<group>"; };
CB27656B29D1E6A100897E0E /* Prompt+CoreDataClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Prompt+CoreDataClass.swift"; sourceTree = "<group>"; };
CB27656C29D1E6A100897E0E /* Prompt+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Prompt+CoreDataProperties.swift"; sourceTree = "<group>"; };
CB28A52129C07BE500F0286A /* KeyboardMonitor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeyboardMonitor.swift; sourceTree = "<group>"; };
CB28A52729C1569900F0286A /* ThinkingAnimationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThinkingAnimationView.swift; sourceTree = "<group>"; };
CB2F972129CED6AE004EBD96 /* ChatRoomInputView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatRoomInputView.swift; sourceTree = "<group>"; };
CB2F972729CEFB65004EBD96 /* ChatRoomToolBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatRoomToolBar.swift; sourceTree = "<group>"; };
CBC4B0FE29B8BF9600650296 /* OSXChatGPT.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = OSXChatGPT.xcdatamodel; sourceTree = "<group>"; };
CBC4B11D29B8D28D00650296 /* Message+CoreDataClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Message+CoreDataClass.swift"; sourceTree = "<group>"; };
CBC4B11E29B8D28D00650296 /* Message+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Message+CoreDataProperties.swift"; sourceTree = "<group>"; };
CBC4B11F29B8D28D00650296 /* Conversation+CoreDataClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Conversation+CoreDataClass.swift"; sourceTree = "<group>"; };
CBC4B12029B8D28D00650296 /* Conversation+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Conversation+CoreDataProperties.swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -119,6 +125,7 @@
CB2F972729CEFB65004EBD96 /* ChatRoomToolBar.swift */,
CB2F972129CED6AE004EBD96 /* ChatRoomInputView.swift */,
182B436429BC5C8700F06778 /* UserInitializeView.swift */,
CB27656529D1DA9800897E0E /* AIPromptView.swift */,
182B436129BC5C8700F06778 /* View.swift */,
182B437A29BC5FBE00F06778 /* EnterAPIView.swift */,
188FB46629C1FA9700E3C18F /* EidtSessionRemarkView.swift */,
Expand Down Expand Up @@ -201,10 +208,12 @@
CBC4B11429B8CB1B00650296 /* Models */ = {
isa = PBXGroup;
children = (
CB27656B29D1E6A100897E0E /* Prompt+CoreDataClass.swift */,
CB27656C29D1E6A100897E0E /* Prompt+CoreDataProperties.swift */,
CB27656729D1E65100897E0E /* Conversation+CoreDataClass.swift */,
CB27656829D1E65100897E0E /* Conversation+CoreDataProperties.swift */,
CBC4B11D29B8D28D00650296 /* Message+CoreDataClass.swift */,
CBC4B11E29B8D28D00650296 /* Message+CoreDataProperties.swift */,
CBC4B11F29B8D28D00650296 /* Conversation+CoreDataClass.swift */,
CBC4B12029B8D28D00650296 /* Conversation+CoreDataProperties.swift */,
182B43A329BF730300F06778 /* NSColor.swift */,
);
path = Models;
Expand Down Expand Up @@ -294,23 +303,26 @@
CB2F972029CE1ADC004EBD96 /* OSXChatGPT.xcdatamodeld in Sources */,
182B436929BC5C8700F06778 /* UserInitializeView.swift in Sources */,
182B43A429BF730300F06778 /* NSColor.swift in Sources */,
CB27656629D1DA9800897E0E /* AIPromptView.swift in Sources */,
182B436B29BC5CBA00F06778 /* AppDelegate.swift in Sources */,
182B437329BC5D1B00F06778 /* CoreDataManager.swift in Sources */,
188FB46729C1FA9700E3C18F /* EidtSessionRemarkView.swift in Sources */,
CB27656A29D1E65100897E0E /* Conversation+CoreDataProperties.swift in Sources */,
182B436629BC5C8700F06778 /* View.swift in Sources */,
182B437B29BC5FBE00F06778 /* EnterAPIView.swift in Sources */,
CB0F5A5F29D059C4005B71D2 /* TextOutputFormat.swift in Sources */,
182B437929BC5D6200F06778 /* OSXChatGPTApp.swift in Sources */,
CB28A52229C07BE500F0286A /* KeyboardMonitor.swift in Sources */,
182B437429BC5D1B00F06778 /* ViewModel.swift in Sources */,
182B436729BC5C8700F06778 /* ChatRoomView.swift in Sources */,
CB27656929D1E65100897E0E /* Conversation+CoreDataClass.swift in Sources */,
CB27656E29D1E6A100897E0E /* Prompt+CoreDataProperties.swift in Sources */,
CB0F5A6029D059C4005B71D2 /* SplashCodeSyntaxHighlighter.swift in Sources */,
CBC4B12429B8D28D00650296 /* Message+CoreDataProperties.swift in Sources */,
CBC4B12629B8D28D00650296 /* Conversation+CoreDataProperties.swift in Sources */,
182B436529BC5C8700F06778 /* SessionsView.swift in Sources */,
CB27656D29D1E6A100897E0E /* Prompt+CoreDataClass.swift in Sources */,
182B437229BC5D1B00F06778 /* HTTPClient.swift in Sources */,
182B436829BC5C8700F06778 /* MainContentView.swift in Sources */,
CBC4B12529B8D28D00650296 /* Conversation+CoreDataClass.swift in Sources */,
CB28A52829C1569900F0286A /* ThinkingAnimationView.swift in Sources */,
182B437529BC5D1B00F06778 /* ChatGPTManager.swift in Sources */,
CB2F972229CED6AE004EBD96 /* ChatRoomInputView.swift in Sources */,
Expand Down
19 changes: 16 additions & 3 deletions OSXChatGPT/OSXChatGPT/DataProvider/ChatGPTManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ enum ChatGPTAnswerType: CaseIterable, ToolBarMenuProtocol {
}
}
enum ChatGPTContext: CaseIterable, ToolBarMenuProtocol {
case context0
case context1
case context2
case context3
Expand All @@ -109,6 +110,8 @@ enum ChatGPTContext: CaseIterable, ToolBarMenuProtocol {

var value: String {
switch self {
case .context0:
return "0"
case .context1:
return "1"
case .context2:
Expand All @@ -130,14 +133,15 @@ enum ChatGPTContext: CaseIterable, ToolBarMenuProtocol {
case .context10:
return "10"
case .infinite:
return "99999"
return "无限"
}
}
var valyeInt: Int {
return Int(self.value) ?? 1
}
static var allCases: [ChatGPTContext] {
return [.context1,
return [.context0,
.context1,
.context2,
.context3,
.context4,
Expand Down Expand Up @@ -222,7 +226,7 @@ class ChatGPTManager {
let key = UserDefaults.standard.value(forKey: OSXChatGPTKEY) as? String
return key ?? ""
}()
var chatGPTSpeaking: Bool = false
@Published var chatGPTSpeaking: Bool = false
var askContextCount: ChatGPTContext = .context3
let gptRoleString: String = "assistant"
var tempMessagePool = MessagePool()
Expand Down Expand Up @@ -260,6 +264,12 @@ extension ChatGPTManager {
}
// MARK: - Chat GTP 提问
extension ChatGPTManager {
func stopResponse() {
httpClient.cancelStream()
self.chatGPTSpeaking = false


}
func askChatGPTStream(messages: [Message], complete:((ChatGPTResponse) -> ())?) {
if chatGPTSpeaking == true {
return
Expand All @@ -271,6 +281,9 @@ extension ChatGPTManager {
let stream = try await httpClient.postStream(chatRequest: request)
let res = ChatGPTResponse(request: request ,state: .replyStart, text: "")
await tempMessagePool.reset()
if self.chatGPTSpeaking == false {
return
}
DispatchQueue.main.async {
complete?(res)
}
Expand Down
31 changes: 30 additions & 1 deletion OSXChatGPT/OSXChatGPT/DataProvider/ViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ import CoreData
import Splash



@MainActor class ViewModel: ObservableObject {
@Published var conversations: [Conversation] = []//所有会话
@Published var messages: [Message] = []//当前会话的消息
@Published var showUserInitialize: Bool = false//显示设置页
@Published var showEditRemark: Bool = false//显示编辑备注
@Published var showAIPrompt: Bool = false //显示自定义提示
var editConversation: Conversation?//编辑备注的会话
@Published var createNewChat: Bool = false//创建新会话
@Published var changeMsgText: String = ""
@State var scrollID = UUID()
var currentConversation: Conversation?//当前会话

@State var model: ChatGPTModel = ChatGPTManager.shared.model
@Published var showStopAnswerBtn: Bool = false

private var allChatRoomViews: [String:ChatRoomView] = [:]

Expand Down Expand Up @@ -156,6 +159,10 @@ extension ViewModel {
messages.insert(contentsOf: results, at: 0)
}
}
func deleteMessage(message: Message) {
messages.removeAll(where: {$0.id == message.id})
CoreDataManager.shared.delete(object: message)
}
func deleteAllMessage(sesstionId: String) {
let request: NSFetchRequest<Message> = Message.fetchRequest()
request.predicate = NSPredicate(format: "sesstionId == %@", sesstionId)
Expand Down Expand Up @@ -198,6 +205,9 @@ extension ViewModel {
if sesstionId.isEmpty {
return
}
if ChatGPTManager.shared.chatGPTSpeaking {
return
}
let msg = Message(context: CoreDataManager.shared.container.viewContext)
msg.sesstionId = sesstionId
msg.text = text
Expand All @@ -214,10 +224,27 @@ extension ViewModel {
sendMsgs.removeAll(where: {$0.type == 1})
sendMessage(sesstionId: sesstionId, messages: sendMsgs, updateBlock: updateBlock)
}

func cancel() {
ChatGPTManager.shared.stopResponse()
self.removeGptThinkMessage()
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
//停止后保存数据到数据库
CoreDataManager.shared.saveData()
//删除最后一天没有内容的消息
if let lastMsg = self.messages.last {
if lastMsg.text == nil || lastMsg.text!.isEmpty {
self.messages.removeAll(where: {$0.id == lastMsg.id})
CoreDataManager.shared.delete(object: lastMsg)
}
}
}
}
private func sendMessage(sesstionId: String, messages: [Message], updateBlock: @escaping(() -> ())) {
var isFeedback = false
var newMsg: Message?
if ChatGPTManager.shared.answerType.valueBool {
self.showStopAnswerBtn = true
}
ChatGPTManager.shared.askChatGPTStream(messages: messages) { rsp in
if rsp.request.answerType == .stream {
isFeedback = true
Expand Down Expand Up @@ -263,13 +290,15 @@ extension ViewModel {
self.messages[self.messages.count - 1] = newMsg!//更新UI
self.changeMsgText = rsp.text//更新滚动
}
self.showStopAnswerBtn = false
updateBlock()
}else if rsp.state == .replyFinish {
self.updateConversation(sesstionId: sesstionId, message:newMsg)
CoreDataManager.shared.saveData()
if self.currentConversation?.sesstionId == sesstionId {
self.changeMsgText = rsp.text//更新滚动
}
self.showStopAnswerBtn = false
updateBlock()
}
}else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Conversation+CoreDataClass.swift
// OSXChatGPT
//
// Created by CoderChan on 2023/3/8.
// Created by CoderChan on 2023/3/27.
//
//

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Conversation+CoreDataProperties.swift
// OSXChatGPT
//
// Created by CoderChan on 2023/3/8.
// Created by CoderChan on 2023/3/27.
//
//

Expand All @@ -16,12 +16,13 @@ extension Conversation {
return NSFetchRequest<Conversation>(entityName: "Conversation")
}

@NSManaged public var id: UUID?
@NSManaged public var remark: String?
@NSManaged public var sesstionId: String
@NSManaged public var updateData: Date?
@NSManaged public var id: UUID?
@NSManaged public var lastMessage: Message?
@NSManaged public var remark: String?
@NSManaged public var prompt: Prompt?

}

extension Conversation : Identifiable {
Expand Down
15 changes: 15 additions & 0 deletions OSXChatGPT/OSXChatGPT/Models/Prompt+CoreDataClass.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Prompt+CoreDataClass.swift
// OSXChatGPT
//
// Created by CoderChan on 2023/3/27.
//
//

import Foundation
import CoreData


public class Prompt: NSManagedObject {

}
48 changes: 48 additions & 0 deletions OSXChatGPT/OSXChatGPT/Models/Prompt+CoreDataProperties.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// Prompt+CoreDataProperties.swift
// OSXChatGPT
//
// Created by CoderChan on 2023/3/27.
//
//

import Foundation
import CoreData


extension Prompt {

@nonobjc public class func fetchRequest() -> NSFetchRequest<Prompt> {
return NSFetchRequest<Prompt>(entityName: "Prompt")
}

@NSManaged public var title: String?
@NSManaged public var prompt: String?
@NSManaged public var author: String?
@NSManaged public var id: UUID?
@NSManaged public var createdDate: Date?
@NSManaged public var serial: Int64
@NSManaged public var sesstion: NSSet?

}

// MARK: Generated accessors for sesstion
extension Prompt {

@objc(addSesstionObject:)
@NSManaged public func addToSesstion(_ value: Conversation)

@objc(removeSesstionObject:)
@NSManaged public func removeFromSesstion(_ value: Conversation)

@objc(addSesstion:)
@NSManaged public func addToSesstion(_ values: NSSet)

@objc(removeSesstion:)
@NSManaged public func removeFromSesstion(_ values: NSSet)

}

extension Prompt : Identifiable {

}
Loading

0 comments on commit 7b2efae

Please sign in to comment.