---
name: multi-profile-management
description: Creazione, configurazione e gestione di profili Hermes multipli e indipendenti con gateway Telegram separati. Include isolamento tools, limitazione accessi e condivisione dati tra profili.
author: Emma
---

# Multi-Profile Management

Skill per gestire più profili Hermes indipendenti sullo stesso server, ciascuno con il proprio agente, gateway Telegram e set di tools.

## Quando Usarla

- Creare un secondo agente per un collega (es. SuperLuca per Raffaele)
- Isolare un agente con accesso limitato (sola lettura FAK, nessun terminale)
- Aggiungere un nuovo utente Telegram con un bot dedicato

## Workflow Completo

### 1. Creare il Profilo

```bash
hermes profile create <nome-profilo>
```

Crea:
- Directory in `~/.hermes/profiles/<nome>/`
- SOUL.md vuota
- Skills bundle sincronizzate
- Wrapper CLI (`~/.local/bin/<nome>`)

### 2. Personalizzare SOUL.md

Scrivere `~/.hermes/profiles/<nome>/SOUL.md` con:
- Identità e nome dell'agente
- Chi è l'utente (ruolo, azienda, stile comunicativo)
- Base di conoscenza pregressa
- Regole di comportamento
- Design system aziendale (se applicabile)
- Rapporti con altri agenti (es. Emma 🦊)

### 3. Configurare API Key

Il profilo ha bisogno di un `.env` con le chiavi API:

```
# ~/.hermes/profiles/<nome>/.env
DEEPSEEK_API_KEY=sk-xxxx...xxxx
GATEWAY_ALLOW_ALL_USERS=true
```

**Attenzione:** Usare la stessa DEEPSEEK_API_KEY del profilo principale (gratuita su api.deepseek.com).

### 4. Creare Bot Telegram

1. Su Telegram, aprire **@BotFather**
2. `/newbot` → scegliere nome e username (finisce con `Bot`)
3. Copiare il **token** (es. `123456789:ABCdef...`)
4. Il token può contenere `!` — in bash va gestito con single quotes o Python

### 5. Limitare i Tools (Opzionale)

Per accesso limitato (es. solo consultazione FAK):

```bash
HERMES_HOME=/home/oem/.hermes/profiles/<nome> <nome> tools disable terminal
HERMES_HOME=/home/oem/.hermes/profiles/<nome> <nome> tools disable file
HERMES_HOME=/home/oem/.hermes/profiles/<nome> <nome> tools disable cronjob
HERMES_HOME=/home/oem/.hermes/profiles/<nome> <nome> tools disable delegation
HERMES_HOME=/home/oem/.hermes/profiles/<nome> <nome> tools disable session_search
HERMES_HOME=/home/oem/.hermes/profiles/<nome> <nome> tools disable messaging
HERMES_HOME=/home/oem/.hermes/profiles/<nome> <nome> tools disable skills
HERMES_HOME=/home/oem/.hermes/profiles/<nome> <nome> tools disable memory
HERMES_HOME=/home/oem/.hermes/profiles/<nome> <nome> tools disable code_execution
HERMES_HOME=/home/oem/.hermes/profiles/<nome> <nome> tools disable browser
HERMES_HOME=/home/oem/.hermes/profiles/<nome> <nome> tools disable vision
```

Tools minimi per funzionare: `web`, `clarify`, `memory` (necessario per il salvataggio sessioni).

### 6. Installare Gateway Telegram

```bash
HERMES_HOME=/home/oem/.hermes/profiles/<nome> <nome> gateway install
```

Rispondere:
- Y → Start the gateway now
- Y → Start automatically on boot

### 7. Passare il Token Telegram

**⚠️ PROBLEMA NOTO:** systemd non carica automaticamente il `.env` del profilo anche con `HERMES_HOME` impostato. Soluzioni:

**Opzione A (consigliata):** Modificare il service systemd per includere il token via `Environment`:

