-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
981 lines (816 loc) · 36.7 KB
/
agent.py
File metadata and controls
981 lines (816 loc) · 36.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
#!/usr/bin/env python3
"""
LocalAgent SDK - Open Source Terminal AI Agent
Enhanced with Smart Build System & Model Management
"""
import sys
import json
import time
from pathlib import Path
from typing import Optional, List, Dict, Callable
import subprocess
import re
import os
# Fix encoding for Windows
if sys.platform == 'win32':
sys.stdout.reconfigure(encoding='utf-8')
sys.stderr.reconfigure(encoding='utf-8')
try:
from rich.console import Console
from rich.panel import Panel
from rich.prompt import Prompt, Confirm
from rich.table import Table
from rich import box
from rich.text import Text
from rich.syntax import Syntax
from rich.tree import Tree
from rich.progress import Progress, SpinnerColumn, TextColumn, BarColumn
from rich.markdown import Markdown
from rich.live import Live
except ImportError:
print("Error: 'rich' library not found!")
print("\nInstall: pip install rich")
sys.exit(1)
# Import banner if available, otherwise use default
try:
from ascii_art import BANNER, SUBTITLE
except ImportError:
BANNER = "LocalAgent"
SUBTITLE = "AI-Powered Code Generation"
# Default models if models.py is not available
DEFAULT_MODELS = [
{
"name": "qwen2.5-coder:3b",
"size": "3B",
"description": "Fast, good for simple projects"
},
{
"name": "qwen2.5-coder:7b",
"size": "7B",
"description": "Balanced speed and quality"
},
{
"name": "deepseek-coder-v2:16b",
"size": "16B",
"description": "Best quality for complex projects"
},
{
"name": "codellama:7b",
"size": "7B",
"description": "Meta's CodeLlama model"
}
]
# Try to import FREE_MODELS, use default if not available
try:
from models import FREE_MODELS
except ImportError:
FREE_MODELS = DEFAULT_MODELS
console = Console()
WORKSPACE_DIR = Path.cwd()
class FileOperation:
"""File or folder operation"""
def __init__(self, path: Path, is_dir: bool = False, content: str = ""):
self.path = path
self.is_dir = is_dir
self.content = content
self.exists = path.exists()
class SafeFileSystem:
"""Safe file operations"""
def __init__(self, workspace: Path):
self.workspace = workspace.resolve()
self.pending_operations: List[FileOperation] = []
def is_safe_path(self, path: Path) -> bool:
"""Check if path is safe"""
try:
resolved = path.resolve()
return str(resolved).startswith(str(self.workspace))
except:
return False
def sanitize_path(self, path_str: str) -> Optional[Path]:
"""Convert to safe path"""
# Clean up path
path_str = path_str.strip()
path_str = path_str.lstrip('/').lstrip('\\')
# Remove dangerous patterns
while '..' in path_str:
path_str = path_str.replace('..', '')
path_str = path_str.lstrip('/')
# Build full path
if not path_str:
return None
full_path = self.workspace / path_str
return full_path if self.is_safe_path(full_path) else None
def plan_file(self, path_str: str, content: str = ""):
"""Plan file creation"""
safe_path = self.sanitize_path(path_str)
if safe_path:
op = FileOperation(safe_path, is_dir=False, content=content)
self.pending_operations.append(op)
return True
return False
def plan_folder(self, path_str: str):
"""Plan folder creation"""
safe_path = self.sanitize_path(path_str)
if safe_path:
op = FileOperation(safe_path, is_dir=True)
self.pending_operations.append(op)
return True
return False
def get_preview_tree(self) -> Tree:
"""Generate preview tree with animation"""
tree = Tree("[bold cyan]Project Structure[/bold cyan]", guide_style="cyan")
folders = sorted([op for op in self.pending_operations if op.is_dir], key=lambda x: str(x.path))
files = sorted([op for op in self.pending_operations if not op.is_dir], key=lambda x: str(x.path))
path_nodes = {}
for op in folders:
rel_path = op.path.relative_to(self.workspace)
parts = rel_path.parts
current = tree
for i, part in enumerate(parts):
key = '/'.join(parts[:i+1])
if key not in path_nodes:
status = "[yellow](exists)[/yellow]" if op.exists else "[green](new)[/green]"
node = current.add(f"[DIR] [cyan]{part}[/cyan] {status}")
path_nodes[key] = node
current = path_nodes[key]
for op in files:
rel_path = op.path.relative_to(self.workspace)
parts = rel_path.parts
if len(parts) > 1:
parent_key = '/'.join(parts[:-1])
current = path_nodes.get(parent_key, tree)
else:
current = tree
status = "[red](overwrite)[/red]" if op.exists else "[green](new)[/green]"
size = f" ({len(op.content)} bytes)" if op.content else ""
current.add(f"[FILE] [white]{parts[-1]}[/white]{size} {status}")
return tree
def execute_operations(self, console: Console) -> bool:
"""Execute operations with animated progress"""
if not self.pending_operations:
return True
overwrites = [op for op in self.pending_operations if op.exists and not op.is_dir]
if overwrites:
console.print("\n[bold yellow]WARNING: Files exist:[/bold yellow]")
for op in overwrites:
rel = op.path.relative_to(self.workspace)
console.print(f" - {rel}")
console.print()
if not Confirm.ask("[yellow]Overwrite existing files?[/yellow]", default=False):
console.print("[red]Operation cancelled[/red]\n")
return False
try:
# Animated file creation
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
BarColumn(),
TextColumn("[progress.percentage]{task.percentage:>3.0f}%"),
console=console
) as progress:
task = progress.add_task("[cyan]Creating files...", total=len(self.pending_operations))
for op in self.pending_operations:
if op.is_dir:
op.path.mkdir(parents=True, exist_ok=True)
progress.update(task, advance=1, description=f"[green]OK[/green] Created folder: [cyan]{op.path.name}[/cyan]")
else:
op.path.parent.mkdir(parents=True, exist_ok=True)
# Fix encoding issue - use utf-8 with error handling
try:
with open(op.path, 'w', encoding='utf-8', errors='replace') as f:
f.write(op.content)
except Exception as e:
console.print(f"[red]Error writing {op.path.name}: {e}[/red]")
continue
progress.update(task, advance=1, description=f"[green]OK[/green] Created file: [cyan]{op.path.name}[/cyan]")
time.sleep(0.05)
console.print(f"\n[bold green]SUCCESS: Created {len(self.pending_operations)} items[/bold green]\n")
return True
except Exception as e:
console.print(f"\n[bold red]ERROR: {str(e)}[/bold red]\n")
return False
finally:
self.pending_operations.clear()
def clear_pending(self):
"""Clear pending operations"""
self.pending_operations.clear()
class CodeAnalyzer:
"""Analyze codebase like Claude Code"""
def __init__(self, workspace: Path):
self.workspace = workspace
self.console = console
def analyze_directory(self, target_path: Optional[Path] = None) -> Dict:
"""Analyze directory structure and files"""
if target_path is None:
target_path = self.workspace
analysis = {
"files": [],
"directories": [],
"languages": {},
"total_size": 0,
"file_count": 0
}
ignore_dirs = {'.git', '__pycache__', 'node_modules', '.venv', 'venv', 'dist', 'build'}
try:
for item in target_path.rglob('*'):
# Skip ignored directories
if any(ignored in item.parts for ignored in ignore_dirs):
continue
if item.is_file():
try:
size = item.stat().st_size
analysis["files"].append(str(item.relative_to(self.workspace)))
analysis["total_size"] += size
analysis["file_count"] += 1
# Count by extension
ext = item.suffix.lower()
if ext:
analysis["languages"][ext] = analysis["languages"].get(ext, 0) + 1
except:
pass
elif item.is_dir():
analysis["directories"].append(str(item.relative_to(self.workspace)))
except Exception as e:
self.console.print(f"[red]Analysis error: {e}[/red]")
return analysis
def display_analysis(self, analysis: Dict):
"""Display analysis results"""
panel_content = f"""[cyan]Files:[/cyan] {analysis['file_count']}
[cyan]Size:[/cyan] {analysis['total_size'] / 1024:.1f} KB
[cyan]Directories:[/cyan] {len(analysis['directories'])}"""
self.console.print(Panel(panel_content, title="[bold]Codebase Summary[/bold]", border_style="cyan"))
if analysis["languages"]:
self.console.print("\n[bold]Languages:[/bold]")
table = Table(box=box.SIMPLE)
table.add_column("Extension", style="cyan")
table.add_column("Files", justify="right", style="green")
for ext, count in sorted(analysis["languages"].items(), key=lambda x: -x[1]):
table.add_row(ext, str(count))
self.console.print(table)
self.console.print()
def search_code(self, pattern: str) -> List[Dict]:
"""Search for pattern in code"""
results = []
ignore_dirs = {'.git', '__pycache__', 'node_modules', '.venv', 'venv'}
try:
for file_path in self.workspace.rglob('*'):
if any(ignored in file_path.parts for ignored in ignore_dirs):
continue
if file_path.is_file():
try:
content = file_path.read_text(encoding='utf-8', errors='ignore')
for i, line in enumerate(content.split('\n'), 1):
if pattern.lower() in line.lower():
results.append({
"file": str(file_path.relative_to(self.workspace)),
"line": i,
"content": line.strip()
})
except:
pass
except Exception as e:
self.console.print(f"[red]Search error: {e}[/red]")
return results
class LocalAgent:
"""Main agent class"""
def __init__(self):
self.console = console
self.current_model = None
self.conversation_history = []
self.filesystem = SafeFileSystem(WORKSPACE_DIR)
self.analyzer = CodeAnalyzer(WORKSPACE_DIR)
self.mode = "build" # Default to build mode
def display_animated_intro(self):
"""Show animated intro"""
self.console.clear()
for line in BANNER.splitlines():
self.console.print(f"[bold cyan]{line}[/bold cyan]")
time.sleep(0.05) # animation speed
time.sleep(0.2)
self.console.print(f"\n[dim]{SUBTITLE}[/dim]\n")
# ⚠️ Global disclaimer (TOP of intro)
intro_note = (
"[bold yellow]IMPORTANT NOTE:[/bold yellow]\n"
"• Build mode may be slow depending on your system.\n"
"• Local Ollama models can make mistakes or be outdated.\n"
"• Sometimes random or incorrect files may be created.\n"
"• Always cross-verify generated code and files.\n"
"• Use the latest model for better accuracy.\n\n"
"[green]Chat mode works fine for general queries.[/green]"
)
self.console.print(
Panel(
intro_note,
title="[bold yellow]Before You Start[/bold yellow]",
border_style="yellow"
)
)
time.sleep(0.5)
def check_prerequisites(self) -> bool:
"""Check if Ollama is installed and running"""
try:
result = subprocess.run(['ollama', 'list'],
capture_output=True,
text=True,
timeout=5,
encoding='utf-8',
errors='replace')
return result.returncode == 0
except (subprocess.TimeoutExpired, FileNotFoundError):
self.console.print(Panel(
"[red]ERROR: Ollama not found[/red]\n\n"
"Install: [cyan]https://ollama.ai[/cyan]\n"
"Then run: [yellow]ollama serve[/yellow]",
title="[bold red]Error[/bold red]",
border_style="red"
))
return False
def get_installed_models(self) -> List[str]:
"""Get list of installed Ollama models"""
try:
result = subprocess.run(['ollama', 'list'],
capture_output=True,
text=True,
timeout=5,
encoding='utf-8',
errors='replace')
if result.returncode == 0:
lines = result.stdout.strip().split('\n')[1:] # Skip header
return [line.split()[0] for line in lines if line.strip()]
except:
pass
return []
def display_model_selection_menu(self):
"""Display available models"""
self.console.print("\n[bold cyan]AVAILABLE MODELS[/bold cyan]\n")
table = Table(box=box.ROUNDED, border_style="cyan")
table.add_column("#", style="cyan", width=5)
table.add_column("Model", style="green")
table.add_column("Size", style="yellow")
table.add_column("Description", style="white")
for idx, model in enumerate(FREE_MODELS, 1):
# Safely get model properties with defaults
model_name = model.get("name", f"model-{idx}")
model_size = model.get("size", "Unknown")
model_desc = model.get("description", "No description available")
table.add_row(
str(idx),
model_name,
model_size,
model_desc
)
self.console.print(table)
self.console.print()
def select_model(self) -> Optional[str]:
"""Model selection with error handling"""
try:
choice = Prompt.ask(
"[cyan]Select model[/cyan]",
choices=[str(i) for i in range(1, len(FREE_MODELS) + 1)] + ['cancel']
)
if choice == 'cancel':
return None
model = FREE_MODELS[int(choice) - 1]
model_name = model.get("name")
if not model_name:
self.console.print("[red]ERROR: Invalid model configuration[/red]")
return None
# Check if already installed
installed = self.get_installed_models()
if model_name not in installed:
self.console.print(f"\n[yellow]Installing {model_name}...[/yellow]\n")
try:
subprocess.run(['ollama', 'pull', model_name], check=True)
self.console.print(f"[green]SUCCESS: Installed {model_name}[/green]\n")
except subprocess.CalledProcessError:
self.console.print(f"[red]ERROR: Failed to install {model_name}[/red]\n")
return None
return model_name
except (ValueError, IndexError, KeyError) as e:
self.console.print(f"[red]ERROR: Invalid selection - {e}[/red]")
return None
def show_thinking_animation(self):
"""Show thinking animation"""
return Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
transient=True,
console=self.console
)
def chat_with_model(self, model: str, prompt: str) -> Optional[str]:
"""Simple chat with model"""
try:
result = subprocess.run(
['ollama', 'run', model, prompt],
capture_output=True,
text=True,
timeout=120,
encoding='utf-8',
errors='replace'
)
if result.returncode == 0:
return result.stdout.strip()
else:
self.console.print(f"[red]Model error: {result.stderr}[/red]")
return None
except subprocess.TimeoutExpired:
self.console.print("[red]Request timed out[/red]")
return None
except Exception as e:
self.console.print(f"[red]Error: {str(e)}[/red]")
return None
def chat_with_model_for_build(self, model: str, user_request: str) -> Optional[str]:
"""Enhanced build-focused prompt that creates proper folder structure"""
# Create a smart, adaptive prompt
build_prompt = f"""You are a code generation assistant. The user wants to build: {user_request}
CRITICAL INSTRUCTIONS:
1. Analyze what the user wants to build (web app, Python script, API, CLI tool, etc.)
2. Generate COMPLETE, WORKING code for their request
3. Create a PROPER folder/directory structure based on the project type
4. Include ALL necessary files (config, dependencies, documentation)
MANDATORY FORMAT - Follow this EXACTLY:
PROJECT: [brief project name]
FILES:
---
PATH: folder_name/file.ext
CONTENT:
[complete file content here]
---
PATH: another_folder/subfolder/file.ext
CONTENT:
[complete file content here]
---
FOLDER STRUCTURE EXAMPLES:
Flask Web App:
---
PATH: app.py
CONTENT:
[Flask application code]
---
PATH: requirements.txt
CONTENT:
[dependencies]
---
PATH: templates/index.html
CONTENT:
[HTML template]
---
PATH: static/css/style.css
CONTENT:
[CSS styles]
---
React App:
---
PATH: package.json
CONTENT:
[package config]
---
PATH: public/index.html
CONTENT:
[HTML]
---
PATH: src/App.js
CONTENT:
[React component]
---
PATH: src/index.js
CONTENT:
[entry point]
---
Python CLI Tool:
---
PATH: main.py
CONTENT:
[main script]
---
PATH: requirements.txt
CONTENT:
[dependencies]
---
PATH: README.md
CONTENT:
[documentation]
---
IMPORTANT:
- Use folder/file.ext format for nested files
- This will automatically create the folder structure
- Include configuration files (package.json, requirements.txt, etc.)
- Add README.md with setup instructions
- Make code production-ready with error handling
Now generate the COMPLETE project with PROPER folder structure for: {user_request}"""
try:
result = subprocess.run(
['ollama', 'run', model, build_prompt],
capture_output=True,
text=True,
timeout=180,
encoding='utf-8',
errors='replace' # Handle encoding errors
)
if result.returncode == 0:
return result.stdout.strip()
else:
self.console.print(f"[red]Model error: {result.stderr}[/red]")
return None
except subprocess.TimeoutExpired:
self.console.print("[red]Request timed out (code generation can take a while)[/red]")
return None
except Exception as e:
self.console.print(f"[red]Error: {str(e)}[/red]")
return None
def parse_file_operations(self, response: str) -> bool:
"""Parse response and extract file operations"""
self.filesystem.clear_pending()
# Look for FILES: section
if "FILES:" not in response:
return False
# Extract files section
files_section = response.split("FILES:")[1] if "FILES:" in response else response
# Parse each file block
file_blocks = re.split(r'\n---\n', files_section)
files_created = 0
for block in file_blocks:
if not block.strip():
continue
# Extract PATH and CONTENT
path_match = re.search(r'PATH:\s*(.+?)(?:\n|$)', block)
content_match = re.search(r'CONTENT:\s*\n(.*)', block, re.DOTALL)
if path_match and content_match:
file_path = path_match.group(1).strip()
file_content = content_match.group(1).strip()
# Clean up the content (remove markdown code blocks if present)
file_content = re.sub(r'^```[\w]*\n', '', file_content)
file_content = re.sub(r'\n```$', '', file_content)
# Create parent directories if needed
if '/' in file_path:
parent_dir = '/'.join(file_path.split('/')[:-1])
self.filesystem.plan_folder(parent_dir)
# Add file
if self.filesystem.plan_file(file_path, file_content):
files_created += 1
return files_created > 0
def show_mode_menu(self):
"""Show current mode with better visibility"""
mode_displays = {
"build": "[bold black on magenta] BUILD MODE [/bold black on magenta]",
"chat": "[bold black on cyan] CHAT MODE [/bold black on cyan]",
"analyze": "[bold black on yellow] ANALYZE MODE [/bold black on yellow]"
}
display = mode_displays.get(self.mode, f"[bold white] {self.mode.upper()} [/bold white]")
self.console.print(f"\n{display}")
self.console.print("[dim]Commands: /help | /chat | /build | /analyze | /model | /exit[/dim]\n")
def show_help(self, mode: str = None):
"""Show comprehensive help"""
self.console.print("\n[bold cyan]" + "="*60 + "[/bold cyan]")
self.console.print("[bold cyan] LocalAgent - Help Guide [/bold cyan]")
self.console.print("[bold cyan]" + "="*60 + "[/bold cyan]\n")
# Mode Switching
self.console.print("[bold yellow]MODE SWITCHING:[/bold yellow]")
self.console.print(" /chat - Switch to chat mode (general Q&A)")
self.console.print(" /build - Switch to build mode (create projects)")
self.console.print(" /analyze - Switch to analyze mode (inspect code)")
self.console.print()
# Model Management
self.console.print("[bold green]MODEL MANAGEMENT:[/bold green]")
self.console.print(" /model - Change AI model")
self.console.print(" /models - List available models")
self.console.print()
# Build Mode
if mode == "build" or mode is None:
self.console.print("[bold magenta]BUILD MODE:[/bold magenta]")
self.console.print(" Just describe what you want to build!")
self.console.print()
self.console.print(" [cyan]Examples:[/cyan]")
self.console.print(" - Create a Flask REST API for user management")
self.console.print(" - Build a React todo app with local storage")
self.console.print(" - Make a Python CLI tool for file encryption")
self.console.print(" - Create an Express.js API with MongoDB")
self.console.print(" - Build an HTML/CSS/JS portfolio website")
self.console.print(" - Create a FastAPI app with authentication")
self.console.print()
# Chat Mode
if mode == "chat" or mode is None:
self.console.print("[bold cyan]CHAT MODE:[/bold cyan]")
self.console.print(" Ask anything - coding help, explanations, debugging, etc.")
self.console.print()
# Analyze Mode
if mode == "analyze" or mode is None:
self.console.print("[bold yellow]ANALYZE MODE:[/bold yellow]")
self.console.print(" analyze [path] - Analyze directory/codebase")
self.console.print(" search <pattern> - Search for code patterns")
self.console.print()
# General
self.console.print("[bold white]GENERAL:[/bold white]")
self.console.print(" /help - Show this help")
self.console.print(" /exit - Exit the agent")
self.console.print()
self.console.print("[bold cyan]" + "="*60 + "[/bold cyan]\n")
def handle_model_change(self):
"""Handle model switching"""
installed = self.get_installed_models()
self.console.print("\n[bold cyan]AVAILABLE MODELS[/bold cyan]\n")
# Show installed models
if installed:
self.console.print("[bold green]Installed:[/bold green]")
for i, model in enumerate(installed, 1):
current = " [yellow]<- current[/yellow]" if model == self.current_model else ""
self.console.print(f" {i}. {model}{current}")
self.console.print()
else:
self.console.print("[yellow]No models installed yet[/yellow]\n")
# Show available models
self.console.print("[bold yellow]Available to install:[/bold yellow]")
for i, model in enumerate(FREE_MODELS, 1):
model_name = model.get("name", "unknown")
model_desc = model.get("description", "")
if model_name not in installed:
self.console.print(f" {i}. {model_name} - {model_desc}")
self.console.print()
choice = Prompt.ask("[cyan]Select model number or 'cancel'[/cyan]")
if choice.lower() == 'cancel':
return
try:
idx = int(choice) - 1
if 0 <= idx < len(installed):
self.current_model = installed[idx]
self.console.print(f"\n[green]SUCCESS: Switched to {self.current_model}[/green]\n")
elif 0 <= idx < len(FREE_MODELS):
new_model = self.select_model()
if new_model:
self.current_model = new_model
except ValueError:
self.console.print("[red]Invalid choice[/red]")
def handle_analyze_mode(self):
"""Handle analyze mode interactions"""
while self.mode == "analyze":
try:
user_input = Prompt.ask(f"[bold black on yellow] ANALYZE [/bold black on yellow] >>>")
if user_input.lower() in ['exit', '/exit']:
return 'exit'
if user_input.lower() in ['chat', '/chat']:
self.mode = "chat"
return None
if user_input.lower() in ['build', '/build']:
self.mode = "build"
return None
if user_input.lower() in ['/help', 'help']:
self.show_help('analyze')
continue
if user_input.lower().startswith('analyze'):
parts = user_input.split(maxsplit=1)
target = Path(parts[1]) if len(parts) > 1 else None
self.console.print("\n[cyan]Analyzing codebase...[/cyan]\n")
analysis = self.analyzer.analyze_directory(target)
self.analyzer.display_analysis(analysis)
elif user_input.lower().startswith('search '):
pattern = user_input[7:].strip()
if pattern:
self.console.print(f"\n[cyan]Searching for: '{pattern}'[/cyan]\n")
results = self.analyzer.search_code(pattern)
if results:
table = Table(box=box.ROUNDED, border_style="yellow")
table.add_column("File", style="cyan")
table.add_column("Line", style="magenta", justify="right")
table.add_column("Content", style="white")
for result in results[:50]:
table.add_row(
result["file"],
str(result["line"]),
result["content"][:80]
)
self.console.print(table)
self.console.print(f"\n[yellow]Found {len(results)} matches{' (showing first 50)' if len(results) > 50 else ''}[/yellow]\n")
else:
self.console.print("[yellow]No matches found[/yellow]\n")
except KeyboardInterrupt:
self.console.print()
continue
def start(self):
"""Enhanced main loop with mode selection"""
self.display_animated_intro()
if not self.check_prerequisites():
return
# Check for installed models first
installed = self.get_installed_models()
if installed:
self.console.print("[bold cyan]>> Installed models:[/bold cyan]\n")
table = Table(box=box.ROUNDED, border_style="cyan")
table.add_column("#", style="cyan", width=5)
table.add_column("Model", style="green")
for idx, model in enumerate(installed, 1):
table.add_row(str(idx), model)
self.console.print(table)
self.console.print()
if Confirm.ask("[cyan]Use installed model?[/cyan]", default=True):
if len(installed) == 1:
self.current_model = installed[0]
else:
choice = Prompt.ask("[cyan]Select[/cyan]", choices=[str(i) for i in range(1, len(installed) + 1)])
self.current_model = installed[int(choice) - 1]
# If no installed model selected, show available models
if not self.current_model:
self.console.print("\n[yellow]No models installed. Please select a model to install:[/yellow]\n")
self.display_model_selection_menu()
self.current_model = self.select_model()
if not self.current_model:
self.console.print("\n[red]No model selected. Exiting...[/red]\n")
return
# Main interaction loop
self.console.print(f"\n[bold green]AGENT ACTIVE[/bold green]")
self.console.print(f"[dim]Model: {self.current_model}[/dim]\n")
self.show_mode_menu()
while True:
try:
# Get mode-specific prompt
if self.mode == "chat":
prompt_text = f"[bold black on cyan] CHAT [/bold black on cyan] >>>"
elif self.mode == "build":
prompt_text = f"[bold black on magenta] BUILD [/bold black on magenta] >>>"
else: # analyze
if self.handle_analyze_mode() == 'exit':
break
self.show_mode_menu()
continue
user_input = Prompt.ask(prompt_text)
# Handle /help
if user_input.lower() in ['/help', 'help']:
self.show_help(self.mode)
continue
# Handle exit
if user_input.lower() in ['exit', 'quit', '/exit', '/quit']:
self.console.print("\n[green]Goodbye![/green]\n")
break
# Handle model switching
if user_input.lower() in ['/model', '/models']:
self.handle_model_change()
continue
# Handle mode switching with /commands
if user_input.lower() in ['chat', '/chat']:
self.mode = "chat"
self.show_mode_menu()
continue
elif user_input.lower() in ['build', '/build']:
self.mode = "build"
self.show_mode_menu()
continue
elif user_input.lower() in ['analyze', '/analyze']:
self.mode = "analyze"
self.show_mode_menu()
continue
if not user_input.strip():
continue
# Process based on mode
if self.mode == "build":
with self.show_thinking_animation() as progress:
task = progress.add_task("[cyan]Planning your build...", total=None)
response = self.chat_with_model_for_build(self.current_model, user_input)
self.console.print()
if response:
# Try to parse and create files
if self.parse_file_operations(response):
# Show what will be created with animation
self.console.print("[bold magenta]FILES TO CREATE:[/bold magenta]\n")
tree = self.filesystem.get_preview_tree()
self.console.print(tree)
self.console.print()
if Confirm.ask("[magenta]Create these files?[/magenta]", default=True):
self.filesystem.execute_operations(self.console)
else:
self.console.print("[yellow]Cancelled file creation[/yellow]\n")
self.filesystem.clear_pending()
else:
# Just show response as conversation
self.console.print(Panel(
response,
title="[bold magenta]Response[/bold magenta]",
border_style="magenta"
))
self.console.print()
else: # chat mode
with self.show_thinking_animation() as progress:
task = progress.add_task("[cyan]Thinking...", total=None)
response = self.chat_with_model(self.current_model, user_input)
self.console.print()
if response:
self.console.print(Panel(
response,
title="[bold cyan]Response[/bold cyan]",
border_style="cyan"
))
self.console.print()
except KeyboardInterrupt:
self.console.print("\n[yellow]Use /exit to quit[/yellow]\n")
continue
except Exception as e:
self.console.print(f"\n[red]Error: {str(e)}[/red]\n")
if __name__ == "__main__":
try:
agent = LocalAgent()
agent.start()
except KeyboardInterrupt:
print("\n\nGoodbye!")
except Exception as e:
print(f"Fatal error: {e}")
import traceback
traceback.print_exc()
sys.exit(1)