
    Fd/              	           d Z ddlZddlZddlZddlZddlZddlZddlm	Z	 ddl
mZ ddlmZ ddlmZ ddlZddl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j                            ej                            ej                            e                     ddd          Z!dde"de#de$e"         fdZ%de&de"ddfdZ'dede&fdZ(de"de#fdZ)	 dde&dede#de&fdZ*	 dde&dede#de&fdZ+ddede#dee         fdZ,dS )zHandles loading of plugins.    N)Path)List)urlparse)zipimporter)AutoGPTPluginTemplate)Config)logger)BaseOpenAIPluginz..zplugins_config.yamlFzip_pathdebugreturnc                    g }t          j        | d          5 }|                                D ][}|                    d          rD|                    d          s/t          j        d| d|            |                    |           \	 ddd           n# 1 swxY w Y   t          |          dk    rt          j        d|  d	           |S )
a  
    Inspect a zipfile for a modules.

    Args:
        zip_path (str): Path to the zipfile.
        debug (bool, optional): Enable debug logging. Defaults to False.

    Returns:
        list[str]: The list of module names found or empty list if none were found.
    rz__init__.py__MACOSXzFound module 'z' in the zipfile at: Nr   z0Module '__init__.py' not found in the zipfile @ .)	zipfileZipFilenamelistendswith
startswithr	   r   appendlen)r   r   resultzfilenames        LC:\Users\Administrator\Downloads\Auto-GPT-master\autogpt\plugins\__init__.pyinspect_zip_for_modulesr      s    F	3	'	' $5NN$$ 	$ 	$D}}]++ $DOOJ4O4O $OdOOOOPPPd###	$$ $ $ $ $ $ $ $ $ $ $ $ $ $ $
 6{{aSSSSTTTMs   A1BBBdata	file_pathc                     t          |d          5 }t          j        | |d           ddd           dS # 1 swxY w Y   dS )z
    Write a dictionary to a JSON file.
    Args:
        data (dict): Dictionary to write.
        file_path (str): Path to the file.
    w   )indentN)openjsondump)r   r   files      r   write_dict_to_json_filer(   2   s     
i		 (	$Q''''( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( ( (s   6::configc                    i }| j         D ]8}| j         dt          |          j         }t	          |           t
          j                            | d          s	 t          j	        | d          }|j
        dk    r|                                }|d         dk    r!t          j        d|d          d	|            |d
         d         dk    r't          j        d|d
         d          d	|            t          || d           nt          j        d| d|j
                    nw# t          j        j        $ r$}t          j        d| d|            Y d}~nDd}~ww xY wt          j        d| d           t          j        t'          | d                    }t
          j                            | d          s7t)          j        |d
         d         dd          }t          || d           n<t          j        d| d           t          j        t'          | d                    }||d||<   :|S )z
    Fetch the manifest for a list of OpenAI plugins.
        Args:
        urls (List): List of URLs to fetch.
    Returns:
        dict: per url dictionary of manifest and spec.
    z/openai/z/ai-plugin.jsonz/.well-known/ai-plugin.json   schema_versionv1zUnsupported manifest version: schem_versionz for apitypeopenapizUnsupported API type: zFailed to fetch manifest for : z%Error while requesting manifest from NzManifest for  already existsz/openapi.jsonurl   )r4   pathtimeoutzOpenAPI spec for )manifestopenapi_spec)plugins_openaiplugins_dirr   netloccreate_directory_if_not_existsosr6   existsrequestsgetstatus_coder%   r	   warnr(   
exceptionsRequestExceptioninfoloadr$   openapi_python_client_get_document)r)   	manifestsr4   openai_plugin_client_dirresponser8   er9   s           r   &fetch_openai_plugins_manifest_and_specrN   =   s    I$ (N (N&,&8#X#X(3--BV#X#X &'?@@@w~~!9JJJKK 	UP#<3(K(K(KLL'3..'}}H 01T99bXo=Vbb]`bb   !v.);;XXe_V5LXXSVXX   !+ %="N"N"N    KUUUx?SUU   &7 P P PNCNN1NNOOOOOOOOP K<<<<===y)A&R&R&R!S!STTHw~~!9HHHII 		W0>UOE*q  L $!9HHH    K@C@@@AAA9T-E*T*T*T%U%UVVL&.MM	#s%   A"D.8D.:3D..E&E!!E&directory_pathc                 4   t           j                            |           s`	 t          j        |            t	          j        d|             dS # t          $ r%}t	          j        d|  d|            Y d}~dS d}~ww xY wt	          j        d|  d           dS )	z
    Create a directory if it does not exist.
    Args:
        directory_path (str): Path to the directory.
    Returns:
        bool: True if the directory was created, else False.
    zCreated directory: TzError creating directory r2   NFz
Directory r3   )	r>   r6   r?   makedirsr	   r   OSErrorrC   rF   )rO   rM   s     r   r=   r=   s   s     7>>.)) 
	K'''L?~??@@@4 	 	 	KINIIaIIJJJ55555	 	@@@@AAAts   +A 
A=A88A=manifests_specsc                 r   |j          d}t          |          r|                                 D ]\  }}| dt          |          j         }t
          j        j        f}t          di ddd}t          j
                    }	t          j        |           t          j                            d          s]t          j        |d         d         d         d||	          }
