297 lines
6.3 KiB
Vue
297 lines
6.3 KiB
Vue
|
<template>
|
||
|
<div class="mode-selector">
|
||
|
<div class="mode-header">
|
||
|
<h3 class="mode-title">对话场景</h3>
|
||
|
<el-dropdown trigger="hover" @command="handleHistorySelect">
|
||
|
<el-button class="history-btn" :icon="Document" text bg>
|
||
|
历史会话
|
||
|
</el-button>
|
||
|
<template #dropdown>
|
||
|
<el-dropdown-menu>
|
||
|
<template v-if="currentModeHistory.length">
|
||
|
<el-dropdown-item
|
||
|
v-for="chat in currentModeHistory"
|
||
|
:key="chat.conversationId"
|
||
|
:command="chat.conversationId"
|
||
|
class="history-item"
|
||
|
>
|
||
|
<div class="history-content">
|
||
|
<span class="history-title">{{ formatChatTitle(chat) }}</span>
|
||
|
<el-button
|
||
|
class="delete-btn"
|
||
|
:icon="Delete"
|
||
|
link
|
||
|
@click.stop="handleDelete(chat.conversationId)"
|
||
|
/>
|
||
|
</div>
|
||
|
</el-dropdown-item>
|
||
|
</template>
|
||
|
<el-dropdown-item v-else disabled>暂无历史会话</el-dropdown-item>
|
||
|
</el-dropdown-menu>
|
||
|
</template>
|
||
|
</el-dropdown>
|
||
|
</div>
|
||
|
<div class="mode-list">
|
||
|
<div
|
||
|
v-for="mode in chatModes"
|
||
|
:key="mode.id"
|
||
|
:class="['mode-item', { active: chatStore.currentMode === mode.id }]"
|
||
|
@click="selectMode(mode.id)"
|
||
|
>
|
||
|
<i :class="mode.icon"></i>
|
||
|
<span>{{ mode.name }}</span>
|
||
|
</div>
|
||
|
</div>
|
||
|
</div>
|
||
|
</template>
|
||
|
|
||
|
<script setup>
|
||
|
import { useChatStore } from '@/store/chat'
|
||
|
import { computed, ref } from 'vue'
|
||
|
import { Document, Delete } from '@element-plus/icons-vue'
|
||
|
import { ElDropdown, ElDropdownMenu, ElDropdownItem, ElButton, ElMessageBox } from 'element-plus'
|
||
|
|
||
|
const chatStore = useChatStore()
|
||
|
|
||
|
const chatModes = computed(() => {
|
||
|
return Object.keys(chatStore.chatModes).map(key => ({
|
||
|
id: key,
|
||
|
name: chatStore.chatModes[key].name,
|
||
|
icon: chatStore.chatModes[key].icon
|
||
|
}))
|
||
|
})
|
||
|
|
||
|
// 获取当前模式下的历史会话
|
||
|
const currentModeHistory = computed(() => {
|
||
|
return chatStore.conversations.filter(chat => chat.mode === chatStore.currentMode)
|
||
|
})
|
||
|
|
||
|
// 格式化会话标题
|
||
|
const formatChatTitle = (chat) => {
|
||
|
// 使用第一条消息作为标题,如果没有则使用默认标题
|
||
|
if (chat.messages && chat.messages.length > 0) {
|
||
|
const firstMessage = chat.messages[0].question
|
||
|
return firstMessage.length > 20 ? firstMessage.substring(0, 20) + '...' : firstMessage
|
||
|
}
|
||
|
return `会话 ${chat.conversationId.substring(0, 8)}`
|
||
|
}
|
||
|
|
||
|
const selectMode = (modeId) => {
|
||
|
chatStore.setCurrentMode(modeId)
|
||
|
}
|
||
|
|
||
|
const handleHistorySelect = (conversationId) => {
|
||
|
chatStore.switchConversation(conversationId)
|
||
|
}
|
||
|
|
||
|
const handleDelete = async (conversationId) => {
|
||
|
try {
|
||
|
await ElMessageBox.confirm(
|
||
|
'确定要删除这个会话吗?删除后无法恢复。',
|
||
|
'删除确认',
|
||
|
{
|
||
|
confirmButtonText: '确定',
|
||
|
cancelButtonText: '取消',
|
||
|
type: 'warning',
|
||
|
}
|
||
|
)
|
||
|
chatStore.deleteConversation(conversationId)
|
||
|
} catch {
|
||
|
// 用户取消删除
|
||
|
}
|
||
|
}
|
||
|
</script>
|
||
|
|
||
|
<style lang="scss" scoped>
|
||
|
$primary-color: #4CAF50;
|
||
|
$text-color: #333;
|
||
|
$text-color-dark: #fff;
|
||
|
$background-color: #ffffff;
|
||
|
$background-color-dark: #1e1e1e;
|
||
|
$border-color: #e0e0e0;
|
||
|
$border-color-dark: #333;
|
||
|
$hover-color: #f5f5f5;
|
||
|
$hover-color-dark: #2d2d2d;
|
||
|
|
||
|
.mode-selector {
|
||
|
width: 240px;
|
||
|
min-width: 240px;
|
||
|
background: $background-color;
|
||
|
border-right: 1px solid $border-color;
|
||
|
height: 100%;
|
||
|
padding: 20px 0;
|
||
|
box-shadow: 2px 0 5px rgba(0, 0, 0, 0.1);
|
||
|
|
||
|
.mode-header {
|
||
|
padding: 0 20px;
|
||
|
margin-bottom: 20px;
|
||
|
display: flex;
|
||
|
align-items: center;
|
||
|
justify-content: space-between;
|
||
|
gap: 10px;
|
||
|
|
||
|
.mode-title {
|
||
|
color: $text-color;
|
||
|
font-size: 16px;
|
||
|
font-weight: bold;
|
||
|
margin: 0;
|
||
|
}
|
||
|
|
||
|
.history-btn {
|
||
|
font-size: 14px;
|
||
|
height: 32px;
|
||
|
padding: 4px 12px;
|
||
|
color: $text-color;
|
||
|
transition: all 0.3s ease;
|
||
|
|
||
|
&:hover {
|
||
|
color: $primary-color;
|
||
|
background: rgba($primary-color, 0.1);
|
||
|
}
|
||
|
|
||
|
&:focus-visible {
|
||
|
outline: none;
|
||
|
box-shadow: none;
|
||
|
}
|
||
|
|
||
|
&:focus {
|
||
|
outline: none;
|
||
|
box-shadow: none;
|
||
|
}
|
||
|
|
||
|
:deep(.el-icon) {
|
||
|
font-size: 16px;
|
||
|
margin-right: 4px;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
.mode-list {
|
||
|
display: flex;
|
||
|
flex-direction: column;
|
||
|
gap: 5px;
|
||
|
}
|
||
|
|
||
|
.mode-item {
|
||
|
padding: 15px 20px;
|
||
|
display: flex;
|
||
|
align-items: center;
|
||
|
gap: 10px;
|
||
|
cursor: pointer;
|
||
|
transition: all 0.3s ease;
|
||
|
color: $text-color;
|
||
|
|
||
|
&:hover {
|
||
|
background: $hover-color;
|
||
|
}
|
||
|
|
||
|
&.active {
|
||
|
background: $primary-color;
|
||
|
color: white;
|
||
|
}
|
||
|
|
||
|
i {
|
||
|
font-size: 18px;
|
||
|
width: 24px;
|
||
|
text-align: center;
|
||
|
}
|
||
|
|
||
|
span {
|
||
|
font-size: 14px;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
:deep(.el-dropdown-menu__item) {
|
||
|
font-size: 13px;
|
||
|
padding: 8px 16px;
|
||
|
line-height: 1.4;
|
||
|
max-width: 300px;
|
||
|
overflow-x: hidden;
|
||
|
|
||
|
&:hover .delete-btn {
|
||
|
opacity: 1;
|
||
|
transform: translateX(0);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
:deep(.el-dropdown-menu) {
|
||
|
min-width: 200px;
|
||
|
max-width: 300px;
|
||
|
}
|
||
|
|
||
|
.history-content {
|
||
|
display: flex;
|
||
|
align-items: center;
|
||
|
justify-content: space-between;
|
||
|
gap: 8px;
|
||
|
width: 100%;
|
||
|
position: relative;
|
||
|
min-width: 0;
|
||
|
}
|
||
|
|
||
|
.history-title {
|
||
|
flex: 1;
|
||
|
white-space: nowrap;
|
||
|
overflow: hidden;
|
||
|
text-overflow: ellipsis;
|
||
|
margin-right: 24px;
|
||
|
min-width: 0;
|
||
|
}
|
||
|
|
||
|
.delete-btn {
|
||
|
position: absolute;
|
||
|
right: -8px;
|
||
|
opacity: 0;
|
||
|
transform: translateX(10px);
|
||
|
transition: all 0.3s ease;
|
||
|
height: 24px;
|
||
|
width: 24px;
|
||
|
padding: 0;
|
||
|
color: #f56c6c;
|
||
|
flex-shrink: 0;
|
||
|
|
||
|
&:hover {
|
||
|
color: #f56c6c;
|
||
|
background-color: rgba(245, 108, 108, 0.1);
|
||
|
}
|
||
|
|
||
|
:deep(.el-icon) {
|
||
|
font-size: 14px;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@media (prefers-color-scheme: dark) {
|
||
|
.mode-selector {
|
||
|
background: $background-color-dark;
|
||
|
border-right-color: $border-color-dark;
|
||
|
|
||
|
.mode-title {
|
||
|
color: $text-color-dark;
|
||
|
}
|
||
|
|
||
|
.mode-item {
|
||
|
color: $text-color-dark;
|
||
|
|
||
|
&:hover {
|
||
|
background: $hover-color-dark;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
.history-btn {
|
||
|
color: $text-color-dark;
|
||
|
|
||
|
&:hover {
|
||
|
color: $primary-color;
|
||
|
background: rgba($primary-color, 0.15);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
.delete-btn {
|
||
|
&:hover {
|
||
|
background-color: rgba(245, 108, 108, 0.15);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
</style>
|