AINewConfigWindowController.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. //
  2. // AINewConfigWindowController.swift
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by Niehaoyu on 2024/4/16.
  6. //
  7. import Cocoa
  8. protocol AIConfigWindowDelegate: AnyObject {
  9. func ai_InputViewDidChooseCurFile(aiConfigWindow: AINewConfigWindowController)
  10. }
  11. @objcMembers class AINewConfigWindowController: NSWindowController, NSWindowDelegate, AIInfoInputViewDelegate {
  12. static var currentWindowController: AINewConfigWindowController!
  13. @IBOutlet weak var contendBox: NSBox!
  14. var aiHeaderView: AIHeaderView!
  15. var aiChatView: AIChatView!
  16. var aiTypeItemView: AITypeItemChooseView!
  17. var aiInfoInputView: AIInfoInputView!
  18. var inputStringHeight: CGFloat = 40
  19. var didSetOriginFrame: Bool = false
  20. var eventLabel: String = "AITools_Start"
  21. weak var aiDelegate: AIConfigWindowDelegate?
  22. var chooseCurFileHandle: ((_ windowVC: AINewConfigWindowController) -> Void)?
  23. deinit {
  24. DistributedNotificationCenter.default.removeObserver(self)
  25. }
  26. @objc static func currentWC() -> AINewConfigWindowController {
  27. if currentWindowController != nil {
  28. return currentWindowController
  29. } else {
  30. let configWC: AINewConfigWindowController = AINewConfigWindowController.init(windowNibName: "AINewConfigWindowController")
  31. currentWindowController = configWC;
  32. return currentWindowController
  33. }
  34. }
  35. override func showWindow(_ sender: Any?) {
  36. super.showWindow(sender)
  37. self.window?.delegate = self
  38. self.aiInfoInputView.setUpTranslateUI()
  39. self.aiChatView.reloadData()
  40. self.aiTypeItemView.refreshUI()
  41. self.refreshViewUI()
  42. }
  43. override func windowDidLoad() {
  44. super.windowDidLoad()
  45. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  46. self.window?.title = NSLocalizedString("AI Tools", comment: "")
  47. self.loadAIHeaderView()
  48. self.loadAIChatView()
  49. self.loadAIInputView()
  50. self.loadAITypeItemView()
  51. self.refreshViewUI()
  52. self.refreshViewColor()
  53. DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChange), name: NSNotification.Name(rawValue: "AppleInterfaceThemeChangedNotification"), object: nil)
  54. }
  55. func refreshViewColor() {
  56. if KMAppearance.isDarkMode() {
  57. self.contendBox.fillColor = NSColor(red: 33/255, green: 33/255, blue: 33/255, alpha: 1)
  58. } else {
  59. self.contendBox.fillColor = NSColor.white
  60. }
  61. self.aiHeaderView.refreshViewColor()
  62. self.aiInfoInputView.refreshUI()
  63. self.aiChatView.reloadData()
  64. self.aiTypeItemView.refreshViewColor()
  65. }
  66. func loadAIHeaderView() {
  67. self.aiHeaderView = AIHeaderView.createFromNib()
  68. self.aiHeaderView.frame = NSMakeRect(0, NSHeight(self.contendBox.frame)-58, NSWidth(self.contendBox.frame), 28)
  69. self.aiHeaderView.autoresizingMask = [.minXMargin, .maxXMargin, .width, .minYMargin]
  70. self.contendBox.addSubview(self.aiHeaderView)
  71. }
  72. func loadAIChatView() {
  73. self.aiChatView = AIChatView.createFromNib()
  74. self.aiChatView.wantsLayer = true
  75. self.aiChatView.layer?.backgroundColor = NSColor.clear.cgColor
  76. self.aiChatView.frame = NSMakeRect(0, 300, NSWidth(self.contendBox.frame), NSHeight(self.contendBox.frame)-400)
  77. self.aiChatView.autoresizingMask = [.width, .height]
  78. self.contendBox.addSubview(self.aiChatView)
  79. self.aiChatView.chooseConfigHandle = {[weak self] view, configType in
  80. DispatchQueue.main.async {
  81. self?.chooseAIFunctionWithType(configType)
  82. }
  83. }
  84. self.aiChatView.cancelAIHandle = {[weak self] view, chatInfoModel in
  85. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.05) {
  86. chatInfoModel.chatInfoState = .stateCancel
  87. self?.aiChatView.reloadData()
  88. }
  89. }
  90. self.aiChatView.continueAITranslateHandle = {[weak self] view, chatInfoModel in
  91. DispatchQueue.main.async {
  92. chatInfoModel.chatInfoState = .stateLoading
  93. self?.aiChatView.reloadData()
  94. self?.continueAiTranslate(chatInfoModel)
  95. }
  96. }
  97. self.aiChatView.redoHandle = {[weak self] view, chatInfoModel in
  98. DispatchQueue.main.async {
  99. let newChatModel = AIChatInfoModel.init()
  100. newChatModel.aiConfigType = chatInfoModel.aiConfigType
  101. newChatModel.infoType = chatInfoModel.infoType
  102. newChatModel.filePath = chatInfoModel.filePath
  103. newChatModel.chatInfoState = .stateLoading
  104. newChatModel.uploadContent = chatInfoModel.uploadContent
  105. newChatModel.translateFromLanguage = chatInfoModel.translateFromLanguage
  106. newChatModel.translateToLanguage = chatInfoModel.translateToLanguage
  107. AIChatInfoManager.defaultManager.modelsArrM.append(newChatModel)
  108. self?.aiChatView.reloadData()
  109. if newChatModel.aiConfigType == .summarize {
  110. self?.aiSummarizeWithModel(newChatModel)
  111. } else if newChatModel.aiConfigType == .reWriting {
  112. self?.aiReWritingWithModel(newChatModel)
  113. } else if newChatModel.aiConfigType == .proofreading {
  114. self?.aiProofreadingWithModel(newChatModel)
  115. } else if newChatModel.aiConfigType == .translate {
  116. self?.startAiTranslateWithModel(newChatModel)
  117. }
  118. }
  119. }
  120. }
  121. func loadAIInputView() {
  122. self.aiInfoInputView = AIInfoInputView.createFromNib()
  123. self.aiInfoInputView.frame = NSMakeRect((NSWidth(self.contendBox.frame)-248)/2.0, 20, 248, 86)
  124. self.aiInfoInputView.autoresizingMask = [.minXMargin, .maxXMargin, .width, .maxYMargin]
  125. self.contendBox.addSubview(self.aiInfoInputView)
  126. self.aiInfoInputView.aiConfigType = .none
  127. self.aiInfoInputView.aiDelegate = self
  128. self.aiInfoInputView.reloadData()
  129. self.aiInfoInputView.startAIHandle = {[unowned self] view in
  130. if view.aiConfigType == .summarize {
  131. let chatModel = AIChatInfoModel.init()
  132. chatModel.aiConfigType = .summarize
  133. chatModel.infoType = .chatFileUpload
  134. chatModel.filePath = self.aiInfoInputView.filePath
  135. AIChatInfoManager.defaultManager.modelsArrM.append(chatModel)
  136. self.aiChatView.reloadData()
  137. self.aiSummarize()
  138. } else if view.aiConfigType == .reWriting {
  139. let chatModel = AIChatInfoModel.init()
  140. chatModel.aiConfigType = .reWriting
  141. chatModel.infoType = .chatStringUpload
  142. chatModel.uploadContent = self.aiInfoInputView.fileEmptyTextView.string
  143. AIChatInfoManager.defaultManager.modelsArrM.append(chatModel)
  144. self.aiChatView.reloadData()
  145. self.aiReWriting()
  146. } else if view.aiConfigType == .proofreading {
  147. let chatModel = AIChatInfoModel.init()
  148. chatModel.aiConfigType = .proofreading
  149. chatModel.infoType = .chatStringUpload
  150. chatModel.uploadContent = self.aiInfoInputView.fileEmptyTextView.string
  151. AIChatInfoManager.defaultManager.modelsArrM.append(chatModel)
  152. self.aiChatView.reloadData()
  153. self.aiProofreading()
  154. } else if view.aiConfigType == .translate {
  155. let chatModel = AIChatInfoModel.init()
  156. chatModel.aiConfigType = .translate
  157. if self.aiInfoInputView.filePath.isEmpty == false {
  158. //文件
  159. chatModel.filePath = self.aiInfoInputView.filePath
  160. chatModel.infoType = .chatFileUpload
  161. } else {
  162. //文字
  163. chatModel.uploadContent = self.aiInfoInputView.fileEmptyTextView.string
  164. chatModel.infoType = .chatStringUpload
  165. }
  166. chatModel.translateFromLanguage = self.aiInfoInputView.fromLanguage
  167. chatModel.translateToLanguage = self.aiInfoInputView.toLanguage
  168. AIChatInfoManager.defaultManager.modelsArrM.append(chatModel)
  169. self.aiChatView.reloadData()
  170. self.startAiTranslate()
  171. }
  172. self.refreshViewUI()
  173. self.aiTypeItemView.reloadData()
  174. }
  175. self.aiInfoInputView.inputFrameUpdateHandle = {[weak self] view, stringSize in
  176. if view.aiConfigType == .summarize {
  177. self?.inputStringHeight = max(stringSize.height, 40)
  178. } else if view.aiConfigType == .reWriting {
  179. self?.inputStringHeight = max(stringSize.height, 40)
  180. } else if view.aiConfigType == .proofreading {
  181. self?.inputStringHeight = max(stringSize.height, 40)
  182. } else if view.aiConfigType == .translate {
  183. if view.filePath.isEmpty {
  184. self?.inputStringHeight = max(stringSize.height, 40)
  185. } else {
  186. self?.inputStringHeight = 40
  187. }
  188. }
  189. self?.refreshViewUI()
  190. }
  191. self.aiInfoInputView.refreshUI()
  192. }
  193. func loadAITypeItemView() {
  194. self.aiTypeItemView = AITypeItemChooseView.createFromNib()
  195. self.aiTypeItemView.wantsLayer = true
  196. self.aiTypeItemView.layer?.backgroundColor = NSColor.clear.cgColor
  197. self.aiTypeItemView.frame = NSMakeRect((NSWidth(self.contendBox.frame)-246)/2, NSMaxY(self.aiInfoInputView.frame)+13, 246, 52)
  198. self.aiTypeItemView.autoresizingMask = [.minXMargin, .maxXMargin, .width, .maxYMargin]
  199. self.contendBox.addSubview(self.aiTypeItemView)
  200. self.aiTypeItemView.chooseTypeHandle = {[weak self] itemView, aiConfigtype in
  201. self?.chooseAIFunctionWithType(aiConfigtype)
  202. }
  203. self.aiTypeItemView.clearHandle = {[weak self] itemView in
  204. if AIChatInfoManager.defaultManager.modelsArrM.count < 1 {
  205. return
  206. }
  207. let alert = NSAlert()
  208. alert.alertStyle = .critical
  209. alert.informativeText = NSLocalizedString("All the AI content will be removed. Are you sure you want to clear the session?", comment: "")
  210. alert.messageText = NSLocalizedString("Clear session", comment: "")
  211. alert.addButton(withTitle: NSLocalizedString("Clear", comment: ""))
  212. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  213. let response = alert.runModal()
  214. if response.rawValue == 1000 {
  215. DispatchQueue.main.async {
  216. AIChatInfoManager.defaultManager.clearData()
  217. self?.aiChatView.reloadData()
  218. }
  219. }
  220. }
  221. }
  222. func chooseAIFunctionWithType(_ aiConfigType: AIConfigType) -> Void {
  223. if AIInfoManager.default().aiInfo.infoDict.keys.count == 0 {
  224. #if VERSION_DMG
  225. KMPurchaseCompareWindowController.dmgPurchaseInstance().showWindow(nil)
  226. #else
  227. AIPurchaseWindowController.currentWC().showWindow(nil)
  228. #endif
  229. return;
  230. }
  231. if self.eventLabel.isEmpty {
  232. self.eventLabel = "AITools_Start"
  233. }
  234. if aiConfigType == .summarize {
  235. FMTrackEventManager.defaultManager.trackOnceEvent(event: "AITools", withProperties: [self.eventLabel:"AISum"])
  236. } else if aiConfigType == .reWriting {
  237. FMTrackEventManager.defaultManager.trackOnceEvent(event: "AITools", withProperties: [self.eventLabel:"AIRewrite"])
  238. } else if aiConfigType == .proofreading {
  239. FMTrackEventManager.defaultManager.trackOnceEvent(event: "AITools", withProperties: [self.eventLabel:"AIProofread"])
  240. } else if aiConfigType == .translate {
  241. FMTrackEventManager.defaultManager.trackOnceEvent(event: "AITools", withProperties: [self.eventLabel:"AITranslate"])
  242. }
  243. self.aiInfoInputView.aiConfigType = aiConfigType
  244. self.aiInfoInputView.reloadData()
  245. self.aiInfoInputView.aiFunctionTypeChanged()
  246. if self.aiInfoInputView.fileEmptyTextView.string.isEmpty == false {
  247. self.aiInfoInputView.updateCountLabelInfo()
  248. self.aiInfoInputView.refreshStringSize()
  249. }
  250. self.refreshViewUI()
  251. }
  252. func setCurrentPDFSelection(_ string: String) -> Void {
  253. self.aiInfoInputView.fileEmptyTextView.string = string
  254. }
  255. func refreshViewUI() {
  256. self.aiHeaderView.frame = NSMakeRect(0, NSHeight(self.contendBox.frame)-58, NSWidth(self.contendBox.frame), 28)
  257. if self.aiInfoInputView.aiConfigType == .none {
  258. self.aiInfoInputView.frame = NSMakeRect(13, 17, 240, 86)
  259. } else if self.aiInfoInputView.aiConfigType == .summarize {
  260. self.aiInfoInputView.frame = NSMakeRect(13, 17, 240, min(96+self.inputStringHeight, 500))
  261. } else if self.aiInfoInputView.aiConfigType == .reWriting {
  262. self.aiInfoInputView.frame = NSMakeRect(13, 17, 240, min(72+self.inputStringHeight, 500))
  263. } else if self.aiInfoInputView.aiConfigType == .proofreading {
  264. self.aiInfoInputView.frame = NSMakeRect(13, 17, 240, min(72+self.inputStringHeight, 500))
  265. } else if self.aiInfoInputView.aiConfigType == .translate {
  266. self.aiInfoInputView.frame = NSMakeRect(13, 17, 240, min(125+self.inputStringHeight, 500))
  267. }
  268. self.aiTypeItemView.frame = NSMakeRect(13, NSMaxY(self.aiInfoInputView.frame)+8, 240, self.aiTypeItemView.viewHeight)
  269. self.aiChatView.frame = NSMakeRect(0, NSMaxY(self.aiTypeItemView.frame)+8, NSWidth(self.contendBox.frame), NSHeight(self.contendBox.frame) - NSMaxY(self.aiTypeItemView.frame) - 16 - 58)
  270. }
  271. //MARK: AI-Action
  272. func aiSummarize() -> Void {
  273. if FileManager.default.fileExists(atPath: self.aiInfoInputView.filePath) {
  274. let chatModel = AIChatInfoModel.init()
  275. chatModel.aiConfigType = .summarize
  276. chatModel.infoType = .chatStringResult
  277. chatModel.filePath = self.aiInfoInputView.filePath
  278. chatModel.chatInfoState = .stateLoading
  279. AIChatInfoManager.defaultManager.modelsArrM.append(chatModel)
  280. self.aiChatView.reloadData()
  281. self.aiSummarizeWithModel(chatModel)
  282. }
  283. }
  284. func aiSummarizeWithModel(_ chatModel: AIChatInfoModel) -> Void {
  285. if FileManager.default.fileExists(atPath: chatModel.filePath) {
  286. AIChatInfoManager.defaultManager.isAILoading = true
  287. //MARK: TestData
  288. // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
  289. // DispatchQueue.main.async {
  290. // AIChatInfoManager.defaultManager.isAILoading = false
  291. // chatModel.chatResult = "AI Summary 返回的结果\nAI Summary 返回的结果\nAI Summary 返回的结果\nAI Summary 返回的结果\nAI Summary 返回的结果\nAI Summary 返回的结果\nAI Summary 返回的结果\nAI Summary 返回的结果"
  292. // chatModel.chatInfoState = .stateSuccess
  293. // self.aiChatView.reloadData()
  294. // self.aiTypeItemView.reloadData()
  295. // self.aiInfoInputView.reloadData()
  296. //
  297. // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 4) {
  298. // DispatchQueue.main.async {
  299. // chatModel.chatInfoState = .stateFailed
  300. // chatModel.chatResult = "Unknown error"
  301. //
  302. // self.aiChatView.reloadData()
  303. // self.aiTypeItemView.reloadData()
  304. // self.aiInfoInputView.reloadData()
  305. // }
  306. // }
  307. // }
  308. // }
  309. //MARK: 正式数据
  310. KMAIRequestServerManager.defaultManager.aiAction(content: chatModel.filePath, state: .extractSummaryFile) { wrapper in
  311. DispatchQueue.main.async {
  312. AIChatInfoManager.defaultManager.isAILoading = false
  313. let resultStr = wrapper.content
  314. chatModel.chatResult = resultStr
  315. chatModel.chatInfoState = wrapper.success ? .stateSuccess : .stateFailed
  316. self.aiChatView.reloadData()
  317. self.aiTypeItemView.reloadData()
  318. self.aiInfoInputView.reloadData()
  319. }
  320. }
  321. } else {
  322. DispatchQueue.main.async {
  323. AIChatInfoManager.defaultManager.isAILoading = false
  324. let resultStr = NSLocalizedString("File Not Exist", comment: "")
  325. chatModel.chatResult = resultStr
  326. chatModel.chatInfoState = .stateFailed
  327. self.aiChatView.reloadData()
  328. self.aiTypeItemView.reloadData()
  329. self.aiInfoInputView.reloadData()
  330. }
  331. }
  332. }
  333. func aiReWriting() -> Void {
  334. //Loading(28+155)->Finish(实际大小)
  335. let chatModel = AIChatInfoModel.init()
  336. chatModel.aiConfigType = .reWriting
  337. chatModel.infoType = .chatStringResult
  338. chatModel.uploadContent = self.aiInfoInputView.fileEmptyTextView.string
  339. chatModel.chatInfoState = .stateLoading
  340. AIChatInfoManager.defaultManager.modelsArrM.append(chatModel)
  341. self.aiChatView.reloadData()
  342. self.aiReWritingWithModel(chatModel)
  343. }
  344. func aiReWritingWithModel(_ chatModel: AIChatInfoModel) -> Void {
  345. AIChatInfoManager.defaultManager.isAILoading = true
  346. //MARK: TestData
  347. // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
  348. // DispatchQueue.main.async {
  349. // AIChatInfoManager.defaultManager.isAILoading = false
  350. // chatModel.chatInfoState = .stateSuccess
  351. // 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返回的结果"
  352. // self.aiChatView.reloadData()
  353. // self.aiTypeItemView.reloadData()
  354. // self.aiInfoInputView.reloadData()
  355. // }
  356. // }
  357. //MARK: 正式数据
  358. KMAIRequestServerManager.defaultManager.aiAction(content: chatModel.uploadContent,
  359. state: .rewrite) { wrapper in
  360. DispatchQueue.main.async {
  361. AIChatInfoManager.defaultManager.isAILoading = false
  362. let resultStr = wrapper.content
  363. chatModel.chatResult = resultStr
  364. chatModel.chatInfoState = wrapper.success ? .stateSuccess : .stateFailed
  365. self.aiChatView.reloadData()
  366. self.aiTypeItemView.reloadData()
  367. self.aiInfoInputView.reloadData()
  368. }
  369. }
  370. }
  371. func aiProofreading() -> Void {
  372. let chatModel = AIChatInfoModel.init()
  373. chatModel.aiConfigType = .proofreading
  374. chatModel.infoType = .chatStringResult
  375. chatModel.uploadContent = self.aiInfoInputView.fileEmptyTextView.string
  376. chatModel.chatInfoState = .stateLoading
  377. AIChatInfoManager.defaultManager.modelsArrM.append(chatModel)
  378. self.aiChatView.reloadData()
  379. self.aiProofreadingWithModel(chatModel)
  380. }
  381. func aiProofreadingWithModel(_ chatModel: AIChatInfoModel) -> Void {
  382. AIChatInfoManager.defaultManager.isAILoading = true
  383. //MARK: TestData
  384. // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
  385. // DispatchQueue.main.async {
  386. // chatModel.chatInfoState = .stateSuccess
  387. // AIChatInfoManager.defaultManager.isAILoading = false
  388. // chatModel.chatResult = "AI Proofreading 返回的结果"
  389. // self.aiChatView.reloadData()
  390. // self.aiTypeItemView.reloadData()
  391. // self.aiInfoInputView.reloadData()
  392. //
  393. // }
  394. // }
  395. //MARK: 正式数据
  396. KMAIRequestServerManager.defaultManager.aiAction(content: chatModel.uploadContent,
  397. state: .correctTypos) { wrapper in
  398. DispatchQueue.main.async {
  399. AIChatInfoManager.defaultManager.isAILoading = false
  400. let resultStr = wrapper.content
  401. chatModel.chatResult = resultStr
  402. chatModel.chatInfoState = wrapper.success ? .stateSuccess : .stateFailed
  403. self.aiChatView.reloadData()
  404. self.aiTypeItemView.reloadData()
  405. self.aiInfoInputView.reloadData()
  406. }
  407. }
  408. }
  409. func startAiTranslate() -> Void {
  410. let chatModel = AIChatInfoModel.init()
  411. chatModel.aiConfigType = .translate
  412. if self.aiInfoInputView.filePath.isEmpty {
  413. //文字
  414. chatModel.infoType = .chatTranslateResult
  415. chatModel.uploadContent = self.aiInfoInputView.fileEmptyTextView.string
  416. chatModel.filePath = ""
  417. } else {
  418. //文件
  419. chatModel.infoType = .chatTranslateResult
  420. chatModel.filePath = self.aiInfoInputView.filePath
  421. chatModel.uploadContent = ""
  422. }
  423. chatModel.translateFromLanguage = self.aiInfoInputView.fromLanguage
  424. chatModel.translateToLanguage = self.aiInfoInputView.toLanguage
  425. chatModel.chatInfoState = .stateLoading
  426. AIChatInfoManager.defaultManager.modelsArrM.append(chatModel)
  427. self.aiChatView.reloadData()
  428. self.startAiTranslateWithModel(chatModel)
  429. }
  430. func startAiTranslateWithModel(_ chatModel: AIChatInfoModel) -> Void {
  431. //MARK: TestData
  432. // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
  433. // DispatchQueue.main.async {
  434. // chatModel.chatInfoState = .stateInfoConfirm
  435. // chatModel.chatResult = "当前翻译需要的内容:"
  436. // self.aiChatView.reloadData()
  437. //
  438. // }
  439. // }
  440. //MARK: Data
  441. if chatModel.filePath.isEmpty == true {
  442. //文字
  443. AIChatInfoManager.defaultManager.isAILoading = true
  444. KMAIRequestServerManager.defaultManager.aiAction(content: chatModel.uploadContent,
  445. state: .textTranslate,
  446. from: chatModel.translateFromLanguage,
  447. to: chatModel.translateToLanguage) { wrapper in
  448. DispatchQueue.main.async {
  449. AIChatInfoManager.defaultManager.isAILoading = false
  450. let resultStr = wrapper.content
  451. chatModel.chatResult = resultStr
  452. chatModel.chatInfoState = wrapper.success ? .stateSuccess : .stateFailed
  453. self.aiChatView.reloadData()
  454. self.aiTypeItemView.reloadData()
  455. self.aiInfoInputView.reloadData()
  456. }
  457. };
  458. } else {
  459. //文件
  460. AIChatInfoManager.defaultManager.isAILoading = true
  461. KMAIRequestServerManager.defaultManager.aiAction(content: chatModel.filePath,
  462. state: .fileTranslate,
  463. from: chatModel.translateFromLanguage,
  464. to: chatModel.translateToLanguage) { wrapper in
  465. DispatchQueue.main.async {
  466. AIChatInfoManager.defaultManager.isAILoading = false
  467. var success = wrapper.success
  468. let resultStr = wrapper.content
  469. if success == false && resultStr == "501" {
  470. success = true
  471. }
  472. if success {
  473. let infoDict: NSDictionary = wrapper.result
  474. var credit: Int = 0
  475. if infoDict["credit"] != nil {
  476. credit = (infoDict["credit"] ?? "0") as! Int
  477. }
  478. var charCount: Int = 0
  479. if infoDict["charCount"] != nil {
  480. charCount = (infoDict["charCount"] ?? "0") as! Int
  481. }
  482. chatModel.creditsValid = true
  483. if resultStr == "501" {
  484. chatModel.creditsValid = false
  485. }
  486. chatModel.costCredits = credit
  487. chatModel.totalChart = charCount
  488. chatModel.chatInfoState = .stateInfoConfirm
  489. chatModel.chatResult = resultStr
  490. self.aiChatView.reloadData()
  491. self.aiTypeItemView.reloadData()
  492. self.aiInfoInputView.reloadData()
  493. } else {
  494. chatModel.chatResult = resultStr
  495. chatModel.chatInfoState = .stateFailed
  496. self.aiChatView.reloadData()
  497. self.aiTypeItemView.reloadData()
  498. self.aiInfoInputView.reloadData()
  499. }
  500. }
  501. };
  502. }
  503. }
  504. func continueAiTranslate(_ chatModel: AIChatInfoModel) -> Void {
  505. //MARK: TestData
  506. // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
  507. // DispatchQueue.main.async {
  508. // chatModel.chatInfoState = .stateSuccess
  509. // chatModel.chatResult = "/Users/kdanmobile/Desktop/Quick Start Guide v1.3.0.pdf"
  510. // self.aiChatView.reloadData()
  511. // self.aiTypeItemView.reloadData()
  512. // self.aiInfoInputView.reloadData()
  513. //
  514. // }
  515. // }
  516. KMAIRequestServerManager.defaultManager.aiTranslationFileTranslateHandle(fileKey: chatModel.chatResult,
  517. from: chatModel.translateFromLanguage,
  518. to: chatModel.translateToLanguage) { wrapper in
  519. DispatchQueue.main.async {
  520. AIChatInfoManager.defaultManager.isAILoading = false
  521. let resultStr = wrapper.content
  522. chatModel.chatResult = resultStr
  523. chatModel.chatInfoState = wrapper.success ? .stateSuccess : .stateFailed
  524. self.aiChatView.reloadData()
  525. self.aiTypeItemView.reloadData()
  526. self.aiInfoInputView.reloadData()
  527. }
  528. }
  529. }
  530. //MARK: AIInfoInputViewDelegate
  531. func ai_InputViewDidChooseCurFile(aiInputView: AIInfoInputView) {
  532. guard let callBack = self.chooseCurFileHandle else {
  533. return
  534. }
  535. callBack(self)
  536. }
  537. //MARK: NSWindowDelegate
  538. func windowDidBecomeMain(_ notification: Notification) {
  539. self.aiInfoInputView.reloadData()
  540. }
  541. func windowWillClose(_ notification: Notification) {
  542. AINewConfigWindowController.currentWindowController = nil
  543. }
  544. @objc func themeChange() {
  545. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  546. self.refreshViewColor()
  547. }
  548. }
  549. }