392 lines
9.5 KiB
Bash
Executable File
392 lines
9.5 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# new-agent.sh — genera el scaffold de un nuevo agente
|
|
#
|
|
# Uso:
|
|
# ./dev-scripts/new-agent.sh <agent-id> [displayname]
|
|
#
|
|
# Ejemplo:
|
|
# ./dev-scripts/new-agent.sh monitor-bot "Monitor Agent"
|
|
#
|
|
# Crea:
|
|
# agents/<agent-id>/config.yaml (basado en el assistant como plantilla)
|
|
# agents/<agent-id>/agent.go (reglas puras vacías, listo para extender)
|
|
# agents/<agent-id>/prompts/ (directorio para system prompt)
|
|
# agents/<agent-id>/data/ (directorio de datos, en .gitignore)
|
|
#
|
|
# También te recuerda los dos pasos manuales que quedan.
|
|
|
|
source "$(dirname "$0")/_common.sh"
|
|
load_env
|
|
|
|
need_arg "${1:-}"
|
|
|
|
ID="$1"
|
|
DISPLAYNAME="${2:-$ID}"
|
|
PACKAGE="$(echo "$ID" | tr '-' '_' | sed 's/_bot//')" # "monitor-bot" → "monitor"
|
|
DIR="agents/$ID"
|
|
|
|
[[ -d "$DIR" ]] && fail "Ya existe agents/$ID — ¿ya fue creado?"
|
|
|
|
info "Creando scaffold para $ID..."
|
|
|
|
mkdir -p "$DIR/prompts" "$DIR/data"
|
|
|
|
# ── config.yaml ────────────────────────────────────────────────────────────
|
|
cat > "$DIR/config.yaml" <<YAML
|
|
# ============================================
|
|
# IDENTIDAD
|
|
# ============================================
|
|
agent:
|
|
id: $ID
|
|
name: "$DISPLAYNAME"
|
|
version: "1.0.0"
|
|
enabled: true
|
|
description: "Descripción del agente $DISPLAYNAME"
|
|
tags: [$(echo "$ID" | tr '-' ',')]
|
|
|
|
# ============================================
|
|
# PERSONALIDAD Y COMPORTAMIENTO
|
|
# ============================================
|
|
personality:
|
|
tone: friendly
|
|
verbosity: concise
|
|
language: es
|
|
languages_supported: [es, en]
|
|
emoji_style: minimal
|
|
prefix: "🤖"
|
|
error_style: helpful
|
|
|
|
templates:
|
|
greeting: "Hola, soy $DISPLAYNAME. ¿En qué puedo ayudarte?"
|
|
unknown_command: "No reconozco ese comando. Escríbeme directamente."
|
|
permission_denied: "No tengo permiso para hacer eso."
|
|
error: "Algo salió mal: {{.Error}}"
|
|
success: "{{.Summary}}"
|
|
busy: "Procesando, dame un momento..."
|
|
|
|
behavior:
|
|
proactive: false
|
|
ask_confirmation: false
|
|
show_reasoning: false
|
|
thread_replies: true
|
|
typing_indicator: true
|
|
acknowledge_receipt: false
|
|
|
|
# ============================================
|
|
# LLM
|
|
# ============================================
|
|
llm:
|
|
primary:
|
|
provider: openai
|
|
model: gpt-4o
|
|
api_key_env: OPENAI_API_KEY
|
|
base_url: ""
|
|
max_tokens: 4096
|
|
temperature: 0.7
|
|
|
|
fallback:
|
|
provider: ""
|
|
model: ""
|
|
api_key_env: ""
|
|
base_url: ""
|
|
max_tokens: 0
|
|
temperature: 0
|
|
|
|
reasoning:
|
|
system_prompt_file: "prompts/system.md"
|
|
context_window: 16384
|
|
memory_messages: 20
|
|
|
|
tool_use:
|
|
enabled: false
|
|
max_iterations: 3
|
|
parallel_calls: false
|
|
|
|
rate_limit:
|
|
requests_per_minute: 30
|
|
tokens_per_minute: 100000
|
|
concurrent_requests: 3
|
|
|
|
# ============================================
|
|
# TOOLS — ajustar según necesidades del agente
|
|
# ============================================
|
|
tools:
|
|
ssh:
|
|
enabled: false
|
|
allowed_targets: []
|
|
forbidden_commands: []
|
|
timeout: 0s
|
|
max_concurrent: 0
|
|
require_confirmation: []
|
|
http:
|
|
enabled: false
|
|
allowed_domains: []
|
|
timeout: 0s
|
|
max_retries: 0
|
|
scripts:
|
|
enabled: false
|
|
scripts_dir: ""
|
|
allowed: []
|
|
timeout: 0s
|
|
sandbox: false
|
|
file_ops:
|
|
enabled: false
|
|
allowed_paths: []
|
|
read_only: true
|
|
mcp:
|
|
enabled: false
|
|
servers: []
|
|
expose:
|
|
port: 0
|
|
tools: []
|
|
|
|
# ============================================
|
|
# MATRIX
|
|
# ============================================
|
|
matrix:
|
|
homeserver: "${MATRIX_HOMESERVER}"
|
|
user_id: "@$ID:${MATRIX_SERVER_NAME}"
|
|
access_token_env: MATRIX_TOKEN_$(echo "$ID" | tr '[:lower:]-' '[:upper:]_' | sed 's/_BOT$//')
|
|
device_id: "$(echo "$ID" | tr '[:lower:]-' '[:upper:]_')01"
|
|
|
|
encryption:
|
|
enabled: false
|
|
store_path: "./data/crypto/"
|
|
trust_mode: tofu
|
|
|
|
rooms:
|
|
listen: []
|
|
respond: []
|
|
admin: []
|
|
|
|
filters:
|
|
command_prefix: "!"
|
|
mention_respond: true
|
|
dm_respond: true
|
|
ignore_bots: true
|
|
ignore_users: []
|
|
min_power_level: 0
|
|
|
|
# ============================================
|
|
# INTER-AGENTES
|
|
# ============================================
|
|
agents:
|
|
peers: []
|
|
delegation:
|
|
enabled: false
|
|
can_delegate_to: []
|
|
can_receive_from: []
|
|
max_delegation_depth: 1
|
|
timeout: 30s
|
|
protocol:
|
|
format: json
|
|
channel: matrix
|
|
heartbeat_interval: 60s
|
|
|
|
# ============================================
|
|
# SSH
|
|
# ============================================
|
|
ssh:
|
|
defaults:
|
|
user: ""
|
|
port: 22
|
|
key_file_env: ""
|
|
known_hosts: ""
|
|
keepalive_interval: 0s
|
|
timeout: 0s
|
|
targets: {}
|
|
|
|
# ============================================
|
|
# SEGURIDAD
|
|
# ============================================
|
|
security:
|
|
roles:
|
|
admin:
|
|
users: ["@admin:\${MATRIX_SERVER_NAME}"]
|
|
actions: ["*"]
|
|
user:
|
|
users: ["*"]
|
|
actions: ["help"]
|
|
audit:
|
|
enabled: false
|
|
log_file: "./data/audit.log"
|
|
log_to_room: ""
|
|
include: []
|
|
secrets:
|
|
provider: env
|
|
|
|
# ============================================
|
|
# SCHEDULING
|
|
# ============================================
|
|
schedules: []
|
|
|
|
# ============================================
|
|
# OBSERVABILIDAD
|
|
# ============================================
|
|
observability:
|
|
logging:
|
|
level: info
|
|
format: json
|
|
output: stdout
|
|
file: "./data/$ID.log"
|
|
metrics:
|
|
enabled: false
|
|
port: 0
|
|
path: /metrics
|
|
export: prometheus
|
|
health:
|
|
enabled: true
|
|
port: 0
|
|
path: /healthz
|
|
tracing:
|
|
enabled: false
|
|
provider: ""
|
|
endpoint: ""
|
|
|
|
# ============================================
|
|
# RESILIENCIA
|
|
# ============================================
|
|
resilience:
|
|
circuit_breaker:
|
|
failure_threshold: 5
|
|
timeout: 30s
|
|
half_open_max: 2
|
|
retry:
|
|
max_attempts: 2
|
|
backoff: exponential
|
|
initial_delay: 1s
|
|
max_delay: 10s
|
|
shutdown:
|
|
timeout: 10s
|
|
drain_messages: true
|
|
save_state: false
|
|
state_file: ""
|
|
queue:
|
|
enabled: true
|
|
max_size: 50
|
|
priority_users: ["@admin:\${MATRIX_SERVER_NAME}"]
|
|
|
|
# ============================================
|
|
# ALMACENAMIENTO
|
|
# ============================================
|
|
storage:
|
|
state:
|
|
backend: sqlite
|
|
path: "./data/$ID.db"
|
|
cache:
|
|
enabled: true
|
|
backend: memory
|
|
ttl: 5m
|
|
max_entries: 200
|
|
history:
|
|
backend: sqlite
|
|
path: "./data/history.db"
|
|
retention: 168h
|
|
YAML
|
|
|
|
# ── agent.go ───────────────────────────────────────────────────────────────
|
|
cat > "$DIR/agent.go" <<GO
|
|
// Package $PACKAGE defines the pure rules for the $DISPLAYNAME.
|
|
package $PACKAGE
|
|
|
|
import "github.com/enmanuel/agents/pkg/decision"
|
|
|
|
// Rules returns the decision rules for the $ID.
|
|
func Rules() []decision.Rule {
|
|
return []decision.Rule{
|
|
{
|
|
Name: "help",
|
|
Match: decision.MatchCommand("help"),
|
|
Actions: []decision.Action{{
|
|
Kind: decision.ActionKindReply,
|
|
Reply: &decision.ReplyAction{
|
|
Content: "Soy $DISPLAYNAME. Escríbeme lo que necesitas.",
|
|
},
|
|
}},
|
|
},
|
|
// Catch-all: DMs y menciones van al LLM
|
|
{
|
|
Name: "llm-fallback",
|
|
Match: func(ctx decision.MessageContext) bool {
|
|
return ctx.IsDirectMsg || ctx.IsMention
|
|
},
|
|
Actions: []decision.Action{{
|
|
Kind: decision.ActionKindLLM,
|
|
LLM: &decision.LLMAction{},
|
|
}},
|
|
},
|
|
}
|
|
}
|
|
GO
|
|
|
|
# ── system prompt ──────────────────────────────────────────────────────────
|
|
cat > "$DIR/prompts/system.md" <<MD
|
|
# $DISPLAYNAME — System Prompt
|
|
|
|
Eres $DISPLAYNAME. Describe aquí el rol, capacidades y restricciones del agente.
|
|
|
|
## Rol
|
|
...
|
|
|
|
## Capacidades
|
|
...
|
|
|
|
## Restricciones
|
|
...
|
|
MD
|
|
|
|
ok "Scaffold creado en $DIR/"
|
|
echo ""
|
|
|
|
# ── Actualizar cmd/launcher/main.go ───────────────────────────────────────
|
|
LAUNCHER="cmd/launcher/main.go"
|
|
|
|
if grep -q "\"$ID\":" "$LAUNCHER" 2>/dev/null; then
|
|
warn "$ID ya está en rulesRegistry de $LAUNCHER — saltando"
|
|
else
|
|
TAB=$'\t'
|
|
IMPORT_LINE="${TAB}${PACKAGE}agent \"github.com/enmanuel/agents/agents/$ID\""
|
|
REGISTRY_LINE="${TAB}\"$ID\": ${PACKAGE}agent.Rules,"
|
|
|
|
# Insertar import después del último import agents/agents/*
|
|
if awk -v new_import="$IMPORT_LINE" '
|
|
{
|
|
lines[NR] = $0
|
|
if ($0 ~ /[a-z_]+agent "github\.com\/enmanuel\/agents\/agents\/[^"]+"/)
|
|
last_import = NR
|
|
}
|
|
END {
|
|
if (!last_import) { for (i=1;i<=NR;i++) print lines[i]; exit 1 }
|
|
for (i = 1; i <= NR; i++) {
|
|
print lines[i]
|
|
if (i == last_import) print new_import
|
|
}
|
|
}
|
|
' "$LAUNCHER" > /tmp/_launcher_tmp; then
|
|
mv /tmp/_launcher_tmp "$LAUNCHER"
|
|
ok "Import añadido en $LAUNCHER"
|
|
else
|
|
warn "No se pudo insertar el import automáticamente — añádelo manualmente:"
|
|
echo -e " ${GRN}${IMPORT_LINE}${RST}"
|
|
fi
|
|
|
|
# Insertar entry en rulesRegistry antes del cierre }
|
|
if awk -v new_entry="$REGISTRY_LINE" '
|
|
/^var rulesRegistry/ { in_reg = 1 }
|
|
in_reg && /^\}/ { print new_entry; in_reg = 0 }
|
|
{ print }
|
|
' "$LAUNCHER" > /tmp/_launcher_tmp; then
|
|
mv /tmp/_launcher_tmp "$LAUNCHER"
|
|
ok "Registry entry añadida en $LAUNCHER"
|
|
else
|
|
warn "No se pudo insertar el registry entry — añádelo manualmente:"
|
|
echo -e " ${GRN}${REGISTRY_LINE}${RST}"
|
|
fi
|
|
fi
|
|
|
|
echo ""
|
|
echo -e "${YLW}Queda 1 paso:${RST} registrar el bot en Matrix y añadir su token a .env:"
|
|
echo ""
|
|
echo -e " ${DIM}./dev-scripts/register.sh $ID \"$DISPLAYNAME\"${RST}"
|
|
echo ""
|