
    ,jr                    v%   U d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlmZ ddlmZ ddlmZmZmZmZmZ ddlmZ  ej        e          Z e            Zeed<   ded	ee         fd
Z dede!d	dfdZ" ej#                    dk    Z$ ej%        d          Z& e'h d          Z(e'e)         ed<   de)d	dfdZ*i Z+ee)ef         ed<   i Z,ee)ee-e-ee)ef         f         f         ed<   i Z.ee)ee-e-ee)ef         f         f         ed<    ej/                    Z0 e'h d          Z1ddl2Z2ddl3m4Z4m5Z5 ddl6m7Z7 dZ8dddddZ9d	ee)         fdZ:d	e;fdZ<dZ=d	ee)         fd Z>dd!ee         d	e)fd"Z?d#e)d	dfd$Z@d	e;fd%ZAd#e)d	e)fd&ZBd	e)fd'ZCd(ZDd	e)fd)ZEdd+e)d	e)fd,ZFdd+e)fd.ZGd	eeH         fd/ZIdd0lJmKZK dd1lLmMZM d	efd2ZNd	efd3ZOd	efd4ZPd	eQee-         ee-         f         fd5ZRdd6ZSd7 ZTd	e;fd8ZUd9 ZVd:ed	dfd;ZWd< ZXd:efd=ZYi d>d?d@i dAg dBi dCdDgdEddFi dGdHdIdJdKdLdMdNdOd?dPdQdRdSdTdSdUd?dVdQdWdXdYdZd[dLd\d]d^dQd_g d`i dadbdcdQdddedfdLdgg dhg didSdjdkdlg dmi dndodpdkdqdkdrdsdtdudvdwdxdSg dyg dydSdzd{d?d?d?d|d}d~ddydydQdSd?dddyd?d?dydyddd
ddyddddSddSdddddddddddSdyddNdddddddddSdddddNdydSddddiddSdddi dd?dSg d]dd?d?ddddddQd?d?d?d~i dddQd?d?d?di ddQd?d?d?d~i ddQd?d?d?di ddQd?d?d?di ddQd?d?d?di ddQd?d?d?di ddQd?d?d?di ddQd?d?d?d~i ddQd?d?d?dLi ddQd?d?d?di ddQd?d?d?dZi ddQd?d?d?di dddi ddydd?ddddddddddNddSddddddyddSddyddyddyddyddœi ddSddddSddSddSddSddSddyddϓddѓddӓdddd՜ddSddydi ddddddSiddyidۜdyg dܢdݜdQdޜdddyd?d?dd?d?d?d?ddd?ddddyiddddidddddddddyd?dddddddddd?d?dddddid	ddSdbdd?d d>did>didd?dydyddddd~dydSdddd	d
ddddddiddSdSdyddd?ddd?d?d?d?d?dSddZd?dNdsdSdyddd?ddGdidg dSdyddydydddSddddHdSdSdddd i i d!d?d"dSd?d?i d#d$dSd?d?dSdydSddSi d?d?dyd%dydSd?d&d'd(dSg d)d*d+d,i d-dyi d?d.d/dSd?d?i d#d0dSd?d?d1d2d3dd4dSdSd5d6g d7i d8i d9dyd:i d;dydSdSd<ddSdyg g d=g dSd>	d?dSdd@dAdSdddBdsd?d?ddSdNdCdDdEdFdGii dHdIdQddddJidKdLddNdMdNdSdOdsi dPdQdRdyidSdyg dSdZdTddydQdUddVdWdXdYdydHdSddydZd[i d\d]d^dydd_d`dadSdbdcdQi dddedfdLddgdhdidydjd?ddSdSd?dkidlddmddnddodpZZg dqdrdsgg dtdugdvgdwZ[ee-ee)         f         edx<   i Z\i dydzd{ddyd|dSd}d~ddddSddgd|dSddddddSd|dSd}dddddSd|dSd}dddddyd|dSd}dddddSd|dSd}dddddyd|dSd}dddddSd|dSd}dddddyd|dSd}dddddSd|dSd}dddddyd|dSd}dddddSd|dSd}dddddSd|dSd}dddddSd|dSd}dddddyd|dSd}dddddSd|dSd}dddddyd|dSd}i dddddSd|dSd}dddddSd|dSd}dddddyd|dSd}dddddSd|dSd}dddddyd|dSd}dÐdĐdĐddSd|dSd}dƐdǐdddyd|dSd}dɐdʐdːddSd|dSd}d͐dΐdddyd|dSd}dАdѐdҐddSd|dSd}dԐdՐdddyd|dSd}dאdؐdِddSd|dۜdܐdݐdd?dyd|dۜdߐddddSd|dۜdddd?dyd|dSd}dddddyd|dSd}dddddyd|dSd}i dddddSd|dSd}dddddyd|dSd}dddddSd|dSd}dddddyd|dSd}dddddSd|dSd}dddddyd|dSd}d ddddSd|dۜdddddyd|dSd}ddd	d
dSd|dSd}dddddyd|dSd}dddddSd|dۜdddddyd|dSd}dddddyd|dSd}dddddyd|dSd}dddddSd|dۜd d!d"ddyd|dSd}d#d$d%d&d'd(gdSd)d*i d+d,d-d.d'd(gdSd)d*d/d0d1d2d'd(gdSd)d*d3d4d5ddyd)dSd}d6d7d8ddyd)dSd}d9d:d;ddyd)dSd}d<d=d>ddyd)dSd}d?d@dAddSd)dSd}dudBdCdDd'd(gdSd)d*dEdFdGdHd'gdyd)d*dIdJdKdLd'gdSd)d*dMdNdOdPdQdRgdSd)d*dSdTdUdPdQdRgdyd)d*dVdWdXdYdQdRgdSd)d*dZd[d\dQdRgdyd)d]d^d_d`dag dbdyd)dSdcdddedfdgdQdRgdyd)d*dhdidjdkdldmgdSd)d*i dndodpdqdlgdSd)d*drdrdsdtdudvgdSd)d*dsdwdxdydzdugdSd)d*d{d|d}d~dSd)dۜdddddSd)dۜdddddSddSd}dddddSddSd}dddddSddSd}dddddSddSd}dddddgdSd)d*dddd)ddddddyd)dۜdddddSd)dۜdddddyd)dSd}dddddSddۜdddddyddۜddddyddi dddddSddۜdddddyddۜdddddyddۜdddddSddۜddÐdĐddSddۜdŐdƐdǐddyddۜdɐdʐdddSddۜd̐d͐dddyddۜdϐdАdddyddۜdҐdӐdddyddۜdՐd֐dאddyddۜdِdڐdddSddۜdܐdݐdddyddۜdߐddddyddۜdddddyddSd}dddddyddSd}dddddyddSd}i dddddyddSd}dddddyddSd}dddddSddSd}dddddyddۜdddddSddۜdddddyddۜddd ddddddddddddSddd	d
dddddddddddddddddddddddddddddddddyddۜddd ddyddۜi d!d"d#ddyddۜd$d%d&ddSddSd}d'd(d)ddSddSd}d*d+d,ddyddSd}d-d.d/ddyddSd}d0d1d2ddSddSd}d3d4d5ddyddSd}d6d7d8ddyddSd}d9d:d;ddyddSd}d<d=d>ddyddSd}d?d@dAddSddSd}dBdCdDddyddۜdEdFdGddyddۜdHdIdJddSddۜdKdLdMddSdNdۜdOdPdQddydNdۜdRdSdTddydNdۜZ]dȐdUe;d	eee)ef                  fdVZ^dWe)fdXZ_d	eee)ef                  fdYZ`d	eee)ef                  fdZZad?d[d\ed]e)d	eee)ef                  fd^Zbd?d[d\ed]e)d	eee)ef                  fd_Zcd`ed	eee)ef                  fdaZd	 dĐdbeee)ef                  d	eee)ef                  fdcZe	 	 dd>e)dde)deeeee)ef                           dbeee)ef                  d	ee-         f
dfZfdged	e-fdhZgd	ee-e-f         fdiZhh djZih dkZjh dlZke G dm dn                      ZldĐdbeee)ef                  d	edn         fdoZmdĐdbeee)ef                  d	dfdpZndĐdbeee)ef                  d	dfdqZodʐdre;dse;d	ee)ef         fdtZpdeHdueHd	eHfdvZqdw Zrdx ZsdĐdyZtdbee)ef         d	ee)ef         fdzZudbee)ef         d	ee)ef         fd{Zvdd|d}eee)ef                  d~e)ded	efdZwd	ee)ef         fdZxd	ee)ef         fdZyd	ee)ef         fdZzi daddcdvddddfddddjddlddnddpddqddddddddddrddtddvdddddddddddd
Z{dged	e)fdZ|de)d	ee)         fdZ}dddddeee)e)f                  dbeee)ef                  duee;         d	ee)e)f         fdZ~de;d	ee)ef         fdZdZdZdZdbee)ef         fdZd	ee)e)f         fdZdaeeee)ee         ee-         f         ee)e)f         f                  ed<   dǐdZded	efdZd	e-fdZde)dge)d	e)fdZde)dge)fdZde)d	e;fdZdĐdge)fdZdĐdZdĐdge)fdZde)dge)d	ee)ef         fdZd	e-fdZde)d	ee)         fdZde)d	e)fdZd Zd Zde)dge)fdZd ZdyadǐdZ e             dyadǐdÄZ e             dS (  a  
Configuration management for Hermes Agent.

Config files are stored in ~/.hermes/ for easy access:
- ~/.hermes/config.yaml  - All settings (model, toolsets, terminal, etc.)
- ~/.hermes/.env         - API keys and secrets

This module provides:
- hermes config          - Show current configuration
- hermes config edit     - Open config in editor
- hermes config set      - Set a specific value
- hermes config wizard   - Re-run setup wizard
    N)	dataclass)Path)DictAnyOptionalListTuple)masked_secret_prompt_CONFIG_PARSE_WARNEDconfig_pathreturnc                 *   	 |                                  rdS |                                 }|j        dk    rdS t          j        d          }|                     | j         d| d          }t          | j        	                    | j         d                    }|D ]8}	 |                                j        |j        k    r dS )# t          $ r Y 5w xY w|                                rdS t          j        | |           |S # t          $ r Y dS w xY w)u  Preserve a corrupted ``config.yaml`` by copying it to a timestamped ``.bak``.

    When the YAML can't be parsed, ``load_config()`` silently falls back to
    ``DEFAULT_CONFIG`` and the user's broken file stays on disk untouched.
    That file is still the user's only copy of their intended overrides — if
    they re-run the setup wizard or ``hermes config set`` (which rewrites
    ``config.yaml``), the broken-but-recoverable content is gone for good.

    This snapshots the corrupted file to ``config.yaml.corrupt.<ts>.bak`` so
    the user can diff/repair it. Unlike Gemini CLI's policy-file recovery
    (which resets the live file to a clean state), we deliberately leave
    ``config.yaml`` in place: hermes never silently mutates the user's config,
    and leaving it means a hand-fixed file is re-read on the next load. The
    backup is best-effort — any failure (permissions, symlink, disk full) is
    swallowed so config loading is never blocked by backup problems.

    Returns the backup path on success, else ``None``. Symlinks are not
    followed/copied (mirrors the Gemini #21541 lstat guard) to avoid
    clobbering whatever a malicious/misconfigured symlink points at.
    Nr   z%Y%m%d-%H%M%Sz	.corrupt.z.bakz.corrupt.*.bak)
is_symlinkstatst_sizetimestrftime	with_namenamelistparentglobOSErrorexistsshutilcopy2	Exception)r   sttsbackup_pathsibling_baksexistings         6/home/ubuntu/.hermes/hermes-agent/hermes_cli/config.py_backup_corrupt_configr$   *   s[   *!!## 	4:?? 4]?++!++{/?,R,R",R,R,RSS
 ##{'7$G$G$GHH
 
 % 	 	H==??*bj88  44 9     	4[+...   ttsL   D D A'D !"CD D 
CD CD -D 
DDexcc                    	 |                                  }t          |           |j        |j        f}n"# t          $ r t          |           ddf}Y nw xY w|t
          v rdS t
                              |           t          |           }d|  d| d}|	|d| dz  }t          	                    |           	 t          j                            d| d	           t          j                                         dS # t          $ r Y dS w xY w)
