devzone 7.4.1 → 7.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,173 +0,0 @@
1
- #!/usr/bin/env bash
2
- # Update check and report for the host (Ollama) models (#668).
3
- # host-check.sh check per detected model: up to date, update available,
4
- # or not verifiable; never downloads anything
5
- # host-check.sh rapport sizes, update state, measurements and the chosen
6
- # default model with its reason
7
- # The registry manifest digest is compared with the local /api/tags digest: one
8
- # GET per model, no download, no "ollama pull" executed (the command is only
9
- # printed for the host). The registry is DEVZONE_OLLAMA_REGISTRY, else
10
- # https://registry.ollama.ai.
11
- # bash 3.2 (macOS). Code and comments in English; the user messages are French
12
- # and live in host-check.message, a data file outside the code check.
13
- set -euo pipefail
14
-
15
- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
16
- # shellcheck source=SCRIPTDIR/host-models.sh
17
- # shellcheck disable=SC1091
18
- . "$SCRIPT_DIR/host-models.sh"
19
-
20
- REG="${DEVZONE_OLLAMA_REGISTRY:-https://registry.ollama.ai}"
21
- REG="${REG%/}"
22
- MESSAGE_FILE="$SCRIPT_DIR/host-check.message"
23
-
24
- # English fallbacks, overridden by the French message file.
25
- MSG_TITRE="host (Ollama) models detected:"
26
- MSG_AUCUN="devzone: no Ollama model detected on {url} (answer on /api/tags)"
27
- MSG_TAILLE="size"
28
- MSG_A_JOUR="up to date"
29
- MSG_MAJ="update available"
30
- MSG_NON="not verifiable"
31
- MSG_PULL="on the host: ollama pull {modele}"
32
- MSG_MESURE="measurement reussite {reussite}, tok_s {tok_s}"
33
- MSG_RETENU="chosen model: {modele} ({raison})"
34
- MSG_RAISON_VAR="DEVZONE_HOST_MODEL"
35
- MSG_RAISON_MESURE="measurement"
36
- MSG_RAISON_REGLE="rule"
37
-
38
- if [ -r "$MESSAGE_FILE" ]; then
39
- # shellcheck source=/dev/null
40
- . "$MESSAGE_FILE"
41
- fi
42
-
43
- # Lowercase hex sha256 of a file (sha256sum on Linux, shasum on macOS).
44
- sha256_fichier() {
45
- if command -v sha256sum >/dev/null 2>&1; then
46
- sha256sum "$1" | cut -d' ' -f1
47
- else
48
- shasum -a 256 "$1" | cut -d' ' -f1
49
- fi
50
- }
51
-
52
- # Prints the update state of one model: MSG_A_JOUR, MSG_MAJ or MSG_NON.
53
- # A name with a slash (hf.co/...), a 404, an unreachable registry or a missing
54
- # digest is not verifiable.
55
- etat_modele() {
56
- local nom="$1" digest="$2" court tag somme tmp
57
- case "$nom" in
58
- */*) printf '%s\n' "$MSG_NON"; return 0 ;;
59
- esac
60
- case "$nom" in
61
- *:*) court="${nom%:*}"; tag="${nom##*:}" ;;
62
- *) court="$nom"; tag="latest" ;;
63
- esac
64
- tmp="$(mktemp "${TMPDIR:-/tmp}/devzone-manifest.XXXXXX")"
65
- if ! curl -fsS --max-time 5 \
66
- -H 'Accept: application/vnd.docker.distribution.manifest.v2+json' \
67
- -o "$tmp" "${REG}/v2/library/${court}/manifests/${tag}" 2>/dev/null; then
68
- rm -f "$tmp"
69
- printf '%s\n' "$MSG_NON"
70
- return 0
71
- fi
72
- somme="$(sha256_fichier "$tmp")"
73
- rm -f "$tmp"
74
- if [ -z "$digest" ]; then
75
- printf '%s\n' "$MSG_NON"
76
- return 0
77
- fi
78
- if [ "$(printf '%s' "$somme" | tr '[:upper:]' '[:lower:]')" = \
79
- "$(printf '%s' "${digest#sha256:}" | tr '[:upper:]' '[:lower:]')" ]; then
80
- printf '%s\n' "$MSG_A_JOUR"
81
- else
82
- printf '%s\n' "$MSG_MAJ"
83
- fi
84
- }
85
-
86
- # Prints "reussite tok_s" for a model, empty when it has no measurement.
87
- mesure_pour() {
88
- local nom="$1" data
89
- [ -f "$MEASURES" ] || return 0
90
- data="$(cat "$MEASURES" 2>/dev/null)" || return 0
91
- [ -n "$data" ] || return 0
92
- printf '%s' "$data" | jq -r --arg nom "$nom" '
93
- .[$nom] | if . == null then empty else "\(.reussite // 0) \(.tok_s // 0)" end
94
- ' 2>/dev/null || return 0
95
- }
96
-
97
- # Why the model is chosen: variable, measurement or rule.
98
- raison_choix() {
99
- local liste="$1" m
100
- if [ -n "${DEVZONE_HOST_MODEL:-}" ]; then
101
- printf '%s\n' "$MSG_RAISON_VAR"
102
- return 0
103
- fi
104
- m="$(mesure_choix "$liste")" || true
105
- if [ -n "$m" ]; then
106
- printf '%s\n' "$MSG_RAISON_MESURE"
107
- else
108
- printf '%s\n' "$MSG_RAISON_REGLE"
109
- fi
110
- }
111
-
112
- # Update check of every detected model. Nothing is downloaded, nothing changes.
113
- check() {
114
- local data ligne nom taille digest etat
115
- data="$(infos)" || true
116
- if [ -z "$data" ]; then
117
- printf '%s\n' "${MSG_AUCUN//\{url\}/$URL}"
118
- return 0
119
- fi
120
- printf '%s\n' "$data" | while IFS=$'\t' read -r nom taille digest; do
121
- [ -n "$nom" ] || continue
122
- etat="$(etat_modele "$nom" "$digest")"
123
- printf '%s : %s\n' "$nom" "$etat"
124
- if [ "$etat" = "$MSG_MAJ" ]; then
125
- printf '%s\n' "${MSG_PULL//\{modele\}/$nom}"
126
- fi
127
- done
128
- return 0
129
- }
130
-
131
- # Report: sizes, update state, measurements and the chosen model with its
132
- # reason. Proposes, changes nothing.
133
- rapport() {
134
- local data liste ligne nom taille digest etat mes r t def raison
135
- data="$(infos)" || true
136
- if [ -z "$data" ]; then
137
- printf '%s\n' "${MSG_AUCUN//\{url\}/$URL}"
138
- return 0
139
- fi
140
- liste="$(printf '%s\n' "$data" | cut -f1)"
141
- printf '%s\n' "$MSG_TITRE"
142
- printf '%s\n' "$data" | while IFS=$'\t' read -r nom taille digest; do
143
- [ -n "$nom" ] || continue
144
- etat="$(etat_modele "$nom" "$digest")"
145
- printf ' %s : %s ; %s %s\n' "$nom" "$etat" "$MSG_TAILLE" "$taille"
146
- mes="$(mesure_pour "$nom")"
147
- if [ -n "$mes" ]; then
148
- r="${mes%% *}"
149
- t="${mes#* }"
150
- ligne="${MSG_MESURE//\{reussite\}/$r}"
151
- ligne="${ligne//\{tok_s\}/$t}"
152
- printf ' %s\n' "$ligne"
153
- fi
154
- if [ "$etat" = "$MSG_MAJ" ]; then
155
- printf '%s\n' "${MSG_PULL//\{modele\}/$nom}"
156
- fi
157
- done
158
- def="$(choix "$liste")" || true
159
- raison="$(raison_choix "$liste")" || true
160
- ligne="${MSG_RETENU//\{modele\}/${def:-}}"
161
- ligne="${ligne//\{raison\}/$raison}"
162
- printf '%s\n' "$ligne"
163
- return 0
164
- }
165
-
166
- # When run directly (not sourced): dispatch the subcommands.
167
- if [ "${0##*/}" = "host-check.sh" ]; then
168
- case "${1:-}" in
169
- check) check ;;
170
- rapport) rapport ;;
171
- *) check ;;
172
- esac
173
- fi
@@ -1,200 +0,0 @@
1
- #!/usr/bin/env bash
2
- # Detection of the models served by Ollama on the host (#665, #668).
3
- # devzone hosts no model: models live on the host (Ollama) and are detected here.
4
- # host-models.sh list the detected model names, one per line
5
- # host-models.sh first the model to use: DEVZONE_HOST_MODEL, else the best
6
- # measured model, else a deterministic rule
7
- # host-models.sh set merge the devzone-host provider into ~/.config/opencode/config.json
8
- # host-models.sh unset remove the devzone-host provider from that file
9
- # host-models.sh check update check of each model (delegates to host-check.sh)
10
- # host-models.sh rapport report (delegates to host-check.sh)
11
- # Default URL: http://host.docker.internal:11434, overridable with DEVZONE_OLLAMA_URL.
12
- # Measurements: DEVZONE_AI_MEASURES, else ~/.config/devzone/ai-measures.json
13
- # ({"<model>": {"reussite": 0..1, "tok_s": n, "date": "..."}}).
14
- # Short timeout; exits 0 and writes nothing when Ollama does not answer.
15
- # bash 3.2 (macOS). Code and comments in English.
16
- set -euo pipefail
17
-
18
- URL="${DEVZONE_OLLAMA_URL:-http://host.docker.internal:11434}"
19
- URL="${URL%/}"
20
- FILE="$HOME/.config/opencode/config.json"
21
- MEASURES="${DEVZONE_AI_MEASURES:-$HOME/.config/devzone/ai-measures.json}"
22
-
23
- # True when a model is an embedding model (Ollama details.type, or a name
24
- # containing "embed", case-insensitive).
25
- is_embedding() {
26
- local low
27
- low="$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]')"
28
- case "$low" in
29
- *embed*) return 0 ;;
30
- *) return 1 ;;
31
- esac
32
- }
33
-
34
- # Raw /api/tags payload, empty when Ollama does not answer.
35
- json_tags() {
36
- curl -fsS --max-time 3 "${URL}/api/tags" 2>/dev/null || return 0
37
- }
38
-
39
- # Prints the detected model names, one per line. Exits 0 and prints nothing
40
- # when Ollama does not answer or the payload is not usable.
41
- modeles() {
42
- local data
43
- data="$(json_tags)" || return 0
44
- [ -n "$data" ] || return 0
45
- # Ignore embedding models (Ollama details.type, or a name containing "embed").
46
- printf '%s' "$data" | jq -r '.models[] | select((.details.type // "") != "embedding") | .name' 2>/dev/null |
47
- while IFS= read -r name; do
48
- is_embedding "$name" || printf '%s\n' "$name"
49
- done || true
50
- return 0
51
- }
52
-
53
- # Prints the detected models as "name<TAB>size" (bytes), one per line.
54
- tailles() {
55
- local data line name
56
- data="$(json_tags)" || return 0
57
- [ -n "$data" ] || return 0
58
- printf '%s' "$data" | jq -r '.models[] | select((.details.type // "") != "embedding") | "\(.name)\t\(.size // 0)"' 2>/dev/null |
59
- while IFS= read -r line; do
60
- name="${line%%$'\t'*}"
61
- is_embedding "$name" || printf '%s\n' "$line"
62
- done || true
63
- return 0
64
- }
65
-
66
- # Prints the detected models as "name<TAB>size<TAB>digest", one per line.
67
- infos() {
68
- local data line name
69
- data="$(json_tags)" || return 0
70
- [ -n "$data" ] || return 0
71
- printf '%s' "$data" | jq -r '.models[] | select((.details.type // "") != "embedding") | "\(.name)\t\(.size // 0)\t\(.digest // "")"' 2>/dev/null |
72
- while IFS= read -r line; do
73
- name="${line%%$'\t'*}"
74
- is_embedding "$name" || printf '%s\n' "$line"
75
- done || true
76
- return 0
77
- }
78
-
79
- # Best measured model among the detected names ($1, one per line): highest
80
- # reussite, then highest tok_s. Prints nothing when the file is absent, invalid
81
- # or no detected model is measured.
82
- mesure_choix() {
83
- local liste="$1" data
84
- [ -f "$MEASURES" ] || return 0
85
- data="$(cat "$MEASURES" 2>/dev/null)" || return 0
86
- [ -n "$data" ] || return 0
87
- printf '%s' "$data" | jq -r --arg liste "$liste" '
88
- ($liste | split("\n") | map(select(length > 0))) as $d
89
- | [to_entries[] | select(.key as $k | $d | index($k))]
90
- | sort_by([-(.value.reussite // 0), -(.value.tok_s // 0)])
91
- | .[0].key // empty
92
- ' 2>/dev/null || return 0
93
- }
94
-
95
- # Deterministic fallback: a name containing "coder" first, then the smallest
96
- # size, then the name order.
97
- regle_choix() {
98
- tailles | LC_ALL=C awk -F'\t' '
99
- {
100
- low = tolower($1)
101
- coder = (index(low, "coder") > 0) ? 0 : 1
102
- size = ($2 + 0)
103
- if (best == "" || coder < bc || (coder == bc && size < bs) || (coder == bc && size == bs && $1 < bn)) {
104
- best = $1
105
- bc = coder
106
- bs = size
107
- bn = $1
108
- }
109
- }
110
- END { if (best != "") print best }
111
- '
112
- }
113
-
114
- # The model to use among the detected names: DEVZONE_HOST_MODEL when set, else
115
- # the best measured model, else the deterministic rule.
116
- choix() {
117
- local liste="$1" nom mesure
118
- [ -n "$liste" ] || return 0
119
- nom="${DEVZONE_HOST_MODEL:-}"
120
- if [ -n "$nom" ]; then
121
- printf '%s\n' "$nom"
122
- return 0
123
- fi
124
- mesure="$(mesure_choix "$liste")" || true
125
- if [ -n "$mesure" ]; then
126
- printf '%s\n' "$mesure"
127
- return 0
128
- fi
129
- regle_choix
130
- }
131
-
132
- # The model to use. Empty when Ollama did not answer or no model was detected.
133
- premier() {
134
- local liste
135
- liste="$(modeles)" || true
136
- [ -n "$liste" ] || return 0
137
- choix "$liste"
138
- }
139
-
140
- # jq program $1 applied to the file (or {}), written back atomically.
141
- # The file is removed when nothing remains but the schema.
142
- rewrite() {
143
- local current="{}" out
144
- [ ! -f "$FILE" ] || current="$(cat "$FILE")"
145
- mkdir -p "$(dirname "$FILE")"
146
- out="$(printf '%s' "$current" | jq "$1")" || {
147
- echo "devzone: host: $FILE is not valid JSON: provider not touched" >&2
148
- return 1
149
- }
150
- if [ "$(printf '%s' "$out" | jq -c 'del(."$schema") | length')" = 0 ]; then
151
- rm -f "$FILE"
152
- else
153
- printf '%s\n' "$out" >"$FILE.tmp" && mv "$FILE.tmp" "$FILE"
154
- fi
155
- }
156
-
157
- # Merges the devzone-host provider (the detected models) into the opencode
158
- # configuration file, keeping everything else. Does nothing when no model was
159
- # detected. baseURL = URL + /v1.
160
- set_provider() {
161
- local liste current="{}" out models baseurl
162
- liste="$(modeles)" || return 0
163
- [ -n "$liste" ] || return 0
164
- baseurl="${URL%/}/v1"
165
- models="$(printf '%s\n' "$liste" | jq -R -n '([inputs | select(length > 0)] | map({(.): {name: .}})) | add // {}')" || return 1
166
- [ ! -f "$FILE" ] || current="$(cat "$FILE")"
167
- mkdir -p "$(dirname "$FILE")"
168
- out="$(printf '%s' "$current" | jq --arg baseurl "$baseurl" --argjson models "$models" '
169
- ."$schema" = (."$schema" // "https://opencode.ai/config.json")
170
- | .provider["devzone-host"] = {npm: "@ai-sdk/openai-compatible", name: "devzone host models (Ollama)", options: {baseURL: $baseurl}, models: $models}
171
- ')" || {
172
- echo "devzone: host: $FILE is not valid JSON: provider not touched" >&2
173
- return 1
174
- }
175
- if [ "$(printf '%s' "$out" | jq -c 'del(."$schema") | length')" = 0 ]; then
176
- rm -f "$FILE"
177
- else
178
- printf '%s\n' "$out" >"$FILE.tmp" && mv "$FILE.tmp" "$FILE"
179
- fi
180
- }
181
-
182
- # Removes the devzone-host and devzone-llm providers from the opencode
183
- # configuration file, keeping everything else. No error when the file is absent.
184
- unset_provider() {
185
- [ ! -f "$FILE" ] && return 0
186
- rewrite 'if .provider then del(.provider["devzone-host"]) | del(.provider["devzone-llm"]) | (if .provider == {} then del(.provider) else . end) else . end'
187
- }
188
-
189
- # When run directly (not sourced): dispatch the subcommands.
190
- if [ "${0##*/}" = "host-models.sh" ]; then
191
- case "${1:-}" in
192
- first) premier ;;
193
- set) set_provider ;;
194
- unset) unset_provider ;;
195
- check | rapport)
196
- exec bash "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/host-check.sh" "$@"
197
- ;;
198
- *) modeles ;;
199
- esac
200
- fi
@@ -1,98 +0,0 @@
1
- #!/usr/bin/env bash
2
- # shellcheck shell=bash
3
- # devzone, ai module: guard of the host container tools.
4
- # Sourced by .devcontainer/devzone (split of the 1 600 lines file, #722):
5
- # functions only, shared variables are defined in devzone.
6
-
7
- # Docker guard for the folders that are not this repository: a Claude Code
8
- # PreToolUse hook in the global settings and the two opencode plugins in the
9
- # global plugins folder. Idempotent, other user settings and hooks preserved.
10
- # Claude Code also receives the limited Kubernetes context via env.KUBECONFIG.
11
- install_docker_guard() {
12
- local script="$ICI/ai/docker-guard.sh" config="$HOME/.claude/settings.json"
13
- local src="$ICI/ai/plugins" dst="$HOME/.config/opencode/plugins"
14
- local tmp f
15
- if ! command -v jq >/dev/null 2>&1; then
16
- echo "devzone : jq missing: docker guard not installed for Claude Code" >&2
17
- elif [ ! -f "$config" ]; then
18
- echo "devzone : $config missing: docker guard not installed for Claude Code" >&2
19
- elif ! jq -e . "$config" >/dev/null 2>&1; then
20
- echo "devzone : $config unreadable (invalid JSON): docker guard not installed for Claude Code" >&2
21
- else
22
- tmp="$(mktemp)"
23
- if jq --arg cmd "bash \"$script\" --format claude" \
24
- --arg kube "$HOME/.kube/agent.config" '
25
- .hooks = (.hooks // {}) |
26
- .hooks.PreToolUse = (.hooks.PreToolUse // []) |
27
- if ([.hooks.PreToolUse[]? | .hooks[]? | (.command // "")] | any(contains("docker-guard.sh")))
28
- then .
29
- else .hooks.PreToolUse += [{"matcher": "Bash", "hooks": [{"type": "command", "command": $cmd}]}]
30
- end |
31
- if (.env | type) == "object" then .env = (.env + {"KUBECONFIG": $kube})
32
- elif (.env | type) == "null" then .env = {"KUBECONFIG": $kube}
33
- else . end
34
- ' "$config" >"$tmp" 2>/dev/null; then
35
- mv "$tmp" "$config"
36
- else
37
- rm -f "$tmp"
38
- echo "devzone : $config unchanged: docker guard not installed for Claude Code" >&2
39
- fi
40
- fi
41
- mkdir -p "$dst"
42
- for f in devzone-guard.js devzone-guard-v2.js; do
43
- [ -f "$src/$f" ] || continue
44
- sed "s|__DEVZONE_DOCKER_GUARD__|$script|g" "$src/$f" >"$dst/$f"
45
- done
46
- echo "devzone : docker guard active for Claude Code and opencode"
47
- }
48
-
49
- # Removes only the Docker guard entries (commands containing docker-guard.sh)
50
- # and the two plugins; the rest of settings.json and of the plugins is kept.
51
- # The Kubernetes context key is removed too, and an empty env object is dropped.
52
- remove_docker_guard() {
53
- local config="$HOME/.claude/settings.json" tmp tmp2
54
- if [ -f "$config" ] && jq -e . "$config" >/dev/null 2>&1; then
55
- tmp="$(mktemp)"
56
- if jq '
57
- if (.hooks.PreToolUse // null) != null then
58
- .hooks.PreToolUse = [
59
- .hooks.PreToolUse[] |
60
- if (.hooks // null) != null then
61
- .hooks = [ .hooks[] | select(((.command // "") | contains("docker-guard.sh")) | not) ] |
62
- select((.hooks | length) > 0)
63
- else
64
- select(((.command // "") | contains("docker-guard.sh")) | not)
65
- end
66
- ] |
67
- if (.hooks.PreToolUse | length) == 0 then del(.hooks.PreToolUse) else . end |
68
- if ((.hooks // {}) | keys | length) == 0 then del(.hooks) else . end
69
- else . end
70
- ' "$config" >"$tmp" 2>/dev/null; then
71
- if [ "$(cat "$tmp")" = "{}" ]; then
72
- rm -f "$tmp" "$config"
73
- else
74
- tmp2="$(mktemp)"
75
- if jq '
76
- if (.env | type) == "object" then
77
- .env = (.env | del(.KUBECONFIG)) |
78
- if (.env | length) == 0 then del(.env) else . end
79
- else . end
80
- ' "$tmp" >"$tmp2" 2>/dev/null; then
81
- if [ "$(cat "$tmp2")" = "{}" ]; then
82
- rm -f "$tmp2" "$config"
83
- else
84
- mv "$tmp2" "$config"
85
- fi
86
- else
87
- rm -f "$tmp2"
88
- mv "$tmp" "$config"
89
- fi
90
- rm -f "$tmp"
91
- fi
92
- else
93
- rm -f "$tmp"
94
- fi
95
- fi
96
- rm -f "$HOME/.config/opencode/plugins/devzone-guard.js" \
97
- "$HOME/.config/opencode/plugins/devzone-guard-v2.js"
98
- }
@@ -1,201 +0,0 @@
1
- #!/usr/bin/env bash
2
- # shellcheck shell=bash
3
- # devzone, ai module: opencode config, assistants install, apply.
4
- # Sourced by .devcontainer/devzone (split of the 1 600 lines file, #722):
5
- # functions only, shared variables are defined in devzone.
6
-
7
- # Marker of the default configuration: first line of opencode.jsonc. The former
8
- # wording (French, before #722) is still recognised. Byte escapes keep the accent
9
- # out of the source (English code check).
10
- OPENCODE_MARKER='// devzone : default opencode configuration'
11
-
12
- # True if the file carries the default-configuration marker
13
- opencode_marked() { grep -qF -e "$OPENCODE_MARKER" "$1"; }
14
-
15
- # Default opencode configuration (versioned model): written at first use,
16
- # updated while it keeps the devzone marker, customised otherwise. The marker
17
- # text is written in opencode.jsonc: it is kept as is.
18
- opencode_config() {
19
- local model="$ICI/opencode.jsonc" target
20
- if [ -f "$HOME/.config/opencode/opencode.jsonc" ]; then
21
- target="$HOME/.config/opencode/opencode.jsonc"
22
- elif [ -f "$HOME/.config/opencode/opencode.json" ]; then
23
- target="$HOME/.config/opencode/opencode.json"
24
- else
25
- target="$HOME/.config/opencode/opencode.jsonc"
26
- fi
27
- mkdir -p "$(dirname "$target")"
28
- if [ ! -f "$target" ]; then
29
- cp "$model" "$target"
30
- echo "opencode : default configuration written"
31
- elif opencode_marked "$target"; then
32
- if ! cmp -s "$model" "$target"; then
33
- cp "$model" "$target"
34
- echo "opencode : default configuration updated"
35
- fi
36
- elif only_schema_key "$target"; then
37
- cp "$model" "$target"
38
- echo "opencode : default configuration updated"
39
- else
40
- echo "opencode : custom configuration, kept"
41
- fi
42
- }
43
-
44
- # Removes the opencode configuration written by devzone if it still carries its
45
- # marker and was not changed; a custom configuration (no marker, or changed) is
46
- # kept. Symmetrical with opencode_config.
47
- remove_opencode_config() {
48
- local model target
49
- model="$ICI/opencode.jsonc"
50
- if [ -f "$HOME/.config/opencode/opencode.jsonc" ]; then
51
- target="$HOME/.config/opencode/opencode.jsonc"
52
- elif [ -f "$HOME/.config/opencode/opencode.json" ]; then
53
- target="$HOME/.config/opencode/opencode.json"
54
- else
55
- return 0
56
- fi
57
- if opencode_marked "$target" && cmp -s "$model" "$target"; then
58
- rm -f "$target"
59
- echo "opencode : default configuration removed"
60
- else
61
- echo "opencode : custom configuration, kept"
62
- fi
63
- }
64
-
65
- # Log of the last npm run (its path is to give on failure), or its folder;
66
- # timestamped names: the last one in glob order is the most recent
67
- last_npm_log() {
68
- local log="" f
69
- for f in "$HOME"/.npm/_logs/*-debug-0.log; do
70
- if [ -f "$f" ]; then log="$f"; fi
71
- done
72
- echo "${log:-$HOME/.npm/_logs}"
73
- }
74
-
75
- # Installs each pinned AI assistant with its own npm command (3 tries each),
76
- # then checks its binary: npm silently drops a platform binary it failed to
77
- # download (#194), and one assistant failing must not block the other.
78
- install_ai() {
79
- local package binary attempt
80
- IA_FAILED=()
81
- for package in "${IA_PAQUETS[@]}"; do
82
- case "$package" in
83
- opencode-ai@*) binary=opencode ;;
84
- *) binary=claude ;;
85
- esac
86
- for attempt in 1 2 3; do
87
- if with_progress npm install -g --no-fund --no-audit --loglevel=error "$package" >/dev/null &&
88
- "$binary" --version >/dev/null 2>&1; then
89
- break
90
- fi
91
- if [ "$attempt" -lt 3 ]; then
92
- echo "devzone : installation of ${package%@*} incomplete, retrying ($((attempt + 1))/3)…" >&2
93
- else
94
- IA_FAILED+=("$package")
95
- fi
96
- done
97
- done
98
- [ "${#IA_FAILED[@]}" -eq 0 ]
99
- }
100
-
101
- apply_ai() {
102
- local log tmp detected
103
- if [ "$1" = on ]; then
104
- echo "devzone : installing Claude Code and opencode (a few minutes)…"
105
- if ! install_ai; then
106
- log="$(last_npm_log)"
107
- echo "npm log: $log" >"$(failure_marker_file ai)"
108
- echo "devzone : installation of ${IA_FAILED[*]} failed (npm install -g, 3 tries); npm log: $log; retry: devzone apply" >&2
109
- return 1
110
- fi
111
- rm -f "$(failure_marker_file ai)"
112
- echo "devzone : Claude Code and opencode installed"
113
- # repowarden guard (Claude Code PreToolUse hook, opencode plugin): the
114
- # agent is stopped before changing main/develop, committing without
115
- # repowarden cc, or bypassing the hooks. Global, in the ~/.claude and
116
- # ~/.config/opencode volumes: kept across rebuilds; rerun, nothing changes
117
- if command -v repowarden >/dev/null 2>&1 && repowarden agents init --global >/dev/null 2>&1; then
118
- echo "devzone : repowarden guard active for Claude Code and opencode"
119
- else
120
- echo "devzone : repowarden guard not connected (repowarden 4.10 or later required): repowarden agents init --global" >&2
121
- fi
122
- install_docker_guard
123
- opencode_config
124
- # Remove the provider of the removed llm module from old configs (#669).
125
- if [ -f "$HOME/.config/opencode/config.json" ] && command -v jq >/dev/null 2>&1; then
126
- tmp="$(mktemp)"
127
- if jq '
128
- if .provider then del(.provider["devzone-llm"]) | (if .provider == {} then del(.provider) else . end) else . end
129
- ' "$HOME/.config/opencode/config.json" >"$tmp" 2>/dev/null; then
130
- if [ "$(cat "$tmp")" = "{}" ] || [ "$(jq -c 'del(."$schema") | length' "$tmp" 2>/dev/null)" = 0 ]; then
131
- rm -f "$tmp" "$HOME/.config/opencode/config.json"
132
- else
133
- mv "$tmp" "$HOME/.config/opencode/config.json"
134
- fi
135
- else
136
- rm -f "$tmp"
137
- fi
138
- fi
139
- # opencode provider of the host models (Ollama), live detection:
140
- # silent and without effect if Ollama does not answer.
141
- set_provider || true
142
- # Reports the state of the detection (success in both cases).
143
- detected="$(modeles 2>/dev/null || true)"
144
- if [ -n "$detected" ]; then
145
- printf "devzone : %s model(s) of the host (Ollama) connected\n" "$(printf '%s\n' "$detected" | grep -c .)"
146
- else
147
- printf "devzone : no Ollama model detected on %s (host models not connected)\n" "$URL"
148
- fi
149
- # devzone skill: installed if missing, updated if unchanged, kept if customised.
150
- install_devzone_skill
151
- echo "devzone : ai active: claude (then /login), opencode auth login"
152
- else
153
- npm uninstall -g "${IA_NOMS[@]}" >/dev/null 2>&1 || true
154
- rm -f "$(failure_marker_file ai)"
155
- # repowarden guard: removed on disable (symmetrical with the enable).
156
- # repowarden does it itself when present; otherwise removal by jq of only
157
- # the PreToolUse entries calling the guard, the rest of settings.json
158
- # intact. The guard is called " repowarden guard " since repowarden
159
- # 4.17.3; " repowarden garde " is its old name, still an alias on the
160
- # repowarden side and recognised here.
161
- if command -v repowarden >/dev/null 2>&1; then
162
- repowarden agents retirer --global >/dev/null 2>&1 || true
163
- elif [ -f "$HOME/.claude/settings.json" ]; then
164
- tmp="$(mktemp)"
165
- if jq '
166
- if (.hooks.PreToolUse // null) != null then
167
- .hooks.PreToolUse = [
168
- .hooks.PreToolUse[] |
169
- if (.hooks // null) != null then
170
- .hooks = [ .hooks[] | select(((.command // "") | (contains("repowarden garde") or contains("repowarden guard"))) | not) ] |
171
- select((.hooks | length) > 0)
172
- else
173
- select(((.command // "") | (contains("repowarden garde") or contains("repowarden guard"))) | not)
174
- end
175
- ] |
176
- if (.hooks.PreToolUse | length) == 0 then del(.hooks.PreToolUse) else . end |
177
- if ((.hooks // {}) | keys | length) == 0 then del(.hooks) else . end
178
- else . end
179
- ' "$HOME/.claude/settings.json" >"$tmp" 2>/dev/null; then
180
- mv "$tmp" "$HOME/.claude/settings.json"
181
- else
182
- rm -f "$tmp"
183
- fi
184
- fi
185
- # opencode plugin of the guard (also removed by repowarden above)
186
- rm -f "$HOME/.config/opencode/plugins/repowarden-garde.js" \
187
- "$HOME/.config/opencode/plugins/repowarden-garde-v2.js"
188
- # docker guard: only its entries and its plugins are removed.
189
- remove_docker_guard
190
- # opencode configuration: removed only if it carries the devzone marker
191
- # and was not changed; customised -> kept.
192
- remove_opencode_config
193
- # Removes the opencode provider of the host models, keeps the rest of the file.
194
- unset_provider || true
195
- # devzone skill: removed only if unchanged; customised -> kept.
196
- remove_devzone_skill
197
- # devzone agent state: kept in the shared volume (disabling it in one
198
- # devzone does not remove the state of the others).
199
- echo "devzone : ai disabled (logins and devzone agent state kept in their volumes; workspace/.agents/ logs kept)"
200
- fi
201
- }