delminio-client / kit / it / lanciatori-client
raw · sha256 093faa8cff38806c959631fe1de5a1bd3840cba2816b26d772db061be7490caf
1#!/usr/bin/env bash
2# © 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.
3sonda_sandbox_codex() {
4 python3 - "${DELMINIO_CLI:-codex}" <<'CODEX_SONDA'
5import subprocess, sys
6if sys.platform != 'linux':
7 sys.exit(0)
8cli = sys.argv[1]
9try:
10 r = subprocess.run([cli, 'sandbox', '-c', 'sandbox_mode="read-only"', '--', 'true'],
11 capture_output=True, text=True, timeout=5)
12 print('ok' if r.returncode == 0 else 'rotta')
13except (OSError, subprocess.TimeoutExpired):
14 print('non_verificata')
15CODEX_SONDA
16}
17porta_del_lancio() { # porta_del_lancio <rotta> <frase per chi resta senza agente>
18 local v p
19 f="$STATO/sistema-$$.txt"
20 codice=$( (umask 077; curl -sS -m 30 -G -o "$f" -w '%{http_code}' -K <(_chiave "$TOKEN") \
21 --data-urlencode "ordine=$porta" --data-urlencode "modello=${DELMINIO_MODELLO:-}" \
22 "$URL/api/porte/$1") 2>/dev/null || true)
23 if [ "$codice" != 200 ] || [ ! -s "$f" ]; then
24 _f "$M_delminio_dato_system" url "$URL" porta "$porta" codice "${codice:-000}" >&2
25 [ -s "$f" ] && { head -c 400 "$f" >&2; echo >&2; }
26 echo "$2" >&2
27 rm -f "$f"
28 exit 1
29 fi
30 for v in "$STATO"/sistema-*.txt "$STATO"/qwen-posta-*.jsonl "$STATO"/qwen-mcp-*.json; do
31 [ -e "$v" ] || continue
32 p=${v##*/sistema-}; p=${p##*/qwen-posta-}; p=${p##*/qwen-mcp-}; p=${p%.txt}; p=${p%.jsonl}; p=${p%.json}
33 [ "$p" = "$$" ] || kill -0 "$p" 2>/dev/null || rm -f "$v"
34 done
35}
36case "${1:-}" in
37 claude)
38 shift
39 if [ "${1:-}" = --ruolo ] && [ -n "${2:-}" ]; then
40 DELMINIO_PORTA=$2; shift 2
41 fi
42 porta="${DELMINIO_PORTA:-principale}"
43 export DELMINIO_PORTA="$porta"
44 rotta=nascita; [ "${DELMINIO_NASCITA:-intera}" = porta ] && rotta=sistema
45 porta_del_lancio "$rotta" "$M_agente_parte_meta"
46 export DELMINIO_SISTEMA=1
47 if [ -z "${DELMINIO_SENZA_PLUGIN:-}" ] && [ -f "$(dirname "$0")/plugin.py" ] \
48 && [ "$(git rev-parse --path-format=absolute --git-common-dir 2>/dev/null)" \
49 = "$(git -C "$(dirname "$0")/.." rev-parse --path-format=absolute --git-common-dir 2>/dev/null)" ]; then
50 DELMINIO_CLAUDE="${DELMINIO_CLI:-claude}" python3 "$(dirname "$0")/plugin.py" claude-al-lancio \
51 "$(git rev-parse --show-toplevel 2>/dev/null || pwd)" >/dev/null 2>&1 || true
52 fi
53 squadra=()
54 if [ -z "${DELMINIO_SENZA_PLUGIN:-}" ] && [ -f "$(dirname "$0")/squadra.py" ]; then
55 while IFS= read -r x; do [ -n "$x" ] && squadra+=("$x"); done < <(python3 "$(dirname "$0")/squadra.py" \
56 lancio claude "$(git rev-parse --show-toplevel 2>/dev/null || pwd)" 2>/dev/null)
57 fi
58 exec "${DELMINIO_CLI:-claude}" --system-prompt-file "$f" ${squadra[@]+"${squadra[@]}"} "$@"
59 ;;
60 codex)
61 shift
62 export DELMINIO_CLI_NOME=codex
63 DELMINIO_SANDBOX_LINUX=$(sonda_sandbox_codex); export DELMINIO_SANDBOX_LINUX
64 if [ "${1:-}" = --ruolo ] && [ -n "${2:-}" ]; then
65 DELMINIO_PORTA=$2; shift 2
66 fi
67 porta="${DELMINIO_PORTA:-principale}"
68 export DELMINIO_PORTA="$porta"
69 [ "${DELMINIO_NASCITA:-intera}" = porta ] && {
70 echo "$M_ruolo_sola_sua" >&2
71 exit 1; }
72 porta_del_lancio nascita "$M_agente_parte_meta_2"
73 export DELMINIO_SISTEMA=1
74 mcp=()
75 if [ -n "${DELMINIO_SENZA_PLUGIN:-}" ] || [ -z "${CODEX_HOME:-}" ] \
76 || ! DELMINIO_CODEX="${DELMINIO_CLI:-codex}" python3 "$(dirname "$0")/plugin.py" codex "$CODEX_HOME" >/dev/null 2>&1; then
77 [ -z "${DELMINIO_SENZA_PLUGIN:-}" ] && echo "$M_codex_senza_plugin" >&2
78 export DELMINIO_MCP_TOKEN="$TOKEN"
79 mcp=(-c "mcp_servers.memoria.url=\"$URL/mcp\"" -c 'mcp_servers.memoria.bearer_token_env_var="DELMINIO_MCP_TOKEN"'
80 -c 'mcp_servers.memoria.env_http_headers={"X-Delminio-Modello"="DELMINIO_MODELLO"}'
81 -c 'mcp_servers.memoria.tools.delminio.approval_mode="approve"'
82 -c 'mcp_servers.memoria.tools.delminio_write.approval_mode="approve"')
83 fi
84 squadra=()
85 if [ -z "${DELMINIO_SENZA_PLUGIN:-}" ] && [ -n "${CODEX_HOME:-}" ] && [ -f "$(dirname "$0")/squadra.py" ]; then
86 while IFS= read -r x; do [ -n "$x" ] && squadra+=("$x"); done < <(python3 "$(dirname "$0")/squadra.py" \
87 lancio codex "$CODEX_HOME" 2>/dev/null)
88 fi
89 case "${DELMINIO_TETTO_USCITE:-}" in ''|*[!0-9]*) DELMINIO_TETTO_USCITE=25000 ;; esac
90 tetto=(-c "tool_output_token_limit=$DELMINIO_TETTO_USCITE")
91 case "${DELMINIO_MIE:-}" in *"|tool_output_token_limit|"*) tetto=() ;; esac
92 gesto=(-c check_for_update_on_startup=false ${mcp[@]+"${mcp[@]}"} ${squadra[@]+"${squadra[@]}"}
93 ${tetto[@]+"${tetto[@]}"}
94 -c "model_instructions_file=\"$f\"")
95 tutti=()
96 while IFS= read -r -d '' x; do tutti+=("$x"); done < <(python3 -c 'import sys
97n = int(sys.argv[1]); g, a = sys.argv[2:2 + n], sys.argv[2 + n:]
98CON_VALORE = {"-c", "--config", "-m", "--model", "-p", "--profile", "-s", "--sandbox", "-a",
99 "--ask-for-approval", "-C", "--cd", "--add-dir", "-i", "--image", "--enable", "--disable",
100 "--remote", "--remote-auth-token-env", "--local-provider"}
101i, sub = 0, None
102while i < len(a):
103 x = a[i]
104 if x == "--": break
105 if x in CON_VALORE: i += 2; continue
106 if x.startswith("-"): i += 1; continue
107 if x in ("exec", "e", "review", "resume", "fork"): sub = i
108 break
109if sub is None:
110 out = g + a
111else:
112 resto, c, j = [], [], 0
113 while j < sub:
114 if a[j] in ("-c", "--config") and j + 1 < sub: c += a[j:j + 2]; j += 2
115 else: resto.append(a[j]); j += 1
116 out = resto + [a[sub]] + g + c + a[sub + 1:]
117sys.stdout.write("".join(x + "\0" for x in out))' "${#gesto[@]}" "${gesto[@]}" "$@")
118 [ ${#tutti[@]} -ge ${#gesto[@]} ] || { echo "$M_delminio_argomenti_codex" >&2; exit 1; }
119 exec "${DELMINIO_CLI:-codex}" --no-daemon "${tutti[@]}"
120 ;;
121 app-codex)
122 shift
123 p="$(dirname "$0")/plugin.py"
124 case "${1:-}" in
125 "") python3 "$p" app-codex; e=$?
126 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 ;;
127 via) python3 "$p" app-codex via && echo "$M_app_codex_tolto" ;;
128 no) python3 "$p" app-codex no && echo "$M_app_codex_no" ;;
129 stato) python3 "$p" app-codex stato ;;
130 *) echo "$M_app_codex_uso" >&2; exit 2 ;;
131 esac
132 ;;
133 qwen)
134 shift
135 export DELMINIO_CLI_NOME=qwen
136 if [ "${1:-}" = --ruolo ] && [ -n "${2:-}" ]; then
137 DELMINIO_PORTA=$2; shift 2
138 fi
139 porta="${DELMINIO_PORTA:-principale}"
140 export DELMINIO_PORTA="$porta"
141 rotta=nascita; mcp_token="$TOKEN"
142 if [ "${DELMINIO_NASCITA:-intera}" = porta ]; then
143 rotta=sistema
144 mcp_token=$(python3 -c 'import json,sys
145s = json.load(open(sys.argv[1]))["mcpServers"]
146print((s.get("memoria-ruolo") or s["delminio-ruolo"])["headers"]["Authorization"].split(" ", 1)[1])' \
147 "${DELMINIO_CANALE_RUOLO:-}" 2>/dev/null) || mcp_token=""
148 [ -n "$mcp_token" ] || { echo "$M_ruolo_sola_sua_2" >&2; exit 1; }
149 fi
150 posta="$STATO/qwen-posta-$$.jsonl"
151 (umask 077; : > "$posta")
152 porta_del_lancio "$rotta" "$M_agente_parte_meta_2"
153 export QWEN_SYSTEM_MD="$f" DELMINIO_SISTEMA=1 DELMINIO_QWEN_POSTA="$posta"
154 radice=$(git rev-parse --show-toplevel 2>/dev/null || true)
155 if [ -n "$radice" ] && [ ! -e "$radice/.qwen/.env" ]; then
156 (umask 077; mkdir -p "$radice/.qwen" && echo "# delminio" > "$radice/.qwen/.env") 2>/dev/null || true
157 escludi=$(cd "$radice" && git rev-parse --path-format=absolute --git-path info/exclude 2>/dev/null) || escludi=""
158 [ -n "$escludi" ] && { grep -qxF ".qwen/.env" "$escludi" 2>/dev/null || echo ".qwen/.env" >> "$escludi"; } 2>/dev/null
159 fi
160 if [ -n "${QWEN_HOME:-}" ] && [ -f "$(dirname "$0")/plugin.py" ]; then
161 if [ "$rotta" = nascita ] && [ -z "${DELMINIO_SENZA_PLUGIN:-}" ] \
162 && DELMINIO_QWEN="${DELMINIO_CLI:-qwen}" python3 "$(dirname "$0")/plugin.py" qwen "$QWEN_HOME" >/dev/null 2>&1; then
163 exec "${DELMINIO_CLI:-qwen}" --input-file "$posta" "$@"
164 fi
165 DELMINIO_QWEN="${DELMINIO_CLI:-qwen}" python3 "$(dirname "$0")/plugin.py" qwen-via "$QWEN_HOME" >/dev/null 2>&1 || true
166 [ "$rotta" = nascita ] && [ -z "${DELMINIO_SENZA_PLUGIN:-}" ] && echo "$M_qwen_senza_plugin" >&2
167 fi
168 mcp="$STATO/qwen-mcp-$$.json"
169 (umask 077; DELMINIO_T="$mcp_token" python3 -c 'import json,os,sys
170print(json.dumps({"mcpServers": {"memoria": {
171 "command": "bash", "args": [sys.argv[2], "ponte-mcp", sys.argv[1]],
172 "env": {"DELMINIO_T": os.environ["DELMINIO_T"],
173 "DELMINIO_MODELLO": os.environ.get("DELMINIO_MODELLO", "")},
174 "timeout": 60000, "trust": True, "alwaysLoadTools": True}}}))' "$URL" "$(cd "$(dirname "$0")" && pwd)/$(basename "$0")" > "$mcp") && [ -s "$mcp" ] \
175 || { echo "$M_delminio_mcp_qwen" >&2; rm -f "$mcp"; exit 1; }
176 exec "${DELMINIO_CLI:-qwen}" --input-file "$posta" --mcp-config "$mcp" "$@"
177 ;;
178 opencode)
179 shift
180 export DELMINIO_CLI_NOME=opencode
181 porta="${DELMINIO_PORTA:-principale}"
182 export DELMINIO_PORTA="$porta"
183 porta_del_lancio nascita "$M_agente_parte_meta_2"
184 export DELMINIO_SISTEMA=1
185 radice=$(git rev-parse --show-toplevel 2>/dev/null || pwd)
186 [ -f "$radice/.opencode/plugins/delminio.js" ] || python3 "$(dirname "$0")/plugin.py" opencode "$radice" >/dev/null 2>&1 || true
187 squadra="{}"
188 if [ -z "${DELMINIO_SENZA_PLUGIN:-}" ] && [ -f "$(dirname "$0")/squadra.py" ]; then
189 squadra=$(python3 "$(dirname "$0")/squadra.py" lancio opencode "$radice" 2>/dev/null) || squadra="{}"
190 fi
191 gratuito=""
192 if [ -n "${DELMINIO_OPENCODE_GRATUITO:-}" ]; then
193 ricordo="${XDG_STATE_HOME:-$HOME/.local/state}/delminio/opencode-gratuito"
194 oggi=$(date +%F)
195 giorno=""; [ -f "$ricordo" ] && read -r giorno gratuito < "$ricordo"
196 if [ "${giorno:-}" != "$oggi" ] || [ -z "$gratuito" ]; then
197 echo "$M_opencode_gratuito_scelgo" >&2
198 gratuito=$(python3 "$(cd "$(dirname "$0")" && pwd)/eseguibili.py" modello-gratuito "${DELMINIO_CLI:-$(command -v opencode)}" 2>/dev/null) || gratuito=""
199 if [ -n "$gratuito" ]; then
200 mkdir -p "$(dirname "$ricordo")" && printf '%s %s\n' "$oggi" "$gratuito" > "$ricordo"
201 else
202 _f "$M_opencode_gratuito_nessuno" nome "$DELMINIO_OPENCODE_GRATUITO" >&2
203 fi
204 fi
205 fi
206 OPENCODE_CONFIG_CONTENT=$(DELMINIO_F="$f" DELMINIO_OPENCODE_SQUADRA="$squadra" DELMINIO_GRATUITO_SCELTO="$gratuito" python3 -c 'import json, os
207c = json.loads(os.environ.get("DELMINIO_OPENCODE_CONFIG") or "{}")
208if os.environ.get("DELMINIO_GRATUITO_SCELTO"):
209 c["model"] = os.environ["DELMINIO_GRATUITO_SCELTO"]
210porta = "{file:" + os.environ["DELMINIO_F"] + "}"
211c.setdefault("agents", {}).setdefault("build", {})["system"] = porta
212def unisci(a, b):
213 for k, v in b.items():
214 if isinstance(v, dict) and isinstance(a.get(k), dict):
215 unisci(a[k], v)
216 elif isinstance(v, list) and isinstance(a.get(k), list):
217 a[k] += [x for x in v if x not in a[k]]
218 else:
219 a.setdefault(k, v)
220try:
221 sq = json.loads(os.environ.get("DELMINIO_OPENCODE_SQUADRA") or "{}")
222except ValueError:
223 sq = {}
224if isinstance(sq, dict):
225 unisci(c, sq)
226print(json.dumps(c))') || { echo "$M_agente_parte_meta_2" >&2; exit 1; }
227 export OPENCODE_CONFIG_CONTENT OPENCODE_DISABLE_AUTOUPDATE=1 OPENCODE_DISABLE_CLAUDE_CODE_PROMPT=1
228 exec "${DELMINIO_CLI:-opencode}" --standalone "$@"
229 ;;
230esac