@@ -24,28 +24,37 @@ class AssistantConfigManager:
2424 """
2525 A class to manage the creation, updating, deletion, and loading of assistant configurations from local files.
2626
27- :param config_folder: The folder path for storing configuration files. Optional, defaults to ' config' .
27+ :param config_folder: The folder path for storing configuration files. Optional, defaults to config folder in the user's home directory .
2828 :type config_folder: str
2929 """
3030 def __init__ (
3131 self ,
32- config_folder : str = 'config'
32+ config_folder : Optional [ str ] = None
3333 ) -> None :
34- self ._config_folder = config_folder
34+ if config_folder is None :
35+ self ._config_folder = self ._default_config_path ()
36+ else :
37+ self ._config_folder = config_folder
3538 self ._last_modified_assistant_name = None
3639 self ._configs : dict [str , AssistantConfig ] = {}
3740 # Load all assistant configurations under the config folder
3841 self .load_configs ()
3942
43+ @staticmethod
44+ def _default_config_path () -> str :
45+ home = os .path .expanduser ("~" )
46+ return os .path .join (home , ".config" , 'azure-ai-assistant' )
47+
4048 @classmethod
4149 def get_instance (
4250 cls ,
43- config_folder : str = 'config'
51+ config_folder : Optional [str ] = None
52+
4453 ) -> 'AssistantConfigManager' :
4554 """
4655 Gets the singleton instance of the AssistantConfigManager object.
4756
48- :param config_folder: The folder path for storing configuration files. Optional, defaults to ' config' .
57+ :param config_folder: The folder path for storing configuration files. Optional, defaults to config folder in the user's home directory .
4958 :type config_folder: str
5059
5160 :return: The singleton instance of the AssistantConfigManager object.
@@ -61,8 +70,8 @@ def update_config(
6170 config_json : str
6271 ) -> str :
6372 """
64- Updates an existing assistant local configuration.
65-
73+ Updates an existing assistant local configuration in memory .
74+
6675 :param name: The name of the configuration to update.
6776 :type name: str
6877 :param config_json: The JSON string containing the updated configuration data.
@@ -75,8 +84,11 @@ def update_config(
7584 logger .info (f"Updating assistant configuration for '{ name } ' with data: { config_json } " )
7685 new_config_data = json .loads (config_json )
7786 self ._validate_config (new_config_data )
78- name = self ._save_config (name , new_config_data )
87+
88+ # Update the configuration in memory without saving to a file
89+ self ._configs [name ] = AssistantConfig (new_config_data )
7990 self ._last_modified_assistant_name = name
91+
8092 return name
8193 except json .JSONDecodeError as e :
8294 raise InvalidJSONError (f"Invalid JSON format: { e } " )
@@ -136,9 +148,6 @@ def get_config(
136148 logger .warning (f"No configuration found for '{ name } '" )
137149 return None
138150
139- # ensure the configurations are up-to-date
140- self ._load_config (name )
141-
142151 # Return the AssistantConfig object for the given name
143152 return self ._configs .get (name , None )
144153
@@ -209,13 +218,19 @@ def _set_last_modified_assistant(self):
209218
210219 self ._last_modified_assistant_name = latest_assistant_name
211220
212- def save_configs (self ) -> None :
221+ def save_configs (
222+ self ,
223+ config_folder : Optional [str ] = None
224+ ) -> None :
213225 """
214226 Saves all assistant local configurations to json files.
227+
228+ :param config_folder: The folder path where the configuration files should be saved. Optional, defaults to the config folder.
229+ :type config_folder: str
215230 """
216231 # Save all assistant configurations to files
217232 for assistant_name , assistant_config in self ._configs .items ():
218- self ._save_config (assistant_name , assistant_config . _get_config_data () )
233+ self .save_config (assistant_name , config_folder or self . _config_folder )
219234
220235 def get_last_modified_assistant (self ) -> str :
221236 """
@@ -296,71 +311,39 @@ def _validate_config(self, config_data):
296311 if 'tool_resources' in config_data and config_data .get ('tool_resources' ) is not None and not isinstance (config_data ['tool_resources' ], dict ):
297312 raise ConfigError ("Assistant 'tool_resources' must be a dictionary in the configuration" )
298313
299- def _save_config (self , assistant_name , config_data ):
300- # Check if the assistant name and configuration data are provided
301- if not assistant_name :
302- raise ConfigError ("Assistant name is required" )
303-
304- if not config_data :
305- raise ConfigError ("Assistant configuration data is required" )
306-
307- logger .info (f"Checking for updates in assistant configuration for '{ assistant_name } '" )
308-
309- # Handle possible change in assistant name within the configuration data
310- if 'name' in config_data and config_data ['name' ] != assistant_name :
311- logger .info (f"Assistant name changed from '{ assistant_name } ' to \" { config_data ['name' ]} \" " )
312-
313- # Construct path for potentially existing old configuration files
314- old_json_config_path = os .path .join (self ._config_folder , f"{ assistant_name } _assistant_config.json" )
315- old_yaml_config_path = os .path .join (self ._config_folder , f"{ assistant_name } _assistant_config.yaml" )
316- old_yml_config_path = os .path .join (self ._config_folder , f"{ assistant_name } _assistant_config.yml" )
317-
318- # Attempt to delete old configuration files if they exist
319- for old_path in [old_json_config_path , old_yaml_config_path , old_yml_config_path ]:
320- if os .path .exists (old_path ):
321- try :
322- os .remove (old_path )
323- logger .info (f"Removed outdated configuration file: { old_path } " )
324- except Exception as e :
325- logger .error (f"Error deleting outdated file: { e } " )
326-
327- # Update the assistant name to the new name from the configuration data
328- assistant_name = config_data ['name' ]
314+ def save_config (
315+ self ,
316+ name : str ,
317+ folder_path : Optional [str ] = None
318+ ) -> None :
319+ """
320+ Saves the specified assistant configuration to a file in the given directory.
329321
330- # Define the new YAML file path for saving the configuration
331- config_filename = f"{ assistant_name } _assistant_config.yaml"
332- config_path = os .path .join (self ._config_folder , config_filename )
322+ :param name: The name of the assistant configuration to save.
323+ :type name: str
324+ :param folder_path: The directory path where the configuration file should be saved. Optional, defaults to the config folder.
325+ :type folder_path: str
326+ """
327+ if name not in self ._configs :
328+ raise ConfigError (f"No configuration found for '{ name } '" )
333329
334- # Update in-memory configuration
335- self . _configs [ assistant_name ] = AssistantConfig ( config_data )
336-
337- logger . info ( f"Saving updated configuration for ' { assistant_name } ' in YAML format" )
330+ config_data = self . _configs [ name ]. _get_config_data () # assuming AssistantConfig has a method to get its data
331+ config_filename = f" { name } _assistant_config.yaml"
332+ folder_path = folder_path or self . _config_folder
333+ config_path = os . path . join ( folder_path , config_filename )
338334
339- # Ensure the configuration directory exists
340- if not os .path .exists (self ._config_folder ):
341- try :
342- os .makedirs (self ._config_folder )
343- except Exception as e :
344- logger .error (f"Error creating config directory: { e } " )
345- raise ConfigError (f"Error creating config directory: { e } " )
335+ # Ensure the directory exists
336+ if not os .path .exists (folder_path ):
337+ os .makedirs (folder_path )
346338
347339 # Save the configuration data in YAML format
348340 try :
349341 with open (config_path , 'w' ) as file :
350342 yaml .dump (config_data , file , sort_keys = False )
343+ logger .info (f"Configuration for '{ name } ' saved successfully at '{ config_path } '" )
351344 except Exception as e :
352- logger .error (f"Error writing to YAML file: { e } " )
353- raise ConfigError (f"Error writing to YAML file: { e } " )
354-
355- # Delete the corresponding JSON file if it exists
356- json_config_path = config_path .replace ('.yaml' , '.json' )
357- if os .path .exists (json_config_path ):
358- try :
359- os .remove (json_config_path )
360- logger .info (f"Removed outdated JSON configuration for '{ assistant_name } '" )
361- except Exception as e :
362- logger .error (f"Error deleting outdated JSON file: { e } " )
363- return assistant_name
345+ logger .error (f"Error saving configuration file at '{ config_path } ': { e } " )
346+ raise ConfigError (f"Error saving configuration file: { e } " )
364347
365348 @property
366349 def configs (self ) -> dict :
0 commit comments