harvesthealth's picture
Refactor: Consolidate AI providers to Blablador (4/5)
8a55782 verified
raw
history blame
3.44 kB
import { NextRequest, NextResponse } from 'next/server';
import { generateObject } from 'ai';
import { z } from 'zod';
import getProviderForModel from '@/lib/ai/provider-manager';
// Schema for the AI's search plan - not file selection!
const searchPlanSchema = z.object({
editType: z.enum([
'UPDATE_COMPONENT',
'ADD_FEATURE',
'FIX_ISSUE',
'UPDATE_STYLE',
'REFACTOR',
'ADD_DEPENDENCY',
'REMOVE_ELEMENT'
]).describe('The type of edit being requested'),
reasoning: z.string().describe('Explanation of the search strategy'),
searchTerms: z.array(z.string()).describe('Specific text to search for (case-insensitive). Be VERY specific - exact button text, class names, etc.'),
regexPatterns: z.array(z.string()).optional().describe('Regex patterns for finding code structures (e.g., "className=[\"\\\'].*header.*[\"\\]")'),
fileTypesToSearch: z.array(z.string()).default(['.jsx', '.tsx', '.js', '.ts']).describe('File extensions to search'),
expectedMatches: z.number().min(1).max(10).default(1).describe('Expected number of matches (helps validate search worked)'),
fallbackSearch: z.object({
terms: z.array(z.string()),
patterns: z.array(z.string()).optional()
}).optional().describe('Backup search if primary fails')
});
export async function POST(request: NextRequest) {
try {
const { prompt, manifest } = await request.json();
console.log('[analyze-edit-intent] Request received');
console.log('[analyze-edit-intent] Prompt:', prompt);
console.log('[analyze-edit-intent] Manifest files count:', manifest?.files ? Object.keys(manifest.files).length : 0);
if (!prompt || !manifest) {
return NextResponse.json({
error: 'prompt and manifest are required'
}, { status: 400 });
}
const validFiles = Object.entries(manifest.files as Record<string, any>)
.filter(([path]) => {
return path.includes('.') && !path.match(/\/\d+$/);
});
const fileSummary = validFiles
.map(([path, info]: [string, any]) => {
const componentName = info.componentInfo?.name || path.split('/').pop();
const childComponents = info.componentInfo?.childComponents?.join(', ') || 'none';
return `- ${path} (${componentName}, renders: ${childComponents})`;
})
.join('\n');
console.log('[analyze-edit-intent] Valid files found:', validFiles.length);
if (validFiles.length === 0) {
console.error('[analyze-edit-intent] No valid files found in manifest');
return NextResponse.json({
success: false,
error: 'No valid files found in manifest'
}, { status: 400 });
}
console.log('[analyze-edit-intent] Analyzing prompt:', prompt);
console.log('[analyze-edit-intent] File summary preview:', fileSummary.split('\n').slice(0, 5).join('\n'));
const { client, actualModel } = getProviderForModel('text');
console.log('[analyze-edit-intent] Using AI model:', actualModel);
const result = await generateObject({
model: client(actualModel),
schema: searchPlanSchema,
messages: [
{
role: 'system',
content: `You are an expert at planning code searches. Your job is to create a search strategy to find the exact code that needs to be edited.
DO NOT GUESS which files to edit. Instead, provide specific search terms that will locate the code.
SEARCH STRATEGY RULES:
1. For text changes (e.g.,