|
r1t!          j        d
|
d         j         d|
d         j                    t(          j                            dd          }t(          j                            |          }	 |j                            |           t          j        |	           n# t          j        |	           w xY w|                    |          }||d<   | S )aN  
    Initialize OpenAI plugins.
    Args:
        manifests_specs (dict): per url dictionary of manifest and spec.
        config (Config): Config instance including plugins config
        debug (bool, optional): Enable debug logging. Defaults to False.
    Returns:
        dict: per url dictionary of manifest, spec and client.
    z/openai/client)project_name_overridepackage_name_overrider8   r/   r4   N)r4   r6   metar)   zError creating OpenAPI client: r   z 
 details: zclient/client/client.py)base_url )r;   r=   itemsr   hostnamerH   MetaTypeSETUPOpenAPIConfigr   cwdr>   chdirr6   r?   create_new_clientr	   rC   headerdetail	importlibutilspec_from_file_locationmodule_from_specloaderexec_moduleClient)rS   r)   r   openai_plugins_dirr4   manifest_specrK   _meta_option_configprev_cwdclient_resultsspecmodulerV   s                 r   initialize_openai_pluginsru      s    #.777%&899 %-"1"7"7"9"9 $	- $	-C*<'W'Wx}}?U'W'W$1:@BL#  -5-5  G xzzHH-...7>>(++ !6!H%j1%8?%"	" " " " K@.:K:R @ @%3A%6%=@ @   >993 D ^44T::F#''///""""""""]]C]00F&,M(##s   FFmanifests_specs_clientsc                 b    i }|                                  D ]\  }}t          |          ||<   |S )a  
    Instantiates BaseOpenAIPlugin instances for each OpenAI plugin.
    Args:
        manifests_specs_clients (dict): per url dictionary of manifest, spec and client.
        config (Config): Config instance including plugins config
        debug (bool, optional): Enable debug logging. Defaults to False.
    Returns:
          plugins (dict): per url dictionary of BaseOpenAIPlugin instances.

    )r\   r
   )rv   r)   r   pluginsr4   manifest_spec_clients         r   !instantiate_openai_plugin_clientsrz      sC     G%<%B%B%D%D > >!!'(<==N    c           
         g }t          | j                  }| j        }d t          j        | j                  D             D ]}|                    d          r|                    t          j        j                  }|d         }d	                    |          }t          |           t          j        |         }	|                    |          st          j        d| d           t!          j        |	          D ]@\  }
}t%          |d          r+t&          |j        v r|                     |                       A|                    d          D ]}	t/          t1          |	          |          x}r|D ]{}t          |	          }	t          |          }t          j        d	|	 d
|            t5          t1          |	                    }|                    t1          |j                            }t;          |          D ]}|                    d          rt=          ||          }t;          |          }d|v r|j        dk    r|j        }|                     |          du}|                    |          }|r8|r6t          j        d| d           |                     |                       |r|st          j        d| d           |st          j        d| d| d           }| j!        rtE          |           }|#                                r}tI          || |          }|%                                D ]W\  }}|                    |          st          j        d| d           3tM          |          }	|                    |	           X|r%t          j'        dtQ          |           d           |D ]-}	t          j'        |	j)         d|	j*         d|	j+                    .|S )a  Scan the plugins directory for plugins and loads them.

    Args:
        config (Config): Config instance including plugins config
        debug (bool, optional): Enable debug logging. Defaults to False.

    Returns:
        List[Tuple[str, Path]]: List of plugins.
    c                 D    g | ]}|                                 |j        S r[   )is_dirr6   ).0fs     r   
<listcomp>z scan_plugins.<locals>.<listcomp>   s'    UUU1!((**UUUUr{   __r   zPlugin z found but not configured	_abc_implz*.zipzPlugin: z	 Module: r   NzLoading plugin z as it was enabled in config.zNot loading plugin z as it was disabled in config.z as it was not found in config. Please check your config. Starting with 0.4.1, plugins will not be loaded unless they are enabled in plugins_config.yaml. Zipped plugins should use the class name (z) as the key.z
Plugins found: z
--------------------r2   z - ),r   r;   plugins_configr>   scandirr   splitr6   sepjoin
__import__sysmodules
is_enabledr	   rC   inspect
getmembershasattrr   	__bases__r   globr   strr   r   load_moduleparentdirgetattr__name__rA   r:   rN   keysru   r\   r
   rF   r   _name_version_description)r)   r   loaded_pluginsplugins_path_pathr   plugin_pathplugin_module_pathplugin_module_namequalified_module_nameplugin_	class_obj
moduleListrt   zipped_packagezipped_modulekeya_modulea_keysplugin_nameplugin_configuredplugin_enabledrS   rv   r4   openai_plugin_metas                             r   scan_pluginsr      s    NV/00*N VU
63E(F(FUUU 3 3!!$'' 	(..rw{;;/3 #); < <()))23(();<< 	KO"4OOOPPP#.v66 	3 	3LAy	;//3)Y-@@@%%iikk222	3 $((11 % %0VeDDD: $	$ # #ffAAAAABBB!,S[[!9!9 . : :3v};M;M N N}--  C~~d++ ! &}c::H ]]F#v--$-1HHH&.&7,:,>,>{,K,KSW,W))7)B)B;)O)O,  "L \+ \ \ \   +11((**====. 
~ 
"L ak a a a    "3 "K!Dk !D !D *5!D !D !D  />  .@HH!! 
	.&?' '# ,C+H+H+J+J . .''%0055 K W*< W W WXXX)*<==%%f---- XVN(;(;VVVWWW  R Rv|PPvPP6;NPPQQQQr{   )F)-__doc__importlib.utilrf   r   r%   r>   r   r   pathlibr   typingr   urllib.parser   	zipimportr   rH   r@   auto_gpt_plugin_templater   openapi_python_client.configr   r`   autogpt.config.configautogpt.logsr	   "autogpt.models.base_open_ai_pluginr
   r6   r   dirnameabspath__file__DEFAULT_PLUGINS_CONFIG_FILEr   boollistr   dictr(   rN   r=   ru   rz   r   r[   r{   r   <module>r      s   ! !       				 



              ! ! ! ! ! ! ! ! ! ! ! !      : : : : : : @ @ @ @ @ @ ( ( ( ( ( (       ? ? ? ? ? ? gllGOOBGOOH--..d<Q  
 c $ 49    ,($ (3 (4 ( ( ( (36 3d 3 3 3 3l3 4    , :?3 33#)3263	3 3 3 3n BG !+1:>	   &b b b b>S9T b b b b b br{   