AumCoreAI commited on
Commit
950dc17
·
verified ·
1 Parent(s): 593650f

Update modules/code_intelligence.py

Browse files
Files changed (1) hide show
  1. modules/code_intelligence.py +976 -0
modules/code_intelligence.py CHANGED
@@ -0,0 +1,976 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Code Intelligence Module for AumCore AI
3
+ Version: 1.0.0
4
+ Author: AumCore AI
5
+ Location: /app/modules/code_intelligence.py
6
+ """
7
+
8
+ import ast
9
+ import re
10
+ import json
11
+ import subprocess
12
+ import tempfile
13
+ import os
14
+ from typing import Dict, List, Optional, Tuple, Any
15
+ from dataclasses import dataclass, field
16
+ from enum import Enum
17
+ import autopep8
18
+ import black
19
+ from datetime import datetime
20
+
21
+ class CodeLanguage(Enum):
22
+ """Supported programming languages"""
23
+ PYTHON = "python"
24
+ JAVASCRIPT = "javascript"
25
+ HTML = "html"
26
+ CSS = "css"
27
+ SQL = "sql"
28
+ JAVA = "java"
29
+ CPP = "cpp"
30
+ GO = "go"
31
+ RUST = "rust"
32
+
33
+ class CodeIssueSeverity(Enum):
34
+ """Code issue severity levels"""
35
+ CRITICAL = "critical" # Security/bug that will break
36
+ HIGH = "high" # Major issue needs fixing
37
+ MEDIUM = "medium" # Should be fixed
38
+ LOW = "low" # Nice to have improvements
39
+ INFO = "info" # Informational only
40
+
41
+ @dataclass
42
+ class CodeIssue:
43
+ """Code issue/improvement suggestion"""
44
+ line: int
45
+ column: int
46
+ severity: CodeIssueSeverity
47
+ message: str
48
+ suggestion: Optional[str] = None
49
+ code_snippet: Optional[str] = None
50
+
51
+ @dataclass
52
+ class CodeAnalysisResult:
53
+ """Result of code analysis"""
54
+ language: CodeLanguage
55
+ issues: List[CodeIssue]
56
+ suggestions: List[str]
57
+ complexity_score: float # 0-100, lower is better
58
+ security_score: float # 0-100, higher is better
59
+ readability_score: float # 0-100, higher is better
60
+ estimated_bugs: int
61
+
62
+ class AumCoreCodeIntelligence:
63
+ """
64
+ Advanced Code Intelligence System
65
+ Analyzes, optimizes, and generates code with AI assistance
66
+ """
67
+
68
+ def __init__(self):
69
+ self._code_patterns = self._load_code_patterns()
70
+ self._templates = self._load_code_templates()
71
+
72
+ def _load_code_patterns(self) -> Dict:
73
+ """Load code patterns for analysis"""
74
+ return {
75
+ "security": {
76
+ "python": [
77
+ (r"exec\(", "Avoid exec() - security risk"),
78
+ (r"eval\(", "Avoid eval() - security risk"),
79
+ (r"subprocess\.call.*shell=True", "Avoid shell=True - security risk"),
80
+ (r"pickle\.loads", "Avoid pickle.loads() with untrusted data"),
81
+ (r"input\(\)", "Validate user input() to prevent injection"),
82
+ (r"os\.system", "Use subprocess.run() instead of os.system()"),
83
+ ],
84
+ "javascript": [
85
+ (r"eval\(", "Avoid eval() - security risk"),
86
+ (r"Function\(", "Avoid Function constructor - security risk"),
87
+ (r"innerHTML.*=", "Use textContent instead of innerHTML to prevent XSS"),
88
+ ],
89
+ "sql": [
90
+ (r"'.*\+.*SELECT", "Use parameterized queries to prevent SQL injection"),
91
+ ]
92
+ },
93
+ "performance": {
94
+ "python": [
95
+ (r"for.*in.*range\(len\(", "Use enumerate() instead of range(len())"),
96
+ (r"\.append\(\) in loop", "Consider list comprehension for better performance"),
97
+ (r"global ", "Avoid global variables for better performance"),
98
+ ]
99
+ },
100
+ "best_practices": {
101
+ "python": [
102
+ (r"except:", "Specify exception type instead of bare except"),
103
+ (r"print\(", "Use logging module instead of print() in production"),
104
+ (r"magic_number", "Use named constants instead of magic numbers"),
105
+ ]
106
+ }
107
+ }
108
+
109
+ def _load_code_templates(self) -> Dict:
110
+ """Load code templates for generation"""
111
+ return {
112
+ "python": {
113
+ "web_api": """from fastapi import FastAPI, HTTPException
114
+ from pydantic import BaseModel
115
+ from typing import Optional
116
+
117
+ app = FastAPI()
118
+
119
+ class Item(BaseModel):
120
+ name: str
121
+ description: Optional[str] = None
122
+ price: float
123
+ tax: Optional[float] = None
124
+
125
+ @app.get("/")
126
+ def read_root():
127
+ return {{"message": "Hello World"}}
128
+
129
+ @app.get("/items/{item_id}")
130
+ def read_item(item_id: int, q: Optional[str] = None):
131
+ return {{"item_id": item_id, "q": q}}
132
+
133
+ @app.post("/items/")
134
+ def create_item(item: Item):
135
+ return item
136
+
137
+ if __name__ == "__main__":
138
+ import uvicorn
139
+ uvicorn.run(app, host="0.0.0.0", port=8000)""",
140
+
141
+ "data_processing": """import pandas as pd
142
+ import numpy as np
143
+ from typing import List, Dict
144
+
145
+ def process_data(file_path: str) -> pd.DataFrame:
146
+ \"\"\"
147
+ Process data from CSV file
148
+
149
+ Args:
150
+ file_path: Path to CSV file
151
+
152
+ Returns:
153
+ Processed DataFrame
154
+ \"\"\"
155
+ try:
156
+ df = pd.read_csv(file_path)
157
+
158
+ # Basic data cleaning
159
+ df = df.dropna()
160
+ df = df.drop_duplicates()
161
+
162
+ # Add derived columns if needed
163
+ if 'price' in df.columns and 'quantity' in df.columns:
164
+ df['total'] = df['price'] * df['quantity']
165
+
166
+ return df
167
+ except Exception as e:
168
+ raise ValueError(f"Error processing file {{file_path}}: {{e}}")
169
+
170
+ def analyze_data(df: pd.DataFrame) -> Dict:
171
+ \"\"\"
172
+ Analyze DataFrame and return statistics
173
+
174
+ Args:
175
+ df: Input DataFrame
176
+
177
+ Returns:
178
+ Dictionary of statistics
179
+ \"\"\"
180
+ stats = {{
181
+ "rows": len(df),
182
+ "columns": list(df.columns),
183
+ "numeric_stats": {{}},
184
+ "missing_values": df.isnull().sum().to_dict()
185
+ }}
186
+
187
+ # Calculate numeric column statistics
188
+ numeric_cols = df.select_dtypes(include=[np.number]).columns
189
+ for col in numeric_cols:
190
+ stats["numeric_stats"][col] = {{
191
+ "mean": df[col].mean(),
192
+ "median": df[col].median(),
193
+ "std": df[col].std(),
194
+ "min": df[col].min(),
195
+ "max": df[col].max()
196
+ }}
197
+
198
+ return stats""",
199
+
200
+ "machine_learning": """from sklearn.model_selection import train_test_split
201
+ from sklearn.preprocessing import StandardScaler
202
+ from sklearn.ensemble import RandomForestClassifier
203
+ from sklearn.metrics import accuracy_score, classification_report
204
+ import pandas as pd
205
+ import numpy as np
206
+ import pickle
207
+
208
+ class MLModel:
209
+ def __init__(self):
210
+ self.model = RandomForestClassifier(n_estimators=100, random_state=42)
211
+ self.scaler = StandardScaler()
212
+ self.feature_names = None
213
+
214
+ def prepare_data(self, df: pd.DataFrame, target_column: str):
215
+ \"\"\"Prepare data for training\"\"\"
216
+ X = df.drop(columns=[target_column])
217
+ y = df[target_column]
218
+
219
+ self.feature_names = X.columns.tolist()
220
+
221
+ # Split data
222
+ X_train, X_test, y_train, y_test = train_test_split(
223
+ X, y, test_size=0.2, random_state=42
224
+ )
225
+
226
+ # Scale features
227
+ X_train_scaled = self.scaler.fit_transform(X_train)
228
+ X_test_scaled = self.scaler.transform(X_test)
229
+
230
+ return X_train_scaled, X_test_scaled, y_train, y_test
231
+
232
+ def train(self, X_train, y_train):
233
+ \"\"\"Train the model\"\"\"
234
+ self.model.fit(X_train, y_train)
235
+
236
+ def evaluate(self, X_test, y_test):
237
+ \"\"\"Evaluate model performance\"\"\"
238
+ y_pred = self.model.predict(X_test)
239
+
240
+ accuracy = accuracy_score(y_test, y_pred)
241
+ report = classification_report(y_test, y_pred, output_dict=True)
242
+
243
+ return {{
244
+ "accuracy": accuracy,
245
+ "report": report,
246
+ "feature_importance": dict(zip(self.feature_names, self.model.feature_importances_))
247
+ }}
248
+
249
+ def predict(self, X):
250
+ \"\"\"Make predictions\"\"\"
251
+ X_scaled = self.scaler.transform(X)
252
+ return self.model.predict(X_scaled)
253
+
254
+ def save(self, path: str):
255
+ \"\"\"Save model to file\"\"\"
256
+ with open(path, 'wb') as f:
257
+ pickle.dump({{
258
+ 'model': self.model,
259
+ 'scaler': self.scaler,
260
+ 'features': self.feature_names
261
+ }}, f)
262
+
263
+ def load(self, path: str):
264
+ \"\"\"Load model from file\"\"\"
265
+ with open(path, 'rb') as f:
266
+ data = pickle.load(f)
267
+ self.model = data['model']
268
+ self.scaler = data['scaler']
269
+ self.feature_names = data['features']"""
270
+ },
271
+ "javascript": {
272
+ "react_component": """import React, { useState, useEffect } from 'react';
273
+ import axios from 'axios';
274
+
275
+ const MyComponent = () => {
276
+ const [data, setData] = useState([]);
277
+ const [loading, setLoading] = useState(true);
278
+ const [error, setError] = useState(null);
279
+
280
+ useEffect(() => {
281
+ const fetchData = async () => {
282
+ try {
283
+ const response = await axios.get('https://api.example.com/data');
284
+ setData(response.data);
285
+ setLoading(false);
286
+ } catch (err) {
287
+ setError(err.message);
288
+ setLoading(false);
289
+ }
290
+ };
291
+
292
+ fetchData();
293
+ }, []);
294
+
295
+ if (loading) return <div>Loading...</div>;
296
+ if (error) return <div>Error: {error}</div>;
297
+
298
+ return (
299
+ <div className="my-component">
300
+ <h1>Data List</h1>
301
+ <ul>
302
+ {data.map(item => (
303
+ <li key={item.id}>{item.name}</li>
304
+ ))}
305
+ </ul>
306
+ </div>
307
+ );
308
+ };
309
+
310
+ export default MyComponent;""",
311
+
312
+ "node_api": """const express = require('express');
313
+ const app = express();
314
+ const port = 3000;
315
+
316
+ // Middleware
317
+ app.use(express.json());
318
+
319
+ // Sample data
320
+ let items = [
321
+ { id: 1, name: 'Item 1', description: 'First item' },
322
+ { id: 2, name: 'Item 2', description: 'Second item' }
323
+ ];
324
+
325
+ // Routes
326
+ app.get('/', (req, res) => {
327
+ res.json({ message: 'Welcome to the API' });
328
+ });
329
+
330
+ app.get('/items', (req, res) => {
331
+ res.json(items);
332
+ });
333
+
334
+ app.get('/items/:id', (req, res) => {
335
+ const item = items.find(i => i.id === parseInt(req.params.id));
336
+ if (!item) return res.status(404).json({ error: 'Item not found' });
337
+ res.json(item);
338
+ });
339
+
340
+ app.post('/items', (req, res) => {
341
+ const newItem = {
342
+ id: items.length + 1,
343
+ name: req.body.name,
344
+ description: req.body.description || ''
345
+ };
346
+ items.push(newItem);
347
+ res.status(201).json(newItem);
348
+ });
349
+
350
+ app.put('/items/:id', (req, res) => {
351
+ const item = items.find(i => i.id === parseInt(req.params.id));
352
+ if (!item) return res.status(404).json({ error: 'Item not found' });
353
+
354
+ item.name = req.body.name || item.name;
355
+ item.description = req.body.description || item.description;
356
+
357
+ res.json(item);
358
+ });
359
+
360
+ app.delete('/items/:id', (req, res) => {
361
+ items = items.filter(i => i.id !== parseInt(req.params.id));
362
+ res.status(204).send();
363
+ });
364
+
365
+ // Start server
366
+ app.listen(port, () => {
367
+ console.log(`Server running at http://localhost:${port}`);
368
+ });"""
369
+ },
370
+ "sql": {
371
+ "database_schema": """-- Users table
372
+ CREATE TABLE users (
373
+ id SERIAL PRIMARY KEY,
374
+ username VARCHAR(50) UNIQUE NOT NULL,
375
+ email VARCHAR(100) UNIQUE NOT NULL,
376
+ password_hash VARCHAR(255) NOT NULL,
377
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
378
+ updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
379
+ is_active BOOLEAN DEFAULT TRUE
380
+ );
381
+
382
+ -- Posts table
383
+ CREATE TABLE posts (
384
+ id SERIAL PRIMARY KEY,
385
+ user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
386
+ title VARCHAR(200) NOT NULL,
387
+ content TEXT NOT NULL,
388
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
389
+ updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
390
+ published BOOLEAN DEFAULT FALSE
391
+ );
392
+
393
+ -- Comments table
394
+ CREATE TABLE comments (
395
+ id SERIAL PRIMARY KEY,
396
+ post_id INTEGER REFERENCES posts(id) ON DELETE CASCADE,
397
+ user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
398
+ content TEXT NOT NULL,
399
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
400
+ );
401
+
402
+ -- Indexes for performance
403
+ CREATE INDEX idx_posts_user_id ON posts(user_id);
404
+ CREATE INDEX idx_comments_post_id ON comments(post_id);
405
+ CREATE INDEX idx_comments_user_id ON comments(user_id);
406
+ CREATE INDEX idx_users_email ON users(email);
407
+
408
+ -- Trigger to update updated_at timestamp
409
+ CREATE OR REPLACE FUNCTION update_updated_at_column()
410
+ RETURNS TRIGGER AS $$
411
+ BEGIN
412
+ NEW.updated_at = CURRENT_TIMESTAMP;
413
+ RETURN NEW;
414
+ END;
415
+ $$ language 'plpgsql';
416
+
417
+ CREATE TRIGGER update_users_updated_at BEFORE UPDATE ON users
418
+ FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
419
+
420
+ CREATE TRIGGER update_posts_updated_at BEFORE UPDATE ON posts
421
+ FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();""",
422
+
423
+ "common_queries": """-- Get all active users with their post count
424
+ SELECT
425
+ u.id,
426
+ u.username,
427
+ u.email,
428
+ COUNT(p.id) as post_count,
429
+ MAX(p.created_at) as latest_post
430
+ FROM users u
431
+ LEFT JOIN posts p ON u.id = p.user_id AND p.published = TRUE
432
+ WHERE u.is_active = TRUE
433
+ GROUP BY u.id, u.username, u.email
434
+ ORDER BY post_count DESC;
435
+
436
+ -- Get posts with comments count
437
+ SELECT
438
+ p.id,
439
+ p.title,
440
+ p.content,
441
+ u.username as author,
442
+ COUNT(c.id) as comment_count,
443
+ p.created_at
444
+ FROM posts p
445
+ JOIN users u ON p.user_id = u.id
446
+ LEFT JOIN comments c ON p.id = c.post_id
447
+ WHERE p.published = TRUE
448
+ GROUP BY p.id, p.title, p.content, u.username, p.created_at
449
+ ORDER BY p.created_at DESC;
450
+
451
+ -- Search posts by keyword
452
+ SELECT
453
+ p.id,
454
+ p.title,
455
+ p.content,
456
+ u.username,
457
+ p.created_at
458
+ FROM posts p
459
+ JOIN users u ON p.user_id = u.id
460
+ WHERE p.published = TRUE
461
+ AND (p.title ILIKE '%search_term%' OR p.content ILIKE '%search_term%')
462
+ ORDER BY
463
+ CASE
464
+ WHEN p.title ILIKE '%search_term%' THEN 1
465
+ ELSE 2
466
+ END,
467
+ p.created_at DESC;"""
468
+ }
469
+ }
470
+
471
+ def analyze_code(self, code: str, language: CodeLanguage = CodeLanguage.PYTHON) -> CodeAnalysisResult:
472
+ """
473
+ Analyze code for issues and improvements
474
+
475
+ Args:
476
+ code: Source code to analyze
477
+ language: Programming language
478
+
479
+ Returns:
480
+ CodeAnalysisResult with analysis details
481
+ """
482
+ issues = []
483
+ suggestions = []
484
+
485
+ # Language-specific analysis
486
+ if language == CodeLanguage.PYTHON:
487
+ issues.extend(self._analyze_python_code(code))
488
+ elif language == CodeLanguage.JAVASCRIPT:
489
+ issues.extend(self._analyze_javascript_code(code))
490
+
491
+ # General pattern matching
492
+ issues.extend(self._pattern_match_code(code, language.value))
493
+
494
+ # Complexity analysis
495
+ complexity_score = self._calculate_complexity(code, language)
496
+
497
+ # Security analysis
498
+ security_score = self._calculate_security_score(code, language, issues)
499
+
500
+ # Readability analysis
501
+ readability_score = self._calculate_readability_score(code, language)
502
+
503
+ # Estimate bugs
504
+ estimated_bugs = self._estimate_bugs(issues)
505
+
506
+ # Generate suggestions
507
+ suggestions = self._generate_suggestions(issues, code, language)
508
+
509
+ return CodeAnalysisResult(
510
+ language=language,
511
+ issues=issues,
512
+ suggestions=suggestions,
513
+ complexity_score=complexity_score,
514
+ security_score=security_score,
515
+ readability_score=readability_score,
516
+ estimated_bugs=estimated_bugs
517
+ )
518
+
519
+ def _analyze_python_code(self, code: str) -> List[CodeIssue]:
520
+ """Analyze Python code specifically"""
521
+ issues = []
522
+
523
+ try:
524
+ # Parse AST for deeper analysis
525
+ tree = ast.parse(code)
526
+
527
+ # AST-based checks
528
+ for node in ast.walk(tree):
529
+ # Check for bare except
530
+ if isinstance(node, ast.ExceptHandler) and node.type is None:
531
+ issues.append(CodeIssue(
532
+ line=node.lineno,
533
+ column=node.col_offset,
534
+ severity=CodeIssueSeverity.MEDIUM,
535
+ message="Bare except clause - specify exception type",
536
+ suggestion="Use 'except ExceptionType:' instead of 'except:'",
537
+ code_snippet=self._get_line(code, node.lineno)
538
+ ))
539
+
540
+ # Check for too many nested blocks
541
+ if isinstance(node, (ast.FunctionDef, ast.ClassDef)):
542
+ complexity = self._calculate_function_complexity(node)
543
+ if complexity > 10:
544
+ issues.append(CodeIssue(
545
+ line=node.lineno,
546
+ column=node.col_offset,
547
+ severity=CodeIssueSeverity.MEDIUM,
548
+ message=f"High function complexity ({complexity})",
549
+ suggestion="Consider breaking function into smaller functions",
550
+ code_snippet=node.name
551
+ ))
552
+
553
+ except SyntaxError as e:
554
+ issues.append(CodeIssue(
555
+ line=e.lineno or 1,
556
+ column=e.offset or 1,
557
+ severity=CodeIssueSeverity.CRITICAL,
558
+ message=f"Syntax error: {e.msg}",
559
+ suggestion="Fix syntax error before further analysis",
560
+ code_snippet=self._get_line(code, e.lineno or 1)
561
+ ))
562
+
563
+ return issues
564
+
565
+ def _analyze_javascript_code(self, code: str) -> List[CodeIssue]:
566
+ """Analyze JavaScript code"""
567
+ issues = []
568
+
569
+ # Simple regex-based checks for JS
570
+ patterns = [
571
+ (r"console\.log\(", "Remove console.log() in production code", CodeIssueSeverity.LOW),
572
+ (r"alert\(", "Avoid alert() - use better user feedback", CodeIssueSeverity.MEDIUM),
573
+ (r"document\.write", "Avoid document.write() - bad practice", CodeIssueSeverity.HIGH),
574
+ ]
575
+
576
+ lines = code.split('\n')
577
+ for i, line in enumerate(lines, 1):
578
+ for pattern, message, severity in patterns:
579
+ if re.search(pattern, line):
580
+ issues.append(CodeIssue(
581
+ line=i,
582
+ column=0,
583
+ severity=severity,
584
+ message=message,
585
+ suggestion="Remove or replace with proper implementation",
586
+ code_snippet=line.strip()
587
+ ))
588
+
589
+ return issues
590
+
591
+ def _pattern_match_code(self, code: str, language: str) -> List[CodeIssue]:
592
+ """Pattern matching for code issues"""
593
+ issues = []
594
+ lines = code.split('\n')
595
+
596
+ # Check security patterns
597
+ if language in self._code_patterns["security"]:
598
+ for pattern, message in self._code_patterns["security"][language]:
599
+ for i, line in enumerate(lines, 1):
600
+ if re.search(pattern, line, re.IGNORECASE):
601
+ issues.append(CodeIssue(
602
+ line=i,
603
+ column=0,
604
+ severity=CodeIssueSeverity.HIGH,
605
+ message=f"Security concern: {message}",
606
+ suggestion="Use safer alternative",
607
+ code_snippet=line.strip()
608
+ ))
609
+
610
+ # Check performance patterns
611
+ if language in self._code_patterns["performance"]:
612
+ for pattern, message in self._code_patterns["performance"][language]:
613
+ for i, line in enumerate(lines, 1):
614
+ if re.search(pattern, line, re.IGNORECASE):
615
+ issues.append(CodeIssue(
616
+ line=i,
617
+ column=0,
618
+ severity=CodeIssueSeverity.MEDIUM,
619
+ message=f"Performance: {message}",
620
+ suggestion="Optimize for better performance",
621
+ code_snippet=line.strip()
622
+ ))
623
+
624
+ return issues
625
+
626
+ def _calculate_complexity(self, code: str, language: CodeLanguage) -> float:
627
+ """Calculate code complexity score (0-100, lower is better)"""
628
+ if language == CodeLanguage.PYTHON:
629
+ # Simple complexity estimation for Python
630
+ lines = code.split('\n')
631
+ if not lines:
632
+ return 0.0
633
+
634
+ complexity_indicators = 0
635
+ for line in lines:
636
+ line_lower = line.lower().strip()
637
+ if any(keyword in line_lower for keyword in ['for ', 'while ', 'if ', 'def ', 'class ', 'try:', 'except:']):
638
+ complexity_indicators += 1
639
+
640
+ complexity = (complexity_indicators / len(lines)) * 100
641
+ return min(100.0, complexity)
642
+
643
+ return 50.0 # Default
644
+
645
+ def _calculate_security_score(self, code: str, language: CodeLanguage, issues: List[CodeIssue]) -> float:
646
+ """Calculate security score (0-100, higher is better)"""
647
+ base_score = 80.0
648
+
649
+ # Deduct for security issues
650
+ security_issues = [i for i in issues if i.severity in [CodeIssueSeverity.CRITICAL, CodeIssueSeverity.HIGH]]
651
+
652
+ deduction = len(security_issues) * 10
653
+ score = max(0.0, base_score - deduction)
654
+
655
+ return score
656
+
657
+ def _calculate_readability_score(self, code: str, language: CodeLanguage) -> float:
658
+ """Calculate readability score (0-100, higher is better)"""
659
+ lines = code.split('\n')
660
+ if not lines:
661
+ return 100.0
662
+
663
+ # Simple readability heuristics
664
+ good_practices = 0
665
+ total_lines = len(lines)
666
+
667
+ for line in lines:
668
+ line_stripped = line.strip()
669
+
670
+ # Check for good practices
671
+ if line_stripped and not line_stripped.startswith('#'):
672
+ # Reasonable line length
673
+ if len(line) <= 100:
674
+ good_practices += 1
675
+
676
+ # Avoid too many spaces
677
+ if not line.startswith(' ' * 4): # More than 3 indentation levels
678
+ good_practices += 1
679
+
680
+ readability = (good_practices / (total_lines * 2)) * 100
681
+ return min(100.0, readability)
682
+
683
+ def _estimate_bugs(self, issues: List[CodeIssue]) -> int:
684
+ """Estimate number of potential bugs"""
685
+ bug_count = 0
686
+
687
+ for issue in issues:
688
+ if issue.severity in [CodeIssueSeverity.CRITICAL, CodeIssueSeverity.HIGH]:
689
+ bug_count += 2
690
+ elif issue.severity == CodeIssueSeverity.MEDIUM:
691
+ bug_count += 1
692
+
693
+ return bug_count
694
+
695
+ def _generate_suggestions(self, issues: List[CodeIssue], code: str, language: CodeLanguage) -> List[str]:
696
+ """Generate improvement suggestions"""
697
+ suggestions = []
698
+
699
+ if not issues:
700
+ suggestions.append("Code looks good! No major issues found.")
701
+ return suggestions
702
+
703
+ # Group suggestions by category
704
+ security_issues = [i for i in issues if i.severity in [CodeIssueSeverity.CRITICAL, CodeIssueSeverity.HIGH]]
705
+ performance_issues = [i for i in issues if "performance" in i.message.lower()]
706
+ style_issues = [i for i in issues if i.severity == CodeIssueSeverity.LOW]
707
+
708
+ if security_issues:
709
+ suggestions.append(f"Fix {len(security_issues)} security issues for better safety")
710
+
711
+ if performance_issues:
712
+ suggestions.append(f"Address {len(performance_issues)} performance concerns")
713
+
714
+ if style_issues:
715
+ suggestions.append(f"Consider {len(style_issues)} style improvements")
716
+
717
+ # Language-specific suggestions
718
+ if language == CodeLanguage.PYTHON:
719
+ suggestions.append("Use type hints for better code clarity")
720
+ suggestions.append("Add docstrings to functions and classes")
721
+
722
+ elif language == CodeLanguage.JAVASCRIPT:
723
+ suggestions.append("Use const/let instead of var")
724
+ suggestions.append("Add error handling for async operations")
725
+
726
+ return suggestions
727
+
728
+ def _calculate_function_complexity(self, node: ast.AST) -> int:
729
+ """Calculate complexity of a function/class from AST"""
730
+ complexity = 0
731
+
732
+ for child in ast.walk(node):
733
+ if isinstance(child, (ast.If, ast.While, ast.For, ast.Try, ast.ExceptHandler)):
734
+ complexity += 1
735
+ elif isinstance(child, ast.BoolOp):
736
+ complexity += len(child.values) - 1
737
+
738
+ return complexity
739
+
740
+ def _get_line(self, code: str, line_number: int) -> str:
741
+ """Get specific line from code"""
742
+ lines = code.split('\n')
743
+ if 1 <= line_number <= len(lines):
744
+ return lines[line_number - 1]
745
+ return ""
746
+
747
+ def generate_code(self,
748
+ template_type: str,
749
+ language: CodeLanguage = CodeLanguage.PYTHON,
750
+ variables: Dict[str, Any] = None) -> str:
751
+ """
752
+ Generate code from template
753
+
754
+ Args:
755
+ template_type: Type of template to use
756
+ language: Programming language
757
+ variables: Variables to substitute in template
758
+
759
+ Returns:
760
+ Generated code
761
+ """
762
+ variables = variables or {}
763
+
764
+ try:
765
+ if language.value in self._templates and template_type in self._templates[language.value]:
766
+ template = self._templates[language.value][template_type]
767
+
768
+ # Simple variable substitution
769
+ for key, value in variables.items():
770
+ placeholder = "{{" + key + "}}"
771
+ template = template.replace(placeholder, str(value))
772
+
773
+ return template
774
+ else:
775
+ return f"# Template '{template_type}' not found for {language.value}"
776
+
777
+ except Exception as e:
778
+ return f"# Error generating code: {str(e)}"
779
+
780
+ def optimize_code(self, code: str, language: CodeLanguage = CodeLanguage.PYTHON) -> str:
781
+ """
782
+ Optimize code for better performance/readability
783
+
784
+ Args:
785
+ code: Source code to optimize
786
+ language: Programming language
787
+
788
+ Returns:
789
+ Optimized code
790
+ """
791
+ if language == CodeLanguage.PYTHON:
792
+ try:
793
+ # Format with autopep8
794
+ optimized = autopep8.fix_code(code)
795
+ return optimized
796
+ except:
797
+ # Fallback to simple formatting
798
+ return code
799
+
800
+ return code
801
+
802
+ def explain_code(self, code: str, language: CodeLanguage = CodeLanguage.PYTHON,
803
+ language_output: str = "en") -> str:
804
+ """
805
+ Generate explanation of code in simple language
806
+
807
+ Args:
808
+ code: Code to explain
809
+ language: Programming language of code
810
+ language_output: Output language (en/hi)
811
+
812
+ Returns:
813
+ Code explanation
814
+ """
815
+ explanations = {
816
+ "python": {
817
+ "en": {
818
+ "import": "Imports modules/libraries for use in code",
819
+ "def": "Defines a function with given name and parameters",
820
+ "class": "Defines a class/blueprint for creating objects",
821
+ "if": "Conditional statement - executes code if condition is true",
822
+ "for": "Loop that iterates over items in a sequence",
823
+ "while": "Loop that continues while condition is true",
824
+ "return": "Returns value from function",
825
+ "try": "Begins exception handling block",
826
+ "except": "Catches and handles exceptions",
827
+ },
828
+ "hi": {
829
+ "import": "कोड में उपयोग के लिए मॉड्यूल/लाइब्रेरी आयात करता है",
830
+ "def": "दिए गए नाम और पैरामीटर्स के साथ एक फ़ंक्शन को परिभाषित करता है",
831
+ "class": "ऑब्जेक्ट बनाने के लिए एक क्लास/ब्लूप्रिंट को परिभाषित करता है",
832
+ "if": "सशर्त स्टेटमेंट - अगर कंडीशन सही है तो कोड एक्जीक्यूट करता है",
833
+ "for": "लूप जो एक सीक्वेंस में आइटम्स पर इटरेट करता है",
834
+ "while": "लूप जो कंडीशन सही रहने तक जारी रहता है",
835
+ "return": "फ़ंक्शन से वैल्यू रिटर्न करता है",
836
+ "try": "एक्सेप्शन हैंडलिंग ब्लॉक शुरू करता है",
837
+ "except": "एक्सेप्शन को कैच और हैंडल करता है",
838
+ }
839
+ }
840
+ }
841
+
842
+ # Simple explanation based on keywords
843
+ lines = code.split('\n')
844
+ explanation_lines = []
845
+
846
+ lang_key = language.value
847
+ output_lang = language_output if language_output in ["en", "hi"] else "en"
848
+
849
+ explanation_dict = explanations.get(lang_key, {}).get(output_lang, {})
850
+
851
+ for i, line in enumerate(lines, 1):
852
+ line_stripped = line.strip()
853
+
854
+ if line_stripped:
855
+ # Find keywords in line
856
+ for keyword, meaning in explanation_dict.items():
857
+ if keyword in line_stripped.split():
858
+ explanation_lines.append(f"Line {i}: {meaning}")
859
+ break
860
+
861
+ if not explanation_lines:
862
+ if output_lang == "en":
863
+ return "Code explanation not available for this snippet."
864
+ else:
865
+ return "इस कोड स्निपेट के लिए स्पष्टीकरण उपलब्ध नहीं है।"
866
+
867
+ if output_lang == "en":
868
+ header = "Code Explanation:\n"
869
+ else:
870
+ header = "कोड स्पष्टीकरण:\n"
871
+
872
+ return header + "\n".join(explanation_lines)
873
+
874
+ def debug_code(self, code: str, error_message: str,
875
+ language: CodeLanguage = CodeLanguage.PYTHON) -> str:
876
+ """
877
+ Suggest fixes for code errors
878
+
879
+ Args:
880
+ code: Code with error
881
+ error_message: Error message from interpreter
882
+ language: Programming language
883
+
884
+ Returns:
885
+ Debugging suggestions
886
+ """
887
+ suggestions = []
888
+
889
+ # Common error patterns
890
+ error_patterns = {
891
+ "python": [
892
+ (r"SyntaxError", "Check for missing colons, parentheses, or quotes"),
893
+ (r"IndentationError", "Check indentation consistency (use 4 spaces)"),
894
+ (r"NameError.*not defined", "Variable/function not defined - check spelling"),
895
+ (r"TypeError", "Check data types and operations compatibility"),
896
+ (r"IndexError", "List/array index out of range"),
897
+ (r"KeyError", "Dictionary key not found"),
898
+ (r"AttributeError", "Object doesn't have the attribute/method"),
899
+ (r"ImportError", "Module not installed or incorrect import path"),
900
+ (r"ValueError", "Function received argument of right type but inappropriate value"),
901
+ ],
902
+ "javascript": [
903
+ (r"ReferenceError", "Variable not defined - check scope and spelling"),
904
+ (r"TypeError", "Value is not of expected type"),
905
+ (r"SyntaxError", "Check syntax - missing brackets, semicolons, etc."),
906
+ (r"RangeError", "Numeric value out of range"),
907
+ ]
908
+ }
909
+
910
+ lang_key = language.value
911
+ if lang_key in error_patterns:
912
+ for pattern, suggestion in error_patterns[lang_key]:
913
+ if re.search(pattern, error_message, re.IGNORECASE):
914
+ suggestions.append(suggestion)
915
+
916
+ if not suggestions:
917
+ suggestions.append("Try checking syntax and variable names")
918
+ suggestions.append("Ensure all required modules/libraries are imported")
919
+ suggestions.append("Check data types and operations compatibility")
920
+
921
+ return "Debug suggestions:\n- " + "\n- ".join(suggestions)
922
+
923
+ # Global instance
924
+ code_intel = AumCoreCodeIntelligence()
925
+
926
+ # Helper functions for easy import
927
+ def analyze_code(code: str, language: str = "python") -> Dict:
928
+ """Analyze code and return results as dictionary"""
929
+ lang_enum = CodeLanguage(language.lower())
930
+ result = code_intel.analyze_code(code, lang_enum)
931
+
932
+ return {
933
+ "language": result.language.value,
934
+ "issues": [
935
+ {
936
+ "line": issue.line,
937
+ "column": issue.column,
938
+ "severity": issue.severity.value,
939
+ "message": issue.message,
940
+ "suggestion": issue.suggestion,
941
+ "code_snippet": issue.code_snippet
942
+ }
943
+ for issue in result.issues
944
+ ],
945
+ "suggestions": result.suggestions,
946
+ "complexity_score": result.complexity_score,
947
+ "security_score": result.security_score,
948
+ "readability_score": result.readability_score,
949
+ "estimated_bugs": result.estimated_bugs
950
+ }
951
+
952
+ def generate_code_template(template_type: str, language: str = "python",
953
+ variables: Dict = None) -> str:
954
+ """Generate code from template"""
955
+ lang_enum = CodeLanguage(language.lower())
956
+ return code_intel.generate_code(template_type, lang_enum, variables)
957
+
958
+ def explain_code_simple(code: str, language: str = "python",
959
+ output_language: str = "en") -> str:
960
+ """Explain code in simple terms"""
961
+ lang_enum = CodeLanguage(language.lower())
962
+ return code_intel.explain_code(code, lang_enum, output_language)
963
+
964
+ # Module exports
965
+ __all__ = [
966
+ 'AumCoreCodeIntelligence',
967
+ 'CodeLanguage',
968
+ 'CodeIssueSeverity',
969
+ 'code_intel',
970
+ 'analyze_code',
971
+ 'generate_code_template',
972
+ 'explain_code_simple'
973
+ ]
974
+
975
+ __version__ = "1.0.0"
976
+ __author__ = "AumCore AI"