
    i*                         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JrJrJrJr  S SKrSSKJrJrJrJrJrJr  S\S\4S	 jrS\4S
 jr " S S\\   5      rg)    N)AnyDictGenericListOptionalType)AsyncLlamaCloud)	WrappedFnretrystop_after_attemptwait_exponentialretry_if_exception_type   )
AgentDataTComparisonOperatorTypedAgentDataTypedAgentDataItemsTypedAggregateGroupTypedAggregateGroupItemsfuncreturnc           	          [        [        S5      [        SSS9[        [        R
                  [        R                  [        R                  45      S9" U 5      $ )a  
Decorator that adds automatic retry logic to agent data API calls.

Applies exponential backoff retry strategy for common network-related exceptions:
- Up to 3 retry attempts
- Exponential wait time between 0.5s and 10s
- Retries on timeout, connection, and HTTP status errors

This ensures resilient API communication in distributed environments where
temporary network issues or service unavailability may occur.
   g      ?
   )minmax)stopwaitr   )r   r   r   r   httpxTimeoutExceptionConnectErrorHTTPStatusError)r   s    e/home/james-whalen/.local/lib/python3.13/site-packages/llama_cloud_services/beta/agent_data/client.pyagent_data_retryr$      sQ     "#2.%##U%7%79N9NO
      c                  @    [         R                  " S5      =(       d    S$ )aE  
Retrieve the default agent ID from environment variables.

Returns:
    The value of LLAMA_DEPLOY_DEPLOYMENT_NAME environment variable,
    or None if not set

Note:
    This provides a convenient way to configure agent ID globally
    via environment variables instead of passing it explicitly
    to each client instance.
LLAMA_DEPLOY_DEPLOYMENT_NAME_public)osgetenv r%   r#   get_default_agent_idr,   -   s     9934A	Ar%   c                      \ rS rSrSr     SS\\   S\S\\   S\\	   S\\   S	\\   4S
 jjr
\S\S\\   4S j5       r\S\S\\   4S j5       r\S\S\S\\   4S j5       r\S\SS4S j5       r\     SS\\\\\\4   4      S\\   S\\   S\\   S\S\\   4S jj5       r\       SS\\\\\\4   4      S\\\      S\\   S\\   S\\   S\\   S\\   S\\   4S jj5       rSrg) AsyncAgentDataClient=   as  
Async client for managing agent-generated structured data with type safety.

This client provides a high-level interface for CRUD operations, searching, and
aggregation of structured data created by agents. It enforces type safety by
validating all data against a specified Pydantic model type.

The client is generic over AgentDataT, which must be a Pydantic BaseModel that
defines the structure of your agent's data output.

Example:
    ```python
    from pydantic import BaseModel
    from llama_cloud.client import AsyncLlamaCloud
    from llama_cloud_services.beta.agent_data import AsyncAgentDataClient

    class ExtractedPerson(BaseModel):
        name: str
        age: int
        email: str

    # Initialize client
    llama_client = AsyncLlamaCloud(token="your-api-key")
    agent_client = AsyncAgentDataClient(
        client=llama_client,
        type=ExtractedPerson,
        collection="extracted_people",
        agent_url_id="person-extraction-agent"
    )

    # Create data
    person = ExtractedPerson(name="John Doe", age=30, email="john@example.com")
    result = await agent_client.create_agent_data(person)

    # Search data
    results = await agent_client.search(
        filter={"age": {"gt": 25}},
        order_by="data.name",
        page_size=20
    )
    ```

Type Parameters:
    AgentDataT: Pydantic BaseModel type that defines the structure of agent data
Ntype
collectionagent_url_idclienttokenbase_urlc                     U=(       d
    [        5       U l        X l        U(       dD  [        U=(       d    [        R
                  " S5      U=(       d    [        R
                  " S5      S9nX@l        Xl        g)aR  
Initialize the AsyncAgentDataClient.

Args:
    type: Pydantic BaseModel class that defines the data structure.
        All agent data will be validated against this type.
    collection: Named collection within the agent for organizing data.
        Defaults to "default". Collections allow logical separation of
        different data types or workflows within the same agent.
    agent_url_id: Unique identifier for the agent. This normally appears in the
        url of an agent within the llama cloud platform. If not provided,
        will attempt to use the LLAMA_DEPLOY_DEPLOYMENT_NAME environment
        variable. Data can only be added to an already existing agent in the
        platform.
    client: AsyncLlamaCloud client instance for API communication. If not provided, will
        construct one from the provided api token and base url
    token: Llama Cloud API token. Reads from LLAMA_CLOUD_API_KEY if not provided
    base_url: Llama Cloud API token. Reads from LLAMA_CLOUD_BASE_URL if not provided, and
        defaults to https://api.cloud.llamaindex.ai

Raises:
    ValueError: If agent_url_id is not provided and the
        LLAMA_DEPLOY_DEPLOYMENT_NAME environment variable is not set

Note:
    The client automatically applies retry logic to all API calls with
    exponential backoff for timeout, connection, and HTTP status errors.
LLAMA_CLOUD_API_KEYLLAMA_CLOUD_BASE_URL)r4   r5   N)r,   r2   r1   r	   r)   r*   r3   r0   )selfr0   r1   r2   r3   r4   r5   s          r#   __init__AsyncAgentDataClient.__init__l   sX    L )B,@,B$$?ryy)>?!FRYY/E%FF 	r%   item_idr   c                    #    U R                   R                  R                  US9I S h  vN n[        R                  " X R
                  S9$  N"7f)Nr<   	validator)r3   betaget_agent_datar   from_rawr0   )r9   r<   raw_datas      r#   get_itemAsyncAgentDataClient.get_item   sH     ))88 9 
 
 &&x99EE
s   'AA#Adatac                    #    U R                   R                  R                  U R                  U R                  UR                  5       S9I S h  vN n[        R                  " X R                  S9$  N"7f)N)
agent_slugr1   rG   r?   )	r3   rA   create_agent_datar2   r1   
model_dumpr   rC   r0   )r9   rG   rD   s      r#   create_item AsyncAgentDataClient.create_item   s_     ));;((" < 
 

 &&x99EE
s   AA2A0#A2c                    #    U R                   R                  R                  UUR                  5       S9I S h  vN n[        R
                  " X0R                  S9$  N"7f)N)r<   rG   r?   )r3   rA   update_agent_datarK   r   rC   r0   )r9   r<   rG   rD   s       r#   update_item AsyncAgentDataClient.update_item   sT      ));;" < 
 
 &&x99EE	
