DialogAnswer.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <template>
  2. <div class="dialog-item dialog-anwser" :class="anwser_klass">
  3. <BtnSelectItem v-if="select_btn_visible" @change="handleSelectChange" />
  4. <div class="dialog-main answer-main">
  5. <div class="dialog-icon answer-icon">
  6. <!-- 根据智能体显示图标 -->
  7. <!-- <img :src="answer.client_custom_icon" /> -->
  8. <!-- 2024-12-26:薛娜提出,使用设计稿的图标 -->
  9. <!-- 需求详情查看 https://note.youdao.com/ynoteshare/index.html?id=d781d48a6d64bab8451026fe65342a7f&type=note&_time=1735176727116 6.1 -->
  10. <img :src="chat_agent_icon" />
  11. </div>
  12. <div class="dialog-content answer-content">
  13. <div>
  14. <DialogContent
  15. :content="answer_html"
  16. :is_loading="is_content_loading"
  17. :max_width="content_max_width"
  18. :show_think="show_think"
  19. :is_internet_search="is_internet_search"
  20. :internet_search="internet_search"
  21. :is_think="is_think"
  22. :deep_answer="deep_answer"
  23. :deep_loading="deep_loading"
  24. @toSearchPage="toSearchPage"
  25. >
  26. <DialogActions
  27. v-if="answer_status === 2"
  28. class="answer-actions-btns"
  29. :action_list="answer_action_list"
  30. @copy="handleAnswerCopy"
  31. @share="handleAnswerShare"
  32. @retry="handleAnswerRetry"
  33. @edit="handleAnswerEdit"
  34. />
  35. </DialogContent>
  36. <!-- <BtnStopChat v-if="answer_status === 1" @stop="handleStopChat" /> -->
  37. <!-- 对话已经结束,并且有引用数据 -->
  38. <template v-if="has_refference && answer_status === 2">
  39. <RefferenceFileBlocks
  40. class="refference-files-blocks-container"
  41. :doc_aggs="full_refference.doc_aggs"
  42. :chunks="full_refference.chunks"
  43. :total="full_refference.total"
  44. />
  45. </template>
  46. </div>
  47. </div>
  48. </div>
  49. </div>
  50. </template>
  51. <script setup>
  52. /**
  53. * 智能体的回答内容,可能需要包括下面一些功能
  54. *
  55. * 4. 回答反馈:分享、点赞、复制
  56. */
  57. import { computed, onMounted } from 'vue';
  58. import { Message } from '../../3rd-libs/arco-vue-libs';
  59. import BtnSelectItem from './BtnSelectItem.vue';
  60. import DialogActions from './DialogActions.vue';
  61. import BtnStopChat from './BtnStopChat.vue';
  62. import DialogContent from '../conversation-content/DialogContent.vue';
  63. import formatAnwserString from '../conversation-dialog/format-anwser-string';
  64. // import { formatFileDownloadStr } from '../conversation-dialog/format-dialog-answer-custom-markdown-str';
  65. // import { getIconIntelligent } from '../common/icon-map';
  66. // import { formatStaticAssetsUrl } from '../../utils/utils';
  67. import applyRefferencePopover4DownloadFile from '../answer-refference/apply-refference-popover-4-download-file';
  68. import applyRefferencePopover4RefFlag from '../answer-refference/apply-refference-popover-4-ref-flag';
  69. // import applyRefferencePopover4FileName from '../answer-refference/apply-refference-popover-4-filename';
  70. import { applyCodeHighLight } from '../conversation-dialog/format-code-string';
  71. import RefferenceFileBlocks from '../answer-refference/RefferenceFileBlocks.vue';
  72. import copyText from '../../3rd-libs/clipboard';
  73. import chat_agent_icon from '../../assets/chat/chat-agent-icon.png';
  74. const props = defineProps({
  75. /**
  76. * 是否显示选择按钮
  77. * 仿照kimi,当用户选择分享时,选择按钮会展示
  78. */
  79. show_select: true,
  80. /**
  81. * 答案内容数据
  82. */
  83. answer: Object,
  84. /**
  85. * 对话区域的宽度
  86. */
  87. width: Number,
  88. answer_action_list: Array
  89. });
  90. const emit = defineEmits(['stop', 'retry', 'select', 'edit', 'toSearchPage']);
  91. const select_btn_visible = computed(() => {
  92. return props.show_select === true;
  93. });
  94. /**
  95. * 当前对话的状态
  96. * 0 加载中
  97. * 1 正在对话
  98. * 2 已结束
  99. */
  100. const answer_status = computed(() => {
  101. const answer = props.answer;
  102. return answer.client_custom_chat_status;
  103. });
  104. /**
  105. * 内容是否正在加载中
  106. */
  107. const is_content_loading = computed(() => {
  108. return answer_status.value === 0;
  109. });
  110. /**
  111. * 内容区域的最大宽度
  112. * 内容区域的宽度,根据父容器的宽度,减去此组件内非内容区域元素(icon等)的宽度,以及横向的的margin/padding
  113. * 最终得出内容区域的最大宽度
  114. */
  115. const content_max_width = computed(() => {
  116. return props.width - 40 - 40 - 20;
  117. });
  118. const system_message = computed(() => {
  119. const answer = props.answer;
  120. return answer.client_custom_running_msg || '';
  121. });
  122. /**
  123. * 智能体回答的内容的html字符串
  124. * 是markdown格式
  125. * 需要使用markdown解析器进行解析
  126. */
  127. const answer_html = computed(() => {
  128. const answer = props.answer;
  129. const str = answer.content;
  130. /**
  131. * 第一步处理位于:../conversation-dialogformat-dialog-answer-from-websocket_receive_message.js
  132. * 此处将websocket的答案数据进行第二步处理
  133. * 使用markdown解析器进行解析,并处理一部分字符串
  134. * 最终生成html字符串
  135. */
  136. let answer_string = formatAnwserString(str);
  137. // 第二步处理已经结束,html字符串已经生成
  138. // 接下来进行最后一步:附加内容的字符串拼接
  139. // 由于SQL和代码不能通过markdown解析,所以此处进行字符串的拼接
  140. // 在markdown转换结束后,添加拼接字符串
  141. // answer.client_custom_attached_html_before_render 的生成过程位于 ../conversation-dialogformat-dialog-answer-from-websocket_receive_message.js
  142. if (answer.client_custom_attached_html_before_render) {
  143. answer_string = answer_string + answer.client_custom_attached_html_before_render;
  144. }
  145. // // 第二步处理已经结束,html字符串已经生成
  146. // // 接下来进行最后一步:附加内容的字符串拼接
  147. // // 由于SQL和代码不能通过markdown解析,所以此处进行字符串的拼接
  148. // // 在markdown转换结束后,添加拼接字符串
  149. // // 拼接python代码
  150. // if (answer.client_custom_linked_code) {
  151. // answer_string = answer_string + answer.client_custom_linked_code;
  152. // }
  153. // // 拼接SQL代码
  154. // if (answer.client_custom_linked_sql) {
  155. // answer_string = answer_string + answer.client_custom_linked_sql;
  156. // }
  157. // // 拼接生成的图片
  158. // if (answer.client_custom_linked_image) {
  159. // const img_url = formatStaticAssetsUrl(answer.client_custom_linked_image);
  160. // answer_string = answer_string + `<img src="${img_url}" />`;
  161. // }
  162. // // 拼接文件的下载链接
  163. // if (answer.client_custom_linked_download_files) {
  164. // const file_name = answer.client_custom_linked_download_files.file_name;
  165. // const file_url = answer.client_custom_linked_download_files.file_url;
  166. // const download_file_str = formatFileDownloadStr(file_name, file_url);
  167. // answer_string = answer_string + download_file_str;
  168. // }
  169. /**
  170. * 系统消息展示
  171. * 如果有系统消息,则展示系统消息
  172. * 如果没有系统消息,则不用展示
  173. */
  174. const loader_str = `<div class="answer-system-message-content-container">${system_message.value} <span class="answer-system-message-content-loader"></span></div>`;
  175. /**
  176. * 所有步骤已经走完
  177. * 返回最终的html字符串
  178. * 此字符串就是要渲染到界面上的html字符串
  179. */
  180. if (system_message.value) {
  181. return `${answer_string}${loader_str}`;
  182. } else {
  183. return answer_string;
  184. }
  185. });
  186. /**
  187. * 判断是否是联网搜索,
  188. * */
  189. const is_internet_search = computed(() => {
  190. const answer = props.answer;
  191. return answer.client_custom_item_internet;
  192. });
  193. /**
  194. * 联网搜索内容
  195. * */
  196. const internet_search = computed(() => {
  197. const answer = props.answer;
  198. return answer.client_custom_item_internet_list;
  199. });
  200. // 判断是否是推理内容
  201. const is_think = computed(() => {
  202. const answer = props.answer;
  203. return answer.client_custom_item_think;
  204. // return true;
  205. });
  206. // 思考内容
  207. const deep_answer = computed(() => {
  208. const answer = props.answer;
  209. const str = answer.deep_answer;
  210. let answer_string = formatAnwserString(str);
  211. return answer_string;
  212. });
  213. // 思考内容输出状态
  214. const deep_loading = computed(() => {
  215. const answer = props.answer;
  216. return answer.client_custom_chat_deep_loading;
  217. });
  218. /**
  219. * 智能体回答区域容器的class值
  220. * 通过唯一ID生成,可以保证界面中每一个答案都有一个唯一的容器类名
  221. * 为后续其他处理(引用效果、代码美化)做准备:后续处理可以通过此class定位相关子元素
  222. */
  223. const anwser_klass = computed(() => {
  224. const answer = props.answer;
  225. return `klass-${answer.client_custom_unique_id}-answer-content`;
  226. });
  227. /**
  228. * 完整引用数据
  229. */
  230. const full_refference = computed(() => {
  231. if (!props.answer) {
  232. return null;
  233. }
  234. const answer = props.answer;
  235. if (!answer.client_custom_linked_refference_full_content) {
  236. return null;
  237. }
  238. if (Object.keys(answer.client_custom_linked_refference_full_content).length === 0) {
  239. return null;
  240. } else {
  241. for (let key in answer.client_custom_linked_refference_full_content) {
  242. if (answer.client_custom_linked_refference_full_content[key].length === -0) {
  243. return null;
  244. }
  245. }
  246. }
  247. return answer.client_custom_linked_refference_full_content;
  248. });
  249. /**
  250. * 是否显示引用效果
  251. */
  252. const has_refference = computed(() => {
  253. if (!full_refference.value) {
  254. return false;
  255. }
  256. return true;
  257. });
  258. const handleSelectChange = () => {
  259. emit('select');
  260. };
  261. /**
  262. * 停止答案的生成
  263. */
  264. const handleStopChat = () => {
  265. emit('stop');
  266. };
  267. /**
  268. * copy
  269. */
  270. const handleAnswerCopy = () => {
  271. copyText(answer_html.value);
  272. Message.success('已复制');
  273. };
  274. /**
  275. * 分享答案
  276. */
  277. const handleAnswerShare = () => {};
  278. /**
  279. * 重新生成答案
  280. */
  281. const handleAnswerRetry = () => {
  282. emit('retry');
  283. };
  284. /**
  285. * 编辑文本
  286. * */
  287. const handleAnswerEdit = () => {
  288. emit('edit');
  289. };
  290. const toSearchPage = () => {
  291. emit('toSearchPage');
  292. };
  293. onMounted(() => {
  294. const container_klass = anwser_klass.value;
  295. const answer = props.answer;
  296. const full_refference = answer.client_custom_linked_refference_full_content;
  297. const download_files = answer.client_custom_linked_download_files;
  298. const download_file_list = answer.client_custom_linked_download_files_List || [];
  299. /**
  300. * 文档引用效果
  301. */
  302. applyRefferencePopover4RefFlag(container_klass, full_refference);
  303. // applyRefferencePopover4FileName(container_klass, full_refference);
  304. if (download_files) {
  305. applyRefferencePopover4DownloadFile(container_klass, [download_files, ...download_file_list]);
  306. }
  307. /**
  308. * 处理代码样式: 美化代码的展示
  309. */
  310. applyCodeHighLight(container_klass);
  311. });
  312. </script>
  313. <style src="./dialog-item-common.css" />
  314. <style lang="css" scoped>
  315. .dialog-anwser {
  316. color: var(--color-text-1);
  317. display: flex;
  318. justify-content: space-between;
  319. }
  320. .answer-main {
  321. }
  322. .answer-content {
  323. display: flex;
  324. justify-content: flex-start;
  325. }
  326. .answer-icon {
  327. margin-right: 22px;
  328. }
  329. .answer-icon img {
  330. width: 100%;
  331. transform: scale(1.6);
  332. }
  333. .answer-actions-btns {
  334. margin-top: 5px;
  335. }
  336. .refference-files-blocks-container {
  337. margin-top: 26px;
  338. }
  339. .dialog-content.answer-content .dialog-chat-answer-content {
  340. padding-right: 45px;
  341. }
  342. </style>
  343. <style>
  344. .answer-system-message-content-container {
  345. display: flex;
  346. align-items: center;
  347. }
  348. .answer-system-message-content-loader {
  349. display: inline-block;
  350. margin-left: 14px;
  351. width: 6px;
  352. height: 6px;
  353. border-radius: 50%;
  354. background-color: #165dff;
  355. box-shadow: 10px 0 #165dff, -10px 0 #165dff;
  356. position: relative;
  357. animation: flash 0.8s ease-out infinite alternate;
  358. }
  359. @keyframes flash {
  360. 0% {
  361. background-color: #fff2;
  362. box-shadow: 10px 0 #fff2, -10px 0 #165dff;
  363. }
  364. 50% {
  365. background-color: #165dff;
  366. box-shadow: 10px 0 #fff2, -10px 0 #fff2;
  367. }
  368. 100% {
  369. background-color: #fff2;
  370. box-shadow: 10px 0 #165dff, -10px 0 #fff2;
  371. }
  372. }
  373. </style>