
    )jq;                        d Z ddlmZ ddlmZmZ ddlmZ  ed           G d d                      Zd$d
Z	ddddddddd%dZ
d&dZd'dZd&dZd(d Zdd!d)d"Zd#S )*u  Provider/model inventory context — shared substrate for the dashboard
``/api/model/options``, the TUI ``model.options``/``model.save_key``
JSON-RPC handlers, and the interactive picker.

Before this module the three call-sites each duplicated:

1. The 17-LOC config-slice that pulls ``model.{default,name,provider,base_url}``,
   ``providers:``, and ``custom_providers:`` out of ``load_config()``;
2. The call into ``list_authenticated_providers`` with the resulting kwargs;
3. (TUI only) a 45-LOC post-pass that merges authenticated rows with
   unconfigured ``CANONICAL_PROVIDERS`` rows and emits ``authenticated``/
   ``auth_type``/``key_env``/``warning`` hints for the picker UI.

Consolidating those three steps into one entry point eliminates two bugs
the duplicates were hiding:

- The dashboard read ``cfg.get("custom_providers")`` directly, missing the
  v12+ keyed ``providers:`` form (which the TUI handled via
  ``get_compatible_custom_providers``).
- The TUI's canonical-merge keyed on ``is_user_defined`` to decide
  ordering. Section 3 of ``list_authenticated_providers`` sets
  ``is_user_defined=True`` even for canonical slugs that appear in the
  ``providers:`` config dict, which silently demoted them to the tail of
  the picker. ``_reorder_canonical`` keys on slug membership instead.

Substrate facts (verified May 2026):
- ``list_authenticated_providers`` already populates each row's
  ``models`` from the curated catalog (same source as the picker). Do
  NOT call ``provider_model_ids()`` per row to "freshen" — that bypasses
  curation and pulls in non-agentic models (Nous /models returns ~400
  IDs including TTS, embeddings, rerankers, image/video generators).
    )annotations)	dataclassreplace)OptionalT)frozenc                  X    e Zd ZU dZded<   ded<   ded<   ded<   ded	<   d
d
d
dddZd
S )ConfigContextzSnapshot of the model + provider config every inventory caller
    needs. Built once via ``load_picker_context()``; the TUI overlays
    live agent state via ``with_overrides()`` before passing through.
    strcurrent_providercurrent_modelcurrent_base_urldictuser_providerslistcustom_providersN)r   r   r   Optional[str]return'ConfigContext'c               R    i }|r||d<   |r||d<   |r||d<   |rt          | fi |n| S )u   Return a copy with truthy overrides applied.

        Truthy-only because the TUI reads agent attributes that may be
        empty strings before an agent is spawned — empties must NOT
        clobber the disk-config values.
        r   r   r   )r   )selfr   r   r   kws        ;/home/wildlama/.hermes/hermes-agent/hermes_cli/inventory.pywith_overrideszConfigContext.with_overrides8   sa      	6%5B!" 	0"/B 	6%5B!"&(2wt""r"""d2    )r   r   r   r   r   r   r   r   )__name__
__module____qualname____doc____annotations__r    r   r   r	   r	   +   s          
 
 +/'+*.3 3 3 3 3 3 3 3r   r	   r   c            	        ddl m} m}  |            }|                    di           }t	          |t
                    r]|                    d|                    dd                    pd}|                    dd          pd}|                    dd          pd}n|rt          |          nd}d}d}|                    d	          }t          |||t	          |t
                    r|ni  | |          
          S )u   Load the disk-config snapshot every consumer needs.

    Replaces the inline 17-LOC config-slice that ``web_server.py`` and
    ``tui_gateway/server.py`` (×2 sites) used to do.
    r   )get_compatible_custom_providersload_configmodeldefaultname providerbase_url	providers)r   r   r   r   r   )hermes_cli.configr"   r#   get
isinstancer   r
   r	   )r"   r#   cfg	model_cfgr   r   r   raws           r   load_picker_contextr1   O   s    ONNNNNNN
+--C$$I)T"" !ivr1J1JKKQr$==R88>B$==R88>B +4;I
''+

