Browse Source

新增新对话

zhangwl 1 month ago
parent
commit
cd21e1cd92
2 changed files with 23 additions and 4 deletions
  1. 13 0
      chat-ai-ui-main/src/components/Header.vue
  2. 10 4
      chat-ai-ui-main/src/stores/chat.ts

+ 13 - 0
chat-ai-ui-main/src/components/Header.vue

@@ -1,10 +1,12 @@
 <script setup lang="ts">
 <script setup lang="ts">
 import { useUserStore } from '../stores/user';
 import { useUserStore } from '../stores/user';
+import { useChatStore } from '../stores/chat';
 import { useRouter } from 'vue-router';
 import { useRouter } from 'vue-router';
 import robotImage from '../assets/robot.png';
 import robotImage from '../assets/robot.png';
 import axios from 'axios';
 import axios from 'axios';
 
 
 const userStore = useUserStore();
 const userStore = useUserStore();
+const chatStore = useChatStore();
 const router = useRouter();
 const router = useRouter();
 
 
 const logout = async () => {
 const logout = async () => {
@@ -60,6 +62,17 @@ const logout = async () => {
 
 
     <!-- 用户信息区域 -->
     <!-- 用户信息区域 -->
     <div class="flex items-center space-x-4">
     <div class="flex items-center space-x-4">
+
+      <!-- 新对话按钮 -->
+      <button
+        @click="chatStore.newConversation()"
+        class="flex items-center space-x-1 text-gray-400 hover:text-green-400 transition-colors duration-200 px-3 py-1 rounded hover:bg-gray-700"
+      >
+        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
+          <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"></path>
+        </svg>
+        <span>新对话</span>
+      </button>
       <!-- 用户头像或图标 -->
       <!-- 用户头像或图标 -->
       <div class="w-8 h-8 bg-blue-500 rounded-full flex items-center justify-center">
       <div class="w-8 h-8 bg-blue-500 rounded-full flex items-center justify-center">
         <span class="text-white text-sm font-medium">
         <span class="text-white text-sm font-medium">

+ 10 - 4
chat-ai-ui-main/src/stores/chat.ts

@@ -34,15 +34,21 @@ const blankMeta = () => ({
 export const useChatStore = defineStore('chat', () => {
 export const useChatStore = defineStore('chat', () => {
   const messages = ref<Message[]>([]);
   const messages = ref<Message[]>([]);
   const isLoading = ref(false);
   const isLoading = ref(false);
+  const sessionId = ref(crypto.randomUUID());
 
 
   const userStore = useUserStore();
   const userStore = useUserStore();
 
 
+  const newConversation = () => {
+    sessionId.value = crypto.randomUUID();
+    messages.value = [];
+  };
+
   const loadChatHistory = async () => {
   const loadChatHistory = async () => {
     if (!userStore.userId) return;
     if (!userStore.userId) return;
     try {
     try {
       const { data } = await axios.get(
       const { data } = await axios.get(
         `${import.meta.env.VITE_API_URL}/chat/history`,
         `${import.meta.env.VITE_API_URL}/chat/history`,
-        { headers: { 'Authorization': `Bearer ${userStore.userId}` } }
+        { headers: { 'Authorization': `Bearer ${userStore.userId}` }, params: { sessionId: sessionId.value } }
       );
       );
       messages.value = data
       messages.value = data
         .filter((msg: FormattedMessage) => msg.content)
         .filter((msg: FormattedMessage) => msg.content)
@@ -74,7 +80,7 @@ export const useChatStore = defineStore('chat', () => {
       } else {
       } else {
         const { data } = await axios.post(
         const { data } = await axios.post(
           `${import.meta.env.VITE_API_URL}/chat/chat`,
           `${import.meta.env.VITE_API_URL}/chat/chat`,
-          { messages: [{ role: 'user', content: message }], stream: false },
+          { messages: [{ role: 'user', content: message }], stream: false, sessionId: sessionId.value },
           { headers: { 'Authorization': `Bearer ${userStore.userId}`, 'Content-Type': 'application/json' } }
           { headers: { 'Authorization': `Bearer ${userStore.userId}`, 'Content-Type': 'application/json' } }
         );
         );
         messages.value.push({
         messages.value.push({
@@ -119,7 +125,7 @@ export const useChatStore = defineStore('chat', () => {
           'Content-Type': 'application/json',
           'Content-Type': 'application/json',
           'Authorization': `Bearer ${userStore.userId}`,
           'Authorization': `Bearer ${userStore.userId}`,
         },
         },
-        body: JSON.stringify({ messages: historySnapshot, stream: true }),
+        body: JSON.stringify({ messages: historySnapshot, stream: true, sessionId: sessionId.value }),
       });
       });
 
 
       if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
       if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
@@ -213,5 +219,5 @@ export const useChatStore = defineStore('chat', () => {
     }
     }
   };
   };
 
 
-  return { messages, isLoading, loadChatHistory, sendMessage };
+  return { messages, isLoading, sessionId, loadChatHistory, sendMessage, newConversation };
 });
 });