ConversationQA.vue 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <template>
  2. <LayoutExperience>
  3. <ContentContainer :containerStyle="containerStyle">
  4. <ChatAsideMenu @openWelcome="openWelcome" @toNewChat="newChat" />
  5. <template v-if="query_value_status === 2">
  6. <ConversationChatContainer
  7. ref="conversation_chat_container"
  8. :agent_id="chat_agent"
  9. :CHAT_AGENT_INFOMATION="CHAT_AGENT_INFOMATION"
  10. :session_id="chat_session"
  11. :chat_history="chat_history"
  12. :key="chat_key"
  13. :conversation_params="conversation_params"
  14. @updateSessionId="updateSessionId"
  15. />
  16. </template>
  17. <template v-if="query_value_status === 3">
  18. <a-result status="error" :title="query_value_message">
  19. <template #extra>
  20. <a-space>
  21. <a-button type="primary" @click="handleBack">{{ $t('conversation.back') }}</a-button>
  22. </a-space>
  23. </template>
  24. </a-result>
  25. </template>
  26. </ContentContainer>
  27. </LayoutExperience>
  28. </template>
  29. <script setup>
  30. import { ref, computed, onMounted, onBeforeUpdate, watch } from 'vue';
  31. import { useRoute, useRouter, onBeforeRouteUpdate } from 'vue-router';
  32. import LayoutExperience from './layout/LayoutExperience.vue';
  33. import ContentContainer from './layout/ContentContainer.vue';
  34. import ChatAsideMenu from './conversation/ChatAsideMenu.vue';
  35. import ConversationChatContainer from './conversation/ConversationChatContainer.vue';
  36. import { useAllAgentStore } from '../base/store/use-all-agents';
  37. import getChatAgentInfomation from '../modules/conversation-board/get-chat-agent-infomation';
  38. import { getConfigField } from '../config/index';
  39. import useConversationChatParameters from '../modules/conversation-chat/use-conversation-chat-parameters';
  40. // 对话配置信息接口数据
  41. const { getConversationParams, api_status, api_message, conversation_params } = useConversationChatParameters();
  42. /**
  43. * 此页面为对话页面
  44. * 用户可能从如下几个地方进入对话页面:
  45. * 1. 从对话历史记录,点击某个记录,进入对话页面,查看此记录的对话内容,此时,路由会存在两个参数
  46. * agent 历史记录对应的agent ID
  47. * session 历史记录的ID
  48. * 2. 从对话智能体大全页面,点击某个智能体,进入对话页面,新建该智能体对应的对话,此时,路由会有一个参数
  49. * agent 对应的agent ID
  50. * 此页面大致流程如下:
  51. * 1. 检查参数是否合法
  52. * A. 检查是否存在必须参数 agent
  53. * B. 如果agent参数存在,则检查agent值是否在缓存中存在,如果不存在,则意味着有人在地址栏修改参数值,无效的agent值于对话没有意义,故,直接做报错处理
  54. * 2. 在参数合法的情况下,进入对话流程
  55. *
  56. * 根据必须参数 agent ID,可能会存在如下相关的操作:
  57. * 1. 此agent对应的主题背景
  58. * 2. 此agent可用、不可用的功能设定
  59. */
  60. const route = useRoute();
  61. const router = useRouter();
  62. const allAgent = useAllAgentStore();
  63. const chat_agent = ref('');
  64. const chat_session = ref('');
  65. const chat_history = ref('');
  66. const conversation_chat_container = ref(null);
  67. /**
  68. * 对话区域的唯一ID
  69. * 智能体的ID + 当前路由地址
  70. */
  71. const chat_key = ref(chat_agent.value + window.location.href);
  72. watch(route, (to) => {
  73. chat_key.value = chat_agent.value + window.location.href;
  74. });
  75. /**
  76. * 对话使用到的agent信息
  77. * agent属性是由接口返回的数据
  78. * config属性是客户端自定义的数据,针对特定的agent的一些个性化配置,可由此进行配置
  79. * 二者结合,形成对话组件所需要的一些参数
  80. */
  81. const CHAT_AGENT_INFOMATION = ref({
  82. agent: {},
  83. config: {}
  84. });
  85. const query_value_status = ref(1); // 1 初始化状态 2 合法有效 3 非法无效,报错
  86. const query_value_message = ref('');
  87. const handleBack = () => {
  88. router.back();
  89. };
  90. const containerStyle = {
  91. paddingLeft: 0,
  92. paddingRight: 0
  93. };
  94. /**
  95. * 检查是否存在合法的agent参数值
  96. * @param agent_id
  97. */
  98. async function isValidAgent(agent_id) {
  99. if (!agent_id) {
  100. return false;
  101. }
  102. // 检查缓存数据中是否存在此agent id值
  103. const agent_list = allAgent.getList();
  104. if (agent_list.length === 0) {
  105. // 通过接口获取所有的agent数据
  106. // TOOD: 如果agent_list为空,则意味着内存和本地存储中都没有记录,则需要通过接口获取agent列表数据
  107. return true;
  108. } else {
  109. const has_cache = agent_list.find((item) => item.id === agent_id);
  110. if (!has_cache) {
  111. // 缓存数据中不存在此ID值,非法的操作
  112. // 可能是有人通过地址栏修改参数值
  113. return false;
  114. }
  115. return true;
  116. }
  117. }
  118. /**
  119. * 获取默认的对话ID
  120. */
  121. async function getDefaultAgentId() {
  122. let default_agent_id = null;
  123. // 获取默认智能体信息
  124. const default_id = getConfigField('DEFAULT_CHAT_AGENT_ID');
  125. const default_name = getConfigField('DEFAULT_CHAT_AGENT_NAME');
  126. // const agent_list = allAgent.getList();
  127. // const len = agent_list.length;
  128. // if (len > 1) {
  129. // for (let i = 0; i < len; i++) {
  130. // const item = agent_list[i];
  131. // if (item.name === default_name) {
  132. // default_agent_id = item.id;
  133. // break;
  134. // }
  135. // }
  136. // if (!default_agent_id) {
  137. // // 未搜索到: 使用第一个
  138. // default_agent_id = agent_list[0].id;
  139. // }
  140. // } else if (len === 1) {
  141. // // 仅有一个
  142. // default_agent_id = agent_list[0].id;
  143. // } else {
  144. // // 为0: 使用配置的ID
  145. // default_agent_id = default_id;
  146. // }
  147. // if (!default_agent_id) {
  148. // // 如果ID依然不生效,则需要请求api
  149. // // TODO: 请求API
  150. // }
  151. return default_agent_id;
  152. }
  153. /**
  154. * agent id已经确认
  155. * @param agent_id
  156. */
  157. function comfirmAgent(agent_id) {
  158. if (!agent_id) {
  159. // 根据agent_id 查询到动态表单数据
  160. getConversationParams(undefined);
  161. }
  162. /**
  163. * 根据agent_id 查询到agent的基本信息
  164. * 从而确定agent的相关功能支持,例如:是否需要上传文件,支持上传什么类型的文件、支持上传多少个文件?
  165. * 例如:主题、背景、等等
  166. */
  167. CHAT_AGENT_INFOMATION.value = getChatAgentInfomation(agent_id);
  168. chat_agent.value = agent_id;
  169. // 根据agent_id 查询到动态表单数据
  170. getConversationParams(chat_agent.value);
  171. }
  172. const updateSessionId = (session_id) => {
  173. chat_session.value = session_id;
  174. };
  175. const openWelcome = () => {
  176. if (conversation_chat_container.value) {
  177. conversation_chat_container.value.toShowWelcome();
  178. }
  179. };
  180. const newChat = async () => {
  181. await init();
  182. if (conversation_chat_container.value) {
  183. conversation_chat_container.value.toNewChat();
  184. }
  185. };
  186. watch(
  187. () => route,
  188. (val) => {
  189. console.log(route);
  190. if (route.query.chat) {
  191. chat_history.value = '';
  192. newChat();
  193. }
  194. chat_history.value = route.query.history || '';
  195. },
  196. {
  197. deep: true,
  198. immediate: true
  199. }
  200. );
  201. async function init() {
  202. const route = useRoute();
  203. const query = route?.query;
  204. let agent_id, session_id;
  205. if (query && query.agent) {
  206. agent_id = query.agent;
  207. }
  208. if (query && query.session) {
  209. session_id = query.session;
  210. }
  211. if (!agent_id) {
  212. query_value_status.value = 2;
  213. query_value_message.value = 'success';
  214. // 路由中不存在智能体ID参数
  215. // 获取到默认的智能体ID
  216. const default_agent_id = await getDefaultAgentId();
  217. // // 将路由重置为包含agent参数的地址
  218. // router.replace({
  219. // path: '/chat/qa',
  220. // query: {
  221. // agent: default_agent_id
  222. // }
  223. // });
  224. chat_agent.value = default_agent_id;
  225. comfirmAgent(default_agent_id);
  226. // // ------------------------------------------------------
  227. // // ------------------------------------------------------
  228. // // ------------------------------------------------------
  229. // // ------------------------------------------------------
  230. // query_value_status.value = 2;
  231. // query_value_message.value = 'success';
  232. } else {
  233. // 有agent参数
  234. // 判断已经缓存的agent列表是否存在
  235. const is_valid = await isValidAgent(agent_id);
  236. if (!is_valid) {
  237. // 不存在参数
  238. query_value_status.value = 3;
  239. query_value_message.value = '非法操作:参数异常';
  240. return;
  241. }
  242. // chat_agent.value = agent_id;
  243. chat_session.value = session_id;
  244. // // 如果存在history_id则获取历史会话内容
  245. // /**
  246. // * 根据agent_id 查询到agent的基本信息
  247. // * 从而确定agent的相关功能支持,例如:是否需要上传文件,支持上传什么类型的文件、支持上传多少个文件?
  248. // * 例如:主题、背景、等等
  249. // */
  250. // CHAT_AGENT_INFOMATION.value = getChatAgentInfomation(agent_id);
  251. comfirmAgent(agent_id);
  252. query_value_status.value = 2;
  253. query_value_message.value = 'success';
  254. }
  255. chat_session.value = session_id;
  256. }
  257. /**
  258. * 处理路由变化
  259. * 监听路由参数的变化
  260. */
  261. onBeforeRouteUpdate(async (to, from) => {
  262. console.log(to.query);
  263. if (to.path === '/chat' || to.path === '/chat/qa') {
  264. if (!to.query.session) {
  265. chat_session.value = null;
  266. }
  267. // if (!to.query.agent) {
  268. // const default_agent_id = await getDefaultAgentId();
  269. // chat_agent.value = default_agent_id;
  270. // console.log(default_agent_id);
  271. // router.replace({
  272. // path: '/chat/qa',
  273. // query: {
  274. // agent: default_agent_id
  275. // }
  276. // });
  277. // }
  278. }
  279. });
  280. onMounted(async () => {
  281. await init();
  282. });
  283. onBeforeUpdate(async () => {
  284. await init();
  285. });
  286. </script>
  287. <style scoped>
  288. .icon-btn {
  289. position: absolute;
  290. }
  291. </style>