.agent/adaptadores/antigravity/gerar-agent-json.sh
Luiz Silva bf9be375e2 refactor: remover extensões .md de todos os artefatos — v3.0.0
- 55 arquivos .md renomeados para sem extensão
- Convenção: artefatos de conhecimento sem extensão, scripts (.sh) e dados (.json) mantêm extensão
- Scripts atualizados: gerar-manifesto.sh, atualizar-agente.sh, atualizar-agente.global.sh, gerar-agent-json.sh, verificar-linhas.sh
- Documentação reescrita: AGENTS, README, EXTEND para refletir uso como submodule .agent direto
- manifesto.json regenerado v3.0.0
- Todas as referências internas entre artefatos atualizadas

BREAKING CHANGE: arquivos não possuem mais extensão .md
2026-06-19 16:04:37 -03:00

124 lines
4.1 KiB
Bash
Executable file

#!/bin/sh
# =============================================================================
# gerar-agent-json.sh — Adaptador Antigravity
# Gera o agent.json de cada agente a partir do persona
#
# Uso:
# ./gerar-agent-json.sh # gera para todos os agentes
# ./gerar-agent-json.sh ag-back # gera para um agente específico
#
# Dependências: yq (para parsear frontmatter YAML)
# Instalar: sudo snap install yq
# =============================================================================
AGENTS_DIR="$(cd "$(dirname "$0")/../.." && pwd)/agents"
GLOBAL_RULES_DIR="$(cd "$(dirname "$0")/../.." && pwd)/global/rules"
gerar_para_agente() {
AGENTE="$1"
PERSONA="$AGENTS_DIR/$AGENTE/persona"
if [ ! -f "$PERSONA" ]; then
echo "⚠ Persona não encontrada: $PERSONA"
return 1
fi
# Extrair frontmatter (entre os dois ---)
FRONTMATTER=$(sed -n '/^---$/,/^---$/p' "$PERSONA" | grep -v "^---$")
# Ler campos do frontmatter com yq
DISPLAY_NAME=$(echo "$FRONTMATTER" | yq '.display_name' 2>/dev/null || echo "$AGENTE")
DESCRIPTION=$(echo "$FRONTMATTER" | yq '.description' 2>/dev/null || echo "")
TOOLS=$(echo "$FRONTMATTER" | yq '.tools[]' 2>/dev/null | tr '\n' ',' | sed 's/,$//')
# Ler corpo do persona (após o segundo ---)
BODY=$(awk '/^---$/{c++; next} c>=2' "$PERSONA")
# Ler rules globais
GLOBAL_RULES=""
for rule_file in "$GLOBAL_RULES_DIR"/*; do
[ -f "$rule_file" ] || continue
case "$rule_file" in *.sh|*.json) continue ;; esac
GLOBAL_RULES="$GLOBAL_RULES\n\n### $(basename "$rule_file")\n$(cat "$rule_file")"
done
# Ler rules do agente
AGENT_RULES=""
AGENT_RULES_DIR="$AGENTS_DIR/$AGENTE/rules"
if [ -d "$AGENT_RULES_DIR" ]; then
for rule_file in "$AGENT_RULES_DIR"/*; do
[ -f "$rule_file" ] || continue
case "$rule_file" in *.sh|*.json) continue ;; esac
AGENT_RULES="$AGENT_RULES\n\n### $(basename "$rule_file")\n$(cat "$rule_file")"
done
fi
# Construir lista de tools como array JSON
TOOLS_JSON=""
if command -v yq >/dev/null 2>&1; then
TOOLS_JSON=$(echo "$FRONTMATTER" | yq -o=json '.tools // []' 2>/dev/null || echo '[]')
else
# fallback: lista padrão de tools
TOOLS_JSON='["view_file","replace_file_content","multi_replace_file_content","write_to_file","run_command","grep_search","list_dir","send_message"]'
fi
# Gerar agent.json
OUTPUT="$AGENTS_DIR/$AGENTE/agent.json"
# Escapar caracteres especiais para JSON
BODY_JSON=$(printf '%s' "$BODY" | python3 -c "import sys,json; print(json.dumps(sys.stdin.read()))" 2>/dev/null || printf '"%s"' "$BODY")
GLOBAL_RULES_JSON=$(printf '%b' "$GLOBAL_RULES" | python3 -c "import sys,json; print(json.dumps(sys.stdin.read()))" 2>/dev/null || echo '""')
AGENT_RULES_JSON=$(printf '%b' "$AGENT_RULES" | python3 -c "import sys,json; print(json.dumps(sys.stdin.read()))" 2>/dev/null || echo '""')
DESC_JSON=$(printf '%s' "$DESCRIPTION" | python3 -c "import sys,json; print(json.dumps(sys.stdin.read()))" 2>/dev/null || echo '""')
NAME_JSON=$(printf '%s' "$DISPLAY_NAME" | python3 -c "import sys,json; print(json.dumps(sys.stdin.read()))" 2>/dev/null || echo "\"$AGENTE\"")
cat > "$OUTPUT" << ENDJSON
{
"name": "$AGENTE",
"display_name": $NAME_JSON,
"description": $DESC_JSON,
"hidden": false,
"config": {
"customAgent": {
"systemPromptSections": [
{
"title": "Identidade e Escopo",
"content": $BODY_JSON
},
{
"title": "Rules Globais (herdadas)",
"content": $GLOBAL_RULES_JSON
},
{
"title": "Rules do Agente",
"content": $AGENT_RULES_JSON
}
],
"toolNames": $TOOLS_JSON,
"systemPromptConfig": {
"includeSections": [
"user_information",
"user_rules",
"skills",
"subagent_reminder",
"messaging",
"artifacts"
]
}
}
}
}
ENDJSON
echo "✅ Gerado: $OUTPUT"
}
# Executar para todos os agentes ou para o especificado
if [ -n "$1" ]; then
gerar_para_agente "$1"
else
for agente_dir in "$AGENTS_DIR"/*/; do
agente=$(basename "$agente_dir")
gerar_para_agente "$agente"
done
fi