AINewConfigWindowController.swift 29 KB

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