156 lines
4.6 KiB
Bash
Executable file
156 lines
4.6 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
# ==============================================================================
|
|
# Utilitário Canônico do Graphify (.agent/scripts/graphify.sh)
|
|
# Gerencia instalação automática via Bun, atualização idempotente do .gitignore,
|
|
# geração sob demanda (ensure) e consultas ao Grafo de Conhecimento para /plano.
|
|
# ==============================================================================
|
|
|
|
set -e
|
|
|
|
export PATH="$HOME/.bun/bin:$PATH"
|
|
export ENGRAM_QUIET_DEPRECATION=1
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
|
|
|
# Resolver diretório raiz do projeto hospedeiro
|
|
resolver_raiz_projeto() {
|
|
local super_dir
|
|
super_dir=$(git -C "$SCRIPT_DIR" rev-parse --show-superproject-working-tree 2>/dev/null || true)
|
|
if [ -n "$super_dir" ]; then
|
|
echo "$super_dir"
|
|
return
|
|
fi
|
|
|
|
local candidato
|
|
candidato="$(cd "$SCRIPT_DIR/../.." && pwd)"
|
|
if [ -d "$candidato/.agent" ] || [ -f "$candidato/memorias-ia/modulo.yml" ]; then
|
|
echo "$candidato"
|
|
return
|
|
fi
|
|
|
|
pwd
|
|
}
|
|
|
|
SUPER_DIR="$(resolver_raiz_projeto)"
|
|
|
|
# 1. Garantir entradas do Graphify no .gitignore do projeto hospedeiro
|
|
garantir_gitignore() {
|
|
local gitignore_file="$SUPER_DIR/.gitignore"
|
|
if [ ! -f "$gitignore_file" ]; then
|
|
touch "$gitignore_file"
|
|
fi
|
|
|
|
local adicionou_cabecalho=0
|
|
if grep -qE "^# Graphify Knowledge Graph" "$gitignore_file" 2>/dev/null; then
|
|
adicionou_cabecalho=1
|
|
fi
|
|
|
|
for entrada in ".graphify/" ".engram/" "graphify-out/" ".evidencias/"; do
|
|
if ! grep -qxF "$entrada" "$gitignore_file" 2>/dev/null; then
|
|
if [ "$adicionou_cabecalho" -eq 0 ]; then
|
|
printf "\n# Graphify Knowledge Graph\n" >> "$gitignore_file"
|
|
adicionou_cabecalho=1
|
|
fi
|
|
printf "%s\n" "$entrada" >> "$gitignore_file"
|
|
fi
|
|
done
|
|
}
|
|
|
|
# 2. Garantir que o Graphify esteja instalado na máquina (via Bun)
|
|
garantir_instalacao() {
|
|
local forcar="${1:-0}"
|
|
if [ "$forcar" = "1" ] || { ! command -v graphify >/dev/null 2>&1 && ! command -v engram >/dev/null 2>&1; }; then
|
|
if command -v bun >/dev/null 2>&1; then
|
|
echo "📦 Instalando/atualizando Graphify globalmente via Bun (@sentropic/graphify)..." >&2
|
|
bun i -g @sentropic/graphify >&2 || true
|
|
fi
|
|
fi
|
|
}
|
|
|
|
# 3. Executar o binário do Graphify com fallback para bunx / bun x
|
|
executar_graphify() {
|
|
if command -v graphify >/dev/null 2>&1; then
|
|
graphify "$@"
|
|
elif command -v engram >/dev/null 2>&1; then
|
|
engram "$@"
|
|
elif command -v bunx >/dev/null 2>&1; then
|
|
bunx @sentropic/graphify "$@"
|
|
elif command -v bun >/dev/null 2>&1; then
|
|
bun x @sentropic/graphify "$@"
|
|
else
|
|
echo "Erro: Graphify não encontrado no PATH e Bun (bun/bunx) não está disponível na máquina." >&2
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# 4. Garantir existência do grafo sob demanda (modo ensure)
|
|
garantir_grafo() {
|
|
garantir_gitignore
|
|
garantir_instalacao 0
|
|
if [ ! -f "$SUPER_DIR/.engram/graph.json" ] && [ ! -f "$SUPER_DIR/.graphify/graph.json" ]; then
|
|
echo "📊 Grafo de conhecimento ainda não existe em $SUPER_DIR. Gerando automaticamente..." >&2
|
|
(cd "$SUPER_DIR" && executar_graphify update . --no-description >&2)
|
|
fi
|
|
}
|
|
|
|
SUBCOMANDO="${1:-update}"
|
|
if [ $# -gt 0 ]; then
|
|
shift
|
|
fi
|
|
|
|
case "$SUBCOMANDO" in
|
|
install)
|
|
garantir_gitignore
|
|
garantir_instalacao 1
|
|
echo "✅ Graphify instalado e .gitignore atualizado em $SUPER_DIR!"
|
|
;;
|
|
gitignore)
|
|
garantir_gitignore
|
|
echo "✅ .gitignore verificado e atualizado em $SUPER_DIR!"
|
|
;;
|
|
ensure)
|
|
garantir_grafo
|
|
;;
|
|
update)
|
|
garantir_gitignore
|
|
garantir_instalacao 0
|
|
echo "📊 Atualizando o grafo de conhecimento (Graphify) em $SUPER_DIR..."
|
|
(cd "$SUPER_DIR" && executar_graphify update . --no-description "$@")
|
|
echo "✅ Grafo atualizado com sucesso!"
|
|
;;
|
|
query)
|
|
if [ $# -lt 1 ]; then
|
|
echo "Uso: sh .agent/scripts/graphify.sh query \"<termo-ou-pergunta>\" [budget]" >&2
|
|
exit 1
|
|
fi
|
|
TERMO="$1"
|
|
BUDGET="${2:-1500}"
|
|
garantir_grafo
|
|
(cd "$SUPER_DIR" && executar_graphify query "$TERMO" --budget "$BUDGET")
|
|
;;
|
|
explain)
|
|
if [ $# -lt 1 ]; then
|
|
echo "Uso: sh .agent/scripts/graphify.sh explain \"<no-ou-arquivo>\"" >&2
|
|
exit 1
|
|
fi
|
|
garantir_grafo
|
|
(cd "$SUPER_DIR" && executar_graphify explain "$@")
|
|
;;
|
|
impact)
|
|
garantir_grafo
|
|
if [ $# -eq 0 ]; then
|
|
(cd "$SUPER_DIR" && executar_graphify review-delta --depth 2)
|
|
else
|
|
CSV_FILES=$(IFS=,; echo "$*")
|
|
(cd "$SUPER_DIR" && executar_graphify review-delta --files "$CSV_FILES" --depth 2)
|
|
fi
|
|
;;
|
|
summary)
|
|
garantir_grafo
|
|
(cd "$SUPER_DIR" && executar_graphify summary --top-hubs 5 --top-communities 5 "$@")
|
|
;;
|
|
*)
|
|
garantir_grafo
|
|
(cd "$SUPER_DIR" && executar_graphify "$SUBCOMANDO" "$@")
|
|
;;
|
|
esac
|