# Copyright 2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

# Upstream caps at <3.14: Rust-backed transitives (pydantic-core) had no
# cp314 wheels at release time. Keep in sync with the HERMES_PYTHON wrapper
# path in src_install.
PYTHON_COMPAT=( python3_13 )
DISTUTILS_SINGLE_IMPL=1
DISTUTILS_USE_PEP517=setuptools

inherit distutils-r1

MY_TAG=2026.9.7

DESCRIPTION="Self-improving AI agent CLI by Nous Research with a built-in learning loop"
HOMEPAGE="https://hermes-agent.nousresearch.com/ https://github.com/NousResearch/hermes-agent"
SRC_URI="https://github.com/NousResearch/hermes-agent/archive/refs/tags/v${MY_TAG}.tar.gz -> ${P}.gh.tar.gz"
S="${WORKDIR}/${PN}-${MY_TAG}"

LICENSE="MIT"
SLOT="0"
KEYWORDS="~amd64"
IUSE="anthropic edge-tts mcp telegram"
RESTRICT="test"

# Upstream exact-pins every core dependency in pyproject.toml; floors below
# track those pins where the (guru/tatsh) tree can satisfy them, bare atoms
# elsewhere. openai lives in guru (2.44.0 >= pinned 2.24.0); fire,
# firecrawl-anydoc and nemo-relay are packaged in this overlay. httpx[socks]
# is python-socks in ::gentoo. git/ripgrep/ffmpeg mirror the runtime PATH of
# the upstream nix derivation (nix/hermes-agent.nix).
RDEPEND="
	dev-vcs/git
	media-video/ffmpeg
	sys-apps/ripgrep
	dev-python/certifi
	dev-python/croniter
	dev-python/cryptography
	>=dev-python/fastapi-0.104
	dev-python/fire
	>=dev-python/firecrawl-anydoc-0.2.4
	dev-python/httptools
	dev-python/httpx
	dev-python/jinja2
	dev-python/markdown
	dev-python/nemo-relay
	>=dev-python/openai-2.24.0
	dev-python/packaging
	dev-python/pathspec
	>=dev-python/pillow-12.3.0
	dev-python/prompt-toolkit
	dev-python/psutil
	dev-python/ptyprocess
	>=dev-python/pydantic-2.13.4
	dev-python/pyjwt
	dev-python/python-dotenv
	dev-python/python-multipart
	dev-python/python-socks
	dev-python/pyyaml
	dev-python/requests
	dev-python/rich
	dev-python/ruamel-yaml
	dev-python/snowballstemmer
	dev-python/starlette
	dev-python/tenacity
	dev-python/uvicorn
	dev-python/uvloop
	dev-python/watchfiles
	dev-python/websockets
	anthropic? ( >=dev-python/anthropic-0.87 )
	edge-tts? ( >=dev-python/edge-tts-7.2.7 )
	mcp? (
		>=dev-python/httpx2-2.5.0
		>=dev-python/mcp-2.0.0
		>=dev-python/starlette-1.3.1
	)
	telegram? ( >=dev-python/python-telegram-bot-22.7 )
"

# setup.py raises unless HERMES_NIX_BUILD=1: upstream retired the
# pip/PyPI/Homebrew wheel channels and only lets their uv2nix derivation
# build wheels. Portage is the same kind of sealed, review-at-the-pin
# package manager, so opt into that build path.
export HERMES_NIX_BUILD=1

src_install() {
	distutils-r1_src_install

	# Bundled assets the wheel deliberately omits (resolved at runtime via
	# HERMES_* env overrides; see hermes_constants.py::_packaged_dir and
	# the mirroring installPhase in nix/hermes-agent.nix). HERMES_WEB_DIST
	# and HERMES_TUI_DIR are intentionally unset: they point at Node-built
	# dashboard/TUI bundles this ebuild does not build.
	insinto /usr/share/${PN}
	doins -r \
		"${S}"/skills \
		"${S}"/optional-skills \
		"${S}"/plugins \
		"${S}"/locales \
		"${S}"/optional-mcps \
		|| die

	# The generated /usr/bin entry-point scripts are plain; re-create them
	# as thin wrappers exporting the same asset-discovery environment the
	# nix derivation's makeWrapper sets (--set-default semantics: an already
	# exported value wins). eutils/makeWrapper no longer exists in the tree,
	# so the wrapper is written by hand.
	local libdir=$(get_libdir)
	dodir /usr/${libdir}/${PN}
	local s
	for s in hermes hermes-agent hermes-acp; do
		mv "${ED}/usr/bin/${s}" "${ED}/usr/${libdir}/${PN}/${s}" || die
		cat > "${ED}/usr/bin/${s}" <<-EOF || die
			#!/bin/sh
			export HERMES_BUNDLED_SKILLS="\${HERMES_BUNDLED_SKILLS:-${EPREFIX}/usr/share/${PN}/skills}"
			export HERMES_OPTIONAL_SKILLS="\${HERMES_OPTIONAL_SKILLS:-${EPREFIX}/usr/share/${PN}/optional-skills}"
			export HERMES_BUNDLED_PLUGINS="\${HERMES_BUNDLED_PLUGINS:-${EPREFIX}/usr/share/${PN}/plugins}"
			export HERMES_BUNDLED_LOCALES="\${HERMES_BUNDLED_LOCALES:-${EPREFIX}/usr/share/${PN}/locales}"
			export HERMES_OPTIONAL_MCPS="\${HERMES_OPTIONAL_MCPS:-${EPREFIX}/usr/share/${PN}/optional-mcps}"
			export HERMES_PYTHON="\${HERMES_PYTHON:-${EPREFIX}/usr/bin/python3.13}"
			export HERMES_BIN="\${HERMES_BIN:-${EPREFIX}/usr/bin/hermes}"
			exec "${EPREFIX}/usr/${libdir}/${PN}/${s}" "\$@"
		EOF
		fperms +x "/usr/bin/${s}" || die
	done
}