§
    }Štj©  ã                   ó†  — U d Z ddlZddlZddlZddlmZ ddlmZmZ ddl	m
Z
 ddl	mZ  ej        e¦  «        Zd	Zd
Z G d„ ded¬¦  «        Z G d„ de¦  «        Zg i dœZeed<   daedz  ed<   dee         fd„Zdefd„Zdeeef         defd„Zdefd„Zdefd„Zdededz  dedz  fd„Zdededdfd„Z dedz  fd „Z!dS )!u®  Detect whether the process is being invoked by an AI coding agent.

Detection is based on environment variables that AI agents set in their shell
sessions. `AI_AGENT` and `AGENT` are treated as a universal standard (any
tool can set its harness id there); the remaining checks are tool-specific and
ordered by priority (first match wins).

The list of known harnesses is maintained on the Hub and exposed at
`{ENDPOINT}/api/agent-harnesses`. We fetch it at most once a day and cache it
locally so the list can be updated without requiring a new client release.

Detection is entirely best-effort: there is no hardcoded list of harnesses. When
the registry cannot be fetched (and no cached copy is available), detection simply
reports "no agent". Any error while fetching/reading the registry is swallowed â€”
detection must never make a process fail.

More details: https://huggingface.co/docs/hub/agents-overview#register-your-agent-harness
é    N)ÚPath)ÚOptionalÚ	TypedDicté   )Ú	constantsé   )Úloggingi€Q é   c                   ó.   — e Zd ZU dZeeef         ed<   dS )ÚHarnessInfozbA single harness entry. `envVars` maps an env var name to a match pattern (see `_env_vars_match`).ÚenvVarsN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚdictÚstrÚ__annotations__© ó    úa/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/huggingface_hub/utils/_detect_agent.pyr   r   4   s,   € € € € € € ØlÐlà�#�s�(Œ^ÐÐÑÐÐr   r   F)Útotalc                   óD   — e Zd ZU dZee         ed<   eeef         ed<   dS )ÚRegistryzJThe agent harness registry, as served by `{ENDPOINT}/api/agent-harnesses`.ÚstandardEnvVarsÚ	harnessesN)	r   r   r   r   Úlistr   r   r   r   r   r   r   r   r   :   s>   € € € € € € ØTÐTà˜#”YÐÐÑØ�C˜Ð$Ô%Ð%Ð%Ñ%Ð%Ð%r   r   )r   r   Ú_EMPTY_REGISTRYÚ	_registryÚreturnc                  ót  — t          ¦   «         } |                      d¦  «        pg }|                      d¦  «        pi }|                     ¦   «         D ]r\  }}|pi                      d¦  «        }|rt          |¦  «        r|c S |D ]>}t          j                             |d¦  «                             ¦   «         |k    r|c c S Œ?Œsd„ |                     ¦   «         D ¦   «         }|D ]S}t          j                             |d¦  «                             ¦   «                              ¦   «         x}r||v r|c S  dS ŒTdS )a[  Return the id of the detected AI agent harness or `None`.

    Harnesses are checked in registry order; for each one we match its env var
    pattern(s) and, failing that, the standard `AI_AGENT` / `AGENT` vars
    against the harness id. The first match wins. When a standard var is set to
    an unrecognized value, `"unknown"` is returned.
    r   r   r   Ú c                 ó6   — h | ]}|                      ¦   «         ’ŒS r   )Úlower)Ú.0Úks     r   ú	<setcomp>zdetect_agent.<locals>.<setcomp>^   s    € Ð@Ð@Ð@¨!˜AŸGšG™IœIÐ@Ð@Ð@r   ÚunknownN)	Ú_get_registryÚgetÚitemsÚ_env_vars_matchÚosÚenvironÚstripÚkeysr$   )	ÚregistryÚstandard_varsr   Ú
