Tachi67 commited on
Commit
702d20f
·
1 Parent(s): 90c4605

Upload 3 files

Browse files
PlanFileEditAtomicFlow.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
8
+ class PlanFileEditAtomicFlow(AtomicFlow):
9
+ def _generate_content(self, plan_file_location, plan_str) -> str:
10
+ content = (
11
+ "The below plan will be written to " +
12
+ plan_file_location + "\n"
13
+ "Edit the plan directly or provide your thoughts down below if you have any suggestions.\n"
14
+ "# Please do provide your feedback in the thoughts section so that JARVIS knows what you "
15
+ "are doing.\n"
16
+ "###########\n"
17
+ "# Plan:\n" +
18
+ plan_str +
19
+ "\n############\n"
20
+ "# Thoughts:"
21
+ )
22
+ return content
23
+
24
+ def _generate_temp_file_location(self, plan_file_location):
25
+ directory = os.path.dirname(plan_file_location)
26
+ ret = os.path.join(directory, 'temp_plan.txt')
27
+ return ret
28
+
29
+ def _write_plan_content_to_file(self, file_location, content: str):
30
+ try:
31
+ with open(file_location, "w") as file:
32
+ file.write(content)
33
+ file_written_timestamp = os.path.getmtime(file_location)
34
+
35
+ return True, f"Plan written to {file_location}", file_location, file_written_timestamp
36
+
37
+ except Exception as e:
38
+ return False, str(e), file_location, 0
39
+
40
+ def _check_input(self, input_data: Dict[str, Any]):
41
+ assert "plan" in input_data, "plan is not passed to PlanFileEditAtomicFlow"
42
+ assert "memory_files" in input_data, "memory_files is not passed to PlanFileEditAtomicFlow"
43
+ assert "plan" in input_data["memory_files"], "plan not in memory files"
44
+ plan_file_loc = input_data["memory_files"]["plan"]
45
+ assert os.path.exists(plan_file_loc), f"{plan_file_loc} does not exist"
46
+ assert os.path.isfile(plan_file_loc), f"{plan_file_loc} is not a file"
47
+
48
+ def _generate_input_to_writer(self, input_data: Dict[str, Any]):
49
+ plan_str = input_data['plan']
50
+ plan_file_location = input_data["memory_files"]["plan"]
51
+ content_to_write = self._generate_content(plan_file_location, plan_str)
52
+ file_location_to_write = self._generate_temp_file_location(plan_file_location)
53
+ return content_to_write, file_location_to_write
54
+
55
+ def run(
56
+ self,
57
+ input_data: Dict[str, Any]
58
+ ):
59
+ self._check_input(input_data)
60
+
61
+ # ~~~ Getting input data to the file editor ~~~
62
+ content_to_write, file_location_to_write = self._generate_input_to_writer(input_data)
63
+
64
+ # ~~~ Calling the writer function ~~~
65
+ result, plan_editor_output, temp_file_location, file_written_timestamp = self._write_plan_content_to_file(
66
+ file_location_to_write, content_to_write)
67
+
68
+ # ~~~ Opening up the file for the user to see ~~~
69
+ if result:
70
+ try:
71
+ subprocess.run(["code", temp_file_location], timeout=10)
72
+ except (subprocess.CalledProcessError, subprocess.TimeoutExpired):
73
+ webbrowser.open(temp_file_location)
74
+
75
+ # ~~~ Generating return variables ~~~
76
+ response = {}
77
+ response["plan_editor_output"] = plan_editor_output
78
+ response["temp_plan_file_location"] = temp_file_location
79
+ response["temp_plan_file_written_timestamp"] = file_written_timestamp
80
+ return response
PlanFileEditAtomicFlow.yaml ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: "PlanFileEditAtomicFlow"
2
+ description: "A flow that writes plan to a temp file"
3
+
4
+ input_interface:
5
+ - "plan"
6
+ - "memory_files" # e.g. in Planner of ExtLib, it takes the memory_files of ExtLib flow
7
+
8
+ output_interface:
9
+ - "plan_editor_output"
10
+ - "temp_plan_file_location"
11
+ - "temp_plan_file_written_timestamp"
__init__.py ADDED
@@ -0,0 +1 @@
 
 
1
+ from .PlanFileEditAtomicFlow import PlanFileEditAtomicFlow