
    iQ                     x   S r SSKJr  SSK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  SSKJrJrJrJrJrJrJrJrJr  \" S	\
S
9r\" S\\
\4   S
9r\\S   \4   r\\\\S   \4   4   r " S S\
\\   5      r " S S\
\\   5      r " S S\
5      r\\\\\\\4   \ \   4   4   r!S\\\4   S\!4S jr"S1r#0 4S\S\\\4   S\\\\\4   \ \   4   4S jjr$ " S S\
\\   5      r% " S S\&5      r'S\!S\\(   4S  jr)S!\\\\\4   \ \   4   S\*\(\+4   4S" jr, " S# S$\
\\   5      r- " S% S&\
\\   5      r.g)'a  
Agent Data API Schema Definitions

This module provides typed wrappers around the raw LlamaCloud agent data API,
enabling type-safe interactions with agent-generated structured data.

The agent data API serves as a persistent storage system for structured data
produced by LlamaCloud agents (particularly extraction agents). It provides
CRUD operations, search capabilities, filtering, and aggregation functionality
for managing agent-generated data at scale.

Key Concepts:
- Agent Slug: Unique identifier for an agent instance
- Collection: Named grouping of data within an agent (defaults to "default"). Data within a collection should be of the same type.
- Agent Data: Individual structured data records with metadata and timestamps

Example Usage:
    ```python
    from pydantic import BaseModel

    class Person(BaseModel):
        name: str
        age: int

    client = AsyncAgentDataClient(
        client=async_llama_cloud,
        type=Person,
        collection="people",
        agent_url_id="my-extraction-agent-xyz"
    )

    # Create typed data
    person = Person(name="John", age=30)
    result = await client.create_agent_data(person)
    print(result.data.name)  # Type-safe access
    ```
    )datetimeN)
ExtractRun)	AgentData)AggregateGroup)	BaseModelFieldValidationError)	GenericListLiteralOptionalDictTypeTypeVarUnionAny
AgentDataT)bound
ExtractedT)erroracceptedrejectedpending_review)gtgteltlteeqincludesc                       \ rS rSr% Sr\" SS9r\\   \	S'   \" SS9r
\\	S'   \" SS9r\\   \	S	'   \" S
S9r\\	S'   \" SS9r\\   \	S'   \" SS9r\\   \	S'   \S\S\\   SS4S j5       rSrg)TypedAgentDataH   aM  
Type-safe wrapper for agent data records.

This class represents a single data record stored in the agent data API,
combining the structured data payload with metadata about when and where
it was created.

Attributes:
    id: Unique identifier for this data record
    agent_url_id: Identifier of the agent that created this data
    collection: Named collection within the agent (used for organization)
    data: The actual structured data payload (typed as AgentDataT)
    created_at: Timestamp when the record was first created
    updated_at: Timestamp when the record was last modified

Example:
    ```python
    # Access typed data
    person_data: TypedAgentData[Person] = await client.get_agent_data(id)
    print(person_data.data.name)  # Type-safe access to Person fields
    print(person_data.created_at)  # Access metadata
    ```
z&Unique identifier for this data recorddescriptionidz.Identifier of the agent that created this dataagent_url_idz7Named collection within the agent for data organization
collectionzThe structured data payloaddatazWhen this record was created
created_atz"When this record was last modified
updated_atraw_data	validatorreturnzTypedAgentData[AgentDataT]c           	          UR                  UR                  5      nU " UR                  UR                  UR                  UUR
                  UR                  S9$ )z
Convert raw API response to typed agent data.

Args:
    raw_data: Raw agent data from the API
    validator: Pydantic model class to validate the data field

Returns:
    TypedAgentData instance with validated data
)r%   r&   r'   r(   r)   r*   )model_validater(   r%   
agent_slugr'   r)   r*   )clsr+   r,   r(   s       e/home/james-whalen/.local/lib/python3.13/site-packages/llama_cloud_services/beta/agent_data/schema.pyfrom_rawTypedAgentData.from_rawn   sS     %33HMMB{{!,,******
 	
     N)__name__
