File size: 2,301 Bytes
bd7cdc8
c7b9e2d
bd7cdc8
 
 
ac5f9e9
 
 
 
 
 
 
 
 
bd7cdc8
ac5f9e9
 
 
 
bd7cdc8
 
 
 
 
 
 
 
 
ac5f9e9
 
 
 
bd7cdc8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac5f9e9
 
 
 
bd7cdc8
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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)