C)#)(d33;ss88==   r   F2   )include_unconfiguredpicker_hintscanonical_orderpricingcapabilitiesforce_fresh_nous_tier
max_modelsctxr3   boolr4   r5   r6   r7   r8   r9   intr   c          	     T   ddl m}  || j        | j        | j        | j        | j        ||          }	|r t          |	          t          |	|           z   }	|rt          |	           |rt          |	          }	|rt          |	|           |rt          |	           |	| j        | j        dS )u  Build the ``{providers, model, provider}`` shape every consumer
    needs from a single substrate call.

    Flags:
    - ``include_unconfigured``: append ``CANONICAL_PROVIDERS`` rows that
      ``list_authenticated_providers`` didn't emit (TUI uses this to show
      the full provider universe in the picker).
    - ``picker_hints``: add ``authenticated``/``auth_type``/``key_env``/
      ``warning`` per row (TUI ``ModelPickerDialog`` shape).
    - ``canonical_order``: reorder canonical-slug rows to
      ``CANONICAL_PROVIDERS`` declaration order; truly-custom rows go
      last (TUI display order).
    - ``pricing``: enrich each row with formatted per-model pricing and,
      for Nous, ``free_tier``/``unavailable_models`` so the GUI picker can
      show $/Mtok columns and gate paid models on free accounts —
      mirroring the ``hermes model`` CLI picker. Adds network calls
      (pricing fetch + Nous tier check); only set for interactive pickers.
    - ``capabilities``: add a per-row ``capabilities`` map
      ``{model: {fast, reasoning}}`` so pickers can gate the model-options
      controls (fast toggle / reasoning) to what each model actually
      supports, instead of offering knobs the backend would reject.
    - ``force_fresh_nous_tier``: bypass the short Nous free-tier cache when
      selecting Portal-recommended Nous models and applying tier gating. Keep
      this false for UI picker opens; explicit auth/model flows can opt in
      when they need freshly-purchased credits to show up immediately.
    r   )list_authenticated_providers)r   r   r   r   r   r8   r9   r8   )r*   r$   r(   )hermes_cli.model_switchr>   r   r   r   r   r   r   _append_unconfigured_rows_apply_picker_hints_reorder_canonical_apply_pricing_apply_capabilities)
r:   r3   r4   r5   r6   r7   r8   r9   r>   rowss
             r   build_models_payloadrG   o   s    J EDDDDD''--')-3  D  ADzz5dC@@@ "D!!! (!$'' Jt3HIIII "D!!! "(  r   rF   
list[dict]Nonec                t   ddl m} 	 ddlm} n# t          $ r d}Y nw xY w| D ]}|                    d          pd}i }|                    d          pg D ]\}d}|8|r6	  |||          }|t          |j                  }n# t          $ r d}Y nw xY wt           ||                    |d	||<   ]||d
<   dS )u  Attach a ``{model: {fast, reasoning}}`` map to each provider row.

    `fast` mirrors ``model_supports_fast_mode`` (the same gate the runtime
    enforces). `reasoning` comes from the models.dev catalog when known and
    defaults to True otherwise — the effort dial is broadly accepted and a
    no-op on models that ignore it, whereas hiding it from a capable-but-
    uncatalogued model is the worse failure.
    r   )model_supports_fast_mode)get_model_capabilitiesNslugr'   modelsT)fast	reasoningr7   )hermes_cli.modelsrK   agent.models_devrL   	Exceptionr,   r;   supports_reasoning)	rF   rK   rL   rowrM   capsr$   rP   metas	            r   rE   rE      sJ    ;:::::&;;;;;;; & & &!%&  # #wwv$"+-WWX&&," 	 	EI%1d1%11$>>D'$()@$A$A	  % % % $III% 55e<<==& DKK
 #N'# #s    "BBBc                `   ddl m}m} d | D             }|j        pd                                }g }|D ]y}|j                                        |v r|                    |j        |                    |j        |j                  |j                                        |k    dg ddd           z|S )zBBuild skeleton rows for canonical providers missing from ``rows``.r   )CANONICAL_PROVIDERS_PROVIDER_LABELSc                B    h | ]}|d                                           S rM   )lower).0rs     r   	<setcomp>z,_append_unconfigured_rows.<locals>.<setcomp>   s&    ,,,!AfIOO,,,r   r'   F	canonical)rM   r&   
is_currentis_user_definedrN   total_modelssource)	rQ   rY   rZ   r   r]   rM   appendr,   label)rF   r:   rY   rZ   seencurextrasentrys           r   rA   rA      s    GGGGGGGG,,t,,,D%2
,
,
.
.CF$ 
 
:%%
(,,UZEE#j..00C7#( !% 
	
 
	
 
	
 
	
 Mr   c                ~   ddl m} | D ]}d|v r|                    d          dk    o|                    d           }| |d<   |r|                    d          rT|                    |d                   }|r|j        nd	}|r|j        r|j        d         nd
}||d<   ||d<   |d	k    r|rd| dnd| d|d<   dS )a0  Add ``authenticated``/``auth_type``/``key_env``/``warning`` per row.

    Mutates ``rows`` in-place. Rows already from
    ``list_authenticated_providers`` are marked ``authenticated=True``;
    the unconfigured skeleton rows from ``_append_unconfigured_rows`` get
    the picker's setup-hint shape.
    r   )PROVIDER_REGISTRYauthenticatedre   ra   rN   rc   rM   api_keyr'   	auth_typekey_envzpaste z to activatez!run `hermes model` to configure ()warningN)hermes_cli.authrm   r,   rp   api_key_env_vars)rF   rm   rU   is_skeletonr.   rp   rq   s          r   rB   rB      s8    211111 
 
c!! ggh'';6Pswwx?P?P;P#.O 	cgg&788 	##CK00%(7CMMi	 ,C ## 	
 %K I I%%'% +W****AYAAA 	I+
 
r   c                    ddl m} d t          |          D             t          fd| D             fd          }fd| D             }||z   S )u  Canonical slugs in ``CANONICAL_PROVIDERS`` declaration order;
    truly-custom rows last.

    Keys on slug membership, NOT ``is_user_defined`` — section 3 of
    ``list_authenticated_providers`` sets ``is_user_defined=True`` on
    rows from the ``providers:`` config dict even when the slug is
    canonical. Keying on the flag would silently demote canonical
    providers configured via the new keyed schema.
    r   )rY   c                $    i | ]\  }}|j         |S r    r\   )r^   ies      r   
<dictcomp>z&_reorder_canonical.<locals>.<dictcomp>%  s     BBB41aQVQBBBr   c              3  0   K   | ]}|d          v |V  dS )rM   Nr    r^   r_   orders     r   	<genexpr>z%_reorder_canonical.<locals>.<genexpr>'  s1      //qAfI......//r   c                     | d                  S )NrM   r    )r_   r~   s    r   <lambda>z$_reorder_canonical.<locals>.<lambda>(  s    eAfI& r   )keyc                (    g | ]}|d          v|S r\   r    r}   s     r   
<listcomp>z&_reorder_canonical.<locals>.<listcomp>*  s'    888A6%!7!7a!7!7!7r   )rQ   rY   	enumeratesorted)rF   rY   canonrj   r~   s       @r   rC   rC     s     655555BB9-@#A#ABBBE////D///&&&&  E 9888888F6>r   r?   c               f   ddl m}m}m}m} d}| D ]}t          |                    dd                                                    }|                    d          pg }	|	sR	  ||          pi }
n# t          $ r i }
Y nw xY w|
svi }|	D ]}|
                    |          }|s|                    dd          }|                    dd          }|                    d	d          }|dk    r ||          nd}|dk    r ||          nd}|r ||          nd}|d
k    o|d
k    p|dk    }||||d||<   |r||d<   |dk    ri	 | ||          }t          |          |d<   |r$ |t          |	          |
d          \  }}||d<   ng |d<   # t          $ r d|d<   g |d<   Y w xY wdS )u  Enrich each provider row with per-model pricing + Nous tier gating.

    Mutates ``rows`` in-place. For every row whose provider supports live
    pricing (openrouter / nous / novita) adds::

        row["pricing"] = {model_id: {"input": "$3.00", "output": "$15.00",
                                     "cache": "$0.30" | None, "free": bool}}

    For Nous additionally adds::

        row["free_tier"] = bool            # current account is free-tier
        row["unavailable_models"] = [...]  # paid models a free user can't pick

    Prices are pre-formatted via ``_format_price_per_mtok`` so the GUI just
    renders strings — identical formatting to the CLI picker. All failures
    are swallowed (best-effort): a row simply gets no ``pricing`` key.
    r   )_format_price_per_mtokcheck_nous_free_tierget_pricing_for_providerpartition_nous_models_by_tierNrM   r'   rN   prompt
completioninput_cache_readfree)inputoutputcacher   r6   nous)force_fresh	free_tierT)r   unavailable_modelsF)rQ   r   r   r   r   r
   r,   r]   rS   r;   r   )rF   r8   r   r   r   r   nous_free_tierrU   rM   rN   raw_pricing	formattedmidpinp_rawout_raw	cache_rawinpoutr   is_free_selectableunavailables                          r   rD   rD   .  s   ,            &*N 5/ 5/37762&&''--//""(b 		22488>BKK 	 	 	KKK	 	%'	 	 	C$$A eeHb))GeeL"--G0"55I5<]]((111C5<]]((111C9BL**9555EVmD)C#)G	 IcNN  	'&C	N6>>/!)%9%9$9& & &N $(#7#7K ! 3/L/LVkT0 0 0,K 1<C,--02C,- / / / $)K ,.()))	/ G5/ 5/s%   $A22B BAFF,+F,N)r   r	   )r:   r	   r3   r;   r4   r;   r5   r;   r6   r;   r7   r;   r8   r;   r9   r<   r   r   )rF   rH   r   rI   )rF   rH   r:   r	   r   rH   )rF   rH   r   rH   )rF   rH   r8   r;   r   rI   )r   
__future__r   dataclassesr   r   typingr   r	   r1   rG   rE   rA   rB   rC   rD   r    r   r   <module>r      si   B # " " " " " * * * * * * * *       $ 3  3  3  3  3  3  3  3F   F "'!"'@ @ @ @ @ @F## ## ## ##R   0#
 #
 #
 #
L   0 #(U/ U/ U/ U/ U/ U/ U/ U/r   