diff --git a/frontend/lib/tools/auto.ts b/frontend/lib/tools/auto.ts index 84c931f0..2e08420d 100644 --- a/frontend/lib/tools/auto.ts +++ b/frontend/lib/tools/auto.ts @@ -16,6 +16,7 @@ import { ImageSource, Message as StoreMessage, SearchCategory, TextSource, Video import { streamText, tool } from 'ai'; import util from 'util'; import { z } from 'zod'; +import { generateText } from 'ai'; export async function autoAnswer( messages: StoreMessage[], @@ -30,6 +31,22 @@ export async function autoAnswer( const newMessages = getHistoryMessages(isPro, messages); const query = newMessages[newMessages.length - 1].content; + let summaryTitle = ''; + let summaryText = ''; + + try { + generateText({ + model: getLLM('gpt-4o-mini'), + system: 'You are a professional writer. Write simple, clear, and concise content.', + prompt: `Please give the following question a concise title: ${query}`, + }).then(({ text }) => { + summaryTitle = `Summary of "${query}"`; + summaryText = text; + }); + } catch (error) { + console.error('Error generating summary:', error); + } + let texts: TextSource[] = []; let images: ImageSource[] = []; let videos: VideoSource[] = []; @@ -173,6 +190,11 @@ export async function autoAnswer( await streamResponse({ videos: videos }, onStream); } + if (summaryText) { + await streamResponse({ sources: texts, title: summaryTitle }, onStream); + await streamResponse({ summary: summaryText }, onStream); + } + incSearchCount(userId).catch((error) => { console.error(`Failed to increment search count for user ${userId}:`, error); });