```bash
# Il service viene creato in:
# ~/.config/systemd/user/hermes-gateway-<nome>.service
# Aggiungere manualmente:
Environment="TELEGRAM_BOT_TOKEN=<token>"
```

**Opzione B:** Aggiungere `EnvironmentFile` al service:
```
EnvironmentFile=/home/oem/.hermes/profiles/<nome>/.env
```

Poi:
```bash
systemctl --user daemon-reload
systemctl --user restart hermes-gateway-<nome>
```

### 8. Verificare Collegamento Telegram

```bash
journalctl --user -u hermes-gateway-<nome> --no-pager | grep "telegram"
```

Cercare: `✓ telegram connected`

In alternativa testare il token direttamente:
```bash
curl -s 'https://api.telegram.org/bot<TOKEN>/getMe'
```

**Nota:** Usare SEMPRE single quotes per l'URL se il token contiene `!`

## Condivisione Dati tra Profili

I profili condividono la stessa macchina Linux e lo stesso filesystem. Per condividere dati FAK senza dare accesso al file system:

### API HTTP per Dati Condivisi

Creare un piccolo server API che espone i dati in sola lettura:

```python
# ~/.hermes/fak-api.py
import json, os, http.server
HOME = os.path.expanduser('~/.hermes')

class FAKHandler(http.server.BaseHTTPRequestHandler):
    def do_GET(self):
        path = self.path.rstrip('/')
        if path == '/fak':
            self._serve_json(self._load_all())
        elif path.startswith('/fak/search?q='):
            q = self._param('q', '').upper()
            self._serve_json(self._search(q))
        # ...
    
    def _serve_json(self, obj):
        self.send_response(200)
        self.send_header('Content-Type', 'application/json')
        self.send_header('Access-Control-Allow-Origin', '*')
        self.end_headers()
        self.wfile.write(json.dumps(obj, indent=2).encode())

if __name__ == '__main__':
    server = http.server.HTTPServer(('0.0.0.0', 8081), FAKHandler)
    server.serve_forever()
```

Avviare in background:
```bash
cd /home/oem/.hermes && python3 fak-api.py
```

Poi l'agente con solo `web` può consultare: `web_extract(urls=['http://localhost:8081/fak'])`

## ⚠️ Pitfall — Token Telegram con `!`

Il carattere `!` (esclamativo) nei token Telegram **NON** è un problema per Telegram ma **lo è per bash** — attiva history expansion.

**Sintomi:** comandi con `echo` o `printf` falliscono con `EOF non atteso` o token troncato.

**Soluzioni:**
- Usare **Python** per scrivere il file (non bash `echo`)
- Usare **single quotes** negli URL curl: `curl -s 'https://api.telegram.org/bot<TOKEN>/getMe'`
- Nel service systemd, usare doppi apici: `Environment="TELEGRAM_BOT_TOKEN=<token>"`
- Usare `write_file` di Hermes per creare file con token

## ⚠️ Pitfall — Gateway Systemd non vede il .env

Il gateway Hermes, quando avviato via systemd con `--profile <nome>`, imposta `HERMES_HOME` ma **non carica automaticamente** il `.env` del profilo. Di conseguenza `os.getenv("TELEGRAM_BOT_TOKEN")` restituisce `None` e Telegram non viene abilitato.

**Sintomo:** `WARNING gateway.run: No messaging platforms enabled.`

**Soluzione definitiva:** Inserire `TELEGRAM_BOT_TOKEN` come `Environment=` direttamente nel file `.service` di systemd (non nel `.env`).

## Verifica Finale

- Gateway attivo: `systemctl --user status hermes-gateway-<nome>`
- Telegram connesso: `journalctl --user -u hermes-gateway-<nome> | grep "telegram connected"`
- Bot funzionante: cercare il bot su Telegram e scrivere "Ciao"
- Tools corretti: `HERMES_HOME=/home/oem/.hermes/profiles/<nome> <nome> tools list`
