@deinossrl/dgp-agent 1.4.6 → 1.4.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.mjs +123 -16
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -34,6 +34,79 @@ import { join, dirname } from 'path';
34
34
 
35
35
  const CONFIG_DIR = join(homedir(), '.dgp-agent');
36
36
  const CONFIG_FILE = join(CONFIG_DIR, 'config.json');
37
+ const PID_FILE = join(CONFIG_DIR, 'agent.pid');
38
+
39
+ /**
40
+ * Mata al agente anterior si existe
41
+ */
42
+ function killPreviousAgent() {
43
+ try {
44
+ if (existsSync(PID_FILE)) {
45
+ const oldPid = parseInt(readFileSync(PID_FILE, 'utf-8').trim(), 10);
46
+ if (oldPid && oldPid !== process.pid) {
47
+ try {
48
+ // Verificar si el proceso existe
49
+ process.kill(oldPid, 0);
50
+ // Si llegamos aquí, el proceso existe - matarlo
51
+ console.log(`\x1b[33m[!] Matando agente anterior (PID: ${oldPid})...\x1b[0m`);
52
+ process.kill(oldPid, 'SIGTERM');
53
+ // Esperar un poco para que muera
54
+ const start = Date.now();
55
+ while (Date.now() - start < 2000) {
56
+ try {
57
+ process.kill(oldPid, 0);
58
+ // Sigue vivo, esperar
59
+ } catch {
60
+ // Ya murió
61
+ break;
62
+ }
63
+ }
64
+ } catch (e) {
65
+ // El proceso no existe, ignorar
66
+ }
67
+ }
68
+ }
69
+ } catch (e) {
70
+ // Ignorar errores
71
+ }
72
+ }
73
+
74
+ /**
75
+ * Guarda el PID actual
76
+ */
77
+ function savePid() {
78
+ try {
79
+ if (!existsSync(CONFIG_DIR)) {
80
+ mkdirSync(CONFIG_DIR, { recursive: true });
81
+ }
82
+ writeFileSync(PID_FILE, String(process.pid), 'utf-8');
83
+ } catch (e) {
84
+ // Ignorar errores
85
+ }
86
+ }
87
+
88
+ /**
89
+ * Limpia el archivo PID al salir
90
+ */
91
+ function cleanupPid() {
92
+ try {
93
+ if (existsSync(PID_FILE)) {
94
+ const savedPid = parseInt(readFileSync(PID_FILE, 'utf-8').trim(), 10);
95
+ if (savedPid === process.pid) {
96
+ // Solo borrar si es nuestro PID
97
+ const { unlinkSync } = require('fs');
98
+ unlinkSync(PID_FILE);
99
+ }
100
+ }
101
+ } catch (e) {
102
+ // Ignorar errores
103
+ }
104
+ }
105
+
106
+ // Limpiar PID al salir
107
+ process.on('exit', cleanupPid);
108
+ process.on('SIGINT', () => { cleanupPid(); process.exit(0); });
109
+ process.on('SIGTERM', () => { cleanupPid(); process.exit(0); });
37
110
 
