openCanvas

This commit is contained in:
董宏凯 committed 2026-05-15 23:27:34 +08:00
1 parent 050b1ad78e
commit 1be600155b
18 files changed
+754 -101

No files matched your search

@@ -1,9 +1,15 @@
import { FileText, Loader2 } from "lucide-react";
import { useSearchParams } from "react-router-dom";
import { useCanvasGraphContext } from "../../contexts/CanvasGraphContext";
import { getCurrentArtifactContent, isCodeArtifact } from "../../utils/artifact";
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 {
@@ -11,11 +17,76 @@ import type {
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 (
<div className="h-full flex flex-col items-center justify-center gap-4 text-muted-foreground">
<Loader2 className="w-6 h-6 animate-spin text-primary" />
<div className="space-y-1 text-center">
{partial.title ? (
<p className="text-sm font-medium text-foreground">{partial.title}</p>
) : null}
<p className="text-xs">AI 正在生成文档…</p>
</div>
</div>
);
}
// 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 <CodeArtifactRenderer content={fakeContent} isStreaming />;
}
const fakeContent: CanvasArtifactText = {
index: 0,
type: "text",
title: partial.title ?? "文档",
fullMarkdown: partial.content ?? "",
};
return <TextArtifactRenderer content={fakeContent} isStreaming />;
}
// ---------------------------------------------------------------------------
// CanvasArtifactPanel
// ---------------------------------------------------------------------------
export function CanvasArtifactPanel() {
const { artifact } = useCanvasGraphContext();
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 (
<div className="h-full flex flex-col overflow-hidden">
<div className="flex items-center gap-2 px-4 py-2.5 border-b shrink-0 bg-muted/30">
<FileText className="w-4 h-4 text-muted-foreground" />
<span className="text-sm font-medium truncate flex-1">
{streamingArtifact.title ?? "正在生成…"}
</span>
<Loader2 className="w-3.5 h-3.5 animate-spin text-primary shrink-0" />
</div>
<div className="flex-1 overflow-hidden">
<StreamingTextPreview partial={streamingArtifact} />
</div>
</div>
);
}
if (!artifact) {
return (
<div className="h-full bg-muted/10">
@@ -29,6 +100,7 @@ export function CanvasArtifactPanel() {
return (
<div className="h-full flex flex-col overflow-hidden">
<CanvasArtifactHeader />
<CanvasQuickActionBar />
<div className="flex-1 overflow-hidden">
{content && isCodeArtifact(content) ? (
<CodeArtifactRenderer content={content as CanvasArtifactCode} />