refactor(frontend): 重构前端第二版
This commit is contained in:
1 parent
2631f78b00
commit
4b562cfa82
205 files changed
+15399
-12015
No files matched your search
@@ -0,0 +1,398 @@
|
||||
/**
|
||||
* features/chat/components/messages/ChatMessageBubble.vue
|
||||
* 单条聊天消息:用户气泡、助手 Markdown、请假/出货子组件、来源引用与操作栏。
|
||||
*/
|
||||
<script setup lang="ts">
|
||||
import { computed } from 'vue';
|
||||
import { Check, Copy, Edit2, LayoutGrid, MessageSquareText, RotateCw } from 'lucide-vue-next';
|
||||
import LeaveMessageBlock, { type LeaveMessageHandlers } from './LeaveMessageBlock.vue';
|
||||
import ShippingInventoryMappingMessage, { type ShippingMappingHandlers } from './ShippingInventoryMappingMessage.vue';
|
||||
import type { ChatMessage, SourceFileLink } from '../../types/chat.types';
|
||||
import type { ShippingInventoryMappingFormPayload } from '../../types/shipping.types';
|
||||
|
||||
export interface ChatMessageBubbleHandlers {
|
||||
renderMarkdown: (text: string) => string;
|
||||
onMarkdownClick: (event: MouseEvent) => void;
|
||||
getShippingInventoryMappingPayload: (msg: ChatMessage) => ShippingInventoryMappingFormPayload | null;
|
||||
canOpenQueryPreview: (msg: ChatMessage) => boolean;
|
||||
getQueryPreviewSummary: (msg: ChatMessage) => string;
|
||||
openQueryPreviewDialog: (msg: ChatMessage) => void;
|
||||
shippingActionButtonClass: (variant?: string) => string;
|
||||
onShippingActionClick: (msg: ChatMessage, action: NonNullable<ChatMessage['shipping_actions']>[number]) => void;
|
||||
getDisplayRetrieverResources: (msg: ChatMessage) => Array<Record<string, unknown>>;
|
||||
getSourceLinks: (resources: Array<Record<string, unknown>>) => SourceFileLink[];
|
||||
shouldExpandSourceDetails: (msg: ChatMessage) => boolean;
|
||||
getSourceTitle: (doc: Record<string, unknown>) => string;
|
||||
getSourceMeta: (doc: Record<string, unknown>) => string;
|
||||
getSourceBrief: (doc: Record<string, unknown>) => string;
|
||||
openPreview: (file: SourceFileLink) => void;
|
||||
}
|
||||
|
||||
const {
|
||||
message,
|
||||
index,
|
||||
messageCount,
|
||||
isThinking,
|
||||
showChatRating,
|
||||
copiedIndex,
|
||||
isShippingNoticeAgent,
|
||||
handlers,
|
||||
leaveHandlers,
|
||||
shippingHandlers,
|
||||
} = defineProps<{
|
||||
message: ChatMessage;
|
||||
index: number;
|
||||
messageCount: number;
|
||||
isThinking: boolean;
|
||||
showChatRating: boolean;
|
||||
copiedIndex: number | null;
|
||||
isShippingNoticeAgent: boolean;
|
||||
handlers: ChatMessageBubbleHandlers;
|
||||
leaveHandlers: LeaveMessageHandlers;
|
||||
shippingHandlers: ShippingMappingHandlers;
|
||||
}>();
|
||||
|
||||
const emit = defineEmits<{
|
||||
copy: [];
|
||||
edit: [content: string];
|
||||
resend: [content: string];
|
||||
regenerate: [];
|
||||
'open-feedback': [];
|
||||
}>();
|
||||
|
||||
const isLeaveMessage = computed(() =>
|
||||
message.type === 'leave-form'
|
||||
|| message.type === 'leave-confirmation'
|
||||
|| message.type === 'leave-result',
|
||||
);
|
||||
|
||||
const retrieverResources = computed(() => handlers.getDisplayRetrieverResources(message));
|
||||
const sourceLinks = computed(() => handlers.getSourceLinks(retrieverResources.value));
|
||||
|
||||
const showSourceSection = computed(() => {
|
||||
if (message.role !== 'assistant' || isLeaveMessage.value) return false;
|
||||
return retrieverResources.value.length > 0
|
||||
|| (message.attribution_parts?.length ?? 0) > 0
|
||||
|| sourceLinks.value.length > 0;
|
||||
});
|
||||
|
||||
const showActions = computed(() => message.type !== 'leave-form');
|
||||
const isLastMessage = computed(() => index === messageCount - 1);
|
||||
const isCopied = computed(() => copiedIndex === index);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div
|
||||
:data-msg-index="index"
|
||||
class="group mx-auto mb-6 flex w-full max-w-4xl animate-slide-up"
|
||||
:class="message.role === 'user' ? 'justify-end' : 'justify-start'"
|
||||
>
|
||||
<div
|
||||
class="flex flex-col"
|
||||
:class="message.role === 'user' ? 'max-w-[88%] md:max-w-[74%] items-end' : 'w-full'"
|
||||
>
|
||||
<div
|
||||
class="text-[15px] leading-relaxed"
|
||||
:class="message.role === 'user'
|
||||
? 'rounded-2xl rounded-tr-sm bg-brand-600 px-5 py-2.5 text-white shadow-sm shadow-brand-500/15'
|
||||
: 'chat-msg-assistant'"
|
||||
>
|
||||
<div v-if="message.role === 'user'" class="whitespace-pre-wrap">{{ message.content }}</div>
|
||||
<div v-else>
|
||||
<LeaveMessageBlock
|
||||
v-if="isLeaveMessage"
|
||||
:message="message"
|
||||
:index="index"
|
||||
:handlers="leaveHandlers"
|
||||
/>
|
||||
<ShippingInventoryMappingMessage
|
||||
v-else-if="message.type === 'shipping-inventory-mapping' && handlers.getShippingInventoryMappingPayload(message)"
|
||||
:message="message"
|
||||
:handlers="shippingHandlers"
|
||||
/>
|
||||
<div
|
||||
v-else
|
||||
class="chat-markdown prose prose-sm max-w-none leading-7 text-primary prose-a:text-blue-600 prose-p:my-1 prose-li:my-0.5 prose-ol:my-2 prose-ul:my-2 prose-headings:my-2 prose-hr:my-3 prose-code:rounded prose-code:bg-slate-100 prose-code:px-1 prose-code:text-blue-700 prose-pre:my-3 prose-pre:border prose-pre:border-slate-700 prose-pre:bg-slate-900 prose-pre:text-slate-100 prose-th:border prose-th:border-slate-200 prose-th:bg-slate-50 prose-th:px-3 prose-th:py-2 prose-th:text-left prose-td:border prose-td:border-slate-200 prose-td:px-3 prose-td:py-2"
|
||||
@click="handlers.onMarkdownClick"
|
||||
v-html="handlers.renderMarkdown(message.content)"
|
||||
/>
|
||||
|
||||
<div
|
||||
v-if="message.role === 'assistant' && handlers.canOpenQueryPreview(message)"
|
||||
class="mt-4"
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
class="inline-flex items-center gap-2 rounded-xl border border-brand-200 bg-brand-50 px-3 py-2 text-xs font-medium text-brand-700 transition hover:bg-brand-100"
|
||||
@click="handlers.openQueryPreviewDialog(message)"
|
||||
>
|
||||
<LayoutGrid class="h-3.5 w-3.5" />
|
||||
{{ handlers.getQueryPreviewSummary(message) }}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div
|
||||
v-if="isShippingNoticeAgent && message.shipping_actions?.length"
|
||||
class="mt-4 space-y-2 border-t border-default pt-4"
|
||||
>
|
||||
<div class="text-xs font-medium text-muted">{{ $t('chat.shippingNoticeActionsHint') }}</div>
|
||||
<div class="flex flex-wrap gap-2">
|
||||
<button
|
||||
v-for="(action, actionIndex) in message.shipping_actions"
|
||||
:key="`${message.local_id || index}-shipping-action-${action.id}-${actionIndex}`"
|
||||
type="button"
|
||||
:class="handlers.shippingActionButtonClass(action.variant)"
|
||||
:disabled="isThinking || message.shipping_actions_used"
|
||||
@click="handlers.onShippingActionClick(message, action)"
|
||||
>
|
||||
{{ action.label }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div v-if="showSourceSection" class="mt-2 space-y-2">
|
||||
<div v-if="sourceLinks.length > 0" class="flex flex-wrap gap-2">
|
||||
<button
|
||||
v-for="(file, linkIndex) in sourceLinks"
|
||||
:key="`src-link-top-${linkIndex}-${file.url}`"
|
||||
type="button"
|
||||
class="break-all rounded-lg border border-brand-200 bg-brand-50 px-2.5 py-1.5 text-xs text-brand-700 transition-colors hover:bg-brand-100"
|
||||
@click="handlers.openPreview(file)"
|
||||
>
|
||||
{{ file.name }}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<details
|
||||
v-if="retrieverResources.length > 0"
|
||||
class="rounded-xl border border-default bg-surface-soft/90 px-4 py-3"
|
||||
:open="handlers.shouldExpandSourceDetails(message)"
|
||||
>
|
||||
<summary class="cursor-pointer text-xs font-semibold uppercase tracking-wide text-secondary">
|
||||
{{ $t('chat.sourceDocs') }} ({{ retrieverResources.length }})
|
||||
</summary>
|
||||
<div class="mt-3 space-y-2">
|
||||
<div
|
||||
v-for="(doc, docIndex) in retrieverResources"
|
||||
:key="`${doc.document_name || 'doc'}-${doc.segment_id || docIndex}`"
|
||||
class="rounded-lg border border-default bg-surface p-3"
|
||||
>
|
||||
<div class="break-all text-sm font-semibold text-primary">{{ handlers.getSourceTitle(doc) }}</div>
|
||||
<div v-if="handlers.getSourceMeta(doc)" class="mt-1 text-xs text-muted">{{ handlers.getSourceMeta(doc) }}</div>
|
||||
<div v-if="handlers.getSourceBrief(doc)" class="mt-2 whitespace-pre-wrap break-all text-xs text-secondary">
|
||||
{{ handlers.getSourceBrief(doc) }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</details>
|
||||
|
||||
<details
|
||||
v-if="message.attribution_parts && message.attribution_parts.length > 0"
|
||||
class="rounded-xl border border-default bg-surface-soft/90 px-4 py-3"
|
||||
open
|
||||
>
|
||||
<summary class="cursor-pointer text-xs font-semibold uppercase tracking-wide text-secondary">
|
||||
{{ $t('chat.attributionParts') }}
|
||||
</summary>
|
||||
<ul class="mt-3 space-y-2">
|
||||
<li
|
||||
v-for="(part, partIndex) in message.attribution_parts"
|
||||
:key="`attr-${partIndex}`"
|
||||
class="rounded-lg border border-default bg-surface p-3 text-xs text-secondary"
|
||||
>
|
||||
{{ part }}
|
||||
</li>
|
||||
</ul>
|
||||
</details>
|
||||
</div>
|
||||
|
||||
<div
|
||||
v-if="showActions"
|
||||
class="mt-1 flex items-center gap-1 opacity-0 transition-opacity group-hover:opacity-100"
|
||||
:class="message.role === 'user' ? 'justify-end text-muted' : 'justify-start text-muted'"
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
class="rounded-md p-1.5 text-xs transition-colors hover-surface-soft"
|
||||
:title="$t('chat.actions.copy')"
|
||||
@click.stop="emit('copy')"
|
||||
>
|
||||
<Check v-if="isCopied" class="h-3.5 w-3.5 text-emerald-500" />
|
||||
<Copy v-else class="h-3.5 w-3.5" />
|
||||
</button>
|
||||
|
||||
<template v-if="message.role === 'user'">
|
||||
<button
|
||||
type="button"
|
||||
class="rounded-md p-1.5 text-xs transition-colors hover-surface-soft"
|
||||
:title="$t('chat.actions.edit')"
|
||||
@click.stop="emit('edit', message.content)"
|
||||
>
|
||||
<Edit2 class="h-3.5 w-3.5" />
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="rounded-md p-1.5 text-xs transition-colors hover-surface-soft"
|
||||
:title="$t('chat.actions.resend')"
|
||||
@click.stop="emit('resend', message.content)"
|
||||
>
|
||||
<RotateCw class="h-3.5 w-3.5" />
|
||||
</button>
|
||||
</template>
|
||||
|
||||
<template v-else>
|
||||
<button
|
||||
v-if="isLastMessage && !isThinking"
|
||||
type="button"
|
||||
class="rounded-md p-1.5 text-xs transition-colors hover-surface-soft"
|
||||
:title="$t('chat.actions.regenerate')"
|
||||
@click="emit('regenerate')"
|
||||
>
|
||||
<RotateCw class="h-3.5 w-3.5" />
|
||||
</button>
|
||||
<button
|
||||
v-if="showChatRating && !isThinking && !message.rated"
|
||||
type="button"
|
||||
class="rounded-md p-1.5 text-xs transition-colors hover-surface-soft"
|
||||
:title="$t('chat.rate')"
|
||||
@click="emit('open-feedback')"
|
||||
>
|
||||
<MessageSquareText class="h-3.5 w-3.5" />
|
||||
</button>
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
:deep(.chat-markdown) {
|
||||
max-width: 100%;
|
||||
}
|
||||
|
||||
:deep(.chat-markdown .chat-table-scroll) {
|
||||
max-width: 100%;
|
||||
overflow-x: auto;
|
||||
overflow-y: hidden;
|
||||
margin: 0.75rem 0;
|
||||
-webkit-overflow-scrolling: touch;
|
||||
}
|
||||
|
||||
:deep(.chat-markdown .chat-table-inner) {
|
||||
width: max-content;
|
||||
min-width: 100%;
|
||||
}
|
||||
|
||||
:deep(.chat-markdown table) {
|
||||
width: max-content;
|
||||
min-width: 100%;
|
||||
border-collapse: collapse;
|
||||
margin: 0;
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
|
||||
:deep(.chat-markdown th),
|
||||
:deep(.chat-markdown td) {
|
||||
border: 1px solid rgb(226 232 240);
|
||||
padding: 0.5rem 0.75rem;
|
||||
text-align: left;
|
||||
vertical-align: top;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
:deep(.chat-markdown th) {
|
||||
background: rgb(248 250 252);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
:deep(.dark .chat-markdown th),
|
||||
:deep(.dark .chat-markdown td) {
|
||||
border-color: rgb(51 65 85);
|
||||
}
|
||||
|
||||
:deep(.dark .chat-markdown th) {
|
||||
background: rgb(30 41 59);
|
||||
}
|
||||
|
||||
:deep(.chat-markdown details) {
|
||||
margin-top: 1rem;
|
||||
border: 1px solid rgb(226 232 240);
|
||||
border-radius: 0.75rem;
|
||||
background: rgb(248 250 252 / 0.9);
|
||||
padding: 0.75rem 1rem;
|
||||
}
|
||||
|
||||
:deep(.dark .chat-markdown details) {
|
||||
border-color: rgb(51 65 85);
|
||||
background: rgb(30 41 59 / 0.65);
|
||||
}
|
||||
|
||||
:deep(.chat-markdown details summary) {
|
||||
cursor: pointer;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 600;
|
||||
letter-spacing: 0.08em;
|
||||
color: rgb(71 85 105);
|
||||
list-style-position: inside;
|
||||
}
|
||||
|
||||
:deep(.dark .chat-markdown details summary) {
|
||||
color: rgb(203 213 225);
|
||||
}
|
||||
|
||||
:deep(.chat-markdown details[open] summary) {
|
||||
margin-bottom: 0.75rem;
|
||||
}
|
||||
|
||||
:deep(.chat-markdown details pre) {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
:deep(.chat-markdown .leave-action-trigger) {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
margin: 0 0.2rem;
|
||||
padding: 0.1rem 0.6rem;
|
||||
border-radius: 9999px;
|
||||
border: 1px solid rgb(59 130 246 / 0.28);
|
||||
background: rgb(239 246 255);
|
||||
color: rgb(29 78 216);
|
||||
font-weight: 600;
|
||||
text-decoration: none;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s ease;
|
||||
}
|
||||
|
||||
:deep(.chat-markdown .leave-action-trigger:hover) {
|
||||
background: rgb(219 234 254);
|
||||
border-color: rgb(59 130 246 / 0.4);
|
||||
}
|
||||
|
||||
:deep(.chat-markdown .leave-intent-highlight) {
|
||||
display: inline-block;
|
||||
margin: 0 0.2rem;
|
||||
padding: 0.05rem 0.4rem;
|
||||
border-radius: 0.5rem;
|
||||
background: rgb(254 249 195);
|
||||
color: rgb(161 98 7);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
:deep(.dark .chat-markdown .leave-action-trigger) {
|
||||
border-color: rgb(96 165 250 / 0.35);
|
||||
background: rgb(30 41 59);
|
||||
color: rgb(147 197 253);
|
||||
}
|
||||
|
||||
:deep(.dark .chat-markdown .leave-action-trigger:hover) {
|
||||
background: rgb(30 64 175 / 0.25);
|
||||
}
|
||||
|
||||
:deep(.dark .chat-markdown .leave-intent-highlight) {
|
||||
background: rgb(113 63 18 / 0.45);
|
||||
color: rgb(253 224 71);
|
||||
}
|
||||
</style>
|
||||
Reference in new issue
Block a user