mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 17:09:20 +08:00
feat: 新对话空态展示能力引导,快捷提示只填入输入框
This commit is contained in:
@@ -33,6 +33,14 @@ import {
|
|||||||
|
|
||||||
defineOptions({ name: 'AgentChat' });
|
defineOptions({ name: 'AgentChat' });
|
||||||
|
|
||||||
|
/** 新对话空态快捷提示:点击只填入输入框,不直接发送 */
|
||||||
|
const starterPrompts = [
|
||||||
|
'近7天的销售额、单量、净利和费比分别是多少',
|
||||||
|
'昨天的经营情况怎么样',
|
||||||
|
'本周销售额和净利帮我汇总一下',
|
||||||
|
'近30天单量和费比有什么变化',
|
||||||
|
];
|
||||||
|
|
||||||
/** 消息气泡视图模型(走 Vue ChatItem,避免 ChatList→WebComponent 丢正文) */
|
/** 消息气泡视图模型(走 Vue ChatItem,避免 ChatList→WebComponent 丢正文) */
|
||||||
type BubbleView = {
|
type BubbleView = {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -42,11 +50,13 @@ type BubbleView = {
|
|||||||
textLoading: boolean;
|
textLoading: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
const loadingSessions = ref(false);
|
const loadingSessions = ref(true);
|
||||||
const loadingMessages = ref(false);
|
const loadingMessages = ref(false);
|
||||||
const sending = ref(false);
|
const sending = ref(false);
|
||||||
const sessions = ref<AgentVO.Session[]>([]);
|
const sessions = ref<AgentVO.Session[]>([]);
|
||||||
const activeThreadId = ref<string | null>(null);
|
const activeThreadId = ref<string | null>(null);
|
||||||
|
/** 点了「新对话」但还没发出第一条消息,此时不请求后端 */
|
||||||
|
const composingNew = ref(false);
|
||||||
const messages = ref<AgentVO.Message[]>([]);
|
const messages = ref<AgentVO.Message[]>([]);
|
||||||
/** 当前流式助手正文(与 messages 末条同步) */
|
/** 当前流式助手正文(与 messages 末条同步) */
|
||||||
const streamingText = ref('');
|
const streamingText = ref('');
|
||||||
@@ -73,6 +83,15 @@ const showMessageLoading = computed(
|
|||||||
() => loadingMessages.value && !sending.value && messages.value.length === 0,
|
() => loadingMessages.value && !sending.value && messages.value.length === 0,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/** 新对话草稿,或还没有任何会话时,展示能力引导 */
|
||||||
|
const showStarter = computed(
|
||||||
|
() =>
|
||||||
|
messages.value.length === 0 &&
|
||||||
|
!showMessageLoading.value &&
|
||||||
|
(composingNew.value ||
|
||||||
|
(!loadingSessions.value && !activeThreadId.value)),
|
||||||
|
);
|
||||||
|
|
||||||
const bubbleViews = computed<BubbleView[]>(() =>
|
const bubbleViews = computed<BubbleView[]>(() =>
|
||||||
messages.value.map((m, idx) => {
|
messages.value.map((m, idx) => {
|
||||||
const isStreamingAssistant =
|
const isStreamingAssistant =
|
||||||
@@ -104,6 +123,7 @@ function onSessionClick(threadId: string) {
|
|||||||
message.warning('回复生成中,请稍候再切换会话');
|
message.warning('回复生成中,请稍候再切换会话');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
composingNew.value = false;
|
||||||
activeThreadId.value = threadId;
|
activeThreadId.value = threadId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -112,7 +132,7 @@ async function refreshSessions() {
|
|||||||
try {
|
try {
|
||||||
const res = await listAgentSessionsApi();
|
const res = await listAgentSessionsApi();
|
||||||
sessions.value = res.items || [];
|
sessions.value = res.items || [];
|
||||||
if (!activeThreadId.value && sessions.value[0]) {
|
if (!composingNew.value && !activeThreadId.value && sessions.value[0]) {
|
||||||
activeThreadId.value = sessions.value[0].threadId;
|
activeThreadId.value = sessions.value[0].threadId;
|
||||||
}
|
}
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
@@ -158,18 +178,30 @@ watch(activeThreadId, (id) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
async function createSession() {
|
/** 仅切换到空白草稿,等第一条消息再 createSession */
|
||||||
try {
|
function beginNewChat() {
|
||||||
const s = await createAgentSessionApi();
|
if (sending.value) {
|
||||||
sessions.value = [s, ...sessions.value];
|
message.warning('回复生成中,请稍候再开新对话');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (composingNew.value && !activeThreadId.value) return;
|
||||||
messagesLoadSeq += 1;
|
messagesLoadSeq += 1;
|
||||||
loadingMessages.value = false;
|
loadingMessages.value = false;
|
||||||
|
composingNew.value = true;
|
||||||
|
activeThreadId.value = null;
|
||||||
messages.value = [];
|
messages.value = [];
|
||||||
streamingText.value = '';
|
streamingText.value = '';
|
||||||
|
draft.value = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
/** 第一条消息发出时才落库 */
|
||||||
|
async function ensureSession(): Promise<string | null> {
|
||||||
|
if (activeThreadId.value) return activeThreadId.value;
|
||||||
|
const s = await createAgentSessionApi();
|
||||||
|
composingNew.value = false;
|
||||||
|
sessions.value = [s, ...sessions.value];
|
||||||
activeThreadId.value = s.threadId;
|
activeThreadId.value = s.threadId;
|
||||||
} catch (e: any) {
|
return s.threadId;
|
||||||
message.error(e?.message || '新建会话失败');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function openSettings() {
|
async function openSettings() {
|
||||||
@@ -239,15 +271,25 @@ function appendStreamToken(t: string) {
|
|||||||
async function send(textOverride?: string) {
|
async function send(textOverride?: string) {
|
||||||
const text = (textOverride ?? draft.value).trim();
|
const text = (textOverride ?? draft.value).trim();
|
||||||
if (!text || sending.value) return;
|
if (!text || sending.value) return;
|
||||||
if (!activeThreadId.value) {
|
|
||||||
await createSession();
|
|
||||||
}
|
|
||||||
const threadId = activeThreadId.value;
|
|
||||||
if (!threadId) return;
|
|
||||||
|
|
||||||
|
sending.value = true;
|
||||||
messagesLoadSeq += 1;
|
messagesLoadSeq += 1;
|
||||||
loadingMessages.value = false;
|
loadingMessages.value = false;
|
||||||
sending.value = true;
|
let threadId = activeThreadId.value;
|
||||||
|
if (!threadId) {
|
||||||
|
try {
|
||||||
|
threadId = await ensureSession();
|
||||||
|
} catch (e: any) {
|
||||||
|
sending.value = false;
|
||||||
|
message.error(e?.message || '新建会话失败');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!threadId) {
|
||||||
|
sending.value = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
streamingText.value = '';
|
streamingText.value = '';
|
||||||
draft.value = '';
|
draft.value = '';
|
||||||
messages.value.push({
|
messages.value.push({
|
||||||
@@ -296,6 +338,11 @@ function onSenderSend(value: string) {
|
|||||||
void send(value);
|
void send(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 把快捷提示写入输入框,留给用户改完再发 */
|
||||||
|
function applyStarterPrompt(text: string) {
|
||||||
|
draft.value = text;
|
||||||
|
}
|
||||||
|
|
||||||
function onSenderStop() {
|
function onSenderStop() {
|
||||||
abort?.abort();
|
abort?.abort();
|
||||||
sending.value = false;
|
sending.value = false;
|
||||||
@@ -313,7 +360,7 @@ void refreshSessions();
|
|||||||
class="flex w-64 shrink-0 flex-col border-r border-border bg-card"
|
class="flex w-64 shrink-0 flex-col border-r border-border bg-card"
|
||||||
>
|
>
|
||||||
<div class="border-b border-border p-3">
|
<div class="border-b border-border p-3">
|
||||||
<Button type="primary" block @click="createSession">
|
<Button type="primary" block @click="beginNewChat">
|
||||||
<template #icon><PlusOutlined /></template>
|
<template #icon><PlusOutlined /></template>
|
||||||
新对话
|
新对话
|
||||||
</Button>
|
</Button>
|
||||||
@@ -326,11 +373,21 @@ void refreshSessions();
|
|||||||
<Spin size="small" />
|
<Spin size="small" />
|
||||||
</div>
|
</div>
|
||||||
<Empty
|
<Empty
|
||||||
v-if="!loadingSessions && sessions.length === 0"
|
v-if="!loadingSessions && sessions.length === 0 && !composingNew"
|
||||||
class="mt-10 px-2"
|
class="mt-10 px-2"
|
||||||
description="暂无会话"
|
description="暂无会话"
|
||||||
/>
|
/>
|
||||||
<div v-else class="h-full space-y-1 overflow-y-auto px-3 py-2">
|
<div v-else class="h-full space-y-1 overflow-y-auto px-3 py-2">
|
||||||
|
<button
|
||||||
|
v-if="composingNew"
|
||||||
|
type="button"
|
||||||
|
class="bg-primary/10 text-primary mb-1 w-full rounded-lg px-3 py-2 text-left text-sm"
|
||||||
|
>
|
||||||
|
<div class="truncate font-medium">新对话</div>
|
||||||
|
<div class="text-muted-foreground mt-0.5 truncate text-xs">
|
||||||
|
尚未发送
|
||||||
|
</div>
|
||||||
|
</button>
|
||||||
<button
|
<button
|
||||||
v-for="s in sessions"
|
v-for="s in sessions"
|
||||||
:key="s.threadId"
|
:key="s.threadId"
|
||||||
@@ -366,7 +423,11 @@ void refreshSessions();
|
|||||||
class="flex h-12 shrink-0 items-center border-b border-border px-4"
|
class="flex h-12 shrink-0 items-center border-b border-border px-4"
|
||||||
>
|
>
|
||||||
<span class="truncate font-medium">
|
<span class="truncate font-medium">
|
||||||
{{ activeSession?.title || '智能助手' }}
|
{{
|
||||||
|
composingNew
|
||||||
|
? '新对话'
|
||||||
|
: activeSession?.title || '智能助手'
|
||||||
|
}}
|
||||||
</span>
|
</span>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
@@ -378,7 +439,7 @@ void refreshSessions();
|
|||||||
加载消息…
|
加载消息…
|
||||||
</div>
|
</div>
|
||||||
<Empty
|
<Empty
|
||||||
v-if="!showMessageLoading && messages.length === 0"
|
v-if="!showStarter && !showMessageLoading && messages.length === 0"
|
||||||
class="absolute inset-0 flex items-center justify-center"
|
class="absolute inset-0 flex items-center justify-center"
|
||||||
description="开始提问吧(需先配置公司模型 API)"
|
description="开始提问吧(需先配置公司模型 API)"
|
||||||
/>
|
/>
|
||||||
@@ -386,6 +447,32 @@ void refreshSessions();
|
|||||||
ref="messagePaneRef"
|
ref="messagePaneRef"
|
||||||
class="agent-td-chat t-chat t-chat--normal min-h-0 flex-1 overflow-y-auto py-4"
|
class="agent-td-chat t-chat t-chat--normal min-h-0 flex-1 overflow-y-auto py-4"
|
||||||
>
|
>
|
||||||
|
<div
|
||||||
|
v-if="showStarter"
|
||||||
|
class="mx-auto flex h-full max-w-xl flex-col justify-center px-2 py-6"
|
||||||
|
>
|
||||||
|
<div class="text-base font-medium">智能助手可以帮你看经营数据</div>
|
||||||
|
<p class="text-muted-foreground mt-2 text-sm leading-6">
|
||||||
|
直接问销售额、单量、净利、费比。说「近7天」「昨天」「本周」「本月」即可,不必自己换算日期。查某家店或某个商品时,带上店铺名或商品名。
|
||||||
|
</p>
|
||||||
|
<ul
|
||||||
|
class="text-muted-foreground mt-3 list-disc space-y-1 pl-5 text-sm leading-6"
|
||||||
|
>
|
||||||
|
<li>工作台概览:销售额、单量、净利、费比</li>
|
||||||
|
<li>店铺 / 商品:销量、ROI、目标达成</li>
|
||||||
|
</ul>
|
||||||
|
<div class="mt-4 flex flex-col gap-2">
|
||||||
|
<button
|
||||||
|
v-for="item in starterPrompts"
|
||||||
|
:key="item"
|
||||||
|
type="button"
|
||||||
|
class="hover:border-primary hover:bg-primary/5 rounded-lg border border-border bg-card px-3 py-2 text-left text-sm transition-colors"
|
||||||
|
@click="applyStarterPrompt(item)"
|
||||||
|
>
|
||||||
|
{{ item }}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
<ChatItem
|
<ChatItem
|
||||||
v-for="item in bubbleViews"
|
v-for="item in bubbleViews"
|
||||||
:key="item.id"
|
:key="item.id"
|
||||||
|
|||||||
Reference in New Issue
Block a user