Skip to content

feat: Refactor Claude Code integration to use PraisonAI Agents #635

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
291 changes: 291 additions & 0 deletions CLAUDE_CODE_INTEGRATION_PRAISONAI_AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,291 @@
# Claude Code Integration with PraisonAI Agents

This document describes the integration of Claude Code with PraisonAI Agents, allowing AI agents to intelligently decide when to use Claude Code for file modifications and coding tasks.

## Overview

The refactored implementation replaces direct `litellm` usage with `praisonaiagents`, making Claude Code a custom tool that the AI agent can choose to use based on the user's request. This provides better intelligence and flexibility while maintaining backward compatibility.

## Key Features

### 🤖 Agent-Driven Decision Making
- AI agent intelligently decides when to use Claude Code vs. regular responses
- No manual detection logic - the agent understands context
- Automatic tool selection based on request type

### 🔧 Claude Code as a Custom Tool
- Claude Code is implemented as a tool function for PraisonAI Agents
- Executes `claude --dangerously-skip-permissions -p "query"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The documentation mentions the use of claude --dangerously-skip-permissions. Given the security implications of this flag, especially when combined with user-provided queries, it would be beneficial to emphasize this risk more prominently. Consider adding a dedicated warning note or expanding on the potential security considerations to ensure users are fully aware.

- Supports `--continue` flag for conversation continuity
- Git operations with automatic branch creation and PR URLs

### ⚙️ Flexible Configuration
- CLI flag: `praisonai code --claudecode`
- UI toggle: "Enable Claude Code (file modifications & coding)"
- Environment variable: `PRAISONAI_CLAUDECODE_ENABLED=true`

### 🔄 Backward Compatibility
- Falls back to `litellm` if `praisonaiagents` is not available
- Maintains all existing functionality
- No breaking changes to existing workflows

## Architecture

### Agent Instructions

The agent receives these instructions to guide tool usage:

```
You are a helpful AI assistant. Use the available tools when needed to provide comprehensive responses.

If Claude Code tool is available and the user's request involves:
- File modifications, code changes, or implementation tasks
- Creating, editing, or debugging code
- Project setup or development tasks
- Git operations or version control

Then use the Claude Code tool to handle those requests.

For informational questions, explanations, or general conversations, respond normally without using Claude Code.
```

### Tool Function: `claude_code_tool`

```python
async def claude_code_tool(query: str) -> str:
"""
Execute Claude Code CLI commands for file modifications and coding tasks.

Args:
query: The user's request that requires file modifications or coding assistance

Returns:
The output from Claude Code execution
"""
```

**Features:**
- Executes Claude Code with `--dangerously-skip-permissions`
- Supports conversation continuity with `--continue` flag
- Automatic git branch creation and management
- PR URL generation for GitHub repositories
- 5-minute timeout for long operations
- Comprehensive error handling

### Available Tools

When Claude Code is enabled, the agent has access to:

1. **Claude Code Tool** - For file modifications and coding tasks
2. **Tavily Web Search** - For internet research (if API key configured)

The agent decides which tool(s) to use based on the user's request.

## Usage

### Command Line Interface

```bash
# Enable Claude Code integration
praisonai code --claudecode

# Use regular mode (no Claude Code)
praisonai code
```

### Environment Variables

```bash
# Force enable Claude Code (overrides UI setting)
export PRAISONAI_CLAUDECODE_ENABLED=true

