DialogAnswer.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. <img :src="answer.client_custom_icon" />
  7. </div>
  8. <div class="dialog-content answer-content">
  9. <div>
  10. <DialogContent :content="answer_html" :is_loading="is_content_loading" :max_width="content_max_width">
  11. <DialogActions
  12. v-if="answer_status === 2"
  13. class="answer-actions-btns"
  14. :action_list="answer_action_list"
  15. @copy="handleAnswerCopy"
  16. @share="handleAnswerShare"
  17. @retry="handleAnswerRetry"
  18. />
  19. </DialogContent>
  20. <BtnStopChat v-if="answer_status === 1" @stop="handleStopChat" />
  21. <!-- 对话已经结束,并且有引用数据 -->
  22. <template v-if="has_refference && answer_status === 2">
  23. <RefferenceFileBlocks
  24. class="refference-files-blocks-container"
  25. :doc_aggs="full_refference.doc_aggs"
  26. :chunks="full_refference.chunks"
  27. :total="full_refference.total"
  28. />
  29. </template>
  30. </div>
  31. </div>
  32. </div>
  33. </div>
  34. </template>
  35. <script setup>
  36. /**
  37. * 智能体的回答内容,可能需要包括下面一些功能
  38. *
  39. * 4. 回答反馈:分享、点赞、复制
  40. */
  41. import { computed, onMounted } from 'vue';
  42. import { Message } from '@arco-design/web-vue';
  43. import BtnSelectItem from './BtnSelectItem.vue';
  44. import DialogActions from './DialogActions.vue';
  45. import BtnStopChat from './BtnStopChat.vue';
  46. import DialogContent from '../conversation-content/DialogContent.vue';
  47. import formatAnwserString from '../conversation-dialog/format-anwser-string';
  48. import { formatFileDownloadStr } from '../conversation-dialog/format-dialog-answer-custom-markdown-str';
  49. import { getIconIntelligent } from '../common/icon-map';
  50. import { formatStaticAssetsUrl } from '../../utils/utils';
  51. import applyRefferencePopover4RefFlag from '../answer-refference/apply-refference-popover-4-ref-flag';
  52. import applyRefferencePopover4FileName from '../answer-refference/apply-refference-popover-4-filename';
  53. import { applyCodeHighLight } from '../conversation-dialog/format-code-string';
  54. import RefferenceFileBlocks from '../answer-refference/RefferenceFileBlocks.vue';
  55. import copyText from '../../3rd-libs/clipboard';
  56. const props = defineProps({
  57. /**
  58. * 是否显示选择按钮
  59. * 仿照kimi,当用户选择分享时,选择按钮会展示
  60. */
  61. show_select: true,
  62. /**
  63. * 答案内容数据
  64. */
  65. answer: Object,
  66. /**
  67. * 对话区域的宽度
  68. */
  69. width: Number,
  70. answer_action_list: Array
  71. });
  72. const emit = defineEmits(['stop', 'retry', 'select']);
  73. const select_btn_visible = computed(() => {
  74. return props.show_select === true;
  75. });
  76. /**
  77. * 当前对话的状态
  78. * 0 加载中
  79. * 1 正在对话
  80. * 2 已结束
  81. */
  82. const answer_status = computed(() => {
  83. const answer = props.answer;
  84. return answer.client_custom_chat_status;
  85. });
  86. /**
  87. * 内容是否正在加载中
  88. */
  89. const is_content_loading = computed(() => {
  90. return answer_status.value === 0;
  91. });
  92. /**
  93. * 内容区域的最大宽度
  94. * 内容区域的宽度,根据父容器的宽度,减去此组件内非内容区域元素(icon等)的宽度,以及横向的的margin/padding
  95. * 最终得出内容区域的最大宽度
  96. */
  97. const content_max_width = computed(() => {
  98. return props.width - 40 - 40 - 20;
  99. });
  100. /**
  101. * 智能体回答的内容的html字符串
  102. * 是markdown格式
  103. * 需要使用markdown解析器进行解析
  104. */
  105. const answer_html = computed(() => {
  106. const answer = props.answer;
  107. const str = answer.content;
  108. /**
  109. * 第一步处理位于:../conversation-dialogformat-dialog-answer-from-websocket_receive_message.js
  110. * 此处将websocket的答案数据进行第二步处理
  111. * 使用markdown解析器进行解析,并处理一部分字符串
  112. * 最终生成html字符串
  113. */
  114. let answer_string = formatAnwserString(str);
  115. // 第二步处理已经结束,html字符串已经生成
  116. // 接下来进行最后一步:附加内容的字符串拼接
  117. // 由于SQL和代码不能通过markdown解析,所以此处进行字符串的拼接
  118. // 在markdown转换结束后,添加拼接字符串
  119. // answer.client_custom_attached_html_before_render 的生成过程位于 ../conversation-dialogformat-dialog-answer-from-websocket_receive_message.js
  120. if (answer.client_custom_attached_html_before_render) {
  121. answer_string = answer_string + answer.client_custom_attached_html_before_render;
  122. }
  123. // // 第二步处理已经结束,html字符串已经生成
  124. // // 接下来进行最后一步:附加内容的字符串拼接
  125. // // 由于SQL和代码不能通过markdown解析,所以此处进行字符串的拼接
  126. // // 在markdown转换结束后,添加拼接字符串
  127. // // 拼接python代码
  128. // if (answer.client_custom_linked_code) {
  129. // answer_string = answer_string + answer.client_custom_linked_code;
  130. // }
  131. // // 拼接SQL代码
  132. // if (answer.client_custom_linked_sql) {
  133. // answer_string = answer_string + answer.client_custom_linked_sql;
  134. // }
  135. // // 拼接生成的图片
  136. // if (answer.client_custom_linked_image) {
  137. // const img_url = formatStaticAssetsUrl(answer.client_custom_linked_image);
  138. // answer_string = answer_string + `<img src="${img_url}" />`;
  139. // }
  140. // // 拼接文件的下载链接
  141. // if (answer.client_custom_linked_download_files) {
  142. // const file_name = answer.client_custom_linked_download_files.file_name;
  143. // const file_url = answer.client_custom_linked_download_files.file_url;
  144. // const download_file_str = formatFileDownloadStr(file_name, file_url);
  145. // answer_string = answer_string + download_file_str;
  146. // }
  147. /**
  148. * 所有步骤已经走完
  149. * 返回最终的html字符串
  150. * 此字符串就是要渲染到界面上的html字符串
  151. */
  152. return answer_string;
  153. });
  154. /**
  155. * 智能体回答区域容器的class值
  156. * 通过唯一ID生成,可以保证界面中每一个答案都有一个唯一的容器类名
  157. * 为后续其他处理(引用效果、代码美化)做准备:后续处理可以通过此class定位相关子元素
  158. */
  159. const anwser_klass = computed(() => {
  160. const answer = props.answer;
  161. return `klass-${answer.client_custom_unique_id}-answer-content`;
  162. });
  163. /**
  164. * 完整引用数据
  165. */
  166. const full_refference = computed(() => {
  167. if (!props.answer) {
  168. return null;
  169. }
  170. const answer = props.answer;
  171. if (!answer.client_custom_linked_refference_full_content) {
  172. return null;
  173. }
  174. if (Object.keys(answer.client_custom_linked_refference_full_content).length === 0) {
  175. return null;
  176. }
  177. return answer.client_custom_linked_refference_full_content;
  178. });
  179. /**
  180. * 是否显示引用效果
  181. */
  182. const has_refference = computed(() => {
  183. if (!full_refference.value) {
  184. return false;
  185. }
  186. return true;
  187. });
  188. const handleSelectChange = () => {
  189. emit('select');
  190. };
  191. /**
  192. * 停止答案的生成
  193. */
  194. const handleStopChat = () => {
  195. emit('stop');
  196. };
  197. /**
  198. * copy
  199. */
  200. const handleAnswerCopy = () => {
  201. copyText(answer_html.value);
  202. Message.success('已复制');
  203. };
  204. /**
  205. * 分享答案
  206. */
  207. const handleAnswerShare = () => {};
  208. /**
  209. * 重新生成答案
  210. */
  211. const handleAnswerRetry = () => {
  212. emit('retry');
  213. };
  214. onMounted(() => {
  215. const container_klass = anwser_klass.value;
  216. const answer = props.answer;
  217. const full_refference = answer.client_custom_linked_refference_full_content;
  218. /**
  219. * 文档引用效果
  220. */
  221. applyRefferencePopover4RefFlag(container_klass, full_refference);
  222. // applyRefferencePopover4FileName(container_klass, full_refference);
  223. /**
  224. * 处理代码样式: 美化代码的展示
  225. */
  226. applyCodeHighLight(container_klass);
  227. });
  228. </script>
  229. <style src="./dialog-item-common.css" />
  230. <style lang="css" scoped>
  231. .dialog-anwser {
  232. color: var(--color-text-1);
  233. display: flex;
  234. justify-content: space-between;
  235. }
  236. .answer-main {
  237. }
  238. .answer-content {
  239. display: flex;
  240. justify-content: flex-start;
  241. }
  242. .answer-icon {
  243. margin-right: 22px;
  244. }
  245. .answer-icon img {
  246. width: 100%;
  247. }
  248. .answer-actions-btns {
  249. margin-top: 5px;
  250. }
  251. .refference-files-blocks-container {
  252. margin-top: 26px;
  253. }
  254. </style>