Skip to content

Clean up route file #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 06-16-updated_error_detection
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 39 additions & 13 deletions app/api/generate/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// app/api/generate/route.ts
import { NextRequest, NextResponse } from 'next/server';
import { processAppRequest } from '@/lib/openai';
import { generateAppCode } from '@/lib/openai';
import { createSandbox } from '@/lib/e2b';
import { componentSchema } from '@/lib/schemas';
import { Benchify } from 'benchify';
Expand All @@ -22,35 +22,40 @@ const extendedComponentSchema = componentSchema.extend({

export async function POST(request: NextRequest) {
try {
const body = await request.json();

// Validate the request using extended schema
const validationResult = extendedComponentSchema.safeParse(body);
// Validate the request
const validation = await validateRequest(request);

if (!validationResult.success) {
return NextResponse.json(
{ error: 'Invalid request format', details: validationResult.error.format() },
{ status: 400 }
);
if (!validation.success) {
return validation.error;
}

const { description, existingFiles, editInstruction, useBuggyCode, useFixer } = validationResult.data;
const { description, existingFiles, editInstruction, useBuggyCode, useFixer } = validation.data;

// Process the app request using centralized logic
const filesToSandbox = await processAppRequest(description, existingFiles, editInstruction, useBuggyCode);
const filesToSandbox = await generateAppCode(description, existingFiles, editInstruction, useBuggyCode);

let repairedFiles = filesToSandbox;

// Repair the generated code using Benchify's API if requested
if (useFixer) {
try {
console.log("Trying fixer")
const { data } = await benchify.fixer.run({
files: filesToSandbox.map((file: { path: string; content: string }) => ({
path: file.path,
contents: file.content
}))
})),
fixes: {
css: false,
imports: false,
stringLiterals: true,
tsSuggestions: false
}
});

console.log('🔧 Benchify fixer data:', data);

if (data) {
const { success, diff } = data;

Expand Down Expand Up @@ -90,4 +95,25 @@ export async function POST(request: NextRequest) {
{ status: 500 }
);
}
}
}


async function validateRequest(request: NextRequest) {
const body = await request.json();
const validationResult = extendedComponentSchema.safeParse(body);

if (!validationResult.success) {
return {
success: false as const,
error: NextResponse.json(
{ error: 'Invalid request format', details: validationResult.error.format() },
{ status: 400 }
)
};
}

return {
success: true as const,
data: validationResult.data
};
}
6 changes: 3 additions & 3 deletions lib/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const fileSchema = z.object({
});

// Generate a new application using AI SDK
export async function generateApp(
export async function createNewApp(
description: string,
): Promise<Array<{ path: string; content: string }>> {
console.log("Creating app with description: ", description);
Expand Down Expand Up @@ -108,7 +108,7 @@ export async function editApp(
}

// Main function to handle both generation and editing
export async function processAppRequest(
export async function generateAppCode(
description: string,
existingFiles?: z.infer<typeof benchifyFileSchema>,
editInstruction?: string,
Expand Down Expand Up @@ -147,7 +147,7 @@ export default App;`
];
} else {
console.log('🤖 Calling AI to generate app...');
return await generateApp(description);
return await createNewApp(description);
}
}
}
Loading