#!/bin/bash # # Purpose : Paths and helpers shared by install.sh, update.sh and # hermes/setup.sh. Source this file. # Used by the scripts that source this file # shellcheck disable=SC2034 SUPERMARKET_REPO="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" COREDESKTOP="$HOME/.local/share/coredesktop" DATA="${XDG_DATA_HOME:-$HOME/.local/share}/supermarket" COMPOSE_ENV="$DATA/compose.env" CORE_CONF="$HOME/.config/coredesktop/core.conf" OLD_STATE="$HOME/.local/state/coremarketing" URL="http://localhost:8470" # The version, worked out the way CoreDesktop's `sibling` shows it: the # latest tag, or else the date and short hash of the latest commit. version() { local tag if tag="$(git -C "$SUPERMARKET_REPO" describe --tags 2> /dev/null)"; then sed -E 's/-([0-9]+)-g[0-9a-f]+$/+\1/' <<< "$tag" return fi git -C "$SUPERMARKET_REPO" log -1 --date=format:%Y.%m.%d --format='%cd (%h)' 2> /dev/null || echo dev } write_compose_env() { mkdir -p "$DATA" && chmod 700 "$DATA" { printf 'SUPERMARKET_DATA=%s\n' "$DATA" printf 'SUPERMARKET_UID=%s\n' "$(id -u)" printf 'SUPERMARKET_GID=%s\n' "$(id -g)" printf 'SUPERMARKET_VERSION=%s\n' "$(version)" printf 'SUPERMARKET_REPO=%s\n' "$SUPERMARKET_REPO" } > "$COMPOSE_ENV" } compose() { docker compose --project-directory "$SUPERMARKET_REPO" -f "$SUPERMARKET_REPO/compose.yaml" \ --env-file "$COMPOSE_ENV" "$@" } # The container can't read ~/.config, so it gets a copy of the Core's # settings. Without a Core, any old copy goes. copy_core_conf() { if [ -f "$CORE_CONF" ]; then install -m 600 "$CORE_CONF" "$DATA/core.conf" else rm -f "$DATA/core.conf" fi } # The LLM servers CoreDesktop's `configure ai-servers` added to opencode, for # the AI settings page. Docker can't resolve .local names, so they become IP # addresses. write_ai_servers() { local conf="$HOME/.config/opencode/opencode.json" servers name url host ip if [ ! -f "$conf" ] || ! command -v jq > /dev/null; then rm -f "$DATA/ai-servers.json" return 0 fi servers="[]" while IFS=$'\t' read -r name url; do [ -n "$url" ] || continue host="$(sed -E 's#^[a-z]+://([^/:]+).*#\1#' <<< "$url")" if [[ "$host" == *.local ]]; then ip="$(getent ahostsv4 "$host" 2> /dev/null | awk 'NR == 1 { print $1 }')" [ -n "$ip" ] && url="${url/$host/$ip}" fi servers="$(jq -c --arg name "$name" --arg url "$url" '. + [{name: $name, base_url: $url}]' <<< "$servers")" done < <(jq -r '(.provider // {}) | to_entries[] | select(.value.npm == "@ai-sdk/openai-compatible" and (.value.options.baseURL // "") != "") | [.value.name // .key, .value.options.baseURL] | @tsv' "$conf" 2> /dev/null) printf '%s\n' "$servers" > "$DATA/ai-servers.json" } # Wait until the app answers, for up to $1 seconds. wait_until_up() { local i for ((i = 0; i < ${1:-60}; i++)); do curl -sf -o /dev/null "$URL/healthz" && return 0 sleep 1 done return 1 } confirm_yes() { local answer read -rp "$1 [Y/n] " answer [[ -z "$answer" || "$answer" =~ ^[Yy] ]] }