An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
Add guest mode with ephemeral chat flow
Yoshiki Miura2d687a92b205
diff --git a/app/api/chat/route.ts b/app/api/chat/route.tsindex efe64e1e..17c76cbe 100644--- a/app/api/chat/route.ts+++ b/app/api/chat/route.ts@@ -4,8 +4,10 @@ import { cookies } from 'next/headers'import { loadChat } from '@/lib/actions/chat'import { calculateConversationTurn, trackChatEvent } from '@/lib/analytics'import { getCurrentUserId } from '@/lib/auth/get-current-user'+import { checkAndEnforceGuestLimit } from '@/lib/rate-limit/guest-limit'import { checkAndEnforceQualityLimit } from '@/lib/rate-limit/quality-limit'import { createChatStreamResponse } from '@/lib/streaming/create-chat-stream-response'+import { createEphemeralChatStreamResponse } from '@/lib/streaming/create-ephemeral-chat-stream-response'import { SearchMode } from '@/lib/types/search'import { selectModel } from '@/lib/utils/model-selection'import { perfLog, perfTime } from '@/lib/utils/perf-logging'@@ -25,7 +27,7 @@ export async function POST(req: Request) {try {const body = await req.json()- const { message, chatId, trigger, messageId, isNewChat } = body+ const { message, messages, chatId, trigger, messageId, isNewChat } = bodyperfLog(`API Route - Start: chatId=${chatId}, trigger=${trigger}, isNewChat=${isNewChat}`@@ -62,14 +64,25 @@ export async function POST(req: Request) {})}- // Check if user is authenticated- if (!userId) {+ const guestChatEnabled = process.env.ENABLE_GUEST_CHAT === 'true'+ const isGuest = !userId+ if (isGuest && !guestChatEnabled) {return new Response('Authentication required', {status: 401,statusText: 'Unauthorized'})}+ if (isGuest) {+ const forwardedFor = req.headers.get('x-forwarded-for') || ''+ const ip =+ forwardedFor.split(',')[0]?.trim() ||+ req.headers.get('x-real-ip') ||+ null+ const guestLimitResponse = await checkAndEnforceGuestLimit(ip)+ if (guestLimitResponse) return guestLimitResponse+ }+const cookieStore = await cookies()// Get search mode from cookie@@ -97,33 +110,45 @@ export async function POST(req: Request) {// Check rate limit for quality modeconst modelTypeCookie = cookieStore.get('modelType')?.value- const modelType =+ const resolvedModelType =modelTypeCookie === 'quality' || modelTypeCookie === 'speed'? modelTypeCookie: undefined- const rateLimitResponse = await checkAndEnforceQualityLimit(- userId,- modelType === 'quality'- )- if (rateLimitResponse) return rateLimitResponse+ const modelType = isGuest ? 'speed' : resolvedModelType+ if (!isGuest) {+ const rateLimitResponse = await checkAndEnforceQualityLimit(+ userId,+ modelType === 'quality'+ )+ if (rateLimitResponse) return rateLimitResponse+ }const streamStart = performance.now()perfLog(`createChatStreamResponse - Start: model=${selectedModel.providerId}:${selectedModel.id}, searchMode=${searchMode}, modelType=${modelType}`)- const response = await createChatStreamResponse({- message,- model: selectedModel,- chatId,- userId: userId, // userId is guaranteed to be non-null after authentication check above- trigger,- messageId,- abortSignal,- isNewChat,- searchMode,- modelType- })+ const response = isGuest+ ? await createEphemeralChatStreamResponse({+ messages: Array.isArray(messages) ? messages : [],+ model: selectedModel,+ abortSignal,+ searchMode,+ modelType,+ chatId+ })+ : await createChatStreamResponse({+ message,+ model: selectedModel,+ chatId,+ userId: userId, // userId is guaranteed to be non-null after authentication check above+ trigger,+ messageId,+ abortSignal,+ isNewChat,+ searchMode,+ modelType+ })perfTime('createChatStreamResponse resolved', streamStart)@@ -134,7 +159,7 @@ export async function POST(req: Request) {let conversationTurn = 1 // Default for new chats// For existing chats, load history and calculate turn number- if (!isNewChat) {+ if (!isNewChat && !isGuest) {const chat = await loadChat(chatId, userId)if (chat?.messages) {// Add 1 to account for the current message being sent@@ -142,18 +167,20 @@ export async function POST(req: Request) {}}- await trackChatEvent({- searchMode,- modelType: modelTypeCookie === 'quality' ? 'quality' : 'speed',- conversationTurn,- isNewChat: isNewChat ?? false,- trigger:- (trigger as 'submit-message' | 'regenerate-message') ??- 'submit-message',- chatId,- userId,- modelId: selectedModel.id- })+ if (!isGuest && userId) {+ await trackChatEvent({+ searchMode,+ modelType: modelTypeCookie === 'quality' ? 'quality' : 'speed',+ conversationTurn,+ isNewChat: isNewChat ?? false,+ trigger:+ (trigger as 'submit-message' | 'regenerate-message') ??+ 'submit-message',+ chatId,+ userId,+ modelId: selectedModel.id+ })+ }} catch (error) {// Log error but don't throw - analytics should never break the appconsole.error('Analytics tracking failed:', error)@@ -162,7 +189,7 @@ export async function POST(req: Request) {// Invalidate the cache for this specific chat after creating the response// This ensures the next load will get fresh data- if (chatId) {+ if (chatId && !isGuest) {revalidateTag(`chat-${chatId}`, 'max')}diff --git a/app/layout.tsx b/app/layout.tsxindex 1d89cb93..1e14224e 100644--- a/app/layout.tsx+++ b/app/layout.tsx@@ -80,7 +80,7 @@ export default async function RootLayout({disableTransitionOnChange><SidebarProvider defaultOpen>- <AppSidebar />+ <AppSidebar hasUser={!!user} /><div className="flex flex-col flex-1 min-w-0"><Header user={user} /><main className="flex flex-1 min-h-0 min-w-0 overflow-hidden">diff --git a/app/page.tsx b/app/page.tsxindex 7314a0e3..1abc98f5 100644--- a/app/page.tsx+++ b/app/page.tsx@@ -1,11 +1,14 @@+import { getCurrentUserId } from '@/lib/auth/get-current-user'+import { BetaBadge } from '@/components/beta-badge'import { ChangelogBanner } from '@/components/changelog-banner'import { Chat } from '@/components/chat'export default async function Page() {+ const userId = await getCurrentUserId()return (<>- <Chat />+ <Chat isGuest={!userId} /><ChangelogBanner /><BetaBadge /></>diff --git a/app/search/[id]/page.tsx b/app/search/[id]/page.tsxindex f2347061..77e499f8 100644--- a/app/search/[id]/page.tsx+++ b/app/search/[id]/page.tsx@@ -44,5 +44,5 @@ export default async function SearchPage(props: {const messages: UIMessage[] = chat.messages- return <Chat id={id} savedMessages={messages} />+ return <Chat id={id} savedMessages={messages} isGuest={!userId} />}diff --git a/app/search/page.tsx b/app/search/page.tsxindex 11f92654..5d82d7e4 100644--- a/app/search/page.tsx+++ b/app/search/page.tsx@@ -1,5 +1,6 @@import { redirect } from 'next/navigation'+import { getCurrentUserId } from '@/lib/auth/get-current-user'import { generateUUID } from '@/lib/utils'import { Chat } from '@/components/chat'@@ -15,5 +16,6 @@ export default async function SearchPage(props: {}const id = generateUUID()- return <Chat id={id} query={q} />+ const userId = await getCurrentUserId()+ return <Chat id={id} query={q} isGuest={!userId} />}diff --git a/components/answer-section.tsx b/components/answer-section.tsxindex 8d6eb84e..0dc13b24 100644--- a/components/answer-section.tsx+++ b/components/answer-section.tsx@@ -29,6 +29,7 @@ export type AnswerSectionProps = {options?: ChatRequestOptions) => Promise<void | string | null | undefined>citationMaps?: Record<string, Record<number, SearchResultItem>>+ isGuest?: boolean}export function AnswerSection({@@ -41,9 +42,11 @@ export function AnswerSection({metadata,status,reload,- citationMaps+ citationMaps,+ isGuest = false}: AnswerSectionProps) {- const enableShare = process.env.NEXT_PUBLIC_SUPABASE_URL !== undefined+ const enableShare =+ process.env.NEXT_PUBLIC_SUPABASE_URL !== undefined && !isGuestconst handleReload = () => {if (reload) {diff --git a/components/app-sidebar.tsx b/components/app-sidebar.tsxindex 7b50f4eb..c20338b7 100644--- a/components/app-sidebar.tsx+++ b/components/app-sidebar.tsx@@ -20,7 +20,11 @@ import { ChatHistorySection } from './sidebar/chat-history-section'import { ChatHistorySkeleton } from './sidebar/chat-history-skeleton'import { IconLogo } from './ui/icons'-export default function AppSidebar() {+export default function AppSidebar({+ hasUser = false+}: {+ hasUser?: boolean+}) {return (<Sidebar side="left" variant="sidebar" collapsible="offcanvas"><SidebarHeader className="flex flex-row justify-between items-center">@@ -41,11 +45,13 @@ export default function AppSidebar() {</SidebarMenuButton></SidebarMenuItem></SidebarMenu>- <div className="flex-1 overflow-y-auto">- <Suspense fallback={<ChatHistorySkeleton />}>- <ChatHistorySection />- </Suspense>- </div>+ {hasUser && (+ <div className="flex-1 overflow-y-auto">+ <Suspense fallback={<ChatHistorySkeleton />}>+ <ChatHistorySection />+ </Suspense>+ </div>+ )}</SidebarContent><SidebarRail /></Sidebar>diff --git a/components/chat-messages.tsx b/components/chat-messages.tsxindex 2516f4c5..44caa4c2 100644--- a/components/chat-messages.tsx+++ b/components/chat-messages.tsx@@ -25,6 +25,7 @@ interface ChatMessagesProps {onQuerySelect: (query: string) => voidstatus: UseChatHelpers<UIMessage<unknown, UIDataTypes, UITools>>['status']chatId?: string+ isGuest?: booleanaddToolResult?: (params: { toolCallId: string; result: any }) => void/** Ref for the scroll container */scrollContainerRef: React.RefObject<HTMLDivElement>@@ -38,6 +39,7 @@ export function ChatMessages({onQuerySelect,status,chatId,+ isGuest = false,addToolResult,scrollContainerRef,onUpdateMessage,@@ -191,6 +193,7 @@ export function ChatMessages({onOpenChange={handleOpenChange}onQuerySelect={onQuerySelect}chatId={chatId}+ isGuest={isGuest}status={status}addToolResult={addToolResult}onUpdateMessage={onUpdateMessage}@@ -217,6 +220,7 @@ export function ChatMessages({onOpenChange={handleOpenChange}onQuerySelect={onQuerySelect}chatId={chatId}+ isGuest={isGuest}status={status}addToolResult={addToolResult}onUpdateMessage={onUpdateMessage}diff --git a/components/chat-panel.tsx b/components/chat-panel.tsxindex f97210e2..e68ad007 100644--- a/components/chat-panel.tsx+++ b/components/chat-panel.tsx@@ -43,6 +43,8 @@ interface ChatPanelProps {setUploadedFiles: React.Dispatch<React.SetStateAction<UploadedFile[]>>/** Callback to reset chatId when starting a new chat */onNewChat?: () => void+ /** Whether the current session is guest */+ isGuest?: boolean}export function ChatPanel({@@ -60,7 +62,8 @@ export function ChatPanel({uploadedFiles,setUploadedFiles,scrollContainerRef,- onNewChat+ onNewChat,+ isGuest = false}: ChatPanelProps) {const router = useRouter()const inputRef = useRef<HTMLTextAreaElement>(null)@@ -227,54 +230,56 @@ export function ChatPanel({{/* Bottom menu area */}<div className="flex items-center justify-between p-3"><div className="flex items-center gap-2">- <FileUploadButton- onFileSelect={async files => {- const newFiles: UploadedFile[] = files.map(file => ({- file,- status: 'uploading'- }))- setUploadedFiles(prev => [...prev, ...newFiles])- await Promise.all(- newFiles.map(async uf => {- const formData = new FormData()- formData.append('file', uf.file)- formData.append('chatId', chatId)- try {- const res = await fetch('/api/upload', {- method: 'POST',- body: formData- })+ {!isGuest && (+ <FileUploadButton+ onFileSelect={async files => {+ const newFiles: UploadedFile[] = files.map(file => ({+ file,+ status: 'uploading'+ }))+ setUploadedFiles(prev => [...prev, ...newFiles])+ await Promise.all(+ newFiles.map(async uf => {+ const formData = new FormData()+ formData.append('file', uf.file)+ formData.append('chatId', chatId)+ try {+ const res = await fetch('/api/upload', {+ method: 'POST',+ body: formData+ })- if (!res.ok) {- throw new Error('Upload failed')- }+ if (!res.ok) {+ throw new Error('Upload failed')+ }- const { file: uploaded } = await res.json()- setUploadedFiles(prev =>- prev.map(f =>- f.file === uf.file- ? {- ...f,- status: 'uploaded',- url: uploaded.url,- name: uploaded.filename,- key: uploaded.key- }- : f+ const { file: uploaded } = await res.json()+ setUploadedFiles(prev =>+ prev.map(f =>+ f.file === uf.file+ ? {+ ...f,+ status: 'uploaded',+ url: uploaded.url,+ name: uploaded.filename,+ key: uploaded.key+ }+ : f+ ))- )- } catch (e) {- toast.error(`Failed to upload ${uf.file.name}`)- setUploadedFiles(prev =>- prev.map(f =>- f.file === uf.file ? { ...f, status: 'error' } : f+ } catch (e) {+ toast.error(`Failed to upload ${uf.file.name}`)+ setUploadedFiles(prev =>+ prev.map(f =>+ f.file === uf.file ? { ...f, status: 'error' } : f+ ))- )- }- })- )- }}- />+ }+ })+ )+ }}+ />+ )}<SearchModeSelector /></div><div className="flex items-center gap-2">@@ -290,7 +295,7 @@ export function ChatPanel({<MessageCirclePlus className="size-4 group-hover:rotate-12 transition-all" /></Button>)}- <ModelTypeSelector />+ <ModelTypeSelector disabled={isGuest} /><Buttontype={isLoading ? 'button' : 'submit'}size={'icon'}diff --git a/components/chat.tsx b/components/chat.tsxindex 9c372440..30e47c9f 100644--- a/components/chat.tsx+++ b/components/chat.tsx@@ -34,11 +34,13 @@ interface ChatSection {export function Chat({id: providedId,savedMessages = [],- query+ query,+ isGuest = false}: {id?: stringsavedMessages?: UIMessage[]query?: string+ isGuest?: boolean}) {const router = useRouter()@@ -102,6 +104,7 @@ export function Chat({trigger, // Use AI SDK's default trigger value directlychatId: chatId,messageId,+ ...(isGuest ? { messages } : {}),message:trigger === 'regenerate-message' &&messageToRegenerate?.role === 'user'@@ -377,7 +380,7 @@ export function Chat({// Push URL state immediately after sending message (for new chats)// Check if we're on the root path (new chat)- if (window.location.pathname === '/') {+ if (!isGuest && window.location.pathname === '/') {window.history.pushState({}, '', `/search/${chatId}`)}}@@ -389,6 +392,21 @@ export function Chat({setUploadedFiles,chatId: chatId})+ const guestDragHandlers = {+ isDragging: false,+ handleDragOver: (e: React.DragEvent<HTMLDivElement>) => {+ e.preventDefault()+ },+ handleDragLeave: (e: React.DragEvent<HTMLDivElement>) => {+ e.preventDefault()+ },+ handleDrop: (e: React.DragEvent<HTMLDivElement>) => {+ e.preventDefault()+ }+ }+ const dragHandlers = isGuest+ ? guestDragHandlers+ : { isDragging, handleDragOver, handleDragLeave, handleDrop }return (<div@@ -397,15 +415,16 @@ export function Chat({messages.length === 0 ? 'items-center justify-center' : '')}data-testid="full-chat"- onDragOver={handleDragOver}- onDragLeave={handleDragLeave}- onDrop={handleDrop}+ onDragOver={dragHandlers.handleDragOver}+ onDragLeave={dragHandlers.handleDragLeave}+ onDrop={dragHandlers.handleDrop}><ChatMessagessections={sections}onQuerySelect={onQuerySelect}status={status}chatId={chatId}+ isGuest={isGuest}addToolResult={({toolCallId,result@@ -465,8 +484,9 @@ export function Chat({setUploadedFiles={setUploadedFiles}scrollContainerRef={scrollContainerRef}onNewChat={handleNewChat}+ isGuest={isGuest}/>- <DragOverlay visible={isDragging} />+ <DragOverlay visible={dragHandlers.isDragging} /><ErrorModalopen={errorModal.open}onOpenChange={open => setErrorModal(prev => ({ ...prev, open }))}diff --git a/components/model-type-selector.tsx b/components/model-type-selector.tsxindex e99f3655..48cc9f36 100644--- a/components/model-type-selector.tsx+++ b/components/model-type-selector.tsx@@ -20,18 +20,24 @@ const MODEL_TYPE_OPTIONS: { value: ModelType; label: string }[] = [{ value: 'quality', label: 'Quality' }]-export function ModelTypeSelector() {+export function ModelTypeSelector({ disabled = false }: { disabled?: boolean }) {const [value, setValue] = useState<ModelType>('speed')const [dropdownOpen, setDropdownOpen] = useState(false)useEffect(() => {+ if (disabled) {+ setValue('speed')+ setCookie('modelType', 'speed')+ return+ }const savedType = getCookie('modelType')if (savedType && ['speed', 'quality'].includes(savedType)) {setValue(savedType as ModelType)}- }, [])+ }, [disabled])const handleTypeSelect = (type: ModelType) => {+ if (disabled) returnsetValue(type)setCookie('modelType', type)setDropdownOpen(false)@@ -45,6 +51,7 @@ export function ModelTypeSelector() {<Buttonvariant="outline"className="text-sm rounded-full shadow-none gap-1 transition-all px-3 py-2 h-auto bg-muted border-none"+ disabled={disabled}><span className="text-xs font-medium">{selectedOption?.label}</span><ChevronDowndiff --git a/components/render-message.tsx b/components/render-message.tsxindex 19a035b6..8bd0bf07 100644--- a/components/render-message.tsx+++ b/components/render-message.tsx@@ -22,6 +22,7 @@ interface RenderMessageProps {onOpenChange: (id: string, open: boolean) => voidonQuerySelect: (query: string) => voidchatId?: string+ isGuest?: booleanstatus?: UseChatHelpers<UIMessage<unknown, UIDataTypes, UITools>>['status']addToolResult?: (params: { toolCallId: string; result: any }) => voidonUpdateMessage?: (messageId: string, newContent: string) => Promise<void>@@ -37,6 +38,7 @@ export function RenderMessage({onOpenChange,onQuerySelect,chatId,+ isGuest = false,status,addToolResult,onUpdateMessage,@@ -145,6 +147,7 @@ export function RenderMessage({)}onOpenChange={open => onOpenChange(messageId, open)}chatId={chatId}+ isGuest={isGuest}showActions={shouldShowActions}messageId={messageId}metadata={message.metadata as UIMessageMetadata | undefined}diff --git a/lib/rate-limit/guest-limit.ts b/lib/rate-limit/guest-limit.tsnew file mode 100644index 00000000..3024a308--- /dev/null+++ b/lib/rate-limit/guest-limit.ts@@ -0,0 +1,109 @@+import { Redis } from '@upstash/redis'++const DEFAULT_GUEST_DAILY_LIMIT = 10++function getGuestDailyLimit(): number {+ const raw = process.env.GUEST_CHAT_DAILY_LIMIT+ const parsed = raw ? Number(raw) : DEFAULT_GUEST_DAILY_LIMIT+ if (!Number.isFinite(parsed) || parsed <= 0) {+ return DEFAULT_GUEST_DAILY_LIMIT+ }+ return Math.floor(parsed)+}++function getSecondsUntilMidnight(): number {+ const now = new Date()+ const midnight = new Date(now)+ midnight.setUTCHours(24, 0, 0, 0)+ return Math.floor((midnight.getTime() - now.getTime()) / 1000)+}++function getNextMidnightTimestamp(): number {+ const now = new Date()+ const midnight = new Date(now)+ midnight.setUTCHours(24, 0, 0, 0)+ return midnight.getTime()+}++async function checkGuestLimit(ip: string): Promise<{+ allowed: boolean+ remaining: number+ resetAt: number+ limit: number+}> {+ if (process.env.MORPHIC_CLOUD_DEPLOYMENT !== 'true') {+ return { allowed: true, remaining: Infinity, resetAt: 0, limit: 0 }+ }++ if (+ !process.env.UPSTASH_REDIS_REST_URL ||+ !process.env.UPSTASH_REDIS_REST_TOKEN+ ) {+ return { allowed: true, remaining: Infinity, resetAt: 0, limit: 0 }+ }++ try {+ const redis = new Redis({+ url: process.env.UPSTASH_REDIS_REST_URL,+ token: process.env.UPSTASH_REDIS_REST_TOKEN+ })++ const dateKey = new Date().toISOString().split('T')[0]+ const key = `rl:guest:chat:${ip}:${dateKey}`+ const count = await Promise.race([+ redis.incr(key),+ new Promise<number>((_, reject) =>+ setTimeout(() => reject(new Error('Redis timeout')), 3000)+ )+ ])++ if (count === 1) {+ const secondsUntilMidnight = getSecondsUntilMidnight()+ await redis.expire(key, secondsUntilMidnight)+ }++ const limit = getGuestDailyLimit()+ const remaining = Math.max(0, limit - count)+ const resetAt = getNextMidnightTimestamp()++ return {+ allowed: count <= limit,+ remaining,+ resetAt,+ limit+ }+ } catch (error) {+ console.error('Guest rate limit check failed:', error)+ return { allowed: true, remaining: Infinity, resetAt: 0, limit: 0 }+ }+}++export async function checkAndEnforceGuestLimit(+ ip: string | null+): Promise<Response | null> {+ if (!ip) return null++ const result = await checkGuestLimit(ip)+ if (!result.allowed) {+ return new Response(+ JSON.stringify({+ error: 'Please sign in to continue.',+ remaining: 0,+ resetAt: result.resetAt,+ limit: result.limit+ }),+ {+ status: 401,+ statusText: 'Unauthorized',+ headers: {+ 'Content-Type': 'application/json',+ 'X-RateLimit-Limit': String(result.limit),+ 'X-RateLimit-Remaining': '0',+ 'X-RateLimit-Reset': String(result.resetAt)+ }+ }+ )+ }++ return null+}diff --git a/lib/streaming/create-ephemeral-chat-stream-response.ts b/lib/streaming/create-ephemeral-chat-stream-response.tsnew file mode 100644index 00000000..3c9dde94--- /dev/null+++ b/lib/streaming/create-ephemeral-chat-stream-response.ts@@ -0,0 +1,149 @@+import {+ consumeStream,+ convertToModelMessages,+ createUIMessageStream,+ createUIMessageStreamResponse,+ pruneMessages,+ smoothStream,+ UIMessage,+ UIMessageStreamWriter+} from 'ai'+import { randomUUID } from 'crypto'+import { Langfuse } from 'langfuse'++import { researcher } from '@/lib/agents/researcher'+import { isTracingEnabled } from '@/lib/utils/telemetry'++import {+ getMaxAllowedTokens,+ shouldTruncateMessages,+ truncateMessages+} from '../utils/context-window'++import { streamRelatedQuestions } from './helpers/stream-related-questions'+import { stripReasoningParts } from './helpers/strip-reasoning-parts'+import { BaseStreamConfig } from './types'++type EphemeralStreamConfig = Pick<+ BaseStreamConfig,+ 'model' | 'abortSignal' | 'searchMode' | 'modelType'+> & {+ messages: UIMessage[]+ chatId?: string+}++export async function createEphemeralChatStreamResponse(+ config: EphemeralStreamConfig+): Promise<Response> {+ const { messages, model, abortSignal, searchMode, modelType, chatId } = config++ if (!messages || messages.length === 0) {+ return new Response('messages are required', {+ status: 400,+ statusText: 'Bad Request'+ })+ }++ // Create parent trace ID for grouping all operations+ let parentTraceId: string | undefined+ let langfuse: Langfuse | undefined++ if (isTracingEnabled()) {+ parentTraceId = randomUUID()+ langfuse = new Langfuse()++ langfuse.trace({+ id: parentTraceId,+ name: 'research',+ metadata: {+ chatId,+ userId: 'guest',+ modelId: `${model.providerId}:${model.id}`,+ trigger: 'submit-message',+ modelType+ }+ })+ }++ const stream = createUIMessageStream<UIMessage>({+ execute: async ({ writer }: { writer: UIMessageStreamWriter }) => {+ try {+ const isOpenAI = `${model.providerId}:${model.id}`.startsWith('openai:')+ const messagesToConvert = isOpenAI+ ? stripReasoningParts(messages)+ : messages++ let modelMessages = await convertToModelMessages(messagesToConvert)++ modelMessages = pruneMessages({+ messages: modelMessages,+ reasoning: 'before-last-message',+ toolCalls: 'before-last-2-messages',+ emptyMessages: 'remove'+ })++ if (shouldTruncateMessages(modelMessages, model)) {+ const maxTokens = getMaxAllowedTokens(model)+ modelMessages = truncateMessages(modelMessages, maxTokens, model.id)+ }++ const researchAgent = researcher({+ model: `${model.providerId}:${model.id}`,+ modelConfig: model,+ writer,+ parentTraceId,+ searchMode,+ modelType+ })++ const result = await researchAgent.stream({+ messages: modelMessages,+ abortSignal,+ experimental_transform: smoothStream({ chunking: 'word' })+ })+ result.consumeStream()+ writer.merge(+ result.toUIMessageStream({+ messageMetadata: ({ part }) => {+ if (part.type === 'start') {+ return {+ traceId: parentTraceId,+ searchMode,+ modelId: `${model.providerId}:${model.id}`+ }+ }+ }+ })+ )++ const responseMessages = (await result.response).messages+ if (responseMessages && responseMessages.length > 0) {+ const lastUserMessage = [...modelMessages]+ .reverse()+ .find(msg => msg.role === 'user')+ const messagesForQuestions = lastUserMessage+ ? [lastUserMessage, ...responseMessages]+ : responseMessages+ await streamRelatedQuestions(+ writer,+ messagesForQuestions,+ abortSignal,+ parentTraceId+ )+ }+ } finally {+ if (langfuse) {+ await langfuse.flushAsync()+ }+ }+ },+ onError: (error: any) => {+ return error instanceof Error ? error.message : String(error)+ }+ })++ return createUIMessageStreamResponse({+ stream,+ consumeSseStream: consumeStream+ })+}