|
| 1 | +--- |
| 2 | +description: Rules for core modules of SGR Agent Core |
| 3 | +globs: sgr_agent_core/**/*.py |
| 4 | +alwaysApply: true |
| 5 | +--- |
| 6 | + |
| 7 | +# Rules for Core Modules |
| 8 | + |
| 9 | +## Base Classes |
| 10 | + |
| 11 | +### BaseAgent (`sgr_agent_core/base_agent.py`) |
| 12 | +- Parent class for all agents |
| 13 | +- Implements two-phase execution cycle: Reasoning → Action |
| 14 | +- Manages agent context, conversation history, and streaming |
| 15 | +- Must be subclassed to implement `_reasoning_phase()`, `_select_action_phase()`, `_action_phase()` |
| 16 | +- Automatically registered in `AgentRegistry` via `AgentRegistryMixin` |
| 17 | + |
| 18 | +### BaseTool (`sgr_agent_core/base_tool.py`) |
| 19 | +- Parent class for all tools |
| 20 | +- Must be a Pydantic model |
| 21 | +- Must implement `__call__(context, config)` method |
| 22 | +- Returns string or JSON string |
| 23 | +- Automatically registered in `ToolRegistry` via `ToolRegistryMixin` |
| 24 | + |
| 25 | +### MCPBaseTool (`sgr_agent_core/base_tool.py`) |
| 26 | +- Base class for MCP-integrated tools |
| 27 | +- Handles MCP client calls |
| 28 | +- Converts MCP responses to tool format |
| 29 | + |
| 30 | +## Configuration Modules |
| 31 | + |
| 32 | +### GlobalConfig (`sgr_agent_core/agent_config.py`) |
| 33 | +- Singleton pattern for global configuration |
| 34 | +- Loads from YAML files (`config.yaml`, `agents.yaml`) |
| 35 | +- Provides default values for all agent settings |
| 36 | + |
| 37 | +### AgentDefinition (`sgr_agent_core/agent_definition.py`) |
| 38 | +- Definition template for creating agents |
| 39 | +- Contains: name, base_class, tools, llm, prompts, execution, search, mcp configs |
| 40 | +- Supports YAML loading |
| 41 | +- Validates required fields |
| 42 | + |
| 43 | +### AgentConfig (`sgr_agent_core/agent_definition.py`) |
| 44 | +- Runtime configuration for agent instance |
| 45 | +- Combines: LLMConfig, SearchConfig, ExecutionConfig, PromptsConfig, MCPConfig |
| 46 | +- Supports hierarchical inheritance from GlobalConfig |
| 47 | + |
| 48 | +## Factory and Services |
| 49 | + |
| 50 | +### AgentFactory (`sgr_agent_core/agent_factory.py`) |
| 51 | +- Creates agent instances from AgentDefinition |
| 52 | +- Resolves agent classes from AgentRegistry |
| 53 | +- Resolves tools from ToolRegistry |
| 54 | +- Builds MCP tools via MCP2ToolConverter |
| 55 | +- Creates OpenAI client with proxy support |
| 56 | + |
| 57 | +### AgentRegistry (`sgr_agent_core/services/registry.py`) |
| 58 | +- Centralized registry for agent classes |
| 59 | +- Automatic registration via `AgentRegistryMixin` |
| 60 | +- Supports lookup by name (case-insensitive) |
| 61 | + |
| 62 | +### ToolRegistry (`sgr_agent_core/services/registry.py`) |
| 63 | +- Centralized registry for tool classes |
| 64 | +- Automatic registration via `ToolRegistryMixin` |
| 65 | +- Supports lookup by name (case-insensitive) |
| 66 | + |
| 67 | +### PromptLoader (`sgr_agent_core/services/prompt_loader.py`) |
| 68 | +- Loads and formats prompts from files or strings |
| 69 | +- Generates system prompts with tool descriptions |
| 70 | +- Formats initial user requests and clarification responses |
| 71 | + |
| 72 | +### MCP2ToolConverter (`sgr_agent_core/services/mcp_service.py`) |
| 73 | +- Converts MCP server tools to BaseTool instances |
| 74 | +- Handles MCP client initialization |
| 75 | +- Builds tools from MCP configuration |
| 76 | + |
| 77 | +## Agent Implementations |
| 78 | + |
| 79 | +### SGRAgent (`sgr_agent_core/agents/sgr_agent.py`) |
| 80 | +- Uses Structured Output approach |
| 81 | +- Creates dynamic JSON schema for tools |
| 82 | +- LLM returns reasoning + tool schema in one call |
| 83 | +- Extracts tool directly from reasoning result |
| 84 | + |
| 85 | +### ToolCallingAgent (`sgr_agent_core/agents/tool_calling_agent.py`) |
| 86 | +- Uses native Function Calling |
| 87 | +- No explicit reasoning phase |
| 88 | +- Uses `tool_choice="required"` for tool selection |
| 89 | +- Best for advanced LLM models |
| 90 | + |
| 91 | +### SGRToolCallingAgent (`sgr_agent_core/agents/sgr_tool_calling_agent.py`) |
| 92 | +- Hybrid approach: SGR + Function Calling |
| 93 | +- Uses ReasoningTool for explicit reasoning |
| 94 | +- Uses Function Calling for tool selection |
| 95 | +- Best balance for most tasks |
| 96 | + |
| 97 | +## Tools |
| 98 | + |
| 99 | +### ReasoningTool (`sgr_agent_core/tools/reasoning_tool.py`) |
| 100 | +- Provides structured reasoning output |
| 101 | +- Contains: reasoning_steps, current_situation, plan_status, enough_data, remaining_steps, task_completed |
| 102 | + |
| 103 | +### ClarificationTool (`sgr_agent_core/tools/clarification_tool.py`) |
| 104 | +- Requests clarification from user |
| 105 | +- Pauses agent execution |
| 106 | +- Waits for user input via `provide_clarification()` |
| 107 | + |
| 108 | +### WebSearchTool (`sgr_agent_core/tools/web_search_tool.py`) |
| 109 | +- Performs web search via Tavily API |
| 110 | +- Returns search results with sources |
| 111 | +- Respects max_searches limit |
| 112 | + |
| 113 | +### ExtractPageContentTool (`sgr_agent_core/tools/extract_page_content_tool.py`) |
| 114 | +- Extracts content from web pages |
| 115 | +- Uses Tavily API for content extraction |
| 116 | +- Respects content_limit |
| 117 | + |
| 118 | +### GeneratePlanTool (`sgr_agent_core/tools/generate_plan_tool.py`) |
| 119 | +- Generates research plan |
| 120 | +- Defines research goal and steps |
| 121 | +- Provides search strategies |
| 122 | + |
| 123 | +### AdaptPlanTool (`sgr_agent_core/tools/adapt_plan_tool.py`) |
| 124 | +- Adapts existing plan based on new information |
| 125 | +- Updates research goal and steps |
| 126 | +- Modifies search strategies |
| 127 | + |
| 128 | +### CreateReportTool (`sgr_agent_core/tools/create_report_tool.py`) |
| 129 | +- Creates final research report |
| 130 | +- Saves report to file |
| 131 | +- Formats report with sources |
| 132 | + |
| 133 | +### FinalAnswerTool (`sgr_agent_core/tools/final_answer_tool.py`) |
| 134 | +- Provides final answer to user |
| 135 | +- Completes agent execution |
| 136 | +- Sets agent state to COMPLETED |
| 137 | + |
| 138 | +## Server and API |
| 139 | + |
| 140 | +### FastAPI Application (`sgr_agent_core/server/app.py`) |
| 141 | +- Main FastAPI application |
| 142 | +- Configures CORS, middleware |
| 143 | +- Registers endpoints |
| 144 | + |
| 145 | +### API Endpoints (`sgr_agent_core/server/endpoints.py`) |
| 146 | +- `/v1/chat/completions` - OpenAI-compatible chat endpoint |
| 147 | +- `/v1/agents/{agent_id}/state` - Get agent state |
| 148 | +- `/v1/agents/{agent_id}/clarification` - Provide clarification |
| 149 | +- `/v1/agents` - List available agents |
| 150 | + |
| 151 | +### Streaming (`sgr_agent_core/stream.py`) |
| 152 | +- OpenAIStreamingGenerator for streaming responses |
| 153 | +- Formats events in OpenAI-compatible format |
| 154 | +- Handles tool calls and content chunks |
| 155 | + |
| 156 | +## General Rules for All Modules |
| 157 | + |
| 158 | +1. **Type hints**: Use type hints everywhere |
| 159 | +2. **Async**: All I/O operations must be async |
| 160 | +3. **Documentation**: Document public methods in English |
| 161 | +4. **Error handling**: Use specific exceptions |
| 162 | +5. **Registry**: Use registry pattern for discovery |
| 163 | +6. **Configuration**: Prefer YAML over hardcoded values |
| 164 | + |
| 165 | +## References |
| 166 | + |
| 167 | +@architecture.mdc |
| 168 | +@code-style.mdc |
| 169 | +@docs/en/framework/main-concepts.md |
0 commit comments