mirror of
https://gitee.com/wanwujie/deer-flow
synced 2026-04-03 06:12:14 +08:00
- SafeCitationContent: add loadingOnly and renderBody props.
- loadingOnly: show only loading indicator or null (e.g. write_file step).
- renderBody(parsed): custom body renderer (e.g. artifact preview).
- message-group write_file: use SafeCitationContent(content, isLoading,
rehypePlugins, loadingOnly) instead of local useParsedCitations +
shouldShowCitationLoading + CitationsLoadingIndicator. Pass rehypePlugins
into ToolCall.
- artifact-file-detail markdown preview: use SafeCitationContent with
renderBody((p) => <ArtifactFilePreview ... cleanContent={p.cleanContent}
citationMap={p.citationMap} />). Remove local shouldShowCitationLoading
and CitationsLoadingIndicator branch.
- core/citations: inline buildCitationMap into use-parsed-citations, remove
from utils; stop exporting hasCitationsBlock (internal to shouldShowCitationLoading).
- inline-citation: make InlineCitationCard, InlineCitationCardBody,
InlineCitationSource file-private (no longer exported).
Co-authored-by: Cursor <cursoragent@cursor.com>
---
refactor(前端): 收拢引用逻辑、精简导出与实现
- SafeCitationContent 新增 loadingOnly、renderBody。
- loadingOnly:仅显示加载或 null(如 write_file 步骤)。
- renderBody(parsed):自定义正文渲染(如 artifact 预览)。
- message-group write_file:改用 SafeCitationContent(loadingOnly),去掉
本地 useParsedCitations + shouldShowCitationLoading + CitationsLoadingIndicator,
并向 ToolCall 传入 rehypePlugins。
- artifact-file-detail 的 markdown 预览:改用 SafeCitationContent +
renderBody 渲染 ArtifactFilePreview,去掉本地加载判断与
CitationsLoadingIndicator 分支。
- core/citations:buildCitationMap 内联到 use-parsed-citations 并从 utils
删除;hasCitationsBlock 不再导出(仅 shouldShowCitationLoading 内部使用)。
- inline-citation:InlineCitationCard/Body/Source 改为文件内私有,不再导出。
336 lines
10 KiB
TypeScript
336 lines
10 KiB
TypeScript
import {
|
|
Code2Icon,
|
|
CopyIcon,
|
|
DownloadIcon,
|
|
EyeIcon,
|
|
LoaderIcon,
|
|
PackageIcon,
|
|
SquareArrowOutUpRightIcon,
|
|
XIcon,
|
|
} from "lucide-react";
|
|
import * as React from "react";
|
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
import { toast } from "sonner";
|
|
import { Streamdown } from "streamdown";
|
|
|
|
import {
|
|
Artifact,
|
|
ArtifactAction,
|
|
ArtifactActions,
|
|
ArtifactContent,
|
|
ArtifactHeader,
|
|
ArtifactTitle,
|
|
} from "@/components/ai-elements/artifact";
|
|
import { createCitationMarkdownComponents } from "@/components/ai-elements/inline-citation";
|
|
import { Select, SelectItem } from "@/components/ui/select";
|
|
import {
|
|
SelectContent,
|
|
SelectGroup,
|
|
SelectTrigger,
|
|
SelectValue,
|
|
} from "@/components/ui/select";
|
|
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group";
|
|
import { CodeEditor } from "@/components/workspace/code-editor";
|
|
import { useArtifactContent } from "@/core/artifacts/hooks";
|
|
import { urlOfArtifact } from "@/core/artifacts/utils";
|
|
import type { Citation } from "@/core/citations";
|
|
import {
|
|
contentWithoutCitationsFromParsed,
|
|
removeAllCitations,
|
|
useParsedCitations,
|
|
} from "@/core/citations";
|
|
import { useI18n } from "@/core/i18n/hooks";
|
|
import { installSkill } from "@/core/skills/api";
|
|
import { streamdownPlugins } from "@/core/streamdown";
|
|
import { checkCodeFile, getFileName } from "@/core/utils/files";
|
|
import { env } from "@/env";
|
|
import { cn } from "@/lib/utils";
|
|
|
|
import { Tooltip } from "../tooltip";
|
|
|
|
import { SafeCitationContent } from "../messages/safe-citation-content";
|
|
import { useThread } from "../messages/context";
|
|
|
|
import { useArtifacts } from "./context";
|
|
|
|
export function ArtifactFileDetail({
|
|
className,
|
|
filepath: filepathFromProps,
|
|
threadId,
|
|
}: {
|
|
className?: string;
|
|
filepath: string;
|
|
threadId: string;
|
|
}) {
|
|
const { t } = useI18n();
|
|
const { artifacts, setOpen, select } = useArtifacts();
|
|
const isWriteFile = useMemo(() => {
|
|
return filepathFromProps.startsWith("write-file:");
|
|
}, [filepathFromProps]);
|
|
const filepath = useMemo(() => {
|
|
if (isWriteFile) {
|
|
const url = new URL(filepathFromProps);
|
|
return decodeURIComponent(url.pathname);
|
|
}
|
|
return filepathFromProps;
|
|
}, [filepathFromProps, isWriteFile]);
|
|
const isSkillFile = useMemo(() => {
|
|
return filepath.endsWith(".skill");
|
|
}, [filepath]);
|
|
const { isCodeFile, language } = useMemo(() => {
|
|
if (isWriteFile) {
|
|
let language = checkCodeFile(filepath).language;
|
|
language ??= "text";
|
|
return { isCodeFile: true, language };
|
|
}
|
|
// Treat .skill files as markdown (they contain SKILL.md)
|
|
if (isSkillFile) {
|
|
return { isCodeFile: true, language: "markdown" };
|
|
}
|
|
return checkCodeFile(filepath);
|
|
}, [filepath, isWriteFile, isSkillFile]);
|
|
const previewable = useMemo(() => {
|
|
return (language === "html" && !isWriteFile) || language === "markdown";
|
|
}, [isWriteFile, language]);
|
|
const { thread } = useThread();
|
|
const { content } = useArtifactContent({
|
|
threadId,
|
|
filepath: filepathFromProps,
|
|
enabled: isCodeFile && !isWriteFile,
|
|
});
|
|
|
|
const parsed = useParsedCitations(
|
|
language === "markdown" ? (content ?? "") : "",
|
|
);
|
|
const cleanContent =
|
|
language === "markdown" && content ? parsed.cleanContent : (content ?? "");
|
|
const contentWithoutCitations =
|
|
language === "markdown" && content
|
|
? contentWithoutCitationsFromParsed(parsed)
|
|
: (content ?? "");
|
|
|
|
const [viewMode, setViewMode] = useState<"code" | "preview">("code");
|
|
const [isInstalling, setIsInstalling] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (previewable) {
|
|
setViewMode("preview");
|
|
} else {
|
|
setViewMode("code");
|
|
}
|
|
}, [previewable]);
|
|
|
|
const handleInstallSkill = useCallback(async () => {
|
|
if (isInstalling) return;
|
|
|
|
setIsInstalling(true);
|
|
try {
|
|
const result = await installSkill({
|
|
thread_id: threadId,
|
|
path: filepath,
|
|
});
|
|
if (result.success) {
|
|
toast.success(result.message);
|
|
} else {
|
|
toast.error(result.message ?? "Failed to install skill");
|
|
}
|
|
} catch (error) {
|
|
console.error("Failed to install skill:", error);
|
|
toast.error("Failed to install skill");
|
|
} finally {
|
|
setIsInstalling(false);
|
|
}
|
|
}, [threadId, filepath, isInstalling]);
|
|
return (
|
|
<Artifact className={cn(className)}>
|
|
<ArtifactHeader className="px-2">
|
|
<div className="flex items-center gap-2">
|
|
<ArtifactTitle>
|
|
{isWriteFile ? (
|
|
<div className="px-2">{getFileName(filepath)}</div>
|
|
) : (
|
|
<Select value={filepath} onValueChange={select}>
|
|
<SelectTrigger className="border-none bg-transparent! shadow-none select-none focus:outline-0 active:outline-0">
|
|
<SelectValue placeholder="Select a file" />
|
|
</SelectTrigger>
|
|
<SelectContent className="select-none">
|
|
<SelectGroup>
|
|
{(artifacts ?? []).map((filepath) => (
|
|
<SelectItem key={filepath} value={filepath}>
|
|
{getFileName(filepath)}
|
|
</SelectItem>
|
|
))}
|
|
</SelectGroup>
|
|
</SelectContent>
|
|
</Select>
|
|
)}
|
|
</ArtifactTitle>
|
|
</div>
|
|
<div className="flex min-w-0 grow items-center justify-center">
|
|
{previewable && (
|
|
<ToggleGroup
|
|
className="mx-auto"
|
|
type="single"
|
|
variant="outline"
|
|
size="sm"
|
|
value={viewMode}
|
|
onValueChange={(value) =>
|
|
setViewMode(value as "code" | "preview")
|
|
}
|
|
>
|
|
<ToggleGroupItem value="code">
|
|
<Code2Icon />
|
|
</ToggleGroupItem>
|
|
<ToggleGroupItem value="preview">
|
|
<EyeIcon />
|
|
</ToggleGroupItem>
|
|
</ToggleGroup>
|
|
)}
|
|
</div>
|
|
<div className="flex items-center gap-2">
|
|
<ArtifactActions>
|
|
{!isWriteFile && filepath.endsWith(".skill") && (
|
|
<Tooltip content={t.toolCalls.skillInstallTooltip}>
|
|
<ArtifactAction
|
|
icon={isInstalling ? LoaderIcon : PackageIcon}
|
|
label={t.common.install}
|
|
tooltip={t.common.install}
|
|
disabled={
|
|
isInstalling ||
|
|
env.NEXT_PUBLIC_STATIC_WEBSITE_ONLY === "true"
|
|
}
|
|
onClick={handleInstallSkill}
|
|
/>
|
|
</Tooltip>
|
|
)}
|
|
{!isWriteFile && (
|
|
<a href={urlOfArtifact({ filepath, threadId })} target="_blank">
|
|
<ArtifactAction
|
|
icon={SquareArrowOutUpRightIcon}
|
|
label={t.common.openInNewWindow}
|
|
tooltip={t.common.openInNewWindow}
|
|
/>
|
|
</a>
|
|
)}
|
|
{isCodeFile && (
|
|
<ArtifactAction
|
|
icon={CopyIcon}
|
|
label={t.clipboard.copyToClipboard}
|
|
disabled={!content}
|
|
onClick={async () => {
|
|
try {
|
|
await navigator.clipboard.writeText(contentWithoutCitations ?? "");
|
|
toast.success(t.clipboard.copiedToClipboard);
|
|
} catch (error) {
|
|
toast.error("Failed to copy to clipboard");
|
|
console.error(error);
|
|
}
|
|
}}
|
|
tooltip={t.clipboard.copyToClipboard}
|
|
/>
|
|
)}
|
|
{!isWriteFile && (
|
|
<a
|
|
href={urlOfArtifact({ filepath, threadId, download: true })}
|
|
target="_blank"
|
|
>
|
|
<ArtifactAction
|
|
icon={DownloadIcon}
|
|
label={t.common.download}
|
|
tooltip={t.common.download}
|
|
/>
|
|
</a>
|
|
)}
|
|
<ArtifactAction
|
|
icon={XIcon}
|
|
label={t.common.close}
|
|
onClick={() => setOpen(false)}
|
|
tooltip={t.common.close}
|
|
/>
|
|
</ArtifactActions>
|
|
</div>
|
|
</ArtifactHeader>
|
|
<ArtifactContent className="p-0">
|
|
{previewable &&
|
|
viewMode === "preview" &&
|
|
language === "markdown" &&
|
|
content && (
|
|
<SafeCitationContent
|
|
content={content}
|
|
isLoading={thread.isLoading}
|
|
rehypePlugins={streamdownPlugins.rehypePlugins}
|
|
className="flex size-full items-center justify-center p-4 my-0"
|
|
renderBody={(p) => (
|
|
<ArtifactFilePreview
|
|
filepath={filepath}
|
|
threadId={threadId}
|
|
content={content}
|
|
language={language ?? "text"}
|
|
cleanContent={p.cleanContent}
|
|
citationMap={p.citationMap}
|
|
/>
|
|
)}
|
|
/>
|
|
)}
|
|
{isCodeFile && viewMode === "code" && (
|
|
<CodeEditor
|
|
className="size-full resize-none rounded-none border-none"
|
|
value={cleanContent ?? ""}
|
|
readonly
|
|
/>
|
|
)}
|
|
{!isCodeFile && (
|
|
<iframe
|
|
className="size-full"
|
|
src={urlOfArtifact({ filepath, threadId })}
|
|
/>
|
|
)}
|
|
</ArtifactContent>
|
|
</Artifact>
|
|
);
|
|
}
|
|
|
|
export function ArtifactFilePreview({
|
|
filepath,
|
|
threadId,
|
|
content,
|
|
language,
|
|
cleanContent,
|
|
citationMap,
|
|
}: {
|
|
filepath: string;
|
|
threadId: string;
|
|
content: string;
|
|
language: string;
|
|
cleanContent: string;
|
|
citationMap: Map<string, Citation>;
|
|
}) {
|
|
if (language === "markdown") {
|
|
const components = createCitationMarkdownComponents({
|
|
citationMap,
|
|
syntheticExternal: true,
|
|
});
|
|
return (
|
|
<div className="size-full px-4">
|
|
<Streamdown
|
|
className="size-full"
|
|
{...streamdownPlugins}
|
|
components={components}
|
|
>
|
|
{cleanContent ?? ""}
|
|
</Streamdown>
|
|
</div>
|
|
);
|
|
}
|
|
if (language === "html") {
|
|
return (
|
|
<iframe
|
|
className="size-full"
|
|
src={urlOfArtifact({ filepath, threadId })}
|
|
/>
|
|
);
|
|
}
|
|
return null;
|
|
}
|
|
|