a  Surface a config.yaml parse failure to user, log, and stderr.

    A YAML parse error in ``~/.hermes/config.yaml`` causes ``load_config()``
    to silently fall back to ``DEFAULT_CONFIG``, which means every user
    override (auxiliary providers, fallback chain, model overrides, etc.)
    is dropped. Before this helper that was a one-line ``print(...)`` that
    scrolled off-screen on the first invocation and was never seen again.

    Now: warn once per (path, mtime_ns, size) on stderr **and** in
    ``agent.log`` / ``errors.log`` at WARNING level so ``hermes logs``
    surfaces it. Re-warns automatically if the file changes (different
    mtime/size), so users editing the config see the next failure. On the
    first warning for a given broken file we also snapshot it to a
    timestamped ``.bak`` (best-effort) so the user's recoverable content
    survives any later rewrite of ``config.yaml`` by the setup wizard or
    ``hermes config set``.
    r   NzFailed to parse : u   . Falling back to default config — every user override (auxiliary providers, fallback chain, model settings) is being IGNORED. Fix the YAML and restart.z+ A copy of the corrupted file was saved to .u   ⚠️  hermes config: 
)r   strst_mtime_nsr   r   r   addr$   loggerwarningsysstderrwriteflushr   )r   r%   r   keyr    msgs         r#   _warn_config_parse_failurer5   `   sQ   $';< ' ' ';A&'
"""S!!!(55K	%; 	% 	%# 	% 	% 	%  K[KKKK
NN3
:3:::;;;
   s"   03 AA8AC; ;
D	D	Windowsz^[A-Za-z_][A-Za-z0-9_]*$>   PATHPAGERSHELLEDITORVISUALBROWSERLD_AUDITLD_DEBUG	GIT_SHELL	NODE_PATH
HERMES_ENV
LD_PRELOAD
PYTHONHOME
PYTHONPATHHERMES_HOMENODE_OPTIONSGIT_EXEC_PATHHERMES_CONFIGPYTHONSTARTUPHERMES_PROFILEPYTHONUSERBASEGIT_SSH_COMMANDLD_LIBRARY_PATHPYTHONEXECUTABLEPYTHONNOUSERSITEDYLD_LIBRARY_PATHDYLD_FRAMEWORK_PATHDYLD_INSERT_LIBRARIESDYLD_FALLBACK_LIBRARY_PATHDYLD_FALLBACK_FRAMEWORK_PATH_ENV_VAR_NAME_DENYLISTr3   c                 >    | t           v rt          d| d          dS )zRaise if ``key`` is in :data:`_ENV_VAR_NAME_DENYLIST`.

    Centralised so both the regular and "secure" env writers share the
    same gate, and so the message is consistent for callers.
    zEnvironment variable a   is on the writer denylist. Names that influence subprocess execution (LD_PRELOAD, PYTHONPATH, PATH, EDITOR, ...) or Hermes runtime location (HERMES_HOME, HERMES_PROFILE, ...) cannot be persisted via the env writer. If you really need this, edit ~/.hermes/.env directly.N)rU   
ValueError)r3   s    r#   _reject_denylisted_env_varrX      s@     $$$'C ' ' '
 
 	
 %$    _LAST_EXPANDED_CONFIG_BY_PATH_LOAD_CONFIG_CACHE_RAW_CONFIG_CACHE>g   IRC_PORT	QQ_APP_ID
IRC_SERVERIRC_CHANNELIRC_USE_TLSIRC_NICKNAMEQQ_STT_MODELTERMINAL_ENVWECOM_BOT_IDWECOM_SECRETWEIXIN_TOKENFEISHU_APP_IDWHATSAPP_MODEOPENAI_API_KEYQQ_STT_API_KEYSIGNAL_ACCOUNTANTHROPIC_TOKENMATRIX_PASSWORDOPENAI_BASE_URLQQ_HOME_CHANNELQQ_STT_BASE_URLSIGNAL_HTTP_URLWEIXIN_BASE_URLMATRIX_DEVICE_IDMATRIX_HOME_ROOMQQ_ALLOWED_USERSQQ_CLIENT_SECRETSMS_HOME_CHANNELTERMINAL_SSH_KEYWEIXIN_DM_POLICYWHATSAPP_ENABLEDANTHROPIC_API_KEYFEISHU_APP_SECRETLANGFUSE_BASE_URLMATRIX_ENCRYPTIONTERMINAL_SSH_PORTWEIXIN_ACCOUNT_IDDINGTALK_CLIENT_IDFEISHU_ENCRYPT_KEYMATRIX_AUTO_THREADQQBOT_HOME_CHANNELQQ_ALLOW_ALL_USERSSLACK_HOME_CHANNELWECOM_HOME_CHANNELFEISHU_HOME_CHANNELHERMES_LANGFUSE_ENVIRC_SERVER_PASSWORDLANGFUSE_PUBLIC_KEYLANGFUSE_SECRET_KEYMATRIX_RECOVERY_KEYQQ_MARKDOWN_SUPPORTSIGNAL_HOME_CHANNELWECOM_CALLBACK_HOSTWECOM_CALLBACK_PORTWEIXIN_CDN_BASE_URLWEIXIN_GROUP_POLICYWEIXIN_HOME_CHANNELBLUEBUBBLES_PASSWORDDISCORD_HOME_CHANNELHERMES_TOOL_PROGRESSQQ_HOME_CHANNEL_NAMESIGNAL_ALLOWED_USERSWECOM_CALLBACK_TOKENWEIXIN_ALLOWED_USERSYUANBAO_HOME_CHANNELDINGTALK_HOME_CHANNELHERMES_LANGFUSE_DEBUGIRC_NICKSERV_PASSWORDMATRIX_DM_AUTO_THREADMATTERMOST_REPLY_MODESMS_HOME_CHANNEL_NAMETELEGRAM_HOME_CHANNELBLUEBUBBLES_SERVER_URLDINGTALK_CLIENT_SECRETMATRIX_REQUIRE_MENTIONQQ_GROUP_ALLOWED_USERSWECOM_CALLBACK_CORP_IDWEIXIN_ALLOW_ALL_USERSHERMES_LANGFUSE_RELEASEMATTERMOST_HOME_CHANNELQQBOT_HOME_CHANNEL_NAMESLACK_HOME_CHANNEL_NAMEWECOM_CALLBACK_AGENT_IDWECOM_HOME_CHANNEL_NAMEBLUEBUBBLES_HOME_CHANNELFEISHU_HOME_CHANNEL_NAMESIGNAL_HOME_CHANNEL_NAMEWEIXIN_HOME_CHANNEL_NAMEDISCORD_HOME_CHANNEL_NAMEFEISHU_VERIFICATION_TOKENHERMES_LANGFUSE_MAX_CHARSHERMES_TOOL_PROGRESS_MODEYUANBAO_HOME_CHANNEL_NAMEDINGTALK_HOME_CHANNEL_NAMEMATRIX_FREE_RESPONSE_ROOMSSIGNAL_GROUP_ALLOWED_USERSTELEGRAM_HOME_CHANNEL_NAMEWECOM_CALLBACK_CORP_SECRETWEIXIN_GROUP_ALLOWED_USERSHERMES_LANGFUSE_SAMPLE_RATEMATTERMOST_HOME_CHANNEL_NAMEBLUEBUBBLES_HOME_CHANNEL_NAMEWECOM_CALLBACK_ENCODING_AES_KEY)Colorscolor)DEFAULT_SOUL_MD)true1yesHomebrewNixOS)brewhomebrewnixnixosc                     t          j        dd                                          } | r:|                                 }|t          v rdS t
                              ||           S t                      dz  }|                                rdS dS )z7Return the package manager owning this install, if any.HERMES_MANAGED r   z.managedN)	osgetenvstriplower_MANAGED_TRUE_VALUES_MANAGED_SYSTEM_NAMESgetget_hermes_homer   )raw
normalizedmanaged_markers      r#   get_managed_systemr   ;  s    
)$b
)
)
/
/
1
1C
 :YY[[
---7$((S999$&&3N w4rY   c                  "    t                      duS )a  Check if Hermes is running in package-manager-managed mode.

    Two signals: the HERMES_MANAGED env var (set by the systemd service),
    or a .managed marker file in HERMES_HOME (set by the NixOS activation
    script, so interactive shells also see it).
    N)r    rY   r#   
is_managedr   J  s     t++rY   zfUpdate your Nix flake input and rebuild (e.g. nix flake update, nixos-rebuild, or home-manager switch)c                  L    t                      } | dk    rdS | dk    rt          S dS )z;Return the preferred upgrade command for a managed install.r   brew upgrade hermes-agentr   N)r   _NIX_UPDATE_MSG)managed_systems    r#   get_managed_update_commandr   W  s4    '))N##**  4rY   project_rootc                    t                      dz  }	 |                    d                                                                          }|r|S n# t          $ r Y nw xY wt                      }|r(|                                                    dd          S | 0t          t                    j	        j	        
                                } | dz                                  rdS d	S )
a  Detect how Hermes was installed: 'docker', 'nixos', 'homebrew', 'git', or 'pip'.

    Resolution order:
    1. Stamped ``~/.hermes/.install_method`` file (written by installers)
    2. HERMES_MANAGED env / .managed marker (NixOS, Homebrew)
    3. .git directory presence -> 'git'
    4. Fallback -> 'pip'

    Note: running inside a container is NOT treated as "docker" on its own.
    The two supported install paths both self-identify via the
    ``.install_method`` stamp (caught by step 1), so neither relies on
    container detection here:
      - the curl installer (scripts/install.sh, the README/website install
        command) git-clones the repo and stamps ``git``;
      - the published ``nousresearch/hermes-agent`` image stamps ``docker``
        at boot via ``docker/stage2-hook.sh``.
    An unsupported manual install dropped into a container (no stamp) was
    wrongly classified as the published image by bare container detection,
    so ``hermes update`` bailed with "doesn't apply inside the Docker
    container". Without that fallback such installs fall through to the
    ``.git``/pip checks and behave like any off-path install. See issue #34397.
    .install_methodutf-8encoding -Nz.gitgitpip)r   	read_textr   r   r   r   replacer   __file__r   resolveis_dir)r   stampmethodmanageds       r#   detect_install_methodr   a  s    .  11E'2288::@@BB 	M	    ""G 1}}&&sC000H~~,3;;==v%%'' u5s   =A 
AAr   c                     t                      dz  }	 |j                            dd           |                    | dz   d           dS # t          $ r Y dS w xY w)z6Write the install method to ~/.hermes/.install_method.r   Tparentsexist_okr)   r   r   N)r   r   mkdir
write_textr   )r   r   s     r#   stamp_install_methodr     sx     11E4$777$99999   s   6A 
AAc                      dt           dt          fd}  | t          j                  rdS  | t          j        pd          rdS dS )a  Return True when the *running* Hermes lives in a ``uv tool`` layout.

    ``uv tool install hermes-agent`` places the install at
    ``.../uv/tools/hermes-agent/...`` (default ``~/.local/share/uv/tools``,
    or ``$UV_TOOL_DIR/...``). Such installs live outside any virtualenv, so
    ``uv pip install`` fails with ``No virtual environment found`` and the
    update path must use ``uv tool upgrade`` instead.

    Detection is intentionally restricted to properties of the running
    interpreter (``sys.prefix`` / ``sys.executable``). We deliberately do
    NOT consult ``uv tool list``: it would also return True when
    ``hermes-agent`` happens to be uv-tool-installed on the machine while
    the *active* Hermes is a regular pip/venv install, causing
    ``hermes update`` to upgrade the wrong copy. It would also block on a
    subprocess call (~seconds) just to compute a recommendation string.
    pathr   c                     t           j                            |                               t           j        d                                          }d|dz   v S )N/z/uv/tools/hermes-agent/)r   r  normpathr   sepr   )r  norms     r#   _has_uv_tool_markerz/is_uv_tool_install.<locals>._has_uv_tool_marker  sD    w%%--bfc::@@BB(D3J66rY   Tr   F)r*   boolr/   prefix
executable)r  s    r#   is_uv_tool_installr    sd    "7# 7$ 7 7 7 7 3:&& t3>/R00 t5rY   c                     | dk    rt           S | dk    rdS | dk    rdS | dk    r(t                      rdS dd	l} |j        d
          rdS dS dS )zAReturn the update command or guidance for a given install method.r   r   r   dockerz,docker pull nousresearch/hermes-agent:latestr   zuv tool upgrade hermes-agentr   Nuvz%uv pip install --upgrade hermes-agentz"pip install --upgrade hermes-agentzhermes update)r   r  r   which)r   r   s     r#   %recommended_update_command_for_methodr    s    **== 	2116< 	;::33?rY   c                  `    t                      } | r| S t                      }t          |          S )z<Return the best update command for the current installation.)r   r   r  )managed_cmdr   s     r#   recommended_update_commandr    s4    ,..K "$$F0888rY   u  ✗ ``hermes update`` doesn't apply inside the Docker container.

Hermes Agent runs as a published image (nousresearch/hermes-agent), not a
git checkout — the container has no working tree to pull into.  Update by
pulling a fresh image and restarting your container instead:

  docker pull nousresearch/hermes-agent:latest
  # then restart whatever started the container, e.g.:
  docker compose up -d --force-recreate hermes-agent
  # or, for ad-hoc runs, exit the current container and `docker run` again

Verify the new version after restart:
  docker run --rm nousresearch/hermes-agent:latest --version

Notes:
  • If you pinned a specific tag (e.g. ``:v0.14.0``) the ``:latest`` tag
    won't move your container — pull the newer tag you actually want, or
    switch to ``:latest`` / ``:main`` for rolling updates.  See available
    tags at https://hub.docker.com/r/nousresearch/hermes-agent/tags
  • Your config and session history live under ``$HERMES_HOME`` (``/opt/data``
    in the container, typically bind-mounted from the host) and persist
    across image upgrades — re-pulling doesn't lose any state.
  • Running a fork?  Build your own image with this repo's ``Dockerfile``
    and replace the ``docker pull`` step with your build/push pipeline.c                      t           S )a  Return the user-facing message for ``hermes update`` inside Docker.

    Centralised so ``cmd_update`` (the apply path) and ``_cmd_update_check``
    (the dry-run path) share the same wording.  See ``_DOCKER_UPDATE_MESSAGE``
    above for the full rationale.
    )_DOCKER_UPDATE_MESSAGEr   rY   r#   format_docker_update_messager    s
     "!rY   modify this Hermes installationactionc                    t                      pd}t          j        dd                                                                          }|dk    r|t
          v rdn|pd}d|  d| dS |d	k    r|pd
}d|  d| dS d|  d| dS )z/Build a user-facing error for managed installs.za package managerr   r   r   r   zCannot z?: this Hermes installation is managed by NixOS (HERMES_MANAGED=ze).
Edit services.hermes-agent.settings in your configuration.nix and run:
  sudo nixos-rebuild switchr   r   zB: this Hermes installation is managed by Homebrew (HERMES_MANAGED=z#).
Use:
  brew upgrade hermes-agentz): this Hermes installation is managed by z:.
Use your package manager to upgrade or reinstall Hermes.)r   r   r   r   r   r   )r  r   r   env_hints       r#   format_managed_messager    s    '))@-@N
)$b
)
)
/
/
1
1
7
7
9
9C   $88866cmV*f * *'* * *	
 ##$**f * *'* * *	
	C& 	C 	C> 	C 	C 	CrY   modify configurationc                 V    t          t          |           t          j                   dS )z+Print user-friendly error for managed mode.fileN)printr  r/   r0   )r  s    r#   managed_errorr"    s&    	
 
(
(sz::::::rY   c                     t           j                            d          dk    rdS ddlm}   |             rdS t                      dz  }	 i }t          |dd	          5 }|D ]q}|                                }d
|v rW|                    d          sB|	                    d
          \  }}}|                                ||                                <   r	 ddd           n# 1 swxY w Y   n# t          $ r Y dS w xY w|                    dd          }|                    dd          }	|                    dd          }
|                    dd          }||	|
|dS )a  Read container mode metadata from HERMES_HOME/.container-mode.

    Returns a dict with keys: backend, container_name, exec_user, hermes_bin
    or None if container mode is not active, we're already inside the
    container, or HERMES_DEV=1 is set.

    The .container-mode file is written by the NixOS activation script when
    container.enable = true. It tells the host CLI to exec into the container
    instead of running locally.
    
HERMES_DEVr   Nr   )is_containerz.container-moderr   r   =#backendr  container_namezhermes-agent	exec_userhermes
hermes_binz /data/current-package/bin/hermes)r)  r*  r+  r-  )r   environr   hermes_constantsr%  r   openr   
startswith	partitionFileNotFoundError)r%  container_mode_fileinfofliner3   _valuer)  r*  r+  r-  s               r#   get_container_exec_infor:  #  s    
z~~l##s**t------|~~ t)++.??	%sW=== 	6 6 6zz||$;;ts';';;$(NN3$7$7MCE(-D%	6	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6    tt hhy(++GXX.??Nh//I,(JKKJ ( 	  s7   
C, A5C C,  C$$C, 'C$(C, ,
C:9C:r   )atomic_replacec                  $    t                      dz  S )zGet the main config file path.zconfig.yamlr;  r   rY   r#   get_config_pathr>  X  s    },,rY   c                  $    t                      dz  S )z&Get the .env file path (for API keys).z.envr;  r   rY   r#   get_env_pathr@  \  s    v%%rY   c                  b    t          t                    j        j                                        S )z'Get the project installation directory.)r   r   r   r   r   rY   r#   get_project_rootrB  `  s     >> '//111rY   c                     t           j        dk    rdS t          j                            dd                                          } t          j                            dd                                          }	 | rt          |           nd}n# t          $ r d}Y nw xY w	 |rt          |          nd}n# t          $ r d}Y nw xY w||fS )a  Read the HERMES_UID / HERMES_GID env vars set by Docker deployments.

    Docker containers running Hermes commonly set these to map the in-container
    user to a host user so volume-mounted state files end up with the right
    ownership. The entrypoint chowns the top-level HERMES_HOME once, but
    subdirectories created at runtime by ``ensure_hermes_home()`` (especially
    for profile namespaces under ``profiles/<name>/``) need the same chown
    or they land as ``root:root`` and block subsequent uid-mapped workers
    with ``PermissionError [Errno 13]``. See #34107.

    Returns ``(uid, gid)`` parsed from the env vars, or ``(None, None)``
    when either is missing/invalid. Returns ``(None, None)`` on Windows
    too (where chown is a no-op anyway).
    win32NN
HERMES_UIDr   
HERMES_GIDN)r/   platformr   r.  r   r   intrW   )uid_strgid_struidgids       r#   _resolve_hermes_uid_gidrN  d  s     |wzjnn\2..4466Gjnn\2..4466G%/c'lll4   %/c'lll4   8Os$   8B BBB3 3CCc                     t                      \  }}||dS 	 t          j        | ||nd||nd           dS # t          t          t
          f$ r Y dS w xY w)u  Chown ``path`` to ``HERMES_UID:HERMES_GID`` if those env vars are set.

    No-op when:
      - Either env var is unset/invalid
      - The current process isn't root (chown will EPERM — silently ignored)
      - On Windows (chown semantics don't apply)

    Used by :func:`_secure_dir` to keep ownership consistent across all
    directories created by :func:`ensure_hermes_home` on Docker deployments.
    See #34107.
    N)rN  r   chownr   AttributeErrorNotImplementedError)r  rL  rM  s      r#   _chown_to_hermes_uidrT    s     '((HC
{s{
?CC?CC	
 	
 	
 	
 	

 ^%89    		s   9 AAc                 T   t                      rdS 	 t          j                            dd                                          }|rt          |d          nd}n# t          $ r d}Y nw xY w	 t          j        | |           n# t          t          f$ r Y nw xY wt          |            dS )uX  Set directory to owner-only access (0700 by default). No-op on Windows.

    Skipped in managed mode — the NixOS module sets group-readable
    permissions (0750) so interactive users in the hermes group can
    share state with the gateway service.

    The mode can be overridden via the HERMES_HOME_MODE environment variable
    (e.g. HERMES_HOME_MODE=0701) for deployments where a web server (nginx,
    caddy, etc.) needs to traverse HERMES_HOME to reach a served subdirectory.
    The execute-only bit on a directory permits cd-through without exposing
    directory listings.

    Also applies ``HERMES_UID``/``HERMES_GID``-based ownership when those env
    vars are set (#34107 — Docker deployments need this so profile subdirs
    created at runtime by kanban workers don't land as root:root and block
    subsequent uid-mapped workers).
    NHERMES_HOME_MODEr      i  )r   r   r.  r   r   rI  rW   chmodr   rS  rT  )r  mode_strmodes      r#   _secure_dirr[    s    $ || :>>"4b99??AA#+6s8Q   
t()   s$   AA A('A(,B BBc                     t           j                            d          st           j                            d          rdS t           j                            d          rdS 	 t          ddd          5 } |                                 }d	d	d	           n# 1 swxY w Y   d
|v sd|v sd|v rdS n# t          t          f$ r Y nw xY wdS )a3  Detect if we're running inside a Docker/Podman/LXC container.

    When Hermes runs in a container with volume-mounted config files, forcing
    0o600 permissions breaks multi-process setups where the gateway and
    dashboard run as different UIDs or the volume mount requires broader
    permissions.
    HERMES_CONTAINERHERMES_SKIP_CHMODTz/.dockerenvz/proc/1/cgroupr&  r   r   Nr  lxckubepodsF)	r   r.  r   r  r   r0  readr   IOError)r6  cgroup_contents     r#   _is_containerrd    s!    
z~~()) RZ^^<O-P-P t	w~~m$$ t"C'::: 	&aVVXXN	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&~%%.)@)@JR`D`D`4 EaW   5s6   #B0 5B
B0 BB0 BB0 0CCc                     t                      st                      rdS 	 t          j                            t          |                     rt          j        | d           dS dS # t          t          f$ r Y dS w xY w)uQ  Set file to owner-only read/write (0600). No-op on Windows.

    Skipped in managed mode — the NixOS activation script sets
    group-readable permissions (0640) on config files.

    Skipped in containers — Docker/Podman volume mounts often need broader
    permissions.  Set HERMES_SKIP_CHMOD=1 to force-skip on other systems.
    Ni  )	r   rd  r   r  r   r*   rX  r   rS  )r  s    r#   _secure_filerf    s     || } 7>>#d))$$ 	"HT5!!!!!	" 	"()   s   AA% %A:9A:homec                     | dz  }|                                 rdS |                    t          d           t          |           dS )zISeed a default SOUL.md into HERMES_HOME if the user doesn't have one yet.zSOUL.mdNr   r   )r   r   r   rf  )rg  	soul_paths     r#   _ensure_default_soul_mdrj    sR    y I 7;;;rY   c                     t                      } t                      rSt          j        d          }	 t	          |            t          j        |           dS # t          j        |           w xY w|                     dd           t          |            dD ]-}| |z  }|                    dd           t          |           .t          |            dS )a   Ensure ~/.hermes directory structure exists with secure permissions.

    In managed mode (NixOS), dirs are created by the activation script with
    setgid + group-writable (2770). We skip mkdir and set umask(0o007) so
    any files created (e.g. SOUL.md) are group-writable (0660).
       Tr   )
cronsessionslogszlogs/curatormemoriespairinghooksimage_cacheaudio_cacheskillsN)r   r   r   umask_ensure_hermes_home_managedr   r[  rj  )rg  	old_umasksubdirds       r#   ensure_hermes_homer{    s     D|| &HUOO		 '---HYBHY

4$
///D
 	 	F vAGGD4G000NNNN%%%%%s   A A-c                    |                                  st          d|  d          dD ]-}| |z  }|                                 st          | d          .| dz  dz                      dd           t          |            dS )	zPManaged-mode variant: verify dirs exist (activation creates them), seed SOUL.md.zHERMES_HOME z7 does not exist. Run 'sudo nixos-rebuild switch' first.)rm  rn  ro  rp  ro  curatorTr   N)r   RuntimeErrorr   rj  )rg  ry  rz  s      r#   rw  rw    s    ;;== 
54 5 5 5
 
 	
 ;  6Mxxzz 	 9 9 9  	 
F]Y%%dT%BBBD!!!!!rY   modelr   	providersfallback_providerscredential_pool_strategiestoolsetsz
hermes-climax_concurrent_sessionsagent	max_turnsZ   gateway_timeouti  restart_drain_timeout   api_max_retries   service_tiertool_use_enforcementautotask_completion_guidanceTenvironment_probeenvironment_hintcoding_contextgateway_timeout_warningi  clarify_timeoutiX  gateway_notify_intervalgateway_auto_continue_freshnessi  image_input_modedisabled_toolsetsterminalr)  local
modal_modecwdr(   timeoutenv_passthroughshell_init_filesauto_source_bashrcdocker_image*nikolaik/python-nodejs:python3.11-nodejs20docker_forward_env
docker_envsingularity_image3docker://nikolaik/python-nodejs:python3.11-nodejs20modal_imagedaytona_imagecontainer_cpu   container_memoryi   container_diski   container_persistentF)docker_volumesdocker_mount_cwd_to_workspacedocker_extra_argsdocker_run_as_host_userpersistent_shellweb)r)  search_backendextract_backendbrowserx      must_respondi,  zhost.docker.internal)managed_persistenceuser_idsession_keyadopt_existing_tabrewrite_loopback_urlsloopback_host_alias)
inactivity_timeoutcommand_timeoutrecord_sessionsallow_private_urlsengineauto_local_for_private_urlscdp_urldialog_policydialog_timeout_scamofoxcheckpoints   i  
   rl     )enabledmax_snapshotsmax_total_size_mbmax_file_size_mb
auto_pruneretention_daysdelete_orphansmin_interval_hoursfile_read_max_charsi tool_outputiP  i  )	max_bytes	max_linesmax_line_lengthtool_loop_guardrails   )exact_failuresame_tool_failureidempotent_no_progress   rW  )warnings_enabledhard_stop_enabled
warn_afterhard_stop_aftercompression      ?皙?i  )r  	thresholdtarget_ratioprotect_last_nhygiene_hard_message_limitprotect_first_nabort_on_summary_failurecodex_gpt55_autoraiseprompt_caching	cache_ttl5m
openrouterg?)response_cacheresponse_cache_ttlmin_coding_scorebedrock)r  provider_filterrefresh_intervalasyncdisabled)guardrail_identifierguardrail_versionstream_processing_modetrace)region	discovery	guardrail	auxiliary)providerr  base_urlapi_keyr  
extra_bodydownload_timeoutih  )r  r  r  r  r  r  <   )visionweb_extractr  
skills_hubapprovalmcptitle_generationtts_audio_tagstriage_specifierkanban_decomposerprofile_describerr}  monitordisplaycompactpersonalityresume_displayfullresume_exchangesresume_max_user_charsresume_max_assistant_chars   resume_max_assistant_linesresume_skip_tool_onlybusy_input_mode	interrupt	interfaceclitui_auto_resume_recenttui_agents_nudgebell_on_completeshow_reasoning	streaming
timestampsfinal_response_markdownr   persistent_outputpersistent_output_max_linespersist_promptsinline_diffsfile_mutation_verifiercredits_noticesturn_completion_explainer	show_costskindefaultlanguageentui_status_indicatorkaomojiuser_message_preview)first_lines
last_linesinterim_assistant_messagestool_progress_commandtool_progress_overridestool_preview_lengthephemeral_system_ttl)telegramdiscord)r  context_pctr  )r  fields)	platformsruntime_footercopy_shortcut	dashboard)	client_id
portal_url)usernamepassword_hashpasswordsecretsession_ttl_seconds)themeshow_token_analyticsoauth
basic_auth
public_urlprivacy
redact_piittsedgevoicezen-US-AriaNeuralpNInz6obpgDQGcFmaJgBeleven_multilingual_v2)voice_idmodel_idzgpt-4o-mini-ttsalloy)r  rY  zgemini-2.5-flash-preview-ttsKore)r  rY  
audio_tagspersona_prompt_fileevei]  i  )r\  r5  sample_ratebit_ratezvoxtral-mini-tts-2603z$c69964a6-ab8b-4f8a-9465-ec0925096ec8)r  r\  zneuphonic/neutts-air-q4-ggufcpu)	ref_audioref_textr  devicezen_US-lessac-medium)	r  rX  
elevenlabsopenaigeminixaimistralneuttspipersttbase)r  r5  z	whisper-1zvoxtral-mini-latest	scribe_v2)r]  language_codetag_audio_eventsdiarize)r  r  r  rj  rm  ri  zctrl+bg      @)
record_keymax_recording_secondsauto_ttsbeep_enabledsilence_thresholdsilence_durationhuman_delayoffi   i	  )rZ  min_msmax_mscontextr  
compressormemoryi  i_  )memory_enableduser_profile_enabledwrite_approvalmemory_char_limituser_char_limitr  
delegation2   )r  r  r  r  api_modeinherit_mcp_toolsetsmax_iterationschild_timeout_secondsreasoning_effortmax_concurrent_childrenmax_spawn_depthorchestrator_enabledsubagent_auto_approveprefill_messages_filegoalsru  )external_dirstemplate_varsinline_shellinline_shell_timeoutguard_agent_createdr  r}     )r  keep)r  interval_hoursmin_idle_hoursstale_after_daysarchive_after_daysprune_builtinsbackuphonchotimezoneslack)require_mentionfree_response_channelsallowed_channelschannel_promptsrB  i   g
ףp=
?gQ?g      ?)zLet me look into that.zOne moment.zChecking on that now.zGive me a sec.zOn it.)r  ambient_enabledambient_pathambient_gain	duck_gainspeech_gainack_enabledack_phrases)r  r  r  auto_threadthread_require_mentionhistory_backfillhistory_backfill_limit	reactionsr  dm_role_auth_guildserver_actionsallow_any_attachmentmax_attachment_bytesvoice_fxwhatsapprA  )r  r  allowed_chats
mattermostmatrix)r  free_response_roomsallowed_rooms	approvalsmanualdeny)rZ  r  	cron_modemcp_reload_confirmdestructive_slash_confirmcommand_allowlistquick_commandsrr  hooks_auto_acceptpersonalitiessecuritytirith)r  domainsshared_files)	r  redact_secretstirith_enabledtirith_pathtirith_timeouttirith_fail_openwebsite_blocklistacked_advisoriesallow_lazy_installsrm  )wrap_responsemax_parallel_jobskanbani    i@8  )dispatch_in_gatewaydispatch_interval_secondsfailure_limitworker_log_rotate_bytesworker_log_backup_countorchestrator_profiledefault_assigneemax_in_progress_per_profileauto_decomposeauto_decompose_per_tickdispatch_stale_timeout_secondscode_executionrZ  projecttoolstool_search)r  threshold_pctsearch_default_limitmax_search_limitloggingINFO)levelmax_size_mbbackup_countmodel_catalogzAhttps://hermes-agent.nousresearch.com/docs/api/model-catalog.json)r  url	ttl_hoursr  network
force_ipv4gateway)strictmedia_delivery_allow_dirstrust_recent_filestrust_recent_files_secondsg?u    ▉g      N@)r  	transportedit_intervalbuffer_thresholdcursorfresh_final_after_secondsrn  )r  r  vacuum_after_pruner  write_json_snapshots
onboardingask)seenprofile_buildupdatesstash)pre_update_backupbackup_keepnon_interactive_local_changeslspdocumentg      @)r  	wait_modewait_timeoutinstall_strategyserversx_searchzgrok-4.20-reasoning)r  timeout_secondsretriessecrets	bitwardenBWS_ACCESS_TOKEN)r  access_token_env
project_idcache_ttl_secondsoverride_existingauto_install
server_urlpaste_collapse_threshold!paste_collapse_threshold_fallbackpaste_collapse_char_threshold_config_version   )FIRECRAWL_API_KEYBROWSERBASE_API_KEYBROWSERBASE_PROJECT_IDFAL_KEYVOICE_TOOLS_OPENAI_KEYELEVENLABS_API_KEY)r{   ri   WHATSAPP_ALLOWED_USERSSLACK_BOT_TOKENSLACK_APP_TOKENSLACK_ALLOWED_USERSTAVILY_API_KEYTERMINAL_MODAL_MODE)r     r  r     ENV_VARS_BY_VERSIONNOUS_BASE_URLzNous Portal base URL overridez.Nous Portal base URL (leave empty for default)r  descriptionpromptr  rM  categoryadvancedOPENROUTER_API_KEYz>OpenRouter API key (for vision, web scraping helpers, and MoA)zOpenRouter API keyzhttps://openrouter.ai/keysvision_analyzemixture_of_agents)r2  r3  r  rM  r  r4  r5  GOOGLE_API_KEYz<Google AI Studio API key (also recognized as GEMINI_API_KEY)zGoogle AI Studio API keyz&https://aistudio.google.com/app/apikeyGEMINI_API_KEYz3Google AI Studio API key (alias for GOOGLE_API_KEY)zGemini API keyGEMINI_BASE_URLz"Google AI Studio base URL overridez)Gemini base URL (leave empty for default)XAI_API_KEYzxAI API keyzhttps://console.x.ai/XAI_BASE_URLzxAI base URL overridez&xAI base URL (leave empty for default)NVIDIA_API_KEYz;NVIDIA NIM API key (build.nvidia.com or local NIM endpoint)zNVIDIA NIM API keyzhttps://build.nvidia.com/NVIDIA_BASE_URLzJNVIDIA NIM base URL override (e.g. http://localhost:8000/v1 for local NIM)z-NVIDIA NIM base URL (leave empty for default)
LM_API_KEYz5LM Studio bearer token for auth-enabled local serversz LM Studio API key / bearer tokenLM_BASE_URLzLM Studio base URL overridez,LM Studio base URL (leave empty for default)GLM_API_KEYzBZ.AI / GLM API key (also recognized as ZAI_API_KEY / Z_AI_API_KEY)zZ.AI / GLM API keyzhttps://z.ai/ZAI_API_KEYz$Z.AI API key (alias for GLM_API_KEY)zZ.AI API keyZ_AI_API_KEYGLM_BASE_URLzZ.AI / GLM base URL overridez-Z.AI / GLM base URL (leave empty for default)KIMI_API_KEYzKimi / Moonshot API keyzKimi API keyzhttps://platform.moonshot.cn/KIMI_BASE_URLz!Kimi / Moonshot base URL overridez'Kimi base URL (leave empty for default)KIMI_CN_API_KEYzKimi / Moonshot China API keyzKimi (China) API keySTEPFUN_API_KEYzStepFun Step Plan API keyzhttps://platform.stepfun.com/STEPFUN_BASE_URLz#StepFun Step Plan base URL overridez4StepFun Step Plan base URL (leave empty for default)ARCEEAI_API_KEYzArcee AI API keyzhttps://chat.arcee.ai/ARCEE_BASE_URLzArcee AI base URL overridez(Arcee base URL (leave empty for default)GMI_API_KEYzGMI Cloud API keyzhttps://www.gmicloud.ai/GMI_BASE_URLzGMI Cloud base URL overridez,GMI Cloud base URL (leave empty for default)MINIMAX_API_KEYzMiniMax API key (international)zMiniMax API keyzhttps://www.minimax.io/MINIMAX_BASE_URLzMiniMax base URL overridez*MiniMax base URL (leave empty for default)MINIMAX_CN_API_KEYz MiniMax API key (China endpoint)zMiniMax (China) API keyzhttps://www.minimaxi.com/MINIMAX_CN_BASE_URLz!MiniMax (China) base URL overridez2MiniMax (China) base URL (leave empty for default)DEEPSEEK_API_KEYz+DeepSeek API key for direct DeepSeek accesszDeepSeek API Keyz&https://platform.deepseek.com/api_keysr2  r3  r  rM  r4  DEEPSEEK_BASE_URLz'Custom DeepSeek API base URL (advanced)zDeepSeek Base URLDASHSCOPE_API_KEYz>Alibaba Cloud DashScope API key (Qwen + multi-provider models)zDashScope API Keyz-https://modelstudio.console.alibabacloud.com/DASHSCOPE_BASE_URLzGCustom DashScope base URL (default: coding-intl OpenAI-compat endpoint)zDashScope Base URLHERMES_QWEN_BASE_URLzBQwen Portal base URL override (default: https://portal.qwen.ai/v1)z.Qwen Portal base URL (leave empty for default)HERMES_GEMINI_CLIENT_IDzfGoogle OAuth client ID for google-gemini-cli (optional; defaults to Google's public gemini-cli client)uK   Google OAuth client ID (optional — leave empty to use the public default)z1https://console.cloud.google.com/apis/credentialsHERMES_GEMINI_CLIENT_SECRETz;Google OAuth client secret for google-gemini-cli (optional)z%Google OAuth client secret (optional)HERMES_GEMINI_PROJECT_IDz@GCP project ID for paid Gemini tiers (free tier auto-provisions)z;GCP project ID for Gemini OAuth (leave empty for free tier)OPENCODE_ZEN_API_KEYz=OpenCode Zen API key (pay-as-you-go access to curated models)zOpenCode Zen API keyzhttps://opencode.ai/authOPENCODE_ZEN_BASE_URLzOpenCode Zen base URL overridez/OpenCode Zen base URL (leave empty for default)OPENCODE_GO_API_KEYz<OpenCode Go API key ($10/month subscription for open models)zOpenCode Go API keyOPENCODE_GO_BASE_URLzOpenCode Go base URL overridez.OpenCode Go base URL (leave empty for default)HF_TOKENzVHugging Face token for Inference Providers (20+ open models via router.huggingface.co)zHugging Face Tokenz&https://huggingface.co/settings/tokensHF_BASE_URLz2Hugging Face Inference Providers base URL overridez%HF base URL (leave empty for default)OLLAMA_API_KEYu>   Ollama Cloud API key (ollama.com — cloud-hosted open models)zOllama Cloud API keyzhttps://ollama.com/settingsOLLAMA_BASE_URLz?Ollama Cloud base URL override (default: https://ollama.com/v1)z)Ollama base URL (leave empty for default)XIAOMI_API_KEYzhXiaomi MiMo API key for MiMo models (mimo-v2.5-pro, mimo-v2.5, mimo-v2-pro, mimo-v2-omni, mimo-v2-flash)zXiaomi MiMo API Keyzhttps://platform.xiaomimimo.comXIAOMI_BASE_URLzFXiaomi MiMo base URL override (default: https://api.xiaomimimo.com/v1)z)Xiaomi base URL (leave empty for default)
AWS_REGIONz?AWS region for Bedrock API calls (e.g. us-east-1, eu-central-1)z
AWS RegionzIhttps://docs.aws.amazon.com/bedrock/latest/userguide/bedrock-regions.htmlAWS_PROFILEzFAWS named profile for Bedrock authentication (from ~/.aws/credentials)zAWS ProfileAZURE_FOUNDRY_API_KEYz0Azure Foundry API key for custom Azure endpointszAzure Foundry API Keyzhttps://ai.azure.com/AZURE_FOUNDRY_BASE_URLzLAzure Foundry base URL (set via 'hermes model' for endpoint-specific config)zAzure Foundry base URLEXA_API_KEYz1Exa API key for AI-native web search and contentszExa API keyzhttps://exa.ai/
web_searchr  tool)r2  r3  r  r  rM  r4  PARALLEL_API_KEYz5Parallel API key for AI-native web search and extractzParallel API keyzhttps://parallel.ai/r!  z-Firecrawl API key for web search and scrapingzFirecrawl API keyzhttps://firecrawl.dev/FIRECRAWL_API_URLz6Firecrawl API URL for self-hosted instances (optional)z)Firecrawl API URL (leave empty for cloud)FIRECRAWL_GATEWAY_URLzQExact Firecrawl tool-gateway origin override for Nous Subscribers only (optional)z9Firecrawl gateway URL (leave empty to derive from domain)TOOL_GATEWAY_DOMAINzShared tool-gateway domain suffix for Nous Subscribers only, used to derive vendor hosts, e.g. nousresearch.com -> firecrawl-gateway.nousresearch.comzTool-gateway domain suffixTOOL_GATEWAY_SCHEMEzShared tool-gateway URL scheme for Nous Subscribers only, used to derive vendor hosts (`https` by default, set `http` for local gateway testing)zTool-gateway URL schemeTOOL_GATEWAY_USER_TOKENzuExplicit Nous Subscriber access token for tool-gateway requests (optional; otherwise read from the Hermes auth store)zTool-gateway user tokenz3Tavily API key for AI-native web search and extractzTavily API keyzhttps://app.tavily.com/homeSEARXNG_URLz<URL of your SearXNG instance for free self-hosted web searchz(SearXNG URL (e.g. http://localhost:8080)z"https://searxng.github.io/searxng/BRAVE_SEARCH_API_KEYzABrave Search API subscription token (free tier: 2,000 queries/mo)zBrave Search subscription tokenzhttps://brave.com/search/api/r"  uU   Browserbase API key for cloud browser (optional — local browser works without this)zBrowserbase API keyzhttps://browserbase.com/browser_navigatebrowser_clickr#  uC   Browserbase project ID (optional — only needed for cloud browser)zBrowserbase project IDBROWSER_USE_API_KEYuU   Browser Use API key for cloud browser (optional — local browser works without this)zBrowser Use API keyzhttps://browser-use.com/FIRECRAWL_BROWSER_TTLz@Firecrawl browser session TTL in seconds (optional, default 300)zBrowser session TTL (seconds))r2  r3  r  rM  r4  AGENT_BROWSER_ENGINEzaBrowser engine for local mode: auto (default Chrome), lightpanda (faster, no screenshots), chromez'Browser engine (auto/lightpanda/chrome)z,https://github.com/vercel-labs/agent-browser)ru  browser_snapshotrv  browser_vision)r2  r3  r  r  rM  r4  r5  CAMOFOX_URLzYCamofox browser server URL for local anti-detection browsing (e.g. http://localhost:9377)zCamofox server URLz)https://github.com/jo-inc/camofox-browserr$  z*FAL API key for image and video generationzFAL API keyzhttps://fal.ai/image_generatevideo_generateKREA_API_KEYz9Krea API key for Krea 2 image generation (Medium + Large)zKrea API keyz'https://www.krea.ai/settings/api-tokensz?OpenAI API key for voice transcription (Whisper) and OpenAI TTSz&OpenAI API Key (for Whisper STT + TTS)z$https://platform.openai.com/api-keysvoice_transcription
openai_ttszMElevenLabs API key for premium text-to-speech voices and Scribe transcriptionzElevenLabs API keyzhttps://elevenlabs.io/elevenlabs_ttsMISTRAL_API_KEYz7Mistral API key for Voxtral TTS and transcription (STT)zMistral API keyzhttps://console.mistral.ai/GITHUB_TOKENzCGitHub token for Skills Hub (higher API rate limits, skill publish)zGitHub Tokenz"https://github.com/settings/tokensNOTION_API_KEYz5Notion integration token (used by the `notion` skill)zNotion API keyz%https://www.notion.so/my-integrationsskillLINEAR_API_KEYz4Linear personal API key (used by the `linear` skill)zLinear API keyz,https://linear.app/settings/account/securityAIRTABLE_API_KEYz=Airtable personal access token (used by the `airtable` skill)zAirtable API keyz"https://airtable.com/create/tokensTENOR_API_KEYz=Tenor API key for GIF search (used by the `gif-search` skill)zTenor API keyz5https://developers.google.com/tenor/guides/quickstartHONCHO_API_KEYz.Honcho API key for AI-native persistent memoryzHoncho API keyzhttps://app.honcho.devhoncho_contextHONCHO_BASE_URLz=Base URL for self-hosted Honcho instances (no API key needed)z,Honcho base URL (e.g. http://localhost:8000))r2  r3  r4  HERMES_LANGFUSE_PUBLIC_KEYz'Langfuse project public key (pk-lf-...)zLangfuse public keyzhttps://cloud.langfuse.comHERMES_LANGFUSE_SECRET_KEYz'Langfuse project secret key (sk-lf-...)zLangfuse secret keyHERMES_LANGFUSE_BASE_URLz9Langfuse server URL (default: https://cloud.langfuse.com)z8Langfuse server URL (leave empty for cloud.langfuse.com)TELEGRAM_BOT_TOKENz"Telegram bot token from @BotFatherzTelegram bot tokenzhttps://t.me/BotFather	messagingTELEGRAM_ALLOWED_USERSzSComma-separated Telegram user IDs allowed to use the bot (get ID from @userinfobot)z+Allowed Telegram user IDs (comma-separated)zhttps://t.me/userinfobotTELEGRAM_PROXYzaProxy URL for Telegram connections (overrides HTTPS_PROXY). Supports http://, https://, socks5://zTelegram proxy URL (optional))r2  r3  rM  r4  DISCORD_BOT_TOKENz'Discord bot token from Developer PortalzDiscord bot tokenz+https://discord.com/developers/applicationsDISCORD_ALLOWED_USERSz7Comma-separated Discord user IDs allowed to use the botz*Allowed Discord user IDs (comma-separated)DISCORD_REPLY_TO_MODEzDiscord reply threading mode: 'off' (no reply references), 'first' (reply on first message only, default), 'all' (reply on every chunk)z"Discord reply mode (off/first/all)r(  zSlack bot token (xoxb-). Get from OAuth & Permissions after installing your app. Required scopes: chat:write, app_mentions:read, channels:history, groups:history, im:history, im:read, im:write, users:read, files:read, files:writezSlack Bot Token (xoxb-...)zhttps://api.slack.com/appsr)  u   Slack app-level token (xapp-) for Socket Mode. Get from Basic Information → App-Level Tokens. Also ensure Event Subscriptions include: message.im, message.channels, message.groups, app_mentionzSlack App Token (xapp-...)MATTERMOST_URLz3Mattermost server URL (e.g. https://mm.example.com)zMattermost server URLzhttps://mattermost.com/deploy/MATTERMOST_TOKENz-Mattermost bot token or personal access tokenzMattermost bot tokenMATTERMOST_ALLOWED_USERSz:Comma-separated Mattermost user IDs allowed to use the botz-Allowed Mattermost user IDs (comma-separated)MATTERMOST_REQUIRE_MENTIONzaRequire @mention in Mattermost channels (default: true). Set to false to respond to all messages.zRequire @mention in channels!MATTERMOST_FREE_RESPONSE_CHANNELSzJComma-separated Mattermost channel IDs where bot responds without @mentionz+Free-response channel IDs (comma-separated)MATRIX_HOMESERVERz7Matrix homeserver URL (e.g. https://matrix.example.org)zMatrix homeserver URLz%https://matrix.org/ecosystem/servers/MATRIX_ACCESS_TOKENz3Matrix access token (preferred over password login)zMatrix access tokenMATRIX_USER_IDz)Matrix user ID (e.g. @hermes:example.org)zMatrix user ID (@user:server)MATRIX_ALLOWED_USERSzLComma-separated Matrix user IDs allowed to use the bot (@user:server format)z)Allowed Matrix user IDs (comma-separated)r   zZRequire @mention in Matrix rooms (default: true). Set to false to respond to all messages.z&Require @mention in rooms (true/false)r   zCComma-separated Matrix room IDs where bot responds without @mentionz(Free-response room IDs (comma-separated)r   z@Auto-create threads for messages in Matrix rooms (default: true)z)Auto-create threads in rooms (true/false)r   z>Auto-create threads for DM messages in Matrix (default: false)z'Auto-create threads in DMs (true/false)rt   zNStable Matrix device ID for E2EE persistence across restarts (e.g. HERMES_BOT)z)Matrix device ID (stable across restarts)r   u   Matrix recovery key for cross-signing verification after device key rotation (from Element: Settings → Security → Recovery Key)zMatrix recovery keyr   zOBlueBubbles server URL for iMessage integration (e.g. http://192.168.1.10:1234)zBlueBubbles server URLzhttps://bluebubbles.app/r   uJ   BlueBubbles server password (from BlueBubbles Server → Settings → API)zBlueBubbles server passwordBLUEBUBBLES_ALLOWED_USERSzJComma-separated iMessage addresses (email or phone) allowed to use the botz,Allowed iMessage addresses (comma-separated)BLUEBUBBLES_ALLOW_ALL_USERSz-Allow all BlueBubbles users without allowlistzAllow All BlueBubbles Usersr^   z.QQ Bot App ID from QQ Open Platform (q.qq.com)z	QQ App IDzhttps://q.qq.com)r2  r3  r  r4  rw   z*QQ Bot Client Secret from QQ Open PlatformzQQ Client Secretrv   z2Comma-separated QQ user IDs allowed to use the botzQQ Allowed Usersr   z=Comma-separated QQ group IDs allowed to interact with the botzQQ Group Allowed Usersr   z4Allow all QQ users without an allowlist (true/false)zAllow All QQ Usersr   z<Default QQ channel/group for cron delivery and notificationszQQ Home Channelr   z$Display name for the QQ home channelzQQ Home Channel Name
QQ_SANDBOXz;Enable QQ sandbox mode for development testing (true/false)zQQ Sandbox Moder_   z*IRC server hostname (e.g. irc.libera.chat)z
IRC serverr`   z"IRC channel to join (e.g. #hermes)zIRC channelrb   z)Bot nickname on IRC (default: hermes-bot)zIRC nicknamer   z!IRC server password (if required)zIRC server passwordr   z)NickServ password for nick identificationzNickServ passwordGATEWAY_ALLOW_ALL_USERSzMAllow all users to interact with messaging bots (true/false). Default: false.zAllow all users (true/false)API_SERVER_ENABLEDzrEnable the OpenAI-compatible API server (true/false). Allows frontends like Open WebUI, LobeChat, etc. to connect.zEnable API server (true/false)API_SERVER_KEYz|Bearer token for API server authentication. Required whenever the API server is enabled; server refuses to start without it.zAPI server auth keyAPI_SERVER_PORTz(Port for the API server (default: 8642).zAPI server portAPI_SERVER_HOSTzsHost/bind address for the API server (default: 127.0.0.1). API_SERVER_KEY is still required even on loopback binds.zAPI server hostAPI_SERVER_MODEL_NAMEzModel name advertised on /v1/models. Defaults to the profile name (or 'hermes-agent' for the default profile). Useful for multi-user setups with OpenWebUI.zAPI server model nameGATEWAY_PROXY_URLu  URL of a remote Hermes API server to forward messages to (proxy mode). When set, the gateway handles platform I/O only — all agent work is delegated to the remote server. Use for Docker E2EE containers that relay to a host agent. Also configurable via gateway.proxy_url in config.yaml.z=Remote Hermes API server URL (e.g. http://192.168.1.100:8642)GATEWAY_PROXY_KEYzBearer token for authenticating with the remote Hermes API server (proxy mode). Must match the API_SERVER_KEY on the remote host.zRemote API server auth keyWEBHOOK_ENABLEDzREnable the webhook platform adapter for receiving events from GitHub, GitLab, etc.zEnable webhooks (true/false)WEBHOOK_PORTz1Port for the webhook HTTP server (default: 8644).zWebhook portWEBHOOK_SECRETz[Global HMAC secret for webhook signature validation (overridable per route in config.yaml).zWebhook secretSUDO_PASSWORDzySudo password for terminal commands requiring root access; set to an explicit empty string to try empty without promptingzSudo passwordsettingHERMES_PREFILL_MESSAGES_FILEzFPath to JSON file with ephemeral prefill messages for few-shot primingzPrefill messages file pathHERMES_EPHEMERAL_SYSTEM_PROMPTzOEphemeral system prompt injected at API-call time (never persisted to sessions)zEphemeral system promptrequired_onlyc                 :   g }t                                           D ]1\  }}t          |          s|                    d|i|ddi           2| sKt                                          D ]1\  }}t          |          s|                    d|i|ddi           2|S )z|
    Check which environment variables are missing.
    
    Returns list of dicts with var info for missing variables.
    r   is_requiredTF)REQUIRED_ENV_VARSitemsget_env_valueappendOPTIONAL_ENV_VARS)r  missingvar_namer5  s       r#   get_missing_env_varsr    s     G ,1133 L L$X&& 	LNNFHJJmTJJKKK  Q/5577 	Q 	QNHd ** QODO-OOPPPNrY   
dotted_keyc           	      j   |                     d          }| }|dd         D ]}t          |t                    rE	 t          |          }n+# t          t
          f$ r t	          d|d|d          w xY w||         }\t          |t                    rC|                    |          }||vst          |t          t          f          si ||<   ||         }t	          dt          |          j	         d|          |d         }t          |t                    r||t          |          <   dS |||<   dS )	u  Set a value at an arbitrarily nested dotted key path.

    Supports both dict and list navigation:
      _set_nested(c, "a.b.c", 1)     → c["a"]["b"]["c"] = 1
      _set_nested(c, "a.0.b", 1)     → c["a"][0]["b"] = 1
      _set_nested(c, "providers.1", "x") → c["providers"][1] = "x"

    Intermediate dicts are created on demand.  List indices are parsed
    from numeric path segments; the referenced index must already exist
    (we do not grow lists — the user is navigating into structure they
    wrote themselves).  If a segment targets a non-container leaf
    (scalar), the leaf is replaced with a fresh dict so the write can
    proceed — this preserves the pre-existing behavior for bare scalar
    overrides (e.g. setting ``a.b.c`` where ``a.b`` was previously a
    string).

    Guards against #17876: before this fix the code unconditionally
    replaced any non-dict value (including lists) with ``{}``, silently
    destroying list-typed config like ``custom_providers`` whenever a
    caller used an indexed path.
    r(   NrP  z!Cannot navigate into list at key z
: segment z is not a numeric indexzCannot navigate into z at key )
split
isinstancer   rI  	TypeErrorrW   dictr   type__name__)	configr  r9  partscurrentpartidxr"   lasts	            r#   _set_nestedr  5  s|   , S!!EGcrc
  gt$$ 	$iiz*   ?
 ? ?#? ? ?  
 clGG&& 		{{4((H7""*Xd|*L*L" "dmGGVW(>VV
VV   9D'4   "D		s   A

(A2c                      t                      } g ddt          dt          dt          ffd t          |            S )z
    Check which config fields are missing or outdated (recursive).
    
    Walks the DEFAULT_CONFIG tree at arbitrary depth and reports any keys
    present in defaults but absent from the user's loaded config.
    r   defaultsr  r
  c                 `   |                                  D ]\  }}|                    d          r|s|n| d| }||vr                    ||d| d           Gt          |t                    r;t          |                    |          t                    r |||         |           d S )Nr8  r(   zNew config option: )r3   r4  r2  )r  r1  r  r  r  r   )r  r  r
  r3   default_valuefull_key_checkr  s         r#   r  z)get_missing_config_fields.<locals>._checkr  s    "*.."2"2 	> 	>C~~c"" "(?ss.?.?#.?.?H'!!#,#C#C#C       
 M400 >ZC@P@PRV5W5W >}gclH===	> 	>rY   )r   )load_configr  r*   DEFAULT_CONFIG)r  r  r  s    @@r#   get_missing_config_fieldsr  h  sj     ]]FG> > > >c > > > > > > > F>6"""NrY   c                  6   	 ddl m} m} n# t          $ r g cY S w xY w	  |             }nF# t          $ r9}ddl} |j        t                                        d|           g cY d}~S d}~ww xY w|sg S t                      }g }|D ]}| d|d          }|	                    d          }	|}
