"use client"; import * as React from "react"; import { ArrowUpIcon, Bot, ChevronDownIcon, Loader2Icon, MoreHorizontalIcon, PaperclipIcon, PencilIcon, PinIcon, PinOffIcon, PlusIcon, Square, Trash2Icon, XIcon, ZapIcon, } from "lucide-react"; import { toast } from "sonner"; import { TodoPopover } from "@/components/todo-popover"; import { Transcript } from "@/components/transcript"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Checkbox } from "@/components/ui/checkbox"; import { DropdownMenu, DropdownMenuContent, DropdownMenuGroup, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; import { ScrollArea } from "@/components/ui/scroll-area"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Textarea } from "@/components/ui/textarea"; import { api } from "@/lib/api"; import { shouldSubmitOnKey, useChatSendMode } from "@/lib/chat-send-mode"; import type { Activity, Agent, ChatAttachment, Conversation, LLMProfile } from "@/lib/types"; import { cn } from "@/lib/utils"; // fmtBytes renders a human file size for attachment chips (mirrors transcript.tsx). function fmtBytes(n: number): string { if (n >= 1 << 20) return `${(n / (1 << 20)).toFixed(1)} MB`; if (n >= 1 << 10) return `${(n / (1 << 10)).toFixed(1)} KB`; return `${n} B`; } // fmtTokens renders a compact token count (1234 → 1.2k, 2_000_000 → 2M). function fmtTokens(n: number): string { if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(n >= 10_000_000 ? 0 : 1)}M`; if (n >= 1000) return `${(n / 1000).toFixed(n >= 10000 ? 0 : 1)}k`; return String(n); } // fmtDuration renders an elapsed milliseconds span compactly (90s → 1m30s). function fmtDuration(ms: number): string { const s = Math.floor(ms / 1000); if (s < 60) return `${s}s`; const m = Math.floor(s / 60); if (m < 60) return `${m}m${String(s % 60).padStart(2, "0")}s`; const h = Math.floor(m / 60); return `${h}h${String(m % 60).padStart(2, "0")}m`; } // HISTORY_PAGE is how many steps one history page loads: the latest page on open, // then one more page each time the user scrolls to the top. Kept modest so a long // thread stays snappy (only ~a page of rows is in the DOM until you scroll up). const HISTORY_PAGE = 200; function conversationIsPinned(conversation: Conversation): boolean { return conversation.pinned ?? Boolean(conversation.pinned_at); } // LiveBadge is the small pulsing "实时" chip reused from the task's main-agent // console — shown while a turn is streaming. function LiveBadge() { return ( 实时 ); } // Composer is the shared bottom input (textarea grows to a cap, Enter sends, // Shift+Enter newlines) — the same affordance across DraftChat and ChatView. function Composer({ value, onChange, onSend, disabled, placeholder, leftSlot, running, onStop, stopDisabled, attachments, onPickFiles, onRemoveAttachment, uploading, }: { value: string; onChange: (v: string) => void; onSend: () => void; disabled: boolean; placeholder: string; leftSlot?: React.ReactNode; running?: boolean; onStop?: () => void; stopDisabled?: boolean; // 方式1 文件上传:传了 onPickFiles 才显示回形针按钮 + 附件 chip 预览。 attachments?: ChatAttachment[]; onPickFiles?: (files: FileList | null) => void; onRemoveAttachment?: (path: string) => void; uploading?: boolean; }) { const fileInputRef = React.useRef(null); const atts = attachments ?? []; // 发送键位由系统设置决定(localStorage),默认 Enter 发送。 const sendMode = useChatSendMode(); function onKeyDown(e: React.KeyboardEvent) { if (!shouldSubmitOnKey(e, sendMode)) return; e.preventDefault(); onSend(); } return (
{atts.length > 0 && (
{atts.map((a) => (
{a.name} {fmtBytes(a.size)} {onRemoveAttachment && ( )}
))}
)}
{leftSlot ?
{leftSlot}
: null} {onPickFiles && ( <> { onPickFiles(e.target.files); e.target.value = ""; // allow re-picking the same file }} /> )}