Repository navigation
Expand file tree
/
Copy pathcli.py
More file actions
745 lines (623 loc) · 34 KB
/
Copy pathcli.py
File metadata and controls
745 lines (623 loc) · 34 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
#!/usr/bin/env python3
"""CLI entry point for the coding agent."""
import argparse
import os
import sys
import time
from pathlib import Path
from typing import Optional
from coding_agent.config import ModelConfig, Settings
from coding_agent.core import CodingAgent, PlanMode, VulnerabilityRemediator, EnhancedPlanner, HierarchicalPlan, PlanStatus
from coding_agent.tools import (
ListDirTool,
ReadFileTool,
RunCommandTool,
RunPythonTool,
SearchFilesTool,
WriteFileTool,
VulnerabilityScannerTool,
)
from coding_agent.utils import setup_logging, interactive_plan_selector
class Loader:
"""Simple loader animation for displaying progress during LLM requests."""
def __init__(self, message: str = "Loading"):
self.message = message
self._running = False
def _animate(self):
"""Display the loading animation."""
frames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]
i = 0
while self._running:
sys.stdout.write(f"\r{frames[i % len(frames)]} {self.message}")
sys.stdout.flush()
time.sleep(0.1)
i += 1
sys.stdout.write("\r" + " " * (len(self.message) + 2) + "\r")
sys.stdout.flush()
def start(self):
"""Start the loader animation."""
self._running = True
import threading
self._thread = threading.Thread(target=self._animate, daemon=True)
self._thread.start()
def stop(self):
"""Stop the loader animation."""
self._running = False
if hasattr(self, '_thread'):
self._thread.join(timeout=0.5)
def create_agent(workspace_dir: str, model_config: ModelConfig, prepare_context: bool = True) -> CodingAgent:
"""Create and configure a coding agent instance.
Args:
workspace_dir: Root directory for file operations.
model_config: LLM model configuration.
prepare_context: Whether to prepare session context before starting.
Returns:
Configured CodingAgent instance.
"""
settings = Settings(
workspace_dir=workspace_dir,
max_iterations=50,
log_level="INFO",
)
agent = CodingAgent(settings=settings, model_config=model_config, prepare_context=prepare_context)
# Register default tools
agent.register_tool(ReadFileTool(workspace_root=workspace_dir))
agent.register_tool(WriteFileTool(workspace_root=workspace_dir))
agent.register_tool(ListDirTool(workspace_root=workspace_dir))
agent.register_tool(SearchFilesTool(workspace_root=workspace_dir))
agent.register_tool(RunCommandTool(workspace_root=workspace_dir))
agent.register_tool(RunPythonTool(workspace_root=workspace_dir))
agent.register_tool(VulnerabilityScannerTool(workspace_root=workspace_dir))
return agent
def cmd_init(args: argparse.Namespace) -> int:
"""Initialize a directory for use with the coding agent.
Args:
args: Parsed command-line arguments.
Returns:
Exit code (0 for success, non-zero for failure).
"""
target_dir = Path(args.directory).resolve()
if not target_dir.exists():
print(f"Creating directory: {target_dir}")
target_dir.mkdir(parents=True, exist_ok=True)
# Create .agent_config.json if it doesn't exist
config_file = target_dir / ".agent_config.json"
if not config_file.exists():
import json
config_content = {
"workspace_dir": str(target_dir),
"model": {
"base_url": os.getenv("LLM_BASE_URL", "http://localhost:11434/v1"),
"api_key": os.getenv("LLM_API_KEY", "ollama"),
"model_name": os.getenv("LLM_MODEL", "qwen2.5-coder:7b")
}
}
with open(config_file, 'w') as f:
json.dump(config_content, f, indent=2)
print(f"Created configuration file: {config_file}")
else:
print(f"Configuration already exists: {config_file}")
# Create .gitignore for agent history
gitignore_file = target_dir / ".gitignore"
if not gitignore_file.exists():
with open(gitignore_file, 'w') as f:
f.write(".agent_history/\n")
f.write("__pycache__/\n")
f.write("*.pyc\n")
print(f"Created .gitignore: {gitignore_file}")
# Create virtualenv recommendation
venv_dir = target_dir / "venv"
if not venv_dir.exists():
print(f"\nTo create a virtual environment, run:")
print(f" cd {target_dir}")
print(f" python -m venv venv")
print(f" source venv/bin/activate # On Windows: venv\\Scripts\\activate")
print(f" pip install -e . # If you have setup.py or pyproject.toml")
print(f"\nDirectory initialized: {target_dir}")
print("You can now use 'agent chat' to start interacting with the coding agent.")
return 0
def cmd_chat(args: argparse.Namespace) -> int:
"""Start an interactive chat session with the coding agent.
Args:
args: Parsed command-line arguments.
Returns:
Exit code (0 for success, non-zero for failure).
"""
workspace_dir = Path(args.workspace).resolve()
if not workspace_dir.exists():
print(f"Error: Workspace directory does not exist: {workspace_dir}")
return 1
# Load model configuration
model_config = ModelConfig(
base_url=args.base_url,
api_key=args.api_key,
model_name=args.model,
)
setup_logging(level=args.log_level)
try:
with create_agent(str(workspace_dir), model_config, args.prepare_context) as agent:
plan_mode = PlanMode(model_config)
print("=" * 60)
print("Coding Agent CLI")
print("=" * 60)
print(f"Workspace: {workspace_dir}")
print(f"Model: {args.model}")
print(f"Base URL: {args.base_url}")
print("=" * 60)
print("\nCommands:")
print(" /help - Show available commands")
print(" /clear - Clear conversation history")
print(" /retry - Retry the last task")
print(" /tools - List available tools")
print(" /plan - Generate a plan for your request")
print(" /scan - Scan for vulnerabilities and create remediation plan")
print(" /quit - Exit the agent")
print("\nEnter your request:\n")
while True:
try:
user_input = input("> ").strip()
except (EOFError, KeyboardInterrupt):
print("\nGoodbye!")
break
if user_input.lower() in ("/quit", "/exit", "quit", "exit", "q"):
print("Goodbye!")
break
if user_input.startswith("/"):
command = user_input.split()[0].lower()
if command == "/help":
print("\nAvailable commands:")
print(" /help - Show this help message")
print(" /clear - Clear conversation history")
print(" /retry - Retry the last task")
print(" /tools - List available tools")
print(" /plan - Generate and review a plan")
print(" /scan - Scan for vulnerabilities")
print(" /quit - Exit the agent")
print(" <text> - Send a message to the agent\n")
elif command == "/clear":
agent.clear_context()
print("Conversation history cleared.\n")
elif command == "/retry":
# Retry the last task
last_task = agent.retry_last_task()
if last_task:
print(f"\nRetrying last task: {last_task}\n")
try:
loader = Loader("Waiting for LLM response...")
loader.start()
response = agent.run(last_task)
loader.stop()
print("\n" + response + "\n")
except Exception as e:
loader.stop()
print(f"\nError: {e}\n")
else:
print("\nNo previous task found to retry.\n")
elif command == "/tools":
print("\nAvailable tools:")
for tool_name in agent.tool_registry.list_tools():
print(f" - {tool_name}")
print()
elif command == "/plan":
# Plan mode workflow with enhanced execution tracking
plan_request = user_input[6:].strip()
if not plan_request:
print("Usage: /plan <your request>")
print("Example: /plan Create a Python script that lists all files in the current directory\n")
continue
print("\nGenerating plan...")
# Get system prompt and available tools
context = agent.get_context()
system_prompt = context.system_prompt or "You are a helpful coding assistant."
available_tools = agent.tool_registry.get_all_schemas()
# Generate plan using EnhancedPlanner for better tracking
planner = EnhancedPlanner(model_config)
loader = Loader("Generating plan...")
loader.start()
hierarchical_plan = planner.generate_hierarchical_plan(
plan_request,
system_prompt,
available_tools
)
loader.stop()
if hierarchical_plan:
# Display plan summary
print("\n" + planner.get_plan_summary(hierarchical_plan))
# Convert plan items to format expected by selector
plan_items = []
for item in hierarchical_plan.get_executable_items():
plan_items.append({
'description': item.description,
'tool_name': item.tool_name,
'enabled': True,
'item_id': item.id
})
# Let user review and modify plan
confirmed, enabled_items = interactive_plan_selector(plan_items)
if confirmed:
# Update the hierarchical plan with user's selections
# Mark deselected items as SKIPPED so they won't be executed
for plan_item_dict in plan_items:
item_id = plan_item_dict.get('item_id')
enabled = plan_item_dict.get('enabled', False)
if not enabled and item_id:
actual_item = hierarchical_plan.get_item(item_id)
if actual_item:
actual_item.status = PlanStatus.SKIPPED
enabled_count = len(enabled_items)
if enabled_count > 0:
print(f"\nExecuting {enabled_count} selected plan items...\n")
# Track execution results
execution_results = []
failed_items = []
# Set up callback for progress updates
def on_item_status_change(item):
status_icon = "✓" if item.status == PlanStatus.COMPLETED else "✗" if item.status == PlanStatus.FAILED else "○"
print(f" {status_icon} {item.description[:50]}... ({item.status.value})")
planner.set_execution_callback(on_item_status_change)
# Create executor function that uses the agent
def execute_plan_item(item) -> tuple[bool, str]:
"""Execute a single plan item using the agent with verification."""
try:
# Capture initial state for file operations
initial_state = {}
if item.tool_name in ('read_file', 'write_file', 'create_file'):
# Will verify file state after execution
pass
# Build request for this specific item, including original requirements
item_request = f"""Original task: {plan_request}
Current step to execute: {item.description}
IMPORTANT: After completing this step, you MUST report:
1. Whether the operation succeeded or failed
2. The exact file path if you created/modified a file
3. Any errors encountered
4. DO NOT claim success if you did not actually perform the action"""
if item.tool_name:
item_request += f"\n\nUse the {item.tool_name} tool to complete this step."
# Get conversation context before
context_before = agent.get_context().get_messages()[-1] if agent.get_context().get_messages() else None
response = agent.run(item_request)
# Get conversation context after to see tool results
context_after = agent.get_context().get_messages()
# Check tool execution results from context
tool_executed = False
tool_succeeded = False
tool_output = ""
# Look for tool results in the conversation
for msg in context_after:
if hasattr(msg, 'role') and msg.role == 'tool':
tool_executed = True
tool_output = msg.content if hasattr(msg, 'content') else ""
tool_succeeded = "Error:" not in tool_output and "File not found" not in tool_output
# Verify file operations actually happened
verification_result = ""
if item.tool_name == 'write_file':
# Try to extract file path from response or item description
import re
path_match = re.search(r'["\']([^"\']*\.py)["\']', response)
if path_match:
file_path = path_match.group(1)
from pathlib import Path
if Path(file_path).exists():
verification_result = f"✓ Verified: File {file_path} exists"
tool_succeeded = True
else:
verification_result = f"✗ FAILED: File {file_path} does not exist"
tool_succeeded = False
else:
verification_result = "⚠ Warning: Could not verify file creation (path not found in response)"
elif item.tool_name == 'read_file':
# Check if tool actually returned content
if tool_output and len(tool_output.strip()) > 0 and "Error:" not in tool_output:
verification_result = "✓ Verified: File was read successfully"
tool_succeeded = True
else:
verification_result = "✗ FAILED: No file content returned"
tool_succeeded = False
# Build comprehensive result report
result_report = []
result_report.append(f"Step: {item.description}")
result_report.append(f"Response: {response[:200]}...")
if tool_executed:
result_report.append(f"Tool executed: Yes")
result_report.append(f"Tool succeeded: {tool_succeeded}")
if tool_output:
result_report.append(f"Tool output: {tool_output[:200]}...")
else:
result_report.append("Tool executed: No tool was called")
if verification_result:
result_report.append(f"Verification: {verification_result}")
full_report = "\n".join(result_report)
# Determine final success based on actual verification
if tool_succeeded and (not verification_result or "✓" in verification_result):
return True, full_report
elif "✗ FAILED" in verification_result:
return False, full_report
elif response.lower().startswith(("error:", "failed:", "i couldn't", "i cannot")):
return False, full_report
elif not tool_executed and item.tool_name:
# LLM claimed to use tool but didn't
return False, f"{full_report}\n✗ CRITICAL: LLM did not call the required tool!"
else:
# Ambiguous case - return success but with warnings
return True, f"{full_report}\n⚠ Warning: Could not fully verify operation"
except Exception as e:
return False, f"Exception during execution: {str(e)}"
# Execute plan with interactive failure handling
loader = Loader("Executing plan...")
loader.start()
success, message = planner.execute_plan(
hierarchical_plan,
execute_plan_item,
create_checkpoints=True,
interactive_on_failure=True
)
loader.stop()
print(f"\n{'='*60}")
if success:
print("✅ Plan executed successfully!")
else:
print(f"❌ {message}")
# Show final plan status
print("\nFinal Status:")
print(planner.get_plan_summary(hierarchical_plan))
print(f"{'='*60}\n")
elif confirmed:
print("\nNo items selected for execution.\n")
else:
print("\nPlan execution cancelled.\n")
else:
print("Failed to generate plan.\n")
elif command == "/scan":
# Vulnerability scanning and remediation workflow
scan_args = user_input[6:].strip()
path = "."
file_pattern = "*.py"
if scan_args:
parts = scan_args.split()
if len(parts) >= 1:
path = parts[0]
if len(parts) >= 2:
file_pattern = parts[1]
print(f"\n🔍 Scanning for vulnerabilities in {path} (pattern: {file_pattern})...")
# Create remediator with agent's model config and tool registry
remediator = VulnerabilityRemediator(
model_config=model_config,
tool_registry=agent.tool_registry
)
# Step 1: Scan for vulnerabilities
loader = Loader("Scanning code...")
loader.start()
findings = remediator.scan_for_vulnerabilities(path=path, file_pattern=file_pattern)
loader.stop()
if not findings:
print("\n✅ No vulnerabilities detected!\n")
else:
print(f"\n⚠️ Found {len(findings)} potential vulnerabilities.\n")
# Step 2: Generate remediation plan
print("Generating remediation plan...")
loader = Loader("Creating plan...")
loader.start()
plan = remediator.generate_remediation_plan()
loader.stop()
# Step 3: Show plan to user for approval
print("\n" + remediator.format_plan_for_display())
# Step 4: Interactive loop for plan modification
while True:
try:
action = input("\nAction (toggle <n>, enable-all, disable-all, approve, cancel): ").strip().lower()
except (EOFError, KeyboardInterrupt):
print("\nGoodbye!")
break
if action.startswith("toggle"):
try:
index = int(action.split()[1]) - 1
new_status = remediator.toggle_plan_item(index)
status_text = "enabled" if new_status else "disabled"
print(f"Item {index + 1} {status_text}.")
print("\n" + remediator.format_plan_for_display())
except (IndexError, ValueError):
print("Usage: toggle <number>")
elif action == "enable-all":
remediator.enable_all_items()
print("All items enabled.")
print("\n" + remediator.format_plan_for_display())
elif action == "disable-all":
remediator.disable_all_items()
print("All items disabled.")
print("\n" + remediator.format_plan_for_display())
elif action in ("approve", "run"):
# Get enabled items count
enabled_count = sum(1 for item in plan.items if item.enabled)
if enabled_count == 0:
print("\n⚠️ No items enabled. Please enable at least one item or use 'cancel'.")
continue
print(f"\n🚀 Executing {enabled_count} selected fix(es)...")
# Apply all enabled fixes
results = remediator.apply_all_enabled_fixes()
success_count = sum(1 for v in results.values() if v)
fail_count = len(results) - success_count
print(f"\n✅ Successfully applied {success_count} fix(es).")
if fail_count > 0:
print(f"❌ Failed to apply {fail_count} fix(es).")
# Verify fixes
print("\nVerifying fixes...")
remaining = remediator.verify_fixes()
if remaining:
print(f"⚠️ {len(remaining)} vulnerability(ies) still remain.")
else:
print("✅ All selected vulnerabilities have been fixed!")
break
elif action == "cancel":
print("\nRemediation cancelled.\n")
break
else:
print("Unknown action. Use: toggle <n>, enable-all, disable-all, approve, cancel")
else:
print(f"Unknown command: {command}. Type /help for available commands.\n")
continue
if not user_input:
continue
try:
# Show loader while waiting for LLM response
loader = Loader("Waiting for LLM response...")
loader.start()
response = agent.run(user_input)
loader.stop()
print("\n" + response + "\n")
except Exception as e:
loader.stop()
print(f"\nError: {e}\n")
except Exception as e:
print(f"Error initializing agent: {e}")
return 1
return 0
def cmd_run(args: argparse.Namespace) -> int:
"""Run a single command with the coding agent.
Args:
args: Parsed command-line arguments.
Returns:
Exit code (0 for success, non-zero for failure).
"""
workspace_dir = Path(args.workspace).resolve()
if not workspace_dir.exists():
print(f"Error: Workspace directory does not exist: {workspace_dir}")
return 1
# Load model configuration
model_config = ModelConfig(
base_url=args.base_url,
api_key=args.api_key,
model_name=args.model,
)
setup_logging(level=args.log_level)
try:
with create_agent(str(workspace_dir), model_config, args.prepare_context) as agent:
response = agent.run(args.command)
print(response)
except Exception as e:
print(f"Error: {e}")
return 1
return 0
def main() -> int:
"""Main entry point for the CLI.
Returns:
Exit code (0 for success, non-zero for failure).
"""
parser = argparse.ArgumentParser(
prog="agent",
description="Coding Agent CLI - AI-powered coding assistant",
)
subparsers = parser.add_subparsers(dest="command", help="Available commands")
# Init command
init_parser = subparsers.add_parser(
"init",
help="Initialize a directory for use with the coding agent",
)
init_parser.add_argument(
"directory",
nargs="?",
default=".",
help="Directory to initialize (default: current directory)",
)
init_parser.set_defaults(func=cmd_init)
# Chat command
chat_parser = subparsers.add_parser(
"chat",
help="Start an interactive chat session with the coding agent",
)
chat_parser.add_argument(
"-w", "--workspace",
default=".",
help="Workspace directory (default: current directory)",
)
chat_parser.add_argument(
"--base-url",
default=os.getenv("LLM_BASE_URL", "http://localhost:11434/v1"),
help="LLM API base URL (default: http://localhost:11434/v1)",
)
chat_parser.add_argument(
"--api-key",
default=os.getenv("LLM_API_KEY", "ollama"),
help="LLM API key (default: ollama)",
)
chat_parser.add_argument(
"--model",
default=os.getenv("LLM_MODEL", "qwen2.5-coder:7b"),
help="LLM model name (default: from .env or qwen2.5-coder:7b)",
)
chat_parser.add_argument(
"--log-level",
choices=["DEBUG", "INFO", "WARNING", "ERROR"],
default="INFO",
help="Logging level (default: INFO)",
)
chat_parser.add_argument(
"--prepare-context",
action="store_true",
default=True,
dest="prepare_context",
help="Prepare session context before starting (file list, OS info, dependencies, Python rules)",
)
chat_parser.add_argument(
"--no-prepare-context",
action="store_false",
dest="prepare_context",
help="Skip session context preparation",
)
chat_parser.set_defaults(func=cmd_chat)
# Run command
run_parser = subparsers.add_parser(
"run",
help="Run a single command with the coding agent",
)
run_parser.add_argument(
"command",
help="Command to execute",
)
run_parser.add_argument(
"-w", "--workspace",
default=".",
help="Workspace directory (default: current directory)",
)
run_parser.add_argument(
"--base-url",
default=os.getenv("LLM_BASE_URL", "http://localhost:11434/v1"),
help="LLM API base URL (default: http://localhost:11434/v1)",
)
run_parser.add_argument(
"--api-key",
default=os.getenv("LLM_API_KEY", "ollama"),
help="LLM API key (default: ollama)",
)
run_parser.add_argument(
"--model",
default=os.getenv("LLM_MODEL", "qwen2.5-coder:7b"),
help="LLM model name (default: from .env or qwen2.5-coder:7b)",
)
run_parser.add_argument(
"--log-level",
choices=["DEBUG", "INFO", "WARNING", "ERROR"],
default="INFO",
help="Logging level (default: INFO)",
)
run_parser.add_argument(
"--prepare-context",
action="store_true",
default=True,
dest="prepare_context",
help="Prepare session context before starting (file list, OS info, dependencies, Python rules)",
)
run_parser.add_argument(
"--no-prepare-context",
action="store_false",
dest="prepare_context",
help="Skip session context preparation",
)
run_parser.set_defaults(func=cmd_run)
args = parser.parse_args()
if not hasattr(args, "func"):
parser.print_help()
return 0
return args.func(args)
if __name__ == "__main__":
sys.exit(main())