o
    $iL                     @   s  U d Z ddlZddlZddlmZ ddlmZmZmZ ddlm	Z	 da
ee ed< dee ddfd	d
ZdedefddZdefddZdedefddZdedefddZdededefddZdedefddZdedefddZdedefdd Ze	dd!d"i g d#ed$e	dd%d"dd&d'd(idgd#ed$e	dd)d"dd&d*d(idgd#ed$e	dd+d"d&d,d(d&d-d(d.ddgd#ed$e	dd/d"dd&d0d(idgd#ed$e	dd1d"dd&d2d(idgd#ed$e	d d3d"dd&d4d(idgd#ed$d5Zeee	f ed6< deee	f fd7d8ZdS )9z*
Built-in tools for the OpenRouter Agent.
    N)datetime)AnyDictOptional)ToolWORKING_DIRECTORY	directoryreturnc                 C   s    | du rda dS tj| a dS )z-Set the working directory for file operationsN)r   ospathabspath)r    r   8/var/www/apps/myagent/mysuperagent/core/tools/builtin.pyset_working_directory   s   r   r   c                 C   s&   t j| r| S trt jt| S | S )z@Resolve a path, prepending working directory if path is relative)r
   r   isabsr   join)r   r   r   r   _resolve_path   s
   r   c                   C   s   t   S )z2Returns current date and time as ISO format string)r   now	isoformatr   r   r   r   get_current_time"   s   r   
expressionc              
      sx   z"t d t fdd| D sW dS t| di ii }|  d| W S  ty; } zdt| W  Y d}~S d}~ww )	z)Evaluate a mathematical expression safelyz0123456789+-*/.() c                 3   s    | ]}| v V  qd S )Nr   ).0callowed_charsr   r   	<genexpr>+   s    zcalculate.<locals>.<genexpr>z0Error: Expression contains disallowed characters__builtins__z = zError evaluating expression: N)setalleval	Exceptionstr)r   resulter   r   r   	calculate'   s   r$   	file_pathc              
   C   s   z#t | }t|ddd}| W  d   W S 1 sw   Y  W dS  ty1   d|   Y S  ty>   d|   Y S  tyY } zd|  dt| W  Y d}~S d}~ww )	z&Read and return the contents of a filerutf-8encodingNzError: File not found: Error: Permission denied: zError reading file : )r   openreadFileNotFoundErrorPermissionErrorr    r!   )r%   resolved_pathfiler#   r   r   r   	read_file4   s   ( r2   contentc              
   C   s   z&t | }t|ddd}|| W d   n1 sw   Y  d|  W S  ty4   d|   Y S  tyO } zd|  dt| W  Y d}~S d}~ww )	Write content to a filewr'   r(   NzSuccessfully wrote to r*   zError writing to file r+   )r   r,   writer/   r    r!   )r%   r3   r0   r1   r#   r   r   r   
write_fileB   s    r7   directory_pathc              
   C   s   z;t | }t|}|sd|  W S g }|D ]}tj||}tj|r.|| d q|| qdt|W S  tyI   d|   Y S  t	yV   d|   Y S  t
yq } zd|  dt| W  Y d}~S d}~ww )	z$List files and directories in a pathzDirectory is empty: /
zError: Directory not found: r*   zError listing directory r+   N)r   r
   listdirr   r   isdirappendsortedr.   r/   r    r!   )r8   r0   entriesr"   entry	full_pathr#   r   r   r   list_directoryO   s(   
 rB   c                 C   sJ   t | }tj|r tj|rd|  S tj|r d|  S d|  S )#Check if a file or directory existszFile exists: zDirectory exists: zPath does not exist: )r   r
   r   existsisfiler<   )r   r0   r   r   r   file_existsf   s   


rF   questionc                 C   s
   d|  S )z1Ask the user a question and return their responsez[USER_INPUT_NEEDED]: r   )rG   r   r   r   ask_userq   s   
rH   zGet the current date and timeobject)type
propertiesrequired)namedescription
parametersfunctionz!Perform mathematical calculationsstringz#Mathematical expression to evaluate)rJ   rN   zRead the contents of a filez%The absolute path to the file to readr4   z&The absolute path to the file to writez The content to write to the file)r%   r3   z*List files and directories in a given pathz*The absolute path to the directory to listrC   zThe absolute path to checkz.Ask the user a question and get their responsezThe question to ask the user)r   r$   r2   r7   rB   rF   rH   BUILTIN_TOOLSc                   C   s   t tS )z.Return a copy of the built-in tools dictionary)dictrR   r   r   r   r   get_builtin_tools   s   rT   )__doc__jsonr
   r   typingr   r   r   core.modelsr   r   r!   __annotations__r   r   r   r$   r2   r7   rB   rF   rH   rR   rT   r   r   r   r   <module>   s    		






l