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

// AI

parent 2b46bfd3
......@@ -13,6 +13,8 @@ import JXSegmentedView
class YHAIMainChatViewController: YHBaseViewController {
var robotId: String = ""
var messages:[YHAIMessage] = []
lazy var tableView: UITableView = {
......@@ -45,13 +47,16 @@ class YHAIMainChatViewController: YHBaseViewController {
messages.append(thinkingMessage)
self.scrollToBottom()
YHAIRequestManager.shared.requestAI(botId: "", conversationId: "", question:text) {
[weak self] res, done in
guard let self = self else { return }
YHAIChatConfiguration.defaultConfig.handleReceiveMessage(res, done, &messages)
self.scrollToBottom()
YHAIRequestManager.shared.getMainChatConversationId {
sesseionId in
YHAIRequestManager.shared.requestAI(botId: self.robotId, conversationId: sesseionId, question:text) {
[weak self] res, done in
guard let self = self else { return }
YHAIChatConfiguration.defaultConfig.handleReceiveMessage(res, done, &messages)
self.scrollToBottom()
}
}
}
return v
}()
......@@ -112,7 +117,7 @@ extension YHAIMainChatViewController: UITableViewDelegate, UITableViewDataSource
let defaultCell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath)
return defaultCell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
......
......@@ -59,8 +59,6 @@ class YHAIRequestManager: NSObject {
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
self.uuid = UUID().uuidString + NSDate().timeIntervalSince1970.description
......@@ -68,11 +66,11 @@ class YHAIRequestManager: NSObject {
// body 请求体参数
var parameters = ["botId" : botId,
"conversationId" : conversationId,
"question" : question]
"question" : question] as! [String: Any]
parameters = ["botId": "7441190609433755702",
"conversationId": sesseionId,
"question": question] as! [String: String]
// parameters = ["botId": "7441190609433755702",
// "conversationId": sesseionId,
// "question": question] as! [String: String]
let headers = self.getCommonHeaders()
......@@ -121,7 +119,6 @@ class YHAIRequestManager: NSObject {
}
}
}
}
}
func dictionaryToHttpBodyStream(_ dic: [String: Any]) -> InputStream? {
......@@ -199,7 +196,7 @@ class YHAIRequestManager: NSObject {
}
extension YHAIRequestManager {
func getSessionId(completion:((String)->())?) {
func getMainChatConversationId(completion:((String)->())?) {
if let sessionId = UserDefaults.standard.value(forKey: YHAIRequestManager.sessionKey) as? String, !sessionId.isEmpty {
completion?(sessionId)
......
......@@ -5,26 +5,139 @@
// Created by Dufet on 2024/11/29.
// Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//
import UIKit
import Alamofire
import IQKeyboardManagerSwift
import JXSegmentedView
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() {
super.viewDidLoad()
// Do any additional setup after loading the view.
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)
}
}
}
/*
// MARK: - Navigation
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 {
if 0 <= indexPath.row, indexPath.row < messages.count {
let msg = messages[indexPath.row]
let cell = YHAIChatConfiguration.defaultConfig.getMessageCell(msg, tableView, indexPath: indexPath)
return cell
}
let defaultCell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell", for: indexPath)
return defaultCell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
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 {
make.height.equalTo(46)
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
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
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
class YHAITabViewController: YHBaseViewController {
var defaltIndex: Int = 0
let viewModel = YHAIViewModel()
let arrItemTitles = ["港小宝", "对话"]
var arrItemVCs : [YHBaseViewController] = []
......@@ -65,6 +66,17 @@ class YHAITabViewController: YHBaseViewController {
return btn
}()
lazy var listVC: YHAIServiceListViewController = {
let vc = YHAIServiceListViewController()
return vc
}()
lazy var mainChatVC: YHAIMainChatViewController = {
let vc = YHAIMainChatViewController()
return vc
}()
@objc func didBackBtnClicked() {
self.navigationController?.popViewController(animated: true)
}
......@@ -77,6 +89,11 @@ class YHAITabViewController: YHBaseViewController {
createUI()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
requestList()
}
deinit {
NotificationCenter.default.removeObserver(self)
}
......@@ -88,14 +105,13 @@ class YHAITabViewController: YHBaseViewController {
view.addSubview(bgImgView)
view.addSubview(headerImgView)
arrItemVCs.removeAll()
for i in 0..<arrItemTitles.count {
if i == 0 { // 银河AI
let vc = YHAIServiceListViewController()
arrItemVCs.append(vc)
arrItemVCs.append(listVC)
} else if i == 1 { // 对话
let vc = YHAIMainChatViewController()
arrItemVCs.append(vc)
arrItemVCs.append(mainChatVC)
}
}
......@@ -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) {
self.segmentedView.selectItemAt(index: itemIndex)
}
......
......@@ -25,6 +25,7 @@ class YHAIProductCell: UICollectionViewCell {
} else {
blackButton.setTitle("去了解", for: .normal)
}
bgImgView.sd_setImage(with: URL(string: model.cover))
}
}
......@@ -122,7 +123,8 @@ class YHAIProductCell: UICollectionViewCell {
titleLabel.transform = transform
subtitleLabel.transform = transform
blackButton.transform = transform
bgImgView.image = UIImage(named: isRotateRight ? "ai_product_left" : "ai_product_right")
if model.cover.isEmpty {
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