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,111 @@
|
||||
/**
|
||||
* 对话页:Markdown 渲染、消息可见性判断、PLM/请假链接点击。
|
||||
*/
|
||||
import { computed } from 'vue';
|
||||
import { marked } from 'marked';
|
||||
import type { ChatMessageBubbleHandlers } from '../components/messages/ChatMessageBubble.vue';
|
||||
import type { ChatMessage, SourceFileLink } from '../types/chat.types';
|
||||
import type { Ref } from 'vue';
|
||||
|
||||
marked.setOptions({ gfm: true, breaks: true });
|
||||
|
||||
export interface ChatPageMarkdownContext {
|
||||
messages: Ref<ChatMessage[]>;
|
||||
isThinking: Ref<boolean>;
|
||||
prepareMarkdownDisplayText: (text: string) => string;
|
||||
handleLeaveMarkdownClick: (event: MouseEvent) => void;
|
||||
parsePlmPreviewLink: (href: string) => SourceFileLink | null;
|
||||
openPreview: (file: SourceFileLink) => Promise<void>;
|
||||
getMessageLinks: (message: Record<string, unknown>) => SourceFileLink[];
|
||||
getDisplayRetrieverResources: (msgItem: Record<string, unknown>) => Record<string, unknown>[];
|
||||
getSourceLinks: (resources?: unknown[]) => SourceFileLink[];
|
||||
shouldExpandSourceDetails: (msgItem: Record<string, unknown>) => boolean;
|
||||
getSourceTitle: ChatMessageBubbleHandlers['getSourceTitle'];
|
||||
getSourceMeta: ChatMessageBubbleHandlers['getSourceMeta'];
|
||||
getSourceBrief: ChatMessageBubbleHandlers['getSourceBrief'];
|
||||
getShippingInventoryMappingPayload: ChatMessageBubbleHandlers['getShippingInventoryMappingPayload'];
|
||||
canOpenQueryPreview: ChatMessageBubbleHandlers['canOpenQueryPreview'];
|
||||
getQueryPreviewSummary: ChatMessageBubbleHandlers['getQueryPreviewSummary'];
|
||||
openQueryPreviewDialog: ChatMessageBubbleHandlers['openQueryPreviewDialog'];
|
||||
shippingActionButtonClass: ChatMessageBubbleHandlers['shippingActionButtonClass'];
|
||||
onShippingActionClick: ChatMessageBubbleHandlers['onShippingActionClick'];
|
||||
}
|
||||
|
||||
export function createChatPageMarkdownApi(ctx: ChatPageMarkdownContext) {
|
||||
const shouldRenderMessage = (msgItem: ChatMessage): boolean => {
|
||||
if (msgItem?.role === 'user') return true;
|
||||
if (msgItem?.type === 'leave-form' || msgItem?.type === 'leave-confirmation' || msgItem?.type === 'leave-result') return true;
|
||||
const content = (msgItem?.content || '').trim();
|
||||
if (content) return true;
|
||||
if (ctx.getMessageLinks(msgItem as unknown as Record<string, unknown>).length > 0) return true;
|
||||
if (ctx.getDisplayRetrieverResources(msgItem as unknown as Record<string, unknown>).length > 0) return true;
|
||||
return Array.isArray(msgItem?.attribution_parts) && msgItem.attribution_parts.length > 0;
|
||||
};
|
||||
|
||||
const shouldShowThinkingBubble = computed(() => {
|
||||
if (!ctx.isThinking.value) return false;
|
||||
let lastUserIndex = -1;
|
||||
for (let i = ctx.messages.value.length - 1; i >= 0; i--) {
|
||||
if (ctx.messages.value[i]?.role === 'user') {
|
||||
lastUserIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (lastUserIndex < 0) return true;
|
||||
return !ctx.messages.value
|
||||
.slice(lastUserIndex + 1)
|
||||
.some((msgItem) => msgItem?.role === 'assistant' && shouldRenderMessage(msgItem));
|
||||
});
|
||||
|
||||
const handleChatMarkdownClick = (event: MouseEvent) => {
|
||||
const target = event.target as HTMLElement | null;
|
||||
const anchor = target?.closest?.('a') as HTMLAnchorElement | null;
|
||||
const preview = ctx.parsePlmPreviewLink(anchor?.getAttribute('href') || '');
|
||||
if (preview) {
|
||||
event.preventDefault();
|
||||
void ctx.openPreview(preview);
|
||||
return;
|
||||
}
|
||||
ctx.handleLeaveMarkdownClick(event);
|
||||
};
|
||||
|
||||
const renderMarkdown = (text: string) => {
|
||||
if (!text) return '';
|
||||
const displayText = ctx.prepareMarkdownDisplayText(text);
|
||||
try {
|
||||
const rendered = marked.parse(displayText);
|
||||
const html = typeof rendered === 'string' ? rendered : displayText;
|
||||
return html.replace(/<table\b[\s\S]*?<\/table>/gi, (tableHtml: string) => (
|
||||
`<div class="chat-table-scroll"><div class="chat-table-inner">${tableHtml}</div></div>`
|
||||
));
|
||||
} catch {
|
||||
return displayText;
|
||||
}
|
||||
};
|
||||
|
||||
const messageBubbleHandlers: ChatMessageBubbleHandlers = {
|
||||
renderMarkdown,
|
||||
onMarkdownClick: handleChatMarkdownClick,
|
||||
getShippingInventoryMappingPayload: ctx.getShippingInventoryMappingPayload,
|
||||
canOpenQueryPreview: ctx.canOpenQueryPreview,
|
||||
getQueryPreviewSummary: ctx.getQueryPreviewSummary,
|
||||
openQueryPreviewDialog: ctx.openQueryPreviewDialog,
|
||||
shippingActionButtonClass: ctx.shippingActionButtonClass,
|
||||
onShippingActionClick: ctx.onShippingActionClick,
|
||||
getDisplayRetrieverResources: (msgItem) => ctx.getDisplayRetrieverResources(msgItem as unknown as Record<string, unknown>),
|
||||
getSourceLinks: (resources) => ctx.getSourceLinks(resources),
|
||||
shouldExpandSourceDetails: (msgItem) => ctx.shouldExpandSourceDetails(msgItem as unknown as Record<string, unknown>),
|
||||
getSourceTitle: ctx.getSourceTitle,
|
||||
getSourceMeta: ctx.getSourceMeta,
|
||||
getSourceBrief: ctx.getSourceBrief,
|
||||
openPreview: ctx.openPreview,
|
||||
};
|
||||
|
||||
return {
|
||||
shouldRenderMessage,
|
||||
shouldShowThinkingBubble,
|
||||
messageBubbleHandlers,
|
||||
renderMarkdown,
|
||||
handleChatMarkdownClick,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user