import { NextResponse } from 'next/server'; import { query } from '@/lib/db-postgres'; import { execInDevContainer } from '@/lib/dev-container'; /** * Builds a fast, high-level summary of the active project's codebase * to inject into the system prompt. This prevents the AI from having * to blind-search the repo to figure out the tech stack on every turn. */ export async function buildCodebaseSummary(projectId: string, projectSlug: string): Promise { if (!projectId || !projectSlug) return ""; try { // We run a fast bash script inside the dev container that finds package.json, // checks for Prisma/Drizzle schemas, and lists the root folders. // Time to execute: ~50ms. const bashScript = ` cd /workspace/${projectSlug} 2>/dev/null || exit 0 echo "=== DEPENDENCIES ===" if [ -f package.json ]; then node -e "const pkg=require('./package.json'); console.log('Dependencies:', Object.keys(pkg.dependencies||{}).join(', ')); console.log('DevDependencies:', Object.keys(pkg.devDependencies||{}).join(', '));" 2>/dev/null || echo "Found package.json" else echo "No package.json found" fi echo -e "\n=== ARCHITECTURE ===" if [ -d src/app ] || [ -d app ]; then echo "- Next.js App Router"; fi if [ -f prisma/schema.prisma ]; then echo "- Prisma ORM (prisma/schema.prisma)"; fi if [ -f drizzle.config.ts ]; then echo "- Drizzle ORM"; fi if [ -d .svelte-kit ]; then echo "- SvelteKit"; fi if [ -f vite.config.ts ] || [ -f vite.config.js ]; then echo "- Vite SPA"; fi if [ -f docker-compose.yml ]; then echo "- Docker Compose deployed"; fi echo -e "\n=== ROOT STRUCTURE ===" ls -la | awk '{print $9}' | grep -v "^$" | grep -v "^.$" | grep -v "^..$" | head -n 15 | tr '\n' ', ' `; const result = await execInDevContainer(projectId, bashScript); if (result.code !== 0 || !result.stdout.trim()) { return ""; } return `\n## CODEBASE SUMMARY (Auto-detected) This is a quick summary of what currently exists in \`/workspace/${projectSlug}/\`: \`\`\`text ${result.stdout.trim().slice(0, 1000)} \`\`\` Use this to orient yourself. Do not guess the stack; if it says Next.js and Prisma, use Next.js and Prisma.`; } catch (error) { console.warn("[Codebase Summary] Failed to generate summary:", error); return ""; } }