(request: Request)
| 6 | import { selectRelevantContent } from '@/lib/content-selection' |
| 7 | |
| 8 | export async function POST(request: Request) { |
| 9 | const requestId = Math.random().toString(36).substring(7) |
| 10 | |
| 11 | try { |
| 12 | const body = await request.json() |
| 13 | const messages = body.messages || [] |
| 14 | |
| 15 | // Extract query from v5 message structure (messages have parts array) |
| 16 | let query = body.query |
| 17 | if (!query && messages.length > 0) { |
| 18 | const lastMessage = messages[messages.length - 1] |
| 19 | if (lastMessage.parts) { |
| 20 | // v5 structure |
| 21 | const textParts = lastMessage.parts.filter((p: any) => p.type === 'text') |
| 22 | query = textParts.map((p: any) => p.text).join(' ') |
| 23 | } else if (lastMessage.content) { |
| 24 | // Fallback for v4 structure |
| 25 | query = lastMessage.content |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | if (!query) { |
| 30 | return NextResponse.json({ error: 'Query is required' }, { status: 400 }) |
| 31 | } |
| 32 | |
| 33 | // Use API key from request body if provided, otherwise fall back to environment variable |
| 34 | const firecrawlApiKey = body.firecrawlApiKey || process.env.FIRECRAWL_API_KEY |
| 35 | const groqApiKey = process.env.GROQ_API_KEY |
| 36 | |
| 37 | if (!firecrawlApiKey) { |
| 38 | return NextResponse.json({ error: 'Firecrawl API key not configured' }, { status: 500 }) |
| 39 | } |
| 40 | |
| 41 | if (!groqApiKey) { |
| 42 | return NextResponse.json({ error: 'Groq API key not configured' }, { status: 500 }) |
| 43 | } |
| 44 | |
| 45 | // Configure Groq with the OSS 120B model |
| 46 | const groq = createGroq({ |
| 47 | apiKey: groqApiKey |
| 48 | }) |
| 49 | |
| 50 | // Always perform a fresh search for each query to ensure relevant results |
| 51 | const isFollowUp = messages.length > 2 |
| 52 | |
| 53 | // Create a UIMessage stream with custom data parts |
| 54 | const stream = createUIMessageStream({ |
| 55 | originalMessages: messages, |
| 56 | execute: async ({ writer }) => { |
| 57 | try { |
| 58 | let sources: Array<{ |
| 59 | url: string |
| 60 | title: string |
| 61 | description?: string |
| 62 | content?: string |
| 63 | markdown?: string |
| 64 | publishedDate?: string |
| 65 | author?: string |
nothing calls this directly
no test coverage detected