ChatMessages.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <script setup lang="ts">
  2. import { ref, nextTick, watch, computed } from 'vue';
  3. import { Message, Theme } from '../../api/dto';
  4. import { StreamMarkdown } from 'streamdown-vue';
  5. interface Props {
  6. messages: Message[];
  7. isLoading: boolean;
  8. theme?: Theme;
  9. }
  10. const props = defineProps<Props>();
  11. const chatContainer = ref<HTMLElement>();
  12. const isDark = computed(() => props.theme === 'dark');
  13. const shikiTheme = computed(() => (isDark.value ? 'github-dark' : 'github-light'));
  14. async function scrollToBottom() {
  15. await nextTick();
  16. if (chatContainer.value) {
  17. chatContainer.value.scrollTop = chatContainer.value.scrollHeight;
  18. }
  19. }
  20. watch(
  21. () => props.messages,
  22. async () => {
  23. await scrollToBottom();
  24. },
  25. { deep: true },
  26. );
  27. function getAvatar(role: string): string {
  28. const avatars: Record<string, string> = {
  29. user: '👤',
  30. assistant: '🤖',
  31. reason: '💭',
  32. action: '⚡',
  33. error: '❌',
  34. };
  35. return avatars[role] || '🤖';
  36. }
  37. function getRoleLabel(role: string): string {
  38. const labels: Record<string, string> = {
  39. user: '用户',
  40. assistant: '猎影助手',
  41. reason: '思考',
  42. action: '执行',
  43. error: '错误',
  44. };
  45. return labels[role] || role;
  46. }
  47. defineExpose({
  48. scrollToBottom,
  49. });
  50. </script>
  51. <template>
  52. <div class="chat-messages" ref="chatContainer">
  53. <div v-for="message in props.messages" :key="message.id" class="message" :class="message.role">
  54. <div class="message-content">
  55. <div v-if="message.role !== 'reason'" class="message-avatar">
  56. {{ getAvatar(message.role) }}
  57. </div>
  58. <div class="message-body">
  59. <div class="message-header">
  60. <span class="message-role">{{ getRoleLabel(message.role) }}</span>
  61. </div>
  62. <div class="message-text">
  63. <div v-for="(item, index) in message.contents" :key="index" class="content-item">
  64. <StreamMarkdown :shiki-theme="shikiTheme" :content="item.text" />
  65. </div>
  66. </div>
  67. <div class="message-time">{{ message.timestamps }}</div>
  68. </div>
  69. </div>
  70. </div>
  71. <!-- 加载指示器 -->
  72. <div v-if="props.isLoading" class="message assistant loading">
  73. <div class="message-content">
  74. <div class="message-avatar">🤖</div>
  75. <div class="message-body">
  76. <div class="loading-indicator">
  77. <span></span>
  78. <span></span>
  79. <span></span>
  80. </div>
  81. </div>
  82. </div>
  83. </div>
  84. </div>
  85. </template>
  86. <style scoped lang="less">
  87. .chat-messages {
  88. flex: 1;
  89. overflow-y: auto;
  90. padding: 24px;
  91. display: flex;
  92. flex-direction: column;
  93. gap: 20px;
  94. background-color: #fff;
  95. }
  96. .message {
  97. display: flex;
  98. animation: fadeIn 0.3s ease-in;
  99. }
  100. @keyframes fadeIn {
  101. from {
  102. opacity: 0;
  103. transform: translateY(10px);
  104. }
  105. to {
  106. opacity: 1;
  107. transform: translateY(0);
  108. }
  109. }
  110. .message.user {
  111. justify-content: flex-end;
  112. }
  113. .message.reason {
  114. border-left: 3px solid #6c757d;
  115. padding-left: 8px;
  116. }
  117. .message.action {
  118. border-left: 3px solid #28a745;
  119. padding-left: 8px;
  120. }
  121. .message.error {
  122. border-left: 3px solid #dc3545;
  123. padding-left: 8px;
  124. }
  125. .message-header {
  126. display: flex;
  127. gap: 8px;
  128. align-items: center;
  129. margin-bottom: 4px;
  130. }
  131. .message-role {
  132. font-size: 11px;
  133. font-weight: 600;
  134. color: @cb-text-secondary;
  135. text-transform: uppercase;
  136. padding: 2px 6px;
  137. border-radius: 4px;
  138. background-color: @cb-vscode-input-background;
  139. }
  140. .message-text {
  141. padding: 0;
  142. border-radius: 0;
  143. background-color: transparent;
  144. line-height: 1.6;
  145. word-wrap: break-word;
  146. transition:
  147. background-color 0.3s,
  148. color 0.3s;
  149. max-width: 800px;
  150. overflow-x: auto;
  151. }
  152. .message.user .message-text {
  153. background-color: @cb-accent;
  154. color: white;
  155. padding: 12px 16px;
  156. border-radius: 12px;
  157. }
  158. .message.assistant .message-text {
  159. background-color: @cb-vscode-input-background;
  160. padding: 12px 16px;
  161. border-radius: 12px;
  162. }
  163. /* 内容项样式 */
  164. .content-item {
  165. margin-bottom: 8px;
  166. }
  167. .content-item:last-child {
  168. margin-bottom: 0;
  169. }
  170. .content-label {
  171. font-size: 11px;
  172. font-weight: 600;
  173. color: @cb-text-secondary;
  174. margin-bottom: 4px;
  175. display: inline-block;
  176. }
  177. .content-tool {
  178. font-size: 11px;
  179. color: #28a745;
  180. font-weight: 500;
  181. margin-left: 8px;
  182. }
  183. .content-text {
  184. padding: 4px 0;
  185. white-space: pre-wrap;
  186. }
  187. .message.user .content-text {
  188. padding: 0;
  189. }
  190. .content-reason {
  191. color: @cb-text-secondary;
  192. padding: 8px;
  193. background-color: rgba(108, 117, 125, 0.1);
  194. border-radius: 6px;
  195. margin-bottom: 8px;
  196. }
  197. .content-action {
  198. color: #28a745;
  199. padding: 8px;
  200. background-color: rgba(40, 167, 69, 0.1);
  201. border-radius: 6px;
  202. margin-bottom: 8px;
  203. }
  204. .content-error {
  205. color: #dc3545;
  206. padding: 8px;
  207. background-color: rgba(220, 53, 69, 0.1);
  208. border-radius: 6px;
  209. margin-bottom: 8px;
  210. }
  211. .content-plain .content-text {
  212. padding: 4px 0;
  213. }
  214. .message-args {
  215. margin-top: 8px;
  216. padding: 8px;
  217. background-color: @cb-vscode-sideBar-background;
  218. border-radius: 6px;
  219. font-size: 12px;
  220. overflow-x: auto;
  221. }
  222. .message-args pre {
  223. margin: 0;
  224. white-space: pre-wrap;
  225. word-wrap: break-word;
  226. color: @cb-text-secondary;
  227. }
  228. .message-content {
  229. display: flex;
  230. gap: 12px;
  231. width: 50%;
  232. }
  233. .message.user .message-content {
  234. flex-direction: row-reverse;
  235. }
  236. .message-avatar {
  237. width: 36px;
  238. height: 36px;
  239. border-radius: 50%;
  240. background-color: @cb-vscode-input-background;
  241. display: flex;
  242. align-items: center;
  243. justify-content: center;
  244. font-size: 18px;
  245. flex-shrink: 0;
  246. transition: background-color 0.3s;
  247. }
  248. .message-body {
  249. display: flex;
  250. flex-direction: column;
  251. gap: 4px;
  252. }
  253. .message.user .message-body {
  254. align-items: flex-end;
  255. }
  256. .message-time {
  257. font-size: 11px;
  258. color: @cb-text-secondary;
  259. padding: 0 4px;
  260. }
  261. .message.loading {
  262. opacity: 0.7;
  263. }
  264. .loading-indicator {
  265. display: flex;
  266. gap: 4px;
  267. padding: 12px 16px;
  268. }
  269. .loading-indicator span {
  270. width: 8px;
  271. height: 8px;
  272. border-radius: 50%;
  273. background-color: @cb-text-secondary;
  274. animation: pulse 1.4s ease-in-out infinite;
  275. }
  276. .loading-indicator span:nth-child(1) {
  277. animation-delay: 0s;
  278. }
  279. .loading-indicator span:nth-child(2) {
  280. animation-delay: 0.2s;
  281. }
  282. .loading-indicator span:nth-child(3) {
  283. animation-delay: 0.4s;
  284. }
  285. @keyframes pulse {
  286. 0%,
  287. 80%,
  288. 100% {
  289. opacity: 0.3;
  290. }
  291. 40% {
  292. opacity: 1;
  293. }
  294. }
  295. </style>