Tachi67 commited on
Commit
f23eecb
·
1 Parent(s): 11634bd

Upload 3 files

Browse files
CodeFileEditAtomicFlow.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import subprocess
2
+ import webbrowser
3
+ from typing import Dict, Any
4
+ from flows.base_flows.atomic import AtomicFlow
5
+ import os
6
+
7
+ class CodeFileEditorAtomicFlow(AtomicFlow):
8
+ def _write_code_to_temp_file(self, code_str, language_of_code, code_lib_location):
9
+ directory = os.path.dirname(code_lib_location)
10
+ if language_of_code.lower() == 'python':
11
+ temp_file_location = os.path.join(directory, 'temp.py')
12
+ else:
13
+ raise NotImplemented
14
+ try:
15
+ content = (
16
+ "The below code will be appended to " +
17
+ code_lib_location + "\n"
18
+ "Edit the code directly or provide your thoughts down below if you have any suggestions.\n"
19
+ "If you prefer not to give any thoughts, just leave it blank, the code will be added to the library as it is "
20
+ "in this file.\n"
21
+ "###########\n"
22
+ "Code:\n" +
23
+ code_str +
24
+ "\n############\n"
25
+ "Thoughts:"
26
+ )
27
+ with open(temp_file_location, "w") as file:
28
+ file.write(content)
29
+ return True, f"Code written to {temp_file_location}", temp_file_location
30
+
31
+ except Exception as e:
32
+ return False, str(e), temp_file_location
33
+
34
+ def _check_input(self, input_data: Dict[str, Any]):
35
+ assert "code" in input_data, "code is not passed to CodeFileEditAtomicFlow"
36
+ assert "language_of_code" in input_data, "language_of_code is not passed to CodeFileEditAtomicFlow"
37
+ assert "memory_files" in input_data, "memory_files is not passed to CodeFileEditAtomicFlow"
38
+ assert "code_library" in input_data["memory_files"], "code_library not in memory files"
39
+ code_lib_loc = input_data["memory_files"]["code_library"]
40
+ assert os.path.exists(code_lib_loc), f"{code_lib_loc} does not exist"
41
+ assert os.path.isfile(code_lib_loc), f"{code_lib_loc} is not a file"
42
+
43
+ def run(
44
+ self,
45
+ input_data: Dict[str, Any]
46
+ ):
47
+ self._check_input(input_data)
48
+ code_str = input_data['code']
49
+ language_of_code = input_data["language_of_code"]
50
+ code_lib_location = input_data["memory_files"]["code_library"]
51
+ result, code_editor_output, temp_file_location = self._write_code_to_temp_file(code_str, language_of_code, code_lib_location)
52
+ if result:
53
+ try:
54
+ subprocess.run(["code", temp_file_location], timeout=10)
55
+ except (subprocess.CalledProcessError, subprocess.TimeoutExpired):
56
+ webbrowser.open(temp_file_location)
57
+ response = {}
58
+ response["code_editor_output"] = code_editor_output
59
+ response["temp_code_file_location"] = temp_file_location
60
+ return response
CodeFileEditAtomicFlow.yaml ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ name: "CodeFileEditorAtomicFlow"
2
+ description: "A flow that writes code to a temp file"
3
+
4
+ input_interface:
5
+ - "code"
6
+ - "language_of_code"
7
+ - "memory_files"
8
+
__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ from .CodeFileEditAtomicFlow import CodeFileEditorAtomicFlow