"use client" import * as React from "react" import { useChat } from "@ai-sdk/react" import { lastAssistantMessageIsCompleteWithToolCalls } from "ai" import { type GatewayModel } from "@/lib/models" import { type ChatUIMessage } from "@/tools" import { ChatMessage } from "@/components/chat-message" import { PromptForm } from "@/components/prompt-form" import { QuestionCard } from "@/components/question-card" import { Suggestions } from "@/components/suggestions" import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert" import { Empty, EmptyContent, EmptyDescription, EmptyHeader, EmptyTitle, } from "@/components/ui/empty" import { MessageScroller, MessageScrollerButton, MessageScrollerContent, MessageScrollerItem, MessageScrollerProvider, MessageScrollerViewport, } from "@/components/ui/message-scroller" export function Chat({ models }: { models: GatewayModel[] }) { const [model, setModel] = React.useState(models[0]?.id ?? "") const { messages, sendMessage, status, stop, error, addToolOutput } = useChat({ // Resume the conversation automatically once the user has answered the // ask_user questionnaire. sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls, }) const resolvedModel = models.some((m) => m.id === model) ? model : (models[0]?.id ?? "") const isBusy = status === "submitted" || status === "streaming" const lastMessage = messages.at(-1) const pendingQuestion = lastMessage?.role === "assistant" ? lastMessage.parts.find( (part): part is Extract => part.type === "tool-ask_user" && (part.state === "input-streaming" || part.state === "input-available") ) : undefined return (
{messages.length === 0 ? (
What can I help with? Pick a model and start chatting. Responses stream through the Vercel AI Gateway. sendMessage( { text: prompt }, { body: { model: resolvedModel } } ) } />
) : ( {messages.map((message) => ( ))} {status === "submitted" && (
Thinking…
)}
{pendingQuestion && ( addToolOutput({ tool: "ask_user", toolCallId, output: answer, options: { body: { model: resolvedModel } }, }) } /> )}
)}
{error && ( Request failed {error.message} )} sendMessage({ text }, { body: { model: resolvedModel } }) } onStop={() => stop()} />
) }