# Set repository path (defaults to current directory)
export PRAISONAI_CODE_REPO_PATH=/path/to/your/repo
```

### UI Settings

In the PraisonAI Code UI, you can:
- Toggle "Enable Claude Code (file modifications & coding)" switch
- Setting is saved to database for persistence
- CLI flag takes precedence over UI setting

## Examples

### File Modification Request
**User:** "Create a new Python function to calculate fibonacci numbers"

**Agent Decision:** Uses Claude Code tool → Creates/modifies files → Returns implementation details

### Informational Request
**User:** "How does fibonacci algorithm work?"

**Agent Decision:** Responds normally → Provides explanation without file modifications

### Mixed Request
**User:** "Explain how sorting works and implement quicksort in Python"

**Agent Decision:** May use Claude Code tool for implementation → Provides both explanation and code files

## Git Integration

When Claude Code makes changes and git is available:

1. **Automatic Branch Creation:** Creates `claude-code-YYYYMMDD_HHMMSS` branch
2. **Commit Changes:** Commits all modifications with descriptive message
3. **Remote Push:** Pushes branch to remote repository (if configured)
4. **PR URL Generation:** Generates GitHub PR creation URL

Example output:
```
📋 **Pull Request Created:**
https://github.com/user/repo/compare/main...claude-code-20241210_143022?quick_pull=1
```

## Error Handling

### Graceful Degradation
- If `praisonaiagents` unavailable → Falls back to `litellm`
- If Claude Code CLI unavailable → Agent works without the tool
- If git unavailable → Code changes without git operations
- Network issues → Continues with local operations

### Error Messages
- Clear error reporting in streaming responses
- Timeout handling for long operations
- Detailed logging for debugging

## Implementation Details

### File Structure
```
src/praisonai/praisonai/
├── cli.py # Added --claudecode flag
└── ui/
└── code.py # Refactored to use praisonaiagents
```

### Key Functions

1. **`handle_with_praisonai_agents()`** - Main agent execution handler
2. **`handle_with_litellm()`** - Fallback for backward compatibility
3. **`claude_code_tool()`** - Claude Code tool implementation
4. **Settings management** - UI toggles and persistence

### Dependencies

**Required:**
- `praisonaiagents` - Core agent framework
- `subprocess` - For Claude Code CLI execution
- `chainlit` - UI framework

**Optional:**
- `tavily` - Web search capability
- `git` - Version control operations
- `claude` CLI - File modification capabilities

## Testing

Use the included test script to verify the integration:

```bash
python test_claude_code_integration.py
```

Tests verify:
- Import functionality
- PraisonAI Agents availability
- Claude Code CLI availability
- Environment variable configuration
- Basic tool execution

## Migration Guide

### From Previous Implementation

The previous implementation used direct Claude Code detection logic. The new implementation:

1. **Replaces detection logic** with agent intelligence
2. **Maintains same UI/CLI interfaces** for easy migration
3. **Preserves all existing functionality** while adding flexibility
4. **Requires no user workflow changes**

### Upgrading

1. Ensure `praisonaiagents` is installed
2. Update PraisonAI to include the refactored code
3. Use `--claudecode` flag or UI toggle as before
4. Existing settings and workflows continue working

## Best Practices

### When to Enable Claude Code
- File modification projects
- Code development workflows
- Repository maintenance tasks
- Implementation and debugging sessions

### When to Disable Claude Code
- Read-only analysis
- Documentation-only sessions
- Learning and educational content
- General Q&A conversations

### Configuration Recommendations
- Use CLI flag for development sessions
- Use UI toggle for mixed workflows
- Set environment variables for CI/CD integration
- Configure repository path for multi-project setups

## Troubleshooting

### Common Issues

**Agent doesn't use Claude Code tool:**
- Verify Claude Code is enabled (`--claudecode` flag or UI toggle)
- Check that request involves file modifications
- Ensure `praisonaiagents` is installed

**Claude Code tool fails:**
- Verify `claude` CLI is installed and in PATH
- Check repository permissions
- Confirm git repository status (if git operations needed)

**Fallback to litellm:**
- Install `praisonaiagents`: `pip install praisonaiagents`
- Check import errors in logs
- Verify Python environment compatibility

### Debug Information

Enable verbose logging:
```bash
export LOGLEVEL=DEBUG
praisonai code --claudecode
```

Check environment:
```python
import os
print("Claude Code enabled:", os.getenv("PRAISONAI_CLAUDECODE_ENABLED"))
print("Repo path:", os.getenv("PRAISONAI_CODE_REPO_PATH"))
```

## Future Enhancements

### Planned Features
- Real-time streaming from PraisonAI Agents
- Enhanced git workflow integration
- Multi-repository support
- Advanced tool coordination
- Performance optimizations

### Extension Points
- Additional coding tools integration
- Custom agent instructions
- Workflow templates
- Integration with other development tools

---

For technical support or questions, please refer to the main PraisonAI documentation or submit issues to the repository.
5 changes: 5 additions & 0 deletions src/praisonai/praisonai/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ def parse_args(self):
parser.add_argument("--call", action="store_true", help="Start the PraisonAI Call server")
parser.add_argument("--public", action="store_true", help="Use ngrok to expose the server publicly (only with --call)")
parser.add_argument("--merge", action="store_true", help="Merge existing agents.yaml with auto-generated agents instead of overwriting")
parser.add_argument("--claudecode", action="store_true", help="Enable Claude Code integration for file modifications and coding tasks")

# If we're in a test environment, parse with empty args to avoid pytest interference
if in_test_env:
Expand All @@ -549,6 +550,10 @@ def parse_args(self):
if args.command == 'code':
args.ui = 'chainlit'
args.code = True

# Handle --claudecode flag for code command
if getattr(args, 'claudecode', False):
os.environ["PRAISONAI_CLAUDECODE_ENABLED"] = "true"
if args.command == 'realtime':
args.realtime = True
if args.command == 'call':
Expand Down
Loading
Loading