MiniChat.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <!-- eslint-disable vue/no-v-html -->
  2. <template>
  3. <div class="mini-chat">
  4. <!-- 左侧折叠区域 -->
  5. <div class="left-side">
  6. <SvgIcon name="add" size="20" />
  7. <Popover trigger="click" :overlay-inner-style="{ padding: '1px' }">
  8. <template #content>
  9. <AIChat style="width: 700px; height: 500px" :visible="true" />
  10. </template>
  11. <SvgIcon :name="dialogVisible ? 'zoom-out' : 'zoom-in'" size="20" @click="openDialog" />
  12. </Popover>
  13. </div>
  14. <!-- 右侧对话框 -->
  15. <div class="right-side">
  16. <!-- 对话区域 -->
  17. <div ref="dialogRef" class="dialog-area">
  18. <div
  19. v-for="message in store.getMessageHistory"
  20. :key="message.id"
  21. class="flex items-center w-100%"
  22. :style="{ alignSelf: message.type === 'user' ? 'flex-end' : 'flex-start' }"
  23. >
  24. <template v-if="message.type === 'user'">
  25. <div class="flex-grow-1"></div>
  26. <div class="ask-message">{{ message.content }}</div>
  27. </template>
  28. <template v-else>
  29. <SvgIcon size="30" class="ml-2px mr-2px" name="ai-logo" />
  30. <div class="answer-message">
  31. <div v-if="message.contentR1" class="color-gray font-size-12px" v-html="formatMessage(message.contentR1)"> </div>
  32. <div v-html="formatMessage(message.content)"> </div>
  33. </div>
  34. </template>
  35. </div>
  36. </div>
  37. <!-- 底部操作栏 -->
  38. <div class="input-area">
  39. <TextArea v-model:value="inputText" placeholder="请输入你的问题" />
  40. <div class="action-bar">
  41. <!-- 左侧深度思考按钮 -->
  42. <div class="think-btn" :class="{ active: store.deepseekR1Enable }" @click="toggleThinking"> <span>深度思考</span> </div>
  43. <!-- 右侧操作按钮 -->
  44. <Space>
  45. <SvgIcon name="send-image" />
  46. <SvgIcon name="send-file" />
  47. <Button type="primary" shape="circle" size="small" :loading="store.streaming" @click="handleSend">
  48. <template #icon>
  49. <SvgIcon name="send" />
  50. </template>
  51. </Button>
  52. </Space>
  53. </div>
  54. </div>
  55. </div>
  56. </div>
  57. </template>
  58. <script lang="ts" setup>
  59. import { ref, onMounted } from 'vue';
  60. import { SvgIcon } from '../Icon';
  61. import { Space, Button, Popover, Input } from 'ant-design-vue';
  62. import AIChat from './index.vue';
  63. import { useAIChat } from '/@/store/modules/AIChat';
  64. const TextArea = Input.TextArea; // 直接导入TextArea组件使用时打包报错
  65. const dialogRef = ref<HTMLElement | null>(null);
  66. const dialogVisible = ref(true);
  67. const inputText = ref(''); // 输入框内容
  68. const store = useAIChat(); //获取用户信息
  69. const openDialog = () => {
  70. dialogVisible.value = !dialogVisible.value;
  71. };
  72. //启用深度思考
  73. const toggleThinking = () => {
  74. store.deepseekR1Enable = !store.deepseekR1Enable;
  75. };
  76. //获取消息列表
  77. async function handleSend() {
  78. store
  79. .sendQuestion(inputText.value, () => {
  80. if (dialogRef.value) {
  81. dialogRef.value.scrollTop = dialogRef.value.scrollHeight;
  82. }
  83. })
  84. .then(() => {
  85. inputText.value = '';
  86. });
  87. }
  88. //格式化消息
  89. function formatMessage(text: string) {
  90. let formatted = text
  91. // 处理换行
  92. .replace(/\n\n/g, '<br>')
  93. .replace(/\n###/g, '<br> ')
  94. .replace(/###/g, '')
  95. .replace(/---/g, '')
  96. // 处理粗体
  97. .replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
  98. // 处理斜体
  99. .replace(/\*(.*?)\*/g, '<em>$1</em>')
  100. // 处理行内代码
  101. .replace(/`([^`]+)`/g, '<code>$1</code>');
  102. return formatted;
  103. }
  104. // 初始化按钮定位
  105. onMounted(() => {
  106. // store.getSessionHistory();
  107. });
  108. </script>
  109. <style lang="less" scoped>
  110. .mini-chat {
  111. display: flex;
  112. }
  113. .left-side {
  114. width: 40px; /* 折叠时宽度 */
  115. background: #0c2842;
  116. transition: width 0.5s ease; /* 平滑过渡动画 */
  117. display: flex;
  118. flex-direction: column;
  119. justify-content: space-around;
  120. align-items: center;
  121. }
  122. .right-side {
  123. flex: 1; /* 占据剩余空间 */
  124. background: #09172c;
  125. display: flex;
  126. flex-direction: column;
  127. .dialog-area {
  128. flex: 1; /* 占据剩余空间 */
  129. gap: 10px; /* 消息块间隔统一控制 */
  130. overflow-y: auto; /* 垂直滚动条 */
  131. padding: 5px;
  132. display: flex;
  133. flex-direction: column;
  134. color: #fff;
  135. .ask-message {
  136. padding: 10px;
  137. border-radius: 5px;
  138. background: #0c2842;
  139. max-width: 80%;
  140. }
  141. .answer-message {
  142. padding: 10px;
  143. border-radius: 5px;
  144. background: #0c2842;
  145. max-width: 90%;
  146. }
  147. }
  148. .input-area {
  149. background-color: #043256;
  150. padding: 5px;
  151. textarea {
  152. background-color: transparent;
  153. width: 100%;
  154. height: 40px;
  155. border: none;
  156. resize: none;
  157. outline: none;
  158. overflow: hidden;
  159. padding: 10px; /* 统一内边距 */
  160. color: #fff;
  161. }
  162. .action-bar {
  163. height: 30px;
  164. display: flex;
  165. align-items: center;
  166. justify-content: space-between;
  167. .think-btn {
  168. border: 1px solid #ccc;
  169. width: 100px;
  170. height: 20px;
  171. line-height: 20px;
  172. text-align: center;
  173. border-radius: 5px;
  174. cursor: pointer;
  175. background: transparent;
  176. color: white;
  177. transition: background 0.3s;
  178. }
  179. .think-btn.active {
  180. background: #1890ff;
  181. color: white;
  182. border-color: #1890ff;
  183. }
  184. }
  185. }
  186. }
  187. </style>
  188. <style>
  189. .zxm-popover-inner-content {
  190. padding: 1px;
  191. }
  192. </style>