Ë
    áß²jB  ã                  óÜ   — U d Z ddlmZ ddlZddlmZmZmZ ddlm	Z	  ej                  e«      Z e«       Zded<   dZdd	„Zdd
„Zdd„Zdd„Z	 	 	 	 	 	 	 	 	 	 dd„Z	 d	 	 	 	 	 	 	 	 	 	 	 dd„Zy)uC  Structured-output (``response_format``) capability for auxiliary requests.

Two sources decide whether an aux request may carry a ``response_format`` type up front:

* the provider profile's ``unsupported_response_formats`` (DeepSeek's native API implements only
  ``json_object`` â€” https://api-docs.deepseek.com/guides/json_mode â€” and answers ``json_schema`` with
  HTTP 400 "This response_format type is unavailable now"), also consulted when a ``custom`` route's
  base_url points at a profiled provider's host, and
* a process-level memo of (endpoint, model, type) triples that already rejected the type once; the
  recovery ladder records the triple when its retry without the field succeeded.

Either way the field is dropped before the first request instead of burning a guaranteed-fail
round-trip per call (#83390, #105191, #113064). Dropping â€” not downgrading to ``json_object`` â€” is the
same end state the rejection retry already produces: ``json_object`` needs the prompt to mention JSON
and some relays return empty content under it, so callers already tolerate prompt compliance.

The memo carries the model because capability is per model on aggregators (openrouter.ai, the Nous
Portal, api.openai.com host dozens of models with different structured-output support), and it is
fed only by rejections that name the *capability* â€” not by schema-validation 400s from providers that
do implement ``json_schema`` ("Invalid schema for response_format 'json_schema': additionalProperties
must be false"), which the ladder still retries once but which say nothing about the next schema.
é    )ÚannotationsN)ÚAnyÚDictÚOptional)Úurlparsezset[tuple[str, str, str]]Ú_REJECTED_ROUTES)
Úunavailablezdoes not supportzdoesn't supportznot supportedzunsupported parameterÚunsupported_parameterzunknown parameterzunrecognized request argumentzunrecognized parameterzextra inputs are not permittedc                ó²   — t        |xs d«      j                  xs dj                  «       xs+ t        | xs d«      j	                  «       j                  «       S )z•Endpoint host:port when known (a base_url override turns a named provider into ``custom``; local
    servers differ by port), else the provider name.Ú )r   ÚnetlocÚlowerÚstrÚstrip)ÚproviderÚbase_urls     úF/home/ubuntu/.hermes/hermes-agent/agent/auxiliary_structured_output.pyÚ
_route_keyr   *   sG   € ô �X’^ Ó$×+Ñ+Ò1¨r×8Ñ8Ó:Òa¼cÀ(Â.ÈbÓ>Q×>WÑ>WÓ>Y×>_Ñ>_Ó>aÐaó    c                óÞ   — | j                  d«      }t        |t        «      r|xs i j                  d«      nd }|€| j                  d«      }t        |t        «      r|j                  d«      S d S )NÚ
extra_bodyÚresponse_formatÚtype)ÚgetÚ
isinstanceÚdict)Úrequest_kwargsr   r   s      r   Ú_response_format_typer   0   sl   € Ø×#Ñ# LÓ1€JÜCMÈjÔZ^ÔC_�zÒ' R×,Ñ,Ð->Ô?Ðei€OØÐØ(×,Ñ,Ð->Ó?ˆÜ*4°_ÄdÔ*Kˆ?×Ñ˜vÓ&ÐUÐQUÐUr   c                ó  — 	 ddl m} t        | xs d«      j                  «       j	                  «       }|dk(  r|rddlm}  ||«      xs |} ||«      }|�t        t        |dd«      xs d«      S dS # t        $ r Y yw xY w)z¯The provider profile's declared unsupported types; a ``custom`` route whose base_url is a profiled
    provider's own host (``api.deepseek.com``) gets that provider's profile.r   )Úget_provider_profiler   Úcustom)Ú_infer_provider_from_url© Úunsupported_response_formats)