d}|	D ](}t          |
t                    r||
v r|
|         }
|
}&d} |)t          |t                    r)|                                s|                    |           |S )a3  Return skill-declared config vars that are missing or empty in config.yaml.

    Scans all enabled skills for ``metadata.hermes.config`` entries, then checks
    which ones are absent or empty under ``skills.config.<key>`` in the user's
    config.yaml.  Returns a list of dicts suitable for prompting.
    r   )discover_all_skill_config_varsSKILL_CONFIG_PREFIXNz)discover_all_skill_config_vars failed: %sr(   r3   )agent.skill_utilsr  r  r   r  	getLoggerr  debugr  r  r  r  r*   r   r  )r  r  all_varser  r  r  varstorage_keyr  r  r9  r  s                r#   get_missing_skill_config_varsr    s   YYYYYYYYY   			
1133    	(##))7	
 	
 	
 						  	]]F$&G    ,;;s5z;;!!#&& 	 	D'4(( TW__!$-=Zs33=EKKMM=NN3Ns&    
) 
A,.A'!A,'A,provider_keyentryr  c          	       
   t          | t                    sdS ddddddddd	}d
| v rd| vr| d
         | d<   h d}|                                D ]7\  }}|| v r.|| vr*t                              d|pd||           | |         | |<   8t          |                                           |z
  t          |                                          z
  }|r>t                              d|pdd                    t          |                               ddl	m
} d}dD ]}	|                     |	          }
t          |
t                    rd|
                                rP|
                                } ||          }|j        r|j        r|} n t                              d|pd|	|           |sdS d}|                     d          }t          |t                    r)|                                r|                                }n(|                                r|                                }|sdS ||d}|                                }|r||d<   |                     d          }t          |t                    r+|                                r|                                |d<   |                     d          }t          |t                    r+|                                r|                                |d<   |                     d          p|                     d          }t          |t                    r+|                                r|                                |d<   |                     d          p|                     d          }t          |t                    r+|                                r|                                |d<   |                     d          }t          |t                    r|r||d<   n&t          |t                     r|rd |D             |d<   |                     d          }t          |t"                    r|dk    r||d<   |                     d          }t          |t"          t$          f          r|dk    r||d<   |                     d          }t          |t&                    r||d<   |                     d          }t          |t                    rt          |          |d<   |S )z>Return a runtime-compatible custom provider entry or ``None``.Nr  r  r  key_envdefault_modelcontext_lengthrate_limit_delay)apiKeybaseUrlapiModekeyEnv	apiKeyEnvdefaultModelcontextLengthrateLimitDelayapi_key_env>   apir  r   r  modelsr  r  r  r  r  r  r  r  r  discover_modelsr  stale_timeout_secondsrequest_timeout_secondsz[providers.%s: camelCase key '%s' auto-mapped to '%s' (use snake_case to avoid this warning)?z-providers.%s: unknown config keys ignored: %s, r   urlparser   )r  r  r  uP   providers.%s: '%s' value '%s' is not a valid URL (no scheme or host) — skippedr   )r   r  r  r  r  r  c                     i | ];}t          |t                    |                                +t          |          i <S r   )r  r*   r   ).0ms     r#   
