This commit is contained in:
2026-02-04 12:51:41 +00:00
parent 4fdbfb0fb3
commit f1e13f87f6
19 changed files with 722 additions and 67 deletions

View File

@@ -5,6 +5,7 @@ import { api } from "@/convex/_generated/api";
import { z } from 'zod'
import { scrapeWebsite, ScrapingError } from '@/lib/scraper'
import { performDeepAnalysis } from '@/lib/analysis-pipeline'
import { logServer } from "@/lib/server-logger";
const bodySchema = z.object({
url: z.string().min(1),
@@ -20,6 +21,7 @@ export async function POST(request: NextRequest) {
detail?: string
}[] = []
try {
const requestId = request.headers.get("x-request-id") ?? undefined;
if (!(await isAuthenticatedNextjs())) {
const redirectUrl = new URL("/auth", request.url);
const referer = request.headers.get("referer");
@@ -99,9 +101,26 @@ export async function POST(request: NextRequest) {
)
}
console.log(`🌐 Scraping: ${url}`)
await logServer({
level: "info",
message: "Scraping website",
labels: ["api", "analyze", "scrape"],
payload: { url },
requestId,
source: "api/analyze",
});
const scrapedContent = await scrapeWebsite(url)
console.log(` ✓ Scraped ${scrapedContent.headings.length} headings, ${scrapedContent.paragraphs.length} paragraphs`)
await logServer({
level: "info",
message: "Scrape complete",
labels: ["api", "analyze", "scrape"],
payload: {
headings: scrapedContent.headings.length,
paragraphs: scrapedContent.paragraphs.length,
},
requestId,
source: "api/analyze",
});
if (jobId) {
await updateTimeline({
key: "scrape",
@@ -111,7 +130,13 @@ export async function POST(request: NextRequest) {
})
}
console.log('🤖 Starting enhanced analysis...')
await logServer({
level: "info",
message: "Starting enhanced analysis",
labels: ["api", "analyze", "analysis"],
requestId,
source: "api/analyze",
});
const progressMap: Record<string, number> = {
features: 35,
competitors: 50,
@@ -128,7 +153,18 @@ export async function POST(request: NextRequest) {
progress: progressMap[update.key] ?? 80,
})
})
console.log(` ✓ Analysis complete: ${analysis.features.length} features, ${analysis.keywords.length} keywords, ${analysis.dorkQueries.length} queries`)
await logServer({
level: "info",
message: "Analysis complete",
labels: ["api", "analyze", "analysis"],
payload: {
features: analysis.features.length,
keywords: analysis.keywords.length,
dorkQueries: analysis.dorkQueries.length,
},
requestId,
source: "api/analyze",
});
if (jobId) {
await updateTimeline({
key: "finalize",
@@ -186,7 +222,14 @@ export async function POST(request: NextRequest) {
persisted = true
}
} catch (persistError) {
console.error("Failed to persist analysis:", persistError)
await logServer({
level: "error",
message: "Failed to persist analysis",
labels: ["api", "analyze", "persist", "error"],
payload: { error: String(persistError) },
requestId,
source: "api/analyze",
});
}
}
@@ -205,7 +248,17 @@ export async function POST(request: NextRequest) {
})
} catch (error: any) {
console.error('❌ Analysis error:', error)
await logServer({
level: "error",
message: "Analysis error",
labels: ["api", "analyze", "error"],
payload: {
message: error?.message,
stack: error?.stack,
},
requestId: request.headers.get("x-request-id") ?? undefined,
source: "api/analyze",
});
if (jobId) {
try {