refactor: 助手记忆页改用 VXE 表格与 Alert 对齐系统列表风格

This commit is contained in:
2026-09-25 00:35:36 +08:00
parent 41f022bd63
commit 0b460e22b0
3 changed files with 188 additions and 68 deletions
+3 -3
View File
@@ -114,7 +114,7 @@ const MemoryIcon = createIconifyIcon('lucide:brain');
// 意见反馈图标
const FeedbackIcon = createIconifyIcon('lucide:message-square-plus');
/** 是否具备智能助手聊天权限(用于「我的记忆」入口;含超管) */
/** 是否具备智能助手聊天权限(用于「助手记忆」入口;含超管) */
const canUseAgentChat = computed(() => hasPermission('agent:chat'));
/** 当前用户可切换的可用公司数 */
@@ -134,14 +134,14 @@ const menus = computed(() => {
icon: UserIcon,
text: '个人中心',
});
// 我的记忆(有助手聊天权才显示)
// 助手记忆(有助手聊天权才显示)
if (canUseAgentChat.value) {
menuItems.push({
handler: () => {
router.push('/agent/memories');
},
icon: MemoryIcon,
text: '我的记忆',
text: '助手记忆',
});
}
// 意见反馈
@@ -1,7 +1,7 @@
import type { RouteRecordRaw } from 'vue-router';
/**
* 用户记忆管理(头像下拉入口;不进侧栏菜单)
* 助手长期记忆管理(头像下拉「助手记忆」入口;不进侧栏菜单)
*/
const staticRoutes: RouteRecordRaw[] = [
{
@@ -10,7 +10,7 @@ const staticRoutes: RouteRecordRaw[] = [
component: () => import('#/views/agent/memories/index.vue'),
meta: {
icon: 'lucide:brain',
title: '我的记忆',
title: '助手记忆',
hideInMenu: true,
ignoreAccess: true,
},
@@ -1,10 +1,16 @@
<script lang="ts" setup>
import { onMounted, ref } from 'vue';
/**
* 助手长期记忆:Page + VXE 表格(与反馈/API Key 等列表页一致)。
*/
import type { VxeGridProps } from '#/adapter/vxe-table';
import { Page } from '@vben/common-ui';
import { ref } from 'vue';
import { Button, Card, Empty, Spin, message } from 'ant-design-vue';
import { EllipsisText, Page } from '@vben/common-ui';
import { Alert, Button, Modal, Popconfirm, Space, Tag, message } from 'ant-design-vue';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import {
deleteAgentMemoryApi,
listAgentMemoriesApi,
@@ -13,79 +19,193 @@ import {
defineOptions({ name: 'AgentMemories' });
const loading = ref(true);
const memories = ref<AgentVO.Memory[]>([]);
const loading = ref(false);
async function loadMemories() {
loading.value = true;
try {
const res = await listAgentMemoriesApi();
memories.value = res.items || [];
} catch (e: any) {
message.error(e?.message || '加载记忆失败');
} finally {
loading.value = false;
}
const KIND_META: Record<string, { color: string; label: string }> = {
note: { color: 'blue', label: '笔记' },
preference: { color: 'purple', label: '偏好' },
fact: { color: 'green', label: '事实' },
};
function kindMeta(kind?: string) {
const k = (kind || 'note').trim() || 'note';
return KIND_META[k] || { color: 'default', label: k };
}
async function removeMemory(id: string) {
function formatTime(raw?: string) {
if (!raw) return '—';
// 后端多为 ISO;展示时去掉过长毫秒/Z
return raw.replace('T', ' ').replace(/\.\d+Z?$/, '').replace(/Z$/, '');
}
const gridOptions: VxeGridProps<AgentVO.Memory> = {
columns: [
{ type: 'seq', width: 56, title: '序号' },
{
field: 'kind',
title: '类型',
width: 96,
slots: { default: 'kind' },
},
{
field: 'summary',
title: '摘要',
minWidth: 200,
slots: { default: 'summary' },
},
{
field: 'text',
title: '原文',
minWidth: 240,
slots: { default: 'text' },
},
{
field: 'key',
title: '键',
width: 120,
showOverflow: 'tooltip',
formatter: ({ cellValue }) => cellValue || '—',
},
{
field: 'updatedAt',
title: '更新时间',
width: 176,
formatter: ({ cellValue }) => formatTime(cellValue),
},
{
title: '操作',
width: 96,
fixed: 'right',
slots: { default: 'action' },
},
],
height: 'auto',
border: true,
stripe: true,
rowConfig: { isHover: true },
showOverflow: true,
size: 'small',
toolbarConfig: {
custom: true,
refresh: true,
zoom: true,
},
pagerConfig: {
enabled: true,
pageSizes: [10, 20, 50],
layouts: ['Total', 'Sizes', 'PrevPage', 'Number', 'NextPage'],
},
proxyConfig: {
ajax: {
query: async ({ page }) => {
loading.value = true;
try {
const res = await listAgentMemoriesApi();
const all = res.items || [];
const pageSize = page.pageSize || 20;
const current = page.currentPage || 1;
const start = (current - 1) * pageSize;
return {
items: all.slice(start, start + pageSize),
total: all.length,
};
} catch (e: any) {
message.error(e?.message || '加载记忆失败');
return { items: [], total: 0 };
} finally {
loading.value = false;
}
},
},
},
};
const [Grid, gridApi] = useVbenVxeGrid({ gridOptions });
async function removeMemory(row: AgentVO.Memory) {
try {
await deleteAgentMemoryApi(id);
memories.value = memories.value.filter((m) => m.id !== id);
message.success('已删除记忆');
await deleteAgentMemoryApi(row.id);
message.success('已删除');
await gridApi.reload();
} catch (e: any) {
message.error(e?.message || '删除失败');
}
}
onMounted(() => {
void loadMemories();
});
function clearAll() {
Modal.confirm({
title: '清空全部助手记忆?',
content: '将逐条删除当前列表中的记忆,且不可恢复。',
okText: '清空',
okType: 'danger',
cancelText: '取消',
async onOk() {
const res = await listAgentMemoriesApi();
const items = res.items || [];
for (const m of items) {
await deleteAgentMemoryApi(m.id);
}
message.success(items.length ? `已清空 ${items.length} 条` : '暂无记忆');
await gridApi.reload();
},
});
}
</script>
<template>
<Page auto-content-height>
<div class="relative mx-auto max-w-3xl pb-8">
<div
v-if="loading"
class="absolute inset-0 z-10 flex items-center justify-center bg-background/50"
>
<Spin />
</div>
<Page
auto-content-height
title="助手记忆"
description="智能助手跨会话保存的称呼、偏好与事实,按需检索,不会默认整包注入对话。"
>
<Alert
type="info"
show-icon
class="mb-4"
message="在智能助手里发送「记住:……」即可写入;不需要的条目可随时删除。"
/>
<Card title="我的记忆" :bordered="false">
<p class="text-muted-foreground mb-4 text-sm">
在智能助手对话中发送「记住:…」可写入;助手按需检索,不会默认注入全部记忆。
</p>
<Empty
v-if="!loading && memories.length === 0"
description="暂无记忆"
/>
<div
v-for="m in memories"
:key="m.id"
class="mb-2 flex items-start gap-2 rounded-md bg-muted/50 px-3 py-2 text-sm"
<Grid :loading="loading">
<template #toolbar-tools>
<Space>
<Button danger @click="clearAll">清空全部</Button>
</Space>
</template>
<template #kind="{ row }">
<Tag :color="kindMeta(row.kind).color">
{{ kindMeta(row.kind).label }}
</Tag>
</template>
<template #summary="{ row }">
<EllipsisText :max-width="360" tooltip-when-ellipsis>
{{ row.summary || row.text || '—' }}
</EllipsisText>
</template>
<template #text="{ row }">
<EllipsisText
v-if="row.summary && row.text && row.summary !== row.text"
:max-width="420"
tooltip-when-ellipsis
>
<div class="min-w-0 flex-1">
<div class="break-words">{{ m.summary || m.text }}</div>
<div
v-if="m.summary && m.text && m.summary !== m.text"
class="text-muted-foreground mt-1 break-words text-xs"
>
{{ m.text }}
</div>
<div
v-if="m.updatedAt"
class="text-muted-foreground mt-1 text-xs"
>
{{ m.updatedAt }}
</div>
</div>
<Button type="link" size="small" danger @click="removeMemory(m.id)">
删除
</Button>
</div>
</Card>
</div>
{{ row.text }}
</EllipsisText>
<span v-else class="text-muted-foreground">—</span>
</template>
<template #action="{ row }">
<Popconfirm
title="确定删除这条记忆?"
ok-text="删除"
cancel-text="取消"
ok-type="danger"
@confirm="removeMemory(row)"
>
<Button type="link" size="small" danger>删除</Button>
</Popconfirm>
</template>
</Grid>
</Page>
</template>