ChatAgentInput.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. <script setup>
  2. import { ref, unref, onMounted, onUnmounted, computed, watch } from 'vue';
  3. import { useMessage, NInput, NSwitch, NPopover, NScrollbar, NUpload, NTooltip, NProgress } from 'naive-ui';
  4. import { useUserStore } from '@/stores/modules/userStore';
  5. import { baseURL } from '@/utils/request';
  6. import { getFormatYesterDay } from '@/utils/format';
  7. import { helperApi } from '@/api/helper';
  8. import { TheArchival } from "@/components"
  9. import SvgIcon from '@/components/SvgIcon';
  10. import 'load-awesome/css/ball-running-dots.min.css';
  11. const props = defineProps({
  12. activeItem: {
  13. type: Object,
  14. default: () => ({})
  15. }
  16. });
  17. const emit = defineEmits(['onClick', 'onEnter']);
  18. const MAX_NUM = 5;
  19. const useStore = useUserStore();
  20. const modelLoading = defineModel('loading');
  21. const switchStatus = defineModel('switch');
  22. const message = useMessage();
  23. const inpVal = ref('');
  24. const inpRef = ref(null);
  25. const isFocusState = ref(false);
  26. const isOpen = ref(false);
  27. const highlightedIndex = ref(0);
  28. const selectedOption = ref(null);
  29. const helperList = ref([]);
  30. const popoverTriggerRef = ref(null);
  31. const popoverInnerRef = ref(null);
  32. const scrollRef = ref(null);
  33. const uploadFileList = ref([]);
  34. const uploadLoading = ref(false);
  35. const agentOptions = computed(() => helperList.value.filter(({ tools }) => tools));
  36. const lastFileListIndex = computed(() => uploadFileList.value.length == 0 ? 0 : uploadFileList.value.length - 1);
  37. const focusInput = _ => isFocusState.value = true;
  38. const blurInput = _ => isFocusState.value = false;
  39. watch(inpVal, (curVal) => {
  40. if (curVal === "@" && curVal.length === 1) {
  41. if ( !unref(agentOptions).length ) {
  42. return message.warning('当前未配置智能体');
  43. }
  44. if ( modelLoading.value ) {
  45. return message.warning('当前对话进行中');
  46. }
  47. isOpen.value = true;
  48. } else {
  49. isOpen.value = false;
  50. }
  51. })
  52. watch(() => props.activeItem, (curVal) => {
  53. selectedOption.value = curVal?.tools ? curVal : null;
  54. })
  55. const handleInpFocus = () => {
  56. inpRef.value?.focus();
  57. }
  58. const commonEmitEvent = (eventName) => {
  59. const val = unref(inpVal);
  60. const len = val.trim().length;
  61. if ( !len ) {
  62. return message.warning('请输入您的问题或需求');
  63. }
  64. if ( len > 2000 ) {
  65. return message.warning('问题限制2000个字以内');
  66. }
  67. if ( modelLoading.value ) {
  68. return message.warning('当前对话进行中');
  69. }
  70. if ( uploadLoading.value ) {
  71. return message.warning('文件上传中,请稍后');
  72. }
  73. emit(eventName, { showVal: val, question: val, selectedOption: selectedOption.value || {}, uploadFileList: uploadFileList.value });
  74. inpVal.value = '';
  75. uploadFileList.value = [];
  76. }
  77. // 回车事件
  78. const handleInpEnter = (event) => {
  79. if (event.key === 'Enter' && !event.shiftKey && inpVal.value) {
  80. event.preventDefault();
  81. commonEmitEvent('onEnter');
  82. inpRef.value?.blur();
  83. }
  84. }
  85. // 点击事件
  86. const handleBtnClick = () => {
  87. commonEmitEvent("onClick");
  88. }
  89. const clearInpVal = () => {
  90. inpVal.value = '';
  91. }
  92. // 键盘上键无限滚动
  93. const scrollKeyUp = (index) => {
  94. const itemLength = agentOptions.value.length;
  95. if ( itemLength - MAX_NUM > index ) {
  96. scrollRef.value.scrollBy({
  97. top: -40 ,
  98. behavior: 'smooth'
  99. })
  100. }
  101. if ( index == itemLength - 1 ) {
  102. scrollRef.value.scrollTo({
  103. top: itemLength * 40,
  104. behavior: 'smooth'
  105. })
  106. }
  107. }
  108. // 键盘下键无限滚动
  109. const scrollKeyDown = (index) => {
  110. if ( index >= MAX_NUM ) {
  111. scrollRef.value.scrollBy({
  112. top: 40,
  113. behavior: 'smooth'
  114. })
  115. }
  116. if ( index == 0 ) {
  117. scrollRef.value.scrollTo({
  118. top: 0,
  119. behavior: 'smooth'
  120. })
  121. }
  122. }
  123. // 键盘事件
  124. const handleKeyDown = (event) => {
  125. const len = unref(agentOptions).length;
  126. if ( !isOpen.value ) return;
  127. switch (event.key) {
  128. case 'ArrowUp':
  129. event.preventDefault();
  130. highlightedIndex.value = (unref(highlightedIndex) - 1 + len) % len;
  131. scrollKeyUp(unref(highlightedIndex), 1)
  132. break;
  133. case 'ArrowDown':
  134. event.preventDefault();
  135. highlightedIndex.value = (unref(highlightedIndex) + 1) % len;
  136. scrollKeyDown(unref(highlightedIndex))
  137. break;
  138. case 'Enter':
  139. event.preventDefault();
  140. selectOption(unref(highlightedIndex));
  141. break;
  142. default:
  143. break;
  144. }
  145. }
  146. const formatFileItem = (file) => {
  147. const { name } = file;
  148. return {
  149. name: name.substring(0, name.lastIndexOf('.')),
  150. url: "",
  151. size: (file.file.size / 1024).toFixed(2) + "KB",
  152. suffix: name.substring( name.lastIndexOf('.') + 1 ).toUpperCase(),
  153. originSuffix:name.substring( name.lastIndexOf('.') )
  154. }
  155. }
  156. // 处理点击空白处关闭
  157. const closePopoverOutside =(event) => {
  158. if (!isOpen.value) return;
  159. const triggerResult = popoverTriggerRef.value.contains(event.target);
  160. const innerResult = popoverInnerRef.value.contains(event.target);
  161. isOpen.value = triggerResult || innerResult;
  162. }
  163. // 选中选项
  164. const selectOption = (index) => {
  165. selectedOption.value = agentOptions.value[index];
  166. highlightedIndex.value = index;
  167. isOpen.value = false;
  168. inpVal.value = selectedOption.value.content;
  169. }
  170. const beforeUpload = ({ file }) => {
  171. if (( file.file?.size / ( 1024 * 1024 ) ) > 5) {
  172. message.warning("文件大小超过5MB,请重新选择");
  173. return false;
  174. }
  175. const index = unref(lastFileListIndex);
  176. uploadFileList.value[index] = {
  177. ...formatFileItem(file),
  178. percentage: 0
  179. }
  180. uploadLoading.value = true
  181. return true;
  182. }
  183. // 上传文件
  184. const handleUploadChange = ({ file }) => {
  185. if (file.status === 'uploading') {
  186. uploadFileList.value[lastFileListIndex.value].percentage = file.percentage;
  187. }
  188. }
  189. // 文件上传完成
  190. const handleFinish = ({ file, event }) => {
  191. try {
  192. const res = JSON.parse( (event?.target).response );
  193. if ( res.code == 200 ) {
  194. uploadFileList.value[lastFileListIndex.value] = {
  195. ...uploadFileList.value[lastFileListIndex.value],
  196. url: res.data,
  197. }
  198. uploadLoading.value = false
  199. }
  200. } catch (error) {
  201. console.log("上传完成, 但是存在错误", error);
  202. }
  203. }
  204. // 上传失败
  205. const handleUploadError = (error) => {
  206. uploadLoading.value = false;
  207. }
  208. // 删除文件
  209. const onRemoveFile = (i) => {
  210. // uploadFileList.value.splice(i, 1);
  211. uploadFileList.value = [];
  212. }
  213. const clearFileList = () => {
  214. uploadFileList.value = [];
  215. }
  216. onMounted(async () => {
  217. const { data } = await helperApi.getHelperList();
  218. const result = getFormatYesterDay(data)
  219. helperList.value = result;
  220. document.addEventListener('keydown', handleKeyDown);
  221. document.addEventListener('click', closePopoverOutside);
  222. })
  223. onUnmounted(() => {
  224. document.removeEventListener('keydown', handleKeyDown);
  225. document.removeEventListener('click', closePopoverOutside);
  226. })
  227. defineExpose({
  228. clearFileList,
  229. clearInpVal,
  230. handleInpFocus,
  231. inpVal,
  232. })
  233. </script>
  234. <template>
  235. <NPopover
  236. trigger="hover"
  237. width="trigger"
  238. display-directive="show"
  239. content-style="padding: 0;"
  240. :show-arrow="false"
  241. :show="isOpen"
  242. >
  243. <template #trigger>
  244. <div class="popover-trigger" ref="popoverTriggerRef">
  245. <div class="chat-inp-outer border-[1px]" :class="[{ 'border-[#2454FF]': isFocusState }]">
  246. <ul class="chat-tools-inner py-[10px] px-[10px] bg-[#fcfcfc]" v-show="selectedOption">
  247. <li class="tools-tips space-x-[10px]">
  248. <span>与</span>
  249. <p class="agent-name space-x-[5px]" @click="isOpen = true">
  250. <img src="https://static.fuxicarbon.com/userupload/db77ffe0cef843278a23b0d2db9505fa.png" alt="">
  251. <span>{{ selectedOption?.title }}</span>
  252. </p>
  253. <span>对话中</span>
  254. </li>
  255. <li class="tools-close" @click="selectedOption = null">
  256. <SvgIcon name="chat-icon-close-btn"></SvgIcon>
  257. </li>
  258. </ul>
  259. <ul class="file-list-wrapper" v-show="uploadFileList.length">
  260. <li class="file-item space-x-[14px]" v-for="(item, index) in uploadFileList" :key="index">
  261. <div class="file-icon"></div>
  262. <div class="file-info">
  263. <p class="title">{{ item.name }}</p>
  264. <p class="info space-x-[8px]">
  265. <span class="suffix">{{ item.suffix }}</span>
  266. <span class="size">{{ item.size }}</span>
  267. </p>
  268. </div>
  269. <span class="close" @click="onRemoveFile(i)" v-show="item.percentage == 100 && !uploadLoading">x</span>
  270. <div class="file-progress" v-if="uploadLoading">
  271. <NProgress
  272. type="line"
  273. color="#3153f5"
  274. :percentage="item.percentage"
  275. :show-indicator="false"
  276. :height="3"
  277. ></NProgress>
  278. </div>
  279. </li>
  280. </ul>
  281. <div class="chat-inp-inner">
  282. <div class="inp-wrapper flex-1" @click="handleInpFocus">
  283. <div class="upload-inner">
  284. <NUpload
  285. accept=".doc, .docx, .pdf, .txt"
  286. :disabled="uploadLoading"
  287. :show-file-list="false"
  288. :action="baseURL + '/qiniuyun/upLoadImage'"
  289. :headers="{
  290. 'Authorization': 'Bearer' + useStore.token,
  291. }"
  292. @on-error="handleUploadError"
  293. @change="handleUploadChange"
  294. @finish="handleFinish"
  295. @before-upload="beforeUpload"
  296. >
  297. <NTooltip trigger="hover">
  298. <template #trigger>
  299. <div class="upload-file-button"></div>
  300. </template>
  301. <span class="text-[12px]">支持上传文件(每次一个, 大小5MB以内)接受.doc、.docx、.pdf、.txt等格式的文件</span>
  302. </NTooltip>
  303. </NUpload>
  304. </div>
  305. <NInput
  306. class="flex-1"
  307. ref="inpRef"
  308. type="textarea"
  309. size="medium"
  310. placeholder="输入@,召唤智能体"
  311. v-model:value="inpVal"
  312. :autosize="{ minRows: 1, maxRows: 5 }"
  313. @focus="focusInput"
  314. @blur="blurInput"
  315. @keypress="handleInpEnter"
  316. />
  317. </div>
  318. <div class="submit-btn">
  319. <button class="btn bg-[#1A2029] hover:bg-[#3C4148]" @click="handleBtnClick">
  320. <SvgIcon name="tool-send-plane" size="22" v-show="!modelLoading"></SvgIcon>
  321. <div style="color: #fff" class="la-ball-running-dots la-sm" v-show="modelLoading">
  322. <div v-for="item in 5" :key="item"></div>
  323. </div>
  324. </button>
  325. </div>
  326. </div>
  327. </div>
  328. <div class="switch-inner pt-[8px] flex justify-between items-center">
  329. <div class="space-x-[6px]">
  330. <NSwitch size="small" v-model:value="switchStatus"></NSwitch>
  331. <span class="text-[12px] text-[#9E9E9E]">使用deepseek R1</span>
  332. </div>
  333. <div>
  334. <TheArchival></TheArchival>
  335. </div>
  336. </div>
  337. <div class="masking-inner text-center text-[#2454FF]"></div>
  338. </div>
  339. </template>
  340. <div class="popover-inner" ref="popoverInnerRef">
  341. <div class="header">
  342. <span>选择智能体</span>
  343. <p class="tools-close" @click="isOpen = false">
  344. <SvgIcon name="chat-icon-close-btn"></SvgIcon>
  345. </p>
  346. </div>
  347. <NScrollbar style="max-height: 200px;" ref="scrollRef" trigger="none">
  348. <div class="item" v-for="item, index in agentOptions" :class="['item', { active: highlightedIndex === index }]" @click="selectOption(index)">
  349. <p class="icon">
  350. <img :src="item.banner" alt="">
  351. </p>
  352. <p class="ml-[10px] space-x-[5px] text">
  353. <span class="text-[15px]">{{item.title}}</span>
  354. <!-- <span class="text-[#888] text-[14px]">这里可以补充个描述</span> -->
  355. </p>
  356. </div>
  357. </NScrollbar>
  358. </div>
  359. </NPopover>
  360. </template>
  361. <style scoped lang="scss">
  362. .chat-inp-outer {
  363. border-radius: 8px;
  364. overflow: hidden;
  365. box-shadow: 0px 3px 12px 0px #97D3FF40;
  366. .chat-tools-inner {
  367. @include flex(x, center, between);
  368. .tools-tips {
  369. @include flex(x, center, start);
  370. color: #666;
  371. font-size: 14px;
  372. .agent-name {
  373. @include flex(x, center, start);
  374. font-weight: bold;
  375. color: #333;
  376. cursor: pointer;
  377. img {
  378. width: 14px;
  379. height: 14px;
  380. }
  381. }
  382. }
  383. }
  384. .chat-inp-inner {
  385. position: relative;
  386. @include flex(x, center, between);
  387. background: #fff;
  388. .inp-wrapper {
  389. @include flex(x, start, center);
  390. padding: 17px 0px 17px 17px;
  391. .upload-inner {
  392. width: 30px;
  393. height: 30px;
  394. padding-top: 2px;
  395. .upload-file-button {
  396. width: 30px;
  397. height: 30px;
  398. background: url("@/assets/svgs/chat/icon-file-default.svg") center center no-repeat;
  399. cursor: pointer;
  400. &:hover {
  401. background: url("@/assets/svgs/chat/icon-file-active.svg") center center no-repeat;
  402. }
  403. }
  404. }
  405. }
  406. .submit-btn {
  407. @include flex(x, center, center);
  408. width: 84px;
  409. .btn {
  410. @include flex(x, center, center);
  411. width: 50px;
  412. height: 32px;
  413. border-radius: 32px;
  414. transition: all .3s;
  415. }
  416. }
  417. }
  418. .file-list-wrapper {
  419. padding: 10px;
  420. padding-bottom: 0px;
  421. background: #fff;
  422. .file-item {
  423. position: relative;
  424. @include flex(x, center, start);
  425. width: 30%;
  426. // height: 52px;
  427. padding: 10px;
  428. border-radius: 4px;
  429. background: #f5f5f5;
  430. .file-icon {
  431. flex-shrink: 0;
  432. width: 36px;
  433. height: 36px;
  434. background: url("@/assets/svgs/chat/icon-file.svg") center center no-repeat;
  435. }
  436. .file-info {
  437. flex: 1;
  438. @include flex(y, start, start);
  439. font-size: 12px;
  440. .title {
  441. width: 170px;
  442. color: #1a2029;
  443. text-overflow: ellipsis;
  444. overflow: hidden;
  445. word-break: break-all;
  446. white-space: nowrap;
  447. }
  448. .info {
  449. flex: 1;
  450. @include flex(x, center, between);
  451. color: #838a95;
  452. }
  453. }
  454. .file-progress {
  455. position: absolute;
  456. left: 0;
  457. bottom: 0;
  458. width: 100%;
  459. margin: 0;
  460. }
  461. .close {
  462. position: absolute;
  463. top: 0;
  464. right: 0;
  465. width: 16px;
  466. height: 16px;
  467. border: 1px solid #fff;
  468. border-radius: 100%;
  469. transform: translate(40%, -40%);
  470. background: #c8c8c8;
  471. font-size: 10px;
  472. text-align: center;
  473. line-height: 12px;
  474. cursor: pointer;
  475. color: #fff;
  476. &:hover {
  477. background: #b7b7b7;
  478. }
  479. }
  480. }
  481. }
  482. }
  483. .popover-inner {
  484. .header {
  485. @include flex(x, center, between);
  486. padding-bottom: 8px;
  487. font-size: 14px;
  488. color: #666;
  489. }
  490. .item {
  491. @include flex(x, center, start);
  492. padding: 8px 10px;
  493. cursor: pointer;
  494. &:hover {
  495. background: #f0fafe;
  496. }
  497. .icon {
  498. @include flex(x, center, center);
  499. width: 24px;
  500. height: 24px;
  501. border-radius: 100%;
  502. background: #e9eef8;
  503. img {
  504. width: 16px;
  505. height: 16px;
  506. }
  507. }
  508. .text {
  509. text-align: left;
  510. overflow: hidden;
  511. white-space: nowrap;
  512. text-overflow: ellipsis;
  513. }
  514. }
  515. .active {
  516. background: #f0fafe;
  517. }
  518. }
  519. .tools-close {
  520. @include flex(x, center, center);
  521. width: 28px;
  522. height: 28px;
  523. border-radius: 6px;
  524. background: #fff;
  525. cursor: pointer;
  526. &:hover {
  527. background: #e9eef8;
  528. }
  529. }
  530. .masking-inner {
  531. position: absolute;
  532. top: -30px;
  533. left: 0;
  534. width: 100%;
  535. height: 30px;
  536. background: linear-gradient(180deg, rgba(232, 241, 250, 0) 0%, #E7F0FA 95%);
  537. }
  538. </style>