#!/usr/bin/env bash # © 2026 Delminio — tutti i diritti riservati. Software proprietario: l'uso con Delminio è regolato dal contratto con cui si usa il servizio; la lettura e la verifica da LICENCE. sonda_sandbox_codex() { python3 - "${DELMINIO_CLI:-codex}" <<'CODEX_SONDA' import subprocess, sys if sys.platform != 'linux': sys.exit(0) cli = sys.argv[1] try: r = subprocess.run([cli, 'sandbox', '-c', 'sandbox_mode="read-only"', '--', 'true'], capture_output=True, text=True, timeout=5) print('ok' if r.returncode == 0 else 'rotta') except (OSError, subprocess.TimeoutExpired): print('non_verificata') CODEX_SONDA } porta_del_lancio() { # porta_del_lancio local v p f="$STATO/sistema-$$.txt" codice=$( (umask 077; curl -sS -m 30 -G -o "$f" -w '%{http_code}' -K <(_chiave "$TOKEN") \ --data-urlencode "ordine=$porta" --data-urlencode "modello=${DELMINIO_MODELLO:-}" \ "$URL/api/porte/$1") 2>/dev/null || true) if [ "$codice" != 200 ] || [ ! -s "$f" ]; then _f "$M_delminio_dato_system" url "$URL" porta "$porta" codice "${codice:-000}" >&2 [ -s "$f" ] && { head -c 400 "$f" >&2; echo >&2; } echo "$2" >&2 rm -f "$f" exit 1 fi for v in "$STATO"/sistema-*.txt "$STATO"/qwen-posta-*.jsonl "$STATO"/qwen-mcp-*.json; do [ -e "$v" ] || continue p=${v##*/sistema-}; p=${p##*/qwen-posta-}; p=${p##*/qwen-mcp-}; p=${p%.txt}; p=${p%.jsonl}; p=${p%.json} [ "$p" = "$$" ] || kill -0 "$p" 2>/dev/null || rm -f "$v" done } case "${1:-}" in claude) shift if [ "${1:-}" = --ruolo ] && [ -n "${2:-}" ]; then DELMINIO_PORTA=$2; shift 2 fi porta="${DELMINIO_PORTA:-principale}" export DELMINIO_PORTA="$porta" rotta=nascita; [ "${DELMINIO_NASCITA:-intera}" = porta ] && rotta=sistema porta_del_lancio "$rotta" "$M_agente_parte_meta" export DELMINIO_SISTEMA=1 if [ -z "${DELMINIO_SENZA_PLUGIN:-}" ] && [ -f "$(dirname "$0")/plugin.py" ] \ && [ "$(git rev-parse --path-format=absolute --git-common-dir 2>/dev/null)" \ = "$(git -C "$(dirname "$0")/.." rev-parse --path-format=absolute --git-common-dir 2>/dev/null)" ]; then DELMINIO_CLAUDE="${DELMINIO_CLI:-claude}" python3 "$(dirname "$0")/plugin.py" claude-al-lancio \ "$(git rev-parse --show-toplevel 2>/dev/null || pwd)" >/dev/null 2>&1 || true fi squadra=() if [ -z "${DELMINIO_SENZA_PLUGIN:-}" ] && [ -f "$(dirname "$0")/squadra.py" ]; then while IFS= read -r x; do [ -n "$x" ] && squadra+=("$x"); done < <(python3 "$(dirname "$0")/squadra.py" \ lancio claude "$(git rev-parse --show-toplevel 2>/dev/null || pwd)" 2>/dev/null) fi exec "${DELMINIO_CLI:-claude}" --system-prompt-file "$f" ${squadra[@]+"${squadra[@]}"} "$@" ;; codex) shift export DELMINIO_CLI_NOME=codex DELMINIO_SANDBOX_LINUX=$(sonda_sandbox_codex); export DELMINIO_SANDBOX_LINUX if [ "${1:-}" = --ruolo ] && [ -n "${2:-}" ]; then DELMINIO_PORTA=$2; shift 2 fi porta="${DELMINIO_PORTA:-principale}" export DELMINIO_PORTA="$porta" [ "${DELMINIO_NASCITA:-intera}" = porta ] && { echo "$M_ruolo_sola_sua" >&2 exit 1; } porta_del_lancio nascita "$M_agente_parte_meta_2" export DELMINIO_SISTEMA=1 mcp=() if [ -n "${DELMINIO_SENZA_PLUGIN:-}" ] || [ -z "${CODEX_HOME:-}" ] \ || ! DELMINIO_CODEX="${DELMINIO_CLI:-codex}" python3 "$(dirname "$0")/plugin.py" codex "$CODEX_HOME" >/dev/null 2>&1; then [ -z "${DELMINIO_SENZA_PLUGIN:-}" ] && echo "$M_codex_senza_plugin" >&2 export DELMINIO_MCP_TOKEN="$TOKEN" mcp=(-c "mcp_servers.memoria.url=\"$URL/mcp\"" -c 'mcp_servers.memoria.bearer_token_env_var="DELMINIO_MCP_TOKEN"' -c 'mcp_servers.memoria.env_http_headers={"X-Delminio-Modello"="DELMINIO_MODELLO"}' -c 'mcp_servers.memoria.tools.delminio.approval_mode="approve"' -c 'mcp_servers.memoria.tools.delminio_write.approval_mode="approve"') fi squadra=() if [ -z "${DELMINIO_SENZA_PLUGIN:-}" ] && [ -n "${CODEX_HOME:-}" ] && [ -f "$(dirname "$0")/squadra.py" ]; then while IFS= read -r x; do [ -n "$x" ] && squadra+=("$x"); done < <(python3 "$(dirname "$0")/squadra.py" \ lancio codex "$CODEX_HOME" 2>/dev/null) fi case "${DELMINIO_TETTO_USCITE:-}" in ''|*[!0-9]*) DELMINIO_TETTO_USCITE=25000 ;; esac tetto=(-c "tool_output_token_limit=$DELMINIO_TETTO_USCITE") case "${DELMINIO_MIE:-}" in *"|tool_output_token_limit|"*) tetto=() ;; esac gesto=(-c check_for_update_on_startup=false ${mcp[@]+"${mcp[@]}"} ${squadra[@]+"${squadra[@]}"} ${tetto[@]+"${tetto[@]}"} -c "model_instructions_file=\"$f\"") tutti=() while IFS= read -r -d '' x; do tutti+=("$x"); done < <(python3 -c 'import sys n = int(sys.argv[1]); g, a = sys.argv[2:2 + n], sys.argv[2 + n:] CON_VALORE = {"-c", "--config", "-m", "--model", "-p", "--profile", "-s", "--sandbox", "-a", "--ask-for-approval", "-C", "--cd", "--add-dir", "-i", "--image", "--enable", "--disable", "--remote", "--remote-auth-token-env", "--local-provider"} i, sub = 0, None while i < len(a): x = a[i] if x == "--": break if x in CON_VALORE: i += 2; continue if x.startswith("-"): i += 1; continue if x in ("exec", "e", "review", "resume", "fork"): sub = i break if sub is None: out = g + a else: resto, c, j = [], [], 0 while j < sub: if a[j] in ("-c", "--config") and j + 1 < sub: c += a[j:j + 2]; j += 2 else: resto.append(a[j]); j += 1 out = resto + [a[sub]] + g + c + a[sub + 1:] sys.stdout.write("".join(x + "\0" for x in out))' "${#gesto[@]}" "${gesto[@]}" "$@") [ ${#tutti[@]} -ge ${#gesto[@]} ] || { echo "$M_delminio_argomenti_codex" >&2; exit 1; } exec "${DELMINIO_CLI:-codex}" --no-daemon "${tutti[@]}" ;; app-codex) shift p="$(dirname "$0")/plugin.py" case "${1:-}" in "") python3 "$p" app-codex; e=$? case $e in 0) echo "$M_app_codex_fatto" ;; 4) echo "$M_app_codex_assente" ;; *) echo "$M_app_codex_non_riuscito" >&2; exit 1 ;; esac ;; via) python3 "$p" app-codex via && echo "$M_app_codex_tolto" ;; no) python3 "$p" app-codex no && echo "$M_app_codex_no" ;; stato) python3 "$p" app-codex stato ;; *) echo "$M_app_codex_uso" >&2; exit 2 ;; esac ;; qwen) shift export DELMINIO_CLI_NOME=qwen if [ "${1:-}" = --ruolo ] && [ -n "${2:-}" ]; then DELMINIO_PORTA=$2; shift 2 fi porta="${DELMINIO_PORTA:-principale}" export DELMINIO_PORTA="$porta" rotta=nascita; mcp_token="$TOKEN" if [ "${DELMINIO_NASCITA:-intera}" = porta ]; then rotta=sistema mcp_token=$(python3 -c 'import json,sys s = json.load(open(sys.argv[1]))["mcpServers"] print((s.get("memoria-ruolo") or s["delminio-ruolo"])["headers"]["Authorization"].split(" ", 1)[1])' \ "${DELMINIO_CANALE_RUOLO:-}" 2>/dev/null) || mcp_token="" [ -n "$mcp_token" ] || { echo "$M_ruolo_sola_sua_2" >&2; exit 1; } fi posta="$STATO/qwen-posta-$$.jsonl" (umask 077; : > "$posta") porta_del_lancio "$rotta" "$M_agente_parte_meta_2" export QWEN_SYSTEM_MD="$f" DELMINIO_SISTEMA=1 DELMINIO_QWEN_POSTA="$posta" radice=$(git rev-parse --show-toplevel 2>/dev/null || true) if [ -n "$radice" ] && [ ! -e "$radice/.qwen/.env" ]; then (umask 077; mkdir -p "$radice/.qwen" && echo "# delminio" > "$radice/.qwen/.env") 2>/dev/null || true escludi=$(cd "$radice" && git rev-parse --path-format=absolute --git-path info/exclude 2>/dev/null) || escludi="" [ -n "$escludi" ] && { grep -qxF ".qwen/.env" "$escludi" 2>/dev/null || echo ".qwen/.env" >> "$escludi"; } 2>/dev/null fi if [ -n "${QWEN_HOME:-}" ] && [ -f "$(dirname "$0")/plugin.py" ]; then if [ "$rotta" = nascita ] && [ -z "${DELMINIO_SENZA_PLUGIN:-}" ] \ && DELMINIO_QWEN="${DELMINIO_CLI:-qwen}" python3 "$(dirname "$0")/plugin.py" qwen "$QWEN_HOME" >/dev/null 2>&1; then exec "${DELMINIO_CLI:-qwen}" --input-file "$posta" "$@" fi DELMINIO_QWEN="${DELMINIO_CLI:-qwen}" python3 "$(dirname "$0")/plugin.py" qwen-via "$QWEN_HOME" >/dev/null 2>&1 || true [ "$rotta" = nascita ] && [ -z "${DELMINIO_SENZA_PLUGIN:-}" ] && echo "$M_qwen_senza_plugin" >&2 fi mcp="$STATO/qwen-mcp-$$.json" (umask 077; DELMINIO_T="$mcp_token" python3 -c 'import json,os,sys print(json.dumps({"mcpServers": {"memoria": { "command": "bash", "args": [sys.argv[2], "ponte-mcp", sys.argv[1]], "env": {"DELMINIO_T": os.environ["DELMINIO_T"], "DELMINIO_MODELLO": os.environ.get("DELMINIO_MODELLO", "")}, "timeout": 60000, "trust": True, "alwaysLoadTools": True}}}))' "$URL" "$(cd "$(dirname "$0")" && pwd)/$(basename "$0")" > "$mcp") && [ -s "$mcp" ] \ || { echo "$M_delminio_mcp_qwen" >&2; rm -f "$mcp"; exit 1; } exec "${DELMINIO_CLI:-qwen}" --input-file "$posta" --mcp-config "$mcp" "$@" ;; opencode) shift export DELMINIO_CLI_NOME=opencode porta="${DELMINIO_PORTA:-principale}" export DELMINIO_PORTA="$porta" porta_del_lancio nascita "$M_agente_parte_meta_2" export DELMINIO_SISTEMA=1 radice=$(git rev-parse --show-toplevel 2>/dev/null || pwd) [ -f "$radice/.opencode/plugins/delminio.js" ] || python3 "$(dirname "$0")/plugin.py" opencode "$radice" >/dev/null 2>&1 || true squadra="{}" if [ -z "${DELMINIO_SENZA_PLUGIN:-}" ] && [ -f "$(dirname "$0")/squadra.py" ]; then squadra=$(python3 "$(dirname "$0")/squadra.py" lancio opencode "$radice" 2>/dev/null) || squadra="{}" fi gratuito="" if [ -n "${DELMINIO_OPENCODE_GRATUITO:-}" ]; then ricordo="${XDG_STATE_HOME:-$HOME/.local/state}/delminio/opencode-gratuito" oggi=$(date +%F) giorno=""; [ -f "$ricordo" ] && read -r giorno gratuito < "$ricordo" if [ "${giorno:-}" != "$oggi" ] || [ -z "$gratuito" ]; then echo "$M_opencode_gratuito_scelgo" >&2 gratuito=$(python3 "$(cd "$(dirname "$0")" && pwd)/eseguibili.py" modello-gratuito "${DELMINIO_CLI:-$(command -v opencode)}" 2>/dev/null) || gratuito="" if [ -n "$gratuito" ]; then mkdir -p "$(dirname "$ricordo")" && printf '%s %s\n' "$oggi" "$gratuito" > "$ricordo" else _f "$M_opencode_gratuito_nessuno" nome "$DELMINIO_OPENCODE_GRATUITO" >&2 fi fi fi OPENCODE_CONFIG_CONTENT=$(DELMINIO_F="$f" DELMINIO_OPENCODE_SQUADRA="$squadra" DELMINIO_GRATUITO_SCELTO="$gratuito" python3 -c 'import json, os c = json.loads(os.environ.get("DELMINIO_OPENCODE_CONFIG") or "{}") if os.environ.get("DELMINIO_GRATUITO_SCELTO"): c["model"] = os.environ["DELMINIO_GRATUITO_SCELTO"] porta = "{file:" + os.environ["DELMINIO_F"] + "}" c.setdefault("agents", {}).setdefault("build", {})["system"] = porta def unisci(a, b): for k, v in b.items(): if isinstance(v, dict) and isinstance(a.get(k), dict): unisci(a[k], v) elif isinstance(v, list) and isinstance(a.get(k), list): a[k] += [x for x in v if x not in a[k]] else: a.setdefault(k, v) try: sq = json.loads(os.environ.get("DELMINIO_OPENCODE_SQUADRA") or "{}") except ValueError: sq = {} if isinstance(sq, dict): unisci(c, sq) print(json.dumps(c))') || { echo "$M_agente_parte_meta_2" >&2; exit 1; } export OPENCODE_CONFIG_CONTENT OPENCODE_DISABLE_AUTOUPDATE=1 OPENCODE_DISABLE_CLAUDE_CODE_PROMPT=1 exec "${DELMINIO_CLI:-opencode}" --standalone "$@" ;; esac