Skip to main content

pi-coding - Coding Agent Package

A full-featured coding agent with file system tools, code editing, and shell command execution for pi-mono-py.

Overview

pi-coding provides a ready-to-use coding agent with common developer tools:

  • File Operations: Read, write, edit, and list files
  • Search: Grep-based text search in files
  • Execution: Run shell commands with timeout
  • Integration: Seamless integration with pi_agent runtime

Installation

cd pi-mono-py
uv sync

Quick Start

Option 1: Use Pre-configured Agent

import asyncio
from pi_coding import create_coding_agent
from pi_ai import Model, ModelCost

agent = create_coding_agent(
    model=Model(
        id="your-model",
        name="Your Model",
        api="openai-completions",
        provider="openai",
        base_url="https://api.openai.com/v1",
        reasoning=False,
        input=["text"],
        cost=ModelCost(input=0.5, output=1.5, cache_read=0.0, cache_write=0.0),
        context_window=128000,
        max_tokens=4096,
    )
)

await agent.prompt("Read the main.py file and tell me what it does")

Option 2: Get Tools Only

import asyncio
from pi_coding import get_coding_tools
from pi_agent import Agent

# Get all coding tools
tools = get_coding_tools()

# Create custom agent with tools
agent = Agent(options={
    "model": your_model,
    "tools": tools,
    "system_prompt": "You are a helpful coding assistant."
})

Available Tools

read_file

Read the contents of a file with line numbers.

# Tool call
{
    "name": "read_file",
    "args": {
        "path": "main.py"
    }
}

# Parameters
- path: Path to the file (relative or absolute)

# Output
- First 100 lines with line numbers
- Total line count
- File path

write_file

Write content to a file (creates parent directories if needed).

# Tool call
{
    "name": "write_file",
    "args": {
        "path": "hello.py",
        "content": "print('Hello, World!')"
    }
}

# Parameters
- path: Path to the file to write
- content: Content to write

# Output
- Line count and character count
- Success confirmation

edit_file

Edit a file by replacing exact string matches.

# Tool call
{
    "name": "edit_file",
    "args": {
        "path": "config.py",
        "old_text": "DEBUG = True",
        "new_text": "DEBUG = False",
        "replace_all": true
    }
}

# Parameters
- path: Path to the file to edit
- old_text: Text to search for
- new_text: New text to replace with
- replace_all: Replace all occurrences (default: false)

# Output
- Number of occurrences replaced
- Success confirmation

list_directory

List files and directories with type and size information.

# Tool call
{
    "name": "list_directory",
    "args": {
        "path": "src"
    }
}

# Parameters
- path: Directory path (default: current directory)

# Output
- File/directory type
- Size (for files)
- Total item count

run_command

Execute a shell command with timeout.

# Tool call
{
    "name": "run_command",
    "args": {
        "command": "pytest tests/",
        "timeout": 60
    }
}

# Parameters
- command: Shell command to execute
- timeout: Timeout in seconds (default: 30)

# Output
- Exit code
- stdout
- stderr

Search for text in files using grep (recursive search).

# Tool call
{
    "name": "grep_search",
    "args": {
        "pattern": "def main",
        "path": "src",
        "case_sensitive": false
    }
}

# Parameters
- pattern: Search pattern (supports regex)
- path: Directory to search (default: current directory)
- case_sensitive: Case-sensitive search (default: false)

# Output
- Matched lines
- Total match count

Examples

Example 1: Read and Edit File

import asyncio
from pi_coding import create_coding_agent

agent = create_coding_agent()

# Read a file
await agent.prompt("Read the README.md file")

# Edit it
await agent.prompt("Replace 'TODO' with 'DONE' in README.md")

Example 2: Create New File

import asyncio
from pi_coding import create_coding_agent

agent = create_coding_agent()

await agent.prompt("""
Create a new file called hello.py with this content:

def greet(name):
    return f'Hello, {name}!'

if __name__ == '__main__':
    print(greet('World'))
""")

Example 3: Explore Project

import asyncio
from pi_coding import create_coding_agent

agent = create_coding_agent()

await agent.prompt("List the files in the src directory")

await agent.prompt("What Python files are in the examples directory?")

Example 4: Run Tests

import asyncio
from pi_coding import create_coding_agent

agent = create_coding_agent()

await agent.prompt("Run pytest on the tests directory")

await agent.prompt("Run the test suite and show me the results")

Example 5: Search and Fix

import asyncio
from pi_coding import create_coding_agent

agent = create_coding_agent()

# Search for TODOs
await agent.prompt("Search for 'TODO' in all Python files")

