Files
hack/tools/scripts/llm-key-hunter/results/_cache/content/alok1515_carbon-tracker.json
T

1 line
3.1 KiB
JSON

{"repo": "alok1515/carbon-tracker", "files": {"src/app/api/ai-summary/route.ts": {"c3bfa1b1de2b2220ed32343b1eed8c95821022bd": {"t": 1785596025.857457, "n": 2820, "h": "6c00cb1d157514ab", "c": "import { NextRequest, NextResponse } from \"next/server\";\nimport connectToDatabase from \"@/db/mongodb\";\nimport { Emissions } from \"@/db/models\";\n\nexport async function POST(request: NextRequest) {\n try {\n const { userId, totalEmissions, monthlyChange, topCategory } = await request.json();\n\n if (!userId) {\n return NextResponse.json({ error: \"userId is required\" }, { status: 400 });\n }\n\n if (!process.env.OPENROUTER_API_KEY) {\n return NextResponse.json({ error: \"OpenRouter API key not configured\" }, { status: 500 });\n }\n\n const model = process.env.OPENROUTER_MODEL || \"kwaipilot/kat-coder-pro:free\";\n\n const categoryNames: { [key: string]: string } = {\n transportation: \"Transportation\",\n electricity: \"Electricity\",\n heating: \"Heating\",\n flights: \"Flights\",\n food: \"Food\",\n manufacturing: \"Manufacturing\",\n energy: \"Energy\",\n waste: \"Waste\",\n product_lca: \"Product Lifecycle\",\n plastic_waste: \"Plastic Waste\",\n paper_waste: \"Paper Waste\",\n metal_waste: \"Metal Waste\",\n glass_waste: \"Glass Waste\",\n electronics: \"Electronics\",\n furniture: \"Furniture\",\n clothing: \"Clothing\",\n general_waste: \"General Waste\"\n };\n\n const humanReadableCategory = categoryNames[topCategory] || topCategory || \"N/A\";\n\n // Build context\n const context = `\nUSER DATA:\n- Total Carbon Footprint (this month): ${totalEmissions.toFixed(2)} kg CO2\n- Monthly Change: ${monthlyChange > 0 ? \"+\" : \"\"}${monthlyChange.toFixed(2)}%\n- Top Emitter Category: ${humanReadableCategory}\n\nTASK:\nGenerate a concise (max 3 sentences), encouraging, and data-driven summary of the user's carbon footprint.\nIf emissions decreased, celebrate the progress. If they increased, give a gentle tip.\nUse exactly one emoji at the start.\n`;\n\n const response = await fetch(\"https://openrouter.ai/api/v1/chat/completions\", {\n method: \"POST\",\n headers: {\n \"Authorization\": `Bearer ${process.env.OPENROUTER_API_KEY}`,\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify({\n model: model,\n messages: [\n { role: \"system\", content: \"You are an AI Sustainability Expert. Provide short, punchy, and personalized carbon footprint summaries.\" },\n { role: \"user\", content: context }\n ],\n }),\n });\n\n if (!response.ok) {\n throw new Error(\"OpenRouter API error\");\n }\n\n const data = await response.json();\n const summary = data.choices?.[0]?.message?.content || \"Your carbon footprint is being analyzed. Keep tracking to see improvements!\";\n\n return NextResponse.json({ summary });\n\n } catch (error) {\n console.error(\"AI Summary Error:\", error);\n return NextResponse.json({ error: \"Failed to generate AI summary\" }, { status: 500 });\n }\n}\n"}}}}