"use client"; import { useEffect, useRef, useState } from "react"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog"; import { useActiveChat } from "@/hooks/use-active-chat"; import { initialArtifactData, useArtifact, useArtifactSelector, } from "@/hooks/use-artifact"; import type { Attachment, ChatMessage } from "@/lib/types"; import { cn } from "@/lib/utils"; import { Artifact } from "./artifact"; import { ChatHeader } from "./chat-header"; import { DataStreamHandler } from "./data-stream-handler"; import { submitEditedMessage } from "./message-editor"; import { Messages } from "./messages"; import { MultimodalInput } from "./multimodal-input"; export function ChatShell() { const { chatId, messages, setMessages, sendMessage, status, stop, regenerate, addToolApprovalResponse, input, setInput, visibilityType, isReadonly, isLoading, votes, currentModelId, setCurrentModelId, showCreditCardAlert, setShowCreditCardAlert, } = useActiveChat(); const [editingMessage, setEditingMessage] = useState( null ); const [attachments, setAttachments] = useState([]); const isArtifactVisible = useArtifactSelector((state) => state.isVisible); const { setArtifact } = useArtifact(); const stopRef = useRef(stop); stopRef.current = stop; const prevChatIdRef = useRef(chatId); useEffect(() => { if (prevChatIdRef.current !== chatId) { prevChatIdRef.current = chatId; stopRef.current(); setArtifact(initialArtifactData); setEditingMessage(null); setAttachments([]); } }, [chatId, setArtifact]); return ( <>
{ const text = msg.parts ?.filter((p) => p.type === "text") .map((p) => p.text) .join(""); setInput(text ?? ""); setEditingMessage(msg); }} regenerate={regenerate} selectedModelId={currentModelId} setMessages={setMessages} status={status} votes={votes} />
{!isReadonly && ( { setEditingMessage(null); setInput(""); }} onModelChange={setCurrentModelId} selectedModelId={currentModelId} selectedVisibilityType={visibilityType} sendMessage={ editingMessage ? async () => { const msg = editingMessage; setEditingMessage(null); await submitEditedMessage({ message: msg, text: input, setMessages, regenerate, }); setInput(""); } : sendMessage } setAttachments={setAttachments} setInput={setInput} setMessages={setMessages} status={status} stop={stop} /> )}
Activate AI Gateway This application requires{" "} {process.env.NODE_ENV === "production" ? "the owner" : "you"} to activate Vercel AI Gateway. Cancel { window.open( "https://vercel.com/d?to=%2F%5Bteam%5D%2F%7E%2Fai%3Fmodal%3Dadd-credit-card", "_blank" ); window.location.href = `${process.env.NEXT_PUBLIC_BASE_PATH ?? ""}/`; }} > Activate ); }