Ú	providersr    r   r   r   Úagent.model_metadatar"   Ú	ExceptionÚtupleÚgetattr)r   r   r    Únamer"   Úprofiles         r   Ú_profile_unsupported_formatsr,   8   s�   € ðÝ2Ü�8’>˜rÓ"×(Ñ(Ó*×0Ñ0Ó2ˆØ�8Ò¡ÝEÙ+¨HÓ5Ò=¸ˆDÙ& tÓ,ˆð QXÐPcŒ5”˜Ð"@À"ÓEÒKÈÓLÐkÐikÐkøô ò Ùðús   ‚AA2 Á2	A>Á=A>c                óx   ‡— t        | xs d«      j                  «       Šd‰v ryt        ˆfd„t        D «       «      S )z¢Whether a structured-output rejection speaks to the route/model's capability (memoisable) rather
    than to this request's schema (retry once, remember nothing).r   zinvalid schemaFc              3  ó&   •K  — | ]  }|‰v –— Œ
 y ­w©Nr#   )Ú.0ÚmarkerÚ	err_lowers     €r   ú	<genexpr>z*is_capability_rejection.<locals>.<genexpr>M   s   øè ø€ ÒO vˆv˜Ô"ÑOùs   ƒ)r   r   ÚanyÚ_CAPABILITY_REJECTION_MARKERS)Úerrorr2   s    @r   Úis_capability_rejectionr7   G   s9   ø€ ô �E’K˜RÓ ×&Ñ&Ó(€IØ˜9Ñ$ØÜÓOÔ1NÔOÓOÐOr   c                ó´   — t        |«      }|rKt        |«      r?t        j                  t	        | |«      t        |j                  d«      xs d«      |f«       yyy)uË   Record that this route's ``rejected_kwargs["model"]`` rejected the ``response_format`` type carried
    by *rejected_kwargs* â€” only when *error* names the capability, never for a schema-validation 400.Úmodelr   N)r   r7   r   Úaddr   r   r   )r   r   Úrejected_kwargsr6   Úformat_types        r   Ú$remember_structured_output_rejectionr=   P   sT   € ô
 (¨Ó8€KÙÔ.¨uÔ5Ü×Ñœj¨°8Ó<¼cÀ/×BUÑBUÐV]ÓB^ÒBdÐbdÓ>eÐgrÐsÕtð 6€{r   c                óœ  — | j                  d«      }t        |t        «      r|j                  d«      nd}|s| S |t        ||«      v xs" t	        ||«      t        |xs d«      |ft        v }|s| S t        j                  d|xs dt	        ||«      xs d|xs d|«       | j                  «       D ��	ci c]  \  }}	|dk7  sŒ||	“Œ c}	}S c c}	}w )	zq*extra_body* minus a ``response_format`` whose type this route+model is known to reject; unchanged
    otherwise.r   r   Nr   zAuxiliary %s: %s (%s) does not accept response_format %s; sending without it (schema enforcement degrades to prompt compliance)Úcallr   r9   )
r   r   r   r,   r   r   r   ÚloggerÚinfoÚitems)
r   r   r   r9   Útaskr   r<   Úknown_unsupportedÚkÚvs
             r   Ú#without_unsupported_response_formatrG   Z   sÝ   € ð !—n‘nÐ%6Ó7€OÜ1;¸OÌTÔ1R�/×%Ñ% fÔ-ÐX\€KÙØÐàÔ3°H¸hÓGÐGò 	_Ü�x Ó*¬C°²¸Ó,<¸kÐJÔN^Ð^ð ñ ØÐÜ
‡K�Kð	=àŠ�œ
 8¨XÓ6ÒD¸*ÀeÒFVÈwÐXcôð
 (×-Ñ-Ó/×J‘T�Q˜°1Ð8IÓ3IˆAˆq‰DÓJÐJùÓJs   Â1CÂ?C)r   úOptional[str]r   rH   Úreturnr   )r   úDict[str, Any]rI   rH   )r   rH   r   rH   rI   r(   )r6   zOptional[BaseException]rI   Úbool)
r   rH   r   rH   r;   rJ   r6   ÚBaseExceptionrI   ÚNoner/   )r   rJ   r   rH   r   rH   r9   rH   rC   rH   rI   rJ   )Ú__doc__Ú
__future__r   ÚloggingÚtypingr   r   r   Úurllib.parser   Ú	getLoggerÚ__name__r@   Úsetr   Ú__annotations__r5   r   r   r,   r7   r=   rG   r#   r   r   ú<module>rW      sÓ   ðòõ, #ã ß &Ñ &Ý !à	ˆ×	Ñ	˜8Ó	$€ñ /2«eÐ Ð+Ó 3ð!Ð óbóVólóPðuØðuØ'4ðuØGUðuØ^kðuà	óuð ðKØðKØ*7ðKØCPðKØYfðKà
ðKð ôKr   