Ë
    tPêj�  ã            
       óf  — U d Z ddlZddlZddlmZmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZmZmZ ddlmZ d	Zeed
<   dZeed<   dZeed<   dZeed<    e	j.                  «       dz  dz  Zeed<    e	j.                  «       dz  dz  Zeed<    e	j.                  «       dz  dz  dz  Zeed<    edd¬«       G d„ d«      «       Z eedd«       eedd «      fZeed!<    eeeef   «      Zeed"<    G d#„ d$e «      Z!d%e	d&eeef   fd'„Z"d(eeef   d)ed*ed&eeef   fd+„Z#d)ed&efd,„Z$d)ed-e	d.ee   d&dfd/„Z%d0Z&y)1aX  Shared handling of Claude Code's ~/.claude/settings.json.

`lite up` patches this file temporarily and restores it on exit; `lite login
--config-claude` patches it persistently. Both need the same merge and the same
apiKeyHelper command, and `up` already imports from `auth`, so the shared parts
live here rather than in either command module.
é    N)ÚMappingÚSequence)Ú	dataclass)ÚPath)ÚFinal)Ú	JsonValueÚTypeAdapterÚValidationError)Úwrite_private_jsonÚenvÚENV_KEYÚapiKeyHelperÚAPI_KEY_HELPER_KEYÚANTHROPIC_BASE_URLÚANTHROPIC_BASE_URL_KEYÚANTHROPIC_API_KEYÚANTHROPIC_API_KEY_KEYz.claudezsettings.jsonÚCLAUDE_SETTINGS_PATHz.litellmzclaude_settings_backup.jsonÚBACKUP_PATHÚ
autorouterÚAUTOROUTE_BACKUP_PATHT)ÚfrozenÚslotsc                   ó0   — e Zd ZU dZeed<   eed<   eed<   y)ÚSettingsFileOwnerzWA command that takes temporary ownership of CLAUDE_SETTINGS_PATH and restores it later.Úbackup_pathÚstart_commandÚstop_commandN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__Ústr© ó    úv/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/proxy/client/cli/commands/claude_settings.pyr   r      s   … áaàÓØÓØÔr&   r   zlite upz	lite downzlite autoroute upzlite autoroute downÚSETTINGS_FILE_OWNERSÚ_SETTINGS_ADAPTERc                   ó   — e Zd ZdZy)ÚClaudeSettingsErrorzURaised for any user-actionable failure while reading or writing Claude Code settings.N)r   r    r!   r"   r%   r&   r'   r+   r+   /   s   „ Ú_r&   r+   ÚpathÚreturnc                 ó  — 	 | j                  «       r| j                  «       nd}|j	                  «       si S 	 t
        j                  |«      S # t        $ r}t        d| › d|› �«      |‚d }~ww xY w# t        $ r t        | › d�«      ‚w xY w)Nr&   zCould not read ú: zM contains invalid JSON (or its root is not an object); cannot proceed safely.)ÚexistsÚ
read_bytesÚOSErrorr+   Ústripr)   Úvalidate_jsonr
   )r,   ÚcontentÚes      r'   Úload_json_or_emptyr7   3   sš   € ðHØ.2¯k©k¬m˜Ÿ™Ô*Àˆð �=‰=Œ?Øˆ	ð
Ü ×.Ñ.¨wÓ7Ð7øô ò HÜ! O°D°6¸¸A¸3Ð"?Ó@ÀaÐGûðHûô ò 
Ü!ØˆfÐaÐbó
ð 	
ð
ús"   ‚"A ·A/ Á	A,ÁA'Á'A,Á/BÚsettingsÚbase_urlÚapi_key_helperc                 ó  — | j                  t        i «      }t        |t        «      r|ni }i |j	                  «       D ��ci c]  \  }}|t
        k7  sŒ||“Œ c}}¥t        |j                  d«      i¥}i | ¥t        |t        |i¥S c c}}w )aY  Return a new settings dict wired to route Claude Code through the proxy.

    Only env.ANTHROPIC_BASE_URL and the top-level apiKeyHelper are overridden; a
    stray env.ANTHROPIC_API_KEY is dropped so it cannot outrank the helper-issued
    token (same reasoning as build_agent_env in agents.py). Every other key is
    preserved untouched.
    ú/)	Úgetr   Ú
