澄清中间件改为 ToolMessage + after_model 停轮,前端提交前取消残留 run;系统/向导 thread_type 从对话列表排除;岗位总结席位加 write_file 上限。 Co-authored-by: Cursor <cursoragent@cursor.com>
411 lines
15 KiB
TypeScript
411 lines
15 KiB
TypeScript
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 { stopActiveThreadBeforeClarificationReply } from "@/core/threads/hooks";
|
|
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<CanvasMode, string> = {
|
|
flash: "快速",
|
|
thinking: "推理",
|
|
pro: "深入",
|
|
ultra: "超级",
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// ChatWelcome
|
|
// ---------------------------------------------------------------------------
|
|
|
|
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>
|
|
);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// ModeIcon
|
|
// ---------------------------------------------------------------------------
|
|
|
|
function ModeIcon({
|
|
mode,
|
|
className,
|
|
}: {
|
|
mode: CanvasMode;
|
|
className?: string;
|
|
}) {
|
|
if (mode === "flash") return <ZapIcon className={className} />;
|
|
if (mode === "thinking") return <LightbulbIcon className={className} />;
|
|
if (mode === "pro") return <GraduationCapIcon className={className} />;
|
|
return <RocketIcon className={cn(className, "text-[#dabb5e]")} />;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 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<CanvasMode>("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<HTMLTextAreaElement>) {
|
|
if (e.key === "Enter" && !e.shiftKey) {
|
|
e.preventDefault();
|
|
void handleSend();
|
|
}
|
|
}
|
|
|
|
const handleClarificationSubmit = useCallback(
|
|
async (answer: string) => {
|
|
await stopActiveThreadBeforeClarificationReply(threadId, thread);
|
|
await sendMessage(answer, { mode });
|
|
},
|
|
[sendMessage, mode, thread, threadId],
|
|
);
|
|
|
|
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<AgentThreadState>. The canvas stream returns
|
|
// BaseStream<CanvasThreadValues> — structurally compatible for message rendering
|
|
// since MessageList only reads thread.messages, isLoading, and isThreadLoading.
|
|
const threadForList = thread as unknown as BaseStream<AgentThreadState>;
|
|
|
|
return (
|
|
<div className="h-full flex flex-col bg-background">
|
|
{/* Message list */}
|
|
<div className="flex-1 overflow-y-auto min-h-0">
|
|
{hasMessages || isStreaming || thread.isThreadLoading ? (
|
|
<MessageList
|
|
className="size-full"
|
|
threadId={threadId ?? "canvas"}
|
|
thread={threadForList}
|
|
paddingBottom={0}
|
|
onClarificationSubmit={handleClarificationSubmit}
|
|
/>
|
|
) : (
|
|
<ChatWelcome />
|
|
)}
|
|
</div>
|
|
|
|
{/* Input area */}
|
|
<div className="p-3 shrink-0">
|
|
<div className="rounded-2xl border border-border/70 bg-background shadow-[0_2px_12px_-4px_rgba(0,0,0,0.08)] overflow-hidden transition-shadow focus-within:shadow-[0_2px_16px_-4px_rgba(0,0,0,0.14)] dark:shadow-none dark:focus-within:ring-1 dark:focus-within:ring-border">
|
|
{/* Selection badge */}
|
|
{hasSelection && selectionPreview && (
|
|
<div className="px-3 pt-2.5">
|
|
<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>
|
|
)}
|
|
|
|
{/* Textarea */}
|
|
<Textarea
|
|
value={input}
|
|
onChange={(e) => setInput(e.target.value)}
|
|
onKeyDown={handleKeyDown}
|
|
placeholder={
|
|
hasSelection
|
|
? "针对选中内容发出指令…"
|
|
: "发消息…"
|
|
}
|
|
className="border-0 shadow-none rounded-none resize-none text-sm min-h-[60px] max-h-[160px] focus-visible:ring-0 px-3 pt-3 pb-2"
|
|
rows={2}
|
|
disabled={isStreaming}
|
|
/>
|
|
|
|
{/* Toolbar */}
|
|
<div className="flex items-center gap-1 px-2 pb-2 pt-1 border-t border-border/40">
|
|
{/* Mode selector */}
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>
|
|
<Button
|
|
variant="ghost"
|
|
size="sm"
|
|
className="h-7 gap-1 rounded-full px-2.5 text-xs font-normal text-muted-foreground hover:text-foreground hover:bg-muted/60"
|
|
>
|
|
<ModeIcon mode={mode} className="h-3 w-3" />
|
|
<span>{MODE_LABELS[mode]}</span>
|
|
<ChevronDownIcon className="h-3 w-3 opacity-60" />
|
|
</Button>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent align="start" className="w-52">
|
|
<DropdownMenuLabel className="text-xs text-muted-foreground">
|
|
推理模式
|
|
</DropdownMenuLabel>
|
|
<DropdownMenuSeparator />
|
|
<DropdownMenuItem
|
|
className={cn(
|
|
"gap-2 text-sm",
|
|
mode === "flash" ? "text-foreground" : "text-muted-foreground",
|
|
)}
|
|
onClick={() => setMode("flash")}
|
|
>
|
|
<ZapIcon className="h-3.5 w-3.5" />
|
|
<div className="flex flex-col flex-1">
|
|
<span className="font-medium">快速</span>
|
|
<span className="text-xs opacity-70">低延迟,即时响应</span>
|
|
</div>
|
|
{mode === "flash" && <CheckIcon className="h-3.5 w-3.5 ml-auto" />}
|
|
</DropdownMenuItem>
|
|
{supportsThinking && (
|
|
<DropdownMenuItem
|
|
className={cn(
|
|
"gap-2 text-sm",
|
|
mode === "thinking" ? "text-foreground" : "text-muted-foreground",
|
|
)}
|
|
onClick={() => setMode("thinking")}
|
|
>
|
|
<LightbulbIcon className="h-3.5 w-3.5" />
|
|
<div className="flex flex-col flex-1">
|
|
<span className="font-medium">推理</span>
|
|
<span className="text-xs opacity-70">思维链推导</span>
|
|
</div>
|
|
{mode === "thinking" && <CheckIcon className="h-3.5 w-3.5 ml-auto" />}
|
|
</DropdownMenuItem>
|
|
)}
|
|
<DropdownMenuItem
|
|
className={cn(
|
|
"gap-2 text-sm",
|
|
mode === "pro" ? "text-foreground" : "text-muted-foreground",
|
|
)}
|
|
onClick={() => setMode("pro")}
|
|
>
|
|
<GraduationCapIcon className="h-3.5 w-3.5" />
|
|
<div className="flex flex-col flex-1">
|
|
<span className="font-medium">深入</span>
|
|
<span className="text-xs opacity-70">深度分析与研究</span>
|
|
</div>
|
|
{mode === "pro" && <CheckIcon className="h-3.5 w-3.5 ml-auto" />}
|
|
</DropdownMenuItem>
|
|
<DropdownMenuItem
|
|
className={cn(
|
|
"gap-2 text-sm",
|
|
mode === "ultra" ? "text-foreground" : "text-muted-foreground",
|
|
)}
|
|
onClick={() => setMode("ultra")}
|
|
>
|
|
<RocketIcon className={cn("h-3.5 w-3.5", mode === "ultra" && "text-[#dabb5e]")} />
|
|
<div className="flex flex-col flex-1">
|
|
<span className={cn("font-medium", mode === "ultra" && "text-[#dabb5e]")}>超级</span>
|
|
<span className="text-xs opacity-70">全力以赴,最强推理</span>
|
|
</div>
|
|
{mode === "ultra" && <CheckIcon className="h-3.5 w-3.5 ml-auto text-[#dabb5e]" />}
|
|
</DropdownMenuItem>
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
|
|
{/* Model selector */}
|
|
{models.length > 0 && (
|
|
<ModelSelector open={modelDialogOpen} onOpenChange={setModelDialogOpen}>
|
|
<ModelSelectorTrigger asChild>
|
|
<Button
|
|
variant="ghost"
|
|
size="sm"
|
|
className="h-7 gap-1 rounded-full px-2.5 text-xs font-normal text-muted-foreground hover:text-foreground hover:bg-muted/60 max-w-[120px]"
|
|
>
|
|
<ModelSelectorName className="truncate text-xs font-normal">
|
|
{selectedModel?.display_name ?? "选择模型"}
|
|
</ModelSelectorName>
|
|
<ChevronDownIcon className="h-3 w-3 shrink-0 opacity-60" />
|
|
</Button>
|
|
</ModelSelectorTrigger>
|
|
<ModelSelectorContent title="选择模型">
|
|
<ModelSelectorInput placeholder="搜索模型…" />
|
|
<ModelSelectorList>
|
|
{models.map((m) => (
|
|
<ModelSelectorItem
|
|
key={m.name}
|
|
value={m.name}
|
|
onSelect={() => handleModelSelect(m.name)}
|
|
>
|
|
<div className="flex min-w-0 flex-1 flex-col">
|
|
<ModelSelectorName>{m.display_name}</ModelSelectorName>
|
|
<span className="text-muted-foreground truncate text-[10px]">
|
|
{m.model}
|
|
</span>
|
|
</div>
|
|
{m.name === modelName ? (
|
|
<CheckIcon className="ml-auto size-4" />
|
|
) : (
|
|
<div className="ml-auto size-4" />
|
|
)}
|
|
</ModelSelectorItem>
|
|
))}
|
|
</ModelSelectorList>
|
|
</ModelSelectorContent>
|
|
</ModelSelector>
|
|
)}
|
|
|
|
{/* Spacer */}
|
|
<div className="flex-1" />
|
|
|
|
{/* Send / Stop button */}
|
|
<Button
|
|
size="icon"
|
|
variant={isStreaming ? "secondary" : "default"}
|
|
className={cn(
|
|
"h-7 w-7 shrink-0 rounded-full",
|
|
isStreaming && "text-muted-foreground",
|
|
)}
|
|
onClick={() => void handleSend()}
|
|
disabled={!isStreaming && !input.trim()}
|
|
>
|
|
{isStreaming ? (
|
|
<Square className="h-3 w-3 fill-current" />
|
|
) : (
|
|
<SendHorizonal className="h-3.5 w-3.5" />
|
|
)}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|