# Fix them
await agent.prompt("Replace all TODO comments with DONE")

Advanced Usage

Custom System Prompt

from pi_coding import CodingAgentConfig

config = CodingAgentConfig(
    system_prompt="""You are a security-focused code reviewer.
Always check for:
- SQL injection vulnerabilities
- Missing input validation
- Hardcoded credentials
- Insecure dependencies"""
)

agent = create_coding_agent(config=config)

Custom Working Directory

from pi_coding import CodingAgentConfig

config = CodingAgentConfig(
    working_dir="/path/to/project",
    command_timeout=60
)

agent = create_coding_agent(config=config)

Custom Model

from pi_coding import CodingAgentConfig
from pi_ai import Model, ModelCost

config = CodingAgentConfig(
    model=Model(
        id="claude-3.5-sonnet",
        name="Claude 3.5 Sonnet",
        api="anthropic-messages",
        provider="anthropic",
        base_url="https://api.anthropic.com",
        reasoning=True,
        input=["text", "image"],
        cost=ModelCost(
            input=3.0,
            output=15.0,
            cache_read=0.3,
            cache_write=0.3
        ),
        context_window=200000,
        max_tokens=8192,
    )
)

agent = create_coding_agent(config=config)

Tool Design Philosophy

All tools follow these principles:

  1. Parameter Validation: JSON Schema with required fields
  2. Error Handling: Try-catch with meaningful error messages
  3. Rich Output: Detailed results with context and metadata
  4. Safety: Timeouts, path validation, permission checks
  5. User-Friendly: Clear success/failure indicators with emojis

API Reference

get_coding_tools() -> list[AgentTool]

Get all coding agent tools.

Returns:

  • List of 6 AgentTool instances

Example:

from pi_coding import get_coding_tools

tools = get_coding_tools()
for tool in tools:
    print(f"{tool.name}: {tool.description}")

create_coding_agent(config=None, **kwargs) -> Agent

Create a pre-configured coding agent.

Parameters:

  • config: CodingAgentConfig instance (optional)
  • **kwargs: Additional options passed to Agent

Returns:

  • Configured Agent instance

Example:

from pi_coding import CodingAgentConfig, create_coding_agent

config = CodingAgentConfig(
    system_prompt="You are a Python expert.",
    command_timeout=60
)

agent = create_coding_agent(config=config)

CodingAgentConfig

Configuration class for coding agent.

Attributes:

  • model: Model instance (optional)
  • system_prompt: System prompt string (optional)
  • working_dir: Working directory path (optional)
  • command_timeout: Command timeout in seconds (default: 30)

Testing

# Run tests
cd packages/pi_coding
uv run pytest

# Run with coverage
uv run pytest --cov=pi_coding --cov-report=html

Troubleshooting

Permission Denied Errors

If you get permission errors:

# Check file permissions
ls -la <file>

# Run with appropriate permissions
sudo python your_script.py

Command Not Found

If commands fail:

# Check if command is installed
which <command>

# Install if needed
apt install <package>  # Debian/Ubuntu
# or
brew install <package>  # macOS

Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new features
  5. Ensure all tests pass
  6. Submit a pull request

License

MIT License - see LICENSE file for details.

Metadata

Release files for pi-mono-coding 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pi-mono-coding 0.1.1
File Size Uploaded Provenance Actions
pi_mono_coding-0.1.1.tar.gz 84.1 kB Without provenance Details for pi_mono_coding-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pi-mono-coding 0.1.1
File Interpreter ABI Platform Actions
pi_mono_coding-0.1.1-py3-none-any.whl Python 3 none any Details for pi_mono_coding-0.1.1-py3-none-any.whl

Total release size: 118.1 kB

Release files / pi_mono_coding-0.1.1.tar.gz

Download URL pi_mono_coding-0.1.1.tar.gz
Size 84.1 kB
Tags Source
SHA-256 checksum
How to use checksums
e2ab2e1d5772552655a84b37ef5dc0d713ac310b068894a40705270d5ee3304b
BLAKE2b-256 checksum
How to use checksums
c4714c778ae617c7233324727aef02c4e2b2773ec3f6bd0585d7c0d09ba7880d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.10

Release files / pi_mono_coding-0.1.1-py3-none-any.whl

Download URL pi_mono_coding-0.1.1-py3-none-any.whl
Size 34.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1a9bd2e84960dff788be3b777da347dc7d599fb6cfc960f1b0530e512f501465
BLAKE2b-256 checksum
How to use checksums
ac8b41835bd79b52363610106e05f7a3b4ebd46d097bc52f28d3e89c7e119077
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.10

Security for pi-mono-coding 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page