o
    hd&                  
   @  s  U d dl mZ d dlZd dlmZ d dlmZmZmZm	Z	m
Z
mZ d dlZd dlmZmZmZmZmZmZmZmZmZmZmZ d dlmZ d dlmZmZmZmZ d dl 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,m-Z- ddl.m/Z/ erddl0m1Z1 eZ2	 eZ3	 eZ4	 eZ5	 e
dee4e3f dZ6eG dd dee6 ej7Z8eG dd de8e Z9eG dd de8e Z:eG dd de8e3 Z;eeeeee$ee!e"f Z<de=d< 	 eG dd de8e< Z>eG dd de8eeeef  Z?eG d d! d!e8e' Z@eG d"d# d#e8e% ZAeG d$d% d%e8e# ZBeG d&d' d'e8e ZCee9e:e;e>e?e@eAeBeCf	 ZDde=d(< 	 ejjG d)d* d*ZEG d+d, d,ZFdS )-    )annotationsN)	dataclass)TYPE_CHECKINGAnyGenericLiteralTypeVarUnion)ResponseResponseComputerToolCallResponseFileSearchToolCallResponseFunctionToolCallResponseFunctionWebSearchResponseInputItemParamResponseOutputItemResponseOutputMessageResponseOutputRefusalResponseOutputTextResponseStreamEvent)ResponseCodeInterpreterToolCall)ComputerCallOutputFunctionCallOutputLocalShellCallOutputMcpApprovalResponse)ImageGenerationCallLocalShellCallMcpApprovalRequestMcpCallMcpListTools)ResponseReasoningItem)	BaseModel)	TypeAlias   )AgentsExceptionModelBehaviorError)Usage)AgentT)boundc                   @  s,   e Zd ZU ded< 	 ded< 	 d
ddZd	S )RunItemBase
Agent[Any]agentr'   raw_itemreturnTResponseInputItemc                 C  s@   t | jtr	| jS t | jtr| jjddS tdt| j )zHConverts this item into an input item suitable for passing to the model.Texclude_unsetzUnexpected raw item type: )
isinstancer,   dictr    
model_dumpr#   typeself r7   M/var/www/html/openai_agents/venv/lib/python3.10/site-packages/agents/items.pyto_input_itemI   s
   zRunItemBase.to_input_itemN)r-   r.   )__name__
__module____qualname____annotations__r9   r7   r7   r7   r8   r)   >   s   
 r)   c                   @  (   e Zd ZU dZded< 	 dZded< dS )MessageOutputItemz"Represents a message from the LLM.r   r,   message_output_itemzLiteral['message_output_item']r4   Nr:   r;   r<   __doc__r=   r4   r7   r7   r7   r8   r?   U   
   
 r?   c                   @  r>   )HandoffCallItemz?Represents a tool call for a handoff from one agent to another.r   r,   handoff_call_itemzLiteral['handoff_call_item']r4   NrA   r7   r7   r7   r8   rD   _   rC   rD   c                   @  s<   e Zd ZU dZded< 	 ded< 	 ded< 	 dZded	< d
S )HandoffOutputItemz#Represents the output of a handoff.r.   r,   r*   source_agenttarget_agenthandoff_output_itemzLiteral['handoff_output_item']r4   NrA   r7   r7   r7   r8   rF   i   s   
 rF   r!   ToolCallItemTypesc                   @  r>   )ToolCallItemzDRepresents a tool call e.g. a function call or computer action call.rJ   r,   tool_call_itemzLiteral['tool_call_item']r4   NrA   r7   r7   r7   r8   rK      rC   rK   c                   @  s2   e Zd ZU dZded< 	 ded< 	 dZded< d	S )
ToolCallOutputItemz%Represents the output of a tool call.z>FunctionCallOutput | ComputerCallOutput | LocalShellCallOutputr,   r   outputtool_call_output_itemz Literal['tool_call_output_item']r4   NrA   r7   r7   r7   r8   rM      s   
 rM   c                   @  r>   )ReasoningItemzRepresents a reasoning item.r   r,   reasoning_itemzLiteral['reasoning_item']r4   NrA   r7   r7   r7   r8   rP      rC   rP   c                   @  r>   )MCPListToolsItemz1Represents a call to an MCP server to list tools.r   r,   mcp_list_tools_itemzLiteral['mcp_list_tools_item']r4   NrA   r7   r7   r7   r8   rR      rC   rR   c                   @  r>   )MCPApprovalRequestItemz&Represents a request for MCP approval.r   r,   mcp_approval_request_itemz$Literal['mcp_approval_request_item']r4   NrA   r7   r7   r7   r8   rT      rC   rT   c                   @  r>   )MCPApprovalResponseItemz1Represents a response to an MCP approval request.r   r,   mcp_approval_response_itemz%Literal['mcp_approval_response_item']r4   NrA   r7   r7   r7   r8   rV      rC   rV   RunItemc                   @  s6   e Zd ZU ded< 	 ded< 	 ded< 	 dd	d
