refactor: 智能助手侧栏支持收起图标轨与居中可拖拽会话搜索

This commit is contained in:
2026-09-25 23:56:19 +08:00
parent a72a43cffc
commit 67d01c678a
2 changed files with 339 additions and 51 deletions
@@ -2,6 +2,7 @@
import { computed, nextTick, onBeforeUnmount, ref, watch } from 'vue';
import { EllipsisText, Page } from '@vben/common-ui';
import { createIconifyIcon } from '@vben/icons';
import {
BarChartOutlined,
CalendarOutlined,
@@ -11,7 +12,7 @@ import {
} from '@ant-design/icons-vue';
import dayjs from 'dayjs';
import { Empty, Select, Spin, Tooltip, message } from 'ant-design-vue';
import { Empty, Popover, Select, Spin, Tooltip, message } from 'ant-design-vue';
import { ChatItem } from '@tdesign-vue-next/chat';
import 'tdesign-vue-next/es/style/index.css';
import '@tdesign-vue-next/chat/es/style/index.css';
@@ -27,6 +28,7 @@ import {
} from '#/api/agent';
import CatalogCiteSender from './catalog-cite-sender.vue';
import SessionSearchModal from './session-search-modal.vue';
import {
composeMessageWithCites,
stripTrailingMention,
@@ -35,6 +37,15 @@ import {
defineOptions({ name: 'AgentChat' });
const AgentBrandIcon = createIconifyIcon('lucide:bot');
const SearchIcon = createIconifyIcon('lucide:search');
const PanelCloseIcon = createIconifyIcon('lucide:panel-left-close');
const PanelOpenIcon = createIconifyIcon('lucide:panel-left');
const NewChatIcon = createIconifyIcon('lucide:square-pen');
const RecentChatIcon = createIconifyIcon('lucide:message-square');
const SIDER_COLLAPSED_KEY = 'agent-chat-sider-collapsed';
/** 空态快捷 chips:短标签展示,点击填入完整提问 */
const starterPrompts = [
{
@@ -77,6 +88,14 @@ const sessions = ref<AgentVO.Session[]>([]);
const activeThreadId = ref<string | null>(null);
/** 点了「新对话」但还没发出第一条消息,此时不请求后端 */
const composingNew = ref(false);
/** 侧栏收起状态(localStorage 持久化) */
const siderCollapsed = ref(
typeof localStorage !== 'undefined' &&
localStorage.getItem(SIDER_COLLAPSED_KEY) === '1',
);
/** 收起态:最近会话浮层 */
const collapsedRecentOpen = ref(false);
const sessionSearchModalRef = ref<{ open: () => void } | null>(null);
const messages = ref<AgentVO.Message[]>([]);
/** 当前流式助手正文(与 messages 末条同步) */
const streamingText = ref('');
@@ -157,9 +176,42 @@ function sessionTime(s: AgentVO.Session) {
return d?.isValid() ? d : null;
}
function setSiderCollapsed(next: boolean) {
siderCollapsed.value = next;
try {
localStorage.setItem(SIDER_COLLAPSED_KEY, next ? '1' : '0');
} catch {
/* ignore */
}
if (!next) {
collapsedRecentOpen.value = false;
}
}
/** 居中弹窗搜索会话(展开/收起侧栏共用入口) */
function openSessionSearch() {
sessionSearchModalRef.value?.open();
}
function onCollapsedRecentOpenChange(open: boolean) {
collapsedRecentOpen.value = open;
}
/** 收起态浮层 / 搜索弹窗点选会话 */
function pickSessionFromOverlay(threadId: string) {
collapsedRecentOpen.value = false;
onSessionClick(threadId);
}
/** 收起态「最近」列表(按接口顺序,最多 20 条) */
const recentSessions = computed(() => sessions.value.slice(0, 20));
/** 侧栏按「今天 / 昨天 / 近7天 / 更早」分组,对齐常见助手布局 */
const sessionGroups = computed<SessionGroup[]>(() => {
const buckets: Record<string, AgentVO.Session[]> = {
const buckets: Record<
'today' | 'yesterday' | 'week' | 'earlier',
AgentVO.Session[]
> = {
today: [],
yesterday: [],
week: [],
@@ -180,14 +232,14 @@ const sessionGroups = computed<SessionGroup[]>(() => {
buckets.earlier.push(s);
}
}
const labels: Array<[string, string]> = [
const labels: Array<[keyof typeof buckets, string]> = [
['today', '今天'],
['yesterday', '昨天'],
['week', '近 7 天'],
['earlier', '更早'],
];
return labels
.map(([key, label]) => ({ key, label, items: buckets[key]! }))
.map(([key, label]) => ({ key, label, items: buckets[key] }))
.filter((g) => g.items.length > 0);
});
@@ -542,67 +594,190 @@ void loadEnabledModels().then(() => refreshSessions());
<template>
<Page auto-content-height content-class="!p-0">
<div class="agent-shell flex h-full min-h-0 w-full overflow-hidden">
<!-- 左轨:新对话 + 按日分组的历史 -->
<aside class="agent-sider flex w-[260px] shrink-0 flex-col border-r border-border">
<div class="flex items-center gap-2 px-3 pb-2 pt-3">
<button
type="button"
class="hover:bg-muted flex h-9 flex-1 items-center gap-2 rounded-lg px-3 text-sm font-medium transition-colors"
@click="beginNewChat"
>
<PlusOutlined />
新对话
</button>
</div>
<div class="relative min-h-0 flex-1">
<div
v-if="loadingSessions"
class="absolute inset-0 z-10 flex items-center justify-center bg-background/50"
>
<Spin size="small" />
<!-- 左轨:顶栏标题/搜索/收起;收起后为图标轨(展开/新建/搜索/最近) -->
<aside
class="agent-sider flex shrink-0 flex-col border-r border-border transition-[width] duration-200"
:class="siderCollapsed ? 'w-[56px]' : 'w-[260px]'"
>
<div
v-if="!siderCollapsed"
class="flex h-12 shrink-0 items-center gap-1 px-3"
>
<div class="flex min-w-0 flex-1 items-center gap-2 truncate text-sm font-semibold">
<AgentBrandIcon class="size-5 shrink-0" />
<span class="truncate">智能助手</span>
</div>
<Empty
v-if="!loadingSessions && sessions.length === 0 && !composingNew"
class="mt-12 px-3"
description="暂无会话"
/>
<div v-else class="h-full overflow-y-auto px-2 pb-4">
<Tooltip title="搜索会话">
<button
v-if="composingNew"
type="button"
class="bg-muted mb-3 w-full rounded-lg px-3 py-2 text-left text-sm font-medium"
class="hover:bg-muted text-muted-foreground hover:text-foreground flex size-9 items-center justify-center rounded-lg transition-colors"
aria-label="搜索会话"
@click="openSessionSearch"
>
<SearchIcon class="size-5" />
</button>
</Tooltip>
<Tooltip title="收起侧栏">
<button
type="button"
class="hover:bg-muted text-muted-foreground hover:text-foreground flex size-9 items-center justify-center rounded-lg transition-colors"
aria-label="收起侧栏"
@click="setSiderCollapsed(true)"
>
<PanelCloseIcon class="size-5" />
</button>
</Tooltip>
</div>
<div
v-else
class="flex min-h-0 flex-1 flex-col items-center gap-1 px-1.5 pt-0 pb-3"
>
<Tooltip title="展开侧栏" placement="right">
<button
type="button"
class="agent-sider-brand-btn hover:bg-muted group flex size-10 items-center justify-center rounded-lg transition-colors"
aria-label="展开侧栏"
@click="setSiderCollapsed(false)"
>
<AgentBrandIcon class="size-5 group-hover:hidden" />
<PanelOpenIcon class="hidden size-5 group-hover:block" />
</button>
</Tooltip>
<Tooltip title="新对话" placement="right">
<button
type="button"
class="hover:bg-muted text-muted-foreground hover:text-foreground flex size-10 items-center justify-center rounded-lg transition-colors"
aria-label="新对话"
@click="beginNewChat"
>
<NewChatIcon class="size-5" />
</button>
</Tooltip>
<Tooltip title="搜索会话" placement="right">
<button
type="button"
class="hover:bg-muted text-muted-foreground hover:text-foreground flex size-10 items-center justify-center rounded-lg transition-colors"
aria-label="搜索会话"
@click="openSessionSearch"
>
<SearchIcon class="size-5" />
</button>
</Tooltip>
<Popover
:open="collapsedRecentOpen"
trigger="click"
placement="rightTop"
:overlay-style="{ padding: 0 }"
@update:open="onCollapsedRecentOpenChange"
>
<template #content>
<div class="agent-sider-pop flex w-64 flex-col p-2">
<div class="text-muted-foreground px-1 pb-2 text-xs font-medium">
最近聊天
</div>
<div class="max-h-72 overflow-y-auto">
<Empty
v-if="recentSessions.length === 0"
class="py-6"
description="暂无会话"
:image="Empty.PRESENTED_IMAGE_SIMPLE"
/>
<button
v-for="s in recentSessions"
:key="s.threadId"
type="button"
class="mb-0.5 w-full rounded-lg px-2.5 py-2 text-left text-sm transition-colors"
:class="
s.threadId === activeThreadId
? 'bg-muted font-medium'
: 'hover:bg-muted/70'
"
@click="pickSessionFromOverlay(s.threadId)"
>
<EllipsisText :tooltip-when-ellipsis="true" class="w-full">
{{ s.title || '新对话' }}
</EllipsisText>
</button>
</div>
</div>
</template>
<Tooltip
:title="collapsedRecentOpen ? '' : '最近聊天'"
placement="right"
>
<button
type="button"
class="hover:bg-muted text-muted-foreground hover:text-foreground flex size-10 items-center justify-center rounded-lg transition-colors"
:class="collapsedRecentOpen ? 'bg-muted text-foreground' : ''"
aria-label="最近聊天"
>
<RecentChatIcon class="size-5" />
</button>
</Tooltip>
</Popover>
</div>
<template v-if="!siderCollapsed">
<div class="px-3 pb-2">
<button
type="button"
class="hover:bg-muted flex h-9 w-full items-center gap-2 rounded-lg px-3 text-sm font-medium transition-colors"
@click="beginNewChat"
>
<PlusOutlined />
新对话
</button>
</div>
<div class="relative min-h-0 flex-1">
<div
v-for="group in sessionGroups"
:key="group.key"
class="mb-3"
v-if="loadingSessions"
class="absolute inset-0 z-10 flex items-center justify-center bg-background/50"
>
<div
class="text-muted-foreground px-3 pb-1 pt-2 text-xs font-medium"
>
{{ group.label }}
</div>
<Spin size="small" />
</div>
<Empty
v-if="!loadingSessions && sessions.length === 0 && !composingNew"
class="mt-12 px-3"
description="暂无会话"
/>
<div v-else class="h-full overflow-y-auto px-2 pb-4">
<button
v-for="s in group.items"
:key="s.threadId"
v-if="composingNew"
type="button"
class="mb-0.5 w-full rounded-lg px-3 py-2 text-left text-sm transition-colors"
:class="
s.threadId === activeThreadId
? 'bg-muted font-medium'
: 'hover:bg-muted/70 text-foreground'
"
@click="onSessionClick(s.threadId)"
class="bg-muted mb-3 w-full rounded-lg px-3 py-2 text-left text-sm font-medium"
>
<EllipsisText :tooltip-when-ellipsis="true" class="w-full">
{{ s.title || '新对话' }}
</EllipsisText>
新对话
</button>
<div
v-for="group in sessionGroups"
:key="group.key"
class="mb-3"
>
<div
class="text-muted-foreground px-3 pb-1 pt-2 text-xs font-medium"
>
{{ group.label }}
</div>
<button
v-for="s in group.items"
:key="s.threadId"
type="button"
class="mb-0.5 w-full rounded-lg px-3 py-2 text-left text-sm transition-colors"
:class="
s.threadId === activeThreadId
? 'bg-muted font-medium'
: 'hover:bg-muted/70 text-foreground'
"
@click="onSessionClick(s.threadId)"
>
<EllipsisText :tooltip-when-ellipsis="true" class="w-full">
{{ s.title || '新对话' }}
</EllipsisText>
</button>
</div>
</div>
</div>
</div>
</template>
</aside>
<!-- 主区:空态居中 / 对话时消息 + 底栏输入 -->
@@ -713,6 +888,12 @@ void loadEnabledModels().then(() => refreshSessions());
</template>
</section>
</div>
<SessionSearchModal
ref="sessionSearchModalRef"
:sessions="sessions"
:active-thread-id="activeThreadId"
@pick="pickSessionFromOverlay"
/>
</Page>
</template>
@@ -0,0 +1,107 @@
<script lang="ts" setup>
/**
* 会话搜索弹窗:居中打开,按标题过滤;点选后回传 threadId。
*/
import { computed, nextTick, ref } from 'vue';
import { EllipsisText, useVbenModal } from '@vben/common-ui';
import { Empty, Input } from 'ant-design-vue';
import type { AgentVO } from '#/api/agent';
defineOptions({ name: 'AgentSessionSearchModal' });
const props = defineProps<{
sessions: AgentVO.Session[];
activeThreadId?: string | null;
}>();
const emit = defineEmits<{
pick: [threadId: string];
}>();
const query = ref('');
const inputRef = ref<{ focus?: () => void } | null>(null);
const hits = computed(() => {
const q = query.value.trim().toLowerCase();
const list = props.sessions || [];
if (!q) return list.slice(0, 30);
return list
.filter((s) => (s.title || '新对话').toLowerCase().includes(q))
.slice(0, 30);
});
const [Modal, modalApi] = useVbenModal({
title: '搜索会话',
class: 'w-[520px]',
draggable: true,
footer: false,
showConfirmButton: false,
showCancelButton: false,
onOpenChange(isOpen: boolean) {
if (!isOpen) {
query.value = '';
return;
}
query.value = '';
void nextTick(() => inputRef.value?.focus?.());
},
});
function open() {
modalApi.open();
}
function pick(threadId: string) {
emit('pick', threadId);
modalApi.close();
}
defineExpose({ open });
</script>
<template>
<Modal>
<div class="flex flex-col gap-3">
<Input
ref="inputRef"
v-model:value="query"
allow-clear
placeholder="按会话标题搜索"
size="large"
/>
<div class="max-h-[360px] min-h-[120px] overflow-y-auto">
<Empty
v-if="hits.length === 0"
class="py-10"
:description="query.trim() ? '无匹配会话' : '暂无会话'"
:image="Empty.PRESENTED_IMAGE_SIMPLE"
/>
<button
v-for="s in hits"
:key="s.threadId"
type="button"
class="mb-0.5 w-full rounded-lg px-3 py-2.5 text-left text-sm transition-colors"
:class="
s.threadId === activeThreadId
? 'bg-muted font-medium'
: 'hover:bg-muted/70'
"
@click="pick(s.threadId)"
>
<EllipsisText :tooltip-when-ellipsis="true" class="w-full">
{{ s.title || '新对话' }}
</EllipsisText>
<div
v-if="s.updatedAt || s.lastMessageAt"
class="text-muted-foreground mt-0.5 text-xs"
>
{{ s.updatedAt || s.lastMessageAt }}
</div>
</button>
</div>
</div>
</Modal>
</template>