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 (
{isWriteFile ? (
{getFileName(filepath)}
) : ( )}
{previewable && ( setViewMode(value as "code" | "preview") } > )}
{!isWriteFile && filepath.endsWith(".skill") && ( )} {!isWriteFile && ( )} {isCodeFile && ( { 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 && ( )} setOpen(false)} tooltip={t.common.close} />
{previewable && viewMode === "preview" && language === "markdown" && content && ( ( )} /> )} {isCodeFile && viewMode === "code" && ( )} {!isCodeFile && (