fix(ai): ensure dev container is running before attempting to generate codebase summary

This commit is contained in:
2026-05-15 16:22:43 -07:00
parent 485ed73572
commit feebeccd8e

View File

@@ -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<string> {
export async function buildCodebaseSummary(
projectId: string,
projectSlug: string,
): Promise<string> {
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 "";