Commit f9b64510 authored by Steven杜宇's avatar Steven杜宇

// AI

parent 2b46bfd3
...@@ -13,6 +13,8 @@ import JXSegmentedView ...@@ -13,6 +13,8 @@ import JXSegmentedView
class YHAIMainChatViewController: YHBaseViewController { class YHAIMainChatViewController: YHBaseViewController {
var robotId: String = ""
var messages:[YHAIMessage] = [] var messages:[YHAIMessage] = []
lazy var tableView: UITableView = { lazy var tableView: UITableView = {
...@@ -45,14 +47,17 @@ class YHAIMainChatViewController: YHBaseViewController { ...@@ -45,14 +47,17 @@ class YHAIMainChatViewController: YHBaseViewController {
messages.append(thinkingMessage) messages.append(thinkingMessage)
self.scrollToBottom() self.scrollToBottom()
YHAIRequestManager.shared.requestAI(botId: "", conversationId: "", question:text) { YHAIRequestManager.shared.getMainChatConversationId {
sesseionId in
YHAIRequestManager.shared.requestAI(botId: self.robotId, conversationId: sesseionId, question:text) {
[weak self] res, done in [weak self] res, done in
guard let self = self else { return } guard let self = self else { return }
YHAIChatConfiguration.defaultConfig.handleReceiveMessage(res, done, &messages) YHAIChatConfiguration.defaultConfig.handleReceiveMessage(res, done, &messages)
self.scrollToBottom() self.scrollToBottom()
} }
} }
}
return v return v
}() }()
......
...@@ -59,8 +59,6 @@ class YHAIRequestManager: NSObject { ...@@ -59,8 +59,6 @@ class YHAIRequestManager: NSObject {
func requestAI(botId: String, conversationId: String, question: String = "", completion: ((_ msg: YHAIMessage?, _ done: Bool) -> Void)?) { func requestAI(botId: String, conversationId: String, question: String = "", completion: ((_ msg: YHAIMessage?, _ done: Bool) -> Void)?) {
self.getSessionId {
sesseionId in
let url = YHBaseUrlManager.shared.curURL() + YHAllApiName.AIChat.agentChat let url = YHBaseUrlManager.shared.curURL() + YHAllApiName.AIChat.agentChat
self.uuid = UUID().uuidString + NSDate().timeIntervalSince1970.description self.uuid = UUID().uuidString + NSDate().timeIntervalSince1970.description
...@@ -68,11 +66,11 @@ class YHAIRequestManager: NSObject { ...@@ -68,11 +66,11 @@ class YHAIRequestManager: NSObject {
// body 请求体参数 // body 请求体参数
var parameters = ["botId" : botId, var parameters = ["botId" : botId,
"conversationId" : conversationId, "conversationId" : conversationId,
"question" : question] "question" : question] as! [String: Any]
parameters = ["botId": "7441190609433755702", // parameters = ["botId": "7441190609433755702",
"conversationId": sesseionId, // "conversationId": sesseionId,
"question": question] as! [String: String] // "question": question] as! [String: String]
let headers = self.getCommonHeaders() let headers = self.getCommonHeaders()
...@@ -122,7 +120,6 @@ class YHAIRequestManager: NSObject { ...@@ -122,7 +120,6 @@ class YHAIRequestManager: NSObject {
} }
} }
} }
}
func dictionaryToHttpBodyStream(_ dic: [String: Any]) -> InputStream? { func dictionaryToHttpBodyStream(_ dic: [String: Any]) -> InputStream? {
do { do {
...@@ -199,7 +196,7 @@ class YHAIRequestManager: NSObject { ...@@ -199,7 +196,7 @@ class YHAIRequestManager: NSObject {
} }
extension YHAIRequestManager { extension YHAIRequestManager {
func getSessionId(completion:((String)->())?) { func getMainChatConversationId(completion:((String)->())?) {
if let sessionId = UserDefaults.standard.value(forKey: YHAIRequestManager.sessionKey) as? String, !sessionId.isEmpty { if let sessionId = UserDefaults.standard.value(forKey: YHAIRequestManager.sessionKey) as? String, !sessionId.isEmpty {
completion?(sessionId) completion?(sessionId)
......
...@@ -5,26 +5,139 @@ ...@@ -5,26 +5,139 @@
// Created by Dufet on 2024/11/29. // Created by Dufet on 2024/11/29.
// Copyright © 2024 https://www.galaxy-immi.com. All rights reserved. // Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
// //
import UIKit import UIKit
import Alamofire
import IQKeyboardManagerSwift
import JXSegmentedView
class YHAIRobotChatViewController: YHBaseViewController { class YHAIRobotChatViewController: YHBaseViewController {
var robotId: String = ""
var conversationId: String = ""
var messages:[YHAIMessage] = []
lazy var tableView: UITableView = {
let tableView = UITableView(frame:.zero, style:.grouped)
if #available(iOS 11.0, *) {
tableView.contentInsetAdjustmentBehavior = .never
}
tableView.showsVerticalScrollIndicator = false
tableView.estimatedSectionHeaderHeight = 1.0
tableView.estimatedSectionFooterHeight = 1.0
tableView.backgroundColor = .clear
tableView.separatorStyle = .none
tableView.delegate = self
tableView.dataSource = self
YHAIChatConfiguration.defaultConfig.registerMessageGroupCells(tableView)
return tableView
}()
lazy var bottomInputView: YHAITextInputView = {
let v = YHAITextInputView(frame: .zero)
v.sendBlock = {
[weak self] text in
guard let self = self else { return }
YHAIChatConfiguration.defaultConfig.removeThinkingMessageFromChatList(&messages)
let question = YHAIMessage.createQuestionMessage(text)
messages.append(question)
messages.append(thinkingMessage)
self.scrollToBottom()
YHAIRequestManager.shared.requestAI(botId: self.robotId, conversationId: self.conversationId, question:text) {
[weak self] res, done in
guard let self = self else { return }
YHAIChatConfiguration.defaultConfig.handleReceiveMessage(res, done, &messages)
self.scrollToBottom()
}
}
return v
}()
lazy var thinkingMessage: YHAIMessage = {
let msg = YHAIMessage.createThinkingMesssage()
return msg
}()
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
gk_navBarAlpha = 0
view.backgroundColor = .clear
IQKeyboardManager.shared.enable = false
gk_navTitle = "新港生活规划师"
self.view.addSubview(self.tableView)
self.view.addSubview(self.bottomInputView)
tableView.snp.makeConstraints { make in
make.left.right.equalTo(0)
make.top.equalTo(0)
make.bottom.equalTo(bottomInputView.snp.top).offset(-10)
}
bottomInputView.snp.makeConstraints { make in
make.left.right.bottom.equalToSuperview()
}
}
func scrollToBottom() {
self.tableView.reloadData {
let indexPath = IndexPath(row: self.messages.count-1, section: 0)
self.tableView.scrollToRow(at: indexPath, at: .bottom, animated: true)
}
}
}
// Do any additional setup after loading the view. extension YHAIRobotChatViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
} }
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messages.count
}
/* func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation if 0 <= indexPath.row, indexPath.row < messages.count {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { let msg = messages[indexPath.row]
// Get the new view controller using segue.destination. let cell = YHAIChatConfiguration.defaultConfig.getMessageCell(msg, tableView, indexPath: indexPath)
// Pass the selected object to the new view controller. return cell
}
let defaultCell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath)
return defaultCell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 1.0
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let view = UIView()
return view
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 1.0
} }
*/
} }
extension YHAIRobotChatViewController: JXSegmentedListContainerViewListDelegate {
func listView() -> UIView {
return view
}
}
...@@ -100,17 +100,6 @@ class YHAIServiceListViewController: YHBaseViewController { ...@@ -100,17 +100,6 @@ class YHAIServiceListViewController: YHBaseViewController {
make.height.equalTo(46) make.height.equalTo(46)
make.bottom.equalTo(-10-k_Height_safeAreaInsetsBottom()) make.bottom.equalTo(-10-k_Height_safeAreaInsetsBottom())
} }
requestList()
}
func requestList() {
viewModel.getAIEntranceList {
[weak self] success, error in
guard let self = self else { return }
serviceArr.removeAll()
serviceArr.append(contentsOf: self.viewModel.listModel.entranceConfig)
self.collectionView.reloadData()
}
} }
} }
...@@ -145,7 +134,17 @@ extension YHAIServiceListViewController: UICollectionViewDelegate, UICollectionV ...@@ -145,7 +134,17 @@ extension YHAIServiceListViewController: UICollectionViewDelegate, UICollectionV
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if 0 <= indexPath.item && indexPath.item < serviceArr.count { if 0 <= indexPath.item && indexPath.item < serviceArr.count {
let text = serviceArr[indexPath.item] let model = serviceArr[indexPath.item]
YHHUD.show(.progress(message: "加载中..."))
YHAIRequestManager.shared.getMainChatConversationId {
sesseionId in
YHHUD.hide()
let vc = YHAIRobotChatViewController()
vc.robotId = model.botId
vc.conversationId = sesseionId
self.navigationController?.pushViewController(vc, animated: true)
}
} }
} }
......
...@@ -12,6 +12,7 @@ import JXSegmentedView ...@@ -12,6 +12,7 @@ import JXSegmentedView
class YHAITabViewController: YHBaseViewController { class YHAITabViewController: YHBaseViewController {
var defaltIndex: Int = 0 var defaltIndex: Int = 0
let viewModel = YHAIViewModel()
let arrItemTitles = ["港小宝", "对话"] let arrItemTitles = ["港小宝", "对话"]
var arrItemVCs : [YHBaseViewController] = [] var arrItemVCs : [YHBaseViewController] = []
...@@ -65,6 +66,17 @@ class YHAITabViewController: YHBaseViewController { ...@@ -65,6 +66,17 @@ class YHAITabViewController: YHBaseViewController {
return btn return btn
}() }()
lazy var listVC: YHAIServiceListViewController = {
let vc = YHAIServiceListViewController()
return vc
}()
lazy var mainChatVC: YHAIMainChatViewController = {
let vc = YHAIMainChatViewController()
return vc
}()
@objc func didBackBtnClicked() { @objc func didBackBtnClicked() {
self.navigationController?.popViewController(animated: true) self.navigationController?.popViewController(animated: true)
} }
...@@ -77,6 +89,11 @@ class YHAITabViewController: YHBaseViewController { ...@@ -77,6 +89,11 @@ class YHAITabViewController: YHBaseViewController {
createUI() createUI()
} }
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
requestList()
}
deinit { deinit {
NotificationCenter.default.removeObserver(self) NotificationCenter.default.removeObserver(self)
} }
...@@ -88,14 +105,13 @@ class YHAITabViewController: YHBaseViewController { ...@@ -88,14 +105,13 @@ class YHAITabViewController: YHBaseViewController {
view.addSubview(bgImgView) view.addSubview(bgImgView)
view.addSubview(headerImgView) view.addSubview(headerImgView)
arrItemVCs.removeAll()
for i in 0..<arrItemTitles.count { for i in 0..<arrItemTitles.count {
if i == 0 { // 银河AI if i == 0 { // 银河AI
let vc = YHAIServiceListViewController() arrItemVCs.append(listVC)
arrItemVCs.append(vc)
} else if i == 1 { // 对话 } else if i == 1 { // 对话
let vc = YHAIMainChatViewController() arrItemVCs.append(mainChatVC)
arrItemVCs.append(vc)
} }
} }
...@@ -151,6 +167,19 @@ class YHAITabViewController: YHBaseViewController { ...@@ -151,6 +167,19 @@ class YHAITabViewController: YHBaseViewController {
} }
} }
func requestList() {
YHHUD.show(.progress(message: "加载中..."))
viewModel.getAIEntranceList {
[weak self] success, error in
YHHUD.hide()
guard let self = self else { return }
self.mainChatVC.robotId = self.viewModel.listModel.mainBotId
self.listVC.serviceArr.removeAll()
self.listVC.serviceArr.append(contentsOf: self.viewModel.listModel.entranceConfig)
self.listVC.collectionView.reloadData()
}
}
func jumpToItemIndex(itemIndex : Int) { func jumpToItemIndex(itemIndex : Int) {
self.segmentedView.selectItemAt(index: itemIndex) self.segmentedView.selectItemAt(index: itemIndex)
} }
......
...@@ -25,6 +25,7 @@ class YHAIProductCell: UICollectionViewCell { ...@@ -25,6 +25,7 @@ class YHAIProductCell: UICollectionViewCell {
} else { } else {
blackButton.setTitle("去了解", for: .normal) blackButton.setTitle("去了解", for: .normal)
} }
bgImgView.sd_setImage(with: URL(string: model.cover))
} }
} }
...@@ -122,7 +123,8 @@ class YHAIProductCell: UICollectionViewCell { ...@@ -122,7 +123,8 @@ class YHAIProductCell: UICollectionViewCell {
titleLabel.transform = transform titleLabel.transform = transform
subtitleLabel.transform = transform subtitleLabel.transform = transform
blackButton.transform = transform blackButton.transform = transform
if model.cover.isEmpty {
bgImgView.image = UIImage(named: isRotateRight ? "ai_product_left" : "ai_product_right") bgImgView.image = UIImage(named: isRotateRight ? "ai_product_left" : "ai_product_right")
} }
}
} }
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