11import { NextRequest , NextResponse } from 'next/server'
2- import { supabase } from '@/lib/supabase'
32
43interface CodeAnalysisRequest {
54 code : string
65 language : string
76 file : string
87 cursorPosition : { line : number ; column : number }
8+ model : any // LLMModel
9+ config : any
910}
1011
1112interface CodeSuggestion {
@@ -30,6 +31,7 @@ interface Diagnostic {
3031
3132export async function POST ( request : NextRequest ) {
3233 try {
34+ const supabase = require ( '@/lib/supabase' ) . supabase
3335 const authHeader = request . headers . get ( 'authorization' )
3436 if ( ! authHeader ) {
3537 return NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } )
@@ -42,14 +44,14 @@ export async function POST(request: NextRequest) {
4244 }
4345
4446 const body : CodeAnalysisRequest = await request . json ( )
45- const { code, language, file, cursorPosition } = body
47+ const { code, language, file, cursorPosition, model , config } = body
4648
47- if ( ! code || ! language ) {
49+ if ( ! code || ! language || ! model || ! config ) {
4850 return NextResponse . json ( { error : 'Missing required fields' } , { status : 400 } )
4951 }
5052
5153 // Analyze code using AI
52- const analysis = await analyzeCodeWithAI ( code , language , file , cursorPosition )
54+ const analysis = await analyzeCodeWithAI ( code , language , file , cursorPosition , model , config )
5355
5456 return NextResponse . json ( {
5557 suggestions : analysis . suggestions ,
@@ -70,109 +72,44 @@ async function analyzeCodeWithAI(
7072 code : string ,
7173 language : string ,
7274 file : string ,
73- cursorPosition : { line : number ; column : number }
75+ cursorPosition : { line : number ; column : number } ,
76+ model : any , // LLMModel
77+ config : any // LLMModelConfig
7478) : Promise < {
7579 suggestions : CodeSuggestion [ ]
7680 diagnostics : Diagnostic [ ]
7781 metrics : any
7882} > {
79- // Get OpenAI API key from environment
80- const apiKey = process . env . OPENAI_API_KEY
81- if ( ! apiKey ) {
82- throw new Error ( 'OpenAI API key not configured' )
83- }
84-
8583 const lines = code . split ( '\n' )
8684 const currentLine = lines [ cursorPosition . line - 1 ] || ''
87- const context = lines . slice ( Math . max ( 0 , cursorPosition . line - 5 ) , cursorPosition . line + 5 ) . join ( '\n' )
8885
8986 try {
90- const response = await fetch ( 'https://api.openai.com/v1/chat/completions' , {
91- method : 'POST' ,
92- headers : {
93- 'Authorization' : `Bearer ${ apiKey } ` ,
94- 'Content-Type' : 'application/json' ,
95- } ,
96- body : JSON . stringify ( {
97- model : 'gpt-4' ,
98- messages : [
99- {
100- role : 'system' ,
101- content : `You are an expert code analyst and assistant. Analyze the provided ${ language } code and provide:
102-
103- 1. **Code Suggestions**: Improvements, optimizations, refactoring opportunities
104- 2. **Diagnostics**: Potential errors, warnings, and issues
105- 3. **Best Practices**: Adherence to ${ language } conventions and patterns
106-
107- Focus on:
108- - Performance optimizations
109- - Code quality improvements
110- - Security vulnerabilities
111- - Best practice violations
112- - Type safety (for TypeScript)
113- - Modern syntax usage
114- - Error handling improvements
115-
116- Return your analysis as JSON in this exact format:
117- {
118- "suggestions": [
119- {
120- "id": "unique-id",
121- "type": "completion|refactor|fix|optimize",
122- "line": number,
123- "column": number,
124- "text": "original text",
125- "replacement": "suggested replacement",
126- "confidence": 0.0-1.0,
127- "description": "brief description"
128- }
129- ],
130- "diagnostics": [
131- {
132- "id": "unique-id",
133- "type": "error|warning|info",
134- "line": number,
135- "column": number,
136- "message": "diagnostic message",
137- "suggestion": "optional fix suggestion"
138- }
139- ]
140- }`
141- } ,
142- {
143- role : 'user' ,
144- content : `Analyze this ${ language } code from file "${ file } ":
145-
146- \`\`\`${ language }
147- ${ code }
148- \`\`\`
149-
150- Current cursor position: Line ${ cursorPosition . line } , Column ${ cursorPosition . column }
151- Current line context: "${ currentLine } "
152-
153- Provide analysis focusing on the area around the cursor and overall code quality.`
154- }
155- ] ,
156- temperature : 0.3 ,
157- max_tokens : 2000 ,
158- } ) ,
87+ const { E2BToolClient, E2BToolType } = require ( '@/lib/e2b/toolClient' )
88+ const e2bToolClient = new E2BToolClient ( )
89+
90+ const result = await e2bToolClient . execute ( {
91+ toolType : 'new_task' as any , //E2BToolType,
92+ userInput : `Analyze this ${ language } code from file "${ file } ":\n\`\`\`${ language } \n${ code } \n\`\`\`\nCurrent cursor position: Line ${ cursorPosition . line } , Column ${ cursorPosition . column } \nCurrent line context: "${ currentLine } "\n\nProvide analysis focusing on the area around the cursor and overall code quality.` ,
93+ model : model ,
94+ config : config ,
95+ userID : 'user-id' , // Replace with actual user ID
96+ teamID : 'team-id' , // Replace with actual team ID
15997 } )
16098
161- if ( ! response . ok ) {
162- throw new Error ( `OpenAI API error: ${ response . statusText } ` )
99+ if ( ! result . success ) {
100+ throw new Error ( result . executionResult . toolResponse || 'Code analysis failed' )
163101 }
164102
165- const result = await response . json ( )
166- const analysisText = result . choices [ 0 ] ?. message ?. content
103+ const analysisText = result . executionResult . aiResponse
167104
168105 if ( ! analysisText ) {
169- throw new Error ( 'No analysis returned from OpenAI ' )
106+ throw new Error ( 'No analysis returned from AI ' )
170107 }
171108
172109 // Parse the JSON response
173110 try {
174111 const analysis = JSON . parse ( analysisText )
175-
112+
176113 // Add IDs if missing
177114 analysis . suggestions = analysis . suggestions ?. map ( ( s : any , i : number ) => ( {
178115 ...s ,
@@ -236,4 +173,4 @@ function calculateComplexity(code: string): number {
236173 }
237174
238175 return complexity
239- }
176+ }
0 commit comments