-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathinterpreter
More file actions
executable file
·132 lines (111 loc) · 4.97 KB
/
interpreter
File metadata and controls
executable file
·132 lines (111 loc) · 4.97 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python3
# -*- coding: utf-8 -*-*
"""
This is the main file for the Code-Interpreter.
It handles command line arguments and initializes the Interpreter.
Command line arguments:
--exec, -e: Executes the code generated from the user's instructions.
--save_code, -s: Saves the generated code.
--mode, -md: Selects the mode of operation. Choices are 'code', 'script', and 'command' and 'vision'.
--model, -m: Sets the model for code generation.
--version, -v: Displays the version of the program.
--lang, -l: Sets the interpreter language. Default is 'python'.
--display_code, -dc: Displays the generated code in the output.
--sandbox / --no-sandbox: Enable or disable sandbox mode (default: sandbox ON).
Author: HeavenHM
Date: 2025/01/01
"""
from libs.interpreter_lib import Interpreter
import argparse
import sys
import traceback
import warnings
from libs.markdown_code import display_markdown_message
from libs.terminal_ui import TerminalUI
from libs.utility_manager import UtilityManager
# The main version of the interpreter.
INTERPRETER_VERSION = "3.2.2"
def build_parser():
parser = argparse.ArgumentParser(description='Code - Interpreter')
parser.add_argument('--exec', '-e', action='store_true', default=False, help='Execute the code')
parser.add_argument('--save_code', '-s', action='store_true', default=False, help='Save the generated code')
parser.add_argument('--mode', '-md', choices=['code', 'script', 'command', 'vision', 'chat'], help='Select the mode (`code` for generating code, `script` for generating shell scripts, `command` for generating single line commands) `vision` for generating text from images')
parser.add_argument('--model', '-m', type=str, default=None, help='Set the model for code generation. (Defaults to the best configured local provider)')
parser.add_argument('--version', '-v', action='version', version='%(prog)s ' + INTERPRETER_VERSION)
parser.add_argument('--lang', '-l', type=str, default='python', help='Set the interpreter language. (Defaults to Python)')
parser.add_argument('--display_code', '-dc', action='store_true', default=False, help='Display the generated code in output')
parser.add_argument('--history', '-hi', action='store_true', default=False, help='Use history as memory')
parser.add_argument('--upgrade', '-up', action='store_true', default=False, help='Upgrade the interpreter')
parser.add_argument('--file', '-f', type=str, nargs='?', const='prompt.txt', default=None, help='Sets the file to read the input prompt from')
# Sandbox control: --sandbox (default ON) / --no-sandbox (unsafe, disables sandbox+timers)
sandbox_group = parser.add_mutually_exclusive_group()
sandbox_group.add_argument(
'--sandbox',
dest='sandbox',
action='store_true',
help='Enable sandbox mode (default: ON)'
)
sandbox_group.add_argument(
'--no-sandbox',
dest='sandbox',
action='store_false',
help='Disable sandbox (UNSAFE)'
)
# Set default to sandbox mode ON
parser.set_defaults(sandbox=True)
# Legacy --unsafe flag kept for backwards compatibility (maps to --no-sandbox)
parser.add_argument(
"--unsafe",
action='store_true',
default=False,
help=argparse.SUPPRESS # hidden; use --no-sandbox instead
)
mode_group = parser.add_mutually_exclusive_group()
mode_group.add_argument('--cli', action='store_true', default=False, help='Launch the classic interactive CLI')
mode_group.add_argument('--tui', action='store_true', default=False, help='Launch the selector-based terminal UI')
return parser
def _get_default_model():
return UtilityManager.get_default_model_name()
def prepare_args(args, argv):
# --unsafe is a legacy alias for --no-sandbox
if getattr(args, 'unsafe', False):
args.sandbox = False
# sandbox=False means unsafe execution
args.unsafe = not args.sandbox
no_runtime_args = len(argv) <= 1
if no_runtime_args and not args.cli and not args.tui:
args.tui = True
if args.tui:
return TerminalUI().launch(args)
if not args.mode:
args.mode = 'code'
if not args.model:
args.model = _get_default_model()
args.cli = True
return args
def main(argv=None):
argv = argv or sys.argv
parser = build_parser()
args = parser.parse_args(argv[1:])
warnings.filterwarnings("ignore")
if args.upgrade:
UtilityManager.upgrade_interpreter()
return
args = prepare_args(args, argv)
interpreter = Interpreter(args)
interpreter.interpreter_main(INTERPRETER_VERSION)
if __name__ == "__main__":
try:
main()
except SystemExit:
pass
except Exception as exception:
if ".env file" in str(exception):
display_markdown_message("Interpreter is not setup properly. Please follow these steps \
to setup the interpreter:\n\
1. Create a .env file in the root directory of the project.\n\
2. Add the required API keys to the .env file or copy them from .env.example.\n\
3. Run the interpreter again.")
else:
display_markdown_message(f"An error occurred interpreter main: {exception}")
traceback.print_exc()