Skip to content

Winter-Soren/soren-agent

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

25 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Soren Agent πŸ€–

A production-ready, extensible AI agent framework built with Python that enables autonomous task execution through a sophisticated tool system, MCP integration, and advanced safety controls.

Overview

Soren Agent is a powerful agentic AI framework designed for developers who need reliable, safe, and extensible AI automation. It combines streaming LLM responses with a robust tool execution system, comprehensive safety policies, and seamless integration with the Model Context Protocol (MCP) ecosystem.

Key Features

Core Capabilities

  • Dual Execution Modes: Run interactively in a rich TUI or execute single commands programmatically
  • Streaming Responses: Real-time text streaming with token-by-token output
  • Multi-Turn Conversations: Maintains context across multiple interactions with intelligent compression
  • Agentic Loop: Autonomous tool calling with configurable turn limits and loop detection
  • Event-Driven Architecture: Comprehensive event system for monitoring agent lifecycle and tool execution

Built-in Tool Suite

The framework includes a comprehensive set of built-in tools:

  • File Operations: read_file, write_file, edit_file - Full file manipulation capabilities
  • Directory Management: list_dir, glob - Navigate and search filesystem
  • Text Search: grep - Powerful pattern matching across files
  • Shell Execution: shell - Execute system commands with safety controls
  • Web Access: web_search, web_fetch - Search and retrieve web content
  • Memory System: memory - Persistent key-value storage for agent preferences
  • Task Management: todo - Built-in task tracking

Intelligent Context Management

  • Automatic Compression: Intelligently compresses conversation history when approaching token limits
  • Smart Pruning: Removes old tool outputs to maintain context efficiency
  • Token Tracking: Real-time monitoring of token usage across conversations
  • Configurable Windows: Support for models with different context window sizes

Advanced Safety & Approval System

Multiple approval policies to match your security requirements:

  • on-request: Prompt for confirmation on mutating operations (default)
  • auto: Automatically approve safe operations, prompt for dangerous ones
  • never: Block all mutating operations
  • yolo: Auto-approve everything (use with caution!)

Safety features include:

  • Dangerous command detection (rm -rf, format, etc.)
  • Path-based safety checks
  • Configurable shell environment policies
  • Environment variable filtering

Session Management

  • Persistent Sessions: Save and resume conversations with full context
  • Checkpoints: Create restore points during long-running tasks
  • Session History: Browse and manage multiple saved sessions
  • Metadata Tracking: Automatic tracking of turns, timestamps, and token usage

Model Context Protocol (MCP) Integration

Seamlessly integrate with the MCP ecosystem:

  • Multiple Transports: Support for stdio and HTTP/SSE connections
  • Dynamic Tool Loading: Automatically discover and register MCP server tools
  • Server Management: Monitor connection status and available tools
  • Configurable Servers: Define multiple MCP servers with custom environments

Example MCP configuration:

[mcp_servers.filesystem]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/workspace"]

[mcp_servers.database]
url = "http://localhost:3000/mcp"

Subagent System

Delegate specialized tasks to purpose-built subagents:

  • Codebase Investigator: Analyzes code structure and dependencies
  • Code Reviewer: Performs automated code reviews
  • Custom Subagents: Define your own with specific tools and constraints

Loop Detection

Prevents infinite loops and repetitive behavior:

  • Detects repeated tool calls with identical parameters
  • Identifies circular reasoning patterns
  • Automatically injects loop-breaking prompts
  • Configurable detection thresholds

Hooks System

Execute custom logic at key points in the agent lifecycle:

[[hooks]]
name = "lint_on_write"
trigger = "after_tool"
command = "npm run lint"
timeout_sec = 30

Supported triggers:

  • before_agent / after_agent: Around agent execution
  • before_tool / after_tool: Around tool calls
  • on_error: When errors occur

Flexible Configuration

Configure via .soren/config.toml:

hooks_enabled = true

[model]
name = "openai/gpt-4o-mini"
temperature = 0.7

[mcp_servers.filesystem]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-filesystem", "/workspace"]

Configuration options:

  • Model selection and parameters
  • Working directory
  • Tool allowlisting
  • Developer/user instructions
  • Shell environment policies
  • MCP server definitions
  • Hook configurations

Rich Terminal UI

Beautiful, informative terminal interface:

  • Color-coded output for different event types
  • Real-time tool execution visualization
  • Diff display for file modifications
  • Token usage statistics
  • Progress indicators

Interactive Commands

