ó
    ±"³j!  ã                  ó  • S SK Jr  S SKrS SKJrJrJr  S SKJr  S SK	J
r
  S SKJr  S SKJr  S SKJr  S S	KJrJrJrJrJr  S S
KJr  SSKJrJr  SSKJr  SSKJr  SSK J!r!  Sr"      SS jr#\
" SS9 " S S\!5      5       r$g)é    )ÚannotationsN)ÚCallableÚ	GeneratorÚSequence)Úcontextmanager)Ú	dataclass)ÚPriceCalculation)ÚAttributeValue)Úbest_effort_price)ÚGEN_AI_PROVIDER_NAME_ATTRIBUTEÚGEN_AI_REQUEST_MODEL_ATTRIBUTEÚrecord_uncaught_errorsÚserialize_anyÚserver_attributes)ÚInstrumentationSettingsé   )ÚImageGenerationInputÚImageGenerationModel)ÚImageGenerationResult)ÚImageGenerationSettings)ÚWrapperImageGenerationModel)Ú InstrumentedImageGenerationModelÚ!instrument_image_generation_modelc                ón   • U(       a-  [        U [        5      (       d  USL a
  [        5       n[        XS9n U $ )z@Instrument an image generation model with OpenTelemetry/logfire.T©Úoptions)Ú
isinstancer   r   )ÚmodelÚ
instruments     Ú\/home/mande/repo/quber/.venv/lib/python3.13/site-packages/pydantic_ai/images/instrumented.pyr   r      s3   € ö œ* UÔ,L×MÑMØ˜ÒÜ0Ó2ˆJä0°ÑKˆà€Ló    F)Úinitc                  óú   ^ • \ rS rSr% SrS\S'    SS.     SU 4S jjjrSSS.       SU 4S	 jjjr\        SS
 j5       r	              SS jr
\        SS j5       r\SS j5       rSrU =r$ )r   é*   zSImage generation model which wraps another model for OpenTelemetry instrumentation.r   Úinstrumentation_settingsNr   c               óT   >• [         TU ]  U5        U=(       d
    [        5       U l        g ©N)ÚsuperÚ__init__r   r%   )ÚselfÚwrappedr   Ú	__class__s      €r    r)   Ú)InstrumentedImageGenerationModel.__init__1   s#   ø€ ô 	‰Ñ˜Ô!Ø(/×(LÔ3JÓ3LˆÕ%r!   ©ÚimagesÚsettingsc             ƒ  óÐ   >#   • U R                  XUS9u  pnU R                  XU5       n[        TU ]  XUS9I S h  v•N nU" U5        UsS S S 5        $  N! , (       d  f       g = f7f)Nr.   )Úprepare_generateÚ_instrumentr(   Úgenerate)r*   Úpromptr/   r0   ÚfinishÚresultr,   s         €r    r4   Ú)InstrumentedImageGenerationModel.generate:   si   øé € ð $(×#8Ñ#8¸ÐYaÐ#8Ð#bÑ ˆ˜Ø×Ñ˜f¨hÔ7¸6Ü ™7Ñ+¨FÈHÐ+ÐU×UˆFÙ�6ŒNØ÷ 8Ñ7ÙU÷ 8Õ7üs,   ƒ&A&©A»A¼AÁ	
