# All functions must be typeddef get_provider(name: str) -> Provider: """Get provider by name.""" passasync def execute_tool( tool: Tool, args: dict[str, Any],) -> ToolResult: """Execute a tool with given arguments.""" pass
# Async file operationsasync def read_file(path: str) -> str: async with aiofiles.open(path, "r") as f: return await f.read()# Async HTTPasync def fetch_data(url: str) -> dict[str, Any]: async with httpx.AsyncClient() as client: response = await client.get(url) return response.json()# Concurrent operationsasync def process_files(paths: list[str]) -> list[str]: tasks = [read_file(path) for path in paths] return await asyncio.gather(*tasks)
Error Handling
Exception Hierarchy
class MiuError(Exception): """Base exception for all miu errors."""class ProviderError(MiuError): """LLM provider error."""class ToolError(MiuError): """Tool execution error."""class AgentError(MiuError): """Agent operation error."""
Pattern
async def execute_tool(tool: Tool) -> ToolResult: try: return await tool.execute() except ProviderError as e: logger.error(f"Provider error: {e}") raise AgentError(f"Tool execution failed") from e
Testing Standards
Structure
import pytestclass TestAnthropicProvider: @pytest.fixture def provider(self) -> AnthropicProvider: return AnthropicProvider(api_key="test-key") def test_initialization(self, provider: AnthropicProvider) -> None: assert provider is not None @pytest.mark.asyncio async def test_execute_async(self, provider: AnthropicProvider) -> None: result = await provider.generate("test") assert result is not None
Conventions
Coverage Target: ≥80% per package
Async Tests: Use @pytest.mark.asyncio
Naming:test_<function_name> or test_<scenario>
Documentation Standards
async def execute( self, tool_name: str, arguments: dict[str, Any],) -> ToolResult: """Execute a tool by name with given arguments. Args: tool_name: Name of the tool to execute. arguments: Arguments to pass to the tool. Returns: ToolResult containing output and metadata. Raises: ToolError: If tool not found or execution fails. """
Security
Never hardcode credentials. Always use environment variables.
# Goodapi_key = os.environ.get("ANTHROPIC_API_KEY")if not api_key: raise ValueError("ANTHROPIC_API_KEY not set")# Bad - never do this!api_key = "sk-1234567890"
Path Validation
def validate_file_path(path: str) -> Path: resolved = Path(path).resolve() base = Path.cwd().resolve() if not str(resolved).startswith(str(base)): raise ValueError(f"Path outside allowed directory") return resolved
Conventional Commits
All commits must follow Conventional Commits format: