ConversationChatContainer.vue 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832
  1. <template>
  2. <div class="conversation-chat-container">
  3. <div class="conversation-chat-main" ref="chat_scrren_container">
  4. <div class="conversation-chat-main-left"></div>
  5. <div class="conversation-chat-main-content" :style="style_conversation_chat_content">
  6. <DialogHeader
  7. v-if="!show_welcome && !is_init_model"
  8. class="conversation-chat-header"
  9. :agent_name="current_agent_name"
  10. :style="style_header"
  11. />
  12. <div class="conversation-chat-content" :style="style_body">
  13. <div class="conversation-chat-main-container" :style="style_conversation_chat_main_container">
  14. <template v-if="show_welcome">
  15. <!-- 有会话ID 获取历史记录,显示加载中状态 -->
  16. <!-- <div v-if="session_id" class="conversation-chat-area history-list-loading"> -->
  17. <!-- <div class="scroll-left"></div> -->
  18. <!-- <div class="history-list-loading-container"><a-spin /></div> -->
  19. <!-- <div class="scroll-right"></div> -->
  20. <!-- </div> -->
  21. <!-- 无会话ID,直接对话,此时可现实欢迎消息:欢迎使用报表合并、欢迎使用文档智能、欢迎使用小数绘图 -->
  22. <!-- <DialogWelcome v-if="!session_id" class="conversation-chat-area" /> -->
  23. <DialogWelcome class="conversation-chat-area" />
  24. </template>
  25. <DialogList
  26. v-if="!show_welcome"
  27. ref="list_scroller"
  28. class="conversation-chat-area"
  29. :height="list_height"
  30. :width="chat_width / 2"
  31. :agent_id="agent_id"
  32. :list="chat_display_list"
  33. :suggestion="chat_suggestion"
  34. :answer_action_list="answer_action_list"
  35. :is_show_right_page="show_right"
  36. @share="handleChatShare"
  37. @edit="handleEditChat"
  38. @retry="handleRetryChat"
  39. @toSearchPage="toSearchPage"
  40. @toShowMindMap="toShowMindMap"
  41. @submit="handleSuggestSubmit"
  42. :style="style_conversation_chat_list"
  43. >
  44. <!-- <template v-slot:left>
  45. <div class="scroll-left"></div>
  46. </template>
  47. <template v-slot:right>
  48. <div class="scroll-right"></div>
  49. </template> -->
  50. </DialogList>
  51. </div>
  52. <DialogInput
  53. v-show="!is_init_model"
  54. ref="dialog_input"
  55. class="conversation-chat-input"
  56. :style="style_conversation_chat_list"
  57. :ask_input_text="ask_input_text"
  58. :disabled="input_disabled"
  59. :enable_text_input="enable_text_input"
  60. :enable_file_upload="enable_file_upload"
  61. :file_accept="file_accept"
  62. :agent_id="agent_id"
  63. @sizeChange="handleInputSizeChange"
  64. @actionConfigChange="handleActionConfigChange"
  65. @submit="handleInputSubmit"
  66. >
  67. <template v-if="show_input_others_opts" v-slot:top>
  68. <DialogInputTop
  69. :user_input_form="conversation_params.user_input_form"
  70. @initModel="initModel"
  71. ref="dialog_input_top"
  72. />
  73. </template>
  74. <template v-slot:top>
  75. <DialogStopSse v-if="show_stop_sse" @stopResponse="stopResponse"></DialogStopSse>
  76. </template>
  77. </DialogInput>
  78. <DialogCommonUsed
  79. v-if="show_welcome"
  80. :style="style_conversation_chat_list"
  81. @updateAskTxt="updatAskTxt"
  82. ></DialogCommonUsed>
  83. </div>
  84. <DialogFooter v-show="!is_init_model" class="conversation-chat-footer" :style="style_footer" />
  85. </div>
  86. <div class="conversation-chat-main-right" :style="style_conversation_chat_right">
  87. <div class="conversation-chat-expand" :style="style_conversation_chat_expand" v-if="is_show_right_page">
  88. <div class="conversation-chat-mind-map" ref="fullScreen_mindMap">
  89. <!-- 全屏时显示的退出按钮 -->
  90. <button v-if="isFullScreen" class="exit-button" @click="exitFullScreen">X</button>
  91. <MindMap
  92. v-if="show_mind_map"
  93. ref="mind_map"
  94. :content="mindMap_content"
  95. :box_style="box_style"
  96. :svg_style="svg_style"
  97. @extend="extendMindMap"
  98. ></MindMap>
  99. <div class="mind_map_error" v-if="mindMap_err.length">{{ mindMap_err }}</div>
  100. <div class="conversation-chat-mind-map-tools">
  101. <ConversationChatMindMapTools
  102. :isFullScreen="isFullScreen"
  103. @toZoomIn="zoomIn"
  104. @toZoomOut="zoomOut"
  105. @toZoomReset="zoomReset"
  106. @toDownload="downloadMap"
  107. @toFullScreen="toFullScreen"
  108. @toExitFullScreen="exitFullScreen"
  109. ></ConversationChatMindMapTools>
  110. </div>
  111. </div>
  112. <div class="conversation-chat-mind-map-spin" v-if="loading_mind_map">
  113. <a-spin tip="加载中..."></a-spin>
  114. </div>
  115. <div class="conversation-chat-web-page" v-if="show_web_page">
  116. <ConversationChatWebPage :search_list="search_list" @toClose="closeWebPage"></ConversationChatWebPage>
  117. </div>
  118. </div>
  119. <div class="conversation-chat-expand" v-if="show_word_viewer">
  120. <ConversationChatWordViewer :edit_txt="edit_txt_value" @close="closeChatWord"></ConversationChatWordViewer>
  121. </div>
  122. </div>
  123. </div>
  124. <ChatAsideExpandBtn v-if="show_right_page_btn" @toggleRight="toggleRight"></ChatAsideExpandBtn>
  125. </div>
  126. </template>
  127. <script setup>
  128. import { ref, onMounted, onUnmounted, watch, computed, nextTick } from 'vue';
  129. import { useI18n } from 'vue-i18n';
  130. import { marked } from 'marked';
  131. // import { Message } from '@arco-design/web-vue';
  132. import { Message } from '../../3rd-libs/arco-vue-libs';
  133. import { jsonStringify } from '../../utils/utils';
  134. import DialogHeader from '../../modules/conversation-chat/DialogHeader.vue';
  135. import DialogWelcome from '../../modules/conversation-chat/DialogWelcome.vue';
  136. import DialogList from '../../modules/conversation-chat/DialogList.vue';
  137. import DialogInput from '../../modules/conversation-chat/DialogInput.vue';
  138. // import DialogInputAttachedAttrs from '../../modules/conversation-chat/DialogInputAttachedAttrs.vue';
  139. import DialogInputTop from '../../modules/conversation-chat/DialogInputTop.vue';
  140. import DialogFooter from '../../modules/conversation-chat/DialogFooter.vue';
  141. import ChatAsideExpandBtn from '../../modules/conversation-aside/ChatAsideExpandBtn.vue';
  142. import MindMap from '../../components/MindMap.vue';
  143. import ConversationChatMindMapTools from '../../modules/conversation-aside/Conversation-chat-mind-tools.vue';
  144. import ConversationChatWebPage from '../../modules/conversation-aside/Conversation-chat-web-page.vue';
  145. import DialogCommonUsed from '../../modules/conversation-chat/DialogCommonUsed.vue';
  146. import DialogStopSse from '../../modules/conversation-chat/DialogStopSse.vue';
  147. import useWebSocketChat from '../../modules/conversation-dialog/use-websocket-chat';
  148. import useFetchEventSource from '../../modules/conversation-dialog/use-sse-chat';
  149. import {
  150. getHistoryLogs,
  151. getDialogChatId,
  152. uploadFileByAgentInfo,
  153. uploadNewChatInfo,
  154. getMindMapData,
  155. getMapById
  156. } from '../../modules/conversation-dialog/api-dialog';
  157. import formatDialogSendOptions from '../../modules/conversation-dialog/format-dialog-send-options';
  158. import formatDialogSendSseOptions from '../../modules/conversation-dialog/format-dialog-send-sse-options';
  159. import formatDialogHistory from '../../modules/conversation-dialog/format-dialog-history';
  160. import formatDialogQuestionByClient from '../../modules/conversation-dialog/format-dialog-question-by-client';
  161. import formatDialogAnswerForLoading from '../../modules/conversation-dialog/format-dialog-answer-for-loading';
  162. import formatDialogAnswerForException from '../../modules/conversation-dialog/format-dialog-answer-for-exception';
  163. import formatDialogAnswerFromWebsocketReceiveMessage from '../../modules/conversation-dialog/format-dialog-answer-from-websocket_receive_message';
  164. import formatDialogAnswerFromSseReceiveMessage from '../../modules/conversation-dialog/format-dialog-answer-from-Sse_receive_message';
  165. import ConversationChatWordViewer from './ConversationChatWordViewer.vue';
  166. import useEleSizeV1 from '../../utils/use-ele-size-v1';
  167. import useInputClear from './use-input-clear';
  168. import useInputGetOptions from './use-input-get-options';
  169. import useScroll2Bottom from './use-scroll-bottom';
  170. import { IconEye } from '@arco-design/web-vue/es/icon';
  171. import { file, files } from 'jszip';
  172. import { getFileType } from '../../modules/form-dynamic/file-accept';
  173. const props = defineProps({
  174. /**
  175. * 智能体的ID
  176. * 此参数新对话中不存在
  177. */
  178. agent_id: String,
  179. /**
  180. * 智能体的一些关键信息
  181. */
  182. CHAT_AGENT_INFOMATION: Object,
  183. /**
  184. * 会话ID
  185. * 如果有此参数,则意味着获取该对话的记录内容
  186. */
  187. session_id: String,
  188. chat_history: String,
  189. /**
  190. * 动态表单数据
  191. * 根据智能体的agent_id 获取到动态表单数据
  192. * */
  193. conversation_params: Object
  194. });
  195. const emit = defineEmits(['updateSessionId']);
  196. const i18n = useI18n();
  197. /**
  198. * input的一些操作方法
  199. */
  200. const inputClearFn1 = useInputClear('dialog_input'); // 主表单清空
  201. const inputClearFn2 = useInputClear('dialog_input_top'); // 顶部附加表单的数据
  202. const getInputTopOptions = useInputGetOptions('dialog_input_top'); // 获取附加表单参数的方法
  203. /**
  204. * 对话区域滚动到底部
  205. */
  206. const scroll2Bottom = useScroll2Bottom('list_scroller');
  207. const scroll2ListBottom = () => {
  208. nextTick(() => {
  209. scroll2Bottom();
  210. });
  211. };
  212. /**
  213. * 整个对话区域的一些尺寸定义
  214. */
  215. const header_height = 30;
  216. const footer_height = 30;
  217. const input_height = ref(100);
  218. /**
  219. * 获取到对话区域的宽度和高度
  220. */
  221. const { width: chat_width, height: chat_height } = useEleSizeV1('chat_scrren_container');
  222. /**
  223. * 对话列表区域的高度
  224. */
  225. const list_height = computed(() => {
  226. return chat_height.value - header_height - footer_height - input_height.value - 20;
  227. });
  228. const input_style = computed(() => {
  229. return {
  230. // height: input_height + 'px'
  231. };
  232. });
  233. const style_header = computed(() => {
  234. return { height: header_height + 'px' };
  235. });
  236. const style_body = computed(() => {
  237. return { height: chat_height - 30 - 30 + 'px' };
  238. });
  239. const style_footer = computed(() => {
  240. return { height: footer_height + 'px' };
  241. });
  242. const style_conversation_chat_content = computed(() => {
  243. if (is_show_right_page.value || show_word_viewer.value) {
  244. return {
  245. width: 55 + '%'
  246. };
  247. } else {
  248. return {
  249. width: 100 + '%'
  250. };
  251. }
  252. });
  253. const style_conversation_chat_right = computed(() => {
  254. if (is_show_right_page.value || show_word_viewer.value) {
  255. return {
  256. width: 35 + '%',
  257. transition: 'width 0.2s ease'
  258. };
  259. } else {
  260. return {
  261. width: 0,
  262. transition: 'width 0.2s ease'
  263. };
  264. }
  265. });
  266. const style_conversation_chat_expand = computed(() => {
  267. if (loading_mind_map.value) {
  268. return {
  269. position: 'relative'
  270. };
  271. } else {
  272. return {
  273. position: ''
  274. };
  275. }
  276. });
  277. const style_conversation_chat_main_container = computed(() => {
  278. if (!show_welcome.value) {
  279. return {
  280. height: 80 + '%',
  281. transition: 'all 0.2s ease'
  282. };
  283. } else {
  284. return {
  285. height: 20 + '%',
  286. transition: 'all 0.2s ease'
  287. };
  288. }
  289. });
  290. const style_conversation_chat_list = computed(() => {
  291. if (is_show_right_page.value || show_word_viewer.value) {
  292. return {
  293. paddingRight: 3 + '%',
  294. paddingLeft: 3 + '%',
  295. boxSizing: 'border-box'
  296. };
  297. } else {
  298. return {
  299. paddingRight: 20 + '%',
  300. paddingLeft: 20 + '%',
  301. boxSizing: 'border-box'
  302. };
  303. }
  304. });
  305. // ------------------------------------------------------------------
  306. // ------------------------------------------------------------------
  307. // ------------------------------------------------------------------
  308. /**
  309. * 对话数据列表
  310. */
  311. const chat_log_list = ref([]); // 历史对话的数据列表
  312. const chat_new_list = ref([]); // 新对话的数据列表
  313. const chat_suggestion = ref([]); // 对话推荐项目
  314. /**
  315. * 当前对话使用的ID
  316. * 当用户有输入后,点击提交,此时创建新对话,新对话的ID为此ID
  317. * 之后用户可以一直使用此ID进行对话。
  318. */
  319. const chat_round_id = ref(null);
  320. /**
  321. * 对话发起的次数记录
  322. * 用户每发起一次会话,此值会加1input_disable
  323. */
  324. const chat_round_sent_count = ref(0);
  325. /**
  326. * 新对话,客户端的提问数据
  327. * 用以临时保存客户端的提问数据
  328. * 每当用户确认进行提问时,将新提问赋给此值
  329. * 一旦对话消息通过websocket发送到服务器,此值可清空
  330. */
  331. const chat_new_question = ref({});
  332. /**
  333. * 界面展示的对话列表
  334. * 当用户从历史会话进入时,历史会话记录有数据,当用户基于历史会话进行对话时,新会话也有数据,
  335. * 当用户从agent进入时,仅有新会话,没有历史会话
  336. *
  337. * 页面渲染的会话列表是 历史会话 + 新会话数据
  338. */
  339. const chat_display_list = computed(() => {
  340. return [].concat(chat_log_list.value, chat_new_list.value);
  341. });
  342. /**
  343. * 是否禁用输入组件
  344. */
  345. const input_disabled = ref(false);
  346. /**
  347. * 是否显示停止按钮
  348. * */
  349. const show_stop_sse = ref(false);
  350. /**
  351. * 菜单ID编号
  352. * 1 报表合并
  353. * 2 知识问答
  354. * 3 文档智能
  355. * 4 智能问答
  356. * 5 智能数据
  357. * 6 小数绘图
  358. * 7 文档出卷
  359. * 8 出题组卷
  360. * 9 文档报告
  361. */
  362. const current_menu_id = computed(() => {
  363. const agent = props.CHAT_AGENT_INFOMATION.agent;
  364. const menu_id = agent.menuId / 1;
  365. return menu_id;
  366. });
  367. const current_agent_name = computed(() => {
  368. const agent = props.CHAT_AGENT_INFOMATION.agent;
  369. const name = agent.name || '新会话';
  370. return name;
  371. });
  372. const current_agent_mode = computed(() => {
  373. const agent = props.CHAT_AGENT_INFOMATION.agent;
  374. const mode = agent.mode;
  375. return mode;
  376. });
  377. /**
  378. * 表单区域:是否显示附加参数表单
  379. *
  380. * 根据动态表单返回的数据中user_input_form中是否有数据来控制是否显示附加参数表单
  381. */
  382. const show_input_others_opts = computed(() => {
  383. if (
  384. props.conversation_params &&
  385. props.conversation_params.user_input_form &&
  386. props.conversation_params.user_input_form.length
  387. ) {
  388. return true;
  389. }
  390. return false;
  391. // const menu_id = current_menu_id.value;
  392. // /**
  393. // * 7 文档出卷
  394. // * 9 文档报告
  395. // */
  396. // if (menu_id === 7 || menu_id === 9) {
  397. // return true;
  398. // }
  399. // return false;
  400. });
  401. /**
  402. * 是否支持文本输入
  403. * 返回 true,输入区域将不存在文本输入框
  404. * 返回 false,输入区域将可以进行文本输入
  405. *
  406. *通过conversation_params.retriever_resource.enabled来控制是否支持文本输入
  407. */
  408. const enable_text_input = computed(() => {
  409. let enabled = false;
  410. if (props.conversation_params && props.conversation_params.retriever_resource) {
  411. enabled = props.conversation_params.retriever_resource.enabled;
  412. }
  413. return enabled;
  414. });
  415. /**
  416. * 对话文本是否必须
  417. * 此值如果为 true,则在发起对话之前,会进行 文本的存在性检查
  418. * 此值如果为 false,则在发起对话之前,不会进行 文件存在性检查
  419. */
  420. const is_chat_text_required = computed(() => {
  421. const is_enable = enable_text_input.value;
  422. if (!is_enable) {
  423. return false;
  424. }
  425. return true;
  426. });
  427. /**
  428. * 默认的文本值
  429. */
  430. const default_text_value = computed(() => {
  431. const menu_id = current_menu_id.value;
  432. /**
  433. * 某些情况下,无需输入文本
  434. * 但是在对话部分,需要展示用户的提问,此时可以给一个默认的文本值
  435. * 例如在报表合并情况下,仅需要上传文件,不需要输入文字
  436. */
  437. if (menu_id === 1) {
  438. return '合并Excel';
  439. }
  440. return '';
  441. });
  442. /**
  443. * 表单区域:是否允许文件上传
  444. * 返回 ture,输入区域将会有文件上传按钮
  445. * 返回 false,输入区域将没有文件上传按钮
  446. *
  447. * 根据 conversation_params.file_upload.enabled来控制是否允许文件上传
  448. */
  449. const enable_file_upload = computed(() => {
  450. let enabled = false;
  451. if (props.conversation_params && props.conversation_params.file_upload) {
  452. enabled = props.conversation_params.file_upload.enabled;
  453. }
  454. return enabled;
  455. });
  456. /**
  457. * 对话文件是否必须上传
  458. * 此值如果返回 true,则 在对话时,会进行文件检查
  459. * 此值如果返回 false,则 在对话时,不会检查文件的存在性
  460. */
  461. const is_chat_file_required = computed(() => {
  462. const is_enable = enable_file_upload.value;
  463. if (!is_enable) {
  464. return false;
  465. }
  466. const menu_id = current_menu_id.value;
  467. // 报表合并: 文件必须
  468. if (menu_id === 1) {
  469. // 当第一次会话时,要求必须上传文件
  470. // 非第一次,不做要求
  471. if (chat_round_sent_count.value === 0) {
  472. return true;
  473. }
  474. }
  475. // 文档出卷:文件必须上传
  476. if (menu_id === 7) {
  477. // 当第一次对话时,必须上传文件,
  478. // 非第一次,不做要求
  479. if (chat_round_sent_count.value === 0) {
  480. return true;
  481. }
  482. }
  483. return false;
  484. });
  485. /**
  486. * 此值在 enable_file_upload === true 的前提下有效
  487. * 表单区域:文件上传允许的文件类型
  488. * 对应表单参数的 accept
  489. */
  490. const file_accept = computed(() => {
  491. const menu_id = current_menu_id.value;
  492. /**
  493. * 1 报表合并:上传几个文档,进行合并 允许 .xlsx
  494. * 3 文档智能:上传文档,让智能体进行解析 允许: .pdf/.docx
  495. * 5 智能数据:上传Excel,生成饼状图 允许 .xlsx
  496. * 6 小数绘图:上传图片,描述图片的内容 允许 .png/jpg/jpeg
  497. * 9 文档报告: 允许 .docx
  498. */
  499. if (menu_id === 1) {
  500. return '.xlsx';
  501. }
  502. if (menu_id === 3) {
  503. /**
  504. * 2025-2-8 16:50
  505. * 王永新和赵源在测试deepseek时发现 文档智能 能上传的文件类型有限
  506. * 要求将accept调整为和dify的 文档 类型一致
  507. */
  508. // return '.pdf,.docx';
  509. return '.txt,.md,.markdown,.pdf,.html,.xlsx,.xls,.docx,.csv,.eml,.msg,.pptx,.ppt,.xml,.epub';
  510. }
  511. // 智能数据
  512. if (menu_id === 5) {
  513. return '.xlsx,.xls,.csv';
  514. }
  515. if (menu_id === 6) {
  516. return '.jpg,.jpeg,.png';
  517. }
  518. if (menu_id === 9) {
  519. return '.docx';
  520. }
  521. return '*';
  522. });
  523. /**
  524. * 答案可用的操作按钮
  525. * 可用的值有 [] 或者 ['copy']
  526. * 后续可能有新值添加
  527. */
  528. const answer_action_list = computed(() => {
  529. const menu_id = current_menu_id.value;
  530. // 报表合并无操作按钮
  531. if (menu_id === 1) {
  532. return [];
  533. }
  534. return ['copy', 'retry', 'edit', 'download'];
  535. });
  536. /**
  537. * 控制对话列表滚动条展示
  538. * 当右侧有页面打开,隐藏滚动条
  539. *
  540. */
  541. const show_right = computed(() => {
  542. return is_show_right_page.value || show_word_viewer.value;
  543. });
  544. /**
  545. * 内容分享操作
  546. * @param chat_id
  547. */
  548. const handleChatShare = (chat_id) => {};
  549. const show_word_viewer = ref(false);
  550. // 要编辑的文本内容
  551. const edit_txt_value = ref('');
  552. const closeChatWord = () => {
  553. show_word_viewer.value = false;
  554. };
  555. // 更新输入框中的文本
  556. const ask_input_text = ref('');
  557. //
  558. const updatAskTxt = (txt) => {
  559. ask_input_text.value = txt;
  560. };
  561. /**
  562. * 编辑文本
  563. * */
  564. const handleEditChat = (item) => {
  565. // edit_txt_value.value = marked(item.answer || '');
  566. edit_txt_value.value = item.answer;
  567. is_show_right_page.value = false;
  568. show_word_viewer.value = true;
  569. };
  570. const retry_action_option = ref(null);
  571. const handleActionConfigChange = (config) => {
  572. retry_action_option.value = config;
  573. };
  574. /**
  575. * 重新生成
  576. * */
  577. const handleRetryChat = async (item) => {
  578. //根据message_id查找对应的问题
  579. const question_idx = chat_display_list.value.findIndex(
  580. (it) => it.client_custom_chat_message_id === item.client_custom_chat_message_id
  581. );
  582. if (question_idx != -1) {
  583. const question_item = chat_display_list.value[question_idx - 1];
  584. let chatMode = retry_action_option.value?.chatMode || 1;
  585. let isDeep = retry_action_option.value?.isDeep || 1;
  586. if (question_item.client_custom_upload_file_list?.length) {
  587. chatMode = 1;
  588. }
  589. if (question_item.client_custom_knowledge_id?.length) {
  590. chatMode = 3;
  591. }
  592. const options = {
  593. files: question_item.client_custom_upload_file_list || [],
  594. text: question_item.question,
  595. config: {
  596. knowledgeId: question_item.client_custom_knowledge_id || [],
  597. isDeep,
  598. chatMode
  599. }
  600. };
  601. toSubmitNewChat(options);
  602. }
  603. };
  604. /**
  605. * 在聊天界面点击推荐项
  606. * 继续对话
  607. */
  608. const handleSuggestSubmit = (suggest) => {};
  609. /**
  610. * 处理页面中的错误消息
  611. * @param msg
  612. */
  613. const handleErrorMessage = (msg) => {
  614. Message.error({ content: msg, position: 'top' });
  615. };
  616. // ---------------------------------------------
  617. /**
  618. * 异常处理
  619. * 异常处理过程是一个会话模拟的过程
  620. * 先在界面上显示加载动画,然后紧接着显示错误提示
  621. * 让用户感知到智能体有处理数据的过程
  622. * 所以在异常处理阶段,有一些需要处理的关键点,比如欢迎界面的显示(如果在显示,则需要隐藏,否则对话显示不了)
  623. * @param exceptionInfo
  624. */
  625. const handleChatRequestionException = (exceptionInfo) => {
  626. // 已结束
  627. const answerData = formatDialogAnswerForException(props.CHAT_AGENT_INFOMATION, exceptionInfo);
  628. // 原来的最后一项弹出
  629. chat_new_list.value.pop();
  630. // 添加新的最后一项
  631. chat_new_list.value.push(answerData);
  632. // 对话已结束:文件上传、文本输入区域放开使用
  633. input_disabled.value = false;
  634. // 对话区域滚动到底部
  635. scroll2ListBottom();
  636. };
  637. /**
  638. * 输入区域的尺寸发生了变化
  639. * 整个对话区域的尺寸也需要依据此变化而变化
  640. * @param size
  641. */
  642. const handleInputSizeChange = (size) => {
  643. const input_width = size.width;
  644. // const input_height = size.height;
  645. setTimeout(() => {
  646. input_height.value = size.height;
  647. });
  648. };
  649. /**
  650. * 是否显示欢迎页
  651. * 当用户进入对话页面,还未正式开始对话时
  652. * 对话区域会显示欢迎内容
  653. * 当用户开始对话时,欢迎内容消失,对话列表开始展示
  654. */
  655. const show_welcome = ref(false);
  656. const mindMap_str = ref('');
  657. /**
  658. * mindMap组件对象
  659. *
  660. * */
  661. const mind_map = ref();
  662. /**
  663. * mindMap 显示的内容
  664. *
  665. * */
  666. const mindMap_content = ref('');
  667. const mindMap_err = ref('');
  668. /**
  669. * 图谱的查询 文本字段
  670. * */
  671. const mindMap_txt = ref('');
  672. const minMap_messageId = ref('');
  673. const fullScreen_mindMap = ref(null);
  674. const isFullScreen = ref(false);
  675. /**
  676. * mindMap 盒子的尺寸
  677. * */
  678. const box_style = computed(() => {
  679. if (is_show_right_page.value) {
  680. return {
  681. width: '100%',
  682. height: 'calc(100% - 30px)'
  683. };
  684. }
  685. });
  686. /**
  687. * mindMap 导图实际尺寸的尺寸
  688. * */
  689. const svg_style = computed(() => {
  690. if (is_show_right_page.value) {
  691. return {
  692. width: '100%',
  693. height: '100%'
  694. };
  695. }
  696. });
  697. /**
  698. * mindMap 展开按钮事件
  699. * node:当前node数据
  700. * full:全部node数据
  701. * el:当前点击的元素
  702. * */
  703. const extendMindMap = async (item) => {
  704. const { node, full, el } = item;
  705. mindMap_txt.value = decodeHtmlCharCodes(node.content);
  706. getMindMapData({ messageId: minMap_messageId.value, query: mindMap_txt.value }).then((res) => {
  707. if (res.code / 1 === 200) {
  708. mindMap_str.value = res.data.mindmap;
  709. mindMap_content.value = res.data.mindmap;
  710. } else {
  711. mindMap_content.value = mindMap_str.value;
  712. }
  713. });
  714. };
  715. // 转码函数 展开节点用到
  716. function decodeHtmlCharCodes(str) {
  717. const textarea = document.createElement('textarea');
  718. textarea.innerHTML = str;
  719. return textarea.value;
  720. }
  721. const is_show_right_page = ref(false); //控制右侧页面展示显示
  722. const show_mind_map = ref(false); //控制右侧思维导图是否显示
  723. const loading_mind_map = ref(false); //初始控制思维导图父盒子显示加载中
  724. const show_web_page = ref(false); //控制网页搜索展示
  725. // 关闭网页搜索
  726. const closeWebPage = () => {
  727. show_web_page.value = false;
  728. is_show_right_page.value = false;
  729. };
  730. // 展示右侧页面按钮
  731. const show_right_page_btn = computed(() => {
  732. return !show_word_viewer.value && !show_welcome.value;
  733. }); //控制右侧顶部按钮开关
  734. /**
  735. * 控制右侧显示/隐藏
  736. * */
  737. const toggleRight = () => {
  738. is_show_right_page.value = !is_show_right_page.value;
  739. if (is_show_right_page.value) {
  740. mindMap_content.value = mindMap_str.value;
  741. setTimeout(() => {
  742. show_mind_map.value = true;
  743. }, 200);
  744. show_web_page.value = true;
  745. } else {
  746. show_mind_map.value = false;
  747. show_web_page.value = false;
  748. }
  749. };
  750. /**
  751. * 放大
  752. * */
  753. const zoomIn = () => {
  754. mind_map.value.zoomIn();
  755. };
  756. /**
  757. * 缩小
  758. * */
  759. const zoomOut = () => {
  760. mind_map.value.zoomOut();
  761. };
  762. /**
  763. * 自适应
  764. * */
  765. const zoomReset = () => {
  766. mind_map.value.zoomReset();
  767. };
  768. const original_obj = ref({
  769. height: '',
  770. width: '',
  771. position: '',
  772. zIndex: ''
  773. }); // 用来存储原始高度
  774. /**
  775. * 全屏
  776. * */
  777. const toFullScreen = () => {
  778. if (isFullScreen.value) {
  779. return;
  780. }
  781. // 动态设置div高度为屏幕可见高度(排除浏览器头部)
  782. if (fullScreen_mindMap.value) {
  783. // 存储原始高度,进入全屏时恢复
  784. original_obj.value.height = fullScreen_mindMap.value.style.height;
  785. original_obj.value.width = fullScreen_mindMap.value.style.width;
  786. original_obj.value.position = fullScreen_mindMap.value.style.position;
  787. original_obj.value.zIndex = fullScreen_mindMap.value.style.zIndex;
  788. // 设置div高度为视口高度,避免覆盖浏览器头部
  789. fullScreen_mindMap.value.style.height = window.innerHeight - 90 + 'px';
  790. fullScreen_mindMap.value.style.width = window.innerWidth - 10 + 'px';
  791. fullScreen_mindMap.value.style.position = 'absolute';
  792. fullScreen_mindMap.value.style.top = '0';
  793. fullScreen_mindMap.value.style.left = '0';
  794. fullScreen_mindMap.value.style.zIndex = '9995';
  795. isFullScreen.value = true;
  796. zoomReset();
  797. }
  798. };
  799. // 退出全屏模式的函数
  800. const exitFullScreen = () => {
  801. // 恢复原始高度
  802. if (fullScreen_mindMap.value) {
  803. fullScreen_mindMap.value.style.height = original_obj.value.height;
  804. fullScreen_mindMap.value.style.width = original_obj.value.width;
  805. fullScreen_mindMap.value.style.position = original_obj.value.position;
  806. fullScreen_mindMap.value.style.zIndex = original_obj.value.zIndex;
  807. isFullScreen.value = false;
  808. zoomReset();
  809. }
  810. };
  811. /**
  812. * 下载
  813. * */
  814. const downloadMap = () => {
  815. mind_map.value.downloadPNG();
  816. };
  817. /**
  818. * 打开欢迎页面
  819. * */
  820. const toShowWelcome = () => {
  821. toNewChat();
  822. show_welcome.value = true;
  823. chat_display_list.value = [];
  824. chat_log_list.value = [];
  825. chat_new_list.value = [];
  826. is_show_right_page.value = false;
  827. };
  828. const toNewChat = () => {
  829. if (is_init_model.value) {
  830. pushInitAnswer();
  831. }
  832. is_init_model.value = false;
  833. chat_display_list.value = [];
  834. if (!show_welcome.value) {
  835. return;
  836. }
  837. show_welcome.value = false;
  838. pushInitAnswer();
  839. };
  840. const pushInitAnswer = () => {
  841. chat_log_list.value.push({
  842. answer: 'Hi,我是Smart~ <br> 很高兴遇见你!你可以随时把网址或文件发给我,我来帮你看看',
  843. client_custom_chat_status: '2',
  844. client_custom_chat_type: '0',
  845. client_custom_item_type: 'a',
  846. client_custom_item_think: false, // 是否有思考
  847. client_custom_item_think_content: '', //思考内容
  848. client_custom_item_internet: false, //是否联网
  849. client_custom_item_internet_list: [], //联网内容
  850. client_custom_chat_message_id: '' //会话的message_id
  851. });
  852. };
  853. /**
  854. * 智能体中区分是首次进去展示动态表单,还是点击按钮进去展示动态表单
  855. * */
  856. const is_init_model = ref(false);
  857. const initModel = (val) => {
  858. is_init_model.value = val;
  859. };
  860. const target_action_config = ref({});
  861. /**
  862. * 初始化sse相关方法
  863. * */
  864. const { startStream, stopStream } = useFetchEventSource();
  865. /**
  866. * 输入已经确认要提交了
  867. *
  868. * 1. 创建会话 get-chat-id
  869. * 2. 文件上传 如果有文件,则需要文件上传,没有文件,则不需要文件上传
  870. * 3. 连接socket,socket 发送文本与文件数据内容
  871. * 4. 接收 socket 返回的数据项
  872. */
  873. const handleInputSubmit = async (options) => {
  874. show_welcome.value = false;
  875. if (!chat_display_list.value.length) {
  876. pushInitAnswer();
  877. }
  878. if (!props.agent_id) {
  879. toSubmitNewChat(options);
  880. return;
  881. }
  882. let ask_text = options.text; // 文本
  883. const ask_files = options.files; // 文件列表
  884. const action_config = options.config; //是否深度思考,是否联网,是否选择知识库
  885. target_action_config.value = {};
  886. // 用户选定的文件数量
  887. const file_total = ask_files.length;
  888. const is_text_required = is_chat_text_required.value;
  889. const is_file_required = is_chat_file_required.value;
  890. // 检查对话文字是否必须
  891. if (is_text_required) {
  892. if (!ask_text) {
  893. const msg = i18n.t('conversation.please_input_text');
  894. handleErrorMessage(msg);
  895. return;
  896. }
  897. } else {
  898. ask_text = default_text_value.value;
  899. }
  900. // 检查对话文件是否必须
  901. if (is_file_required) {
  902. if (file_total === 0) {
  903. const msg = i18n.t('conversation.please_upload_file');
  904. handleErrorMessage(msg);
  905. return;
  906. }
  907. }
  908. // --------------------------------------------
  909. // --------------------------------------------
  910. // --------------------------------------------
  911. // --------------------------------------------
  912. // --------------------------------------------消息发送前的准备工作
  913. // 保存附加参数数据,由于下方有一个 inputClearFn2() 方法的执行
  914. // 会将所有数据清空,故附件参数需要在清理之前保存
  915. const other_options = getInputTopOptions();
  916. /**
  917. * 当新消息发送时
  918. * 意味着新一轮对话的开始
  919. * 此时应该将之前一轮对话的信息进行清空
  920. *
  921. * 整理客户端提问的数据
  922. * 组装Question数据
  923. */
  924. const client_question = formatDialogQuestionByClient(ask_text, ask_files);
  925. // 保存用户的提问信息
  926. // 当消息正式发出后,将提问数据放入列表中
  927. chat_new_question.value = client_question;
  928. // 消息即将发送,记录用户的提问数据
  929. chat_new_list.value.push(chat_new_question.value);
  930. // 客户端答案数据(loading状态),客户端模拟的答案数据,依据此数据展示加载中的动画
  931. const answerLoadingMsg = formatDialogAnswerForLoading(props.CHAT_AGENT_INFOMATION);
  932. chat_new_list.value.push(answerLoadingMsg);
  933. /**
  934. * 会话已确认发送
  935. * 隐藏欢迎内容,显示对话内容
  936. */
  937. show_welcome.value = false;
  938. // 清空文本数据和文件数据,避免下一次会话冲突
  939. // 清空表单里的数据
  940. inputClearFn1();
  941. inputClearFn2();
  942. // 清空可能存在的推荐项目
  943. chat_suggestion.value = [];
  944. // 清空已保存的提问数据(提问数据已经推入数组,此处无需继续保存)
  945. chat_new_question.value = null;
  946. // 文件上传、文本输入区域禁用
  947. input_disabled.value = true;
  948. // 对话区域滚动到底部
  949. scroll2ListBottom();
  950. // --------------------------------------------
  951. // --------------------------------------------
  952. // --------------------------------------------
  953. // --------------------------------------------
  954. // --------------------------------------------
  955. const agent_id = props.agent_id;
  956. // 对话分为三个步骤
  957. // 1. 创建会话,如果已经创建,则不需要再次创建
  958. // 2. 上传文件,如果不存在文件,则无需文件上传
  959. // 3. 连接websocket
  960. /**
  961. * 创建会话 get chat id
  962. * 判断对话是否已经创建
  963. * 如果对话已经创建,则使用已经创建的对话
  964. * 否则,创建新对话
  965. */
  966. if (chat_round_id.value === null) {
  967. const session_id = props.session_id;
  968. if (session_id) {
  969. // 已有会话ID,使用此会话ID
  970. chat_round_id.value = session_id;
  971. } else {
  972. // 无会话ID,则是首页会话
  973. if (!agent_id) {
  974. return;
  975. }
  976. // 未创建chat,开始创建
  977. try {
  978. const chatResult = await getDialogChatId(agent_id);
  979. if (chatResult.code / 1 === 200) {
  980. // 成功
  981. const res_data = chatResult.data;
  982. chat_round_id.value = res_data.chat_id;
  983. } else {
  984. // 失败
  985. // 处理异常:直接在界面上显示
  986. handleChatRequestionException({
  987. message: `code: ${chatResult.code} message: ${chatResult.msg}`
  988. });
  989. return;
  990. }
  991. } catch (err) {
  992. console.log('err-nfsdnakiuwirea7', err);
  993. // 处理错误
  994. handleChatRequestionException({
  995. message: err.message
  996. });
  997. return;
  998. }
  999. }
  1000. } else {
  1001. // 已创建chat,使用已创建的会话ID,进入下一步流程
  1002. }
  1003. /**
  1004. * 查看动态表单中是否有文件需要上传,如果有,则需要先上传文件
  1005. * */
  1006. if (other_options && other_options.files) {
  1007. const files_obj = other_options.files;
  1008. const chat_id = chat_round_id.value;
  1009. const menu_id = current_menu_id.value;
  1010. for (const key in files_obj) {
  1011. const upload_files = files_obj[key].map((item) => item.file);
  1012. const uploadResult = await uploadFileByAgentInfo(agent_id, chat_id, upload_files, menu_id);
  1013. if (uploadResult && uploadResult.length) {
  1014. files_obj[key] = files_obj[key].map((file) => {
  1015. const uploadedFile = uploadResult.find((uploaded) => uploaded.name === file.name);
  1016. // 如果找到了匹配的文件,则回填 id
  1017. if (uploadedFile) {
  1018. file.upload_file_id = uploadedFile.id;
  1019. file.url = '';
  1020. }
  1021. return file;
  1022. });
  1023. // 判断如果对应的文件是单文件上传,则显示为对象
  1024. if (files_obj[key].some((item) => item.limit_1)) {
  1025. files_obj[key] = files_obj[key][0];
  1026. }
  1027. }
  1028. }
  1029. other_options.options = {
  1030. ...other_options.options,
  1031. ...files_obj
  1032. };
  1033. }
  1034. // 文件上传后的信息
  1035. let files_response = null;
  1036. /**
  1037. * 如果存在文件数据,则进行文件上传
  1038. */
  1039. if (file_total !== 0) {
  1040. // 进行文件上传,获取文件信息
  1041. const chat_id = chat_round_id.value;
  1042. if (agent_id) {
  1043. try {
  1044. const menu_id = current_menu_id.value;
  1045. const uploadResult = await uploadFileByAgentInfo(agent_id, chat_id, ask_files, menu_id);
  1046. if (uploadResult.length) {
  1047. files_response = uploadResult.map((item) => {
  1048. return {
  1049. ...item,
  1050. url: '',
  1051. type: getFileType(item),
  1052. transfer_method: 'local_file',
  1053. upload_file_id: item.id
  1054. };
  1055. });
  1056. }
  1057. // if (uploadResult.code / 1 === 200) {
  1058. // // 文件上传成功
  1059. // files_response = uploadResult.data;
  1060. // } else {
  1061. // // 文件上传异常
  1062. // handleChatRequestionException({
  1063. // message: `code: ${uploadResult.code} \n\n message: ${uploadResult.msg}`
  1064. // });
  1065. // return;
  1066. // }
  1067. } catch (err) {
  1068. console.log('err-fhskafhakfjslkfjl', err);
  1069. handleChatRequestionException({
  1070. message: err.message
  1071. });
  1072. return;
  1073. }
  1074. } else {
  1075. }
  1076. }
  1077. // TODO: 重新组装sse参数,调用sse接口问答
  1078. const query_params = {
  1079. agent_id,
  1080. mode: current_agent_mode.value, //智能体类型
  1081. query: ask_text,
  1082. files: files_response || [],
  1083. inputs: other_options ? other_options.options : [],
  1084. session_id: props.session_id || ''
  1085. };
  1086. startStream(query_params, sseOnMessage);
  1087. return;
  1088. };
  1089. // 首页对话
  1090. const toSubmitNewChat = async (options) => {
  1091. let ask_text = options.text; // 文本
  1092. const ask_files = options.files; // 文件列表
  1093. const action_config = options.config; //是否深度思考,是否联网,是否选择知识库
  1094. target_action_config.value = options.config;
  1095. let parent_id = '';
  1096. if (chat_display_list.value.length) {
  1097. const lastItem = chat_display_list.value[chat_display_list.value.length - 1];
  1098. if (lastItem.parent_id) {
  1099. parent_id = lastItem.parent_id;
  1100. }
  1101. }
  1102. mindMap_txt.value = options.text;
  1103. // 用户选定的文件数量
  1104. const file_total = ask_files.length;
  1105. if (!ask_text) {
  1106. const msg = i18n.t('conversation.please_input_text');
  1107. handleErrorMessage(msg);
  1108. return;
  1109. }
  1110. const other_options = getInputTopOptions();
  1111. const knowledgeIdList = target_action_config.value?.knowledgeId || [];
  1112. const client_question = formatDialogQuestionByClient(ask_text, ask_files, knowledgeIdList);
  1113. // 保存用户的提问信息
  1114. // 当消息正式发出后,将提问数据放入列表中
  1115. chat_new_question.value = client_question;
  1116. // 消息即将发送,记录用户的提问数据
  1117. chat_new_list.value.push(chat_new_question.value);
  1118. // 客户端答案数据(loading状态),客户端模拟的答案数据,依据此数据展示加载中的动画
  1119. const answerLoadingMsg = formatDialogAnswerForLoading(props.CHAT_AGENT_INFOMATION);
  1120. chat_new_list.value.push(answerLoadingMsg);
  1121. inputClearFn1();
  1122. inputClearFn2();
  1123. // 清空可能存在的推荐项目
  1124. chat_suggestion.value = [];
  1125. // 清空已保存的提问数据(提问数据已经推入数组,此处无需继续保存)
  1126. chat_new_question.value = null;
  1127. // 文件上传、文本输入区域禁用
  1128. input_disabled.value = true;
  1129. // 对话区域滚动到底部
  1130. scroll2ListBottom();
  1131. // 文件上传后的信息
  1132. let files_response = null;
  1133. // 判断是否有要上传的文件,如果有上传文件,否则组合参数进行对话
  1134. if (file_total !== 0) {
  1135. try {
  1136. const uploadResult = await uploadNewChatInfo(1, ask_files);
  1137. files_response = uploadResult.map((item) => {
  1138. return {
  1139. ...item,
  1140. url: '',
  1141. type: getFileType(item),
  1142. transfer_method: 'local_file',
  1143. upload_file_id: item.id
  1144. };
  1145. });
  1146. } catch (e) {
  1147. handleChatRequestionException({
  1148. message: e.message
  1149. });
  1150. }
  1151. }
  1152. // 组合参数
  1153. const query_params = {
  1154. query: ask_text,
  1155. files: files_response,
  1156. session_id: props.session_id || '',
  1157. knowledgeId: action_config.knowledgeId,
  1158. chatMode: action_config.chatMode,
  1159. isDeep: action_config.isDeep,
  1160. parentId: parent_id
  1161. };
  1162. startStream(query_params, sseOnMessage);
  1163. return;
  1164. };
  1165. /**
  1166. * 网页数据
  1167. * */
  1168. const search_list = ref([]);
  1169. const toSearchPage = (item) => {
  1170. search_list.value = [...item.client_custom_item_internet_list];
  1171. minMap_messageId.value = item.client_custom_chat_message_id;
  1172. if (!is_show_right_page.value) {
  1173. is_show_right_page.value = true;
  1174. // if (!mindMap_content.value) {
  1175. // mindMap_err.value = '暂无内容';
  1176. // }
  1177. toShowMindMap(item);
  1178. }
  1179. if (!show_web_page.value) {
  1180. show_web_page.value = true;
  1181. }
  1182. };
  1183. const toShowMindMap = async (item) => {
  1184. const res = await getHistoryMapData(item.client_custom_chat_message_id);
  1185. if (res.code / 1 === 200) {
  1186. if (res.data.mindmap) {
  1187. mindMap_content.value = res.data.mindmap;
  1188. mindMap_err.value = '';
  1189. } else {
  1190. (mindMap_content.value = ''), (mindMap_err.value = '暂无内容');
  1191. }
  1192. } else {
  1193. (mindMap_content.value = ''), (mindMap_err.value = '暂无内容');
  1194. }
  1195. is_show_right_page.value = true;
  1196. minMap_messageId.value = item.client_custom_chat_message_id;
  1197. setTimeout(() => {
  1198. show_mind_map.value = true;
  1199. }, 200);
  1200. };
  1201. /**
  1202. * sseOnMessage处理函数
  1203. * */
  1204. const sseOnMessage = (ev) => {
  1205. // return;
  1206. // sse流返回的数据
  1207. const res_data = JSON.parse(ev.data);
  1208. if (res_data.session_id) {
  1209. emit('updateSessionId', res_data.session_id);
  1210. }
  1211. if (res_data.message_id) {
  1212. minMap_messageId.value = res_data.message_id;
  1213. }
  1214. console.log(res_data);
  1215. //sse流式结束
  1216. if (res_data.event === 'message_end') {
  1217. stopStream();
  1218. if (props.agent_id) {
  1219. return;
  1220. } else {
  1221. is_show_right_page.value = true;
  1222. loading_mind_map.value = true;
  1223. getMindMapData({ messageId: minMap_messageId.value, query: mindMap_txt.value })
  1224. .then((res) => {
  1225. if (res.code / 1 === 200) {
  1226. mindMap_str.value = res.data.mindmap;
  1227. mindMap_content.value = res.data.mindmap;
  1228. mindMap_err.value = '';
  1229. if (!res.data.mindmap) {
  1230. mindMap_err.value = '文本内容太少,不足以生成思维导图,请重新尝试';
  1231. }
  1232. } else {
  1233. mindMap_err.value = '文本内容太少,不足以生成思维导图,请重新尝试';
  1234. }
  1235. })
  1236. .catch((e) => {
  1237. mindMap_err.value = '文本内容太少,不足以生成思维导图,请重新尝试';
  1238. })
  1239. .finally(() => {
  1240. loading_mind_map.value = false;
  1241. setTimeout(() => {
  1242. show_mind_map.value = true;
  1243. }, 200);
  1244. });
  1245. }
  1246. }
  1247. if (res_data.event === 'node_finished' && res_data.data.title && res_data.data.title === 'SearXNG 搜索') {
  1248. if (res_data.data.outputs?.json) {
  1249. search_list.value = res_data.data.outputs.json;
  1250. if (search_list.value.length) {
  1251. show_web_page.value = true;
  1252. }
  1253. }
  1254. }
  1255. if (res_data.event === 'error' || res_data.message === 'error') {
  1256. //sse错误,显示错误信息
  1257. handleChatRequestionException({
  1258. message: res_data.error
  1259. });
  1260. return;
  1261. }
  1262. // TODO: 将返回数据填充到页面
  1263. // 获取当前对话中最后一个对话数据
  1264. // 最后一个对话数据为当前正在进行的对话
  1265. const last_msg = chat_new_list.value[chat_new_list.value.length - 1];
  1266. /**
  1267. * 要混入的回答参数
  1268. * client_custom_item_think: false, // 是否有思考
  1269. * client_custom_item_think_content: '', //思考内容
  1270. * client_custom_item_internet: false, //是否联网
  1271. * client_custom_item_internet_list: [], //联网内容
  1272. * client_custom_chat_message_id: '' //会话的message_id
  1273. *
  1274. */
  1275. if (Object.keys(target_action_config.value)) {
  1276. res_data.isDeep = target_action_config.value.isDeep / 1 === 2 ? true : false;
  1277. res_data.isInternet = target_action_config.value.chatMode / 1 === 2 ? true : false;
  1278. }
  1279. /**
  1280. * 整理返回的数据
  1281. *
  1282. */
  1283. const currentChat = formatDialogAnswerFromSseReceiveMessage(res_data, last_msg, props.CHAT_AGENT_INFOMATION);
  1284. const chat_status = currentChat.status;
  1285. // 处理对话数据
  1286. const handleChatData = () => {
  1287. const chat_data = currentChat.data; // 对话相关的数据项
  1288. // 原来的最后一项弹出
  1289. chat_new_list.value.pop();
  1290. // 添加新的最后一项
  1291. chat_new_list.value.push(chat_data);
  1292. // 滚动到底部
  1293. scroll2ListBottom();
  1294. };
  1295. if (chat_status === 0) {
  1296. // 未开始: 此状态不存在,无需处理
  1297. }
  1298. if (chat_status === 1) {
  1299. // 此轮对话仍然在进行中
  1300. handleChatData();
  1301. show_stop_sse.value = true;
  1302. }
  1303. if (chat_status === 2) {
  1304. // 此轮对话已结束
  1305. handleChatData();
  1306. // 对话已结束:检查是否存在推荐数据
  1307. const chat_suggest = currentChat.suggest; // 对话相关的推荐数据
  1308. if (chat_suggest) {
  1309. chat_suggestion.value = chat_suggest;
  1310. }
  1311. // 对话已结束:文件上传、文本输入区域放开使用
  1312. input_disabled.value = false;
  1313. show_stop_sse.value = false;
  1314. }
  1315. if (chat_status === 3) {
  1316. // 此轮对话出错了
  1317. // TODO: 错误处理
  1318. // 此轮对话已结束
  1319. handleChatData();
  1320. // 对话已结束:文件上传、文本输入区域放开使用
  1321. input_disabled.value = false;
  1322. show_stop_sse.value = false;
  1323. }
  1324. };
  1325. // 停止响应
  1326. const stopResponse = () => {
  1327. stopStream();
  1328. show_stop_sse.value = false;
  1329. input_disabled.value = false;
  1330. // 停止后修改最后一项的状态为已结束
  1331. const lastItem = chat_new_list.value.pop();
  1332. lastItem.client_custom_chat_status = 2;
  1333. chat_new_list.value.push(lastItem);
  1334. scroll2ListBottom();
  1335. };
  1336. watch(
  1337. () => props.agent_id,
  1338. () => {
  1339. if (props.agent_id) {
  1340. show_welcome.value = false;
  1341. }
  1342. }
  1343. );
  1344. watch(
  1345. () => props.session_id,
  1346. () => {
  1347. if (props.session_id) {
  1348. /**
  1349. * 当页面中有记录,则不调用历史记录
  1350. * 防止在新会话中每次问答后更新session_id去调用历史记录,
  1351. * */
  1352. if (chat_display_list.value.length) {
  1353. return;
  1354. }
  1355. // 有历史记录:获取历史记录内容进行展示
  1356. getHistoryLogs(props.session_id)
  1357. .then(async (res) => {
  1358. if (res.code / 1 === 200) {
  1359. const res_data = res.data;
  1360. // 成功
  1361. const session_list = res_data.message;
  1362. // for (let key in session_list) {
  1363. // if (session_list[key].role == 'assistant') {
  1364. // const res = await getHistoryMapData(session_list[key].id);
  1365. // if (res.code / 1 === 200) {
  1366. // session_list[key].mindMap = res.data.mindmap;
  1367. // } else {
  1368. // session_list[key].mindMap = '';
  1369. // }
  1370. // }
  1371. // }
  1372. // 将列表数据进行转化
  1373. const list = formatDialogHistory(session_list, props.CHAT_AGENT_INFOMATION);
  1374. // 如果历史记录不为空
  1375. // 则需要展示对话内容
  1376. // 此时欢迎内容就不需要展示
  1377. if (list.length !== 0) {
  1378. show_welcome.value = false;
  1379. // 滚动到底部
  1380. scroll2ListBottom();
  1381. }
  1382. chat_log_list.value = list;
  1383. } else {
  1384. // 异常
  1385. // TODO:处理异常
  1386. show_welcome.value = false;
  1387. // handleChatRequestionException({
  1388. // message: res.msg
  1389. // });
  1390. // 使用消息提示,而非界面错误提示
  1391. handleErrorMessage('历史会话异常:' + res.msg);
  1392. }
  1393. })
  1394. .catch((err) => {
  1395. console.log('err-mfsjakfjsdkalfjs', err);
  1396. show_welcome.value = false;
  1397. // handleChatRequestionException({
  1398. // message: err.message
  1399. // });
  1400. handleErrorMessage('历史会话错误:' + err.message);
  1401. });
  1402. } else {
  1403. // 无历史记录:等待创建会话内容(用户开始输入、提交完成),列表区域显示欢迎界面
  1404. }
  1405. }
  1406. );
  1407. const getHistoryMapData = async (message_id) => {
  1408. const res = await getMapById(message_id);
  1409. return res;
  1410. };
  1411. onMounted(() => {
  1412. const agent_id = props.agent_id;
  1413. const session_id = props.session_id;
  1414. /**
  1415. * 如果选择智能体,则不显示欢迎页面
  1416. *
  1417. */
  1418. if (props.chat_history) {
  1419. show_welcome.value = false;
  1420. console.log(11111111111111111111);
  1421. } else {
  1422. if (!agent_id && !session_id) {
  1423. show_welcome.value = true;
  1424. } else {
  1425. show_welcome.value = false;
  1426. }
  1427. }
  1428. // 组件挂载
  1429. if (session_id) {
  1430. // 有历史记录:获取历史记录内容进行展示
  1431. getHistoryLogs(session_id)
  1432. .then(async (res) => {
  1433. if (res.code / 1 === 200) {
  1434. const res_data = res.data;
  1435. // 成功
  1436. const session_list = res_data.message;
  1437. // for (let key in session_list) {
  1438. // if (session_list[key].role == 'assistant') {
  1439. // const res = await getHistoryMapData(session_list[key].id);
  1440. // if (res.code / 1 === 200) {
  1441. // session_list[key].mindMap = res.data.mindMap;
  1442. // } else {
  1443. // session_list[key].mindMap = '';
  1444. // }
  1445. // }
  1446. // }
  1447. // 将列表数据进行转化
  1448. const list = formatDialogHistory(session_list, props.CHAT_AGENT_INFOMATION);
  1449. // 如果历史记录不为空
  1450. // 则需要展示对话内容
  1451. // 此时欢迎内容就不需要展示
  1452. if (list.length !== 0) {
  1453. show_welcome.value = false;
  1454. // 滚动到底部
  1455. scroll2ListBottom();
  1456. }
  1457. chat_log_list.value = list;
  1458. } else {
  1459. // 异常
  1460. // TODO:处理异常
  1461. show_welcome.value = false;
  1462. // handleChatRequestionException({
  1463. // message: res.msg
  1464. // });
  1465. // 使用消息提示,而非界面错误提示
  1466. handleErrorMessage('历史会话异常:' + res.msg);
  1467. }
  1468. })
  1469. .catch((err) => {
  1470. console.log('err-mfsjakfjsdkalfjs', err);
  1471. show_welcome.value = false;
  1472. // handleChatRequestionException({
  1473. // message: err.message
  1474. // });
  1475. handleErrorMessage('历史会话错误:' + err.message);
  1476. });
  1477. } else {
  1478. // 无历史记录:等待创建会话内容(用户开始输入、提交完成),列表区域显示欢迎界面
  1479. }
  1480. });
  1481. // ---------------------------------------------------------
  1482. onUnmounted(() => {
  1483. // 组件卸载,关闭组件内的websocket连接
  1484. // closeWSConnect();
  1485. });
  1486. defineExpose({
  1487. toShowWelcome,
  1488. toNewChat
  1489. });
  1490. </script>
  1491. <style lang="css" scoped>
  1492. .conversation-chat-container {
  1493. position: relative;
  1494. display: flex;
  1495. justify-content: center;
  1496. margin-left: auto;
  1497. margin-right: auto;
  1498. height: calc(100% - 52px);
  1499. padding-top: 52px;
  1500. }
  1501. .conversation-chat-left {
  1502. width: 25%;
  1503. flex-shrink: 0;
  1504. }
  1505. .conversation-chat-right {
  1506. width: 25%;
  1507. flex-shrink: 0;
  1508. }
  1509. .conversation-chat-main {
  1510. flex-grow: 1;
  1511. width: 100%;
  1512. height: 100%;
  1513. /* max-width: 960px; */
  1514. margin-left: auto;
  1515. margin-right: auto;
  1516. display: flex;
  1517. /* flex-direction: column; */
  1518. flex-direction: row;
  1519. /* gap: 5%; */
  1520. /* background-color: #aaa; */
  1521. }
  1522. .conversation-chat-main-left {
  1523. /* flex-grow: 1; */
  1524. /* flex-basis: 10%; */
  1525. width: 10%;
  1526. flex-direction: column;
  1527. overflow: hidden;
  1528. height: 100%;
  1529. }
  1530. .conversation-chat-main-content {
  1531. /* flex-grow: 5; */
  1532. /* flex-basis: 50%; */
  1533. flex-direction: column;
  1534. overflow: hidden;
  1535. margin: 0 auto;
  1536. height: 100%;
  1537. }
  1538. .conversation-chat-main-right {
  1539. /* flex-grow: 4; */
  1540. /* flex-basis: 40%; */
  1541. flex-direction: column;
  1542. overflow: hidden;
  1543. height: 100%;
  1544. text-align: right;
  1545. }
  1546. .conversation-chat-expand {
  1547. text-align: right;
  1548. height: 100%;
  1549. width: 100%;
  1550. }
  1551. .conversation-chat-mind-map {
  1552. position: relative;
  1553. /* width: 100%; */
  1554. height: 330px;
  1555. padding: 5px;
  1556. border-radius: 12px;
  1557. background-color: var(--color-bg-1);
  1558. }
  1559. .conversation-chat-mind-map-spin {
  1560. position: absolute;
  1561. left: 0;
  1562. top: 0;
  1563. width: 100%;
  1564. height: 330px;
  1565. padding: 5px;
  1566. border-radius: 12px;
  1567. background-color: var(--color-bg-1);
  1568. display: flex;
  1569. align-items: center;
  1570. justify-content: center;
  1571. }
  1572. .mind_map_error {
  1573. position: absolute;
  1574. left: 0;
  1575. top: 0;
  1576. width: 100%;
  1577. height: 330px;
  1578. display: flex;
  1579. align-items: center;
  1580. justify-content: center;
  1581. background-color: var(--color-bg-1);
  1582. }
  1583. .exit-button {
  1584. position: absolute;
  1585. right: 10px;
  1586. top: 10px;
  1587. border: none;
  1588. cursor: pointer;
  1589. z-index: 9999;
  1590. }
  1591. .conversation-chat-mind-map-tools {
  1592. text-align: right;
  1593. }
  1594. .conversation-chat-web-page {
  1595. margin-top: 20px;
  1596. height: calc(100% - 380px);
  1597. padding: 10px;
  1598. border-radius: 12px;
  1599. background-color: var(--color-bg-1);
  1600. }
  1601. .conversation-chat-content {
  1602. flex-grow: 1;
  1603. height: calc(100% - 60px);
  1604. display: flex;
  1605. flex-direction: column;
  1606. overflow: hidden;
  1607. padding-top: 8px;
  1608. }
  1609. .conversation-chat-main-container {
  1610. height: 80%;
  1611. /* flex-grow: 1; */
  1612. }
  1613. .conversation-chat-area {
  1614. flex-grow: 1;
  1615. overflow: auto;
  1616. /* max-width: 960px; */
  1617. }
  1618. .history-list-loading {
  1619. display: flex;
  1620. justify-content: space-between;
  1621. align-items: center;
  1622. min-height: 300px;
  1623. width: 100%;
  1624. }
  1625. .history-list-loading-container {
  1626. display: flex;
  1627. justify-content: center;
  1628. align-items: center;
  1629. }
  1630. .conversation-input-attached {
  1631. flex-shrink: 0;
  1632. width: 100%;
  1633. margin-left: auto;
  1634. margin-right: auto;
  1635. /* margin-top: 5px; */
  1636. margin-bottom: -10px;
  1637. padding: 5px;
  1638. border-radius: 10px;
  1639. display: flex;
  1640. }
  1641. .conversation-chat-input {
  1642. flex-shrink: 0;
  1643. width: 100%;
  1644. margin-left: auto;
  1645. margin-right: auto;
  1646. margin-top: 5px;
  1647. margin-bottom: 5px;
  1648. padding: 5px;
  1649. border-radius: 10px;
  1650. /* display: flex; */
  1651. }
  1652. /* .scroll-left {
  1653. width: 15%;
  1654. flex-shrink: 0;
  1655. }
  1656. .scroll-right {
  1657. width: 15%;
  1658. flex-shrink: 0;
  1659. } */
  1660. </style>