<dictcomp>z4_normalize_custom_provider_entry.<locals>.<dictcomp>   sO      
  
  
*Q*<*< 
AB 
FFB 
  
  
rY   r  r  )r  r  r  r-   r.   setkeysjoinsortedurllib.parser  r   r*   r   schemenetlocr   rI  floatr	  )r  r  _CAMEL_ALIASES_KNOWN_KEYScamelsnakeunknownr  r  url_keyraw_url	candidateparsedr   raw_namer   r  r  r  
model_namer  r  r  r  r  s                            r#    _normalize_custom_provider_entryr    sh    eT"" t '),	& 	&N )5"8"8 /i  K ',,.. ( (uE>>e500NN9#UE  
 !<E%L%**,,+-N4G4G4I4I0J0JJG 
;C6'??!;!;	
 	
 	

 &%%%%%H-  ))G$$gs## 	 	IXi((F}  $6 'C)  
  tDyy  H(C   $X^^%5%5 $~~					 $!!## t " "J
  %%''L 2%1
>"ii	""G'3 0GMMOO 0 '
9ii	""G'3 0GMMOO 0 '
9yy$$>		+(>(>H(C   2X^^%5%5 2!)!1!1
:7##Auyy'A'AJ*c"" 1z'7'7'9'9 1(..00
7YYx  F&$ 	
F 	
%
8	FD	!	! 
f 

 
  
 & 
  
  

8 YY/00N.#&& 6>A+=+='5
#$yy!344"S%L11 :6F!6K6K)9
%&ii 122O/4(( 8(7
$%<((J*d## 4#'
#3#3
< rY   c                    t          t          | t                    rt          |           n| |          }|dS d|d         i}dD ]}||v r||         ||<   d|v r|d         |d<   d|v r|d         |d	<   |S )
zDTranslate a legacy custom provider entry to the v12 providers shape.r  Nr  r  )r   r  r  r  r  r  r  r  r  r  r  r  )r  r  r  )r  r  r   provider_entryfields        r#   )_custom_provider_entry_to_provider_configr  7  s     2!%..9UE!  J t&+Z
-C%DN	 6 6 J$.u$5N5!**4W*='Z&0&<{#rY   providers_dictc                     t          | t                    sg S g }|                                 D ]:\  }}t          |t	          |                    }||                    |           ;|S )zMNormalize ``providers`` config entries into the legacy custom-provider shape.r  )r  r  r  r  r*   r  )r  custom_providersr3   r  r   s        r#   "providers_dict_to_custom_providersr  [  sw    nd++ 	-/$**,, 0 0
U5e#c((SSS
!##J///rY   r  c                    | t                      } g t                      t                      dt          t          t          t
          f                  ddffd}|                     d          }|4t          |t                    sg S |D ]} |t          |                     t          |                     d                    D ]} ||           S )ay  Return a deduplicated custom-provider view across legacy and v12+ config.

    ``custom_providers`` remains the on-disk legacy format, while ``providers``
    is the newer keyed schema.  Runtime and picker flows still need a single
    list-shaped view, but we should not materialise that compatibility layer
    back into config.yaml because it duplicates entries in UIs.
    Nr  r   c                 >   | d S t          |                     dd          pd                                                                          }t          |                     dd          pd                                                                          }t          |                     dd          pd                                                              d                                          }t          |                     dd          pd                                                                          }|||f}|r|v rd S |r|r|v rd S                     |            |r                    |           |r|r                    |           d S d S d S )Nr  r   r   r  r  r  )r*   r   r   r   rstripr  r,   )	r  r  r   r  r  pair
compatibleseen_name_url_pairsseen_provider_keyss	         r#   _append_if_newz7get_compatible_custom_providers.<locals>._append_if_newz  s   =F599^R88>B??EEGGMMOO599VR((.B//5577==??uyyR006B77==??FFsKKQQSSEIIgr**0b117799??AAh& 	L,>>>F 	H 	)<!<!<F%    	1""<000 	*H 	*##D)))))	* 	* 	* 	*rY   r  r  )r  r  r   r   r*   r   r   r  r   r  r  )r  r!  r  r  r  r  r   s       @@@r#   get_compatible_custom_providersr"  i  s    ~')J!ee"uu*htCH~6 *4 * * * * * * * *( zz"455#*D11 	I% 	D 	DEN;EBBCCCC3FJJ{4K4KLL  urY   r  r  c                    | r|sdS |T	 t          |          }nC# t          $ r6 |Y dS |                    d          }t          |t                    r|ng }Y nw xY wt          |t                    sdS |pd                    d          }|sdS |D ]}t          |t                    s|                    d          pd                    d          }|r||k    rK|                    d          }t          |t                    sv|                    |           }	t          |	t                    s|	                    d          }
|
	 t          |
          }n# t          t          f$ r Y w xY w|dk    r|c S dS )	a  Look up a per-model ``context_length`` override from ``custom_providers``.

    Matches any entry whose ``base_url`` equals ``base_url`` (trailing-slash
    insensitive) and returns ``custom_providers[i].models.<model>.context_length``
    if present and valid.  Returns ``None`` when no override applies.

    This is the single source of truth for custom-provider context overrides,
    used by:
      * ``AIAgent.__init__`` (startup resolution)
      * ``AIAgent.switch_model`` (mid-session ``/model`` switch)
      * ``hermes_cli.model_switch.resolve_display_context_length`` (``/model`` confirmation display)
      * ``gateway.run._format_session_info`` (``/info`` display)
      * ``agent.model_metadata.get_model_context_length`` (when custom_providers is threaded through)

    Before this helper existed, the lookup was duplicated in ``run_agent.py``'s
    startup path only; every other path (notably ``/model`` switch) fell back
    to the 128K default.  See #15779.
    Nr  r   r  r  r  r  r   )
r"  r   r   r  r   r  r  rI  r  rW   )r  r  r  r  r   
target_urlr  	entry_urlr  	model_cfgraw_ctxctxs               r#   "get_custom_provider_context_lengthr)    s   0   t	D>vFF 	D 	D 	D~tt**/00C&0d&;&;Css		D
 &-- t.b((--J t!  %&& 	YYz**0b88==	 	I338$$&$'' 	JJu%%	)T** 	-- 011?	g,,CC:& 	 	 	H	77JJJ 4s&    A.AAEE/.E/r9  c                     t          | t                    rdS 	 t          |           }n# t          t          f$ r Y dS w xY wt          |d          S )zHReturn a safe integer config version, treating invalid values as legacy.r   )r  r	  rI  r  rW   max)r9  versions     r#   _coerce_config_versionr-    s`    % qe**z"   qqw??s   ) >>c                     t          t                              dd                    pd} t                      }|                                s| | fS 	 t          |d          5 }t          j        |          pi }ddd           n# 1 swxY w Y   n+# t          $ r}t          ||           | | fcY d}~S d}~ww xY wt          |t                    si }t          |                    d                    }|| fS )a  
    Check the raw on-disk config schema version.

    ``load_config()`` deliberately starts from ``DEFAULT_CONFIG`` and deep-merges
    the user's file, which is correct for runtime reads but wrong for deciding
    whether the user's persisted schema has been migrated. A config file with no
    raw ``_config_version`` must remain visible as legacy instead of inheriting
    the latest default version in memory.

    Returns (current_version, latest_version).
    r  r  r   r   N)r-  r  r   r>  r   r0  yaml	safe_loadr   r5   r  r  )latestr   r6  r  r  r  s         r#   check_config_versionr2    sW    $N$6$67H!$L$LMMRQRF!##K v~+000 	-A^A&&,"F	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-    	#;222v~	 fd## $VZZ0A%B%BCCGF?sB   B #B:B B

B B
B 
B:B5/B:5B:>   r  r  r  r  r  r  r  rn  r  r  r  r  r(  r  r  fallback_modelr  r  r  r  >
   r   r  r  r  r  r  r  r  r  r  >   r  r  r  r  c                   2    e Zd ZU dZeed<   eed<   eed<   dS )ConfigIssuez$A detected config structure problem.severitymessagehintN)r  
__module____qualname____doc__r*   __annotations__r   rY   r#   r5  r5  !  s1         ..MMMLLL
IIIIIrY   r5  c                 d	   | 2	 t                      } n"# t          $ r t          ddd          gcY S w xY wg }|                     d          }|t	          |t
                    r|                    t          ddd                     t	          |t
                    r!t          |                                          nt                      }|t          z  }|r5|                    t          dd	t          |           d
