"""Advanced reasoning and planning skill for Agentux. """ from __future__ import annotations import json import re from typing import Any, Dict, List, Optional from .base import BaseSkill class ReasoningSkill(BaseSkill): """Enhances the agent's reasoning and planning capabilities. This skill provides advanced reasoning patterns, task decomposition, and strategic thinking capabilities that enable the agent to tackle complex, multi-step problems like Claude Code. """ def __init__(self, agent: Agent, config: Optional[Dict[str, Any]] = None): super().__init__(agent, config) self.reasoning_patterns = { "task_decomposition": self._decompose_task, "step_by_step_planning": self._create_step_by_step_plan, "hierarchical_planning": self._create_hierarchical_plan, "reflection": self._reflect_on_progress, "problem_solving": self._apply_problem_solving_framework } async def initialize(self) -> None: """Initialize the reasoning skill.""" # No async initialization needed for this skill pass async def execute(self, operation: str, **kwargs) -> Any: """Execute a reasoning operation. Args: operation: The reasoning operation to perform **kwargs: Additional arguments for the operation Returns: The result of the reasoning operation """ if operation not in self.reasoning_patterns: raise ValueError(f"Unknown reasoning operation: {operation}") return await self.reasoning_patterns[operation](**kwargs) async def _decompose_task(self, task_description: str, max_subtasks: int = 5) -> List[Dict[str, Any]]: """Break down a complex task into smaller, manageable subtasks. Args: task_description: Description of the task to decompose max_subtasks: Maximum number of subtasks to create Returns: List of subtask dictionaries with descriptions and estimated complexity """ # Use the agent's own capabilities to decompose the task prompt = f"""Break down the following task into {max_subtasks} or fewer smaller, manageable subtasks. For each subtask, provide: 1. A clear description 2. Estimated complexity (simple, medium, complex) 3. Any dependencies on other subtasks Task: {task_description} Format your response as a JSON array of objects, where each object has: - "description": string describing the subtask - "complexity": "simple", "medium", or "complex" - "dependencies": array of integers indicating which other subtasks this depends on (by index) Example: [{{"description": "Set up development environment", "complexity": "simple", "dependencies": []}}] Only return the JSON array, nothing else.""" # This would normally call the agent's provider, but for now we'll return a structured approach # In a full implementation, this would use self.agent.run() or similar # For demonstration, return a generic decomposition return [ { "description": f"Analyze and understand: {task_description}", "complexity": "simple", "dependencies": [] }, { "description": f"Plan approach for: {task_description}", "complexity": "medium", "dependencies": [0] }, { "description": f"Execute solution for: {task_description}", "complexity": "complex", "dependencies": [1] }, { "description": f"Verify and test results for: {task_description}", "complexity": "medium", "dependencies": [2] }, { "description": f"Document and finalize: {task_description}", "complexity": "simple", "dependencies": [3] } ][:max_subtasks] async def _create_step_by_step_plan(self, goal: str, constraints: Optional[List[str]] = None) -> Dict[str, Any]: """Create a detailed step-by-step plan to achieve a goal. Args: goal: The goal to achieve constraints: Any constraints or limitations Returns: A structured plan with steps, estimated time, and resources needed """ constraints = constraints or [] # In a full implementation, this would use the agent's reasoning capabilities # For now, return a structured template return { "goal": goal, "constraints": constraints, "steps": [ { "step": 1, "description": "Understand the requirements and gather information", "estimated_time": "5-15 minutes", "tools_needed": ["search", "filesystem"] }, { "step": 2, "description": "Design the solution approach", "estimated_time": "10-30 minutes", "tools_needed": ["think", "todo"] }, { "step": 3, "description": "Implement the solution", "estimated_time": "30-120 minutes", "tools_needed": ["filesystem", "shell", "git"] }, { "step": 4, "description": "Test and validate the solution", "estimated_time": "15-30 minutes", "tools_needed": ["shell", "filesystem"] }, { "step": 5, "description": "Document and cleanup", "estimated_time": "5-15 minutes", "tools_needed": ["filesystem"] } ], "estimated_total_time": "65-210 minutes", "success_criteria": [ "The goal stated in the request has been achieved", "Any tests or validation checks pass", "The solution follows best practices and conventions" ] } async def _create_hierarchical_plan(self, objective: str) -> Dict[str, Any]: """Create a hierarchical plan with multiple levels of detail. Args: objective: The high-level objective Returns: A hierarchical plan structure """ return { "objective": objective, "levels": { "strategic": { "timeframe": "Long-term (weeks/months)", "goals": [ f"Achieve {objective}", "Establish sustainable processes", "Create reusable components" ] }, "tactical": { "timeframe": "Medium-term (days/weeks)", "goals": [ "Design system architecture", "Implement core features", "Set up development environment" ] }, "operational": { "timeframe": "Short-term (hours/days)", "goals": [ "Write specific code modules", "Fix individual bugs", "Complete defined tasks" ] } } } async def _reflect_on_progress(self, completed_work: List[str], remaining_work: List[str]) -> Dict[str, Any]: """Reflect on progress made and suggest adjustments. Args: completed_work: List of completed tasks/work items remaining_work: List of remaining tasks/work items Returns: Reflection insights and recommendations """ return { "progress_assessment": { "completed_count": len(completed_work), "remaining_count": len(remaining_work), "completion_percentage": len(completed_work) / max(len(completed_work) + len(remaining_work), 1) * 100 }, "insights": [ f"Completed {len(completed_work)} tasks so far", f"Have {len(remaining_work)} tasks remaining", "Progress is on track" if len(completed_work) >= len(remaining_work) else "May need to adjust approach" ], "recommendations": [ "Continue with current approach" if len(completed_work) > 0 else "Consider starting with simpler tasks", "Break down large remaining tasks into smaller pieces", "Verify completed work before moving on" ], "suggested_adjustments": [] } async def _apply_problem_solving_framework(self, problem: str) -> Dict[str, Any]: """Apply a structured problem-solving framework. Args: problem: Description of the problem to solve Returns: Structured analysis and solution approach """ return { "problem": problem, "framework_applied": "IDEAL (Identify, Define, Explore, Act, Look)", "steps": { "Identify": { "description": "Clearly identify what the problem is", "questions": [ "What exactly is not working or needs to be created?", "What are the symptoms or error messages?", "When does the problem occur?" ] }, "Define": { "description": "Define the problem clearly and specifically", "questions": [ "What are the boundaries of this problem?", "What would a successful solution look like?", "What constraints or requirements exist?" ] }, "Explore": { "description": "Explore possible solutions and approaches", "questions": [ "What similar problems have been solved before?", "What different approaches could work?", "What resources or tools are available?" ] }, "Act": { "description": "Act on the best solution found", "questions": [ "What is the most promising approach?", "What steps need to be taken to implement it?", "What risks or challenges might arise?" ] }, "Look": { "description": "Look back and evaluate the results", "questions": [ "Did the solution work as expected?", "What was learned from this process?", "How can this approach be improved or generalized?" ] } } }