diff --git a/vibn-frontend/lib/ai/project-context/codebase-summary.ts b/vibn-frontend/lib/ai/project-context/codebase-summary.ts index 6cb2d1d4..65c76d8a 100644 --- a/vibn-frontend/lib/ai/project-context/codebase-summary.ts +++ b/vibn-frontend/lib/ai/project-context/codebase-summary.ts @@ -1,29 +1,47 @@ -import { NextResponse } from 'next/server'; -import { query } from '@/lib/db-postgres'; -import { execInDevContainer } from '@/lib/dev-container'; +import { NextResponse } from "next/server"; +import { query } from "@/lib/db-postgres"; +import { + ensureDevContainer, + execInDevContainer, + getDevContainerStatus, +} from "@/lib/dev-container"; +import { authSession } from "@/lib/auth/session-server"; /** * 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 { +export async function buildCodebaseSummary( + projectId: string, + projectSlug: string, +): Promise { if (!projectId || !projectSlug) return ""; try { + const session = await authSession(); + if (!session?.workspace) return ""; + + // Ensure the container is actually running before we try to exec inside it + await ensureDevContainer({ + projectId, + projectSlug, + projectName: projectSlug, + workspace: session.workspace, + }); // 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 @@ -31,13 +49,13 @@ export async function buildCodebaseSummary(projectId: string, projectSlug: strin 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 ""; } @@ -48,7 +66,6 @@ This is a quick summary of what currently exists in \`/workspace/${projectSlug}/ ${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 "";