d                     nt	          |t                    rt          |          D ]\  }}t	          |t
                    s>|                    t          dd| dt          |          j         dd                     X|                    d          s(|                    t          dd| dd                     |                    d          s(|                    t          dd| dd                     |                     d          }|t	          |t                    rt          |          D ]\  }}t	          |t
                    s=|                    t          dd| dt          |          j         d                     W|                    d          s(|                    t          dd| dd                     |                    d          s(|                    t          dd| dd                     nt	          |t
                    s:|                    t          dd t          |          j         d!                     nt|rr|                    d          s$|                    t          dd"d                     |                    d          s$|                    t          dd#d$                     t	          |t
                    r.d| vr*d|pi v r$|                    t          dd%d&                     |                     d          }|r&|s$|                    t          dd'd(                     | D ]V}	|	                    d)          r|	t           vr5|	t          v r,|                    t          dd*|	 d+d,|	 d-                     W|S ).a"  Validate config.yaml structure and return a list of detected issues.

    Catches common YAML formatting mistakes that produce confusing runtime
    errors (like "Unknown provider") instead of clear diagnostics.

    Can be called with a pre-loaded config dict, or will load from disk.
    NerrorzCould not load config.yamlz+Run 'hermes setup' to create a valid configr  uO   custom_providers is a dict — it must be a YAML list (items prefixed with '-')zeChange to:
  custom_providers:
    - name: my-provider
      base_url: https://...
      api_key: ...r.   zRoot-level keys z( look like custom_providers entry fieldszLThese should be indented under a '- name: ...' list entry, not at root levelzcustom_providers[z] is not a dict (got )z1Each entry should have at minimum: name, base_urlr   z] is missing 'name' fieldz#Add a name, e.g.: name: my-providerr  z] is missing 'base_url' fieldzDAdd the API endpoint URL, e.g.: base_url: https://api.example.com/v1r3  zfallback_model[z] should be a dict, got z!Each entry needs provider + modelr  z] is missing 'provider' fieldz/Add: provider: openrouter (or another provider)r  z] is missing 'model' fieldzAdd: model: <model-name>zAfallback_model should be a dict with 'provider' and 'model', got zZChange to:
  fallback_model:
    provider: openrouter
    model: anthropic/claude-sonnet-4uH   fallback_model is missing 'provider' field — fallback will be disableduE   fallback_model is missing 'model' field — fallback will be disabledz8Add: model: anthropic/claude-sonnet-4 (or another model)zGfallback_model appears inside custom_providers instead of at root levelzDMove fallback_model to the top level of config.yaml (no indentation)u[   custom_providers defined but no 'model' section — Hermes won't know which provider to useziAdd a model section:
  model:
    provider: custom
    default: your-model-name
    base_url: https://...r8  zRoot-level key 'uW   ' looks misplaced — should it be under 'model:' or inside a 'custom_providers' entry?zMove 'z' under the appropriate section)r  r   r5  r   r  r  r  r  r  _CUSTOM_PROVIDER_LIKE_FIELDSr  r   	enumerater  r  r1  _KNOWN_ROOT_KEYS)
r  issuescpcp_keys
suspiciousir  fbr&  r3   s
             r#   validate_config_structurerI  *  s    ~	w ]]FF 	w 	w 	w)EGtuuvvvv	w !#F 
&	'	'B	~b$ (	MM+a%     )32t(<(<Gc"''))nnn#%%G #??J kcvj'9'9cccb    
 D!! 	%bMM  5!%.. MM+![A[[DKKDX[[[K# #   
 yy(( MM+!HAHHH=# #   
 yy,, MM+!LALLL^# #    
$	%	%B	~b$ +	%bMM  5!%.. MM+[![[T%[[EY[[;# #     !99Z00 k%NaNNNM' '   
 !99W-- k%KaKKK6' '   ( B%% 	MM+gTXY[T\T\Tegg7       	66*%% k^E    
 66'?? k[N     "d  0 > >CSXZX`^`CaCakUR
 
 	 	 	 

7##I	 	) 	ki(
 
 	 	 	   >># 	&&&32N+N+NMM+3====     Ms    22c                 T   	 t          |           }n# t          $ r Y dS w xY w|sdS dg}|D ]1}|j        dk    rdnd}|                    d| d|j                    2|                    d           t
          j                            d	                    |          d
z              dS )zPrint config structure warnings to stderr at startup.

    Called early in CLI and gateway init so users see problems before
    they hit cryptic "Unknown provider" errors.  Prints nothing if
    config is healthy.
    Nu3   [33m⚠ Config issues detected in config.yaml:[0mr>  u   [31m✗[0mu   [33m⚠[0m  r   z2  [2mRun 'hermes doctor' for fix suggestions.[0mr)   

)	rI  r   r6  r  r7  r/   r0   r1   r   )r  rC  linescimarkers        r#   print_config_warningsrP    s    *622    HIE 1 1)+)?)?%%EY/&//2://0000	LLKLLLJTYYu%%./////s    
  c                    t           j                            d          }t           j                            d          }| !	 t                      } n# t          $ r Y dS w xY w|                     di           }t          |t                    r|                    dd          nd}|dv}g }|r|                    d| d	           |r|s|                    d
| d	           |rt           j                            dd          }|                    dd           |                    d           |                    d| d           t          j
                            d                    |          dz              dS dS )u   Warn if MESSAGING_CWD or TERMINAL_CWD is set in .env instead of config.yaml.

    These env vars are deprecated — the canonical setting is terminal.cwd
    in config.yaml.  Prints a migration hint to stderr.
    MESSAGING_CWDTERMINAL_CWDNr  r  r(   >   r   r  r  r(   u     [33m⚠[0m MESSAGING_CWD=u&    found in .env — this is deprecated.u     [33m⚠[0m TERMINAL_CWD=rE   z	~/.hermesr   u/   [33m⚠ Deprecated .env settings detected:[0mzN  [2mMove to config.yaml instead:  terminal:\n    cwd: /your/project/path[0mz'  [2mThen remove the old entries from z	/.env[0mr)   rL  )r   r.  r   r  r   r  r  r  insertr/   r0   r1   r   )r  messaging_cwdterminal_cwd_envterminal_cfg
config_cwdconfig_has_explicit_cwdrM  	hint_paths           r#   warn_deprecated_cwd_env_varsr[    s    JNN?33Mz~~n55~	 ]]FF 	 	 	FF	 ::j"--L1;L$1O1OX!!%---UXJ(0HHE 
#- # # #	
 	
 	
  
 7 
#1A # # #	
 	
 	
  
4JNN=+>>	QOPPP>	
 	
 	
 	PPPP	
 	
 	
 	
5))F233333
4 
4s   A 
AAinteractivequietc           
      3  ^ g g g d}	 t                      }|r|st          d| d           n# t          $ r Y nw xY wt                      \  }}|dk     rFt	                      }|                    di           }t          |t                    si }d|vrt          d          }t          d          }	|r7|	                                d	v r!d
|d<   |d         
                    d           n|	r_|		                                dv rI|		                                |d<   |d         
                    d|		                                 d           n d|d<   |d         
                    d           ||d<   t          |           |st          d|d                     |dk     rt	                      }d|vrt          j        dd          }
|
r]|
                                rI|
                                |d<   |d         
                    d|
                                 d           n d|d<   |d         
                    d           t          |           |s|d         pd}t          d|            |dk     rD	 t          d          }|r!t          dd           |st          d           n# t          $ r Y nw xY w|dk     rt	                      }|                    d           }t          |t                     r|r|                    d!i           }t          |t                    si }d"}|D ]}t          |t                    s|                    d#d          }|                    d$d          p-|                    d%d          p|                    d&d          pd}|sv|                                	                                                    d'd(                              d)d                              d*d          }d+|v r|                    d+d(          }d+|v |                    d(          }|sE	 d"d,lm}  ||          }|j        pd-                    d.d(          }n# t          $ r d/| }Y nw xY w|}|}||v r| d(| }|d0z  }||v t+          ||1          }||s|                    d#d2           |                    d3          d4v r|                    d3d2           |||<   |d0z  }|d"k    r||d!<   |                    d d2           t          |           |sqt          d5| d6           t!          |                                          | d2         D ]3}||         }t          d7| d8|                    d&d                      4|d9k     rLd:D ]I}	 t          |          }|r%t          |d           |st          d;| d<           :# t          $ r Y Fw xY w|d=k     r]t1                      }|                    d>i           }t          |t                    r#d?|v r|d?         }|                    d@dA          }t	                      }|                    d>i           } |                     d?d2           |dBv rSh dC}!||!v rJ|                    dAi           }"t          |"t                    rd?|"vr|                     dAi           }#||#d?<   nJ|                    |i           }$t          |$t                    rd?|$vr|                     |i           }%||%d?<   | |d><   t          |           |st          dD           |dEk     rt1                      }|                    di           }t          |t                    si }dF|vrEdG|dF<   ||d<   |d         
                    dH           t          |           |st          dI           |dJk     rEt1                      }|                    di           }t          |t                    si }|                    dK          }&t          |&t                    r|&r|                    dLi           }'t          |'t                    si }'|&                                D ]#\  }(})|(|'vri |'|(<   d|'|(         vr|)|'|(         d<   $|'|dL<   ||d<   t          |           |sCdM                    dN |&                                D                       }*t          dO|*            |d         
                    dP           |dQk     rt1                      }|                    dRi           }+t          |+t                    r|+                    dSd2          },|+                    dTd2          }-|+                    dUd2          }.g }/|,rt9          |,                                          r}|                    dVi           }0|0                    dRi           }1|1                    d?          s<t9          |,                                          |1d?<   |/
                    dW|,            |-rt9          |-                                          dXvr|                    dVi           }0|0                    dRi           }1|1                    d@          r|1                    d@          dYk    r<t9          |-                                          |1d@<   |/
                    dZ|-            |.rt9          |.                                          r}|                    dVi           }0|0                    dRi           }1|1                    d$          s<t9          |.                                          |1d$<   |/
                    d[|.            |/s|,|-|.M|+|dR<   t          |           |s7|/r&t          d\dM                    |/                      nt          d]           |d^k     r&t1                      }|                    d_          }2t          |2t                    si }2d`|2vr|2                    dag           pg }3t          |3t                     sg }3t;          |3          }4g }5	 t=                      d_z  }6|6                                rtA          |6!                                          D ]}7|7                                s|7dbz  }8|8"                                s|7dcz  }8|8"                                sJ	 tG          |8dde          5 }9tI          j%        |9          pi }:d2d2d2           n# 1 swxY w Y   n# t          $ r i }:Y nw xY w|:                    d#          p|7j&        };|;|4v r|5
                    |;           n# t          $ r g }5Y nw xY w|5|2d`<   |2|d_<   t          |           |d         
                    dftO          |5           dg           |s2|5r!t          dhtO          |5           di           nt          dj           |dkk     r	 t=                      dlz  dmz  }<|<(                    dGdGn           n8# t          $ r+}=|do         
                    dp|< d8|=            Y d2}=~=nd2}=~=ww xY wt1                      }dq}>tR                              dmi           }?|                    dm          }@t          |@t                    si }@g }A|?                                D ]5\  }B}C|B|@vr,tU          j+        |C          |@|B<   |A
                    |B           6|Ar|@|dm<   dG}>tR                              dVi                               dmi           }D|                    dV          }Et          |Et                    si }E|E                    dm          }Ft          |Ft                    si }Fg }G|D                                D ]5\  }B}C|B|Fvr,tU          j+        |C          |F|B<   |G
                    |B           6|Gr|F|Edm<   |E|dV<   dG}>|>rt          |           |ArS|d         
                    drtO          |A           ds           |s%t          dtdM                    |A                      |GrS|d         
                    dutO          |G           ds           |s%t          dvdM                    |G                      |dwk     rt1                      }|                    dx          }Ht          |Ht                    r^|H                    dy          dzk    rEd0|Hdy<   |H|dx<   t          |           |d         
                    d{           |st          d|           |d}k     rt1                      }dq}>d~D ]}I|                    |I          }Jt          |Jt                    rd|Jvr1|J                    d          }Kt          |Kt8                    r&|K                                	                                n|K}L|Ldk    |Jd<   |J||I<   dG}>|d         
                    |I d|Jd                     |>r t          |           |st          d           ||k     r|st          d| d|            tY          dG          }M|Mr7|s5t          d           |MD ]#}Nt          d|Nd#          d8|Nd                     $| r%|Mr"t          d           |MD ]}N|N                    d%          rt          d|Nd%                     |N                    d          rt[          d|Nd          d8          }On+t]          d|Nd          d8                                          }O|OrPt          |Nd#         |O           |d         
                    |Nd#                    t          d|Nd#                     n%|do         
                    d|Nd#          d           t                       tY          dq          }P|Mrd |MD             nt;                      ^^fd|PD             }Pt;                      }Qt_          |d0z   |d0z             D ]0}R|Q0                    tb                              |Rg                      1|Qr9| r6|s3d tA          |Q          D             }S|Srt          dtO          |S           d           |SD ].\  };}Tt          d|; d|T                    dd                      /t                       	 t]          d                                          	                                }Un# td          tf          f$ r d}UY nw xY w|Udv rVt                       |SD ]C\  };}T|T                    d%          r?t          d|T                    d|;                      t          d|Td%                     n&t          d|T                    d|;                      |T                    d          r(t[          d|T                    d|;           d          }On9t]          d|T                    d|;           d                                          }O|Or=t          |;|O           |d         
                    |;           t          d|;            t                       Ent          d           ti                      }V|Vr{t	                      }|VD ]U}W|Wd         }|Wd         }Xtk          |||X           |d         
                    |           |st          d| d|X            V||d<   t          |           n(||k     r"t	                      }||d<   t          |           tm                      }Y|Yr*| r'|s$t          dtO          |Y           d           |YD ]=}N|N                    dd          }Zt          d|Nd          d|Nd          d|Z d*           >t                       	 t]          d                                          	                                }Un# td          tf          f$ r d}UY nw xY w|Udv rTt                       t	                      }	 d"dl7m8}[ n# t          $ r d}[Y nw xY w|YD ]
}N|N                    dd          }X|Xrd|X d*nd}\t]          d|Nd          |\ d8                                          }O|Os|Xrt9          |X          }O|Or[|[ d.|Nd          }]tk          ||]|O           |d         
                    |Nd                    t          d|Nd          d|O            n<|do         
                    d|Nd          d|N                    dd           d           t                       t          |           nt          d           |S )a<  
    Migrate config to latest version, prompting for new required fields.
    
    Args:
        interactive: If True, prompt user for missing values
        quiet: If True, suppress output
        
    Returns:
        Dict with migration results: {"env_added": [...], "config_added": [...], "warnings": [...]}
    )	env_addedconfig_addedwarningsu     ✓ Repaired .env file (z corrupted entries fixed)r-  r  tool_progressr   r   >   0nofalser}  r`  z;display.tool_progress=off (from HERMES_TOOL_PROGRESS=false)>   allnewzdisplay.tool_progress=z! (from HERMES_TOOL_PROGRESS_MODE)rf  z#display.tool_progress=all (default)u-     ✓ Migrated tool progress to config.yaml: r  r  HERMES_TIMEZONEr   z	timezone=z (from HERMES_TIMEZONE)z$timezone= (empty, uses server-local)(server-local)u%     ✓ Added timezone to config.yaml: 	   rm   u8     ✓ Cleared ANTHROPIC_TOKEN from .env (no longer used)   r  r  r   r   r  r  r  r   r   (r?  z--r  endpointr(   z	endpoint-r  r  Nr  >   no-keyno-key-requiredr   u     ✓ Migrated z) custom provider(s) to providers: sectionu       → r'      )	LLM_MODELOPENAI_MODELu     ✓ Cleared u>    from .env (no longer used — config.yaml is source of truth)   rp  r  r  r  >   r  local_command>   base.entiny.enlarge-v1large-v2large-v3small.en	medium.enlarge-v3-turbodistil-large-v2distil-large-v3distil-small.endistil-medium.endistil-large-v3.5rq  tinylargesmallturbomediumu;     ✓ Migrated legacy stt.model to provider-specific config   r<  Tz1display.interim_assistant_messages=true (default)u3     ✓ Added display.interim_assistant_messages=true   r>  rE  r  c              3   *   K   | ]\  }}| d | V  dS )r'  Nr   )r  pr  s      r#   	<genexpr>z!migrate_config.<locals>.<genexpr>  s0      $R$RDAqZZAZZ$R$R$R$R$R$RrY   u>     ✓ Migrated tool_progress_overrides → display.platforms: z9display.platforms (migrated from tool_progress_overrides)   r  summary_modelsummary_providersummary_base_urlr  model=>   r   r  r  	provider=z	base_url=u@     ✓ Migrated compression.summary_* → auxiliary.compression: u/     ✓ Removed unused compression.summary_* keys   pluginsr  r  plugin.yaml
plugin.ymlr   r   z$plugins.enabled (opt-in allow-list, z grandfathered)u(     ✓ Plugins now opt-in: grandfathered z( existing plugin(s) into plugins.enabledum     ✓ Plugins now opt-in: no existing plugins to grandfather. Use `hermes plugins enable <name>` to activate.   ro  r}  r   ra  zCould not create Fz	curator (z default key(s))u.     ✓ Seeded curator defaults in config.yaml: zauxiliary.curator (u8     ✓ Seeded auxiliary.curator defaults in config.yaml:    r  r  r  u   model_catalog.ttl_hours 24→1uB     ✓ Lowered model_catalog.ttl_hours to 1 (hourly picker refresh)r   )r  ru  
write_modeapprover  u   .write_mode → write_approval=u:     ✓ Renamed write_mode → write_approval (boolean gate)zConfig version:     → r  u0   
⚠️  Missing required environment variables:u      • r2  z
Let's configure them now:
z  Get your key at: rM  rK  r3  r_  u     ✓ Saved zSkipped z - some features may not workc                     h | ]
}|d          S )r   r   r  vs     r#   	<setcomp>z!migrate_config.<locals>.<setcomp>  s    555Aai555rY   c                 R    g | ]#}|d          v|                     d          !|$S )r   r5  r   )r  r  required_namess     r#   
<listcomp>z"migrate_config.<locals>.<listcomp>  sC       V9N**1553D3D* 	
***rY   c                 \    g | ])}t          |          s|t          v |t          |         f*S r   )r  r  )r  r   s     r#   r  z"migrate_config.<locals>.<listcomp>  sK     
 
 
 &&
 ,03D+D+D $T*++D+D+DrY   
  z$ new optional key(s) in this update:u       • u    — z  Configure new keys? [y/N]: n>   yr   z (Enter to skip): z1  Set later with: hermes config set <key> <value>r3   r4  u     ✓ Added  = r  z! skill setting(s) not configured:r  r
  z (from skill: z#  Configure skill settings? [y/N]: )r  zskills.configz (default: u    — skill 'r  z' may ask for it later)9sanitize_env_filer!  r   r2  r  r   r  r  r  r   r  save_configr   r   r   save_env_valuer   r   r  r  hostnamer  popr  read_raw_config
setdefaultr  r   r*   r  r   r   r  iterdirr   r0  r/  r0  r   lenr   r  copydeepcopyr  r
   inputrangeupdater/  EOFErrorKeyboardInterruptr  r  r  r  r  )_r\  r]  resultsfixescurrent_ver
latest_verr  r  old_enabledold_modeold_tz
tz_display	old_tokencustom_listr  migrated_countr  old_nameold_urlr3   r  r  base_keysuffix	new_entryepdead_varold_valr   raw_sttlegacy_modelr  rp  _local_models	raw_local	local_cfgraw_providerprovider_cfgold_overridesrE  platrZ  migratedcomps_model
s_provider
s_base_urlmigrated_keysauxaux_compplugins_cfgr  disabled_setgrandfathereduser_plugins_dirchildmanifest_file_mfmanifestr   curator_dirr  touched_curator_defaultsraw_curatoradded_curatorkr  _aux_curator_defaultsraw_auxraw_aux_curator	added_auxraw_mc	subsystemsuboldold_normmissing_envr  r9  missing_optionalnew_var_namesvernew_and_unsetr5  answermissing_configr  r4  missing_skill_config
skill_namer  default_hintr  r  s_                                                                                                 @r#   migrate_configr    s    CCG!## 	Q 	QOuOOOPPP    344K Q**Y++'4(( 	G'))'(>??K$%@AAH V{00226JJJ+0('../lmmmm Vhnn...@@+3>>+;+;('../{HXHX/{/{/{||||+0('../TUUU 'F9 b`goF^``aaa QV##Y0"55F W&,,.. W%+\\^^z"'../b6<<>>/b/b/bcccc%'z"'../UVVV L#J/C3C
JjJJKKK Q	%&788I V0"555 VTUUU 	 	 	D	 Rjj!344k4(( :	E[ :	E#ZZR88Nnd33 $!#N$ *$ *$!%..  99VR00))J33iuyy7K7KiuyyY^`bOcOcigi  nn&&,,..66sC@@HHbQQYYZ]_abbckk++dC00C ckkiinn ;;999999!)'!2!2%<*EEc3OO$ ; ; ;:.::; '^++%0000CaKF ^++ F!$  	 $ 0MM&$///==++/PPPMM)T222&/s#!#!!&4{#

-t444F### EeNeeefff#N$7$7$9$9::N?;K;KL E E+C0CCCub0A0ACCDDDD R5 	 	H'11 y"8R000  ywxwwwxxx    R ''%$$gt$$ %	VG););"7+L{{:w77H ]]F**UB''CGGGT""" 555! ! !  =00 !(GR 8 8I%i66 :':R:R$'NN7B$?$?	-9	'*  '{{8R88!,55 99T9T#&>>(B#?#?L,8L)F5M VTUUU R ""**Y++'4(( 	G'w6648G01 'F9N#**+^___ MKLLL R ""**Y++'4(( 	G$=>>mT** 	h} 	hK44Ii.. 	+1133 < <
dy((&(IdO")D/997;IdOO4#,GK  'F9 c99$R$RM<O<O<Q<Q$R$R$RRRaW_aabbbN#**+fggg R ""zz-,,dD!! 	Qhh55G"4d;;J"4d;;JM =3w<<--// =''R88>>-<<||G,, =(+G(:(:(<(<HW%!(();'););<<< Cc*oo3355\II''R88>>-<<||J// C8<<
3K3Kv3U3U+.z??+@+@+B+BHZ(!(()AZ)A)ABBB Cc*oo3355 C''R88>>-<<||J// C+.z??+@+@+B+BHZ(!(()AZ)A)ABBB Q 3z7MQ[Qg(,}%F### Q$ Q{aeajajkxayay{{||||OPPP R ""jj+++t,, 	KK''"z266<"Hh-- x==L (*M##2#4#4y#@ #**,, 3!'(8(@(@(B(B!C!C 3 3$||~~ %$(-(=,3355 A,1L,@M,3355 %$*!%mg!F!F!F E#+/>#+>+>+D"E E E E E E E E E E E E E E E( * * *')HHH*'||F33Auz<//$%,,T2222 # # # "# &3K	" +F9N#**Zs=7I7IZZZ    
  	X}--X X X   
 J  0 R	O)++f4y@KdT:::: 	O 	O 	OJ&&'M;'M'M!'M'MNNNNNNNN	O !"" +..y"==jj+++t,, 	K#%%++-- 	( 	(DAq##!%q!1!1A$$Q''' 	 +F9G {B//33IrBB 	 **[))'4(( 	G!++i00/400 	! O!	)//11 	$ 	$DAq''%)]1%5%5"  ### 	!0GI")F;G 	 '..DM 2 2DDD    699]336 6    '..J#i..JJJ    299Y//2 2   R ""O,,fd## 	\

;(?(?2(E(E"#F;&,F?#N#**+KLLL \Z[[[ R ""- 	 	I**Y''Cc4(( L,C,C'',''C.8c.B.BKsyy{{((***H%-%:C ! #F9GN#**TTSAQ=RTT     	T TRSSSZ???:??@@@ 'T:::K A5 AABBB 	A 	AC?CK??3}+=??@@@@ { -... 	 	Cwwu~~ :8CJ88999wwz"" >,-C#h--C-C-CDD43x=44455;;== bs6{E222$++CK8882S[223333
#**+`c&k+`+`+`aaaGGGG ,%@@@9DO555555#%%N   #   EEM[1_j1n55 ? ?044S"==>>>> $K $KU $K
 
}--
 
 

  	KQ]++QQQRRR+ K K
dIIIDHH]B,G,GIIJJJJGGG>??EEGGMMOO/0    %%"/  JD$xx DB488M4#@#@BBCCCADKAABBBBB488M4#@#@BBCCCxx
++ a 4M(D!9!9MMM! ! !&&W488Hd+C+C&W&W&W X X ^ ^ ` ` 5&tU333,33D9993T33444GGGG!$ IJJJ /00N # 	8 	8E,CI&GW---N#**3/// 86S66W66777 %/ !F	z	!	!$. !F 9:: $G $GE $GQS-..QQQRRR' 	_ 	_C)44J]SZ]]c-.@]]PZ]]]^^^^	@AAGGIIOOQQFF+, 	 	 	FFF	 \!!GGG ]]F6AAAAAAA 6 6 6&5###6+  '')R00;BJ7W7777B3x=B,BBBCCIIKK ) )LLE %8"G"G3u:"G"GKU;;;N+223u:>>>?U????@@@@J'..h3u:hh3777C;P;Phhh   EFFFNs   %/ 
<<=2J0 0
J=<J=.Q11RR26W))
W65W6Bv*  u 1uu u	u u	u v*  u/,v* .u//:v* *v98v9=+y) )
z3!zzT3AU UAU%U$AU%`3A`? `?AaaAaa:Ab bAbbAboverridec                    |                                  }|                                D ]X\  }}||v rJt          ||         t                    r/t          |t                    rt	          ||         |          ||<   S|||<   Y|S )a  Recursively merge *override* into *base*, preserving nested defaults.

    Keys in *override* take precedence. If both values are dicts the merge
    recurses, so a user who overrides only ``tts.elevenlabs.voice_id`` will
    keep the default ``tts.elevenlabs.model_id`` intact.
    )r  r  r  r  _deep_merge)rq  r  resultr3   r9  s        r#   r  r  m  s     YY[[Fnn&&    
U6MM6#;-- 5$''  &fSk599F3KKF3KKMrY   c                    t          | t                    rt          j        dd |           S t          | t                    rd |                                 D             S t          | t                    rd | D             S | S )a  Recursively expand ``${VAR}`` references in config values.

    Only string values are processed; dict keys, numbers, booleans, and
    None are left untouched.  Unresolved references (variable not in
    ``os.environ``) are kept verbatim so callers can detect them.
    z\${([^}]+)}c                     t           j                            |                     d          |                     d                    S )Nr  r   )r   r.  r   group)r  s    r#   <lambda>z"_expand_env_vars.<locals>.<lambda>  s*    bjnnQWWQZZ<< rY   c                 4    i | ]\  }}|t          |          S r   _expand_env_vars)r  r  r  s      r#   r  z$_expand_env_vars.<locals>.<dictcomp>  s'    ???41a#A&&???rY   c                 ,    g | ]}t          |          S r   r  )r  items     r#   r  z$_expand_env_vars.<locals>.<listcomp>  s!    7774 &&777rY   )r  r*   rer  r  r  r   )objs    r#   r	  r	    s     #s 
v<<
 
 	

 #t @??399;;????#t 87737777JrY   c                     t          | t                    sdS i }| D ]V}t          |t                    r(t          |                    d          t                    s dS |d         }||v r dS |||<   W|S )zHReturn a name-indexed dict only when all items have unique string names.Nr   )r  r   r  r   r*   )r  indexedr  r   s       r#   _items_by_unique_namer    s    eT"" tG  $%% 	Z8H8H#-N-N 	44F|7??44NrY   c                    t          | t                    rft          t                    rQt          j        d          r<| k    rS t          t                    r| k    rS t	                    | k    rS | S t          | t
                    r6t          t
                    r!fd|                                 D             S t          | t                    rqt          t                    r\t          |           }t                    t                    |fd| D             S fdt          |           D             S | S )a  Restore raw ``${VAR}`` templates when a value is otherwise unchanged.

    ``load_config()`` expands env refs for runtime use. When a caller later
    persists that config after modifying some unrelated setting, keep the
    original on-disk template instead of writing the expanded plaintext
    secret back to ``config.yaml``.

    Prefer preserving the raw template when ``current`` still matches either
    the value previously returned by ``load_config()`` for this config path or
    the current environment expansion of ``raw``. This handles env-var
    rotation between load and save while still treating mixed literal/template
    string edits as caller-owned once their rendered value diverges.
    z	\${[^}]+}c                     i | ]T\  }}|t          |                    |          t          t                    r                    |          nd           US N)_preserve_env_ref_templatesr   r  r  )r  r3   r9  loaded_expandedr   s      r#   r  z/_preserve_env_ref_templates.<locals>.<dictcomp>  sr     
 
 
 U ,,6,M,MW##C(((SW 
 
 
rY   Nc                     g | ]c}t          |                    |                    d                     (                    |                    d                     nd          dS )r   N)r  r   )r  r  loaded_by_nameraw_by_names     r#   r  z/_preserve_env_ref_templates.<locals>.<listcomp>  s{         ,OODHHV$4$455<J<VN&&txx'7'7888\`   rY   c           
          g | ]a\  }}t          ||t                    k     r|         nd t          t                    r|t                    k     r|         nd           bS r  )r  r  r  r   )r  indexr  r  r   s      r#   r  z/_preserve_env_ref_templates.<locals>.<listcomp>  s     	
 	
 	
 t (#c#hh..E

Dot449>_AUAU9U9U  && 	
 	
 	
rY   )
r  r*   r  searchr	  r  r  r   r  rA  )r  r   r  current_by_namer  r  s    `` @@r#   r  r    s    '3 JsC$8$8 RY|UX=Y=Y c>>Jos++ 	?0J0JJC  G++J'4   
ZT%:%: 

 
 
 
 
 &mmoo
 
 
 	
 '4   
ZT%:%: 

 088+C00.??&;+B     $   	
 	
 	
 	
 	
  )11	
 	
 	
 		
 NrY   c                 j    t           fddD                       }|s S t                                           d          }t          |t                    s|rd|ini }| d<   dD ]I}                     |          }|r|                    |          s|||<                        |d           J S )u  Move stale root-level provider/base_url/context_length into model section.

    Some users (or older code) placed ``provider:``, ``base_url:``, or
    ``context_length:`` at the config root instead of inside ``model:``.
    These root-level keys are only used as a fallback when the corresponding
    ``model.*`` key is empty — they never override an existing value.
    After migration the root-level keys are removed so they can't cause
    confusion on subsequent loads.
    c              3   B   K   | ]}                     |          V  d S r  r  )r  r  r  s     r#   r  z-_normalize_root_model_keys.<locals>.<genexpr>  s-      UUQ6::a==UUUUUUrY   )r  r  r  r  r4  N)anyr  r   r  r  )r  has_rootr  r3   root_vals   `    r#   _normalize_root_model_keysr"    s     UUUU*TUUUUUH &\\FJJwEeT""  &+3E""w9  ::c?? 	"EIIcNN 	"!E#J

3MrY   c                     t          |           } t          |                     d          pi           }d| v rd|vr| d         |d<   d|vrt          d         d         |d<   || d<   |                     dd           | S )z;Normalize legacy root-level max_turns into agent.max_turns.r  r  N)r  r   r  r  )r  agent_configs     r#   _normalize_max_turns_configr%    s    &\\F

7++1r22LfL!@!@$*;$7[!,&&$27$;K$H[!"F7O
JJ{D!!!MrY   )r4  cfgr  c                    t          | t                    s|S | }|D ]+}t          |t                    s|c S ||vr|c S ||         },|S )u%  Traverse nested dict keys safely, returning ``default`` on any miss.

    Canonical helper for the ``cfg.get("X", {}).get("Y", default)`` pattern
    that appears 50+ times across the codebase. Handles three common gotchas
    in one place:

      1. Missing intermediate keys (returns ``default``, no KeyError).
      2. An intermediate value that's not a dict (e.g. a user wrote a string
         where a section was expected). Returns ``default`` instead of
         AttributeError on ``.get()``.
      3. ``cfg is None`` (callers sometimes pass ``load_config() or None``).

    Named ``cfg_get`` rather than ``cfg_path`` to avoid shadowing the
    ubiquitous ``cfg_path = _hermes_home / "config.yaml"`` local variable
    that appears in gateway/run.py, cron/scheduler.py, main.py, etc.

    Explicit ``None`` values are returned as-is (matches ``dict.get(key,
    default)`` semantics — ``default`` is only returned when the key is
    *absent*, not when it's present but set to ``None``).

    Examples:
        >>> cfg_get({"agent": {"reasoning_effort": "high"}}, "agent", "reasoning_effort")
        'high'
        >>> cfg_get({}, "agent", "reasoning_effort", default="medium")
        'medium'
        >>> cfg_get({"agent": "oops_a_string"}, "agent", "reasoning_effort", default="low")
        'low'
        >>> cfg_get(None, "anything", default=42)
        42
        >>> cfg_get({"a": {"b": None}}, "a", "b", default="def")  # explicit None preserved
        >>> cfg_get({"a": {"b": False}}, "a", "b", default=True)  # falsy values preserved
        False
    )r  r  )r&  r4  r  noder3   s        r#   cfg_getr)    sm    D c4   D  $%% 	NNNd??NNNCyKrY   c                  $   t           5  	 t                      } |                                 }|j        |j        f}n%# t
          t          f$ r i cY cddd           S w xY wt          |           }t          	                    |          }|4|dd         |k    r&t          j        |d                   cddd           S 	 t          | d          5 }t          j        |          pi }ddd           n# 1 swxY w Y   n5# t          $ r(}t!          | |           i cY d}~cddd           S d}~ww xY wt#          |t$                    si }|d         |d         t          j        |          ft          |<   |cddd           S # 1 swxY w Y   dS )u  Read ~/.hermes/config.yaml as-is, without merging defaults or migrating.

    Returns the raw YAML dict, or ``{}`` if the file doesn't exist or can't
    be parsed.  Use this for lightweight config reads where you just need a
    single value and don't want the overhead of ``load_config()``'s deep-merge
    + migration pipeline.

    Cached on the config file's (mtime_ns, size) — same strategy as
    ``load_config()``. Returns a deepcopy on every call since some callers
    mutate the result before passing to ``save_config()``.
    Nr  r   r   r   r  )_CONFIG_LOCKr>  r   r+   r   r3  r   r*   r\   r   r  r  r0  r/  r0  r   r5   r  r  )r   r   	cache_keypath_keycachedr6  datar  s           r#   r  r  A  s    
  	)++K!!##B4II!7+ 	 	 	II       
	 {##"&&x00&!*	"9"9=++       	kG444 /~a((.B/ / / / / / / / / / / / / / / 	 	 	&{A666IIIII%        	 $%% 	D'0|Yq\4=QUCVCV&W(#/                 s   F0;FAFAAF D C4(D 4C8	8D ;C8	<D ?F 
D2
D-D2F-D22AFF	F	c                  "    t          d          S )u  Load configuration from ~/.hermes/config.yaml.

    Cached on the config file's (mtime_ns, size). Returns a deepcopy of
    the cached value when unchanged, since most call sites mutate the
    result (e.g. ``cfg["model"]["default"] = ...`` before ``save_config``).
    The cache is keyed on ``str(config_path)`` so profile switches
    (which change ``HERMES_HOME`` and therefore ``get_config_path()``)
    don't collide.

    Read-only callers should use ``load_config_readonly()`` to skip the
    defensive deepcopy — that path matters in agent-loop hot spots like
    ``get_provider_request_timeout`` which is called once per API turn.
    Twant_deepcopy_load_config_implr   rY   r#   r  r  g  s     40000rY   c                  "    t          d          S )u  Fast-path variant of ``load_config()`` for callers that ONLY READ.

    Returns the cached config dict directly without the defensive deepcopy
    that ``load_config()`` applies. **Mutating the returned dict (or any
    nested structure) corrupts the in-process cache for every subsequent
    caller** — only use this when you are absolutely sure your code path
    will not write to the result. If you need to mutate or pass to
    ``save_config``, call ``load_config()`` instead.

    Why this exists: ``load_config()`` cache-hit cost is ~265us per call,
    half of which (~135us) is the defensive deepcopy. The agent loop calls
    into config reads (timeouts, thresholds, feature flags) ~20-50x per
    conversation; skipping deepcopy here removes a measurable allocation
    source and the GC pressure that comes with it.

    Note: this returns a plain ``dict`` (not ``MappingProxyType``) so
    existing ``isinstance(x, dict)`` guards downstream keep working. The
    safety guarantee is purely documented, not enforced — be careful.
    Fr1  r3  r   rY   r#   load_config_readonlyr6  x  s    ( 51111rY   rd   rS  TERMINAL_TIMEOUTlifetime_secondsTERMINAL_LIFETIME_SECONDSTERMINAL_DOCKER_IMAGETERMINAL_DOCKER_FORWARD_ENVTERMINAL_SINGULARITY_IMAGETERMINAL_MODAL_IMAGETERMINAL_DAYTONA_IMAGEssh_hostTERMINAL_SSH_HOSTssh_userTERMINAL_SSH_USERssh_portr   ssh_keyry   TERMINAL_CONTAINER_CPUTERMINAL_CONTAINER_MEMORYTERMINAL_CONTAINER_DISKTERMINAL_CONTAINER_PERSISTENTTERMINAL_DOCKER_VOLUMESTERMINAL_DOCKER_ENV&TERMINAL_DOCKER_MOUNT_CWD_TO_WORKSPACETERMINAL_DOCKER_EXTRA_ARGS TERMINAL_DOCKER_RUN_AS_HOST_USER(TERMINAL_DOCKER_PERSIST_ACROSS_PROCESSESTERMINAL_DOCKER_ORPHAN_REAPERTERMINAL_SANDBOX_DIRTERMINAL_PERSISTENT_SHELL)
r  r  r  r  r  r  docker_persist_across_processesdocker_orphan_reapersandbox_dirr  c                     t          | t          t          f          rt          j        |           S t          |           S r  )r  r   r  jsondumpsr*   )r9  s    r#   _terminal_env_valuerX    s4    %$&& !z%   u::rY   c                     d}|                      |          sdS t                              | t          |          d                   S )z;Return the env var mirrored by a ``terminal.*`` config key.z	terminal.N)r1  TERMINAL_CONFIG_ENV_MAPr   r  )r3   r
  s     r#   terminal_config_env_var_for_keyr[    sC    F>>&!! t"&&s3v;;<<'8999rY   )envr  r  r\  c                    | t           j        n| }t                      }t          |                    d          t
                    }||n|}||nt                      }t          |t
                    r|                    di           ni }t          |t
                    s|S t                                          D ]\  }	}
|	|vr
||	         }|	dk    r\t          |pd          
                                }|dv r@t          |t                    rt           j                            |          }|s|
|vrt          |          ||
<   |S )a`  Bridge ``terminal.*`` config into the env vars terminal tools read.

    ``tools.terminal_tool`` is intentionally environment-driven because it also
    runs in child processes (TUI, dashboard PTY, gateway workers).  This helper
    gives those child-process launch paths the same config bridge as classic
    CLI without importing ``cli.py`` and paying for its startup side effects.

    When the user config contains a ``terminal`` section, config.yaml is
    authoritative and overrides existing env values.  Otherwise defaults only
    backfill missing env vars so exported/.env values keep working.
    Nr  r  r   >   r  r  r(   )r   r.  r  r  r   r  r6  rZ  r  r*   r   r  
expanduserrX  )r\  r  r  target
raw_configfile_has_terminal_configshould_overrider&  rW  cfg_keyenv_varr9  raw_cwds                r#   apply_terminal_config_to_envrf    s\   " ;RZZCF ""J)*..*D*DdKK2:2B..O&&&,@,B,BC.8d.C.CK377:r***LlD)) 399;; 9 9,&&W%e%+2&&,,..G...%%% 2**511 	9gV331%88F7OMrY   r2  c                    t           5  t                       t                      }t          |          }	 |                                }|j        |j        f}n# t          $ r d }Y nw xY wt          	                    |          }|@|>|d d         |k    r0| rt          j        |d                   n|d         cd d d            S t          j        t                    }|	 t          |d          5 }t          j        |          pi }d d d            n# 1 swxY w Y   d|v r_t!          |	                    d          pi           }	|		                    d          |d         |	d<   |	|d<   |                    dd            t%          ||          }n'# t&          $ r}
t)          ||
           Y d }
~
nd }
~
ww xY wt+          t-          |                    }t/          |          }t          j        |          t0          |<   |>t          j        |          }|d         |d         |ft          |<   | s|cd d d            S nt                              |d            |cd d d            S # 1 swxY w Y   d S )Nr  r   r   r  r  r   r  )r+  r{  r>  r*   r   r+   r   r3  r[   r   r  r  r  r0  r/  r0  r  r  r  r   r5   r"  r%  r	  rZ   )r2  r   r-  r   r,  r.  r  r6  user_configagent_user_configr  r   expandedcached_copys                 r#   r4  r4    sj   	 7 7%''{##	!!##B46NBJ3OII  	 	 	III	 $''11)"7F2A2J)<S<S/<K4=+++&)7 7 7 7 7 7 7 7 ~.. ;+888 :A"&."3"3"9rK: : : : : : : : : : : : : : : +--(,[__W-E-E-K(L(L%(,,[99A9D[9Q)+6+<K(OOK666$V[99 ; ; ;*;::::::::; 00KF0S0STT
#J//26-2I2I%h/ 
 -11K,5aL)A,+Tx( ! #"_7 7 7 7 7 7 7 7\# ""8T222 o7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7s   ,I,"AI,A'$I,&A''AI,I,"F3D
FD	FD	A6FI,
F9F4/I,4F99A<I,I,,I03I0u-  
# ── Security ──────────────────────────────────────────────────────────
# Secret redaction is ON by default — strings that look like API keys,
# tokens, and passwords are masked in tool output, logs, and chat
# responses before the model or user ever sees them. Set redact_secrets
# to false to disable (e.g. when developing the redactor itself).
# tirith pre-exec scanning is enabled by default when the tirith binary
# is available. Configure via security.tirith_* keys or env vars
# (TIRITH_ENABLED, TIRITH_BIN, TIRITH_TIMEOUT, TIRITH_FAIL_OPEN).
#
# security:
#   redact_secrets: true
#   tirith_enabled: true
#   tirith_path: "tirith"
#   tirith_timeout: 5
#   tirith_fail_open: true
u@  
# ── Fallback Model ────────────────────────────────────────────────────
# Automatic provider failover when primary is unavailable.
# Uncomment and configure to enable. Triggers on rate limits (429),
# overload (529), service errors (503), or connection failures.
#
# Supported providers:
#   openrouter   (OPENROUTER_API_KEY)  — routes to any model
#   openai-codex (OAuth — hermes auth) — OpenAI Codex
#   nous         (OAuth — hermes auth) — Nous Portal
#   zai          (ZAI_API_KEY)         — Z.AI / GLM
#   kimi-coding  (KIMI_API_KEY)        — Kimi / Moonshot
#   kimi-coding-cn (KIMI_CN_API_KEY)   — Kimi / Moonshot (China)
#   minimax      (MINIMAX_API_KEY)     — MiniMax
#   minimax-cn   (MINIMAX_CN_API_KEY)  — MiniMax (China)
#   bedrock      (AWS IAM / boto3)     — AWS Bedrock (Converse API)
#
# For custom OpenAI-compatible endpoints, add base_url and key_env.
#
# fallback_model:
#   provider: openrouter
#   model: anthropic/claude-sonnet-4
u  
# ── Security ──────────────────────────────────────────────────────────
# Secret redaction is ON by default. Set to false to pass tool output,
# logs, and chat responses through unmodified (e.g. for redactor dev).
#
# security:
#   redact_secrets: true

# ── Fallback Model ────────────────────────────────────────────────────
# Automatic provider failover when primary is unavailable.
# Uncomment and configure to enable. Triggers on rate limits (429),
# overload (529), service errors (503), or connection failures.
#
# Supported providers:
#   openrouter   (OPENROUTER_API_KEY)  — routes to any model
#   openai-codex (OAuth — hermes auth) — OpenAI Codex
#   nous         (OAuth — hermes auth) — Nous Portal
#   zai          (ZAI_API_KEY)         — Z.AI / GLM
#   kimi-coding  (KIMI_API_KEY)        — Kimi / Moonshot
#   kimi-coding-cn (KIMI_CN_API_KEY)   — Kimi / Moonshot (China)
#   minimax      (MINIMAX_API_KEY)     — MiniMax
#   minimax-cn   (MINIMAX_CN_API_KEY)  — MiniMax (China)
#   bedrock      (AWS IAM / boto3)     — AWS Bedrock (Converse API)
#
# For custom OpenAI-compatible endpoints, add base_url and key_env.
#
# fallback_model:
#   provider: openrouter
#   model: anthropic/claude-sonnet-4
c           
         t           5  t                      rt          d           	 ddd           dS ddlm} t                       t                      }t          t          |                     }|}t          t          t                                          }|r6t          ||t                              t          |                              }g }|                    di           }|r|                    d          |                    t                     |                    di           }d}	t!          |t"                    rt%          d	 |D                       }	nLt!          |t&                    r7t)          |                    d
          o|                    d                    }	|	s|                    t*                      ||||rd                    |          nd           t/          |           t1          j        |          t          t          |          <   ddd           dS # 1 swxY w Y   dS )z,Save configuration to ~/.hermes/config.yaml.zsave configurationNr   atomic_yaml_writer  r  r3  Fc              3      K   | ]C}t          |t                    o)|                    d           o|                    d          V  DdS )r  r  N)r  r  r   )r  r  s     r#   r  zsave_config.<locals>.<genexpr>  sN      gg_`jD11ZaeeJ6G6GZAEERYNNggggggrY   r  r  r   )extra_content)r+  r   r"  utilsrn  r{  r>  r"  r%  r  r  rZ   r   r*   r  _SECURITY_COMMENTr  r   r  r  r	  _FALLBACK_COMMENTr   rf  r  r  )
r  rn  r   current_normalizedr   raw_existingr  secrH  fb_is_valids
             r#   r  r  m  s   	 '\ '\<< 	.///'\ '\ '\ '\ '\ '\ '\ '\ 	,+++++%''78STZ8[8[\\'
12MoN_N_2`2`aa 	4-11#k2B2BCC J nnZ,, 	,cgg.//7LL*+++^^,b11b$ 	GggdfgggggKKD!! 	Grvvj11EbffWooFFK 	,LL*+++,1;"''%...t	
 	
 	
 	

 	[!!!:>-HZ:[:[%c+&6&67O'\ '\ '\ '\ '\ '\ '\ '\ '\ '\ '\ '\ '\ '\ '\ '\ '\ '\s   H6G5H66H:=H:c                  Z   t                      } 	 |                                 j        }|                                 j        }t	          |           ||f}n/# t
          $ r t	          |           ddf}Y nt          $ r d}Y nw xY w|&t          t          \  }}||k    rt          |          S i }| 	                                rddd}t          | fi |5 }|                                }	ddd           n# 1 swxY w Y   t          |	          }
|
D ]}|                                }|rn|                    d          sYd|v rU|                    d          \  }}}|                                                    d          ||                                <   ||t          |          fa|S )u  Load environment variables from ~/.hermes/.env.

    Sanitizes lines before parsing so that corrupted files (e.g.
    concatenated KEY=VALUE pairs on a single line) are handled
    gracefully instead of producing mangled values such as duplicated
    bot tokens.  See #8908.

    The parsed dict is memoised keyed on the .env file mtime, because
    ``get_env_value()`` is called dozens-to-hundreds of times per
    interactive menu render (`hermes tools`, `hermes setup`, status
    panels). Sanitisation is O(lines × known-keys), so re-parsing the
    same file on every call was burning ~300ms of CPU per `hermes tools`
    menu paint on top of the OAuth-refresh slowness. The mtime check
    invalidates the cache when the user edits .env mid-process.
    N	utf-8-sigr   r   errorsr(  r'  z"')r@  r   st_mtimer   r*   r3  r   
