o
    h                     @  s   d Z ddlmZ ddlZddlmZmZmZmZm	Z	m
Z
 ddlmZ ddlmZ ddlmZ ddlmZ ed	ed
ZG dd deee ZG dd deZG dd deZee eB eB ZeeeefZd&ddZd'ddZ	d(d)d$d%ZdS )*z&Elicitation utilities for MCP servers.    )annotationsN)GenericLiteralTypeVarUnionget_args
get_origin)	BaseModel)	FieldInfo)ServerSession)	RequestIdElicitSchemaModelT)boundc                   @  s&   e Zd ZU dZdZded< ded< dS )AcceptedElicitationz)Result when user accepts the elicitation.acceptzLiteral['accept']actionr   dataN__name__
__module____qualname____doc__r   __annotations__ r   r   W/var/www/html/openai_agents/venv/lib/python3.10/site-packages/mcp/server/elicitation.pyr      s   
 r   c                   @     e Zd ZU dZdZded< dS )DeclinedElicitationz*Result when user declines the elicitation.declinezLiteral['decline']r   Nr   r   r   r   r   r         
 r   c                   @  r   )CancelledElicitationz)Result when user cancels the elicitation.cancelzLiteral['cancel']r   Nr   r   r   r   r   r      r   r   schematype[BaseModel]returnNonec                 C  s6   | j  D ]\}}t|std| dt dqdS )zCValidate that a Pydantic model only contains primitive field types.zElicitation schema field 'z' must be a primitive type z_ or Optional of these types. Complex types like lists, dicts, or nested models are not allowed.N)model_fieldsitems_is_primitive_field	TypeError_ELICITATION_PRIMITIVE_TYPES)r!   
field_name
field_infor   r   r   _validate_elicitation_schema+   s   r,   r+   r
   boolc                 C  sX   | j }|tju r
dS |tv rdS t|}|tu s|tju r*t|}tdd |D S dS )zDCheck if a field is a primitive type allowed in elicitation schemas.Tc                 s  s"    | ]}|t ju p|tv V  qd S N)typesNoneTyper)   ).0argr   r   r   	<genexpr>G   s     z&_is_primitive_field.<locals>.<genexpr>F)	
annotationr/   r0   r)   r   r   	UnionTyper   all)r+   r4   originargsr   r   r   r'   6   s   
r'   sessionr   messagestrtype[ElicitSchemaModelT]related_request_idRequestId | None%ElicitationResult[ElicitSchemaModelT]c                   s   t | | }| j|||dI dH }|jdkr)|jdur)||j}t|dS |jdkr1t S |jdkr9t S t	d|j )a  Elicit information from the client/user with schema validation.

    This method can be used to interactively ask for additional information from the
    client within a tool's execution. The client might display the message to the
    user and collect a response according to the provided schema. Or in case a
    client is an agent, it might decide how to handle the elicitation -- either by asking
    the user or automatically generating a response.
    )r:   requestedSchemar=   Nr   )r   r   r    zUnexpected elicitation action: )
r,   model_json_schemaelicitr   contentmodel_validater   r   r   
ValueError)r9   r:   r!   r=   json_schemaresultvalidated_datar   r   r   elicit_with_validationL   s    


rI   )r!   r"   r#   r$   )r+   r
   r#   r-   r.   )
r9   r   r:   r;   r!   r<   r=   r>   r#   r?   ) r   
__future__r   r/   typingr   r   r   r   r   r   pydanticr	   pydantic.fieldsr
   mcp.server.sessionr   	mcp.typesr   r   r   r   r   ElicitationResultr;   intfloatr-   r)   r,   r'   rI   r   r   r   r   <module>   s$     

