janito 2.3.0__py3-none-any.whl → 2.3.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- janito/__init__.py +6 -6
- janito/cli/chat_mode/shell/autocomplete.py +21 -21
- janito/cli/chat_mode/shell/commands/clear.py +12 -12
- janito/cli/chat_mode/shell/commands/multi.py +51 -51
- janito/cli/chat_mode/shell/input_history.py +62 -62
- janito/cli/cli_commands/list_models.py +35 -35
- janito/cli/cli_commands/list_providers.py +9 -9
- janito/cli/cli_commands/list_tools.py +53 -53
- janito/cli/cli_commands/model_selection.py +50 -50
- janito/cli/cli_commands/model_utils.py +95 -95
- janito/cli/cli_commands/set_api_key.py +19 -19
- janito/cli/cli_commands/show_config.py +51 -51
- janito/cli/cli_commands/show_system_prompt.py +62 -62
- janito/cli/core/__init__.py +4 -4
- janito/cli/core/event_logger.py +59 -59
- janito/cli/core/getters.py +33 -33
- janito/cli/core/unsetters.py +54 -54
- janito/cli/single_shot_mode/__init__.py +6 -6
- janito/config.py +5 -5
- janito/config_manager.py +112 -112
- janito/drivers/anthropic/driver.py +113 -113
- janito/formatting_token.py +54 -54
- janito/i18n/__init__.py +35 -35
- janito/i18n/messages.py +23 -23
- janito/i18n/pt.py +47 -47
- janito/llm/__init__.py +5 -5
- janito/llm/agent.py +443 -443
- janito/llm/auth.py +63 -63
- janito/llm/driver_config_builder.py +34 -34
- janito/llm/driver_input.py +12 -12
- janito/llm/message_parts.py +60 -60
- janito/llm/model.py +38 -38
- janito/llm/provider.py +196 -196
- janito/provider_registry.py +176 -176
- janito/providers/anthropic/model_info.py +22 -22
- janito/providers/anthropic/provider.py +2 -0
- janito/providers/azure_openai/model_info.py +16 -16
- janito/providers/azure_openai/provider.py +3 -0
- janito/providers/deepseek/__init__.py +1 -1
- janito/providers/deepseek/model_info.py +16 -16
- janito/providers/deepseek/provider.py +94 -91
- janito/providers/google/provider.py +3 -0
- janito/providers/mistralai/provider.py +3 -0
- janito/providers/openai/provider.py +4 -0
- janito/tools/adapters/__init__.py +1 -1
- janito/tools/adapters/local/ask_user.py +102 -102
- janito/tools/adapters/local/copy_file.py +84 -84
- janito/tools/adapters/local/create_directory.py +69 -69
- janito/tools/adapters/local/create_file.py +82 -82
- janito/tools/adapters/local/fetch_url.py +97 -97
- janito/tools/adapters/local/find_files.py +138 -138
- janito/tools/adapters/local/get_file_outline/__init__.py +1 -1
- janito/tools/adapters/local/get_file_outline/core.py +117 -117
- janito/tools/adapters/local/get_file_outline/java_outline.py +40 -40
- janito/tools/adapters/local/get_file_outline/markdown_outline.py +14 -14
- janito/tools/adapters/local/get_file_outline/python_outline.py +303 -303
- janito/tools/adapters/local/get_file_outline/python_outline_v2.py +156 -156
- janito/tools/adapters/local/get_file_outline/search_outline.py +33 -33
- janito/tools/adapters/local/python_code_run.py +166 -166
- janito/tools/adapters/local/python_command_run.py +164 -164
- janito/tools/adapters/local/python_file_run.py +163 -163
- janito/tools/adapters/local/run_bash_command.py +176 -176
- janito/tools/adapters/local/run_powershell_command.py +219 -219
- janito/tools/adapters/local/search_text/__init__.py +1 -1
- janito/tools/adapters/local/search_text/core.py +201 -201
- janito/tools/adapters/local/search_text/pattern_utils.py +73 -73
- janito/tools/adapters/local/search_text/traverse_directory.py +145 -145
- janito/tools/adapters/local/validate_file_syntax/__init__.py +1 -1
- janito/tools/adapters/local/validate_file_syntax/core.py +106 -106
- janito/tools/adapters/local/validate_file_syntax/css_validator.py +35 -35
- janito/tools/adapters/local/validate_file_syntax/html_validator.py +93 -93
- janito/tools/adapters/local/validate_file_syntax/js_validator.py +27 -27
- janito/tools/adapters/local/validate_file_syntax/json_validator.py +6 -6
- janito/tools/adapters/local/validate_file_syntax/markdown_validator.py +109 -109
- janito/tools/adapters/local/validate_file_syntax/ps1_validator.py +32 -32
- janito/tools/adapters/local/validate_file_syntax/python_validator.py +5 -5
- janito/tools/adapters/local/validate_file_syntax/xml_validator.py +11 -11
- janito/tools/adapters/local/validate_file_syntax/yaml_validator.py +6 -6
- janito/tools/adapters/local/view_file.py +167 -167
- janito/tools/inspect_registry.py +17 -17
- janito/tools/tool_base.py +105 -105
- janito/tools/tool_events.py +58 -58
- janito/tools/tool_run_exception.py +12 -12
- janito/tools/tool_use_tracker.py +81 -81
- janito/tools/tool_utils.py +45 -45
- janito/tools/tools_schema.py +104 -104
- janito/version.py +4 -4
- {janito-2.3.0.dist-info → janito-2.3.1.dist-info}/METADATA +390 -388
- {janito-2.3.0.dist-info → janito-2.3.1.dist-info}/RECORD +93 -93
- {janito-2.3.0.dist-info → janito-2.3.1.dist-info}/WHEEL +0 -0
- {janito-2.3.0.dist-info → janito-2.3.1.dist-info}/entry_points.txt +0 -0
- {janito-2.3.0.dist-info → janito-2.3.1.dist-info}/licenses/LICENSE +0 -0
- {janito-2.3.0.dist-info → janito-2.3.1.dist-info}/top_level.txt +0 -0
@@ -1,166 +1,166 @@
|
|
1
|
-
import subprocess
|
2
|
-
import os
|
3
|
-
import sys
|
4
|
-
import tempfile
|
5
|
-
import threading
|
6
|
-
from janito.tools.tool_base import ToolBase
|
7
|
-
from janito.report_events import ReportAction
|
8
|
-
from janito.tools.adapters.local.adapter import register_local_tool
|
9
|
-
from janito.i18n import tr
|
10
|
-
|
11
|
-
|
12
|
-
@register_local_tool
|
13
|
-
class PythonCodeRunTool(ToolBase):
|
14
|
-
"""
|
15
|
-
Tool to execute Python code by passing it to the interpreter via standard input (stdin).
|
16
|
-
|
17
|
-
Parameters:
|
18
|
-
code (str): The Python code to execute as a string.
|
19
|
-
timeout (int): Timeout in seconds for the command. Defaults to 60.
|
20
|
-
|
21
|
-
Returns:
|
22
|
-
str: Output and status message, or file paths/line counts if output is large.
|
23
|
-
"""
|
24
|
-
provides_execution = True
|
25
|
-
tool_name = "python_code_run"
|
26
|
-
|
27
|
-
def run(self, code: str, timeout: int = 60) -> str:
|
28
|
-
if not code.strip():
|
29
|
-
self.report_warning(tr("ℹ️ Empty code provided."), ReportAction.EXECUTE)
|
30
|
-
return tr("Warning: Empty code provided. Operation skipped.")
|
31
|
-
self.report_action(
|
32
|
-
tr("⚡ Running: python (stdin mode) ...\n{code}\n", code=code),
|
33
|
-
ReportAction.EXECUTE,
|
34
|
-
)
|
35
|
-
self.report_stdout("\n")
|
36
|
-
try:
|
37
|
-
with (
|
38
|
-
tempfile.NamedTemporaryFile(
|
39
|
-
mode="w+",
|
40
|
-
prefix="python_stdin_stdout_",
|
41
|
-
delete=False,
|
42
|
-
encoding="utf-8",
|
43
|
-
) as stdout_file,
|
44
|
-
tempfile.NamedTemporaryFile(
|
45
|
-
mode="w+",
|
46
|
-
prefix="python_stdin_stderr_",
|
47
|
-
delete=False,
|
48
|
-
encoding="utf-8",
|
49
|
-
) as stderr_file,
|
50
|
-
):
|
51
|
-
process = subprocess.Popen(
|
52
|
-
[sys.executable],
|
53
|
-
stdin=subprocess.PIPE,
|
54
|
-
stdout=subprocess.PIPE,
|
55
|
-
stderr=subprocess.PIPE,
|
56
|
-
text=True,
|
57
|
-
bufsize=1,
|
58
|
-
universal_newlines=True,
|
59
|
-
encoding="utf-8",
|
60
|
-
env={**os.environ, "PYTHONIOENCODING": "utf-8"},
|
61
|
-
)
|
62
|
-
stdout_lines, stderr_lines = self._stream_process_output(
|
63
|
-
process, stdout_file, stderr_file, code
|
64
|
-
)
|
65
|
-
return_code = self._wait_for_process(process, timeout)
|
66
|
-
if return_code is None:
|
67
|
-
return tr(
|
68
|
-
"Code timed out after {timeout} seconds.", timeout=timeout
|
69
|
-
)
|
70
|
-
stdout_file.flush()
|
71
|
-
stderr_file.flush()
|
72
|
-
self.report_success(
|
73
|
-
tr("✅ Return code {return_code}", return_code=return_code),
|
74
|
-
ReportAction.EXECUTE,
|
75
|
-
)
|
76
|
-
return self._format_result(
|
77
|
-
stdout_file.name, stderr_file.name, return_code
|
78
|
-
)
|
79
|
-
except Exception as e:
|
80
|
-
self.report_error(tr("❌ Error: {error}", error=e), ReportAction.EXECUTE)
|
81
|
-
return tr("Error running code via stdin: {error}", error=e)
|
82
|
-
|
83
|
-
def _stream_process_output(self, process, stdout_file, stderr_file, code):
|
84
|
-
stdout_lines = 0
|
85
|
-
stderr_lines = 0
|
86
|
-
|
87
|
-
def stream_output(stream, file_obj, report_func, count_func):
|
88
|
-
nonlocal stdout_lines, stderr_lines
|
89
|
-
for line in stream:
|
90
|
-
file_obj.write(line)
|
91
|
-
file_obj.flush()
|
92
|
-
report_func(line.rstrip("\r\n"))
|
93
|
-
if count_func == "stdout":
|
94
|
-
stdout_lines += 1
|
95
|
-
else:
|
96
|
-
stderr_lines += 1
|
97
|
-
|
98
|
-
stdout_thread = threading.Thread(
|
99
|
-
target=stream_output,
|
100
|
-
args=(process.stdout, stdout_file, self.report_stdout, "stdout"),
|
101
|
-
)
|
102
|
-
stderr_thread = threading.Thread(
|
103
|
-
target=stream_output,
|
104
|
-
args=(process.stderr, stderr_file, self.report_stderr, "stderr"),
|
105
|
-
)
|
106
|
-
stdout_thread.start()
|
107
|
-
stderr_thread.start()
|
108
|
-
process.stdin.write(code)
|
109
|
-
process.stdin.close()
|
110
|
-
stdout_thread.join()
|
111
|
-
stderr_thread.join()
|
112
|
-
return stdout_lines, stderr_lines
|
113
|
-
|
114
|
-
def _wait_for_process(self, process, timeout):
|
115
|
-
try:
|
116
|
-
return process.wait(timeout=timeout)
|
117
|
-
except subprocess.TimeoutExpired:
|
118
|
-
process.kill()
|
119
|
-
self.report_error(
|
120
|
-
tr("❌ Timed out after {timeout} seconds.", timeout=timeout),
|
121
|
-
ReportAction.EXECUTE,
|
122
|
-
)
|
123
|
-
return None
|
124
|
-
|
125
|
-
def _format_result(self, stdout_file_name, stderr_file_name, return_code):
|
126
|
-
with open(stdout_file_name, "r", encoding="utf-8", errors="replace") as out_f:
|
127
|
-
stdout_content = out_f.read()
|
128
|
-
with open(stderr_file_name, "r", encoding="utf-8", errors="replace") as err_f:
|
129
|
-
stderr_content = err_f.read()
|
130
|
-
max_lines = 100
|
131
|
-
stdout_lines = stdout_content.count("\n")
|
132
|
-
stderr_lines = stderr_content.count("\n")
|
133
|
-
|
134
|
-
def head_tail(text, n=10):
|
135
|
-
lines = text.splitlines()
|
136
|
-
if len(lines) <= 2 * n:
|
137
|
-
return "\n".join(lines)
|
138
|
-
return "\n".join(
|
139
|
-
lines[:n]
|
140
|
-
+ ["... ({} lines omitted) ...".format(len(lines) - 2 * n)]
|
141
|
-
+ lines[-n:]
|
142
|
-
)
|
143
|
-
|
144
|
-
if stdout_lines <= max_lines and stderr_lines <= max_lines:
|
145
|
-
result = f"Return code: {return_code}\n--- python_code_run: STDOUT ---\n{stdout_content}"
|
146
|
-
if stderr_content.strip():
|
147
|
-
result += f"\n--- python_code_run: STDERR ---\n{stderr_content}"
|
148
|
-
return result
|
149
|
-
else:
|
150
|
-
result = f"stdout_file: {stdout_file_name} (lines: {stdout_lines})\n"
|
151
|
-
if stderr_lines > 0 and stderr_content.strip():
|
152
|
-
result += f"stderr_file: {stderr_file_name} (lines: {stderr_lines})\n"
|
153
|
-
result += f"returncode: {return_code}\n"
|
154
|
-
result += (
|
155
|
-
"--- python_code_run: STDOUT (head/tail) ---\n"
|
156
|
-
+ head_tail(stdout_content)
|
157
|
-
+ "\n"
|
158
|
-
)
|
159
|
-
if stderr_content.strip():
|
160
|
-
result += (
|
161
|
-
"--- python_code_run: STDERR (head/tail) ---\n"
|
162
|
-
+ head_tail(stderr_content)
|
163
|
-
+ "\n"
|
164
|
-
)
|
165
|
-
result += "Use the view_file tool to inspect the contents of these files when needed."
|
166
|
-
return result
|
1
|
+
import subprocess
|
2
|
+
import os
|
3
|
+
import sys
|
4
|
+
import tempfile
|
5
|
+
import threading
|
6
|
+
from janito.tools.tool_base import ToolBase
|
7
|
+
from janito.report_events import ReportAction
|
8
|
+
from janito.tools.adapters.local.adapter import register_local_tool
|
9
|
+
from janito.i18n import tr
|
10
|
+
|
11
|
+
|
12
|
+
@register_local_tool
|
13
|
+
class PythonCodeRunTool(ToolBase):
|
14
|
+
"""
|
15
|
+
Tool to execute Python code by passing it to the interpreter via standard input (stdin).
|
16
|
+
|
17
|
+
Parameters:
|
18
|
+
code (str): The Python code to execute as a string.
|
19
|
+
timeout (int): Timeout in seconds for the command. Defaults to 60.
|
20
|
+
|
21
|
+
Returns:
|
22
|
+
str: Output and status message, or file paths/line counts if output is large.
|
23
|
+
"""
|
24
|
+
provides_execution = True
|
25
|
+
tool_name = "python_code_run"
|
26
|
+
|
27
|
+
def run(self, code: str, timeout: int = 60) -> str:
|
28
|
+
if not code.strip():
|
29
|
+
self.report_warning(tr("ℹ️ Empty code provided."), ReportAction.EXECUTE)
|
30
|
+
return tr("Warning: Empty code provided. Operation skipped.")
|
31
|
+
self.report_action(
|
32
|
+
tr("⚡ Running: python (stdin mode) ...\n{code}\n", code=code),
|
33
|
+
ReportAction.EXECUTE,
|
34
|
+
)
|
35
|
+
self.report_stdout("\n")
|
36
|
+
try:
|
37
|
+
with (
|
38
|
+
tempfile.NamedTemporaryFile(
|
39
|
+
mode="w+",
|
40
|
+
prefix="python_stdin_stdout_",
|
41
|
+
delete=False,
|
42
|
+
encoding="utf-8",
|
43
|
+
) as stdout_file,
|
44
|
+
tempfile.NamedTemporaryFile(
|
45
|
+
mode="w+",
|
46
|
+
prefix="python_stdin_stderr_",
|
47
|
+
delete=False,
|
48
|
+
encoding="utf-8",
|
49
|
+
) as stderr_file,
|
50
|
+
):
|
51
|
+
process = subprocess.Popen(
|
52
|
+
[sys.executable],
|
53
|
+
stdin=subprocess.PIPE,
|
54
|
+
stdout=subprocess.PIPE,
|
55
|
+
stderr=subprocess.PIPE,
|
56
|
+
text=True,
|
57
|
+
bufsize=1,
|
58
|
+
universal_newlines=True,
|
59
|
+
encoding="utf-8",
|
60
|
+
env={**os.environ, "PYTHONIOENCODING": "utf-8"},
|
61
|
+
)
|
62
|
+
stdout_lines, stderr_lines = self._stream_process_output(
|
63
|
+
process, stdout_file, stderr_file, code
|
64
|
+
)
|
65
|
+
return_code = self._wait_for_process(process, timeout)
|
66
|
+
if return_code is None:
|
67
|
+
return tr(
|
68
|
+
"Code timed out after {timeout} seconds.", timeout=timeout
|
69
|
+
)
|
70
|
+
stdout_file.flush()
|
71
|
+
stderr_file.flush()
|
72
|
+
self.report_success(
|
73
|
+
tr("✅ Return code {return_code}", return_code=return_code),
|
74
|
+
ReportAction.EXECUTE,
|
75
|
+
)
|
76
|
+
return self._format_result(
|
77
|
+
stdout_file.name, stderr_file.name, return_code
|
78
|
+
)
|
79
|
+
except Exception as e:
|
80
|
+
self.report_error(tr("❌ Error: {error}", error=e), ReportAction.EXECUTE)
|
81
|
+
return tr("Error running code via stdin: {error}", error=e)
|
82
|
+
|
83
|
+
def _stream_process_output(self, process, stdout_file, stderr_file, code):
|
84
|
+
stdout_lines = 0
|
85
|
+
stderr_lines = 0
|
86
|
+
|
87
|
+
def stream_output(stream, file_obj, report_func, count_func):
|
88
|
+
nonlocal stdout_lines, stderr_lines
|
89
|
+
for line in stream:
|
90
|
+
file_obj.write(line)
|
91
|
+
file_obj.flush()
|
92
|
+
report_func(line.rstrip("\r\n"))
|
93
|
+
if count_func == "stdout":
|
94
|
+
stdout_lines += 1
|
95
|
+
else:
|
96
|
+
stderr_lines += 1
|
97
|
+
|
98
|
+
stdout_thread = threading.Thread(
|
99
|
+
target=stream_output,
|
100
|
+
args=(process.stdout, stdout_file, self.report_stdout, "stdout"),
|
101
|
+
)
|
102
|
+
stderr_thread = threading.Thread(
|
103
|
+
target=stream_output,
|
104
|
+
args=(process.stderr, stderr_file, self.report_stderr, "stderr"),
|
105
|
+
)
|
106
|
+
stdout_thread.start()
|
107
|
+
stderr_thread.start()
|
108
|
+
process.stdin.write(code)
|
109
|
+
process.stdin.close()
|
110
|
+
stdout_thread.join()
|
111
|
+
stderr_thread.join()
|
112
|
+
return stdout_lines, stderr_lines
|
113
|
+
|
114
|
+
def _wait_for_process(self, process, timeout):
|
115
|
+
try:
|
116
|
+
return process.wait(timeout=timeout)
|
117
|
+
except subprocess.TimeoutExpired:
|
118
|
+
process.kill()
|
119
|
+
self.report_error(
|
120
|
+
tr("❌ Timed out after {timeout} seconds.", timeout=timeout),
|
121
|
+
ReportAction.EXECUTE,
|
122
|
+
)
|
123
|
+
return None
|
124
|
+
|
125
|
+
def _format_result(self, stdout_file_name, stderr_file_name, return_code):
|
126
|
+
with open(stdout_file_name, "r", encoding="utf-8", errors="replace") as out_f:
|
127
|
+
stdout_content = out_f.read()
|
128
|
+
with open(stderr_file_name, "r", encoding="utf-8", errors="replace") as err_f:
|
129
|
+
stderr_content = err_f.read()
|
130
|
+
max_lines = 100
|
131
|
+
stdout_lines = stdout_content.count("\n")
|
132
|
+
stderr_lines = stderr_content.count("\n")
|
133
|
+
|
134
|
+
def head_tail(text, n=10):
|
135
|
+
lines = text.splitlines()
|
136
|
+
if len(lines) <= 2 * n:
|
137
|
+
return "\n".join(lines)
|
138
|
+
return "\n".join(
|
139
|
+
lines[:n]
|
140
|
+
+ ["... ({} lines omitted) ...".format(len(lines) - 2 * n)]
|
141
|
+
+ lines[-n:]
|
142
|
+
)
|
143
|
+
|
144
|
+
if stdout_lines <= max_lines and stderr_lines <= max_lines:
|
145
|
+
result = f"Return code: {return_code}\n--- python_code_run: STDOUT ---\n{stdout_content}"
|
146
|
+
if stderr_content.strip():
|
147
|
+
result += f"\n--- python_code_run: STDERR ---\n{stderr_content}"
|
148
|
+
return result
|
149
|
+
else:
|
150
|
+
result = f"stdout_file: {stdout_file_name} (lines: {stdout_lines})\n"
|
151
|
+
if stderr_lines > 0 and stderr_content.strip():
|
152
|
+
result += f"stderr_file: {stderr_file_name} (lines: {stderr_lines})\n"
|
153
|
+
result += f"returncode: {return_code}\n"
|
154
|
+
result += (
|
155
|
+
"--- python_code_run: STDOUT (head/tail) ---\n"
|
156
|
+
+ head_tail(stdout_content)
|
157
|
+
+ "\n"
|
158
|
+
)
|
159
|
+
if stderr_content.strip():
|
160
|
+
result += (
|
161
|
+
"--- python_code_run: STDERR (head/tail) ---\n"
|
162
|
+
+ head_tail(stderr_content)
|
163
|
+
+ "\n"
|
164
|
+
)
|
165
|
+
result += "Use the view_file tool to inspect the contents of these files when needed."
|
166
|
+
return result
|
@@ -1,164 +1,164 @@
|
|
1
|
-
import subprocess
|
2
|
-
import os
|
3
|
-
import sys
|
4
|
-
import tempfile
|
5
|
-
import threading
|
6
|
-
from janito.tools.tool_base import ToolBase
|
7
|
-
from janito.report_events import ReportAction
|
8
|
-
from janito.tools.adapters.local.adapter import register_local_tool
|
9
|
-
from janito.i18n import tr
|
10
|
-
|
11
|
-
|
12
|
-
@register_local_tool
|
13
|
-
class PythonCommandRunTool(ToolBase):
|
14
|
-
"""
|
15
|
-
Tool to execute Python code using the `python -c` command-line flag.
|
16
|
-
|
17
|
-
Parameters:
|
18
|
-
code (str): The Python code to execute as a string.
|
19
|
-
timeout (int): Timeout in seconds for the command. Defaults to 60.
|
20
|
-
|
21
|
-
Returns:
|
22
|
-
str: Output and status message, or file paths/line counts if output is large.
|
23
|
-
"""
|
24
|
-
provides_execution = True
|
25
|
-
tool_name = "python_command_run"
|
26
|
-
|
27
|
-
def run(self, code: str, timeout: int = 60) -> str:
|
28
|
-
if not code.strip():
|
29
|
-
self.report_warning(tr("ℹ️ Empty code provided."), ReportAction.EXECUTE)
|
30
|
-
return tr("Warning: Empty code provided. Operation skipped.")
|
31
|
-
self.report_action(
|
32
|
-
tr("🐍 Running: python -c ...\n{code}\n", code=code), ReportAction.EXECUTE
|
33
|
-
)
|
34
|
-
self.report_stdout("\n")
|
35
|
-
try:
|
36
|
-
with (
|
37
|
-
tempfile.NamedTemporaryFile(
|
38
|
-
mode="w+",
|
39
|
-
prefix="python_cmd_stdout_",
|
40
|
-
delete=False,
|
41
|
-
encoding="utf-8",
|
42
|
-
) as stdout_file,
|
43
|
-
tempfile.NamedTemporaryFile(
|
44
|
-
mode="w+",
|
45
|
-
prefix="python_cmd_stderr_",
|
46
|
-
delete=False,
|
47
|
-
encoding="utf-8",
|
48
|
-
) as stderr_file,
|
49
|
-
):
|
50
|
-
process = subprocess.Popen(
|
51
|
-
[sys.executable, "-c", code],
|
52
|
-
stdout=subprocess.PIPE,
|
53
|
-
stderr=subprocess.PIPE,
|
54
|
-
text=True,
|
55
|
-
bufsize=1,
|
56
|
-
universal_newlines=True,
|
57
|
-
encoding="utf-8",
|
58
|
-
env={**os.environ, "PYTHONIOENCODING": "utf-8"},
|
59
|
-
)
|
60
|
-
stdout_lines, stderr_lines = self._stream_process_output(
|
61
|
-
process, stdout_file, stderr_file
|
62
|
-
)
|
63
|
-
return_code = self._wait_for_process(process, timeout)
|
64
|
-
if return_code is None:
|
65
|
-
return tr(
|
66
|
-
"Code timed out after {timeout} seconds.", timeout=timeout
|
67
|
-
)
|
68
|
-
stdout_file.flush()
|
69
|
-
stderr_file.flush()
|
70
|
-
self.report_success(
|
71
|
-
tr("✅ Return code {return_code}", return_code=return_code),
|
72
|
-
ReportAction.EXECUTE,
|
73
|
-
)
|
74
|
-
return self._format_result(
|
75
|
-
stdout_file.name, stderr_file.name, return_code
|
76
|
-
)
|
77
|
-
except Exception as e:
|
78
|
-
self.report_error(tr("❌ Error: {error}", error=e), ReportAction.EXECUTE)
|
79
|
-
return tr("Error running code: {error}", error=e)
|
80
|
-
|
81
|
-
def _stream_process_output(self, process, stdout_file, stderr_file):
|
82
|
-
stdout_lines = 0
|
83
|
-
stderr_lines = 0
|
84
|
-
|
85
|
-
def stream_output(stream, file_obj, report_func, count_func):
|
86
|
-
nonlocal stdout_lines, stderr_lines
|
87
|
-
for line in stream:
|
88
|
-
file_obj.write(line)
|
89
|
-
file_obj.flush()
|
90
|
-
from janito.tools.tool_base import ReportAction
|
91
|
-
|
92
|
-
report_func(line.rstrip("\r\n"), ReportAction.EXECUTE)
|
93
|
-
if count_func == "stdout":
|
94
|
-
stdout_lines += 1
|
95
|
-
else:
|
96
|
-
stderr_lines += 1
|
97
|
-
|
98
|
-
stdout_thread = threading.Thread(
|
99
|
-
target=stream_output,
|
100
|
-
args=(process.stdout, stdout_file, self.report_stdout, "stdout"),
|
101
|
-
)
|
102
|
-
stderr_thread = threading.Thread(
|
103
|
-
target=stream_output,
|
104
|
-
args=(process.stderr, stderr_file, self.report_stderr, "stderr"),
|
105
|
-
)
|
106
|
-
stdout_thread.start()
|
107
|
-
stderr_thread.start()
|
108
|
-
stdout_thread.join()
|
109
|
-
stderr_thread.join()
|
110
|
-
return stdout_lines, stderr_lines
|
111
|
-
|
112
|
-
def _wait_for_process(self, process, timeout):
|
113
|
-
try:
|
114
|
-
return process.wait(timeout=timeout)
|
115
|
-
except subprocess.TimeoutExpired:
|
116
|
-
process.kill()
|
117
|
-
self.report_error(
|
118
|
-
tr("❌ Timed out after {timeout} seconds.", timeout=timeout),
|
119
|
-
ReportAction.EXECUTE,
|
120
|
-
)
|
121
|
-
return None
|
122
|
-
|
123
|
-
def _format_result(self, stdout_file_name, stderr_file_name, return_code):
|
124
|
-
with open(stdout_file_name, "r", encoding="utf-8", errors="replace") as out_f:
|
125
|
-
stdout_content = out_f.read()
|
126
|
-
with open(stderr_file_name, "r", encoding="utf-8", errors="replace") as err_f:
|
127
|
-
stderr_content = err_f.read()
|
128
|
-
max_lines = 100
|
129
|
-
stdout_lines = stdout_content.count("\n")
|
130
|
-
stderr_lines = stderr_content.count("\n")
|
131
|
-
|
132
|
-
def head_tail(text, n=10):
|
133
|
-
lines = text.splitlines()
|
134
|
-
if len(lines) <= 2 * n:
|
135
|
-
return "\n".join(lines)
|
136
|
-
return "\n".join(
|
137
|
-
lines[:n]
|
138
|
-
+ ["... ({} lines omitted) ...".format(len(lines) - 2 * n)]
|
139
|
-
+ lines[-n:]
|
140
|
-
)
|
141
|
-
|
142
|
-
if stdout_lines <= max_lines and stderr_lines <= max_lines:
|
143
|
-
result = f"Return code: {return_code}\n--- python_command_run: STDOUT ---\n{stdout_content}"
|
144
|
-
if stderr_content.strip():
|
145
|
-
result += f"\n--- python_command_run: STDERR ---\n{stderr_content}"
|
146
|
-
return result
|
147
|
-
else:
|
148
|
-
result = f"stdout_file: {stdout_file_name} (lines: {stdout_lines})\n"
|
149
|
-
if stderr_lines > 0 and stderr_content.strip():
|
150
|
-
result += f"stderr_file: {stderr_file_name} (lines: {stderr_lines})\n"
|
151
|
-
result += f"returncode: {return_code}\n"
|
152
|
-
result += (
|
153
|
-
"--- python_command_run: STDOUT (head/tail) ---\n"
|
154
|
-
+ head_tail(stdout_content)
|
155
|
-
+ "\n"
|
156
|
-
)
|
157
|
-
if stderr_content.strip():
|
158
|
-
result += (
|
159
|
-
"--- python_command_run: STDERR (head/tail) ---\n"
|
160
|
-
+ head_tail(stderr_content)
|
161
|
-
+ "\n"
|
162
|
-
)
|
163
|
-
result += "Use the view_file tool to inspect the contents of these files when needed."
|
164
|
-
return result
|
1
|
+
import subprocess
|
2
|
+
import os
|
3
|
+
import sys
|
4
|
+
import tempfile
|
5
|
+
import threading
|
6
|
+
from janito.tools.tool_base import ToolBase
|
7
|
+
from janito.report_events import ReportAction
|
8
|
+
from janito.tools.adapters.local.adapter import register_local_tool
|
9
|
+
from janito.i18n import tr
|
10
|
+
|
11
|
+
|
12
|
+
@register_local_tool
|
13
|
+
class PythonCommandRunTool(ToolBase):
|
14
|
+
"""
|
15
|
+
Tool to execute Python code using the `python -c` command-line flag.
|
16
|
+
|
17
|
+
Parameters:
|
18
|
+
code (str): The Python code to execute as a string.
|
19
|
+
timeout (int): Timeout in seconds for the command. Defaults to 60.
|
20
|
+
|
21
|
+
Returns:
|
22
|
+
str: Output and status message, or file paths/line counts if output is large.
|
23
|
+
"""
|
24
|
+
provides_execution = True
|
25
|
+
tool_name = "python_command_run"
|
26
|
+
|
27
|
+
def run(self, code: str, timeout: int = 60) -> str:
|
28
|
+
if not code.strip():
|
29
|
+
self.report_warning(tr("ℹ️ Empty code provided."), ReportAction.EXECUTE)
|
30
|
+
return tr("Warning: Empty code provided. Operation skipped.")
|
31
|
+
self.report_action(
|
32
|
+
tr("🐍 Running: python -c ...\n{code}\n", code=code), ReportAction.EXECUTE
|
33
|
+
)
|
34
|
+
self.report_stdout("\n")
|
35
|
+
try:
|
36
|
+
with (
|
37
|
+
tempfile.NamedTemporaryFile(
|
38
|
+
mode="w+",
|
39
|
+
prefix="python_cmd_stdout_",
|
40
|
+
delete=False,
|
41
|
+
encoding="utf-8",
|
42
|
+
) as stdout_file,
|
43
|
+
tempfile.NamedTemporaryFile(
|
44
|
+
mode="w+",
|
45
|
+
prefix="python_cmd_stderr_",
|
46
|
+
delete=False,
|
47
|
+
encoding="utf-8",
|
48
|
+
) as stderr_file,
|
49
|
+
):
|
50
|
+
process = subprocess.Popen(
|
51
|
+
[sys.executable, "-c", code],
|
52
|
+
stdout=subprocess.PIPE,
|
53
|
+
stderr=subprocess.PIPE,
|
54
|
+
text=True,
|
55
|
+
bufsize=1,
|
56
|
+
universal_newlines=True,
|
57
|
+
encoding="utf-8",
|
58
|
+
env={**os.environ, "PYTHONIOENCODING": "utf-8"},
|
59
|
+
)
|
60
|
+
stdout_lines, stderr_lines = self._stream_process_output(
|
61
|
+
process, stdout_file, stderr_file
|
62
|
+
)
|
63
|
+
return_code = self._wait_for_process(process, timeout)
|
64
|
+
if return_code is None:
|
65
|
+
return tr(
|
66
|
+
"Code timed out after {timeout} seconds.", timeout=timeout
|
67
|
+
)
|
68
|
+
stdout_file.flush()
|
69
|
+
stderr_file.flush()
|
70
|
+
self.report_success(
|
71
|
+
tr("✅ Return code {return_code}", return_code=return_code),
|
72
|
+
ReportAction.EXECUTE,
|
73
|
+
)
|
74
|
+
return self._format_result(
|
75
|
+
stdout_file.name, stderr_file.name, return_code
|
76
|
+
)
|
77
|
+
except Exception as e:
|
78
|
+
self.report_error(tr("❌ Error: {error}", error=e), ReportAction.EXECUTE)
|
79
|
+
return tr("Error running code: {error}", error=e)
|
80
|
+
|
81
|
+
def _stream_process_output(self, process, stdout_file, stderr_file):
|
82
|
+
stdout_lines = 0
|
83
|
+
stderr_lines = 0
|
84
|
+
|
85
|
+
def stream_output(stream, file_obj, report_func, count_func):
|
86
|
+
nonlocal stdout_lines, stderr_lines
|
87
|
+
for line in stream:
|
88
|
+
file_obj.write(line)
|
89
|
+
file_obj.flush()
|
90
|
+
from janito.tools.tool_base import ReportAction
|
91
|
+
|
92
|
+
report_func(line.rstrip("\r\n"), ReportAction.EXECUTE)
|
93
|
+
if count_func == "stdout":
|
94
|
+
stdout_lines += 1
|
95
|
+
else:
|
96
|
+
stderr_lines += 1
|
97
|
+
|
98
|
+
stdout_thread = threading.Thread(
|
99
|
+
target=stream_output,
|
100
|
+
args=(process.stdout, stdout_file, self.report_stdout, "stdout"),
|
101
|
+
)
|
102
|
+
stderr_thread = threading.Thread(
|
103
|
+
target=stream_output,
|
104
|
+
args=(process.stderr, stderr_file, self.report_stderr, "stderr"),
|
105
|
+
)
|
106
|
+
stdout_thread.start()
|
107
|
+
stderr_thread.start()
|
108
|
+
stdout_thread.join()
|
109
|
+
stderr_thread.join()
|
110
|
+
return stdout_lines, stderr_lines
|
111
|
+
|
112
|
+
def _wait_for_process(self, process, timeout):
|
113
|
+
try:
|
114
|
+
return process.wait(timeout=timeout)
|
115
|
+
except subprocess.TimeoutExpired:
|
116
|
+
process.kill()
|
117
|
+
self.report_error(
|
118
|
+
tr("❌ Timed out after {timeout} seconds.", timeout=timeout),
|
119
|
+
ReportAction.EXECUTE,
|
120
|
+
)
|
121
|
+
return None
|
122
|
+
|
123
|
+
def _format_result(self, stdout_file_name, stderr_file_name, return_code):
|
124
|
+
with open(stdout_file_name, "r", encoding="utf-8", errors="replace") as out_f:
|
125
|
+
stdout_content = out_f.read()
|
126
|
+
with open(stderr_file_name, "r", encoding="utf-8", errors="replace") as err_f:
|
127
|
+
stderr_content = err_f.read()
|
128
|
+
max_lines = 100
|
129
|
+
stdout_lines = stdout_content.count("\n")
|
130
|
+
stderr_lines = stderr_content.count("\n")
|
131
|
+
|
132
|
+
def head_tail(text, n=10):
|
133
|
+
lines = text.splitlines()
|
134
|
+
if len(lines) <= 2 * n:
|
135
|
+
return "\n".join(lines)
|
136
|
+
return "\n".join(
|
137
|
+
lines[:n]
|
138
|
+
+ ["... ({} lines omitted) ...".format(len(lines) - 2 * n)]
|
139
|
+
+ lines[-n:]
|
140
|
+
)
|
141
|
+
|
142
|
+
if stdout_lines <= max_lines and stderr_lines <= max_lines:
|
143
|
+
result = f"Return code: {return_code}\n--- python_command_run: STDOUT ---\n{stdout_content}"
|
144
|
+
if stderr_content.strip():
|
145
|
+
result += f"\n--- python_command_run: STDERR ---\n{stderr_content}"
|
146
|
+
return result
|
147
|
+
else:
|
148
|
+
result = f"stdout_file: {stdout_file_name} (lines: {stdout_lines})\n"
|
149
|
+
if stderr_lines > 0 and stderr_content.strip():
|
150
|
+
result += f"stderr_file: {stderr_file_name} (lines: {stderr_lines})\n"
|
151
|
+
result += f"returncode: {return_code}\n"
|
152
|
+
result += (
|
153
|
+
"--- python_command_run: STDOUT (head/tail) ---\n"
|
154
|
+
+ head_tail(stdout_content)
|
155
|
+
+ "\n"
|
156
|
+
)
|
157
|
+
if stderr_content.strip():
|
158
|
+
result += (
|
159
|
+
"--- python_command_run: STDERR (head/tail) ---\n"
|
160
|
+
+ head_tail(stderr_content)
|
161
|
+
+ "\n"
|
162
|
+
)
|
163
|
+
result += "Use the view_file tool to inspect the contents of these files when needed."
|
164
|
+
return result
|