_env_cacher  r   r0  	readlines_sanitize_env_linesr   r1  r2  )env_pathmtimesizer,  
cached_keycached_varsenv_varsopen_kwr6  	raw_linesrM  r7  r3   r8  r9  s                  r#   load_envr    s   " ~~H(}}&]]E40		 0 0 0]]D$/			   			 !7",
K""$$$!H C  +i@@(&&g&& 	&!I	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& $I.. 	C 	CD::<<D CDOOC00 CSD[[ $s 3 3Q(-(;(;E(B(B%h0
Os*   AA B3B BC55C9<C9r}  c                  
    da dS )a  Clear the load_env() process-level memo.

    Writers that mutate .env (set_env_value, save_env, etc.) call this
    to guarantee the next load_env() sees their change even on
    filesystems with coarse mtime resolution. Reads invalidate naturally
    via the mtime/size check.
    N)r}  r   rY   r#   invalidate_env_cacher    s     JJJrY   rM  c           	         t          t                                                    t          z  }g }| D ]}|                    d          }|                                }|r|                    d          r|                    |dz              \g |D ]u}|dz   }|                    |          }|dk    rS                    ||t          |          z   f           |                    ||t          |          z             }|dk    Svt          fdD                       }	t          |	          dk    r}t          |	          D ]k\  }
}|
dz   t          |	          k     r|	|
dz            nt          |          }|||                                         }|r|                    |dz              l|                    |dz              |S )a  Fix corrupted .env lines before reading or writing.

    Handles two known corruption patterns:
    1. Concatenated KEY=VALUE pairs on a single line (missing newline between
       entries, e.g. ``ANTHROPIC_API_KEY=sk-...OPENAI_BASE_URL=https://...``).
    2. Stale ``KEY=***`` placeholder entries left by incomplete setup runs.

    Uses a known-keys set (OPTIONAL_ENV_VARS + _EXTRA_ENV_KEYS) so we only
    split on real Hermes env var names, avoiding false positives from values
    that happen to contain uppercase text with ``=``.
    z
r(  r)   r'  r   c                 V    h | ]#\  t          fd D                       !$S )c              3   J   K   | ]\  }}|k    o|k    o	||ffk    V  d S r  r   )r  s2e2r  ss      r#   r  z0_sanitize_env_lines.<locals>.<setcomp>.<genexpr>  sZ        B a:B!G:RQF(:     rY   )r  )r  r  r  match_rangess    @@r#   r  z&_sanitize_env_lines.<locals>.<setcomp>  sj     "
 "
 "
!Q     *    "
"
 "
 "
rY   r  )r  r  r  _EXTRA_ENV_KEYSr  r   r1  r  findr  r  rA  )rM  
known_keys	sanitizedr7  r   strippedkey_nameneedler  split_positionsrG  posendr  r  s                 @r#   r  r    s    &++--..@JI &. &.kk&!!99;;  	8..s33 	S4Z((( /1" 	? 	?H^F--''C((##S#F*;$<===mmFC#f++,=>> (( ! "
 "
 "
 "
&"
 "
 "
   !###O44 2 2301AO8L8L0L0Loa!e,,RUV^R_R_C(..00 2$$TD[111	2 X_----rY   c                  $   t                      } |                                 sdS ddd}ddi}t          | fi |5 }|                                }ddd           n# 1 swxY w Y   t	          |          }||k    rdS t          t          |          t          |          z
            }|dk    rVt          d t          ||          D                       }|t          t          |          t          |          z
            z  }t          j
        t          | j                  d	d
          \  }}	 t          j        |dfi |5 }|                    |           |                                 t          j        |                                           ddd           n# 1 swxY w Y   t'          ||            n5# t(          $ r( 	 t          j        |           n# t,          $ r Y nw xY w w xY wt/          |            t1                       |S )zRead, sanitize, and rewrite ~/.hermes/.env in place.

    Returns the number of lines that were fixed (concatenation splits +
    placeholder removals).  Returns 0 when no changes are needed.
    r   ry  r   rz  r   r   Nc              3   ,   K   | ]\  }}||k    d V  dS )r  Nr   )r  abs      r#   r  z$sanitize_env_file.<locals>.<genexpr>;  s*      KK$!QAFFAFFFFKKrY   .tmp.env_dirr  r
  w)r@  r   r0  r~  r  absr  sumziptempfilemkstempr*   r   r   fdopen
writelinesr2   fsyncfilenor<  BaseExceptionunlinkr   rf  r  )	r  read_kwwrite_kwr6  original_linesr  r  fdtmp_paths	            r#   r  r  "  s    ~~H?? q&)<<GG$H	h	"	"'	"	" 'a' ' ' ' ' ' ' ' ' ' ' ' ' ' ' $N33IN""q I^!4!4455EzzKK#ni"@"@KKKKKS^^c.&9&99:::#HO(<(<VT[\\\LBYr3++(++ 	!qLL###GGIIIHQXXZZ   	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	x****   	Ih 	 	 	D	 Lsf   AA #A 3F> AF"F> "F&&F> )F&*F> >
G0	GG0
G+(G0*G++G0c                    	 |                     d           |S # t          $ r Y nw xY wg }t          |          D ]E\  }}t          |          dk    r-|                    d| d|dt          |          dd           F|                     dd	                              d          }t          d
|  dd                    d |dd         D                       z   t          |          dk    rdndz   dz   t          j
                   |S )u1  Warn and strip non-ASCII characters from credential values.

    API keys and tokens must be pure ASCII — they are sent as HTTP header
    values which httpx/httpcore encode as ASCII.  Non-ASCII characters
    (commonly introduced by copy-pasting from rich-text editors or PDFs
    that substitute lookalike Unicode glyphs for ASCII letters) cause
    ``UnicodeEncodeError: 'ascii' codec can't encode character`` at
    request time.

    Returns the sanitized (ASCII-only) value.  Prints a warning if any
    non-ASCII characters were found and removed.
    ascii   z  position r'   z (U+04Xr?  ignore)r{  z
  Warning: z contains non-ASCII characters that will break API requests.
  This usually happens when copy-pasting from a PDF, rich-text editor,
  or web page that substitutes lookalike Unicode glyphs for ASCII letters.

r)   c              3       K   | ]	}d | V  
dS )rK  Nr   )r  r7  s     r#   r  z._check_non_ascii_credential.<locals>.<genexpr>o  s(      ::DKKK::::::rY   Nr  z
  ... and morer   z

  The non-ASCII characters have been stripped automatically.
  If authentication fails, re-copy the key from the provider's dashboard.
r  )encodeUnicodeEncodeErrorrA  ordr  decoder!  r   r  r/   r0   )r3   r9  	bad_charsrG  chr  s         r#   _check_non_ascii_credentialr  P  sc   W    I5!! J J2r77S==H1HHHH#b''HHHHIIIWX66==gFFI		 	 	 	 ))::IbqbM:::
:
:		;
 "%Y!!3!3	=W	W Z
 
 
 
 s    
&&c                    t                      rt          d|             dS t                              |           st	          d|           t          |            |                    dd                              dd          }t          | |          }t                       t                      }ddd	}d
di}g }|
                                rHt          |fi |5 }|                                }ddd           n# 1 swxY w Y   t          |          }d}t          |          D ]>\  }}	|	                                                    |  d          r|  d| d||<   d} n?|sH|r+|d                             d          s|dxx         dz  cc<   |                    |  d| d           t'          j        t+          |j                  dd          \  }
}d}|
                                r=	 t/          j        |                                j                  }n# t4          $ r Y nw xY w	 t7          j        |
dfi |5 }|                    |           |                                 t7          j        |                                            ddd           n# 1 swxY w Y   tC          ||           |'	 t7          j"        ||           n# t4          $ r Y nw xY wtG          |           n5# tH          $ r( 	 t7          j%        |           n# t4          $ r Y nw xY w w xY w|t6          j&        | <   tO                       dS )z)Save or update a value in ~/.hermes/.env.zset N#Invalid environment variable name: r)   r   ry  r   rz  r   r   Fr'  TrP  r  r  r  r  )(r   r"  _ENV_VAR_NAME_REmatchrW   rX   r   r  r{  r@  r   r0  r~  r  rA  r   r1  endswithr  r  r  r*   r   r   S_IMODEst_moder   r   r  r  r2   r  r  r<  rX  rf  r  r  r.  r  )r3   r9  r  r  r  rM  r6  foundrG  r7  r  r  original_modes                r#   r  r  x  s1   || lSll###!!#&& HFsFFGGGs###MM$##++D"55E'U33E~~H ')<<GG$HE +(&&g&& 	"!KKMME	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" $E** EU##  4::<<""c999-- 	)))))E!HEE	
  ) 	r++D11 	"IIIIII''e'''(((#HO(<(<VT[\\\LBM 	 L)@AAMM 	 	 	D	Yr3++(++ 	!qLLGGIIIHQXXZZ   	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	x*** $=1111    """   	Ih 	 	 	D	 BJsOs    DDD8+H$ $
H10H15K8 AJ$K8 $J((K8 +J(,K8 K K8 
K%"K8 $K%%K8 8
L*LL*
L%"L*$L%%L*c                 T    t                      rt          d             dS t                                         st	          d           t                      }|                                s"t          j        	                     d           dS ddd}dd	i}t          |fi |5 }|                                }ddd           n# 1 swxY w Y   t          |          } fd
|D             }t          |          t          |          k     }|rdt          j        t!          |j                  dd          \  }}	d}
	 t%          j        |                                j                  }
n# t*          $ r Y nw xY w	 t          j        |dfi |5 }|                    |           |                                 t          j        |                                           ddd           n# 1 swxY w Y   t7          |	|           |
'	 t          j        ||
           n# t*          $ r Y nw xY wt;          |           n5# t<          $ r( 	 t          j        |	           n# t*          $ r Y nw xY w w xY wt          j        	                     d           tA                       |S )zzRemove a key from ~/.hermes/.env and os.environ.

    Returns True if the key was found and removed, False otherwise.
    zremove Fr  Nry  r   rz  r   r   c                 h    g | ].}|                                                      d           ,|/S )r'  )r   r1  )r  r7  r3   s     r#   r  z$remove_env_value.<locals>.<listcomp>  s:    RRR$tzz||/F/F#yyy/Q/QRRRRrY   r  r  r  r  )!r   r"  r  r  rW   r@  r   r   r.  r  r0  r~  r  r  r  r  r*   r   r   r  r  r   r  r  r2   r  r  r<  rX  rf  r  r  r  )r3   r  r  r  r6  rM  	new_linesr  r  r  r  s   `          r#   remove_env_valuer    sV   
 || ooo&&&u!!#&& HFsFFGGG~~H?? 

sD!!!u&)<<GG$H	h	"	"'	"	" a              &&ERRRR%RRRI	NNSZZ'E 'C,@,@X_```H	 L)@AAMM 	 	 	D		2s//h// %1Y'''			$$$% % % % % % % % % % % % % % % 8X... (HX}5555   D X&&& 	 	 		(####   	 JNN3Ls   *CCC+E1 1
E>=E>I AG1%I 1G55I 8G59I H% $I %
H2/I 1H22I 
I7I%$I7%
I2/I71I22I7c                 H    |pt           } |d|             |dd           dS )zBPersist an Anthropic OAuth/setup token and clear the API-key slot.rm   r|   r   Nr  r9  save_fnwriters      r#   save_anthropic_oauth_tokenr    s8    &F
Fe$$$
F#####rY   c                 H    | pt           } |dd            |dd           dS )zHUse Claude Code's own credential files instead of persisting env tokens.rm   r   r|   Nr  )r  r  s     r#   %use_anthropic_claude_code_credentialsr    s8    &F
Fb!!!
F#####rY   c                 H    |pt           } |d|             |dd           dS )zBPersist an Anthropic API key and clear the OAuth/setup-token slot.r|   rm   r   Nr  r  s      r#   save_anthropic_api_keyr    s8    &F
F&&&
Fb!!!!!rY   c                 .    t          | |           d| ddS )NTF)success	stored_as	validatedr  )r3   r9  s     r#   save_env_value_securer    s*    3  rY   c                  v   t                      } t          t                                                    t          z  }d}|                                 D ]<\  }}t          j                            |          |k    r|t          j        |<   |dz  }=|D ]&}|| vr |t          j        v rt          j        |= |dz  }'|S )u&  Re-read ~/.hermes/.env into os.environ. Returns count of vars updated.

    Adds/updates vars that changed and removes vars that were deleted from
    the .env file (but only vars known to Hermes — OPTIONAL_ENV_VARS and
    _EXTRA_ENV_KEYS — to avoid clobbering unrelated environment).
    r   r  )	r  r  r  r  r  r  r   r.  r   )r  r  countr3   r9  s        r#   
reload_envr    s     zzH&++--..@JEnn&&  
U:>>#%''#BJsOQJE  h3"*#4#4
3QJELrY   c                     | t           j        v rt           j        |          S t                      }|                    |           S )z/Get a value from ~/.hermes/.env or environment.)r   r.  r  r   )r3   r  s     r#   r  r  4  s:     bjz# zzH<<rY   c                 X    ddl m}  || t          dt          j                            S )u   Redact an API key for display.

    Thin wrapper over :func:`agent.redact.mask_secret` — preserves the
    "(not set)" placeholder in dim color for the empty case.
    r   )mask_secret	(not set))empty)agent.redactr  r   r   DIM)r3   r  s     r#   
redact_keyr  C  s7     )(((((;s%VZ"@"@AAAArY   c                     t                      } t                       t          t          dt          j                             t          t          dt          j                             t          t          dt          j                             t                       t          t          dt          j        t          j                             t          dt                                  t          dt                                  t          dt                                  t                       t          t          dt          j        t          j                             g d	}|D ]7\  }}t          |          }t          d
|ddt          |                      8ddlm}  |            }t          d
dddt          |                      t                       t          t          dt          j        t          j                             t          d|                     dd                      |                     di                               dt          d         d                   }t          d|            	 t                                          d          }|ot!          |                                          t!          |                                          k    r+t          t          d| dt          j                             n# t&          $ r Y nw xY wt                       t          t          dt          j        t          j                             |                     di           }	t          d|	                    d          pd            t          d |	                    d!d"          rd#nd$            t          d%|	                    d&d"          rd#nd$            t)          |	                    d'i           t*                    r|	                    d'i           ni }
|
                    d(d)          }|
                    d*d)          }t          d+| d,| d-           t                       t          t          d.t          j        t          j                             |                     d/i           }t          d0|                    d1d2                      t          d3|                    d4d5                      t          d6|                    d7d8           d9           |                    d1          d:k    r(t          d;|                    d<d=                      nj|                    d1          d>k    r(t          d?|                    d@dA                      n)|                    d1          dBk    rLt          dC|                    dDd=                      t          dE          }t          dF|rdGndH            n|                    d1          dIk    rLt          dJ|                    dKd=                      t          dL          }t          dM|rdGndH            n_|                    d1          dNk    rFt          dO          }t          dP          }t          dQ|pdH            t          dR|pdH            t                       t          t          dSt          j        t          j                             |                     dTdU          }|rt          dV|            n*t          dVt          dWt          j                              t                       t          t          dXt          j        t          j                             |                     dYi           }|                    dZd[          }t          d\|rd]nd^            |r(t          d_|                    d`da          dbz  dcdd           t          de|                    dfdg          dbz  dcdh           t          di|                    djdk           dl           t          dm|                    dndo           dp           |                     dqi                               dYi           }|                    ddU          pdr}t          d|            |                    dsdt          }|r|dtk    rt          du|            |                     dqi           }|                    dvi           |                    dwi           dx}t/          dy |                                D                       }|rt                       t          t          dzt          j        t          j                             |                                D ]\  }}|                    dsdt          }|                    ddU          }|dtk    s|rId{| g}|r|                    d||            t          d
|d}d
d~                    |                      t                       t          t          dt          j        t          j                             t          d          } t          d          }!t          d| rdGnt          dt          j                              t          d|!rdGnt          dt          j                              	 ddlm}"m}#  |"            }$|$r |#|$          }%t                       t          t          dt          j        t          j                             |$D ]}&|&d         }'|%                    |'dU          }|&                    ddU          }(|rt!          |          nt          dHt          j                  })t          d
|'dd|) d
t          d|( dt          j                              n# t&          $ r Y nw xY wt                       t          t          dt          j                             t          t          dt          j                             t          t          dt          j                             t          t          dt          j                             t                       dS )zDisplay current configuration.u   ┌─────────────────────────────────────────────────────────┐u@   │              ⚕ Hermes Configuration                    │u   └─────────────────────────────────────────────────────────┘u	   ◆ Pathsz  Config:       z  Secrets:      z  Install:      u   ◆ API Keys)	)r6  
OpenRouter)r%  zOpenAI (STT/TTS))rj  Exa)rm  Parallel)r!  	Firecrawl)r+  Tavily)r"  Browserbase)rw  zBrowser Use)r$  FALrK  z<14r   r   )get_anthropic_key	Anthropicu	   ◆ Modelz  Model:        r  znot setr  r  z  Max turns:    HERMES_MAX_ITERATIONSNu9                   ⚠ .env has stale HERMES_MAX_ITERATIONS=z& (run 'hermes doctor --fix' to remove)u   ◆ Displayr  z  Personality:  r  nonez  Reasoning:    r'  Fonr}  z  Bell:         r&  r9  r:  r  r;  z  User preview: first z line(s), last z line(s)u   ◆ Terminalr  z  Backend:      r)  r  z  Working dir:  r  r(   z  Timeout:      r  r	  r  r  z  Docker image: r  r  singularityz  Image:        r  r  modalz  Modal image:  r  MODAL_TOKEN_IDz  Modal token:  
configuredr  daytonaz  Daytona image: r  DAYTONA_API_KEYz  API key:      sshr@  rB  z  SSH host:     z  SSH user:     u   ◆ Timezoner  r   z  Timezone:     ri  u   ◆ Context Compressionr  r  Tz  Enabled:      r   rd  z  Threshold:    r  r  d   z.0f%z  Target ratio: r  r  z% of threshold preservedz  Protect last: r  r  z	 messagesz  Protect first: r  r  z non-system head messagesr  z(auto)r  r  z  Provider:     r
  r  )VisionzWeb extractc              3   v   K   | ]4}|                     d d          dk    p|                     dd          V  5dS )r  r  r  r   Nr  )r  ts     r#   r  zshow_config.<locals>.<genexpr>  s\         	
j&!!V+AquuWb/A/A     rY   u    ◆ Auxiliary Models (overrides)r  r  12sr  u   ◆ Messaging Platformsr  r  z  Telegram:     znot configuredz  Discord:      )r  resolve_skill_config_valuesu   ◆ Skill Settingsr3   r  z<20s[]u   ────────────────────────────────────────────────────────────z+  hermes config edit     # Edit config filez!  hermes config set <key> <value>z+  hermes setup           # Run setup wizard)r  r!  r   r   CYANBOLDr>  r@  rB  r  r  hermes_cli.authr  r   r  r  r*   r   YELLOWr   r  r  r  r  valuesr  r  r   r  r  r  )*r  r  env_keyr   r9  r  anthropic_value_cfg_max_turns
_env_ghostr  ump	ump_firstump_lastr  modal_tokendaytona_keyr?  rA  tzr  r  	_aux_comp_smcomp_providerr  	aux_taskshas_overrideslabeltask_cfgprovmdlr  telegram_tokendiscord_tokenr  r  
skill_varsresolvedr  r3   r  display_vals*                                             r#   show_configr$  M  s   ]]F	GGG	%  D  FL  FQ  R  R  S  S  S	%RTZT_
`
`aaa	%  D  FL  FQ  R  R  S  S  S 
GGG	%V[&+
6
6777	
0_..
0
0111	
-\^^
-
-...	
1-//
1
1222 
GGG	%V[
9
9:::
 
 
D  3 3g&&14111j//112222111111''))O	
>{
>
>
>O!<!<
>
>??? 
GGG	%V[&+
6
6777	
=VZZ;;
=
=>>>ZZ,,00nW>UVa>bccN	
-^
-
-...	ZZ^^$;<<
!c*oo&;&;&=&=^ATATAZAZA\A\&\&\%9J 9 9 9    
     
GGG	%v{FK
8
8999jjB''G	
CW[[77A6
C
CDDD	
VW[[1A5%I%ITTTu
V
VWWW	
XW[[1CU%K%KVTTQV
X
XYYY5?Lbdf@g@gim5n5n
v'++,b
1
1
1tvCq))Iww|Q''H	
O9
O
OX
O
O
OPPP 
GGG	%V[
9
9:::zz*b))H	
?X\\)W==
?
?@@@	
7X\\%55
7
7888	
;X\\)R88
;
;
;<<<||I(**mn>j!k!kmmnnnn	i	 	 M	1	1{.ACx!y!y{{||||	i	 	 G	+	+lm=i!j!jllmmm#$455O!M+OOPPPP	i	 	 I	-	-o(,,@l"m"mooppp#$566O!M+OOPPPP	i	 	 E	)	) !455 !455:!8[::;;;:!8[::;;; 
GGG	%V[
9
9:::	J	#	#B	 H%%%&&&&F'7!D!DFFGGG 
GGG	%)6;
D
DEEE**]B//Kooi..G	
9g7UU4
9
9::: 
6Pd!C!Cc!IPPPPQQQj!F!F!LjjjjkkkQ1A2!F!FQQQRRRb+//2CQ"G"GbbbcccJJ{B//33M2FF	mmGR((4H&&&'''!j&99 	6]f444]44555 

;++I }}Xr22 }}]B77 I   !!##    M  
<e6V[QQRRR(00 	< 	<OE8<<
F33D,,w++Cv~~~+T++, 1LL#000:5:::		%(8(8::;;; 
GGG	%)6;
D
DEEE"#788N!"566M	
f^d\\GWY_YcAdAd
f
fggg	
e]c\\FVX^Xb@c@c
e
efffaaaaaaaa3355
 		]22:>>HGGG%,fk6;GGHHH! ] ]%j S"-- WWWb11
,1Uc%jjju[&*7U7U[3[[[k[[U;Lz;L;L;Lfj5Y5Y[[\\\\    
GGG	%
FJ
'
'(((	%=vz
J
JKKK	%3VZ
@
@AAA	%=vz
J
JKKK	GGGGGs&   BL$ $
L10L1 C8p9 9
qqc                  N   t                      rt          d           dS t                      } |                                 s&t	          t
                     t          d|             t          j        d          pt          j        d          }|s5ddl	}ddl
}|j        dk    rg d}ng d	}|D ]} |j        |          r|} n|s#t          d
           t          d|             dS t          d|  d| d           t          j        |t          |           g           dS )z"Open config file in user's editor.zedit configurationNzCreated r:   r;   r   rD  )notepadcodevimvinano)r*  r(  r)  r'  r&  z#No editor found. Config file is at:rK  zOpening  in z...)r   r"  r>  r   r  r  r!  r   r   r   r/   rH  r  
subprocessrunr*   )r   editorr   _sys
candidatescmds         r#   edit_configr2    sx   || *+++!##K  (N###&&&''' Yx  7BIh$7$7F 
 	=G##AAAJJAAAJ 	 	Cv|C     3444 ;  !!!	
1[
1
1f
1
1
1222NFC,,-.....rY   c                    t                      rt          d           dS g d}|                                 |v sN|                                                     d          s'|                                                     d          rEt          |                                 |           t          d|  dt                                  dS t                      }i }|	                                rS	 t          |d	          5 }t          j        |          pi }ddd           n# 1 swxY w Y   n# t          $ r i }Y nw xY w|                                d
v rd}nu|                                dv rd}n\|                                rt!          |          }n8|                    ddd                                          rt%          |          }t'          || |           t)                       ddlm}  |||d           t/          |           }|r#| dk    rt          |t1          |                     t          d|  d| d|            dS )zSet a configuration value.zset configuration valuesN)r6  rj   r|   r%  rj  rm  r!  rn  ro  rp  rq  rr  r+  r"  r#  rw  r$  r  r  r@  rB  ry   r  r(  r)  r  r  )_API_KEY_TOKENTERMINAL_SSHu   ✓ Set r+  r   r   >   r  r   r   T>   rd  r}  re  Fr(   r   r  r   rm  )	sort_keyszterminal.cwdr  )r   r"  upperr  r1  r  r!  r@  r>  r   r0  r/  r0  r   r   isdigitrI  r   r  r  r{  rq  rn  r[  rX  )r3   r9  api_keysr   rh  r6  rn  rd  s           r#   set_config_valuer;  *  s   || 0111
 
 
H yy{{h#))++"6"67M"N"NRUR[R[R]R]RhRhiwRxRxsyy{{E***222,..22333
 "##KK 	kG444 6"nQ//526 6 6 6 6 6 6 6 6 6 6 6 6 6 6 	 	 	KKK	 {{}}---	0	0	0	 E

	sB	"	"	*	*	,	, eS%((( ''''''k;%@@@@ .c22G <3.((w 3E : :;;;	
5S
5
5U
5
5
5
566666s6   2D2 D&D2 &D**D2 -D*.D2 2E Ec           	         t          | dd          }||dk    rt                       dS |dk    rt                       dS |dk    rt          | dd          }t          | dd          }|r|mt          d           t                       t          d	           t          d
           t          d           t          d           t	          j        d           t          ||           dS |dk    rt          t                                 dS |dk    rt          t                                 dS |dk    rt                       t          t          dt          j        t          j                             t                       t          d          }t                      }t                      \  }}|s?|s=||k    r7t          t          dt          j                             t                       dS ||k     rt          d| d|            |r t          dt#          |           d           d |D             }d |D             }	|r=t          dt#          |           d           |D ]}
t          d|
d                     |	rzt          dt#          |	           d            |	D ]W}
|
                    d!g           }|r!d"d#                    |dd$                    d%nd&}t          d|
d          |            Xt                       t)          d'd(          }t                       |d)         s|d*         r't          t          d+t          j                             |d,         rCt                       |d,         D ],}t          t          d-| t          j                             -t                       dS |d.k    rt                       t          t          d/t          j        t          j                             t                       t                      \  }}||k    rt          d| d0           n.t          t          d| d| d1t          j                             t                       t          t          d2t          j                             t,          D ]O}t/          |          rt          d3|            $t          t          d4| d5t          j                             Pt                       t          t          d6t          j                             t2                                          D ]\  }}t/          |          rt          d3|            '|                    d!g           }|r dd#                    |dd$                    nd&}t          t          d7| | t          j                             t                      }|rUt                       t          t          d8t#          |           d9t          j                             t          d:           t                       dS t          d;|            t                       t          d<           t          d=           t          d>           t          d?           t          d@           t          dA           t          dB           t          dC           t	          j        d           dS )DzHandle config subcommands.config_commandNshoweditr  r3   r9  z&Usage: hermes config set <key> <value>z	Examples:z3  hermes config set model anthropic/claude-sonnet-4z+  hermes config set terminal.backend dockerz0  hermes config set OPENROUTER_API_KEY sk-or-...r  r  zenv-pathmigrateu*   🔄 Checking configuration for updates...Fr  u    ✓ Configuration is up to date!z  Config version: r  r  z1 new config option(s) will be added with defaultsc                 <    g | ]}|                     d           |S )r  r  r  s     r#   r  z"config_command.<locals>.<listcomp>  s)    KKK!aeeM6J6JKAKKKrY   c                 f    g | ].}|                     d           |                     d          ,|/S )r  r5  r  r  s     r#   r  z"config_command.<locals>.<listcomp>  sN     
 
 
55''
01j0A0A

 
 
rY   u   
  ⚠️  z required API key(s) missing:u	        • r   u   
  ℹ️  z$ optional API key(s) not configured:r  z (enables: r  r  r?  r   T)r\  r]  r_  r`  u   ✓ Configuration updated!ra  u
     ⚠️  checku   📋 Configuration Statusu    ✓z (update available)z  Required:u       ✓ u       ✗ z
 (missing)z  Optional:u       ○ rK  z new config option(s) availablez+    Run 'hermes config migrate' to add themzUnknown config command: zAvailable commands:z4  hermes config           Show current configurationz/  hermes config edit      Open config in editorz6  hermes config set <key> <value>   Set a config valuez;  hermes config check     Check for missing/outdated configz8  hermes config migrate   Update config with new optionsz/  hermes config path      Show config file pathz-  hermes config env-path  Show .env file path)getattrr$  r2  r!  r/   exitr;  r>  r@  r   r   r  r  r  r  r2  GREENr  r   r   r  r
  r  r  REDr  r  r  )argssubcmdr3   r9  r  r  r  r  required_missingoptional_missingr  r  	tools_strr  r.   r  r5  s                    r#   r=  r=  p  sA   T+T22F~6))	6			5dE4((gt,, 	em:;;;GGG+GHHH?@@@DEEEHQKKKe$$$$$	6		o     	:		lnn	9		e@&+v{[[\\\ +???244"6"8"8Z 	> 	kZ6O6O%:FLIIJJJGGGF ##E{EEEEFFF 	a_^,,___```KK{KKK
 