Powerful command interface for session control:

  • /help - Show available commands
  • /config - Display current configuration
  • /model <name> - Switch LLM model
  • /approval <policy> - Change approval policy
  • /tools - List available tools
  • /mcp - Show MCP server status
  • /stats - Display session statistics
  • /save - Save current session
  • /resume <id> - Resume saved session
  • /checkpoint - Create restore point
  • /restore <id> - Restore from checkpoint
  • /clear - Clear conversation history
  • /exit - Quit the agent

πŸš€ Getting Started

Prerequisites

  • Python 3.12+
  • OpenRouter API key (or compatible LLM provider)

Installation

  1. Clone the repository:
git clone <repository-url>
cd soren-agent
  1. Install dependencies using uv:
uv sync
  1. Set up environment variables:
# Create .env file
echo "OPENROUTER_API_KEY=your_api_key_here" > .env
echo "OPENROUTER_BASE_URL=https://openrouter.ai/api/v1" >> .env
  1. Configure the agent:
# Edit .soren/config.toml with your preferences

Usage

Interactive Mode:

python main.py

Single Command Mode:

python main.py "Analyze the codebase and create a summary"

Custom Working Directory:

python main.py --cwd /path/to/project "List all Python files"

Project Structure

soren-agent/
β”œβ”€β”€ agent/              # Core agent implementation
β”‚   β”œβ”€β”€ agent.py       # Main agent loop and orchestration
β”‚   β”œβ”€β”€ session.py     # Session management and state
β”‚   β”œβ”€β”€ events.py      # Event system definitions
β”‚   └── persistence.py # Session persistence
β”œβ”€β”€ client/            # LLM client abstraction
β”‚   β”œβ”€β”€ llm_client.py  # OpenAI-compatible client
β”‚   └── response.py    # Response parsing and streaming
β”œβ”€β”€ config/            # Configuration management
β”‚   β”œβ”€β”€ config.py      # Configuration models
β”‚   └── loader.py      # Config loading and validation
β”œβ”€β”€ contexts/          # Context management
β”‚   β”œβ”€β”€ manager.py     # Message history management
β”‚   β”œβ”€β”€ compaction.py  # Context compression
β”‚   └── loop_detector.py # Loop detection logic
β”œβ”€β”€ hooks/             # Hook system
β”‚   └── hook_system.py # Hook execution engine
β”œβ”€β”€ safety/            # Safety and approval
β”‚   └── approval.py    # Approval policies and checks
β”œβ”€β”€ tools/             # Tool system
β”‚   β”œβ”€β”€ base.py        # Tool base classes
β”‚   β”œβ”€β”€ registry.py    # Tool registration and invocation
β”‚   β”œβ”€β”€ discovery.py   # Dynamic tool discovery
β”‚   β”œβ”€β”€ subagents.py   # Subagent definitions
β”‚   β”œβ”€β”€ builtin/       # Built-in tools
β”‚   └── mcp/           # MCP integration
β”‚       β”œβ”€β”€ client.py  # MCP client implementation
β”‚       β”œβ”€β”€ mcp_manager.py # MCP server management
β”‚       └── mcp_tool.py    # MCP tool wrapper
β”œβ”€β”€ ui/                # User interface
β”‚   └── tui.py         # Terminal UI implementation
β”œβ”€β”€ utils/             # Utilities
β”‚   β”œβ”€β”€ errors.py      # Error definitions
β”‚   β”œβ”€β”€ paths.py       # Path utilities
β”‚   └── text.py        # Text processing
└── main.py            # Entry point

Advanced Usage

Creating Custom Tools

from tools.base import Tool, ToolInvocation, ToolResult, ToolKind
from pydantic import BaseModel

class MyToolParams(BaseModel):
    input: str

class MyCustomTool(Tool):
    name = "my_tool"
    description = "Does something useful"
    kind = ToolKind.READ
    schema = MyToolParams
    
    async def execute(self, invocation: ToolInvocation) -> ToolResult:
        params = MyToolParams(**invocation.params)
        # Your logic here
        return ToolResult.success_result("Done!")

Defining Subagents

from tools.subagents import SubagentDefinition

custom_subagent = SubagentDefinition(
    name="my_subagent",
    description="Specialized task handler",
    instructions="You are an expert at...",
    allowed_tools=["read_file", "write_file"],
    max_turns=10
)

Custom Hooks

[[hooks]]
name = "security_scan"
trigger = "before_tool"
command = "python security_check.py"
timeout_sec = 15
enabled = true

Contributing

Contributions are welcome! Please feel free to submit issues and pull requests.

Acknowledgments

About

AI pair programmer in your terminal - understands code, edits files, runs commands, searches the web, and completes tasks autonomously with safety controls

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages