
    [j                        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dS )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]
_providersproviderr   returnNonec                <   t          | t                    s$t          dt          |           j                   | j        }t          |t                    r|                                st          d          t          5  t                              |          }| t          |<   ddd           n# 1 swxY w Y   |0t                              d|t          |          j                   dS t                              d|t          |           j                   dS )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)r	   r   existings      :/home/ice/.hermes/hermes-agent/agent/video_gen_registry.pyregister_providerr   (   sJ    h 011 
->>*- -
 
 	
 =DdC   P

 PNOOO	 $ $>>$''#
4$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ EtTRZ^^Mdeeeee>d8nnF]^^^^^s    %B11B58B5List[VideoGenProvider]c                     t           5  t          t                                                    } ddd           n# 1 swxY w Y   t	          | d           S )z0Return all registered providers, sorted by name.Nc                    | j         S )Nr   )ps    r   <lambda>z list_providers.<locals>.<lambda>D   s    qv     )key)r   listr   valuessorted)itemss    r   list_providersr(   @   s    	 * *Z&&(())* * * * * * * * * * * * * * *%--....s   ';??r   r   Optional[VideoGenProvider]c                    t          | t                    sdS t          5  t                              |                                           cddd           S # 1 swxY w Y   dS )z5Return the provider registered under *name*, or None.N)r   r   r   r   r   r   r   s    r   get_providerr+   G   s    dC   t	 , ,~~djjll++, , , , , , , , , , , , , , , , , ,s   ,AAAc                   	 d} 	 ddl m}  |            }t          |t                    r|                    d          nd}t          |t                    rR|                    d          }t          |t
                    r(|                                r|                                } n2# t          $ r%}t          	                    d|           Y d}~nd}~ww xY wt          5  t          t                    }ddd           n# 1 swxY w Y   | r6|                    |           }||S t          	                    d|            dS dd		fd|                                D             }t          |          dk    r|d         S dS )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 closedr    r   r
   boolc                    	 t          |                                           S # t          $ r,}t                              d| j        |           Y d}~dS 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)r/   is_available	Exceptionr   r   r   )r    excs     r   _is_available_safez/get_active_provider.<locals>._is_available_safeo   se    	(())) 	 	 	LLI16SVWWW55555	s    # 
A!AAc                *    g | ]} |          |S  r6   ).0r    r4   s     r   
<listcomp>z'get_active_provider.<locals>.<listcomp>{   s*    GGGq1C1CA1F1FGGGGr"      )r    r   r
   r/   )hermes_cli.configr-   r   dictr   r   r   r2   r   r   r   r   r%   len)

configuredr-   cfgsectionrawr3   snapshotr	   	availabler4   s
            @r   get_active_providerrC   O   s    !%J
O111111kmm*4S$*?*?I#''+&&&Tgt$$ 	)++j))C#s## )		 ) YY[[
 O O OH#NNNNNNNNO 
 $ $
##$ $ $ $ $ $ $ $ $ $ $ $ $ $ $  <<
++OS	
 	
 	
 t    HGGGHOO--GGGI
9~~|4s*   B#B) )
C3CC"DD
Dc                 x    t           5  t                                           ddd           dS # 1 swxY w Y   dS )z"Clear the registry. **Test-only.**N)r   r   clearr6   r"   r   _reset_for_testsrF      s~    	                   s   /33)r	   r   r
   r   )r
   r   )r   r   r
   r)   )r
   r)   )r
   r   )__doc__
__future__r   logging	threadingtypingr   r   r   agent.video_gen_providerr   	getLoggerr   r   r   __annotations__Lockr   r   r(   r+   rC   rF   r6   r"   r   <module>rP      s    0 # " " " " "      ' ' ' ' ' ' ' ' ' ' 5 5 5 5 5 5		8	$	$ +-
 , , , ,	_ _ _ _0/ / / /, , , ,0 0 0 0f     r"   