123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689 |
- //
- // AINewConfigWindowController.swift
- // PDF Reader Pro Edition
- //
- // Created by Niehaoyu on 2024/4/16.
- //
- import Cocoa
- protocol AIConfigWindowDelegate: AnyObject {
-
- func ai_InputViewDidChooseCurFile(aiConfigWindow: AINewConfigWindowController)
-
- }
- @objcMembers class AINewConfigWindowController: NSWindowController, NSWindowDelegate, AIInfoInputViewDelegate {
- static var currentWindowController: AINewConfigWindowController!
-
- @IBOutlet weak var contendBox: NSBox!
-
- var aiHeaderView: AIHeaderView!
- var aiChatView: AIChatView!
- var aiTypeItemView: AITypeItemChooseView!
- var aiInfoInputView: AIInfoInputView!
-
- var inputStringHeight: CGFloat = 40
- var didSetOriginFrame: Bool = false
-
- var eventLabel: String = "AITools_Start"
-
- weak var aiDelegate: AIConfigWindowDelegate?
-
- var chooseCurFileHandle: ((_ windowVC: AINewConfigWindowController) -> Void)?
-
- deinit {
- DistributedNotificationCenter.default.removeObserver(self)
- }
-
- @objc static func currentWC() -> AINewConfigWindowController {
- if currentWindowController != nil {
- return currentWindowController
- } else {
- let configWC: AINewConfigWindowController = AINewConfigWindowController.init(windowNibName: "AINewConfigWindowController")
- currentWindowController = configWC;
- return currentWindowController
- }
- }
-
- override func showWindow(_ sender: Any?) {
- super.showWindow(sender)
-
- self.window?.delegate = self
-
- self.aiInfoInputView.setUpTranslateUI()
-
- self.aiChatView.reloadData()
-
- self.aiTypeItemView.refreshUI()
-
- self.refreshViewUI()
-
- }
-
- override func windowDidLoad() {
- super.windowDidLoad()
- // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
-
- self.window?.title = NSLocalizedString("AI Tools", comment: "")
-
- self.loadAIHeaderView()
-
- self.loadAIChatView()
-
- self.loadAIInputView()
-
- self.loadAITypeItemView()
-
- self.refreshViewUI()
-
- self.refreshViewColor()
-
- DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChange), name: NSNotification.Name(rawValue: "AppleInterfaceThemeChangedNotification"), object: nil)
-
- }
-
- func refreshViewColor() {
- if KMAppearance.isDarkMode() {
- self.contendBox.fillColor = NSColor(red: 33/255, green: 33/255, blue: 33/255, alpha: 1)
- } else {
- self.contendBox.fillColor = NSColor.white
- }
- self.aiHeaderView.refreshViewColor()
- self.aiInfoInputView.refreshUI()
- self.aiChatView.reloadData()
- self.aiTypeItemView.refreshViewColor()
- }
-
- func loadAIHeaderView() {
- self.aiHeaderView = AIHeaderView.createFromNib()
- self.aiHeaderView.frame = NSMakeRect(0, NSHeight(self.contendBox.frame)-58, NSWidth(self.contendBox.frame), 28)
- self.aiHeaderView.autoresizingMask = [.minXMargin, .maxXMargin, .width, .minYMargin]
- self.contendBox.addSubview(self.aiHeaderView)
-
- }
-
- func loadAIChatView() {
- self.aiChatView = AIChatView.createFromNib()
- self.aiChatView.wantsLayer = true
- self.aiChatView.layer?.backgroundColor = NSColor.clear.cgColor
- self.aiChatView.frame = NSMakeRect(0, 300, NSWidth(self.contendBox.frame), NSHeight(self.contendBox.frame)-400)
- self.aiChatView.autoresizingMask = [.width, .height]
- self.contendBox.addSubview(self.aiChatView)
-
- self.aiChatView.chooseConfigHandle = {[weak self] view, configType in
- DispatchQueue.main.async {
- self?.chooseAIFunctionWithType(configType)
- }
-
- }
-
- self.aiChatView.cancelAIHandle = {[weak self] view, chatInfoModel in
- DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.05) {
- chatInfoModel.chatInfoState = .stateCancel
- self?.aiChatView.reloadData()
- }
- }
-
- self.aiChatView.continueAITranslateHandle = {[weak self] view, chatInfoModel in
- DispatchQueue.main.async {
- chatInfoModel.chatInfoState = .stateLoading
-
- self?.aiChatView.reloadData()
-
- self?.continueAiTranslate(chatInfoModel)
- }
- }
-
- self.aiChatView.redoHandle = {[weak self] view, chatInfoModel in
- DispatchQueue.main.async {
- let newChatModel = AIChatInfoModel.init()
- newChatModel.aiConfigType = chatInfoModel.aiConfigType
- newChatModel.infoType = chatInfoModel.infoType
- newChatModel.filePath = chatInfoModel.filePath
- newChatModel.chatInfoState = .stateLoading
- newChatModel.uploadContent = chatInfoModel.uploadContent
- newChatModel.translateFromLanguage = chatInfoModel.translateFromLanguage
- newChatModel.translateToLanguage = chatInfoModel.translateToLanguage
-
- AIChatInfoManager.defaultManager.modelsArrM.append(newChatModel)
-
- self?.aiChatView.reloadData()
-
- if newChatModel.aiConfigType == .summarize {
- self?.aiSummarizeWithModel(newChatModel)
-
- } else if newChatModel.aiConfigType == .reWriting {
- self?.aiReWritingWithModel(newChatModel)
-
- } else if newChatModel.aiConfigType == .proofreading {
- self?.aiProofreadingWithModel(newChatModel)
-
- } else if newChatModel.aiConfigType == .translate {
- self?.startAiTranslateWithModel(newChatModel)
-
- }
- }
- }
-
- }
-
- func loadAIInputView() {
- self.aiInfoInputView = AIInfoInputView.createFromNib()
- self.aiInfoInputView.frame = NSMakeRect((NSWidth(self.contendBox.frame)-248)/2.0, 20, 248, 86)
- self.aiInfoInputView.autoresizingMask = [.minXMargin, .maxXMargin, .width, .maxYMargin]
- self.contendBox.addSubview(self.aiInfoInputView)
- self.aiInfoInputView.aiConfigType = .none
- self.aiInfoInputView.aiDelegate = self
- self.aiInfoInputView.reloadData()
- self.aiInfoInputView.startAIHandle = {[unowned self] view in
- if view.aiConfigType == .summarize {
- let chatModel = AIChatInfoModel.init()
- chatModel.aiConfigType = .summarize
- chatModel.infoType = .chatFileUpload
- chatModel.filePath = self.aiInfoInputView.filePath
- AIChatInfoManager.defaultManager.modelsArrM.append(chatModel)
-
- self.aiChatView.reloadData()
-
- self.aiSummarize()
- } else if view.aiConfigType == .reWriting {
- let chatModel = AIChatInfoModel.init()
- chatModel.aiConfigType = .reWriting
- chatModel.infoType = .chatStringUpload
- chatModel.uploadContent = self.aiInfoInputView.fileEmptyTextView.string
- AIChatInfoManager.defaultManager.modelsArrM.append(chatModel)
-
- self.aiChatView.reloadData()
-
- self.aiReWriting()
- } else if view.aiConfigType == .proofreading {
- let chatModel = AIChatInfoModel.init()
- chatModel.aiConfigType = .proofreading
- chatModel.infoType = .chatStringUpload
- chatModel.uploadContent = self.aiInfoInputView.fileEmptyTextView.string
- AIChatInfoManager.defaultManager.modelsArrM.append(chatModel)
-
- self.aiChatView.reloadData()
-
- self.aiProofreading()
- } else if view.aiConfigType == .translate {
- let chatModel = AIChatInfoModel.init()
- chatModel.aiConfigType = .translate
-
- if self.aiInfoInputView.filePath.isEmpty == false {
- //文件
- chatModel.filePath = self.aiInfoInputView.filePath
- chatModel.infoType = .chatFileUpload
- } else {
- //文字
- chatModel.uploadContent = self.aiInfoInputView.fileEmptyTextView.string
- chatModel.infoType = .chatStringUpload
- }
- chatModel.translateFromLanguage = self.aiInfoInputView.fromLanguage
- chatModel.translateToLanguage = self.aiInfoInputView.toLanguage
-
- AIChatInfoManager.defaultManager.modelsArrM.append(chatModel)
-
- self.aiChatView.reloadData()
-
- self.startAiTranslate()
- }
- self.refreshViewUI()
-
- self.aiTypeItemView.reloadData()
-
- }
-
- self.aiInfoInputView.inputFrameUpdateHandle = {[weak self] view, stringSize in
- if view.aiConfigType == .summarize {
- self?.inputStringHeight = max(stringSize.height, 40)
- } else if view.aiConfigType == .reWriting {
- self?.inputStringHeight = max(stringSize.height, 40)
- } else if view.aiConfigType == .proofreading {
- self?.inputStringHeight = max(stringSize.height, 40)
- } else if view.aiConfigType == .translate {
- if view.filePath.isEmpty {
- self?.inputStringHeight = max(stringSize.height, 40)
- } else {
- self?.inputStringHeight = 40
- }
- }
- self?.refreshViewUI()
- }
- self.aiInfoInputView.refreshUI()
-
- }
-
- func loadAITypeItemView() {
- self.aiTypeItemView = AITypeItemChooseView.createFromNib()
- self.aiTypeItemView.wantsLayer = true
- self.aiTypeItemView.layer?.backgroundColor = NSColor.clear.cgColor
- self.aiTypeItemView.frame = NSMakeRect((NSWidth(self.contendBox.frame)-246)/2, NSMaxY(self.aiInfoInputView.frame)+13, 246, 52)
- self.aiTypeItemView.autoresizingMask = [.minXMargin, .maxXMargin, .width, .maxYMargin]
- self.contendBox.addSubview(self.aiTypeItemView)
-
- self.aiTypeItemView.chooseTypeHandle = {[weak self] itemView, aiConfigtype in
- self?.chooseAIFunctionWithType(aiConfigtype)
- }
-
- self.aiTypeItemView.clearHandle = {[weak self] itemView in
- if AIChatInfoManager.defaultManager.modelsArrM.count < 1 {
- return
- }
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.informativeText = NSLocalizedString("All the AI content will be removed. Are you sure you want to clear the session?", comment: "")
- alert.messageText = NSLocalizedString("Clear session", comment: "")
- alert.addButton(withTitle: NSLocalizedString("Clear", comment: ""))
- alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
- let response = alert.runModal()
- if response.rawValue == 1000 {
- DispatchQueue.main.async {
- AIChatInfoManager.defaultManager.clearData()
- self?.aiChatView.reloadData()
- }
- }
- }
-
- }
-
- func chooseAIFunctionWithType(_ aiConfigType: AIConfigType) -> Void {
- if AIInfoManager.default().aiInfo.infoDict.keys.count == 0 {
- #if VERSION_DMG
- KMPurchaseCompareWindowController.dmgPurchaseInstance().showWindow(nil)
- #else
- AIPurchaseWindowController.currentWC().showWindow(nil)
- #endif
- return;
- }
-
- if self.eventLabel.isEmpty {
- self.eventLabel = "AITools_Start"
- }
-
- if aiConfigType == .summarize {
- FMTrackEventManager.defaultManager.trackOnceEvent(event: "AITools", withProperties: [self.eventLabel:"AISum"])
- } else if aiConfigType == .reWriting {
- FMTrackEventManager.defaultManager.trackOnceEvent(event: "AITools", withProperties: [self.eventLabel:"AIRewrite"])
- } else if aiConfigType == .proofreading {
- FMTrackEventManager.defaultManager.trackOnceEvent(event: "AITools", withProperties: [self.eventLabel:"AIProofread"])
- } else if aiConfigType == .translate {
- FMTrackEventManager.defaultManager.trackOnceEvent(event: "AITools", withProperties: [self.eventLabel:"AITranslate"])
- }
-
- self.aiInfoInputView.aiConfigType = aiConfigType
- self.aiInfoInputView.reloadData()
- self.aiInfoInputView.aiFunctionTypeChanged()
-
- if self.aiInfoInputView.fileEmptyTextView.string.isEmpty == false {
- self.aiInfoInputView.updateCountLabelInfo()
- self.aiInfoInputView.refreshStringSize()
- }
-
- self.refreshViewUI()
-
- }
-
- func setCurrentPDFSelection(_ string: String) -> Void {
- self.aiInfoInputView.fileEmptyTextView.string = string
- }
-
- func refreshViewUI() {
- self.aiHeaderView.frame = NSMakeRect(0, NSHeight(self.contendBox.frame)-58, NSWidth(self.contendBox.frame), 28)
- if self.aiInfoInputView.aiConfigType == .none {
- self.aiInfoInputView.frame = NSMakeRect(13, 17, 240, 86)
- } else if self.aiInfoInputView.aiConfigType == .summarize {
- self.aiInfoInputView.frame = NSMakeRect(13, 17, 240, min(96+self.inputStringHeight, 500))
- } else if self.aiInfoInputView.aiConfigType == .reWriting {
- self.aiInfoInputView.frame = NSMakeRect(13, 17, 240, min(72+self.inputStringHeight, 500))
- } else if self.aiInfoInputView.aiConfigType == .proofreading {
- self.aiInfoInputView.frame = NSMakeRect(13, 17, 240, min(72+self.inputStringHeight, 500))
- } else if self.aiInfoInputView.aiConfigType == .translate {
- self.aiInfoInputView.frame = NSMakeRect(13, 17, 240, min(125+self.inputStringHeight, 500))
- }
-
- self.aiTypeItemView.frame = NSMakeRect(13, NSMaxY(self.aiInfoInputView.frame)+8, 240, self.aiTypeItemView.viewHeight)
- self.aiChatView.frame = NSMakeRect(0, NSMaxY(self.aiTypeItemView.frame)+8, NSWidth(self.contendBox.frame), NSHeight(self.contendBox.frame) - NSMaxY(self.aiTypeItemView.frame) - 16 - 58)
-
- }
-
-
-
- //MARK: AI-Action
- func aiSummarize() -> Void {
- if FileManager.default.fileExists(atPath: self.aiInfoInputView.filePath) {
-
- let chatModel = AIChatInfoModel.init()
- chatModel.aiConfigType = .summarize
- chatModel.infoType = .chatStringResult
- chatModel.filePath = self.aiInfoInputView.filePath
- chatModel.chatInfoState = .stateLoading
-
- AIChatInfoManager.defaultManager.modelsArrM.append(chatModel)
-
- self.aiChatView.reloadData()
-
- self.aiSummarizeWithModel(chatModel)
- }
- }
-
- func aiSummarizeWithModel(_ chatModel: AIChatInfoModel) -> Void {
- if FileManager.default.fileExists(atPath: chatModel.filePath) {
- AIChatInfoManager.defaultManager.isAILoading = true
-
- //MARK: TestData
- // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
- // DispatchQueue.main.async {
- // AIChatInfoManager.defaultManager.isAILoading = false
- // chatModel.chatResult = "AI Summary 返回的结果\nAI Summary 返回的结果\nAI Summary 返回的结果\nAI Summary 返回的结果\nAI Summary 返回的结果\nAI Summary 返回的结果\nAI Summary 返回的结果\nAI Summary 返回的结果"
- // chatModel.chatInfoState = .stateSuccess
- // self.aiChatView.reloadData()
- // self.aiTypeItemView.reloadData()
- // self.aiInfoInputView.reloadData()
- //
- // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 4) {
- // DispatchQueue.main.async {
- // chatModel.chatInfoState = .stateFailed
- // chatModel.chatResult = "Unknown error"
- //
- // self.aiChatView.reloadData()
- // self.aiTypeItemView.reloadData()
- // self.aiInfoInputView.reloadData()
- // }
- // }
- // }
- // }
-
- //MARK: 正式数据
- KMAIRequestServerManager.defaultManager.aiAction(content: chatModel.filePath, state: .extractSummaryFile) { wrapper in
- DispatchQueue.main.async {
- AIChatInfoManager.defaultManager.isAILoading = false
-
- let resultStr = wrapper.content
- chatModel.chatResult = resultStr
- chatModel.chatInfoState = wrapper.success ? .stateSuccess : .stateFailed
-
- self.aiChatView.reloadData()
- self.aiTypeItemView.reloadData()
- self.aiInfoInputView.reloadData()
- }
- }
- } else {
- DispatchQueue.main.async {
- AIChatInfoManager.defaultManager.isAILoading = false
-
- let resultStr = NSLocalizedString("File Not Exist", comment: "")
- chatModel.chatResult = resultStr
- chatModel.chatInfoState = .stateFailed
-
- self.aiChatView.reloadData()
- self.aiTypeItemView.reloadData()
- self.aiInfoInputView.reloadData()
- }
- }
- }
-
- func aiReWriting() -> Void {
- //Loading(28+155)->Finish(实际大小)
-
- let chatModel = AIChatInfoModel.init()
- chatModel.aiConfigType = .reWriting
- chatModel.infoType = .chatStringResult
- chatModel.uploadContent = self.aiInfoInputView.fileEmptyTextView.string
- chatModel.chatInfoState = .stateLoading
- AIChatInfoManager.defaultManager.modelsArrM.append(chatModel)
-
- self.aiChatView.reloadData()
-
- self.aiReWritingWithModel(chatModel)
- }
-
- func aiReWritingWithModel(_ chatModel: AIChatInfoModel) -> Void {
- AIChatInfoManager.defaultManager.isAILoading = true
-
- //MARK: TestData
- // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
- // DispatchQueue.main.async {
- // AIChatInfoManager.defaultManager.isAILoading = false
- // chatModel.chatInfoState = .stateSuccess
- // chatModel.chatResult = "AI Rewrite返回的结果AI Rewrite返回的结果AI Rewrite返回的结果AI Rewrite返回的结果AI Rewrite返回的结果AI Rewrite返回的结果AI Rewrite返回的结果AI Rewrite返回的结果AI Rewrite返回的结果AI Rewrite返回的结果AI Rewrite返回的结果AI Rewrite返回的结果AI Rewrite返回的结果"
- // self.aiChatView.reloadData()
- // self.aiTypeItemView.reloadData()
- // self.aiInfoInputView.reloadData()
- // }
- // }
-
- //MARK: 正式数据
- KMAIRequestServerManager.defaultManager.aiAction(content: chatModel.uploadContent,
- state: .rewrite) { wrapper in
- DispatchQueue.main.async {
- AIChatInfoManager.defaultManager.isAILoading = false
-
- let resultStr = wrapper.content
- chatModel.chatResult = resultStr
- chatModel.chatInfoState = wrapper.success ? .stateSuccess : .stateFailed
- self.aiChatView.reloadData()
- self.aiTypeItemView.reloadData()
- self.aiInfoInputView.reloadData()
- }
- }
- }
-
- func aiProofreading() -> Void {
- let chatModel = AIChatInfoModel.init()
- chatModel.aiConfigType = .proofreading
- chatModel.infoType = .chatStringResult
- chatModel.uploadContent = self.aiInfoInputView.fileEmptyTextView.string
- chatModel.chatInfoState = .stateLoading
- AIChatInfoManager.defaultManager.modelsArrM.append(chatModel)
-
- self.aiChatView.reloadData()
-
- self.aiProofreadingWithModel(chatModel)
- }
-
- func aiProofreadingWithModel(_ chatModel: AIChatInfoModel) -> Void {
- AIChatInfoManager.defaultManager.isAILoading = true
-
- //MARK: TestData
- // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
- // DispatchQueue.main.async {
- // chatModel.chatInfoState = .stateSuccess
- // AIChatInfoManager.defaultManager.isAILoading = false
- // chatModel.chatResult = "AI Proofreading 返回的结果"
- // self.aiChatView.reloadData()
- // self.aiTypeItemView.reloadData()
- // self.aiInfoInputView.reloadData()
- //
- // }
- // }
-
- //MARK: 正式数据
- KMAIRequestServerManager.defaultManager.aiAction(content: chatModel.uploadContent,
- state: .correctTypos) { wrapper in
- DispatchQueue.main.async {
- AIChatInfoManager.defaultManager.isAILoading = false
- let resultStr = wrapper.content
- chatModel.chatResult = resultStr
- chatModel.chatInfoState = wrapper.success ? .stateSuccess : .stateFailed
- self.aiChatView.reloadData()
- self.aiTypeItemView.reloadData()
- self.aiInfoInputView.reloadData()
- }
- }
- }
-
- func startAiTranslate() -> Void {
-
- let chatModel = AIChatInfoModel.init()
- chatModel.aiConfigType = .translate
-
- if self.aiInfoInputView.filePath.isEmpty {
- //文字
- chatModel.infoType = .chatTranslateResult
- chatModel.uploadContent = self.aiInfoInputView.fileEmptyTextView.string
- chatModel.filePath = ""
- } else {
- //文件
- chatModel.infoType = .chatTranslateResult
- chatModel.filePath = self.aiInfoInputView.filePath
- chatModel.uploadContent = ""
- }
- chatModel.translateFromLanguage = self.aiInfoInputView.fromLanguage
- chatModel.translateToLanguage = self.aiInfoInputView.toLanguage
- chatModel.chatInfoState = .stateLoading
- AIChatInfoManager.defaultManager.modelsArrM.append(chatModel)
-
- self.aiChatView.reloadData()
-
- self.startAiTranslateWithModel(chatModel)
- }
-
- func startAiTranslateWithModel(_ chatModel: AIChatInfoModel) -> Void {
-
- //MARK: TestData
- // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
- // DispatchQueue.main.async {
- // chatModel.chatInfoState = .stateInfoConfirm
- // chatModel.chatResult = "当前翻译需要的内容:"
- // self.aiChatView.reloadData()
- //
- // }
- // }
-
- //MARK: Data
- if chatModel.filePath.isEmpty == true {
- //文字
- AIChatInfoManager.defaultManager.isAILoading = true
- KMAIRequestServerManager.defaultManager.aiAction(content: chatModel.uploadContent,
- state: .textTranslate,
- from: chatModel.translateFromLanguage,
- to: chatModel.translateToLanguage) { wrapper in
- DispatchQueue.main.async {
- AIChatInfoManager.defaultManager.isAILoading = false
-
- let resultStr = wrapper.content
- chatModel.chatResult = resultStr
- chatModel.chatInfoState = wrapper.success ? .stateSuccess : .stateFailed
-
- self.aiChatView.reloadData()
- self.aiTypeItemView.reloadData()
- self.aiInfoInputView.reloadData()
- }
- };
- } else {
- //文件
- AIChatInfoManager.defaultManager.isAILoading = true
- KMAIRequestServerManager.defaultManager.aiAction(content: chatModel.filePath,
- state: .fileTranslate,
- from: chatModel.translateFromLanguage,
- to: chatModel.translateToLanguage) { wrapper in
- DispatchQueue.main.async {
- AIChatInfoManager.defaultManager.isAILoading = false
-
- var success = wrapper.success
- let resultStr = wrapper.content
- if success == false && resultStr == "501" {
- success = true
- }
- if success {
- let infoDict: NSDictionary = wrapper.result
- var credit: Int = 0
- if infoDict["credit"] != nil {
- credit = (infoDict["credit"] ?? "0") as! Int
- }
-
- var charCount: Int = 0
- if infoDict["charCount"] != nil {
- charCount = (infoDict["charCount"] ?? "0") as! Int
- }
-
- chatModel.creditsValid = true
- if resultStr == "501" {
- chatModel.creditsValid = false
- }
- chatModel.costCredits = credit
- chatModel.totalChart = charCount
- chatModel.chatInfoState = .stateInfoConfirm
- chatModel.chatResult = resultStr
-
- self.aiChatView.reloadData()
- self.aiTypeItemView.reloadData()
- self.aiInfoInputView.reloadData()
-
- } else {
- chatModel.chatResult = resultStr
- chatModel.chatInfoState = .stateFailed
-
- self.aiChatView.reloadData()
- self.aiTypeItemView.reloadData()
- self.aiInfoInputView.reloadData()
- }
- }
- };
- }
-
- }
-
- func continueAiTranslate(_ chatModel: AIChatInfoModel) -> Void {
- //MARK: TestData
- // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
- // DispatchQueue.main.async {
- // chatModel.chatInfoState = .stateSuccess
- // chatModel.chatResult = "/Users/kdanmobile/Desktop/Quick Start Guide v1.3.0.pdf"
- // self.aiChatView.reloadData()
- // self.aiTypeItemView.reloadData()
- // self.aiInfoInputView.reloadData()
- //
- // }
- // }
-
- KMAIRequestServerManager.defaultManager.aiTranslationFileTranslateHandle(fileKey: chatModel.chatResult,
- from: chatModel.translateFromLanguage,
- to: chatModel.translateToLanguage) { wrapper in
- DispatchQueue.main.async {
- AIChatInfoManager.defaultManager.isAILoading = false
-
- let resultStr = wrapper.content
- chatModel.chatResult = resultStr
- chatModel.chatInfoState = wrapper.success ? .stateSuccess : .stateFailed
-
- self.aiChatView.reloadData()
- self.aiTypeItemView.reloadData()
- self.aiInfoInputView.reloadData()
- }
- }
- }
-
- //MARK: AIInfoInputViewDelegate
- func ai_InputViewDidChooseCurFile(aiInputView: AIInfoInputView) {
- guard let callBack = self.chooseCurFileHandle else {
- return
- }
- callBack(self)
-
- }
-
- //MARK: NSWindowDelegate
- func windowDidBecomeMain(_ notification: Notification) {
- self.aiInfoInputView.reloadData()
-
- }
-
- func windowWillClose(_ notification: Notification) {
-
- AINewConfigWindowController.currentWindowController = nil
- }
-
- @objc func themeChange() {
- DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
- self.refreshViewColor()
- }
- }
- }
|