2026-03-23 18:39:46 +08:00

468 lines
12 KiB
Vue

<template>
<div class="ai-chat-wrapper">
<!-- Header -->
<div class="ai-header">
<div class="header-title">
<span v-if="showHistory">Chat History</span>
<el-dropdown v-else trigger="click" @command="handleIdentityChange">
<span class="el-dropdown-link">
{{ aiStore.currentIdentity?.name || '' }}
<el-icon class="el-icon--right"><ArrowDown /></el-icon>
</span>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item
v-for="identity in AI_IDENTITIES"
:key="identity.key"
:command="identity.key"
:disabled="identity.key === aiStore.currentIdentityKey"
>
<el-icon><component :is="getIconComponent(identity.icon)" /></el-icon>
{{ identity.name }}
</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
</div>
<div class="actions">
<el-tooltip content="New Chat" placement="bottom">
<el-button link @click="handleNewChat">
<el-icon><Plus /></el-icon>
</el-button>
</el-tooltip>
<el-tooltip :content="showHistory ? 'Back to Chat' : 'History'" placement="bottom">
<el-button link @click="toggleHistory">
<el-icon><component :is="showHistory ? ChatDotRound : Clock" /></el-icon>
</el-button>
</el-tooltip>
<el-button link @click="closeAi" class="close-btn">
<el-icon><Close /></el-icon>
</el-button>
</div>
</div>
<!-- Content -->
<div class="ai-content">
<div v-if="showHistory" class="history-list">
<el-scrollbar>
<div v-if="aiStore.sessions.length === 0" class="empty-history">
No chat history for {{ aiStore.currentIdentity?.name || '' }}
</div>
<div
v-for="session in aiStore.sessions"
:key="session.id"
class="history-item"
:class="{ active: aiStore.currentSessionId === session.id }"
@click="loadSession(session.id)"
>
<div class="history-title">{{ session.title }}</div>
<div class="history-time">{{ formatTime(session.createdAt) }}</div>
<el-button link class="delete-btn" @click.stop="deleteSession(session.id)">
<el-icon><Delete /></el-icon>
</el-button>
</div>
</el-scrollbar>
</div>
<el-scrollbar v-else ref="scrollbarRef" always style="height: 100%">
<div class="messages-list">
<div v-if="aiStore.messages.length === 0" class="empty-chat">
<div class="empty-icon">
<el-icon :size="48"><component :is="getIconComponent(aiStore.currentIdentity?.icon || '')" /></el-icon>
</div>
<p>{{ aiStore.currentIdentity?.description || '' }}</p>
<p>{{ aiStore.currentIdentity?.systemPrompt || 'How can I help you today?' }}</p>
</div>
<div v-for="(msg, index) in aiStore.messages" :key="index" class="message-row" :class="msg.role">
<div class="avatar">
<el-avatar :size="32" :icon="msg.role === 'user' ? UserFilled : getIconComponent(aiStore.currentIdentity?.icon || '')" :class="msg.role === 'user' ? 'user-avatar' : 'ai-avatar'" />
</div>
<div class="message-bubble">
<div class="message-text">{{ msg.content }}</div>
</div>
</div>
</div>
</el-scrollbar>
</div>
<!-- Footer -->
<div class="ai-footer" v-if="!showHistory">
<div class="input-area">
<el-input
v-model="inputMessage"
:placeholder="`Ask ${aiStore.currentIdentity?.name || ''} something...`"
@keyup.enter="sendMessage"
resize="none"
type="textarea"
:autosize="{ minRows: 1, maxRows: 4 }"
class="chat-input"
>
</el-input>
<el-button type="primary" circle :icon="Position" @click="sendMessage" :disabled="!inputMessage.trim()" class="send-btn" />
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, nextTick, onMounted, watch } from 'vue'
import { useAiStore, AI_IDENTITIES } from '@/stores/modules/ai'
import { Plus, Clock, Close, UserFilled, Service, Position, ChatDotRound, Delete, ArrowDown } from '@element-plus/icons-vue'
import type { ElScrollbar } from 'element-plus'
import dayjs from 'dayjs'
const aiStore = useAiStore()
const inputMessage = ref('')
const scrollbarRef = ref<InstanceType<typeof ElScrollbar>>()
const showHistory = ref(false)
const getIconComponent = (iconName: string) => {
const icons: Record<string, any> = { Service }
return icons[iconName] || Service
}
const scrollToBottom = () => {
nextTick(() => {
if (scrollbarRef.value) {
const wrap = scrollbarRef.value.wrapRef
if (wrap) {
wrap.scrollTop = wrap.scrollHeight
}
}
})
}
const closeAi = () => {
aiStore.isVisible = false
}
const handleNewChat = () => {
aiStore.startNewChat()
showHistory.value = false
inputMessage.value = ''
// scrollToBottom is called but list is empty, so it's fine
}
const toggleHistory = () => {
showHistory.value = !showHistory.value
}
const loadSession = (sessionId: string) => {
aiStore.loadSession(sessionId)
showHistory.value = false
scrollToBottom()
}
const deleteSession = (sessionId: string) => {
aiStore.deleteSession(sessionId)
}
const handleIdentityChange = (key: string) => {
aiStore.setIdentity(key)
showHistory.value = false
scrollToBottom()
}
const formatTime = (timestamp: number) => {
return dayjs(timestamp).format('MM-DD HH:mm')
}
const sendMessage = () => {
if (!inputMessage.value.trim()) return
const content = inputMessage.value
aiStore.addMessage('user', content)
inputMessage.value = ''
scrollToBottom()
// Simulate AI response
setTimeout(() => {
const identity = aiStore.currentIdentity
aiStore.addMessage('ai', `[${identity?.name || ''}] I received: "${content}". \n\n${identity?.systemPrompt || 'This is a simulated response.'}`)
scrollToBottom()
}, 1000)
}
watch(() => aiStore.messages.length, () => {
if (!showHistory.value) {
scrollToBottom()
}
})
onMounted(() => {
scrollToBottom()
})
</script>
<style lang="scss" scoped>
@use "@/styles/variables.scss" as *;
.ai-chat-wrapper {
display: flex;
flex-direction: column;
// flex: 1;
width: 100%;
height: 100%;
background-color: #fff;
overflow: hidden;
.ai-header {
height: 50px;
flex-shrink: 0;
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 15px;
background-color: #3b3b3b;
box-shadow: 0 1px 4px rgba(0, 21, 41, .08);
color: #fff;
z-index: 1;
.header-title {
font-size: 16px;
font-weight: 600;
cursor: pointer;
.el-dropdown-link {
color: #fff;
display: flex;
align-items: center;
gap: 5px;
}
}
.actions {
display: flex;
gap: 5px;
:deep(.el-button) {
color: #fff;
font-size: 18px;
padding: 8px;
&:hover {
background-color: rgba(255,255,255,0.1);
}
&.close-btn:hover {
background-color: #f56c6c;
}
}
}
}
.ai-content {
flex: 1;
overflow: hidden;
padding: 0;
position: relative;
.messages-list {
padding: 20px;
padding-bottom: 0;
}
.empty-chat {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
color: #909399;
margin-top: 50px;
font-size: 14px;
.empty-icon {
margin-bottom: 20px;
color: var(--el-color-primary);
background: var(--el-color-primary-light-9);
padding: 20px;
border-radius: 50%;
}
p {
margin: 5px 0;
&:first-of-type {
font-weight: bold;
font-size: 16px;
color: #606266;
margin-bottom: 10px;
}
}
}
.history-list {
padding: 15px;
height: 100%;
.empty-history {
text-align: center;
color: #909399;
margin-top: 50px;
font-size: 14px;
}
.history-item {
padding: 12px;
border-radius: 8px;
margin-bottom: 8px;
background-color: #f4f4f5;
cursor: pointer;
display: flex;
align-items: center;
justify-content: space-between;
transition: all 0.2s;
&:hover {
background-color: #e9e9eb;
}
&.active {
background-color: #ecf5ff;
border: 1px solid #c6e2ff;
}
.history-title {
font-size: 14px;
color: #303133;
flex: 1;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
margin-right: 10px;
}
.history-time {
font-size: 12px;
color: #909399;
margin-right: 8px;
}
.delete-btn {
color: #f56c6c;
opacity: 0;
transition: opacity 0.2s;
padding: 2px;
font-size: 16px;
&:hover {
background-color: rgba(245, 108, 108, 0.1);
}
}
&:hover .delete-btn {
opacity: 1;
}
}
}
.message-row {
display: flex;
margin-bottom: 20px;
gap: 12px;
&.user {
flex-direction: row-reverse;
.message-bubble {
background-color: #653eb3;
color: #fff;
border-radius: 12px 0 12px 12px;
border: none;
}
.user-avatar {
background-color: #653eb3;
}
}
&.ai {
.message-bubble {
background-color: #f4f4f5;
color: #303133;
border-radius: 0 12px 12px 12px;
border: 1px solid #e9e9eb;
}
.ai-avatar {
background-color: #909399;
}
}
.avatar {
flex-shrink: 0;
}
.message-bubble {
padding: 12px 16px;
max-width: 85%;
word-break: break-word;
font-size: 14px;
line-height: 1.6;
box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}
}
}
.ai-footer {
width: 100%;
min-height: 80px;
// justify-content: flex-end;
flex-shrink: 0;
padding: 15px;
border-top: 1px solid #eee;
background-color: #fff;
z-index: 1;
.input-area {
display: flex;
align-items: flex-end;
gap: 10px;
.chat-input {
flex: 1;
:deep(.el-textarea__inner) {
border-radius: 8px;
padding: 8px 12px;
resize: none;
box-shadow: none;
background-color: #f4f4f5;
border: 1px solid transparent;
scrollbar-width: none;
-ms-overflow-style: none;
&::-webkit-scrollbar {
display: none;
}
&:focus {
background-color: #fff;
box-shadow: 0 0 0 1px #653eb3 inset;
}
}
}
.send-btn {
margin-bottom: 2px;
background-color: #653eb3;
border-color: #653eb3;
color: white;
&:hover {
background-color: #7a5ac9;
border-color: #7a5ac9;
}
&.is-disabled {
background-color: #a0cfff;
border-color: #a0cfff;
opacity: 0.6;
}
}
}
}
}
</style>