index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. <template>
  2. <div class="flex gap-2">
  3. <div class="h-11 w-11">
  4. <img class="h-full w-full" src="@/assets/images/chat/default-logo.png" />
  5. </div>
  6. <div
  7. class="flex flex-1 flex-col overflow-hidden"
  8. :class="
  9. (item.slot ? '' : 'max-w-fit') +
  10. ' ' +
  11. (item.workflows?.length > 0 ? 'w-full max-w-full' : '')
  12. "
  13. >
  14. <template v-if="item.prologue">
  15. <div
  16. class="answer-markdown w-fit overflow-hidden rounded-lg rounded-tl-none bg-[#EAF1FF] p-2.75 text-[#303133]"
  17. v-html="item.prologue"
  18. />
  19. <template v-if="item.prologueQuestions?.length > 0">
  20. <template v-if="item.prologueNum == 0">
  21. <div class="mt-2 ml-2.5 flex flex-col gap-2">
  22. <template v-for="pq in item.prologueQuestions">
  23. <div
  24. class="__hover w-fit text-sm text-[var(--czr-main-color)]"
  25. @click="$emit('setText', { text: pq, send: true })"
  26. >
  27. {{ pq }}
  28. </div>
  29. </template>
  30. </div>
  31. </template>
  32. <template v-else>
  33. <div class="mt-2 flex flex-wrap gap-x-6 gap-y-1.5">
  34. <template
  35. v-for="pq in item.prologueQuestions.filter(
  36. (v, i) => i < item.prologueNum,
  37. )"
  38. >
  39. <div
  40. class="__hover w-fit rounded-sm bg-[#F6F6F6] px-2.5 py-1 text-sm text-[var(--czr-main-color)]"
  41. @click="$emit('setText', { text: pq, send: true })"
  42. >
  43. {{ pq }}
  44. </div>
  45. </template>
  46. </div>
  47. </template>
  48. </template>
  49. </template>
  50. <template v-else-if="item.loading">
  51. <div class="answer-loading">
  52. <span></span>
  53. <span></span>
  54. <span></span>
  55. </div>
  56. </template>
  57. <template v-else>
  58. <div
  59. class="max-w-full rounded-lg rounded-tl-none bg-[#EAF1FF] p-2.75 text-[#303133]"
  60. :class="item.workflows?.length > 0 ? 'w-full' : 'w-fit'"
  61. >
  62. <template v-if="item.workflows?.length > 0">
  63. <div
  64. class="flex flex-col rounded-sm p-2 text-xs"
  65. :class="
  66. item.workflowStatus === 'succeeded'
  67. ? 'bg-[var(--czr-success-color)]/15'
  68. : item.workflowStatus === 'failed'
  69. ? 'bg-[var(--czr-error-color)]/15'
  70. : 'bg-[#ffffff]/50'
  71. "
  72. >
  73. <div
  74. class="__hover flex items-center"
  75. @click="state.workflowExpend = !state.workflowExpend"
  76. >
  77. <template v-if="item.workflowStatus === 'succeeded'">
  78. <SvgIcon
  79. name="success"
  80. size="14"
  81. color="var(--czr-success-color)"
  82. />
  83. </template>
  84. <template v-else-if="item.workflowStatus === 'failed'">
  85. <SvgIcon
  86. name="failed"
  87. size="14"
  88. color="var(--czr-error-color)"
  89. />
  90. </template>
  91. <template v-else>
  92. <SvgIcon name="wait" size="14" />
  93. </template>
  94. <div class="ml-0.5">工作流</div>
  95. <template v-if="state.workflowExpend">
  96. <SvgIcon
  97. name="czr_arrow"
  98. size="8"
  99. rotate="90"
  100. class="ml-auto"
  101. />
  102. </template>
  103. <template v-else>
  104. <SvgIcon name="czr_arrow" size="8" class="ml-1" />
  105. </template>
  106. </div>
  107. <div class="mt-2 flex flex-col gap-1">
  108. <template v-for="wf in item.workflows">
  109. <div class="rounded-sm bg-[#ffffff] p-1">
  110. <div
  111. class="flex items-center"
  112. :class="wf.status ? '__hover' : 'cursor-no-drop'"
  113. @click="
  114. wf.status ? (wf.__expend = !wf.__expend) : undefined
  115. "
  116. >
  117. <template v-if="wf.__expend">
  118. <SvgIcon name="czr_arrow" size="8" rotate="90" />
  119. </template>
  120. <template v-else>
  121. <SvgIcon name="czr_arrow" size="8" />
  122. </template>
  123. <img
  124. :src="nodeSources[wf.nodeType].icon"
  125. class="ml-1 size-4"
  126. />
  127. <div class="ml-0.5 flex-1 font-bold" v-title>
  128. {{ wf.title }}
  129. </div>
  130. <div class="ml-auto flex items-center gap-1">
  131. <div class="text-[10px]">
  132. <template v-if="wf.tokens">
  133. {{ wf.tokens }} Tokens ·
  134. </template>
  135. {{ formatTimeDuration(wf.elapsedTime) }}
  136. </div>
  137. <template v-if="wf.status === 'succeeded'">
  138. <SvgIcon
  139. name="success"
  140. size="12"
  141. color="var(--czr-success-color)"
  142. />
  143. </template>
  144. <template v-else-if="wf.status === 'failed'">
  145. <SvgIcon
  146. name="failed"
  147. size="12"
  148. color="var(--czr-error-color)"
  149. />
  150. </template>
  151. <template v-else>
  152. <div class="text-[var(--czr-main-color)]/70">
  153. Running
  154. </div>
  155. <SvgIcon name="wait" size="12" />
  156. </template>
  157. </div>
  158. </div>
  159. <div v-if="wf.__expend" class="mt-1 flex flex-col gap-1">
  160. <div
  161. v-if="wf.error"
  162. class="rounded-sm border-1 border-[var(--czr-error-color)]/10 shadow"
  163. >
  164. <div
  165. class="flex items-center justify-between rounded-t-sm bg-[#000000]/10 p-1 text-xs"
  166. >
  167. 错误信息
  168. <el-tooltip content="复制" placement="top">
  169. <SvgIcon
  170. class="__hover"
  171. name="copy"
  172. size="14"
  173. @click="onCopy(wf.error)"
  174. />
  175. </el-tooltip>
  176. </div>
  177. <div
  178. class="bg-[var(--czr-error-color)]/10 p-1 pt-1 break-words"
  179. >
  180. {{ wf.error }}
  181. </div>
  182. </div>
  183. <div
  184. v-if="wf.inputData"
  185. class="rounded-sm border-1 border-[#000000]/5 shadow"
  186. >
  187. <div
  188. class="flex items-center justify-between rounded-t-sm bg-[#000000]/10 p-1 text-xs"
  189. >
  190. 输入
  191. <el-tooltip content="复制" placement="top">
  192. <SvgIcon
  193. class="__hover"
  194. name="copy"
  195. size="14"
  196. @click="onCopy(JSON.stringify(wf.inputData))"
  197. />
  198. </el-tooltip>
  199. </div>
  200. <pre
  201. class="mt-1 p-1 break-words"
  202. style="margin-bottom: 0"
  203. >{{ wf.inputData }}</pre
  204. >
  205. </div>
  206. <div
  207. v-if="wf.outputData"
  208. class="rounded-sm border-1 border-[#000000]/5 shadow"
  209. >
  210. <div
  211. class="flex items-center justify-between rounded-t-sm bg-[#000000]/10 p-1 text-xs"
  212. >
  213. 输出
  214. <el-tooltip content="复制" placement="top">
  215. <SvgIcon
  216. class="__hover"
  217. name="copy"
  218. size="14"
  219. @click="onCopy(JSON.stringify(wf.outputData))"
  220. />
  221. </el-tooltip>
  222. </div>
  223. <pre
  224. class="mt-1 p-1 break-words"
  225. style="margin-bottom: 0"
  226. >{{ wf.outputData }}</pre
  227. >
  228. </div>
  229. </div>
  230. </div>
  231. </template>
  232. </div>
  233. </div>
  234. </template>
  235. <template v-for="part in textCpt">
  236. <template v-if="part.type === 'think'">
  237. <thinkCom :text="part.text" />
  238. </template>
  239. <template v-else>
  240. <div
  241. class="answer-markdown"
  242. :class="{ error: item.error }"
  243. v-html="md.render(part.text.replace(/^<think>/, ''))"
  244. />
  245. </template>
  246. </template>
  247. </div>
  248. <div
  249. class="mt-2 flex items-center gap-2.5 px-2.5 text-[0.63rem] text-[#909399]"
  250. v-if="item.finished && !item.mock"
  251. >
  252. <div v-if="item.time">{{ formatTimeDuration(item.time) }}</div>
  253. <div v-if="item.tokens">{{ item.tokens }} Tokens</div>
  254. <div class="mx-auto" v-if="item.time || item.tokens" />
  255. <template v-if="!item.error && item.messageId">
  256. <template v-if="item.feedback == 1">
  257. <el-tooltip content="取消喜欢" placement="top">
  258. <SvgIcon
  259. class="__hover"
  260. name="good"
  261. size="20"
  262. :active="true"
  263. @click="$emit('onFeedback', item, 0)"
  264. />
  265. </el-tooltip>
  266. </template>
  267. <template v-else-if="item.feedback == 2">
  268. <el-tooltip content="取消不喜欢" placement="top">
  269. <SvgIcon
  270. class="__hover"
  271. name="good"
  272. size="20"
  273. rotate="180"
  274. color="var(--czr-error-color)"
  275. @click="$emit('onFeedback', item, 0)"
  276. />
  277. </el-tooltip>
  278. </template>
  279. <template v-else>
  280. <el-tooltip content="喜欢" placement="top">
  281. <SvgIcon
  282. class="__hover"
  283. name="good"
  284. size="20"
  285. @click="$emit('onFeedback', item, 1)"
  286. />
  287. </el-tooltip>
  288. <el-tooltip content="不喜欢" placement="top">
  289. <SvgIcon
  290. class="__hover"
  291. name="good"
  292. size="20"
  293. rotate="180"
  294. @click="$emit('onFeedback', item, 2)"
  295. />
  296. </el-tooltip>
  297. </template>
  298. </template>
  299. <template
  300. v-if="textCpt.filter((v) => v.type === 'response')[0]?.text"
  301. >
  302. <el-tooltip content="复制" placement="top">
  303. <SvgIcon
  304. class="__hover"
  305. name="copy"
  306. size="24"
  307. @click="
  308. onCopy(textCpt.filter((v) => v.type === 'response')[0]?.text)
  309. "
  310. />
  311. </el-tooltip>
  312. </template>
  313. <el-tooltip content="重新生成" placement="top">
  314. <SvgIcon
  315. class="__hover"
  316. name="refresh"
  317. size="20"
  318. @click="$emit('onReBuild', item)"
  319. />
  320. </el-tooltip>
  321. </div>
  322. <template v-if="item.slot">
  323. <slot :name="item.slot" :props="item.slotProps" />
  324. </template>
  325. <template v-if="item.adviseLoading">
  326. <div class="mt-2 ml-2.5 text-sm text-[var(--czr-main-color)]">
  327. 问题建议生成中……
  328. </div>
  329. </template>
  330. <template v-else-if="item.advise?.length > 0">
  331. <div class="mt-2 ml-2.5 flex flex-col gap-2">
  332. <template v-for="ad in item.advise">
  333. <div
  334. class="__hover w-fit text-sm text-[var(--czr-main-color)]"
  335. @click="$emit('setText', { text: ad, send: true })"
  336. >
  337. {{ ad }}
  338. </div>
  339. </template>
  340. </div>
  341. </template>
  342. </template>
  343. </div>
  344. </div>
  345. </template>
  346. <script setup lang="ts">
  347. import { computed, getCurrentInstance, onMounted, reactive, watch } from 'vue'
  348. import MarkdownIt from 'markdown-it'
  349. import hljs from 'highlight.js'
  350. import { marked } from 'marked'
  351. import DOMPurify from 'dompurify'
  352. import thinkCom from './think.vue'
  353. import { copy } from '@/utils/czr-util'
  354. import { ElMessage } from 'element-plus'
  355. import useTextToSpeech from '../audio/useTextToSpeech'
  356. import { nodeSources } from '@/views/workflow/config'
  357. const { speak, stop } = useTextToSpeech()
  358. const md = new MarkdownIt({
  359. html: false, // 在源码中启用 HTML 标签
  360. highlight: (str, lang) => {
  361. if (lang && hljs.getLanguage(lang)) {
  362. try {
  363. return `<pre class="hljs" style="padding: 10px"><code>${hljs.highlight(str, { language: lang }).value}</code></pre>`
  364. } catch (__) {}
  365. }
  366. return `<pre class="hljs" style="padding: 10px"><code>${md.utils.escapeHtml(str)}</code></pre>`
  367. },
  368. })
  369. const emit = defineEmits(['setText', 'onReBuild', 'onFeedback'])
  370. const props = defineProps({
  371. item: {} as any,
  372. goodMap: {} as any,
  373. badMap: {} as any,
  374. })
  375. const state: any = reactive({
  376. workflowExpend: false,
  377. })
  378. const textCpt = computed(() => {
  379. const segments: any = []
  380. const rawContent = props.item.text
  381. // 正则表达式匹配<think>标签及其内容
  382. const thinkRegex = /<think>([\s\S]*?)<\/think>/g
  383. let match
  384. let lastIndex = 0
  385. while ((match = thinkRegex.exec(rawContent)) !== null) {
  386. // 添加think标签前的普通内容
  387. if (match.index > lastIndex) {
  388. segments.push({
  389. type: 'response',
  390. text: rawContent.substring(lastIndex, match.index),
  391. })
  392. }
  393. // 添加think内容
  394. segments.push({
  395. type: 'think',
  396. text: match[1].trim(),
  397. })
  398. lastIndex = thinkRegex.lastIndex
  399. }
  400. // 添加剩余内容
  401. if (lastIndex < rawContent.length) {
  402. segments.push({
  403. type: 'response',
  404. text: rawContent.substring(lastIndex).trim(),
  405. })
  406. }
  407. return segments
  408. })
  409. const onCopy = (text) => {
  410. copy(text)
  411. ElMessage.success('复制成功!')
  412. }
  413. const formatTimeDuration = (timestamp) => {
  414. if (timestamp < 1000) {
  415. return `${Math.round(timestamp)} ms`
  416. } else if (timestamp < 60000) {
  417. const seconds = timestamp / 1000
  418. return `${seconds.toFixed(1)} s` // 一位小数
  419. } else {
  420. const minutes = Math.floor(timestamp / 60000)
  421. const remainingSeconds = Math.floor((timestamp % 60000) / 1000)
  422. return `${minutes} m ${remainingSeconds} s`
  423. }
  424. }
  425. </script>
  426. <style lang="scss" scoped>
  427. .answer-loading {
  428. width: 50px;
  429. height: 100%;
  430. display: inline-flex;
  431. gap: 6px;
  432. align-items: center;
  433. justify-content: center;
  434. span {
  435. display: inline-block;
  436. width: 8px;
  437. height: 8px;
  438. border-radius: 50%;
  439. background-color: #6c9eff;
  440. animation: ellipsis-bounce 1.4s infinite ease-in-out;
  441. }
  442. span:nth-child(1) {
  443. animation-delay: -0.32s;
  444. }
  445. span:nth-child(2) {
  446. animation-delay: -0.16s;
  447. }
  448. @keyframes ellipsis-bounce {
  449. 0%,
  450. 80%,
  451. 100% {
  452. transform: translateY(4px);
  453. }
  454. 40% {
  455. transform: translateY(-6px);
  456. }
  457. }
  458. }
  459. .answer-markdown {
  460. line-height: 1.5;
  461. &.error {
  462. color: red;
  463. }
  464. > * {
  465. word-break: break-all;
  466. text-align: justify;
  467. line-height: inherit;
  468. margin-bottom: 12px;
  469. &:last-child {
  470. margin-bottom: 0;
  471. }
  472. }
  473. }
  474. </style>