# Local AI Agent Integration Guide for Copilot CLI

You now have a fully integrated Copilot CLI + Local Agent system!

## 🔗 Integration Architecture

```
Your Request
    ↓
Copilot CLI (me)
    ↓
MCP Server Bridge
    ↓
Local Agent API (REST)
    ↓
Backend: DeepSeek + Ollama + Ansible
    ↓
Result back to Copilot CLI
```

## 🎯 What You Can Do Now

### 1. **Execute commands through Copilot CLI**
```bash
copilot
> Execute this command on the agent: "ls -la /opt/local-agent"
```
I will use the `local_execute` tool to run it.

### 2. **Ask questions with local DeepSeek**
```bash
copilot
> Ask the local agent's DeepSeek: "Analyze what's consuming the most CPU"
```
I will use the `local_deepseek` tool.

### 3. **Run Ansible playbooks through me**
```bash
copilot
> Deploy using Ansible playbook at /opt/local-agent/config/playbook.yml
```
I will use the `local_ansible` tool.

### 4. **Full workflow coordination**
```bash
copilot
> Plan a system backup, then execute it via the local agent
```
I will:
  1. Create a plan (with /plan command)
  2. Generate Ansible playbook
  3. Send to local agent for execution
  4. Monitor and report results

## 🚀 Getting Started

### Step 1: Start Copilot CLI
```bash
copilot
```

### Step 2: Enable MCP Integration
From within Copilot CLI, use:
```
/mcp
```
Then select or configure the local-agent MCP server.

### Step 3: Test Integration
```
Tell me what services are running on the local agent
```
This will fetch status via the API.

## 📊 Available Tools (via MCP)

### local_execute
Execute shell commands on the local agent
```json
{
  "command": "df -h",
  "timeout": 300,
  "safe_mode": true
}
```

### local_deepseek
Query the local DeepSeek model
```json
{
  "query": "What is causing high memory usage?",
  "model": "deepseek-chat",
  "reasoning": false
}
```

### local_ansible
Run Ansible playbooks
```json
{
  "playbook_path": "/opt/local-agent/config/playbook.yml",
  "extra_vars": {"action": "backup"},
  "check": false
}
```

### local_services
Get service status (ollama, ssh, local-agent)

### local_logs
Get recent agent logs (last 50 lines)

## 🔄 Integration Workflows

### Workflow 1: Diagnosis & Fix
```
You: "The server is slow, diagnose and fix it"
    ↓
Me: Create plan for diagnosis
    ↓
Me: Use local_execute to check processes
    ↓
Me: Query local_deepseek for analysis
    ↓
Me: Generate fix (kill service, scale resources, etc)
    ↓
Me: Execute via local_ansible
    ↓
You: See results
```

### Workflow 2: Code Deploy with Monitoring
```
You: "Deploy the new API version"
    ↓
Me: Pull latest code
    ↓
Me: Create deployment plan
    ↓
Me: Use local_ansible to deploy
    ↓
Me: Use local_execute to health check
    ↓
Me: Report status
```

### Workflow 3: Backup & Recovery
```
You: "Backup database and verify"
    ↓
Me: Create backup playbook
    ↓
Me: Run via local_ansible
    ↓
Me: Query DeepSeek for verification steps
    ↓
Me: Execute verification
    ↓
You: See backup status and location
```

## 🔒 Security Features

✅ **Safety Mode** - Dangerous commands blocked by default
✅ **Timeout Protection** - Commands abort if taking too long
✅ **Audit Trail** - All actions logged to `/opt/local-agent/logs/`
✅ **SSH Keys Only** - No password authentication
✅ **Sudo Restrictions** - Dangerous ops require confirmation

## 📡 API Endpoints (Direct Access)

If you want to use the API directly:

```bash
# Health check
curl http://localhost:8888/health

# Execute command
curl -X POST http://localhost:8888/execute \
  -H "Content-Type: application/json" \
  -d '{"command":"whoami"}'

# Ask DeepSeek
curl -X POST http://localhost:8888/deepseek \
  -H "Content-Type: application/json" \
  -d '{"query":"What is this error?","model":"deepseek-chat"}'

# Run Ansible
curl -X POST http://localhost:8888/ansible \
  -H "Content-Type: application/json" \
  -d '{"playbook_path":"/opt/local-agent/config/playbook.yml"}'

# Get service status
curl http://localhost:8888/services

# Get logs
curl http://localhost:8888/logs?lines=50
```

## 🎮 Example Commands in Copilot CLI

```
# Simple execution
Execute: "uname -a"

# With analysis
Check the CPU and memory usage, then ask DeepSeek if it's normal

# Full workflow
Create a plan to monitor the system for 5 minutes, then diagnose any issues

# Deployment
Deploy the application using Ansible, then verify it's working

# Emergency fix
Something is wrong with the SSH service. Use the agent to diagnose and restart if needed.
```

## ⚙️ Configuration

### Config File
`/opt/local-agent/config/agent.conf`
- Adjust timeouts, models, safety settings

### Playbook
`/opt/local-agent/config/playbook.yml`
- Customize Ansible automation

### Logs
`/opt/local-agent/logs/`
- All operations logged for audit

## 🆘 Troubleshooting

### API not responding
```bash
systemctl status local-agent-api
systemctl restart local-agent-api
```

### MCP not connecting
```bash
# Check MCP config
cat ~/.copilot/mcp-config.json

# Restart Copilot CLI with debug
copilot --debug
```

### Commands blocked
- Check `/opt/local-agent/logs/agent.log` for why
- May need to adjust safety patterns in API server

## 💡 Pro Tips

1. **Use `/plan` in Copilot CLI** before complex operations
2. **Check status** before deployments: local_services tool
3. **Review logs** after operations: local_logs tool
4. **Test with `--check`** on Ansible playbooks first
5. **Use `/context`** to see token usage and what's being sent

## 🎯 Next Steps

1. Start Copilot CLI: `copilot`
2. Use `/mcp` to enable local agent integration
3. Try: "Tell me what services are running"
4. Experiment with complex tasks!

---

**You now have a Copilot CLI+ setup with NO LIMITS and NO QUOTAS!** 🚀
