88from pydantic import BaseModel , ValidationError
99
1010from agentstack .utils import get_package_path
11+ from agentstack .generation .files import ConfigFile , EnvFile
1112from .gen_utils import insert_code_after_tag , string_in_file
1213from ..utils import open_json_file , get_framework , term_color
1314
1415
1516TOOL_INIT_FILENAME = "src/tools/__init__.py"
16- AGENTSTACK_JSON_FILENAME = "agentstack.json"
17+
1718FRAMEWORK_FILENAMES : dict [str , str ] = {
1819 'crewai' : 'src/crew.py' ,
1920}
@@ -87,9 +88,9 @@ def add_tool(tool_name: str, path: Optional[str] = None):
8788 path = './'
8889
8990 framework = get_framework (path )
90- agentstack_json = open_json_file ( f' { path } { AGENTSTACK_JSON_FILENAME } ' )
91+ agentstack_config = ConfigFile ( path )
9192
92- if tool_name in agentstack_json . get ( ' tools' , []) :
93+ if tool_name in agentstack_config . tools :
9394 print (term_color (f'Tool { tool_name } is already installed' , 'red' ))
9495 sys .exit (1 )
9596
@@ -100,27 +101,20 @@ def add_tool(tool_name: str, path: Optional[str] = None):
100101 shutil .copy (tool_file_path , f'{ path } src/tools/{ tool_name } _tool.py' ) # Move tool from package to project
101102 add_tool_to_tools_init (tool_data , path ) # Export tool from tools dir
102103 add_tool_to_agent_definition (framework , tool_data , path ) # Add tool to agent definition
103- if tool_data .env : # if the env vars aren't in the .env files, add them
104- # tool_data.env is a dict, key is the env var name, value is the value
105- for var , value in tool_data .env .items ():
106- env_var = f'{ var } ={ value } '
107- if not string_in_file (f'{ path } .env' , env_var ):
108- insert_code_after_tag (f'{ path } .env' , '# Tools' , [env_var , ])
109- if not string_in_file (f'{ path } .env.example' , env_var ):
110- insert_code_after_tag (f'{ path } .env.example' , '# Tools' , [env_var , ])
111104
112- if tool_data .post_install :
113- os .system (tool_data .post_install )
105+ if tool_data .env : # add environment variables which don't exist
106+ with EnvFile (path ) as env :
107+ for var , value in tool_data .env .items ():
108+ env .append_if_new (var , value )
109+ with EnvFile (path , filename = ".env.example" ) as env :
110+ for var , value in tool_data .env .items ():
111+ env .append_if_new (var , value )
114112
115113 if tool_data .post_install :
116114 os .system (tool_data .post_install )
117-
118- if not agentstack_json .get ('tools' ):
119- agentstack_json ['tools' ] = []
120- agentstack_json ['tools' ].append (tool_name )
121115
122- with open ( f' { path } { AGENTSTACK_JSON_FILENAME } ' , 'w' ) as f :
123- json . dump ( agentstack_json , f , indent = 4 )
116+ with agentstack_config as config :
117+ config . tools . append ( tool_name )
124118
125119 print (term_color (f'🔨 Tool { tool_name } added to agentstack project successfully' , 'green' ))
126120 if tool_data .cta :
@@ -133,9 +127,9 @@ def remove_tool(tool_name: str, path: Optional[str] = None):
133127 path = './'
134128
135129 framework = get_framework ()
136- agentstack_json = open_json_file ( f' { path } { AGENTSTACK_JSON_FILENAME } ' )
130+ agentstack_config = ConfigFile ( path )
137131
138- if not tool_name in agentstack_json . get ( ' tools' , []) :
132+ if not tool_name in agentstack_config . tools :
139133 print (term_color (f'Tool { tool_name } is not installed' , 'red' ))
140134 sys .exit (1 )
141135
@@ -152,9 +146,8 @@ def remove_tool(tool_name: str, path: Optional[str] = None):
152146 os .system (tool_data .post_remove )
153147 # We don't remove the .env variables to preserve user data.
154148
155- agentstack_json ['tools' ].remove (tool_name )
156- with open (f'{ path } { AGENTSTACK_JSON_FILENAME } ' , 'w' ) as f :
157- json .dump (agentstack_json , f , indent = 4 )
149+ with agentstack_config as config :
150+ config .tools .remove (tool_name )
158151
159152 print (term_color (f'🔨 Tool { tool_name } ' , 'green' ), term_color ('removed' , 'red' ), term_color ('from agentstack project successfully' , 'green' ))
160153
0 commit comments