Ë
    áß²jó  ã                  óú   — d Z ddlmZ ddlZddlZddlZddlZddlmZm	Z	m
Z
mZmZ dZdZ eh d£«      Z eh d£«      Zdd	„Zdd
„Zddd„Zdd„Zdd„Zej.                  fdd„Zej.                  fdd„Zdd„Zdd„Zy)a#  Who wrote the user side of a turn, carried from the dispatcher into the recipient's turn.

The dispatcher sets ``HERMES_TURN_AUTHOR`` on the recipient's one-shot subprocess only. A cached
gateway agent sees several authors over its lifetime, so the author is read per turn, never per agent.
é    )ÚannotationsN)ÚAnyÚDictÚMappingÚMutableMappingÚOptionalÚHERMES_TURN_AUTHORéÈ   >   ÚCcÚCnÚCoÚCs>   Ú1ÚyesÚtruec                óˆ   — t        | t        «      sydj                  d„ | D «       «      j                  «       }|sy|dt         S )zÅStrip whitespace, control and unassigned characters, then cap the length. None when nothing is left.
    Format characters and non-breaking spaces stay so emoji sequences and display names survive.NÚ c              3  óX   K  — | ]"  }t        j                  |«      t        vsŒ|–— Œ$ y ­w©N)ÚunicodedataÚcategoryÚ_DROPPED_CATEGORIES)Ú.0Úchs     ú6/home/ubuntu/.hermes/hermes-agent/agent/turn_author.pyú	<genexpr>z_clean_text.<locals>.<genexpr>   s#   è ø€ Ò]˜"¬×)=Ñ)=¸bÓ)AÔI\Ò)\”2Ñ]ùs   ‚ *£*)Ú
isinstanceÚstrÚjoinÚstripÚ_MAX_FIELD_LEN)ÚvalueÚtexts     r   Ú_clean_textr$      sA   € ô �eœSÔ!ØØ�7‰7Ñ] Ô]Ó]×cÑcÓe€DÙØØ�”Ð Ð ó    c                ó°   — t        | t        «      r$| j                  «       j                  «       t        v S t        | t
        t        f«      xr t        | «      S r   )r   r   r    ÚlowerÚ_TRUTHYÚboolÚint)r"   s    r   Ú	_bot_flagr+   #   s@   € Ü�%œÔØ�{‰{‹}×"Ñ"Ó$¬Ð/Ð/Ü�eœd¤C˜[Ó)Ò9¬d°5«kÐ9r%   c                óH   — |xs dj                  «       }|rd|› d| › �S d| › �S )zØ``bot:<profile>`` for a bot on the recipient's own install, ``bot:<origin>/<profile>`` for one on another
    install. The origin is the Desktop's connection id on a relayed dm and the sender's hostname on a peer dm.r   úbot:ú/)r    )ÚprofileÚorigins     r   Úbot_author_idr1   )   s7   € ð Šl˜×!Ñ!Ó#€FÙ(.ˆT�&�˜˜7˜)Ð$ÐD°d¸7¸)Ð4DÐDr%   c                 óˆ   — 	 t        j                  «       } t        | «      xs dj	                  dd«      S # t        $ r Y yw xY w)zbThis machine's hostname as an author-id origin, cleaned like any author field. Empty when unknown.r   r.   )ÚsocketÚgethostnameÚ	Exceptionr$   Úreplace)Úhosts    r   Úlocal_originr8   0   sH   € ðÜ×!Ñ!Ó#ˆô ˜ÓÒ# ×,Ñ,¨S°"Ó5Ð5øô ò Ùðús   ‚5 µ	AÁ Ac                ó  — 	 t        | t        t        f«      rt        j                  | «      } t        | t
        «      syt        | j                  d«      «      t        | j                  d«      «      t        | j                  d«      «      dœ}|d   €|d   €yt        | j                  d«      «      }|rG|d   rB|d   j                  d«      r.d|d   vr't        t        |d   t        d«      d |«      «      |d<   |S # t        $ r Y yw xY w)	aØ  Normalize a dict or JSON string into ``{"id", "name", "is_bot"}``; None for anything else or without id and name.
    The id is whatever the transport knows the sender by: ``bot:<profile>`` on a bot-mode delivery inside one
    install, ``bot:<connection>/<profile>`` when the Desktop relayed it from another machine,
    ``bot:<hostname>/<profile>`` on a peer dm, the platform user id elsewhere.
    An ``origin`` field qualifies a bare ``bot:<profile>`` id the same way.NÚidÚnameÚis_bot)r:   r;   r<   r0   r-   r.   )r   r   ÚbytesÚjsonÚloadsr   r$   Úgetr+   Ú
startswithr1   Úlenr5   )ÚrawÚauthorr0   s      r   Úparse_turn_authorrE   :   sö   € ðÜ�cœC¤˜<Ô(Ü—*‘*˜S“/ˆCÜ˜#œwÔ'Øä˜cŸg™g d›mÓ,Ü §¡¨£Ó0Ü §¡¨Ó 1Ó2ñ
ˆð
 �$‰<Ð F¨6¡NÐ$:ØÜ˜SŸW™W XÓ.Ó/ˆÙ�f˜T’l v¨d¡|×'>Ñ'>¸vÔ'FÈ3ÐV\Ð]aÑVbÑKbÜ&¤}°V¸D±\Ä#ÀfÃ+À,Ð5OÐQWÓ'XÓYˆF�4‰LØˆøÜò Ùðús   ‚;C< ¾AC< ÂA$C< Ã<	DÄDc                ó>   — t        | j                  t        «      «      S )zDThe author the dispatcher placed in ``HERMES_TURN_AUTHOR``, or None.)rE   r@   ÚTURN_AUTHOR_ENV©Úenvirons    r   Úturn_author_from_envrJ   T   s   € ä˜WŸ[™[¬Ó9Ó:Ð:r%   c                ó@   — t        | j                  t        d«      «      S )zaRead and remove ``HERMES_TURN_AUTHOR`` so subprocesses started during the turn do not inherit it.N)rE   ÚpoprG   rH   s    r   Útake_turn_author_from_envrM   Y   s   € ä˜WŸ[™[¬¸$Ó?Ó@Ð@r%   c                ó<   — t         t        j                  | d¬«      iS )z=The environment entry a dispatcher merges into a child's env.)ú,ú:)Ú
separators)rG   r>   Údumps©rD   s    r   Úturn_author_envrT   ^   s   € äœTŸZ™Z¨¸:ÔFÐGÐGr%   c                óx   — t        | t        «      r"| j                  d«      r| j                  d«      syd| d   › �S )z_``a2a:<bot id>``, the shared name for a bot author's turns. None for a human or an id-less bot.r<   r:   Nza2a:)r   Údictr@   rS   s    r   Úa2a_keyrW   c   s7   € ä�fœdÔ#¨6¯:©:°hÔ+?ÀvÇzÁzÐRVÔGWØØ�&˜‘,�Ð Ð r%   )r"   r   ÚreturnúOptional[str])r"   r   rX   r)   r   )r/   r   r0   rY   rX   r   )rX   r   )rC   r   rX   úOptional[Dict[str, Any]])rI   zMapping[str, str]rX   rZ   )rI   zMutableMapping[str, str]rX   rZ   )rD   zDict[str, Any]rX   zDict[str, str])rD   rZ   rX   rY   )Ú__doc__Ú
__future__r   r>   Úosr3   r   Útypingr   r   r   r   r   rG   r!   Ú	frozensetr   r(   r$   r+   r1   r8   rE   rI   rJ   rM   rT   rW   © r%   r   ú<module>ra      sˆ   ðñõ #ã Û 	Û Û ß ?Õ ?à&€à€ñ  Ò 8Ó9Ð Ù
Ò(Ó
)€ó!ó:ôEó6óð4 79·j±jô ;ð
 CEÇ*Á*ô Aó
Hô
!r%   