Commit 90587ce2 authored by Steven杜宇's avatar Steven杜宇

// AI

parent 0a544f2e
...@@ -18,11 +18,7 @@ class YHAIMainChatViewController: YHBaseViewController { ...@@ -18,11 +18,7 @@ class YHAIMainChatViewController: YHBaseViewController {
var isNeedAutoResponseImage = false var isNeedAutoResponseImage = false
var isHaveAutoResponseText = false var isHaveAutoResponseText = false
var messages:[YHAIChatMessage] = [] var messages:[YHAIChatMessage] = []
var disableScrollToBottom: Bool = false
var isUserScrolling: Bool = false
var lastUserScrollTime: CFTimeInterval = 0.0
let minimumScrollInterval: CFTimeInterval = 2.0
let manager = YHAIRequestManager() let manager = YHAIRequestManager()
let viewModel = YHAIViewModel() let viewModel = YHAIViewModel()
...@@ -111,11 +107,11 @@ class YHAIMainChatViewController: YHBaseViewController { ...@@ -111,11 +107,11 @@ class YHAIMainChatViewController: YHBaseViewController {
self.bottomInputView.showKeyBoard(false) self.bottomInputView.showKeyBoard(false)
} }
func reloadAndScrollToBottom(_ forceScrollToBottom: Bool = false, _ isNeedAccurate: Bool = false) { func reloadAndScrollToBottom() {
self.tableView.reloadData() self.tableView.reloadData()
if !forceScrollToBottom && !canTriggerProgrammaticScroll() { if canTriggerProgrammaticScroll() {
return return
} }
...@@ -148,10 +144,11 @@ class YHAIMainChatViewController: YHBaseViewController { ...@@ -148,10 +144,11 @@ class YHAIMainChatViewController: YHBaseViewController {
let question = YHAIChatMessage.createQuestionMessage(text) let question = YHAIChatMessage.createQuestionMessage(text)
self.messages.append(question) self.messages.append(question)
self.messages.append(self.thinkingMessage) self.messages.append(self.thinkingMessage)
self.reloadAndScrollToBottom(forceScrollToBottom) self.reloadAndScrollToBottom()
self.bottomInputView.status = .loading self.bottomInputView.status = .loading
self.chatConfig.disableHandleMessage = false self.chatConfig.disableHandleMessage = false
self.manager.disableHandleMessage = false self.manager.disableHandleMessage = false
self.disableScrollToBottom = false
self.manager.requestAI(botId: self.robotId, conversationId: self.conversationId, question:text) { self.manager.requestAI(botId: self.robotId, conversationId: self.conversationId, question:text) {
[weak self] res, done in [weak self] res, done in
guard let self = self else { return } guard let self = self else { return }
...@@ -160,7 +157,7 @@ class YHAIMainChatViewController: YHBaseViewController { ...@@ -160,7 +157,7 @@ class YHAIMainChatViewController: YHBaseViewController {
print("RESPONSE-DONE") print("RESPONSE-DONE")
self.bottomInputView.status = self.bottomInputView.textView.text.isEmpty ? .disableSend : .enableSend self.bottomInputView.status = self.bottomInputView.textView.text.isEmpty ? .disableSend : .enableSend
} }
self.reloadAndScrollToBottom(forceScrollToBottom) self.reloadAndScrollToBottom()
} }
} }
} }
...@@ -185,7 +182,7 @@ class YHAIMainChatViewController: YHBaseViewController { ...@@ -185,7 +182,7 @@ class YHAIMainChatViewController: YHBaseViewController {
} }
messages.insert(contentsOf: results, at: 0) messages.insert(contentsOf: results, at: 0)
autoResponseTextMessage() autoResponseTextMessage()
self.reloadAndScrollToBottom(true, true) self.reloadAndScrollToBottom()
} }
} }
...@@ -264,6 +261,8 @@ extension YHAIMainChatViewController: UITableViewDelegate, UITableViewDataSource ...@@ -264,6 +261,8 @@ extension YHAIMainChatViewController: UITableViewDelegate, UITableViewDataSource
cell.messageClick = { cell.messageClick = {
[weak self] text in [weak self] text in
guard let self = self else { return } guard let self = self else { return }
self.bottomInputView.showKeyBoard(false)
if self.isNeedStopResonse() { if self.isNeedStopResonse() {
self.stopAutoResponse { success in self.stopAutoResponse { success in
self.sendMessage(text) self.sendMessage(text)
...@@ -350,7 +349,7 @@ extension YHAIMainChatViewController: UITableViewDelegate, UITableViewDataSource ...@@ -350,7 +349,7 @@ extension YHAIMainChatViewController: UITableViewDelegate, UITableViewDataSource
if 0 <= indexPath.row, indexPath.row < messages.count { if 0 <= indexPath.row, indexPath.row < messages.count {
let message = messages[indexPath.row] let message = messages[indexPath.row]
let msgType = message.getType() let msgType = message.getType()
if !message.isTextMessage() { if msgType != .text {
return UITableView.automaticDimension return UITableView.automaticDimension
} }
...@@ -366,7 +365,7 @@ extension YHAIMainChatViewController: UITableViewDelegate, UITableViewDataSource ...@@ -366,7 +365,7 @@ extension YHAIMainChatViewController: UITableViewDelegate, UITableViewDataSource
textHeight = 20.0 textHeight = 20.0
} }
if message.isSelf || msgType == .recommendText { if message.isSelf {
return textHeight + 16.0*2 + 16.0 return textHeight + 16.0*2 + 16.0
} }
...@@ -405,25 +404,22 @@ extension YHAIMainChatViewController: UITableViewDelegate, UITableViewDataSource ...@@ -405,25 +404,22 @@ extension YHAIMainChatViewController: UITableViewDelegate, UITableViewDataSource
} }
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) { func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
isUserScrolling = true disableScrollToBottom = true
lastUserScrollTime = CACurrentMediaTime() self.bottomInputView.showKeyBoard(false)
} }
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) { func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if !decelerate { if !decelerate {
isUserScrolling = false
} }
} }
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
isUserScrolling = false
lastUserScrollTime = CACurrentMediaTime()
} }
// 检查是否可以执行代码触发的滚动 // 检查是否可以执行代码触发的滚动
func canTriggerProgrammaticScroll() -> Bool { func canTriggerProgrammaticScroll() -> Bool {
let currentTime = CACurrentMediaTime() return !disableScrollToBottom
return (currentTime - lastUserScrollTime) >= minimumScrollInterval
} }
} }
......
...@@ -245,40 +245,44 @@ class YHAIRequestManager: NSObject { ...@@ -245,40 +245,44 @@ class YHAIRequestManager: NSObject {
self.uuid = UUID().uuidString + NSDate().timeIntervalSince1970.description self.uuid = UUID().uuidString + NSDate().timeIntervalSince1970.description
} }
// if receiveMessage.isUserfulMessage() {
// if receiveMessage.body.isDone() {
// completion?(nil, sessionDone)
// } else {
// completion?(receiveMessage, sessionDone)
// }
// }
if receiveMessage.isUserfulMessage() { if receiveMessage.isUserfulMessage() {
if receiveMessage.body.isDone() { if receiveMessage.body.isDone() {
completion?(nil, sessionDone) completion?(nil, sessionDone)
return
}
if receiveMessage.isNeedSpiceMessage() {
let text = receiveMessage.body.contentText
if text.count > 1 {
let textArray = Array(text)
for (index, character) in textArray.enumerated() {
let msg = receiveMessage.copy() as! YHAIChatMessage
msg.body.contentText = String(character)
msg.body.status = ""
if index == textArray.count-1 {
msg.body.status = receiveMessage.body.status
}
msg.updateBodyToData()
printLog("AAAAA- \(msg)")
sessionDone = receiveMessage.body.isDone()
completion?(msg, sessionDone)
}
} else {
completion?(receiveMessage, sessionDone)
}
} else { } else {
completion?(receiveMessage, sessionDone) completion?(receiveMessage, sessionDone)
} }
} }
// if receiveMessage.isUserfulMessage() {
// if receiveMessage.isNeedSpiceMessage() {
// let text = receiveMessage.body.contentText
// if text.count > 1 {
// let textArray = Array(text)
// for (index, character) in textArray.enumerated() {
// let msg = receiveMessage.copy() as! YHAIChatMessage
// msg.body.contentText = String(character)
// msg.body.status = ""
// if index == textArray.count-1 {
// msg.body.status = receiveMessage.body.status
// }
// msg.updateBodyToData()
// printLog("AAAAA- \(msg)")
// sessionDone = receiveMessage.body.isDone()
// completion?(msg, sessionDone)
// }
//
// } else {
// completion?(receiveMessage, sessionDone)
// }
//
// } else {
// completion?(receiveMessage, sessionDone)
// }
// }
} }
} // arr 结束 } // arr 结束
} }
......
...@@ -15,9 +15,7 @@ class YHAIRobotChatViewController: YHBaseViewController { ...@@ -15,9 +15,7 @@ class YHAIRobotChatViewController: YHBaseViewController {
var myTitle: String = "" var myTitle: String = ""
var robotId: String = "" var robotId: String = ""
var listItemId: Int = 0 var listItemId: Int = 0
var isUserScrolling: Bool = false var disableScrollToBottom: Bool = false
var lastUserScrollTime: CFTimeInterval = 0.0
let minimumScrollInterval: CFTimeInterval = 2.0
var conversationId: String = "" var conversationId: String = ""
var messages:[YHAIChatMessage] = [] var messages:[YHAIChatMessage] = []
var isNeedShowBannerHeader: Bool = false var isNeedShowBannerHeader: Bool = false
...@@ -181,11 +179,11 @@ class YHAIRobotChatViewController: YHBaseViewController { ...@@ -181,11 +179,11 @@ class YHAIRobotChatViewController: YHBaseViewController {
self.navigationController?.popViewController(animated: true) self.navigationController?.popViewController(animated: true)
} }
func reloadAndScrollToBottom(_ forceScrollToBottom: Bool = false, _ isNeedAccurate: Bool = false) { func reloadAndScrollToBottom() {
self.tableView.reloadData() self.tableView.reloadData()
if !forceScrollToBottom && !canTriggerProgrammaticScroll() { if canTriggerProgrammaticScroll() {
return return
} }
...@@ -219,7 +217,8 @@ class YHAIRobotChatViewController: YHBaseViewController { ...@@ -219,7 +217,8 @@ class YHAIRobotChatViewController: YHBaseViewController {
self.bottomInputView.status = .loading self.bottomInputView.status = .loading
self.chatConfig.disableHandleMessage = false self.chatConfig.disableHandleMessage = false
self.manager.disableHandleMessage = false self.manager.disableHandleMessage = false
self.reloadAndScrollToBottom(forceScrollToBottom) self.disableScrollToBottom = false
self.reloadAndScrollToBottom()
self.manager.requestAI(botId: self.robotId, conversationId: self.conversationId, question:text) { self.manager.requestAI(botId: self.robotId, conversationId: self.conversationId, question:text) {
[weak self] res, done in [weak self] res, done in
...@@ -229,7 +228,7 @@ class YHAIRobotChatViewController: YHBaseViewController { ...@@ -229,7 +228,7 @@ class YHAIRobotChatViewController: YHBaseViewController {
print("RESPONSE-DONE") print("RESPONSE-DONE")
self.bottomInputView.status = self.bottomInputView.textView.text.isEmpty ? .disableSend : .enableSend self.bottomInputView.status = self.bottomInputView.textView.text.isEmpty ? .disableSend : .enableSend
} }
self.reloadAndScrollToBottom(forceScrollToBottom) self.reloadAndScrollToBottom()
} }
} }
...@@ -251,7 +250,7 @@ class YHAIRobotChatViewController: YHBaseViewController { ...@@ -251,7 +250,7 @@ class YHAIRobotChatViewController: YHBaseViewController {
return !uuids.contains($0.messageId) return !uuids.contains($0.messageId)
} }
messages.insert(contentsOf: results, at: 0) messages.insert(contentsOf: results, at: 0)
reloadAndScrollToBottom(true, true) reloadAndScrollToBottom()
autoResponseLocalTextMessage() autoResponseLocalTextMessage()
} }
...@@ -276,7 +275,7 @@ class YHAIRobotChatViewController: YHBaseViewController { ...@@ -276,7 +275,7 @@ class YHAIRobotChatViewController: YHBaseViewController {
let msg = self.chatConfig.createRobotResponseLocalPictureMessage("ai_auto_chat_img", previewUrl:previewUrl) let msg = self.chatConfig.createRobotResponseLocalPictureMessage("ai_auto_chat_img", previewUrl:previewUrl)
messages.append(msg) messages.append(msg)
} }
self.reloadAndScrollToBottom(true) self.reloadAndScrollToBottom()
} }
func uploadEvaluationMessage(_ msg: YHAIChatMessage, callback:((Bool)->())? = nil) { func uploadEvaluationMessage(_ msg: YHAIChatMessage, callback:((Bool)->())? = nil) {
...@@ -490,7 +489,7 @@ extension YHAIRobotChatViewController: UITableViewDelegate, UITableViewDataSourc ...@@ -490,7 +489,7 @@ extension YHAIRobotChatViewController: UITableViewDelegate, UITableViewDataSourc
if 0 <= indexPath.row, indexPath.row < messages.count { if 0 <= indexPath.row, indexPath.row < messages.count {
let message = messages[indexPath.row] let message = messages[indexPath.row]
let msgType = message.getType() let msgType = message.getType()
if !message.isTextMessage() { if msgType != .text {
return UITableView.automaticDimension return UITableView.automaticDimension
} }
...@@ -506,7 +505,7 @@ extension YHAIRobotChatViewController: UITableViewDelegate, UITableViewDataSourc ...@@ -506,7 +505,7 @@ extension YHAIRobotChatViewController: UITableViewDelegate, UITableViewDataSourc
textHeight = 20.0 textHeight = 20.0
} }
if message.isSelf || msgType == .recommendText { if message.isSelf {
return textHeight + 16.0*2 + 16.0 return textHeight + 16.0*2 + 16.0
} }
...@@ -550,25 +549,22 @@ extension YHAIRobotChatViewController: UITableViewDelegate, UITableViewDataSourc ...@@ -550,25 +549,22 @@ extension YHAIRobotChatViewController: UITableViewDelegate, UITableViewDataSourc
} }
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) { func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
isUserScrolling = true disableScrollToBottom = true
lastUserScrollTime = CACurrentMediaTime() self.bottomInputView.showKeyBoard(false)
} }
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) { func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
if !decelerate { if !decelerate {
isUserScrolling = false
} }
} }
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
isUserScrolling = false
lastUserScrollTime = CACurrentMediaTime()
} }
// 检查是否可以执行代码触发的滚动 // 检查是否可以执行代码触发的滚动
func canTriggerProgrammaticScroll() -> Bool { func canTriggerProgrammaticScroll() -> Bool {
let currentTime = CACurrentMediaTime() return !disableScrollToBottom
return (currentTime - lastUserScrollTime) >= minimumScrollInterval
} }
} }
......
...@@ -52,7 +52,11 @@ class YHAITextMessageCell: UITableViewCell { ...@@ -52,7 +52,11 @@ class YHAITextMessageCell: UITableViewCell {
whiteContentView.snp.remakeConstraints { make in whiteContentView.snp.remakeConstraints { make in
make.left.equalTo(20) make.left.equalTo(20)
make.right.equalTo(-20) if message.getType() == .recommendText {
make.right.lessThanOrEqualTo(-20)
} else {
make.right.equalTo(-20)
}
make.top.equalTo(16) make.top.equalTo(16)
make.bottom.equalTo(0) make.bottom.equalTo(0)
} }
...@@ -263,7 +267,7 @@ class YHAITextMessageCell: UITableViewCell { ...@@ -263,7 +267,7 @@ class YHAITextMessageCell: UITableViewCell {
@objc func didMessageClicked() { @objc func didMessageClicked() {
self.endEditing(true) UIApplication.shared.yhKeyWindow()?.endEditing(true)
if message.getType() == .recommendText { if message.getType() == .recommendText {
let text = message.getText() let text = message.getText()
messageClick?(text) messageClick?(text)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment