janito 3.14.2__py3-none-any.whl → 3.15.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.
Files changed (37) hide show
  1. janito/platform_discovery.py +1 -8
  2. janito/plugins/tools/local/adapter.py +3 -2
  3. janito/plugins/tools/local/ask_user.py +111 -112
  4. janito/plugins/tools/local/copy_file.py +86 -87
  5. janito/plugins/tools/local/create_directory.py +111 -112
  6. janito/plugins/tools/local/create_file.py +0 -1
  7. janito/plugins/tools/local/delete_text_in_file.py +133 -134
  8. janito/plugins/tools/local/fetch_url.py +465 -466
  9. janito/plugins/tools/local/find_files.py +142 -143
  10. janito/plugins/tools/local/markdown_view.py +0 -1
  11. janito/plugins/tools/local/move_file.py +130 -131
  12. janito/plugins/tools/local/open_html_in_browser.py +50 -51
  13. janito/plugins/tools/local/open_url.py +36 -37
  14. janito/plugins/tools/local/python_code_run.py +171 -172
  15. janito/plugins/tools/local/python_command_run.py +170 -171
  16. janito/plugins/tools/local/python_file_run.py +171 -172
  17. janito/plugins/tools/local/read_chart.py +258 -259
  18. janito/plugins/tools/local/read_files.py +57 -58
  19. janito/plugins/tools/local/remove_directory.py +54 -55
  20. janito/plugins/tools/local/remove_file.py +57 -58
  21. janito/plugins/tools/local/replace_text_in_file.py +275 -276
  22. janito/plugins/tools/local/run_bash_command.py +182 -183
  23. janito/plugins/tools/local/run_powershell_command.py +217 -218
  24. janito/plugins/tools/local/show_image.py +0 -1
  25. janito/plugins/tools/local/show_image_grid.py +0 -1
  26. janito/plugins/tools/local/view_file.py +0 -1
  27. janito/providers/alibaba/model_info.py +2 -2
  28. janito/providers/alibaba/provider.py +1 -1
  29. janito/tools/base.py +19 -12
  30. janito/tools/tool_base.py +122 -121
  31. janito/tools/tools_schema.py +104 -104
  32. {janito-3.14.2.dist-info → janito-3.15.1.dist-info}/METADATA +9 -29
  33. {janito-3.14.2.dist-info → janito-3.15.1.dist-info}/RECORD +37 -37
  34. {janito-3.14.2.dist-info → janito-3.15.1.dist-info}/WHEEL +0 -0
  35. {janito-3.14.2.dist-info → janito-3.15.1.dist-info}/entry_points.txt +0 -0
  36. {janito-3.14.2.dist-info → janito-3.15.1.dist-info}/licenses/LICENSE +0 -0
  37. {janito-3.14.2.dist-info → janito-3.15.1.dist-info}/top_level.txt +0 -0
@@ -1,171 +1,170 @@
1
- import subprocess
2
- import os
3
- import sys
4
- import tempfile
5
- import threading
6
- from janito.tools.tool_base import ToolBase, ToolPermissions
7
- from janito.report_events import ReportAction
8
- from janito.plugins.tools.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
- Args:
18
- code (str): The Python code to execute as a string.
19
- timeout (int): Timeout in seconds for the command. Defaults to 60.
20
- silent (bool): If True, suppresses progress and status messages. Defaults to False.
21
-
22
- Returns:
23
- str: Output and status message, or file paths/line counts if output is large.
24
- """
25
-
26
- permissions = ToolPermissions(execute=True)
27
- tool_name = "python_command_run"
28
-
29
- def run(self, code: str, timeout: int = 60, silent: bool = False) -> str:
30
- if not code.strip():
31
- self.report_warning(tr("ℹ️ Empty code provided."), ReportAction.EXECUTE)
32
- return tr("Warning: Empty code provided. Operation skipped.")
33
- if not silent:
34
- self.report_action(
35
- tr("🐍 Running: python -c ...\n{code}\n", code=code),
36
- ReportAction.EXECUTE,
37
- )
38
- self.report_stdout("\n")
39
- else:
40
- self.report_action(tr("⚡ Executing..."), ReportAction.EXECUTE)
41
- try:
42
- with (
43
- tempfile.NamedTemporaryFile(
44
- mode="w+",
45
- prefix="python_cmd_stdout_",
46
- delete=False,
47
- encoding="utf-8",
48
- ) as stdout_file,
49
- tempfile.NamedTemporaryFile(
50
- mode="w+",
51
- prefix="python_cmd_stderr_",
52
- delete=False,
53
- encoding="utf-8",
54
- ) as stderr_file,
55
- ):
56
- process = subprocess.Popen(
57
- [sys.executable, "-c", code],
58
- stdout=subprocess.PIPE,
59
- stderr=subprocess.PIPE,
60
- text=True,
61
- bufsize=1,
62
- universal_newlines=True,
63
- encoding="utf-8",
64
- env={**os.environ, "PYTHONIOENCODING": "utf-8"},
65
- )
66
- stdout_lines, stderr_lines = self._stream_process_output(
67
- process, stdout_file, stderr_file
68
- )
69
- return_code = self._wait_for_process(process, timeout)
70
- if return_code is None:
71
- return tr(
72
- "Code timed out after {timeout} seconds.", timeout=timeout
73
- )
74
- stdout_file.flush()
75
- stderr_file.flush()
76
- if not silent:
77
- self.report_success(
78
- tr("✅ Return code {return_code}", return_code=return_code),
79
- ReportAction.EXECUTE,
80
- )
81
- return self._format_result(
82
- stdout_file.name, stderr_file.name, return_code
83
- )
84
- except Exception as e:
85
- self.report_error(tr(" Error: {error}", error=e), ReportAction.EXECUTE)
86
- return tr("Error running code: {error}", error=e)
87
-
88
- def _stream_process_output(self, process, stdout_file, stderr_file):
89
- stdout_lines = 0
90
- stderr_lines = 0
91
-
92
- def stream_output(stream, file_obj, report_func, count_func):
93
- nonlocal stdout_lines, stderr_lines
94
- for line in stream:
95
- file_obj.write(line)
96
- file_obj.flush()
97
- from janito.tools.tool_base import ReportAction
98
-
99
- report_func(line.rstrip("\r\n"), ReportAction.EXECUTE)
100
- if count_func == "stdout":
101
- stdout_lines += 1
102
- else:
103
- stderr_lines += 1
104
-
105
- stdout_thread = threading.Thread(
106
- target=stream_output,
107
- args=(process.stdout, stdout_file, self.report_stdout, "stdout"),
108
- )
109
- stderr_thread = threading.Thread(
110
- target=stream_output,
111
- args=(process.stderr, stderr_file, self.report_stderr, "stderr"),
112
- )
113
- stdout_thread.start()
114
- stderr_thread.start()
115
- stdout_thread.join()
116
- stderr_thread.join()
117
- return stdout_lines, stderr_lines
118
-
119
- def _wait_for_process(self, process, timeout):
120
- try:
121
- return process.wait(timeout=timeout)
122
- except subprocess.TimeoutExpired:
123
- process.kill()
124
- self.report_error(
125
- tr("❌ Timed out after {timeout} seconds.", timeout=timeout),
126
- ReportAction.EXECUTE,
127
- )
128
- return None
129
-
130
- def _format_result(self, stdout_file_name, stderr_file_name, return_code):
131
- with open(stdout_file_name, "r", encoding="utf-8", errors="replace") as out_f:
132
- stdout_content = out_f.read()
133
- with open(stderr_file_name, "r", encoding="utf-8", errors="replace") as err_f:
134
- stderr_content = err_f.read()
135
- max_lines = 100
136
- stdout_lines = stdout_content.count("\n")
137
- stderr_lines = stderr_content.count("\n")
138
-
139
- def head_tail(text, n=10):
140
- lines = text.splitlines()
141
- if len(lines) <= 2 * n:
142
- return "\n".join(lines)
143
- return "\n".join(
144
- lines[:n]
145
- + ["... ({} lines omitted) ...".format(len(lines) - 2 * n)]
146
- + lines[-n:]
147
- )
148
-
149
- if stdout_lines <= max_lines and stderr_lines <= max_lines:
150
- result = f"Return code: {return_code}\n--- python_command_run: STDOUT ---\n{stdout_content}"
151
- if stderr_content.strip():
152
- result += f"\n--- python_command_run: STDERR ---\n{stderr_content}"
153
- return result
154
- else:
155
- result = f"stdout_file: {stdout_file_name} (lines: {stdout_lines})\n"
156
- if stderr_lines > 0 and stderr_content.strip():
157
- result += f"stderr_file: {stderr_file_name} (lines: {stderr_lines})\n"
158
- result += f"returncode: {return_code}\n"
159
- result += (
160
- "--- python_command_run: STDOUT (head/tail) ---\n"
161
- + head_tail(stdout_content)
162
- + "\n"
163
- )
164
- if stderr_content.strip():
165
- result += (
166
- "--- python_command_run: STDERR (head/tail) ---\n"
167
- + head_tail(stderr_content)
168
- + "\n"
169
- )
170
- result += "Use the view_file tool to inspect the contents of these files when needed."
171
- 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, ToolPermissions
7
+ from janito.report_events import ReportAction
8
+ from janito.plugins.tools.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
+ Args:
18
+ code (str): The Python code to execute as a string.
19
+ timeout (int): Timeout in seconds for the command. Defaults to 60.
20
+ silent (bool): If True, suppresses progress and status messages. Defaults to False.
21
+
22
+ Returns:
23
+ str: Output and status message, or file paths/line counts if output is large.
24
+ """
25
+
26
+ permissions = ToolPermissions(execute=True)
27
+
28
+ def run(self, code: str, timeout: int = 60, silent: bool = False) -> str:
29
+ if not code.strip():
30
+ self.report_warning(tr("ℹ️ Empty code provided."), ReportAction.EXECUTE)
31
+ return tr("Warning: Empty code provided. Operation skipped.")
32
+ if not silent:
33
+ self.report_action(
34
+ tr("🐍 Running: python -c ...\n{code}\n", code=code),
35
+ ReportAction.EXECUTE,
36
+ )
37
+ self.report_stdout("\n")
38
+ else:
39
+ self.report_action(tr("⚡ Executing..."), ReportAction.EXECUTE)
40
+ try:
41
+ with (
42
+ tempfile.NamedTemporaryFile(
43
+ mode="w+",
44
+ prefix="python_cmd_stdout_",
45
+ delete=False,
46
+ encoding="utf-8",
47
+ ) as stdout_file,
48
+ tempfile.NamedTemporaryFile(
49
+ mode="w+",
50
+ prefix="python_cmd_stderr_",
51
+ delete=False,
52
+ encoding="utf-8",
53
+ ) as stderr_file,
54
+ ):
55
+ process = subprocess.Popen(
56
+ [sys.executable, "-c", code],
57
+ stdout=subprocess.PIPE,
58
+ stderr=subprocess.PIPE,
59
+ text=True,
60
+ bufsize=1,
61
+ universal_newlines=True,
62
+ encoding="utf-8",
63
+ env={**os.environ, "PYTHONIOENCODING": "utf-8"},
64
+ )
65
+ stdout_lines, stderr_lines = self._stream_process_output(
66
+ process, stdout_file, stderr_file
67
+ )
68
+ return_code = self._wait_for_process(process, timeout)
69
+ if return_code is None:
70
+ return tr(
71
+ "Code timed out after {timeout} seconds.", timeout=timeout
72
+ )
73
+ stdout_file.flush()
74
+ stderr_file.flush()
75
+ if not silent:
76
+ self.report_success(
77
+ tr("✅ Return code {return_code}", return_code=return_code),
78
+ ReportAction.EXECUTE,
79
+ )
80
+ return self._format_result(
81
+ stdout_file.name, stderr_file.name, return_code
82
+ )
83
+ except Exception as e:
84
+ self.report_error(tr("❌ Error: {error}", error=e), ReportAction.EXECUTE)
85
+ return tr("Error running code: {error}", error=e)
86
+
87
+ def _stream_process_output(self, process, stdout_file, stderr_file):
88
+ stdout_lines = 0
89
+ stderr_lines = 0
90
+
91
+ def stream_output(stream, file_obj, report_func, count_func):
92
+ nonlocal stdout_lines, stderr_lines
93
+ for line in stream:
94
+ file_obj.write(line)
95
+ file_obj.flush()
96
+ from janito.tools.tool_base import ReportAction
97
+
98
+ report_func(line.rstrip("\r\n"), ReportAction.EXECUTE)
99
+ if count_func == "stdout":
100
+ stdout_lines += 1
101
+ else:
102
+ stderr_lines += 1
103
+
104
+ stdout_thread = threading.Thread(
105
+ target=stream_output,
106
+ args=(process.stdout, stdout_file, self.report_stdout, "stdout"),
107
+ )
108
+ stderr_thread = threading.Thread(
109
+ target=stream_output,
110
+ args=(process.stderr, stderr_file, self.report_stderr, "stderr"),
111
+ )
112
+ stdout_thread.start()
113
+ stderr_thread.start()
114
+ stdout_thread.join()
115
+ stderr_thread.join()
116
+ return stdout_lines, stderr_lines
117
+
118
+ def _wait_for_process(self, process, timeout):
119
+ try:
120
+ return process.wait(timeout=timeout)
121
+ except subprocess.TimeoutExpired:
122
+ process.kill()
123
+ self.report_error(
124
+ tr("❌ Timed out after {timeout} seconds.", timeout=timeout),
125
+ ReportAction.EXECUTE,
126
+ )
127
+ return None
128
+
129
+ def _format_result(self, stdout_file_name, stderr_file_name, return_code):
130
+ with open(stdout_file_name, "r", encoding="utf-8", errors="replace") as out_f:
131
+ stdout_content = out_f.read()
132
+ with open(stderr_file_name, "r", encoding="utf-8", errors="replace") as err_f:
133
+ stderr_content = err_f.read()
134
+ max_lines = 100
135
+ stdout_lines = stdout_content.count("\n")
136
+ stderr_lines = stderr_content.count("\n")
137
+
138
+ def head_tail(text, n=10):
139
+ lines = text.splitlines()
140
+ if len(lines) <= 2 * n:
141
+ return "\n".join(lines)
142
+ return "\n".join(
143
+ lines[:n]
144
+ + ["... ({} lines omitted) ...".format(len(lines) - 2 * n)]
145
+ + lines[-n:]
146
+ )
147
+
148
+ if stdout_lines <= max_lines and stderr_lines <= max_lines:
149
+ result = f"Return code: {return_code}\n--- python_command_run: STDOUT ---\n{stdout_content}"
150
+ if stderr_content.strip():
151
+ result += f"\n--- python_command_run: STDERR ---\n{stderr_content}"
152
+ return result
153
+ else:
154
+ result = f"stdout_file: {stdout_file_name} (lines: {stdout_lines})\n"
155
+ if stderr_lines > 0 and stderr_content.strip():
156
+ result += f"stderr_file: {stderr_file_name} (lines: {stderr_lines})\n"
157
+ result += f"returncode: {return_code}\n"
158
+ result += (
159
+ "--- python_command_run: STDOUT (head/tail) ---\n"
160
+ + head_tail(stdout_content)
161
+ + "\n"
162
+ )
163
+ if stderr_content.strip():
164
+ result += (
165
+ "--- python_command_run: STDERR (head/tail) ---\n"
166
+ + head_tail(stderr_content)
167
+ + "\n"
168
+ )
169
+ result += "Use the view_file tool to inspect the contents of these files when needed."
170
+ return result