feat: agregar display name, health check y mejorar notificacion — issue 0044
Pipeline de creacion formalizado con 3 mejoras:
1. Display name automatico (paso 7): create-full.sh ahora configura
el display name en Matrix via PUT al profile API con el token
del bot recien creado. Evita que los bots aparezcan como @id:server.
2. Health check post-arranque: nuevo script health-check.sh que busca
en los logs del launcher mensajes de arranque exitoso ("e2ee ready",
"runner started", etc.) con timeout configurable (default 30s).
3. Notificacion enriquecida: notify-developer.sh ahora incluye
descripcion del agente (leida de config.yaml), tools habilitadas,
formato markdown con estructura clara, y reintentos con backoff
(hasta 3 intentos) si el envio de DM falla.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,14 @@
|
||||
#!/usr/bin/env bash
|
||||
# notify-developer.sh — envía DM a los desarrolladores al crear un bot/agente
|
||||
#
|
||||
# El propio bot recién creado envía un mensaje de bienvenida enriquecido con:
|
||||
# - Nombre y tipo (agent/robot)
|
||||
# - Descripción (leída de config.yaml)
|
||||
# - Tools habilitadas (si es agent con tools)
|
||||
# - Instrucciones de uso
|
||||
#
|
||||
# Reintenta hasta 3 veces con backoff si el envío falla.
|
||||
#
|
||||
# Uso:
|
||||
# ./dev-scripts/agent/notify-developer.sh <agent-id> <type> <display-name>
|
||||
#
|
||||
@@ -35,18 +43,132 @@ if [[ -z "${DEVELOPER_MATRIX_USERS:-}" ]]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# ── Construir mensaje ────────────────────────────────────────────────────
|
||||
# ── Leer descripcion del config.yaml ────────────────────────────────────
|
||||
CONFIG_FILE=""
|
||||
for candidate in "agents/${ID}/config.yaml" "agents/_specials/${ID}/config.yaml"; do
|
||||
if [[ -f "$candidate" ]]; then
|
||||
CONFIG_FILE="$candidate"
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
||||
DESCRIPTION=""
|
||||
TOOLS_LIST=""
|
||||
if [[ -n "$CONFIG_FILE" ]]; then
|
||||
# Extraer descripcion (entre comillas si las tiene)
|
||||
DESCRIPTION=$(grep -m1 '^\s*description:' "$CONFIG_FILE" | sed 's/.*description:\s*"\?\(.*\)"\?$/\1/' | sed 's/"$//')
|
||||
|
||||
# Extraer tools habilitadas (buscar lineas "enabled: true" dentro de secciones de tools)
|
||||
if grep -q 'tool_use:' "$CONFIG_FILE" 2>/dev/null; then
|
||||
TOOL_USE_ENABLED=$(awk '/tool_use:/,/^[^ ]/' "$CONFIG_FILE" | grep -m1 'enabled:' | awk '{print $2}')
|
||||
if [[ "$TOOL_USE_ENABLED" == "true" ]]; then
|
||||
# Listar secciones de tools habilitadas
|
||||
TOOLS_LIST=$(awk '/^tools:/,/^[a-z]/' "$CONFIG_FILE" | grep -B1 'enabled: true' | grep -v 'enabled' | grep -v '^--$' | sed 's/://g' | xargs 2>/dev/null || true)
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
# ── Construir mensaje enriquecido ────────────────────────────────────────
|
||||
if [[ "$TYPE" == "robot" ]]; then
|
||||
EMOJI="🤖"
|
||||
TYPE_LABEL="Robot"
|
||||
COMMANDS_MSG="Mis comandos: help, ping, status, info, version"
|
||||
USAGE_MSG="Mis comandos built-in: \`help\`, \`ping\`, \`status\`, \`info\`, \`version\`."
|
||||
USAGE_MSG="${USAGE_MSG}\nEscribeme directamente con un comando (sin prefijo \`!\`)."
|
||||
else
|
||||
EMOJI="🧠"
|
||||
TYPE_LABEL="Agente"
|
||||
COMMANDS_MSG="Escríbeme directamente o usa !help para ver mis comandos"
|
||||
USAGE_MSG="Escríbeme por DM o mencioname en un room."
|
||||
USAGE_MSG="${USAGE_MSG}\nUsa \`!help\` para ver mis comandos disponibles."
|
||||
fi
|
||||
|
||||
MSG="${EMOJI} ¡Hola! Soy **${DISPLAYNAME}** (${TYPE_LABEL}). Acabo de ser creado. ${COMMANDS_MSG}."
|
||||
# Construir mensaje markdown
|
||||
MSG="${EMOJI} **¡Hola! Soy ${DISPLAYNAME}** (${TYPE_LABEL})"
|
||||
MSG="${MSG}\n"
|
||||
|
||||
if [[ -n "$DESCRIPTION" ]]; then
|
||||
MSG="${MSG}\n${DESCRIPTION}"
|
||||
MSG="${MSG}\n"
|
||||
fi
|
||||
|
||||
if [[ -n "$TOOLS_LIST" ]]; then
|
||||
MSG="${MSG}\n**Herramientas:** ${TOOLS_LIST}"
|
||||
MSG="${MSG}\n"
|
||||
fi
|
||||
|
||||
MSG="${MSG}\n${USAGE_MSG}"
|
||||
|
||||
# ── Funcion de envio con reintentos ──────────────────────────────────────
|
||||
send_dm() {
|
||||
local user_id="$1"
|
||||
local max_retries=3
|
||||
local retry=0
|
||||
local backoff=2
|
||||
|
||||
while [[ $retry -lt $max_retries ]]; do
|
||||
# Crear DM room (o reutilizar existente)
|
||||
ROOM_RESP=$(curl -sf -X POST "${MATRIX_HOMESERVER}/_matrix/client/v3/createRoom" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{
|
||||
\"is_direct\": true,
|
||||
\"invite\": [\"${user_id}\"],
|
||||
\"preset\": \"trusted_private_chat\"
|
||||
}" 2>&1) || {
|
||||
retry=$((retry + 1))
|
||||
if [[ $retry -lt $max_retries ]]; then
|
||||
warn " Intento $retry/$max_retries fallo al crear room con $user_id — reintentando en ${backoff}s..."
|
||||
sleep "$backoff"
|
||||
backoff=$((backoff * 2))
|
||||
continue
|
||||
fi
|
||||
warn " No se pudo crear DM room con $user_id tras $max_retries intentos"
|
||||
return 1
|
||||
}
|
||||
|
||||
ROOM_ID=$(echo "$ROOM_RESP" | grep -o '"room_id":"[^"]*"' | cut -d'"' -f4)
|
||||
if [[ -z "$ROOM_ID" ]]; then
|
||||
retry=$((retry + 1))
|
||||
if [[ $retry -lt $max_retries ]]; then
|
||||
warn " Respuesta inesperada — reintentando en ${backoff}s..."
|
||||
sleep "$backoff"
|
||||
backoff=$((backoff * 2))
|
||||
continue
|
||||
fi
|
||||
warn " Respuesta inesperada al crear room: $ROOM_RESP"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Enviar mensaje con formato markdown
|
||||
TXN_ID="notify-$(date +%s%N)"
|
||||
# Escapar newlines para el JSON
|
||||
MSG_ESCAPED=$(echo -e "$MSG")
|
||||
MSG_JSON=$(echo -e "$MSG" | python3 -c "import sys,json; print(json.dumps(sys.stdin.read().rstrip()))" 2>/dev/null || echo "\"${MSG}\"")
|
||||
|
||||
SEND_RESP=$(curl -sf -X PUT \
|
||||
"${MATRIX_HOMESERVER}/_matrix/client/v3/rooms/${ROOM_ID}/send/m.room.message/${TXN_ID}" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{
|
||||
\"msgtype\": \"m.text\",
|
||||
\"body\": ${MSG_JSON},
|
||||
\"format\": \"org.matrix.custom.html\",
|
||||
\"formatted_body\": ${MSG_JSON}
|
||||
}" 2>&1) || {
|
||||
retry=$((retry + 1))
|
||||
if [[ $retry -lt $max_retries ]]; then
|
||||
warn " Intento $retry/$max_retries fallo al enviar mensaje — reintentando en ${backoff}s..."
|
||||
sleep "$backoff"
|
||||
backoff=$((backoff * 2))
|
||||
continue
|
||||
fi
|
||||
warn " No se pudo enviar mensaje a $user_id tras $max_retries intentos"
|
||||
return 1
|
||||
}
|
||||
|
||||
ok "DM enviado a $user_id"
|
||||
return 0
|
||||
done
|
||||
}
|
||||
|
||||
# ── Enviar DM a cada desarrollador ───────────────────────────────────────
|
||||
IFS=',' read -ra DEVS <<< "$DEVELOPER_MATRIX_USERS"
|
||||
@@ -58,38 +180,5 @@ for dev in "${DEVS[@]}"; do
|
||||
USER_ID="@${dev}:${MATRIX_SERVER_NAME}"
|
||||
info "Enviando DM de $ID a $USER_ID..."
|
||||
|
||||
# Crear DM room (o reutilizar existente)
|
||||
ROOM_RESP=$(curl -sf -X POST "${MATRIX_HOMESERVER}/_matrix/client/v3/createRoom" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{
|
||||
\"is_direct\": true,
|
||||
\"invite\": [\"${USER_ID}\"],
|
||||
\"preset\": \"trusted_private_chat\"
|
||||
}" 2>&1) || {
|
||||
warn " No se pudo crear DM room con $USER_ID"
|
||||
continue
|
||||
}
|
||||
|
||||
ROOM_ID=$(echo "$ROOM_RESP" | grep -o '"room_id":"[^"]*"' | cut -d'"' -f4)
|
||||
if [[ -z "$ROOM_ID" ]]; then
|
||||
warn " Respuesta inesperada al crear room: $ROOM_RESP"
|
||||
continue
|
||||
fi
|
||||
|
||||
# Enviar mensaje
|
||||
TXN_ID="notify-$(date +%s%N)"
|
||||
SEND_RESP=$(curl -sf -X PUT \
|
||||
"${MATRIX_HOMESERVER}/_matrix/client/v3/rooms/${ROOM_ID}/send/m.room.message/${TXN_ID}" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{
|
||||
\"msgtype\": \"m.text\",
|
||||
\"body\": \"${MSG}\"
|
||||
}" 2>&1) || {
|
||||
warn " No se pudo enviar mensaje a $USER_ID"
|
||||
continue
|
||||
}
|
||||
|
||||
ok "DM enviado a $USER_ID"
|
||||
send_dm "$USER_ID"
|
||||
done
|
||||
|
||||
Reference in New Issue
Block a user