(request: Request)
| 3 | import { NextResponse } from "next/server"; |
| 4 | |
| 5 | export async function POST(request: Request) { |
| 6 | try { |
| 7 | const { url, searchResults } = await request.json(); |
| 8 | |
| 9 | if (!url || !searchResults) { |
| 10 | return NextResponse.json( |
| 11 | { error: "URL and search results are required" }, |
| 12 | { status: 400 }, |
| 13 | ); |
| 14 | } |
| 15 | |
| 16 | // Parse and format search results for better context |
| 17 | let parsedResults; |
| 18 | try { |
| 19 | parsedResults = JSON.parse(searchResults); |
| 20 | } catch (error) { |
| 21 | console.warn("Failed to parse search results:", error); |
| 22 | parsedResults = searchResults; // Use as is if already parsed |
| 23 | } |
| 24 | |
| 25 | const prompt = `You are a helpful assistant that writes clear, concise summaries of web content. |
| 26 | Based on the search results and content from ${url}, write a brief but comprehensive overview. |
| 27 | |
| 28 | Focus on: |
| 29 | - The main purpose or value proposition |
| 30 | - Key features or main points |
| 31 | - Target audience or use cases |
| 32 | - What makes it unique or noteworthy |
| 33 | |
| 34 | Format the response in markdown and keep it under 200 words. Make it engaging and informative. |
| 35 | |
| 36 | Context from the webpage: |
| 37 | ${JSON.stringify(parsedResults, null, 2)}`; |
| 38 | |
| 39 | const { text } = await generateText({ |
| 40 | model: anthropic("claude-3-haiku-20240307"), |
| 41 | messages: [ |
| 42 | { |
| 43 | role: "user", |
| 44 | content: prompt, |
| 45 | }, |
| 46 | ], |
| 47 | temperature: 0.7, |
| 48 | }); |
| 49 | |
| 50 | return NextResponse.json({ overview: text }); |
| 51 | } catch (error) { |
| 52 | const errorMessage = error instanceof Error ? error.message : "Unknown error occurred"; |
| 53 | console.error("Error generating overview:", errorMessage); |
| 54 | return NextResponse.json( |
| 55 | { error: `Failed to generate overview: ${errorMessage}` }, |
| 56 | { status: 500 }, |
| 57 | ); |
| 58 | } |
| 59 | } |
nothing calls this directly
no outgoing calls
no test coverage detected