isinstanceÚdictÚitemsr   r   Úrstripr   )r8   r9   r:   Úraw_envÚbase_envÚkeyÚvaluer   s           r'   Úmerge_claude_settingsrF   B   sˆ   € ð —\‘\¤'¨2Ó.€GÜ!+¨G´TÔ!:‘gÀ€HðØ(0¯©Ó(8×
Y™*˜#˜u¸CÔCXÓ<Xˆ3�‰:Ó
Yðä §¡°Ó 4ñ€Cð JˆhÐIœ Ô&8¸.ÑIÐIùó Zs   ¿BÁBc                 ó¢   — t        j                  d«      }|€t        d«      ‚t        j                  |«      › dt        j                  | «      › d�S )aÐ  Build the shell command Claude Code should run for its apiKeyHelper.

    Resolves `lite` to an absolute path so the helper works regardless of the
    PATH visible to whatever subprocess Claude Code spawns it from. Passing
    --base-url explicitly (rather than relying on the bare invocation Claude
    Code would otherwise use) makes `print-token` enforce that the cached
    token was actually issued for this proxy -- without it, a token minted
    for a different, previously-logged-into proxy would be handed to
    whichever server the settings currently point at.

    --base-url belongs to the top-level `lite` group, so it has to precede the
    subcommand; click rejects it outright after `print-token`.
    Úlitez\Could not find `lite` on your PATH. Claude Code's apiKeyHelper needs an absolute path to it.z --base-url z auth print-token)ÚshutilÚwhichr+   ÚshlexÚquote)r9   Ú	lite_paths     r'   Úresolve_api_key_helperrN   U   sR   € ô —|‘| FÓ+€IØÐÜ!Øjó
ð 	
ô �k‰k˜)Ó$Ð% \´%·+±+¸hÓ2GÐ1HÐHYÐZÐZr&   Úsettings_pathÚownersc                 óH  — |D ]R  }|j                   j                  «       sŒt        d|j                  › d|› d|j                   › d|j                  › d�	«      ‚ | j                  d«      }t        |«      }t        |«      }|j                  t        «      }|�%t        |t        «      st        |› dt        › d	�«      ‚t        |||«      }|j                  «       r|j                  «       n|}		 t        t!        |	«      |«       y# t"        $ r}
t        d
|	› d|
› �«      |
‚d}
~
ww xY w)zÓPersistently point Claude Code at base_url, preserving every unrelated setting.

    Refuses while any owner holds a backup: each restores its backup when it
    stops, which would silently undo this write.
    ú`z` is currently managing z (backup at z*) and will restore it when it stops. Run `z` first, then retry.r<   Nz has a non-object "z@" value, which this would discard. Fix or remove it, then retry.zCould not write r/   )r   r0   r+   r   r   rA   rN   r7   r=   r   r>   r?   rF   Ú
is_symlinkÚresolver   r$   r2   )r9   rO   rP   ÚownerÚnormalized_base_urlr:   ÚexistingrB   ÚmergedÚtargetr6   s              r'   Úwrite_claude_settingsrZ   k   sD  € ð ò ˆØ×Ñ×#Ñ#Õ%Ü%Ø�E×'Ñ'Ð(Ð(@ÀÀÈ|Ø×$Ñ$Ð%ð &Ø×*Ñ*Ð+Ð+?ðAóð ðð "*§¡°Ó!5ÐÜ2Ð3FÓG€NÜ(¨Ó7€HØ—\‘\¤'Ó*€GØÐ¤:¨g´tÔ#<Ü!ØˆoÐ0´°	ð :,ð ,ó
ð 	
ô *¨(Ð4GÈÓX€Fð 0=×/GÑ/GÔ/I�M×)Ñ)Ô+È}€FðKÜœ3˜v›;¨Õ/øÜò KÜ!Ð$4°V°H¸B¸q¸cÐ"BÓCÈÐJûðKús   Ã+D Ä	D!Ä
DÄD!)r   r   r   r   r   r   r   r(   r+   r   r7   rF   rN   rZ   )'r"   rK   rI   Úcollections.abcr   r   Údataclassesr   Úpathlibr   Útypingr   Úpydanticr   r	   r
   Ú'litellm.litellm_core_utils.private_jsonr   r   r#   r   r   r   Úhomer   r   r   r   r(   r?   r$   r)   Ú	Exceptionr+   r7   rF   rN   rZ   Ú__all__r%   r&   r'   ú<module>rd      sÇ  ðòó Û ß -Ý !Ý Ý ç <Ñ <å Fà€ˆÓ Ø*Ð �EÓ *Ø 4Ð ˜Ó 4Ø2Ð �uÓ 2à'˜dŸi™i›k¨IÑ5¸ÑGÐ �eÓ GØ�T—Y‘Y“[ :Ñ-Ð0MÑM€ˆUÓ MØ(˜tŸy™y›{¨ZÑ7¸,ÑFÐIfÑfÐ �uÓ fñ �$˜dÔ#÷ð ó $ðñ �k 9¨kÓ:ÙÐ+Ð-@ÐBWÓXðÐ �eó ñ
 ' t¨C°¨NÑ';Ó<Ð �5Ó <ô`˜)ô `ð
˜Tð 
 d¨3°	¨>Ñ&:ó 
ðJØ�c˜9�nÑ%ðJØ14ðJØFIðJà	ˆ#ˆyˆ.ÑóJð&[ Sð [¨Só [ð,K Cð K¸ð KÀhÐO`ÑFað KÐfjó KðB�r&   