Ai Integration
Comprehensive guidelines for AI-assisted development within the Editor platform
AI Integration Documentation
Overview
This document provides information about the AI integration system, including setup, configuration, troubleshooting, and maintenance procedures for both OpenAI and Anthropic APIs.
๐ API Keys & Configuration
Storage Locations
- Keys are stored in IndexedDB via
AIConfigService - Backed up in local storage for redundancy
- Configuration persisted in
gograb-ai-chat-configsdatabase
Security Best Practices
- Never store API keys in code or documentation
- Use environment variables for production deployments
- Store keys securely in IndexedDB for development
- Rotate keys regularly for security
๐ Proxy Server Architecture
Anthropic Proxy Server
- Location:
/anthropic-proxy/proxy-server.js - Port: 3001
- Endpoint:
http://localhost:3001/api/anthropic/messages - Start Command:
cd anthropic-proxy && node proxy-server.js
Proxy Server Features
- Custom HTTPS request implementation (not http-proxy-middleware)
- Header mapping:
x-api-keyโx-anthropic-api-key - Comprehensive logging and debugging
- Direct API calls to Anthropic's endpoint
OpenAI Proxy
- Uses Angular dev server proxy configuration
- Configured in
apps/ux-test/proxy.conf.json - Routes
/api/openai/*to OpenAI API
๐ง Service Configuration
Key Services
- AIFactoryService: Main service coordinator
- AnthropicService: Handles Anthropic API calls
- OpenAIService: Handles OpenAI API calls
- AIConfigService: Manages API keys and configuration
Critical Configuration Points
Anthropic Service (libs/gograb-ai/src/lib/services/anthropic.service.ts)
// Message role mapping - CRITICAL
role: msg.role === MessageRole.USER ? 'user' : 'assistant'
AI Factory Service (libs/gograb-ai/src/lib/services/ai-factory.service.ts)
// Model routing - CRITICAL
const service = this.getService(currentProvider); // NOT getBestAvailableService()
Proxy Configuration (apps/ux-test/proxy.conf.json)
{
"/api/anthropic/**": {
"target": "http://localhost:3001",
"secure": false,
"changeOrigin": true,
"logLevel": "debug"
}
}
๐จ Known Issues & Solutions
Issue 1: 404 Errors on Anthropic API
Cause: Anthropic proxy server not running Solution: Start proxy server on port 3001
cd anthropic-proxy && node proxy-server.js
Issue 2: Model Selection Routing Problems
Cause: Using getBestAvailableService() instead of getService(provider)
Solution: Ensure sendMessage() uses the correct service selection
const service = this.getService(currentProvider);
Issue 3: 400 Bad Request - Role Mapping
Cause: Sending enum values instead of strings for message roles Solution: Map enum to string values
role: msg.role === MessageRole.USER ? 'user' : 'assistant'
Issue 4: CORS Errors
Cause: Direct API calls from browser Solution: Use proxy servers for all API calls
Issue 5: Connection Timeouts
Cause: Startup connection tests blocking initialization Solution: Skip startup tests, test on first message
// Skip startup connection tests - will test when first message is sent
๐โโ๏ธ Startup Procedures
Development Environment Setup
- Start Anthropic Proxy:
cd anthropic-proxy && node proxy-server.js - Start Angular Dev Server:
nx serve ux-test(port 5300) - Verify Proxy: Check
http://localhost:3001/api/anthropic/messages
Port Configuration
- ux-test: 5300 (
nx serve ux-test) - editor-web: 5200 (
npx nx serve editor-web) - notes: 5010 (
nx serve docs-angular) - Anthropic Proxy: 3001
๐งช Testing Procedures
Manual API Testing
# Test Anthropic API via proxy (use your own API key)
curl -X POST http://localhost:3001/api/anthropic/messages \
-H "Content-Type: application/json" \
-H "x-api-key: YOUR_ANTHROPIC_API_KEY" \
-d '{
"model": "claude-3-haiku-20240307",
"max_tokens": 50,
"messages": [{"role": "user", "content": "Hello"}]
}'
Component Testing
- Use AI Tester component at
/ai-tester - Test both OpenAI and Anthropic connections
- Verify model selection and message routing
๐ Debugging Procedures
Console Logging
Key services provide comprehensive logging:
AIFactoryService: Service selection and initializationAnthropicService: API requests and responsesOpenAIService: API requests and responsesAIConfigService: API key management
Common Debug Checks
- Proxy Server Status: Check if running on port 3001
- API Keys: Verify keys are present and valid
- Model Selection: Ensure correct provider is selected
- Message Format: Verify role mapping is correct
- Network: Check proxy configuration and CORS
๐ฆ Dependencies
Required Packages
@angular/common/http: HTTP clientrxjs: Reactive programmingdexie: IndexedDB wrapper (for configuration storage)
Custom Implementations
- Custom HTTPS request for Anthropic proxy
- IndexedDB configuration storage
- Message role mapping utilities
๐ ๏ธ Maintenance Procedures
Regular Checks
- API Key Rotation: Update keys in
AIConfigService - Proxy Server: Ensure running and responding
- Database Cleanup: Monitor IndexedDB storage
- Log Review: Check for error patterns
Backup Procedures
- API keys stored in multiple locations
- Configuration backed up to IndexedDB
- Service settings preserved in local storage
๐ Success Indicators
Healthy System Logs
โ
AI Factory initialized - ready for requests
โ
OpenAI API response received via proxy
โ
ANTHROPIC SUCCESS: {response data}
โ
Chat configuration loaded successfully
Performance Metrics
- API response time < 2 seconds
- Proxy server uptime > 99%
- Message delivery success rate > 95%
๐ Recovery Procedures
If Anthropic API Fails
- Restart proxy server:
cd anthropic-proxy && node proxy-server.js - Check API key validity
- Verify proxy configuration
- Test with curl command
If OpenAI API Fails
- Check Angular dev server proxy
- Verify API key in storage
- Restart Angular dev server if needed
- Test with direct API call
If Both APIs Fail
- Restart all services
- Clear IndexedDB storage
- Re-initialize API keys
- Check network connectivity
Production Deployment
Unified System Integration
In production mode (port 9200), the AI integration is handled differently:
- Integrated AI Proxy: Production server includes built-in Anthropic proxy
- No External Proxy Needed: The proxy is integrated into the production server
- Environment Variables: Use Railway environment variables for API keys
- Health Monitoring: AI proxy status included in unified status API
Railway Environment Variables
Set these environment variables in Railway deployment:
OPENAI_API_KEY=your-openai-key-here
ANTHROPIC_API_KEY=your-anthropic-key-here
API Endpoints
Development (Port 9100)
- External proxy on port 3001 (if available)
Production (Port 9200)
- Integrated proxy:
/api/anthropic/messages - Health check:
/api/anthropic/health
โ ๏ธ SECURITY NOTE: Never commit API keys to version control. Always use environment variables or secure storage mechanisms.
๐ก TIP: Always test both APIs after any changes to ensure continued functionality.
Last Updated: 2025-01-12 This guide covers both development and production AI integration setups.