A&ÁAÁ
A#ÁA&c              #  óø  ^ ^
^^^#   • SmT ST R                    3nTSS.T R                  T R                  5      E[        U5      [        U5      S.Em
T R                  R
                  nS nU(       a\  U(       aU  UR                  5        VVs0 s H  u  pxUS;  d  M  Xx_M     nnnU(       a"  [        R                  " [        U5      5      T
S'   T R                  R                  (       a  UT
S'   [        R                  " S	S
S0S
S0S
S0S.U(       a  SS
S	00O0 ET R                  R                  (       a  SS
S00O0 ES.5      T
S'   S m T R                  R                  R                  UT
SSS9 m[        TT R                  R                  S9   SU
UUU U4S jjn	U	v •  S S S 5        S S S 5        T(       a  T" 5         g g s  snnf ! , (       d  f       N,= f! , (       d  f       N5= f! T(       a  T" 5         f f = f7f)NÚimage_generationÚ Úimage)úgen_ai.operation.namezgen_ai.output.type)Úprompt_lengthÚinput_image_count)Úextra_headersÚ
extra_bodyÚimage_generation_settingsr5   ÚobjectÚtypeÚinteger)r>   r?   Úimage_countÚstring)rD   Ú
propertieszlogfire.json_schemaF)Ú
attributesÚrecord_exceptionÚset_status_on_exception)Úinclude_contentc                óŒ  >• [        T[           5      n[        T[           5      nU R                  =(       d    Un[	        U R
                  U R                  U R                  U R                  U R                  S9nTR                  TXX0U5      mT	R                  5       (       d  g T	R                  TR                  XU5      5        g )N)Ú
model_nameÚprovider_api_urlÚprovider_nameÚgenai_request_timestamp)Ústrr   r   rN   r   ÚusageÚprovider_urlrP   Ú	timestampÚ_metric_recorderÚis_recordingÚset_attributesÚ_response_attributes)
r7   rP   Úrequest_modelÚresponse_modelÚprice_calculationrI   Ú	operationÚrecord_metricsr*   Úspans
        €€€€€r    r6   Ú<InstrumentedImageGenerationModel._instrument.<locals>.finish{   sµ   ø€ Ü$'¨
Ô3QÑ(RÓ$S�MÜ$'¨
Ô3QÑ(RÓ$S�MØ%+×%6Ñ%6×%G¸-�NÜ(9ØŸ™Ø#)×#4Ñ#4Ø)/×)<Ñ)<Ø&,×&:Ñ&:Ø06×0@Ñ0@ñ)Ð%ð &*×%:Ñ%:Ø! =ÀÐYjó&�Nð  ×,Ñ,×.Ñ.Øà×'Ñ'¨×(AÑ(AÀ&ÐZkÓ(lÕmr!   )r7   r   )rN   Úmodel_attributesr+   Úlenr%   Ú include_model_request_parametersÚitemsÚjsonÚdumpsr   rL   ÚtracerÚstart_as_current_spanr   )r*   r5   r/   r0   Ú	span_nameÚinclude_settingsÚrecorded_settingsÚkeyÚvaluer6   rI   r]   r^   r_   s   `         @@@@r    r3   Ú,InstrumentedImageGenerationModel._instrumentG   s  üé € ð 'ˆ	Ø �k  4§?¡?Ð"3Ð4ˆ	ð &/Ø")ñ1
ð ×#Ñ# D§L¡LÓ1ð1
ô ! ›[Ü!$ V£ò1
ˆ
ð  ×8Ñ8×YÑYÐØ6:ÐÞÖ(à-5¯^©^Ô-=ô!Ú-=™z˜sÀÐLkÑAk“
�’
Ñ-=ð ñ !ö !Ü:>¿*º*Ä]ÐSdÓEeÓ:f�
Ð6Ñ7à×(Ñ(×8×8Ø#)ˆJ�xÑ ä,0¯JªJà à&,¨iÐ%8Ø*0°)Ð)<Ø$*¨IÐ#6ñö M^Ð3°f¸hÐ5GÑHÐceð	ð
 :>×9VÑ9V×9f×9f˜ 6¨8Ð"4Ñ5Ðlnðñ	ó-
ˆ
Ð(Ñ)ð 59ˆð 	!à×-Ñ-×4Ñ4×JÑJØ¨*ÀuÐfkð Kñ àÜ& t¸T×=ZÑ=Z×=jÑ=jÓk÷nó nð* ’÷1 l÷ö: ÙÕ ð ùóm!÷8 lÕkú÷õ ûö: ÙÕ ð üsa   ‡BG:ÂF>ÂF>Â!B&G:Å&G& Å.GÆGÆGÆ&G& Æ.G:Ç
G	ÇGÇ
G#ÇG& Ç&G7Ç7G:c                ó0   ^ ^^^^^^• SUUUUUUU 4S jjnU$ )Nc                 ó  >• [         TST[        TST0n TR                  R                  (       a@  TR                  R
                  R                  TR                  R                  0 U ESS0E5        TR                  R                  (       a@  TR                  R
                  R                  TR                  R                  0 U ESS0E5        Tb:  TR                  R                  R                  [        TR                  5      U 5        g g )Nr=   úgen_ai.response.modelzgen_ai.token.typeÚinputÚoutput)r   r   rS   Úinput_tokensr%   Útokens_histogramÚrecordÚoutput_tokensÚcost_histogramÚfloatÚtotal_price)Úmetric_attributesr]   r\   rP   rZ   r[   r7   r*   s    €€€€€€€r    r^   ÚIInstrumentedImageGenerationModel._metric_recorder.<locals>.record_metricsž   sæ   ø€ ä.°Ø'¨Ü.°Ø'¨ð	!Ðð �|‰|×(×(Ø×-Ñ-×>Ñ>×EÑEØ—L‘L×-Ñ-ØGÐ(ÐGÐ*=¸wÑGôð �|‰|×)×)Ø×-Ñ-×>Ñ>×EÑEØ—L‘L×.Ñ.ØHÐ(ÐHÐ*=¸xÑHôð !Ñ,Ø×-Ñ-×<Ñ<×CÑCÜÐ+×7Ñ7Ó8Ø%õð -r!   )ÚreturnÚNone© )r*   r]   rP   rZ   r[   r7   r\   r^   s   ``````` r    rV   Ú1InstrumentedImageGenerationModel._metric_recorder•   s   þ€ ÷	õ 	ð. Ðr!   c                ó¤  • 0 U R                   R                  5       EU[        U R                  5      S.En[	        U R                  5       HG  u  pESU 3nUR
                  (       a  UR
                  X6 S3'   UR                  R                  X6 S3'   MI     U(       a  [        UR                  5      US'   U R                  b  U R                  US'   U$ )N)rq   rF   zimage.z.output_formatz.media_typezoperation.costzgen_ai.response.id)rS   Úopentelemetry_attributesrb   r/   Ú	enumerateÚoutput_formatÚcontentÚ
media_typery   rz   Úprovider_response_id)r7   r[   r\   rI   Úimage_indexr<   Úprefixs          r    rY   Ú5InstrumentedImageGenerationModel._response_attributes·   sÌ   € ð1
Ø�l‰l×3Ñ3Ó5ð1
à%3Ü˜vŸ}™}Ó-ò1
ˆ
ô #,¨F¯M©MÖ":ÑˆKØ˜k˜]Ð+ˆFØ×"×"Ø8=×8KÑ8K�
˜X ^Ð4Ñ5Ø16·±×1IÑ1IˆJ˜ Ð-Ó.ñ	 #;ö Ü+0Ð1B×1NÑ1NÓ+OˆJÐ'Ñ(à×&Ñ&Ñ2Ø/5×/JÑ/JˆJÐ+Ñ,àÐr!   c                óp   • [         U R                  [        U R                  0[	        U R
                  5      E$ r'   )r   Úsystemr   rN   r   Úbase_url)r   s    r    ra   Ú1InstrumentedImageGenerationModel.model_attributesÏ   s5   € ô +¨E¯L©LÜ*¨E×,<Ñ,<ð
ô   §¡Ó/ð
ð 	
r!   )r%   )r+   zImageGenerationModel | strr   zInstrumentationSettings | Noner}   r~   )r5   rR   r/   z%Sequence[ImageGenerationInput] | Noner0   úImageGenerationSettings | Noner}   r   )r5   rR   r/   zSequence[ImageGenerationInput]r0   r�   r}   z2Generator[Callable[[ImageGenerationResult], None]])r]   rR   rP   rR   rZ   rR   r[   rR   r7   r   r\   úPriceCalculation | Noner}   zCallable[[], None])r7   r   r[   rR   r\   r�   r}   údict[str, AttributeValue])r   r   r}   r‘   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ú__annotations__r)   r4   r   r3   rV   ÚstaticmethodrY   ra   Ú__static_attributes__Ú__classcell__)r,   s   @r    r   r   *   sJ  ø‡ á]à5Ó5Ø2ð 37ñ	Mà+ðMð 0ð	Mð
 
÷Mð Mð 9=Ø37ñàðð 6ð	ð
 1ðð 
÷ð ð ðK!àðK!ð /ðK!ð 1ð	K!ð
 
<óK!ó ðK!ðZ àð ð ð ð ð	 ð
 ð ð &ð ð 3ð ð 
ô ðD ðØ%ðØ7:ðØOfðà	"óó ðð. ó
ó ö
r!   r   )r   r   r   zInstrumentationSettings | boolr}   r   )%Ú
__future__r   re   Úcollections.abcr   r   r   Ú
contextlibr   Údataclassesr   Úgenai_prices.typesr	   Úopentelemetry.util.typesr
   Úpydantic_ai._genai_pricesr   Úpydantic_ai._instrumentationr   r   r   r   r   Úpydantic_ai.models.instrumentedr   Úbaser   r   r7   r   r0   r   Úwrapperr   Ú__all__r   r   r   r!   r    Ú<module>r§      s‚   ðÝ "ã ß 9Ñ 9Ý %Ý !å /Ý 3å 7÷õ õ Dç <Ý )Ý -Ý 0à
Q€ð
Øð
Ø-Kð
àô
ñ �Ñôj
Ð'Bó j
ó ñj
r!   