__module____qualname____firstlineno____doc__r   r%   r   str__annotations__r&   r'   r(   r   r)   r   r*   classmethodr   r   r3   __static_attributes__r6   r5   r2   r!   r!   H   s    0 *RSBSDL#  !&M!J  )FGD*G%*7U%VJ"V%*8&J"  
 
-1*-=
	%
 
r5   r!   c                   n    \ rS rSr% Sr\" SS9r\\\	      \
S'   \" SS9r\\   \
S'   \" SS9r\\
S	'   S
rg)TypedAgentDataItems   a  
Paginated collection of agent data records.

This class represents a page of search results from the agent data API,
providing both the data records and pagination metadata.

Attributes:
    items: List of agent data records in this page
    total: Total number of records matching the query (only present if requested)
    has_more: Whether there are more records available beyond this page

Example:
    ```python
    # Search with pagination
    results = await client.search(
        page_size=10,
        include_total=True
    )

    for item in results.items:
        print(item.data.name)

    if results.has_more:
        # Load next page
        next_page = await client.search(
            page_size=10,
            offset=10
        )
    ```
z'List of agent data records in this pager#   itemszFTotal number of records matching the query (only present if requested)totalz9Whether there are more records available beyond this pagehas_morer6   N)r7   r8   r9   r:   r;   r   rC   r   r!   r   r=   rD   r   intrE   boolr?   r6   r5   r2   rA   rA      sW    > /4=/E4z*+  !\E8C=  OHd r5   rA   c                       \ rS rSr% Sr\" SSS9r\\   \	S'   \" SSS9r
\\   \	S'   \" SS	S9r\\   \	S
'   \" SSS9r\\   \	S'   \" SSS9r\\   \	S'   Srg)ExtractedFieldMetadata   zU
Metadata for an extracted data field, such as confidence, and citation information.
Nz_symbol for how the citation/confidence was derived: 'INFERRED FROM TEXT', 'VERBATIM EXTRACTION'r#   	reasoningzRThe confidence score for the field, combined with parsing confidence if applicable
confidencezCThe confidence score for the field based on the extracted text onlyextraction_confidencez*The page number that the field occurred onpage_numberz5The original text this field's value was derived frommatching_textr6   )r7   r8   r9   r:   r;   r   rK   r   r<   r=   rL   floatrM   rN   rF   rO   r?   r6   r5   r2   rI   rI      s      %u Ix}  #(h#J  .3Y.8E?  "'F"K#  $)K$M8C= r5   rI   field_metadatar-   c                     U R                  5        VVs0 s H  u  pU[        ;  d  M  U[        U5      _M     snn$ s  snnf )N)rC    _METADATA_FIELDS_SIBLING_TO_LEAF)_parse_extracted_field_metadata_recursive)rQ   kvs      r2   parse_extracted_field_metadatarW      sJ    
 #((**DA44 	84Q77*  s   <<rK   field_valueadditional_fieldsc           	         [        U [        5      (       a  U $ [        U [        5      (       Ga<  1 Skn[        UR	                  U R                  5       5      5      S:  a   0 U EUEn[        R                  U5      nSU ;   a}  [        U S   5      S:  ak  U S   S   nSU;   a5  [        US   [        R                  5      (       a  [        US   5      Ul
        SU;   a"  [        US   [        5      (       a
  US   Ul        U$ U R                  5        VVs0 s H  u  pgU[        ;   d  M  Xg_M     nnnU R                  5        VVs0 s H  u  pgU[        ;  d  M  U[!        Xq5      _M     snn$ [        U ["        5      (       a  U  Vs/ s H  n[!        U5      PM     sn$ [%        SU  S35      e! [         a     Nf = fs  snnf s  snnf s  snf )zX
Parse the extracted field metadata into a dictionary of field names to field metadata.
>   citationrL   rM   r   r[   pagerO   zInvalid field value: z0. Expected ExtractedFieldMetadata, dict, or list)
isinstancerI   dictlenintersectionkeysr/   numbersNumberrF   rN   r<   rO   r	   rC   rS   rT   list
ValueError)	rX   rY   indicator_fieldsmerged	validatedfirst_citationrU   rV   items	            r2   rT   rT      s    +566	K	&	& O,,[-=-=-?@AAE=K=+<=2AA&I	 ,[5L1MPQ1Q%0%<Q%?N/J&v.5 5 14N64J0K	-&.8Z&7> > 3A2Q	/  
 $))+
+44 AD+ 	 
 $))+
+88 OA8NN+
 	

 
K	&	&LWXKD9$?KXX#K=0`a
 	
 # 


 Ys1   BF* F:&F:G G G*
F76F7c                      \ rS rSr% Sr\" SS9r\\S'   \" SS9r	\\S'   \" SS9r
\\S	'   \" S
SS9r\\   \S'   \" \SS9r\\S'   \" S
SS9r\\   \S'   \" S
SS9r\\   \S'   \" S
SS9r\\   \S'   \" \SS9r\\\\4      \S'   \S0 S
S
S
S
4S\S	\S\S\\   S\\   S\\   S\\\\4      SS4S jj5       r\     S S\S\\   S\\   S\\   S\\   S	\S\\\\4      SS4S jj5       rSrg
)!ExtractedDatai  aq  
Wrapper for extracted data with workflow status tracking.

This class is designed for extraction workflows where data goes through
review and approval stages. It maintains both the original extracted data
and the current state after any modifications.

Attributes:
    original_data: The data as originally extracted from the source
    data: The current state of the data (may differ from original after edits)
    status: Current workflow status (in_review, accepted, rejected, error)
    confidence: Confidence scores for individual fields (if available)
    file_id: The llamacloud file ID of the file that was used to extract the data
    file_name: The name of the file that was used to extract the data
    file_hash: A content hash of the file that was used to extract the data, for de-duplication

Status Workflow:
    - "pending_review": Initial state, awaiting human review
    - "accepted": Data approved and ready for use
    - "rejected": Data rejected, needs re-extraction or manual fix
    - "error": Processing error occurred

Example:
    ```python
    # Create extracted data for review
    extracted = ExtractedData.create(
        data=person_data,
        status="pending_review",
        confidence={"name": 0.95, "age": 0.87}
    )

    # Later, after review
    if extracted.status == "accepted":
        # Use the data
        process_person(extracted.data)
    ```
z6The original data that was extracted from the documentr#   original_datazBThe latest state of the data. Will differ if data has been updatedr(   z The status of the extracted datastatusNz3The overall confidence score for the extracted dataoverall_confidencezPage links, and perhaps eventually bounding boxes, for individual fields in the extracted data. Structure is expected to have a )default_factoryr$   rQ   z4The ID of the file that was used to extract the datafile_idz6The name of the file that was used to extract the data	file_namez6The hash of the file that was used to extract the data	file_hashzJAdditional metadata about the extracted data, such as errors, tokens, etc.metadatar   r-   zExtractedData[ExtractedT]c                 X    [        U5      nU " UUUU[        U5      UUUU=(       d    0 S9	$ )ay  
Create a new ExtractedData instance with sensible defaults.

Args:
    extracted_data: The extracted data payload
    status: Initial workflow status
    field_metadata: Optional confidence scores, citations, and other metadata for fields
    file_id: The llamacloud file ID of the file that was used to extract the data
    file_name: The name of the file that was used to extract the data
    file_hash: A content hash of the file that was used to extract the data, for de-duplication
    metadata: Arbitrary additional application-specific data about the extracted data

Returns:
    New ExtractedData instance ready for storage
)	rm   r(   rn   rQ   ro   rq   rr   rs   rt   )rW   calculate_overall_confidence)	r1   r(   rn   rQ   rq   rr   rs   rt   normalized_field_metadatas	            r2   createExtractedData.createW  sB    4 %C>$R!4;<UV^

 
	
r5   resultschemac                 J   U=(       d    UR                   R                  nU=(       d    UR                   R                  n [        UR                  R                  S0 5      5      n UR                  UR                  5      n	U R                  U	UUUUUU=(       d    0 S9$ ! [         a    0 n NHf = f! [         ag  n
[        [        [        [        4      R                  UR                  =(       d    0 SUS[        U
5      0U=(       d    0 EUUUS9n[        U5      U
eSn
A
ff = f)z=
Create an ExtractedData instance from an extraction result.
rQ   )r(   rn   rQ   rq   rr   rs   rt   r   extraction_error)r(   rn   rQ   rt   rq   rr   rs   N)filer%   namerW   extraction_metadatagetr	   r/   r(   rx   rl   r   r<   r   InvalidExtractionData)r1   rz   r{   rs   rr   rq   rn   rt   rQ   r(   einvalid_items               r2   from_extraction_result$ExtractedData.from_extraction_result~  s!    +V[[^^1!1!1		 ;**../?DN	=((5D::-##!R     	 N	   
	=(c3h8??[[&B-,c!fIRI## @ L (51<
	=s+    %B &8B1 B.-B.1
D";A"DD"r6   )NNNr   N)r7   r8   r9   r:   r;   r   rm   r   r=   r(   rn   
StatusTypero   r   rP   r^   rQ   ExtractedFieldMetaDataDictrq   r<   rr   rs   rt   r   r   r>   rx   r   r   r   r?   r6   r5   r2   rl   rl     s   $L !&L!M:  XD*  +MNFJN*/I+  27 W2N.  #PGXc]   %R Ix}   %R Ix}  */`*HhtCH~& 
  .57!%#'#'-1$
$
 $
 3	$

 #$
 C=$
 C=$
 4S>*$
 
%$
 $
L 
 $(#'!%--1,=,= Z ,= C=	,=
 C=,= #,= ,= 4S>*,= 
%,= ,=r5   rl   c                   D   ^  \ rS rSrSrS\\\\4      4U 4S jjr	Sr
U =r$ )r   i  zJ
Exception raised when the extracted data does not conform to the schema.
r   c                 0   > Xl         [        TU ]	  S5        g )Nz;Not able to parse the extracted data, parsed invalid format)r   super__init__)selfr   	__class__s     r2   r   InvalidExtractionData.__init__  s    (VWr5   )r   )r7   r8   r9   r:   r;   rl   r   r<   r   r   r?   __classcell__)r   s   @r2   r   r     s)    X]4S>%B X Xr5   r   rt   c                 2    [        U 5      u  pUS:X  a  gX-  $ )@
Calculate the overall confidence score for the extracted data.
r   N)'_calculate_overall_confidence_recursive)rt   	numeratordenominators      r2   rv   rv     s&     EXNIa""r5   rL   c                 j   [        U [        5      (       a  U R                  b  U R                  S4$ g[        U [        5      (       a3  SnSnU R	                  5        H  n[        U5      u  pEX-  nX%-  nM     X4$ [        U [        5      (       a%  SnSnU  H  n[        U5      u  pEX-  nX%-  nM     X4$ g)r      )r   r   r   )r]   rI   rL   r^   valuesr   rd   )rL   r   r   valuenumdens         r2   r   r     s     *455  ,((!++*d##	&&(E>uEHCIK ) %%	J	%	%	E>uEHCIK   %%r5   c                       \ rS rSr% Sr\" SS9r\\\	4   \
S'   \" SS9r\\   \
S'   \" SS9r\\   \
S	'   \S
\S\\   SS4S j5       rSrg)TypedAggregateGroupi  aY  
Represents a group of agent data records aggregated by common field values.

This class is used for grouping and analyzing agent data based on shared
characteristics. It's particularly useful for generating summaries and
statistics across large datasets.

Attributes:
    group_key: The field values that define this group
    count: Number of records in this group (if count aggregation was requested)
    first_item: Representative data record from this group (if requested)

Example:
    ```python
    # Group by age range
    groups = await client.aggregate_agent_data(
        group_by=["age_range"],
        count=True,
        first=True
    )

    for group in groups.items:
        print(f"Age range {group.group_key['age_range']}: {group.count} people")
        if group.first_item:
            print(f"Example: {group.first_item.name}")
    ```
z'The field values that define this groupr#   	group_keyzDNumber of records in this group (if count aggregation was requested)countz9Representative data record from this group (if requested)
first_itemr+   r,   r-   zTypedAggregateGroup[AgentDataT]c                 z    UR                   nUb  UR                  U5      nU " UR                  UR                  US9$ )z
Convert raw API response to typed aggregate group.

Args:
    raw_data: Raw aggregate group from the API
    validator: Pydantic model class to validate the first_item field

Returns:
    TypedAggregateGroup instance with validated first_item
)r   r   r   )r   r/   r   r   )r1   r+   r,   r   s       r2   r3   TypedAggregateGroup.from_raw
  sE     ,4+>+>
!"11*=J((..!
 	
r5   r6   N)r7   r8   r9   r:   r;   r   r   r   r<   r   r=   r   r   rF   r   r   r>   r   r   r3   r?   r6   r5   r2   r   r     s    8 !&=!ItCH~  !ZE8C=  (-O(J$  
%
26z2B
	*
 
r5   r   c                   n    \ rS rSr% Sr\" SS9r\\\	      \
S'   \" SS9r\\   \
S'   \" SS9r\\
S	'   S
rg)TypedAggregateGroupItemsi#  a  
Paginated collection of aggregate groups.

This class represents a page of aggregation results from the agent data API,
providing both the grouped data and pagination metadata.

Attributes:
    items: List of aggregate groups in this page
    total: Total number of groups matching the query (only present if requested)
    has_more: Whether there are more groups available beyond this page

Example:
    ```python
    # Get first page of groups
    results = await client.aggregate_agent_data(
        group_by=["department"],
        count=True,
        page_size=20
    )

    for group in results.items:
        dept = group.group_key["department"]
        print(f"{dept}: {group.count} employees")

    # Load more if needed
    if results.has_more:
        next_page = await client.aggregate_agent_data(
            group_by=["department"],
            count=True,
            page_size=20,
            offset=20
        )
    ```
z%List of aggregate groups in this pager#   rC   zETotal number of groups matching the query (only present if requested)rD   z8Whether there are more groups available beyond this pagerE   r6   N)r7   r8   r9   r:   r;   r   rC   r   r   r   r=   rD   r   rF   rE   rG   r?   r6   r5   r2   r   r   #  sY    !F 49;4E4#J/0  ![E8C=  NHd r5   r   )/r;   r   rb   llama_cloudr   llama_cloud.types.agent_datar   !llama_cloud.types.aggregate_groupr   pydanticr   r   r	   typingr
   r   r   r   r   r   r   r   r   r   r^   r   r<   r   ComparisonOperatorr!   rA   rI   rd   r   rW   rS   rT   rl   	Exceptionr   rP   rv   tuplerF   r   r   r   r6   r5   r2   <module>r      s  $L   " 2 < 6 6
 
 
 \3
 \y$)?@
 7LMsRS
g@A3F	GG 
=
Y
 3 =
@()WZ%8 (VY 4 "%tCH~tCy@	AA 
cN %0=  
 )+1
1
CH~1
 !4S>49<=1
hX=Iwz2 X=vXI X	#(	#e_	#,d38nd3iGH
5#:>=
)WZ%8 =
@,y'**= ,r5   