mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 21:09:19 +08:00
feat: aichat 改用 tdesign chat
This commit is contained in:
@@ -26,6 +26,7 @@
|
||||
"#/*": "./src/*"
|
||||
},
|
||||
"dependencies": {
|
||||
"@tdesign-vue-next/chat": "^0.7.0",
|
||||
"@vben/access": "workspace:*",
|
||||
"@vben/common-ui": "workspace:*",
|
||||
"@vben/constants": "workspace:*",
|
||||
@@ -42,20 +43,16 @@
|
||||
"@vben/utils": "workspace:*",
|
||||
"@vueuse/core": "catalog:",
|
||||
"ant-design-vue": "catalog:",
|
||||
"ant-design-x-vue": "^1.6.0",
|
||||
"dayjs": "catalog:",
|
||||
"dompurify": "^3.4.15",
|
||||
"marked": "^18.0.13",
|
||||
"papaparse": "^5.4.1",
|
||||
"pinia": "catalog:",
|
||||
"spark-md5": "^3.0.2",
|
||||
"tdesign-vue-next": "^1.20.8",
|
||||
"vue": "catalog:",
|
||||
"vue-router": "catalog:",
|
||||
"vue-waterfall-plugin-next": "^3.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/dompurify": "^3.2.0",
|
||||
"@types/marked": "^6.0.0",
|
||||
"@types/papaparse": "^5.3.14",
|
||||
"@types/spark-md5": "^3.0.5"
|
||||
}
|
||||
|
||||
@@ -1,19 +1,8 @@
|
||||
<script lang="ts" setup>
|
||||
import type {
|
||||
BubbleListProps,
|
||||
BubbleProps,
|
||||
Conversation,
|
||||
} from 'ant-design-x-vue';
|
||||
|
||||
import { computed, h, nextTick, onBeforeUnmount, ref, watch } from 'vue';
|
||||
import { computed, nextTick, onBeforeUnmount, ref, watch } from 'vue';
|
||||
|
||||
import { Page } from '@vben/common-ui';
|
||||
import {
|
||||
PlusOutlined,
|
||||
RobotOutlined,
|
||||
SettingOutlined,
|
||||
UserOutlined,
|
||||
} from '@ant-design/icons-vue';
|
||||
import { PlusOutlined, SettingOutlined } from '@ant-design/icons-vue';
|
||||
|
||||
import {
|
||||
Button,
|
||||
@@ -26,7 +15,9 @@ import {
|
||||
Spin,
|
||||
message,
|
||||
} from 'ant-design-vue';
|
||||
import { BubbleList, Conversations, Sender } from 'ant-design-x-vue';
|
||||
import { ChatItem, ChatSender } from '@tdesign-vue-next/chat';
|
||||
import 'tdesign-vue-next/es/style/index.css';
|
||||
import '@tdesign-vue-next/chat/es/style/index.css';
|
||||
|
||||
import {
|
||||
createAgentSessionApi,
|
||||
@@ -40,10 +31,17 @@ import {
|
||||
updateAgentLlmSettingsApi,
|
||||
} from '#/api/agent';
|
||||
|
||||
import { renderAgentMarkdown } from './markdown';
|
||||
|
||||
defineOptions({ name: 'AgentChat' });
|
||||
|
||||
/** 消息气泡视图模型(走 Vue ChatItem,避免 ChatList→WebComponent 丢正文) */
|
||||
type BubbleView = {
|
||||
id: string;
|
||||
role: 'user' | 'assistant';
|
||||
name: string;
|
||||
content: string;
|
||||
textLoading: boolean;
|
||||
};
|
||||
|
||||
const loadingSessions = ref(false);
|
||||
const loadingMessages = ref(false);
|
||||
const sending = ref(false);
|
||||
@@ -63,12 +61,7 @@ const llmForm = ref({
|
||||
apiKeyMasked: '' as string | null,
|
||||
configured: false,
|
||||
});
|
||||
const bubbleListRef = ref<{
|
||||
scrollTo?: (info: {
|
||||
key?: string | number;
|
||||
block?: ScrollLogicalPosition;
|
||||
}) => void;
|
||||
} | null>(null);
|
||||
const messagePaneRef = ref<HTMLElement | null>(null);
|
||||
let abort: AbortController | null = null;
|
||||
let messagesLoadSeq = 0;
|
||||
|
||||
@@ -80,79 +73,38 @@ const showMessageLoading = computed(
|
||||
() => loadingMessages.value && !sending.value && messages.value.length === 0,
|
||||
);
|
||||
|
||||
const renderAssistantMarkdown: BubbleProps['messageRender'] = (content) => {
|
||||
const html = renderAgentMarkdown(String(content ?? ''));
|
||||
return h('div', { class: 'agent-md', innerHTML: html || '…' });
|
||||
};
|
||||
|
||||
/** Bubble.List 角色预设:SSE 已流式输出,关闭组件自带 typing 动画 */
|
||||
const bubbleRoles: BubbleListProps['roles'] = {
|
||||
assistant: {
|
||||
placement: 'start',
|
||||
variant: 'filled',
|
||||
avatar: {
|
||||
icon: h(RobotOutlined),
|
||||
style: { background: '#1677ff', color: '#fff' },
|
||||
},
|
||||
messageRender: renderAssistantMarkdown,
|
||||
styles: { content: { maxWidth: 'min(720px, 85%)' } },
|
||||
},
|
||||
user: {
|
||||
placement: 'end',
|
||||
variant: 'filled',
|
||||
avatar: {
|
||||
icon: h(UserOutlined),
|
||||
style: { background: '#87d068', color: '#fff' },
|
||||
},
|
||||
styles: { content: { maxWidth: 'min(720px, 85%)' } },
|
||||
},
|
||||
};
|
||||
|
||||
const bubbleItems = computed<BubbleListProps['items']>(() =>
|
||||
const bubbleViews = computed<BubbleView[]>(() =>
|
||||
messages.value.map((m, idx) => {
|
||||
const isStreamingAssistant =
|
||||
sending.value &&
|
||||
idx === messages.value.length - 1 &&
|
||||
m.role === 'assistant';
|
||||
const content = isStreamingAssistant
|
||||
const text = isStreamingAssistant
|
||||
? streamingText.value || m.content || ''
|
||||
: m.content || '';
|
||||
const role = m.role === 'user' ? 'user' : 'assistant';
|
||||
return {
|
||||
key: m.id || `${m.seq}-${idx}`,
|
||||
id: m.id || `${m.seq}-${idx}`,
|
||||
role,
|
||||
content,
|
||||
loading: isStreamingAssistant && !content,
|
||||
name: role === 'user' ? '我' : '智能助手',
|
||||
content: text,
|
||||
textLoading: isStreamingAssistant && !text,
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
||||
/** 侧栏会话列表:映射为 Conversations items(label 用字符串,避免 Typography ellipsis+children 告警) */
|
||||
const conversationItems = computed<Conversation[]>(() =>
|
||||
sessions.value.map((s) => {
|
||||
const ts = s.updatedAt || s.lastMessageAt || s.createdAt;
|
||||
const timeMs = ts ? Date.parse(ts) : undefined;
|
||||
return {
|
||||
key: s.threadId,
|
||||
label: s.title || '新对话',
|
||||
timestamp: Number.isFinite(timeMs) ? timeMs : undefined,
|
||||
};
|
||||
}),
|
||||
);
|
||||
function scrollChatToBottom() {
|
||||
const el = messagePaneRef.value;
|
||||
if (!el) return;
|
||||
el.scrollTop = el.scrollHeight;
|
||||
}
|
||||
|
||||
function onConversationActiveChange(key: string) {
|
||||
function onSessionClick(threadId: string) {
|
||||
if (sending.value) {
|
||||
message.warning('回复生成中,请稍候再切换会话');
|
||||
return;
|
||||
}
|
||||
activeThreadId.value = key;
|
||||
}
|
||||
|
||||
function scrollBubblesToBottom() {
|
||||
const items = bubbleItems.value || [];
|
||||
const last = items[items.length - 1];
|
||||
if (last?.key == null) return;
|
||||
bubbleListRef.value?.scrollTo?.({ key: last.key, block: 'end' });
|
||||
activeThreadId.value = threadId;
|
||||
}
|
||||
|
||||
async function refreshSessions() {
|
||||
@@ -185,7 +137,7 @@ async function loadMessages(threadId: string) {
|
||||
messages.value = res.items || [];
|
||||
streamingText.value = '';
|
||||
await nextTick();
|
||||
scrollBubblesToBottom();
|
||||
scrollChatToBottom();
|
||||
} catch (e: any) {
|
||||
if (seq === messagesLoadSeq && !sending.value) {
|
||||
message.error(e?.message || '加载消息失败');
|
||||
@@ -281,7 +233,7 @@ function appendStreamToken(t: string) {
|
||||
if (last?.role === 'assistant') {
|
||||
messages.value[idx] = { ...last, content: streamingText.value };
|
||||
}
|
||||
void nextTick().then(scrollBubblesToBottom);
|
||||
void nextTick().then(scrollChatToBottom);
|
||||
}
|
||||
|
||||
async function send(textOverride?: string) {
|
||||
@@ -309,7 +261,7 @@ async function send(textOverride?: string) {
|
||||
content: '',
|
||||
});
|
||||
await nextTick();
|
||||
scrollBubblesToBottom();
|
||||
scrollChatToBottom();
|
||||
|
||||
abort?.abort();
|
||||
abort = streamAgentChat({
|
||||
@@ -340,11 +292,11 @@ async function send(textOverride?: string) {
|
||||
});
|
||||
}
|
||||
|
||||
function onSenderSubmit(msg: string) {
|
||||
void send(msg);
|
||||
function onSenderSend(value: string) {
|
||||
void send(value);
|
||||
}
|
||||
|
||||
function onSenderCancel() {
|
||||
function onSenderStop() {
|
||||
abort?.abort();
|
||||
sending.value = false;
|
||||
}
|
||||
@@ -374,17 +326,32 @@ void refreshSessions();
|
||||
<Spin size="small" />
|
||||
</div>
|
||||
<Empty
|
||||
v-if="!loadingSessions && conversationItems.length === 0"
|
||||
v-if="!loadingSessions && sessions.length === 0"
|
||||
class="mt-10 px-2"
|
||||
description="暂无会话"
|
||||
/>
|
||||
<Conversations
|
||||
v-else
|
||||
class="agent-conversations h-full overflow-y-auto px-3 py-2"
|
||||
:items="conversationItems"
|
||||
:active-key="activeThreadId ?? undefined"
|
||||
@active-change="onConversationActiveChange"
|
||||
/>
|
||||
<div v-else class="h-full space-y-1 overflow-y-auto px-3 py-2">
|
||||
<button
|
||||
v-for="s in sessions"
|
||||
:key="s.threadId"
|
||||
type="button"
|
||||
class="w-full rounded-lg px-3 py-2 text-left text-sm transition-colors"
|
||||
:class="
|
||||
s.threadId === activeThreadId
|
||||
? 'bg-primary/10 text-primary'
|
||||
: 'hover:bg-muted text-foreground'
|
||||
"
|
||||
@click="onSessionClick(s.threadId)"
|
||||
>
|
||||
<div class="truncate font-medium">{{ s.title || '新对话' }}</div>
|
||||
<div
|
||||
v-if="s.updatedAt || s.lastMessageAt"
|
||||
class="text-muted-foreground mt-0.5 truncate text-xs"
|
||||
>
|
||||
{{ s.updatedAt || s.lastMessageAt }}
|
||||
</div>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="border-t border-border p-3">
|
||||
<Button block @click="openSettings">
|
||||
@@ -394,7 +361,7 @@ void refreshSessions();
|
||||
</div>
|
||||
</aside>
|
||||
|
||||
<section class="flex min-h-0 min-w-0 flex-1 flex-col">
|
||||
<section class="agent-chat-main flex min-h-0 min-w-0 flex-1 flex-col">
|
||||
<header
|
||||
class="flex h-12 shrink-0 items-center border-b border-border px-4"
|
||||
>
|
||||
@@ -403,7 +370,7 @@ void refreshSessions();
|
||||
</span>
|
||||
</header>
|
||||
|
||||
<div class="relative min-h-0 flex-1">
|
||||
<div class="relative flex min-h-0 flex-1 flex-col">
|
||||
<div
|
||||
v-if="showMessageLoading"
|
||||
class="text-muted-foreground absolute inset-0 z-10 flex items-center justify-center bg-background/60 text-sm"
|
||||
@@ -415,27 +382,34 @@ void refreshSessions();
|
||||
class="absolute inset-0 flex items-center justify-center"
|
||||
description="开始提问吧(需先配置公司模型 API)"
|
||||
/>
|
||||
<BubbleList
|
||||
v-show="messages.length > 0"
|
||||
ref="bubbleListRef"
|
||||
class="h-full px-4 py-4"
|
||||
:roles="bubbleRoles"
|
||||
:items="bubbleItems"
|
||||
:auto-scroll="true"
|
||||
/>
|
||||
<div
|
||||
ref="messagePaneRef"
|
||||
class="agent-td-chat t-chat t-chat--normal min-h-0 flex-1 overflow-y-auto py-4"
|
||||
>
|
||||
<ChatItem
|
||||
v-for="item in bubbleViews"
|
||||
:key="item.id"
|
||||
class="agent-td-item"
|
||||
:role="item.role"
|
||||
:name="item.name"
|
||||
:content="item.content"
|
||||
:text-loading="item.textLoading"
|
||||
variant="base"
|
||||
animation="gradient"
|
||||
/>
|
||||
</div>
|
||||
<div class="shrink-0 border-t border-border p-3">
|
||||
<ChatSender
|
||||
v-model="draft"
|
||||
class="agent-td-sender"
|
||||
:loading="sending"
|
||||
placeholder="输入问题,Enter 发送"
|
||||
:textarea-props="{ autosize: { minRows: 2, maxRows: 6 } }"
|
||||
@send="onSenderSend"
|
||||
@stop="onSenderStop"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<footer class="shrink-0 border-t border-border p-3">
|
||||
<Sender
|
||||
v-model:value="draft"
|
||||
:loading="sending"
|
||||
placeholder="输入问题,Enter 发送(Shift+Enter 换行)"
|
||||
:auto-size="{ minRows: 2, maxRows: 6 }"
|
||||
submit-type="enter"
|
||||
@submit="onSenderSubmit"
|
||||
@cancel="onSenderCancel"
|
||||
/>
|
||||
</footer>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
@@ -499,57 +473,26 @@ void refreshSessions();
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.agent-conversations :deep(.ant-conversations-list) {
|
||||
padding-inline: 0;
|
||||
.agent-td-chat {
|
||||
--td-bg-color-container: transparent;
|
||||
padding-inline: 28px;
|
||||
}
|
||||
.agent-md :deep(p) {
|
||||
margin: 0.35em 0;
|
||||
.agent-td-item {
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
.agent-md :deep(p:first-child) {
|
||||
margin-top: 0;
|
||||
/* 气泡按内容收缩,左右再留一截,避免贴边铺满 */
|
||||
.agent-td-item :deep(.t-chat__inner) {
|
||||
box-sizing: border-box;
|
||||
padding-inline: 4px;
|
||||
}
|
||||
.agent-md :deep(p:last-child) {
|
||||
margin-bottom: 0;
|
||||
.agent-td-item :deep(.t-chat__content) {
|
||||
width: auto;
|
||||
max-width: min(720px, calc(100% - 32px));
|
||||
}
|
||||
.agent-md :deep(ul),
|
||||
.agent-md :deep(ol) {
|
||||
margin: 0.35em 0;
|
||||
padding-left: 1.25em;
|
||||
.agent-td-item :deep(.t-chat__inner.user) {
|
||||
justify-content: flex-end;
|
||||
}
|
||||
.agent-md :deep(pre) {
|
||||
margin: 0.5em 0;
|
||||
overflow-x: auto;
|
||||
border-radius: 0.5rem;
|
||||
background: rgba(0, 0, 0, 0.06);
|
||||
padding: 0.6em 0.75em;
|
||||
font-size: 0.85em;
|
||||
}
|
||||
.agent-md :deep(code) {
|
||||
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
|
||||
font-size: 0.9em;
|
||||
}
|
||||
.agent-md :deep(:not(pre) > code) {
|
||||
border-radius: 0.25rem;
|
||||
background: rgba(0, 0, 0, 0.06);
|
||||
padding: 0.1em 0.35em;
|
||||
}
|
||||
.agent-md :deep(a) {
|
||||
text-decoration: underline;
|
||||
}
|
||||
.agent-md :deep(blockquote) {
|
||||
margin: 0.4em 0;
|
||||
border-left: 3px solid rgba(0, 0, 0, 0.2);
|
||||
padding-left: 0.75em;
|
||||
opacity: 0.9;
|
||||
}
|
||||
.agent-md :deep(table) {
|
||||
margin: 0.5em 0;
|
||||
border-collapse: collapse;
|
||||
font-size: 0.9em;
|
||||
}
|
||||
.agent-md :deep(th),
|
||||
.agent-md :deep(td) {
|
||||
border: 1px solid rgba(0, 0, 0, 0.12);
|
||||
padding: 0.25em 0.5em;
|
||||
.agent-td-sender {
|
||||
width: 100%;
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
/** 轻量 Markdown → 安全 HTML(流式场景下未闭合围栏也能尽量渲染)。 */
|
||||
import DOMPurify from 'dompurify';
|
||||
import { marked } from 'marked';
|
||||
|
||||
marked.setOptions({
|
||||
gfm: true,
|
||||
breaks: true,
|
||||
});
|
||||
|
||||
export function renderAgentMarkdown(source: string): string {
|
||||
const raw = source || '';
|
||||
if (!raw.trim()) return '';
|
||||
try {
|
||||
const html = marked.parse(raw, { async: false }) as string;
|
||||
return DOMPurify.sanitize(html, {
|
||||
USE_PROFILES: { html: true },
|
||||
});
|
||||
} catch {
|
||||
return DOMPurify.sanitize(raw.replace(/</g, '<').replace(/>/g, '>'));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user