feat: 发布草稿助手 UI 对齐智能助手页交互

This commit is contained in:
2026-09-26 16:58:04 +08:00
parent 85bac4f710
commit 5567b6d281
3 changed files with 319 additions and 161 deletions
@@ -28,9 +28,16 @@ const CollapseIcon = createIconifyIcon('lucide:minimize-2');
const draft = defineModel<string>({ required: true });
const cites = defineModel<CatalogCite[]>('cites', { required: true });
const props = defineProps<{
loading?: boolean;
}>();
const props = withDefaults(
defineProps<{
loading?: boolean;
/** 输入框占位;默认面向通用助手 */
placeholder?: string;
}>(),
{
placeholder: '问点什么,或用 @ 引用店铺 / 商品 / SKU',
},
);
const emit = defineEmits<{
send: [value: string];
@@ -432,7 +439,7 @@ onBeforeUnmount(() => {
v-model="draft"
class="agent-td-sender"
:loading="loading"
placeholder="问点什么,或用 @ 引用店铺 / 商品 / SKU"
:placeholder="placeholder"
:textarea-props="{ autosize: textareaAutosize }"
@send="onChatSend"
@stop="emit('stop')"
@@ -1078,16 +1078,12 @@ html.dark .agent-session-item--active,
}
.agent-td-item {
margin-bottom: 16px;
width: 100%;
}
/* 短消息也固定左右:气泡 fit-content + 贴边,避免被 tdesign justify-center 居中 */
.agent-td-item :deep(.t-chat__inner) {
/* ChatItem 根节点即 .t-chat__inner,样式打在本类上 */
display: flex;
box-sizing: border-box;
width: 100%;
padding-inline: 4px;
margin-bottom: 16px;
padding-inline: 20px;
}
.agent-td-item :deep(.t-chat__content) {
@@ -1097,7 +1093,7 @@ html.dark .agent-session-item--active,
justify-content: flex-start !important;
}
.agent-td-item--assistant :deep(.t-chat__inner) {
.agent-td-item--assistant {
justify-content: flex-start;
}
@@ -1108,7 +1104,7 @@ html.dark .agent-session-item--active,
margin-left: 0;
}
.agent-td-item--user :deep(.t-chat__inner) {
.agent-td-item--user {
justify-content: flex-end;
}
@@ -1132,6 +1128,7 @@ html.dark .agent-session-item--active,
.agent-tool-status {
justify-content: flex-start;
padding-inline: 20px;
}
/* 浅色:给气泡补边框与底色,避免与主背景糊在一起 */
@@ -1143,8 +1140,9 @@ html.dark .agent-session-item--active,
padding-block: 10px;
}
.agent-td-item :deep(.t-chat__inner.user .t-chat__text--variant--base .t-chat__detail),
.agent-td-item :deep(.t-chat__inner.user .t-chat__detail) {
.agent-td-item--user :deep(.t-chat__text--variant--base .t-chat__detail),
.agent-td-item--user :deep(.t-chat__detail),
.agent-td-item.user :deep(.t-chat__detail) {
background: hsl(var(--primary) / 0.1);
border-color: hsl(var(--primary) / 0.22);
}
@@ -1157,10 +1155,12 @@ html.dark .agent-td-item :deep(.t-chat__detail),
background: hsl(var(--muted) / 0.4);
}
html.dark .agent-td-item :deep(.t-chat__inner.user .t-chat__text--variant--base .t-chat__detail),
html.dark .agent-td-item :deep(.t-chat__inner.user .t-chat__detail),
.dark .agent-td-item :deep(.t-chat__inner.user .t-chat__text--variant--base .t-chat__detail),
.dark .agent-td-item :deep(.t-chat__inner.user .t-chat__detail) {
html.dark .agent-td-item--user :deep(.t-chat__text--variant--base .t-chat__detail),
html.dark .agent-td-item--user :deep(.t-chat__detail),
html.dark .agent-td-item.user :deep(.t-chat__detail),
.dark .agent-td-item--user :deep(.t-chat__text--variant--base .t-chat__detail),
.dark .agent-td-item--user :deep(.t-chat__detail),
.dark .agent-td-item.user :deep(.t-chat__detail) {
background: hsl(var(--primary) / 0.22);
border-color: transparent;
}
@@ -1,16 +1,13 @@
<script lang="ts" setup>
/**
* 拼多多发布页右侧智能体栏:一草稿一会话,执行 client_tool 改表单。
* UI 对齐通用智能助手(气泡、加载、底栏输入与模型选择)。
*/
import { computed, nextTick, onBeforeUnmount, ref, watch } from 'vue';
import {
MenuFoldOutlined,
MenuUnfoldOutlined,
SendOutlined,
} from '@ant-design/icons-vue';
import { Button, Empty, Input, Select, Spin, Tooltip, message } from 'ant-design-vue';
import { ChatItem } from '@tdesign-vue-next/chat';
import { createIconifyIcon } from '@vben/icons';
import { Empty, Select, Spin, Tooltip, message } from 'ant-design-vue';
import { ChatItem, ChatLoading } from '@tdesign-vue-next/chat';
import 'tdesign-vue-next/es/style/index.css';
import '@tdesign-vue-next/chat/es/style/index.css';
@@ -22,11 +19,21 @@ import {
streamAgentChat,
type AgentVO,
} from '#/api/agent';
import CatalogCiteSender from '#/views/agent/chat/catalog-cite-sender.vue';
import {
composeMessageWithCites,
stripTrailingMention,
type CatalogCite,
} from '#/views/agent/chat/catalog-cite';
import type { PublishEditorAgentApi } from './commands';
defineOptions({ name: 'PddPublishAgentPanel' });
const AgentBrandIcon = createIconifyIcon('lucide:bot');
const PanelCloseIcon = createIconifyIcon('lucide:panel-left-close');
const PanelOpenIcon = createIconifyIcon('lucide:panel-left');
const props = defineProps<{
draftId: number;
collapsed?: boolean;
@@ -42,9 +49,18 @@ const emit = defineEmits<{
const SCENE = 'ecom_goods_publish' as const;
const starterPrompts = [
'根据已填的 SKU 商家编码,找相似拼多多商品并生成标题',
'参考渠道商品帮我整理规格轴',
'把当前标题改得更适合拼多多搜索',
{
label: '按编码生成标题',
prompt: '根据已填的 SKU 商家编码,找相似拼多多商品并生成标题',
},
{
label: '整理规格轴',
prompt: '参考渠道商品帮我整理规格轴',
},
{
label: '优化标题',
prompt: '把当前标题改得更适合拼多多搜索',
},
];
type BubbleView = {
@@ -64,6 +80,7 @@ const streamingText = ref('');
const toolStatusLabel = ref('');
const toolStatusActive = ref(false);
const draft = ref('');
const citations = ref<CatalogCite[]>([]);
const enabledModels = ref<AgentVO.LlmModelItem[]>([]);
const selectedModelKey = ref<string | undefined>(undefined);
const patchingModel = ref(false);
@@ -111,6 +128,12 @@ const showStarter = computed(
!loadingSession.value,
);
const showMessageLoading = computed(
() =>
(loadingSession.value || loadingMessages.value) &&
messages.value.length === 0,
);
const bubbleViews = computed<BubbleView[]>(() =>
messages.value.map((m, idx) => {
const isStreamingAssistant =
@@ -148,6 +171,11 @@ function syncSelectedFromSession() {
}
}
function clearToolStatus() {
toolStatusActive.value = false;
toolStatusLabel.value = '';
}
/** 滚到消息区底部;多拍几次等 ChatItem 异步撑高后再定住 */
function scrollChatToBottom() {
const run = () => {
@@ -232,6 +260,8 @@ async function bootstrap() {
loadingSession.value = true;
messages.value = [];
session.value = null;
draft.value = '';
citations.value = [];
try {
await loadEnabledModels();
const threadId = await ensureSession();
@@ -275,8 +305,7 @@ async function onModelChange(key: string) {
function appendStreamToken(t: string) {
if (toolStatusActive.value) {
toolStatusActive.value = false;
toolStatusLabel.value = '';
clearToolStatus();
}
streamingText.value += t;
const idx = messages.value.length - 1;
@@ -293,9 +322,7 @@ function onStreamTool(payload: { phase: string; label: string }) {
toolStatusLabel.value = payload.label;
void nextTick().then(scrollChatToBottom);
} else if (payload.phase === 'end') {
// 结束后清状态,避免卡在「读取发布表单…」直到下一段 token
toolStatusActive.value = false;
toolStatusLabel.value = '';
clearToolStatus();
}
}
@@ -312,8 +339,12 @@ async function onClientTool(call: AgentVO.ClientToolCall) {
}
}
async function send(textOverride?: string) {
const text = (textOverride ?? draft.value).trim();
async function send(rawOverride?: string) {
const raw = (rawOverride ?? draft.value).trim();
const text = composeMessageWithCites(
stripTrailingMention(raw),
citations.value,
);
if (!text || sending.value) return;
sending.value = true;
@@ -333,9 +364,9 @@ async function send(textOverride?: string) {
}
streamingText.value = '';
toolStatusActive.value = false;
toolStatusLabel.value = '';
clearToolStatus();
draft.value = '';
citations.value = [];
messages.value.push({
seq: (messages.value.at(-1)?.seq || 0) + 1,
role: 'user',
@@ -376,8 +407,7 @@ async function send(textOverride?: string) {
}
sending.value = false;
streamingText.value = '';
toolStatusActive.value = false;
toolStatusLabel.value = '';
clearToolStatus();
},
onError: (msg) => {
sending.value = false;
@@ -387,40 +417,48 @@ async function send(textOverride?: string) {
messages.value[idx] = { ...last, content: `错误:${msg}` };
}
streamingText.value = '';
toolStatusActive.value = false;
toolStatusLabel.value = '';
clearToolStatus();
message.error(msg);
},
});
}
function onSenderSend(value: string) {
void send(value);
}
function applyStarterPrompt(text: string) {
draft.value = text;
}
function onStop() {
function onSenderStop() {
abort?.abort();
sending.value = false;
toolStatusActive.value = false;
toolStatusLabel.value = '';
clearToolStatus();
}
onBeforeUnmount(() => abort?.abort());
</script>
<template>
<!-- 收起:窄条 -->
<!-- 收起:窄条(悬停 bot→展开图标,对齐智能助手) -->
<div
v-if="collapsed"
class="border-border bg-card flex h-full w-14 shrink-0 flex-col items-center border-l py-3"
class="publish-agent-rail border-border flex h-full w-14 shrink-0 flex-col items-center border-l px-1.5 py-2"
>
<Tooltip title="展开发布助手" placement="left">
<Button type="text" @click="emit('expand')">
<template #icon><MenuUnfoldOutlined /></template>
</Button>
<button
type="button"
class="hover:bg-muted group flex size-10 items-center justify-center rounded-lg transition-colors"
aria-label="展开发布助手"
@click="emit('expand')"
>
<AgentBrandIcon class="size-5 group-hover:hidden" />
<PanelOpenIcon class="hidden size-5 group-hover:block" />
</button>
</Tooltip>
<div
class="text-muted-foreground mt-4 write-vertical-right text-xs tracking-widest"
class="text-muted-foreground mt-3 text-xs tracking-widest"
style="writing-mode: vertical-rl"
>
发布助手
@@ -430,150 +468,263 @@ onBeforeUnmount(() => abort?.abort());
<!-- 展开 -->
<aside
v-else
class="border-border bg-card flex h-full min-h-0 w-full min-w-[280px] flex-col border-l"
class="publish-agent-panel border-border flex h-full min-h-0 w-full min-w-[280px] flex-col border-l"
>
<header
class="flex h-12 shrink-0 items-center gap-2 border-b border-border px-3"
>
<span class="min-w-0 flex-1 truncate text-sm font-medium">发布助手</span>
<Select
:value="selectedModelKey"
:options="modelOptions"
:loading="patchingModel"
:disabled="sending || enabledModels.length === 0"
placeholder="模型"
class="w-36 shrink-0"
size="small"
:allow-clear="false"
@change="(v) => onModelChange(String(v ?? ''))"
/>
<header class="flex h-12 shrink-0 items-center gap-1 px-3">
<div class="min-w-0 flex-1 truncate text-sm font-semibold">发布助手</div>
<Tooltip title="收起">
<Button type="text" size="small" @click="emit('collapse')">
<template #icon><MenuFoldOutlined /></template>
</Button>
<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="emit('collapse')"
>
<PanelCloseIcon class="size-5" />
</button>
</Tooltip>
</header>
<div class="relative flex min-h-0 flex-1 flex-col">
<section class="relative flex min-h-0 flex-1 flex-col">
<div
v-if="loadingSession || loadingMessages"
class="absolute inset-0 z-10 flex items-center justify-center bg-background/50"
v-if="showMessageLoading"
class="text-muted-foreground absolute inset-0 z-10 flex items-center justify-center bg-background/60 text-sm"
>
<Spin size="small" />
<Spin size="small" class="mr-2" />
加载中…
</div>
<!-- 空态:说明 + 居中输入 + chips -->
<div
ref="messagePaneRef"
class="publish-agent-chat t-chat t-chat--normal min-h-0 flex-1 overflow-y-auto py-3"
v-if="showStarter"
class="flex min-h-0 flex-1 flex-col justify-center px-3 pb-6 pt-4"
>
<div
v-if="showStarter"
class="flex h-full flex-col justify-center px-1 py-4"
>
<div class="text-sm font-medium">帮你完善本页草稿</div>
<p class="text-muted-foreground mt-2 text-xs leading-5">
可按已填 SKU 编码查内部/拼多多商品,生成标题并写回规格轴。改表单会直接落到左侧编辑区。
<div class="mb-5 px-1">
<div class="text-base font-semibold tracking-tight">
帮你完善本页草稿
</div>
<p class="text-muted-foreground mt-1.5 text-xs leading-5">
可按已填 SKU 编码查商品、生成标题并写回规格轴;改表单会落到左侧编辑区。
</p>
<div class="mt-3 flex flex-col gap-1.5">
<button
v-for="item in starterPrompts"
:key="item"
type="button"
class="hover:border-primary hover:bg-primary/5 rounded-md border border-border bg-background px-2.5 py-1.5 text-left text-xs transition-colors"
@click="applyStarterPrompt(item)"
>
{{ item }}
</button>
</div>
</div>
<Empty
v-else-if="!loadingSession && !loadingMessages && messages.length === 0"
class="mt-16"
description="开始提问吧"
/>
<ChatItem
v-for="item in bubbleViews"
:key="item.id"
class="publish-agent-item"
:role="item.role"
:name="item.name"
:content="item.content"
:text-loading="item.textLoading"
variant="base"
animation="gradient"
/>
<div
v-if="toolStatusActive && toolStatusLabel"
class="text-muted-foreground flex items-center gap-2 px-1 py-1 text-xs"
>
<span
class="border-primary/40 inline-block size-3 animate-spin rounded-full border-2 border-t-transparent"
/>
<span>{{ toolStatusLabel }}…</span>
<div class="publish-agent-composer w-full">
<CatalogCiteSender
v-model="draft"
v-model:cites="citations"
:loading="sending"
placeholder="描述想改的标题、规格,或用 @ 引用"
@send="onSenderSend"
@stop="onSenderStop"
>
<template #footer-extra>
<Select
:value="selectedModelKey"
:options="modelOptions"
:loading="patchingModel"
:disabled="sending || enabledModels.length === 0"
placeholder="模型"
size="small"
class="publish-agent-model-select min-w-0 max-w-[160px] flex-1"
:allow-clear="false"
@change="(v) => onModelChange(String(v ?? ''))"
/>
</template>
</CatalogCiteSender>
<div class="mt-4 flex flex-wrap gap-2">
<Tooltip
v-for="item in starterPrompts"
:key="item.label"
:title="item.prompt"
>
<button
type="button"
class="hover:bg-muted border-border text-foreground/80 hover:text-foreground inline-flex cursor-pointer items-center rounded-full border bg-card px-3 py-1.5 text-xs transition-colors"
@click="applyStarterPrompt(item.prompt)"
>
{{ item.label }}
</button>
</Tooltip>
</div>
</div>
</div>
<div class="shrink-0 border-t border-border p-2">
<div class="flex gap-1.5">
<Input.TextArea
v-model:value="draft"
:auto-size="{ minRows: 2, maxRows: 5 }"
:disabled="sending"
placeholder="描述你想改的标题、规格…"
class="flex-1"
@keydown.enter.exact.prevent="() => send()"
<!-- 对话态 -->
<template v-else>
<div
ref="messagePaneRef"
class="publish-agent-chat t-chat t-chat--normal min-h-0 flex-1 overflow-y-auto px-3 pb-3 pt-4"
>
<Empty
v-if="!showMessageLoading && messages.length === 0"
class="mt-16"
description="开始提问吧"
/>
<div class="flex flex-col gap-1">
<Button
v-if="sending"
type="default"
size="small"
@click="onStop"
>
停止
</Button>
<Button
v-else
type="primary"
size="small"
:disabled="!draft.trim()"
@click="() => send()"
>
<template #icon><SendOutlined /></template>
</Button>
<ChatItem
v-for="item in bubbleViews"
:key="item.id"
class="publish-agent-item"
:class="[
item.role === 'user'
? 'publish-agent-item--user'
: 'publish-agent-item--assistant',
{ 'publish-agent-item--loading': item.textLoading },
]"
:role="item.role"
:name="item.name"
:content="item.textLoading ? undefined : item.content"
:text-loading="false"
variant="base"
>
<template v-if="item.textLoading" #content>
<ChatLoading animation="dots" />
</template>
</ChatItem>
<div
v-if="toolStatusActive && toolStatusLabel"
class="text-muted-foreground flex items-center gap-2 px-1 py-1 text-xs"
>
<ChatLoading animation="dots" :text="`${toolStatusLabel}…`" />
</div>
</div>
</div>
</div>
<div class="publish-agent-composer shrink-0 px-3 pb-3 pt-1">
<CatalogCiteSender
v-model="draft"
v-model:cites="citations"
:loading="sending"
placeholder="描述想改的标题、规格,或用 @ 引用"
@send="onSenderSend"
@stop="onSenderStop"
>
<template #footer-extra>
<Select
:value="selectedModelKey"
:options="modelOptions"
:loading="patchingModel"
:disabled="sending || enabledModels.length === 0"
placeholder="模型"
size="small"
class="publish-agent-model-select min-w-0 max-w-[160px] flex-1"
:allow-clear="false"
@change="(v) => onModelChange(String(v ?? ''))"
/>
</template>
</CatalogCiteSender>
</div>
</template>
</section>
</aside>
</template>
<style scoped>
.publish-agent-rail,
.publish-agent-panel {
background: hsl(var(--muted) / 0.45);
}
html.dark .publish-agent-rail,
html.dark .publish-agent-panel,
.dark .publish-agent-rail,
.dark .publish-agent-panel {
background: hsl(var(--background-deep, var(--background)));
}
.publish-agent-composer :deep(.t-chat-sender) {
border-radius: 20px;
box-shadow: 0 0 0 1px hsl(var(--border) / 0.8);
}
.publish-agent-composer :deep(.t-chat-sender__textarea__wrapper) {
border-radius: 20px;
padding: 0;
margin-bottom: 0 !important;
}
.publish-agent-model-select :deep(.ant-select-selector) {
border-radius: 999px !important;
}
/*
* TDesign `.t-chat { padding: 0 }` 会盖掉 Tailwind px-*,容器左右留白写这里。
* 不要动 `.t-chat__detail` 的 padding,那是气泡内文字边距。
* ChatItem 根节点就是 .t-chat__inner,padding / justify 必须打在本类上。
*/
.publish-agent-chat.t-chat {
--td-bg-color-container: transparent;
padding: 12px 16px !important;
box-sizing: border-box;
}
.publish-agent-item {
margin-bottom: 10px;
}
.publish-agent-item :deep(.t-chat__inner) {
display: flex;
box-sizing: border-box;
padding-inline: 4px;
width: 100%;
margin-bottom: 12px;
padding-inline: 12px;
}
.publish-agent-item :deep(.t-chat__content) {
width: auto;
max-width: calc(100% - 8px);
flex: 0 1 auto !important;
width: fit-content !important;
max-width: calc(100% - 4px);
justify-content: flex-start !important;
}
.publish-agent-item :deep(.t-chat__inner.user) {
.publish-agent-item--assistant {
justify-content: flex-start;
}
.publish-agent-item--assistant :deep(.t-chat__content) {
align-self: flex-start;
align-items: flex-start !important;
margin-right: auto;
margin-left: 0;
}
.publish-agent-item--user {
justify-content: flex-end;
}
.publish-agent-item--user :deep(.t-chat__content) {
align-self: flex-end;
align-items: flex-end !important;
margin-left: auto;
margin-right: 0;
}
.publish-agent-item :deep(.t-chat__detail) {
width: fit-content;
max-width: 100%;
margin-inline: 0;
}
.publish-agent-item :deep(.t-chat__avatar),
.publish-agent-item :deep(.t-chat__name) {
display: none;
}
/* 浅色:气泡边框与底色,避免与侧栏糊在一起 */
.publish-agent-item :deep(.t-chat__text--variant--base .t-chat__detail),
.publish-agent-item :deep(.t-chat__detail) {
border: 1px solid hsl(var(--border));
border-radius: 12px;
background: hsl(var(--background));
padding-block: 8px;
}
.publish-agent-item--user :deep(.t-chat__text--variant--base .t-chat__detail),
.publish-agent-item--user :deep(.t-chat__detail),
.publish-agent-item.user :deep(.t-chat__detail) {
background: hsl(var(--primary) / 0.1);
border-color: hsl(var(--primary) / 0.22);
}
html.dark .publish-agent-item :deep(.t-chat__text--variant--base .t-chat__detail),
html.dark .publish-agent-item :deep(.t-chat__detail),
.dark .publish-agent-item :deep(.t-chat__text--variant--base .t-chat__detail),
.dark .publish-agent-item :deep(.t-chat__detail) {
border-color: transparent;
background: hsl(var(--muted) / 0.4);
}
html.dark .publish-agent-item--user :deep(.t-chat__text--variant--base .t-chat__detail),
html.dark .publish-agent-item--user :deep(.t-chat__detail),
html.dark .publish-agent-item.user :deep(.t-chat__detail),
.dark .publish-agent-item--user :deep(.t-chat__text--variant--base .t-chat__detail),
.dark .publish-agent-item--user :deep(.t-chat__detail),
.dark .publish-agent-item.user :deep(.t-chat__detail) {
background: hsl(var(--primary) / 0.22);
border-color: transparent;
}
</style>