2025-12-18 01:00:25 +08:00
|
|
|
|
<script setup lang="ts">
|
|
|
|
|
|
import type { ChatMessage } from '@/types/chat'
|
2025-12-21 23:16:49 +08:00
|
|
|
|
import { useSessionManager } from '@/composables/useSessionManager'
|
|
|
|
|
|
import SessionSelector from '@/components/SessionSelector.vue'
|
|
|
|
|
|
import { useChatSessionStore } from '@/stores/modules/chatSession'
|
2025-12-18 01:00:25 +08:00
|
|
|
|
import { useScroll } from '@vueuse/core'
|
2025-12-19 18:02:31 +08:00
|
|
|
|
import { streamApi } from '@/api/agent'
|
2025-12-18 01:00:25 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-12-21 23:16:49 +08:00
|
|
|
|
const sessionManager = useSessionManager()
|
|
|
|
|
|
sessionManager.initialize()
|
|
|
|
|
|
const sessionStore = useChatSessionStore()
|
|
|
|
|
|
|
|
|
|
|
|
// Messages from current session - get directly from store
|
|
|
|
|
|
const messages = computed(() => sessionStore.currentSession?.messages || [])
|
2025-12-18 01:00:25 +08:00
|
|
|
|
|
|
|
|
|
|
const inputText = ref('')
|
|
|
|
|
|
const isLoading = ref(false)
|
|
|
|
|
|
const messagesContainer = ref<HTMLElement>()
|
|
|
|
|
|
|
2025-12-19 18:02:31 +08:00
|
|
|
|
// 流式响应相关状态
|
|
|
|
|
|
const currentAiMessage = ref<ChatMessage | null>(null)
|
|
|
|
|
|
const isStreaming = ref(false)
|
|
|
|
|
|
const streamController = ref<AbortController | null>(null)
|
|
|
|
|
|
|
2025-12-18 01:00:25 +08:00
|
|
|
|
const { arrivedState } = useScroll(messagesContainer, {
|
|
|
|
|
|
offset: { bottom: 100 },
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
// Auto-scroll to bottom when new messages arrive
|
|
|
|
|
|
watch(messages, () => {
|
|
|
|
|
|
nextTick(() => {
|
|
|
|
|
|
if (messagesContainer.value && arrivedState.bottom) {
|
|
|
|
|
|
messagesContainer.value.scrollTop = messagesContainer.value.scrollHeight
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}, { deep: true })
|
|
|
|
|
|
|
|
|
|
|
|
function handleSend(text: string) {
|
refactor: remove i18n dependency and hardcode strings in components
- Updated ChatInput.vue to remove i18n and handle model value updates directly.
- Refactored NavBar.vue to use a static title map instead of i18n.
- Simplified TabBar.vue by replacing i18n calls with hardcoded titles.
- Removed i18n usage in various pages (charts, counter, forgot-password, index, keepalive, llm-chat, login, mock, profile, register, scroll-cache, settings, unocss).
- Deleted localization files (en-US.json, zh-CN.json) and i18n utility functions.
- Updated constants to provide static app name and description.
- Adjusted page titles in set-page-title.ts to use static names.
- Cleaned up TypeScript types by removing unused i18n types.
2025-12-19 10:09:47 +08:00
|
|
|
|
if (!text.trim())
|
|
|
|
|
|
return
|
2025-12-18 01:00:25 +08:00
|
|
|
|
|
2025-12-21 23:16:49 +08:00
|
|
|
|
// 添加用户消息到当前会话
|
2025-12-18 01:00:25 +08:00
|
|
|
|
const userMessage: ChatMessage = {
|
|
|
|
|
|
id: Date.now(),
|
|
|
|
|
|
content: text,
|
|
|
|
|
|
sender: 'user',
|
|
|
|
|
|
timestamp: new Date(),
|
refactor: remove i18n dependency and hardcode strings in components
- Updated ChatInput.vue to remove i18n and handle model value updates directly.
- Refactored NavBar.vue to use a static title map instead of i18n.
- Simplified TabBar.vue by replacing i18n calls with hardcoded titles.
- Removed i18n usage in various pages (charts, counter, forgot-password, index, keepalive, llm-chat, login, mock, profile, register, scroll-cache, settings, unocss).
- Deleted localization files (en-US.json, zh-CN.json) and i18n utility functions.
- Updated constants to provide static app name and description.
- Adjusted page titles in set-page-title.ts to use static names.
- Cleaned up TypeScript types by removing unused i18n types.
2025-12-19 10:09:47 +08:00
|
|
|
|
status: 'sent',
|
2025-12-18 01:00:25 +08:00
|
|
|
|
}
|
2025-12-21 23:16:49 +08:00
|
|
|
|
const memory = sessionManager.handleNewMessage(userMessage)
|
2025-12-18 01:00:25 +08:00
|
|
|
|
|
2025-12-19 18:02:31 +08:00
|
|
|
|
// 创建 AI 消息占位符
|
|
|
|
|
|
const aiMessage: ChatMessage = {
|
|
|
|
|
|
id: Date.now() + 1,
|
|
|
|
|
|
content: '',
|
|
|
|
|
|
sender: 'ai',
|
|
|
|
|
|
timestamp: new Date(),
|
|
|
|
|
|
status: 'sending',
|
|
|
|
|
|
}
|
2025-12-21 23:16:49 +08:00
|
|
|
|
sessionManager.handleNewMessage(aiMessage)
|
|
|
|
|
|
|
|
|
|
|
|
// 设置当前 AI 消息引用(获取最后一条消息)
|
|
|
|
|
|
const currentSession = sessionStore.currentSession
|
|
|
|
|
|
const sessionId = sessionStore.currentSessionId
|
|
|
|
|
|
if (currentSession && currentSession.messages.length > 0) {
|
|
|
|
|
|
currentAiMessage.value = currentSession.messages[currentSession.messages.length - 1]
|
|
|
|
|
|
}
|
2025-12-19 18:02:31 +08:00
|
|
|
|
|
|
|
|
|
|
// 清空输入
|
2025-12-18 01:00:25 +08:00
|
|
|
|
inputText.value = ''
|
|
|
|
|
|
|
2025-12-19 18:02:31 +08:00
|
|
|
|
// 设置加载状态
|
2025-12-18 01:00:25 +08:00
|
|
|
|
isLoading.value = true
|
2025-12-19 18:02:31 +08:00
|
|
|
|
isStreaming.value = true
|
|
|
|
|
|
|
2025-12-21 23:16:49 +08:00
|
|
|
|
// 创建 AbortController
|
2025-12-19 18:02:31 +08:00
|
|
|
|
const controller = new AbortController()
|
|
|
|
|
|
streamController.value = controller
|
|
|
|
|
|
|
2025-12-21 23:16:49 +08:00
|
|
|
|
// 调用流式 API,传递 memory 参数
|
2025-12-19 18:02:31 +08:00
|
|
|
|
streamApi(
|
2025-12-21 23:16:49 +08:00
|
|
|
|
{
|
|
|
|
|
|
message: text,
|
|
|
|
|
|
options: memory ? { memory } : undefined
|
|
|
|
|
|
},
|
2025-12-19 18:02:31 +08:00
|
|
|
|
{
|
|
|
|
|
|
onopen(response) {
|
|
|
|
|
|
console.log('SSE 连接已建立:', response)
|
|
|
|
|
|
},
|
|
|
|
|
|
onmessage(data) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const parsed = JSON.parse(data) as { text: string }
|
2025-12-21 23:16:49 +08:00
|
|
|
|
if (parsed.text && currentAiMessage.value && sessionId) {
|
|
|
|
|
|
// 使用 store 更新消息内容
|
|
|
|
|
|
sessionStore.updateMessageContent(sessionId, currentAiMessage.value.id, parsed.text)
|
|
|
|
|
|
// 更新本地引用
|
|
|
|
|
|
currentAiMessage.value.content += parsed.text
|
2025-12-19 18:02:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('解析 SSE 数据失败:', error)
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
onclose() {
|
|
|
|
|
|
console.log('SSE 连接已关闭')
|
|
|
|
|
|
completeMessage()
|
|
|
|
|
|
},
|
|
|
|
|
|
onerror(error) {
|
|
|
|
|
|
console.error('SSE 连接错误:', error)
|
|
|
|
|
|
handleStreamError(error)
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
{ signal: controller.signal }
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 完成消息处理
|
|
|
|
|
|
function completeMessage() {
|
2025-12-21 23:16:49 +08:00
|
|
|
|
if (currentAiMessage.value && sessionStore.currentSession) {
|
|
|
|
|
|
const sessionId = sessionStore.currentSessionId
|
|
|
|
|
|
const messageId = currentAiMessage.value.id
|
|
|
|
|
|
|
|
|
|
|
|
// 更新消息状态为 'sent'
|
|
|
|
|
|
const session = sessionStore.currentSession
|
|
|
|
|
|
if (session && sessionId) {
|
|
|
|
|
|
const updatedMessages = session.messages.map(msg =>
|
|
|
|
|
|
msg.id === messageId ? { ...msg, status: 'sent' as const } : msg
|
|
|
|
|
|
)
|
|
|
|
|
|
sessionStore.updateSession(sessionId, { messages: updatedMessages })
|
2025-12-19 18:02:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
cleanupStream()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 处理流式错误
|
|
|
|
|
|
function handleStreamError(error: Error) {
|
2025-12-21 23:16:49 +08:00
|
|
|
|
if (currentAiMessage.value && sessionStore.currentSession) {
|
|
|
|
|
|
const sessionId = sessionStore.currentSessionId
|
|
|
|
|
|
const messageId = currentAiMessage.value.id
|
|
|
|
|
|
|
|
|
|
|
|
// 更新消息状态为 'error' 并添加错误标记
|
|
|
|
|
|
const session = sessionStore.currentSession
|
|
|
|
|
|
if (session && sessionId) {
|
|
|
|
|
|
const updatedMessages = session.messages.map(msg => {
|
|
|
|
|
|
if (msg.id === messageId) {
|
|
|
|
|
|
return {
|
|
|
|
|
|
...msg,
|
|
|
|
|
|
status: 'error' as const,
|
|
|
|
|
|
content: msg.content + ' (连接错误)'
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return msg
|
|
|
|
|
|
})
|
|
|
|
|
|
sessionStore.updateSession(sessionId, { messages: updatedMessages })
|
2025-12-19 18:02:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 显示错误提示(可集成 vant 的 Toast 组件)
|
|
|
|
|
|
console.error('AI 响应错误:', error)
|
|
|
|
|
|
cleanupStream()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 清理流式连接
|
|
|
|
|
|
function cleanupStream() {
|
|
|
|
|
|
isLoading.value = false
|
|
|
|
|
|
isStreaming.value = false
|
|
|
|
|
|
streamController.value = null
|
|
|
|
|
|
currentAiMessage.value = null
|
2025-12-18 01:00:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function handleKeydown(e: KeyboardEvent) {
|
|
|
|
|
|
// You can add additional keyboard shortcuts here
|
|
|
|
|
|
console.log('Keydown event:', e.key)
|
|
|
|
|
|
}
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<template>
|
refactor: remove i18n dependency and hardcode strings in components
- Updated ChatInput.vue to remove i18n and handle model value updates directly.
- Refactored NavBar.vue to use a static title map instead of i18n.
- Simplified TabBar.vue by replacing i18n calls with hardcoded titles.
- Removed i18n usage in various pages (charts, counter, forgot-password, index, keepalive, llm-chat, login, mock, profile, register, scroll-cache, settings, unocss).
- Deleted localization files (en-US.json, zh-CN.json) and i18n utility functions.
- Updated constants to provide static app name and description.
- Adjusted page titles in set-page-title.ts to use static names.
- Cleaned up TypeScript types by removing unused i18n types.
2025-12-19 10:09:47 +08:00
|
|
|
|
<div class="bg-gray-50 flex flex-col h-screen dark:bg-gray-950">
|
2025-12-18 01:00:25 +08:00
|
|
|
|
|
2025-12-21 23:16:49 +08:00
|
|
|
|
<!-- Session selector -->
|
|
|
|
|
|
<SessionSelector />
|
|
|
|
|
|
|
2025-12-18 01:00:25 +08:00
|
|
|
|
<!-- Messages container -->
|
2025-12-21 23:16:49 +08:00
|
|
|
|
<div
|
2025-12-18 01:00:25 +08:00
|
|
|
|
ref="messagesContainer"
|
2025-12-21 23:16:49 +08:00
|
|
|
|
class="p-4 pb-24 flex-1 overflow-auto"
|
2025-12-18 01:00:25 +08:00
|
|
|
|
>
|
|
|
|
|
|
<van-empty
|
|
|
|
|
|
v-if="messages.length === 0"
|
|
|
|
|
|
description="No messages yet. Start a conversation!"
|
|
|
|
|
|
class="mt-20"
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
|
|
<div v-else>
|
|
|
|
|
|
<ChatBubble
|
|
|
|
|
|
v-for="message in messages"
|
|
|
|
|
|
:key="message.id"
|
|
|
|
|
|
:message="message"
|
|
|
|
|
|
:show-timestamp="true"
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
2025-12-21 23:16:49 +08:00
|
|
|
|
</div>
|
2025-12-18 01:00:25 +08:00
|
|
|
|
|
|
|
|
|
|
<!-- Input area -->
|
refactor: remove i18n dependency and hardcode strings in components
- Updated ChatInput.vue to remove i18n and handle model value updates directly.
- Refactored NavBar.vue to use a static title map instead of i18n.
- Simplified TabBar.vue by replacing i18n calls with hardcoded titles.
- Removed i18n usage in various pages (charts, counter, forgot-password, index, keepalive, llm-chat, login, mock, profile, register, scroll-cache, settings, unocss).
- Deleted localization files (en-US.json, zh-CN.json) and i18n utility functions.
- Updated constants to provide static app name and description.
- Adjusted page titles in set-page-title.ts to use static names.
- Cleaned up TypeScript types by removing unused i18n types.
2025-12-19 10:09:47 +08:00
|
|
|
|
<div class="bottom-0 left-0 right-0 fixed z-10">
|
2025-12-18 01:00:25 +08:00
|
|
|
|
<ChatInput
|
|
|
|
|
|
v-model="inputText"
|
|
|
|
|
|
:disabled="isLoading"
|
|
|
|
|
|
:loading="isLoading"
|
|
|
|
|
|
placeholder="Type your message..."
|
|
|
|
|
|
@send="handleSend"
|
|
|
|
|
|
@keydown="handleKeydown"
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<route lang="json5">
|
|
|
|
|
|
{
|
|
|
|
|
|
name: 'LLMChat',
|
|
|
|
|
|
meta: {
|
|
|
|
|
|
title: 'AI Chat',
|
|
|
|
|
|
requiresAuth: false
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
refactor: remove i18n dependency and hardcode strings in components
- Updated ChatInput.vue to remove i18n and handle model value updates directly.
- Refactored NavBar.vue to use a static title map instead of i18n.
- Simplified TabBar.vue by replacing i18n calls with hardcoded titles.
- Removed i18n usage in various pages (charts, counter, forgot-password, index, keepalive, llm-chat, login, mock, profile, register, scroll-cache, settings, unocss).
- Deleted localization files (en-US.json, zh-CN.json) and i18n utility functions.
- Updated constants to provide static app name and description.
- Adjusted page titles in set-page-title.ts to use static names.
- Cleaned up TypeScript types by removing unused i18n types.
2025-12-19 10:09:47 +08:00
|
|
|
|
</route>
|