/* eslint-disable @next/next/no-img-element */ 'use client'; import { File, ExternalLink, PlusIcon, Download } from 'lucide-react'; import { useState } from 'react'; import { Message } from './ChatWindow'; type PDF = { title: string; url: string; type?: 'academic' | 'document' | 'article'; }; export const SearchPDFs = ({ query, chatHistory, }: { query: string; chatHistory: Message[]; }) => { const [pdfs, setPdfs] = useState(null); const [loading, setLoading] = useState(false); const getPDFType = (title: string): 'academic' | 'document' | 'article' => { const lowerTitle = title.toLowerCase(); if (lowerTitle.includes('paper') || lowerTitle.includes('journal') || lowerTitle.includes('research')) { return 'academic'; } if (lowerTitle.includes('article') || lowerTitle.includes('blog')) { return 'article'; } return 'document'; }; const getTypeColor = (type: 'academic' | 'document' | 'article') => { switch (type) { case 'academic': return 'bg-blue-500/10 text-blue-500'; case 'article': return 'bg-green-500/10 text-green-500'; default: return 'bg-red-500/10 text-red-500'; } }; const formatChatHistory = (history: Message[]) => { return history.map(msg => { return [msg.role === 'user' ? 'human' : 'ai', msg.content]; }); }; const searchForPDFs = async () => { setLoading(true); const chatModelProvider = localStorage.getItem('chatModelProvider'); const chatModel = localStorage.getItem('chatModel'); const customOpenAIBaseURL = localStorage.getItem('openAIBaseURL'); const customOpenAIKey = localStorage.getItem('openAIApiKey'); try { const res = await fetch( `${process.env.NEXT_PUBLIC_API_URL}/search`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ query: `PDF documents about: ${query}`, focusMode: 'webSearch', optimizationMode: 'balanced', history: formatChatHistory(chatHistory), chatModel: { provider: chatModelProvider, model: chatModel, ...(chatModelProvider === 'custom_openai' && { customOpenAIKey: customOpenAIKey, customOpenAIBaseURL: customOpenAIBaseURL, }), }, }), }, ); const data = await res.json(); console.log("Search response:", data); // Extract PDF results from the message and sources let pdfResults: PDF[] = []; // Check for PDF URLs in sources if (data.sources && Array.isArray(data.sources)) { pdfResults = data.sources .filter((source: any) => source.metadata?.url?.toLowerCase().endsWith('.pdf') || source.metadata?.title?.includes('PDF') || source.metadata?.url?.includes('.pdf') ) .map((source: any) => ({ title: source.metadata.title || 'PDF Document', url: source.metadata.url, type: getPDFType(source.metadata.title || '') })); } setPdfs(pdfResults); } catch (error) { console.error('Error searching for PDFs:', error); setPdfs([]); } finally { setLoading(false); } }; return ( <> {!loading && pdfs === null && ( )} {loading && (
{[...Array(4)].map((_, i) => (
))}
)} {pdfs !== null && pdfs.length > 0 && (

PDF Documents

({pdfs.length})
{pdfs.map((pdf, i) => (

{pdf.title}

{new URL(pdf.url).hostname}

))}
)} {pdfs !== null && pdfs.length === 0 && (

PDF Documents

No PDF documents found related to your query.

)} ); };