import type { BaseStream } from "@langchain/langgraph-sdk/react"; import { CheckIcon, ChevronDownIcon, GraduationCapIcon, LightbulbIcon, RocketIcon, SendHorizonal, Sparkles, Square, X, ZapIcon, } from "lucide-react"; import { useCallback, useMemo, useState, type KeyboardEvent } from "react"; import { ModelSelector, ModelSelectorContent, ModelSelectorInput, ModelSelectorItem, ModelSelectorList, ModelSelectorName, ModelSelectorTrigger, } from "@/components/ai-elements/model-selector"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Textarea } from "@/components/ui/textarea"; import { MessageList } from "@/components/workspace/messages"; import { useModels } from "@/core/models/hooks"; import type { AgentThreadState } from "@/core/threads"; import { cn } from "@/lib/utils"; import { useCanvasGraphContext } from "../../contexts/CanvasGraphContext"; import { useCanvasStreamContext } from "../../contexts/CanvasStreamContext"; import { useCanvasThreadContext } from "../../contexts/CanvasThreadContext"; import { useCanvasSelection } from "../../hooks/useCanvasSelection"; import { getCurrentArtifactContent, isCodeArtifact } from "../../utils/artifact"; import { buildMessageWithSelection, hasActiveSelection, resolveCodeSelection, } from "../../utils/selection"; // --------------------------------------------------------------------------- // Types // --------------------------------------------------------------------------- type CanvasMode = "flash" | "thinking" | "pro" | "ultra"; const MODE_LABELS: Record = { flash: "快速", thinking: "推理", pro: "深入", ultra: "超级", }; // --------------------------------------------------------------------------- // ChatWelcome // --------------------------------------------------------------------------- function ChatWelcome() { return (

开始创作

发送消息让 AI 帮你生成文档或代码,
或在右侧选择快速开始模板

); } // --------------------------------------------------------------------------- // ModeIcon // --------------------------------------------------------------------------- function ModeIcon({ mode, className, }: { mode: CanvasMode; className?: string; }) { if (mode === "flash") return ; if (mode === "thinking") return ; if (mode === "pro") return ; return ; } // --------------------------------------------------------------------------- // CanvasChatPanel // --------------------------------------------------------------------------- export function CanvasChatPanel() { const { artifact } = useCanvasGraphContext(); const { thread, sendMessage, threadId } = useCanvasStreamContext(); const { modelName, setModelName } = useCanvasThreadContext(); const { selection, clearSelection } = useCanvasSelection(); const { models } = useModels(); const [input, setInput] = useState(""); const [mode, setMode] = useState("pro"); const [modelDialogOpen, setModelDialogOpen] = useState(false); const hasSelection = hasActiveSelection(selection); const isStreaming = thread.isLoading; const hasMessages = thread.messages.length > 0; const selectedModel = useMemo( () => models.find((m) => m.name === modelName) ?? models[0], [models, modelName], ); const supportsThinking = selectedModel?.supports_thinking ?? false; // 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() { if (isStreaming) { await thread.stop(); return; } const text = input.trim(); if (!text) return; setInput(""); const finalText = hasSelection ? buildMessageWithSelection(text, selection, resolvedCode) : text; clearSelection(); await sendMessage(finalText, { mode }); } function handleKeyDown(e: KeyboardEvent) { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); void handleSend(); } } const handleClarificationSubmit = useCallback( async (answer: string) => { await sendMessage(answer, { mode }); }, [sendMessage, mode], ); const handleModelSelect = useCallback( (name: string) => { setModelName(name); setModelDialogOpen(false); // If selected model doesn't support thinking, fall back to flash const model = models.find((m) => m.name === name); if (!model?.supports_thinking && mode === "thinking") { setMode("flash"); } }, [models, mode, setModelName], ); // MessageList expects BaseStream. The canvas stream returns // BaseStream — structurally compatible for message rendering // since MessageList only reads thread.messages, isLoading, and isThreadLoading. const threadForList = thread as unknown as BaseStream; return (
{/* Message list */}
{hasMessages || isStreaming || thread.isThreadLoading ? ( ) : ( )}
{/* Input area */}
{/* Selection badge */} {hasSelection && selectionPreview && (
{selection.textSelection ? "选中文本:" : "选中代码:"} {selectionPreview}
)} {/* Textarea */}