Skip to content

An always-on second brain you talk to. Voice notes in Telegram → typed, linked knowledge in your Obsidian vault. Runs 24/7 on the Claude subscription you already have.

3.8 KB
Raw Blame
#!/bin/bash
# dbrain — one command to control your d-brain assistant.
#
# Usage: dbrain <status|restart|logs|doctor|clear|attach|repair|login>
#
# Designed for non-technical users: every command prints plain-language output
# and the next thing to try if something is wrong.
set -euo pipefail

RUNTIME_DIR="${DBRAIN_RUNTIME_DIR:-$HOME/.dbrain}"
PROJECT_FILE="$RUNTIME_DIR/project.path"
PROJECT_DIR="${DBRAIN_PROJECT:-$( [ -f "$PROJECT_FILE" ] && cat "$PROJECT_FILE" || echo "$HOME/projects/dbrain" )}"
NAME_FILE="$RUNTIME_DIR/brain.name"
BRAIN="$( [ -f "$NAME_FILE" ] && cat "$NAME_FILE" || echo "dbrain" )"

cmd="${1:-status}"

_uv() { (cd "$PROJECT_DIR" && uv run "$@"); }

case "$cmd" in
  status)
    echo "📋 Статус d-brain"
    if [ -f "$RUNTIME_DIR/STATUS.md" ]; then cat "$RUNTIME_DIR/STATUS.md"; else echo "(нет STATUS.md — watchdog ещё не запускался)"; fi
    echo "— bot:      $(systemctl --user is-active dbrain-bot.service 2>/dev/null || echo unknown)"
    echo "— watchdog: $(systemctl --user is-active dbrain-watchdog.service 2>/dev/null || echo unknown)"
    if tmux has-session -t "$BRAIN" 2>/dev/null; then echo "— мозг:     жив ($BRAIN)"; else echo "— мозг:     НЕ запущен"; fi
    ;;
  restart)
    echo "♻️  Перезапускаю сервисы…"
    systemctl --user reset-failed dbrain-bot.service dbrain-watchdog.service 2>/dev/null || true
    systemctl --user restart dbrain-bot.service dbrain-watchdog.service
    echo "Готово. Проверь: dbrain status"
    ;;
  logs)
    journalctl --user -u dbrain-bot.service -n "${2:-80}" --no-pager 2>/dev/null || echo "Логи недоступны (нет systemd --user?)"
    ;;
  doctor)
    echo "🩺 Запускаю осмотр…"
    _uv python -m d_brain.services.doctor
    ;;
  clear)
    if tmux has-session -t "$BRAIN" 2>/dev/null; then
      tmux send-keys -t "$BRAIN" "/clear" Enter
      echo "🧹 Память сессии очищена (durable-данные в файлах не тронуты)."
    else
      echo "Мозг не запущен — нечего чистить. Попробуй: dbrain repair"
    fi
    ;;
  attach)
    exec tmux attach -t "$BRAIN"
    ;;
  repair)
    echo "🔧 Чиню: сброс ошибок + перезапуск…"
    systemctl --user reset-failed dbrain-bot.service dbrain-watchdog.service 2>/dev/null || true
    systemctl --user restart dbrain-bot.service dbrain-watchdog.service 2>/dev/null || true
    echo "Если не помогло, посмотри: dbrain logs"
    ;;
  login)
    echo "🔑 Запускаю вход в Claude (нужна подписка)…"
    echo "Открой ссылку ниже на телефоне/ноутбуке, подтверди и вставь код сюда:"
    # Pin the config dir to the one the brain runs under (the systemd units set
    # CLAUDE_CONFIG_DIR=$HOME/.claude), so the new token lands where the
    # session reads it — not in some other dir the login shell might point to.
    CLAUDE_CONFIG_DIR="${CLAUDE_CONFIG_DIR:-$HOME/.claude}" claude setup-token
    # Auth is read only at session START. The live tmux brain holds the old
    # (logged-out) auth in memory; `dbrain restart` won't help (KillMode=process
    # keeps the brain alive). Recreate BOTH sessions so the bot rebuilds them
    # with the fresh login on the next message / cron job.
    echo "♻️  Пересоздаю сессию, чтобы она подхватила новый вход…"
    tmux kill-session -t "$BRAIN" 2>/dev/null || true
    tmux kill-session -t "${BRAIN}_cron" 2>/dev/null || true
    echo "Готово. Бот поднимет сессию заново при следующем сообщении. Проверь: dbrain status"
    ;;
  *)
    echo "Usage: dbrain <status|restart|logs|doctor|clear|attach|repair|login>"
    exit 1
    ;;
esac