Spaces:
Sleeping
Sleeping
AI Agent Architecture & Microservices Design
Overview
The AI Agent is built as a modular microservices system with clear separation of concerns. Each component is independently testable and can be extended without affecting others.
Core Components
1. Services Layer (src/services/)
Services are stateless, reusable modules that handle specific functionality.
Gemini Service (gemini.js)
- Purpose: Handle all Google Generative AI interactions
- Methods:
generate(prompt, options)- Single-turn generationchat(message)- Multi-turn conversationanalyzeCode(code, language)- Code analysis with structured outputgenerateCommitMessage(changes)- Create semantic commit messagesclearHistory()- Reset conversation state
Example:
import { geminiService } from './services/gemini.js';
const analysis = await geminiService.analyzeCode(code, 'javascript');
const response = await geminiService.chat("What does this function do?");
GitHub Service (github.js)
- Purpose: GitHub API integration
- Methods:
createIssue(issue)- Create GitHub issuecreatePullRequest(pr)- Create PRgetFileContents(path)- Read file from repolistFiles(path)- List directory contentstriggerWorkflow(workflowId, inputs)- Trigger GitHub ActionsgetRepoInfo()- Repository metadatalistIssues(state)- Get open/closed issuesaddIssueComment(issueNumber, body)- Comment on issue
Example:
import { githubService } from './services/github.js';
const issue = await githubService.createIssue({
title: "Bug: Login fails",
body: "Steps to reproduce...",
labels: ['bug', 'critical']
});
Project Service (project.js)
- Purpose: Project analysis and exploration
- Methods:
getProjectStructure()- Files and directoriesgetFileContent(path)- Read specific filescanForIssues()- Automated issue detectiongetReadme()- README.md contentgetDockerfile()- Dockerfile contentgetSourceFiles()- List programming files
Example:
import { projectService } from './services/project.js';
const issues = await projectService.scanForIssues();
const readme = await projectService.getReadme();
2. Agents Layer (src/agents/)
Agents orchestrate multiple services to accomplish complex tasks.
Scanner Agent (scanner.js)
- Purpose: Periodic project analysis and remediation
- Workflow:
- Get project structure
- Scan for issues
- Analyze critical files with AI
- Generate recommendations
- Create GitHub issues if enabled
Key Methods:
scannerAgent.startPeriodicScanning() // Begin hourly scans
scannerAgent.performScan() // One-time full scan
scannerAgent.getLastScan() // Get last report
Example Flow:
[Scan Triggered]
β
[Get Project Structure] β GitHub Service
β
[Detect Issues] β Project Service
β
[AI Analysis] β Gemini Service
β
[Generate Recommendations] β Gemini Service
β
[Create Issue] β GitHub Service (if enabled)
Chat Agent (chat.js)
- Purpose: Multi-turn conversation management
- Workflow:
- Start conversation session
- Maintain context about project
- Process commands or queries
- Generate AI responses
- Track conversation history
Key Methods:
chatAgent.startConversation(sessionId) // Initialize session
chatAgent.processMessage(sessionId, msg) // Handle user input
chatAgent.getHistory(sessionId) // Get conversation
chatAgent.clearConversation(sessionId) // End session
Commands:
/scan β Trigger project scan
/status β Get repository status
/issues β List open issues
/help β Show available commands
3. API Routes (src/api/)
Express routes that expose services and agents via HTTP.
Chat Routes (chat.js)
POST /api/chat/start # Initialize session
POST /api/chat/message # Send message
GET /api/chat/history/:sessionId # Get conversation
DELETE /api/chat/clear/:sessionId # Clear conversation
GET /api/chat/stats # Chat statistics
Scanner Routes (scanner.js)
POST /api/scanner/scan # Manual scan
GET /api/scanner/last-report # Last report
GET /api/scanner/issues # Current issues
POST /api/scanner/start-continuous # Start auto-scanning
POST /api/scanner/stop-continuous # Stop auto-scanning
Project Routes (project.js)
GET /api/project/structure # Project layout
GET /api/project/file?path=... # File content
GET /api/project/readme # README.md
GET /api/project/dockerfile # Dockerfile
GET /api/project/source-files # Source files list
4. Configuration & Utilities
Environment Config (config/env.js)
- Centralized configuration management
- Validates required variables at startup
- Type-safe environment access
import { config, validateConfig } from './config/env.js';
console.log(config.GEMINI_API_KEY);
validateConfig(); // Throws if missing required vars
Logger (config/logger.js)
- Structured logging with levels
- Timestamp and context support
- Configurable verbosity
import { logger } from './config/logger.js';
logger.info('Something happened', { details: {...} });
logger.error('Failed', { error: err.message });
logger.debug('Debug info', { data });
Task Runner (utils/taskRunner.js)
- Task queue execution with retry logic
- Priority-based ordering
- Timeout management
- Execution history
import { taskRunner } from './utils/taskRunner.js';
taskRunner.addTask({
id: 'analyze-code',
name: 'Analyze codebase',
priority: 10,
execute: async () => { /* task code */ },
retries: 3,
timeout: 30000
});
const results = await taskRunner.runAll();
Data Flow Diagrams
Chat Flow
User Input
β
[Chat API] POST /api/chat/message
β
[Chat Agent] processMessage()
β
Load Project Context? β [Project Service] getProjectStructure()
β
Is Command? (/scan, /status, etc)
ββ Yes β Execute specific handler
β ββ May call GitHub Service
β
ββ No β [Gemini Service] chat()
ββ Multi-turn conversation
β
[Chat Agent] Store in history
β
Return Response to Client
β
Update Chat UI
Scanner Flow
[Timer] Every SCAN_INTERVAL
β
[Scanner Agent] performScan()
β
[Project Service] getProjectStructure()
β
[Project Service] scanForIssues()
β
[Gemini Service] analyzeCode() (per file)
β
[Gemini Service] generateRecommendations()
β
[Create Report]
β
Issues Found + AUTO_FIX enabled?
ββ Yes β [GitHub Service] createIssue()
ββ No β [Store Report]
β
Return Report to Client
Request Flow
Browser Client
β
JavaScript fetch() β API Request
β
[Express Router]
ββ Parse request
ββ Call service/agent
ββ Handle errors
ββ Return JSON response
β
Client Updates UI
Extension Points
Adding a New Scanner
- Create analyzer in
src/agents/scanner.js:
async analyzeProject() {
const sourceFiles = await projectService.getSourceFiles();
const analysis = {
files: [],
overallHealth: 'unknown',
};
for (const file of sourceFiles.slice(0, 5)) {
try {
const content = await projectService.getFileContent(file.path);
// YOUR CUSTOM ANALYSIS HERE
const customAnalysis = await your_analysis(content);
analysis.files.push({
name: file.name,
analysis: customAnalysis,
});
} catch (e) {
logger.warn(`Failed to analyze ${file.name}`);
}
}
return analysis;
}
- Results appear in scan reports and recommendations.
Adding a New Command
- Extend
src/agents/chat.js:
async handleMessage(message, context) {
if (message.startsWith('/mycommand')) {
return this.handleMyCommand(context);
}
// ... existing code
}
async handleMyCommand(context) {
// Your command logic
return "Command result";
}
- Add to
/helptext:
getHelpText() {
return `
/mycommand - Description of what it does
...existing commands...
`;
}
Adding a New API Endpoint
- Create route in
src/api/newfeature.js:
import express from 'express';
import { someService } from '../services/some.js';
const router = express.Router();
router.get('/endpoint', async (req, res) => {
try {
const result = await someService.doSomething();
res.json({ success: true, result });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
export default router;
- Mount in
src/server.js:
import newFeatureRoutes from './api/newfeature.js';
app.use('/api/newfeature', newFeatureRoutes);
Performance Considerations
Caching
- Project structure: 5-minute cache
- File contents: On-demand (no cache)
- API responses: Browser cache (Cache-Control headers)
Optimization
- Analyze only first 5 source files
- Limit file size for AI analysis
- Use pagination for large lists
- Compress API responses
Scaling
- Separate scanner into background job
- Use message queue for long-running tasks
- Cache expensive computations
- Add rate limiting for API endpoints
Testing
Each component can be tested independently:
// Test Gemini Service
const response = await geminiService.analyzeCode(code, 'javascript');
assert(response.issues !== undefined);
// Test GitHub Service
const issue = await githubService.createIssue({
title: 'Test',
body: 'Test body'
});
assert(issue.number !== undefined);
// Test Project Service
const structure = await projectService.getProjectStructure();
assert(structure.files.length > 0);
// Test Chat Agent
const session = chatAgent.startConversation('test-session');
assert(session.sessionId === 'test-session');
Deployment Architecture
βββββββββββββββββββββββββββββββββββββββββββ
β Hugging Face Space (Docker) β
βββββββββββββββββββββββββββββββββββββββββββ€
β βββββββββββββββββββββββββββββββββββββ β
β β Express Server (Port 7860) β β
β βββββββββββββββββββββββββββββββββββββ€ β
β β Static Files (HTML/CSS/JS) β β
β βββββββββββββββββββββββββββββββββββββ€ β
β β API Routes β β
β β ββ /api/chat β β
β β ββ /api/scanner β β
β β ββ /api/project β β
β βββββββββββββββββββββββββββββββββββββ€ β
β β Agents β β
β β ββ Chat Agent (sessions) β β
β β ββ Scanner Agent (periodic) β β
β βββββββββββββββββββββββββββββββββββββ€ β
β β Services β β
β β ββ Gemini Service β β
β β ββ GitHub Service β β
β β ββ Project Service β β
β βββββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββ
β
βββ [Google Gemini API]
βββ [GitHub API]
βββ [Hugging Face API]
Security Best Practices
- Environment Variables: Never log or expose API keys
- Input Validation: Sanitize all user inputs
- Error Handling: Don't expose stack traces in production
- Rate Limiting: Add per-IP or per-session limits
- Token Rotation: Regularly refresh API tokens
- Logging: Log security-relevant events
Architecture Version: 1.0.0 Last Updated: November 17, 2024