@@ -69,9 +69,9 @@ def to_v3(self) -> 'TemplateConfig':
6969 framework = self .framework ,
7070 method = self .method ,
7171 manager_agent = None ,
72- agents = [TemplateConfig .Agent (** agent .dict ()) for agent in self .agents ],
73- tasks = [TemplateConfig .Task (** task .dict ()) for task in self .tasks ],
74- tools = [TemplateConfig .Tool (** tool .dict ()) for tool in self .tools ],
72+ agents = [TemplateConfig .Agent (** agent .model_dump ()) for agent in self .agents ],
73+ tasks = [TemplateConfig .Task (** task .model_dump ()) for task in self .tasks ],
74+ tools = [TemplateConfig .Tool (** tool .model_dump ()) for tool in self .tools ],
7575 inputs = self .inputs ,
7676 )
7777
@@ -144,17 +144,22 @@ def write_to_file(self, filename: Path):
144144 f .write (json .dumps (model_dump , indent = 4 ))
145145
146146 @classmethod
147- def from_template_name (cls , name : str ) -> 'TemplateConfig' :
148- # if url
149- if name .startswith ('https://' ):
150- return cls .from_url (name )
151-
152- # if .json file
153- if name .endswith ('.json' ):
154- path = os .getcwd () / Path (name )
147+ def from_user_input (cls , identifier : str ):
148+ """
149+ Load a template from a user-provided identifier.
150+ Three cases will be tried: A URL, a file path, or a template name.
151+ """
152+ if identifier .startswith ('https://' ):
153+ return cls .from_url (identifier )
154+
155+ if identifier .endswith ('.json' ):
156+ path = Path () / identifier
155157 return cls .from_file (path )
156158
157- # if named template
159+ return cls .from_template_name (identifier )
160+
161+ @classmethod
162+ def from_template_name (cls , name : str ) -> 'TemplateConfig' :
158163 path = get_package_path () / f'templates/proj_templates/{ name } .json'
159164 if not name in get_all_template_names ():
160165 raise ValidationError (f"Template { name } not bundled with agentstack." )
@@ -164,8 +169,11 @@ def from_template_name(cls, name: str) -> 'TemplateConfig':
164169 def from_file (cls , path : Path ) -> 'TemplateConfig' :
165170 if not os .path .exists (path ):
166171 raise ValidationError (f"Template { path } not found." )
167- with open (path , 'r' ) as f :
168- return cls .from_json (json .load (f ))
172+ try :
173+ with open (path , 'r' ) as f :
174+ return cls .from_json (json .load (f ))
175+ except json .JSONDecodeError as e :
176+ raise ValidationError (f"Error decoding template JSON.\n { e } " )
169177
170178 @classmethod
171179 def from_url (cls , url : str ) -> 'TemplateConfig' :
@@ -174,7 +182,10 @@ def from_url(cls, url: str) -> 'TemplateConfig':
174182 response = requests .get (url )
175183 if response .status_code != 200 :
176184 raise ValidationError (f"Failed to fetch template from { url } " )
177- return cls .from_json (response .json ())
185+ try :
186+ return cls .from_json (response .json ())
187+ except json .JSONDecodeError as e :
188+ raise ValidationError (f"Error decoding template JSON.\n { e } " )
178189
179190 @classmethod
180191 def from_json (cls , data : dict ) -> 'TemplateConfig' :
@@ -193,8 +204,6 @@ def from_json(cls, data: dict) -> 'TemplateConfig':
193204 for error in e .errors ():
194205 err_msg += f"{ ' ' .join ([str (loc ) for loc in error ['loc' ]])} : { error ['msg' ]} \n "
195206 raise ValidationError (err_msg )
196- except json .JSONDecodeError as e :
197- raise ValidationError (f"Error decoding template JSON.\n { e } " )
198207
199208
200209def get_all_template_paths () -> list [Path ]:
0 commit comments