代码提交

This commit is contained in:
董宏凯 committed 2026-05-15 14:08:01 +08:00
1 parent b81f64a253
commit 050b1ad78e
45 files changed
+5011 -2694

No files matched your search

@@ -0,0 +1,205 @@
import { Loader2, SendHorizonal, Sparkles, User, X } from "lucide-react";
import { useRef, useState, type KeyboardEvent } from "react";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Textarea } from "@/components/ui/textarea";
import { useCanvasGraphContext } from "../../contexts/CanvasGraphContext";
import { useCanvasSelection } from "../../hooks/useCanvasSelection";
import { useCanvasStream } from "../../hooks/useCanvasStream";
import { getCurrentArtifactContent, isCodeArtifact } from "../../utils/artifact";
import {
buildMessageWithSelection,
hasActiveSelection,
resolveCodeSelection,
} from "../../utils/selection";
function ChatWelcome() {
return (
<div className="flex flex-col items-center justify-center h-full text-center px-6 space-y-3">
<div className="w-10 h-10 rounded-full bg-primary/10 flex items-center justify-center">
<Sparkles className="w-5 h-5 text-primary" />
</div>
<div className="space-y-1">
<p className="text-sm font-medium">开始创作</p>
<p className="text-xs text-muted-foreground leading-relaxed">
发送消息让 AI 帮你生成文档或代码,
<br />
或在右侧选择快速开始模板
</p>
</div>
</div>
);
}
export function CanvasChatPanel() {
const { messages, isStreaming, artifact } = useCanvasGraphContext();
const { sendMessage } = useCanvasStream();
const { selection, clearSelection } = useCanvasSelection();
const [input, setInput] = useState("");
const textareaRef = useRef<HTMLTextAreaElement>(null);
const hasMessages = messages.length > 0;
const hasSelection = hasActiveSelection(selection);
// Resolve selected code snippet when artifact is a code artifact
const currentContent = getCurrentArtifactContent(artifact);
const resolvedCode =
selection.codeSelection && currentContent && isCodeArtifact(currentContent)
? resolveCodeSelection(
currentContent.code,
selection.codeSelection.startCharIndex,
selection.codeSelection.endCharIndex,
)
: undefined;
// Build a short preview for the selection badge
const selectionPreview = (() => {
if (selection.textSelection?.selectedText) {
const t = selection.textSelection.selectedText;
return t.length > 40 ? t.slice(0, 40) + "…" : t;
}
if (resolvedCode) {
return resolvedCode.length > 40
? resolvedCode.slice(0, 40) + "…"
: resolvedCode;
}
return null;
})();
async function handleSend() {
const text = input.trim();
if (!text || isStreaming) return;
setInput("");
// Embed selection context into the message when something is selected
const finalText = hasSelection
? buildMessageWithSelection(text, selection, resolvedCode)
: text;
clearSelection();
await sendMessage(finalText);
}
function handleKeyDown(e: KeyboardEvent<HTMLTextAreaElement>) {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
void handleSend();
}
}
return (
<div className="h-full flex flex-col bg-background">
{/* Message list */}
<div className="flex-1 overflow-y-auto min-h-0">
{hasMessages ? (
<div className="p-4 space-y-4">
{messages.map((msg, i) => {
const isHuman = msg.type === "human";
const text =
typeof msg.content === "string"
? msg.content
: JSON.stringify(msg.content);
return (
<div
key={msg.id ?? i}
className={`flex gap-2 ${isHuman ? "justify-end" : "justify-start"}`}
>
{!isHuman && (
<div className="w-6 h-6 rounded-full bg-primary/10 flex items-center justify-center shrink-0 mt-0.5">
<Sparkles className="w-3 h-3 text-primary" />
</div>
)}
<div
className={`max-w-[80%] rounded-lg px-3 py-2 text-sm ${
isHuman
? "bg-primary text-primary-foreground"
: "bg-muted text-foreground"
}`}
>
{text}
</div>
{isHuman && (
<div className="w-6 h-6 rounded-full bg-muted flex items-center justify-center shrink-0 mt-0.5">
<User className="w-3 h-3" />
</div>
)}
</div>
);
})}
{isStreaming && (
<div className="flex gap-2 justify-start">
<div className="w-6 h-6 rounded-full bg-primary/10 flex items-center justify-center shrink-0 mt-0.5">
<Sparkles className="w-3 h-3 text-primary" />
</div>
<div className="bg-muted rounded-lg px-3 py-2">
<Loader2 className="w-3 h-3 animate-spin text-muted-foreground" />
</div>
</div>
)}
</div>
) : (
<ChatWelcome />
)}
</div>
{/* Selection badge */}
{hasSelection && selectionPreview && (
<div className="px-3 pt-2 shrink-0">
<Badge
variant="secondary"
className="flex items-center gap-1.5 max-w-full text-xs font-normal py-1 px-2 h-auto"
>
<span className="shrink-0 text-muted-foreground">
{selection.textSelection ? "选中文本:" : "选中代码:"}
</span>
<span className="truncate min-w-0 font-mono">
{selectionPreview}
</span>
<button
onClick={clearSelection}
className="shrink-0 ml-auto hover:text-foreground text-muted-foreground transition-colors"
aria-label="清除选区"
>
<X className="w-3 h-3" />
</button>
</Badge>
</div>
)}
{/* Input area */}
<div className="border-t p-3 shrink-0">
<div className="flex gap-2 items-end">
<Textarea
ref={textareaRef}
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyDown={handleKeyDown}
placeholder={
hasSelection
? "针对选中内容发出指令…"
: "发送消息以开始… (Enter 发送,Shift+Enter 换行)"
}
className="min-h-[40px] max-h-[160px] resize-none text-sm"
rows={1}
disabled={isStreaming}
/>
<Button
size="icon"
className="h-9 w-9 shrink-0"
disabled={!input.trim() || isStreaming}
onClick={() => void handleSend()}
>
{isStreaming ? (
<Loader2 className="h-4 w-4 animate-spin" />
) : (
<SendHorizonal className="h-4 w-4" />
)}
</Button>
</div>
</div>
</div>
);
}