CoolFace
Apppublic

OpenJerro/A2A-SIN-IRC

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
server.js134 linesDownload Raw Back to root
1const express = require('express');2const path = require('path');3const fs = require('fs');4 5const app = express();6const PORT = process.env.PORT || 7860;7 8// Agent name from env or default9const AGENT_NAME = process.env.AGENT_NAME || 'SIN-Agent';10const AGENT_DESCRIPTION = process.env.AGENT_DESCRIPTION || 'SIN A2A Agent';11 12// Middleware13app.use(express.json());14app.use(express.static(path.join(__dirname, 'public')));15 16// Health endpoint17app.get('/health', (req, res) => {18  res.json({19    status: 'ok',20    agent: AGENT_NAME,21    timestamp: new Date().toISOString(),22    uptime: process.uptime(),23    version: '1.0.0'24  });25});26 27// A2A Card endpoint28app.get('/.well-known/agent-card.json', (req, res) => {29  const cardPath = path.join(__dirname, '.well-known', 'agent-card.json');30  if (fs.existsSync(cardPath)) {31    res.json(JSON.parse(fs.readFileSync(cardPath, 'utf8')));32  } else {33    res.json({34      name: AGENT_NAME,35      description: AGENT_DESCRIPTION,36      url: `https://${process.env.SPACE_HOST || 'localhost'}/a2a/v1`,37      protocol: 'A2A',38      version: '1.0.0',39      capabilities: ['chat', 'task'],40      status: 'active'41    });42  }43});44 45// A2A v1 endpoint46app.post('/a2a/v1', async (req, res) => {47  try {48    const { action, params } = req.body;49    50    switch (action) {51      case 'agent.help':52        res.json({53          result: {54            name: AGENT_NAME,55            description: AGENT_DESCRIPTION,56            commands: ['help', 'health', 'status'],57            version: '1.0.0'58          }59        });60        break;61      62      case 'agent.health':63        res.json({64          result: {65            status: 'ok',66            agent: AGENT_NAME,67            uptime: process.uptime()68          }69        });70        break;71      72      case 'agent.card':73        res.json({74          result: {75            name: AGENT_NAME,76            description: AGENT_DESCRIPTION,77            protocol: 'A2A',78            version: '1.0.0'79          }80        });81        break;82      83      default:84        res.json({85          result: {86            message: `Unknown action: ${action}. Use 'agent.help' for available commands.`,87            agent: AGENT_NAME88          }89        });90    }91  } catch (error) {92    res.status(500).json({93      error: error.message,94      agent: AGENT_NAME95    });96  }97});98 99// A2A GET endpoint100app.get('/a2a/v1', (req, res) => {101  res.json({102    name: AGENT_NAME,103    description: AGENT_DESCRIPTION,104    protocol: 'A2A',105    version: '1.0.0',106    endpoints: {107      post: '/a2a/v1',108      card: '/.well-known/agent-card.json',109      health: '/health'110    }111  });112});113 114// Root endpoint115app.get('/', (req, res) => {116  res.json({117    name: AGENT_NAME,118    description: AGENT_DESCRIPTION,119    status: 'active',120    endpoints: {121      health: '/health',122      a2a: '/a2a/v1',123      card: '/.well-known/agent-card.json'124    }125  });126});127 128// Start server129app.listen(PORT, '0.0.0.0', () => {130  console.log(`${AGENT_NAME} listening on port ${PORT}`);131  console.log(`Health: http://localhost:${PORT}/health`);132  console.log(`A2A: http://localhost:${PORT}/a2a/v1`);133});134