#!/bin/sh
# A reference launcher for Hermes Agent's CLI as an OpenRig community harness. Copy it, own it, and change it: OpenRig
# doesn't ship or run it. It follows docs/reference/community-harness-presets.md (`names_conversation`):
#   - a new launch (OPENRIG_CONVERSATION set): Hermes creates the session titled with that name;
#   - a resume (OPENRIG_RESUME_REF set): Hermes reopens that session, and never creates one. A name that matches nothing
#     ends with Hermes's own "No session found" error, so a gone conversation isn't replaced by a new one.
# Put the model, provider and max turns in the seat's Hermes config.yaml, not on the command line. Hermes v0.22.4's CLI
# creates a `-c <name> --create-if-missing` session with no launch flags, then reopens it and refuses any creation flag
# that differs, so such flags fail even on a new name. Other arguments go only to a new session, never to a resume.
set -eu

# Managed run authority belongs to the parent runner, never this native TUI or its probes.
# Keep legacy hook-only environments unchanged.
if [ "${OPENRIG_RUN_ID+x}" = x ]; then
  unset OPENRIG_RUN_ID OPENRIG_ACTIVITY_HOOK_TOKEN
fi

hermes="${OPENRIG_HERMES:-hermes}"

# Each seat gets its own Hermes home, so seats don't share sessions or a gateway, and a seat's stop concerns only its own.
# Its settings (model, provider, agent.max_turns) come from that home's config.yaml, and its key from the environment
# (an OpenRouter key, say), not from the person's ~/.hermes.
HERMES_HOME="${OPENRIG_HOME:-$HOME/.openrig}/state/hermes/${OPENRIG_NODE_ID:-${OPENRIG_SESSION_NAME:-default}}"
export HERMES_HOME
mkdir -p "$HERMES_HOME"

# Hermes's tools see its shared gateway's environment except for the names a profile passes through, so a seat whose
# config.yaml doesn't pass OPENRIG_SESSION_NAME would run `rig` as another seat, or as none (see config.yaml.example).
if ! grep -qs OPENRIG_SESSION_NAME "$HERMES_HOME/config.yaml"; then
  echo "openrig-hermes-launch: $HERMES_HOME/config.yaml doesn't pass OPENRIG_SESSION_NAME to Hermes's tools, so rig run from Hermes won't act as this seat; see config.yaml.example" >&2
fi

# The seat's declared model (OPENRIG_MODEL, when the preset sets passes_model): Hermes takes it only from config.yaml, and
# Hermes v0.22.4 fixes a session's launch settings when it creates it. So it's written for a new session only, and only
# into a config.yaml that is absent or empty: a seat's own config is left byte-for-byte as it is, whatever it says. On a
# resume this launcher applies no OpenRig model.
if [ -n "${OPENRIG_MODEL:-}" ] && [ -z "${OPENRIG_RESUME_REF:-}" ]; then
  case "$OPENRIG_MODEL" in
    ''|*[!A-Za-z0-9._:/@+-]*) echo "openrig-hermes-launch: not writing unusual model id \"$OPENRIG_MODEL\" into $HERMES_HOME/config.yaml" >&2 ;;
    *) if [ -s "$HERMES_HOME/config.yaml" ]; then
         echo "openrig-hermes-launch: the seat has its own $HERMES_HOME/config.yaml; OPENRIG_MODEL was not applied; set the model there" >&2
       else
         printf 'model: "%s"\n' "$OPENRIG_MODEL" >> "$HERMES_HOME/config.yaml"
       fi ;;
  esac
fi

if [ -n "${OPENRIG_RESUME_REF:-}" ]; then
  exec "$hermes" chat -c "$OPENRIG_RESUME_REF"
fi
if [ -n "${OPENRIG_CONVERSATION:-}" ]; then
  # This first_input example targets Hermes v0.23.4's --query-file route (the naming notes above are from v0.22.4).
  # Its source seeds the interactive TTY from this file without a paste/Enter race.
  # Passing the path avoids shell expansion and argument-size limits on the startup text.
  if [ -n "${OPENRIG_FIRST_INPUT:-}" ]; then
    if [ ! -f "$OPENRIG_FIRST_INPUT" ] || [ ! -r "$OPENRIG_FIRST_INPUT" ] || [ ! -s "$OPENRIG_FIRST_INPUT" ]; then
      echo "openrig-hermes-launch: the first-input file is missing, unreadable or empty; nothing was started" >&2
      exit 1
    fi
    set -- "$@" --query-file "$OPENRIG_FIRST_INPUT"
  fi
  exec "$hermes" chat -c "$OPENRIG_CONVERSATION" --create-if-missing "$@"
fi
echo "openrig-hermes-launch: OpenRig passed no conversation name; is names_conversation set in the preset?" >&2
exit 2
