File size: 20,013 Bytes
06306cb | 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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 | /**
* OpenClaw Gateway Server para Hugging Face Spaces
*
* Arquitetura:
* - Servidor Express otimizado para produção
* - Proxy transparente para API do OpenClaw.ai
* - Suporte a múltiplas APIs de IA gratuitas (OpenRouter, Gemini, Groq)
* - Rate limiting, compression, segurança via helmet
* - Healthcheck endpoint para monitoramento do HF
* - Graceful shutdown com cleanup de recursos
*
* @author Senior DevOps Audit
* @version 1.0.0
*/
'use strict';
// Carrega variáveis de ambiente antes de qualquer import
require('dotenv').config();
const express = require('express');
const helmet = require('helmet');
const compression = require('compression');
const { createProxyMiddleware } = require('http-proxy-middleware');
const rateLimit = require('express-rate-limit');
// =============================================================================
// CONFIGURAÇÃO CENTRALIZADA - Validada na inicialização
// =============================================================================
const CONFIG = {
port: parseInt(process.env.PORT || '7860', 10),
host: process.env.HOST || '0.0.0.0',
nodeEnv: process.env.NODE_ENV || 'production',
openclaw: {
apiKey: process.env.OPENCLAW_API_KEY || '',
baseUrl: process.env.OPENCLAW_BASE_URL || 'https://api.openclaw.ai',
},
ai: {
openrouterKey: process.env.OPENROUTER_API_KEY || '',
geminiKey: process.env.GEMINI_API_KEY || '',
groqKey: process.env.GROQ_API_KEY || '',
},
};
// Validação de configuração crítica na inicialização
// Fail-fast: melhor crashar com mensagem clara do que funcionar incorretamente
function validateConfig() {
const warnings = [];
if (!CONFIG.openclaw.apiKey) {
warnings.push('OPENCLAW_API_KEY não configurada. Proxy para API externa desabilitado.');
}
const hasAnyAI = CONFIG.ai.openrouterKey || CONFIG.ai.geminiKey || CONFIG.ai.groqKey;
if (!hasAnyAI) {
warnings.push('Nenhuma chave de API de IA configurada. Configure OPENROUTER_API_KEY, GEMINI_API_KEY ou GROQ_API_KEY.');
}
warnings.forEach(w => console.warn(`[CONFIG WARN] ${w}`));
return true;
}
// =============================================================================
// INICIALIZAÇÃO DO SERVIDOR EXPRESS
// =============================================================================
const app = express();
// Helmet: Configura headers de segurança HTTP
// Desabilitamos contentSecurityPolicy para não bloquear o iframe do HF
app.use(helmet({
contentSecurityPolicy: false,
crossOriginEmbedderPolicy: false,
}));
// Compression: gzip/brotli em todas as respostas
// Reduz bandwidth significativamente para payloads de IA (JSON grandes)
app.use(compression({
level: 6, // Balanço entre CPU e compressão
threshold: 1024, // Comprimir apenas respostas > 1KB
}));
// Body parser: Limite de 10MB para suportar payloads grandes de IA
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: true, limit: '10mb' }));
// Rate limiting: Protege contra abuse e evita esgotar quotas de APIs gratuitas
// 100 requests por 15 minutos por IP é generoso para uso normal
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutos
max: 100,
standardHeaders: true,
legacyHeaders: false,
message: { error: 'Muitas requisições. Tente novamente em 15 minutos.' },
});
app.use(limiter);
// =============================================================================
// ENDPOINT: HEALTHCHECK
// Usado pelo HEALTHCHECK do Dockerfile e pelo HF para verificar se o Space
// está vivo. Deve responder em < 1 segundo e nunca falhar se o servidor está up.
// =============================================================================
app.get('/health', (req, res) => {
// Coleta de métricas de memória para debug
const memUsage = process.memoryUsage();
res.status(200).json({
status: 'healthy',
timestamp: new Date().toISOString(),
uptime: Math.floor(process.uptime()),
environment: CONFIG.nodeEnv,
memory: {
rss: `${Math.round(memUsage.rss / 1024 / 1024)}MB`,
heapUsed: `${Math.round(memUsage.heapUsed / 1024 / 1024)}MB`,
heapTotal: `${Math.round(memUsage.heapTotal / 1024 / 1024)}MB`,
},
services: {
openclaw: CONFIG.openclaw.apiKey ? 'configured' : 'not_configured',
openrouter: CONFIG.ai.openrouterKey ? 'configured' : 'not_configured',
gemini: CONFIG.ai.geminiKey ? 'configured' : 'not_configured',
groq: CONFIG.ai.groqKey ? 'configured' : 'not_configured',
},
});
});
// =============================================================================
// ENDPOINT: INTERFACE WEB (Dashboard)
// Página HTML para visualizar o status e configurar integrações
// =============================================================================
app.get('/', (req, res) => {
const html = `<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OpenClaw Gateway - HF Space</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: 'Segoe UI', system-ui, sans-serif;
background: #0f0f23;
color: #e0e0e0;
min-height: 100vh;
padding: 2rem;
}
.container { max-width: 900px; margin: 0 auto; }
h1 {
font-size: 2rem;
background: linear-gradient(135deg, #6366f1, #8b5cf6);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
margin-bottom: 0.5rem;
}
.subtitle { color: #888; margin-bottom: 2rem; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 1rem; margin-bottom: 2rem; }
.card {
background: #1a1a2e;
border: 1px solid #2a2a4a;
border-radius: 12px;
padding: 1.5rem;
}
.card h3 { color: #6366f1; margin-bottom: 1rem; font-size: 0.9rem; text-transform: uppercase; letter-spacing: 0.1em; }
.status { display: flex; align-items: center; gap: 0.5rem; margin-bottom: 0.5rem; }
.dot { width: 8px; height: 8px; border-radius: 50%; }
.dot.green { background: #22c55e; box-shadow: 0 0 8px #22c55e; }
.dot.red { background: #ef4444; }
.dot.yellow { background: #f59e0b; }
.endpoint {
background: #0f0f23;
border: 1px solid #2a2a4a;
border-radius: 8px;
padding: 1rem;
margin-bottom: 0.5rem;
font-family: monospace;
font-size: 0.85rem;
}
.method {
display: inline-block;
padding: 0.2rem 0.6rem;
border-radius: 4px;
font-size: 0.75rem;
font-weight: bold;
margin-right: 0.5rem;
}
.get { background: #065f46; color: #6ee7b7; }
.post { background: #1e3a5f; color: #93c5fd; }
.footer { color: #555; font-size: 0.8rem; text-align: center; margin-top: 2rem; }
#metrics { font-size: 0.85rem; color: #888; }
</style>
</head>
<body>
<div class="container">
<h1>🦅 OpenClaw Gateway</h1>
<p class="subtitle">Hugging Face Space · Node.js ${process.version} · ${CONFIG.nodeEnv}</p>
<div class="grid">
<div class="card">
<h3>Status dos Serviços</h3>
<div class="status">
<div class="dot ${CONFIG.openclaw.apiKey ? 'green' : 'red'}"></div>
<span>OpenClaw API</span>
</div>
<div class="status">
<div class="dot ${CONFIG.ai.openrouterKey ? 'green' : 'yellow'}"></div>
<span>OpenRouter</span>
</div>
<div class="status">
<div class="dot ${CONFIG.ai.geminiKey ? 'green' : 'yellow'}"></div>
<span>Google Gemini</span>
</div>
<div class="status">
<div class="dot ${CONFIG.ai.groqKey ? 'green' : 'yellow'}"></div>
<span>Groq</span>
</div>
</div>
<div class="card">
<h3>Métricas em Tempo Real</h3>
<div id="metrics">Carregando...</div>
</div>
<div class="card">
<h3>Configuração Rápida</h3>
<p style="font-size:0.85rem; color:#888; line-height:1.6">
Configure suas chaves em:<br>
<strong style="color:#6366f1">HF Space Settings</strong><br>
→ Variables and Secrets
</p>
</div>
</div>
<div class="card">
<h3>Endpoints Disponíveis</h3>
<div style="margin-top: 1rem;">
<div class="endpoint">
<span class="method get">GET</span>/health · Healthcheck
</div>
<div class="endpoint">
<span class="method post">POST</span>/api/chat · Chat com IA (OpenRouter/Gemini/Groq)
</div>
<div class="endpoint">
<span class="method post">POST</span>/api/openclaw/execute · Executar task OpenClaw
</div>
<div class="endpoint">
<span class="method get">GET</span>/api/models · Listar modelos disponíveis
</div>
</div>
</div>
<div class="footer">
OpenClaw Gateway v1.0.0 · Rodando em Hugging Face Spaces · Uptime: <span id="uptime">0</span>s
</div>
</div>
<script>
async function updateMetrics() {
try {
const r = await fetch('/health');
const d = await r.json();
document.getElementById('metrics').innerHTML =
'<div>RAM: ' + d.memory.heapUsed + ' / ' + d.memory.heapTotal + '</div>' +
'<div>RSS: ' + d.memory.rss + '</div>' +
'<div>Uptime: ' + d.uptime + 's</div>';
document.getElementById('uptime').textContent = d.uptime;
} catch(e) {}
}
updateMetrics();
setInterval(updateMetrics, 5000);
</script>
</body>
</html>`;
res.status(200).send(html);
});
// =============================================================================
// ENDPOINT: CHAT COM IA
// Roteamento inteligente: usa a primeira API configurada disponível
// Ordem de prioridade: OpenRouter > Gemini > Groq
// =============================================================================
app.post('/api/chat', async (req, res) => {
// Import dinâmico de node-fetch (ESM no CommonJS)
const fetch = (await import('node-fetch')).default;
const { message, model, provider } = req.body;
if (!message) {
return res.status(400).json({ error: 'Campo "message" é obrigatório.' });
}
// Seleção de provider com fallback automático
let selectedProvider = provider || 'auto';
if (selectedProvider === 'auto') {
if (CONFIG.ai.openrouterKey) selectedProvider = 'openrouter';
else if (CONFIG.ai.geminiKey) selectedProvider = 'gemini';
else if (CONFIG.ai.groqKey) selectedProvider = 'groq';
else {
return res.status(503).json({
error: 'Nenhuma API de IA configurada. Adicione OPENROUTER_API_KEY, GEMINI_API_KEY ou GROQ_API_KEY nos Secrets do Space.'
});
}
}
try {
let response;
// --- OpenRouter ---
if (selectedProvider === 'openrouter') {
const r = await fetch('https://openrouter.ai/api/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${CONFIG.ai.openrouterKey}`,
'Content-Type': 'application/json',
'HTTP-Referer': `https://huggingface.co/spaces`,
'X-Title': 'OpenClaw HF Gateway',
},
body: JSON.stringify({
model: model || 'google/gemma-2-9b-it:free', // Modelo gratuito de alta qualidade
messages: [{ role: 'user', content: message }],
max_tokens: 4096,
}),
});
if (!r.ok) {
const err = await r.text();
throw new Error(`OpenRouter error ${r.status}: ${err}`);
}
const data = await r.json();
response = {
provider: 'openrouter',
model: data.model,
content: data.choices[0]?.message?.content || '',
usage: data.usage,
};
}
// --- Google Gemini ---
else if (selectedProvider === 'gemini') {
const geminiModel = model || 'gemini-1.5-flash';
const r = await fetch(
`https://generativelanguage.googleapis.com/v1beta/models/${geminiModel}:generateContent?key=${CONFIG.ai.geminiKey}`,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
contents: [{ parts: [{ text: message }] }],
generationConfig: { maxOutputTokens: 8192 },
}),
}
);
if (!r.ok) {
const err = await r.text();
throw new Error(`Gemini error ${r.status}: ${err}`);
}
const data = await r.json();
response = {
provider: 'gemini',
model: geminiModel,
content: data.candidates[0]?.content?.parts[0]?.text || '',
usage: data.usageMetadata,
};
}
// --- Groq ---
else if (selectedProvider === 'groq') {
const r = await fetch('https://api.groq.com/openai/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${CONFIG.ai.groqKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: model || 'llama-3.1-8b-instant', // Ultra-rápido e gratuito
messages: [{ role: 'user', content: message }],
max_tokens: 8192,
}),
});
if (!r.ok) {
const err = await r.text();
throw new Error(`Groq error ${r.status}: ${err}`);
}
const data = await r.json();
response = {
provider: 'groq',
model: data.model,
content: data.choices[0]?.message?.content || '',
usage: data.usage,
};
}
else {
return res.status(400).json({ error: `Provider desconhecido: ${selectedProvider}` });
}
res.json({ success: true, ...response });
} catch (error) {
console.error('[API CHAT ERROR]', error.message);
res.status(500).json({
error: 'Erro ao chamar API de IA',
details: error.message,
});
}
});
// =============================================================================
// ENDPOINT: EXECUTAR TASK OPENCLAW
// Proxy para a API real do OpenClaw.ai com autenticação injetada
// =============================================================================
app.post('/api/openclaw/execute', async (req, res) => {
if (!CONFIG.openclaw.apiKey) {
return res.status(503).json({
error: 'OPENCLAW_API_KEY não configurada. Adicione nos Secrets do Space.'
});
}
const fetch = (await import('node-fetch')).default;
try {
const r = await fetch(`${CONFIG.openclaw.baseUrl}/v1/execute`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${CONFIG.openclaw.apiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(req.body),
});
const data = await r.json();
res.status(r.status).json(data);
} catch (error) {
console.error('[OPENCLAW EXECUTE ERROR]', error.message);
res.status(500).json({ error: 'Erro ao conectar com OpenClaw API', details: error.message });
}
});
// =============================================================================
// ENDPOINT: LISTAR MODELOS DISPONÍVEIS
// =============================================================================
app.get('/api/models', (req, res) => {
res.json({
providers: {
openrouter: {
configured: !!CONFIG.ai.openrouterKey,
freeModels: [
'google/gemma-2-9b-it:free',
'meta-llama/llama-3.1-8b-instruct:free',
'mistralai/mistral-7b-instruct:free',
'microsoft/phi-3-mini-128k-instruct:free',
'nousresearch/hermes-3-llama-3.1-405b:free',
],
},
gemini: {
configured: !!CONFIG.ai.geminiKey,
freeModels: [
'gemini-1.5-flash',
'gemini-1.5-flash-8b',
'gemini-1.0-pro',
],
},
groq: {
configured: !!CONFIG.ai.groqKey,
freeModels: [
'llama-3.1-8b-instant',
'llama-3.1-70b-versatile',
'mixtral-8x7b-32768',
'gemma2-9b-it',
],
},
},
});
});
// =============================================================================
// PROXY PARA OPENCLAW API (todas as rotas /api/openclaw/*)
// Transparente: injeta autenticação e repassa para o servidor real
// =============================================================================
if (CONFIG.openclaw.apiKey && CONFIG.openclaw.baseUrl) {
app.use('/api/openclaw', createProxyMiddleware({
target: CONFIG.openclaw.baseUrl,
changeOrigin: true,
pathRewrite: { '^/api/openclaw': '' },
on: {
proxyReq: (proxyReq) => {
proxyReq.setHeader('Authorization', `Bearer ${CONFIG.openclaw.apiKey}`);
},
error: (err, req, res) => {
console.error('[PROXY ERROR]', err.message);
res.status(502).json({ error: 'Erro no proxy para OpenClaw API', details: err.message });
},
},
}));
}
// =============================================================================
// HANDLER DE ERROS GLOBAL
// Captura qualquer erro não tratado e retorna resposta JSON padronizada
// CRÍTICO: Sem isso, erros causam crash do processo
// =============================================================================
app.use((err, req, res, _next) => {
console.error('[UNHANDLED ERROR]', err.stack);
res.status(500).json({
error: 'Erro interno do servidor',
message: CONFIG.nodeEnv === 'production' ? 'Verifique os logs.' : err.message,
});
});
// =============================================================================
// HANDLER DE ROTAS NÃO ENCONTRADAS
// =============================================================================
app.use((req, res) => {
res.status(404).json({ error: `Rota não encontrada: ${req.method} ${req.path}` });
});
// =============================================================================
// INICIALIZAÇÃO COM GRACEFUL SHUTDOWN
// Graceful shutdown: fecha conexões existentes antes de matar o processo.
// Evita requests interrompidas no meio quando o HF reinicia o Space.
// =============================================================================
validateConfig();
const server = app.listen(CONFIG.port, CONFIG.host, () => {
console.log(`✅ OpenClaw Gateway iniciado`);
console.log(`🌐 Endereço: http://${CONFIG.host}:${CONFIG.port}`);
console.log(`🔧 Ambiente: ${CONFIG.nodeEnv}`);
console.log(`📊 Node.js: ${process.version}`);
console.log(`💾 Memória máx: ${process.env.NODE_OPTIONS || 'padrão'}`);
});
// Timeout para requests longas (30s para APIs de IA que podem ser lentas)
server.timeout = 30000;
server.keepAliveTimeout = 65000; // Maior que timeout de load balancer do HF (60s)
server.headersTimeout = 66000;
// Graceful shutdown handler
function gracefulShutdown(signal) {
console.log(`\n[SHUTDOWN] Recebido ${signal}. Encerrando servidor...`);
server.close((err) => {
if (err) {
console.error('[SHUTDOWN ERROR]', err);
process.exit(1);
}
// Garbage collection manual se disponível (via --expose-gc)
if (global.gc) {
global.gc();
}
console.log('[SHUTDOWN] Servidor encerrado com sucesso.');
process.exit(0);
});
// Force kill após 10s se o servidor não fechar sozinho
setTimeout(() => {
console.error('[SHUTDOWN TIMEOUT] Forçando encerramento após 10s.');
process.exit(1);
}, 10000);
}
process.on('SIGTERM', () => gracefulShutdown('SIGTERM'));
process.on('SIGINT', () => gracefulShutdown('SIGINT'));
// Handler para exceções não capturadas
// Loga o erro mas NÃO derruba o processo (resilência em produção)
process.on('uncaughtException', (err) => {
console.error('[UNCAUGHT EXCEPTION]', err.stack);
// Em produção: log e continua. Em desenvolvimento: considera derrubar
});
process.on('unhandledRejection', (reason) => {
console.error('[UNHANDLED REJECTION]', reason);
}); |