import { FileTextIcon, GlobeIcon } from "lucide-react"; import { useEffect, useMemo, useRef, useState, type CSSProperties } from "react"; import type { MessageFileLinkTarget } from "@/components/ai-elements/message.js"; import { useWorkspaceServices } from "@/hooks/useWorkspaceServices.js"; import { useZCodeIntl } from "@/i18n/IntlProvider.js"; import { type AssistantPreviewCard, type AssistantPreviewCardFileStatService, type AssistantPreviewCardsAutoOpenRequest, getAssistantPreviewCardFilePath, shouldOpenAssistantHtmlInBrowser, } from "@/lib/assistantPreviewCards.js"; import { getAssistantPreviewCardsValidationSignature, resolveAssistantPreviewCardsWithoutFileStat, resolveValidatedAssistantPreviewCards, } from "@/lib/assistantPreviewCardValidation.js"; import type { CodeViewerSource } from "@/lib/codeViewer.js"; import { FileDisplayIcon, resolveFileDisplayDescriptor } from "@/lib/fileDisplay.js"; import { OpenSplitButton } from "@/OpenSplitButton.js"; interface AssistantPreviewCardValidationResult { visibleCards: AssistantPreviewCard[]; settled: boolean; } function buildAssistantPreviewCardFileSource( card: Extract, scope: { workspacePath?: string; workspaceIdentity?: string; workspaceRemoteSessionId?: string; }, ): CodeViewerSource { return { type: "file", title: card.title, path: card.path, ...(scope.workspacePath ? { workspacePath: scope.workspacePath } : {}), ...(scope.workspaceIdentity ? { workspaceIdentity: scope.workspaceIdentity } : {}), ...(scope.workspaceRemoteSessionId ? { workspaceRemoteSessionId: scope.workspaceRemoteSessionId } : {}), }; } function buildAssistantPreviewPptxAutoOpenRequest( visibleCards: readonly AssistantPreviewCard[], baseKey: string, scope: { workspacePath?: string; workspaceIdentity?: string; workspaceRemoteSessionId?: string; }, ): AssistantPreviewCardsAutoOpenRequest | null { const pptxCards = visibleCards.filter( (card): card is Extract => card.type === "file" && card.kind === "pptx", ); if (pptxCards.length === 0) return null; return { // stat 结果对应的最终卡片签名必须进入一次性 key;同一 turn 的候选若晚到, // 不会把旧的校验投影误当成已经消费的新结果。 key: JSON.stringify([baseKey, getAssistantPreviewCardsValidationSignature(pptxCards)]), sources: pptxCards.map((card) => buildAssistantPreviewCardFileSource(card, scope)), }; } interface AssistantPreviewCardsProps { cards: AssistantPreviewCard[]; workspacePath?: string; workspaceIdentity?: string; workspaceRemoteSessionId?: string; onOpenBrowserUrl?: (url: string) => void; onOpenFileLink?: (target: MessageFileLinkTarget) => void; onOpenCodeViewer?: (source: CodeViewerSource) => void; /** Desktop 完成态生成产物:批量打开本轮已通过校验的 PPTX。 */ autoOpenPptxKey?: string; onAutoOpenPptx?: (request: AssistantPreviewCardsAutoOpenRequest) => void; } function shouldRenderAssistantPreviewCardAsFile( card: AssistantPreviewCard, scope: { workspaceIdentity?: string; workspaceRemoteSessionId?: string; }, ): boolean { const filePath = getAssistantPreviewCardFilePath(card); return ( card.type !== "website" || (card.url.startsWith("file://") && filePath !== null && !shouldOpenAssistantHtmlInBrowser({ path: filePath, ...scope })) ); } function useAssistantPreviewCardValidation( cards: AssistantPreviewCard[], scope: { workspacePath?: string; workspaceIdentity?: string; workspaceRemoteSessionId?: string; } = {}, ): AssistantPreviewCardValidationResult { const { fileService } = useWorkspaceServices( scope.workspacePath, scope.workspaceRemoteSessionId, scope.workspaceIdentity, ); const cardsSignature = useMemo(() => getAssistantPreviewCardsValidationSignature(cards), [cards]); const validationCardsRef = useRef({ cards, signature: cardsSignature }); // timeline 重建时会传入内容相同但引用不同的 cards 数组,effect 若依赖数组引用会重复发起 RPC。 // 只在语义签名变化时替换校验快照;workspace fileService 变化仍会使用同一快照重新校验。 if (validationCardsRef.current.signature !== cardsSignature) { validationCardsRef.current = { cards, signature: cardsSignature }; } const validationCards = validationCardsRef.current.cards; const statFreeVisibleCards = useMemo( () => resolveAssistantPreviewCardsWithoutFileStat(validationCards), [validationCards], ); const [validationResult, setValidationResult] = useState<{ fileService: AssistantPreviewCardFileStatService | null; signature: string; visibleCards: AssistantPreviewCard[]; }>(() => ({ fileService: null, signature: "", visibleCards: [], })); useEffect(() => { if (statFreeVisibleCards) { return; } let disposed = false; // 历史消息切换时,上一条消息的已通过 stat 结果会在 effect 清理前短暂复用。 // 这里按当前候选签名批量二次校验,全部算完后一次发布,避免文件卡先闪一批再被替换。 void resolveValidatedAssistantPreviewCards(validationCards, fileService).then( (visibleCards) => { if (disposed) return; setValidationResult({ fileService, signature: cardsSignature, visibleCards, }); }, () => { if (disposed) return; setValidationResult({ fileService, signature: cardsSignature, visibleCards: [], }); }, ); return () => { disposed = true; }; }, [cardsSignature, fileService, statFreeVisibleCards, validationCards]); if (statFreeVisibleCards) { return { visibleCards: statFreeVisibleCards, settled: true, }; } const hasCurrentValidation = validationResult.fileService === fileService && validationResult.signature === cardsSignature; return { visibleCards: hasCurrentValidation ? validationResult.visibleCards : [], settled: hasCurrentValidation, }; } export function AssistantPreviewCards({ cards, workspacePath, workspaceIdentity, workspaceRemoteSessionId, onOpenBrowserUrl, onOpenFileLink, onOpenCodeViewer, autoOpenPptxKey, onAutoOpenPptx, }: AssistantPreviewCardsProps) { const { intl } = useZCodeIntl(); const { visibleCards, settled } = useAssistantPreviewCardValidation(cards, { workspacePath, workspaceIdentity, workspaceRemoteSessionId, }); useEffect(() => { if (!settled || !autoOpenPptxKey || !onAutoOpenPptx) return; const request = buildAssistantPreviewPptxAutoOpenRequest(visibleCards, autoOpenPptxKey, { workspacePath, workspaceIdentity, workspaceRemoteSessionId, }); if (!request) return; // 自动打开只能消费最终可见卡片;这样与 15 个候选、10 张上限和 Host stat // 完全同源,不会打开卡片中并不存在的文件。 onAutoOpenPptx(request); }, [ autoOpenPptxKey, onAutoOpenPptx, settled, visibleCards, workspaceIdentity, workspacePath, workspaceRemoteSessionId, ]); if (!settled || visibleCards.length === 0) { return null; } return (
{visibleCards.map((card, index) => ( ))}
); } function AssistantPreviewCardRow({ card, animationDelayMs, subtitle, workspacePath, workspaceIdentity, workspaceRemoteSessionId, onOpenBrowserUrl, onOpenFileLink, onOpenCodeViewer, }: { card: AssistantPreviewCard; animationDelayMs: number; subtitle: string; workspacePath?: string; workspaceIdentity?: string; workspaceRemoteSessionId?: string; onOpenBrowserUrl?: (url: string) => void; onOpenFileLink?: (target: MessageFileLinkTarget) => void; onOpenCodeViewer?: (source: CodeViewerSource) => void; }) { const filePath = getAssistantPreviewCardFilePath(card); const renderAsFile = shouldRenderAssistantPreviewCardAsFile(card, { workspaceIdentity, workspaceRemoteSessionId, }); const descriptor = filePath ? resolveFileDisplayDescriptor(filePath) : null; const fileSource = renderAsFile && filePath ? { type: "file" as const, title: card.title, path: filePath, ...(workspacePath ? { workspacePath } : {}), ...(workspaceIdentity ? { workspaceIdentity } : {}), ...(workspaceRemoteSessionId ? { workspaceRemoteSessionId } : {}), } : null; return (
{!renderAsFile && card.type === "website" ? ( ) : descriptor ? ( ) : ( )}
{card.title}
{subtitle}
); }