ZdS )ModelResponsezlist[TResponseOutputItem]rN   r%   usage
str | Noneresponse_idr-   list[TResponseInputItem]c                 C  s   dd | j D S )zPConvert the output into a list of input items suitable for passing to the model.c                 S  s   g | ]}|j d dqS )Tr/   )r3   ).0itr7   r7   r8   
<listcomp>   s    z0ModelResponse.to_input_items.<locals>.<listcomp>)rN   r5   r7   r7   r8   to_input_items   s   zModelResponse.to_input_itemsN)r-   r]   )r:   r;   r<   r=   ra   r7   r7   r7   r8   rY      s   
 rY   c                   @  s`   e Zd ZedddZeddd	ZedddZed ddZed!ddZed"ddZ	dS )#ItemHelpersmessageTResponseOutputItemr-   strc                 C  sT   t |tsdS |jsdS |jd }t |tr|jS t |tr!|jS tdt| )z9Extracts the last text content or refusal from a message. zUnexpected content type: )	r1   r   contentr   textr   refusalr$   r4   clsrc   last_contentr7   r7   r8   extract_last_content   s   



z ItemHelpers.extract_last_contentr[   c                 C  s2   t |tr|js
dS |jd }t |tr|jS dS )zHExtracts the last text content from a message, if any. Ignores refusals.Nrg   )r1   r   rh   r   ri   rk   r7   r7   r8   extract_last_text   s   


zItemHelpers.extract_last_textinputstr | list[TResponseInputItem]r]   c                 C  s   t |tr|ddgS | S )zDConverts a string or list of input items into a list of input items.user)rh   role)r1   re   copy)rl   rp   r7   r7   r8   input_to_new_input_list  s   
z#ItemHelpers.input_to_new_input_listitemslist[RunItem]c                 C  s*   d}|D ]}t |tr|| |7 }q|S )zFConcatenates all the text content from a list of message output items.rf   )r1   r?   text_message_output)rl   rv   ri   itemr7   r7   r8   text_message_outputs  s   
z ItemHelpers.text_message_outputsr?   c                 C  s*   d}|j jD ]}t|tr||j7 }q|S )z@Extracts all the text content from a single message output item.rf   )r,   rh   r1   r   ri   )rl   rc   ri   ry   r7   r7   r8   rx   "  s   

zItemHelpers.text_message_output	tool_callr   rN   r   c                 C  s   |j |ddS )z@Creates a tool call output item from a tool call and its output.function_call_output)call_idrN   r4   )r}   )rl   r{   rN   r7   r7   r8   rO   +  s   z!ItemHelpers.tool_call_output_itemN)rc   rd   r-   re   )rc   rd   r-   r[   )rp   rq   r-   r]   )rv   rw   r-   re   )rc   r?   r-   re   )r{   r   rN   re   r-   r   )
r:   r;   r<   classmethodrn   ro   ru   rz   rx   rO   r7   r7   r7   r8   rb      s    rb   )G
__future__r   abcdataclassesr   typingr   r   r   r   r   r	   pydanticopenai.types.responsesr
   r   r   r   r   r   r   r   r   r   r   :openai.types.responses.response_code_interpreter_tool_callr   0openai.types.responses.response_input_item_paramr   r   r   r   +openai.types.responses.response_output_itemr   r   r   r   r   .openai.types.responses.response_reasoning_itemr   r    typing_extensionsr!   
exceptionsr#   r$   rZ   r%   r+   r&   	TResponser.   rd   TResponseStreamEventr'   ABCr)   r?   rD   rF   rJ   r=   rK   rM   rP   rR   rT   rV   rX   rY   rb   r7   r7   r7   r8   <module>   s     4		
	
				