#!/usr/bin/env bash
# toolkit-llm.sh  -  run the multi-agent-mcp's free-model CLI.
#
# Sourced by bulk-read.sh and route-state.sh. `ma_toolkit_llm <args>` runs
# `multi-agent-mcp-llm <args>` from the first place it exists:
#
#   1. MULTI_AGENT_MCP_LLM            a path to the CLI script
#   2. multi-agent-mcp-llm on PATH    a global install
#   3. npx -p @mmerterden/multi-agent-mcp@latest, the same package
#      the MCP registration runs
#
# A maintainer's working copy (prefs.global.devToolkit) is deliberately not a
# source: it can sit on an unreleased branch, and a run should use what ships.
#
# The CLI prints one JSON object and exits 0 on a verified answer, 3 on
# ESCALATE_TO_CLAUDE, 2 on a usage error. Any other status (no node, no network
# for npx) is the caller's cue to keep its own default.

ma_toolkit_llm() {
  # The variable's earlier name is read while the current one is unset.
  local cli="${MULTI_AGENT_MCP_LLM:-${MULTI_AGENT_TOOLKIT_LLM:-}}"
  if [ -n "$cli" ] && [ -f "$cli" ]; then
    node "$cli" "$@"
    return $?
  fi
  if command -v multi-agent-mcp-llm >/dev/null 2>&1; then
    multi-agent-mcp-llm "$@"
    return $?
  fi
  command -v npx >/dev/null 2>&1 || return 127
  npx -y --@mmerterden:registry=https://registry.npmjs.org -p @mmerterden/multi-agent-mcp@latest multi-agent-mcp-llm "$@"
}
