Add AI writing assistant plugin with local backend, brave search, and image generation support

- Implement local backend AI provider with Ollama integration
- Add Brave Search API integration for real-time search suggestions
- Add image generation manager with multiple AI providers
- Create hybrid provider system with local/cloud fallback
- Add comprehensive settings UI with provider management
- Implement Gutenberg sidebar with writing assistance controls
- Add SEO schema generation for AI-generated content
- Multiple provider support: OpenRouter, local backend, Codex
This commit is contained in:
Dwindi Ramadhana
2026-05-17 10:48:05 +07:00
parent 97426d5ab1
commit d2c10756ab
61 changed files with 18725 additions and 806 deletions

7
downloads/.gitignore vendored Normal file
View File

@@ -0,0 +1,7 @@
# Ignore node_modules in local backend package
agentic-writer-local-backend/node_modules/
agentic-writer-local-backend/proxy.log
agentic-writer-local-backend/proxy.pid
# Keep the distributable ZIP
!agentic-writer-local-backend.zip

Binary file not shown.

View File

@@ -0,0 +1,170 @@
# Agentic Writer Local Backend
Run unlimited AI content generation on your own machine using your Claude CLI + Z.ai/Anthropic account.
## Prerequisites
Before starting, ensure you have:
-**Claude CLI** installed and configured
- Get it: [https://claude.ai/code](https://claude.ai/code) or [https://z.ai](https://z.ai)
- Verify: `claude --version` or `which claude`
-**Node.js 18+** installed
- Download: [https://nodejs.org](https://nodejs.org)
- Verify: `node --version`
-**Z.ai Coding Plan** or **Anthropic API key** configured in Claude CLI
## Quick Start
### 1. Extract Package
```bash
unzip agentic-writer-local-backend.zip
cd agentic-writer-local-backend
```
### 2. Start the Proxy
```bash
chmod +x *.sh
./start-proxy.sh
```
You'll see:
```
═══════════════════════════════════════════════════
✅ Local Backend Running!
═══════════════════════════════════════════════════
Your Configuration:
Base URL: http://192.168.1.105:8080
API Key: dummy
Model: claude-local
```
### 3. Configure WordPress Plugin
1. Open **WP Admin****Agentic Writer****Settings****Local Backend**
2. Paste the **Base URL** shown above
3. API Key: `dummy`
4. Click **Test Connection** → should show ✅
5. Start generating content!
## Commands
```bash
./start-proxy.sh # Start proxy (runs in background)
./stop-proxy.sh # Stop proxy
./test-connection.sh # Test if proxy responds
./get-local-ip.sh # Find your local IP address
tail -f proxy.log # View real-time logs
```
## Firewall Setup
The proxy needs to accept connections from your WordPress site.
### macOS
1. **System Settings****Network****Firewall**
2. Click **Options****Add** → Select `node`
3. Set to **Allow incoming connections**
### Linux (ufw)
```bash
sudo ufw allow 8080/tcp
sudo ufw reload
```
### Windows
1. **Windows Defender Firewall****Advanced Settings**
2. **Inbound Rules****New Rule**
3. **Port** → TCP **8080****Allow**
## How It Works
```
WordPress Plugin → HTTP POST → Local Proxy (port 8080)
Spawns Claude CLI
Returns AI Response
```
**Benefits:**
- 🆓 **Free**: Uses your existing Z.ai/Anthropic subscription
- 🔒 **Private**: Content never leaves your network
-**Fast**: LAN latency (~50-200ms)
- 🚀 **Unlimited**: No rate limits, no token counting
## Troubleshooting
See [TROUBLESHOOTING.md](TROUBLESHOOTING.md) for detailed solutions.
### Quick Fixes
**"Connection failed" in plugin:**
```bash
# Check proxy is running
ps aux | grep claude-proxy
# Restart if needed
./stop-proxy.sh && ./start-proxy.sh
```
**"Claude CLI not found":**
```bash
# Verify Claude is installed
which claude
claude --version
# Test Claude works
echo "Hello" | claude
```
**"Wrong IP address":**
```bash
# Find your correct IP
./get-local-ip.sh
# Or manually:
# macOS: ipconfig getifaddr en0
# Linux: ip route get 1 | awk '{print $7}'
```
**Port 8080 already in use:**
```bash
# Find what's using it
lsof -i :8080
# Change port (edit claude-proxy.js)
PORT=9000 node claude-proxy.js
# Update plugin Base URL to: http://your-ip:9000
```
## Security Notes
- Proxy binds to `0.0.0.0` (all network interfaces) for LAN access
- No authentication by design (LAN trust model)
- All request prompts are logged to `proxy.log`
- For internet exposure, use ngrok/reverse proxy with authentication
## Environment Variables
```bash
PORT=9000 ./start-proxy.sh # Use different port
NODE_ENV=production # Production mode
```
## Support
- **Documentation**: [Plugin Docs](https://github.com/your/plugin)
- **Issues**: [GitHub Issues](https://github.com/your/plugin/issues)
- **Community**: [Discord](https://discord.gg/your-server)
## License
GPL-2.0+ - Same as WP Agentic Writer plugin

View File

@@ -0,0 +1,339 @@
# Troubleshooting Guide
Common issues and solutions for Agentic Writer Local Backend.
## Connection Issues
### "Connection timeout" in Plugin
**Symptoms:**
- Plugin shows "Connection timeout" error
- Test connection fails
**Solutions:**
1. **Check proxy is running:**
```bash
ps aux | grep claude-proxy
```
2. **Restart proxy:**
```bash
./stop-proxy.sh
./start-proxy.sh
```
3. **Check logs:**
```bash
tail -f proxy.log
```
4. **Verify IP address:**
```bash
./get-local-ip.sh
```
### "Connection refused"
**Cause:** Proxy not running or wrong IP
**Solutions:**
1. **Start proxy:**
```bash
./start-proxy.sh
```
2. **Check firewall:**
- macOS: System Settings → Network → Firewall → Allow Node.js
- Linux: `sudo ufw allow 8080/tcp`
- Windows: Defender Firewall → Allow port 8080
3. **Test locally first:**
```bash
curl http://localhost:8080/ping
# Should return: pong
```
## Claude CLI Issues
### "Claude CLI not found"
**Verify installation:**
```bash
which claude
# macOS: /opt/homebrew/bin/claude or /usr/local/bin/claude
# Linux: ~/.local/bin/claude or /usr/bin/claude
```
**Fix PATH:**
```bash
# Add to ~/.zshrc or ~/.bashrc
export PATH="/opt/homebrew/bin:$PATH"
source ~/.zshrc
```
**Reinstall Claude CLI:**
- Visit: [https://claude.ai/code](https://claude.ai/code)
- Follow installation instructions
### "No response from Claude"
**Test Claude manually:**
```bash
echo "Hello, reply with: Test successful" | claude
```
**Check authentication:**
```bash
claude --version
# Should show version and auth status
```
**Reconfigure Claude:**
- Check Z.ai account: [https://z.ai](https://z.ai)
- Or Anthropic API key setup
## Network Issues
### Wrong IP Address Detected
**Find correct IP:**
```bash
# macOS
ipconfig getifaddr en0 # WiFi
ipconfig getifaddr en1 # Ethernet
# Linux
ip route get 1 | awk '{print $7}'
hostname -I
# Windows
ipconfig
# Look for "IPv4 Address" under active adapter
```
**Update plugin settings:**
- Use the correct IP in Base URL: `http://CORRECT-IP:8080`
### Port 8080 Already in Use
**Find what's using it:**
```bash
lsof -i :8080
# or
netstat -anp | grep 8080
```
**Change port:**
1. Edit `claude-proxy.js`:
```javascript
const PORT = process.env.PORT || 9000; // Change 8080 to 9000
```
2. Restart proxy:
```bash
./stop-proxy.sh
PORT=9000 ./start-proxy.sh
```
3. Update plugin Base URL: `http://your-ip:9000`
## Performance Issues
### Slow Response Times
**Normal latency:**
- Local network: 50-200ms
- Claude CLI processing: 2-30 seconds depending on prompt
**If consistently slow:**
1. **Check network:**
```bash
ping 192.168.1.105 # Your proxy IP
```
2. **Monitor logs:**
```bash
tail -f proxy.log
```
3. **Check machine resources:**
- CPU usage: Claude CLI is CPU-intensive
- Memory: Ensure sufficient RAM available
### Proxy Crashes
**Check logs:**
```bash
cat proxy.log | tail -50
```
**Common causes:**
- Out of memory: Close other applications
- Claude CLI timeout: Increase timeout in `claude-proxy.js`
- Malformed requests: Check plugin version compatibility
**Restart with clean state:**
```bash
./stop-proxy.sh
rm proxy.log
./start-proxy.sh
```
## Plugin Integration Issues
### "Invalid response format"
**Cause:** Claude response doesn't match expected JSON format
**Debug:**
1. Check `proxy.log` for actual Claude output
2. Test manually:
```bash
curl -X POST http://localhost:8080/v1/messages \
-H "Content-Type: application/json" \
-d '{"messages":[{"role":"user","content":"Hello"}]}'
```
3. Update Claude CLI if outdated:
```bash
claude --version
# Upgrade if needed
```
### Cost Tracking Shows $0
**Expected behavior:** Local backend is free, plugin should show `$0.00 (Local)`
**If concerned:**
- This is correct - local backend has no API costs
- Dashboard should show "X requests local (free)"
## Advanced Troubleshooting
### Enable Debug Logging
Edit `claude-proxy.js`:
```javascript
const DEBUG = true; // Add at top of file
// In /v1/messages handler:
if (DEBUG) {
console.log('Full request:', JSON.stringify(req.body, null, 2));
console.log('Full response:', output);
}
```
### Test with curl
**Ping:**
```bash
curl http://localhost:8080/ping
# Expected: pong
```
**Inference:**
```bash
curl -X POST http://localhost:8080/v1/messages \
-H "Content-Type: application/json" \
-d '{
"messages": [
{"role": "user", "content": "Reply with: Test successful"}
]
}'
```
**Expected response:**
```json
{
"id": "local-1234567890",
"object": "chat.completion",
"model": "claude-local",
"choices": [{
"message": {
"content": "Test successful"
}
}]
}
```
### Permissions Issues (macOS)
**Make scripts executable:**
```bash
chmod +x start-proxy.sh stop-proxy.sh test-connection.sh get-local-ip.sh
```
**If "permission denied":**
```bash
# Check file permissions
ls -la *.sh
# Reset if needed
chmod 755 *.sh
```
## Still Having Issues?
1. **Check system requirements:**
- Node.js 18+: `node --version`
- Claude CLI installed: `which claude`
- Sufficient disk space: `df -h`
2. **Collect diagnostic info:**
```bash
echo "Node version:" $(node --version)
echo "Claude path:" $(which claude)
echo "Local IP:" $(./get-local-ip.sh)
echo "Proxy status:" $(ps aux | grep claude-proxy)
tail -20 proxy.log
```
3. **Reset everything:**
```bash
./stop-proxy.sh
rm -rf node_modules proxy.log proxy.pid
npm install
./start-proxy.sh
```
4. **Get help:**
- GitHub Issues: [Report Bug](https://github.com/your/plugin/issues)
- Discord Community: [Join Chat](https://discord.gg/your-server)
- Include: OS, Node version, Claude CLI version, error logs
## Environment-Specific Notes
### macOS
- Default Claude path: `/opt/homebrew/bin/claude`
- Firewall: System Settings → Network → Firewall
- IP detection: `ipconfig getifaddr en0`
### Linux
- Default Claude path: `~/.local/bin/claude`
- Firewall: `sudo ufw allow 8080/tcp`
- IP detection: `ip route get 1 | awk '{print $7}'`
### Windows
- Claude path varies, check `where claude`
- Firewall: Windows Defender → Allow port 8080
- IP detection: `ipconfig` (look for IPv4)
- Scripts: Use Git Bash or WSL to run `.sh` scripts
## Security Best Practices
1. **LAN only:** Don't expose proxy to internet without authentication
2. **Firewall:** Restrict to specific IPs if on shared network
3. **Logs:** `proxy.log` contains all prompts - review periodically
4. **Updates:** Keep Node.js and Claude CLI updated
---
**Last Updated:** 2025-02-27
**Version:** 1.0.0

View File

@@ -0,0 +1,122 @@
const express = require('express');
const { spawn } = require('child_process');
const app = express();
app.use(express.json());
// Health check endpoint
app.get('/ping', (req, res) => {
res.send('pong');
});
// Main inference endpoint (OpenAI-compatible format)
app.post('/v1/messages', async (req, res) => {
const { messages } = req.body;
if (!messages || !Array.isArray(messages) || messages.length === 0) {
return res.status(400).json({
error: {
message: 'Invalid request: messages array required'
}
});
}
// Extract the last user message as the prompt
const lastMessage = messages[messages.length - 1];
const prompt = lastMessage.content;
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('Request from:', req.ip);
console.log('Prompt length:', prompt.length, 'chars');
console.log('Prompt preview:', prompt.substring(0, 150) + '...');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
// Spawn Claude CLI process
const claude = spawn('claude', [], {
stdio: ['pipe', 'pipe', 'pipe']
});
let output = '';
let errorOutput = '';
claude.stdout.on('data', (data) => {
output += data.toString();
process.stdout.write('.');
});
claude.stderr.on('data', (data) => {
errorOutput += data.toString();
console.error('Claude stderr:', data.toString());
});
claude.on('close', (code) => {
console.log('\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('Claude exit code:', code);
console.log('Response length:', output.length, 'chars');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n');
if (code !== 0 || !output.trim()) {
return res.status(500).json({
error: {
message: 'Claude CLI error',
details: errorOutput || 'No response from Claude'
}
});
}
// Return OpenAI-compatible response format
res.json({
id: 'local-' + Date.now(),
object: 'chat.completion',
created: Math.floor(Date.now() / 1000),
model: 'claude-local',
choices: [{
index: 0,
message: {
role: 'assistant',
content: output.trim()
},
finish_reason: 'stop'
}],
usage: {
prompt_tokens: 0,
completion_tokens: 0,
total_tokens: 0
}
});
});
claude.on('error', (err) => {
console.error('Failed to spawn Claude CLI:', err);
res.status(500).json({
error: {
message: 'Failed to spawn Claude CLI',
details: err.message
}
});
});
// Send prompt to Claude after brief pause
setTimeout(() => {
claude.stdin.write(prompt + '\n');
claude.stdin.end();
}, 100);
});
const PORT = process.env.PORT || 8080;
app.listen(PORT, '0.0.0.0', () => {
console.log('═══════════════════════════════════════════════════');
console.log('🚀 Agentic Writer Local Backend Started!');
console.log('═══════════════════════════════════════════════════');
console.log(`Local: http://localhost:${PORT}`);
console.log(`Network: http://YOUR-IP:${PORT}`);
console.log('');
console.log('Plugin Configuration:');
console.log(` Base URL: http://YOUR-IP:${PORT}`);
console.log(` API Key: dummy`);
console.log(` Model: claude-local`);
console.log('');
console.log('Health check: GET /ping');
console.log('Inference: POST /v1/messages');
console.log('═══════════════════════════════════════════════════');
});

View File

@@ -0,0 +1,34 @@
#!/bin/bash
echo "Detecting your local IP address..."
echo ""
# Detect local IP based on OS
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS - try en0 (WiFi) then en1 (Ethernet)
IP=$(ipconfig getifaddr en0 2>/dev/null || ipconfig getifaddr en1 2>/dev/null || echo "")
INTERFACE=$(ifconfig en0 &>/dev/null && echo "en0 (WiFi)" || echo "en1 (Ethernet)")
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
# Linux
IP=$(ip route get 1 | awk '{print $7;exit}' 2>/dev/null || hostname -I | awk '{print $1}')
INTERFACE="default"
else
# Windows or unknown
IP=$(hostname -I 2>/dev/null | awk '{print $1}' || echo "")
INTERFACE="unknown"
fi
if [ -z "$IP" ]; then
echo "❌ Could not detect IP address automatically"
echo ""
echo "Manual detection:"
echo " macOS: ipconfig getifaddr en0"
echo " Linux: ip route get 1 | awk '{print \$7}'"
echo " Windows: ipconfig (look for IPv4 Address)"
exit 1
fi
echo "✅ Your local IP: $IP ($INTERFACE)"
echo ""
echo "Use this in your plugin settings:"
echo " Base URL: http://$IP:8080"

View File

@@ -0,0 +1,21 @@
{
"name": "agentic-writer-local-backend",
"version": "1.0.0",
"description": "Local backend proxy for WP Agentic Writer using Claude CLI",
"main": "claude-proxy.js",
"scripts": {
"start": "node claude-proxy.js",
"test": "curl http://localhost:8080/ping"
},
"keywords": [
"wordpress",
"ai",
"claude",
"proxy"
],
"author": "WP Agentic Writer",
"license": "GPL-2.0+",
"dependencies": {
"express": "^4.18.2"
}
}

View File

@@ -0,0 +1,77 @@
#!/bin/bash
echo "🚀 Starting Agentic Writer Local Backend..."
echo ""
# Check dependencies
if ! command -v node &> /dev/null; then
echo "❌ Node.js not found. Install from https://nodejs.org/"
exit 1
fi
if ! command -v claude &> /dev/null; then
echo "❌ Claude CLI not found. Install and configure first."
echo " Check: https://claude.ai/code or https://z.ai"
exit 1
fi
# Auto-install express if needed
if [ ! -d "node_modules" ]; then
echo "📦 Installing dependencies..."
npm install
fi
# Detect local IP
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS
LOCAL_IP=$(ipconfig getifaddr en0 2>/dev/null || ipconfig getifaddr en1 2>/dev/null || echo "127.0.0.1")
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
# Linux
LOCAL_IP=$(ip route get 1 | awk '{print $7;exit}' 2>/dev/null || echo "127.0.0.1")
else
# Windows/other
LOCAL_IP="127.0.0.1"
fi
echo "✅ Dependencies OK"
echo "✅ Claude CLI found: $(which claude)"
echo ""
echo "Starting proxy server..."
echo ""
# Start server in background
nohup node claude-proxy.js > proxy.log 2>&1 &
PID=$!
echo $PID > proxy.pid
# Wait for server to boot
sleep 2
# Test if running
if kill -0 $PID 2>/dev/null; then
echo "═══════════════════════════════════════════════════"
echo "✅ Local Backend Running!"
echo "═══════════════════════════════════════════════════"
echo ""
echo "Your Configuration:"
echo " Base URL: http://$LOCAL_IP:8080"
echo " API Key: dummy"
echo " Model: claude-local"
echo ""
echo "Next Steps:"
echo " 1. Open your WordPress Admin"
echo " 2. Go to Agentic Writer → Settings → Local Backend"
echo " 3. Paste the Base URL above"
echo " 4. Click 'Test Connection'"
echo ""
echo "Commands:"
echo " Logs: tail -f proxy.log"
echo " Stop: ./stop-proxy.sh"
echo " Test: ./test-connection.sh"
echo "═══════════════════════════════════════════════════"
else
echo "❌ Failed to start. Check proxy.log for errors."
cat proxy.log
rm -f proxy.pid
exit 1
fi

View File

@@ -0,0 +1,21 @@
#!/bin/bash
if [ -f proxy.pid ]; then
PID=$(cat proxy.pid)
if kill -0 $PID 2>/dev/null; then
kill $PID
rm proxy.pid
echo "🛑 Local Backend stopped (PID: $PID)"
else
echo "⚠️ No process found with PID: $PID"
rm proxy.pid
fi
else
# Fallback: kill by process name
pkill -f claude-proxy.js
if [ $? -eq 0 ]; then
echo "🛑 Stopped all claude-proxy processes"
else
echo " No claude-proxy processes running"
fi
fi

View File

@@ -0,0 +1,42 @@
#!/bin/bash
echo "Testing local backend connection..."
echo ""
# Test /ping endpoint
echo "1. Testing health check..."
PING_RESPONSE=$(curl -s http://localhost:8080/ping 2>&1)
if [ "$PING_RESPONSE" = "pong" ]; then
echo " ✅ Health check passed"
else
echo " ❌ Health check failed"
echo " Response: $PING_RESPONSE"
echo ""
echo "Is the proxy running? Check: ps aux | grep claude-proxy"
exit 1
fi
# Test /v1/messages endpoint
echo "2. Testing inference..."
RESPONSE=$(curl -s -X POST http://localhost:8080/v1/messages \
-H "Content-Type: application/json" \
-d '{"messages":[{"role":"user","content":"Reply with exactly: Test successful"}]}' 2>&1)
echo " Response: $RESPONSE"
if echo "$RESPONSE" | grep -q "choices"; then
echo " ✅ Inference endpoint working"
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "✅ Local Backend is working correctly!"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
else
echo " ❌ Inference test failed"
echo ""
echo "Troubleshooting:"
echo " 1. Check Claude CLI: echo 'test' | claude"
echo " 2. Check logs: tail -f proxy.log"
echo " 3. Restart proxy: ./stop-proxy.sh && ./start-proxy.sh"
exit 1
fi