#!/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" printf 'SUPERMARKET_TZ=%s\n' "$(host_timezone)" } > "$COMPOSE_ENV" } # host_timezone prints this computer's time zone (e.g. America/Denver), so # the app shows and schedules times in it; UTC if it can't tell. host_timezone() { local tz="" if [ -L /etc/localtime ]; then tz="$(readlink -f /etc/localtime)" tz="${tz#*/zoneinfo/}" fi if [ -z "$tz" ] || [ "$tz" = "$(readlink -f /etc/localtime)" ]; then tz="$(cat /etc/timezone 2> /dev/null || true)" fi echo "${tz:-UTC}" } 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. # Supermarket can use the Anthropic key and model Hermes already has, so AI # is set up once. hermes/setup.sh asks, and records the answer in # $HERMES_AI_CHOICE; this copies just the key and model (never the rest of # Hermes' .env) to $HERMES_AI, which the app reads when it has no key of its # own. The app calls the model directly, without Hermes' tools. HERMES_AI="$DATA/hermes-ai.json" HERMES_AI_CHOICE="$DATA/hermes-ai.choice" # hermes_config KEY: a value from Hermes' config, read with the real command # (CoreDesktop's `hermes` is the sandboxed one). hermes_config() { local unsandboxed="$COREDESKTOP/sandbox/bin/unsandboxed" if [ -x "$unsandboxed" ]; then PATH="$HOME/.local/bin:$PATH" "$unsandboxed" hermes config get "$1" 2> /dev/null elif [ -x "$HOME/.local/bin/hermes" ]; then "$HOME/.local/bin/hermes" config get "$1" 2> /dev/null fi } # hermes_ai_key prints Hermes' Anthropic key, if it has one. hermes_ai_key() { sed -n 's/^ANTHROPIC_API_KEY=//p' "${HERMES_HOME:-$HOME/.hermes}/.env" 2> /dev/null | tail -n 1 | tr -d "\"' \r" } # sync_hermes_ai: refresh (or remove) the shared key. Quiet unless asked to # report with "-v". sync_hermes_ai() { local key provider model tmp if [ "$(cat "$HERMES_AI_CHOICE" 2> /dev/null)" != yes ]; then rm -f "$HERMES_AI" return 0 fi key="$(hermes_ai_key)" provider="$(hermes_config model.provider)" model="$(hermes_config model.default)" if [ "$provider" != anthropic ] || [ -z "$key" ] || ! command -v jq > /dev/null; then rm -f "$HERMES_AI" [ "${1:-}" = -v ] && echo "Hermes doesn't use an Anthropic key, so there's none to share; set Supermarket's AI provider under Settings." return 0 fi mkdir -p "$DATA" tmp="$(mktemp "$HERMES_AI.XXXXXX")" chmod 600 "$tmp" # The key goes through the environment, not the command line. if KEY="$key" MODEL="$model" jq -n '{provider: "anthropic", model: env.MODEL, api_key: env.KEY}' > "$tmp"; then mv -f "$tmp" "$HERMES_AI" [ "${1:-}" = -v ] && echo "Supermarket uses Hermes' Anthropic key and model ($model)." else rm -f "$tmp" fi return 0 } 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] ]] }