11import os , sys
2- from typing import Optional , Any , List
3- import importlib .resources
2+ from typing import Optional , Union , Any
43from pathlib import Path
5- import json
6- import sys
7- from typing import Optional , List , Dict , Union
8-
9- from . import get_agent_names
10- from .gen_utils import insert_code_after_tag , string_in_file
11- from ..utils import open_json_file , get_framework , term_color
12- import os
134import shutil
145import fileinput
156import ast
167
8+ from agentstack import frameworks
179from agentstack import packaging
1810from agentstack import ValidationError
19- from agentstack .utils import get_package_path
11+ from agentstack .utils import term_color
2012from agentstack .tools import ToolConfig
2113from agentstack .generation import astools
2214from agentstack .generation .files import ConfigFile , EnvFile
23- from agentstack import frameworks
24- from .gen_utils import insert_code_after_tag , string_in_file
25- from ..utils import open_json_file , get_framework , term_color
2615
2716
2817# This is the filename of the location of tool imports in the user's project.
@@ -44,7 +33,7 @@ def get_import_for_tool(self, tool: ToolConfig) -> ast.Import:
4433 raises a ValidationError if the tool is imported multiple times.
4534 """
4635 all_imports = astools .get_all_imports (self .tree )
47- tool_imports = [i for i in all_imports if tool .name in i .names [ 0 ]. name ]
36+ tool_imports = [i for i in all_imports if tool .module_name == i .module ]
4837
4938 if len (tool_imports ) > 1 :
5039 raise ValidationError (f"Multiple imports for tool { tool .name } found in { self .filename } " )
@@ -77,106 +66,100 @@ def remove_import_for_tool(self, framework: str, tool: ToolConfig):
7766 Remove an import for a tool.
7867 raises a ValidationError if the tool is not imported.
7968 """
80- tool_import = self .get_imports_for_tool (tool )
69+ tool_import = self .get_import_for_tool (tool )
8170 if not tool_import :
8271 raise ValidationError (f"Tool { tool .name } not imported in { self .filename } " )
8372
8473 start , end = self .get_node_range (tool_import )
8574 self .edit_node_range (start , end , "" )
8675
8776
88- def add_tool (tool_name : str , agents : Optional [List [str ]] = [], path : Optional [str ] = None ):
89- if path :
90- path = path .endswith ('/' ) and path or path + '/'
91- else :
92- path = './'
93-
94- framework = get_framework (path )
77+ def add_tool (tool_name : str , agents : Optional [list [str ]] = [], path : Optional [Path ] = None ):
78+ if path is None : path = Path ()
9579 agentstack_config = ConfigFile (path )
80+ framework = agentstack_config .framework
9681
9782 if tool_name in agentstack_config .tools :
9883 print (term_color (f'Tool { tool_name } is already installed' , 'red' ))
9984 sys .exit (1 )
10085
101- tool_data = ToolConfig .from_tool_name (tool_name )
102- tool_file_path = tool_data .get_impl_file_path (framework )
86+ tool = ToolConfig .from_tool_name (tool_name )
87+ tool_file_path = tool .get_impl_file_path (framework )
10388
104- if tool_data .packages :
105- packaging .install (' ' .join (tool_data .packages ))
106- shutil .copy (tool_file_path , f'{ path } src/tools/{ tool_name } _tool.py' ) # Move tool from package to project
89+ if tool .packages :
90+ packaging .install (' ' .join (tool .packages ))
91+
92+ # Move tool from package to project
93+ shutil .copy (tool_file_path , path / f'src/tools/{ tool .module_name } .py' )
10794
10895 try : # Edit the user's project tool init file to include the tool
10996 with ToolsInitFile (path / TOOLS_INIT_FILENAME ) as tools_init :
110- tools_init .add_import_for_tool (tool_data )
97+ tools_init .add_import_for_tool (framework , tool )
11198 except ValidationError as e :
11299 print (term_color (f"Error adding tool:\n { e } " , 'red' ))
113- sys .exit (1 )
114100
115101 # Edit the framework entrypoint file to include the tool in the agent definition
116- if not len ( agents ) : # If no agents are specified, add the tool to all agents
102+ if not agents : # If no agents are specified, add the tool to all agents
117103 agents = frameworks .get_agent_names (framework , path )
118104 for agent_name in agents :
119- frameworks .add_tool (framework , tool_data , agent_name , path )
105+ frameworks .add_tool (framework , tool , agent_name , path )
120106
121- if tool_data .env : # add environment variables which don't exist
107+ if tool .env : # add environment variables which don't exist
122108 with EnvFile (path ) as env :
123- for var , value in tool_data .env .items ():
109+ for var , value in tool .env .items ():
124110 env .append_if_new (var , value )
125111 with EnvFile (path , filename = ".env.example" ) as env :
126- for var , value in tool_data .env .items ():
112+ for var , value in tool .env .items ():
127113 env .append_if_new (var , value )
128114
129- if tool_data .post_install :
130- os .system (tool_data .post_install )
115+ if tool .post_install :
116+ os .system (tool .post_install )
131117
132118 with agentstack_config as config :
133- config .tools .append (tool_name )
134-
135- print (term_color (f'🔨 Tool { tool_name } added to agentstack project successfully' , 'green' ))
136- if tool_data .cta :
137- print (term_color (f'🪩 { tool_data .cta } ' , 'blue' ))
119+ config .tools .append (tool .name )
138120
121+ print (term_color (f'🔨 Tool { tool .name } added to agentstack project successfully' , 'green' ))
122+ if tool .cta :
123+ print (term_color (f'🪩 { tool .cta } ' , 'blue' ))
139124
140- def remove_tool (tool_name : str , agents : Optional [List [str ]] = [], path : Optional [str ] = None ):
141- if path :
142- path = path .endswith ('/' ) and path or path + '/'
143- else :
144- path = './'
145125
146- framework = get_framework ()
126+ def remove_tool (tool_name : str , agents : Optional [list [str ]] = [], path : Optional [Path ] = None ):
127+ if path is None : path = Path ()
147128 agentstack_config = ConfigFile (path )
129+ framework = agentstack_config .framework
148130
149131 if not tool_name in agentstack_config .tools :
150132 print (term_color (f'Tool { tool_name } is not installed' , 'red' ))
151133 sys .exit (1 )
152134
153- tool_data = ToolConfig .from_tool_name (tool_name )
154- if tool_data .packages :
155- packaging .remove (' ' .join (tool_data .packages ))
135+ tool = ToolConfig .from_tool_name (tool_name )
136+ if tool .packages :
137+ packaging .remove (' ' .join (tool .packages ))
138+
156139 try :
157- os .remove (f' { path } src/tools/{ tool_name } _tool .py' )
140+ os .remove (path / f' src/tools/{ tool . module_name } .py' )
158141 except FileNotFoundError :
159- print (f'"src/tools/{ tool_name } _tool .py" not found' )
142+ print (f'"src/tools/{ tool . module_name } .py" not found' )
160143
161144 try : # Edit the user's project tool init file to exclude the tool
162145 with ToolsInitFile (path / TOOLS_INIT_FILENAME ) as tools_init :
163- tools_init .remove_import_for_tool (tool_data )
146+ tools_init .remove_import_for_tool (framework , tool )
164147 except ValidationError as e :
165148 print (term_color (f"Error removing tool:\n { e } " , 'red' ))
166- sys .exit (1 )
167149
168150 # Edit the framework entrypoint file to exclude the tool in the agent definition
169- if not len ( agents ) : # If no agents are specified, remove the tool from all agents
151+ if not agents : # If no agents are specified, remove the tool from all agents
170152 agents = frameworks .get_agent_names (framework , path )
171153 for agent_name in agents :
172- frameworks .remove_tool (framework , tool_data , agent_name , path )
154+ frameworks .remove_tool (framework , tool , agent_name , path )
173155
174- if tool_data .post_remove :
175- os .system (tool_data .post_remove )
156+ if tool .post_remove :
157+ os .system (tool .post_remove )
176158 # We don't remove the .env variables to preserve user data.
177159
178160 with agentstack_config as config :
179- config .tools .remove (tool_name )
161+ config .tools .remove (tool . name )
180162
181- print (term_color (f'🔨 Tool { tool_name } ' , 'green' ), term_color ('removed' , 'red' ), term_color ('from agentstack project successfully' , 'green' ))
163+ print (term_color (f'🔨 Tool { tool_name } ' , 'green' ), term_color ('removed' , 'red' ),
164+ term_color ('from agentstack project successfully' , 'green' ))
182165
0 commit comments