ó
    ­[j  ã                   ó  • S r SSKrSSKrSSKJrJrJrJrJrJ	r	  SSK
Jr  SSKJr  S\\   S\\\4   4S	 jrS
\S\\\4   S\4S jrS\\   S\4S jrS\\   S\\\4   4S jrS\\\\\4   \\   4   S\\\4   4S jrg)z§
Schema utilities for the ADE SDK.

This module provides utility functions for converting Pydantic models to JSON schemas
that can be used with the ADE API endpoints.
é    N)ÚAnyÚDictÚTypeÚUnionÚMappingÚcast)Ú	BaseModelé   )ÚPYDANTIC_V1ÚmodelÚreturnc                 óà   • [         (       a@  [        R                  " U R                  5       5      nSU;   a  UR	                  S5      US'   U$ [        R                  " U R                  5       5      $ )af  Return a model's JSON schema, keyed the same way regardless of pydantic major.

Pydantic v2 exposes `model_json_schema()` and nests shared definitions under
`$defs`; pydantic v1 only has `.schema()` and nests them under `definitions`.
We normalize to `$defs` here so callers (and `_resolve_refs`) don't need to
know which pydantic major produced the schema.
Údefinitionsú$defs)r   ÚcopyÚdeepcopyÚschemaÚpopÚmodel_json_schema)r   r   s     Ú[/home/mande/repo/quber/.venv/lib/python3.13/site-packages/landingai_ade/lib/schema_utils.pyÚ_model_json_schemar      sX   € ÷ ‚{ô
 —’˜uŸ|™|›~Ó.ˆØ˜FÓ"Ø$Ÿj™j¨Ó7ˆF�7‰OØˆô �=Š=˜×0Ñ0Ó2Ó3Ð3ó    ÚobjÚdefsc           	      ó´  • [        U [        5      (       a†  SU ;   aQ  [        U S   [        5      (       a9  U S   R                  S5      S   n[	        [
        R                  " X   5      U5      $ U R                  5        VVs0 s H  u  p4U[	        XA5      _M     snn$ [        U [        5      (       a  U  Vs/ s H  n[	        XQ5      PM     sn$ U $ s  snnf s  snf )aW  
Resolve JSON Schema $refs to create a flat schema.

This function recursively resolves all $ref references in a JSON schema
by replacing them with their definitions from the $defs section.

Args:
    obj: The schema object (or part of it) to process
    defs: Dictionary of schema definitions

Returns:
    The schema with all $refs resolved
z$refÚ/éÿÿÿÿ)	Ú
isinstanceÚdictÚstrÚsplitÚ_resolve_refsr   r   ÚitemsÚlist)r   r   Úref_nameÚkÚvÚitems         r   r"   r"   (   sµ   € ô �#”t×ÑØ�S‹=œZ¨¨F©´S×9Ñ9Ø˜6‘{×(Ñ(¨Ó-¨bÑ1ˆHÜ ¤§¢¨t©~Ó!>ÀÓEÐEØ69·i±i´kÔB²k©d¨a�”= Ó)Ò)±kÒBÐBÜ	�Cœ×	Ñ	Ù69Ó:²c¨d”˜dÖ)±cÑ:Ð:Ø€Jùó Cùâ:s   Â CÂ5Cc                 óè   • [        U [        5      (       a  [        U [        5      (       d  [	        S5      e[        U 5      nUR                  S0 5      n[        X5      n[        R                  " U5      $ )aL  
Convert a Pydantic model to a JSON schema string.

This utility function takes a Pydantic BaseModel class and converts it to a
JSON schema string with all $refs resolved, suitable for use with the ADE API.

Args:
    model: A Pydantic BaseModel class defining the schema

Returns:
    JSON string representation of the schema

Raises:
    TypeError: If model is not a Pydantic BaseModel subclass

Example:
    >>> from pydantic import BaseModel, Field
    >>> from landingai_ade.lib.schema_utils import pydantic_to_json_schema
    >>>
    >>> class Person(BaseModel):
    ...     name: str = Field(description="Person's name")
    ...     age: int = Field(description="Person's age")
    >>> schema_json = pydantic_to_json_schema(Person)
    >>> # Now use schema_json with the SDK:
    >>> # client.extract(schema=schema_json, markdown="...")
ú+model must be a Pydantic BaseModel subclassr   )
r   ÚtypeÚ
issubclassr	   Ú	TypeErrorr   r   r"   ÚjsonÚdumps©r   r   r   s      r   Úpydantic_to_json_schemar1   @   s]   € ô< 	�5œ$×ÑÜ�uœi×(Ñ(äÐEÓFÐFä Ó&€FØ�:‰:�g˜rÓ"€DÜ˜6Ó(€FÜ�:Š:�fÓÐr   c                 óò   • [        U [        5      (       a  [        U [        5      (       d  [	        S5      e[        U 5      nUR                  S0 5      n[        [        [        [        4   [        X5      5      $ )zFLike `pydantic_to_json_schema` but returns a dict with $refs resolved.r*   r   )r   r+   r,   r	   r-   r   r   r   r   r    r   r"   r0   s      r   Úpydantic_to_schema_dictr3   i   s^   € ô 	�5œ$×ÑÜ�uœi×(Ñ(äÐEÓFÐFÜ Ó&€FØ�:‰:�g˜rÓ"€DÜ””Sœ#�X‘¤¨fÓ ;Ó<Ð<r   r   c                 ó¬  • [        U [        5      (       a   [        U [        5      (       a  [	        U 5      $ [        U [
        5      (       a  [        U 5      $ [        U [        5      (       aS  [        R                  " U 5      n[        U[        5      (       d  [        S5      e[        [        [        [        4   U5      $ [        S[        U 5      < 35      e)z¢Accept a pydantic model class, a dict, or a JSON string; return a JSON-Schema dict.

The V2 extract endpoint takes `schema` as a JSON object in the request body.
z+schema JSON string must decode to an objectzUnsupported schema type: )r   r+   r,   r	   r3   r   r   r    r.   Úloadsr-   r   r   r   )r   Úparseds     r   Úcoerce_schema_to_dictr7   u   s    € ô
 �&œ$×Ñ¤J¨v´y×$AÑ$AÜ& vÓ.Ð.Ü�&œ'×"Ñ"Ü�F‹|ÐÜ�&œ#×ÑÜ—j’j Ó(ˆÜ˜&¤$×'Ñ'ÜÐIÓJÐJÜ”Dœœc˜‘N FÓ+Ð+Ü
Ð/´°V³Ñ/?Ð@Ó
AÐAr   )Ú__doc__r   r.   Útypingr   r   r   r   r   r   Úpydanticr	   Ú_compatr   r    r   r"   r1   r3   r7   © r   r   Ú<module>r=      sÒ   ðñó Û ß 8× 8å å !ð4˜d 9™oð 4°$°s¸C°x±.ô 4ð.�sð  $ s¨C x¡.ð °Sô ð0& 4¨	¡?ð &°sô &ðR	= 4¨	¡?ð 	=°t¸CÀ¸H±~ô 	=ðB %¨¨W°S¸#°XÑ->ÀÀYÁÐ(OÑ"Pð BÐUYÐZ]Ð_bÐZbÑUcõ Br   