14
14
import os
15
15
import subprocess
16
16
import time
17
- import litellm # Main libray for LLM's
17
+ import litellm # Main libray for LLM's
18
18
from typing import List
19
19
from libs .code_interpreter import CodeInterpreter
20
20
from libs .history_manager import History
25
25
from dotenv import load_dotenv
26
26
import shlex
27
27
import shutil
28
+ import logging
28
29
29
30
class Interpreter :
30
31
logger = None
@@ -40,7 +41,7 @@ def __init__(self, args):
40
41
self .code_interpreter = CodeInterpreter ()
41
42
self .package_manager = PackageManager ()
42
43
self .history_manager = History (self .history_file )
43
- self .logger = Logger .initialize_logger ("logs/interpreter.log" )
44
+ self .logger = Logger .initialize ("logs/interpreter.log" )
44
45
self .client = None
45
46
self .config_values = None
46
47
self .system_message = ""
@@ -188,7 +189,7 @@ def get_prompt(self, message: str, chat_history: List[dict]) -> str:
188
189
189
190
# Use the Messages API from Anthropic.
190
191
if 'claude-3' in self .INTERPRETER_MODEL :
191
- messages = [
192
+ messages = [
192
193
{
193
194
"role" : "user" ,
194
195
"content" : [
@@ -212,15 +213,16 @@ def get_prompt(self, message: str, chat_history: List[dict]) -> str:
212
213
213
214
def execute_last_code (self , os_name ):
214
215
try :
215
- code_file ,code_snippet = self .utility_manager .get_code_history ( self .INTERPRETER_LANGUAGE )
216
-
216
+ code_file , code_snippet = self .utility_manager .get_output_history ( mode = self . INTERPRETER_MODE , os_name = os_name , language = self .INTERPRETER_LANGUAGE )
217
+
217
218
# check if the code is empty
218
- if code_snippet is None :
219
- self .logger .error ("Code history is empty." )
220
- print ("Code history is empty. - Please use -s or --save_code to save the code." )
219
+ if code_snippet is None or code_file is None :
220
+ self .logger .error ("Code history or file is empty." )
221
+ display_markdown_message ("Code history or file is empty. - Please use **-s** flag or **/save** command to save the code." )
221
222
return
222
223
223
- display_code (code_snippet )
224
+ display_code (code_snippet ) # Display the code first.
225
+
224
226
# Execute the code if the user has selected.
225
227
code_output , code_error = self .execute_code (code_snippet , os_name )
226
228
if code_output :
@@ -449,19 +451,19 @@ def get_mode_prompt(self, task, os_name):
449
451
450
452
def execute_code (self , extracted_code , os_name ):
451
453
# If the interpreter mode is Vision, do not execute the code.
452
- if self .INTERPRETER_MODE in ['vision' ,'chat' ]:
454
+ if self .INTERPRETER_MODE in ['vision' , 'chat' ]:
453
455
return None , None
454
456
455
457
execute = 'y' if self .EXECUTE_CODE else input ("Execute the code? (Y/N): " )
456
458
if execute .lower () == 'y' :
457
459
try :
458
460
code_output , code_error = "" , ""
459
461
if self .SCRIPT_MODE :
460
- code_output , code_error = self .code_interpreter .execute_script (extracted_code , os_type = os_name )
462
+ code_output , code_error = self .code_interpreter .execute_script (script = extracted_code , os_type = os_name )
461
463
elif self .COMMAND_MODE :
462
- code_output , code_error = self .code_interpreter .execute_command (extracted_code )
464
+ code_output , code_error = self .code_interpreter .execute_command (command = extracted_code )
463
465
elif self .CODE_MODE :
464
- code_output , code_error = self .code_interpreter .execute_code (extracted_code , language = self .INTERPRETER_LANGUAGE )
466
+ code_output , code_error = self .code_interpreter .execute_code (code = extracted_code , language = self .INTERPRETER_LANGUAGE )
465
467
return code_output , code_error
466
468
except Exception as exception :
467
469
self .logger .error (f"Error occurred while executing code: { str (exception )} " )
@@ -623,20 +625,21 @@ def interpreter_main(self, version):
623
625
continue
624
626
625
627
# LOG - Command section.
626
- elif task .lower () == '/log ' :
627
- # Toggle the log level to Verbose/Silent.
628
-
628
+ elif task .lower () == '/debug ' :
629
+
630
+ # Toggle the log level to Debug/Silent.
629
631
logger_mode = Logger .get_current_level ()
630
632
logger_mode = logger_mode .lower ()
631
-
632
- if logger_mode == 'debug' :
633
- Logger .set_silent_mode ()
634
- display_markdown_message ("Logger mode changed to **Silent**." )
633
+ print ("Logger mode: {}" .format (logger_mode ))
634
+
635
+ if logger_mode == 'error' :
636
+ Logger .set_level_to_info ()
637
+ display_markdown_message ("**Debug** mode **disabled**" )
635
638
else :
636
- Logger .set_verbose_mode ()
637
- display_markdown_message ("Logger mode changed to **Verbose**. " )
639
+ Logger .set_level_to_debug ()
640
+ display_markdown_message ("**Debug** mode **enabled** " )
638
641
continue
639
-
642
+
640
643
# LIST - Command section.
641
644
elif task .lower () == '/list' :
642
645
# Get the models info
@@ -673,26 +676,66 @@ def interpreter_main(self, version):
673
676
674
677
# EXECUTE - Command section.
675
678
elif task .lower () == '/execute' :
679
+ os_name = os_platform [0 ].lower ()
676
680
self .execute_last_code (os_name )
677
681
continue
678
682
679
683
# SAVE - Command section.
680
684
elif task .lower () == '/save' :
681
- latest_code_extension = 'py' if self .INTERPRETER_LANGUAGE == 'python' else 'js'
682
- latest_code_name = f"output/code_{ time .strftime ('%Y_%m_%d-%H_%M_%S' , time .localtime ())} ." + latest_code_extension
685
+ mode_type = self .INTERPRETER_MODE
686
+ latest_code_extension : str = ""
687
+
688
+ # Get the OS platform.
689
+ os_name = os_platform [0 ].lower ()
690
+
691
+ # Set the script or command extension based on the OS platform and mode type
692
+ extensions = {
693
+ "script" : {
694
+ "darwin" : ".applescript" ,
695
+ "linux" : ".sh" ,
696
+ "windows" : ".bat"
697
+ },
698
+ "command" : {
699
+ "darwin" : ".sh" ,
700
+ "linux" : ".sh" ,
701
+ "windows" : ".bat"
702
+ },
703
+ "code" : lambda lang : '.py' if lang == 'python' else '.js'
704
+ }
705
+
706
+ lower_os_name = os_name .lower ()
707
+ if mode_type .lower () in extensions :
708
+ if mode_type .lower () == "code" :
709
+ latest_code_extension = extensions ["code" ](self .INTERPRETER_LANGUAGE )
710
+ else :
711
+ for key in extensions [mode_type .lower ()]:
712
+ if key in lower_os_name :
713
+ latest_code_extension = extensions [mode_type .lower ()][key ]
714
+ break
715
+ else :
716
+ raise ValueError (f"Unsupported operating system: { os_name } " )
717
+ else :
718
+ raise ValueError (f"Unsupported mode type: { mode_type } " )
719
+
720
+ latest_code_name = f"output/{ mode_type } _{ time .strftime ('%Y_%m_%d-%H_%M_%S' , time .localtime ())} " + latest_code_extension
683
721
latest_code = code_snippet
684
722
self .code_interpreter .save_code (latest_code_name , latest_code )
685
- display_markdown_message (f"Code saved successfully to { latest_code_name } . " )
723
+ display_markdown_message (f"{ mode_type . capitalize () } saved successfully to ** { latest_code_name } ** " )
686
724
continue
687
725
688
726
# EDIT - Command section.
689
727
elif task .lower () == '/edit' :
690
- code_file , code_snippet = self .utility_manager .get_code_history (self .INTERPRETER_LANGUAGE )
691
-
692
728
# Get the OS platform.
693
- os_platform = self .utility_manager .get_os_platform ()
694
729
os_name = os_platform [0 ].lower ()
730
+
731
+ code_file , code_snippet = self .utility_manager .get_output_history (mode = self .INTERPRETER_MODE , os_name = os_name , language = self .INTERPRETER_LANGUAGE )
695
732
733
+ # check if the code is empty
734
+ if code_snippet is None or code_file is None :
735
+ self .logger .error ("Code history or file is empty." )
736
+ display_markdown_message ("Code history or file is empty. - Please use **-s** flag or **/save** command to save the code." )
737
+ continue
738
+
696
739
# Attempt to open with default editor.
697
740
self .logger .info (f"Opening code in default editor for os '{ os_platform } '" )
698
741
try :
@@ -714,8 +757,8 @@ def interpreter_main(self, version):
714
757
self .logger .error ("No suitable editor found." )
715
758
continue
716
759
717
- # DEBUG - Command section.
718
- elif task .lower () == '/debug ' :
760
+ # FIX - Command section.
761
+ elif task .lower () == '/fix ' :
719
762
720
763
if not code_error :
721
764
code_error = code_output
@@ -724,12 +767,12 @@ def interpreter_main(self, version):
724
767
display_markdown_message ("Error: No error found in the code to fix." )
725
768
continue
726
769
727
- debug_prompt = f"Fix the errors in { self .INTERPRETER_LANGUAGE } language.\n Code is \n '{ code_snippet } '\n And Error is \n '{ code_error } '\n "
770
+ fix_prompt = f"Fix the errors in { self .INTERPRETER_LANGUAGE } language.\n Code is \n '{ code_snippet } '\n And Error is \n '{ code_error } '\n "
728
771
f"give me output only in code and no other text or explanation. And comment in code where you fixed the error.\n "
729
772
730
773
# Start the LLM Request.
731
- self .logger .info (f"Debug Prompt: { debug_prompt } " )
732
- generated_output = self .generate_content (debug_prompt , self .history , config_values = self .config_values , image_file = extracted_file_name )
774
+ self .logger .info (f"Fix Prompt: { fix_prompt } " )
775
+ generated_output = self .generate_content (fix_prompt , self .history , config_values = self .config_values , image_file = extracted_file_name )
733
776
734
777
# Extract the code from the generated output.
735
778
self .logger .info (f"Generated output type { type (generated_output )} " )
@@ -910,7 +953,7 @@ def interpreter_main(self, version):
910
953
prompt += "\n " + "using Python use Pandas save the table in file called 'table.md'"
911
954
elif self .INTERPRETER_LANGUAGE == 'javascript' :
912
955
prompt += "\n " + "using JavaScript use DataTables save the table in file called 'table.html'"
913
-
956
+
914
957
# Start the LLM Request.
915
958
self .logger .info (f"Prompt: { prompt } " )
916
959
@@ -953,11 +996,11 @@ def interpreter_main(self, version):
953
996
elif self .SAVE_CODE and self .COMMAND_MODE :
954
997
self .code_interpreter .save_code (f"output/command_{ current_time } .txt" , code_snippet )
955
998
self .logger .info ("Command saved successfully." )
956
-
999
+
957
1000
elif self .SAVE_CODE and self .SCRIPT_MODE :
958
1001
self .code_interpreter .save_code (f"output/script_{ current_time } .txt" , code_snippet )
959
1002
self .logger .info ("Script saved successfully." )
960
-
1003
+
961
1004
# Execute the code if the user has selected.
962
1005
code_output , code_error = self .execute_code (code_snippet , os_name )
963
1006
0 commit comments