ó
    ±"³j�  ã                  óÞ   • S SK Jr  S SKJrJrJr  S SKJrJrJ	r	  S SK
JrJrJr  S SKJr  S SKJrJr  SSKJr  \(       a  S S	KJr  S S
KJr  \ " S S\\   5      5       rSS jrSS jrSS jrg)é    )Úannotations)ÚKW_ONLYÚ	dataclassÚreplace)ÚTYPE_CHECKINGÚAnyÚcast)ÚModelMessageÚModelRequestÚSystemPromptPart)ÚModel)Ú
AgentDepsTÚ
RunContexté   )ÚAbstractCapability)ÚModelRequestContext)ÚModelSettingsc                  óZ   • \ rS rSr% SrSrS\S'    S\S'   SrS	\S
'          SS jrSr	g)ÚReinjectSystemPrompté   uÖ  Capability that reinjects the agent's configured `system_prompt` when missing from history.

Ensures the agent's configured `system_prompt` is present at the head of the first
`ModelRequest` on every model request.

Intended for callers that reconstruct a `message_history` from a source that doesn't
round-trip system prompts â€” UI frontends, database persistence layers, conversation
compaction pipelines. By default, if any `SystemPromptPart` is already present anywhere
in the history (for example, preserved from a prior run or handed off from another
agent), this capability leaves the messages untouched so that existing system prompts
remain authoritative. Set `replace_existing=True` to instead strip any existing
`SystemPromptPart`s before prepending the agent's configured prompt â€” useful when the
history comes from an untrusted source (such as a UI frontend) and the server's prompt
must win.

The UI adapters automatically add this capability in `manage_system_prompt='server'` mode
with `replace_existing=True`. Add it explicitly with
`Agent(..., capabilities=[ReinjectSystemPrompt()])` or per-run via the `capabilities=`
argument on [`Agent.run`][pydantic_ai.agent.AbstractAgent.run] to get the same behavior
anywhere.
FÚboolÚreplace_existingr   Ú_Úreinject_system_promptz
str | NoneÚidc              ƒ  óØ  #   • UR                   nU R                  (       a  [        U5        O[        U5      (       a  U$ UR                  c  U$ UR
                  n[        U[        5      (       a  [        SU5      OS nUR                  R                  UR                  UUUR                  UR                  [        SUR                  5      S9I S h  v•N nU(       a  [        X65        U$  N7f)Nz
Model[Any]zModelSettings | None)ÚdepsÚmodelÚmessage_historyÚpromptÚusageÚmodel_settings)Úmessagesr   Ú_strip_system_promptsÚ_has_system_promptÚagentr   Ú
isinstancer   r	   Úsystem_prompt_partsr   r    r!   r"   Ú_prepend_to_first_request)ÚselfÚctxÚrequest_contextr#   Ú	ctx_modelr   Ú	sys_partss          Úl/home/mande/repo/quber/.venv/lib/python3.13/site-packages/pydantic_ai/capabilities/reinject_system_prompt.pyÚbefore_model_requestÚ)ReinjectSystemPrompt.before_model_request7   sÎ   é € ð
 #×+Ñ+ˆØ× × Ü! (Õ+Ü ×)Ñ)Ø"Ð"à�9‰9Ñà"Ð"ð —I‘Iˆ	Ü1;¸IÄu×1MÑ1M”�\ 9Ô-ÐSWˆØŸ)™)×7Ñ7Ø—‘ØØ$Ø—:‘:Ø—)‘)ô  Ð 6¸×8JÑ8JÓKð 8ð 	
÷ 	
ˆ	ö Ü% hÔ:ØÐñ	
ùs   ‚CC*ÃC(ÃC*© N)r+   zRunContext[AgentDepsT]r,   r   Úreturnr   )
Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   Ú__annotations__r   r0   Ú__static_attributes__r2   ó    r/   r   r      sP   ‡ ñð, #Ð�dÓ"ð2ð ƒJà-€Bˆ
Ó-ððà#ðð -ðð 
÷	r;   r   c                óŠ   • U  H=  n[        U[        5      (       d  M  [        S UR                   5       5      (       d  M=    g   g)Nc              3  óB   #   • U  H  n[        U[        5      v •  M     g 7f©N)r'   r   )Ú.0Úps     r/   Ú	<genexpr>Ú%_has_system_prompt.<locals>.<genexpr>[   s   é € Ð0dÒZcÐUV´¸AÔ?O×1PÐ1PÒZcùs   ‚TF)r'   r   ÚanyÚparts)r#   Úmsgs     r/   r%   r%   Y   s8   € ÛˆÜ�cœ<×(Ó(¬SÑ0dÐZ]×ZcÒZcÓ0d×-dÓ-dÙñ ð r;   c                óF  • / nU  H�  n[        U[        5      (       ag  UR                   Vs/ s H  n[        U[        5      (       a  M  UPM     nnU(       d  MT  [	        U5      [	        UR                  5      :w  a	  [        X$S9nUR                  U5        M’     XS S & g s  snf )N©rD   )r'   r   rD   r   Úlenr   Úappend)r#   ÚkeptrE   r@   Úfiltered_partss        r/   r$   r$   `   s€   € Ø!€DÛˆÜ�cœ<×(Ñ(Ø),¯ªÓZª A¼*ÀQÔHX×:YŸa©ˆNÐZÞ!ÙÜ�>Ó"¤c¨#¯)©)£nÓ4Ü˜cÑ8�Ø�‰�CÖñ ð ‰Q�Kùò [s   «BÁBc                ór   • [        S [        U 5       5       5      u  p#[        U/ UQUR                  QS9X'   g )Nc              3  óZ   #   • U  H!  u  p[        U[        5      (       d  M  X4v •  M#     g 7fr>   )r'   r   )r?   ÚiÚms      r/   rA   Ú,_prepend_to_first_request.<locals>.<genexpr>n   s#   é € ÐbÒ/B¡t qÄjÐQRÔT`×Fa›F˜Q�FÒ/Bùs   ‚+¡
+rG   )ÚnextÚ	enumerater   rD   )r#   r.   rN   Úfirst_requests       r/   r)   r)   m   s9   € ÜÑb¬y¸Ô/BÓbÓbÑ€AÜ˜-Ð/Q°Ð/Q¸]×=PÑ=PÐ/QÑR€H‚Kr;   N)r#   úlist[ModelMessage]r3   r   )r#   rT   r3   ÚNone)r#   rT   r.   zlist[SystemPromptPart]r3   rU   )Ú
__future__r   Údataclassesr   r   r   Útypingr   r   r	   Úpydantic_ai.messagesr
   r   r   Úpydantic_ai.modelsr   Úpydantic_ai.toolsr   r   Úabstractr   r   Úpydantic_ai.settingsr   r   r%   r$   r)   r2   r;   r/   Ú<module>r^      sa   ðÝ "ç 3Ñ 3ß +Ñ +ç MÑ MÝ $ß 4å (æÝ6Ý2ð ôDÐ-¨jÑ9ó Dó ðDôNô
õSr;   