fix(ai): ensure dev container is running before attempting to generate codebase summary
This commit is contained in:
@@ -1,29 +1,47 @@
|
|||||||
import { NextResponse } from 'next/server';
|
import { NextResponse } from "next/server";
|
||||||
import { query } from '@/lib/db-postgres';
|
import { query } from "@/lib/db-postgres";
|
||||||
import { execInDevContainer } from '@/lib/dev-container';
|
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
|
* 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 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.
|
* 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 "";
|
if (!projectId || !projectSlug) return "";
|
||||||
|
|
||||||
try {
|
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,
|
// We run a fast bash script inside the dev container that finds package.json,
|
||||||
// checks for Prisma/Drizzle schemas, and lists the root folders.
|
// checks for Prisma/Drizzle schemas, and lists the root folders.
|
||||||
// Time to execute: ~50ms.
|
// Time to execute: ~50ms.
|
||||||
const bashScript = `
|
const bashScript = `
|
||||||
cd /workspace/${projectSlug} 2>/dev/null || exit 0
|
cd /workspace/${projectSlug} 2>/dev/null || exit 0
|
||||||
|
|
||||||
echo "=== DEPENDENCIES ==="
|
echo "=== DEPENDENCIES ==="
|
||||||
if [ -f package.json ]; then
|
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"
|
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
|
else
|
||||||
echo "No package.json found"
|
echo "No package.json found"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo -e "\n=== ARCHITECTURE ==="
|
echo -e "\n=== ARCHITECTURE ==="
|
||||||
if [ -d src/app ] || [ -d app ]; then echo "- Next.js App Router"; fi
|
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 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 [ -d .svelte-kit ]; then echo "- SvelteKit"; fi
|
||||||
if [ -f vite.config.ts ] || [ -f vite.config.js ]; then echo "- Vite SPA"; 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
|
if [ -f docker-compose.yml ]; then echo "- Docker Compose deployed"; fi
|
||||||
|
|
||||||
echo -e "\n=== ROOT STRUCTURE ==="
|
echo -e "\n=== ROOT STRUCTURE ==="
|
||||||
ls -la | awk '{print $9}' | grep -v "^$" | grep -v "^.$" | grep -v "^..$" | head -n 15 | tr '\n' ', '
|
ls -la | awk '{print $9}' | grep -v "^$" | grep -v "^.$" | grep -v "^..$" | head -n 15 | tr '\n' ', '
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const result = await execInDevContainer(projectId, bashScript);
|
const result = await execInDevContainer(projectId, bashScript);
|
||||||
|
|
||||||
if (result.code !== 0 || !result.stdout.trim()) {
|
if (result.code !== 0 || !result.stdout.trim()) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
@@ -48,7 +66,6 @@ This is a quick summary of what currently exists in \`/workspace/${projectSlug}/
|
|||||||
${result.stdout.trim().slice(0, 1000)}
|
${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.`;
|
Use this to orient yourself. Do not guess the stack; if it says Next.js and Prisma, use Next.js and Prisma.`;
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn("[Codebase Summary] Failed to generate summary:", error);
|
console.warn("[Codebase Summary] Failed to generate summary:", error);
|
||||||
return "";
|
return "";
|
||||||
|
|||||||
Reference in New Issue
Block a user