from typing import Dict, Any from aiflows.base_flows.atomic import AtomicFlow class SaveCodeAtomicFlow(AtomicFlow): """This flow appends the code to the code library file. *Input Interface*: - `code` (str): the code to be appended to the code library - `memory_files` (dict): the dictionary of memory files *Output Interface*: - `result` (str): the result of the flow, to be returned to the controller of the caller - `summary` (str): the summary of the flow, to be appended to logs """ def _check_input(self, input_data: Dict[str, Any]): """Check if the input data is valid :param input_data: the input data :return: None """ assert "code" in input_data, "code is not passed to SaveCodeAtomicFlow" assert "memory_files" in input_data, "memory_files is not passed to SaveCodeFlow" assert "code_library" in input_data["memory_files"], "code_library not in memory_files" # create the code library if it doesn't exist yet code_library_location = input_data["memory_files"]["code_library"] with open(code_library_location, 'a') as file: pass def _call(self, input_data: Dict[str, Any]): """The internal logic of the flow :param input_data: the input data :return: the output data """ try: code_to_append = input_data["code"] code_lib_loc = input_data["memory_files"]["code_library"] with open(code_lib_loc, 'a') as file: file.write(code_to_append + '\n') return { "result": "code successfully written to the library", "summary": f"ExtendLibrayFlow/SaveCodeAtomicFlow: code written to {code_lib_loc}" } except Exception as e: error_msg = str(e) return { "result": f"error occurred: {error_msg}", "summary": f"ExtendLibrayFlow/SaveCodeAtomicFlow: error occurred: {error_msg}" } def run( self, input_data: Dict[str, Any] ): """Run the flow :param input_data: the input data :return: the output data """ self._check_input(input_data) return self._call(input_data)