Jelajahi Sumber

feat:优化AI助手交互效果

Jerry Liang 3 bulan lalu
induk
melakukan
af88897bdf

+ 2 - 1
service/controllers/aiChatControllers.go

@@ -110,7 +110,8 @@ func CreateAiSession(c *fiber.Ctx) error {
 	}
 
 	return Success(c, fiber.Map{
-		"sessionId": sess.SessionId,
+		"sessionId":      sess.SessionId,
+		"welcomeMessage": sess.WelcomeMessage,
 	})
 }
 

+ 1 - 1
service/services/aiChatServices.go

@@ -186,7 +186,7 @@ func (m *ACPManager) GetSessionList(userId string) []SessionListItem {
 	m.mu.RLock()
 	defer m.mu.RUnlock()
 
-	var list []SessionListItem
+	list := make([]SessionListItem, 0)
 
 	// 当前活跃会话排在最前
 	if sess, ok := m.sessions[userId]; ok {

+ 13 - 15
src/layout/header/Actions/AiChat/ChatPanel.vue

@@ -180,7 +180,7 @@ function formatTime(ts: number): string {
 
     <!-- 输入区域 -->
     <div class="border-t p-3 dark:border-neutral-700">
-      <div class="flex gap-2">
+      <div class="flex items-center gap-2">
         <NInput
           v-model:value="inputText"
           type="textarea"
@@ -189,20 +189,18 @@ function formatTime(ts: number): string {
           :disabled="isStreaming"
           @keydown="handleKeydown"
         />
-        <div class="flex flex-col justify-end">
-          <NButton
-            v-if="!isStreaming"
-            type="primary"
-            size="small"
-            :disabled="!inputText.trim()"
-            @click="handleSend"
-          >
-            <span class="i-ph:paper-plane-tilt" />
-          </NButton>
-          <NButton v-else type="error" size="small" @click="cancelChat">
-            <span class="i-ph:stop-circle" />
-          </NButton>
-        </div>
+        <NButton
+          v-if="!isStreaming"
+          type="primary"
+          size="small"
+          :disabled="!inputText.trim()"
+          @click="handleSend"
+        >
+          <span class="i-ph:paper-plane-tilt" />
+        </NButton>
+        <NButton v-else type="error" size="small" @click="cancelChat">
+          <span class="i-ph:stop-circle" />
+        </NButton>
       </div>
     </div>
   </div>

+ 14 - 3
src/layout/header/Actions/AiChat/useAiChat.ts

@@ -77,6 +77,7 @@ export function useAiChat() {
 
   // 创建新会话
   async function createNewSession() {
+    isLoadingSession.value = true
     try {
       const resp = await fetch(`${getBaseUrl()}/ai/sessions/new`, {
         method: 'POST',
@@ -95,14 +96,18 @@ export function useAiChat() {
           // 切换到新会话
           currentSessionId.value = json.data.sessionId
           messages.value = []
-          welcomeMessage.value = ''
+          welcomeMessage.value = json.data.welcomeMessage || '欢迎使用AI助手'
           // 刷新列表
           await fetchSessionList()
         }
       }
-    } catch {
+    }
+    catch {
       // 忽略
     }
+    finally {
+      isLoadingSession.value = false
+    }
   }
 
   // 加载历史会话
@@ -189,8 +194,14 @@ export function useAiChat() {
         }
         // 刷新列表
         await fetchSessionList()
+
+        // 如果没有任何会话了,自动创建新会话
+        if (sessionList.value.length === 0) {
+          await createNewSession()
+        }
       }
-    } catch {
+    }
+    catch {
       // 忽略
     }
   }