harness_idÚinfoÚenv_varsÚvarÚlowercased_harnessesÚvalues	            r   Údetect_agentr9   I   sq  € õ ‰Œ€HØ—L’LÐ!2Ñ3Ô3Ð9°r€MØ—’˜[Ñ)Ô)Ð/¨R€Ià%ŸOšOÑ-Ô-ð "ð "Ñˆ
�DØ�J˜B×#Ò# IÑ.Ô.ˆØð 	�¨Ñ1Ô1ð 	ØÐÐÐØ ð 	"ð 	"ˆCÝŒz�~Š~˜c 2Ñ&Ô&×,Ò,Ñ.Ô.°*Ò<Ð<Ø!Ð!Ð!Ð!Ð!Ð!ð =ð	"ð
 AÐ@¨y¯~ª~Ñ/?Ô/?Ð@Ñ@Ô@ÐØð ð ˆÝ”J—N’N 3¨Ñ+Ô+×1Ò1Ñ3Ô3×9Ò9Ñ;Ô;Ð;ˆ5ð 	ØÐ,Ð,Ð,Ø���Ø�9�9ð	ð
 ˆ4r   c                  ó"   — t          ¦   «         duS )zDReturn `True` if the process is being invoked by an AI coding agent.N)r9   r   r   r   Úis_agentr;   h   s   € å‰>Œ> Ð%Ð%r   r5   c                 ó¢   — |                       ¦   «         D ]9\  }}t          j                             |¦  «        }|sŒ'|dk    r dS ||k    r dS Œ:dS )záReturn `True` if any `(var, pattern)` from the harness matches the environment.

    Supported patterns:
      - `"*"`: the variable is set to any non-empty value
      - `"<value>"`: the variable equals this exact value
    Ú*TF)r+   r-   r.   r*   )r5   r6   Úpatternr8   s       r   r,   r,   m   sk   € ð !ŸšÑ(Ô(ð ð ‰ˆˆWÝ”
—’˜sÑ#Ô#ˆØð 	ØØ�cŠ>ˆ>Ø�4�4Ø�GÒÐØ�4�4ð àˆ5r   c                  ó¤   — t           €C	 t          ¦   «         a n3# t          $ r& t                               dd¬¦  «         t
          a Y nw xY wt           S )zµReturn the harness registry, loading (and caching in-process) on first call.

    Best-effort: any unexpected error degrades to an empty registry so detection
    never raises.
    Nz+Could not resolve agent harnesses registry.T©Úexc_info)r   Ú_load_registryÚ	ExceptionÚloggerÚdebugr   r   r   r   r)   r)      s`   € õ Ðð	(Ý&Ñ(Ô(ˆIˆIøÝð 	(ð 	(ð 	(Ý�LŠLÐFÐQUˆLÑVÔVÐVÝ'ˆIˆIˆIð	(øøøõ Ðs   ‰ ˜-AÁAc                  óÊ   — t           j        } t          | t          ¬¦  «        x}r|S t	          ¦   «         x}�t          | |¦  «         |S t          | d¬¦  «        x}r|S t          S )zÆResolve the registry from the local cache or the Hub.

    No hardcoded list: if the Hub is unreachable and no cached copy exists, an
    empty registry is returned (i.e. no agent is detected).
    )Úmax_ageN)r   ÚAGENT_HARNESSES_PATHÚ_read_cached_registryÚ_REGISTRY_TTL_SECONDSÚ_fetch_registryÚ_write_cached_registryr   )ÚpathÚcachedÚfetchedÚstales       r   rB   rB   �   s}   € õ Ô)€Dõ ' tÕ5JÐKÑKÔKÐK€vð Øˆõ #Ñ$Ô$Ð$ˆÐ1Ý˜t WÑ-Ô-Ð-Øˆõ & d°DÐ9Ñ9Ô9Ð9€uð ØˆÝÐr   rM   rG   c                 ó”  — 	 t           j                             | ¦  «        sdS |�9t          j        ¦   «         t           j                             | ¦  «        z
  |k    rdS t          | d¬¦  «        5 }t          j        |¦  «        cddd¦  «         S # 1 swxY w Y   dS # t          $ r  t           
                    dd¬¦  «         Y dS w xY w)zBReturn the cached registry, or `None` if missing/stale/unreadable.Núutf-8©Úencodingz/Could not read cached agent harnesses registry.Tr@   )r-   rM   ÚexistsÚtimeÚgetmtimeÚopenÚjsonÚloadrC   rD   rE   )rM   rG   Úfs      r   rI   rI   ¦   s  € ð	ÝŒw�~Š~˜dÑ#Ô#ð 	Ø�4ØÐ¥D¤I¡K¤Kµ"´'×2BÒ2BÀ4Ñ2HÔ2HÑ$HÈWÒ#TÐ#TØ�4Ý�$ Ð)Ñ)Ô)ð 	 ¨QÝ”9˜Q‘<”<ð	 ð 	 ð 	 ð 	 ñ 	 ô 	 ð 	 ð 	 ð 	 ð 	 ð 	 ð 	 øøøð 	 ð 	 ð 	 ð 	 ð 	 ð 	 øåð ð ð Ý�ŠÐFÐQUˆÑVÔVÐVØˆtˆtðøøøs@   ‚B £9B ÁB Á/BÂB ÂBÂB ÂBÂB Â&CÃCr1   c                 ó4  — 	 t          | ¦  «        j                             dd¬¦  «         t          | dd¬¦  «        5 }t	          j        ||¦  «         d d d ¦  «         d S # 1 swxY w Y   d S # t          $ r  t                               dd¬¦  «         Y d S w xY w)NT)ÚparentsÚexist_okÚwrR   rS   z)Could not cache agent harnesses registry.r@   )	r   ÚparentÚmkdirrX   rY   ÚdumprC   rD   rE   )rM   r1   r[   s      r   rL   rL   ´   sñ   € ðQÝˆT‰
Œ
Ô×Ò¨°tÐÑ<Ô<Ð<Ý�$˜ gÐ.Ñ.Ô.ð 	#°!ÝŒI�h Ñ"Ô"Ð"ð	#ð 	#ð 	#ñ 	#ô 	#ð 	#ð 	#ð 	#ð 	#ð 	#ð 	#ð 	#øøøð 	#ð 	#ð 	#ð 	#ð 	#ð 	#øåð Qð Qð QÝ�ŠÐ@È4ˆÑPÔPÐPÐPÐPÐPðQøøøs4   ‚;A- ½A ÁA- Á A$Á$A- Á'A$Á(A- Á-&BÂBc                  ó8  — t           j        rdS 	 ddlm}   | ¦   «                              t           j        › d�t          ¬¦  «        }|                     ¦   «          |                     ¦   «         S # t          $ r  t                               dd¬¦  «         Y dS w xY w)	zMFetch the registry from the Hub. Returns `None` when offline or on any error.Nr   )Úget_sessionz/api/agent-harnesses)Útimeoutz6Could not fetch agent harnesses registry from the Hub.Tr@   )r   ÚHF_HUB_OFFLINEÚ_httprd   r*   ÚENDPOINTÚ_REGISTRY_FETCH_TIMEOUTÚraise_for_statusrY   rC   rD   rE   )rd   Úresponses     r   rK   rK   ½   s·   € åÔð ØˆtðØ&Ð&Ð&Ð&Ð&Ð&à�;‘=”=×$Ò$ÝÔ!Ð7Ð7Ð7Ý+ð %ñ 
ô 
ˆð 	×!Ò!Ñ#Ô#Ð#Ø�}Š}‰ŒÐøÝð ð ð Ý�ŠÐMÐX\ˆÑ]Ô]Ð]Øˆtˆtðøøøs   �AA/ Á/&BÂB)"r   rY   r-   rV   Úpathlibr   Útypingr   r   r"   r   r	   Ú
get_loggerr   rD   rJ   ri   r   r   r   r   r   r   r9   Úboolr;   r   r,   r)   rB   ÚintrI   rL   rK   r   r   r   ú<module>rq      sR  ððð ð ð& €€€Ø 	€	€	€	Ø €€€Ø Ð Ð Ð Ð Ð Ø &Ð &Ð &Ð &Ð &Ð &Ð &Ð &à Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð ð 
ˆÔ	˜HÑ	%Ô	%€ð "Ð ð Ð ðð ð ð ð �) 5ð ñ ô ð ð&ð &ð &ð &ð &ˆyñ &ô &ð &ð 13ÀÐDÐD€�Ð DÐ DÑ Dð "€	ˆ8�d‰?Ð !Ð !Ñ !ð�h˜s”mð ð ð ð ð>&�$ð &ð &ð &ð &ð
˜d 3¨ 8œnð °ð ð ð ð ð$�xð ð ð ð ð ˜ð ð ð ð ð. ð ¨c°D©jð ¸XÈ¹_ð ð ð ð ðQ ð Q°ð Q¸Tð Qð Qð Qð Qð˜ D™ð ð ð ð ð ð r   