o
    i                     @   s   d dl mZ d dl mZ d dlmZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dlm
Z
 eeZeeeeeef f Zd	eeef d
dfddZdeeef d
dfddZdeeeef  d
ee	 fddZG dd dZG dd dZdS )    )Any)Union)
get_logger)Document)Message)ToolCall)ToolDefinition)
ToolResult	tool_callreturnr   c                 C   s   t | ts	td| d}| d}|rt |tstd|du s't |ts+tdt||d}| d}|rAt |trA||d< | d	}|rQt |trQ||d	< |S )
z,Extract and validate a tool call dictionary.z$Each tool_call must be a dictionary.name	argumentsz)ToolCall name must be a non-empty string.Nz(ToolCall arguments must be a dictionary.)r   r   tool_idtype)
isinstancedict	TypeErrorgetstrr   )r
   r   r   formatted_tool_callr   	tool_type r   H/home/ubuntu/.local/lib/python3.10/site-packages/ddtrace/llmobs/utils.py_extract_tool_call   s    




r   tool_resultr	   c                 C   s   t | ts	td| d}|du st |tstdt|d}| d}|r0t |tr0||d< | d}|r@t |tr@||d< | d}|rPt |trP||d< |S )	z.Extract and validate a tool result dictionary.z&Each tool_result must be a dictionary.resultNz#ToolResult result must be a string.)r   r   r   r   )r   r   r   r   r   r	   )r   r   formatted_tool_resultr   r   r   r   r   r   _extract_tool_result-   s    





r   tool_definitionsc                 C   s   t | tstd g S g }t| D ]_\}}t |ts"td| q|d}|r.t |ts5td| qt|d}|d}|durSt |tsOtd| n||d< |d	}|durlt |tshtd
| n||d	< |	| q|S )z,Return a list of validated tool definitions.z0tool_definitions must be a list of dictionaries.z;Tool definition at index %s must be a dictionary. Skipping.r   zITool definition at index %s must have a non-empty 'name' field. Skipping.)r   descriptionNzWTool definition 'description' at index %s must be a string. Skipping description field.schemazQTool definition 'schema' at index %s must be a dictionary. Skipping schema field.)
r   listlogwarning	enumerater   r   r   r   append)r   validated_tool_definitionsitool_defr   validated_tool_defr   r    r   r   r   extract_tool_definitionsI   s6   








r*   c                   @   s8   e Zd Zdeeeeef  eeef ef fddZdS )Messagesmessagesc           
      C   s$  g | _ t|ts|g}|D ]}t|tr| j t|d qt|ts'td|dd}|d}t|ts;tdt|d}|rOt|tsKtd||d< |d}|d urlt|tsatd	d
d |D }||d< |d}|d urt|ts~tddd |D }	|	|d< | j | qd S )N)contentz?messages must be a string, dictionary, or list of dictionaries.r-    rolez!Message content must be a string.zMessage role must be a string.
tool_callsztool_calls must be a list.c                 S      g | ]}t |qS r   )r   ).0r
   r   r   r   
<listcomp>       z%Messages.__init__.<locals>.<listcomp>tool_resultsztool_results must be a list.c                 S   r1   r   )r   )r2   r   r   r   r   r3      r4   )	r,   r   r!   r   r%   r   r   r   r   )
selfr,   messager-   r/   msg_dictr0   formatted_tool_callsr5   formatted_tool_resultsr   r   r   __init__u   s@   










zMessages.__init__N)	__name__
__module____qualname__r   r!   r   r   r   r;   r   r   r   r   r+   t   s    0r+   c                   @   s(   e Zd Zdeee eef fddZdS )	Documents	documentsc                 C   s  g | _ t|ts|g}|D ]q}t|tr| j t|d qt|ts'td|d}|d}|d}|d}t|tsDtdt|d}|rXt|tsTtd||d< |rgt|tsctd	||d< |rxt|t	t
fsttd
||d< | j | qd S )N)textz@documents must be a string, dictionary, or list of dictionaries.rA   r   idscorezDocument text must be a string.zdocument name must be a string.zdocument id must be a string.z+document score must be an integer or float.)r@   r   r!   r   r%   r   r   r   r   intfloat)r6   r@   documentdocument_textdocument_namedocument_iddocument_scoreformatted_documentr   r   r   r;      s<   










zDocuments.__init__N)r<   r=   r>   r   r!   DocumentTyper   r;   r   r   r   r   r?      s     r?   N)typingr   r   ddtrace.internal.loggerr   ddtrace.llmobs.typesr   r   r   r   r	   r<   r"   r   r   rD   rE   rL   r   r   r!   r*   r+   r?   r   r   r   r   <module>   s    "+(