
    `gj                        U d Z ddlmZ ddlZddlZddlmZmZmZ ddl	m
Z
  ej                  e      Zi Zded<    ej                          ZddZdd	Zdd
ZddZddZy)a  
Video Generation Provider Registry
==================================

Central map of registered providers. Populated by plugins at import-time via
``PluginContext.register_video_gen_provider()``; consumed by the
``video_generate`` tool to dispatch each call to the active backend.

Active selection
----------------
The active provider is chosen by ``video_gen.provider`` in ``config.yaml``.
If unset, :func:`get_active_provider` applies fallback logic:

1. If exactly one *available* provider is registered, use it.
2. Otherwise return ``None`` (the tool surfaces a helpful error pointing
   the user at ``hermes tools``).

Mirrors ``agent/image_gen_registry.py`` so the two surfaces behave the
same: the unconfigured fallback is filtered by ``is_available()`` so a box
that has credentials for only one backend (e.g. DeepInfra, while the
``fal``/``xai`` plugins also register unconditionally) auto-selects it
instead of returning ``None``.
    )annotationsN)DictListOptional)VideoGenProviderzDict[str, VideoGenProvider]
_providersc                   t        | t              s!t        dt        |       j                         | j
                  }t        |t              r|j                         st        d      t        5  t        j                  |      }| t        |<   ddd       +t        j                  d|t        |      j                         yt        j                  d|t        |       j                         y# 1 sw Y   axY w)u   Register a video generation provider.

    Re-registration (same ``name``) overwrites the previous entry and logs
    a debug message — this makes hot-reload scenarios (tests, dev loops)
    behave predictably.
    z=register_provider() expects a VideoGenProvider instance, got z3Video gen provider .name must be a non-empty stringNz.Video gen provider '%s' re-registered (was %r)z'Registered video gen provider '%s' (%s))
isinstancer   	TypeErrortype__name__namestrstrip
ValueError_lockr   getloggerdebug)providerr   existings      K/root/.hermes/venv/lib/python3.12/site-packages/agent/video_gen_registry.pyregister_providerr   (   s     h 01>**+-
 	
 ==DdC 

NOO	 $>>$'#
4$ EtTRZ^MdMde>d8nF]F]^$ $s   /C..C7c                     t         5  t        t        j                               } ddd       t	         d       S # 1 sw Y   xY w)z0Return all registered providers, sorted by name.Nc                    | j                   S )Nr   )ps    r   <lambda>z list_providers.<locals>.<lambda>D   s
    qvv     )key)r   listr   valuessorted)itemss    r   list_providersr%   @   s;    	 *Z&&()*%-..* *s	   ;Ac                    t        | t              syt        5  t        j	                  | j                               cddd       S # 1 sw Y   yxY w)z5Return the provider registered under *name*, or None.N)r
   r   r   r   r   r   r   s    r   get_providerr'   G   s9    dC 	 ,~~djjl+, , ,s   #AAc                    d} 	 ddl m}  |       }t        |t              r|j	                  d      nd}t        |t              rA|j	                  d      }t        |t
              r |j                         r|j                         } t        5  t        t              }ddd       | r,j	                  |       }||S t        j                  d|        yd
d}j                         D 	cg c]  }	 ||	      s|	 }
}	t        |
      d	k(  r|
d   S y# t        $ r }t        j                  d|       Y d}~d}~ww xY w# 1 sw Y   xY wc c}	w )zResolve the currently-active provider.

    Reads ``video_gen.provider`` from config.yaml; falls back per the
    module docstring.
    Nr   )load_config	video_genr   z1Could not read video_gen.provider from config: %szEvideo_gen.provider='%s' configured but not registered; failing closedc                    	 t        | j                               S # t        $ r+}t        j	                  d| j
                  |       Y d}~yd}~ww xY w)zDWrap ``is_available()`` so a buggy provider doesn't kill resolution.z.video_gen provider %s.is_available() raised %sNF)boolis_available	Exceptionr   r   r   )r   excs     r   _is_available_safez/get_active_provider.<locals>._is_available_safeo   sB    	()) 	LLI166SVW	s    	A!A

A   )r   r   returnr,   )hermes_cli.configr)   r
   dictr   r   r   r.   r   r   r   r   r"   len)
configuredr)   cfgsectionrawr/   snapshotr   r0   r   	availables              r   get_active_providerr<   O   s.    !%J
O1m*4S$*?#''+&Tgt$++j)C#s#		 YY[
 
 $
#$ <<
+OS	
  %OO-Gq1CA1FGIG
9~|A  OH#NNO$ $2 Hs0   BD D;'E5E	D8D33D8;Ec                 b    t         5  t        j                          ddd       y# 1 sw Y   yxY w)z"Clear the registry. **Test-only.**N)r   r   clear r   r   _reset_for_testsr@      s&    	   s   %.)r   r   r2   None)r2   zList[VideoGenProvider])r   r   r2   Optional[VideoGenProvider])r2   rB   )r2   rA   )__doc__
__future__r   logging	threadingtypingr   r   r   agent.video_gen_providerr   	getLoggerr   r   r   __annotations__Lockr   r   r%   r'   r<   r@   r?   r   r   <module>rL      sf   0 #   ' ' 5			8	$ +-
' ,	_0/,0fr   