import { FileText, Loader2 } from "lucide-react";
import { useSearchParams } from "react-router-dom";
import { useCanvasGraphContext } from "../../contexts/CanvasGraphContext";
import type { StreamingArtifactPartial } from "../../api/runs";
import {
getCurrentArtifactContent,
isCodeArtifact,
} from "../../utils/artifact";
import { CanvasArtifactHeader } from "./CanvasArtifactHeader";
import { CanvasEmptyState } from "./CanvasEmptyState";
import { CanvasQuickActionBar } from "./CanvasQuickActionBar";
import { TextArtifactRenderer } from "./TextArtifactRenderer";
import { CodeArtifactRenderer } from "./CodeArtifactRenderer";
import type {
CanvasArtifactCode,
CanvasArtifactText,
} from "../../types/artifact";
// ---------------------------------------------------------------------------
// StreamingOverlay — shown while update_artifact args are streaming in
// ---------------------------------------------------------------------------
function StreamingTextPreview({ partial }: { partial: StreamingArtifactPartial }) {
const hasContent = partial.content && partial.content.trim();
if (!hasContent) {
// Args started but content not yet arrived — show skeleton
return (
{partial.title ? (
{partial.title}
) : null}
AI 正在生成文档…
);
}
// Stream content into the real renderer (read-only, isStreaming = true)
if (partial.type === "code") {
const fakeContent: CanvasArtifactCode = {
index: 0,
type: "code",
title: partial.title ?? "代码",
language: (partial.language as CanvasArtifactCode["language"]) ?? "other",
code: partial.content ?? "",
};
return ;
}
const fakeContent: CanvasArtifactText = {
index: 0,
type: "text",
title: partial.title ?? "文档",
fullMarkdown: partial.content ?? "",
};
return ;
}
// ---------------------------------------------------------------------------
// CanvasArtifactPanel
// ---------------------------------------------------------------------------
export function CanvasArtifactPanel() {
const { artifact, streamingArtifact, isStreaming } = useCanvasGraphContext();
const [searchParams] = useSearchParams();
const initialType = searchParams.get("type") as "text" | "code" | null;
// While streaming and we have partial data, show the streaming preview
if (isStreaming && streamingArtifact) {
return (
{streamingArtifact.title ?? "正在生成…"}
);
}
// While streaming but no partial content yet — show a loading placeholder
// so the template picker doesn't flash during generation.
if (isStreaming && !artifact) {
return (
);
}
if (!artifact) {
return (
);
}
const content = getCurrentArtifactContent(artifact);
return (
{content && isCodeArtifact(content) ? (
) : content ? (
) : null}
);
}