"
 
 

  	1U%5!6!6UUUVVV' 1 1/#f+//0000 	<\%5!6!6\\\]]]' < <,,EJRA$))E"1"I*>*>AAAAPR	:#f+:y::;;;; !T???; 	E7>#: 	E%4flCCDDD: 	DGGG":. D De222FMBBCCCC	7		e/fkJJKKK"6"8"8Z*$$8{8889999%^[^^z^^^`f`mnnoooeM6;//000) 	J 	JHX&& J+++,,,,e;x;;;VZHHIIIIeM6;//000/5577 	K 	KNHdX&& K+++,,,,"-->CK:DIIeBQBi$8$8:::	e<x<<<fjIIJJJJ244 	AGGG%QS00QQQSYS`aabbb?@@@ 	111222#$$$DEEE?@@@FGGGKLLLHIII?@@@=>>>rY   c            	      ~   t           rdS da 	 ddlm}   |             D ]}|j        dvr|j        D ]z}|t
          v r|                    d           o|                    d           }|j        p|j         d|rd	nd
 |j        p|j         d|rd	nd |j	        pd|dddt
          |<   {dS # t          $ r Y dS w xY w)u   Populate OPTIONAL_ENV_VARS from provider profiles not already listed.

    Called once at module load time. Idempotent — repeated calls are no-ops.
    NTr   )list_providers>   r  	_BASE_URL_URLr   zAPI keyzbase URL overridez"base URL (leave empty for default)r  r1  )_profile_env_vars_injectedr  rN  	auth_typer  r  r  display_namer   
signup_urlr   )rN  _pp_var_is_keys       r#   _inject_profile_env_varsrX    sO    " !%,,,,,,!>## 	 	C}L00  ,,,"mmK888VvAVAV=V&)&6&B#(#t#tRYErYY_r#t#t!$!1!=SX  A  AW@~		Z~  A  A>1T ' * $+ +!$''		 	    s   BB. .
B<;B<c            	         t           rdS da 	 ddl} t          t                                                    j        d         }|dz  dz  }|                                sdS |                                D ]_}|                                s|dz  }|                                s|dz  }|                                sK	 t          |d	d
          5 } | j
        |          pi }ddd           n# 1 swxY w Y   n# t          $ r Y w xY w|                    d          p|                    d          p|j        }t          |                    d          pg           }|                    |                    d          pg            |D ]C}	t!          |	t"                    r|	}
i }n6t!          |	t$                    r |	                    d          r|	d         }
|	}nS|
t&          v r]|
                                t+          |                    d          p|                    d                    }|s2|                    d          durt-          fddD                       }|                    d          p| d|                    d          p|
|                    d          pd||                    d          pddt&          |
<   EadS # t          $ r Y dS w xY w)u   Populate OPTIONAL_ENV_VARS from bundled platform plugin manifests.

    Called once at module load time. Idempotent — repeated calls are no-ops.
    Failures are swallowed so a malformed plugin.yaml can't break CLI import.
    NTr   r  r  rE  r  r  r&  r   r   r  r   requires_envoptional_envrM  rN  Fc              3   B   K   | ]}                     |          V  d S r  )r  )r  suf
name_uppers     r#   r  z3_inject_platform_plugin_env_vars.<locals>.<genexpr>n  sE       $ $ #++C00$ $ $ $ $ $rY   )r5  _SECRET_KEY	_PASSWORD_JSONr2  z configurationr3  r  r4  r  rT  )"_platform_plugin_env_vars_injectedr/  r   r   r   r   r   r  r   r0  r0  r   r   r   r   extendr  r*   r  r  r8  r	  r  )r/  	repo_rootplatforms_dirr  manifest_pathr6  r  r  entriesr  r   meta	is_secretr^  s                @r#    _inject_platform_plugin_env_varsrk  :  s    * )-&9 NN**,,4Q7	!I-;##%% 	F"**,, .	 .	E<<>> !M1M '')) 5 % 4 '')) -w??? 71-t~a006BH7 7 7 7 7 7 7 7 7 7 7 7 7 7 7   LL))OX\\&-A-AOUZE8<<77=2>>GNN8<<77=2>>>   eS))  D!#DDt,, 61B1B  =D DD,,, "ZZ\\
 *!5!5!K(9K9KLL	  *)=)=)F)F # $ $ $ $#V$ $ $ ! !I // 4#333"hhx008D88E??2d ) $ 4 4 C	+ 	+!$'')#.	 .	^    sb   AK !AK C>C2&C>2C6	6C>9C6	:C>=K >
DK 
DGK 
K&%K&r  )r  )r  )r   N)FrE  )TF)r;  r  rV  r  r   rH  r  r   r   r,  r/   r  	threadingr   dataclassesr   pathlibr   typingr   r   r   r   r	   hermes_cli.secret_promptr
   r  r  r-   r  r   r<  r$   r   r5   system_IS_WINDOWScompiler  	frozensetrU   r*   rX   rZ   r[   rI  r\   RLockr+  r  r/  hermes_cli.colorsr   r   hermes_cli.default_soulr   r   r   r   r	  r   r   r   r   r   r  r  r  r  r  r  r"  r  r:  r/  r   rq  r<  r>  r@  rB  tuplerN  rT  r[  rd  rf  rj  r{  rw  r  r/  r  r  r  r  r  r  r  r  r  r"  r)  r-  r2  rB  _VALID_CUSTOM_PROVIDER_FIELDSr@  r5  rI  rP  r[  r  r  r	  r  r  r"  r%  r)  r  r  r6  rZ  rX  r[  rf  r4  rr  rs  _COMMENTED_SECTIONSr  r  r}  r  r  r   r  r  r  r  r  r  r  r  r  r  r  r  r$  r2  r;  r=  rQ  rX  rc  rk  r   rY   r#   <module>r{     sX6        				  				       



       ! ! ! ! ! !       3 3 3 3 3 3 3 3 3 3 3 3 3 3 9 9 9 9 9 9		8	$	$
  CEE c ! ! !3 3$ 3 3 3 3l*D *y *T * * * *X ho9,2:9:: P *3 4 4 4 * * 	#   *
C 
D 
 
 
 
  13 tCH~ 2 2 2 BD DeCd38n$<==> C C C AC 4U3T#s(^#;<<= B B B y   ) 6 6 6 6 6n  + + + + + + + + 3 3 3 3 3 3 , 	  HSM    ,D , , , , {HSM    % % %# % % % %P     D    8# #    $9C 9 9 9 90K 6"c " " " " 3 s    8; ;# ; ; ; ;*$ * * * *d - , , , , ,            - - - - -&d & & & &2$ 2 2 2 2x}hsm'C!D    <   :  @t    2  $$ 4    & & &6"d " " " "4}R}} "} !"	}
 } t}  tRt 	4t  	 !t2 	13t4 	5t@ 	AtL 	#DMt\ 	T]tj 	BktH 	&ItP 	"3Qt\ 	3]tl 	"3mtH 	*4Itd 	Fetf 	Rgt}~  O7OfO 	sO 	3	O 	2O* 	B+OD 	dEOF 	DGOH 	bIOT 	bUOV 	RWOX 	CYOZ 	E[O^ 	_O` 	DaOb 	%cOd 	eOt  */ $)
 !]O O O}` 
 a}l ! # '+
 (
 $) "' &+#9
 
)% %m}N  
 !   5 O}N	 7O	}n	  o	}@
  "!"&'
 
 !"&'
 
 A
}^
 &) %* "&/! !_
}h Ti}N !  O} }^ ! $
 
 %'!#&-
 
 _}v  "
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
G Gw}H  }5}r} 	&} 	B} 	 } 	%c} 	%a}  	 !}" 	;#}. 	U/}8 	!%9}B 	DC}D 	EE}F 	%G}H 	UI}J 	eK}L 	"7M} }T 	TU}V 	&sW}^ 	4_}` 	a}p 	!$q}z 	4{}H 	$TI}J 	UK}L 		M}V 	DW}\ 		]}^ 	!
 !
_}f 	%dg}h 	 i}j 	"2k}l 	qm}| 	}} }b %d+#U+
 
 555
 
  y} } }I}H  !& 
 
4 #$
 
6 eS SI}t eu}F 
'

 /0
 

 '
 
 4  
 $&
 
  	
 
 ->
 

 3	
 
 *	
Y9 9G}| 

 

 [
 *
 $ %	
 
 }}f !$  g}x  y}P ,Q}Z  $  !
 - [}T  !%!$ #$  $ "'K& &U}j Rk}~ 
 	R}T 
   "  %  M' 'U}z    !  
 
5 { }@" bA"} } }H" I"}N" "$	 O"}^" "$"' "$ !  !&
 !) #   
 
U: :_"}X$  Y$}h$  i$}v$ "$	 w$}F% ! G%}b%  # &*) c%}R& S&}V& bW&}h& Ri&}t& u&}|& R}&}B' # 
 
   $5 C'}|'  
 "	 	}'}^(   $ &(  $3#$ !#  (,
  $% +0i5 5_(}N*  		O*} } }~*     %& "!
 
*}l+  m+}B, R   C,}d,  	e	e,}v,   &( # '*K& &w,}T.     
 &*; U.}^/    # ! !&; _/}d0   e0}z0  #   *1; {0}P2 
    # C" "Q2}b3  '   c3}F4   !3!$
 "& ! ;
 
 G4}l5 m5}n5 (o5}p5 $Tq5}x5 ry5} }L6 YXX "67E E E		- - T#tCy.)     \6B \ W&+"$78 \& U,7 '\6 L"7 7\F ;= G\V $& W\f .: g\v T&* w\F cA G\V N4 W\f 4@ g\v [& w\F =  G\V =  W\f 5A g\v 0 . w\F :; G\ \V 6(. W\f 2-. g\v <H w\F )$' G\V 3< W\f *%) g\v 4@ w\F 8#( G\V 2> W\f 9+* g\v :F w\F D$7 G\T @% U\b W%> c\p `& q\@ [B A\P _B   Q\ \ \` "T9B$ $a\p YO! !q\@	 V() A	\P	 7C Q	\`	 U') a	\p	 6B q	\@
 o&7 A
\N
 K9 O
\^
 W(, _
\n
 X= o
\~
  B'0 
\L _= M\\ XZ ]\l _ m\| I)& }\J e* K\^ J . _\ \ \n N$%. o\~ F%'. \N O= O\^ jM _\n  o. o\~  j+ \N  O+   O\^ L",. _\n U<3 o\~ Z3. \N n')$o6 O\^ \*)$o6 _\n n')$o6 o\~ Y1$o6 \L z;=\\\ M\^ r&:$o6 _\n C "$45 o\ \ \~ R 8"# \N X:5'6 O\^ f&'"$9: _\n P#, o\| \ 3 }\V N"6 W\f M"= g\v V$3 w\F V!F G\Z G"'"# [\j V@ k\x !@'+# #y\F !@'+# #G\T RL! !U\h ;&' i\v l?) w\D z1	 E\ \ \P @%< Q\^ P> _\l  a6 m\z \ /+ {\L G /+ M\^ L)/ _\l F( m\z SA! !{\H !z0# #I\V (c?* *W\d P)6 e\r L' s\@ B1 A\N e= O\\ s: ]\l !\<# #m\| Y= }\ \ \L W; M\\ g= ]\l  ]' m\| h*) }\J c/ K\X  c@" "Y\f "F/$ $g\p G!	 q\| C$	 }\H K$ I\R V* S\\ M& ]\f U# g\p =(   q\z T# {\D C E\R ; S\ \ \` B  a\n :' o\~ B% \N f0   O\^  L2 _\n  V' o\~ A# \N  M# O\^  u) _\n  yQ o\~  [. \N k0 O\\ J  ]\j t" k\@   S! A \\  #_.% %] \j  %h+' 'k \ \ D!  d38n9M    ,0C 0 0 0 0f4S#X#7    8,tDcN'; , , , ,d A A AA A d38n	A A A AN ! ! !! ! d38n	! ! ! !Hs tDcN?S     (,/ /T#s(^$/	$sCx./ / / /j 8<'+	> >>> tDcN34> T#s(^$	>
 c]> > > >B# #    eCHo    J   ! ! !   WVV         M MhtCH~&> M$}J] M M M M`0 0(4S>": 0d 0 0 0 0,*4 *4$sCx.)A *4T *4 *4 *4 *4Zm	 m	 m	D m	T#s(^ m	 m	 m	 m	`d d t    (  (  = = = =@tCH~ $sCx.    <S#X 4S>      GK + + +$sCx.) +# + +s + + + +^#c3h # # # #L1T#s(^ 1 1 1 1"2d38n 2 2 2 2.~' 
> !	
 3 + 7 5 ) - # # # ! -  3!" /#$ </'%M5A'Q;)37   >s s    : :# : : : : %)'+#	( ( (	$sCx.	!( T#s(^$( tn	(
 
#s(^( ( ( (V8 8c3h 8 8 8 8v $ 2 @)\S#X )\ )\ )\ )\X6$sCx. 6 6 6 6| [_
HU5huox}!DEtCQTH~UVW ^ ^ ^	 	 	 	9t 9 9 9 9 9x+3 + + + +\%S % % % % % %PG GC G G G GT:# :$ : : : :z$ $c $ $ $ $$ $ $ $" "# " " " "s 3 4S>    C    ,s x}    BC BC B B B Br r rj%/ %/ %/P?7# ?7c ?7 ?7 ?7 ?7LF F F\ #    >     . &+ "C C C CN !   " " " " "rY   