s   6AA#Ac                 f   #    U R                   R                  R                  US9I S h  vN   g  N7f)Nr>   )r3   rA   delete_agent_data)r9   r<   s     r#   delete_item AsyncAgentDataClient.delete_item   s&     kk000AAAs   '1/1filterorder_byoffset	page_sizeinclude_totalc           
      R  #    U R                   R                  R                  U R                  U R                  UUUUUS9I Sh  vN n[        UR                   Vs/ s H!  n[        R                  " XpR                  S9PM#     snUR                  SLUR                  S9$  N\s  snf 7f)a  
Search agent data with filtering, sorting, and pagination.
Args:
    filter: Filter conditions to apply to the search. Dict mapping field names to FilterOperation objects. Filters only by data fields
        Examples:
        - {"age": {"gt": 18}} - age greater than 18
        - {"status": {"eq": "active"}} - status equals "active"
        - {"tags": {"includes": ["python", "ml"]}} - tags include "python" or "ml"
        - {"created_at": {"gte": "2024-01-01"}} - created after date
        - {"score": {"lt": 100, "gte": 50}} - score between 50 and 100
    order_by: Comma delimited list of fields to sort results by. Can order by standard agent fields like created_at, or by data fields. Data fields must be prefixed with "data.". If ordering desceding, use a " desc" suffix.
        Examples:
        - "data.name desc, created_at" - sort by name in descending order, and then by creation date
    page_size: Maximum number of items to return per page. Defaults to 10.
    offset: Number of items to skip from the beginning. Defaults to 0.
    include_total: Whether to include the total count in the response. Defaults to False to improve performance. It's recommended to only request on the first page.
)rI   r1   rV   rW   rX   rY   rZ   Nr?   itemshas_moretotal)r3   rA   5search_agent_data_api_v_1_beta_agent_data_search_postr2   r1   r   r]   r   rC   r0   next_page_token
total_size)r9   rV   rW   rX   rY   rZ   rawitems           r#   searchAsyncAgentDataClient.search   s     4 KK$$ZZ((' [ 
 
 #ORyyOXt''		By ((4..
 	

s$   AB'B B'(B"B'"B'group_bycountfirstc                 V  #    U R                   R                  R                  U R                  U R                  UUUUUUUS9	I Sh  vN n[        UR                   V	s/ s H!  n	[        R                  " XR                  S9PM#     sn	UR                  SLUR                  S9$  N\s  sn	f 7f)a:  
Aggregate agent data into groups according to the group_by fields.
Args:
    filter: Filter conditions to apply to the search. Dict mapping field names to FilterOperation objects. Filters only by data fields
        See search for more details on filtering.
    group_by: List of fields to group by. Groups strictly by equality. Can only group by data fields.
        Examples:
        - ["name"] - group by name
        - ["name", "age"] - group by name and age
    count: Whether to include the count of items in each group.
    first: Whether to include the first item in each group.
    order_by: Comma delimited list of fields to sort results by. See search for more details on ordering.
    offset: Number of groups to skip from the beginning. Defaults to 0.
    page_size: Maximum number of groups to return per page.
)	rI   r1   rY   rV   rW   rg   rh   ri   rX   Nr?   r\   )r3   rA   ;aggregate_agent_data_api_v_1_beta_agent_data_aggregate_postr2   r1   r   r]   r   rC   r0   ra   rb   )
r9   rV   rg   rh   ri   rW   rX   rY   rc   rd   s
             r#   	aggregateAsyncAgentDataClient.aggregate   s     4 KK$$``(( a 

 

 (  II%D $,,TYYG% ((4..
 	


s$   AB)B"B)(B$B)$B))r2   r3   r1   r0   )defaultNNNN)NNNNF)NNNNNNN)__name__
__module____qualname____firstlineno____doc__r   r   strr   r	   r:   r$   r   rE   rL   rP   rT   r   r   r   intboolr   re   r   r   rl   __static_attributes__r+   r%   r#   r.   r.   =   se   ,b $&*,0#"&/:/ / sm	/
 )/ }/ 3-/b Fc FnZ.H F F Fj F^J5O F F FF",F	
	#F F B B B B  FJ"& $#'#(
c4(:C(?#@@AB(
 3-(
 	(

 C=(
 (
 
Z	((
 (
T  FJ(, $ $"& $#'+
c4(:C(?#@@AB+
 49%+
 ~	+

 ~+
 3-+
 +
 C=+
 
"*	-+
 +
r%   r.   )r)   typingr   r   r   r   r   r   llama_cloud.clientr	   tenacityr
   r   r   r   r   r   schemar   r   r   r   r   r   r$   rt   r,   r.   r+   r%   r#   <module>r|      s^    	 ; ; .   9  *Bc B U
7:. U
r%   