2026-03-10 16:09:09 +08:00
|
|
|
<script setup lang="ts">
|
2026-03-11 17:22:47 +08:00
|
|
|
import { ref, nextTick, onMounted, onUnmounted } from 'vue'
|
2026-03-13 14:33:25 +08:00
|
|
|
import { useChat } from './chat/chat'
|
2026-03-12 10:49:44 +08:00
|
|
|
import ChatHeader from '@/components/chat/ChatHeader.vue'
|
|
|
|
|
import ChatMessage from '@/components/chat/ChatMessage.vue'
|
|
|
|
|
import ChatInput from '@/components/chat/ChatInput.vue'
|
|
|
|
|
import ChatSidebar from '@/components/chat/ChatSidebar.vue'
|
|
|
|
|
import ChatAgentSelector from '@/components/chat/ChatAgentSelector.vue'
|
2026-03-13 14:33:25 +08:00
|
|
|
import './chat/chat.css'
|
2026-03-12 10:49:44 +08:00
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
chatModels,
|
|
|
|
|
selectedModel,
|
|
|
|
|
showModelDropdown,
|
|
|
|
|
chatAgents,
|
|
|
|
|
selectedAgent,
|
|
|
|
|
messages,
|
|
|
|
|
chatSessions,
|
2026-03-13 14:33:25 +08:00
|
|
|
currentSessionId,
|
2026-03-12 10:49:44 +08:00
|
|
|
groupChats,
|
|
|
|
|
showAgentSelector,
|
|
|
|
|
selectMode,
|
|
|
|
|
selectedAgents,
|
|
|
|
|
groupChatName,
|
|
|
|
|
inputMessage,
|
|
|
|
|
isLoading,
|
|
|
|
|
sidebarCollapsed,
|
|
|
|
|
fetchModels,
|
|
|
|
|
openAgentSelector,
|
|
|
|
|
toggleAgentSelection,
|
|
|
|
|
confirmAgentSelection,
|
|
|
|
|
cancelAgentSelection,
|
|
|
|
|
selectAgent,
|
2026-03-13 14:33:25 +08:00
|
|
|
selectGroup,
|
2026-03-12 10:49:44 +08:00
|
|
|
selectSession,
|
|
|
|
|
newChat,
|
2026-03-13 14:33:25 +08:00
|
|
|
clearMessages,
|
2026-03-12 10:49:44 +08:00
|
|
|
toggleSidebar,
|
2026-03-13 14:33:25 +08:00
|
|
|
createSession,
|
|
|
|
|
saveMessage,
|
|
|
|
|
deleteSession,
|
|
|
|
|
init,
|
|
|
|
|
cleanup,
|
2026-03-12 10:49:44 +08:00
|
|
|
} = useChat()
|
2026-03-10 16:09:09 +08:00
|
|
|
|
2026-03-12 10:49:44 +08:00
|
|
|
const messagesContainer = ref<HTMLElement | null>(null)
|
2026-03-11 17:22:47 +08:00
|
|
|
|
2026-03-12 10:49:44 +08:00
|
|
|
// Mock 流式响应(用于测试前端流式效果)
|
|
|
|
|
const mockStreamResponse = async (content: string, messageIndex: number) => {
|
|
|
|
|
const chars = content.split('')
|
|
|
|
|
for (let i = 0; i < chars.length; i++) {
|
|
|
|
|
messages.value[messageIndex].content += chars[i]
|
|
|
|
|
await nextTick()
|
|
|
|
|
scrollToBottom()
|
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 15))
|
2026-03-11 17:22:47 +08:00
|
|
|
}
|
2026-03-12 10:49:44 +08:00
|
|
|
messages.value[messageIndex].isStreaming = false
|
|
|
|
|
isLoading.value = false
|
2026-03-11 17:22:47 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-12 10:49:44 +08:00
|
|
|
// 滚动到底部
|
|
|
|
|
const scrollToBottom = () => {
|
|
|
|
|
if (messagesContainer.value) {
|
|
|
|
|
messagesContainer.value.scrollTop = messagesContainer.value.scrollHeight
|
2026-03-11 14:26:25 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-12 10:49:44 +08:00
|
|
|
// 切换模型下拉框
|
|
|
|
|
const toggleModelDropdown = () => {
|
|
|
|
|
showModelDropdown.value = !showModelDropdown.value
|
2026-03-11 14:26:25 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-12 10:49:44 +08:00
|
|
|
// 选择模型
|
|
|
|
|
const handleSelectModel = (model: any) => {
|
|
|
|
|
selectedModel.value = model
|
|
|
|
|
showModelDropdown.value = false
|
2026-03-11 14:26:25 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-10 16:09:09 +08:00
|
|
|
// 发送消息
|
|
|
|
|
const sendMessage = async () => {
|
|
|
|
|
if (!inputMessage.value.trim() || isLoading.value) return
|
|
|
|
|
|
|
|
|
|
const userContent = inputMessage.value.trim()
|
|
|
|
|
inputMessage.value = ''
|
|
|
|
|
|
2026-03-13 14:33:25 +08:00
|
|
|
// 重置输入框高度
|
|
|
|
|
nextTick(() => {
|
|
|
|
|
const textarea = document.querySelector('.chat-input-textarea') as HTMLTextAreaElement
|
|
|
|
|
if (textarea) {
|
|
|
|
|
textarea.style.height = 'auto'
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 如果没有会话,创建一个新会话
|
|
|
|
|
if (!currentSessionId.value) {
|
|
|
|
|
await createSession()
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-12 10:49:44 +08:00
|
|
|
const userMessage = {
|
2026-03-10 16:09:09 +08:00
|
|
|
id: Date.now(),
|
2026-03-12 10:49:44 +08:00
|
|
|
role: 'user' as const,
|
2026-03-10 16:09:09 +08:00
|
|
|
content: userContent,
|
|
|
|
|
timestamp: new Date()
|
|
|
|
|
}
|
|
|
|
|
messages.value.push(userMessage)
|
|
|
|
|
|
2026-03-13 14:33:25 +08:00
|
|
|
// 保存用户消息到后端
|
|
|
|
|
await saveMessage('user', userContent)
|
|
|
|
|
|
2026-03-12 10:49:44 +08:00
|
|
|
const aiMessage = {
|
2026-03-10 16:09:09 +08:00
|
|
|
id: Date.now() + 1,
|
2026-03-12 10:49:44 +08:00
|
|
|
role: 'assistant' as const,
|
2026-03-10 16:09:09 +08:00
|
|
|
content: '',
|
|
|
|
|
timestamp: new Date(),
|
|
|
|
|
isStreaming: true
|
|
|
|
|
}
|
|
|
|
|
messages.value.push(aiMessage)
|
|
|
|
|
|
|
|
|
|
nextTick(() => scrollToBottom())
|
|
|
|
|
|
|
|
|
|
isLoading.value = true
|
2026-03-11 16:26:10 +08:00
|
|
|
|
|
|
|
|
try {
|
2026-03-11 17:22:47 +08:00
|
|
|
const requestBody: any = {
|
|
|
|
|
agent_id: selectedAgent.value?.id || 1,
|
|
|
|
|
message: userContent,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (selectedModel.value) {
|
|
|
|
|
requestBody.model_id = selectedModel.value.id
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-13 14:33:25 +08:00
|
|
|
// 传入 session_id
|
|
|
|
|
if (currentSessionId.value) {
|
|
|
|
|
requestBody.session_id = currentSessionId.value
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-12 10:49:44 +08:00
|
|
|
const response = await fetch(`/api/agent/chat/stream`, {
|
2026-03-11 16:26:10 +08:00
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
},
|
2026-03-11 17:22:47 +08:00
|
|
|
body: JSON.stringify(requestBody),
|
2026-03-11 16:26:10 +08:00
|
|
|
})
|
|
|
|
|
|
2026-03-12 10:49:44 +08:00
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`Request failed: ${response.status}`)
|
|
|
|
|
}
|
2026-03-10 16:09:09 +08:00
|
|
|
|
2026-03-13 14:33:25 +08:00
|
|
|
// 真正的流式处理:边读取边显示
|
2026-03-12 10:49:44 +08:00
|
|
|
const reader = response.body.getReader()
|
|
|
|
|
const decoder = new TextDecoder()
|
|
|
|
|
let buffer = ''
|
2026-03-13 14:33:25 +08:00
|
|
|
const aiMessageIndex = messages.value.length - 1
|
2026-03-10 16:09:09 +08:00
|
|
|
|
2026-03-12 10:49:44 +08:00
|
|
|
while (true) {
|
|
|
|
|
const { done, value } = await reader.read()
|
|
|
|
|
if (done) break
|
2026-03-10 16:09:09 +08:00
|
|
|
|
2026-03-12 10:49:44 +08:00
|
|
|
buffer += decoder.decode(value, { stream: true })
|
2026-03-10 16:09:09 +08:00
|
|
|
|
2026-03-12 10:49:44 +08:00
|
|
|
const lines = buffer.split('\n')
|
|
|
|
|
buffer = lines.pop() || ''
|
2026-03-10 17:38:57 +08:00
|
|
|
|
2026-03-12 10:49:44 +08:00
|
|
|
for (const line of lines) {
|
|
|
|
|
if (line.startsWith('data: ')) {
|
2026-03-13 14:33:25 +08:00
|
|
|
const data = line.slice(6).trim()
|
2026-03-12 10:49:44 +08:00
|
|
|
if (data && data !== '[DONE]') {
|
2026-03-13 14:33:25 +08:00
|
|
|
// 直接累加内容并显示(真正的流式)
|
|
|
|
|
messages.value[aiMessageIndex].content += data
|
|
|
|
|
await nextTick()
|
|
|
|
|
scrollToBottom()
|
2026-03-12 10:49:44 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-10 16:09:09 +08:00
|
|
|
|
2026-03-13 14:33:25 +08:00
|
|
|
// 处理剩余buffer中的数据
|
|
|
|
|
if (buffer.startsWith('data: ')) {
|
|
|
|
|
const data = buffer.slice(6).trim()
|
|
|
|
|
if (data && data !== '[DONE]') {
|
|
|
|
|
messages.value[aiMessageIndex].content += data
|
2026-03-12 10:49:44 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-10 17:38:57 +08:00
|
|
|
|
2026-03-12 10:49:44 +08:00
|
|
|
messages.value[aiMessageIndex].isStreaming = false
|
|
|
|
|
isLoading.value = false
|
|
|
|
|
scrollToBottom()
|
2026-03-13 14:33:25 +08:00
|
|
|
|
|
|
|
|
// 保存 AI 消息到后端
|
|
|
|
|
await saveMessage('assistant', messages.value[aiMessageIndex].content)
|
2026-03-12 10:49:44 +08:00
|
|
|
} catch (error: any) {
|
|
|
|
|
console.error('[Stream] 错误:', error)
|
|
|
|
|
const errorIndex = messages.value.findIndex(m => m.isStreaming)
|
|
|
|
|
if (errorIndex > -1) {
|
|
|
|
|
messages.value[errorIndex].content = `Error: ${error.message || 'Failed to send message'}`
|
|
|
|
|
messages.value[errorIndex].isStreaming = false
|
|
|
|
|
}
|
|
|
|
|
isLoading.value = false
|
2026-03-10 16:09:09 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-03-13 14:33:25 +08:00
|
|
|
|
|
|
|
|
// 初始化
|
|
|
|
|
onMounted(() => {
|
|
|
|
|
console.log('[Chat] Component mounted, calling init()')
|
|
|
|
|
init()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
onUnmounted(() => {
|
|
|
|
|
cleanup()
|
|
|
|
|
})
|
2026-03-12 10:49:44 +08:00
|
|
|
</script>
|
2026-03-10 16:09:09 +08:00
|
|
|
|
2026-03-12 10:49:44 +08:00
|
|
|
<template>
|
|
|
|
|
<div class="h-screen flex bg-[#09090b]">
|
|
|
|
|
<!-- 主聊天区域 -->
|
|
|
|
|
<div class="flex-1 flex flex-col bg-[#09090b]">
|
|
|
|
|
<!-- 顶部栏 -->
|
|
|
|
|
<ChatHeader
|
|
|
|
|
:selected-agent="selectedAgent"
|
|
|
|
|
:chat-models="chatModels"
|
|
|
|
|
:selected-model="selectedModel"
|
|
|
|
|
:show-model-dropdown="showModelDropdown"
|
|
|
|
|
:sidebar-collapsed="sidebarCollapsed"
|
|
|
|
|
@toggle-dropdown="toggleModelDropdown"
|
|
|
|
|
@select-model="handleSelectModel"
|
|
|
|
|
@toggle-sidebar="toggleSidebar"
|
2026-03-13 14:33:25 +08:00
|
|
|
@clear-chat="clearMessages"
|
|
|
|
|
@new-chat="newChat"
|
2026-03-12 10:49:44 +08:00
|
|
|
/>
|
2026-03-10 16:09:09 +08:00
|
|
|
|
2026-03-12 10:49:44 +08:00
|
|
|
<!-- 消息区域 -->
|
|
|
|
|
<div ref="messagesContainer" class="flex-1 overflow-y-auto py-4">
|
2026-03-13 14:33:25 +08:00
|
|
|
<!-- 空状态欢迎提示 -->
|
|
|
|
|
<div v-if="messages.length === 0" class="h-full flex items-center justify-center">
|
|
|
|
|
<div class="text-center">
|
|
|
|
|
<div class="text-5xl mb-4">{{ selectedAgent?.avatar || '🧠' }}</div>
|
|
|
|
|
<h2 class="text-xl font-semibold text-white mb-2">和 {{ selectedAgent?.name || 'AI' }} 开始对话</h2>
|
|
|
|
|
<p class="text-white/40 text-sm">发送消息开始聊天</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<!-- 消息列表 -->
|
|
|
|
|
<div v-else class="px-6">
|
2026-03-12 10:49:44 +08:00
|
|
|
<ChatMessage
|
|
|
|
|
v-for="message in messages"
|
|
|
|
|
:key="message.id"
|
|
|
|
|
:message="message"
|
|
|
|
|
:selected-agent="selectedAgent"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<!-- 输入区域 -->
|
|
|
|
|
<ChatInput
|
|
|
|
|
v-model="inputMessage"
|
|
|
|
|
:loading="isLoading"
|
|
|
|
|
@send="sendMessage"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<!-- 右侧边栏 -->
|
|
|
|
|
<ChatSidebar
|
|
|
|
|
:collapsed="sidebarCollapsed"
|
|
|
|
|
:chat-agents="chatAgents"
|
|
|
|
|
:selected-agent="selectedAgent"
|
|
|
|
|
:chat-sessions="chatSessions"
|
|
|
|
|
:group-chats="groupChats"
|
|
|
|
|
@open-agent-selector="openAgentSelector"
|
|
|
|
|
@select-agent="selectAgent"
|
|
|
|
|
@select-session="selectSession"
|
2026-03-13 14:33:25 +08:00
|
|
|
@select-group="selectGroup"
|
2026-03-12 10:49:44 +08:00
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<!-- 智能体选择弹窗 -->
|
|
|
|
|
<ChatAgentSelector
|
|
|
|
|
:show="showAgentSelector"
|
|
|
|
|
:select-mode="selectMode"
|
|
|
|
|
:chat-agents="chatAgents"
|
|
|
|
|
:selected-agents="selectedAgents"
|
|
|
|
|
:group-chat-name="groupChatName"
|
|
|
|
|
@close="cancelAgentSelection"
|
|
|
|
|
@toggle-select="toggleAgentSelection"
|
|
|
|
|
@confirm="confirmAgentSelection"
|
|
|
|
|
@update:group-chat-name="groupChatName = $event"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|