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