38
111
  /**
39
112
  * Lee la configuración del archivo
@@ -129,6 +202,10 @@ async function fetchPlatformConfig() {
129
202
  // Variable para config de la plataforma (se carga async)
130
203
  let platformConfig = null;
131
204
 
205
+ // Versión y modo del agente
206
+ const AGENT_VERSION = '1.4.8';
207
+ let AGENT_MODE = 'status'; // 'status' | 'deploy' | 'ai'
208
+
132
209
  // Configuración (prioridad: env vars > archivo config > platform config > defaults)
133
210
  const CONFIG = {
134
211
  apiUrl: process.env.DGP_API_URL || fileConfig.apiUrl || 'https://asivayhbrqennwiwttds.supabase.co/functions/v1/dgp-agent-status',
@@ -156,6 +233,27 @@ const colors = {
156
233
  bold: '\x1b[1m',
157
234
  };
158
235
 
236
+ /**
237
+ * Genera un banner con padding automático
238
+ */
239
+ function printBanner(lines, color = 'cyan') {
240
+ const maxLen = Math.max(...lines.map(l => l.length));
241
+ const width = maxLen + 10; // padding de 5 a cada lado
242
+ const c = colors[color];
243
+ const r = colors.reset;
244
+
245
+ console.log('');
246
+ console.log(`${c}╔${'═'.repeat(width)}╗${r}`);
247
+ for (const line of lines) {
248
+ const padding = width - line.length - 2;
249
+ const leftPad = Math.floor(padding / 2);
250
+ const rightPad = padding - leftPad;
251
+ console.log(`${c}║${' '.repeat(leftPad + 1)}${line}${' '.repeat(rightPad + 1)}║${r}`);
252
+ }
253
+ console.log(`${c}╚${'═'.repeat(width)}╝${r}`);
254
+ console.log('');
255
+ }
256
+
159
257
  function log(message, color = 'reset') {
160
258
  const timestamp = new Date().toLocaleTimeString();
161
259
  console.log(`${colors.gray}[${timestamp}]${colors.reset} ${colors[color]}${message}${colors.reset}`);
@@ -393,12 +491,15 @@ Devuelve los comandos necesarios en formato JSON.`;
393
491
  * Modo IA - Escucha tareas de la web y las ejecuta con Claude
394
492
  */
395
493
  async function runAIMode() {
396
- console.log('');
397
- console.log(`${colors.cyan}╔═══════════════════════════════════════════════════════╗${colors.reset}`);
398
- console.log(`${colors.cyan}║ DGP Agent - AI Mode v1.4.6 🤖 ║${colors.reset}`);
399
- console.log(`${colors.cyan}║ Powered by Claude ║${colors.reset}`);
400
- console.log(`${colors.cyan}╚═══════════════════════════════════════════════════════╝${colors.reset}`);
401
- console.log('');
494
+ // Matar agente anterior y guardar PID
495
+ killPreviousAgent();
496
+ savePid();
497
+ AGENT_MODE = 'ai';
498
+
499
+ printBanner([
500
+ `DGP Agent - AI Mode v${AGENT_VERSION} 🤖`,
501
+ 'Powered by Claude'
502
+ ], 'cyan');
402
503
 
403
504
  // Intentar cargar config global desde la plataforma si no hay API key local
404
505
  if (!CONFIG.anthropicApiKey) {
@@ -697,7 +798,8 @@ async function reportStatus(status) {
697
798
  const payload = {
698
799
  machine_id: CONFIG.machineId,
699
800
  timestamp: new Date().toISOString(),
700
- agent_version: '1.4.3',
801
+ agent_version: AGENT_VERSION,
802
+ agent_mode: AGENT_MODE,
701
803
  status,
702
804
  };
703
805
 
@@ -993,12 +1095,15 @@ function printStatus(status) {
993
1095
  * Loop principal del agente
994
1096
  */
995
1097
  async function runAgent(deployMode = false) {
996
- console.log('');
997
- console.log(`${colors.green}╔═══════════════════════════════════════════════════════╗${colors.reset}`);
998
- console.log(`${colors.green}║ DGP Agent - Despliegue-GPT Local Agent ║${colors.reset}`);
999
- console.log(`${colors.green}║ @deinossrl/dgp-agent v1.4.6 ║${colors.reset}`);
1000
- console.log(`${colors.green}╚═══════════════════════════════════════════════════════╝${colors.reset}`);
1001
- console.log('');
1098
+ // Matar agente anterior y guardar PID
1099
+ killPreviousAgent();
1100
+ savePid();
1101
+ AGENT_MODE = deployMode ? 'deploy' : 'status';
1102
+
1103
+ printBanner([
1104
+ 'DGP Agent - Despliegue-GPT Local Agent',
1105
+ `@deinossrl/dgp-agent v${AGENT_VERSION}`
1106
+ ], 'green');
1002
1107
 
1003
1108
  if (!isGitRepo()) {
1004
1109
  logError('Not a git repository. Run this command from inside a git repo.');
@@ -1006,11 +1111,11 @@ async function runAgent(deployMode = false) {
1006
1111
  }
1007
1112
 
1008
1113
  logInfo(`Machine ID: ${CONFIG.machineId}`);
1114
+ logInfo(`Mode: ${AGENT_MODE.toUpperCase()}`);
1009
1115
  logInfo(`Report interval: ${CONFIG.interval}s`);
1010
1116
  logInfo(`API URL: ${CONFIG.apiUrl}`);
1011
1117
 
1012
1118
  if (deployMode) {
1013
- logInfo(`Deploy mode: ACTIVE`);
1014
1119
  logInfo(`Command poll interval: ${CONFIG.commandPollInterval}s`);
1015
1120
  }
1016
1121
 
@@ -1112,7 +1217,7 @@ async function showStatus() {
1112
1217
  function showHelp() {
1113
1218
  console.log(`
1114
1219
  ${colors.bold}${colors.cyan}DGP Agent - Despliegue-GPT Local Agent${colors.reset}
1115
- ${colors.gray}@deinossrl/dgp-agent v1.4.6${colors.reset}
1220
+ ${colors.gray}@deinossrl/dgp-agent v1.4.7${colors.reset}
1116
1221
 
1117
1222
  ${colors.bold}DESCRIPCIÓN${colors.reset}
1118
1223
  Agente local que reporta el estado de tu repositorio Git
@@ -1163,6 +1268,8 @@ ${colors.bold}REQUISITOS PARA DEPLOY${colors.reset}
1163
1268
  - Permisos sudo para reload nginx (vía sudoers sin password)
1164
1269
 
1165
1270
  ${colors.bold}CHANGELOG${colors.reset}
1271
+ ${colors.cyan}v1.4.8${colors.reset} - Banner dinámico con padding automático
1272
+ ${colors.cyan}v1.4.7${colors.reset} - Modo AI visible en web, auto-kill agente anterior
1166
1273
  ${colors.cyan}v1.4.6${colors.reset} - Fix: versión en banner AI Mode
1167
1274
  ${colors.cyan}v1.4.5${colors.reset} - Modo escucha: recibe tareas AI desde la web
1168
1275
  ${colors.cyan}v1.4.4${colors.reset} - Config global (compartida por todos los agentes)
@@ -1332,7 +1439,7 @@ switch (command) {
1332
1439
  case 'version':
1333
1440
  case '-v':
1334
1441
  case '--version':
1335
- console.log('@deinossrl/dgp-agent v1.4.6');
1442
+ console.log('@deinossrl/dgp-agent v1.4.7');
1336
1443
  break;
1337
1444
  case 'ai':
1338
1445
  case '--ai':
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@deinossrl/dgp-agent",
3
- "version": "1.4.6",
3
+ "version": "1.4.8",
4
4
  "description": "Agente local para Despliegue-GPT - Reporta el estado del repositorio Git a la plataforma TenMinute IA",
5
5
  "main": "index.mjs",
6
6
  "bin": {