Commit 5772f8c9 authored by Steven杜宇's avatar Steven杜宇

// AI

parent 2f5db187
......@@ -14,10 +14,13 @@ import JXSegmentedView
class YHAIMainChatViewController: YHBaseViewController {
var robotId: String = ""
var conversationId: String = ""
var historyLastMessageId: String = ""
var messages:[YHAIChatMessage] = []
let manager = YHAIRequestManager()
var conversationId: String = ""
let viewModel = YHAIViewModel()
lazy var tableView: UITableView = {
......@@ -34,6 +37,9 @@ class YHAIMainChatViewController: YHBaseViewController {
tableView.delegate = self
tableView.dataSource = self
YHAIChatConfiguration.defaultConfig.registerMessageGroupCells(tableView)
tableView.es.addYHPullToRefresh {
self.getHistoryMessages(false)
}
return tableView
}()
......@@ -97,6 +103,14 @@ class YHAIMainChatViewController: YHBaseViewController {
bottomInputView.snp.makeConstraints { make in
make.left.right.bottom.equalToSuperview()
}
YHHUD.show(.progress(message: "加载中..."))
self.manager.getMainChatConversationId {
sesseionId in
YHHUD.hide()
self.conversationId = sesseionId
self.getHistoryMessages(false)
}
}
func scrollToBottom() {
......@@ -131,6 +145,24 @@ class YHAIMainChatViewController: YHBaseViewController {
}
}
}
func getHistoryMessages(_ isLoading: Bool) {
if isLoading {
YHHUD.show(.progress(message: "加载中..."))
}
viewModel.getHistoryChatMessages(botId: robotId, conversationId: conversationId, messageId: historyLastMessageId) {
[weak self] success, error in
YHHUD.hide()
guard let self = self else { return }
self.historyLastMessageId = self.viewModel.lastHistroyMsgId
self.tableView.es.stopPullToRefresh()
let results = self.viewModel.historyMessages.map {
return $0.convertToChatMessage()
}
messages.append(contentsOf: results)
self.tableView.reloadData()
}
}
}
extension YHAIMainChatViewController: UITableViewDelegate, UITableViewDataSource {
......
......@@ -28,7 +28,6 @@ class YHAIRequestManager: NSObject {
var requestHeader = HTTPHeaders()
requestHeader.add(name: "Accept", value: "text/event-stream")
requestHeader.add(name: "Connection", value: "keep-alive")
requestHeader.add(name:"appid",value:"1")
let dateStr = String(Date().timeIntervalSince1970)
requestHeader.add(name:"timestamp",value:dateStr)
......@@ -219,7 +218,7 @@ class YHAIRequestManager: NSObject {
if receiveMessage.isFinishd() {
// 一段话结束需要重新生成uuid 来
uuid = UUID().uuidString + NSDate().timeIntervalSince1970.description
self.uuid = UUID().uuidString + NSDate().timeIntervalSince1970.description
}
}
} // arr 结束
......
......@@ -149,11 +149,12 @@ class YHAIRobotChatViewController: YHBaseViewController {
if isLoading {
YHHUD.show(.progress(message: "加载中..."))
}
viewModel.getHistoryChatMessages(boltId: robotId, conversationId: conversationId, messageId: historyLastMessageId) {
viewModel.getHistoryChatMessages(botId: robotId, conversationId: conversationId, messageId: historyLastMessageId) {
[weak self] success, error in
YHHUD.hide()
guard let self = self else { return }
messages.removeAll()
self.historyLastMessageId = self.viewModel.lastHistroyMsgId
self.tableView.es.stopPullToRefresh()
let results = self.viewModel.historyMessages.map {
return $0.convertToChatMessage()
}
......
......@@ -19,19 +19,25 @@ class YHAIHistoryMessage: SmartCodable {
var role: String = ""
var type: String = ""
var cardsInfo: YHAIListinfoModel?
required init() {
}
func convertToChatMessage() -> YHAIChatMessage {
let msg = YHAIChatMessage()
msg.uuid = self.messageId
msg.isSelf = self.role == "user"
msg.isDone = true
let body = YHAIMessageBody()
body.contentType = self.contentType
body.contentText = self.contentText
body.botId = self.botId
body.chatId = self.chatId
body.type = self.type
body.cardsInfo = self.cardsInfo
msg.body = body
msg.setFinished()
msg.updateBodyToData()
......
......@@ -15,6 +15,7 @@ class YHAIViewModel: NSObject {
var listModel: YHAIListModel = YHAIListModel()
var sessionId: String = ""
var historyMessages: [YHAIHistoryMessage] = []
var lastHistroyMsgId: String = ""
func getAIEntranceList(callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
......@@ -71,11 +72,11 @@ class YHAIViewModel: NSObject {
}
}
func getHistoryChatMessages(boltId: String, conversationId: String, messageId: String, callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
func getHistoryChatMessages(botId: String, conversationId: String, messageId: String, callBackBlock:@escaping (_ success: Bool,_ error:YHErrorModel?)->()) {
let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.AIChat.chatHistory
let params:[String : Any] = [
"boltId" : boltId,
"botId" : botId,
"conversationId" : conversationId,
"messageId" : messageId
]
......@@ -92,7 +93,10 @@ class YHAIViewModel: NSObject {
return
}
self.historyMessages = arr
self.historyMessages = arr.reversed()
if let lastMsg = arr.last {
self.lastHistroyMsgId = lastMsg.messageId
}
callBackBlock(true,nil)
} else {
......
......@@ -47,8 +47,6 @@ class YHAITextInputView: UIView {
let v = UIView()
v.backgroundColor = .white
v.layer.cornerRadius = 12.0
v.layer.borderWidth = 2.0
v.layer.borderColor = UIColor.brandMainColor.cgColor
v.clipsToBounds = true
return v
}()
......
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