AINewConfigWindowController.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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. self.aiInfoInputView.aiConfigType = aiConfigType
  235. self.aiInfoInputView.reloadData()
  236. self.aiInfoInputView.aiFunctionTypeChanged()
  237. if self.aiInfoInputView.fileEmptyTextView.string.isEmpty == false {
  238. self.aiInfoInputView.updateCountLabelInfo()
  239. self.aiInfoInputView.refreshStringSize()
  240. }
  241. self.refreshViewUI()
  242. }
  243. func setCurrentPDFSelection(_ string: String) -> Void {
  244. self.aiInfoInputView.fileEmptyTextView.string = string
  245. }
  246. func refreshViewUI() {
  247. self.aiHeaderView.frame = NSMakeRect(0, NSHeight(self.contendBox.frame)-58, NSWidth(self.contendBox.frame), 28)
  248. if self.aiInfoInputView.aiConfigType == .none {
  249. self.aiInfoInputView.frame = NSMakeRect(13, 17, 240, 86)
  250. } else if self.aiInfoInputView.aiConfigType == .summarize {
  251. self.aiInfoInputView.frame = NSMakeRect(13, 17, 240, min(96+self.inputStringHeight, 500))
  252. } else if self.aiInfoInputView.aiConfigType == .reWriting {
  253. self.aiInfoInputView.frame = NSMakeRect(13, 17, 240, min(72+self.inputStringHeight, 500))
  254. } else if self.aiInfoInputView.aiConfigType == .proofreading {
  255. self.aiInfoInputView.frame = NSMakeRect(13, 17, 240, min(72+self.inputStringHeight, 500))
  256. } else if self.aiInfoInputView.aiConfigType == .translate {
  257. self.aiInfoInputView.frame = NSMakeRect(13, 17, 240, min(125+self.inputStringHeight, 500))
  258. }
  259. self.aiTypeItemView.frame = NSMakeRect(13, NSMaxY(self.aiInfoInputView.frame)+8, 240, self.aiTypeItemView.viewHeight)
  260. self.aiChatView.frame = NSMakeRect(0, NSMaxY(self.aiTypeItemView.frame)+8, NSWidth(self.contendBox.frame), NSHeight(self.contendBox.frame) - NSMaxY(self.aiTypeItemView.frame) - 16 - 58)
  261. }
  262. //MARK: AI-Action
  263. func aiSummarize() -> Void {
  264. if FileManager.default.fileExists(atPath: self.aiInfoInputView.filePath) {
  265. let chatModel = AIChatInfoModel.init()
  266. chatModel.aiConfigType = .summarize
  267. chatModel.infoType = .chatStringResult
  268. chatModel.filePath = self.aiInfoInputView.filePath
  269. chatModel.chatInfoState = .stateLoading
  270. AIChatInfoManager.defaultManager.modelsArrM.append(chatModel)
  271. self.aiChatView.reloadData()
  272. self.aiSummarizeWithModel(chatModel)
  273. }
  274. }
  275. func aiSummarizeWithModel(_ chatModel: AIChatInfoModel) -> Void {
  276. if FileManager.default.fileExists(atPath: chatModel.filePath) {
  277. AIChatInfoManager.defaultManager.isAILoading = true
  278. //MARK: TestData
  279. // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
  280. // DispatchQueue.main.async {
  281. // AIChatInfoManager.defaultManager.isAILoading = false
  282. // chatModel.chatResult = "AI Summary 返回的结果\nAI Summary 返回的结果\nAI Summary 返回的结果\nAI Summary 返回的结果\nAI Summary 返回的结果\nAI Summary 返回的结果\nAI Summary 返回的结果\nAI Summary 返回的结果"
  283. // chatModel.chatInfoState = .stateSuccess
  284. // self.aiChatView.reloadData()
  285. // self.aiTypeItemView.reloadData()
  286. // self.aiInfoInputView.reloadData()
  287. //
  288. // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 4) {
  289. // DispatchQueue.main.async {
  290. // chatModel.chatInfoState = .stateFailed
  291. // chatModel.chatResult = "Unknown error"
  292. //
  293. // self.aiChatView.reloadData()
  294. // self.aiTypeItemView.reloadData()
  295. // self.aiInfoInputView.reloadData()
  296. // }
  297. // }
  298. // }
  299. // }
  300. //MARK: 正式数据
  301. KMAIRequestServerManager.defaultManager.aiAction(content: chatModel.filePath, state: .extractSummaryFile) { wrapper in
  302. DispatchQueue.main.async {
  303. AIChatInfoManager.defaultManager.isAILoading = false
  304. let resultStr = wrapper.content
  305. chatModel.chatResult = resultStr
  306. chatModel.chatInfoState = wrapper.success ? .stateSuccess : .stateFailed
  307. self.aiChatView.reloadData()
  308. self.aiTypeItemView.reloadData()
  309. self.aiInfoInputView.reloadData()
  310. }
  311. }
  312. } else {
  313. DispatchQueue.main.async {
  314. AIChatInfoManager.defaultManager.isAILoading = false
  315. let resultStr = NSLocalizedString("File Not Exist", comment: "")
  316. chatModel.chatResult = resultStr
  317. chatModel.chatInfoState = .stateFailed
  318. self.aiChatView.reloadData()
  319. self.aiTypeItemView.reloadData()
  320. self.aiInfoInputView.reloadData()
  321. }
  322. }
  323. }
  324. func aiReWriting() -> Void {
  325. //Loading(28+155)->Finish(实际大小)
  326. let chatModel = AIChatInfoModel.init()
  327. chatModel.aiConfigType = .reWriting
  328. chatModel.infoType = .chatStringResult
  329. chatModel.uploadContent = self.aiInfoInputView.fileEmptyTextView.string
  330. chatModel.chatInfoState = .stateLoading
  331. AIChatInfoManager.defaultManager.modelsArrM.append(chatModel)
  332. self.aiChatView.reloadData()
  333. self.aiReWritingWithModel(chatModel)
  334. }
  335. func aiReWritingWithModel(_ chatModel: AIChatInfoModel) -> Void {
  336. AIChatInfoManager.defaultManager.isAILoading = true
  337. //MARK: TestData
  338. // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
  339. // DispatchQueue.main.async {
  340. // AIChatInfoManager.defaultManager.isAILoading = false
  341. // chatModel.chatInfoState = .stateSuccess
  342. // 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返回的结果"
  343. // self.aiChatView.reloadData()
  344. // self.aiTypeItemView.reloadData()
  345. // self.aiInfoInputView.reloadData()
  346. // }
  347. // }
  348. //MARK: 正式数据
  349. KMAIRequestServerManager.defaultManager.aiAction(content: chatModel.uploadContent,
  350. state: .rewrite) { wrapper in
  351. DispatchQueue.main.async {
  352. AIChatInfoManager.defaultManager.isAILoading = false
  353. let resultStr = wrapper.content
  354. chatModel.chatResult = resultStr
  355. chatModel.chatInfoState = wrapper.success ? .stateSuccess : .stateFailed
  356. self.aiChatView.reloadData()
  357. self.aiTypeItemView.reloadData()
  358. self.aiInfoInputView.reloadData()
  359. }
  360. }
  361. }
  362. func aiProofreading() -> Void {
  363. let chatModel = AIChatInfoModel.init()
  364. chatModel.aiConfigType = .proofreading
  365. chatModel.infoType = .chatStringResult
  366. chatModel.uploadContent = self.aiInfoInputView.fileEmptyTextView.string
  367. chatModel.chatInfoState = .stateLoading
  368. AIChatInfoManager.defaultManager.modelsArrM.append(chatModel)
  369. self.aiChatView.reloadData()
  370. self.aiProofreadingWithModel(chatModel)
  371. }
  372. func aiProofreadingWithModel(_ chatModel: AIChatInfoModel) -> Void {
  373. AIChatInfoManager.defaultManager.isAILoading = true
  374. //MARK: TestData
  375. // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
  376. // DispatchQueue.main.async {
  377. // chatModel.chatInfoState = .stateSuccess
  378. // AIChatInfoManager.defaultManager.isAILoading = false
  379. // chatModel.chatResult = "AI Proofreading 返回的结果"
  380. // self.aiChatView.reloadData()
  381. // self.aiTypeItemView.reloadData()
  382. // self.aiInfoInputView.reloadData()
  383. //
  384. // }
  385. // }
  386. //MARK: 正式数据
  387. KMAIRequestServerManager.defaultManager.aiAction(content: chatModel.uploadContent,
  388. state: .correctTypos) { wrapper in
  389. DispatchQueue.main.async {
  390. AIChatInfoManager.defaultManager.isAILoading = false
  391. let resultStr = wrapper.content
  392. chatModel.chatResult = resultStr
  393. chatModel.chatInfoState = wrapper.success ? .stateSuccess : .stateFailed
  394. self.aiChatView.reloadData()
  395. self.aiTypeItemView.reloadData()
  396. self.aiInfoInputView.reloadData()
  397. }
  398. }
  399. }
  400. func startAiTranslate() -> Void {
  401. let chatModel = AIChatInfoModel.init()
  402. chatModel.aiConfigType = .translate
  403. if self.aiInfoInputView.filePath.isEmpty {
  404. //文字
  405. chatModel.infoType = .chatTranslateResult
  406. chatModel.uploadContent = self.aiInfoInputView.fileEmptyTextView.string
  407. chatModel.filePath = ""
  408. } else {
  409. //文件
  410. chatModel.infoType = .chatTranslateResult
  411. chatModel.filePath = self.aiInfoInputView.filePath
  412. chatModel.uploadContent = ""
  413. }
  414. chatModel.translateFromLanguage = self.aiInfoInputView.fromLanguage
  415. chatModel.translateToLanguage = self.aiInfoInputView.toLanguage
  416. chatModel.chatInfoState = .stateLoading
  417. AIChatInfoManager.defaultManager.modelsArrM.append(chatModel)
  418. self.aiChatView.reloadData()
  419. self.startAiTranslateWithModel(chatModel)
  420. }
  421. func startAiTranslateWithModel(_ chatModel: AIChatInfoModel) -> Void {
  422. //MARK: TestData
  423. // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
  424. // DispatchQueue.main.async {
  425. // chatModel.chatInfoState = .stateInfoConfirm
  426. // chatModel.chatResult = "当前翻译需要的内容:"
  427. // self.aiChatView.reloadData()
  428. //
  429. // }
  430. // }
  431. //MARK: Data
  432. if chatModel.filePath.isEmpty == true {
  433. //文字
  434. AIChatInfoManager.defaultManager.isAILoading = true
  435. KMAIRequestServerManager.defaultManager.aiAction(content: chatModel.uploadContent,
  436. state: .textTranslate,
  437. from: chatModel.translateFromLanguage,
  438. to: chatModel.translateToLanguage) { wrapper in
  439. DispatchQueue.main.async {
  440. AIChatInfoManager.defaultManager.isAILoading = false
  441. let resultStr = wrapper.content
  442. chatModel.chatResult = resultStr
  443. chatModel.chatInfoState = wrapper.success ? .stateSuccess : .stateFailed
  444. self.aiChatView.reloadData()
  445. self.aiTypeItemView.reloadData()
  446. self.aiInfoInputView.reloadData()
  447. }
  448. };
  449. } else {
  450. //文件
  451. AIChatInfoManager.defaultManager.isAILoading = true
  452. KMAIRequestServerManager.defaultManager.aiAction(content: chatModel.filePath,
  453. state: .fileTranslate,
  454. from: chatModel.translateFromLanguage,
  455. to: chatModel.translateToLanguage) { wrapper in
  456. DispatchQueue.main.async {
  457. AIChatInfoManager.defaultManager.isAILoading = false
  458. var success = wrapper.success
  459. let resultStr = wrapper.content
  460. if success == false && resultStr == "501" {
  461. success = true
  462. }
  463. if success {
  464. let infoDict: NSDictionary = wrapper.result
  465. var credit: Int = 0
  466. if infoDict["credit"] != nil {
  467. credit = (infoDict["credit"] ?? "0") as! Int
  468. }
  469. var charCount: Int = 0
  470. if infoDict["charCount"] != nil {
  471. charCount = (infoDict["charCount"] ?? "0") as! Int
  472. }
  473. chatModel.creditsValid = true
  474. if resultStr == "501" {
  475. chatModel.creditsValid = false
  476. }
  477. chatModel.costCredits = credit
  478. chatModel.totalChart = charCount
  479. chatModel.chatInfoState = .stateInfoConfirm
  480. chatModel.chatResult = resultStr
  481. self.aiChatView.reloadData()
  482. self.aiTypeItemView.reloadData()
  483. self.aiInfoInputView.reloadData()
  484. } else {
  485. chatModel.chatResult = resultStr
  486. chatModel.chatInfoState = .stateFailed
  487. self.aiChatView.reloadData()
  488. self.aiTypeItemView.reloadData()
  489. self.aiInfoInputView.reloadData()
  490. }
  491. }
  492. };
  493. }
  494. }
  495. func continueAiTranslate(_ chatModel: AIChatInfoModel) -> Void {
  496. //MARK: TestData
  497. // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3) {
  498. // DispatchQueue.main.async {
  499. // chatModel.chatInfoState = .stateSuccess
  500. // chatModel.chatResult = "/Users/kdanmobile/Desktop/Quick Start Guide v1.3.0.pdf"
  501. // self.aiChatView.reloadData()
  502. // self.aiTypeItemView.reloadData()
  503. // self.aiInfoInputView.reloadData()
  504. //
  505. // }
  506. // }
  507. KMAIRequestServerManager.defaultManager.aiTranslationFileTranslateHandle(fileKey: chatModel.chatResult,
  508. from: chatModel.translateFromLanguage,
  509. to: chatModel.translateToLanguage) { wrapper in
  510. DispatchQueue.main.async {
  511. AIChatInfoManager.defaultManager.isAILoading = false
  512. let resultStr = wrapper.content
  513. chatModel.chatResult = resultStr
  514. chatModel.chatInfoState = wrapper.success ? .stateSuccess : .stateFailed
  515. self.aiChatView.reloadData()
  516. self.aiTypeItemView.reloadData()
  517. self.aiInfoInputView.reloadData()
  518. }
  519. }
  520. }
  521. //MARK: AIInfoInputViewDelegate
  522. func ai_InputViewDidChooseCurFile(aiInputView: AIInfoInputView) {
  523. guard let callBack = self.chooseCurFileHandle else {
  524. return
  525. }
  526. callBack(self)
  527. }
  528. //MARK: NSWindowDelegate
  529. func windowDidBecomeMain(_ notification: Notification) {
  530. self.aiInfoInputView.reloadData()
  531. }
  532. func windowWillClose(_ notification: Notification) {
  533. AINewConfigWindowController.currentWindowController = nil
  534. }
  535. @objc func themeChange() {
  536. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  537. self.refreshViewColor()
  538. }
  539. }
  540. }