appimepp/OpenSIN-Code-Security
0
1const express = require('express');2const { exec } = require('child_process');3const path = require('path');4const fs = require('fs');5const http = require('http');6 7const app = express();8const PORT = process.env.PORT || 7860;9const AGENT_NAME = process.env.AGENT_NAME || process.env.SPACE_ID?.split('/')[1] || 'SIN-Agent';10const AGENT_DESCRIPTION = process.env.AGENT_DESCRIPTION || `SIN A2A Agent: ${AGENT_NAME}`;11 12app.use(express.json());13app.use(express.static(path.join(__dirname, 'public')));14 15// Agent registry16const agentRegistry = {17 'sin-coding-ceo': { role: 'Coding Team Lead', capabilities: ['code-review', 'task-delegation', 'architecture'] },18 'sin-hermes': { role: 'Dispatcher', capabilities: ['dispatch', 'routing', 'load-balancing'] },19 'sin-zeus': { role: 'Control Plane', capabilities: ['orchestration', 'monitoring', 'governance'] },20 'sin-simone-mcp': { role: 'MCP Server', capabilities: ['lsp', 'semantic-analysis', 'code-refactoring'] },21 'sin-authenticator': { role: 'Auth Manager', capabilities: ['oauth', 'token-rotation', 'session-management'] },22 'sin-google-apps': { role: 'Google Apps Integration', capabilities: ['docs', 'sheets', 'drive', 'gmail'] },23 'sin-supabase': { role: 'Database Manager', capabilities: ['queries', 'migrations', 'backups'] },24 'sin-server': { role: 'Backend Server', capabilities: ['api', 'websocket', 'middleware'] },25 'sin-passwordmanager': { role: 'Password Manager', capabilities: ['store', 'retrieve', 'rotate'] },26 'sin-team-coding': { role: 'Coding Team', capabilities: ['frontend', 'backend', 'testing'] },27};28 29// Health endpoint30app.get('/health', (req, res) => {31 const agentInfo = agentRegistry[AGENT_NAME] || { role: 'A2A Agent', capabilities: ['chat', 'task'] };32 res.json({33 status: 'ok',34 agent: AGENT_NAME,35 role: agentInfo.role,36 description: AGENT_DESCRIPTION,37 timestamp: new Date().toISOString(),38 uptime: process.uptime(),39 version: '3.0.0',40 capabilities: agentInfo.capabilities,41 memory: process.memoryUsage(),42 pid: process.pid43 });44});45 46// A2A Card endpoint47app.get('/.well-known/agent-card.json', (req, res) => {48 const cardPath = path.join(__dirname, '.well-known', 'agent-card.json');49 if (fs.existsSync(cardPath)) {50 const card = JSON.parse(fs.readFileSync(cardPath, 'utf8'));51 card.version = '3.0.0';52 card.name = AGENT_NAME;53 res.json(card);54 } else {55 res.json({56 name: AGENT_NAME,57 description: AGENT_DESCRIPTION,58 url: `https://${process.env.SPACE_HOST || 'localhost'}/a2a/v1`,59 protocol: 'A2A',60 version: '3.0.0',61 capabilities: ['chat', 'task', 'help', 'status', 'execute', 'list'],62 status: 'active'63 });64 }65});66 67// A2A v1 endpoint - REAL AGENT LOGIC68app.post('/a2a/v1', async (req, res) => {69 try {70 const { action, params } = req.body;71 72 switch (action) {73 case 'agent.help':74 res.json({75 result: {76 name: AGENT_NAME,77 description: AGENT_DESCRIPTION,78 version: '3.0.0',79 commands: [80 { name: 'agent.help', description: 'Show available commands' },81 { name: 'agent.health', description: 'Check agent health' },82 { name: 'agent.status', description: 'Show agent status' },83 { name: 'agent.execute', description: 'Execute a command', params: { command: 'string' } },84 { name: 'agent.list', description: 'List all agents in fleet' }85 ]86 }87 });88 break;89 90 case 'agent.health':91 res.json({92 result: {93 status: 'ok',94 agent: AGENT_NAME,95 uptime: process.uptime(),96 memory: process.memoryUsage(),97 timestamp: new Date().toISOString()98 }99 });100 break;101 102 case 'agent.status':103 res.json({104 result: {105 agent: AGENT_NAME,106 status: 'active',107 version: '3.0.0',108 capabilities: ['chat', 'task', 'help', 'status', 'execute', 'list'],109 uptime: process.uptime()110 }111 });112 break;113 114 case 'agent.list':115 res.json({116 result: {117 fleet: Object.keys(agentRegistry).map(name => ({118 name,119 ...agentRegistry[name]120 })),121 total: Object.keys(agentRegistry).length122 }123 });124 break;125 126 case 'agent.execute':127 if (!params || !params.command) {128 res.json({ error: 'Missing command parameter', usage: 'agent.execute({ command: "ls -la" })' });129 break;130 }131 exec(params.command, { timeout: 30000 }, (error, stdout, stderr) => {132 res.json({133 result: {134 command: params.command,135 stdout: stdout || '',136 stderr: stderr || '',137 error: error ? error.message : null138 }139 });140 });141 break;142 143 default:144 res.json({145 result: {146 message: `Unknown action: ${action}. Use 'agent.help' for available commands.`,147 agent: AGENT_NAME,148 available_actions: ['agent.help', 'agent.health', 'agent.status', 'agent.execute', 'agent.list']149 }150 });151 }152 } catch (error) {153 res.status(500).json({154 error: error.message,155 agent: AGENT_NAME156 });157 }158});159 160// A2A GET endpoint161app.get('/a2a/v1', (req, res) => {162 res.json({163 name: AGENT_NAME,164 description: AGENT_DESCRIPTION,165 protocol: 'A2A',166 version: '3.0.0',167 endpoints: {168 post: '/a2a/v1',169 card: '/.well-known/agent-card.json',170 health: '/health'171 },172 capabilities: ['chat', 'task', 'help', 'status', 'execute', 'list']173 });174});175 176// Root endpoint177app.get('/', (req, res) => {178 res.json({179 name: AGENT_NAME,180 description: AGENT_DESCRIPTION,181 status: 'active',182 version: '3.0.0',183 endpoints: {184 health: '/health',185 a2a: '/a2a/v1',186 card: '/.well-known/agent-card.json'187 },188 capabilities: ['chat', 'task', 'help', 'status', 'execute', 'list']189 });190});191 192// Start server193app.listen(PORT, '0.0.0.0', () => {194 console.log(`${AGENT_NAME} v3.0.0 listening on port ${PORT}`);195 console.log(`Health: http://localhost:${PORT}/health`);196 console.log(`A2A: http://localhost:${PORT}/a2a/v1`);197 console.log(`Capabilities: chat, task, help, status, execute, list`);198});199 